1 /* Copyright 2020 Canaan Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <rtthread.h>
17 #include <rtdevice.h>
18 #include <stdlib.h>
19 #include "../interdrv/adc/drv_adc.h"
20 #include "utest.h"
21 
22 /*
23  * 测试 ADC 驱动的读操作
24  * 1. 查找 ADC 设备。
25  * 2. 启用 ADC 设备。
26  * 3. 启用每个 ADC 通道并读取其值。
27  * 4. 验证读取的值在预期范围内。
28  * 5. 禁用每个 ADC 通道并验证读取值为 0。
29  *
30  * 本测试基于 01Studio 开发板,该开发板自带排针,并引出 SoC 的以下 4 个 ADC 通道
31  * 板级排针编号 | SoC 的 ADC 通道编号 | 输入电压范围
32  * -------------+---------------------+-------------
33  * 32           | ADC0                | (0 ~ 3.6V)
34  * 36           | ADC1                | (0 ~ 3.6V)
35  * 38           | ADC2                | (0 ~ 1.8V)
36  * 40           | ADC3                | (0 ~ 1.8V)
37  * SoC 的 ADC 通道默认只支持最大 1.8V 的输入电压,对于 ADC0 和 ADC1 通道,开发板
38  * 通过增加功放将最大支持电压提升到 3.6V(而且同样采用了分压机制,导致实际 ADC
39  * 通道的输入电压只有板级排针电压的一半)。
40  *
41  * 测试时注意连接输入的最大电压不要超过额定值,否则可能会损坏 ADC 通道。
42  *
43  * 另外注意这个adc 只有 12bit,所以读取的值范围是 0 ~ 4095
44  *
45  * 具体测试最大 1.8V 的 ADC 通道(譬如 38/40)时,可以自己通过两个 10K 欧姆的电
46  * 阻将模拟输入从 3.3V 分压(将可调电阻调制最大时万用表实测 A 点电压为 1.69V 左右):
47  *          +----------+    +---------------+
48  * 3.3V ----| 10K 欧姆 |----| 可调 10K 欧姆 |---- 接地
49  *          +----------+    +---------------+
50  *                                  A
51  *                                  |
52  *                              ADC2/ADC3
53  *
54  * 具体测试最大 3.6V 的 ADC 通道(譬如 32/36)时,可以直接引入 3.3V。
55  *                          +---------------+
56  * 3.3V --------------------| 可调 10K 欧姆 |---- 接地
57  *                          +---------------+
58  *                                  A
59  *                                  |
60  *                              ADC0/ADC1
61  */
test_read(void)62 static void test_read(void)
63 {
64     int i;
65     rt_err_t ret = RT_EOK;
66     rt_uint32_t value, vol;
67     rt_adc_device_t adc_dev;
68 
69     adc_dev = (rt_adc_device_t)rt_device_find(K230_ADC_NAME);
70     uassert_not_null(adc_dev);
71 
72     ret = rt_adc_enable(adc_dev, 0);
73     uassert_int_equal(ret, RT_EOK);
74 
75     for (i = 0; i < 4; i++)
76     {
77         ret = rt_adc_enable(adc_dev, i);
78         uassert_int_equal(ret, RT_EOK);
79 
80         value = rt_adc_read(adc_dev, i);
81         /* 转换为对应电压值,对应 12 位 ADC 最大值 4095, 内部基准最大电压值 1.8V,数据精度乘以 100 保留 2 位小数 */
82         vol = value * 180 / 4095;
83         if (i == 0 || i == 1)
84             vol = vol * 2; /* ADC0/ADC1 分压后实际电压是输入电压的二分之一 */
85 
86         LOG_I("ADC chan[%d] read value: %d, calculated voltage is: %d.%02dV\n",
87               i, value, vol / 100, vol % 100);
88     }
89 
90     for (i = 0; i < ADC_MAX_CHANNEL; i++)
91     {
92         ret = rt_adc_disable(adc_dev, i);
93         uassert_int_equal(ret, RT_EOK);
94 
95         value = rt_adc_read(adc_dev, i);
96         uassert_int_equal(value, 0);
97     }
98 
99     return;
100 }
101 
utest_tc_init(void)102 static rt_err_t utest_tc_init(void)
103 {
104 
105     return RT_EOK;
106 }
107 
utest_tc_cleanup(void)108 static rt_err_t utest_tc_cleanup(void)
109 {
110 
111     return RT_EOK;
112 }
113 
testcase(void)114 static void testcase(void)
115 {
116     UTEST_UNIT_RUN(test_read);
117 }
118 UTEST_TC_EXPORT(testcase, "adc", utest_tc_init, utest_tc_cleanup, 100);