extmod/zephyr_ble: Add Zephyr BLE host stack with RP2 port integration.#19
Draft
andrewleech wants to merge 9 commits intoreview/zephyr-ble-corefrom
Draft
extmod/zephyr_ble: Add Zephyr BLE host stack with RP2 port integration.#19andrewleech wants to merge 9 commits intoreview/zephyr-ble-corefrom
andrewleech wants to merge 9 commits intoreview/zephyr-ble-corefrom
Conversation
577fa84 to
eb4a81a
Compare
|
Code size report: |
Owner
Author
|
/review |
|
Review failed. Retry with |
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>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Switch L2CAP CoC from recv+alloc_buf to the seg_recv API, which gives the application per-PDU callbacks with manual credit control. The old path issued one credit per SDU, forcing the peer to wait for the full SDU to be delivered before sending the next one. With seg_recv, credits are issued one per non-last PDU (allowing the peer to pipeline all K-frames of a single SDU) and one credit per SDU from recvinto() (for the first PDU of the next SDU), keeping at most one assembled SDU buffered. Work around a Zephyr bug in l2cap_chan_seg_recv_rx_init() which leaves rx.mps at zero for seg_recv channels (unlike l2cap_chan_rx_init for the normal path), causing immediate channel disconnect on the first received PDU. Set rx.mps = BT_L2CAP_RX_MTU in l2cap_create_channel() and use bt_l2cap_chan_give_credits() in accept/connect paths, matching the pattern from Zephyr's credits_seg_recv test. Also enable Data Length Extension (DLE) so the controller can negotiate 251-byte PDU payloads, reducing per-PDU overhead. TX pipeline: allow up to L2CAP_SDU_BUF_COUNT-1 SDUs in flight concurrently (tracked via tx_in_flight counter) rather than stalling after every send. On nRF52840 dongle (PCA10059) with PYBD (NimBLE) as central: perf_l2cap.py before: ~2,184 B/s perf_l2cap.py after: ~11,518 B/s (5.3x improvement) All 11 BLE multitests pass. Signed-off-by: Andrew Leech <andrew@alelec.net>
Replace single-SDU L2CAP accumulation buffer with a FIFO that holds multiple SDUs. Deep initial credit window (fills rx_buf) allows the peer to pipeline SDUs without per-SDU credit round-trips, which is critical for Z2Z throughput where each credit round-trip costs 2+ connection intervals. Add deferred L2CAP recv notification (rx_notify_pending) to avoid re-entrancy between seg_recv_cb and Python IRQ handlers. Each port's port_run_task must call flush_recv_notify() after work_process completes. Disable DLE auto-negotiation (CONFIG_BT_AUTO_DATA_LEN_UPDATE 0) for CYW43 compatibility — CYW43 disconnects with "Instant Passed" (0x16) when DLE is negotiated. Add l2cap_status_cb TX kick via bt_tx_irq_raise() to unblock queued SDUs when credits arrive. Run codeformat.py on extmod files. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
fedbc7f to
ffe840c
Compare
Add ZEPHYR_BLE_POLL_INTERVAL_MS define (default 128ms) matching NimBLE convention. IRQ-driven ports use poll_now() for immediate processing; this is a fallback for timer housekeeping. Change CONFIG_BT_AUTO_DATA_LEN_UPDATE to #ifndef guard so ports with capable controllers can override via CFLAGS. Move random data generation out of the timed window in perf_l2cap.py and use getrandbits(8) for faster generation. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
ffe840c to
673bd99
Compare
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## review/zephyr-ble-core #19 +/- ##
=======================================================
Coverage 98.45% 98.45%
=======================================================
Files 175 175
Lines 22635 22635
=======================================================
Hits 22286 22286
Misses 349 349 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Cleanup unused functions, macros and debug helpers that were never called or only conditionally compiled behind disabled feature flags. Removes dead registry system, PSA crypto stubs, LIFO operations, and various unused helper functions and inlines from kernel/device/config headers. Also deletes gatt_pragma.h which is unreferenced. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
b3df7e5 to
c197acc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds the Zephyr BLE host as a third backend for
modbluetooth, integrated as a port-agnostic extmod. The Zephyr host code is compiled against a HAL shim layer (extmod/zephyr_ble/hal/) that replaces Zephyr's kernel primitives (semaphores, work queues, timers, FIFOs, memory slabs) with MicroPython-compatible implementations. This means the Zephyr BLE host runs cooperatively on the main MicroPython task without requiring Zephyr RTOS itself.This was motivated by limitations in the existing stacks — NimBLE doesn't have an active BLE pre-qualification and is missing some newer BLE features, while BTstack's MicroPython integration lacks pairing/bonding and L2CAP channel support. The Zephyr BLE host stack has active qualification, full feature coverage, and is under active development by multiple silicon vendors.
The RP2 port is the first integration, providing two build variants for Pico W and Pico 2 W:
zephyr_ble— cooperative polling from the main task (preferred)zephyr_ble_freertos— HCI processing on a dedicated FreeRTOS taskAlso included: a
gap_unpair()API addition across all BLE backends, micropython-lib updates for aioble robustness, and bond key persistence via Python secret store callbacks.flowchart TD subgraph "extmod/zephyr_ble (port-agnostic)" MOD[modbluetooth_zephyr.c] --> HAL[HAL shim layer] HAL --> WORK[work queues] HAL --> SEM[semaphores] HAL --> TIMER[timers] HAL --> FIFO[FIFOs] HAL --> H4[HCI H4 transport] end subgraph "Port integration (e.g. RP2)" HCI_DRV[mpzephyrport_rp2.c<br/>CYW43 HCI driver] --> H4 POLL[main loop polling] --> WORK end subgraph "Zephyr BLE host (lib/zephyr)" HOST[hci_core / gatt / att / smp / l2cap] end HOST --> MOD HCI_DRV --> HOSTTesting
All 12 BLE multitests passing on Pico W and Pico 2 W (both
zephyr_blevariant) with PYBD (NimBLE) as central:ble_gap_advertise,ble_gap_connect,ble_characteristic,ble_gap_pair,ble_gap_pair_bond,ble_subscribe,ble_irq_calls,ble_gattc_discover_services,ble_l2cap,perf_gatt_notify,perf_l2cap,ble_gap_unpair.Performance: ~24ms/notification (GATT), ~2184 B/s (L2CAP) on Pico W; ~25ms/notification, ~7956 B/s on Pico 2 W.
Not tested:
zephyr_ble_freertosvariant on Pico 2 W.Trade-offs and Alternatives
The HAL shim layer is substantial (~3K lines) because it reimplements Zephyr kernel primitives. The alternative would be running actual Zephyr RTOS, but that would limit this to the Zephyr port only. The shim approach allows any MicroPython port with an HCI transport to use the Zephyr BLE host.
The
lib/zephyrsubmodule adds the full Zephyr BLE host source. Only the host stack files are compiled — no kernel, no drivers, no board support. The submodule is pinned to a specific commit with two small patches (wrapper files forgatt.candconn.cto expose static internals needed for clean teardown).MTU is compile-time only (
CONFIG_BT_L2CAP_TX_MTU=512); runtimeble.config(mtu=X)is not supported.ble_mtu.pytest is skipped.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.