(I already tested my hardware setup with some library I found online, which worked fine. So the hardware is OK, and the error must be in my code...)
My code is rather simple:
Code: Select all
#include <Arduino.h>
#include "SPIFFS.h"
#include "driver/i2s.h"
#include "driver/gpio.h"
// Pins
#define PIN_OUT_AUDIO_BCLK 23
#define PIN_OUT_AUDIO_LCLK 22
#define PIN_OUT_AUDIO_DATA 4
// I2S configuration
static const i2s_port_t i2s_num = i2s_port_t::I2S_NUM_0;
i2s_mode_t i2s_mode = (i2s_mode_t)(i2s_mode_t::I2S_MODE_MASTER | i2s_mode_t::I2S_MODE_TX);
static const i2s_config_t i2s_config = {
.mode = i2s_mode,
.sample_rate = 44100,
.bits_per_sample = i2s_bits_per_sample_t::I2S_BITS_PER_SAMPLE_8BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_MSB),
.intr_alloc_flags = 0, // default interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = false};
static const i2s_pin_config_t pin_config = {
.bck_io_num = PIN_OUT_AUDIO_BCLK,
.ws_io_num = PIN_OUT_AUDIO_LCLK,
.data_out_num = PIN_OUT_AUDIO_DATA,
.data_in_num = I2S_PIN_NO_CHANGE};
void setup()
{
SPIFFS.begin();
fs::File file = SPIFFS.open("/click2.wav");
i2s_driver_install(i2s_num, &i2s_config, 0, NULL);
i2s_set_pin(i2s_num, &pin_config);
while (file.available())
{
uint8_t sample = file.read();
size_t bytesWritten = 0;
i2s_write(i2s_num, &sample, 1, &bytesWritten, 100);
delayMicroseconds(3); // Just for testing
}
}
void loop()
{
}