blob: ddacf39fe14c8a3667a1f377487a92f6d5e4fd44 [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
wangyouqiang2b352b22023-10-21 15:18:43 +080026void mbtk_adc_close(void)
27{
28 system("echo 3 > /sys/kernel/debug/adc");
29}
liubin281ac462023-07-19 14:22:54 +080030
31int mbtk_adc_get(mbtk_adc_enum channle)
32{
33 int ret = 0;
34 int fd = 0;
35 char adc_buf[24] = {0};
36 char *adc_value = NULL;
37 char adc = (channle == MBTK_ADC0 ? '0' : '1');
38
liubinb8a0dd72023-08-04 15:26:48 +080039 if(!access(ADC_DEVICE_802, R_OK)) {
40 LOGI("DEV:%s", ADC_DEVICE_802);
41 fd = open(ADC_DEVICE_802, O_RDWR|O_CREAT|O_TRUNC, 0644);
42 } else {
43 if(!access(ADC_DEVICE_803, R_OK)) {
44 LOGI("DEV:%s", ADC_DEVICE_803);
45 fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
46 } else {
47 LOGE("No found ADC devices.");
48 return -1;
49 }
50 }
51
liubin281ac462023-07-19 14:22:54 +080052 if(fd < 0) {
53 LOGE("[%s] file open error\n", __FUNCTION__);
54 return -2;
55 }
56 ret = write(fd, &adc, 1);
57 if (ret < 0) {
58 LOGE("%s: error writing to file!\n", __FUNCTION__);
59 close(fd);
60 return -2;
61 }
62 ret = read(fd, adc_buf, 24);
63 if (ret < 0) {
64 LOGE("%s: error writing to file!\n", __FUNCTION__);
65 close(fd);
66 return -2;
67 }else{
68 LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf);
69 adc_value = strstr(adc_buf, "channel");
70 }
71 close(fd);
72 if(adc_value)
wangyouqiang59d8dd22023-10-19 18:03:54 +080073 {
74 //LOGI("%s adc: %s\n", __FUNCTION__, adc_value);
75 }
liubin281ac462023-07-19 14:22:54 +080076 else
77 return -2;
78
79 return atoi(&adc_value[9]);
80}