Stereo DAC via DMA
Posted: Fri Feb 21, 2025 2:56 pm
Hi guys! It must be some stupid mistake! But I can't find it!... So the following program supposed to send sawtooth signal to DAC1 and sine wave to DAC2 via DMA. But I have sine wave on BOTH pins GPIO25 and GPIO26! (ESP32 Wemos LoLin32 with ESP32-D0WD CPU) Appreciate for any help!!!!
Code: Select all
#include "driver/i2s.h"
#define SAMPLE_RATE 400000
#define WAVEFORM_SAMPLES 256
uint8_t stereo_buffer[WAVEFORM_SAMPLES * 2];
uint8_t wave_data1[WAVEFORM_SAMPLES];
uint8_t wave_data2[WAVEFORM_SAMPLES];
void generateWaveforms() {
for (int i = 0; i < WAVEFORM_SAMPLES; i++) {
wave_data1[i] = (uint8_t)((255 * i) / WAVEFORM_SAMPLES);
wave_data2[i] = (uint8_t)(127.5 * (1 + sin(2 * 3.14159 * i / WAVEFORM_SAMPLES)));
}
for (int i = 0; i < WAVEFORM_SAMPLES; i++) {
stereo_buffer[i * 2] = wave_data1[i]; // DAC1
stereo_buffer[i * 2 + 1] = wave_data2[i]; // DAC2
}
}
void setup() {
Serial.begin(115200);
generateWaveforms();
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_8BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = 0,
.dma_buf_count = 8,
.dma_buf_len = WAVEFORM_SAMPLES * 2,
.use_apll = false,
};
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, NULL);
i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
i2s_set_sample_rates(I2S_NUM_0, i2s_config.sample_rate);
}
void loop() {
size_t bytes_written;
i2s_write(I2S_NUM_0, stereo_buffer, sizeof(stereo_buffer), &bytes_written, portMAX_DELAY);
}