blob: 601d9b06cff3b97412635d17845793e22598860c [file] [log] [blame]
b.liu450e3992024-10-10 10:59:29 +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>
b.liu9e8584b2024-11-06 19:21:28 +080019#include <unistd.h>
20#include <stdlib.h>
21
b.liu450e3992024-10-10 10:59:29 +080022#include "mbtk_log.h"
23#include "mbtk_type.h"
24#include "mbtk_adc.h"
25
26int mbtk_adc_close(const char* adc_dev)
27{
28 int ret = 0;
29 int fd = 0;
30 char adc = '3';
31 //system("echo 3 > /sys/kernel/debug/adc");
32 if(adc_dev != NULL && !access(adc_dev, R_OK))
33 {
34 //LOGI("DEV:%s", ADC_DEVICE_803);
35 fd = open(adc_dev, 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;
55}
56
57int mbtk_adc_get(const char* adc_dev, 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' : (channle == MBTK_ADC1 ? '1' : '2'));
64
65
66 if(adc_dev != NULL && !access(adc_dev, R_OK))
67 {
68 LOGI("[adc] DEV:%s", adc_dev);
69 fd = open(adc_dev, O_RDWR|O_CREAT|O_TRUNC, 0644);
70 }
71 else
72 {
73 LOGE("No found ADC devices : %s", adc_dev ? adc_dev : "NULL");
74 return -1;
75 }
76
77 if(fd < 0) {
78 LOGE("[%s] file open error\n", __FUNCTION__);
79 return -2;
80 }
81 ret = write(fd, &adc, 1);
82 if (ret < 0) {
83 LOGE("%s: error writing to file!\n", __FUNCTION__);
84 close(fd);
85 return -2;
86 }
87 ret = read(fd, adc_buf, 24);
88 if (ret < 0) {
89 LOGE("%s: error writing to file!\n", __FUNCTION__);
90 close(fd);
91 return -2;
92 }else{
93 //LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf);
94 adc_value = strstr(adc_buf, "channel");
95 }
96 close(fd);
97 if(adc_value)
98 {
99 //LOGI("%s adc: %s\n", __FUNCTION__, adc_value);
100 }
101 else
102 return -2;
103
104 return atoi(&adc_value[9]);
105}