From 59743aed13a4240e03f28c38231dc96b3c2cce6e Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Thu, 28 Feb 2019 21:40:35 +0100 Subject: [PATCH] USBUS cdc acm: Add STDIO wrapper for CDC ACM --- Makefile.dep | 17 +++--- makefiles/pseudomodules.inc.mk | 1 + sys/auto_init/usb/auto_init_usb.c | 8 +++ sys/usb/usbus/cdc/acm/Makefile | 3 ++ sys/usb/usbus/cdc/acm/cdc_acm_stdio.c | 74 +++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 sys/usb/usbus/cdc/acm/cdc_acm_stdio.c diff --git a/Makefile.dep b/Makefile.dep index cc006103c8..1339dbca5c 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -401,7 +401,7 @@ ifneq (,$(filter newlib,$(USEMODULE))) ifeq (,$(filter newlib_syscalls_%,$(USEMODULE))) USEMODULE += newlib_syscalls_default endif - ifeq (,$(filter stdio_rtt,$(USEMODULE))) + ifeq (,$(filter stdio_rtt stdio_cdc_acm,$(USEMODULE))) USEMODULE += stdio_uart endif endif @@ -414,6 +414,11 @@ ifneq (,$(filter posix_sockets,$(USEMODULE))) USEMODULE += xtimer endif +ifneq (,$(filter stdio_cdc_acm,$(USEMODULE))) + USEMODULE += usbus_cdc_acm + USEMODULE += isrpipe +endif + ifneq (,$(filter stdio_rtt,$(USEMODULE))) USEMODULE += xtimer endif @@ -874,17 +879,17 @@ ifneq (,$(filter tlsf-malloc,$(USEMODULE))) USEPKG += tlsf endif -ifneq (,$(filter usbus_cdc_acm,$(USEMODULE))) - USEMODULE += tsrb - USEMODULE += usbus -endif - ifneq (,$(filter usbus,$(USEMODULE))) FEATURES_REQUIRED += periph_usbdev USEMODULE += core_thread_flags USEMODULE += event endif +ifneq (,$(filter usbus_cdc_acm,$(USEMODULE))) + USEMODULE += tsrb + USEMODULE += usbus +endif + ifneq (,$(filter usbus_cdc_ecm,$(USEMODULE))) USEMODULE += iolist USEMODULE += fmt diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 818839dbe5..e9462ecac8 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -80,6 +80,7 @@ PSEUDOMODULES += sock_tcp PSEUDOMODULES += sock_udp PSEUDOMODULES += stdin PSEUDOMODULES += stdio_ethos +PSEUDOMODULES += stdio_cdc_acm PSEUDOMODULES += stdio_uart_rx PSEUDOMODULES += sock_dtls diff --git a/sys/auto_init/usb/auto_init_usb.c b/sys/auto_init/usb/auto_init_usb.c index 6d85d3622b..dfaf2d848e 100644 --- a/sys/auto_init/usb/auto_init_usb.c +++ b/sys/auto_init/usb/auto_init_usb.c @@ -28,6 +28,9 @@ #include "usb/usbus/cdc/ecm.h" usbus_cdcecm_device_t cdcecm; #endif +#ifdef MODULE_USBUS_CDC_ACM +#include "usb/usbus/cdc/acm.h" +#endif static char _stack[USBUS_STACKSIZE]; static usbus_t usbus; @@ -42,6 +45,11 @@ void auto_init_usb(void) usbus_init(&usbus, usbdev); /* USBUS function handlers initialization */ +#ifdef MODULE_STDIO_CDC_ACM + void usb_cdc_acm_stdio_init(usbus_t *usbus); + usb_cdc_acm_stdio_init(&usbus); +#endif + #ifdef MODULE_USBUS_CDC_ECM usbus_cdcecm_init(&usbus, &cdcecm); #endif diff --git a/sys/usb/usbus/cdc/acm/Makefile b/sys/usb/usbus/cdc/acm/Makefile index 7624bfb51e..dac9843796 100644 --- a/sys/usb/usbus/cdc/acm/Makefile +++ b/sys/usb/usbus/cdc/acm/Makefile @@ -1,4 +1,7 @@ MODULE = usbus_cdc_acm SRC = cdc_acm.c +ifneq (,$(filter stdio_cdc_acm,$(USEMODULE))) + SRC += cdc_acm_stdio.c +endif include $(RIOTBASE)/Makefile.base diff --git a/sys/usb/usbus/cdc/acm/cdc_acm_stdio.c b/sys/usb/usbus/cdc/acm/cdc_acm_stdio.c new file mode 100644 index 0000000000..c61af0b223 --- /dev/null +++ b/sys/usb/usbus/cdc/acm/cdc_acm_stdio.c @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2018 Koen Zandberg + * + * 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 sys + * @{ + * + * @file + * @brief CDC ACM stdio implementation for USBUS CDC ACM + * + * This file implements a USB CDC ACM callback and read/write functions. + * + * + * @} + */ + +#include + +#include "isrpipe.h" + +#include "usb/usbus.h" +#include "usb/usbus/cdc/acm.h" + +#if MODULE_VFS +#include "vfs.h" +#endif + +static usbus_cdcacm_device_t cdcacm; +static uint8_t _cdc_tx_buf_mem[USBUS_CDC_ACM_STDIO_BUF_SIZE]; +static uint8_t _cdc_rx_buf_mem[USBUS_CDC_ACM_STDIO_BUF_SIZE]; +static isrpipe_t _cdc_stdio_isrpipe = ISRPIPE_INIT(_cdc_rx_buf_mem); + +void stdio_init(void) +{ + /* Initialize this side of the CDC ACM pipe */ +#if MODULE_VFS + vfs_bind_stdio(); +#endif +} + +ssize_t stdio_read(void* buffer, size_t len) +{ + (void)buffer; + (void)len; + return isrpipe_read(&_cdc_stdio_isrpipe, buffer, len); +} + +ssize_t stdio_write(const void* buffer, size_t len) +{ + usbus_cdc_acm_submit(&cdcacm, buffer, len); + usbus_cdc_acm_flush(&cdcacm); + /* Use tsrb and flush */ + return len; +} + +static void _cdc_acm_rx_pipe(usbus_cdcacm_device_t *cdcacm, + uint8_t *data, size_t len) +{ + (void)cdcacm; + for (size_t i = 0; i < len; i++) { + isrpipe_write_one(&_cdc_stdio_isrpipe, data[i]); + } +} + +void usb_cdc_acm_stdio_init(usbus_t *usbus) +{ + usbus_cdc_acm_init(usbus, &cdcacm, _cdc_acm_rx_pipe, NULL, + _cdc_tx_buf_mem, sizeof(_cdc_tx_buf_mem)); +}