blob: 1286d7dff0bc314d19c6d5b09a8fdce7b84dcc8d [file] [log] [blame]
yu.dong638a4a92023-10-18 00:14:06 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sc_bsp.h>
5#include <errno.h>
yu.dongdb55df52025-05-06 01:40:47 -07006#include <fcntl.h>
7#include <unistd.h>
yu.dong638a4a92023-10-18 00:14:06 -07008#include "lynq-adc.h"
9#include "liblog/lynq_deflog.h"
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15#define ADC0_NODES "/sys/kernel/debug/pmu_zx29/adc0"
16#define ADC1_NODES "/sys/kernel/debug/pmu_zx29/adc1"
17#define ADC2_NODES "/sys/kernel/debug/pmu_zx29/adc2"
18
yu.dongdb55df52025-05-06 01:40:47 -070019static int adc_count_initialized = 0;
20
yu.dong638a4a92023-10-18 00:14:06 -070021/********************************************************************
22* @brief: qser_adc_show, function to read ADC value from specific channel
23* @param qadc [IN]: ADC_CHANNEL_E, the ADC channel to read from
24* @return : int, the read ADC value, or error code if failed
25* @todo: NA
26* @see: NA
27* @warning: NA
28*********************************************************************/
29int qser_adc_show(ADC_CHANNEL_E qadc)
30{
31 int adc_value;
xf.lif8024dc2024-09-26 03:10:16 -070032 char cmd_ret[1024]={0};
33 FILE *fd;
yu.dongdb55df52025-05-06 01:40:47 -070034
35 if (!adc_count_initialized) {
36 int fd_init;
37 if ((fd_init = open("/sys/kernel/debug/pmu_zx29/set_adc_count", O_WRONLY)) != -1) {
38 ssize_t ret = write(fd_init, "1", 1);
39 close(fd_init);
40 adc_count_initialized = 1;
41 LYDBGLOG("ADC count initialized, write returned: %zd bytes\n", ret);
42 } else {
43 LYERRLOG("Failed to initialize ADC count: %s\n", strerror(errno));
44 }
45 }
46
yu.dong638a4a92023-10-18 00:14:06 -070047 switch(qadc)
48 {
xf.lif8024dc2024-09-26 03:10:16 -070049 case QADC_NONE:
50 {
51 LYERRLOG("function %s line %d\n", __FUNCTION__, __LINE__);
52 return 0;
53 }
54 case ADC0:
55 {
56 LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__);
57 fd = fopen(ADC0_NODES, "r");
58 if(fd == NULL)
59 {
60 LYERRLOG("fopen error: %s", strerror(errno));
61 return -1;
62 }
63 if(fgets(cmd_ret, sizeof(cmd_ret), fd) == NULL)
64 {
65 LYERRLOG("fgets error!!!\n");
66 fclose(fd);
67 return -1;
68 }
69 fclose(fd);
70 break;
71 }
72 case ADC1:
73 {
74 LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__);
75 fd = fopen(ADC1_NODES, "r");
76 if(fd == NULL)
77 {
78 LYERRLOG("fopen error: %s", strerror(errno));
79 return -1;
80 }
81 if(fgets(cmd_ret, sizeof(cmd_ret), fd) == NULL)
82 {
83 LYERRLOG("fgets error!!!\n");
84 fclose(fd);
85 return -1;
86 }
87 fclose(fd);
88 break;
89 }
90 case ADC2:
91 {
92 LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__);
93 fd = fopen(ADC2_NODES, "r");
94 if(fd == NULL)
95 {
96 LYERRLOG("fopen error: %s", strerror(errno));
97 return -1;
98 }
99 if(fgets(cmd_ret, sizeof(cmd_ret), fd) == NULL)
100 {
101 LYERRLOG("fgets error!!!\n");
102 fclose(fd);
103 return -1;
104 }
105 fclose(fd);
106 break;
107 }
108 default:
109 {
110 LYERRLOG("input error\n");
111 return -1;
112 }
yu.dong638a4a92023-10-18 00:14:06 -0700113 }
xf.lif8024dc2024-09-26 03:10:16 -0700114
yu.dong638a4a92023-10-18 00:14:06 -0700115 adc_value = atoi(cmd_ret);
116 if (adc_value < 0 || adc_value > 12000)
117 {
118 LYERRLOG("bad adc value %s!", cmd_ret);
119 return -1;
120 }
121 return adc_value;
122}
123
124DEFINE_LYNQ_LIB_LOG(LYNQ_ADC)
125
126#ifdef __cplusplus
127}
128#endif
129