test_adc2_with_wifi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. Tests for the adc2 device driver
  3. */
  4. #include "esp_system.h"
  5. #include "driver/adc.h"
  6. #include "driver/dac.h"
  7. #include "unity.h"
  8. #include "esp_system.h"
  9. #include "esp_event.h"
  10. #include "esp_wifi.h"
  11. #include "esp_log.h"
  12. #include "nvs_flash.h"
  13. #include "test_utils.h"
  14. #include "driver/i2s.h"
  15. #include "driver/gpio.h"
  16. static const char* TAG = "test_adc2";
  17. #ifdef CONFIG_IDF_TARGET_ESP32
  18. #define ADC_TEST_WIDTH ADC_WIDTH_BIT_12
  19. #define ADC_TEST_RESOLUTION (4096)
  20. #define ADC_TEST_DAC_RANGE (256)
  21. #define ADC_TEST_CH1 ADC2_CHANNEL_8
  22. #define ADC_TEST_CH2 ADC2_CHANNEL_9
  23. #define ADC_TEST_ERROR (600)
  24. #define ADC1_CHANNEL_4_IO (32)
  25. #define SAMPLE_RATE (36000)
  26. #define SAMPLE_BITS (16)
  27. #elif defined CONFIG_IDF_TARGET_ESP32S2
  28. #define ADC_TEST_WIDTH ADC_WIDTH_BIT_13 //ESP32S2 only support 13 bit width
  29. #define ADC_TEST_RESOLUTION (8192)
  30. #define ADC_TEST_DAC_RANGE (210)
  31. #define ADC_TEST_CH1 ADC2_CHANNEL_6
  32. #define ADC_TEST_CH2 ADC2_CHANNEL_7
  33. #define ADC_TEST_ERROR (1500)
  34. #endif
  35. #define DEFAULT_SSID "TEST_SSID"
  36. #define DEFAULT_PWD "TEST_PASS"
  37. static void wifi_event_handler(void* arg, esp_event_base_t event_base,
  38. int32_t event_id, void* event_data)
  39. {
  40. printf("ev_handle_called.\n");
  41. switch(event_id) {
  42. case WIFI_EVENT_STA_START:
  43. ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
  44. //do not actually connect in test case
  45. //;
  46. break;
  47. case WIFI_EVENT_STA_DISCONNECTED:
  48. ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED");
  49. TEST_ESP_OK(esp_wifi_connect());
  50. break;
  51. default:
  52. break;
  53. }
  54. return ;
  55. }
  56. static void ip_event_handler(void* arg, esp_event_base_t event_base,
  57. int32_t event_id, void* event_data)
  58. {
  59. ip_event_got_ip_t *event;
  60. printf("ev_handle_called.\n");
  61. switch(event_id) {
  62. case IP_EVENT_STA_GOT_IP:
  63. event = (ip_event_got_ip_t*)event_data;
  64. ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP");
  65. ESP_LOGI(TAG, "got ip:" IPSTR "\n", IP2STR(&event->ip_info.ip));
  66. break;
  67. default:
  68. break;
  69. }
  70. return ;
  71. }
  72. static int event_init(void)
  73. {
  74. TEST_ESP_OK(esp_event_loop_create_default());
  75. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
  76. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL));
  77. return ESP_OK;
  78. }
  79. static int event_deinit(void)
  80. {
  81. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler));
  82. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler));
  83. return ESP_OK;
  84. }
  85. TEST_CASE("adc2 work with wifi","[adc]")
  86. {
  87. int read_raw;
  88. int target_value;
  89. test_case_uses_tcpip();
  90. //adc and dac init
  91. TEST_ESP_OK( dac_output_enable( DAC_CHANNEL_1 ));
  92. TEST_ESP_OK( dac_output_enable( DAC_CHANNEL_2 ));
  93. TEST_ESP_OK( dac_output_voltage( DAC_CHANNEL_1, 30 ));
  94. TEST_ESP_OK( dac_output_voltage( DAC_CHANNEL_2, 60 ));
  95. TEST_ESP_OK( adc2_config_channel_atten( ADC_TEST_CH1, ADC_ATTEN_0db ));
  96. TEST_ESP_OK( adc2_config_channel_atten( ADC_TEST_CH2, ADC_ATTEN_0db ));
  97. //init wifi
  98. printf("nvs init\n");
  99. esp_err_t r = nvs_flash_init();
  100. if (r == ESP_ERR_NVS_NO_FREE_PAGES || r == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  101. printf("no free pages or nvs version mismatch, erase..\n");
  102. TEST_ESP_OK(nvs_flash_erase());
  103. r = nvs_flash_init();
  104. }
  105. TEST_ESP_OK( r);
  106. esp_netif_init();
  107. event_init();
  108. esp_netif_create_default_wifi_sta();
  109. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  110. TEST_ESP_OK(esp_wifi_init(&cfg));
  111. wifi_config_t wifi_config = {
  112. .sta = {
  113. .ssid = DEFAULT_SSID,
  114. .password = DEFAULT_PWD
  115. },
  116. };
  117. TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA));
  118. TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  119. //test read value
  120. TEST_ESP_OK( adc2_get_raw( ADC_TEST_CH1, ADC_TEST_WIDTH, &read_raw ));
  121. target_value = 30*ADC_TEST_RESOLUTION*3/ADC_TEST_DAC_RANGE; //3 = 3.3/1.1
  122. printf("dac set: %d, adc read: %d (target_value: %d)\n", 30, read_raw, target_value );
  123. TEST_ASSERT_INT_WITHIN( ADC_TEST_ERROR, target_value, read_raw );
  124. TEST_ESP_OK( adc2_get_raw( ADC_TEST_CH2, ADC_TEST_WIDTH, &read_raw ));
  125. target_value = 60*ADC_TEST_RESOLUTION*3/ADC_TEST_DAC_RANGE;
  126. printf("dac set: %d, adc read: %d (target_value: %d)\n", 60, read_raw, target_value );
  127. TEST_ASSERT_INT_WITHIN( ADC_TEST_ERROR, target_value, read_raw );
  128. //now start wifi
  129. printf("wifi start...\n");
  130. TEST_ESP_OK(esp_wifi_start());
  131. //test reading during wifi on
  132. #ifdef CONFIG_IDF_TARGET_ESP32
  133. TEST_ASSERT_EQUAL( adc2_get_raw( ADC_TEST_CH1, ADC_TEST_WIDTH, &read_raw ), ESP_ERR_TIMEOUT );
  134. TEST_ASSERT_EQUAL( adc2_get_raw( ADC_TEST_CH2, ADC_TEST_WIDTH, &read_raw ), ESP_ERR_TIMEOUT );
  135. #elif defined CONFIG_IDF_TARGET_ESP32S2
  136. TEST_ASSERT_EQUAL( adc2_get_raw( ADC_TEST_CH1, ADC_TEST_WIDTH, &read_raw ), ESP_OK );
  137. TEST_ASSERT_EQUAL( adc2_get_raw( ADC_TEST_CH2, ADC_TEST_WIDTH, &read_raw ), ESP_OK );
  138. #endif
  139. //wifi stop again
  140. printf("wifi stop...\n");
  141. TEST_ESP_OK( esp_wifi_stop() );
  142. TEST_ESP_OK(esp_wifi_deinit());
  143. event_deinit();
  144. nvs_flash_deinit();
  145. //test read value
  146. TEST_ESP_OK( adc2_get_raw( ADC_TEST_CH1, ADC_TEST_WIDTH, &read_raw ));
  147. target_value = 30*ADC_TEST_RESOLUTION*3/ADC_TEST_DAC_RANGE; //3 = 3.3/1.1
  148. printf("dac set: %d, adc read: %d (target_value: %d)\n", 30, read_raw, target_value );
  149. TEST_ASSERT_INT_WITHIN( ADC_TEST_ERROR, target_value, read_raw );
  150. TEST_ESP_OK( adc2_get_raw( ADC_TEST_CH2, ADC_TEST_WIDTH, &read_raw ));
  151. target_value = 60*ADC_TEST_RESOLUTION*3/ADC_TEST_DAC_RANGE;
  152. printf("dac set: %d, adc read: %d (target_value: %d)\n", 60, read_raw, target_value );
  153. TEST_ASSERT_INT_WITHIN( ADC_TEST_ERROR, target_value, read_raw );
  154. printf("test passed...\n");
  155. TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of esp_netif and event_loop.");
  156. }
  157. #ifdef CONFIG_IDF_TARGET_ESP32
  158. static void i2s_adc_init(void)
  159. {
  160. i2s_config_t i2s_config = {
  161. .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
  162. .sample_rate = SAMPLE_RATE,
  163. .bits_per_sample = SAMPLE_BITS,
  164. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  165. .intr_alloc_flags = 0,
  166. .dma_buf_count = 2,
  167. .dma_buf_len = 1024,
  168. .use_apll = 0,
  169. };
  170. // install and start I2S driver
  171. i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  172. // init ADC pad
  173. i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_4);
  174. // enable adc sampling, ADC_WIDTH_BIT_12, ADC_ATTEN_DB_11 hard-coded in adc_i2s_mode_init
  175. i2s_adc_enable(I2S_NUM_0);
  176. }
  177. static void i2s_adc_test(void)
  178. {
  179. uint16_t *i2sReadBuffer = (uint16_t *)calloc(1024, sizeof(uint16_t));
  180. size_t bytesRead;
  181. for (int loop = 0; loop < 10; loop++) {
  182. for (int level = 0; level <= 1; level++) {
  183. if (level == 0) {
  184. gpio_set_pull_mode(ADC1_CHANNEL_4_IO, GPIO_PULLDOWN_ONLY);
  185. } else {
  186. gpio_set_pull_mode(ADC1_CHANNEL_4_IO, GPIO_PULLUP_ONLY);
  187. }
  188. vTaskDelay(200 / portTICK_RATE_MS);
  189. // read data from adc, will block until buffer is full
  190. i2s_read(I2S_NUM_0, (void *)i2sReadBuffer, 1024 * sizeof(uint16_t), &bytesRead, portMAX_DELAY);
  191. // calc average
  192. int64_t adcSumValue = 0;
  193. for (size_t i = 0; i < 1024; i++) {
  194. adcSumValue += i2sReadBuffer[i] & 0xfff;
  195. }
  196. int adcAvgValue = adcSumValue / 1024;
  197. printf("adc average val: %d\n", adcAvgValue);
  198. if (level == 0) {
  199. TEST_ASSERT_LESS_THAN(100, adcAvgValue);
  200. } else {
  201. TEST_ASSERT_GREATER_THAN(4000, adcAvgValue);
  202. }
  203. }
  204. }
  205. free(i2sReadBuffer);
  206. }
  207. static void i2s_adc_release(void)
  208. {
  209. i2s_adc_disable(I2S_NUM_0);
  210. i2s_driver_uninstall(I2S_NUM_0);
  211. }
  212. TEST_CASE("adc1 and i2s work with wifi","[adc][ignore]")
  213. {
  214. i2s_adc_init();
  215. //init wifi
  216. printf("nvs init\n");
  217. esp_err_t r = nvs_flash_init();
  218. if (r == ESP_ERR_NVS_NO_FREE_PAGES || r == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  219. printf("no free pages or nvs version mismatch, erase..\n");
  220. TEST_ESP_OK(nvs_flash_erase());
  221. r = nvs_flash_init();
  222. }
  223. TEST_ESP_OK(r);
  224. esp_netif_init();
  225. event_init();
  226. esp_netif_create_default_wifi_sta();
  227. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  228. TEST_ESP_OK(esp_wifi_init(&cfg));
  229. wifi_config_t wifi_config = {
  230. .sta = {
  231. .ssid = DEFAULT_SSID,
  232. .password = DEFAULT_PWD
  233. },
  234. };
  235. TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA));
  236. TEST_ESP_OK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  237. i2s_adc_test();
  238. //now start wifi
  239. printf("wifi start...\n");
  240. TEST_ESP_OK(esp_wifi_start());
  241. //test reading during wifi on
  242. i2s_adc_test();
  243. //wifi stop again
  244. printf("wifi stop...\n");
  245. TEST_ESP_OK( esp_wifi_stop() );
  246. TEST_ESP_OK(esp_wifi_deinit());
  247. event_deinit();
  248. nvs_flash_deinit();
  249. i2s_adc_test();
  250. i2s_adc_release();
  251. printf("test passed...\n");
  252. TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of esp_netif and event_loop.");
  253. }
  254. #endif