Skip to content

Commit efca591

Browse files
committed
extmod/zephyr_ble: Remove last two Zephyr submodule patches.
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>
1 parent de9b220 commit efca591

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Wrapper for Zephyr hci_driver.c that adds cooperative poll_rx function.
3+
*
4+
* hci_driver.c's static sem_recv, recv_fifo, node_rx_recv(), and
5+
* process_node() are inaccessible from outside the translation unit.
6+
* We compile hci_driver.c as part of this TU so we can provide
7+
* hci_driver_poll_rx() without patching the Zephyr submodule.
8+
*
9+
* This file REPLACES hci_driver.c in the build — do not compile both.
10+
*/
11+
12+
#include "lib/zephyr/subsys/bluetooth/controller/hci/hci_driver.c"
13+
14+
/* Cooperative polling: process one recv_fifo node per call.
15+
* Called from the main loop when running without real Zephyr threads
16+
* (e.g. MicroPython's cooperative BLE integration).
17+
* Returns true if more nodes are pending, false if recv_fifo is empty.
18+
* Caller should interleave work processing between calls to match
19+
* full Zephyr's threading model.
20+
*/
21+
bool hci_driver_poll_rx(const struct device *dev)
22+
{
23+
const struct hci_driver_data *data = dev->data;
24+
25+
/* Process controller RX — moves PDUs from LL into recv_fifo
26+
* and sends completed-event notifications directly via bt_recv.
27+
*/
28+
#if !defined(CONFIG_BT_CTLR_RX_PRIO_STACK_SIZE)
29+
if (k_sem_count_get(&sem_recv) > 0) {
30+
k_sem_take(&sem_recv, K_NO_WAIT);
31+
node_rx_recv(dev);
32+
}
33+
#endif /* !CONFIG_BT_CTLR_RX_PRIO_STACK_SIZE */
34+
35+
/* Process ONE recv_fifo node */
36+
struct node_rx_pdu *node_rx = k_fifo_get(&recv_fifo, K_NO_WAIT);
37+
38+
if (node_rx == NULL) {
39+
return false;
40+
}
41+
42+
struct net_buf *buf = process_node(node_rx);
43+
44+
while (buf) {
45+
struct net_buf *frag = net_buf_ref(buf);
46+
47+
buf = net_buf_frag_del(NULL, buf);
48+
49+
if (frag->len > 1) {
50+
data->recv(dev, frag);
51+
} else {
52+
net_buf_unref(frag);
53+
}
54+
}
55+
56+
/* Return true if more nodes pending */
57+
return !k_fifo_is_empty(&recv_fifo);
58+
}

extmod/zephyr_ble/zephyr_ble.mk

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,9 @@ ifeq ($(MICROPY_BLUETOOTH_ZEPHYR_CONTROLLER),1)
194194
CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_ZEPHYR_CONTROLLER=1
195195

196196
# Controller HCI interface
197-
SRC_THIRDPARTY_C += $(addprefix $(ZEPHYR_LIB_DIR)/subsys/bluetooth/controller/hci/, \
198-
hci_driver.c \
199-
hci.c \
200-
)
197+
# hci_driver.c compiled via wrapper (adds poll_rx accessing file-scope variables)
198+
SRC_THIRDPARTY_C += $(ZEPHYR_LIB_DIR)/subsys/bluetooth/controller/hci/hci.c
199+
SRC_THIRDPARTY_C += $(ZEPHYR_BLE_EXTMOD_DIR)/hci_driver_wrapper.c
201200

202201
# Nordic vendor HCI (reads FICR device address for stable BLE identity)
203202
SRC_THIRDPARTY_C += $(ZEPHYR_LIB_DIR)/subsys/bluetooth/controller/ll_sw/nordic/hci/hci_vendor.c
@@ -299,6 +298,12 @@ $(BUILD)/$(ZEPHYR_LIB_DIR)/subsys/bluetooth/controller/%.o: CFLAGS += -Wno-error
299298
-include zephyr/bluetooth/hci_vs.h -include zephyr/bluetooth/buf.h \
300299
-include zephyr_ble_irq.h $(CFLAGS_CONTROLLER_COMPAT)
301300

301+
# hci_driver_wrapper.c includes hci_driver.c — needs full controller CFLAGS
302+
$(BUILD)/$(ZEPHYR_BLE_EXTMOD_DIR)/hci_driver_wrapper.o: CFLAGS += -Wno-error -Wno-format \
303+
-Wno-unused-variable -Wno-lto-type-mismatch \
304+
-include zephyr/bluetooth/hci_vs.h -include zephyr/bluetooth/buf.h \
305+
-include zephyr_ble_irq.h $(CFLAGS_CONTROLLER_COMPAT)
306+
302307
endif # MICROPY_BLUETOOTH_ZEPHYR_CONTROLLER
303308

304309
endif

0 commit comments

Comments
 (0)