1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "freertos/FreeRTOS.h"
20 #include "freertos/task.h"
22 #include "nvs_flash.h"
23 #include "esp_system.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"
36 /* event for handler "bt_av_hdl_stack_up */
38 BT_APP_EVT_STACK_UP = 0,
41 /* handler for bluetooth stack enabled events */
42 static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
47 /* Initialize NVS — it is used to store PHY calibration data */
48 esp_err_t ret = nvs_flash_init();
49 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
50 ESP_ERROR_CHECK(nvs_flash_erase());
51 ret = nvs_flash_init();
53 ESP_ERROR_CHECK( ret );
55 i2s_config_t i2s_config = {
56 #ifdef CONFIG_A2DP_SINK_OUTPUT_INTERNAL_DAC
57 .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
59 .mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
62 .bits_per_sample = 16,
63 .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
64 .communication_format = I2S_COMM_FORMAT_I2S_MSB,
67 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 //Interrupt level 1
71 i2s_driver_install(0, &i2s_config, 0, NULL);
72 #ifdef CONFIG_A2DP_SINK_OUTPUT_INTERNAL_DAC
73 i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
76 i2s_pin_config_t pin_config = {
77 .bck_io_num = CONFIG_I2S_BCK_PIN,
78 .ws_io_num = CONFIG_I2S_LRCK_PIN,
79 .data_out_num = CONFIG_I2S_DATA_PIN,
80 .data_in_num = -1 //Not used
83 i2s_set_pin(0, &pin_config);
87 ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
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(ret));
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(ret));
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(ret));
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(ret));
111 /* create application task */
112 bt_app_task_start_up();
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);
117 #ifdef CONFIG_BT_SSP_ENABLE
118 esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
119 esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
120 esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
121 #endif ///CONFIG_BT_SSP_ENABLE
124 void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
127 #ifdef CONFIG_BT_SSP_ENABLE
128 case ESP_BT_GAP_AUTH_CMPL_EVT:{
129 if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
130 ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name);
131 esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
133 ESP_LOGE(BT_AV_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
137 case ESP_BT_GAP_CFM_REQ_EVT:
138 ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
139 esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
141 case ESP_BT_GAP_KEY_NOTIF_EVT:
142 ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
144 case ESP_BT_GAP_KEY_REQ_EVT:
145 ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
147 #endif ///CONFIG_BT_SSP_ENABLE
149 ESP_LOGI(BT_AV_TAG, "event: %d", event);
155 static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
157 ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event);
159 case BT_APP_EVT_STACK_UP: {
160 /* set up device name */
161 char *dev_name = "ESP_SPEAKER";
162 esp_bt_dev_set_device_name(dev_name);
164 esp_bt_gap_register_callback(bt_app_gap_cb);
165 /* initialize A2DP sink */
166 esp_a2d_register_callback(&bt_app_a2d_cb);
167 esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb);
170 /* initialize AVRCP controller */
172 esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
174 /* set discoverable and connectable mode, wait to be connected */
175 esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
179 ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event);