1212#include "hal/i2s_hal.h"
1313#include "sdkconfig.h"
1414
15+ #ifndef SOC_I2S_SUPPORTS_TDM
16+ #define I2S_HAL_DEFAULT_MSB_RIGHT (false) // Default msb_right bit to false
17+ #define I2S_HAL_DEFAULT_RIGHT_FIRST (I2S_HAL_DEFAULT_MSB_RIGHT) // Normally right_first bit keeps same as msb_right
18+ #endif // SOC_I2S_SUPPORTS_TDM
19+
1520/**
1621 * @brief Calculate the closest sample rate clock configuration.
1722 * clock relationship:
@@ -185,9 +190,9 @@ void i2s_hal_tx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t *
185190#if CONFIG_IDF_TARGET_ESP32
186191 i2s_ll_tx_enable_msb_right (hal -> dev , hal_cfg -> sample_bits <= I2S_BITS_PER_SAMPLE_16BIT );
187192#else
188- i2s_ll_tx_enable_msb_right (hal -> dev , false );
193+ i2s_ll_tx_enable_msb_right (hal -> dev , I2S_HAL_DEFAULT_MSB_RIGHT );
189194#endif
190- i2s_ll_tx_enable_right_first (hal -> dev , false );
195+ i2s_ll_tx_enable_right_first (hal -> dev , I2S_HAL_DEFAULT_RIGHT_FIRST );
191196 i2s_ll_tx_force_enable_fifo_mod (hal -> dev , true);
192197#endif
193198}
@@ -213,9 +218,9 @@ void i2s_hal_rx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t *
213218#if CONFIG_IDF_TARGET_ESP32
214219 i2s_ll_rx_enable_msb_right (hal -> dev , hal_cfg -> sample_bits <= I2S_BITS_PER_SAMPLE_16BIT );
215220#else
216- i2s_ll_rx_enable_msb_right (hal -> dev , false );
221+ i2s_ll_rx_enable_msb_right (hal -> dev , I2S_HAL_DEFAULT_MSB_RIGHT );
217222#endif
218- i2s_ll_rx_enable_right_first (hal -> dev , false );
223+ i2s_ll_rx_enable_right_first (hal -> dev , I2S_HAL_DEFAULT_RIGHT_FIRST );
219224 i2s_ll_rx_force_enable_fifo_mod (hal -> dev , true);
220225#endif
221226}
@@ -248,8 +253,8 @@ void i2s_hal_tx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t
248253 i2s_ll_tx_set_active_chan_mask (hal -> dev , hal_cfg -> chan_mask >> 16 );
249254 i2s_ll_tx_set_chan_num (hal -> dev , chan_num );
250255#else
251- 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
252- #endif
256+ i2s_ll_tx_set_chan_mod (hal -> dev , hal_cfg -> chan_fmt );
257+ #endif // SOC_I2S_SUPPORTS_TDM
253258#if SOC_I2S_SUPPORTS_PDM_CODEC
254259 if (hal_cfg -> mode & I2S_MODE_PDM ) {
255260 // Fixed to 16 while using mono mode and 32 while using stereo mode
@@ -290,10 +295,18 @@ void i2s_hal_rx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t
290295 i2s_ll_rx_set_chan_num (hal -> dev , chan_num );
291296#if SOC_I2S_SUPPORTS_PDM_RX
292297 is_mono = (hal_cfg -> mode & I2S_MODE_PDM ) ? false : true;
293- #endif
298+ #endif // SOC_I2S_SUPPORTS_PDM_RX
294299#else
295- i2s_ll_rx_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
296- #endif
300+ /* rx_chan_mod is related to msb_right, we take it into consideration here.
301+ * It is calculated again here instead of reading the value from the register,
302+ * so that we can avoid introducing the calling sequence dependency */
303+ bool is_msb_right = I2S_HAL_DEFAULT_MSB_RIGHT ; // Set default to false for ESP32-S2
304+ #if CONFIG_IDF_TARGET_ESP32
305+ /* Specially, `msb_right` on esp32 is related to sample bits and PDM mode */
306+ is_msb_right |= (hal_cfg -> sample_bits <= I2S_BITS_PER_SAMPLE_16BIT ) || (hal_cfg -> mode & I2S_MODE_PDM );
307+ #endif // CONFIG_IDF_TARGET_ESP32
308+ i2s_ll_rx_set_chan_mod (hal -> dev , hal_cfg -> chan_fmt , is_msb_right );
309+ #endif // SOC_I2S_SUPPORTS_TDM
297310 i2s_ll_rx_set_sample_bit (hal -> dev , chan_bits , data_bits );
298311 i2s_ll_rx_enable_mono_mode (hal -> dev , is_mono );
299312
0 commit comments