test_esp32.c 4.6 KB

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