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)
36#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 2, 0)
39template <
typename T,
typename =
void>
45template <
typename T,
typename =
void>
55template <
typename T = i2s_chan_config_t>
60 result.dma_desc_num = 6;
61 result.dma_frame_num = 240;
63 result.dma_burst_size = 0;
65 result.auto_clear_after_cb =
false;
66 result.auto_clear_before_cb =
false;
68 result.dma_buffer_in_psram =
false;
70 result.allow_pd =
false;
71 result.intr_priority = 0;
77#define I2S_CHANNEL_DEFAULT_CONFIG_SAFE(port, role) getDefaultChannelConfig(port, role)
79#define I2S_CHANNEL_DEFAULT_CONFIG_SAFE(port, role) I2S_CHANNEL_DEFAULT_CONFIG(port, role)
140 LOGE(
"Did not expect go get here");
153 uint32_t written =
bytes_written_.load(std::memory_order_relaxed);
154 uint32_t sent =
bytes_sent_.load(std::memory_order_relaxed);
162 uint32_t backlog = written > sent ? written - sent : 0;
218 if (i2s_channel_write(
tx_chan, src, size_bytes, &result,
222 bytes_written_.fetch_add((uint32_t)result, std::memory_order_relaxed);
228 if (i2s_channel_read(
rx_chan, dest, size_bytes, &result,
266 std::memory_order_relaxed);
273 i2s_chan_handle_t &
rx_chan,
int txPin,
291 i2s_chan_handle_t &
rx_chan,
int txPin,
int rxPin,
294 LOGI(
"tx: %d, rx: %d", txPin, rxPin);
295 i2s_std_config_t std_cfg;
302 .dout = (gpio_num_t)txPin,
303 .din = (gpio_num_t)rxPin,
314 if (i2s_channel_init_std_mode(
tx_chan, &std_cfg) != ESP_OK) {
315 LOGE(
"i2s_channel_init_std_mode %s",
"tx");
320 i2s_event_callbacks_t cbs = {};
322 if (i2s_channel_register_event_callback(
tx_chan, &cbs, self) != ESP_OK) {
323 LOGE(
"i2s_channel_register_event_callback %s",
"tx");
336 if (frame_size <= 0) frame_size = 1;
338 chan_cfg.dma_frame_num * frame_size;
342 if (i2s_channel_enable(
tx_chan) != ESP_OK) {
343 LOGE(
"i2s_channel_enable %s",
"tx");
349 if (i2s_channel_init_std_mode(
rx_chan, &std_cfg) != ESP_OK) {
350 LOGE(
"i2s_channel_init_std_mode %s",
"rx");
353 if (i2s_channel_enable(
rx_chan) != ESP_OK) {
354 LOGE(
"i2s_channel_enable %s",
"rx");
359 LOGD(
"%s - %s", __func__,
"started");
366 i2s_std_slot_config_t result;
370 result = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(
375 result = I2S_STD_PCM_SLOT_DEFAULT_CONFIG(
380 result = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(
389 result.slot_mask = I2S_STD_SLOT_LEFT;
392 result.slot_mask = I2S_STD_SLOT_RIGHT;
395 LOGW(
"Using channel_format: I2SChannelSelect::Left for mono");
396 result.slot_mask = I2S_STD_SLOT_LEFT;
412 frame_size = (frame_size == 0) ? 1 : frame_size;
413 if (size > 0) result.dma_frame_num = size / frame_size;
414 if (result.dma_frame_num == 0) result.dma_frame_num = 1;
415 LOGI(
"frame_size: %d", (
int)frame_size);
416 LOGI(
"dma_frame_num: %d", (
int)result.dma_frame_num);
417 LOGI(
"total buffer size: %d", (
int)(result.dma_frame_num * frame_size));
424 i2s_std_clk_config_t clk_cfg;
425 memset(&clk_cfg, 0,
sizeof(i2s_std_clk_config_t));
432 LOGI(
"mclk_multiple=%d", clk_cfg.mclk_multiple);
438 LOGI(
"mclk_multiple=384");
442 clk_cfg.mclk_multiple =
cfg.
use_apll ? I2S_MCLK_MULTIPLE_128 : I2S_MCLK_MULTIPLE_256;
443 LOGI(
"mclk_multiple=%d", clk_cfg.mclk_multiple);
452 soc_periph_i2s_clk_src_t result = I2S_CLK_SRC_DEFAULT;
455#if SOC_I2S_HW_VERSION_2
456 LOGI(
"pin_mclk is input");
457 result = I2S_CLK_SRC_EXTERNAL;
460 LOGE(
"pin_mclk as input not supported");
467#if SOC_I2S_SUPPORTS_APLL
468 result = I2S_CLK_SRC_APLL;
469 LOGI(
"clk_src is I2S_CLK_SRC_APLL");
470#elif SOC_I2S_SUPPORTS_PLL_F160M
471 result = I2S_CLK_SRC_PLL_160M;
472 LOGI(
"clk_src is I2S_CLK_SRC_PLL_160M");
480 i2s_chan_handle_t &
rx_chan)
override {
485 rc = i2s_channel_reconfig_std_clock(
tx_chan, &clock_cfg) == ESP_OK;
490 rc = i2s_channel_reconfig_std_clock(
rx_chan, &clock_cfg) == ESP_OK;
500 struct DriverPDM :
public DriverCommon {
502 i2s_chan_handle_t &rx_chan,
int txPin,
int rxPin,
505 return startTX(cfg, tx_chan, txPin);
507 return startRX(cfg, rx_chan, rxPin);
509 LOGE(
"Only RX and TX is supported for PDM")
514 i2s_pdm_tx_slot_config_t getTxSlotConfig(I2SConfigESP32V1 &cfg) {
515#ifdef SOC_I2S_HW_VERSION_2
516 return I2S_PDM_TX_SLOT_DAC_DEFAULT_CONFIG(
517 (i2s_data_bit_width_t)cfg.bits_per_sample,
518 (i2s_slot_mode_t)cfg.channels);
520 return I2S_PDM_TX_SLOT_DEFAULT_CONFIG(
521 (i2s_data_bit_width_t)cfg.bits_per_sample,
522 (i2s_slot_mode_t)cfg.channels);
526 i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) {
528 (i2s_port_t)cfg.port_no,
529 cfg.is_master ? I2S_ROLE_MASTER : I2S_ROLE_SLAVE);
532 i2s_pdm_tx_clk_config_t getTxClockConfig(I2SConfigESP32V1 &cfg) {
533#if defined(I2S_PDM_TX_CLK_DAC_DEFAULT_CONFIG)
534 return I2S_PDM_TX_CLK_DAC_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
536 return I2S_PDM_TX_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
540 bool startTX(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,
int txPin) {
541 i2s_pdm_tx_config_t pdm_tx_cfg = {
542 .clk_cfg = getTxClockConfig(cfg),
543 .slot_cfg = getTxSlotConfig(cfg),
546 .clk = (gpio_num_t)cfg.pin_bck,
547 .dout = (gpio_num_t)txPin,
548#if SOC_I2S_PDM_MAX_TX_LINES > 1
549 .dout2 = I2S_GPIO_UNUSED,
558 if (i2s_channel_init_pdm_tx_mode(tx_chan, &pdm_tx_cfg) != ESP_OK) {
559 LOGE(
"i2s_channel_init_pdm_tx_mode %s",
"tx");
562 if (i2s_channel_enable(tx_chan) != ESP_OK) {
563 LOGE(
"i2s_channel_enable %s",
"tx");
569#if defined(USE_PDM_RX)
570 i2s_pdm_rx_slot_config_t getRxSlotConfig(I2SConfigESP32V1 &cfg) {
571 return I2S_PDM_RX_SLOT_DEFAULT_CONFIG(
572 (i2s_data_bit_width_t)cfg.bits_per_sample,
573 (i2s_slot_mode_t)cfg.channels);
575 i2s_pdm_rx_clk_config_t getRxClockConfig(I2SConfigESP32V1 &cfg) {
576 return I2S_PDM_RX_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
578 bool startRX(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &rx_chan,
int rxPin) {
579 i2s_pdm_rx_config_t pdm_rx_cfg = {
580 .clk_cfg = getRxClockConfig(cfg),
581 .slot_cfg = getRxSlotConfig(cfg),
584 .clk = (gpio_num_t)cfg.pin_bck,
585 .din = (gpio_num_t)rxPin,
593 if (i2s_channel_init_pdm_rx_mode(rx_chan, &pdm_rx_cfg) != ESP_OK) {
594 LOGE(
"i2s_channel_init_pdm_rx_mode %s",
"rx");
597 if (i2s_channel_enable(rx_chan) != ESP_OK) {
598 LOGE(
"i2s_channel_enable %s",
"tx");
604 bool startRX(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &rx_chan,
int rxPin) {
605 LOGE(
"PDM RX not supported");
616 struct DriverTDM :
public DriverCommon {
617 bool startChannels(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,
618 i2s_chan_handle_t &rx_chan,
int txPin,
int rxPin,
619 I2SDriverESP32V1 * ) {
620 i2s_tdm_config_t tdm_cfg = {
621 .clk_cfg = getClockConfig(cfg),
622 .slot_cfg = getSlotConfig(cfg),
625 .mclk = (gpio_num_t)cfg.pin_mck,
626 .bclk = (gpio_num_t)cfg.pin_bck,
627 .ws = (gpio_num_t)cfg.pin_ws,
628 .dout = (gpio_num_t)txPin,
629 .din = (gpio_num_t)rxPin,
639 if (cfg.rx_tx_mode == TX_MODE || cfg.rx_tx_mode == RXTX_MODE) {
640 if (i2s_channel_init_tdm_mode(tx_chan, &tdm_cfg) != ESP_OK) {
641 LOGE(
"i2s_channel_init_tdm_tx_mode %s",
"tx");
645 if (cfg.rx_tx_mode == RX_MODE || cfg.rx_tx_mode == RXTX_MODE) {
646 if (i2s_channel_init_tdm_mode(rx_chan, &tdm_cfg) != ESP_OK) {
647 LOGE(
"i2s_channel_init_tdm_tx_mode %s",
"rx");
655 i2s_tdm_slot_config_t getSlotConfig(I2SConfigESP32V1 &cfg) {
657 for (
int j = 0; j < cfg.channels; j++) {
661 i2s_tdm_slot_config_t slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(
662 (i2s_data_bit_width_t)cfg.bits_per_sample, I2S_SLOT_MODE_STEREO,
663 (i2s_tdm_slot_mask_t)slots);
665 switch (cfg.i2s_format) {
670 slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(
671 (i2s_data_bit_width_t)cfg.bits_per_sample, I2S_SLOT_MODE_STEREO,
672 (i2s_tdm_slot_mask_t)slots);
676 slot_cfg = I2S_TDM_MSB_SLOT_DEFAULT_CONFIG(
677 (i2s_data_bit_width_t)cfg.bits_per_sample, I2S_SLOT_MODE_STEREO,
678 (i2s_tdm_slot_mask_t)slots);
681 slot_cfg = I2S_TDM_PCM_LONG_SLOT_DEFAULT_CONFIG(
682 (i2s_data_bit_width_t)cfg.bits_per_sample, I2S_SLOT_MODE_STEREO,
683 (i2s_tdm_slot_mask_t)slots);
686 LOGE(
"TDM: Unsupported format");
692 i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) {
694 (i2s_port_t)cfg.port_no,
695 cfg.is_master ? I2S_ROLE_MASTER : I2S_ROLE_SLAVE);
698 i2s_tdm_clk_config_t getClockConfig(I2SConfigESP32V1 &cfg) {
699 return I2S_TDM_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
727 LOGE(
"Channels not started");
740 if (i2s_new_channel(&chan_cfg, NULL, &
rx_chan) != ESP_OK) {
746 if (i2s_new_channel(&chan_cfg, &
tx_chan, NULL) != ESP_OK) {
776 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 portMAX_DELAY
Definition BufferBlocking.h:14
#define pdMS_TO_TICKS(ms)
Definition BufferBlocking.h:17
uint32_t TickType_t
Definition BufferBlocking.h:11
#define I2S_CHANNEL_DEFAULT_CONFIG_SAFE(port, role)
Definition I2SDriverESP32V1.h:77
#define I2S_MCLK_MULTIPLE_192
Definition I2SDriverESP32V1.h:25
#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
Definition InitializerList.h:7