i2s_example_main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /* I2S Example
  7. * This example code will output 100Hz sine wave and triangle wave to 2-channel of I2S driver
  8. * Every 5 seconds, it will change bits_per_sample [16, 24, 32] for i2s data
  9. */
  10. #include <stdio.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/queue.h"
  13. #include "freertos/task.h"
  14. #include "driver/i2s_std.h"
  15. #include "driver/gpio.h"
  16. #include "esp_system.h"
  17. #include "esp_log.h"
  18. #include "esp_attr.h"
  19. #include <math.h>
  20. #define EXAMPLE_SAMPLE_RATE (36000)
  21. #define EXAMPLE_DATA_BIT_WIDTH (I2S_DATA_BIT_WIDTH_16BIT)
  22. #define I2S_NUM (0)
  23. #define WAVE_FREQ_HZ (100)
  24. #define PI (3.14159265)
  25. #define I2S_BCK_IO (GPIO_NUM_4)
  26. #define I2S_WS_IO (GPIO_NUM_5)
  27. #define I2S_DO_IO (GPIO_NUM_18)
  28. #define I2S_DI_IO (GPIO_NUM_18) /// Loopback internally if data_out and data_in signal are bound to a same GPIO
  29. #define SAMPLE_PER_CYCLE (EXAMPLE_SAMPLE_RATE/WAVE_FREQ_HZ)
  30. static i2s_chan_handle_t tx_handle = NULL;
  31. static i2s_chan_handle_t rx_handle = NULL;
  32. static volatile int is_overflow = 0;
  33. static uint32_t* example_generate_triangle_sine_waves(int bits, uint32_t *buf_len)
  34. {
  35. uint32_t len = ((bits + 8) / 16)*SAMPLE_PER_CYCLE * 4;
  36. uint32_t *samples_data = malloc(len);
  37. double triangle_float = -(pow(2, bits) / 2 - 1);
  38. double triangle_step = (double) pow(2, bits) / SAMPLE_PER_CYCLE;
  39. for (int i = 0; i < SAMPLE_PER_CYCLE; i++) {
  40. double sin_float = sin(i * 2 * PI / SAMPLE_PER_CYCLE);
  41. if (sin_float >= 0) {
  42. triangle_float += triangle_step;
  43. } else {
  44. triangle_float -= triangle_step;
  45. }
  46. sin_float *= (pow(2, bits) / 2 - 1);
  47. if (bits == 16) {
  48. samples_data[i] = ((short)triangle_float << 16) | (short)sin_float;
  49. } else if (bits == 24) { //1-bytes unused
  50. samples_data[i * 2] = ((int) triangle_float) << 8;
  51. samples_data[i * 2 + 1] = ((int) sin_float) << 8;
  52. } else {
  53. samples_data[i * 2] = ((int) triangle_float);
  54. samples_data[i * 2 + 1] = ((int) sin_float);
  55. }
  56. }
  57. *buf_len = len;
  58. return samples_data;
  59. }
  60. static IRAM_ATTR bool i2s_rx_queue_overflow_callback(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx)
  61. {
  62. is_overflow++;
  63. return false;
  64. }
  65. static void example_i2s_read_task(void * args)
  66. {
  67. uint32_t *rx_buf = calloc(1, 8192);
  68. size_t bytes_read = 0;
  69. uint32_t cnt = 0;
  70. while (1) {
  71. if (i2s_channel_read(rx_handle, rx_buf, 8192, &bytes_read, 1000) == ESP_OK) {
  72. if (cnt == 0) {
  73. printf("\n[i2s read] %d bytes are read successfully\n", bytes_read);
  74. printf("----------------------------------------------\n");
  75. printf("[0] %4x [1] %4x [2] %4x [3] %4x\n\n", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3]);
  76. }
  77. cnt++;
  78. cnt %= 10;
  79. /* If the polling time is too long, there will be data dropped event */
  80. // vTaskDelay(10);
  81. } else {
  82. printf("[i2s read] %d bytes are read, timeout triggered\n\n", bytes_read);
  83. }
  84. }
  85. vTaskDelete(NULL);
  86. }
  87. static void example_i2s_write_task(void * args)
  88. {
  89. uint32_t buf_len = 0;
  90. uint32_t *tx_buf = example_generate_triangle_sine_waves(EXAMPLE_DATA_BIT_WIDTH, &buf_len);
  91. size_t bytes_written = 0;
  92. uint32_t cnt = 0;
  93. while (1) {
  94. if (i2s_channel_write(tx_handle, tx_buf, buf_len, &bytes_written, 1000) == ESP_OK) {
  95. if (cnt == 0) {
  96. printf("[i2s write] %d bytes are written successfully\n", bytes_written);
  97. }
  98. cnt++;
  99. cnt %= 20;
  100. } else {
  101. printf("[i2s write] %d bytes are written, timeout triggered\n", bytes_written);
  102. }
  103. }
  104. vTaskDelete(NULL);
  105. }
  106. static void example_i2s_init_std_duplex(void)
  107. {
  108. i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
  109. /* Giving both tx and rx handle will make the i2s works in full-duplex mode and can share the bclk and ws signal */
  110. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle));
  111. i2s_std_config_t std_cfg = {
  112. .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(EXAMPLE_SAMPLE_RATE),
  113. .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(EXAMPLE_DATA_BIT_WIDTH, I2S_SLOT_MODE_STEREO),
  114. .gpio_cfg = {
  115. .mclk = I2S_GPIO_UNUSED,
  116. .bclk = I2S_BCK_IO,
  117. .ws = I2S_WS_IO,
  118. .dout = I2S_DO_IO,
  119. .din = I2S_DI_IO,
  120. .invert_flags = {
  121. .mclk_inv = false,
  122. .bclk_inv = false,
  123. .ws_inv = false,
  124. },
  125. },
  126. };
  127. #if SOC_I2S_SUPPORTS_APLL
  128. // APLL clock is more accurate when sample rate is high
  129. std_cfg.clk_cfg.clk_src = I2S_CLK_SRC_APLL;
  130. #endif
  131. /* Initialize the tx channel handle to standard mode */
  132. ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle, &std_cfg));
  133. /* Initialize the rx channel handle to standard mode */
  134. ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle, &std_cfg));
  135. printf("I2S tx and rx channels have been initialized to standard duplex mode\n");
  136. i2s_event_callbacks_t cbs = {
  137. .on_recv = NULL,
  138. .on_recv_q_ovf = i2s_rx_queue_overflow_callback,
  139. .on_sent = NULL,
  140. .on_send_q_ovf = NULL,
  141. };
  142. ESP_ERROR_CHECK(i2s_channel_register_event_callback(rx_handle, &cbs, NULL));
  143. ESP_ERROR_CHECK(i2s_channel_enable(tx_handle));
  144. ESP_ERROR_CHECK(i2s_channel_enable(rx_handle));
  145. printf("I2S tx and rx channels enabled\n");
  146. }
  147. void app_main(void)
  148. {
  149. //for 36Khz sample rates, we create 100Hz sine wave, every cycle need 36000/100 = 360 samples (4-bytes or 8-bytes each sample)
  150. example_i2s_init_std_duplex();
  151. xTaskCreate(example_i2s_write_task, "i2s write task", 4096, NULL, 5, NULL);
  152. xTaskCreate(example_i2s_read_task, "i2s read task", 8192, NULL, 5, NULL);
  153. while (1) {
  154. if (is_overflow > 0) {
  155. printf("[i2s monitor] RX message Queue overflowed\n");
  156. is_overflow--;
  157. }
  158. vTaskDelay(1);
  159. }
  160. }