Skip to content

stm32/btstack: Add support for btstack on WB55 / rfcore. #9309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions extmod/btstack/btstack_hci_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block = {
&btstack_uart_set_wakeup_handler,
};


STATIC void mp_bluetooth_hci_uart_char_cb(uint8_t chr) {
recv_buf[recv_idx++] = chr;
if (recv_idx == recv_len) {
#if HCI_TRACE
printf(COL_BLUE "> [% 8d] %02x", (int)mp_hal_ticks_ms(), recv_buf[0]);
for (size_t i = 1; i < recv_len; ++i) {
printf(":%02x", recv_buf[i]);
}
printf(COL_OFF "\n");
#endif
recv_idx = 0;
recv_len = 0;
if (recv_handler) {
recv_handler();
}
}
}

void mp_bluetooth_btstack_hci_uart_process(void) {
bool host_wake = mp_bluetooth_hci_controller_woken();

Expand All @@ -174,23 +193,19 @@ void mp_bluetooth_btstack_hci_uart_process(void) {

// Append any new bytes to the recv buffer, notifying bstack if we've got
// the number of bytes it was looking for.
int chr;
while (recv_idx < recv_len && (chr = mp_bluetooth_hci_uart_readchar()) >= 0) {
recv_buf[recv_idx++] = chr;
if (recv_idx == recv_len) {
#if HCI_TRACE
printf(COL_BLUE "> [% 8d] %02x", (int)mp_hal_ticks_ms(), recv_buf[0]);
for (size_t i = 1; i < recv_len; ++i) {
printf(":%02x", recv_buf[i]);
}
printf(COL_OFF "\n");
#endif
recv_idx = 0;
recv_len = 0;
if (recv_handler) {
recv_handler();
while (recv_idx < recv_len) {
#if MICROPY_PY_BLUETOOTH_HCI_READ_MODE == MICROPY_PY_BLUETOOTH_HCI_READ_MODE_BYTE
int chr = mp_bluetooth_hci_uart_readchar();
if (chr < 0) {
break;
}
mp_bluetooth_hci_uart_char_cb(chr);
}
#elif MICROPY_PY_BLUETOOTH_HCI_READ_MODE == MICROPY_PY_BLUETOOTH_HCI_READ_MODE_PACKET
if (mp_bluetooth_hci_uart_readpacket(mp_bluetooth_hci_uart_char_cb) < 0) {
break;
}
#endif
}

if (host_wake) {
Expand Down
5 changes: 3 additions & 2 deletions extmod/btstack/modbluetooth_btstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

#include "lib/btstack/src/btstack.h"

#define DEBUG_printf(...) // printf("btstack: " __VA_ARGS__)
#include <stdio.h>
#define DEBUG_printf(...) printf("btstack: " __VA_ARGS__)

#ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME
#define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY BTSTACK"
Expand Down Expand Up @@ -368,7 +369,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
event_type == SM_EVENT_PAIRING_COMPLETE ||
// event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle
event_type == HCI_EVENT_ENCRYPTION_CHANGE) {
DEBUG_printf(" --> enc/auth/pair/bond change\n", );
DEBUG_printf(" --> enc/auth/pair/bond change\n");
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
uint16_t conn_handle;
switch (event_type) {
Expand Down
99 changes: 99 additions & 0 deletions ports/stm32/mpbtstackport.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,104 @@ static const btstack_run_loop_t mp_btstack_runloop_stm32 = {
&mp_btstack_runloop_get_time_ms,
};


// #if defined(STM32WB)

// /******************************************************************************/
// // HCI over IPCC

// #include "rfcore.h"

// static void (*transport_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
// static int hci_acl_can_send_now;


// STATIC void rfcore_transport_init(const void *transport_config){
// rfcore_init();
// }

// STATIC int rfcore_transport_open(void){
// return 0;
// }

// STATIC int rfcore_transport_close(void){
// return 0;
// }


// STATIC void rfcore_transport_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
// transport_packet_handler = handler;
// }

// STATIC int rfcore_transport_can_send_packet_now(uint8_t packet_type) {
// // if (cpu2_state != CPU2_STATE_READY) return 0;
// // switch (packet_type)
// // {
// // case HCI_COMMAND_DATA_PACKET:
// // return 1;

// // case HCI_ACL_DATA_PACKET:
// // return hci_acl_can_send_now;
// // }
// return 1;
// }

// STATIC int rfcore_transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){
// TL_CmdPacket_t *ble_cmd_buff = &BleCmdBuffer;

// switch (packet_type){
// case HCI_COMMAND_DATA_PACKET:
// ble_cmd_buff->cmdserial.type = packet_type;
// ble_cmd_buff->cmdserial.cmd.plen = size;
// memcpy((void *)&ble_cmd_buff->cmdserial.cmd, packet, size);
// TL_BLE_SendCmd(NULL, 0);
// transport_notify_packet_send();
// break;

// case HCI_ACL_DATA_PACKET:
// hci_acl_can_send_now = 0;
// ((TL_AclDataPacket_t *)HciAclDataBuffer)->AclDataSerial.type = packet_type;
// memcpy((void *)&(((TL_AclDataPacket_t *)HciAclDataBuffer)->AclDataSerial.handle),packet, size);
// TL_BLE_SendAclData(NULL, 0);
// transport_notify_packet_send();
// break;

// default:
// transport_send_hardware_error(0x01); // invalid HCI packet
// break;
// }
// return 0;
// }

// STATIC const hci_transport_t hci_wb55_transport = {
// "stm32wb-vhci",
// &rfcore_transport_init,
// &rfcore_transport_open,
// &rfcore_transport_close,
// &rfcore_transport_register_packet_handler,
// &rfcore_transport_can_send_packet_now,
// &rfcore_transport_send_packet,
// NULL, // set baud rate
// NULL, // reset link
// NULL, // set SCO config
// };

// void mp_bluetooth_btstack_port_init(void) {
// btstack_run_loop_init(&mp_btstack_runloop_stm32);

// // hci_dump_open(NULL, HCI_DUMP_STDOUT);
// hci_init(&hci_wb55_transport, NULL);
// }

// #else

/******************************************************************************/
// HCI over UART

#ifndef MICROPY_HW_BLE_UART_BAUDRATE_SECONDARY
#define MICROPY_HW_BLE_UART_BAUDRATE_SECONDARY MICROPY_HW_BLE_UART_BAUDRATE
#endif

STATIC const hci_transport_config_uart_t hci_transport_config_uart = {
HCI_TRANSPORT_CONFIG_UART,
MICROPY_HW_BLE_UART_BAUDRATE,
Expand Down Expand Up @@ -163,4 +261,5 @@ void mp_bluetooth_btstack_port_start(void) {
hci_power_control(HCI_POWER_ON);
}


#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
5 changes: 2 additions & 3 deletions ports/stm32/rfcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
#if MICROPY_BLUETOOTH_NIMBLE
// For mp_bluetooth_nimble_hci_uart_wfi
#include "nimble/nimble_npl.h"
#else
#error "STM32WB must use NimBLE."
#endif

#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
Expand Down Expand Up @@ -413,9 +411,10 @@ STATIC size_t tl_process_msg(volatile tl_list_node_t *head, unsigned int ch, par
bool added_to_free_queue = false;
size_t len = 0;
while (cur != head) {
volatile tl_list_node_t *next = tl_list_unlink(cur);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change (moving the line up) is what solved my HardFault...
In the processing of tl_parse_hci_msg() below a new message might be sent which changes the cur / head of ipcc_mem_ble_evt_queue
"snapshotting" the cur / next here before processing preserves them. Indeed removing it from the linked list before processing perhaps makes sense? Or might be dangerous, I'm not entirely certain...

len += tl_parse_hci_msg((uint8_t *)cur->body, parse);

volatile tl_list_node_t *next = tl_list_unlink(cur);

// If this node is allocated from the memmgr event pool, then place it into the free buffer.
if ((uint8_t *)cur >= ipcc_membuf_memmgr_evt_pool && (uint8_t *)cur < ipcc_membuf_memmgr_evt_pool + sizeof(ipcc_membuf_memmgr_evt_pool)) {
Expand Down