From bb337cde8e4916d69791edc27510d7f8bb4e0676 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Sat, 1 Oct 2022 13:02:22 +0200 Subject: [PATCH] pkg/tinyusb: use auto_init for tinyusb stack and thread setup --- pkg/tinyusb/Kconfig | 9 +++++++++ pkg/tinyusb/Makefile.dep | 2 ++ pkg/tinyusb/contrib/tinyusb.c | 13 +++++++++++++ pkg/tinyusb/doc.txt | 9 ++++++--- sys/auto_init/include/auto_init_priorities.h | 6 ++++++ tests/pkg_tinyusb_cdc_msc/main.c | 9 ++++++--- 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/pkg/tinyusb/Kconfig b/pkg/tinyusb/Kconfig index c40fb3b3bb..9d6b9797fb 100644 --- a/pkg/tinyusb/Kconfig +++ b/pkg/tinyusb/Kconfig @@ -42,6 +42,15 @@ menuconfig PACKAGE_TINYUSB if PACKAGE_TINYUSB +config MODULE_AUTO_INIT_TINYUSB + bool "Auto-initialize the tinyUSB package" + default y + depends on MODULE_AUTO_INIT + help + The tinyUSB stack including the used peripherals are initialized + automatically at startup. Additionally, the auto-initialization + starts the tinyUSB thread. + config MODULE_TINYUSB_COMMON bool help diff --git a/pkg/tinyusb/Makefile.dep b/pkg/tinyusb/Makefile.dep index 1105e31ce7..eaaacc48c1 100644 --- a/pkg/tinyusb/Makefile.dep +++ b/pkg/tinyusb/Makefile.dep @@ -6,6 +6,8 @@ USEMODULE += tinyusb_common USEMODULE += tinyusb_contrib USEMODULE += tinyusb_hw +DEFAULT_MODULE += auto_init_tinyusb + ifeq (,$(filter tinyusb_class_%,$(USEMODULE))) $(error At least one tinyusb_class_* module has to be enabled) endif diff --git a/pkg/tinyusb/contrib/tinyusb.c b/pkg/tinyusb/contrib/tinyusb.c index d7d481e5da..e4f3ecf3d1 100644 --- a/pkg/tinyusb/contrib/tinyusb.c +++ b/pkg/tinyusb/contrib/tinyusb.c @@ -16,6 +16,10 @@ #include "tinyusb.h" #include "tinyusb_hw.h" +#if IS_USED(MODULE_AUTO_INIT) +#include "auto_init_utils.h" +#endif + #define ENABLE_DEBUG 0 #include "debug.h" @@ -79,3 +83,12 @@ int tinyusb_setup(void) return 0; } + +void tinyusb_auto_init(void) +{ + tinyusb_setup(); +} + +#if IS_USED(MODULE_AUTO_INIT_TINYUSB) +AUTO_INIT(tinyusb_auto_init, AUTO_INIT_PRIO_MOD_TINYUSB); +#endif diff --git a/pkg/tinyusb/doc.txt b/pkg/tinyusb/doc.txt index ce1066fb20..7933e36a54 100644 --- a/pkg/tinyusb/doc.txt +++ b/pkg/tinyusb/doc.txt @@ -26,14 +26,17 @@ * USEMODULE += tinyusb_class_cdc tinyusb_class_msc * ``` * - * Add `tinyusb_setup()` to your main function to initialize the tinyUSB stack - * including used peripherals and to start the tinyUSB thread. + * Either add `tinyusb_setup()` to your main function to explicitly initialize + * the tinyUSB stack including the used peripherals and start the tinyUSB + * thread, or use the `auto_init` module (**default**). + * * ```c * int main(void) * { * ... + * // If auto-initialization is not used (module `auto_init`), * // initialize the tinyUSB stack including used peripherals and - * // start the tinyUSB thread + * // start the tinyUSB thread. Auto-initialization is used by default. * tinyusb_setup(); * * while (1) { diff --git a/sys/auto_init/include/auto_init_priorities.h b/sys/auto_init/include/auto_init_priorities.h index 8dd90b5ce3..be941ded7d 100644 --- a/sys/auto_init/include/auto_init_priorities.h +++ b/sys/auto_init/include/auto_init_priorities.h @@ -239,6 +239,12 @@ extern "C" { */ #define AUTO_INIT_PRIO_MOD_USBUS 1350 #endif +#ifndef AUTO_INIT_PRIO_MOD_TINYUSB +/** + * @brief tinyUSB priority + */ +#define AUTO_INIT_PRIO_MOD_TINYUSB 1350 +#endif #ifndef AUTO_INIT_PRIO_MOD_GNRC_NETIF /** * @brief GNRC netif priority diff --git a/tests/pkg_tinyusb_cdc_msc/main.c b/tests/pkg_tinyusb_cdc_msc/main.c index 5b59c6124f..06dbfd5214 100644 --- a/tests/pkg_tinyusb_cdc_msc/main.c +++ b/tests/pkg_tinyusb_cdc_msc/main.c @@ -88,9 +88,12 @@ int main(void) THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST, led_thread_impl, NULL, "led"); - /* initialize the tinyUSB stack including used peripherals and - * start the tinyUSB thread */ - tinyusb_setup(); + if (!IS_USED(MODULE_AUTO_INIT)) { + /* If auto-initialization is not used (module `auto_init`), + * initialize the tinyUSB stack including used peripherals and + * start the tinyUSB thread. Auto-initialization is used by default. */ + tinyusb_setup(); + } while (1) { ztimer_sleep(ZTIMER_MSEC, 10);