support hci usb h4 for usb bluetooth

This commit is contained in:
sakumisu
2024-01-25 22:03:35 +08:00
parent 24511c4d4b
commit cdfb7c3fb2
8 changed files with 431 additions and 157 deletions

View File

@@ -109,6 +109,7 @@ CherryUSB Host Stack has the following functions
- Support USB Video CLASS - Support USB Video CLASS
- Support USB Audio CLASS - Support USB Audio CLASS
- Support Remote NDIS (RNDIS) - Support Remote NDIS (RNDIS)
- Support USB Bluetooth class (support nimble and zephyr bluetooth stack, support **CLASS:0xE0** or vendor class like cdc acm)
- Support Vendor class - Support Vendor class
- Support USB modeswitch - Support USB modeswitch
- Support multi host with the same USB IP - Support multi host with the same USB IP
@@ -128,6 +129,7 @@ CherryUSB Host Stack resource usage (GCC 10.2 with -O2):
|usbh_audio.c | ~3100 | 128 | 4 + sizeof(struct usbh_audio) * x | 0 | |usbh_audio.c | ~3100 | 128 | 4 + sizeof(struct usbh_audio) * x | 0 |
|usbh_rndis.c | ~3900 | 4096 + 2 * 2048 | sizeof(struct usbh_rndis) * 1 | 0 | |usbh_rndis.c | ~3900 | 4096 + 2 * 2048 | sizeof(struct usbh_rndis) * 1 | 0 |
|usbh_cdc_ecm.c | ~2500 | 2 * 1514 | sizeof(struct usbh_cdc_ecm) * 1 | 0 | |usbh_cdc_ecm.c | ~2500 | 2 * 1514 | sizeof(struct usbh_cdc_ecm) * 1 | 0 |
|usbh_bluetooth.c | ~2300 | 2 * 2048(default) | sizeof(struct usbh_bluetooth) * 1 | 0 |
Among them, `sizeof(struct usbh_hub)` and `sizeof(struct usbh_hubport)` are affected by the following macros Among them, `sizeof(struct usbh_hub)` and `sizeof(struct usbh_hubport)` are affected by the following macros

View File

@@ -107,6 +107,7 @@ CherryUSB Host 协议栈当前实现以下功能:
- Support USB Video CLASS - Support USB Video CLASS
- Support USB Audio CLASS - Support USB Audio CLASS
- 支持 Remote NDIS (RNDIS) - 支持 Remote NDIS (RNDIS)
- 支持 USB Bluetooth (支持 nimble and zephyr bluetooth 协议栈,支持 **CLASS: 0xE0** 或者厂家自定义类,类似于 cdc acm 功能)
- 支持 Vendor 类 class - 支持 Vendor 类 class
- 支持 USB modeswitch - 支持 USB modeswitch
- 支持相同 USB IP 的多主机 - 支持相同 USB IP 的多主机
@@ -126,6 +127,7 @@ CherryUSB Host 协议栈资源占用说明GCC 10.2 with -O2
|usbh_audio.c | ~3100 | 128 | 4 + sizeof(struct usbh_audio) * x | 0 | |usbh_audio.c | ~3100 | 128 | 4 + sizeof(struct usbh_audio) * x | 0 |
|usbh_rndis.c | ~3900 | 4096 + 2 * 2048 | sizeof(struct usbh_rndis) * 1 | 0 | |usbh_rndis.c | ~3900 | 4096 + 2 * 2048 | sizeof(struct usbh_rndis) * 1 | 0 |
|usbh_cdc_ecm.c | ~2500 | 2 * 1514 | sizeof(struct usbh_cdc_ecm) * 1 | 0 | |usbh_cdc_ecm.c | ~2500 | 2 * 1514 | sizeof(struct usbh_cdc_ecm) * 1 | 0 |
|usbh_bluetooth.c | ~2300 | 2 * 2048(default) | sizeof(struct usbh_bluetooth) * 1 | 0 |
其中,`sizeof(struct usbh_hub)``sizeof(struct usbh_hubport)` 受以下宏影响: 其中,`sizeof(struct usbh_hub)``sizeof(struct usbh_hubport)` 受以下宏影响:

View File

@@ -130,6 +130,16 @@
#define CONFIG_USBHOST_MSC_TIMEOUT 5000 #define CONFIG_USBHOST_MSC_TIMEOUT 5000
#endif #endif
#define CONFIG_USBHOST_BLUETOOTH_HCI_H4
// #define CONFIG_USBHOST_BLUETOOTH_HCI_LOG
#ifndef CONFIG_USBHOST_BLUETOOTH_TX_SIZE
#define CONFIG_USBHOST_BLUETOOTH_TX_SIZE 2048
#endif
#ifndef CONFIG_USBHOST_BLUETOOTH_RX_SIZE
#define CONFIG_USBHOST_BLUETOOTH_RX_SIZE 2048
#endif
/* ================ USB Device Port Configuration ================*/ /* ================ USB Device Port Configuration ================*/
#define USBD_IRQHandler USBD_IRQHandler #define USBD_IRQHandler USBD_IRQHandler

View File

@@ -10,34 +10,45 @@
static struct usbh_bluetooth g_bluetooth_class; static struct usbh_bluetooth g_bluetooth_class;
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_cmd_buf[512]; #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_event_buf[512]; USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_tx_buf[1 + CONFIG_USBHOST_BLUETOOTH_TX_SIZE];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_acl_buf[1024]; USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_rx_buf[1 + CONFIG_USBHOST_BLUETOOTH_RX_SIZE];
#else
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_cmd_buf[1 + 256];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_evt_buf[1 + 256];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_tx_buf[1 + CONFIG_USBHOST_BLUETOOTH_TX_SIZE];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_rx_buf[1 + CONFIG_USBHOST_BLUETOOTH_RX_SIZE];
#endif
static int usbh_bluetooth_connect(struct usbh_hubport *hport, uint8_t intf) static int usbh_bluetooth_connect(struct usbh_hubport *hport, uint8_t intf)
{ {
struct usb_endpoint_descriptor *ep_desc; struct usb_endpoint_descriptor *ep_desc;
int ret; int ret = 0;
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
uint8_t mult; uint8_t mult;
uint16_t mps; uint16_t mps;
#endif
struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class; struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
if (hport->config.config_desc.bNumInterfaces == (intf + 1)) { #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
if (intf != 0) {
return 0; return 0;
} }
#endif
memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth)); memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth));
bluetooth_class->hport = hport; bluetooth_class->hport = hport;
bluetooth_class->intf = intf; bluetooth_class->intf = intf;
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
bluetooth_class->num_of_intf_altsettings = hport->config.intf[intf + 1].altsetting_num; bluetooth_class->num_of_intf_altsettings = hport->config.intf[intf + 1].altsetting_num;
#endif
hport->config.intf[intf].priv = bluetooth_class; hport->config.intf[intf].priv = bluetooth_class;
for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) { for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc; ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) { if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
if (ep_desc->bEndpointAddress & 0x80) { if (ep_desc->bEndpointAddress & 0x80) {
USBH_EP_INIT(bluetooth_class->intin, ep_desc); USBH_EP_INIT(bluetooth_class->intin, ep_desc);
@@ -45,14 +56,17 @@ static int usbh_bluetooth_connect(struct usbh_hubport *hport, uint8_t intf)
return -USB_ERR_NOTSUPP; return -USB_ERR_NOTSUPP;
} }
} else { } else {
#endif
if (ep_desc->bEndpointAddress & 0x80) { if (ep_desc->bEndpointAddress & 0x80) {
USBH_EP_INIT(bluetooth_class->bulkin, ep_desc); USBH_EP_INIT(bluetooth_class->bulkin, ep_desc);
} else { } else {
USBH_EP_INIT(bluetooth_class->bulkout, ep_desc); USBH_EP_INIT(bluetooth_class->bulkout, ep_desc);
} }
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
} }
#endif
} }
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
USB_LOG_INFO("Num of altsettings:%u\r\n", bluetooth_class->num_of_intf_altsettings); USB_LOG_INFO("Num of altsettings:%u\r\n", bluetooth_class->num_of_intf_altsettings);
for (uint8_t i = 0; i < bluetooth_class->num_of_intf_altsettings; i++) { for (uint8_t i = 0; i < bluetooth_class->num_of_intf_altsettings; i++) {
@@ -77,7 +91,7 @@ static int usbh_bluetooth_connect(struct usbh_hubport *hport, uint8_t intf)
return ret; return ret;
} }
USB_LOG_INFO("Bluetooth select altsetting 0\r\n"); USB_LOG_INFO("Bluetooth select altsetting 0\r\n");
#endif
snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT); snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT);
USB_LOG_INFO("Register Bluetooth Class:%s\r\n", hport->config.intf[intf].devname); USB_LOG_INFO("Register Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
usbh_bluetooth_run(bluetooth_class); usbh_bluetooth_run(bluetooth_class);
@@ -102,7 +116,7 @@ static int usbh_bluetooth_disconnect(struct usbh_hubport *hport, uint8_t intf)
if (bluetooth_class->bulkout) { if (bluetooth_class->bulkout) {
usbh_kill_urb(&bluetooth_class->bulkout_urb); usbh_kill_urb(&bluetooth_class->bulkout_urb);
} }
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
if (bluetooth_class->intin) { if (bluetooth_class->intin) {
usbh_kill_urb(&bluetooth_class->intin_urb); usbh_kill_urb(&bluetooth_class->intin_urb);
} }
@@ -114,7 +128,7 @@ static int usbh_bluetooth_disconnect(struct usbh_hubport *hport, uint8_t intf)
// if (bluetooth_class->isoin) { // if (bluetooth_class->isoin) {
// usbh_kill_urb(&bluetooth_class->isoinin_urb); // usbh_kill_urb(&bluetooth_class->isoinin_urb);
// } // }
#endif
if (hport->config.intf[intf].devname[0] != '\0') { if (hport->config.intf[intf].devname[0] != '\0') {
USB_LOG_INFO("Unregister Bluetooth Class:%s\r\n", hport->config.intf[intf].devname); USB_LOG_INFO("Unregister Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
usbh_bluetooth_stop(bluetooth_class); usbh_bluetooth_stop(bluetooth_class);
@@ -126,22 +140,26 @@ static int usbh_bluetooth_disconnect(struct usbh_hubport *hport, uint8_t intf)
return ret; return ret;
} }
int usbh_bluetooth_hci_cmd(uint8_t *buffer, uint32_t buflen) #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_LOG
static void usbh_bluetooth_hci_dump(uint8_t *data, uint32_t len)
{ {
struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class; uint32_t i = 0;
struct usb_setup_packet *setup = bluetooth_class->hport->setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE; for (i = 0; i < len; i++) {
setup->bRequest = 0x00; if (i % 16 == 0) {
setup->wValue = 0; USB_LOG_RAW("\r\n");
setup->wIndex = bluetooth_class->intf; }
setup->wLength = buflen;
memcpy(g_bluetooth_cmd_buf, buffer, buflen); USB_LOG_RAW("%02x ", data[i]);
return usbh_control_transfer(bluetooth_class->hport, setup, g_bluetooth_cmd_buf); }
USB_LOG_RAW("\r\n");
} }
#else
#define usbh_bluetooth_hci_dump(data, len)
#endif
int usbh_bluetooth_hci_acl_out(uint8_t *buffer, uint32_t buflen) static int usbh_bluetooth_hci_bulk_out(uint8_t *buffer, uint32_t buflen)
{ {
struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class; struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
struct usbh_urb *urb = &bluetooth_class->bulkout_urb; struct usbh_urb *urb = &bluetooth_class->bulkout_urb;
@@ -155,7 +173,96 @@ int usbh_bluetooth_hci_acl_out(uint8_t *buffer, uint32_t buflen)
return ret; return ret;
} }
void usbh_bluetooth_hci_event_rx_thread(void *argument) #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
{
int ret;
g_bluetooth_tx_buf[0] = hci_type;
memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
ret = usbh_bluetooth_hci_bulk_out(g_bluetooth_tx_buf, buflen + 1);
return ret;
}
void usbh_bluetooth_hci_rx_thread(void *argument)
{
int ret;
uint32_t ep_mps;
uint8_t retry = 0;
uint16_t actual_len = 0;
ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);
USB_LOG_INFO("Create hc rx thread\r\n");
while (1) {
usbh_bulk_urb_fill(&g_bluetooth_class.bulkin_urb, g_bluetooth_class.hport, g_bluetooth_class.bulkin, &g_bluetooth_rx_buf[actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
if (ret < 0) {
if (ret == -USB_ERR_SHUTDOWN) {
goto delete;
} else {
retry++;
if (retry == 3) {
retry = 0;
goto delete;
}
continue;
}
}
actual_len += g_bluetooth_class.bulkin_urb.actual_length;
if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len);
usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len);
actual_len = 0;
} else {
/* read continue util read short packet */
}
}
// clang-format off
delete :
USB_LOG_INFO("Delete hc acl rx thread\r\n");
usb_osal_thread_delete(NULL);
// clang-format on
}
#else
static int usbh_bluetooth_hci_cmd(uint8_t *buffer, uint32_t buflen)
{
struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
struct usb_setup_packet *setup = bluetooth_class->hport->setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = 0x00;
setup->wValue = 0;
setup->wIndex = bluetooth_class->intf;
setup->wLength = buflen;
return usbh_control_transfer(bluetooth_class->hport, setup, buffer);
}
int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
{
int ret;
if (hci_type == USB_BLUETOOTH_HCI_CMD) {
g_bluetooth_cmd_buf[0] = USB_BLUETOOTH_HCI_CMD;
memcpy(&g_bluetooth_cmd_buf[1], buffer, buflen);
usbh_bluetooth_hci_dump(g_bluetooth_cmd_buf, buflen + 1);
ret = usbh_bluetooth_hci_cmd(&g_bluetooth_cmd_buf[1], buflen);
} else if (hci_type == USB_BLUETOOTH_HCI_ACL) {
g_bluetooth_tx_buf[0] = USB_BLUETOOTH_HCI_ACL;
memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
ret = usbh_bluetooth_hci_bulk_out(&g_bluetooth_tx_buf[1], buflen);
} else {
ret = -1;
}
return ret;
}
void usbh_bluetooth_hci_evt_rx_thread(void *argument)
{ {
int ret; int ret;
uint32_t ep_mps; uint32_t ep_mps;
@@ -168,7 +275,7 @@ void usbh_bluetooth_hci_event_rx_thread(void *argument)
USB_LOG_INFO("Create hc event rx thread\r\n"); USB_LOG_INFO("Create hc event rx thread\r\n");
while (1) { while (1) {
usbh_int_urb_fill(&g_bluetooth_class.intin_urb, g_bluetooth_class.hport, g_bluetooth_class.intin, &g_bluetooth_event_buf[actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL); usbh_int_urb_fill(&g_bluetooth_class.intin_urb, g_bluetooth_class.hport, g_bluetooth_class.intin, &g_bluetooth_evt_buf[1 + actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
ret = usbh_submit_urb(&g_bluetooth_class.intin_urb); ret = usbh_submit_urb(&g_bluetooth_class.intin_urb);
if (ret < 0) { if (ret < 0) {
if (ret == -USB_ERR_SHUTDOWN) { if (ret == -USB_ERR_SHUTDOWN) {
@@ -188,7 +295,9 @@ void usbh_bluetooth_hci_event_rx_thread(void *argument)
} }
actual_len += g_bluetooth_class.intin_urb.actual_length; actual_len += g_bluetooth_class.intin_urb.actual_length;
if (g_bluetooth_class.intin_urb.actual_length != ep_mps) { if (g_bluetooth_class.intin_urb.actual_length != ep_mps) {
usbh_bluetooth_hci_rx_callback(USB_BLUETOOTH_HCI_EVT, g_bluetooth_event_buf, actual_len); g_bluetooth_evt_buf[0] = USB_BLUETOOTH_HCI_EVT;
usbh_bluetooth_hci_dump(g_bluetooth_evt_buf, actual_len + 1);
usbh_bluetooth_hci_read_callback(g_bluetooth_evt_buf, actual_len + 1);
actual_len = 0; actual_len = 0;
} else { } else {
/* read continue util read short packet */ /* read continue util read short packet */
@@ -213,7 +322,7 @@ void usbh_bluetooth_hci_acl_rx_thread(void *argument)
USB_LOG_INFO("Create hc acl rx thread\r\n"); USB_LOG_INFO("Create hc acl rx thread\r\n");
while (1) { while (1) {
usbh_bulk_urb_fill(&g_bluetooth_class.bulkin_urb, g_bluetooth_class.hport, g_bluetooth_class.bulkin, &g_bluetooth_acl_buf[actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL); usbh_bulk_urb_fill(&g_bluetooth_class.bulkin_urb, g_bluetooth_class.hport, g_bluetooth_class.bulkin, &g_bluetooth_rx_buf[1 + actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb); ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
if (ret < 0) { if (ret < 0) {
if (ret == -USB_ERR_SHUTDOWN) { if (ret == -USB_ERR_SHUTDOWN) {
@@ -229,7 +338,9 @@ void usbh_bluetooth_hci_acl_rx_thread(void *argument)
} }
actual_len += g_bluetooth_class.bulkin_urb.actual_length; actual_len += g_bluetooth_class.bulkin_urb.actual_length;
if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) { if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
usbh_bluetooth_hci_rx_callback(USB_BLUETOOTH_HCI_ACL_IN, g_bluetooth_acl_buf, actual_len); g_bluetooth_rx_buf[0] = USB_BLUETOOTH_HCI_ACL;
usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len + 1);
usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len + 1);
actual_len = 0; actual_len = 0;
} else { } else {
/* read continue util read short packet */ /* read continue util read short packet */
@@ -241,8 +352,9 @@ delete :
usb_osal_thread_delete(NULL); usb_osal_thread_delete(NULL);
// clang-format on // clang-format on
} }
#endif
__WEAK void usbh_bluetooth_hci_rx_callback(uint8_t hci_type, uint8_t *data, uint32_t len) __WEAK void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
{ {
} }
@@ -269,3 +381,15 @@ CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_class_info = {
.pid = 0x00, .pid = 0x00,
.class_driver = &bluetooth_class_driver .class_driver = &bluetooth_class_driver
}; };
#ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_h4_nrf_class_info = {
.match_flags = USB_CLASS_MATCH_VENDOR | USB_CLASS_MATCH_PRODUCT | USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
.class = 0xff,
.subclass = 0x00,
.protocol = 0x00,
.vid = 0x2fe3,
.pid = 0x000c,
.class_driver = &bluetooth_class_driver
};
#endif

View File

@@ -6,27 +6,29 @@
#ifndef USBH_BLUETOOTH_H #ifndef USBH_BLUETOOTH_H
#define USBH_BLUETOOTH_H #define USBH_BLUETOOTH_H
#define USB_BLUETOOTH_HCI_CMD 0 #define USB_BLUETOOTH_HCI_NONE 0x00
#define USB_BLUETOOTH_HCI_EVT 1 #define USB_BLUETOOTH_HCI_CMD 0x01
#define USB_BLUETOOTH_HCI_ACL_OUT 2 #define USB_BLUETOOTH_HCI_ACL 0x02
#define USB_BLUETOOTH_HCI_ACL_IN 3 #define USB_BLUETOOTH_HCI_SCO 0x03
#define USB_BLUETOOTH_HCI_ISO_OUT 4 #define USB_BLUETOOTH_HCI_EVT 0x04
#define USB_BLUETOOTH_HCI_ISO_IN 5 #define USB_BLUETOOTH_HCI_ISO 0x05
struct usbh_bluetooth { struct usbh_bluetooth {
struct usbh_hubport *hport; struct usbh_hubport *hport;
uint8_t intf;
struct usb_endpoint_descriptor *bulkin; /* Bulk IN endpoint */ struct usb_endpoint_descriptor *bulkin; /* Bulk IN endpoint */
struct usb_endpoint_descriptor *bulkout; /* Bulk OUT endpoint */ struct usb_endpoint_descriptor *bulkout; /* Bulk OUT endpoint */
struct usb_endpoint_descriptor *intin; /* INTR endpoint */
struct usb_endpoint_descriptor *isoin; /* Bulk IN endpoint */
struct usb_endpoint_descriptor *isoout; /* Bulk OUT endpoint */
struct usbh_urb bulkin_urb; /* Bulk IN urb */ struct usbh_urb bulkin_urb; /* Bulk IN urb */
struct usbh_urb bulkout_urb; /* Bulk OUT urb */ struct usbh_urb bulkout_urb; /* Bulk OUT urb */
struct usbh_urb intin_urb; /* INTR IN urb */ #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
struct usbh_urb *isoin_urb; /* Bulk IN urb */ struct usb_endpoint_descriptor *intin; /* INTR endpoint */
struct usbh_urb *isoout_urb; /* Bulk OUT urb */ struct usb_endpoint_descriptor *isoin; /* Bulk IN endpoint */
uint8_t intf; struct usb_endpoint_descriptor *isoout; /* Bulk OUT endpoint */
struct usbh_urb intin_urb; /* INTR IN urb */
struct usbh_urb *isoin_urb; /* Bulk IN urb */
struct usbh_urb *isoout_urb; /* Bulk OUT urb */
uint8_t num_of_intf_altsettings; uint8_t num_of_intf_altsettings;
#endif
}; };
#ifdef __cplusplus #ifdef __cplusplus
@@ -36,17 +38,14 @@ extern "C" {
void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class); void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class);
void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class); void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class);
/* OpCode(OCF+OGF:2bytes) + ParamLength + Paramas */ int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen);
int usbh_bluetooth_hci_cmd(uint8_t *buffer, uint32_t buflen); void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len);
/* Handle (12bits) + Packet_Boundary_Flag(2bits) + BC_flag(2bits) + data_len(2bytes) + data */ #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
int usbh_bluetooth_hci_acl_out(uint8_t *buffer, uint32_t buflen); void usbh_bluetooth_hci_rx_thread(void *argument);
void usbh_bluetooth_hci_event_rx_thread(void *argument); #else
void usbh_bluetooth_hci_evt_rx_thread(void *argument);
void usbh_bluetooth_hci_acl_rx_thread(void *argument); void usbh_bluetooth_hci_acl_rx_thread(void *argument);
#endif
/* USB_BLUETOOTH_HCI_EVT : EventCode(1byte) + ParamLength + Parama0 + Parama1 + Parama2;
USB_BLUETOOTH_HCI_ACL : Handle (12bits) + Packet_Boundary_Flag(2bits) + BC_flag(2bits) + data_len(2bytes) + data
*/
void usbh_bluetooth_hci_rx_callback(uint8_t hci_type, uint8_t *data, uint32_t len);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -1,5 +1,9 @@
## Note ## Note
当前支持 nimble 主线 1.6.0 版本,**禁止使用其余软件包或者第三方包**
- ble_hs_startup_set_evmask_tx 函数中 BLE_HCI_OCF_CB_SET_EVENT_MASK2 功能需要关闭 - ble_hs_startup_set_evmask_tx 函数中 BLE_HCI_OCF_CB_SET_EVENT_MASK2 功能需要关闭
``` ```
@@ -19,14 +23,147 @@
``` ```
- 如果使用 rt-thread 中的软件包,请删除软件包中 SConscript 文件以下内容 - 主线中头文件和源文件参与编译例子
``` ```
# if rtconfig.CROSS_TOOL == 'keil': 源文件
#LOCAL_CCFLAGS += ' --gnu --diag_suppress=111'
# __BYTE_ORDER__ & __ORDER_BIG_ENDIAN__ & __ORDER_LITTLE_ENDIAN__ is not defined in keil, the specific values comes from gcc. nimble/host/src/ble_att_clt.c
# CPPDEFINES.append('__ORDER_LITTLE_ENDIAN__=1234') nimble/host/src/ble_att_cmd.c
# CPPDEFINES.append('__ORDER_BIG_ENDIAN__=4321') nimble/host/src/ble_att_svr.c
# CPPDEFINES.append('__BYTE_ORDER__=1234') nimble/host/src/ble_att.c
nimble/host/src/ble_audio_broadcast.c
nimble/host/src/ble_dtm.c
nimble/host/src/ble_eatt.c
nimble/host/src/ble_eddystone.c
nimble/host/src/ble_gap.c
nimble/host/src/ble_gattc.c
nimble/host/src/ble_gatts.c
nimble/host/src/ble_gatts_lcl.c
nimble/host/src/ble_hs.c
nimble/host/src/ble_hs_adv.c
nimble/host/src/ble_hs_atomic.c
nimble/host/src/ble_hs_cfg.c
nimble/host/src/ble_hs_conn.c
nimble/host/src/ble_hs_flow.c
nimble/host/src/ble_hs_hci.c
nimble/host/src/ble_hs_hci_cmd.c
nimble/host/src/ble_hs_hci_evt.c
nimble/host/src/ble_hs_hci_util.c
nimble/host/src/ble_hs_id.c
nimble/host/src/ble_hs_log.c
nimble/host/src/ble_hs_mbuf.c
nimble/host/src/ble_hs_misc.c
nimble/host/src/ble_hs_mqueue.c
nimble/host/src/ble_hs_periodic_sync.c
nimble/host/src/ble_hs_pvcy.c
nimble/host/src/ble_hs_shutdown.c
nimble/host/src/ble_hs_startup.c
nimble/host/src/ble_hs_stop.c
nimble/host/src/ble_ibeacon.c
nimble/host/src/ble_iso.c
nimble/host/src/ble_l2cap.c
nimble/host/src/ble_l2cap_coc.c
nimble/host/src/ble_l2cap_sig.c
nimble/host/src/ble_l2cap_sig_cmd.c
nimble/host/src/ble_sm.c
nimble/host/src/ble_sm_alg.c
nimble/host/src/ble_sm_cmd.c
nimble/host/src/ble_sm_lgcy.c
nimble/host/src/ble_sm_sc.c
nimble/host/src/ble_store.c
nimble/host/src/ble_store_util.c
nimble/host/src/ble_uuid.c
nimble/host/services/gap/src/ble_svc_gap.c
nimble/host/services/gatt/src/ble_svc_gatt.c
nimble/host/services/bas/src/ble_svc_bas.c
nimble/host/services/dis/src/ble_svc_dis.c
# nimble/host/store/config/src/ble_store_config.c
# nimble/host/store/config/src/ble_store_config_conf.c
nimble/host/store/ram/src/ble_store_ram.c
nimble/host/util/src/addr.c
nimble/transport/common/hci_h4/src/hci_h4.c
nimble/transport/src/transport.c
ext/tinycrypt/src/aes_decrypt.c
ext/tinycrypt/src/aes_encrypt.c
ext/tinycrypt/src/cbc_mode.c
ext/tinycrypt/src/ccm_mode.c
ext/tinycrypt/src/cmac_mode.c
ext/tinycrypt/src/ctr_mode.c
ext/tinycrypt/src/ctr_prng.c
ext/tinycrypt/src/ecc.c
ext/tinycrypt/src/ecc_dh.c
ext/tinycrypt/src/ecc_dsa.c
ext/tinycrypt/src/ecc_platform_specific.c
ext/tinycrypt/src/hmac.c
ext/tinycrypt/src/hmac_prng.c
ext/tinycrypt/src/sha256.c
ext/tinycrypt/src/utils.c
===========头文件=================
nimble/include
nimble/host/include
nimble/host/services/ans/include
nimble/host/services/bas/include
nimble/host/services/dis/include
nimble/host/services/gap/include
nimble/host/services/gatt/include
nimble/host/services/ias/include
nimble/host/services/lls/include
nimble/host/services/tps/include
nimble/host/store/ram/include
nimble/host/util/include
nimble/transport/include
ext/tinycrypt/include
nimble/transport/common/hci_h4/include
``` ```
- porting 请禁止使用 nimble 官方源码,请使用 porting 目录下的文件
```
头文件
porting/nimble/include
porting/npl/freertos/include
源文件
porting/nimble/src/endian.c
porting/nimble/src/mem.c
porting/nimble/src/nimble_port.c
porting/nimble/src/os_mbuf.c
porting/nimble/src/os_mempool.c
porting/nimble/src/os_msys_init.c
porting/npl/freertos/src/nimble_port_freertos.c
porting/npl/freertos/src/npl_os_freertos.c
```
- 初始化 nimble请注意必须在 usb bluetooth 识别以后才能初始化
```
#include "nimble/nimble_port.h"
#include "usbh_core.h"
void nimble_thread_entry(void *parameter)
{
nimble_port_run();
}
void nimble_init(void)
{
nimble_port_init();
usb_osal_thread_create("nimble", 2048, CONFIG_USBHOST_PSC_PRIO + 1, nimble_thread_entry, NULL);
}
```

View File

@@ -18,23 +18,6 @@ void ble_transport_ll_init(void)
/* nothing here */ /* nothing here */
} }
static void hci_dump(uint8_t hci_type, uint8_t *data, uint32_t len)
{
uint32_t i = 0;
USB_LOG_DBG("\r\nhci type:%u", hci_type);
for (i = 0; i < len; i++) {
if (i % 16 == 0) {
USB_LOG_DBG("\r\n");
}
USB_LOG_DBG("%02x ", data[i]);
}
USB_LOG_DBG("\r\n");
}
static int hci_usb_frame_cb(uint8_t pkt_type, void *data) static int hci_usb_frame_cb(uint8_t pkt_type, void *data)
{ {
switch (pkt_type) { switch (pkt_type) {
@@ -49,37 +32,16 @@ static int hci_usb_frame_cb(uint8_t pkt_type, void *data)
return -1; return -1;
} }
static int ble_usb_transport_init(void) void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
{ {
hci_h4_sm_init(&g_hci_h4sm, &hci_h4_allocs_from_ll, hci_usb_frame_cb); size_t remaining = len;
return 0; uint8_t pkt_indicator;
}
void usbh_bluetooth_hci_rx_callback(uint8_t hci_type, uint8_t *data, uint32_t len) pkt_indicator = *data++;
{ remaining -= sizeof(pkt_indicator);
uint8_t pkt_type = 0;
switch (hci_type) { hci_h4_sm_rx(&g_hci_h4sm, &pkt_indicator, 1);
case USB_BLUETOOTH_HCI_EVT: hci_h4_sm_rx(&g_hci_h4sm, data, remaining);
pkt_type = HCI_H4_EVT;
break;
case USB_BLUETOOTH_HCI_ACL_IN:
pkt_type = HCI_H4_ACL;
break;
// case USB_BLUETOOTH_HCI_ISO_IN:
// break;
default:
USB_LOG_ERR("Unknown HCI type %u\r\n", hci_type);
return;
}
hci_dump(pkt_type, data, len);
hci_h4_sm_rx(&g_hci_h4sm, &pkt_type, 1);
hci_h4_sm_rx(&g_hci_h4sm, data, len);
} }
int ble_transport_to_ll_cmd_impl(void *buf) int ble_transport_to_ll_cmd_impl(void *buf)
@@ -88,9 +50,7 @@ int ble_transport_to_ll_cmd_impl(void *buf)
uint8_t *cmd_pkt_data = (uint8_t *)buf; uint8_t *cmd_pkt_data = (uint8_t *)buf;
size_t pkt_len = cmd_pkt_data[2] + 3; size_t pkt_len = cmd_pkt_data[2] + 3;
hci_dump(HCI_H4_CMD, buf, pkt_len); ret = usbh_bluetooth_hci_write(USB_BLUETOOTH_HCI_CMD, buf, pkt_len);
ret = usbh_bluetooth_hci_cmd(buf, pkt_len);
if (ret < 0) { if (ret < 0) {
ret = BLE_ERR_MEM_CAPACITY; ret = BLE_ERR_MEM_CAPACITY;
} else { } else {
@@ -107,9 +67,7 @@ int ble_transport_to_ll_acl_impl(struct os_mbuf *om)
int ret = 0; int ret = 0;
while (x != NULL) { while (x != NULL) {
hci_dump(HCI_H4_ACL, x->om_data, x->om_len); ret = usbh_bluetooth_hci_write(USB_BLUETOOTH_HCI_ACL, x->om_data, x->om_len);
ret = usbh_bluetooth_hci_acl_out(x->om_data, x->om_len);
if (ret < 0) { if (ret < 0) {
ret = BLE_ERR_MEM_CAPACITY; ret = BLE_ERR_MEM_CAPACITY;
break; break;
@@ -124,7 +82,6 @@ int ble_transport_to_ll_acl_impl(struct os_mbuf *om)
return ret; return ret;
} }
__WEAK void usbh_bluetooth_run_callback(void) __WEAK void usbh_bluetooth_run_callback(void)
{ {
/* bt_enable() */ /* bt_enable() */
@@ -137,11 +94,14 @@ __WEAK void usbh_bluetooth_stop_callback(void)
void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class) void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
{ {
ble_usb_transport_init(); hci_h4_sm_init(&g_hci_h4sm, &hci_h4_allocs_from_ll, hci_usb_frame_cb);
usb_osal_thread_create("ble_event", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_event_rx_thread, NULL); #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
usb_osal_thread_create("ble_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_rx_thread, NULL);
#else
usb_osal_thread_create("ble_evt", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_evt_rx_thread, NULL);
usb_osal_thread_create("ble_acl", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_acl_rx_thread, NULL); usb_osal_thread_create("ble_acl", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_acl_rx_thread, NULL);
#endif
usbh_bluetooth_run_callback(); usbh_bluetooth_run_callback();
} }

View File

@@ -1,31 +1,51 @@
/*
* Copyright (c) 2024, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "usbh_core.h" #include "usbh_core.h"
#include "usbh_bluetooth.h" #include "usbh_bluetooth.h"
#include "byteorder.h" #include <sys/byteorder.h>
#include "hci_host.h" #include <drivers/bluetooth/hci_driver.h>
#include "hci_driver.h"
static void hci_dump(uint8_t hci_type, uint8_t *data, uint32_t len) #ifndef CONFIG_BT_RECV_IS_RX_THREAD
#error usb bluetooth must enable CONFIG_BT_RECV_IS_RX_THREAD
#endif
/* compatible with low version that less than v2.7.5 */
/* @brief The HCI event shall be given to bt_recv_prio */
#define __BT_HCI_EVT_FLAG_RECV_PRIO BIT(0)
/* @brief The HCI event shall be given to bt_recv. */
#define __BT_HCI_EVT_FLAG_RECV BIT(1)
/** @brief Get HCI event flags.
*
* Helper for the HCI driver to get HCI event flags that describes rules that.
* must be followed.
*
* When CONFIG_BT_RECV_IS_RX_THREAD is enabled the flags
* __BT_HCI_EVT_FLAG_RECV and __BT_HCI_EVT_FLAG_RECV_PRIO indicates if the event
* should be given to bt_recv or bt_recv_prio.
*
* @param evt HCI event code.
*
* @return HCI event flags for the specified event.
*/
static inline uint8_t __bt_hci_evt_get_flags(uint8_t evt)
{ {
uint32_t i = 0; switch (evt) {
case BT_HCI_EVT_DISCONN_COMPLETE:
USB_LOG_DBG("hci type:%u\r\n", hci_type); return __BT_HCI_EVT_FLAG_RECV | __BT_HCI_EVT_FLAG_RECV_PRIO;
/* fallthrough */
for (i = 0; i < len; i++) { #if defined(CONFIG_BT_CONN) || defined(CONFIG_BT_ISO)
if (i % 16 == 0) { case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
USB_LOG_DBG("\r\n"); #if defined(CONFIG_BT_CONN)
} case BT_HCI_EVT_DATA_BUF_OVERFLOW:
#endif /* defined(CONFIG_BT_CONN) */
USB_LOG_DBG("%02x ", data[i]); #endif /* CONFIG_BT_CONN || CONFIG_BT_ISO */
case BT_HCI_EVT_CMD_COMPLETE:
case BT_HCI_EVT_CMD_STATUS:
return __BT_HCI_EVT_FLAG_RECV_PRIO;
default:
return __BT_HCI_EVT_FLAG_RECV;
} }
USB_LOG_DBG("\r\n");
} }
static bool is_hci_event_discardable(const uint8_t *evt_data) static bool is_hci_event_discardable(const uint8_t *evt_data)
@@ -184,23 +204,25 @@ static struct net_buf *usbh_bt_iso_recv(uint8_t *data, size_t remaining)
return buf; return buf;
} }
static int usbh_hci_host_rcv_pkt(uint8_t pkt_indicator, uint8_t *data, uint16_t len) static int usbh_hci_host_rcv_pkt(uint8_t *data, uint32_t len)
{ {
struct net_buf *buf = NULL; struct net_buf *buf = NULL;
size_t remaining = len; size_t remaining = len;
bool prio = true; uint8_t pkt_indicator;
pkt_indicator = *data++;
remaining -= sizeof(pkt_indicator);
switch (pkt_indicator) { switch (pkt_indicator) {
case USB_BLUETOOTH_HCI_EVT: case USB_BLUETOOTH_HCI_EVT:
buf = usbh_bt_evt_recv(data, remaining); buf = usbh_bt_evt_recv(data, remaining);
break; break;
case USB_BLUETOOTH_HCI_ACL_IN: case USB_BLUETOOTH_HCI_ACL:
buf = usbh_bt_acl_recv(data, remaining); buf = usbh_bt_acl_recv(data, remaining);
prio = false;
break; break;
case USB_BLUETOOTH_HCI_ISO_IN: case USB_BLUETOOTH_HCI_SCO:
buf = usbh_bt_iso_recv(data, remaining); buf = usbh_bt_iso_recv(data, remaining);
break; break;
@@ -209,10 +231,19 @@ static int usbh_hci_host_rcv_pkt(uint8_t pkt_indicator, uint8_t *data, uint16_t
return -1; return -1;
} }
hci_dump(pkt_indicator, buf->data, buf->len);
if (buf) { if (buf) {
bt_recv(buf); if (pkt_indicator == USB_BLUETOOTH_HCI_EVT) {
struct bt_hci_evt_hdr *hdr = (void *)buf->data;
uint8_t evt_flags = __bt_hci_evt_get_flags(hdr->evt);
if (evt_flags & __BT_HCI_EVT_FLAG_RECV_PRIO) {
bt_recv_prio(buf);
} else {
bt_recv(buf);
}
} else {
bt_recv(buf);
}
} }
return 0; return 0;
@@ -226,23 +257,29 @@ static int bt_usbh_open(void)
static int bt_usbh_send(struct net_buf *buf) static int bt_usbh_send(struct net_buf *buf)
{ {
int err = 0; int err = 0;
uint8_t pkt_indicator = bt_buf_get_type(buf); uint8_t pkt_indicator;
hci_dump(pkt_indicator, buf->data, buf->len); switch (bt_buf_get_type(buf)) {
switch (pkt_indicator) {
case BT_BUF_ACL_OUT: case BT_BUF_ACL_OUT:
usbh_bluetooth_hci_acl_out(buf->data, buf->len); pkt_indicator = USB_BLUETOOTH_HCI_ACL;
break; break;
case BT_BUF_CMD: case BT_BUF_CMD:
usbh_bluetooth_hci_cmd(buf->data, buf->len); pkt_indicator = USB_BLUETOOTH_HCI_CMD;
break; break;
case BT_BUF_ISO_OUT: case BT_BUF_ISO_OUT:
pkt_indicator = USB_BLUETOOTH_HCI_ISO;
break; break;
default: default:
USB_LOG_ERR("Unknown type %u\r\n", pkt_indicator); USB_LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
goto done; goto done;
} }
err = usbh_bluetooth_hci_write(pkt_indicator, buf->data, buf->len);
if (err < 0) {
err = 255;
} else {
err = 0;
}
done: done:
net_buf_unref(buf); net_buf_unref(buf);
@@ -250,7 +287,7 @@ done:
} }
static const struct bt_hci_driver usbh_drv = { static const struct bt_hci_driver usbh_drv = {
.name = "usbhost btble", .name = "hci_usb",
.open = bt_usbh_open, .open = bt_usbh_open,
.send = bt_usbh_send, .send = bt_usbh_send,
.bus = BT_HCI_DRIVER_BUS_USB, .bus = BT_HCI_DRIVER_BUS_USB,
@@ -273,9 +310,12 @@ void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
{ {
bt_hci_driver_register(&usbh_drv); bt_hci_driver_register(&usbh_drv);
usb_osal_thread_create("ble_event", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_event_rx_thread, NULL); #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
usb_osal_thread_create("ble_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_rx_thread, NULL);
#else
usb_osal_thread_create("ble_evt", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_evt_rx_thread, NULL);
usb_osal_thread_create("ble_acl", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_acl_rx_thread, NULL); usb_osal_thread_create("ble_acl", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_acl_rx_thread, NULL);
#endif
usbh_bluetooth_run_callback(); usbh_bluetooth_run_callback();
} }
@@ -284,7 +324,7 @@ void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class)
usbh_bluetooth_stop_callback(); usbh_bluetooth_stop_callback();
} }
void usbh_bluetooth_hci_rx_callback(uint8_t hci_type, uint8_t *data, uint32_t len) void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
{ {
usbh_hci_host_rcv_pkt(hci_type, data, len); usbh_hci_host_rcv_pkt(data, len);
} }