i2s_stream.c有需要改进的地方(不判断返回值导致reboot)
Posted: Fri Dec 20, 2019 8:46 am
audio_element_handle_t i2s_stream_init(i2s_stream_cfg_t *config);函数在调用i2s_driver_install()时未判断返回值,在i2s驱动安装失败时,后面的程序执行导致reboot!!!!
代码中我自己添加了保护:
//modify by li begin 2019/11/13
if(i2s_driver_install(i2s->config.i2s_port, &i2s->config.i2s_config, 0, NULL) != ESP_OK){
audio_free(i2s);
return NULL;
}
//modify by li end 2019/11/13[/color]
代码中我自己添加了保护:
//modify by li begin 2019/11/13
if(i2s_driver_install(i2s->config.i2s_port, &i2s->config.i2s_config, 0, NULL) != ESP_OK){
audio_free(i2s);
return NULL;
}
//modify by li end 2019/11/13[/color]
- audio_element_handle_t i2s_stream_init(i2s_stream_cfg_t *config)
- {
- audio_element_cfg_t cfg = DEFAULT_AUDIO_ELEMENT_CONFIG();
- audio_element_handle_t el;
- cfg.open = _i2s_open;
- cfg.close = _i2s_close;
- cfg.process = _i2s_process;
- cfg.destroy = _i2s_destroy;
- cfg.task_stack = config->task_stack;
- cfg.task_prio = config->task_prio;
- cfg.task_core = config->task_core;
- cfg.out_rb_size = config->out_rb_size;
- cfg.multi_out_rb_num = config->multi_out_num;
- cfg.tag = "iis";
- cfg.buffer_len = I2S_STREAM_BUF_SIZE;
- i2s_stream_t *i2s = audio_calloc(1, sizeof(i2s_stream_t));
- AUDIO_MEM_CHECK(TAG, i2s, return NULL);
- memcpy(&i2s->config, config, sizeof(i2s_stream_cfg_t));
- i2s->type = config->type;
- i2s->use_alc = config->use_alc;
- i2s->volume = config->volume;
- if (config->type == AUDIO_STREAM_READER) {
- cfg.read = _i2s_read;
- } else if (config->type == AUDIO_STREAM_WRITER) {
- cfg.write = _i2s_write;
- }
- el = audio_element_init(&cfg);
- AUDIO_MEM_CHECK(TAG, el, {
- audio_free(i2s);
- return NULL;
- });
- audio_element_setdata(el, i2s);
- audio_element_info_t info;
- audio_element_getinfo(el, &info);
- info.sample_rates = config->i2s_config.sample_rate;
- info.channels = config->i2s_config.channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
- info.bits = config->i2s_config.bits_per_sample;
- audio_element_setinfo(el, &info);
- [color=#FF0000] //modify by li begin 2019/11/13
- if(i2s_driver_install(i2s->config.i2s_port, &i2s->config.i2s_config, 0, NULL) != ESP_OK){
- audio_free(i2s);
- return NULL;
- }
- //modify by li end 2019/11/13[/color]
- if ((config->i2s_config.mode & I2S_MODE_DAC_BUILT_IN) != 0) {
- i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
- } else {
- i2s_pin_config_t i2s_pin_cfg = {0};
- get_i2s_pins(i2s->config.i2s_port, &i2s_pin_cfg);
- i2s_set_pin(i2s->config.i2s_port, &i2s_pin_cfg);
- }
- i2s_mclk_gpio_select(i2s->config.i2s_port, GPIO_NUM_0);
- return el;
- }