From 9299df004fa5bd8a990bf656b74b4340577d800a Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Tue, 18 Aug 2026 21:47:48 +0200 Subject: [PATCH] Move driver listing to Module definition --- Documentation/ideas.md | 3 - Platforms/platform-esp32/source/module.cpp | 136 ++++++--------------- Platforms/platform-posix/source/module.cpp | 18 +-- 3 files changed, 41 insertions(+), 116 deletions(-) diff --git a/Documentation/ideas.md b/Documentation/ideas.md index c1c302c05..5bee0f5d3 100644 --- a/Documentation/ideas.md +++ b/Documentation/ideas.md @@ -46,14 +46,11 @@ ## Medium Priority -- `platform-esp32`'s module drivers are declared in start/stop of the module but they should be set via `Module::drivers` - `struct Driver` has an `.owner`, but it's not always set. Either validate on Module construct that it matches, or otherwise set it during module start. The problem: NULL parent currently means that driver is not removable. This clashes with setting it dynamically. Consider some kind of flag to determine removability. - Consider moving certain drivers into separate modules: audio, bt, wifi, etc - Consider using https://github.com/Graphify-Labs/graphify - Consider implementing LVGL gridnav in apps https://lvgl.io/docs/open/9.3/details/auxiliary-modules/gridnav.html - Make USB host driver disabled by default, so it doesn't consume memory -- Filtering for apps in App Hub: - - apps that only work on a specific device - Diceware app has large "+" and "-' buttons on Cardputer. It should be smaller. - TactilityTool: Make API compatibility table (and check for compatibility in the tool itself) - Improve EspLcdDisplay to contain all the standard configuration options, and implement a default init function. Add a configuration class. diff --git a/Platforms/platform-esp32/source/module.cpp b/Platforms/platform-esp32/source/module.cpp index 8e2b492d3..750931ab6 100644 --- a/Platforms/platform-esp32/source/module.cpp +++ b/Platforms/platform-esp32/source/module.cpp @@ -2,9 +2,6 @@ #include #endif -#include -#include -#include #include #include @@ -60,122 +57,61 @@ extern Driver esp32_usb_midi_device_driver; extern Driver esp32_usb_cdc_device_driver; #endif -static error_t start() { - /* We crash when construct fails, because if a single driver fails to construct, - * there is no guarantee that the previously constructed drivers can be destroyed */ - check(driver_construct_add(&esp32_adc_oneshot_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_gpio_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_i2c_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_i2c_master_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_i2s_driver) == ERROR_NONE); +static Driver* const platform_esp32_drivers[] = { + &esp32_adc_oneshot_driver, + &esp32_gpio_driver, + &esp32_i2c_driver, + &esp32_i2c_master_driver, + &esp32_i2s_driver, #if SOC_LCD_I80_SUPPORTED - check(driver_construct_add(&esp32_i8080_driver) == ERROR_NONE); + &esp32_i8080_driver, #endif - check(driver_construct_add(&esp32_pwm_ledc_driver) == ERROR_NONE); + &esp32_pwm_ledc_driver, #if SOC_SDMMC_HOST_SUPPORTED - check(driver_construct_add(&esp32_sdmmc_driver) == ERROR_NONE); + &esp32_sdmmc_driver, #endif - check(driver_construct_add(&esp32_sdspi_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_spi_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_uart_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_grove_driver) == ERROR_NONE); + &esp32_sdspi_driver, + &esp32_spi_driver, + &esp32_uart_driver, + &esp32_grove_driver, #if defined(CONFIG_SOC_WIFI_SUPPORTED) || defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED) - check(driver_construct_add(&esp32_wifi_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_wifi_pinned_driver) == ERROR_NONE); + &esp32_wifi_driver, + &esp32_wifi_pinned_driver, #endif #if defined(CONFIG_BT_NIMBLE_ENABLED) - check(driver_construct_add(&esp32_bluetooth_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_ble_serial_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_ble_midi_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_ble_hid_device_driver) == ERROR_NONE); + &esp32_bluetooth_driver, + &esp32_ble_serial_driver, + &esp32_ble_midi_driver, + &esp32_ble_hid_device_driver, #endif #if SOC_USB_OTG_SUPPORTED - check(driver_construct_add(&esp32_usbhost_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_usbhost_hid_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_usbhost_hid_keyboard_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_usbhost_midi_driver) == ERROR_NONE); - check(driver_construct_add(&esp32_usbhost_msc_driver) == ERROR_NONE); -#endif - // usbdevice0 and its children (usbdevicehid0, usbdevicemsc0, ...) are declared per-board in - // .dts, same pattern as usbhost0 above - the devicetree compiler constructs/adds/starts their - // Device instances and wires parent/child relationships. Only driver registration happens - // here. -#if SOC_USB_OTG_SUPPORTED && (CONFIG_TINYUSB_HID_COUNT || CONFIG_TINYUSB_MSC_ENABLED || CONFIG_TINYUSB_MIDI_COUNT || CONFIG_TINYUSB_CDC_ENABLED) - check(driver_construct_add(&esp32_usb_device_controller_driver) == ERROR_NONE); -#endif -#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_HID_COUNT - check(driver_construct_add(&esp32_usb_hid_device_driver) == ERROR_NONE); -#endif -#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_MSC_ENABLED - check(driver_construct_add(&esp32_usb_msc_device_driver) == ERROR_NONE); -#endif -#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_MIDI_COUNT - check(driver_construct_add(&esp32_usb_midi_device_driver) == ERROR_NONE); -#endif -#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_CDC_ENABLED - check(driver_construct_add(&esp32_usb_cdc_device_driver) == ERROR_NONE); -#endif - return ERROR_NONE; -} - -static error_t stop() { - /* We crash when destruct fails, because if a single driver fails to destruct, - * there is no guarantee that the previously destroyed drivers can be recovered */ -#if defined(CONFIG_SOC_WIFI_SUPPORTED) || defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED) - check(driver_remove_destruct(&esp32_wifi_pinned_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_wifi_driver) == ERROR_NONE); -#endif -#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_CDC_ENABLED - check(driver_remove_destruct(&esp32_usb_cdc_device_driver) == ERROR_NONE); -#endif -#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_MIDI_COUNT - check(driver_remove_destruct(&esp32_usb_midi_device_driver) == ERROR_NONE); -#endif -#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_MSC_ENABLED - check(driver_remove_destruct(&esp32_usb_msc_device_driver) == ERROR_NONE); -#endif -#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_HID_COUNT - check(driver_remove_destruct(&esp32_usb_hid_device_driver) == ERROR_NONE); + &esp32_usbhost_driver, + &esp32_usbhost_hid_driver, + &esp32_usbhost_hid_keyboard_driver, + &esp32_usbhost_midi_driver, + &esp32_usbhost_msc_driver, #endif #if SOC_USB_OTG_SUPPORTED && (CONFIG_TINYUSB_HID_COUNT || CONFIG_TINYUSB_MSC_ENABLED || CONFIG_TINYUSB_MIDI_COUNT || CONFIG_TINYUSB_CDC_ENABLED) - check(driver_remove_destruct(&esp32_usb_device_controller_driver) == ERROR_NONE); + &esp32_usb_device_controller_driver, #endif -#if SOC_USB_OTG_SUPPORTED - check(driver_remove_destruct(&esp32_usbhost_msc_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_usbhost_midi_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_usbhost_hid_keyboard_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_usbhost_hid_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_usbhost_driver) == ERROR_NONE); +#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_HID_COUNT + &esp32_usb_hid_device_driver, #endif -#if defined(CONFIG_BT_NIMBLE_ENABLED) - check(driver_remove_destruct(&esp32_ble_hid_device_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_ble_midi_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_ble_serial_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_bluetooth_driver) == ERROR_NONE); +#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_MSC_ENABLED + &esp32_usb_msc_device_driver, #endif - check(driver_remove_destruct(&esp32_adc_oneshot_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_gpio_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_i2c_master_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_i2s_driver) == ERROR_NONE); -#if SOC_LCD_I80_SUPPORTED - check(driver_remove_destruct(&esp32_i8080_driver) == ERROR_NONE); +#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_MIDI_COUNT + &esp32_usb_midi_device_driver, #endif - check(driver_remove_destruct(&esp32_pwm_ledc_driver) == ERROR_NONE); -#if SOC_SDMMC_HOST_SUPPORTED - check(driver_remove_destruct(&esp32_sdmmc_driver) == ERROR_NONE); +#if SOC_USB_OTG_SUPPORTED && CONFIG_TINYUSB_CDC_ENABLED + &esp32_usb_cdc_device_driver, #endif - check(driver_remove_destruct(&esp32_sdspi_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_spi_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_uart_driver) == ERROR_NONE); - check(driver_remove_destruct(&esp32_grove_driver) == ERROR_NONE); - return ERROR_NONE; -} + nullptr +}; Module platform_esp32_module = { .name = "platform-esp32", - .start = start, - .stop = stop, + .drivers = platform_esp32_drivers, .symbols = nullptr, .internal = nullptr }; diff --git a/Platforms/platform-posix/source/module.cpp b/Platforms/platform-posix/source/module.cpp index 5c83f7787..9d9b8d0e0 100644 --- a/Platforms/platform-posix/source/module.cpp +++ b/Platforms/platform-posix/source/module.cpp @@ -1,26 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 -#include -#include #include extern "C" { extern Driver posix_wifi_driver; -static error_t start() { - check(driver_construct_add(&posix_wifi_driver) == ERROR_NONE); - return ERROR_NONE; -} - -static error_t stop() { - check(driver_remove_destruct(&posix_wifi_driver) == ERROR_NONE); - return ERROR_NONE; -} +static Driver* const platform_posix_drivers[] = { + &posix_wifi_driver, + nullptr +}; struct Module platform_posix_module = { .name = "platform-posix", - .start = start, - .stop = stop, + .drivers = platform_posix_drivers, .symbols = nullptr, .internal = nullptr };