test_adc2.c 7.4 KB

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