test_esp32.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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
  16. */
  17. #include "esp_system.h"
  18. #include "driver/adc.h"
  19. #include "driver/dac.h"
  20. #include "driver/rtc_io.h"
  21. #include "driver/gpio.h"
  22. #include "unity.h"
  23. #include "esp_system.h"
  24. #include "esp_event.h"
  25. #include "esp_wifi.h"
  26. #include "esp_log.h"
  27. #include "nvs_flash.h"
  28. #include "test_utils.h"
  29. #if !DISABLED_FOR_TARGETS(ESP8266, ESP32S2) // This testcase for ESP32
  30. /*
  31. * ADC DMA testcase
  32. */
  33. #include "driver/i2s.h"
  34. #include "test/test_common_adc.h"
  35. //i2s number
  36. #define EXAMPLE_I2S_NUM (0)
  37. //i2s sample rate
  38. #define EXAMPLE_I2S_SAMPLE_RATE (150000)
  39. //i2s data bits
  40. #define EXAMPLE_I2S_SAMPLE_BITS (16)
  41. //enable display buffer for debug
  42. #define EXAMPLE_I2S_BUF_DEBUG (0)
  43. //I2S read buffer length
  44. #define EXAMPLE_I2S_READ_LEN (16 * 1024)
  45. //I2S data format, ADC-I2S only support mono.
  46. #define EXAMPLE_I2S_FORMAT I2S_CHANNEL_FMT_ONLY_RIGHT
  47. //I2S built-in ADC unit
  48. #define I2S_ADC_UNIT ADC_UNIT_1
  49. //I2S built-in ADC channel
  50. #define I2S_ADC_CHANNEL ADC1_CHANNEL_4
  51. /**
  52. * @brief I2S ADC/DAC mode init.
  53. */
  54. static void example_i2s_init(void)
  55. {
  56. int i2s_num = EXAMPLE_I2S_NUM;
  57. i2s_config_t i2s_config = {
  58. .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
  59. .sample_rate = EXAMPLE_I2S_SAMPLE_RATE,
  60. .bits_per_sample = EXAMPLE_I2S_SAMPLE_BITS,
  61. .channel_format = EXAMPLE_I2S_FORMAT,
  62. .intr_alloc_flags = 0,
  63. .dma_buf_count = 2,
  64. .dma_buf_len = 1024,
  65. .use_apll = 0,
  66. };
  67. //install and start i2s driver
  68. TEST_ESP_OK( i2s_driver_install(i2s_num, &i2s_config, 0, NULL) );
  69. //init ADC pad
  70. TEST_ESP_OK( i2s_set_adc_mode(I2S_ADC_UNIT, I2S_ADC_CHANNEL) );
  71. }
  72. static void example_i2s_deinit(void)
  73. {
  74. TEST_ESP_OK( i2s_driver_uninstall(EXAMPLE_I2S_NUM) );
  75. }
  76. /**
  77. * @brief debug buffer data
  78. */
  79. static void example_disp_buf(uint8_t *buf, int length)
  80. {
  81. printf("\n======");
  82. for (int i = 0; i < length; i += 2) {
  83. uint16_t data = ((uint16_t)buf[i+1] << 8) | (uint16_t)buf[i];
  84. adc_digi_output_data_t *p = (adc_digi_output_data_t *)&data;
  85. if ((i) % 16 == 0) printf("\n");
  86. printf("[%d_%d] ", p->type1.channel, p->type1.data);
  87. }
  88. printf("\n======\n");
  89. }
  90. static esp_err_t adc_dma_data_check(uint8_t *buf, int length, int ideal_level)
  91. {
  92. for (int i = 0; i < length; i += 2) {
  93. uint16_t data = ((uint16_t)buf[i+1] << 8) | (uint16_t)buf[i];
  94. adc_digi_output_data_t *p = (adc_digi_output_data_t *)&data;
  95. if (p->type1.channel != I2S_ADC_CHANNEL) {
  96. TEST_FAIL_MESSAGE("I2S-DMA data channel error!");
  97. }
  98. if (ideal_level == 1) { // high level 3.3v
  99. TEST_ASSERT_EQUAL( 0xFFF, p->type1.data );
  100. } else if (ideal_level == 0) { // low level 0v
  101. TEST_ASSERT_LESS_THAN( 10, p->type1.data );
  102. } else if (ideal_level == 2) { // middle level 1.4v
  103. TEST_ASSERT_INT_WITHIN( 128, 1586, p->type1.data );
  104. } else if (ideal_level == 3) { // normal level
  105. } else { // no check
  106. }
  107. }
  108. return ESP_OK;
  109. }
  110. static void adc_dma_read(uint8_t *buf, int length)
  111. {
  112. size_t bytes_read = 0;
  113. int flash_wr_size = 0;
  114. vTaskDelay(pdTICKS_TO_MS(100));
  115. while (flash_wr_size < length) {
  116. //read data from I2S bus, in this case, from ADC.
  117. TEST_ESP_OK( i2s_read(EXAMPLE_I2S_NUM, (void *) buf + flash_wr_size, length - flash_wr_size, &bytes_read, portMAX_DELAY) );
  118. flash_wr_size += bytes_read;
  119. example_disp_buf((uint8_t *) buf, 128);
  120. }
  121. }
  122. TEST_CASE("ADC DMA read", "[adc dma]")
  123. {
  124. int i2s_read_len = EXAMPLE_I2S_READ_LEN;
  125. char *i2s_read_buff = (char *) calloc(i2s_read_len, sizeof(char));
  126. example_i2s_init();
  127. TEST_ESP_OK( i2s_adc_enable(EXAMPLE_I2S_NUM) );
  128. adc_fake_tie_low(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
  129. adc_dma_read((uint8_t *)i2s_read_buff, i2s_read_len);
  130. adc_dma_data_check((uint8_t *)i2s_read_buff, i2s_read_len, 0);
  131. adc_fake_tie_middle(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
  132. adc_dma_read((uint8_t *)i2s_read_buff, i2s_read_len);
  133. adc_dma_data_check((uint8_t *)i2s_read_buff, i2s_read_len, 2);
  134. adc_fake_tie_high(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
  135. adc_dma_read((uint8_t *)i2s_read_buff, i2s_read_len);
  136. adc_dma_data_check((uint8_t *)i2s_read_buff, i2s_read_len, 1);
  137. adc_io_normal(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
  138. TEST_ESP_OK( i2s_adc_disable(EXAMPLE_I2S_NUM) );
  139. if (i2s_read_buff) {
  140. free(i2s_read_buff);
  141. i2s_read_buff = NULL;
  142. }
  143. example_i2s_deinit();
  144. }
  145. #endif // !DISABLED_FOR_TARGETS(ESP8266, ESP32S2)