I'm unsure about the correctness of my pin setup, as the documentation does not clarify whether using a common bclk and ws pins for simplex mode is permissible. This setup is necessary due to my custom PCB design. Below are the relevant code segments:
Separate I2S config macros for Tx and RX:
- #define I2S_CONFIG_DEFAULT_RX(sample_rate, channel_fmt, bits_per_chan) { \
- .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(sample_rate), \
- .slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(bits_per_chan, channel_fmt), \
- .gpio_cfg = { \
- .mclk = GPIO_NUM_NC, \
- .bclk = GPIO_I2S_SCLK, \
- .ws = GPIO_I2S_LRCK, \
- .dout = GPIO_NUM_NC, \
- .din = GPIO_I2S_SDIN, \
- .invert_flags = { \
- .mclk_inv = false, \
- .bclk_inv = false, \
- .ws_inv = false, \
- }, \
- }, \
- }
- #define I2S_CONFIG_DEFAULT_TX(sample_rate, channel_fmt, bits_per_chan) { \
- .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(sample_rate), \
- .slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(bits_per_chan, channel_fmt), \
- .gpio_cfg = { \
- .mclk = GPIO_NUM_NC, \
- .bclk = GPIO_I2S_SCLK, \
- .ws = GPIO_I2S_LRCK, \
- .dout = GPIO_I2S_DOUT, \
- .din = GPIO_NUM_NC, \
- .invert_flags = { \
- .mclk_inv = false, \
- .bclk_inv = false, \
- .ws_inv = false, \
- }, \
- }, \
- }
- static esp_err_t bsp_i2s_init_microphone(i2s_port_t i2s_num, uint32_t sample_rate, i2s_slot_mode_t channel_format, i2s_data_bit_width_t bits_per_chan)
- {
- esp_err_t ret_val = ESP_OK;
- i2s_chan_config_t rx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(i2s_num, I2S_ROLE_MASTER);
- ret_val |= i2s_new_channel(&rx_chan_cfg, NULL, &rxHandle);
- i2s_std_config_t rx_std_cfg = I2S_CONFIG_DEFAULT_RX(sample_rate, channel_format, bits_per_chan);
- rx_std_cfg.slot_cfg.slot_mask = I2S_STD_SLOT_BOTH;
- rx_std_cfg.clk_cfg.mclk_multiple = (i2s_mclk_multiple_t) (I2S_MCLK_MULTIPLE_384);
- ret_val |= i2s_channel_init_std_mode(rxHandle, &rx_std_cfg);
- ret_val |= i2s_channel_enable(rxHandle);
- return ret_val;
- }
- static esp_err_t bsp_i2s_init_speaker(i2s_port_t i2s_num, uint32_t sample_rate, i2s_slot_mode_t channel_format, i2s_data_bit_width_t bits_per_chan)
- {
- esp_err_t ret_val = ESP_OK;
- i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(i2s_num, I2S_ROLE_SLAVE);
- ret_val |= i2s_new_channel(&tx_chan_cfg, &txHandle, NULL);
- i2s_std_config_t tx_std_cfg = I2S_CONFIG_DEFAULT_TX(sample_rate, channel_format, bits_per_chan);
- // speaker is set to mono
- tx_std_cfg.slot_cfg.slot_mask = I2S_STD_SLOT_RIGHT;
- ret_val |= i2s_channel_init_std_mode(txHandle, &tx_std_cfg);
- return ret_val;
- }
- void app_main(void)
- {
- .
- .
- .
- err = bsp_i2s_init_microphone(I2S_NUM_1, 16000, I2S_SLOT_MODE_STEREO, I2S_DATA_BIT_WIDTH_32BIT);
- err = bsp_i2s_init_speaker(I2S_NUM_0, 16000, I2S_SLOT_MODE_MONO, I2S_DATA_BIT_WIDTH_16BIT);
- .
- .
- .
- }