├── README.md ├── code ├── CMakeLists.txt ├── Kconfig.projbuild ├── bt_app_av.c ├── bt_app_av.h ├── bt_app_core.c ├── bt_app_core.h ├── component.mk └── main.c └── esp32_a2dp.pdf /README.md: -------------------------------------------------------------------------------- 1 | # ESP32 Bluetooth Audio Receiver with I2S output and volume control 2 | 3 | I tried to use the A2DP_Sink_Example from Espressif. But there was one big drawback - the volume control over my smartphone didn't work. 4 | Instead I got disturbed sound from the ESP32 if I put the volume on my smartphone a little bit higher. 5 | 6 | I fixed the code in the Espressif-example in order to control the volume over the smartphone once again properly. 7 | 8 | Watch my YouTube video on this: https://youtu.be/4a10Tl5eR30 9 | -------------------------------------------------------------------------------- /code/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | idf_component_register(SRCS "bt_app_av.c" 2 | "bt_app_core.c" 3 | "main.c" 4 | INCLUDE_DIRS ".") -------------------------------------------------------------------------------- /code/Kconfig.projbuild: -------------------------------------------------------------------------------- 1 | menu "A2DP Example Configuration" 2 | 3 | choice EXAMPLE_A2DP_SINK_OUTPUT 4 | prompt "A2DP Sink Output" 5 | default EXAMPLE_A2DP_SINK_OUTPUT_EXTERNAL_I2S 6 | help 7 | Select to use Internal DAC or external I2S driver 8 | 9 | config EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC 10 | bool "Internal DAC" 11 | help 12 | Select this to use Internal DAC sink output 13 | 14 | config EXAMPLE_A2DP_SINK_OUTPUT_EXTERNAL_I2S 15 | bool "External I2S Codec" 16 | help 17 | Select this to use External I2S sink output 18 | 19 | endchoice 20 | 21 | config EXAMPLE_I2S_LRCK_PIN 22 | int "I2S LRCK (WS) GPIO" 23 | default 22 24 | depends on EXAMPLE_A2DP_SINK_OUTPUT_EXTERNAL_I2S 25 | help 26 | GPIO number to use for I2S LRCK(WS) Driver. 27 | 28 | config EXAMPLE_I2S_BCK_PIN 29 | int "I2S BCK GPIO" 30 | default 26 31 | depends on EXAMPLE_A2DP_SINK_OUTPUT_EXTERNAL_I2S 32 | help 33 | GPIO number to use for I2S BCK Driver. 34 | 35 | config EXAMPLE_I2S_DATA_PIN 36 | int "I2S DATA GPIO" 37 | default 25 38 | depends on EXAMPLE_A2DP_SINK_OUTPUT_EXTERNAL_I2S 39 | help 40 | GPIO number to use for I2S Data Driver. 41 | 42 | endmenu 43 | -------------------------------------------------------------------------------- /code/bt_app_av.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | This example code is in the Public Domain (or CC0 licensed, at your option.) 4 | 5 | Unless required by applicable law or agreed to in writing, this 6 | software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 7 | CONDITIONS OF ANY KIND, either express or implied. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "esp_log.h" 15 | 16 | #include "bt_app_core.h" 17 | #include "bt_app_av.h" 18 | #include "esp_bt_main.h" 19 | #include "esp_bt_device.h" 20 | #include "esp_gap_bt_api.h" 21 | #include "esp_a2dp_api.h" 22 | #include "esp_avrc_api.h" 23 | 24 | #include "freertos/FreeRTOS.h" 25 | #include "freertos/task.h" 26 | #include "driver/i2s.h" 27 | 28 | #include "sys/lock.h" 29 | 30 | // AVRCP used transaction label 31 | #define APP_RC_CT_TL_GET_CAPS (0) 32 | #define APP_RC_CT_TL_GET_META_DATA (1) 33 | #define APP_RC_CT_TL_RN_TRACK_CHANGE (2) 34 | #define APP_RC_CT_TL_RN_PLAYBACK_CHANGE (3) 35 | #define APP_RC_CT_TL_RN_PLAY_POS_CHANGE (4) 36 | 37 | /* a2dp event handler */ 38 | static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param); 39 | /* avrc CT event handler */ 40 | static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param); 41 | /* avrc TG event handler */ 42 | static void bt_av_hdl_avrc_tg_evt(uint16_t event, void *p_param); 43 | 44 | static uint32_t s_pkt_cnt = 0; 45 | static esp_a2d_audio_state_t s_audio_state = ESP_A2D_AUDIO_STATE_STOPPED; 46 | static const char *s_a2d_conn_state_str[] = {"Disconnected", "Connecting", "Connected", "Disconnecting"}; 47 | static const char *s_a2d_audio_state_str[] = {"Suspended", "Stopped", "Started"}; 48 | static esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap; 49 | static _lock_t s_volume_lock; 50 | static xTaskHandle s_vcs_task_hdl = NULL; 51 | uint8_t s_volume = 0; 52 | //static uint8_t s_volume = 0; 53 | static bool s_volume_notify; 54 | 55 | /* callback for A2DP sink */ 56 | void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param) 57 | { 58 | switch (event) { 59 | case ESP_A2D_CONNECTION_STATE_EVT: 60 | case ESP_A2D_AUDIO_STATE_EVT: 61 | case ESP_A2D_AUDIO_CFG_EVT: { 62 | bt_app_work_dispatch(bt_av_hdl_a2d_evt, event, param, sizeof(esp_a2d_cb_param_t), NULL); 63 | break; 64 | } 65 | default: 66 | ESP_LOGE(BT_AV_TAG, "Invalid A2DP event: %d", event); 67 | break; 68 | } 69 | } 70 | 71 | void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len) 72 | { 73 | write_ringbuf(data, len); 74 | if (++s_pkt_cnt % 100 == 0) { 75 | ESP_LOGI(BT_AV_TAG, "Audio packet count %u", s_pkt_cnt); 76 | } 77 | } 78 | 79 | void bt_app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param) 80 | { 81 | esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(param); 82 | uint8_t *attr_text = (uint8_t *) malloc (rc->meta_rsp.attr_length + 1); 83 | memcpy(attr_text, rc->meta_rsp.attr_text, rc->meta_rsp.attr_length); 84 | attr_text[rc->meta_rsp.attr_length] = 0; 85 | 86 | rc->meta_rsp.attr_text = attr_text; 87 | } 88 | 89 | void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param) 90 | { 91 | switch (event) { 92 | case ESP_AVRC_CT_METADATA_RSP_EVT: 93 | bt_app_alloc_meta_buffer(param); 94 | /* fall through */ 95 | case ESP_AVRC_CT_CONNECTION_STATE_EVT: 96 | case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: 97 | case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: 98 | case ESP_AVRC_CT_REMOTE_FEATURES_EVT: 99 | case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT: { 100 | bt_app_work_dispatch(bt_av_hdl_avrc_ct_evt, event, param, sizeof(esp_avrc_ct_cb_param_t), NULL); 101 | break; 102 | } 103 | default: 104 | ESP_LOGE(BT_RC_CT_TAG, "Invalid AVRC event: %d", event); 105 | break; 106 | } 107 | } 108 | 109 | void bt_app_rc_tg_cb(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param) 110 | { 111 | switch (event) { 112 | case ESP_AVRC_TG_CONNECTION_STATE_EVT: 113 | case ESP_AVRC_TG_REMOTE_FEATURES_EVT: 114 | case ESP_AVRC_TG_PASSTHROUGH_CMD_EVT: 115 | case ESP_AVRC_TG_SET_ABSOLUTE_VOLUME_CMD_EVT: 116 | case ESP_AVRC_TG_REGISTER_NOTIFICATION_EVT: 117 | bt_app_work_dispatch(bt_av_hdl_avrc_tg_evt, event, param, sizeof(esp_avrc_tg_cb_param_t), NULL); 118 | break; 119 | default: 120 | ESP_LOGE(BT_RC_TG_TAG, "Invalid AVRC event: %d", event); 121 | break; 122 | } 123 | } 124 | 125 | static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param) 126 | { 127 | ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event); 128 | esp_a2d_cb_param_t *a2d = NULL; 129 | switch (event) { 130 | case ESP_A2D_CONNECTION_STATE_EVT: { 131 | a2d = (esp_a2d_cb_param_t *)(p_param); 132 | uint8_t *bda = a2d->conn_stat.remote_bda; 133 | ESP_LOGI(BT_AV_TAG, "A2DP connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]", 134 | s_a2d_conn_state_str[a2d->conn_stat.state], bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); 135 | if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) { 136 | esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); 137 | bt_i2s_task_shut_down(); 138 | } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED){ 139 | esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE); 140 | bt_i2s_task_start_up(); 141 | } 142 | break; 143 | } 144 | case ESP_A2D_AUDIO_STATE_EVT: { 145 | a2d = (esp_a2d_cb_param_t *)(p_param); 146 | ESP_LOGI(BT_AV_TAG, "A2DP audio state: %s", s_a2d_audio_state_str[a2d->audio_stat.state]); 147 | s_audio_state = a2d->audio_stat.state; 148 | if (ESP_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) { 149 | s_pkt_cnt = 0; 150 | } 151 | break; 152 | } 153 | case ESP_A2D_AUDIO_CFG_EVT: { 154 | a2d = (esp_a2d_cb_param_t *)(p_param); 155 | ESP_LOGI(BT_AV_TAG, "A2DP audio stream configuration, codec type %d", a2d->audio_cfg.mcc.type); 156 | // for now only SBC stream is supported 157 | if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) { 158 | int sample_rate = 16000; 159 | char oct0 = a2d->audio_cfg.mcc.cie.sbc[0]; 160 | if (oct0 & (0x01 << 6)) { 161 | sample_rate = 32000; 162 | } else if (oct0 & (0x01 << 5)) { 163 | sample_rate = 44100; 164 | } else if (oct0 & (0x01 << 4)) { 165 | sample_rate = 48000; 166 | } 167 | i2s_set_clk(0, sample_rate, 16, 2); 168 | 169 | ESP_LOGI(BT_AV_TAG, "Configure audio player %x-%x-%x-%x", 170 | a2d->audio_cfg.mcc.cie.sbc[0], 171 | a2d->audio_cfg.mcc.cie.sbc[1], 172 | a2d->audio_cfg.mcc.cie.sbc[2], 173 | a2d->audio_cfg.mcc.cie.sbc[3]); 174 | ESP_LOGI(BT_AV_TAG, "Audio player configured, sample rate=%d", sample_rate); 175 | } 176 | break; 177 | } 178 | default: 179 | ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event); 180 | break; 181 | } 182 | } 183 | 184 | static void bt_av_new_track(void) 185 | { 186 | // request metadata 187 | uint8_t attr_mask = ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM | ESP_AVRC_MD_ATTR_GENRE; 188 | esp_avrc_ct_send_metadata_cmd(APP_RC_CT_TL_GET_META_DATA, attr_mask); 189 | 190 | // register notification if peer support the event_id 191 | if (esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap, 192 | ESP_AVRC_RN_TRACK_CHANGE)) { 193 | esp_avrc_ct_send_register_notification_cmd(APP_RC_CT_TL_RN_TRACK_CHANGE, ESP_AVRC_RN_TRACK_CHANGE, 0); 194 | } 195 | } 196 | 197 | static void bt_av_playback_changed(void) 198 | { 199 | if (esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap, 200 | ESP_AVRC_RN_PLAY_STATUS_CHANGE)) { 201 | esp_avrc_ct_send_register_notification_cmd(APP_RC_CT_TL_RN_PLAYBACK_CHANGE, ESP_AVRC_RN_PLAY_STATUS_CHANGE, 0); 202 | } 203 | } 204 | 205 | static void bt_av_play_pos_changed(void) 206 | { 207 | if (esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap, 208 | ESP_AVRC_RN_PLAY_POS_CHANGED)) { 209 | esp_avrc_ct_send_register_notification_cmd(APP_RC_CT_TL_RN_PLAY_POS_CHANGE, ESP_AVRC_RN_PLAY_POS_CHANGED, 10); 210 | } 211 | } 212 | 213 | void bt_av_notify_evt_handler(uint8_t event_id, esp_avrc_rn_param_t *event_parameter) 214 | { 215 | switch (event_id) { 216 | case ESP_AVRC_RN_TRACK_CHANGE: 217 | bt_av_new_track(); 218 | break; 219 | case ESP_AVRC_RN_PLAY_STATUS_CHANGE: 220 | ESP_LOGI(BT_AV_TAG, "Playback status changed: 0x%x", event_parameter->playback); 221 | bt_av_playback_changed(); 222 | break; 223 | case ESP_AVRC_RN_PLAY_POS_CHANGED: 224 | ESP_LOGI(BT_AV_TAG, "Play position changed: %d-ms", event_parameter->play_pos); 225 | bt_av_play_pos_changed(); 226 | break; 227 | } 228 | } 229 | 230 | static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param) 231 | { 232 | ESP_LOGD(BT_RC_CT_TAG, "%s evt %d", __func__, event); 233 | esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(p_param); 234 | switch (event) { 235 | case ESP_AVRC_CT_CONNECTION_STATE_EVT: { 236 | uint8_t *bda = rc->conn_stat.remote_bda; 237 | ESP_LOGI(BT_RC_CT_TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]", 238 | rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); 239 | 240 | if (rc->conn_stat.connected) { 241 | // get remote supported event_ids of peer AVRCP Target 242 | esp_avrc_ct_send_get_rn_capabilities_cmd(APP_RC_CT_TL_GET_CAPS); 243 | } else { 244 | // clear peer notification capability record 245 | s_avrc_peer_rn_cap.bits = 0; 246 | } 247 | break; 248 | } 249 | case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: { 250 | ESP_LOGI(BT_RC_CT_TAG, "AVRC passthrough rsp: key_code 0x%x, key_state %d", rc->psth_rsp.key_code, rc->psth_rsp.key_state); 251 | break; 252 | } 253 | case ESP_AVRC_CT_METADATA_RSP_EVT: { 254 | ESP_LOGI(BT_RC_CT_TAG, "AVRC metadata rsp: attribute id 0x%x, %s", rc->meta_rsp.attr_id, rc->meta_rsp.attr_text); 255 | free(rc->meta_rsp.attr_text); 256 | break; 257 | } 258 | case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: { 259 | ESP_LOGI(BT_RC_CT_TAG, "AVRC event notification: %d", rc->change_ntf.event_id); 260 | bt_av_notify_evt_handler(rc->change_ntf.event_id, &rc->change_ntf.event_parameter); 261 | break; 262 | } 263 | case ESP_AVRC_CT_REMOTE_FEATURES_EVT: { 264 | ESP_LOGI(BT_RC_CT_TAG, "AVRC remote features %x, TG features %x", rc->rmt_feats.feat_mask, rc->rmt_feats.tg_feat_flag); 265 | break; 266 | } 267 | case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT: { 268 | ESP_LOGI(BT_RC_CT_TAG, "remote rn_cap: count %d, bitmask 0x%x", rc->get_rn_caps_rsp.cap_count, 269 | rc->get_rn_caps_rsp.evt_set.bits); 270 | s_avrc_peer_rn_cap.bits = rc->get_rn_caps_rsp.evt_set.bits; 271 | bt_av_new_track(); 272 | bt_av_playback_changed(); 273 | bt_av_play_pos_changed(); 274 | break; 275 | } 276 | default: 277 | ESP_LOGE(BT_RC_CT_TAG, "%s unhandled evt %d", __func__, event); 278 | break; 279 | } 280 | } 281 | 282 | static void volume_set_by_controller(uint8_t volume) 283 | { 284 | ESP_LOGI(BT_RC_TG_TAG, "Volume is set by remote controller %d%%\n", (uint32_t)volume * 100 / 0x7f); 285 | _lock_acquire(&s_volume_lock); 286 | s_volume = volume; 287 | _lock_release(&s_volume_lock); 288 | } 289 | 290 | static void volume_set_by_local_host(uint8_t volume) 291 | { 292 | return; 293 | ESP_LOGI(BT_RC_TG_TAG, "Volume is set locally to: %d%%", (uint32_t)volume * 100 / 0x7f); 294 | _lock_acquire(&s_volume_lock); 295 | s_volume = volume; 296 | _lock_release(&s_volume_lock); 297 | 298 | if (s_volume_notify) { 299 | esp_avrc_rn_param_t rn_param; 300 | rn_param.volume = s_volume; 301 | esp_avrc_tg_send_rn_rsp(ESP_AVRC_RN_VOLUME_CHANGE, ESP_AVRC_RN_RSP_CHANGED, &rn_param); 302 | s_volume_notify = false; 303 | } 304 | } 305 | 306 | static void volume_change_simulation(void *arg) 307 | { 308 | ESP_LOGI(BT_RC_TG_TAG, "start volume change simulation"); 309 | 310 | for (;;) { 311 | vTaskDelay(10000 / portTICK_RATE_MS); 312 | 313 | uint8_t volume = (s_volume + 5) & 0x7f; 314 | volume_set_by_local_host(volume); 315 | } 316 | } 317 | 318 | static void bt_av_hdl_avrc_tg_evt(uint16_t event, void *p_param) 319 | { 320 | ESP_LOGD(BT_RC_TG_TAG, "%s evt %d", __func__, event); 321 | esp_avrc_tg_cb_param_t *rc = (esp_avrc_tg_cb_param_t *)(p_param); 322 | switch (event) { 323 | case ESP_AVRC_TG_CONNECTION_STATE_EVT: { 324 | uint8_t *bda = rc->conn_stat.remote_bda; 325 | ESP_LOGI(BT_RC_TG_TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]", 326 | rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); 327 | if (rc->conn_stat.connected) { 328 | // create task to simulate volume change 329 | xTaskCreate(volume_change_simulation, "vcsT", 2048, NULL, 5, &s_vcs_task_hdl); 330 | } else { 331 | vTaskDelete(s_vcs_task_hdl); 332 | ESP_LOGI(BT_RC_TG_TAG, "Stop volume change simulation"); 333 | } 334 | break; 335 | } 336 | case ESP_AVRC_TG_PASSTHROUGH_CMD_EVT: { 337 | ESP_LOGI(BT_RC_TG_TAG, "AVRC passthrough cmd: key_code 0x%x, key_state %d", rc->psth_cmd.key_code, rc->psth_cmd.key_state); 338 | break; 339 | } 340 | case ESP_AVRC_TG_SET_ABSOLUTE_VOLUME_CMD_EVT: { 341 | ESP_LOGI(BT_RC_TG_TAG, "AVRC set absolute volume: %d%%", (int)rc->set_abs_vol.volume * 100/ 0x7f); 342 | volume_set_by_controller(rc->set_abs_vol.volume); 343 | break; 344 | } 345 | case ESP_AVRC_TG_REGISTER_NOTIFICATION_EVT: { 346 | ESP_LOGI(BT_RC_TG_TAG, "AVRC register event notification: %d, param: 0x%x", rc->reg_ntf.event_id, rc->reg_ntf.event_parameter); 347 | if (rc->reg_ntf.event_id == ESP_AVRC_RN_VOLUME_CHANGE) { 348 | s_volume_notify = true; 349 | esp_avrc_rn_param_t rn_param; 350 | rn_param.volume = s_volume; 351 | esp_avrc_tg_send_rn_rsp(ESP_AVRC_RN_VOLUME_CHANGE, ESP_AVRC_RN_RSP_INTERIM, &rn_param); 352 | } 353 | break; 354 | } 355 | case ESP_AVRC_TG_REMOTE_FEATURES_EVT: { 356 | ESP_LOGI(BT_RC_TG_TAG, "AVRC remote features %x, CT features %x", rc->rmt_feats.feat_mask, rc->rmt_feats.ct_feat_flag); 357 | break; 358 | } 359 | default: 360 | ESP_LOGE(BT_RC_TG_TAG, "%s unhandled evt %d", __func__, event); 361 | break; 362 | } 363 | } 364 | -------------------------------------------------------------------------------- /code/bt_app_av.h: -------------------------------------------------------------------------------- 1 | /* 2 | This example code is in the Public Domain (or CC0 licensed, at your option.) 3 | 4 | Unless required by applicable law or agreed to in writing, this 5 | software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 6 | CONDITIONS OF ANY KIND, either express or implied. 7 | */ 8 | 9 | #ifndef __BT_APP_AV_H__ 10 | #define __BT_APP_AV_H__ 11 | 12 | #include 13 | #include "esp_a2dp_api.h" 14 | #include "esp_avrc_api.h" 15 | 16 | #define BT_AV_TAG "BT_AV" 17 | #define BT_RC_TG_TAG "RCTG" 18 | #define BT_RC_CT_TAG "RCCT" 19 | 20 | /** 21 | * @brief callback function for A2DP sink 22 | */ 23 | void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param); 24 | 25 | /** 26 | * @brief callback function for A2DP sink audio data stream 27 | */ 28 | void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len); 29 | 30 | /** 31 | * @brief callback function for AVRCP controller 32 | */ 33 | void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param); 34 | 35 | /** 36 | * @brief callback function for AVRCP target 37 | */ 38 | void bt_app_rc_tg_cb(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param); 39 | 40 | extern uint8_t s_volume; 41 | 42 | #endif /* __BT_APP_AV_H__*/ 43 | -------------------------------------------------------------------------------- /code/bt_app_core.c: -------------------------------------------------------------------------------- 1 | /* 2 | This example code is in the Public Domain (or CC0 licensed, at your option.) 3 | 4 | Unless required by applicable law or agreed to in writing, this 5 | software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 6 | CONDITIONS OF ANY KIND, either express or implied. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include "freertos/xtensa_api.h" 13 | #include "freertos/FreeRTOSConfig.h" 14 | #include "freertos/FreeRTOS.h" 15 | #include "freertos/queue.h" 16 | #include "freertos/task.h" 17 | #include "esp_log.h" 18 | #include "bt_app_core.h" 19 | #include "driver/i2s.h" 20 | #include "freertos/ringbuf.h" 21 | //add to see variable s_volume 22 | #include "bt_app_av.h" 23 | 24 | static void bt_app_task_handler(void *arg); 25 | static bool bt_app_send_msg(bt_app_msg_t *msg); 26 | static void bt_app_work_dispatched(bt_app_msg_t *msg); 27 | 28 | static xQueueHandle s_bt_app_task_queue = NULL; 29 | static xTaskHandle s_bt_app_task_handle = NULL; 30 | static xTaskHandle s_bt_i2s_task_handle = NULL; 31 | static RingbufHandle_t s_ringbuf_i2s = NULL;; 32 | 33 | bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback) 34 | { 35 | ESP_LOGD(BT_APP_CORE_TAG, "%s event 0x%x, param len %d", __func__, event, param_len); 36 | 37 | bt_app_msg_t msg; 38 | memset(&msg, 0, sizeof(bt_app_msg_t)); 39 | 40 | msg.sig = BT_APP_SIG_WORK_DISPATCH; 41 | msg.event = event; 42 | msg.cb = p_cback; 43 | 44 | if (param_len == 0) { 45 | return bt_app_send_msg(&msg); 46 | } else if (p_params && param_len > 0) { 47 | if ((msg.param = malloc(param_len)) != NULL) { 48 | memcpy(msg.param, p_params, param_len); 49 | /* check if caller has provided a copy callback to do the deep copy */ 50 | if (p_copy_cback) { 51 | p_copy_cback(&msg, msg.param, p_params); 52 | } 53 | return bt_app_send_msg(&msg); 54 | } 55 | } 56 | 57 | return false; 58 | } 59 | 60 | static bool bt_app_send_msg(bt_app_msg_t *msg) 61 | { 62 | if (msg == NULL) { 63 | return false; 64 | } 65 | 66 | if (xQueueSend(s_bt_app_task_queue, msg, 10 / portTICK_RATE_MS) != pdTRUE) { 67 | ESP_LOGE(BT_APP_CORE_TAG, "%s xQueue send failed", __func__); 68 | return false; 69 | } 70 | return true; 71 | } 72 | 73 | static void bt_app_work_dispatched(bt_app_msg_t *msg) 74 | { 75 | if (msg->cb) { 76 | msg->cb(msg->event, msg->param); 77 | } 78 | } 79 | 80 | static void bt_app_task_handler(void *arg) 81 | { 82 | bt_app_msg_t msg; 83 | for (;;) { 84 | if (pdTRUE == xQueueReceive(s_bt_app_task_queue, &msg, (portTickType)portMAX_DELAY)) { 85 | ESP_LOGD(BT_APP_CORE_TAG, "%s, sig 0x%x, 0x%x", __func__, msg.sig, msg.event); 86 | switch (msg.sig) { 87 | case BT_APP_SIG_WORK_DISPATCH: 88 | bt_app_work_dispatched(&msg); 89 | break; 90 | default: 91 | ESP_LOGW(BT_APP_CORE_TAG, "%s, unhandled sig: %d", __func__, msg.sig); 92 | break; 93 | } // switch (msg.sig) 94 | 95 | if (msg.param) { 96 | free(msg.param); 97 | } 98 | } 99 | } 100 | } 101 | 102 | void bt_app_task_start_up(void) 103 | { 104 | s_bt_app_task_queue = xQueueCreate(10, sizeof(bt_app_msg_t)); 105 | xTaskCreate(bt_app_task_handler, "BtAppT", 3072, NULL, configMAX_PRIORITIES - 3, &s_bt_app_task_handle); 106 | return; 107 | } 108 | 109 | void bt_app_task_shut_down(void) 110 | { 111 | if (s_bt_app_task_handle) { 112 | vTaskDelete(s_bt_app_task_handle); 113 | s_bt_app_task_handle = NULL; 114 | } 115 | if (s_bt_app_task_queue) { 116 | vQueueDelete(s_bt_app_task_queue); 117 | s_bt_app_task_queue = NULL; 118 | } 119 | } 120 | 121 | static void bt_i2s_task_handler(void *arg) 122 | { 123 | uint8_t *data = NULL; 124 | size_t item_size = 0; 125 | size_t bytes_written = 0; 126 | 127 | for (;;) { 128 | data = (uint8_t *)xRingbufferReceive(s_ringbuf_i2s, &item_size, (portTickType)portMAX_DELAY); 129 | if (item_size != 0){ 130 | int16_t * pcmdata = (int16_t *)data; 131 | for (int i=0; i 13 | #include 14 | #include 15 | 16 | #define BT_APP_CORE_TAG "BT_APP_CORE" 17 | 18 | #define BT_APP_SIG_WORK_DISPATCH (0x01) 19 | 20 | /** 21 | * @brief handler for the dispatched work 22 | */ 23 | typedef void (* bt_app_cb_t) (uint16_t event, void *param); 24 | 25 | /* message to be sent */ 26 | typedef struct { 27 | uint16_t sig; /*!< signal to bt_app_task */ 28 | uint16_t event; /*!< message event id */ 29 | bt_app_cb_t cb; /*!< context switch callback */ 30 | void *param; /*!< parameter area needs to be last */ 31 | } bt_app_msg_t; 32 | 33 | /** 34 | * @brief parameter deep-copy function to be customized 35 | */ 36 | typedef void (* bt_app_copy_cb_t) (bt_app_msg_t *msg, void *p_dest, void *p_src); 37 | 38 | /** 39 | * @brief work dispatcher for the application task 40 | */ 41 | bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback); 42 | 43 | void bt_app_task_start_up(void); 44 | 45 | void bt_app_task_shut_down(void); 46 | 47 | void bt_i2s_task_start_up(void); 48 | 49 | void bt_i2s_task_shut_down(void); 50 | 51 | size_t write_ringbuf(const uint8_t *data, size_t size); 52 | 53 | #endif /* __BT_APP_CORE_H__ */ 54 | -------------------------------------------------------------------------------- /code/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # "main" pseudo-component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | -------------------------------------------------------------------------------- /code/main.c: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include "freertos/FreeRTOS.h" 20 | #include "freertos/task.h" 21 | #include "nvs.h" 22 | #include "nvs_flash.h" 23 | #include "esp_system.h" 24 | #include "esp_log.h" 25 | 26 | #include "esp_bt.h" 27 | #include "bt_app_core.h" 28 | #include "bt_app_av.h" 29 | #include "esp_bt_main.h" 30 | #include "esp_bt_device.h" 31 | #include "esp_gap_bt_api.h" 32 | #include "esp_a2dp_api.h" 33 | #include "esp_avrc_api.h" 34 | #include "driver/i2s.h" 35 | 36 | /* event for handler "bt_av_hdl_stack_up */ 37 | enum { 38 | BT_APP_EVT_STACK_UP = 0, 39 | }; 40 | 41 | /* handler for bluetooth stack enabled events */ 42 | static void bt_av_hdl_stack_evt(uint16_t event, void *p_param); 43 | 44 | 45 | void app_main() 46 | { 47 | /* Initialize NVS — it is used to store PHY calibration data */ 48 | esp_err_t err = nvs_flash_init(); 49 | if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { 50 | ESP_ERROR_CHECK(nvs_flash_erase()); 51 | err = nvs_flash_init(); 52 | } 53 | ESP_ERROR_CHECK(err); 54 | 55 | i2s_config_t i2s_config = { 56 | #ifdef CONFIG_EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC 57 | .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN, 58 | #else 59 | .mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX 60 | #endif 61 | .sample_rate = 44100, 62 | .bits_per_sample = 16, 63 | .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels 64 | .communication_format = I2S_COMM_FORMAT_I2S_MSB, 65 | .dma_buf_count = 6, 66 | .dma_buf_len = 60, 67 | .intr_alloc_flags = 0, //Default interrupt priority 68 | .tx_desc_auto_clear = true //Auto clear tx descriptor on underflow 69 | }; 70 | 71 | 72 | i2s_driver_install(0, &i2s_config, 0, NULL); 73 | #ifdef CONFIG_EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC 74 | i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN); 75 | i2s_set_pin(0, NULL); 76 | #else 77 | i2s_pin_config_t pin_config = { 78 | .bck_io_num = CONFIG_EXAMPLE_I2S_BCK_PIN, 79 | .ws_io_num = CONFIG_EXAMPLE_I2S_LRCK_PIN, 80 | .data_out_num = CONFIG_EXAMPLE_I2S_DATA_PIN, 81 | .data_in_num = -1 //Not used 82 | }; 83 | 84 | i2s_set_pin(0, &pin_config); 85 | #endif 86 | 87 | 88 | ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE)); 89 | 90 | esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); 91 | if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) { 92 | ESP_LOGE(BT_AV_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(err)); 93 | return; 94 | } 95 | 96 | if ((err = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) { 97 | ESP_LOGE(BT_AV_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(err)); 98 | return; 99 | } 100 | 101 | if ((err = esp_bluedroid_init()) != ESP_OK) { 102 | ESP_LOGE(BT_AV_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err)); 103 | return; 104 | } 105 | 106 | if ((err = esp_bluedroid_enable()) != ESP_OK) { 107 | ESP_LOGE(BT_AV_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err)); 108 | return; 109 | } 110 | 111 | /* create application task */ 112 | bt_app_task_start_up(); 113 | 114 | /* Bluetooth device name, connection mode and profile set up */ 115 | bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL); 116 | 117 | #if (CONFIG_BT_SSP_ENABLED == true) 118 | /* Set default parameters for Secure Simple Pairing */ 119 | esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE; 120 | esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO; 121 | esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t)); 122 | #endif 123 | 124 | /* 125 | * Set default parameters for Legacy Pairing 126 | * Use fixed pin code 127 | */ 128 | esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED; 129 | esp_bt_pin_code_t pin_code; 130 | pin_code[0] = '1'; 131 | pin_code[1] = '2'; 132 | pin_code[2] = '3'; 133 | pin_code[3] = '4'; 134 | esp_bt_gap_set_pin(pin_type, 4, pin_code); 135 | 136 | } 137 | 138 | void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) 139 | { 140 | switch (event) { 141 | case ESP_BT_GAP_AUTH_CMPL_EVT: { 142 | if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) { 143 | ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name); 144 | esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN); 145 | } else { 146 | ESP_LOGE(BT_AV_TAG, "authentication failed, status:%d", param->auth_cmpl.stat); 147 | } 148 | break; 149 | } 150 | 151 | #if (CONFIG_BT_SSP_ENABLED == true) 152 | case ESP_BT_GAP_CFM_REQ_EVT: 153 | ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val); 154 | esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true); 155 | break; 156 | case ESP_BT_GAP_KEY_NOTIF_EVT: 157 | ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey); 158 | break; 159 | case ESP_BT_GAP_KEY_REQ_EVT: 160 | ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!"); 161 | break; 162 | #endif 163 | 164 | default: { 165 | ESP_LOGI(BT_AV_TAG, "event: %d", event); 166 | break; 167 | } 168 | } 169 | return; 170 | } 171 | static void bt_av_hdl_stack_evt(uint16_t event, void *p_param) 172 | { 173 | ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event); 174 | switch (event) { 175 | case BT_APP_EVT_STACK_UP: { 176 | /* set up device name */ 177 | char *dev_name = "HELLO_YOUTUBE"; 178 | esp_bt_dev_set_device_name(dev_name); 179 | 180 | esp_bt_gap_register_callback(bt_app_gap_cb); 181 | 182 | /* initialize AVRCP controller */ 183 | esp_avrc_ct_init(); 184 | esp_avrc_ct_register_callback(bt_app_rc_ct_cb); 185 | /* initialize AVRCP target */ 186 | assert (esp_avrc_tg_init() == ESP_OK); 187 | esp_avrc_tg_register_callback(bt_app_rc_tg_cb); 188 | 189 | esp_avrc_rn_evt_cap_mask_t evt_set = {0}; 190 | esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_SET, &evt_set, ESP_AVRC_RN_VOLUME_CHANGE); 191 | assert(esp_avrc_tg_set_rn_evt_cap(&evt_set) == ESP_OK); 192 | 193 | /* initialize A2DP sink */ 194 | esp_a2d_register_callback(&bt_app_a2d_cb); 195 | esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb); 196 | esp_a2d_sink_init(); 197 | 198 | /* set discoverable and connectable mode, wait to be connected */ 199 | esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); 200 | break; 201 | } 202 | default: 203 | ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event); 204 | break; 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /esp32_a2dp.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YetAnotherElectronicsChannel/ESP32_Bluetooth_Audio_Receiver/45b717769bd41eeabb2c55e0fab98a74d36be51b/esp32_a2dp.pdf --------------------------------------------------------------------------------