1 /*
2 * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 /*
7 Tests for the adc device driver
8 */
9 #include "esp_system.h"
10 #include "driver/adc.h"
11 #include "driver/rtc_io.h"
12 #include "driver/gpio.h"
13 #include "unity.h"
14 #include "esp_system.h"
15 #include "esp_event.h"
16 #include "esp_wifi.h"
17 #include "esp_log.h"
18 #include "nvs_flash.h"
19 #include "test_utils.h"
20 #include "soc/adc_periph.h"
21
22 #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3,ESP32C3)
23 #include "driver/dac.h"
24
25 static const char *TAG = "test_adc";
26
27 #ifdef CONFIG_IDF_TARGET_ESP32
28 #define ADC1_TEST_WIDTH ADC_WIDTH_BIT_12
29 #define ADC2_TEST_WIDTH ADC_WIDTH_BIT_12
30 #elif defined CONFIG_IDF_TARGET_ESP32S2
31 #define ADC1_TEST_WIDTH ADC_WIDTH_BIT_13 //ESP32S2 only support 13 bit width
32 #define ADC2_TEST_WIDTH ADC_WIDTH_BIT_13 //ESP32S2 only support 13 bit width
33 #endif
34
35 #define ADC1_TEST_ATTEN ADC_ATTEN_DB_11
36 #define ADC2_TEST_ATTEN ADC_ATTEN_DB_11
37
38 #if CONFIG_IDF_TARGET_ESP32
39 #define ADC1_TEST_CHANNEL_NUM 8
40 #elif CONFIG_IDF_TARGET_ESP32S2
41 #define ADC1_TEST_CHANNEL_NUM 10
42 #endif
43 #define ADC2_TEST_CHANNEL_NUM 6
44
45 static const int adc1_ch[ADC1_TEST_CHANNEL_NUM] = {
46 ADC1_CHANNEL_0,
47 ADC1_CHANNEL_1,
48 ADC1_CHANNEL_2,
49 ADC1_CHANNEL_3,
50 ADC1_CHANNEL_4,
51 ADC1_CHANNEL_5,
52 ADC1_CHANNEL_6,
53 ADC1_CHANNEL_7,
54 #if CONFIG_IDF_TARGET_ESP32S2
55 ADC1_CHANNEL_8,
56 ADC1_CHANNEL_9,
57 #endif
58 };
59
60 static const int adc2_ch[ADC2_TEST_CHANNEL_NUM] = {
61 ADC2_CHANNEL_0,
62 ADC2_CHANNEL_1,
63 ADC2_CHANNEL_2,
64 ADC2_CHANNEL_3,
65 ADC2_CHANNEL_4,
66 ADC2_CHANNEL_5,
67 };
68
69 #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
70
adc_fake_tie_middle(adc_unit_t adc_unit,adc_channel_t channel)71 void adc_fake_tie_middle(adc_unit_t adc_unit, adc_channel_t channel)
72 {
73 gpio_num_t gpio_num = 0;
74 if (adc_unit & ADC_UNIT_1) {
75 gpio_num = ADC_GET_IO_NUM(0, channel);
76 TEST_ESP_OK(rtc_gpio_init(gpio_num));
77 TEST_ESP_OK(rtc_gpio_pullup_en(gpio_num));
78 TEST_ESP_OK(rtc_gpio_pulldown_en(gpio_num));
79 TEST_ESP_OK(gpio_set_pull_mode(gpio_num, GPIO_PULLUP_PULLDOWN));
80 TEST_ESP_OK(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
81 }
82 if (adc_unit & ADC_UNIT_2) {
83 gpio_num = ADC_GET_IO_NUM(1, channel);
84 TEST_ESP_OK(rtc_gpio_init(gpio_num));
85 TEST_ESP_OK(rtc_gpio_pullup_en(gpio_num));
86 TEST_ESP_OK(rtc_gpio_pulldown_en(gpio_num));
87 TEST_ESP_OK(gpio_set_pull_mode(gpio_num, GPIO_PULLUP_PULLDOWN));
88 TEST_ESP_OK(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
89 }
90 vTaskDelay(10 / portTICK_RATE_MS);
91 }
92
adc_fake_tie_high(adc_unit_t adc_unit,adc_channel_t channel)93 void adc_fake_tie_high(adc_unit_t adc_unit, adc_channel_t channel)
94 {
95 gpio_num_t gpio_num = 0;
96 if (adc_unit & ADC_UNIT_1) {
97 gpio_num = ADC_GET_IO_NUM(0, channel);
98 TEST_ESP_OK(rtc_gpio_init(gpio_num));
99 TEST_ESP_OK(rtc_gpio_pullup_en(gpio_num));
100 TEST_ESP_OK(rtc_gpio_pulldown_dis(gpio_num));
101 TEST_ESP_OK(gpio_set_pull_mode(gpio_num, GPIO_PULLUP_ONLY));
102 TEST_ESP_OK(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_OUTPUT_ONLY));
103 TEST_ESP_OK(rtc_gpio_set_level(gpio_num, 1));
104 }
105 if (adc_unit & ADC_UNIT_2) {
106 gpio_num = ADC_GET_IO_NUM(1, channel);
107 TEST_ESP_OK(rtc_gpio_init(gpio_num));
108 TEST_ESP_OK(rtc_gpio_pullup_en(gpio_num));
109 TEST_ESP_OK(rtc_gpio_pulldown_dis(gpio_num));
110 TEST_ESP_OK(gpio_set_pull_mode(gpio_num, GPIO_PULLUP_ONLY));
111 TEST_ESP_OK(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_OUTPUT_ONLY));
112 TEST_ESP_OK(rtc_gpio_set_level(gpio_num, 1));
113 }
114 vTaskDelay(10 / portTICK_RATE_MS);
115 }
116
adc_fake_tie_low(adc_unit_t adc_unit,adc_channel_t channel)117 void adc_fake_tie_low(adc_unit_t adc_unit, adc_channel_t channel)
118 {
119 gpio_num_t gpio_num = 0;
120 if (adc_unit & ADC_UNIT_1) {
121 gpio_num = ADC_GET_IO_NUM(0, channel);
122 TEST_ESP_OK(rtc_gpio_init(gpio_num));
123 TEST_ESP_OK(rtc_gpio_pullup_dis(gpio_num));
124 TEST_ESP_OK(rtc_gpio_pulldown_en(gpio_num));
125 TEST_ESP_OK(gpio_set_pull_mode(gpio_num, GPIO_PULLDOWN_ONLY));
126 TEST_ESP_OK(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_OUTPUT_ONLY));
127 TEST_ESP_OK(rtc_gpio_set_level(gpio_num, 0));
128 }
129 if (adc_unit & ADC_UNIT_2) {
130 gpio_num = ADC_GET_IO_NUM(1, channel);
131 TEST_ESP_OK(rtc_gpio_init(gpio_num));
132 TEST_ESP_OK(rtc_gpio_pullup_dis(gpio_num));
133 TEST_ESP_OK(rtc_gpio_pulldown_en(gpio_num));
134 TEST_ESP_OK(gpio_set_pull_mode(gpio_num, GPIO_PULLDOWN_ONLY));
135 TEST_ESP_OK(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_OUTPUT_ONLY));
136 TEST_ESP_OK(rtc_gpio_set_level(gpio_num, 0));
137 }
138 vTaskDelay(10 / portTICK_RATE_MS);
139 }
140
adc_io_normal(adc_unit_t adc_unit,adc_channel_t channel)141 void adc_io_normal(adc_unit_t adc_unit, adc_channel_t channel)
142 {
143 gpio_num_t gpio_num = 0;
144 if (adc_unit & ADC_UNIT_1) {
145 gpio_num = ADC_GET_IO_NUM(0, channel);
146 TEST_ESP_OK(rtc_gpio_init(gpio_num));
147 TEST_ESP_OK(rtc_gpio_pullup_dis(gpio_num));
148 TEST_ESP_OK(rtc_gpio_pulldown_dis(gpio_num));
149 TEST_ESP_OK(gpio_set_pull_mode(gpio_num, GPIO_FLOATING));
150 TEST_ESP_OK(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
151 }
152 if (adc_unit & ADC_UNIT_2) {
153 gpio_num = ADC_GET_IO_NUM(1, channel);
154 TEST_ESP_OK(rtc_gpio_init(gpio_num));
155 TEST_ESP_OK(rtc_gpio_pullup_dis(gpio_num));
156 TEST_ESP_OK(rtc_gpio_pulldown_dis(gpio_num));
157 TEST_ESP_OK(gpio_set_pull_mode(gpio_num, GPIO_FLOATING));
158 TEST_ESP_OK(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
159 }
160 vTaskDelay(10 / portTICK_RATE_MS);
161 }
162
163 TEST_CASE("ADC1 rtc read", "[adc1]")
164 {
165 int adc1_val[ADC1_TEST_CHANNEL_NUM] = {0};
166
167 /* adc1 Configure */
168 adc1_config_width(ADC1_TEST_WIDTH);
169 ESP_LOGI(TAG, "ADC1 [CH - GPIO]:");
170 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
171 TEST_ESP_OK( adc1_config_channel_atten(adc1_ch[i], ADC1_TEST_ATTEN) );
172 ESP_LOGI(TAG, "[CH%d - IO%d]:", adc1_ch[i], ADC_GET_IO_NUM(0, adc1_ch[i]));
173 }
174 printf("ADC tie normal read: ");
175 vTaskDelay(10 / portTICK_RATE_MS);
176
177 /* adc Read */
178 printf("ADC1: ");
179 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
180 adc1_val[i] = adc1_get_raw((adc1_channel_t)adc1_ch[i]);
181 printf("CH%d-%d ", adc1_ch[i], adc1_val[i]);
182 }
183 printf("\n");
184
185 /* tie high */
186 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
187 adc_fake_tie_high(ADC_UNIT_1, adc1_ch[i]);
188 }
189 printf("ADC tie high read: ");
190 vTaskDelay(50 / portTICK_RATE_MS);
191 /* adc Read */
192 printf("ADC1: ");
193 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
194 adc1_val[i] = adc1_get_raw((adc1_channel_t)adc1_ch[i]);
195 printf("CH%d-%d ", adc1_ch[i], adc1_val[i]);
196 #ifdef CONFIG_IDF_TARGET_ESP32S2
197 TEST_ASSERT_EQUAL( adc1_val[i], 0x1fff );
198 #endif
199 }
200 printf("\n");
201
202 /* tie low */
203 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
204 adc_fake_tie_low(ADC_UNIT_1, adc1_ch[i]);
205 }
206 printf("ADC tie low read: ");
207 vTaskDelay(50 / portTICK_RATE_MS);
208 /* adc Read */
209 printf("ADC1: ");
210 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
211 adc1_val[i] = adc1_get_raw((adc1_channel_t)adc1_ch[i]);
212 printf("CH%d-%d ", adc1_ch[i], adc1_val[i]);
213 #ifdef CONFIG_IDF_TARGET_ESP32S2
214 TEST_ASSERT_INT_WITHIN( 100, 0, adc1_val[i] );
215 #endif
216 }
217 printf("\n");
218
219 /* tie midedle */
220 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
221 adc_fake_tie_middle(ADC_UNIT_1, adc1_ch[i]);
222 }
223 printf("ADC tie mid read: ");
224 vTaskDelay(50 / portTICK_RATE_MS);
225 /* adc Read */
226 printf("ADC1: ");
227 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
228 adc1_val[i] = adc1_get_raw((adc1_channel_t)adc1_ch[i]);
229 printf("CH%d-%d ", adc1_ch[i], adc1_val[i]);
230 #ifdef CONFIG_IDF_TARGET_ESP32S2
231 TEST_ASSERT_NOT_EQUAL( adc1_val[i], 0x1fff );
232 TEST_ASSERT_NOT_EQUAL( adc1_val[i], 0 );
233 #endif
234 }
235 printf("\n");
236
237 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
238 adc_io_normal(ADC_UNIT_1, adc1_ch[i]);
239 }
240 }
241
242 TEST_CASE("ADC2 rtc read", "[adc2]")
243 {
244 int adc2_val[ADC2_TEST_CHANNEL_NUM] = {0};
245
246 /* adc2 Configure */
247 ESP_LOGI(TAG, "ADC2 [CH - GPIO]:");
248 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
249 TEST_ESP_OK( adc2_config_channel_atten(adc2_ch[i], ADC2_TEST_ATTEN) );
250 ESP_LOGI(TAG, "[CH%d - IO%d]:", adc2_ch[i], ADC_GET_IO_NUM(1, adc2_ch[i]));
251 }
252 printf("ADC float read: ");
253 vTaskDelay(10 / portTICK_RATE_MS);
254
255 /* adc Read */
256 printf("ADC2: ");
257 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
258 TEST_ESP_OK( adc2_get_raw((adc2_channel_t)adc2_ch[i], ADC2_TEST_WIDTH, &adc2_val[i]) );
259 printf("CH%d-%d ", adc2_ch[i], adc2_val[i]);
260 }
261 printf("\n");
262
263 /* tie high */
264 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
265 adc_fake_tie_high(ADC_UNIT_2, adc2_ch[i]);
266 }
267 printf("ADC tie high read: ");
268 vTaskDelay(10 / portTICK_RATE_MS);
269 /* adc Read */
270 printf("ADC2: ");
271 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
272 TEST_ESP_OK( adc2_get_raw((adc2_channel_t)adc2_ch[i], ADC2_TEST_WIDTH, &adc2_val[i]) );
273 printf("CH%d-%d ", adc2_ch[i], adc2_val[i]);
274 #ifdef CONFIG_IDF_TARGET_ESP32S2
275 TEST_ASSERT_EQUAL( adc2_val[i], 0x1fff );
276 #endif
277 }
278 printf("\n");
279
280 /* tie low */
281 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
282 adc_fake_tie_low(ADC_UNIT_2, adc2_ch[i]);
283 }
284 printf("ADC tie low read: ");
285 vTaskDelay(10 / portTICK_RATE_MS);
286 /* adc Read */
287 printf("ADC2: ");
288 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
289 TEST_ESP_OK( adc2_get_raw((adc2_channel_t)adc2_ch[i], ADC2_TEST_WIDTH, &adc2_val[i]) );
290 printf("CH%d-%d ", adc2_ch[i], adc2_val[i]);
291 #ifdef CONFIG_IDF_TARGET_ESP32S2
292 TEST_ASSERT_INT_WITHIN( 100, 0, adc2_val[i] );
293 #endif
294 }
295 printf("\n");
296
297 /* tie midedle */
298 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
299 adc_fake_tie_middle(ADC_UNIT_2, adc2_ch[i]);
300 }
301 printf("ADC tie middle read: ");
302 vTaskDelay(10 / portTICK_RATE_MS);
303 /* adc Read */
304 printf("ADC2: ");
305 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
306 TEST_ESP_OK( adc2_get_raw((adc2_channel_t)adc2_ch[i], ADC2_TEST_WIDTH, &adc2_val[i]) );
307 printf("CH%d-%d ", adc2_ch[i], adc2_val[i]);
308 #ifdef CONFIG_IDF_TARGET_ESP32S2
309 TEST_ASSERT_NOT_EQUAL( 0, adc2_val[i] );
310 TEST_ASSERT_NOT_EQUAL( 0x1fff, adc2_val[i] );
311 #endif
312 }
313 printf("\n");
314
315 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
316 adc_io_normal(ADC_UNIT_1, adc1_ch[i]);
317 }
318 }
319
320 #include "touch_scope.h"
321 /**
322 * 0: ADC1 channels raw data debug.
323 * 1: ADC2 channels raw data debug.
324 */
325 #define SCOPE_DEBUG_TYPE 1
326 #define SCOPE_DEBUG_CHANNEL_MAX (10)
327 #define SCOPE_DEBUG_ENABLE (0)
328 #define SCOPE_UART_BUADRATE (256000)
329 #define SCOPE_DEBUG_FREQ_MS (50)
330
331 /**
332 * Manual test: Capture ADC-DMA data and display it on the serial oscilloscope. Used to observe the stability of the data.
333 * Use step:
334 * 1. Call this function in `esp-idf/tools/unit-test-app/main/app_main.c`.
335 * 2. Use `ESP-Tuning Tool`(download from `www.espressif.com`) to capture.
336 * 3. The readings of multiple channels will be displayed on the tool.
337 */
test_adc_slope_debug(void)338 void test_adc_slope_debug(void)
339 {
340 float scope_temp[SCOPE_DEBUG_CHANNEL_MAX] = {0}; // max scope channel is 10.
341 test_tp_scope_debug_init(0, -1, -1, SCOPE_UART_BUADRATE);
342
343 #if SCOPE_DEBUG_TYPE == 0
344 /* adc1 Configure */
345 adc1_config_width(ADC1_TEST_WIDTH);
346 ESP_LOGI(TAG, "ADC1 [CH - GPIO] atten %d:", ADC1_TEST_ATTEN);
347 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
348 TEST_ESP_OK( adc1_config_channel_atten(adc1_ch[i], ADC1_TEST_ATTEN) );
349 ESP_LOGI(TAG, "[CH%d - IO%d]", adc1_ch[i], ADC_GET_IO_NUM(0, adc1_ch[i]));
350 }
351 /* tie midedle */
352 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
353 adc_fake_tie_middle(ADC_UNIT_1, adc1_ch[i]);
354 }
355 vTaskDelay(10 / portTICK_RATE_MS);
356
357 while (1) {
358 /* adc Read */
359 for (int i = 0; i < ADC1_TEST_CHANNEL_NUM; i++) {
360 scope_temp[i] = adc1_get_raw((adc1_channel_t)adc1_ch[i]);
361 }
362 test_tp_print_to_scope(scope_temp, ADC1_TEST_CHANNEL_NUM);
363 vTaskDelay(SCOPE_DEBUG_FREQ_MS / portTICK_RATE_MS);
364 }
365 #elif SCOPE_DEBUG_TYPE == 1
366 int adc2_val[ADC2_TEST_CHANNEL_NUM] = {0};
367
368 /* adc2 Configure */
369 ESP_LOGI(TAG, "ADC2 [CH - GPIO] atten %d:", ADC2_TEST_ATTEN);
370 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
371 TEST_ESP_OK( adc2_config_channel_atten(adc2_ch[i], ADC2_TEST_ATTEN) );
372 ESP_LOGI(TAG, "[CH%d - IO%d]:", adc2_ch[i], ADC_GET_IO_NUM(1, adc2_ch[i]));
373 }
374 /* tie midedle */
375 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
376 adc_fake_tie_middle(ADC_UNIT_2, adc2_ch[i]);
377 }
378 vTaskDelay(10 / portTICK_RATE_MS);
379
380 while (1) {
381 /* adc Read */
382 printf("ADC2: ");
383 for (int i = 0; i < ADC2_TEST_CHANNEL_NUM; i++) {
384 adc2_get_raw((adc2_channel_t)adc2_ch[i], ADC2_TEST_WIDTH, &adc2_val[i]);
385 scope_temp[i] = adc2_val[i];
386 }
387
388 test_tp_print_to_scope(scope_temp, ADC2_TEST_CHANNEL_NUM);
389 vTaskDelay(SCOPE_DEBUG_FREQ_MS / portTICK_RATE_MS);
390 }
391 #endif
392 }
393
394 #endif
395