blob: c0ef5fce9b933dfeee7ed5507b90fa76051f0c2a [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/**
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
liubinb8a0dd72023-08-04 15:26:48 +080023#define ADC_DEVICE_802 "/sys/devices/soc.0/d4000000.apb/pxa2xx-i2c.2/i2c-2/2-0030/pm802-bat/adc"
liubin6648d642023-09-02 13:43:19 +080024#define ADC_DEVICE_803 "/sys/kernel/debug/adc"
liubinb8a0dd72023-08-04 15:26:48 +080025
liubin281ac462023-07-19 14:22:54 +080026
27int mbtk_adc_get(mbtk_adc_enum channle)
28{
29 int ret = 0;
30 int fd = 0;
31 char adc_buf[24] = {0};
32 char *adc_value = NULL;
33 char adc = (channle == MBTK_ADC0 ? '0' : '1');
34
liubinb8a0dd72023-08-04 15:26:48 +080035 if(!access(ADC_DEVICE_802, R_OK)) {
36 LOGI("DEV:%s", ADC_DEVICE_802);
37 fd = open(ADC_DEVICE_802, O_RDWR|O_CREAT|O_TRUNC, 0644);
38 } else {
39 if(!access(ADC_DEVICE_803, R_OK)) {
40 LOGI("DEV:%s", ADC_DEVICE_803);
41 fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
42 } else {
43 LOGE("No found ADC devices.");
44 return -1;
45 }
46 }
47
liubin281ac462023-07-19 14:22:54 +080048 if(fd < 0) {
49 LOGE("[%s] file open error\n", __FUNCTION__);
50 return -2;
51 }
52 ret = write(fd, &adc, 1);
53 if (ret < 0) {
54 LOGE("%s: error writing to file!\n", __FUNCTION__);
55 close(fd);
56 return -2;
57 }
58 ret = read(fd, adc_buf, 24);
59 if (ret < 0) {
60 LOGE("%s: error writing to file!\n", __FUNCTION__);
61 close(fd);
62 return -2;
63 }else{
64 LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf);
65 adc_value = strstr(adc_buf, "channel");
66 }
67 close(fd);
68 if(adc_value)
69 LOGI("%s adc: %s\n", __FUNCTION__, adc_value);
70 else
71 return -2;
72
73 return atoi(&adc_value[9]);
74}