diff --git a/components/hal/esp32c3/include/hal/i2s_ll.h b/components/hal/esp32c3/include/hal/i2s_ll.h index 50d4e2060a..53a4213621 100644 --- a/components/hal/esp32c3/include/hal/i2s_ll.h +++ b/components/hal/esp32c3/include/hal/i2s_ll.h @@ -625,11 +625,11 @@ static inline void i2s_ll_tx_enable_pdm_hp_filter(i2s_dev_t *hw, bool enable) * @brief Enable I2S TX PDM sigma-delta codec * * @param hw Peripheral I2S hardware instance address. - * @param dither I2S TX PDM sigmadelta dither value + * @param enable whether enable sd dac one line mode */ static inline void i2s_ll_tx_enable_pdm_sd_codec(i2s_dev_t *hw, bool enable) { - hw->tx_pcm2pdm_conf.tx_pdm_dac_2out_en = enable; + hw->tx_pcm2pdm_conf.tx_pdm_dac_2out_en = !enable; hw->tx_pcm2pdm_conf.tx_pdm_dac_mode_en = enable; } diff --git a/components/hal/esp32h2/include/hal/i2s_ll.h b/components/hal/esp32h2/include/hal/i2s_ll.h index a74a70b2c0..60519fbf06 100644 --- a/components/hal/esp32h2/include/hal/i2s_ll.h +++ b/components/hal/esp32h2/include/hal/i2s_ll.h @@ -626,11 +626,11 @@ static inline void i2s_ll_tx_enable_pdm_hp_filter(i2s_dev_t *hw, bool enable) * @brief Enable I2S TX PDM sigma-delta codec * * @param hw Peripheral I2S hardware instance address. - * @param dither I2S TX PDM sigmadelta dither value + * @param enable whether enable sd dac one line mode */ static inline void i2s_ll_tx_enable_pdm_sd_codec(i2s_dev_t *hw, bool enable) { - hw->tx_pcm2pdm_conf.tx_pdm_dac_2out_en = enable; + hw->tx_pcm2pdm_conf.tx_pdm_dac_2out_en = !enable; hw->tx_pcm2pdm_conf.tx_pdm_dac_mode_en = enable; } diff --git a/components/hal/esp32s3/include/hal/i2s_ll.h b/components/hal/esp32s3/include/hal/i2s_ll.h index 11ed3ebb15..f8a63ed16c 100644 --- a/components/hal/esp32s3/include/hal/i2s_ll.h +++ b/components/hal/esp32s3/include/hal/i2s_ll.h @@ -680,11 +680,11 @@ static inline void i2s_ll_tx_enable_pdm_hp_filter(i2s_dev_t *hw, bool enable) * @brief Enable I2S TX PDM sigma-delta codec * * @param hw Peripheral I2S hardware instance address. - * @param dither I2S TX PDM sigmadelta dither value + * @param enable whether enable sd dac one line mode */ static inline void i2s_ll_tx_enable_pdm_sd_codec(i2s_dev_t *hw, bool enable) { - hw->tx_pcm2pdm_conf.tx_dac_2out_en = enable; + hw->tx_pcm2pdm_conf.tx_dac_2out_en = !enable; hw->tx_pcm2pdm_conf.tx_dac_mode_en = enable; } diff --git a/components/hal/i2s_hal.c b/components/hal/i2s_hal.c index 0fdc56c550..a95aa8cc78 100644 --- a/components/hal/i2s_hal.c +++ b/components/hal/i2s_hal.c @@ -98,7 +98,7 @@ void i2s_hal_init(i2s_hal_context_t *hal, int i2s_num) } #if SOC_I2S_SUPPORTS_PDM_TX -void i2s_hal_tx_set_pdm_mode_default(i2s_hal_context_t *hal, uint32_t sample_rate) +void i2s_hal_tx_set_pdm_mode_default(i2s_hal_context_t *hal, uint32_t sample_rate, bool is_mono) { /* enable pdm tx mode */ i2s_ll_tx_enable_pdm(hal->dev, true); @@ -130,8 +130,8 @@ void i2s_hal_tx_set_pdm_mode_default(i2s_hal_context_t *hal, uint32_t sample_rat /* set pdm tx high pass filter parameters */ i2s_ll_tx_set_pdm_hp_filter_param0(hal->dev, 6); i2s_ll_tx_set_pdm_hp_filter_param5(hal->dev, 7); - /* enable pdm sigma-delta codec */ - i2s_ll_tx_enable_pdm_sd_codec(hal->dev, true); + /* enable pdm sigma-delta dac */ + i2s_ll_tx_enable_pdm_sd_codec(hal->dev, is_mono); /* set pdm tx sigma-delta codec dither */ i2s_ll_tx_set_pdm_sd_dither(hal->dev, 0); i2s_ll_tx_set_pdm_sd_dither2(hal->dev, 1); @@ -249,6 +249,13 @@ void i2s_hal_tx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t i2s_ll_tx_set_chan_num(hal->dev, chan_num); #else i2s_ll_tx_set_chan_mod(hal->dev, hal_cfg->chan_fmt < I2S_CHANNEL_FMT_ONLY_RIGHT ? hal_cfg->chan_fmt : (hal_cfg->chan_fmt >> 1)); // 0-two channel;1-right;2-left;3-righ;4-left +#endif +#if SOC_I2S_SUPPORTS_PDM_CODEC + if (hal_cfg->mode & I2S_MODE_PDM) { + // Fixed to 16 while using mono mode and 32 while using stereo mode + data_bits = hal_cfg->chan_fmt == I2S_CHANNEL_FMT_RIGHT_LEFT ? 32 : 16; + chan_bits = data_bits; + } #endif i2s_ll_tx_set_sample_bit(hal->dev, chan_bits, data_bits); i2s_ll_tx_enable_mono_mode(hal->dev, is_mono); @@ -259,7 +266,13 @@ void i2s_hal_tx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t i2s_ll_tx_enable_msb_shift(hal->dev, shift_en); i2s_ll_tx_set_ws_width(hal->dev, ws_width); #if SOC_I2S_SUPPORTS_TDM - i2s_ll_tx_set_half_sample_bit(hal->dev, chan_num * chan_bits / 2); + uint32_t half_sample_bits = chan_num * chan_bits / 2; +#if SOC_I2S_SUPPORTS_PDM_CODEC + if (hal_cfg->mode & I2S_MODE_PDM) { + half_sample_bits = 16; // Fixed to 16 in PDM mode + } +#endif + i2s_ll_tx_set_half_sample_bit(hal->dev, half_sample_bits); #endif } @@ -322,7 +335,7 @@ void i2s_hal_config_param(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cf #if SOC_I2S_SUPPORTS_PDM_TX if (hal_cfg->mode & I2S_MODE_PDM) { /* Set tx pdm mode */ - i2s_hal_tx_set_pdm_mode_default(hal, hal_cfg->sample_rate); + i2s_hal_tx_set_pdm_mode_default(hal, hal_cfg->sample_rate, hal_cfg->chan_fmt != I2S_CHANNEL_FMT_RIGHT_LEFT); } else #endif { diff --git a/components/hal/include/hal/i2s_hal.h b/components/hal/include/hal/i2s_hal.h index 0e75b80143..2be94f66d1 100644 --- a/components/hal/include/hal/i2s_hal.h +++ b/components/hal/include/hal/i2s_hal.h @@ -352,8 +352,9 @@ void i2s_hal_rx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t * * * @param hal Context of the HAL layer * @param sample_rate PDM sample rate + * @param is_mono whether is mono */ -void i2s_hal_tx_set_pdm_mode_default(i2s_hal_context_t *hal, uint32_t sample_rate); +void i2s_hal_tx_set_pdm_mode_default(i2s_hal_context_t *hal, uint32_t sample_rate, bool is_mono); #endif #if SOC_I2S_SUPPORTS_PDM_RX