From 0bd67d98d9c88afe28fb71128f045fd384fb54ef Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 26 Oct 2022 21:13:05 +0200 Subject: [PATCH 1/4] cpu: always rely on stdio.inc.mk for default stdio selection --- cpu/avr8_common/Makefile.dep | 5 ----- cpu/esp_common/Makefile.dep | 4 ---- makefiles/stdio.inc.mk | 8 ++++---- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/cpu/avr8_common/Makefile.dep b/cpu/avr8_common/Makefile.dep index 6f0042d7b157..80c7f8d19725 100644 --- a/cpu/avr8_common/Makefile.dep +++ b/cpu/avr8_common/Makefile.dep @@ -12,11 +12,6 @@ USEMODULE += malloc_thread_safe # The AVR-libc provides no strerror, so we provide it via tiny_strerror USEMODULE += tiny_strerror_as_strerror -# the atmel port uses stdio_uart by default -ifeq (,$(filter stdio_% slipdev_stdio,$(USEMODULE))) - USEMODULE += stdio_uart -endif - # static C++ constructors need guards for thread safe initialization ifneq (,$(filter cpp,$(FEATURES_USED))) USEMODULE += cxx_ctor_guards diff --git a/cpu/esp_common/Makefile.dep b/cpu/esp_common/Makefile.dep index 23970904ca61..a608ac94c6af 100644 --- a/cpu/esp_common/Makefile.dep +++ b/cpu/esp_common/Makefile.dep @@ -16,10 +16,6 @@ ifneq (,$(filter mtd,$(USEMODULE))) USEMODULE += periph_flash endif -ifeq (,$(filter stdio_% slipdev_stdio,$(USEMODULE))) - USEMODULE += stdio_uart -endif - ifeq (xtensa,$(CPU_ARCH)) USEMODULE += esp_xtensa USEMODULE += xtensa diff --git a/makefiles/stdio.inc.mk b/makefiles/stdio.inc.mk index a6dbca1d6536..90047a7f5dfa 100644 --- a/makefiles/stdio.inc.mk +++ b/makefiles/stdio.inc.mk @@ -2,6 +2,7 @@ STDIO_MODULES = \ slipdev_stdio \ stdio_cdc_acm \ stdio_ethos \ + stdio_native \ stdio_nimble \ stdio_null \ stdio_rtt \ @@ -10,10 +11,9 @@ STDIO_MODULES = \ stdio_telnet \ # -ifneq (,$(filter newlib picolibc,$(USEMODULE))) - ifeq (,$(filter $(STDIO_MODULES),$(USEMODULE))) - USEMODULE += stdio_uart - endif +# select stdio_uart if no other stdio module is slected +ifeq (,$(filter $(STDIO_MODULES),$(USEMODULE))) + USEMODULE += stdio_uart endif ifneq (,$(filter stdio_cdc_acm,$(USEMODULE))) From 5938bd815a57cd31a56ee948d0281d605f499e89 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 26 Oct 2022 21:20:12 +0200 Subject: [PATCH 2/4] pkg/tinyusb: provide common tusb_config.h --- .../{tinyusb_config.h => tusb_config.h} | 11 +++-- pkg/tinyusb/doc.txt | 23 +++++++---- tests/pkg_tinyusb_cdc_msc/Makefile | 2 - tests/pkg_tinyusb_cdc_msc/tusb_config.h | 41 ------------------- 4 files changed, 22 insertions(+), 55 deletions(-) rename pkg/tinyusb/contrib/include/{tinyusb_config.h => tusb_config.h} (98%) delete mode 100644 tests/pkg_tinyusb_cdc_msc/tusb_config.h diff --git a/pkg/tinyusb/contrib/include/tinyusb_config.h b/pkg/tinyusb/contrib/include/tusb_config.h similarity index 98% rename from pkg/tinyusb/contrib/include/tinyusb_config.h rename to pkg/tinyusb/contrib/include/tusb_config.h index 9e0221a8e3d5..f090b43113a8 100644 --- a/pkg/tinyusb/contrib/include/tinyusb_config.h +++ b/pkg/tinyusb/contrib/include/tusb_config.h @@ -31,10 +31,13 @@ * @author Gunar Schorcht */ -#ifndef TINYUSB_CONFIG_H -#define TINYUSB_CONFIG_H +#ifndef TUSB_CONFIG_H +#define TUSB_CONFIG_H -#include "tusb_config.h" /* defined by the application */ +/* defined by the application */ +#if __has_include("tinyusb_app_config.h") +#include "tinyusb_app_config.h" +#endif #include "tinyusb.h" #if !DOXYGEN @@ -425,5 +428,5 @@ extern "C" { #endif #endif /* !DOXYGEN */ -#endif /* TINYUSB_CONFIG_H */ +#endif /* TUSB_CONFIG_H */ /** @} */ diff --git a/pkg/tinyusb/doc.txt b/pkg/tinyusb/doc.txt index 86023f9b37a2..bb68fc572bea 100644 --- a/pkg/tinyusb/doc.txt +++ b/pkg/tinyusb/doc.txt @@ -51,20 +51,27 @@ * } * ``` * - * Create a file `tusb_config.h` in your application directory which includes - * at least the file `tinyusb_config.h` from the tinyUSB package. This file is - * required by the tinyUSB stack and can be used to override the default - * configuration defined in `tinyusb_config.h`. + * Create a file `tinyusb_app_config.h` in your application directory. + * This file is can be used to override the default configuration defined + * in `tusb_config.h`. * ```c * #define CONFIG_TUSBD_CDC_NUMOF 2 - * - * #include "tinyusb_config.h" * ``` * + * By default, the number of `CONFIG_TUSBD_*_NUMOF` device class and + * `CONFIG_TUSBH_*_NUMOF` host class interfaces are defined to 1 if the + * corresponding `tinyusb_class_*` and `tinyusb_device`/`tinyusb_host` + * module are enabled, and 0 otherwise. + * That is, there is one interface of each class. + * + * For example, if the `tinyusb_device` and `tinyusb_class_cdc` modules are + * enabled, `CONFIG_TUSBD_CDC_NUMOF` is defined to 1 by default. The number of + * all other `CONFIG_TUSBD_*_NUMOF` device class interfaces are 0. + * * Add the application path to the include paths at the end of your * application's Makefile. This is necessary so that the tinyUSB stack - * uses the file `tusb_config.h` from your application directory and thus the - * file `tinyusb_config.h` from the tinyUSB package. + * uses the file `tinyusb_app_config.h` from your application directory + * and thus the file `tusb_config.h` from the tinyUSB package. * ```makefile * USEPKG += tinyusb * USEMODULE += tinyusb_class_cdc diff --git a/tests/pkg_tinyusb_cdc_msc/Makefile b/tests/pkg_tinyusb_cdc_msc/Makefile index 846cd4130dfd..543e18c7b846 100644 --- a/tests/pkg_tinyusb_cdc_msc/Makefile +++ b/tests/pkg_tinyusb_cdc_msc/Makefile @@ -9,5 +9,3 @@ USEMODULE += tinyusb_class_msc USEMODULE += tinyusb_device include $(RIOTBASE)/Makefile.include - -INCLUDES += -I$(APPDIR) diff --git a/tests/pkg_tinyusb_cdc_msc/tusb_config.h b/tests/pkg_tinyusb_cdc_msc/tusb_config.h deleted file mode 100644 index dcde62b0b6eb..000000000000 --- a/tests/pkg_tinyusb_cdc_msc/tusb_config.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2022 Gunar Schorcht - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -#ifndef TUSB_CONFIG_H -#define TUSB_CONFIG_H - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * By default, the number of `CFG_TUD_*` device class and `CFG_TUH_*` - * host class interfaces is defined to 1 if the corresponding `tinyusb_class_*` - * and `tinyusb_device`/`tinyusb_host` module are enabled, and 0 otherwise. - * That is, there is one interface of each class. - * - * For example, if the `tinyusb_device` and `tinyusb_class_cdc` modules are - * enabled, `CFG_TUD_CDC` is defined to 1 by default. The number of all other - * `CFG_TUD_*` device class interfaces are 0. - * - * To define a different number of device class or host class interfaces, - * just define them here to override these default values, for example: - * ```c - * #define CFG_TUD_CDC 2 - * #define CFG_TUD_HID 3 - * ``` - */ - -/* Default configuration defined by RIOT package tinyUSB has to be included last */ -#include "tinyusb_config.h" - -#ifdef __cplusplus -} -#endif - -#endif /* TUSB_CONFIG_H */ From 03bbd6badf2fa31b4d6c4ab3dc3ec56fa08077ee Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 26 Oct 2022 16:32:58 +0200 Subject: [PATCH 3/4] pkg/tinyusb: implement stdio via CDC ACM --- makefiles/stdio.inc.mk | 5 ++ pkg/tinyusb/Makefile | 5 +- pkg/tinyusb/Makefile.dep | 5 ++ pkg/tinyusb/cdc_acm_stdio/Makefile | 3 + pkg/tinyusb/cdc_acm_stdio/cdc_acm_stdio.c | 80 +++++++++++++++++++++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 pkg/tinyusb/cdc_acm_stdio/Makefile create mode 100644 pkg/tinyusb/cdc_acm_stdio/cdc_acm_stdio.c diff --git a/makefiles/stdio.inc.mk b/makefiles/stdio.inc.mk index 90047a7f5dfa..5ceb452228bb 100644 --- a/makefiles/stdio.inc.mk +++ b/makefiles/stdio.inc.mk @@ -9,6 +9,7 @@ STDIO_MODULES = \ stdio_semihosting \ stdio_uart \ stdio_telnet \ + stdio_tinyusb_cdc_acm \ # # select stdio_uart if no other stdio module is slected @@ -22,6 +23,10 @@ ifneq (,$(filter stdio_cdc_acm,$(USEMODULE))) USEMODULE += stdio_available endif +ifneq (,$(filter stdio_tinyusb_cdc_acm,$(USEMODULE))) + USEPKG += tinyusb +endif + ifneq (,$(filter stdio_rtt,$(USEMODULE))) USEMODULE += ztimer_msec endif diff --git a/pkg/tinyusb/Makefile b/pkg/tinyusb/Makefile index f4947cafd796..9fc37cb78bb8 100644 --- a/pkg/tinyusb/Makefile +++ b/pkg/tinyusb/Makefile @@ -11,9 +11,12 @@ PSRC = $(PKG_SOURCE_DIR)/src .PHONY: all -all: $(filter tinyusb_%,$(USEMODULE)) +all: $(filter tinyusb_% stdio_tinyusb_cdc_acm,$(USEMODULE)) $(QQ)"$(MAKE)" -C $(PSRC) -f $(RIOTBASE)/Makefile.base MODULE=tinyusb +stdio_tinyusb_cdc_acm: + $(QQ)"$(MAKE)" -C $(RIOTPKG)/$(PKG_NAME)/cdc_acm_stdio + tinyusb_contrib: $(QQ)"$(MAKE)" -C $(RIOTPKG)/$(PKG_NAME)/contrib diff --git a/pkg/tinyusb/Makefile.dep b/pkg/tinyusb/Makefile.dep index 2658b63799cc..fb62f89d19fc 100644 --- a/pkg/tinyusb/Makefile.dep +++ b/pkg/tinyusb/Makefile.dep @@ -8,6 +8,11 @@ USEMODULE += tinyusb_hw DEFAULT_MODULE += auto_init_tinyusb +ifneq (,$(filter stdio_tinyusb_cdc_acm, $(USEMODULE))) + USEMODULE += tinyusb_class_cdc + USEMODULE += tinyusb_device +endif + ifeq (,$(filter tinyusb_class_%,$(USEMODULE))) $(error At least one tinyusb_class_* module has to be enabled) endif diff --git a/pkg/tinyusb/cdc_acm_stdio/Makefile b/pkg/tinyusb/cdc_acm_stdio/Makefile new file mode 100644 index 000000000000..4ccfa930ea97 --- /dev/null +++ b/pkg/tinyusb/cdc_acm_stdio/Makefile @@ -0,0 +1,3 @@ +MODULE = stdio_tinyusb_cdc_acm + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/tinyusb/cdc_acm_stdio/cdc_acm_stdio.c b/pkg/tinyusb/cdc_acm_stdio/cdc_acm_stdio.c new file mode 100644 index 000000000000..00c75404ce6c --- /dev/null +++ b/pkg/tinyusb/cdc_acm_stdio/cdc_acm_stdio.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2022 ML!PA Consulting GmbH + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup pkg_tinyusb + * @{ + * + * @file + * @brief CDC ACM stdio implementation for tinyUSB CDC ACM + * + * This file implements a USB CDC ACM callback and read/write functions. + * + * @author Benjamin Valentin + * + * @} + */ + +#include +#include + +#include "tusb.h" +#include "tinyusb.h" + +#if MODULE_VFS +#include "vfs.h" +#endif + +#ifdef MODULE_USB_BOARD_RESET +#include "usb_board_reset_internal.h" +#endif + +static mutex_t data_lock = MUTEX_INIT_LOCKED; + +void stdio_init(void) +{ + /* Initialize this side of the CDC ACM pipe */ +#if MODULE_VFS + vfs_bind_stdio(); +#endif +} + +#if IS_USED(MODULE_STDIO_AVAILABLE) +int stdio_available(void) +{ + return tud_cdc_available(); +} +#endif + +ssize_t stdio_read(void* buffer, size_t len) +{ + mutex_lock(&data_lock); + return tud_cdc_read(buffer, len); +} + +ssize_t stdio_write(const void* buffer, size_t len) +{ + const char *start = buffer; + + while (tud_cdc_connected() && len) { + size_t n = tud_cdc_write(buffer, len); + buffer = (char *)buffer + n; + len -= n; + }; + + tud_cdc_write_flush(); + + return (char *)buffer - start; +} + +void tud_cdc_rx_cb(uint8_t itf) +{ + (void)itf; + + mutex_unlock(&data_lock); +} From ea0cf85d730108d48b75579cbb445bc4b48a223d Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 26 Oct 2022 16:41:49 +0200 Subject: [PATCH 4/4] tests/pkg_tinyusb_cdc_acm_stdio: add test for stdio via CDC ACM --- tests/pkg_tinyusb_cdc_acm_stdio/Makefile | 12 ++++++++++++ tests/pkg_tinyusb_cdc_acm_stdio/main.c | 1 + 2 files changed, 13 insertions(+) create mode 100644 tests/pkg_tinyusb_cdc_acm_stdio/Makefile create mode 120000 tests/pkg_tinyusb_cdc_acm_stdio/main.c diff --git a/tests/pkg_tinyusb_cdc_acm_stdio/Makefile b/tests/pkg_tinyusb_cdc_acm_stdio/Makefile new file mode 100644 index 000000000000..a14b018016b8 --- /dev/null +++ b/tests/pkg_tinyusb_cdc_acm_stdio/Makefile @@ -0,0 +1,12 @@ +include ../Makefile.tests_common + +USB_VID ?= $(USB_VID_TESTING) +USB_PID ?= $(USB_PID_TESTING) + +USEMODULE += stdio_tinyusb_cdc_acm + +USEMODULE += shell +USEMODULE += shell_commands +USEMODULE += ps + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg_tinyusb_cdc_acm_stdio/main.c b/tests/pkg_tinyusb_cdc_acm_stdio/main.c new file mode 120000 index 000000000000..121c7ff68ea3 --- /dev/null +++ b/tests/pkg_tinyusb_cdc_acm_stdio/main.c @@ -0,0 +1 @@ +../usbus_cdc_acm_stdio/main.c \ No newline at end of file