完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我用I2S_es8311这个示例时发现,扬声器有时候能够播放音乐有时候不能
我只修改了示例代码中的io口,和给PA的使能的GPIO 以下是代码 /* * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/i2s_std.h" #include "esp_system.h" #include "esp_check.h" #include "es8311.h" /* I2C port and GPIOs */ #define I2C_NUM (0) #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 #define I2C_SCL_IO (GPIO_NUM_6) #define I2C_SDA_IO (GPIO_NUM_7) #else #define I2C_SCL_IO (GPIO_NUM_18) #define I2C_SDA_IO (GPIO_NUM_17) #endif /* I2S port and GPIOs */ #define I2S_NUM (0) #define I2S_MCK_IO (GPIO_NUM_16) #define I2S_BCK_IO (GPIO_NUM_9) #define I2S_WS_IO (GPIO_NUM_45) #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 #define I2S_DO_IO (GPIO_NUM_2) #define I2S_DI_IO (GPIO_NUM_3) #else #define I2S_DO_IO (GPIO_NUM_8) #define I2S_DI_IO (GPIO_NUM_NC) #endif /* Example configurations */ #define EXAMPLE_RECV_BUF_SIZE (2400) #define EXAMPLE_SAMPLE_RATE (16000)//(48000) #define EXAMPLE_MCLK_MULTIPLE (384) // If not using 24-bit data width, 256 should be enough #define EXAMPLE_MCLK_FREQ_HZ (EXAMPLE_SAMPLE_RATE * EXAMPLE_MCLK_MULTIPLE) #define EXAMPLE_VOICE_VOLUME 70//CONFIG_EXAMPLE_VOICE_VOLUME #if CONFIG_EXAMPLE_MODE_ECHO #define EXAMPLE_MIC_GAIN CONFIG_EXAMPLE_MIC_GAIN #endif static const char *TAG = "i2s_es8311"; static const char err_reason[][30] = {"input param is invalid", "operation timeout" }; static i2s_chan_handle_t tx_handle = NULL; static i2s_chan_handle_t rx_handle = NULL; /* Import music file as buffer */ #if CONFIG_EXAMPLE_MODE_MUSIC extern const uint8_t music_pcm_start[] asm("_binary_canon_pcm_start"); extern const uint8_t music_pcm_end[] asm("_binary_canon_pcm_end"); #endif static esp_err_t es8311_codec_init(void) { /* Initialize I2C peripheral */ i2c_config_t es_i2c_cfg = { .sda_io_num = I2C_SDA_IO, .scl_io_num = I2C_SCL_IO, .mode = I2C_MODE_MASTER, .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE, .master.clk_speed = 100000, }; ESP_RETURN_ON_ERROR(i2c_param_config(I2C_NUM, &es_i2c_cfg), TAG, "config i2c failed"); ESP_RETURN_ON_ERROR(i2c_driver_install(I2C_NUM, I2C_MODE_MASTER, 0, 0, 0), TAG, "install i2c driver failed"); /* Initialize es8311 codec */ es8311_handle_t es_handle = es8311_create(I2C_NUM, ES8311_ADDRRES_0); ESP_RETURN_ON_FALSE(es_handle, ESP_FAIL, TAG, "es8311 create failed"); es8311_clock_config_t es_clk = { .mclk_inverted = false, .sclk_inverted = false, .mclk_from_mclk_pin = true, .mclk_frequency = EXAMPLE_MCLK_FREQ_HZ, .sample_frequency = EXAMPLE_SAMPLE_RATE }; ESP_ERROR_CHECK(es8311_init(es_handle, &es_clk, ES8311_RESOLUTION_16, ES8311_RESOLUTION_16)); ESP_RETURN_ON_ERROR(es8311_sample_frequency_config(es_handle, EXAMPLE_SAMPLE_RATE * EXAMPLE_MCLK_MULTIPLE, EXAMPLE_SAMPLE_RATE), TAG, "set es8311 sample frequency failed"); ESP_RETURN_ON_ERROR(es8311_voice_volume_set(es_handle, EXAMPLE_VOICE_VOLUME, NULL), TAG, "set es8311 volume failed"); ESP_RETURN_ON_ERROR(es8311_microphone_config(es_handle, false), TAG, "set es8311 microphone failed"); #if CONFIG_EXAMPLE_MODE_ECHO ESP_RETURN_ON_ERROR(es8311_microphone_gain_set(es_handle, EXAMPLE_MIC_GAIN), TAG, "set es8311 microphone gain faield"); #endif return ESP_OK; } static esp_err_t i2s_driver_init(void) { i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM, I2S_ROLE_MASTER); chan_cfg.auto_clear = true; // Auto clear the legacy data in the DMA buffer ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle)); i2s_std_config_t std_cfg = { .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(EXAMPLE_SAMPLE_RATE), .slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO), .gpio_cfg = { .mclk = I2S_MCK_IO, .bclk = I2S_BCK_IO, .ws = I2S_WS_IO, .dout = I2S_DO_IO, .din = I2S_DI_IO, .invert_flags = { .mclk_inv = false, .bclk_inv = false, .ws_inv = false, }, }, }; std_cfg.clk_cfg.mclk_multiple = EXAMPLE_MCLK_MULTIPLE; ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle, &std_cfg)); ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle, &std_cfg)); ESP_ERROR_CHECK(i2s_channel_enable(tx_handle)); ESP_ERROR_CHECK(i2s_channel_enable(rx_handle)); return ESP_OK; } #if CONFIG_EXAMPLE_MODE_MUSIC static void i2s_music(void *args) { esp_err_t ret = ESP_OK; size_t bytes_write = 0; while (1) { vTaskDelay(500 / portTICK_PERIOD_MS); /* Write music to earphone */ ret = i2s_channel_write(tx_handle, music_pcm_start, music_pcm_end - music_pcm_start, &bytes_write, portMAX_DELAY); if (ret != ESP_OK) { /* Since we set timeout to 'portMAX_DELAY' in 'i2s_channel_write' so you won't reach here unless you set other timeout value, if timeout detected, it means write operation failed. */ ESP_LOGE(TAG, "[music] i2s write failed, %s", err_reason[ret == ESP_ERR_TIMEOUT]); abort(); } if (bytes_write > 0) { ESP_LOGI(TAG, "[music] i2s music played, %d bytes are written.", bytes_write); } else { ESP_LOGE(TAG, "[music] i2s music play falied."); abort(); } vTaskDelay(500 / portTICK_PERIOD_MS); } vTaskDelete(NULL); } #else static void i2s_echo(void *args) { int *mic_data = malloc(EXAMPLE_RECV_BUF_SIZE); if (!mic_data) { ESP_LOGE(TAG, "[echo] No memory for read data buffer"); abort(); } esp_err_t ret = ESP_OK; size_t bytes_read = 0; size_t bytes_write = 0; ESP_LOGI(TAG, "[echo] Echo start"); while (1) { memset(mic_data, 0, EXAMPLE_RECV_BUF_SIZE); /* Read sample data from mic */ ret = i2s_channel_read(rx_handle, mic_data, EXAMPLE_RECV_BUF_SIZE, &bytes_read, 1000); if (ret != ESP_OK) { ESP_LOGE(TAG, "[echo] i2s read failed, %s", err_reason[ret == ESP_ERR_TIMEOUT]); abort(); } /* Write sample data to earphone */ ret = i2s_channel_write(tx_handle, mic_data, EXAMPLE_RECV_BUF_SIZE, &bytes_write, 1000); if (ret != ESP_OK) { ESP_LOGE(TAG, "[echo] i2s write failed, %s", err_reason[ret == ESP_ERR_TIMEOUT]); abort(); } if (bytes_read != bytes_write) { ESP_LOGW(TAG, "[echo] %d bytes read but only %d bytes are written", bytes_read, bytes_write); } } vTaskDelete(NULL); } #endif void PA_CTRL(void) { ESP_LOGI(TAG, "configured PA_CTRL_gpio"); gpio_reset_pin(GPIO_NUM_48); gpio_set_direction(GPIO_NUM_48,GPIO_MODE_OUTPUT); gpio_set_level(GPIO_NUM_48, 1); } void app_main(void) { vTaskDelay(2000 / portTICK_PERIOD_MS); printf("i2s es8311 codec example startn-----------------------------n"); /* Initialize i2s peripheral */ if (i2s_driver_init() != ESP_OK) { ESP_LOGE(TAG, "i2s driver init failed"); abort(); } else { ESP_LOGI(TAG, "i2s driver init success"); } /* Initialize i2c peripheral and config es8311 codec by i2c */ if (es8311_codec_init() != ESP_OK) { ESP_LOGE(TAG, "es8311 codec init failed"); abort(); } else { ESP_LOGI(TAG, "es8311 codec init success"); } #if CONFIG_EXAMPLE_MODE_MUSIC /* Play a piece of music in music mode */ ESP_LOGI(TAG, "set PA_CTRL 1"); PA_CTRL(); vTaskDelay(1000 / portTICK_PERIOD_MS); xTaskCreate(i2s_music, "i2s_music", 4096, NULL, 5, NULL); #else /* Echo the sound from MIC in echo mode */ xTaskCreate(i2s_echo, "i2s_echo", 8192, NULL, 5, NULL); #endif } |
|
相关推荐
1个回答
|
|
您提到只修改了示例代码中的IO口和给PA的使能的GPIO。
为了解决这个问题,我们可以尝试以下步骤: 1. 确保硬件连接正确:首先,请确保您的ESP32-S3-Korvo-2 V3.0开发板与ES8311音频编解码器之间的硬件连接正确。检查I2S数据线、时钟线、电源线等是否连接正确。 2. 检查GPIO配置:确保您修改的GPIO配置正确。例如,确保您为I2S数据、时钟和WS(Word Select)信号分配了正确的GPIO引脚。同时,确保您为PA(功率放大器)的使能信号分配了正确的GPIO引脚。 3. 检查电源:确保您的开发板和音频编解码器都有足够的电源供应。如果电源不稳定,可能会导致音频播放出现问题。 4. 检查代码逻辑:检查您的代码逻辑,确保在初始化I2S和ES8311之前,您已经正确配置了GPIO。同时,确保在播放音乐之前,您已经正确初始化了I2S驱动和ES8311。 5. 更新示例代码:如果您使用的是旧版本的示例代码,请尝试从Espressif的官方GitHub仓库获取最新版本的示例代码,并根据您的硬件配置进行相应的修改。 6. 检查音频文件:确保您使用的音频文件格式和采样率与您的I2S配置相匹配。如果音频文件格式或采样率不正确,可能会导致播放问题。 7. 调试和日志:在代码中添加调试信息和日志输出,以便更好地了解问题所在。您可以使用printf或ESP_LOGI等函数来输出调试信息。 8. 检查I2S驱动:确保您使用的I2S驱动与您的ESP32-S3-Korvo-2 V3.0开发板和ES8311音频编解码器兼容。如果驱动不兼容,可能会导致播放问题。 |
|
|
|
只有小组成员才能发言,加入小组>>
350 浏览 0 评论
1196 浏览 1 评论
588浏览 6评论
484浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
468浏览 5评论
467浏览 4评论
443浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 10:30 , Processed in 0.638264 second(s), Total 47, Slave 41 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号