blob: 18f323e5b0121327404ef5886519f1b02e50bd2d [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
wangyouqiang6de4f692023-10-26 16:37:28 +080026int mbtk_adc_close(void)
wangyouqiang2b352b22023-10-21 15:18:43 +080027{
wangyouqiang6de4f692023-10-26 16:37:28 +080028 int ret = 0;
29 int fd = 0;
30 char adc = '3';
31 //system("echo 3 > /sys/kernel/debug/adc");
32 if(!access(ADC_DEVICE_803, R_OK))
33 {
34 LOGI("DEV:%s", ADC_DEVICE_803);
35 fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
36 }
37 else
38 {
39 LOGE("No found ADC devices.");
40 return -1;
41 }
42
43 if(fd < 0) {
44 LOGE("[%s] file open error\n", __FUNCTION__);
45 return -2;
46 }
47 ret = write(fd, &adc, 1);
48 if (ret < 0) {
49 LOGE("%s: error writing to file!\n", __FUNCTION__);
50 close(fd);
51 return -2;
52 }
53 close(fd);
54 return 0;
wangyouqiang2b352b22023-10-21 15:18:43 +080055}
liubin281ac462023-07-19 14:22:54 +080056
57int mbtk_adc_get(mbtk_adc_enum channle)
58{
59 int ret = 0;
60 int fd = 0;
61 char adc_buf[24] = {0};
62 char *adc_value = NULL;
63 char adc = (channle == MBTK_ADC0 ? '0' : '1');
64
liubinb8a0dd72023-08-04 15:26:48 +080065 if(!access(ADC_DEVICE_802, R_OK)) {
66 LOGI("DEV:%s", ADC_DEVICE_802);
67 fd = open(ADC_DEVICE_802, O_RDWR|O_CREAT|O_TRUNC, 0644);
68 } else {
69 if(!access(ADC_DEVICE_803, R_OK)) {
70 LOGI("DEV:%s", ADC_DEVICE_803);
71 fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
72 } else {
73 LOGE("No found ADC devices.");
74 return -1;
75 }
76 }
77
liubin281ac462023-07-19 14:22:54 +080078 if(fd < 0) {
79 LOGE("[%s] file open error\n", __FUNCTION__);
80 return -2;
81 }
82 ret = write(fd, &adc, 1);
83 if (ret < 0) {
84 LOGE("%s: error writing to file!\n", __FUNCTION__);
85 close(fd);
86 return -2;
87 }
88 ret = read(fd, adc_buf, 24);
89 if (ret < 0) {
90 LOGE("%s: error writing to file!\n", __FUNCTION__);
91 close(fd);
92 return -2;
93 }else{
94 LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf);
95 adc_value = strstr(adc_buf, "channel");
96 }
97 close(fd);
98 if(adc_value)
wangyouqiang59d8dd22023-10-19 18:03:54 +080099 {
100 //LOGI("%s adc: %s\n", __FUNCTION__, adc_value);
101 }
liubin281ac462023-07-19 14:22:54 +0800102 else
103 return -2;
104
105 return atoi(&adc_value[9]);
106}