From 036ffe90ce67dfa4488cfe9e4337ddbb8de6e0c4 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 8 Sep 2026 19:07:28 +0100 Subject: [PATCH 1/2] zephyr-cp: enable BLE on the Raspberry Pi Pico W Same CYW43439 as the Pico 2 W, and the shared-bus HCI driver needed no board-specific work, so this is the Pico 2 W change applied to the RP2040 board: - overlay: hang the infineon,cyw43-bt-hci node off the stock infineon,airoc-wifi node and point zephyr,bt-hci at it. - overlay: the settings partition had the same defect as the Pico 2 W's -- originally 2K at 0x180800, neither erase-sector aligned nor big enough for one of the RP2040's 4K sectors; main has since made it one aligned 4K sector at 0x17f000, which is still one short of the two nvs_mount() insists on, so bt_enable() fails before opening the HCI driver. Give settings two sectors at 0x17e000, taken from the code partition. nvm and circuitpy stay at 0x180000 and 0x181000 to match ports/raspberrypi (cptools/check_partitions.py checks this), so CIRCUITPY is not moved. - conf: BT_EXT_ADV=n, since this controller rejects the Bluetooth 5 extended advertising commands, plus a deeper system workqueue stack. HW_STACK_PROTECTION is deliberately left off here: it earned its place on the Cortex-M33 but costs RAM, and this is the constrained board. Builds at 68.67% flash and 84.32% RAM of 264K, so BLE fits with about 42K to spare. Verified on hardware after the "ranges" fix (now on main): the adapter comes up as WiFi MAC + 2 and a legacy LE scan returns nearby advertisers. That run used the earlier layout with settings at 0x181000; the 0x17e000 placement is build-tested only. Co-Authored-By: Claude Opus 5 --- .../rpi_pico_w_zephyr/autogen_board_info.toml | 2 +- .../raspberrypi/rpi_pico_w_zephyr/board.conf | 9 +++ .../rpi_pico_w_zephyr/board.overlay | 55 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/autogen_board_info.toml b/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/autogen_board_info.toml index 321aa7a01f6..012cc8c2dc9 100644 --- a/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/autogen_board_info.toml @@ -3,7 +3,7 @@ name = "Raspberry Pi Foundation Raspberry Pi Pico" [modules] __future__ = true -_bleio = false +_bleio = true # Zephyr board has _bleio _eve = false _pew = false _pixelmap = false diff --git a/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.conf b/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.conf index 1e2d7ae1cd2..ad03cc6d10e 100644 --- a/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.conf +++ b/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.conf @@ -20,3 +20,12 @@ CONFIG_MBEDTLS_ENTROPY_C=y CONFIG_MBEDTLS_CTR_DRBG_C=y CONFIG_TEST_RANDOM_GENERATOR=y + +# Bluetooth over the shared gSPI bus (CONFIG_BT itself comes from the +# zephyr,bt-hci chosen node in the overlay). +# +# The CYW43439's BT controller firmware does not implement the Bluetooth 5 +# extended advertising/scanning commands, and the port defaults BT_EXT_ADV on, +# so turn it off here and let the host use the legacy 0x200B/0x200C commands. +CONFIG_BT_EXT_ADV=n +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 diff --git a/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.overlay b/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.overlay index f8f00f5cb37..2a2ca56d9ec 100644 --- a/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.overlay +++ b/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.overlay @@ -1,3 +1,58 @@ /* Partition layout is owned by the mcuboot fork (dts/raspberrypi/rpi_pico.dtsi) * and applied to this image by sysbuild. This overlay only adds board/app config. */ + +/* + * Settings partition: one exception to the layout above. sysbuild prepends the + * adaboot layout (dts/raspberrypi/rpi_pico_w.dtsi) to this overlay, so what + * is set here wins. + * + * The settings partition backs Bluetooth bond keys via NVS, so it must be + * erase-sector aligned and hold at least two of the RP2040's 4K sectors. + * The layout gives it one (0x17f000, 4K); nvs_mount() rejects a single sector + * with -EINVAL, which makes bt_enable() fail before ever opening the HCI + * driver and surfaces as a bare OSError from "import _bleio". + * + * nvm and the filesystem must stay where ports/raspberrypi puts them + * (cptools/check_partitions.py enforces this), so the second sector comes out + * of the code partition, not out of the CIRCUITPY drive. The code partition + * still starts at 0x100, after the second stage bootloader. + */ +&code_partition { + reg = <0x100 (0x17e000 - 0x100)>; +}; + +/delete-node/ &storage_partition; + +&flash0 { + partitions { + storage_partition: partition@17e000 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x17e000 0x2000>; + }; + }; +}; + +/* + * Bluetooth. Same CYW43439 as the Pico 2 W: HCI is not on a UART, it shares + * the WiFi gSPI bus, so hang the shared-bus HCI transport off the board's + * stock infineon,airoc-wifi node and point the host at it. CONFIG_BT then + * enables itself via the zephyr,bt-hci chosen (see + * ../../../internal-modules/circuitpython-kconfig/Kconfig). + */ +&pio0_spi0 { + airoc-wifi@0 { + cyw43_bt_hci: cyw43_bt_hci { + compatible = "infineon,cyw43-bt-hci"; + status = "okay"; + }; + }; +}; + +/ { + chosen { + zephyr,bt-hci = &cyw43_bt_hci; + }; +}; + #include "../../../app.overlay" From b610e43da295bfbdb5f34b90f3c491bb2e0658cb Mon Sep 17 00:00:00 2001 From: tyeth Date: Sat, 3 Oct 2026 14:45:18 +0100 Subject: [PATCH 2/2] zephyr-cp: two BT connections on the Pico W, to give the heap back Upstream's prj.conf now sets CONFIG_BT_MAX_CONN=5 for every board, and each connection is reserved in static RAM. On the Pico W (264 KB, most of it already taken by the CYW43 WiFi + BT stacks) that left the CircuitPython heap at ~16.5 KB, down from ~42 KB. Two connections cover the BLE workflow plus one more central. Co-Authored-By: Claude Opus 5.5 --- .../boards/raspberrypi/rpi_pico_w_zephyr/board.conf | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.conf b/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.conf index ad03cc6d10e..84002ed1f05 100644 --- a/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.conf +++ b/ports/zephyr-cp/boards/raspberrypi/rpi_pico_w_zephyr/board.conf @@ -29,3 +29,10 @@ CONFIG_TEST_RANDOM_GENERATOR=y # so turn it off here and let the host use the legacy 0x200B/0x200C commands. CONFIG_BT_EXT_ADV=n CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 + +# Connections. prj.conf allows 5 on every board, and every connection reserves +# its buffers and state in static RAM up front. The Pico W's 264 KB is mostly +# spoken for by the WiFi and BT stacks already: with 5, the CircuitPython heap +# left over came to ~16.5 KB, too little for adafruit_ble plus a program. Two +# still covers the BLE workflow plus one more central. +CONFIG_BT_MAX_CONN=2