#include "btc_task.h"
#include "btc_main.h"
#include "future.h"
-#include "esp_phy_init.h"
static bool esp_already_enable = false;
static bool esp_already_init = false;
esp_already_init = false;
- esp_phy_rf_deinit();
-
return ESP_OK;
}
/* not for user call, so don't put to include file */
extern void btdm_osi_funcs_register(void *osi_funcs);
extern void btdm_controller_init(void);
+extern void btdm_controller_deinit(void);
+extern int btdm_controller_enable(esp_bt_mode_t mode);
+extern int btdm_controller_disable(esp_bt_mode_t mode);
+extern void btdm_rf_bb_init(void);
/* VHCI function interface */
typedef struct vhci_host_callback {
esp_err_t (* _read_efuse_mac)(uint8_t mac[6]);
};
+/* Static variable declare */
+static bool btdm_bb_init_flag = false;
+
+static xTaskHandle btControllerTaskHandle;
+
static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
static void IRAM_ATTR interrupt_disable(void)
{
btdm_osi_funcs_register(&osi_funcs);
- esp_phy_load_cal_and_init();
-
btdm_controller_init();
}
+static bool bb_inited;
void esp_bt_controller_init()
{
+ bb_inited = false;
xTaskCreatePinnedToCore(bt_controller_task, "btController",
ESP_TASK_BT_CONTROLLER_STACK, NULL,
- ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0);
+ ESP_TASK_BT_CONTROLLER_PRIO, &btControllerTaskHandle, 0);
+}
+
+void esp_bt_controller_deinit(void)
+{
+ vTaskDelete(btControllerTaskHandle);
+ bb_inited = false;
+}
+
+esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
+{
+ int ret;
+
+ if (mode != ESP_BT_MODE_BTDM) {
+ return ESP_ERR_INVALID_ARG;
+ }
+
+ esp_phy_load_cal_and_init();
+
+ if (btdm_bb_init_flag == false) {
+ btdm_bb_init_flag = true;
+ btdm_rf_bb_init(); /* only initialise once */
+ }
+
+ ret = btdm_controller_enable(mode);
+ if (ret) {
+ return ESP_ERR_INVALID_STATE;
+ }
+
+ return ESP_OK;
+}
+
+esp_err_t esp_bt_controller_disable(esp_bt_mode_t mode)
+{
+ int ret;
+
+ if (mode != ESP_BT_MODE_BTDM) {
+ return ESP_ERR_INVALID_ARG;
+ }
+
+ ret = btdm_controller_disable(mode);
+ if (ret) {
+ return ESP_ERR_INVALID_STATE;
+ }
+
+ esp_phy_rf_deinit();
+
+ return ESP_OK;
}
#endif
extern "C" {
#endif
+/**
+ * @brief Bluetooth mode for controller enable/disable
+ */
+typedef enum {
+ ESP_BT_MODE_ILDE = 0x00, /*!< Bluetooth is not run */
+ ESP_BT_MODE_BLE = 0x01, /*!< Run BLE mode */
+ ESP_BT_MODE_CLASSIC_BT = 0x02, /*!< Run Classic BT mode */
+ ESP_BT_MODE_BTDM = 0x03, /*!< Run dual mode */
+} esp_bt_mode_t;
/**
- * @brief Initialize BT controller
+ * @brief Initialize BT controller to allocate task and other resource.
*
* This function should be called only once, before any other BT functions are called.
*/
void esp_bt_controller_init(void);
+/**
+ * @brief De-initialize BT controller to free resource and delete task.
+ *
+ * This function should be called only once, after any other BT functions are called.
+ * This function is not whole completed, esp_bt_controller_init cannot called after this function.
+ */
+void esp_bt_controller_deinit(void);
+
+/**
+ * @brief Enable BT controller
+ * @param mode : the mode(BLE/BT/BTDM) to enable.
+ * Now only support BTDM.
+ */
+esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode);
+
+/**
+ * @brief Disable BT controller
+ * @param mode : the mode(BLE/BT/BTDM) to disable.
+ * Now only support BTDM.
+ */
+esp_err_t esp_bt_controller_disable(esp_bt_mode_t mode);
+
+
+
/** @brief esp_vhci_host_callback
* used for vhci call host function to notify what host need to do
*/
-Subproject commit 9c1eea6bb03adc3b3847fff79c3f017652840a46
+Subproject commit 69616af7653f4de6e3b78f475dc10e73f0a20ece
/* Count value to indicate if there is peripheral that has initialized PHY and RF */
static int s_phy_rf_init_count = 0;
+static bool s_mac_rst_flag = false;
static _lock_t s_phy_rf_init_lock;
_lock_acquire(&s_phy_rf_init_lock);
if (s_phy_rf_init_count == 0) {
if (is_sleep == false) {
- REG_SET_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST);
- REG_CLR_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST);
+ if (s_mac_rst_flag == false) {
+ s_mac_rst_flag = true;
+ REG_SET_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST);
+ REG_CLR_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST);
+ }
}
// Enable WiFi peripheral clock
SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, 0x87cf);
Enumerations
^^^^^^^^^^^^
+.. doxygenenum:: esp_bt_mode_t
Structures
^^^^^^^^^^
^^^^^^^^^
.. doxygenfunction:: esp_bt_controller_init
+.. doxygenfunction:: esp_bt_controller_deinit
+.. doxygenfunction:: esp_bt_controller_enable
+.. doxygenfunction:: esp_bt_controller_disable
.. doxygenfunction:: esp_vhci_host_check_send_available
.. doxygenfunction:: esp_vhci_host_send_packet
.. doxygenfunction:: esp_vhci_host_register_callback
void app_main()
{
esp_bt_controller_init();
+
+ if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) {
+ return;
+ }
+
xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0);
}
esp_bt_controller_init();
+ ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
+ if (ret) {
+ BLUFI_ERROR("%s enable bt controller failed\n", __func__);
+ return;
+ }
+
ret = esp_bluedroid_init();
if (ret) {
BLUFI_ERROR("%s init bluedroid failed\n", __func__);
void app_main()
{
esp_bt_controller_init();
+ esp_bt_controller_enable(ESP_BT_MODE_BTDM);
+
gattc_client_test();
}
esp_bt_controller_init();
+ ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
+ if (ret) {
+ ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__);
+ return;
+ }
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
#include "esp_bt_main.h"
#include "gatts_table_creat_demo.h"
+#define GATTS_TABLE_TAG "GATTS_TABLE_DEMO"
#define HEART_PROFILE_NUM 1
#define HEART_PROFILE_APP_IDX 0
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
- LOG_ERROR("GAP_EVT, event %d\n", event);
+ ESP_LOGE(GATTS_TABLE_TAG, "GAP_EVT, event %d\n", event);
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
static void gatts_profile_event_handler(esp_gatts_cb_event_t event,
esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
- LOG_ERROR("event = %x\n",event);
+ ESP_LOGE(GATTS_TABLE_TAG, "event = %x\n",event);
switch (event) {
case ESP_GATTS_REG_EVT:
- LOG_INFO("%s %d\n", __func__, __LINE__);
+ ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__);
esp_ble_gap_set_device_name(SAMPLE_DEVICE_NAME);
- LOG_INFO("%s %d\n", __func__, __LINE__);
+ ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__);
esp_ble_gap_config_adv_data(&heart_rate_adv_config);
- LOG_INFO("%s %d\n", __func__, __LINE__);
+ ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__);
esp_ble_gatts_create_attr_tab(heart_rate_gatt_db, gatts_if,
HRS_IDX_NB, HEART_RATE_SVC_INST_ID);
break;
case ESP_GATTS_CONGEST_EVT:
break;
case ESP_GATTS_CREAT_ATTR_TAB_EVT:{
- LOG_ERROR("The number handle =%x\n",param->add_attr_tab.num_handle);
+ ESP_LOGE(GATTS_TABLE_TAG, "The number handle =%x\n",param->add_attr_tab.num_handle);
if(param->add_attr_tab.num_handle == HRS_IDX_NB){
memcpy(heart_rate_handle_table, param->add_attr_tab.handles,
sizeof(heart_rate_handle_table));
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
esp_ble_gatts_cb_param_t *param)
{
- LOG_INFO("EVT %d, gatts if %d\n", event, gatts_if);
+ ESP_LOGI(GATTS_TABLE_TAG, "EVT %d, gatts if %d\n", event, gatts_if);
/* If event is register event, store the gatts_if for each profile */
if (event == ESP_GATTS_REG_EVT) {
if (param->reg.status == ESP_GATT_OK) {
heart_rate_profile_tab[HEART_PROFILE_APP_IDX].gatts_if = gatts_if;
} else {
- LOG_INFO("Reg app failed, app_id %04x, status %d\n",
+ ESP_LOGI(GATTS_TABLE_TAG, "Reg app failed, app_id %04x, status %d\n",
param->reg.app_id,
param->reg.status);
return;
esp_err_t ret;
esp_bt_controller_init();
- LOG_INFO("%s init bluetooth\n", __func__);
+
+ ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
+ if (ret) {
+ ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed\n", __func__);
+ return;
+ }
+
+ ESP_LOGI(GATTS_TABLE_TAG, "%s init bluetooth\n", __func__);
ret = esp_bluedroid_init();
if (ret) {
- LOG_ERROR("%s init bluetooth failed\n", __func__);
+ ESP_LOGE(GATTS_TABLE_TAG, "%s init bluetooth failed\n", __func__);
return;
}
ret = esp_bluedroid_enable();
if (ret) {
- LOG_ERROR("%s enable bluetooth failed\n", __func__);
+ ESP_LOGE(GATTS_TABLE_TAG, "%s enable bluetooth failed\n", __func__);
return;
}