@@ -42,6 +42,7 @@ static i2c_master_bus_handle_t i2c_handle = NULL; // I2C Handle
4242static i2s_chan_handle_t i2s_tx_chan = NULL ;
4343static i2s_chan_handle_t i2s_rx_chan = NULL ;
4444static const audio_codec_data_if_t * i2s_data_if = NULL ; /* Codec data interface */
45+ #define BSP_ES7210_CODEC_ADDR ES7210_CODEC_DEFAULT_ADDR
4546
4647/* Can be used for `i2s_std_gpio_config_t` and/or `i2s_std_config_t` initialization */
4748#define BSP_I2S_GPIO_CFG \
@@ -188,6 +189,7 @@ esp_err_t bsp_spiffs_unmount(void)
188189
189190esp_err_t bsp_audio_init (const i2s_std_config_t * i2s_config )
190191{
192+ esp_err_t ret = ESP_FAIL ;
191193 if (i2s_tx_chan && i2s_rx_chan )
192194 {
193195 /* Audio was initialized before */
@@ -197,7 +199,7 @@ esp_err_t bsp_audio_init(const i2s_std_config_t *i2s_config)
197199 /* Setup I2S peripheral */
198200 i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG (CONFIG_BSP_I2S_NUM , I2S_ROLE_MASTER );
199201 chan_cfg .auto_clear = true; // Auto clear the legacy data in the DMA buffer
200- ESP_ERROR_CHECK (i2s_new_channel (& chan_cfg , & i2s_tx_chan , & i2s_rx_chan ));
202+ BSP_ERROR_CHECK_RETURN_ERR (i2s_new_channel (& chan_cfg , & i2s_tx_chan , & i2s_rx_chan ));
201203
202204 /* Setup I2S channels */
203205 const i2s_std_config_t std_cfg_default = BSP_I2S_DUPLEX_MONO_CFG (22050 );
@@ -209,34 +211,46 @@ esp_err_t bsp_audio_init(const i2s_std_config_t *i2s_config)
209211
210212 if (i2s_tx_chan != NULL )
211213 {
212- ESP_ERROR_CHECK (i2s_channel_init_std_mode (i2s_tx_chan , p_i2s_cfg ));
213- ESP_ERROR_CHECK (i2s_channel_enable (i2s_tx_chan ));
214+ ESP_GOTO_ON_ERROR (i2s_channel_init_std_mode (i2s_tx_chan , p_i2s_cfg ), err , TAG , "I2S channel initialization failed" );
215+ ESP_GOTO_ON_ERROR (i2s_channel_enable (i2s_tx_chan ), err , TAG , "I2S enabling failed" );
214216 }
215-
216217 if (i2s_rx_chan != NULL )
217218 {
218- ESP_ERROR_CHECK (i2s_channel_init_std_mode (i2s_rx_chan , p_i2s_cfg ));
219- ESP_ERROR_CHECK (i2s_channel_enable (i2s_rx_chan ));
219+ ESP_GOTO_ON_ERROR (i2s_channel_init_std_mode (i2s_rx_chan , p_i2s_cfg ), err , TAG , "I2S channel initialization failed" );
220+ ESP_GOTO_ON_ERROR (i2s_channel_enable (i2s_rx_chan ), err , TAG , "I2S enabling failed" );
220221 }
221222
222223 audio_codec_i2s_cfg_t i2s_cfg = {
223224 .port = CONFIG_BSP_I2S_NUM ,
224- .tx_handle = i2s_tx_chan ,
225225 .rx_handle = i2s_rx_chan ,
226+ .tx_handle = i2s_tx_chan ,
226227 };
227228 i2s_data_if = audio_codec_new_i2s_data (& i2s_cfg );
229+ BSP_NULL_CHECK_GOTO (i2s_data_if , err );
228230
229231 return ESP_OK ;
232+
233+ err :
234+ if (i2s_tx_chan )
235+ {
236+ i2s_del_channel (i2s_tx_chan );
237+ }
238+ if (i2s_rx_chan )
239+ {
240+ i2s_del_channel (i2s_rx_chan );
241+ }
242+
243+ return ret ;
230244}
231245
232246esp_codec_dev_handle_t bsp_audio_codec_speaker_init (void )
233247{
234248 if (i2s_data_if == NULL )
235249 {
236250 /* Initilize I2C */
237- ESP_ERROR_CHECK (bsp_i2c_init ());
251+ BSP_ERROR_CHECK_RETURN_NULL (bsp_i2c_init ());
238252 /* Configure I2S peripheral and Power Amplifier */
239- ESP_ERROR_CHECK (bsp_audio_init (NULL ));
253+ BSP_ERROR_CHECK_RETURN_NULL (bsp_audio_init (NULL ));
240254 }
241255 assert (i2s_data_if );
242256
@@ -248,7 +262,7 @@ esp_codec_dev_handle_t bsp_audio_codec_speaker_init(void)
248262 .bus_handle = i2c_handle ,
249263 };
250264 const audio_codec_ctrl_if_t * i2c_ctrl_if = audio_codec_new_i2c_ctrl (& i2c_cfg );
251- assert (i2c_ctrl_if );
265+ BSP_NULL_CHECK (i2c_ctrl_if , NULL );
252266
253267 esp_codec_dev_hw_gain_t gain = {
254268 .pa_voltage = 5.0 ,
@@ -258,7 +272,7 @@ esp_codec_dev_handle_t bsp_audio_codec_speaker_init(void)
258272 es8311_codec_cfg_t es8311_cfg = {
259273 .ctrl_if = i2c_ctrl_if ,
260274 .gpio_if = gpio_if ,
261- .codec_mode = ESP_CODEC_DEV_TYPE_OUT ,
275+ .codec_mode = ESP_CODEC_DEV_WORK_MODE_DAC ,
262276 .pa_pin = BSP_POWER_AMP_IO ,
263277 .pa_reverted = false,
264278 .master_mode = false,
@@ -269,10 +283,10 @@ esp_codec_dev_handle_t bsp_audio_codec_speaker_init(void)
269283 .hw_gain = gain ,
270284 };
271285 const audio_codec_if_t * es8311_dev = es8311_codec_new (& es8311_cfg );
272- assert (es8311_dev );
286+ BSP_NULL_CHECK (es8311_dev , NULL );
273287
274288 esp_codec_dev_cfg_t codec_dev_cfg = {
275- .dev_type = ESP_CODEC_DEV_TYPE_IN_OUT ,
289+ .dev_type = ESP_CODEC_DEV_TYPE_OUT ,
276290 .codec_if = es8311_dev ,
277291 .data_if = i2s_data_if ,
278292 };
@@ -284,50 +298,32 @@ esp_codec_dev_handle_t bsp_audio_codec_microphone_init(void)
284298 if (i2s_data_if == NULL )
285299 {
286300 /* Initilize I2C */
287- ESP_ERROR_CHECK (bsp_i2c_init ());
301+ BSP_ERROR_CHECK_RETURN_NULL (bsp_i2c_init ());
288302 /* Configure I2S peripheral and Power Amplifier */
289- ESP_ERROR_CHECK (bsp_audio_init (NULL ));
303+ BSP_ERROR_CHECK_RETURN_NULL (bsp_audio_init (NULL ));
290304 }
291305 assert (i2s_data_if );
292306
293- const audio_codec_gpio_if_t * gpio_if = audio_codec_new_gpio ();
294-
295307 audio_codec_i2c_cfg_t i2c_cfg = {
296308 .port = BSP_I2C_NUM ,
297- .addr = ES8311_CODEC_DEFAULT_ADDR ,
309+ .addr = BSP_ES7210_CODEC_ADDR ,
298310 .bus_handle = i2c_handle ,
299311 };
300312 const audio_codec_ctrl_if_t * i2c_ctrl_if = audio_codec_new_i2c_ctrl (& i2c_cfg );
301- assert (i2c_ctrl_if );
302-
303- esp_codec_dev_hw_gain_t gain = {
304- .pa_voltage = 5.0 ,
305- .codec_dac_voltage = 3.3 ,
306- };
313+ BSP_NULL_CHECK (i2c_ctrl_if , NULL );
307314
308- es8311_codec_cfg_t es8311_cfg = {
315+ es7210_codec_cfg_t es7210_cfg = {
309316 .ctrl_if = i2c_ctrl_if ,
310- .gpio_if = gpio_if ,
311- .codec_mode = ESP_CODEC_DEV_WORK_MODE_BOTH ,
312- .pa_pin = BSP_POWER_AMP_IO ,
313- .pa_reverted = false,
314- .master_mode = false,
315- .use_mclk = true,
316- .digital_mic = false,
317- .invert_mclk = false,
318- .invert_sclk = false,
319- .hw_gain = gain ,
320317 };
318+ const audio_codec_if_t * es7210_dev = es7210_codec_new (& es7210_cfg );
319+ BSP_NULL_CHECK (es7210_dev , NULL );
321320
322- const audio_codec_if_t * es8311_dev = es8311_codec_new (& es8311_cfg );
323- assert (es8311_dev );
324-
325- esp_codec_dev_cfg_t codec_es8311_dev_cfg = {
321+ esp_codec_dev_cfg_t codec_es7210_dev_cfg = {
326322 .dev_type = ESP_CODEC_DEV_TYPE_IN ,
327- .codec_if = es8311_dev ,
323+ .codec_if = es7210_dev ,
328324 .data_if = i2s_data_if ,
329325 };
330- return esp_codec_dev_new (& codec_es8311_dev_cfg );
326+ return esp_codec_dev_new (& codec_es7210_dev_cfg );
331327}
332328
333329// Bit number used to represent command and parameter
0 commit comments