components/driver/i2s.c
/**
 * @brief   Initialize I2S driver configurations
 *
 * @param   i2s_num     I2S device number
 * @param   i2s_config  I2S configurations - see i2s_config_t struct
 * @return
 *      - ESP_OK        I2S initialize success
 *      - ESP_ERR_INVALID_ARG   No channel enabled in multiple channel format
 */
@brief   Initialize I2S driver configurations
@param   i2s_num     I2S device number
@param   i2s_config  I2S configurations - see i2s_config_t struct
@return
- ESP_OK        I2S initialize success
- ESP_ERR_INVALID_ARG   No channel enabled in multiple channel format
@brief Initialize I2S driver configurations @param i2s_num I2S device number @param i2s_config I2S configurations - see i2s_config_t struct @return - ESP_OK I2S initialize success - ESP_ERR_INVALID_ARG No channel enabled in multiple channel format
static esp_err_t i2s_driver_init(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
{
    ESP_RETURN_ON_FALSE(i2s_config, ESP_ERR_INVALID_ARG, TAG, "The pointer of I2S configuration structure is NULL");
    p_i2s[i2s_num]->i2s_num = i2s_num;
    p_i2s[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
    p_i2s[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
    p_i2s[i2s_num]->last_buf_size = 0;
    p_i2s[i2s_num]->use_apll = i2s_config->use_apll;
    p_i2s[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
    p_i2s[i2s_num]->mclk_multiple = i2s_config->mclk_multiple;
    p_i2s[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
    p_i2s[i2s_num]->hal_cfg.mode = i2s_config->mode;
    p_i2s[i2s_num]->hal_cfg.sample_rate = i2s_config->sample_rate;
    p_i2s[i2s_num]->hal_cfg.comm_fmt = i2s_config->communication_format;
    p_i2s[i2s_num]->hal_cfg.chan_fmt = i2s_config->channel_format;
    p_i2s[i2s_num]->hal_cfg.sample_bits = i2s_config->bits_per_sample;
    p_i2s[i2s_num]->hal_cfg.chan_bits = (uint32_t)i2s_config->bits_per_chan < (uint32_t)i2s_config->bits_per_sample ?
                                        (uint32_t)i2s_config->bits_per_sample : (uint32_t)i2s_config->bits_per_chan;
#if SOC_I2S_SUPPORTS_TDM
    p_i2s[i2s_num]->hal_cfg.left_align    = i2s_config->left_align;
    p_i2s[i2s_num]->hal_cfg.big_edin      = i2s_config->big_edin;
    p_i2s[i2s_num]->hal_cfg.bit_order_msb = i2s_config->bit_order_msb;
    p_i2s[i2s_num]->hal_cfg.skip_msk      = i2s_config->skip_msk;
    switch (i2s_config->channel_format) {
    case I2S_CHANNEL_FMT_RIGHT_LEFT:    
    case I2S_CHANNEL_FMT_ALL_RIGHT:     
    case I2S_CHANNEL_FMT_ALL_LEFT:
        p_i2s[i2s_num]->hal_cfg.chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1;
        p_i2s[i2s_num]->hal_cfg.total_chan = 2;
        break;
    case I2S_CHANNEL_FMT_ONLY_RIGHT:    
    case I2S_CHANNEL_FMT_ONLY_LEFT:
        p_i2s[i2s_num]->hal_cfg.chan_mask = I2S_TDM_ACTIVE_CH0;
        p_i2s[i2s_num]->hal_cfg.total_chan = 2;
        break;
    case I2S_CHANNEL_FMT_MULTIPLE:
        ESP_RETURN_ON_FALSE(i2s_config->chan_mask, ESP_ERR_INVALID_ARG, TAG, "i2s all channel are disabled");
        p_i2s[i2s_num]->hal_cfg.chan_mask = i2s_config->chan_mask;
        uint32_t max_channel = i2s_get_max_channel_num(p_i2s[i2s_num]->hal_cfg.chan_mask);
        p_i2s[i2s_num]->hal_cfg.total_chan    = p_i2s[i2s_num]->hal_cfg.total_chan < max_channel ? max_channel :
                                                p_i2s[i2s_num]->hal_cfg.total_chan;
        break;
    default:
        ESP_RETURN_ON_FALSE(false, ESP_ERR_INVALID_ARG, TAG, "wrong i2s channel format, going to uninstall i2s");
    }
    p_i2s[i2s_num]->hal_cfg.active_chan   = i2s_get_active_channel_num(&p_i2s[i2s_num]->hal_cfg);
#else
    p_i2s[i2s_num]->hal_cfg.active_chan   = i2s_get_active_channel_num(&p_i2s[i2s_num]->hal_cfg);
    p_i2s[i2s_num]->hal_cfg.total_chan    = 2;
#endif
    return ESP_OK;
}
static esp_err_t i2s_driver_init ( i2s_port_t i2s_num , const i2s_config_t * i2s_config ) { ESP_RETURN_ON_FALSE ( i2s_config , ESP_ERR_INVALID_ARG , TAG , " " ) ; p_i2s [ i2s_num ] -> i2s_num = i2s_num ; p_i2s [ i2s_num ] -> dma_buf_count = i2s_config -> dma_buf_count ; p_i2s [ i2s_num ] -> dma_buf_len = i2s_config -> dma_buf_len ; p_i2s [ i2s_num ] -> last_buf_size = 0 ; p_i2s [ i2s_num ] -> use_apll = i2s_config -> use_apll ; p_i2s [ i2s_num ] -> fixed_mclk = i2s_config -> fixed_mclk ; p_i2s [ i2s_num ] -> mclk_multiple = i2s_config -> mclk_multiple ; p_i2s [ i2s_num ] -> tx_desc_auto_clear = i2s_config -> tx_desc_auto_clear ; p_i2s [ i2s_num ] -> hal_cfg . mode = i2s_config -> mode ; p_i2s [ i2s_num ] -> hal_cfg . sample_rate = i2s_config -> sample_rate ; p_i2s [ i2s_num ] -> hal_cfg . comm_fmt = i2s_config -> communication_format ; p_i2s [ i2s_num ] -> hal_cfg . chan_fmt = i2s_config -> channel_format ; p_i2s [ i2s_num ] -> hal_cfg . sample_bits = i2s_config -> bits_per_sample ; p_i2s [ i2s_num ] -> hal_cfg . chan_bits = ( uint32_t ) i2s_config -> bits_per_chan < ( uint32_t ) i2s_config -> bits_per_sample ? ( uint32_t ) i2s_config -> bits_per_sample : ( uint32_t ) i2s_config -> bits_per_chan ; #if SOC_I2S_SUPPORTS_TDM p_i2s [ i2s_num ] -> hal_cfg . left_align = i2s_config -> left_align ; p_i2s [ i2s_num ] -> hal_cfg . big_edin = i2s_config -> big_edin ; p_i2s [ i2s_num ] -> hal_cfg . bit_order_msb = i2s_config -> bit_order_msb ; p_i2s [ i2s_num ] -> hal_cfg . skip_msk = i2s_config -> skip_msk ; switch ( i2s_config -> channel_format ) { case I2S_CHANNEL_FMT_RIGHT_LEFT : case I2S_CHANNEL_FMT_ALL_RIGHT : case I2S_CHANNEL_FMT_ALL_LEFT : p_i2s [ i2s_num ] -> hal_cfg . chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1 ; p_i2s [ i2s_num ] -> hal_cfg . total_chan = 2 ; break ; case I2S_CHANNEL_FMT_ONLY_RIGHT : case I2S_CHANNEL_FMT_ONLY_LEFT : p_i2s [ i2s_num ] -> hal_cfg . chan_mask = I2S_TDM_ACTIVE_CH0 ; p_i2s [ i2s_num ] -> hal_cfg . total_chan = 2 ; break ; case I2S_CHANNEL_FMT_MULTIPLE : ESP_RETURN_ON_FALSE ( i2s_config -> chan_mask , ESP_ERR_INVALID_ARG , TAG , " " ) ; p_i2s [ i2s_num ] -> hal_cfg . chan_mask = i2s_config -> chan_mask ; uint32_t max_channel = i2s_get_max_channel_num ( p_i2s [ i2s_num ] -> hal_cfg . chan_mask ) ; p_i2s [ i2s_num ] -> hal_cfg . total_chan = p_i2s [ i2s_num ] -> hal_cfg . total_chan < max_channel ? max_channel : p_i2s [ i2s_num ] -> hal_cfg . total_chan ; break ; default : ESP_RETURN_ON_FALSE ( false , ESP_ERR_INVALID_ARG , TAG , " " ) ; } p_i2s [ i2s_num ] -> hal_cfg . active_chan = i2s_get_active_channel_num ( & p_i2s [ i2s_num ] -> hal_cfg ) ; #else p_i2s [ i2s_num ] -> hal_cfg . active_chan = i2s_get_active_channel_num ( & p_i2s [ i2s_num ] -> hal_cfg ) ; p_i2s [ i2s_num ] -> hal_cfg . total_chan = 2 ; #endif return ESP_OK ; }
@brief   Initialize I2S driver configurations
 @param   i2s_num     I2S device number
 @param   i2s_config  I2S configurations - see i2s_config_t struct
 @return
      - ESP_OK        I2S initialize success
      - ESP_ERR_INVALID_ARG   No channel enabled in multiple channel format
@brief Initialize I2S driver configurations @param i2s_num I2S device number @param i2s_config I2S configurations - see i2s_config_t struct @return - ESP_OK I2S initialize success - ESP_ERR_INVALID_ARG No channel enabled in multiple channel format
/* I2S driver configuration assignment */ /* I2S HAL configuration assignment */ /* I2S HAL TDM configuration assignment */ /* Set chan_mask according to channel format */ // fall through // fall through // fall through /* Get the max actived channel number */ /* If total channel is smaller than max actived channel number then set it to the max active channel number */ /* Calculate actived channel number in channel mask */ /* Calculate actived channel number in channel mask */ /* Total channel number is equal to the actived channel number in non-TDM mode */