2016-10-18 09:49:00 -04:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2017-04-05 09:19:15 -04:00
|
|
|
#include <string.h>
|
2016-10-18 09:49:00 -04:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "freertos/xtensa_api.h"
|
|
|
|
#include "freertos/portmacro.h"
|
|
|
|
#include "esp_types.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_task.h"
|
|
|
|
#include "esp_intr.h"
|
|
|
|
#include "esp_attr.h"
|
2017-02-16 09:06:02 -05:00
|
|
|
#include "esp_phy_init.h"
|
2016-10-18 09:49:00 -04:00
|
|
|
#include "bt.h"
|
|
|
|
|
|
|
|
#if CONFIG_BT_ENABLED
|
|
|
|
|
2017-06-13 05:14:50 -04:00
|
|
|
#define BTDM_INIT_PERIOD (5000) /* ms */
|
|
|
|
|
2017-02-21 04:46:59 -05:00
|
|
|
/* Bluetooth system and controller config */
|
2017-03-16 05:14:20 -04:00
|
|
|
#define BTDM_CFG_BT_EM_RELEASE (1<<0)
|
|
|
|
#define BTDM_CFG_BT_DATA_RELEASE (1<<1)
|
|
|
|
#define BTDM_CFG_HCI_UART (1<<2)
|
|
|
|
#define BTDM_CFG_CONTROLLER_RUN_APP_CPU (1<<3)
|
2017-02-21 04:46:59 -05:00
|
|
|
/* Other reserved for future */
|
|
|
|
|
2016-10-18 09:49:00 -04:00
|
|
|
/* not for user call, so don't put to include file */
|
|
|
|
extern void btdm_osi_funcs_register(void *osi_funcs);
|
2017-04-05 09:19:15 -04:00
|
|
|
extern void btdm_controller_init(uint32_t config_mask, esp_bt_controller_config_t *config_opts);
|
2017-02-20 12:05:37 -05:00
|
|
|
extern void btdm_controller_schedule(void);
|
2017-02-17 06:24:58 -05:00
|
|
|
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);
|
2016-10-18 09:49:00 -04:00
|
|
|
|
2017-01-03 02:53:06 -05:00
|
|
|
/* VHCI function interface */
|
|
|
|
typedef struct vhci_host_callback {
|
|
|
|
void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
|
|
|
|
int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/
|
|
|
|
} vhci_host_callback_t;
|
|
|
|
|
|
|
|
extern bool API_vhci_host_check_send_available(void);
|
|
|
|
extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
|
|
|
|
extern void API_vhci_host_register_callback(const vhci_host_callback_t *callback);
|
2016-10-18 09:49:00 -04:00
|
|
|
|
|
|
|
#define BT_DEBUG(...)
|
|
|
|
#define BT_API_CALL_CHECK(info, api_call, ret) \
|
|
|
|
do{\
|
|
|
|
esp_err_t __err = (api_call);\
|
|
|
|
if ((ret) != __err) {\
|
|
|
|
BT_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\
|
|
|
|
return __err;\
|
|
|
|
}\
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
struct osi_funcs_t {
|
|
|
|
xt_handler (*_set_isr)(int n, xt_handler f, void *arg);
|
|
|
|
void (*_ints_on)(unsigned int mask);
|
|
|
|
void (*_interrupt_disable)(void);
|
|
|
|
void (*_interrupt_restore)(void);
|
|
|
|
void (*_task_yield)(void);
|
2017-06-07 02:58:17 -04:00
|
|
|
void (*_task_yield_from_isr)(void);
|
2016-10-18 09:49:00 -04:00
|
|
|
void *(*_semphr_create)(uint32_t max, uint32_t init);
|
|
|
|
int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw);
|
|
|
|
int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms);
|
|
|
|
void *(*_mutex_create)(void);
|
|
|
|
int32_t (*_mutex_lock)(void *mutex);
|
|
|
|
int32_t (*_mutex_unlock)(void *mutex);
|
2017-03-01 08:04:12 -05:00
|
|
|
int32_t (* _read_efuse_mac)(uint8_t mac[6]);
|
2017-03-24 02:57:07 -04:00
|
|
|
void (* _srand)(unsigned int seed);
|
|
|
|
int (* _rand)(void);
|
2016-10-18 09:49:00 -04:00
|
|
|
};
|
|
|
|
|
2017-02-17 06:24:58 -05:00
|
|
|
/* Static variable declare */
|
|
|
|
static bool btdm_bb_init_flag = false;
|
2017-06-13 05:14:50 -04:00
|
|
|
static xSemaphoreHandle btdm_init_sem;
|
2017-02-20 12:05:37 -05:00
|
|
|
static esp_bt_controller_status_t btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
|
2017-04-05 09:19:15 -04:00
|
|
|
static esp_bt_controller_config_t btdm_cfg_opts;
|
2017-02-17 06:24:58 -05:00
|
|
|
static xTaskHandle btControllerTaskHandle;
|
|
|
|
|
2016-10-18 09:49:00 -04:00
|
|
|
static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
|
|
|
static void IRAM_ATTR interrupt_disable(void)
|
|
|
|
{
|
|
|
|
portENTER_CRITICAL(&global_int_mux);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void IRAM_ATTR interrupt_restore(void)
|
|
|
|
{
|
|
|
|
portEXIT_CRITICAL(&global_int_mux);
|
|
|
|
}
|
|
|
|
|
2017-06-07 02:58:17 -04:00
|
|
|
static void IRAM_ATTR task_yield_from_isr(void)
|
|
|
|
{
|
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void *IRAM_ATTR semphr_create_wrapper(uint32_t max, uint32_t init)
|
2016-10-18 09:49:00 -04:00
|
|
|
{
|
|
|
|
return (void *)xSemaphoreCreateCounting(max, init);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
|
|
|
|
{
|
|
|
|
return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t IRAM_ATTR semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
|
|
|
|
{
|
2016-12-21 20:42:21 -05:00
|
|
|
return (int32_t)xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
|
2016-10-18 09:49:00 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void *IRAM_ATTR mutex_create_wrapper(void)
|
2016-10-18 09:49:00 -04:00
|
|
|
{
|
|
|
|
return (void *)xSemaphoreCreateMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
|
|
|
|
{
|
|
|
|
return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
|
|
|
|
{
|
|
|
|
return (int32_t)xSemaphoreGive(mutex);
|
|
|
|
}
|
|
|
|
|
2017-03-01 08:04:12 -05:00
|
|
|
static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6])
|
|
|
|
{
|
|
|
|
return esp_read_mac(mac, ESP_MAC_BT);
|
|
|
|
}
|
|
|
|
|
2017-03-24 02:57:07 -04:00
|
|
|
static void IRAM_ATTR srand_wrapper(unsigned int seed)
|
|
|
|
{
|
|
|
|
/* empty function */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int IRAM_ATTR rand_wrapper(void)
|
|
|
|
{
|
|
|
|
return (int)esp_random();
|
|
|
|
}
|
|
|
|
|
2016-10-18 09:49:00 -04:00
|
|
|
static struct osi_funcs_t osi_funcs = {
|
|
|
|
._set_isr = xt_set_interrupt_handler,
|
|
|
|
._ints_on = xt_ints_on,
|
|
|
|
._interrupt_disable = interrupt_disable,
|
|
|
|
._interrupt_restore = interrupt_restore,
|
|
|
|
._task_yield = vPortYield,
|
2017-06-07 02:58:17 -04:00
|
|
|
._task_yield_from_isr = task_yield_from_isr,
|
2016-10-18 09:49:00 -04:00
|
|
|
._semphr_create = semphr_create_wrapper,
|
|
|
|
._semphr_give_from_isr = semphr_give_from_isr_wrapper,
|
|
|
|
._semphr_take = semphr_take_wrapper,
|
|
|
|
._mutex_create = mutex_create_wrapper,
|
|
|
|
._mutex_lock = mutex_lock_wrapper,
|
|
|
|
._mutex_unlock = mutex_unlock_wrapper,
|
2017-03-24 02:57:07 -04:00
|
|
|
._read_efuse_mac = read_mac_wrapper,
|
|
|
|
._srand = srand_wrapper,
|
|
|
|
._rand = rand_wrapper,
|
2016-10-18 09:49:00 -04:00
|
|
|
};
|
|
|
|
|
2017-01-03 02:53:06 -05:00
|
|
|
bool esp_vhci_host_check_send_available(void)
|
|
|
|
{
|
|
|
|
return API_vhci_host_check_send_available();
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
|
|
|
|
{
|
|
|
|
API_vhci_host_send_packet(data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
|
|
|
|
{
|
|
|
|
API_vhci_host_register_callback((const vhci_host_callback_t *)callback);
|
|
|
|
}
|
|
|
|
|
2017-02-21 04:46:59 -05:00
|
|
|
static uint32_t btdm_config_mask_load(void)
|
|
|
|
{
|
|
|
|
uint32_t mask = 0x0;
|
|
|
|
|
|
|
|
#ifdef CONFIG_BT_DRAM_RELEASE
|
|
|
|
mask |= (BTDM_CFG_BT_EM_RELEASE | BTDM_CFG_BT_DATA_RELEASE);
|
2017-03-16 05:14:20 -04:00
|
|
|
#endif
|
2017-04-05 09:19:15 -04:00
|
|
|
#ifdef CONFIG_BT_HCI_UART
|
2017-03-16 05:14:20 -04:00
|
|
|
mask |= BTDM_CFG_HCI_UART;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_BTDM_CONTROLLER_RUN_APP_CPU
|
|
|
|
mask |= BTDM_CFG_CONTROLLER_RUN_APP_CPU;
|
2017-02-21 04:46:59 -05:00
|
|
|
#endif
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2016-10-18 09:49:00 -04:00
|
|
|
static void bt_controller_task(void *pvParam)
|
|
|
|
{
|
2017-02-21 04:46:59 -05:00
|
|
|
uint32_t btdm_cfg_mask = 0;
|
|
|
|
|
|
|
|
btdm_cfg_mask = btdm_config_mask_load();
|
2017-04-05 09:19:15 -04:00
|
|
|
|
|
|
|
btdm_osi_funcs_register(&osi_funcs);
|
2017-03-16 05:14:20 -04:00
|
|
|
|
|
|
|
btdm_controller_init(btdm_cfg_mask, &btdm_cfg_opts);
|
2017-02-21 04:46:59 -05:00
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
|
2017-06-13 05:14:50 -04:00
|
|
|
|
|
|
|
xSemaphoreGive(btdm_init_sem);
|
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
/* Loop */
|
|
|
|
btdm_controller_schedule();
|
2017-06-13 05:14:50 -04:00
|
|
|
|
|
|
|
/* never run here */
|
2016-10-18 09:49:00 -04:00
|
|
|
}
|
|
|
|
|
2017-04-05 09:19:15 -04:00
|
|
|
esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
|
2016-10-18 09:49:00 -04:00
|
|
|
{
|
2017-04-05 09:19:15 -04:00
|
|
|
BaseType_t ret;
|
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
|
2017-04-05 09:19:15 -04:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
2017-02-20 12:05:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-05 09:19:15 -04:00
|
|
|
if (cfg == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2017-06-13 05:14:50 -04:00
|
|
|
btdm_init_sem = xSemaphoreCreateBinary();
|
|
|
|
if (btdm_init_sem == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
2017-04-05 09:19:15 -04:00
|
|
|
memcpy(&btdm_cfg_opts, cfg, sizeof(esp_bt_controller_config_t));
|
|
|
|
|
|
|
|
ret = xTaskCreatePinnedToCore(bt_controller_task, "btController",
|
2016-11-24 13:10:15 -05:00
|
|
|
ESP_TASK_BT_CONTROLLER_STACK, NULL,
|
2017-04-05 09:19:15 -04:00
|
|
|
ESP_TASK_BT_CONTROLLER_PRIO, &btControllerTaskHandle, CONFIG_BTDM_CONTROLLER_RUN_CPU);
|
|
|
|
|
|
|
|
if (ret != pdPASS) {
|
|
|
|
memset(&btdm_cfg_opts, 0x0, sizeof(esp_bt_controller_config_t));
|
2017-06-13 05:14:50 -04:00
|
|
|
vSemaphoreDelete(btdm_init_sem);
|
2017-04-05 09:19:15 -04:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
2017-06-13 05:14:50 -04:00
|
|
|
xSemaphoreTake(btdm_init_sem, BTDM_INIT_PERIOD/portTICK_PERIOD_MS);
|
|
|
|
vSemaphoreDelete(btdm_init_sem);
|
|
|
|
|
2017-04-05 09:19:15 -04:00
|
|
|
return ESP_OK;
|
2017-02-17 06:24:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void esp_bt_controller_deinit(void)
|
|
|
|
{
|
2017-04-05 09:19:15 -04:00
|
|
|
memset(&btdm_cfg_opts, 0x0, sizeof(esp_bt_controller_config_t));
|
2017-02-17 06:24:58 -05:00
|
|
|
vTaskDelete(btControllerTaskHandle);
|
2017-02-20 12:05:37 -05:00
|
|
|
btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
|
2017-02-17 06:24:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
2017-02-17 06:24:58 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;
|
|
|
|
|
2017-02-17 06:24:58 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_bt_controller_disable(esp_bt_mode_t mode)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
2017-02-17 06:24:58 -05:00
|
|
|
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();
|
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
|
|
|
|
|
2017-02-17 06:24:58 -05:00
|
|
|
return ESP_OK;
|
2016-10-18 09:49:00 -04:00
|
|
|
}
|
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
esp_bt_controller_status_t esp_bt_controller_get_status(void)
|
|
|
|
{
|
|
|
|
return btdm_controller_status;
|
|
|
|
}
|
|
|
|
|
2016-10-18 09:49:00 -04:00
|
|
|
#endif
|