1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <aos/errno.h>
7 #include <aos/kernel.h>
8 #include <k_api.h>
9 #include "aos/hal/adc.h"
10 #include "aos/cli.h"
11 struct adc_check {
12 adc_dev_t st_adc_info;
13 uint32_t test_threshold;
14 uint32_t test_jitter;
15 };
16
17 static struct adc_check edu_adc_dev[3] = {
18 {{0, 1000, 0x12345678}, 200, 250}, // mic adc
19 {{1, 1000, 0x12345678}, 1500, 300}, // vol
20 {{2, 1000, 0x12345678}, 1100, 300}, // extra IO
21 };
22
23 #define TEST_VOLT_MV 1500
24
adc_test_process(uint32_t port)25 static int32_t adc_test_process(uint32_t port)
26 {
27 int32_t ret = 0;
28 uint32_t output = 0;
29 uint32_t test_sum = 0;
30 uint32_t test_avrg = 0;
31 uint32_t test_min = 3300;
32 uint32_t test_max = 0;
33
34 ret = hal_adc_init(&edu_adc_dev[port].st_adc_info);
35 if (ret) {
36 printf("\r\n=====adc test : adc init failed===\r\n");
37 return -1;
38 }
39
40 for (int32_t i = 0; i < 34; i++) {
41 hal_adc_value_get(&edu_adc_dev[port].st_adc_info, &output, 1000);
42 test_sum += output;
43
44 /* the min sampling voltage */
45 if (test_min >= output) {
46 test_min = output;
47 }
48 /* the max sampling voltage */
49 if (test_max <= output) {
50 test_max = output;
51 }
52
53 osDelay(20);
54 }
55
56 hal_adc_finalize(&edu_adc_dev[port].st_adc_info);
57
58 test_avrg = (test_sum - test_min - test_max) >> 5;
59
60 printf("\r\n=====adc test : the samping volage is:%dmv===\r\n", test_avrg);
61 if (port != 0) {
62 if (((test_avrg - edu_adc_dev[port].test_jitter) > edu_adc_dev[port].test_threshold) || ((test_avrg + edu_adc_dev[port].test_jitter) < edu_adc_dev[port].test_threshold)) {
63 printf("\r\n=====adc test : the samping volage is out of scope===\r\n");
64 printf("\r\n=====adc test : FAIL===\r\n");
65 return -1;
66 }
67 } else {
68 if (test_avrg < 1) {
69 printf("\r\n=====adc test : the samping volage is out of scope===\r\n");
70 printf("\r\n=====adc test : FAIL===\r\n");
71 return -1;
72 }
73 // goto
74 }
75 printf("=====adc test : Done=====\r\n");
76
77 return 0;
78 }
79
adc_autotest(uint32_t port)80 static int adc_autotest(uint32_t port)
81 {
82 int32_t ret = 0;
83
84 printf("\r\n\r\n");
85 printf("***************************************************************\r\n");
86 printf("*************************** ADC Test **************************\r\n");
87 printf("***************************************************************\r\n");
88 printf("** Note: adc Test include 3 section **\r\n");
89 printf("***************************************************************\r\n");
90 printf("=====adc test : Start=====\r\n");
91
92 ret = adc_test_process(port);
93 if (ret) {
94 printf("\r\n=====adc test : FAIL ===\r\n");
95 return -1;
96 }
97
98 printf("\r\n=====adc test : PASS===\r\n");
99
100 return 0;
101 }
102
handle_adctest_cmd(char * pwbuf,int blen,int argc,char ** argv)103 static void handle_adctest_cmd(char *pwbuf, int blen, int argc, char **argv)
104 {
105 if (argv != 2)
106 return;
107
108 adc_autotest(atoi(argv[1]));
109 }
110
111 static struct cli_command adctest_cmd = {
112 .name = "adctest index",
113 .help = "adctest index",
114 .function = handle_adctest_cmd
115 };
116
adc_test(uint32_t port)117 int adc_test(uint32_t port)
118 {
119 aos_cli_register_command(&adctest_cmd);
120 return adc_autotest(port);
121 }
122