driver: esp32: add bluetooth support
Add support to BLE and BR/EDR feature. Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
This commit is contained in:
parent
6ae038ca27
commit
a3c35c93ff
8 changed files with 389 additions and 6 deletions
313
drivers/bluetooth/hci/hci_esp32.c
Normal file
313
drivers/bluetooth/hci/hci_esp32.c
Normal file
|
@ -0,0 +1,313 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define LOG_MODULE_NAME bt_hci_driver_esp32
|
||||
#include "common/log.h"
|
||||
|
||||
#include <init.h>
|
||||
#include <sys/byteorder.h>
|
||||
|
||||
#include <drivers/bluetooth/hci_driver.h>
|
||||
|
||||
#include <esp_bt.h>
|
||||
|
||||
#define HCI_CMD 0x01
|
||||
#define HCI_ACL 0x02
|
||||
#define HCI_SCO 0x03
|
||||
#define HCI_EVT 0x04
|
||||
#define HCI_ISO 0x05
|
||||
|
||||
#define HCI_BT_ESP32_TIMEOUT K_MSEC(2000)
|
||||
|
||||
static K_SEM_DEFINE(hci_send_sem, 1, 1);
|
||||
|
||||
static bool is_hci_event_discardable(const uint8_t *evt_data)
|
||||
{
|
||||
uint8_t evt_type = evt_data[0];
|
||||
|
||||
switch (evt_type) {
|
||||
#if defined(CONFIG_BT_BREDR)
|
||||
case BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI:
|
||||
case BT_HCI_EVT_EXTENDED_INQUIRY_RESULT:
|
||||
return true;
|
||||
#endif
|
||||
case BT_HCI_EVT_LE_META_EVENT: {
|
||||
uint8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)];
|
||||
|
||||
switch (subevt_type) {
|
||||
case BT_HCI_EVT_LE_ADVERTISING_REPORT:
|
||||
return true;
|
||||
case BT_HCI_EVT_LE_EXT_ADVERTISING_REPORT:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static struct net_buf *bt_esp_evt_recv(uint8_t *data, size_t remaining)
|
||||
{
|
||||
bool discardable = false;
|
||||
struct bt_hci_evt_hdr hdr;
|
||||
struct net_buf *buf;
|
||||
|
||||
if (remaining < sizeof(hdr)) {
|
||||
BT_ERR("Not enough data for event header");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
discardable = is_hci_event_discardable(data);
|
||||
|
||||
memcpy((void *)&hdr, data, sizeof(hdr));
|
||||
data += sizeof(hdr);
|
||||
remaining -= sizeof(hdr);
|
||||
|
||||
if (remaining != hdr.len) {
|
||||
BT_ERR("Event payload length is not correct");
|
||||
return NULL;
|
||||
}
|
||||
BT_DBG("len %u", hdr.len);
|
||||
|
||||
buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT);
|
||||
if (!buf) {
|
||||
if (discardable) {
|
||||
BT_DBG("Discardable buffer pool full, ignoring event");
|
||||
} else {
|
||||
BT_ERR("No available event buffers!");
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
net_buf_add_mem(buf, &hdr, sizeof(hdr));
|
||||
net_buf_add_mem(buf, data, remaining);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
static struct net_buf *bt_esp_acl_recv(uint8_t *data, size_t remaining)
|
||||
{
|
||||
struct bt_hci_acl_hdr hdr;
|
||||
struct net_buf *buf;
|
||||
|
||||
if (remaining < sizeof(hdr)) {
|
||||
BT_ERR("Not enough data for ACL header");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
|
||||
if (buf) {
|
||||
memcpy((void *)&hdr, data, sizeof(hdr));
|
||||
data += sizeof(hdr);
|
||||
remaining -= sizeof(hdr);
|
||||
|
||||
net_buf_add_mem(buf, &hdr, sizeof(hdr));
|
||||
} else {
|
||||
BT_ERR("No available ACL buffers!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (remaining != sys_le16_to_cpu(hdr.len)) {
|
||||
BT_ERR("ACL payload length is not correct");
|
||||
net_buf_unref(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BT_DBG("len %u", remaining);
|
||||
net_buf_add_mem(buf, data, remaining);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
static struct net_buf *bt_esp_iso_recv(uint8_t *data, size_t remaining)
|
||||
{
|
||||
struct bt_hci_iso_hdr hdr;
|
||||
struct net_buf *buf;
|
||||
|
||||
if (remaining < sizeof(hdr)) {
|
||||
BT_ERR("Not enough data for ISO header");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buf = bt_buf_get_rx(BT_BUF_ISO_IN, K_NO_WAIT);
|
||||
if (buf) {
|
||||
memcpy((void *)&hdr, data, sizeof(hdr));
|
||||
data += sizeof(hdr);
|
||||
remaining -= sizeof(hdr);
|
||||
|
||||
net_buf_add_mem(buf, &hdr, sizeof(hdr));
|
||||
} else {
|
||||
BT_ERR("No available ISO buffers!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (remaining != sys_le16_to_cpu(hdr.len)) {
|
||||
BT_ERR("ISO payload length is not correct");
|
||||
net_buf_unref(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BT_DBG("len %zu", remaining);
|
||||
net_buf_add_mem(buf, data, remaining);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int hci_esp_host_rcv_pkt(uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint8_t pkt_indicator;
|
||||
struct net_buf *buf = NULL;
|
||||
size_t remaining = len;
|
||||
|
||||
BT_HEXDUMP_DBG(data, len, "host packet data:");
|
||||
|
||||
pkt_indicator = *data++;
|
||||
remaining -= sizeof(pkt_indicator);
|
||||
|
||||
switch (pkt_indicator) {
|
||||
case HCI_EVT:
|
||||
buf = bt_esp_evt_recv(data, remaining);
|
||||
break;
|
||||
|
||||
case HCI_ACL:
|
||||
buf = bt_esp_acl_recv(data, remaining);
|
||||
break;
|
||||
|
||||
case HCI_SCO:
|
||||
buf = bt_esp_iso_recv(data, remaining);
|
||||
break;
|
||||
|
||||
default:
|
||||
BT_ERR("Unknown HCI type %u", pkt_indicator);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (buf) {
|
||||
BT_DBG("Calling bt_recv(%p)", buf);
|
||||
|
||||
bt_recv(buf);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void hci_esp_controller_rcv_pkt_ready(void)
|
||||
{
|
||||
k_sem_give(&hci_send_sem);
|
||||
}
|
||||
|
||||
static esp_vhci_host_callback_t vhci_host_cb = {
|
||||
hci_esp_controller_rcv_pkt_ready,
|
||||
hci_esp_host_rcv_pkt
|
||||
};
|
||||
|
||||
static int bt_esp32_send(struct net_buf *buf)
|
||||
{
|
||||
int err = 0;
|
||||
uint8_t pkt_indicator;
|
||||
|
||||
BT_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
|
||||
|
||||
switch (bt_buf_get_type(buf)) {
|
||||
case BT_BUF_ACL_OUT:
|
||||
pkt_indicator = HCI_ACL;
|
||||
break;
|
||||
case BT_BUF_CMD:
|
||||
pkt_indicator = HCI_CMD;
|
||||
break;
|
||||
case BT_BUF_ISO_OUT:
|
||||
pkt_indicator = HCI_ISO;
|
||||
break;
|
||||
default:
|
||||
BT_ERR("Unknown type %u", bt_buf_get_type(buf));
|
||||
goto done;
|
||||
}
|
||||
net_buf_push_u8(buf, pkt_indicator);
|
||||
|
||||
BT_HEXDUMP_DBG(buf->data, buf->len, "Final HCI buffer:");
|
||||
|
||||
if (!esp_vhci_host_check_send_available()) {
|
||||
BT_WARN("Controller not ready to receive packets");
|
||||
}
|
||||
|
||||
if (k_sem_take(&hci_send_sem, HCI_BT_ESP32_TIMEOUT) == 0) {
|
||||
esp_vhci_host_send_packet(buf->data, buf->len);
|
||||
} else {
|
||||
BT_ERR("Send packet timeout error");
|
||||
err = -ETIMEDOUT;
|
||||
}
|
||||
|
||||
done:
|
||||
net_buf_unref(buf);
|
||||
k_sem_give(&hci_send_sem);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int bt_esp32_ble_init(void)
|
||||
{
|
||||
int ret;
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
|
||||
#ifdef CONFIG_BT_BREDR
|
||||
esp_bt_mode_t mode = ESP_BT_MODE_BTDM;
|
||||
#else
|
||||
esp_bt_mode_t mode = ESP_BT_MODE_BLE;
|
||||
#endif
|
||||
|
||||
ret = esp_bt_controller_init(&bt_cfg);
|
||||
if (ret) {
|
||||
BT_ERR("Bluetooth controller init failed %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_bt_controller_enable(mode);
|
||||
if (ret) {
|
||||
BT_ERR("Bluetooth controller enable failed: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_vhci_host_register_callback(&vhci_host_cb);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bt_esp32_open(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = bt_esp32_ble_init();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
BT_DBG("ESP32 BT started");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct bt_hci_driver drv = {
|
||||
.name = "BT ESP32",
|
||||
.open = bt_esp32_open,
|
||||
.send = bt_esp32_send,
|
||||
.bus = BT_HCI_DRIVER_BUS_IPM,
|
||||
#if defined(CONFIG_BT_DRIVER_QUIRK_NO_AUTO_DLE)
|
||||
.quirks = BT_QUIRK_NO_AUTO_DLE,
|
||||
#endif
|
||||
};
|
||||
|
||||
static int bt_esp32_init(const struct device *unused)
|
||||
{
|
||||
ARG_UNUSED(unused);
|
||||
|
||||
bt_hci_driver_register(&drv);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(bt_esp32_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
|
Loading…
Add table
Add a link
Reference in a new issue