Skip to content

Commit 63d67ca

Browse files
jimmopi-anl
authored andcommitted
stm32/wb55: Add usb/bluetooth transparent mode to stm32wb55.
#define MICROPY_HW_STM32WB_TRANSPARENT_MODE (1) This will disable normal BLE Stack usage and allow forwarding all HCI from stdin to BLE code.
1 parent ed42002 commit 63d67ca

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

ports/stm32/modstm.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ STATIC const mp_rom_map_elem_t stm_module_globals_table[] = {
5050
{ MP_ROM_QSTR(MP_QSTR_rfcore_status), MP_ROM_PTR(&rfcore_status_obj) },
5151
{ MP_ROM_QSTR(MP_QSTR_rfcore_fw_version), MP_ROM_PTR(&rfcore_fw_version_obj) },
5252
{ MP_ROM_QSTR(MP_QSTR_rfcore_sys_hci), MP_ROM_PTR(&rfcore_sys_hci_obj) },
53+
#if MICROPY_HW_STM32WB_TRANSPARENT_MODE
54+
{ MP_ROM_QSTR(MP_QSTR_rfcore_transparent), MP_ROM_PTR(&rfcore_transparent_obj) },
55+
#endif
5356
#endif
5457
};
5558

ports/stm32/mpbthciport.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,84 @@ int mp_bluetooth_hci_uart_readchar(void) {
181181
}
182182
}
183183

184+
#if MICROPY_HW_STM32WB_TRANSPARENT_MODE
185+
#include "py/stream.h"
186+
187+
STATIC int rfcore_transparent_msg_cb(void *env, const uint8_t *buf, size_t len) {
188+
mp_hal_stdout_tx_strn((const char*)buf, len);
189+
return 0;
190+
}
191+
192+
#define STATE_IDLE 0
193+
#define STATE_NEED_LEN 1
194+
#define STATE_IN_PAYLOAD 2
195+
196+
#define HCI_KIND_BT_CMD (0x01) // <kind=1><?><?><len>
197+
#define HCI_KIND_BT_ACL (0x02) // <kind=2><?><?><len LSB><len MSB>
198+
#define HCI_KIND_BT_EVENT (0x04) // <kind=4><op><len><data...>
199+
#define HCI_KIND_VENDOR_RESPONSE (0x11)
200+
#define HCI_KIND_VENDOR_EVENT (0x12)
201+
202+
STATIC mp_obj_t rfcore_transparent(void) {
203+
rfcore_ble_init();
204+
205+
mp_hal_set_interrupt_char(-1);
206+
207+
uint8_t buf[1024];
208+
size_t rx = 0;
209+
size_t len = 0;
210+
int state = 0;
211+
int cmd_type = 0;
212+
213+
while (true) {
214+
if (state == STATE_IN_PAYLOAD && len == 0) {
215+
rfcore_ble_hci_cmd(rx, buf);
216+
// mp_hal_stdout_tx_strn((const char*)buf, rx);
217+
rx = 0;
218+
len = 0;
219+
state = STATE_IDLE;
220+
}
221+
222+
if (mp_hal_stdio_poll(MP_STREAM_POLL_RD) & MP_STREAM_POLL_RD) {
223+
uint8_t c = mp_hal_stdin_rx_chr();
224+
225+
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)) {
226+
cmd_type = c;
227+
state = STATE_NEED_LEN;
228+
buf[rx++] = c;
229+
len = 0;
230+
} else if (state == STATE_NEED_LEN) {
231+
buf[rx++] = c;
232+
if (cmd_type == HCI_KIND_BT_ACL && rx == 4) {
233+
len = c;
234+
}
235+
if (cmd_type == HCI_KIND_BT_ACL && rx == 5) {
236+
len += ((size_t)c) << 8;
237+
state = STATE_IN_PAYLOAD;
238+
}
239+
if (cmd_type == HCI_KIND_BT_EVENT && rx == 3) {
240+
len = c;
241+
state = STATE_IN_PAYLOAD;
242+
}
243+
if (cmd_type == HCI_KIND_BT_CMD && rx == 4) {
244+
len = c;
245+
state = STATE_IN_PAYLOAD;
246+
}
247+
} else if (state == STATE_IN_PAYLOAD) {
248+
buf[rx++] = c;
249+
--len;
250+
}
251+
}
252+
253+
rfcore_ble_check_msg(rfcore_transparent_msg_cb, NULL);
254+
}
255+
256+
return mp_const_none;
257+
}
258+
MP_DEFINE_CONST_FUN_OBJ_0(rfcore_transparent_obj, rfcore_transparent);
259+
#endif // MICROPY_HW_STM32WB_TRANSPARENT_MODE
260+
261+
184262
#else
185263

186264
/******************************************************************************/

ports/stm32/rfcore.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ void ipcc_init(uint32_t irq_pri) {
261261
// Enable receive IRQ on the BLE channel.
262262
LL_C1_IPCC_EnableIT_RXO(IPCC);
263263
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);
264+
#if !MICROPY_HW_STM32WB_TRANSPARENT_MODE
264265
LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE);
266+
#endif
265267
NVIC_SetPriority(IPCC_C1_RX_IRQn, irq_pri);
266268
HAL_NVIC_EnableIRQ(IPCC_C1_RX_IRQn);
267269

@@ -447,10 +449,12 @@ STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_h
447449
// Clear receive channel (allows RF core to send more data to us).
448450
LL_C1_IPCC_ClearFlag_CHx(IPCC, ch);
449451

452+
#if !MICROPY_HW_STM32WB_TRANSPARENT_MODE
450453
if (ch == IPCC_CH_BLE) {
451454
// Renable IRQs for BLE now that we've cleared the flag.
452455
LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE);
453456
}
457+
#endif
454458
}
455459
}
456460

@@ -642,7 +646,9 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) {
642646
break;
643647
}
644648
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE
645-
mp_bluetooth_nimble_hci_uart_wfi();
649+
if (LL_C1_IPCC_IsEnabledReceiveChannel(IPCC, IPCC_CH_BLE)) {
650+
mp_bluetooth_nimble_hci_uart_wfi();
651+
}
646652
#endif
647653
}
648654

ports/stm32/rfcore.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ void rfcore_end_flash_erase(void);
4141
MP_DECLARE_CONST_FUN_OBJ_0(rfcore_status_obj);
4242
MP_DECLARE_CONST_FUN_OBJ_1(rfcore_fw_version_obj);
4343
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_sys_hci_obj);
44-
44+
#if MICROPY_HW_STM32WB_TRANSPARENT_MODE
45+
MP_DECLARE_CONST_FUN_OBJ_0(rfcore_transparent_obj);
46+
#endif
4547
#endif // MICROPY_INCLUDED_STM32_RFCORE_H

ports/unix/mpbthciport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ STATIC int configure_uart(void) {
143143
toptions.c_cflag |= CRTSCTS;
144144

145145
// 1Mbit (TODO: make this configurable).
146-
speed_t brate = B1000000;
146+
speed_t brate = B115200;
147147
cfsetospeed(&toptions, brate);
148148
cfsetispeed(&toptions, brate);
149149

0 commit comments

Comments
 (0)