12
12
#include "hal/i2s_hal.h"
13
13
#include "sdkconfig.h"
14
14
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
+
15
20
/**
16
21
* @brief Calculate the closest sample rate clock configuration.
17
22
* clock relationship:
@@ -185,9 +190,9 @@ void i2s_hal_tx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t *
185
190
#if CONFIG_IDF_TARGET_ESP32
186
191
i2s_ll_tx_enable_msb_right (hal -> dev , hal_cfg -> sample_bits <= I2S_BITS_PER_SAMPLE_16BIT );
187
192
#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 );
189
194
#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 );
191
196
i2s_ll_tx_force_enable_fifo_mod (hal -> dev , true);
192
197
#endif
193
198
}
@@ -213,9 +218,9 @@ void i2s_hal_rx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t *
213
218
#if CONFIG_IDF_TARGET_ESP32
214
219
i2s_ll_rx_enable_msb_right (hal -> dev , hal_cfg -> sample_bits <= I2S_BITS_PER_SAMPLE_16BIT );
215
220
#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 );
217
222
#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 );
219
224
i2s_ll_rx_force_enable_fifo_mod (hal -> dev , true);
220
225
#endif
221
226
}
@@ -248,8 +253,8 @@ void i2s_hal_tx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t
248
253
i2s_ll_tx_set_active_chan_mask (hal -> dev , hal_cfg -> chan_mask >> 16 );
249
254
i2s_ll_tx_set_chan_num (hal -> dev , chan_num );
250
255
#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
253
258
#if SOC_I2S_SUPPORTS_PDM_CODEC
254
259
if (hal_cfg -> mode & I2S_MODE_PDM ) {
255
260
// 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
290
295
i2s_ll_rx_set_chan_num (hal -> dev , chan_num );
291
296
#if SOC_I2S_SUPPORTS_PDM_RX
292
297
is_mono = (hal_cfg -> mode & I2S_MODE_PDM ) ? false : true;
293
- #endif
298
+ #endif // SOC_I2S_SUPPORTS_PDM_RX
294
299
#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
297
310
i2s_ll_rx_set_sample_bit (hal -> dev , chan_bits , data_bits );
298
311
i2s_ll_rx_enable_mono_mode (hal -> dev , is_mono );
299
312
0 commit comments