test_adc2.c 8.4 KB

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