Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/tinyusb: implement stdio via CDC ACM #18804

Merged
merged 4 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions cpu/avr8_common/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions cpu/esp_common/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions makefiles/stdio.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ STDIO_MODULES = \
slipdev_stdio \
stdio_cdc_acm \
stdio_ethos \
stdio_native \
stdio_nimble \
stdio_null \
stdio_rtt \
stdio_semihosting \
stdio_uart \
stdio_telnet \
stdio_tinyusb_cdc_acm \
#

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)))
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pkg/tinyusb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions pkg/tinyusb/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pkg/tinyusb/cdc_acm_stdio/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE = stdio_tinyusb_cdc_acm

include $(RIOTBASE)/Makefile.base
80 changes: 80 additions & 0 deletions pkg/tinyusb/cdc_acm_stdio/cdc_acm_stdio.c
Original file line number Diff line number Diff line change
@@ -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 <benjamin.valentin@ml-pa.com>
*
* @}
*/

#include <stdio.h>
#include <sys/types.h>

#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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
* @author Gunar Schorcht <gunar@schorcht.net>
*/

#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
Expand Down Expand Up @@ -425,5 +428,5 @@ extern "C" {
#endif

#endif /* !DOXYGEN */
#endif /* TINYUSB_CONFIG_H */
#endif /* TUSB_CONFIG_H */
/** @} */
23 changes: 15 additions & 8 deletions pkg/tinyusb/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
* ```
*
benpicco marked this conversation as resolved.
Show resolved Hide resolved
* 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
Expand Down
12 changes: 12 additions & 0 deletions tests/pkg_tinyusb_cdc_acm_stdio/Makefile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/pkg_tinyusb_cdc_acm_stdio/main.c
2 changes: 0 additions & 2 deletions tests/pkg_tinyusb_cdc_msc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ USEMODULE += tinyusb_class_msc
USEMODULE += tinyusb_device

include $(RIOTBASE)/Makefile.include

INCLUDES += -I$(APPDIR)
41 changes: 0 additions & 41 deletions tests/pkg_tinyusb_cdc_msc/tusb_config.h

This file was deleted.