From 8608f9367fc29e498f42f5249aa248dd2044d567 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 26 Sep 2023 17:23:34 +1000 Subject: Tune buffer sizes and locations for I2S --- src/drivers/i2s_dac.cpp | 9 +++++++-- src/drivers/include/i2s_dac.hpp | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/i2s_dac.cpp b/src/drivers/i2s_dac.cpp index 2fdd826f..b28a8ee1 100644 --- a/src/drivers/i2s_dac.cpp +++ b/src/drivers/i2s_dac.cpp @@ -41,8 +41,13 @@ static const i2s_port_t kI2SPort = I2S_NUM_0; auto I2SDac::create(IGpios& expander) -> std::optional { i2s_chan_handle_t i2s_handle; - i2s_chan_config_t channel_config = - I2S_CHANNEL_DEFAULT_CONFIG(kI2SPort, I2S_ROLE_MASTER); + i2s_chan_config_t channel_config{ + .id = kI2SPort, + .role = I2S_ROLE_MASTER, + .dma_desc_num = 2, + .dma_frame_num = kI2SBufferLengthFrames, + .auto_clear = false, + }; ESP_ERROR_CHECK(i2s_new_channel(&channel_config, &i2s_handle, NULL)); // diff --git a/src/drivers/include/i2s_dac.hpp b/src/drivers/include/i2s_dac.hpp index 6bc5b6a4..b61f7989 100644 --- a/src/drivers/include/i2s_dac.hpp +++ b/src/drivers/include/i2s_dac.hpp @@ -18,7 +18,6 @@ #include "esp_err.h" #include "freertos/FreeRTOS.h" #include "freertos/portmacro.h" -#include "freertos/ringbuf.h" #include "freertos/stream_buffer.h" #include "result.hpp" #include "span.hpp" @@ -28,6 +27,13 @@ namespace drivers { +// DMA max buffer size for I2S is 4092. We normalise to 2-channel, 16 bit +// audio, which gives us a max of 4092 / 2 / 2 (16 bits) frames. This in turn +// means that at 48kHz, we have about 21ms of budget to fill each buffer. +// We base this off of the maximum DMA size in order to minimise the amount of +// work the CPU has to do to service the DMA callbacks. +constexpr size_t kI2SBufferLengthFrames = 1023; + /** * Interface for a DAC that receives PCM samples over I2S. */ -- cgit v1.2.3