Trouble playing WAV on ESP32 using I2S with internal DAC

tim23x
Posts: 1
Joined: Sun Jul 28, 2019 11:24 am

Trouble playing WAV on ESP32 using I2S with internal DAC

Postby tim23x » Sat Aug 10, 2019 12:33 pm

I'm trying to use I2S and internal DAC to play WAV files from SPIFF on a Heltec WiFi LoRa 32 V2, using the Arduino IDE.

I have an audio amp and an oscilloscope hooked up to DAC2 (pin 25) of the board and I'm not getting any signal. I've simplified the problem by generating a sine wave (as in the ESP-IDF examples). Here's the code:
  1. #include <Streaming.h>
  2. #include <driver/i2s.h>
  3. #include "freertos/queue.h"
  4.  
  5. #define SAMPLE_RATE     (22050)
  6. #define SAMPLE_SIZE     4000
  7. #define PI              (3.14159265)
  8. #define I2S_BCK_IO      (GPIO_NUM_26)
  9. #define I2S_WS_IO       (GPIO_NUM_25)
  10. #define I2S_DO_IO       (GPIO_NUM_22)
  11. #define I2S_DI_IO       (-1)
  12.  
  13.  
  14. size_t i2s_bytes_write = 0;
  15. static const int i2s_num = 0;
  16.  
  17. int sample_data[SAMPLE_SIZE];
  18.  
  19. i2s_config_t i2s_config = {
  20.     .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),                                  // Only TX
  21.     .sample_rate = SAMPLE_RATE,
  22.     .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  23.     .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,                           //2-channels
  24.     .communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_I2S,
  25.     .intr_alloc_flags = 0,//ESP_INTR_FLAG_LEVEL1  
  26.     .dma_buf_count = 8,
  27.     .dma_buf_len = 64,
  28.     .use_apll = false                           //Interrupt level 1
  29. };
  30.  
  31. i2s_pin_config_t pin_config = {
  32.     .bck_io_num = I2S_BCK_IO,
  33.     .ws_io_num = I2S_WS_IO,
  34.     .data_out_num = I2S_DO_IO,
  35.     .data_in_num = I2S_DI_IO                                               //Not used
  36. };
  37.  
  38. static void setup_sine_wave()
  39. {
  40.     unsigned int i;
  41.     int sample_val;
  42.     double sin_float;
  43.     size_t i2s_bytes_write = 0;
  44.  
  45.     for (i = 0; i < SAMPLE_SIZE; i++)
  46.     {
  47.         sin_float = sin(i * PI / 180.0);
  48.         sin_float *= 127;
  49.         sample_val = (uint8_t)sin_float;
  50.         sample_data[i] = sample_val;
  51.         Serial << sample_data[i] << ",";
  52.         delay(1);
  53.     }
  54.     Serial << endl << "Sine wave generation complete" << endl;
  55. }
  56.  
  57. void setup() {
  58.     pinMode(26, OUTPUT);
  59.     Serial.begin(115200);
  60.     i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  61.     //i2s_set_pin(I2S_NUM_0, NULL);
  62.     i2s_set_pin(I2S_NUM_0, &pin_config);
  63.     i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN);
  64.     i2s_set_sample_rates(I2S_NUM_0, 22050); //set sample rates
  65.     setup_sine_wave();
  66.     i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
  67.     i2s_write(I2S_NUM_0, &sample_data, SAMPLE_SIZE, &i2s_bytes_write, 500);
  68.     i2s_driver_uninstall(I2S_NUM_0); //stop & destroy i2s driver
  69. }
  70.  
  71. void loop()
  72. {
  73.     i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  74.     i2s_write(I2S_NUM_0, &sample_data, SAMPLE_SIZE, &i2s_bytes_write, 500);
  75.     delay(100);
  76.     i2s_driver_uninstall(I2S_NUM_0);
  77.     delay(10);
  78. }
The code uploads and runs OK but I still get no signal on pin 25. I also looked on pin 26 (DAC1) but that seems to be used by LoRa_IRQ. Can anyone help me out?

Who is online

Users browsing this forum: No registered users and 75 guests