1 /*
2  * Copyright (C) 2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stdint.h>
6 #include <aos/adc.h>
7 #include <aos/kernel.h>
8 
9 #if AOS_COMP_CLI
10 #include "aos/cli.h"
11 #endif
12 
13 /*TESTCASE1: Get ADC raw data, and then convert it into voltage. */
aos_adc_test_raw_data(uint32_t id,uint32_t channel)14 static int aos_adc_test_raw_data(uint32_t id, uint32_t channel)
15 {
16     int ret;
17     aos_adc_ref_t ref;
18     int32_t data = -1;
19     uint32_t resolution = 0;
20     uint32_t range = 0;
21     int32_t voltage = 0;
22 
23     ret = aos_adc_get(&ref, id);
24     if (ret != 0) {
25         printf("aos_adc_get fail, ret:%d\r\n", ret);
26         return -1;
27     }
28 
29     ret = aos_adc_read(&ref, channel, &data);
30     if (ret != 0) {
31         printf("aos_adc_read fail, ret:%d\r\n", ret);
32         return -1;
33     }
34     printf("Read raw data: %d rom device:%d channel:%d\r\n", data, id, channel);
35 
36     ret = aos_adc_get_resolution(&ref, &resolution);
37     if (ret != 0) {
38         printf("aos_adc_get_resolution fail, ret:%d\r\n", ret);
39         return -1;
40     }
41 
42     ret = aos_adc_get_range(&ref, channel, &range);
43     if (ret != 0) {
44         printf("aos_adc_get_range fail, ret:%d\r\n", ret);
45         return -1;
46     }
47 
48     voltage = (data * range) / (1 << resolution);
49 
50     printf("%s:Read : %d.%dV from device:%d channel:%d\r\n",
51             __func__, voltage / 1000, voltage % 1000, id, channel);
52 
53     aos_adc_put(&ref);
54     return 0;
55 }
56 
57 /*TESTCASE2: Get the input voltage directly. */
aos_adc_test_voltage(uint32_t id,uint32_t channel)58 static int aos_adc_test_voltage(uint32_t id, uint32_t channel)
59 {
60     int ret;
61     aos_adc_ref_t ref;
62     int32_t data = -1;
63 
64     ret = aos_adc_get(&ref, id);
65     if (ret != 0) {
66         printf("aos_adc_get fail, ret:%d\r\n", ret);
67         return -1;
68     }
69 
70     ret = aos_adc_read_voltage(&ref, channel, &data);
71     if (ret != 0) {
72         printf("aos_adc_read fail, ret:%d\r\n", ret);
73         return -1;
74     }
75     printf("%s:Read voltage: %d.%dV from device:%d channel:%d\r\n",
76             __func__, data / 1000, data % 1000, id, channel);
77 
78     aos_adc_put(&ref);
79     return 0;
80 }
81 
aos_adc_cli_cmd(int argc,char ** argv)82 static void aos_adc_cli_cmd(int argc, char **argv)
83 {
84     aos_status_t ret;
85     uint32_t index = argc > 1 ? atoi(argv[1]) : 0;
86     uint32_t channel = argc > 2 ? atoi(argv[2]) : 0;
87 
88     printf("aos adc example test begin ...\r\n");
89     printf("argc:%d, device index:%d, channel:%d\r\n", argc, index, channel);
90     ret = aos_adc_test_raw_data(index, channel);
91     if (ret != 0) {
92         printf("aos adc example test fail, ret:%d!\r\n", ret);
93         return;
94     }
95     ret = aos_adc_test_voltage(index, channel);
96 
97     printf("aos adc example test %s!\r\n", (ret == 0) ? "success" : "fail");
98 }
99 
100 #if AOS_COMP_CLI
101 /* reg args: fun, cmd, description*/
102 ALIOS_CLI_CMD_REGISTER(aos_adc_cli_cmd, aos_adc_example, aos adc example)
103 #endif
104