I'm currently trying to get this example to work on my custom-board: https://github.com/espressif/esp-adf/bl ... _example.c
I changed it up a litte. The code is below.
My problem is, that the controls (prev, play, pause, next) stop working after a short time. I can use them like 2-10 times and then the connected device doesn't react to them anymore. Volume up and down always work fine, even after the others stopped working.
The functions (periph_bt_avrc_next, ect.) always return ESP_OK.
There also isn't a single debug/error message on the UART (except for my own ones created with debug_writeLine()).
Unfortunately, I have no idea how to search for or find the problem.
Thank you!
Edit:
If I just reconnect my bluetooth device it starts working again for a few times. ESP32 didn't get restarted in that time.
It happens with all bluetooth devices connected to the ESP32.
Code: music.c Select all
#include "music.h"
#include "debug.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
static esp_periph_handle_t bt_periph = NULL;
audio_event_iface_handle_t music_evt;
audio_element_handle_t bt_stream_reader, i2s_stream_writer;
bool music_isPaused = false;
void music_init(void)
{
audio_pipeline_handle_t pipeline;
debug_writeLine("music: [ 1 ] Init Bluetooth");
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT));
ESP_ERROR_CHECK(esp_bluedroid_init());
ESP_ERROR_CHECK(esp_bluedroid_enable());
esp_bt_dev_set_device_name("Play me daddy :)");
esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
debug_writeLine("music: [ 2 ] Start codec chip");
audio_board_handle_t board_handle = audio_board_init();
audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);
debug_writeLine("music: [ 3 ] Create audio pipeline for playback");
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
pipeline = audio_pipeline_init(&pipeline_cfg);
debug_writeLine("music: [4] Create i2s stream to write data to codec chip");
i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
i2s_cfg.type = AUDIO_STREAM_WRITER;
i2s_cfg.i2s_config.use_apll = false;
i2s_cfg.i2s_config.intr_alloc_flags = ESP_INTR_FLAG_IRAM,
i2s_stream_writer = i2s_stream_init(&i2s_cfg);
debug_writeLine("music: [4.1] Get Bluetooth stream");
a2dp_stream_config_t a2dp_config = {
.type = AUDIO_STREAM_READER,
.user_callback = { 0 },
.audio_hal = board_handle->audio_hal,
};
bt_stream_reader = a2dp_stream_init(&a2dp_config);
debug_writeLine("music: [4.2] Register all elements to audio pipeline");
audio_pipeline_register(pipeline, bt_stream_reader, "bt");
audio_pipeline_register(pipeline, i2s_stream_writer, "i2s");
debug_writeLine("music: [4.3] Link it together [Bluetooth]-->bt_stream_reader-->i2s_stream_writer-->[codec_chip]");
const char *link_tag[2] = { "bt", "i2s" };
audio_pipeline_link(pipeline, &link_tag[0], 2);
debug_writeLine("music: [ 5 ] Initialize peripherals");
esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
debug_writeLine("music: [5.2] Create Bluetooth peripheral");
bt_periph = bt_create_periph();
debug_writeLine("music: [5.3] Start all peripherals");
esp_periph_start(set, bt_periph);
debug_writeLine("music: [ 6 ] Set up event listener");
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
music_evt = audio_event_iface_init(&evt_cfg);
debug_writeLine("music: [6.1] Listening event from all elements of pipeline");
audio_pipeline_set_listener(pipeline, music_evt);
debug_writeLine("music: [ 7 ] Start audio_pipeline");
audio_pipeline_run(pipeline);
debug_writeLine("music: [ 8 ] Listen for all pipeline events");
xTaskCreate(music_mainTask, "music_mainTask", 4096, NULL, 5, NULL);
}
void music_mainTask(void *pvParameters)
{
while (true)
{
audio_event_iface_msg_t msg;
esp_err_t ret = audio_event_iface_listen(music_evt, &msg, portMAX_DELAY);
debug_writeLine("TEST");
if (ret != ESP_OK)
{
debug_writeLine("music: [ * ] Event interface error : %d", ret);
continue;
}
if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) bt_stream_reader
&& msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO)
{
audio_element_info_t music_info = { 0 };
audio_element_getinfo(bt_stream_reader, &music_info);
debug_writeLine("music: [ * ] Receive music info from Bluetooth, sample_rates=%d, bits=%d, ch=%d",
music_info.sample_rates,
music_info.bits,
music_info.channels);
audio_element_setinfo(i2s_stream_writer, &music_info);
i2s_stream_set_clk(i2s_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
continue;
}
/* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
&& msg.cmd == AEL_MSG_CMD_REPORT_STATUS
&& (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED)))
{
debug_writeLine("music: [ * ] Stop event received");
break;
}
}
}
void music_volUp(void)
{
debug_writeLine("music: Vol+");
esp_err_t err = periph_bt_volume_up(bt_periph);
debug_writeLine("music: err was %d", err);
}
void music_volDown(void)
{
debug_writeLine("music: Vol-");
esp_err_t err = periph_bt_volume_down(bt_periph);
debug_writeLine("music: err was %d", err);
}
void music_nextSong(void)
{
debug_writeLine("music: Next");
esp_err_t err = periph_bt_avrc_next(bt_periph);
debug_writeLine("music: err was %d", err);
}
void music_prevSong(void)
{
debug_writeLine("music: Prev");
esp_err_t err = periph_bt_avrc_prev(bt_periph);
debug_writeLine("music: err was %d", err);
}
void music_pausePlay(void)
{
if (music_isPaused)
{
debug_writeLine("music: Play");
esp_err_t err = periph_bt_play(bt_periph);
debug_writeLine("music: err was %d", err);
}
else
{
debug_writeLine("music: Pause");
esp_err_t err = periph_bt_pause(bt_periph);
debug_writeLine("music: err was %d", err);
}
music_isPaused = !music_isPaused;
}