From 1853dc9e04af480b572d0ab9a45776a87c4d051c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 19 Nov 2026 13:41:43 -0800 Subject: [PATCH 1/2] zephyr-cp: add input support This allows the mouse to be used in native_sim to mimic touch drivers like the GT911 used on the Raspberry Pi Touch display. --- .../bindings/zephyr_input/__init__.c | 66 +++++++++++++++++++ .../boards/native/native_sim_lvgl/board.conf | 9 +++ .../zephyr-cp/cptools/build_circuitpython.py | 1 + ports/zephyr-cp/cptools/zephyr2cp.py | 3 + ports/zephyr-cp/prj.conf | 4 ++ 5 files changed, 83 insertions(+) create mode 100644 ports/zephyr-cp/bindings/zephyr_input/__init__.c diff --git a/ports/zephyr-cp/bindings/zephyr_input/__init__.c b/ports/zephyr-cp/bindings/zephyr_input/__init__.c new file mode 100644 index 00000000000..36560af8a30 --- /dev/null +++ b/ports/zephyr-cp/bindings/zephyr_input/__init__.c @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: MIT + +#include +#include + +#include "py/obj.h" +#include "py/runtime.h" +#include "zephyr/input/input.h" + +typedef struct { + int32_t x; + int32_t y; + bool pressed; +} zephyr_input_touch_state_t; +static zephyr_input_touch_state_t touch_state; +static zephyr_input_touch_state_t buffered_touch_state; + +static void zephyr_input_callback(struct input_event *event, void *user_data) { + (void)user_data; + if (event->type == INPUT_EV_ABS && event->code == INPUT_ABS_X) { + touch_state.x = event->value; + } else if (event->type == INPUT_EV_ABS && event->code == INPUT_ABS_Y) { + touch_state.y = event->value; + } else if (event->type == INPUT_EV_KEY && event->code == INPUT_BTN_TOUCH) { + touch_state.pressed = event->value != 0; + } + // Like the GT911 driver, retain the latest complete touch sample. + if (event->sync) { + buffered_touch_state = touch_state; + } +} + +INPUT_CALLBACK_DEFINE(NULL, zephyr_input_callback, NULL); + +//| """Access input devices provided by Zephyr.""" +//| +//| def touch() -> tuple[int, int] | None: +//| """Return the latest buffered touch coordinates, or ``None`` when released. +//| +//| On native_sim, SDL mouse button and motion events are reported as touch. +//| """ +//| ... +static mp_obj_t zephyr_input_touch(void) { + if (!buffered_touch_state.pressed) { + return mp_const_none; + } + mp_obj_t coordinates[] = { + mp_obj_new_int(buffered_touch_state.x), + mp_obj_new_int(buffered_touch_state.y), + }; + return mp_obj_new_tuple(2, coordinates); +} +static MP_DEFINE_CONST_FUN_OBJ_0(zephyr_input_touch_obj, zephyr_input_touch); + +static const mp_rom_map_elem_t zephyr_input_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zephyr_input) }, + { MP_ROM_QSTR(MP_QSTR_touch), MP_ROM_PTR(&zephyr_input_touch_obj) }, +}; +static MP_DEFINE_CONST_DICT(zephyr_input_module_globals, zephyr_input_module_globals_table); + +const mp_obj_module_t zephyr_input_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&zephyr_input_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_zephyr_input, zephyr_input_module); diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl/board.conf b/ports/zephyr-cp/boards/native/native_sim_lvgl/board.conf index 13f7a316086..7a8f3764a94 100644 --- a/ports/zephyr-cp/boards/native/native_sim_lvgl/board.conf +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl/board.conf @@ -8,6 +8,7 @@ CONFIG_IOBROKER_PACKAGE_ONE_TO_ONE=y CONFIG_EMUL=y CONFIG_GPIO=y CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME=n +CONFIG_SPEED_OPTIMIZATIONS=y # So we can test safe mode CONFIG_NATIVE_SIM_REBOOT=y @@ -26,12 +27,20 @@ CONFIG_DISPLAY=y CONFIG_SDL_DISPLAY=y # Don't require hardware acceleration so the software renderer can be used. CONFIG_SDL_DISPLAY_USE_HARDWARE_ACCELERATOR=n +# Keep the priority-5 CircuitPython audio thread above display/input work. +CONFIG_SDL_THREAD_PRIORITY=6 +CONFIG_SDL_DISPLAY_THREAD_PRIORITY=6 + +# Route SDL mouse button/motion events through Zephyr's touch input driver. +CONFIG_INPUT_SDL_TOUCH=y +CONFIG_INPUT_MODE_SYNCHRONOUS=y # LVGL-based displayio compositing core. LVGL renders on its own RTOS thread (this # workqueue, also matching the RTOS deployment model); CircuitPython's compositor mirror # mutations take the glue's LVGL mutex so they don't interleave with lv_timer_handler. CONFIG_LVGL=y CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE=y +CONFIG_LV_Z_LVGL_WORKQUEUE_PRIORITY=6 # LVGL allocates objects and draw buffers from its own heap sizing. CONFIG_LV_Z_MEM_POOL_SIZE=16777216 # Cache decoded images (OnDiskBitmaps and other LVGL images) so they're only decoded diff --git a/ports/zephyr-cp/cptools/build_circuitpython.py b/ports/zephyr-cp/cptools/build_circuitpython.py index 48971d77f4c..aa307afab64 100644 --- a/ports/zephyr-cp/cptools/build_circuitpython.py +++ b/ports/zephyr-cp/cptools/build_circuitpython.py @@ -97,6 +97,7 @@ "audiofreeverb", "audiomixer", "audiomp3", + "audiotap", "synthio", ], "busio": ["fourwire", "i2cdisplaybus", "sdcardio", "sharpdisplay"], diff --git a/ports/zephyr-cp/cptools/zephyr2cp.py b/ports/zephyr-cp/cptools/zephyr2cp.py index b19a2b88ade..481490b4299 100644 --- a/ports/zephyr-cp/cptools/zephyr2cp.py +++ b/ports/zephyr-cp/cptools/zephyr2cp.py @@ -1146,6 +1146,9 @@ def zephyr_dts_to_cp_board(board_id, portdir, builddir, zephyrbuilddir, mpconfig if driver not in active_zephyr_devices: active_zephyr_devices[driver] = [] active_zephyr_devices[driver].append(node.labels) + elif driver == "input": + board_info["zephyr_input"] = True + logger.info("Supported Zephyr input device: %s", underscored) elif driver in EXCEPTIONAL_DRIVERS: pass elif driver in BUSIO_CLASSES: diff --git a/ports/zephyr-cp/prj.conf b/ports/zephyr-cp/prj.conf index 1eab058eec7..098fba60600 100644 --- a/ports/zephyr-cp/prj.conf +++ b/ports/zephyr-cp/prj.conf @@ -48,6 +48,10 @@ CONFIG_ASSERT=n CONFIG_EVENTS=y +# Enable the input subsystem so boards with status-okay input devices in their +# devicetree get a working zephyr_input module via zephyr2cp. +CONFIG_INPUT=y + # Enable the RTC driver layer so boards with a status-okay `rtc` devicetree # alias (e.g. native_sim's rtc-emul, STM32, RP2040, SmartBond) get a working # rtc module via common-hal/rtc. Boards without the alias only pay for the From bc7600e1ea5bb91ea994f9dbcc73a7d7c1281625 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 20 Nov 2026 11:24:53 -0800 Subject: [PATCH 2/2] Disable the input driver for board buttons --- .../zephyr-cp/boards/nordic/nrf54l15tag/board.overlay | 8 ++++++++ ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.conf | 4 ++++ .../zephyr-cp/boards/st/stm32wba65i_dk1/board.overlay | 11 +++++++++++ 3 files changed, 23 insertions(+) diff --git a/ports/zephyr-cp/boards/nordic/nrf54l15tag/board.overlay b/ports/zephyr-cp/boards/nordic/nrf54l15tag/board.overlay index 0dbeace63b0..582aec3fc0f 100644 --- a/ports/zephyr-cp/boards/nordic/nrf54l15tag/board.overlay +++ b/ports/zephyr-cp/boards/nordic/nrf54l15tag/board.overlay @@ -1,5 +1,13 @@ // No app.overlay because it doesn't have USB. +// Keep the button nodes and their GPIOs available for board pin-name +// generation, but don't instantiate the GPIO-keys input driver. The +// zephyr_input module currently exposes touch input only, and the input +// subsystem otherwise consumes most of this board's remaining flash. +&{/buttons} { + status = "disabled"; +}; + // The nrf54l15tag Zephyr board doesn't enable a UART or set zephyr,console, // unlike the nrf54l15dk. Set up UART30 as the console using available pins. &uart30 { diff --git a/ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.conf b/ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.conf index d83be8dddb5..7247e3f6d09 100644 --- a/ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.conf +++ b/ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.conf @@ -1,6 +1,10 @@ # USB OTG on STM32WBA requires VOS Range 1. Keep HCLK > 16 MHz. CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=32000000 +# GPIO was previously selected indirectly by the GPIO-keys input driver. +# CircuitPython's digitalio support needs it even with input nodes disabled. +CONFIG_GPIO=y + # Bluetooth doesn't start up for some reason. # CONFIG_BT=y # CONFIG_BT_PERIPHERAL=y diff --git a/ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.overlay b/ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.overlay index 8c82ae4e754..ce05f93571e 100644 --- a/ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.overlay +++ b/ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.overlay @@ -3,6 +3,17 @@ status = "okay"; }; +/* Keep the button/key nodes available for board pin-name generation, but + * don't instantiate the input drivers. zephyr_input currently exposes touch + * input only, so these key devices don't provide useful input through it. */ +&{/gpio_keys} { + status = "disabled"; +}; + +&{/adc-keys} { + status = "disabled"; +}; + /* The board's &rtc node is already status-okay in the upstream devicetree, so * alias it so the rtc module picks it up like other RTC boards do. */ / {