8000 stm32/wb55: Add usb/bluetooth transparent mode to stm32wb55. by andrewleech · Pull Request #7703 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

stm32/wb55: Add usb/bluetooth transparent mode to stm32wb55. #7703

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
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
3 changes: 3 additions & 0 deletions ports/stm32/modstm.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ STATIC const mp_rom_map_elem_t stm_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_rfcore_status), MP_ROM_PTR(&rfcore_status_obj) },
{ MP_ROM_QSTR(MP_QSTR_rfcore_fw_version), MP_ROM_PTR(&rfcore_fw_version_obj) },
{ MP_ROM_QSTR(MP_QSTR_rfcore_sys_hci), MP_ROM_PTR(&rfcore_sys_hci_obj) },
#if MICROPY_HW_STM32WB_TRANSPARENT_MODE
{ MP_ROM_QSTR(MP_QSTR_rfcore_transparent), MP_ROM_PTR(&rfcore_transparent_obj) },
#endif
#endif
};

Expand Down
78 changes: 78 additions & 0 deletions ports/stm32/mpbthciport.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,84 @@ int mp_bluetooth_hci_uart_readchar(void) {
}
}

#if MICROPY_HW_STM32WB_TRANSPARENT_MODE
#include "py/stream.h"

STATIC int rfcore_transparent_msg_cb(void *env, const uint8_t *buf, size_t len) {
mp_hal_stdout_tx_strn((const char*)buf, len);
return 0;
}

#define STATE_IDLE 0
#define STATE_NEED_LEN 1
#define STATE_IN_PAYLOAD 2

#define HCI_KIND_BT_CMD (0x01) // <kind=1><?><?><len>
#define HCI_KIND_BT_ACL (0x02) // <kind=2><?><?><len LSB><len MSB>
#define HCI_KIND_BT_EVENT (0x04) // <kind=4><op><len><data...>
#define HCI_KIND_VENDOR_RESPONSE (0x11)
#define HCI_KIND_VENDOR_EVENT (0x12)

STATIC mp_obj_t rfcore_transparent(void) {
rfcore_ble_init();

mp_hal_set_interrupt_char(-1);

uint8_t buf[1024];
size_t rx = 0;
size_t len = 0;
int state = 0;
int cmd_type = 0;

while (true) {
if (state == STATE_IN_PAYLOAD && len == 0) {
rfcore_ble_hci_cmd(rx, buf);
// mp_hal_stdout_tx_strn((const char*)buf, rx);
rx = 0;
len = 0;
state = STATE_IDLE;
}

if (mp_hal_stdio_poll(MP_STREAM_POLL_RD) & MP_STREAM_POLL_RD) {
uint8_t c = mp_hal_stdin_rx_chr();

if (state == STATE_IDLE && (c == HCI_KIND_BT_CMD || c == HCI_KIND_BT_ACL || c == HCI_KIND_BT_EVENT || c == HCI_KIND_VENDOR_RESPONSE || c == HCI_KIND_VENDOR_EVENT)) {
cmd_type = c;
state = STATE_NEED_LEN;
buf[rx++] = c;
len = 0;
} else if (state == STATE_NEED_LEN) {
buf[rx++] = c;
if (cmd_type == HCI_KIND_BT_ACL && rx == 4) {
len = c;
}
if (cmd_type == HCI_KIND_BT_ACL && rx == 5) {
len += ((size_t)c) << 8;
state = STATE_IN_PAYLOAD;
}
if (cmd_type == HCI_KIND_BT_EVENT && rx == 3) {
len = c;
state = STATE_IN_PAYLOAD;
}
if (cmd_type == HCI_KIND_BT_CMD && rx == 4) {
len = c;
state = STATE_IN_PAYLOAD;
}
} else if (state == STATE_IN_PAYLOAD) {
buf[rx++] = c;
--len;
}
}

rfcore_ble_check_msg(rfcore_transparent_msg_cb, NULL);
}

return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(rfcore_transparent_obj, rfcore_transparent);
#endif // MICROPY_HW_STM32WB_TRANSPARENT_MODE


#else

/******************************************************************************/
Expand Down
13 changes: 10 additions & 3 deletions ports/stm32/rfcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#if MICROPY_BLUETOOTH_NIMBLE
// For mp_bluetooth_nimble_hci_uart_wfi
#include "nimble/nimble_npl.h"
// For mp_bluetooth_nimble_ble_state
#include "modbluetooth_nimble.h"
#else
#error "STM32WB must use NimBLE."
#endif
Expand Down Expand Up @@ -261,7 +263,6 @@ void ipcc_init(uint32_t irq_pri) {
// Enable receive IRQ on the BLE channel.
LL_C1_IPCC_EnableIT_RXO(IPCC);
LL_C1_IPCC_DisableReceiveChannel(IPCC, LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4 | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6);
LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE);
NVIC_SetPriority(IPCC_C1_RX_IRQn, irq_pri);
HAL_NVIC_EnableIRQ(IPCC_C1_RX_IRQn);

Expand Down Expand Up @@ -447,7 +448,7 @@ STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_h
// Clear receive channel (allows RF core to send more data to us).
LL_C1_IPCC_ClearFlag_CHx(IPCC, ch);

if (ch == IPCC_CH_BLE) {
if (ch == IPCC_CH_BLE && (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF)) {
// Renable IRQs for BLE now that we've cleared the flag.
LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE);
}
Expand Down Expand Up @@ -585,6 +586,10 @@ static const struct {
void rfcore_ble_init(void) {
DEBUG_printf("rfcore_ble_init\n");

if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE);
}

Copy link
Member

Choose a reason for hiding this comment

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

Extra line added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

// Clear any outstanding messages from ipcc_init.
tl_check_msg(&ipcc_mem_sys_queue, IPCC_CH_SYS, NULL);

Expand Down Expand Up @@ -642,7 +647,9 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) {
break;
}
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE
mp_bluetooth_nimble_hci_uart_wfi();
if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
mp_bluetooth_nimble_hci_uart_wfi();
}
#endif
}

Expand Down
7 changes: 6 additions & 1 deletion ports/stm32/rfcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ void rfcore_ble_init(void);
void rfcore_ble_hci_cmd(size_t len, const uint8_t *src);
void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env);
void rfcore_ble_set_txpower(uint8_t level);
#if MICROPY_HW_STM32WB_TRANSPARENT_MODE
void rfcore_ble_disable_ble_rx_interrupt(void);
#endif

void rfcore_start_flash_erase(void);
void rfcore_end_flash_erase(void);

MP_DECLARE_CONST_FUN_OBJ_0(rfcore_status_obj);
MP_DECLARE_CONST_FUN_OBJ_1(rfcore_fw_version_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_sys_hci_obj);

#if MICROPY_HW_STM32WB_TRANSPARENT_MODE
MP_DECLARE_CONST_FUN_OBJ_0(rfcore_transparent_obj);
#endif
#endif // MICROPY_INCLUDED_STM32_RFCORE_H
0