mirror of
https://github.com/espressif/esp-nimble.git
synced 2026-09-19 07:29:54 +00:00
nimble/transport: Update USB transport
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you 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.
|
||||
#
|
||||
|
||||
syscfg.defs:
|
||||
BLE_TRANS_NRF5340_SYSINIT_STAGE:
|
||||
description: >
|
||||
Sysinit stage for the RAM BLE transport.
|
||||
value: 100
|
||||
|
||||
syscfg.vals.'!BLE_HCI_BRIDGE:
|
||||
BLE_HCI_EVT_HI_BUF_COUNT: 2
|
||||
BLE_HCI_EVT_LO_BUF_COUNT: 8
|
||||
BLE_ACL_BUF_COUNT: 4
|
||||
BLE_ACL_BUF_SIZE: 255
|
||||
|
||||
syscfg.vals.'!BLE_HCI_BRIDGE && !BLE_EXT_ADV':
|
||||
BLE_HCI_EVT_BUF_SIZE: 70
|
||||
|
||||
syscfg.vals.'!BLE_HCI_BRIDGE && BLE_EXT_ADV':
|
||||
BLE_HCI_EVT_BUF_SIZE: 257
|
||||
|
||||
syscfg.restrictions:
|
||||
- '!(BLE_HOST && BLE_CONTROLLER)'
|
||||
@@ -35,6 +35,3 @@ pkg.deps:
|
||||
|
||||
pkg.apis:
|
||||
- ble_transport
|
||||
|
||||
pkg.init:
|
||||
ble_hci_usb_init: 'MYNEWT_VAL(BLE_TRANS_USB_SYSINIT_STAGE)'
|
||||
|
||||
@@ -25,78 +25,20 @@
|
||||
#include "mem/mem.h"
|
||||
|
||||
#include "nimble/ble.h"
|
||||
#include "nimble/ble_hci_trans.h"
|
||||
#include "nimble/hci_common.h"
|
||||
#include "nimble/transport.h"
|
||||
|
||||
#include <class/bth/bth_device.h>
|
||||
|
||||
/*
|
||||
* The MBUF payload size must accommodate the HCI data header size plus the
|
||||
* maximum ACL data packet length. The ACL block size is the size of the
|
||||
* mbufs we will allocate.
|
||||
*/
|
||||
#define ACL_BLOCK_SIZE OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \
|
||||
+ BLE_MBUF_MEMBLOCK_OVERHEAD \
|
||||
+ BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT)
|
||||
|
||||
struct usb_ble_hci_pool_cmd {
|
||||
uint8_t cmd[BLE_HCI_TRANS_CMD_SZ];
|
||||
bool allocated;
|
||||
};
|
||||
|
||||
/* (Pseudo)pool for HCI commands */
|
||||
static struct usb_ble_hci_pool_cmd usb_ble_hci_pool_cmd;
|
||||
|
||||
static ble_hci_trans_rx_cmd_fn *ble_hci_usb_rx_cmd_ll_cb;
|
||||
static void *ble_hci_usb_rx_cmd_ll_arg;
|
||||
|
||||
static ble_hci_trans_rx_acl_fn *ble_hci_usb_rx_acl_ll_cb;
|
||||
static void *ble_hci_usb_rx_acl_ll_arg;
|
||||
|
||||
static struct os_mempool ble_hci_usb_evt_hi_pool;
|
||||
static os_membuf_t ble_hci_usb_evt_hi_buf[
|
||||
OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
|
||||
MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
|
||||
];
|
||||
|
||||
static struct os_mempool ble_hci_usb_evt_lo_pool;
|
||||
static os_membuf_t ble_hci_usb_evt_lo_buf[
|
||||
OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
|
||||
MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
|
||||
];
|
||||
|
||||
static uint8_t ble_hci_pool_acl_mempool_buf[
|
||||
OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_ACL_BUF_COUNT),
|
||||
ACL_BLOCK_SIZE)];
|
||||
static struct os_mempool ble_hci_pool_acl_mempool;
|
||||
static struct os_mbuf_pool ble_hci_pool_acl_mbuf_pool;
|
||||
|
||||
static struct os_mbuf *incoming_acl_data;
|
||||
|
||||
static struct os_mbuf *
|
||||
ble_hci_trans_acl_buf_alloc(void)
|
||||
{
|
||||
struct os_mbuf *m;
|
||||
#define TX_Q_SIZE (MYNEWT_VAL(BLE_TRANSPORT_ACL_FROM_LL_COUNT) + \
|
||||
MYNEWT_VAL(BLE_TRANSPORT_EVT_COUNT) + \
|
||||
MYNEWT_VAL(BLE_TRANSPORT_EVT_DISCARDABLE_COUNT))
|
||||
|
||||
m = os_mbuf_get_pkthdr(&ble_hci_pool_acl_mbuf_pool,
|
||||
sizeof(struct ble_mbuf_hdr));
|
||||
return m;
|
||||
}
|
||||
|
||||
void
|
||||
ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb,
|
||||
void *cmd_arg,
|
||||
ble_hci_trans_rx_acl_fn *acl_cb,
|
||||
void *acl_arg)
|
||||
{
|
||||
ble_hci_usb_rx_cmd_ll_cb = cmd_cb;
|
||||
ble_hci_usb_rx_cmd_ll_arg = cmd_arg;
|
||||
ble_hci_usb_rx_acl_ll_cb = acl_cb;
|
||||
ble_hci_usb_rx_acl_ll_arg = acl_arg;
|
||||
}
|
||||
|
||||
#define BLE_HCI_USB_EVT_COUNT \
|
||||
(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT) + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT))
|
||||
(MYNEWT_VAL(BLE_TRANSPORT_EVT_COUNT))
|
||||
|
||||
/**
|
||||
* A packet to be sent over the USB. This can be a command, an event, or ACL
|
||||
@@ -107,11 +49,9 @@ struct ble_hci_pkt {
|
||||
void *data;
|
||||
};
|
||||
|
||||
static struct os_mempool ble_hci_pkt_pool;
|
||||
static os_membuf_t ble_hci_pkt_buf[
|
||||
OS_MEMPOOL_SIZE(BLE_HCI_USB_EVT_COUNT + 1 +
|
||||
MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT),
|
||||
sizeof(struct ble_hci_pkt))];
|
||||
static struct os_mempool pool_tx_q;
|
||||
static os_membuf_t pool_tx_q_buf[ OS_MEMPOOL_SIZE(TX_Q_SIZE,
|
||||
sizeof(struct ble_hci_pkt)) ];
|
||||
|
||||
struct tx_queue {
|
||||
STAILQ_HEAD(, ble_hci_pkt) queue;
|
||||
@@ -151,7 +91,7 @@ tud_bt_acl_data_sent_cb(uint16_t sent_bytes)
|
||||
STAILQ_REMOVE_HEAD(&ble_hci_tx_acl_queue.queue, next);
|
||||
next_acl = STAILQ_FIRST(&ble_hci_tx_acl_queue.queue);
|
||||
OS_EXIT_CRITICAL(sr);
|
||||
os_memblock_put(&ble_hci_pkt_pool, curr_acl);
|
||||
os_memblock_put(&pool_tx_q, curr_acl);
|
||||
if (next_acl != NULL) {
|
||||
om = next_acl->data;
|
||||
}
|
||||
@@ -178,13 +118,13 @@ tud_bt_event_sent_cb(uint16_t sent_bytes)
|
||||
|
||||
assert(hci_ev != NULL && hci_ev[1] + sizeof(struct ble_hci_ev) == sent_bytes);
|
||||
|
||||
ble_hci_trans_buf_free(hci_ev);
|
||||
ble_transport_free(hci_ev);
|
||||
|
||||
OS_ENTER_CRITICAL(sr);
|
||||
STAILQ_REMOVE_HEAD(&ble_hci_tx_evt_queue.queue, next);
|
||||
next_evt = STAILQ_FIRST(&ble_hci_tx_evt_queue.queue);
|
||||
OS_EXIT_CRITICAL(sr);
|
||||
os_memblock_put(&ble_hci_pkt_pool, curr_evt);
|
||||
os_memblock_put(&pool_tx_q, curr_evt);
|
||||
|
||||
if (next_evt != NULL) {
|
||||
hci_ev = next_evt->data;
|
||||
@@ -201,10 +141,11 @@ tud_bt_acl_data_received_cb(void *acl_data, uint16_t data_len)
|
||||
int rc;
|
||||
|
||||
if (om == NULL) {
|
||||
om = ble_hci_trans_acl_buf_alloc();
|
||||
om = ble_transport_alloc_acl_from_hs();
|
||||
assert(om != NULL);
|
||||
}
|
||||
assert(om->om_len + data_len <= MYNEWT_VAL(BLE_ACL_BUF_SIZE) + BLE_HCI_DATA_HDR_SZ);
|
||||
assert(om->om_len + data_len <= MYNEWT_VAL(BLE_TRANSPORT_ACL_SIZE) +
|
||||
BLE_HCI_DATA_HDR_SZ);
|
||||
|
||||
os_mbuf_append(om, acl_data, data_len);
|
||||
incoming_acl_data = om;
|
||||
@@ -213,7 +154,7 @@ tud_bt_acl_data_received_cb(void *acl_data, uint16_t data_len)
|
||||
len = data[2] + (data[3] << 8) + BLE_HCI_DATA_HDR_SZ;
|
||||
if (incoming_acl_data->om_len >= len) {
|
||||
incoming_acl_data = NULL;
|
||||
rc = ble_hci_usb_rx_acl_ll_cb(om, ble_hci_usb_rx_acl_ll_arg);
|
||||
rc = ble_transport_to_ll_acl(om);
|
||||
(void)rc;
|
||||
}
|
||||
}
|
||||
@@ -225,17 +166,14 @@ tud_bt_hci_cmd_cb(void *hci_cmd, size_t cmd_len)
|
||||
uint8_t *buf;
|
||||
int rc = -1;
|
||||
|
||||
assert(ble_hci_usb_rx_cmd_ll_cb);
|
||||
if (ble_hci_usb_rx_cmd_ll_cb) {
|
||||
buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
|
||||
assert(buf != NULL);
|
||||
memcpy(buf, hci_cmd, min(cmd_len, BLE_HCI_TRANS_CMD_SZ));
|
||||
buf = ble_transport_alloc_cmd();
|
||||
assert(buf != NULL);
|
||||
memcpy(buf, hci_cmd, min(cmd_len, 258));
|
||||
|
||||
rc = ble_hci_usb_rx_cmd_ll_cb(buf, ble_hci_usb_rx_cmd_ll_arg);
|
||||
}
|
||||
rc = ble_transport_to_ll_cmd(buf);
|
||||
|
||||
if (rc != 0) {
|
||||
ble_hci_trans_buf_free(buf);
|
||||
ble_transport_free(buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,7 +190,7 @@ ble_hci_trans_ll_tx(struct tx_queue *queue, struct os_mbuf *om)
|
||||
return 0;
|
||||
}
|
||||
|
||||
pkt = os_memblock_get(&ble_hci_pkt_pool);
|
||||
pkt = os_memblock_get(&pool_tx_q);
|
||||
if (pkt == NULL) {
|
||||
os_mbuf_free_chain(om);
|
||||
return BLE_ERR_MEM_CAPACITY;
|
||||
@@ -270,29 +208,24 @@ ble_hci_trans_ll_tx(struct tx_queue *queue, struct os_mbuf *om)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
ble_hci_trans_ll_acl_tx(struct os_mbuf *om)
|
||||
{
|
||||
return ble_hci_trans_ll_tx(&ble_hci_tx_acl_queue, om);
|
||||
}
|
||||
|
||||
int
|
||||
ble_hci_trans_ll_evt_tx(uint8_t *hci_ev)
|
||||
static int
|
||||
ble_hci_trans_ll_evt_tx(void *buf)
|
||||
{
|
||||
struct ble_hci_pkt *pkt;
|
||||
uint8_t *hci_ev = buf;
|
||||
os_sr_t sr;
|
||||
bool first;
|
||||
|
||||
assert(hci_ev != NULL);
|
||||
|
||||
if (!tud_ready()) {
|
||||
ble_hci_trans_buf_free(hci_ev);
|
||||
ble_transport_free(hci_ev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pkt = os_memblock_get(&ble_hci_pkt_pool);
|
||||
pkt = os_memblock_get(&pool_tx_q);
|
||||
if (pkt == NULL) {
|
||||
ble_hci_trans_buf_free(hci_ev);
|
||||
ble_transport_free(hci_ev);
|
||||
return BLE_ERR_MEM_CAPACITY;
|
||||
}
|
||||
|
||||
@@ -308,108 +241,27 @@ ble_hci_trans_ll_evt_tx(uint8_t *hci_ev)
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t *
|
||||
ble_hci_trans_buf_alloc(int type)
|
||||
int
|
||||
ble_transport_to_hs_acl(struct os_mbuf *om)
|
||||
{
|
||||
uint8_t *buf;
|
||||
|
||||
switch (type) {
|
||||
case BLE_HCI_TRANS_BUF_CMD:
|
||||
assert(!usb_ble_hci_pool_cmd.allocated);
|
||||
usb_ble_hci_pool_cmd.allocated = 1;
|
||||
buf = usb_ble_hci_pool_cmd.cmd;
|
||||
break;
|
||||
|
||||
case BLE_HCI_TRANS_BUF_EVT_HI:
|
||||
buf = os_memblock_get(&ble_hci_usb_evt_hi_pool);
|
||||
if (buf == NULL) {
|
||||
/* If no high-priority event buffers remain, try to grab a
|
||||
* low-priority one.
|
||||
*/
|
||||
buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_HCI_TRANS_BUF_EVT_LO:
|
||||
buf = os_memblock_get(&ble_hci_usb_evt_lo_pool);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
buf = NULL;
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void
|
||||
ble_hci_trans_buf_free(uint8_t *buf)
|
||||
{
|
||||
int rc;
|
||||
|
||||
/* XXX: this may look a bit odd, but the controller uses the command
|
||||
* buffer to send back the command complete/status as an immediate
|
||||
* response to the command. This was done to insure that the controller
|
||||
* could always send back one of these events when a command was received.
|
||||
* Thus, we check to see which pool the buffer came from so we can free
|
||||
* it to the appropriate pool
|
||||
*/
|
||||
if (os_memblock_from(&ble_hci_usb_evt_hi_pool, buf)) {
|
||||
rc = os_memblock_put(&ble_hci_usb_evt_hi_pool, buf);
|
||||
assert(rc == 0);
|
||||
} else if (os_memblock_from(&ble_hci_usb_evt_lo_pool, buf)) {
|
||||
rc = os_memblock_put(&ble_hci_usb_evt_lo_pool, buf);
|
||||
assert(rc == 0);
|
||||
} else {
|
||||
assert(usb_ble_hci_pool_cmd.allocated);
|
||||
usb_ble_hci_pool_cmd.allocated = 0;
|
||||
}
|
||||
(void)rc;
|
||||
return ble_hci_trans_ll_tx(&ble_hci_tx_acl_queue, om);
|
||||
}
|
||||
|
||||
int
|
||||
ble_hci_trans_reset(void)
|
||||
ble_transport_to_hs_evt(void *buf)
|
||||
{
|
||||
return 0;
|
||||
return ble_hci_trans_ll_evt_tx(buf);
|
||||
}
|
||||
|
||||
void
|
||||
ble_hci_usb_init(void)
|
||||
ble_transport_hs_init(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
/* Ensure this function only gets called by sysinit. */
|
||||
SYSINIT_ASSERT_ACTIVE();
|
||||
|
||||
rc = mem_init_mbuf_pool(ble_hci_pool_acl_mempool_buf, &ble_hci_pool_acl_mempool, &ble_hci_pool_acl_mbuf_pool,
|
||||
MYNEWT_VAL(BLE_ACL_BUF_COUNT), ACL_BLOCK_SIZE, "ble_hci_acl");
|
||||
SYSINIT_PANIC_ASSERT(rc == 0);
|
||||
|
||||
rc = os_mempool_init(&ble_hci_usb_evt_hi_pool,
|
||||
MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
|
||||
MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
|
||||
ble_hci_usb_evt_hi_buf,
|
||||
"ble_hci_usb_evt_hi_pool");
|
||||
SYSINIT_PANIC_ASSERT(rc == 0);
|
||||
|
||||
rc = os_mempool_init(&ble_hci_usb_evt_lo_pool,
|
||||
MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
|
||||
MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
|
||||
ble_hci_usb_evt_lo_buf,
|
||||
"ble_hci_usb_evt_lo_pool");
|
||||
SYSINIT_PANIC_ASSERT(rc == 0);
|
||||
|
||||
/*
|
||||
* Create memory pool of packet list nodes. NOTE: the number of these
|
||||
* buffers should be, at least, the total number of event buffers (hi
|
||||
* and lo), the number of command buffers (currently 1) and the total
|
||||
* number of buffers that the controller could possibly hand to the host.
|
||||
*/
|
||||
rc = os_mempool_init(&ble_hci_pkt_pool,
|
||||
BLE_HCI_USB_EVT_COUNT + 1 +
|
||||
MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT),
|
||||
sizeof (struct ble_hci_pkt),
|
||||
ble_hci_pkt_buf,
|
||||
"ble_hci_usb_pkt_pool");
|
||||
rc = os_mempool_init(&pool_tx_q, TX_Q_SIZE, sizeof(struct ble_hci_pkt),
|
||||
pool_tx_q_buf, "ble_hci_usb_tx_q");
|
||||
SYSINIT_PANIC_ASSERT(rc == 0);
|
||||
}
|
||||
|
||||
@@ -26,21 +26,3 @@ syscfg.defs:
|
||||
number of ACL buffers that the controller tells the host it
|
||||
has.
|
||||
value: 12
|
||||
|
||||
BLE_TRANS_USB_SYSINIT_STAGE:
|
||||
description: >
|
||||
Sysinit stage for the USB BLE transport.
|
||||
value: 500
|
||||
|
||||
syscfg.vals.BLE_EXT_ADV:
|
||||
BLE_HCI_EVT_BUF_SIZE: 257
|
||||
|
||||
syscfg.vals:
|
||||
BLE_HCI_EVT_BUF_SIZE: 70
|
||||
BLE_HCI_EVT_HI_BUF_COUNT: 8
|
||||
BLE_HCI_EVT_LO_BUF_COUNT: 8
|
||||
BLE_ACL_BUF_COUNT: 12
|
||||
BLE_ACL_BUF_SIZE: 255
|
||||
|
||||
syscfg.restrictions:
|
||||
- '!BLE_HOST'
|
||||
|
||||
Reference in New Issue
Block a user