test_adc2_with_wifi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. Tests for the adc2 device driver
  8. */
  9. #include "esp_system.h"
  10. #include "driver/adc.h"
  11. #include "unity.h"
  12. #include "esp_system.h"
  13. #include "esp_event.h"
  14. #include "esp_wifi.h"
  15. #include "esp_log.h"
  16. #include "nvs_flash.h"
  17. #include "test_utils.h"
  18. #include "driver/i2s.h"
  19. #include "driver/gpio.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "driver/gpio.h"
  23. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3, ESP32S3)
  24. static const char* TAG = "test_adc2";
  25. #define DEFAULT_SSID "TEST_SSID"
  26. #define DEFAULT_PWD "TEST_PASS"
  27. #if CONFIG_IDF_TARGET_ESP32
  28. #define ADC2_CHAN1 ADC2_CHANNEL_9
  29. #define ADC_WIDTH ADC_WIDTH_BIT_12
  30. #define ADC_HIGH 4095
  31. #define ADC_ERROR_THRES 20
  32. #elif CONFIG_IDF_TARGET_ESP32S2
  33. #define ADC2_CHAN1 ADC2_CHANNEL_7
  34. #define ADC_WIDTH ADC_WIDTH_BIT_13
  35. #define ADC_HIGH 8191
  36. #define ADC_ERROR_THRES 100
  37. #elif CONFIG_IDF_TARGET_ESP32C3
  38. #define ADC2_CHAN1 ADC2_CHANNEL_0
  39. #define ADC_WIDTH ADC_WIDTH_BIT_12
  40. #define ADC_HIGH 4095
  41. #define ADC_ERROR_THRES 100
  42. #elif CONFIG_IDF_TARGET_ESP32S3
  43. #define ADC2_CHAN1 ADC2_CHANNEL_0
  44. #define ADC_WIDTH ADC_WIDTH_BIT_12
  45. #define ADC_HIGH 4095
  46. #define ADC_ERROR_THRES 100
  47. #endif
  48. #define ADC_LOW 0
  49. #define TEST_NUM 8
  50. #define MINUS_UNTIL_ZERO(a, b) ( ((a) > (b)) ? ((a)-(b)): 0)
  51. #define TIME_REMAIN(start, now, timeout) ((start) >= (now) ? MINUS_UNTIL_ZERO((timeout), (now)-(start)) : -1)
  52. static void wifi_event_handler(void* arg, esp_event_base_t event_base,
  53. int32_t event_id, void* event_data)
  54. {
  55. printf("ev_handle_called.\n");
  56. switch(event_id) {
  57. case WIFI_EVENT_STA_START:
  58. ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
  59. //do not actually connect in test case
  60. //;
  61. break;
  62. case WIFI_EVENT_STA_DISCONNECTED:
  63. ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED");
  64. TEST_ESP_OK(esp_wifi_connect());
  65. break;
  66. default:
  67. break;
  68. }
  69. return ;
  70. }
  71. static void ip_event_handler(void* arg, esp_event_base_t event_base,
  72. int32_t event_id, void* event_data)
  73. {
  74. ip_event_got_ip_t *event;
  75. printf("ev_handle_called.\n");
  76. switch(event_id) {
  77. case IP_EVENT_STA_GOT_IP:
  78. event = (ip_event_got_ip_t*)event_data;
  79. ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP");
  80. ESP_LOGI(TAG, "got ip:" IPSTR "\n", IP2STR(&event->ip_info.ip));
  81. break;
  82. default:
  83. break;
  84. }
  85. return ;
  86. }
  87. static int event_init(void)
  88. {
  89. TEST_ESP_OK(esp_event_loop_create_default());
  90. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
  91. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL));
  92. return ESP_OK;
  93. }
  94. static int event_deinit(void)
  95. {
  96. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler));
  97. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler));
  98. return ESP_OK;
  99. }
  100. TEST_CASE("adc2 work with wifi","[adc]")
  101. {
  102. test_case_uses_tcpip();
  103. //---------------------------------WiFi init-----------------------------------//
  104. printf("nvs init\n");
  105. esp_err_t r = nvs_flash_init();
  106. if (r == ESP_ERR_NVS_NO_FREE_PAGES || r == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  107. printf("no free pages or nvs version mismatch, erase..\n");
  108. TEST_ESP_OK(nvs_flash_erase());
  109. r = nvs_flash_init();
  110. }
  111. TEST_ESP_OK( r);
  112. esp_netif_init();
  113. event_init();
  114. esp_netif_create_default_wifi_sta();
  115. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  116. TEST_ESP_OK(esp_wifi_init(&cfg));
  117. wifi_config_t wifi_config = {
  118. .sta = {
  119. .ssid = DEFAULT_SSID,
  120. .password = DEFAULT_PWD
  121. },
  122. };
  123. TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA));
  124. TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  125. //---------------------------------ADC init-----------------------------------//
  126. int read_raw;
  127. int target_value;
  128. gpio_num_t test_adc_io;
  129. bool test_list[TEST_NUM] ={1, 1, 0, 0, 1, 0, 1, 0};
  130. adc2_pad_get_io_num(ADC2_CHAN1, &test_adc_io);
  131. TEST_ESP_OK(adc2_config_channel_atten(ADC2_CHAN1, ADC_ATTEN_0db));
  132. printf("test_adc_io is %d\n", test_adc_io);
  133. //---------------------------------GPIO init-----------------------------------//
  134. gpio_config_t gpio_cfg = {
  135. .pin_bit_mask = BIT64(test_adc_io),
  136. .mode = GPIO_MODE_OUTPUT,
  137. //for powersave reasons, the GPIO should not be floating, select pullup
  138. .pull_up_en = true,
  139. .pull_down_en = false,
  140. .intr_type = GPIO_INTR_DISABLE,
  141. };
  142. gpio_config(&gpio_cfg);
  143. for (int i = 0; i < TEST_NUM; i++) {
  144. TEST_ESP_OK(gpio_set_level(test_adc_io, test_list[i]));
  145. target_value = test_list[i] ? ADC_HIGH : ADC_LOW;
  146. /* ADC2 single read before WIFI start */
  147. TEST_ESP_OK(adc2_get_raw(ADC2_CHAN1, ADC_WIDTH, &read_raw));
  148. printf("Before WiFi starts, ADC read: %d (target_value: %d)\n", read_raw, target_value);
  149. TEST_ASSERT_INT_WITHIN(ADC_ERROR_THRES, target_value, read_raw);
  150. /* ADC2 single read when WIFI is on */
  151. TEST_ESP_OK(esp_wifi_start());
  152. #if CONFIG_IDF_TARGET_ESP32
  153. TEST_ASSERT_EQUAL(adc2_get_raw(ADC2_CHAN1, ADC_WIDTH, &read_raw), ESP_ERR_TIMEOUT);
  154. #elif SOC_ADC_ARBITER_SUPPORTED
  155. esp_err_t ret;
  156. int32_t start = xTaskGetTickCount();
  157. int32_t now;
  158. int32_t remain_wait_ms = 0;
  159. int32_t timeout = pdMS_TO_TICKS(10);
  160. do {
  161. now = xTaskGetTickCount();
  162. remain_wait_ms = pdTICKS_TO_MS(TIME_REMAIN(start, now, timeout));
  163. ret = adc2_get_raw(ADC2_CHAN1, ADC_WIDTH, &read_raw);
  164. if (ret == ESP_OK) {
  165. printf("When WiFi is ON, ADC read: %d (target_value: %d)\n", read_raw, target_value);
  166. TEST_ASSERT_INT_WITHIN(ADC_ERROR_THRES, target_value, read_raw);
  167. break;
  168. } else if (ret == ESP_ERR_INVALID_STATE) {
  169. continue;
  170. } else {
  171. TEST_ESP_OK(ret);
  172. }
  173. } while (remain_wait_ms);
  174. #endif
  175. /* ADC2 single read after WIFI is off */
  176. TEST_ESP_OK(esp_wifi_stop());
  177. TEST_ESP_OK(adc2_get_raw(ADC2_CHAN1, ADC_WIDTH, &read_raw));
  178. printf("After WiFi is OFF, ADC read: %d (target_value: %d)\n", read_raw, target_value);
  179. TEST_ASSERT_INT_WITHIN(ADC_ERROR_THRES, target_value, read_raw);
  180. }
  181. TEST_ESP_OK(esp_wifi_deinit());
  182. event_deinit();
  183. nvs_flash_deinit();
  184. TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of esp_netif and event_loop.");
  185. }
  186. #endif //#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3, ESP32S3)
  187. #ifdef CONFIG_IDF_TARGET_ESP32
  188. #define ADC1_CHANNEL_4_IO (32)
  189. #define SAMPLE_RATE (36000)
  190. #define SAMPLE_BITS (16)
  191. static void i2s_adc_init(void)
  192. {
  193. i2s_config_t i2s_config = {
  194. .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
  195. .sample_rate = SAMPLE_RATE,
  196. .bits_per_sample = SAMPLE_BITS,
  197. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  198. .intr_alloc_flags = 0,
  199. .dma_buf_count = 2,
  200. .dma_buf_len = 1024,
  201. .use_apll = 0,
  202. };
  203. // install and start I2S driver
  204. i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  205. // init ADC pad
  206. i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_4);
  207. // enable adc sampling, ADC_WIDTH_BIT_12, ADC_ATTEN_DB_11 hard-coded in adc_i2s_mode_init
  208. i2s_adc_enable(I2S_NUM_0);
  209. }
  210. static void i2s_adc_test(void)
  211. {
  212. uint16_t *i2sReadBuffer = (uint16_t *)calloc(1024, sizeof(uint16_t));
  213. size_t bytesRead;
  214. for (int loop = 0; loop < 10; loop++) {
  215. for (int level = 0; level <= 1; level++) {
  216. if (level == 0) {
  217. gpio_set_pull_mode(ADC1_CHANNEL_4_IO, GPIO_PULLDOWN_ONLY);
  218. } else {
  219. gpio_set_pull_mode(ADC1_CHANNEL_4_IO, GPIO_PULLUP_ONLY);
  220. }
  221. vTaskDelay(200 / portTICK_RATE_MS);
  222. // read data from adc, will block until buffer is full
  223. i2s_read(I2S_NUM_0, (void *)i2sReadBuffer, 1024 * sizeof(uint16_t), &bytesRead, portMAX_DELAY);
  224. // calc average
  225. int64_t adcSumValue = 0;
  226. for (size_t i = 0; i < 1024; i++) {
  227. adcSumValue += i2sReadBuffer[i] & 0xfff;
  228. }
  229. int adcAvgValue = adcSumValue / 1024;
  230. printf("adc average val: %d\n", adcAvgValue);
  231. if (level == 0) {
  232. TEST_ASSERT_LESS_THAN(100, adcAvgValue);
  233. } else {
  234. TEST_ASSERT_GREATER_THAN(4000, adcAvgValue);
  235. }
  236. }
  237. }
  238. free(i2sReadBuffer);
  239. }
  240. static void i2s_adc_release(void)
  241. {
  242. i2s_adc_disable(I2S_NUM_0);
  243. i2s_driver_uninstall(I2S_NUM_0);
  244. }
  245. TEST_CASE("adc1 and i2s work with wifi","[adc][ignore]")
  246. {
  247. i2s_adc_init();
  248. //init wifi
  249. printf("nvs init\n");
  250. esp_err_t r = nvs_flash_init();
  251. if (r == ESP_ERR_NVS_NO_FREE_PAGES || r == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  252. printf("no free pages or nvs version mismatch, erase..\n");
  253. TEST_ESP_OK(nvs_flash_erase());
  254. r = nvs_flash_init();
  255. }
  256. TEST_ESP_OK(r);
  257. esp_netif_init();
  258. event_init();
  259. esp_netif_create_default_wifi_sta();
  260. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  261. TEST_ESP_OK(esp_wifi_init(&cfg));
  262. wifi_config_t wifi_config = {
  263. .sta = {
  264. .ssid = DEFAULT_SSID,
  265. .password = DEFAULT_PWD
  266. },
  267. };
  268. TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA));
  269. TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  270. i2s_adc_test();
  271. //now start wifi
  272. printf("wifi start...\n");
  273. TEST_ESP_OK(esp_wifi_start());
  274. //test reading during wifi on
  275. i2s_adc_test();
  276. //wifi stop again
  277. printf("wifi stop...\n");
  278. TEST_ESP_OK( esp_wifi_stop() );
  279. TEST_ESP_OK(esp_wifi_deinit());
  280. event_deinit();
  281. nvs_flash_deinit();
  282. i2s_adc_test();
  283. i2s_adc_release();
  284. printf("test passed...\n");
  285. TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of esp_netif and event_loop.");
  286. }
  287. #endif