4#if defined(ESP32) && !USE_LEGACY_I2S || defined(DOXYGEN)
8#include "driver/i2s_pdm.h"
9#include "driver/i2s_std.h"
10#include "driver/i2s_tdm.h"
11#include "esp_system.h"
14#define IS_I2S_IMPLEMENTED
19#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
24#if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 1, 4)
25#define I2S_MCLK_MULTIPLE_192 static_cast<i2s_mclk_multiple_t>(192)
86 LOGE(
"Did not expect go get here");
99 uint32_t backlog =
bytes_written_.load(std::memory_order_relaxed) -
134 if (i2s_channel_write(
tx_chan, src, size_bytes, &result,
138 bytes_written_.fetch_add((uint32_t)result, std::memory_order_relaxed);
144 if (i2s_channel_read(
rx_chan, dest, size_bytes, &result,
182 std::memory_order_relaxed);
189 i2s_chan_handle_t &
rx_chan,
int txPin,
207 i2s_chan_handle_t &
rx_chan,
int txPin,
int rxPin,
210 LOGI(
"tx: %d, rx: %d", txPin, rxPin);
211 i2s_std_config_t std_cfg;
218 .dout = (gpio_num_t)txPin,
219 .din = (gpio_num_t)rxPin,
230 if (i2s_channel_init_std_mode(
tx_chan, &std_cfg) != ESP_OK) {
231 LOGE(
"i2s_channel_init_std_mode %s",
"tx");
236 i2s_event_callbacks_t cbs = {};
238 if (i2s_channel_register_event_callback(
tx_chan, &cbs, self) != ESP_OK) {
239 LOGE(
"i2s_channel_register_event_callback %s",
"tx");
252 if (frame_size <= 0) frame_size = 1;
254 chan_cfg.dma_frame_num * frame_size;
258 if (i2s_channel_enable(
tx_chan) != ESP_OK) {
259 LOGE(
"i2s_channel_enable %s",
"tx");
265 if (i2s_channel_init_std_mode(
rx_chan, &std_cfg) != ESP_OK) {
266 LOGE(
"i2s_channel_init_std_mode %s",
"rx");
269 if (i2s_channel_enable(
rx_chan) != ESP_OK) {
270 LOGE(
"i2s_channel_enable %s",
"rx");
275 LOGD(
"%s - %s", __func__,
"started");
282 i2s_std_slot_config_t result;
286 result = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(
291 result = I2S_STD_PCM_SLOT_DEFAULT_CONFIG(
296 result = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(
305 result.slot_mask = I2S_STD_SLOT_LEFT;
308 result.slot_mask = I2S_STD_SLOT_RIGHT;
311 LOGW(
"Using channel_format: I2SChannelSelect::Left for mono");
312 result.slot_mask = I2S_STD_SLOT_LEFT;
322 i2s_chan_config_t result = I2S_CHANNEL_DEFAULT_CONFIG(
328 frame_size = (frame_size == 0) ? 1 : frame_size;
329 if (size > 0) result.dma_frame_num = size / frame_size;
330 if (result.dma_frame_num == 0) result.dma_frame_num = 1;
331 LOGI(
"frame_size: %d", (
int)frame_size);
332 LOGI(
"dma_frame_num: %d", (
int)result.dma_frame_num);
333 LOGI(
"total buffer size: %d", (
int)(result.dma_frame_num * frame_size));
340 i2s_std_clk_config_t clk_cfg;
341 memset(&clk_cfg, 0,
sizeof(i2s_std_clk_config_t));
348 LOGI(
"mclk_multiple=%d", clk_cfg.mclk_multiple);
354 LOGI(
"mclk_multiple=384");
358 clk_cfg.mclk_multiple =
cfg.
use_apll ? I2S_MCLK_MULTIPLE_128 : I2S_MCLK_MULTIPLE_256;
359 LOGI(
"mclk_multiple=%d", clk_cfg.mclk_multiple);
368 soc_periph_i2s_clk_src_t result = I2S_CLK_SRC_DEFAULT;
371#if SOC_I2S_HW_VERSION_2
372 LOGI(
"pin_mclk is input");
373 result = I2S_CLK_SRC_EXTERNAL;
376 LOGE(
"pin_mclk as input not supported");
383#if SOC_I2S_SUPPORTS_APLL
384 result = I2S_CLK_SRC_APLL;
385 LOGI(
"clk_src is I2S_CLK_SRC_APLL");
386#elif SOC_I2S_SUPPORTS_PLL_F160M
387 result = I2S_CLK_SRC_PLL_160M;
388 LOGI(
"clk_src is I2S_CLK_SRC_PLL_160M");
396 i2s_chan_handle_t &
rx_chan)
override {
401 rc = i2s_channel_reconfig_std_clock(
tx_chan, &clock_cfg) == ESP_OK;
406 rc = i2s_channel_reconfig_std_clock(
rx_chan, &clock_cfg) == ESP_OK;
416 struct DriverPDM :
public DriverCommon {
418 i2s_chan_handle_t &rx_chan,
int txPin,
int rxPin,
421 return startTX(cfg, tx_chan, txPin);
423 return startRX(cfg, rx_chan, rxPin);
425 LOGE(
"Only RX and TX is supported for PDM")
430 i2s_pdm_tx_slot_config_t getTxSlotConfig(I2SConfigESP32V1 &cfg) {
431#ifdef SOC_I2S_HW_VERSION_2
432 return I2S_PDM_TX_SLOT_DAC_DEFAULT_CONFIG(
433 (i2s_data_bit_width_t)cfg.bits_per_sample,
434 (i2s_slot_mode_t)cfg.channels);
436 return I2S_PDM_TX_SLOT_DEFAULT_CONFIG(
437 (i2s_data_bit_width_t)cfg.bits_per_sample,
438 (i2s_slot_mode_t)cfg.channels);
442 i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) {
443 return I2S_CHANNEL_DEFAULT_CONFIG(
444 (i2s_port_t)cfg.port_no,
445 cfg.is_master ? I2S_ROLE_MASTER : I2S_ROLE_SLAVE);
448 i2s_pdm_tx_clk_config_t getTxClockConfig(I2SConfigESP32V1 &cfg) {
449#if defined(I2S_PDM_TX_CLK_DAC_DEFAULT_CONFIG)
450 return I2S_PDM_TX_CLK_DAC_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
452 return I2S_PDM_TX_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
456 bool startTX(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,
int txPin) {
457 i2s_pdm_tx_config_t pdm_tx_cfg = {
458 .clk_cfg = getTxClockConfig(cfg),
459 .slot_cfg = getTxSlotConfig(cfg),
462 .clk = (gpio_num_t)cfg.pin_bck,
463 .dout = (gpio_num_t)txPin,
464#if SOC_I2S_PDM_MAX_TX_LINES > 1
465 .dout2 = I2S_GPIO_UNUSED,
474 if (i2s_channel_init_pdm_tx_mode(tx_chan, &pdm_tx_cfg) != ESP_OK) {
475 LOGE(
"i2s_channel_init_pdm_tx_mode %s",
"tx");
478 if (i2s_channel_enable(tx_chan) != ESP_OK) {
479 LOGE(
"i2s_channel_enable %s",
"tx");
485#if defined(USE_PDM_RX)
486 i2s_pdm_rx_slot_config_t getRxSlotConfig(I2SConfigESP32V1 &cfg) {
487 return I2S_PDM_RX_SLOT_DEFAULT_CONFIG(
488 (i2s_data_bit_width_t)cfg.bits_per_sample,
489 (i2s_slot_mode_t)cfg.channels);
491 i2s_pdm_rx_clk_config_t getRxClockConfig(I2SConfigESP32V1 &cfg) {
492 return I2S_PDM_RX_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
494 bool startRX(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &rx_chan,
int rxPin) {
495 i2s_pdm_rx_config_t pdm_rx_cfg = {
496 .clk_cfg = getRxClockConfig(cfg),
497 .slot_cfg = getRxSlotConfig(cfg),
500 .clk = (gpio_num_t)cfg.pin_bck,
501 .din = (gpio_num_t)rxPin,
509 if (i2s_channel_init_pdm_rx_mode(rx_chan, &pdm_rx_cfg) != ESP_OK) {
510 LOGE(
"i2s_channel_init_pdm_rx_mode %s",
"rx");
513 if (i2s_channel_enable(rx_chan) != ESP_OK) {
514 LOGE(
"i2s_channel_enable %s",
"tx");
520 bool startRX(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &rx_chan,
int rxPin) {
521 LOGE(
"PDM RX not supported");
532 struct DriverTDM :
public DriverCommon {
533 bool startChannels(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,
534 i2s_chan_handle_t &rx_chan,
int txPin,
int rxPin,
535 I2SDriverESP32V1 * ) {
536 i2s_tdm_config_t tdm_cfg = {
537 .clk_cfg = getClockConfig(cfg),
538 .slot_cfg = getSlotConfig(cfg),
541 .mclk = (gpio_num_t)cfg.pin_mck,
542 .bclk = (gpio_num_t)cfg.pin_bck,
543 .ws = (gpio_num_t)cfg.pin_ws,
544 .dout = (gpio_num_t)txPin,
545 .din = (gpio_num_t)rxPin,
555 if (cfg.rx_tx_mode == TX_MODE || cfg.rx_tx_mode == RXTX_MODE) {
556 if (i2s_channel_init_tdm_mode(tx_chan, &tdm_cfg) != ESP_OK) {
557 LOGE(
"i2s_channel_init_tdm_tx_mode %s",
"tx");
561 if (cfg.rx_tx_mode == RX_MODE || cfg.rx_tx_mode == RXTX_MODE) {
562 if (i2s_channel_init_tdm_mode(rx_chan, &tdm_cfg) != ESP_OK) {
563 LOGE(
"i2s_channel_init_tdm_tx_mode %s",
"rx");
571 i2s_tdm_slot_config_t getSlotConfig(I2SConfigESP32V1 &cfg) {
573 for (
int j = 0; j < cfg.channels; j++) {
577 i2s_tdm_slot_config_t slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(
578 (i2s_data_bit_width_t)cfg.bits_per_sample, I2S_SLOT_MODE_STEREO,
579 (i2s_tdm_slot_mask_t)slots);
581 switch (cfg.i2s_format) {
586 slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(
587 (i2s_data_bit_width_t)cfg.bits_per_sample, I2S_SLOT_MODE_STEREO,
588 (i2s_tdm_slot_mask_t)slots);
592 slot_cfg = I2S_TDM_MSB_SLOT_DEFAULT_CONFIG(
593 (i2s_data_bit_width_t)cfg.bits_per_sample, I2S_SLOT_MODE_STEREO,
594 (i2s_tdm_slot_mask_t)slots);
597 slot_cfg = I2S_TDM_PCM_LONG_SLOT_DEFAULT_CONFIG(
598 (i2s_data_bit_width_t)cfg.bits_per_sample, I2S_SLOT_MODE_STEREO,
599 (i2s_tdm_slot_mask_t)slots);
602 LOGE(
"TDM: Unsupported format");
608 i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) {
609 return I2S_CHANNEL_DEFAULT_CONFIG(
610 (i2s_port_t)cfg.port_no,
611 cfg.is_master ? I2S_ROLE_MASTER : I2S_ROLE_SLAVE);
614 i2s_tdm_clk_config_t getClockConfig(I2SConfigESP32V1 &cfg) {
615 return I2S_TDM_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
643 LOGE(
"Channels not started");
656 if (i2s_new_channel(&chan_cfg, NULL, &
rx_chan) != ESP_OK) {
662 if (i2s_new_channel(&chan_cfg, &
tx_chan, NULL) != ESP_OK) {
692 LOGE(
"Unsupported signal_type");
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define TRACEE()
Definition AudioLoggerIDF.h:34
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define IRAM_ATTR
Definition AudioStreams.h:13
#define I2S_MCLK_MULTIPLE_192
Definition I2SDriverESP32V1.h:25
#define portMAX_DELAY
Definition QueueZephyr.h:14
#define pdMS_TO_TICKS(ms)
Definition QueueZephyr.h:17
uint32_t TickType_t
Definition QueueZephyr.h:11
#define assert(T)
Definition avr.h:10
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:26
@ RXTX_MODE
Definition AudioTypes.h:26
@ TX_MODE
Definition AudioTypes.h:26
@ RX_MODE
Definition AudioTypes.h:26