i2s_example_main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "esp_system.h"
  14. #include <math.h>
  15. #define SAMPLE_RATE (36000)
  16. #define I2S_NUM (0)
  17. #define WAVE_FREQ_HZ (100)
  18. #define PI 3.14159265
  19. #define SAMPLE_PER_CYCLE (SAMPLE_RATE/WAVE_FREQ_HZ)
  20. static void setup_triangle_sine_waves(int bits)
  21. {
  22. int *samples_data = malloc(((bits+8)/16)*SAMPLE_PER_CYCLE*4);
  23. unsigned int i, sample_val;
  24. double sin_float, triangle_float, triangle_step = (double) pow(2, bits) / SAMPLE_PER_CYCLE;
  25. 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);
  26. triangle_float = -(pow(2, bits)/2 - 1);
  27. for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
  28. sin_float = sin(i * PI / 180.0);
  29. if(sin_float >= 0)
  30. triangle_float += triangle_step;
  31. else
  32. triangle_float -= triangle_step;
  33. sin_float *= (pow(2, bits)/2 - 1);
  34. if (bits == 16) {
  35. sample_val = 0;
  36. sample_val += (short)triangle_float;
  37. sample_val = sample_val << 16;
  38. sample_val += (short) sin_float;
  39. samples_data[i] = sample_val;
  40. } else if (bits == 24) { //1-bytes unused
  41. samples_data[i*2] = ((int) triangle_float) << 8;
  42. samples_data[i*2 + 1] = ((int) sin_float) << 8;
  43. } else {
  44. samples_data[i*2] = ((int) triangle_float);
  45. samples_data[i*2 + 1] = ((int) sin_float);
  46. }
  47. }
  48. i2s_set_clk(I2S_NUM, SAMPLE_RATE, bits, 2);
  49. //Using push
  50. // for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
  51. // if (bits == 16)
  52. // i2s_push_sample(0, &samples_data[i], 100);
  53. // else
  54. // i2s_push_sample(0, &samples_data[i*2], 100);
  55. // }
  56. // or write
  57. i2s_write_bytes(I2S_NUM, (const char *)samples_data, ((bits+8)/16)*SAMPLE_PER_CYCLE*4, 100);
  58. free(samples_data);
  59. }
  60. void app_main()
  61. {
  62. //for 36Khz sample rates, we create 100Hz sine wave, every cycle need 36000/100 = 360 samples (4-bytes or 8-bytes each sample)
  63. //depend on bits_per_sample
  64. //using 6 buffers, we need 60-samples per buffer
  65. //if 2-channels, 16-bit each channel, total buffer is 360*4 = 1440 bytes
  66. //if 2-channels, 24/32-bit each channel, total buffer is 360*8 = 2880 bytes
  67. i2s_config_t i2s_config = {
  68. .mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
  69. .sample_rate = SAMPLE_RATE,
  70. .bits_per_sample = 16,
  71. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  72. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  73. .dma_buf_count = 6,
  74. .dma_buf_len = 60,
  75. .use_apll = false,
  76. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 //Interrupt level 1
  77. };
  78. i2s_pin_config_t pin_config = {
  79. .bck_io_num = 26,
  80. .ws_io_num = 25,
  81. .data_out_num = 22,
  82. .data_in_num = -1 //Not used
  83. };
  84. i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
  85. i2s_set_pin(I2S_NUM, &pin_config);
  86. int test_bits = 16;
  87. while (1) {
  88. setup_triangle_sine_waves(test_bits);
  89. vTaskDelay(5000/portTICK_RATE_MS);
  90. test_bits += 8;
  91. if(test_bits > 32)
  92. test_bits = 16;
  93. }
  94. }