i2s_example_main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* I2S Example
  2. This example code will output 100Hz sine wave and triangle wave to 2-channel of I2S driver
  3. Every 5 seconds, it will change bits_per_sample [16, 24, 32] for i2s data
  4. This example code is in the Public Domain (or CC0 licensed, at your option.)
  5. Unless required by applicable law or agreed to in writing, this
  6. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7. CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <stdio.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "driver/i2s.h"
  13. #include "driver/gpio.h"
  14. #include "esp_system.h"
  15. #include <math.h>
  16. #define SAMPLE_RATE (36000)
  17. #define I2S_NUM (0)
  18. #define WAVE_FREQ_HZ (100)
  19. #define PI (3.14159265)
  20. #define I2S_BCK_IO (GPIO_NUM_13)
  21. #define I2S_WS_IO (GPIO_NUM_15)
  22. #define I2S_DO_IO (GPIO_NUM_21)
  23. #define I2S_DI_IO (-1)
  24. #define SAMPLE_PER_CYCLE (SAMPLE_RATE/WAVE_FREQ_HZ)
  25. static void setup_triangle_sine_waves(int bits)
  26. {
  27. int *samples_data = malloc(((bits+8)/16)*SAMPLE_PER_CYCLE*4);
  28. unsigned int i, sample_val;
  29. double sin_float, triangle_float, triangle_step = (double) pow(2, bits) / SAMPLE_PER_CYCLE;
  30. size_t i2s_bytes_write = 0;
  31. printf("\r\nTest bits=%d free mem=%d, written data=%d\n", bits, esp_get_free_heap_size(), ((bits+8)/16)*SAMPLE_PER_CYCLE*4);
  32. triangle_float = -(pow(2, bits)/2 - 1);
  33. for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
  34. sin_float = sin(i * 2 * PI / SAMPLE_PER_CYCLE);
  35. if(sin_float >= 0)
  36. triangle_float += triangle_step;
  37. else
  38. triangle_float -= triangle_step;
  39. sin_float *= (pow(2, bits)/2 - 1);
  40. if (bits == 16) {
  41. sample_val = 0;
  42. sample_val += (short)triangle_float;
  43. sample_val = sample_val << 16;
  44. sample_val += (short) sin_float;
  45. samples_data[i] = sample_val;
  46. } else if (bits == 24) { //1-bytes unused
  47. samples_data[i*2] = ((int) triangle_float) << 8;
  48. samples_data[i*2 + 1] = ((int) sin_float) << 8;
  49. } else {
  50. samples_data[i*2] = ((int) triangle_float);
  51. samples_data[i*2 + 1] = ((int) sin_float);
  52. }
  53. }
  54. i2s_set_clk(I2S_NUM, SAMPLE_RATE, bits, 2);
  55. //Using push
  56. // for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
  57. // if (bits == 16)
  58. // i2s_push_sample(0, &samples_data[i], 100);
  59. // else
  60. // i2s_push_sample(0, &samples_data[i*2], 100);
  61. // }
  62. // or write
  63. i2s_write(I2S_NUM, samples_data, ((bits+8)/16)*SAMPLE_PER_CYCLE*4, &i2s_bytes_write, 100);
  64. free(samples_data);
  65. }
  66. void app_main(void)
  67. {
  68. //for 36Khz sample rates, we create 100Hz sine wave, every cycle need 36000/100 = 360 samples (4-bytes or 8-bytes each sample)
  69. //depend on bits_per_sample
  70. //using 6 buffers, we need 60-samples per buffer
  71. //if 2-channels, 16-bit each channel, total buffer is 360*4 = 1440 bytes
  72. //if 2-channels, 24/32-bit each channel, total buffer is 360*8 = 2880 bytes
  73. i2s_config_t i2s_config = {
  74. .mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
  75. .sample_rate = SAMPLE_RATE,
  76. .bits_per_sample = 16,
  77. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  78. .communication_format = I2S_COMM_FORMAT_STAND_MSB,
  79. .dma_buf_count = 6,
  80. .dma_buf_len = 60,
  81. .use_apll = false,
  82. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 //Interrupt level 1
  83. };
  84. i2s_pin_config_t pin_config = {
  85. .bck_io_num = I2S_BCK_IO,
  86. .ws_io_num = I2S_WS_IO,
  87. .data_out_num = I2S_DO_IO,
  88. .data_in_num = I2S_DI_IO //Not used
  89. };
  90. i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
  91. i2s_set_pin(I2S_NUM, &pin_config);
  92. int test_bits = 16;
  93. while (1) {
  94. setup_triangle_sine_waves(test_bits);
  95. vTaskDelay(5000/portTICK_RATE_MS);
  96. test_bits += 8;
  97. if(test_bits > 32)
  98. test_bits = 16;
  99. }
  100. }