liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame^] | 1 | /** |
| 2 | * \file mbtk_adc.c |
| 3 | * \brief A Documented file. |
| 4 | * |
| 5 | * Detailed description |
| 6 | * \Author: js.wang <js.wang@mobiletek.cn> |
| 7 | * \Version: 1.0.0 |
| 8 | * \Date: 2022-04-22 |
| 9 | */ |
| 10 | #include <fcntl.h> |
| 11 | #include <stdint.h> |
| 12 | #include <limits.h> |
| 13 | #include <termios.h> |
| 14 | #include <stdarg.h> |
| 15 | #include <dirent.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/statfs.h> |
| 18 | #include <sys/types.h> |
| 19 | #include "mbtk_log.h" |
| 20 | #include "mbtk_type.h" |
| 21 | #include "mbtk_adc.h" |
| 22 | |
| 23 | #define ADC_DEVICE "/sys/devices/soc.0/d4000000.apb/pxa2xx-i2c.2/i2c-2/2-0030/pm802-bat/adc" |
| 24 | |
| 25 | int mbtk_adc_get(mbtk_adc_enum channle) |
| 26 | { |
| 27 | int ret = 0; |
| 28 | int fd = 0; |
| 29 | char adc_buf[24] = {0}; |
| 30 | char *adc_value = NULL; |
| 31 | char adc = (channle == MBTK_ADC0 ? '0' : '1'); |
| 32 | |
| 33 | fd = open(ADC_DEVICE, O_RDWR|O_CREAT|O_TRUNC, 0644); |
| 34 | if(fd < 0) { |
| 35 | LOGE("[%s] file open error\n", __FUNCTION__); |
| 36 | return -2; |
| 37 | } |
| 38 | ret = write(fd, &adc, 1); |
| 39 | if (ret < 0) { |
| 40 | LOGE("%s: error writing to file!\n", __FUNCTION__); |
| 41 | close(fd); |
| 42 | return -2; |
| 43 | } |
| 44 | ret = read(fd, adc_buf, 24); |
| 45 | if (ret < 0) { |
| 46 | LOGE("%s: error writing to file!\n", __FUNCTION__); |
| 47 | close(fd); |
| 48 | return -2; |
| 49 | }else{ |
| 50 | LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf); |
| 51 | adc_value = strstr(adc_buf, "channel"); |
| 52 | } |
| 53 | close(fd); |
| 54 | if(adc_value) |
| 55 | LOGI("%s adc: %s\n", __FUNCTION__, adc_value); |
| 56 | else |
| 57 | return -2; |
| 58 | |
| 59 | return atoi(&adc_value[9]); |
| 60 | } |