test_esp32.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Tests for the adc device driver on ESP32 only
  16. */
  17. #include "sdkconfig.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp_system.h"
  20. #include "driver/adc.h"
  21. #include "driver/rtc_io.h"
  22. #include "driver/gpio.h"
  23. #include "unity.h"
  24. #include "esp_system.h"
  25. #include "esp_event.h"
  26. #include "esp_wifi.h"
  27. #include "esp_log.h"
  28. #include "nvs_flash.h"
  29. #include "test_utils.h"
  30. #include "esp_rom_sys.h"
  31. #include "driver/dac.h"
  32. /*
  33. * ADC DMA testcase
  34. */
  35. #include "driver/i2s.h"
  36. #include "test/test_common_adc.h"
  37. //i2s number
  38. #define EXAMPLE_I2S_NUM (0)
  39. //i2s sample rate
  40. #define EXAMPLE_I2S_SAMPLE_RATE (150000)
  41. //i2s data bits
  42. #define EXAMPLE_I2S_SAMPLE_BITS (16)
  43. //enable display buffer for debug
  44. #define EXAMPLE_I2S_BUF_DEBUG (0)
  45. //I2S read buffer length
  46. #define EXAMPLE_I2S_READ_LEN (16 * 1024)
  47. //I2S data format, ADC-I2S only support mono.
  48. #define EXAMPLE_I2S_FORMAT I2S_CHANNEL_FMT_ONLY_RIGHT
  49. //I2S built-in ADC unit
  50. #define I2S_ADC_UNIT ADC_UNIT_1
  51. //I2S built-in ADC channel
  52. #define I2S_ADC_CHANNEL ADC1_CHANNEL_4
  53. /**
  54. * @brief I2S ADC/DAC mode init.
  55. */
  56. static void example_i2s_init(void)
  57. {
  58. int i2s_num = EXAMPLE_I2S_NUM;
  59. i2s_config_t i2s_config = {
  60. .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
  61. .sample_rate = EXAMPLE_I2S_SAMPLE_RATE,
  62. .bits_per_sample = EXAMPLE_I2S_SAMPLE_BITS,
  63. .channel_format = EXAMPLE_I2S_FORMAT,
  64. .intr_alloc_flags = 0,
  65. .dma_buf_count = 2,
  66. .dma_buf_len = 1024,
  67. .use_apll = 0,
  68. };
  69. //install and start i2s driver
  70. TEST_ESP_OK( i2s_driver_install(i2s_num, &i2s_config, 0, NULL) );
  71. //init ADC pad
  72. TEST_ESP_OK( i2s_set_adc_mode(I2S_ADC_UNIT, I2S_ADC_CHANNEL) );
  73. }
  74. static void example_i2s_deinit(void)
  75. {
  76. TEST_ESP_OK( i2s_driver_uninstall(EXAMPLE_I2S_NUM) );
  77. }
  78. /**
  79. * @brief debug buffer data
  80. */
  81. static void example_disp_buf(uint8_t *buf, int length)
  82. {
  83. printf("\n======");
  84. for (int i = 0; i < length; i += 2) {
  85. uint16_t data = ((uint16_t)buf[i+1] << 8) | (uint16_t)buf[i];
  86. adc_digi_output_data_t *p = (adc_digi_output_data_t *)&data;
  87. if ((i) % 16 == 0) printf("\n");
  88. printf("[%d_%d] ", p->type1.channel, p->type1.data);
  89. }
  90. printf("\n======\n");
  91. }
  92. static esp_err_t adc_dma_data_check(uint8_t *buf, int length, int ideal_level)
  93. {
  94. for (int i = 0; i < length; i += 2) {
  95. uint16_t data = ((uint16_t)buf[i+1] << 8) | (uint16_t)buf[i];
  96. adc_digi_output_data_t *p = (adc_digi_output_data_t *)&data;
  97. if (p->type1.channel != I2S_ADC_CHANNEL) {
  98. TEST_FAIL_MESSAGE("I2S-DMA data channel error!");
  99. }
  100. if (ideal_level == 1) { // high level 3.3v
  101. TEST_ASSERT_EQUAL( 0xFFF, p->type1.data );
  102. } else if (ideal_level == 0) { // low level 0v
  103. TEST_ASSERT_LESS_THAN( 10, p->type1.data );
  104. } else if (ideal_level == 2) { // middle level 1.4v
  105. TEST_ASSERT_INT_WITHIN( 128, 1586, p->type1.data );
  106. } else if (ideal_level == 3) { // normal level
  107. } else { // no check
  108. }
  109. }
  110. return ESP_OK;
  111. }
  112. static void adc_dma_read(uint8_t *buf, int length)
  113. {
  114. size_t bytes_read = 0;
  115. int flash_wr_size = 0;
  116. vTaskDelay(pdTICKS_TO_MS(100));
  117. while (flash_wr_size < length) {
  118. //read data from I2S bus, in this case, from ADC.
  119. TEST_ESP_OK( i2s_read(EXAMPLE_I2S_NUM, (void *) buf + flash_wr_size, length - flash_wr_size, &bytes_read, portMAX_DELAY) );
  120. flash_wr_size += bytes_read;
  121. example_disp_buf((uint8_t *) buf, 128);
  122. }
  123. }
  124. TEST_CASE("ADC DMA read", "[adc dma]")
  125. {
  126. int i2s_read_len = EXAMPLE_I2S_READ_LEN;
  127. char *i2s_read_buff = (char *) calloc(i2s_read_len, sizeof(char));
  128. example_i2s_init();
  129. TEST_ESP_OK( i2s_adc_enable(EXAMPLE_I2S_NUM) );
  130. adc_fake_tie_low(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
  131. adc_dma_read((uint8_t *)i2s_read_buff, i2s_read_len);
  132. adc_dma_data_check((uint8_t *)i2s_read_buff, i2s_read_len, 0);
  133. adc_fake_tie_middle(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
  134. adc_dma_read((uint8_t *)i2s_read_buff, i2s_read_len);
  135. adc_dma_data_check((uint8_t *)i2s_read_buff, i2s_read_len, 2);
  136. adc_fake_tie_high(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
  137. adc_dma_read((uint8_t *)i2s_read_buff, i2s_read_len);
  138. adc_dma_data_check((uint8_t *)i2s_read_buff, i2s_read_len, 1);
  139. adc_io_normal(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
  140. TEST_ESP_OK( i2s_adc_disable(EXAMPLE_I2S_NUM) );
  141. if (i2s_read_buff) {
  142. free(i2s_read_buff);
  143. i2s_read_buff = NULL;
  144. }
  145. example_i2s_deinit();
  146. }
  147. #endif // CONFIG_IDF_TARGET_ESP32