Skip to content

ports/stm32: Add Zephyr BLE support for WB55 and PYBD.#20

Draft
andrewleech wants to merge 21 commits intopr/zephyr-ble-corefrom
pr/zephyr-ble-stm32
Draft

ports/stm32: Add Zephyr BLE support for WB55 and PYBD.#20
andrewleech wants to merge 21 commits intopr/zephyr-ble-corefrom
pr/zephyr-ble-stm32

Conversation

@andrewleech
Copy link
Copy Markdown
Owner

Summary

Adds Zephyr BLE integration for the STM32 port, targeting the NUCLEO_WB55 and PYBD_SF6 boards. The WB55's BLE controller runs on the M0+ coprocessor (CPU2) and communicates with the M4 host via IPCC mailbox — the HCI driver in mpzephyrport.c bridges this to the Zephyr BLE host's H4 transport.

The IPCC/rfcore integration required careful handling of the CPU2 lifecycle: HCI_Reset must not be sent to CPU2 as it corrupts all 192KB of SRAM1. This is prevented by a drv_quirk_no_reset() hook in the Zephyr host's hci_core.c.

Build variants: NUCLEO_WB55 BOARD_VARIANT=zephyr_ble and PYBD_SF6 BOARD_VARIANT=zephyr_ble.

Depends on #19.

Testing

11/11 BLE multitests passing on WB55 with nRF52840 DK as peripheral. PYBD variant builds but has not been tested with Zephyr BLE (PYBD is primarily used as a NimBLE reference central).

Trade-offs and Alternatives

The STM32 integration touches several files beyond the BLE-specific code (linker scripts, FreeRTOS hooks, system_stm32 changes) because the WB55's dual-core architecture and IPCC memory layout require careful memory section placement. These changes are guarded by MICROPY_BLUETOOTH_ZEPHYR defines where possible.

Generative AI

I used generative AI tools when creating this PR, but a human has checked the code and is responsible for the description above.

@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch from ba9ebb7 to 86b9e6a Compare March 5, 2026 04:34
@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch from 86b9e6a to 123460b Compare March 5, 2026 05:54
@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch from 123460b to 8e853d6 Compare March 5, 2026 06:33
@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch from 8e853d6 to 7e0b581 Compare March 5, 2026 06:54
@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch from 7e0b581 to 86dba95 Compare March 5, 2026 12:38
@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch from 86dba95 to edf2fd6 Compare March 6, 2026 02:53
@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch from edf2fd6 to c408c95 Compare March 6, 2026 06:26
@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch from c408c95 to 7dfdb11 Compare March 6, 2026 23:09
@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch 3 times, most recently from 50639b0 to c11d224 Compare March 11, 2026 12:08
@andrewleech
Copy link
Copy Markdown
Owner Author

/review

@mpy-reviewer
Copy link
Copy Markdown

mpy-reviewer Bot commented Mar 13, 2026

Review failed. Retry with /review.

@andrewleech andrewleech force-pushed the pr/zephyr-ble-stm32 branch 3 times, most recently from 0c5c401 to f18c8ac Compare March 16, 2026 10:15
pi-anl added 21 commits April 19, 2026 23:48
Extends the Zephyr BLE HAL layer to support running the BLE controller
on-core alongside the host stack. All changes are guarded by
MICROPY_BLUETOOTH_ZEPHYR_CONTROLLER so host-only ports are unaffected.

New files provide IRQ management (NVIC wrappers), ISR dispatch table,
clock control (HFCLK/LFCLK), and controller kernel stubs (k_poll,
k_thread_create). Existing shims are updated to use real PRIMASK-based
interrupt control and IPSR-based ISR detection when the controller is
active.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Adds Zephyr BLE controller source files (ULL, LLL, ticker, HAL) to
zephyr_ble.mk under MICROPY_BLUETOOTH_ZEPHYR_CONTROLLER=1 guard. Adds
controller configuration defines (CONFIG_BT_CTLR_*, CONFIG_SOC_*,
ticker, LLCP) and header stubs needed by controller code (devicetree,
IRQ, entropy, version).

Also enables L2CAP dynamic channels (COC) in the shared build flags
and adds LTO type-mismatch warning suppression for stub declarations
that intentionally differ from Zephyr internals.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Add 1kHz SysTick handler with millisecond counter (uwTick), PendSV
dispatch mechanism for deferred soft timer processing, and SysTick
init function. Enable SEVONPEND so WFE wakes on SysTick interrupts.

Also unconditionally enable the MicroPython scheduler (previously
gated behind MICROPY_HW_ENABLE_USBDEV) since BLE event processing
requires mp_sched_schedule_node().

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Add Makefile integration, linker script extensions, and board variant
configurations (PCA10056, PCA10059) for building the nRF port with
Zephyr BLE. Includes PCA10059 DAPLink variant and hci_driver_poll_rx
support for the on-core controller.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Add mpzephyrport_nrf.c with on-core BLE controller initialization,
LFXO startup, cooperative HCI polling, and scheduler node callback
handling. Includes NULL callback guard for safe deinit during scheduler
node draining.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Extend the bt_disable deinit path to the nRF port's on-core controller.
The controller is shut down via ll_deinit() called from bt_disable(),
with proper LFXO re-start on next init.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Add work processing interleaving to the nRF port's controller polling
loop. Each HCI packet is followed by a work_process() call to ensure
connection events are handled before subsequent packets.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Move the remaining two Zephyr submodule patches (HCI driver, quirk
reset) into wrapper files, eliminating all custom patches from the
lib/zephyr submodule.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Add mp_bluetooth_zephyr_l2cap_flush_recv_notify() call to
port_run_task and re-entrancy guard to prevent recursive
port_run_task execution.

Update lib/zephyr submodule with LLL preempt ticker fix —
ticker_stop(TICKER_ID_LLL_PREEMPT) in init_reset() prevents
ll_deinit assertion on second bt_disable/bt_enable cycle.

Run codeformat.py on nRF port files and hci_driver_wrapper.c.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Use ZEPHYR_BLE_POLL_INTERVAL_MS for poll timer values.
Enable CONFIG_BT_AUTO_DATA_LEN_UPDATE for PCA10056 and
PCA10059 on-core controller variants.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
New Unix variant `zephyr_ble` that provides:
- Pthread-based soft timer backend for machine.Timer
- Linux HCI_CHANNEL_USER socket transport for USB BT adapters
- Zephyr BLE stack using RP2 cooperative polling pattern

The HCI socket driver uses AF_BLUETOOTH/BTPROTO_HCI with
HCI_CHANNEL_USER for exclusive raw access to a USB Bluetooth
adapter. Device selection via MICROPYBTHCI env var with
auto-detection fallback. Non-blocking socket reads in
port_run_task driven by the soft timer.

Build: make -C ports/unix VARIANT=zephyr_ble

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
- Replace softtimer_unix.c with version that has proper deinit,
  SIGUSR1 main-thread wakeup, and GC marking integration.
- Add soft_timer_init/deinit calls to main.c, GC marking to
  gccollect.c.
- Add MICROPY_SCHED_HOOK_SCHEDULED to wake main thread from
  blocking syscalls when timer callbacks are scheduled.
- Enable L2CAP channels and pairing/bonding by default in
  modbluetooth.h (all stacks support these features).
- Guard le_dbg variable with ZEPHYR_BLE_DEBUG in
  l2cap_disconnected_cb.
- Document HCI TX buffer size bounds.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Add Zephyr BLE variant configuration for PYBD_SF6 board, with HCI UART
readpacket support for bulk reading from the BT coprocessor.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Update mp_handle_pending() calls to use the
mp_handle_pending_behaviour_t enum instead of bool, matching
the current signature in py/scheduler.c.

Add mp_bluetooth_zephyr_l2cap_flush_recv_notify() call to
port_run_task, consistent with RP2 and nRF ports. Without this,
deferred L2CAP recv notifications were never delivered to Python.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Use ZEPHYR_BLE_POLL_INTERVAL_MS for poll timer values.
Enable CONFIG_BT_AUTO_DATA_LEN_UPDATE for NUCLEO_WB55.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Replace NUM_COMPLETED_PACKETS-based ACL flow control with direct IPCC
channel flag polling.  The IPCC flag clears when M0+ consumes the shared
SRAM2B buffer, which is much faster than waiting for the radio round-trip
needed by NUM_COMPLETED_PACKETS.

Debug instrumentation confirmed the IPCC flag always clears before the
next ACL send attempt, so the timeout path is defensive only.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants