test_esp32.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 dac device driver
  16. Hardware connection:
  17. - ESP32: GPIO25 <---> GPIO26
  18. - ESP32S2: GPIO17 <---> GPIO18
  19. */
  20. #include "esp_system.h"
  21. #include "driver/adc.h"
  22. #include "driver/dac.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 "test_dac_audio_file.h"
  31. #include "driver/i2s.h"
  32. #if !DISABLED_FOR_TARGETS(ESP8266, ESP32S2) // This testcase for ESP32
  33. /*
  34. * DAC DMA config.
  35. */
  36. //enable record sound and save in flash
  37. #define RECORD_IN_FLASH_EN (1)
  38. //enable replay recorded sound in flash
  39. #define REPLAY_FROM_FLASH_EN (1)
  40. //i2s number
  41. #define EXAMPLE_I2S_NUM (0)
  42. //i2s sample rate
  43. #define EXAMPLE_I2S_SAMPLE_RATE (16000)
  44. //i2s data bits
  45. #define EXAMPLE_I2S_SAMPLE_BITS (16)
  46. //enable display buffer for debug
  47. #define EXAMPLE_I2S_BUF_DEBUG (0)
  48. //I2S read buffer length
  49. #define EXAMPLE_I2S_READ_LEN (16 * 1024)
  50. //I2S data format
  51. #define EXAMPLE_I2S_FORMAT (I2S_CHANNEL_FMT_RIGHT_LEFT)
  52. //I2S channel number
  53. #define EXAMPLE_I2S_CHANNEL_NUM ((EXAMPLE_I2S_FORMAT < I2S_CHANNEL_FMT_ONLY_RIGHT) ? (2) : (1))
  54. /**
  55. * @brief I2S ADC/DAC mode init.
  56. */
  57. static void example_i2s_init(void)
  58. {
  59. int i2s_num = EXAMPLE_I2S_NUM;
  60. i2s_config_t i2s_config = {
  61. .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
  62. .sample_rate = EXAMPLE_I2S_SAMPLE_RATE,
  63. .bits_per_sample = EXAMPLE_I2S_SAMPLE_BITS,
  64. .channel_format = EXAMPLE_I2S_FORMAT,
  65. .intr_alloc_flags = 0,
  66. .dma_buf_count = 2,
  67. .dma_buf_len = 1024,
  68. .use_apll = 0,
  69. };
  70. //install and start i2s driver
  71. TEST_ESP_OK( i2s_driver_install(i2s_num, &i2s_config, 0, NULL) );
  72. //init DAC pad
  73. TEST_ESP_OK( i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN) );
  74. }
  75. static void example_i2s_deinit(void)
  76. {
  77. TEST_ESP_OK( i2s_set_dac_mode(I2S_DAC_CHANNEL_DISABLE) );
  78. TEST_ESP_OK( i2s_driver_uninstall(EXAMPLE_I2S_NUM) );
  79. }
  80. /**
  81. * @brief Set i2s clock for example audio file
  82. */
  83. static void example_set_file_play_mode(void)
  84. {
  85. TEST_ESP_OK( i2s_set_clk(EXAMPLE_I2S_NUM, 16000, EXAMPLE_I2S_SAMPLE_BITS, 1) );
  86. }
  87. /**
  88. * @brief Scale data to 16bit/32bit for I2S DMA output.
  89. * DAC can only output 8bit data value.
  90. * I2S DMA will still send 16 bit or 32bit data, the highest 8bit contains DAC data.
  91. */
  92. static int example_i2s_dac_data_scale(uint8_t *d_buff, uint8_t *s_buff, uint32_t len)
  93. {
  94. uint32_t j = 0;
  95. #if (EXAMPLE_I2S_SAMPLE_BITS == 16)
  96. for (int i = 0; i < len; i++) {
  97. d_buff[j++] = 0;
  98. d_buff[j++] = s_buff[i];
  99. }
  100. return (len * 2);
  101. #else
  102. for (int i = 0; i < len; i++) {
  103. d_buff[j++] = 0;
  104. d_buff[j++] = 0;
  105. d_buff[j++] = 0;
  106. d_buff[j++] = s_buff[i];
  107. }
  108. return (len * 4);
  109. #endif
  110. }
  111. /**
  112. * @brief debug buffer data
  113. */
  114. static void example_disp_buf(uint8_t *buf, int length)
  115. {
  116. printf("======\n");
  117. for (int i = 0; i < length; i++) {
  118. printf("%02x ", buf[i]);
  119. if ((i + 1) % 8 == 0) {
  120. printf("\n");
  121. }
  122. }
  123. printf("======\n");
  124. }
  125. /**
  126. * @brief Reset i2s clock and mode
  127. */
  128. static void example_reset_play_mode(void)
  129. {
  130. TEST_ESP_OK( i2s_set_clk(EXAMPLE_I2S_NUM, EXAMPLE_I2S_SAMPLE_RATE, EXAMPLE_I2S_SAMPLE_BITS, EXAMPLE_I2S_CHANNEL_NUM) );
  131. }
  132. TEST_CASE("DAC DMA output", "[dac]")
  133. {
  134. size_t bytes_written;
  135. int i2s_read_len = EXAMPLE_I2S_READ_LEN;
  136. uint8_t *i2s_write_buff = (uint8_t *) calloc(i2s_read_len, sizeof(char));
  137. int offset = 0;
  138. int tot_size = sizeof(audio_table);
  139. printf("Playing file example: \n");
  140. example_i2s_init();
  141. example_set_file_play_mode();
  142. while (offset < tot_size) {
  143. int play_len = ((tot_size - offset) > (4 * 1024)) ? (4 * 1024) : (tot_size - offset);
  144. int i2s_wr_len = example_i2s_dac_data_scale(i2s_write_buff, (uint8_t *)(audio_table + offset), play_len);
  145. i2s_write(EXAMPLE_I2S_NUM, i2s_write_buff, i2s_wr_len, &bytes_written, portMAX_DELAY);
  146. offset += play_len;
  147. example_disp_buf((uint8_t *) i2s_write_buff, 32);
  148. }
  149. example_reset_play_mode();
  150. free(i2s_write_buff);
  151. example_i2s_deinit();
  152. }
  153. #endif // !DISABLED_FOR_TARGETS(ESP8266, ESP32S2)