From 725472cbc42243407670d61ef05ee08639f09542 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Sat, 25 Dec 2021 13:29:28 +0100 Subject: [PATCH 1/5] sys/arduino: add Serial over stdio support If module `arduino_serial_stdio` is used and `ARDUINO_UART_DEV` is `UART_UNDEF`, the STDIO is used for `Serial`. It requires that the used `stdio` backend implements `stdio_available`. --- sys/arduino/Kconfig | 5 +++ sys/arduino/Makefile.include | 1 + sys/arduino/serialport.cpp | 56 +++++++++++++++++++++++++++++++ tests/sys_arduino/app.config.test | 3 ++ 4 files changed, 65 insertions(+) create mode 100644 tests/sys_arduino/app.config.test diff --git a/sys/arduino/Kconfig b/sys/arduino/Kconfig index b277840975..6c648e7b23 100644 --- a/sys/arduino/Kconfig +++ b/sys/arduino/Kconfig @@ -34,3 +34,8 @@ config MODULE_ARDUINO_PWM depends on MODULE_ARDUINO depends on TEST_KCONFIG select MODULE_PERIPH_PWM + +config MODULE_ARDUINO_SERIAL_STDIO + bool "Use STDIO as Serial" + depends on MODULE_ARDUINO + depends on TEST_KCONFIG \ No newline at end of file diff --git a/sys/arduino/Makefile.include b/sys/arduino/Makefile.include index c9a218d7db..769c1bb9cc 100644 --- a/sys/arduino/Makefile.include +++ b/sys/arduino/Makefile.include @@ -26,3 +26,4 @@ INCLUDES += -I$(RIOTBASE)/sys/arduino/include CXXEXFLAGS += -std=c++11 PSEUDOMODULES += arduino_pwm +PSEUDOMODULES += arduino_serial_stdio diff --git a/sys/arduino/serialport.cpp b/sys/arduino/serialport.cpp index 9b527b3efb..2f3602d418 100644 --- a/sys/arduino/serialport.cpp +++ b/sys/arduino/serialport.cpp @@ -22,8 +22,14 @@ extern "C" { #include #include +#include "assert.h" #include "fmt.h" #include "irq.h" +#include "kernel_defines.h" + +#if IS_USED(MODULE_ARDUINO_SERIAL_STDIO) +#include "stdio_base.h" +#endif } #include "serialport.hpp" @@ -36,11 +42,23 @@ void rx_cb(void *arg, uint8_t c) SerialPort::SerialPort(uart_t dev) { +#if !IS_USED(MODULE_ARDUINO_SERIAL_STDIO) + assert(dev != UART_UNDEF); +#endif this->dev = dev; } int SerialPort::available(void) { +#if IS_USED(MODULE_ARDUINO_SERIAL_STDIO) + if (this->dev == UART_UNDEF) { +#if IS_USED(MODULE_STDIO_AVAILABLE) + return stdio_available(); +#else /* IS_USED(MODULE_STDIO_AVAILABLE) */ + return 0; +#endif /* IS_USED(MODULE_STDIO_AVAILABLE) */ + } +#endif /* IS_USED(MODULE_ARDUINO_SERIAL_STDIO) */ return (int)rx_buf.avail; } @@ -48,11 +66,21 @@ void SerialPort::begin(long baudrate) { /* this clears the contents of the ringbuffer... */ ringbuffer_init(&rx_buf, rx_mem, SERIAL_RX_BUFSIZE); +#if IS_USED(MODULE_ARDUINO_SERIAL_STDIO) + if (this->dev == UART_UNDEF) { + return; + } +#endif uart_init(dev, (uint32_t)baudrate, rx_cb, (void *)&rx_buf); } void SerialPort::end(void) { +#if IS_USED(MODULE_ARDUINO_SERIAL_STDIO) + if (this->dev == UART_UNDEF) { + return; + } +#endif uart_poweroff(dev); } @@ -252,6 +280,16 @@ int SerialPort::read(void) int res = -1; irq_disable(); +#if IS_USED(MODULE_ARDUINO_SERIAL_STDIO) + if (this->dev == UART_UNDEF) { + char chr; + if (this->available()) { + res = (stdio_read((void *)&chr, 1) == 1) ? chr : -1; + } + irq_enable(); + return res; + } +#endif if (rx_buf.avail > 0) { res = ringbuffer_get_one(&rx_buf); } @@ -262,18 +300,36 @@ int SerialPort::read(void) int SerialPort::write(int val) { +#if IS_USED(MODULE_ARDUINO_SERIAL_STDIO) + if (this->dev == UART_UNDEF) { + stdio_write((const void *)&val, 1); + return 1; + } +#endif uart_write(dev, (uint8_t *)&val, 1); return 1; } int SerialPort::write(const char *str) { +#if IS_USED(MODULE_ARDUINO_SERIAL_STDIO) + if (this->dev == UART_UNDEF) { + stdio_write((const void *)str, strlen(str)); + return strlen(str); + } +#endif uart_write(dev, (uint8_t *)str, strlen(str)); return strlen(str); } int SerialPort::write(char *buf, int len) { +#if IS_USED(MODULE_ARDUINO_SERIAL_STDIO) + if (this->dev == UART_UNDEF) { + stdio_write((const void *)buf, len); + return len; + } +#endif uart_write(dev, (uint8_t *)buf, len); return len; } diff --git a/tests/sys_arduino/app.config.test b/tests/sys_arduino/app.config.test new file mode 100644 index 0000000000..7a14c2de6a --- /dev/null +++ b/tests/sys_arduino/app.config.test @@ -0,0 +1,3 @@ +# this file enables modules defined in Kconfig. Do not use this file for +# application configuration. This is only needed during migration. +CONFIG_MODULE_ARDUINO=y From 2dcd76195416fbe775378464e6ce17a0121bef04 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Sat, 25 Dec 2021 13:31:27 +0100 Subject: [PATCH 2/5] sys/arduino: enable arduino_serial_stdio if stdio_cdc_acm is used --- sys/Makefile.dep | 3 +++ sys/arduino/Kconfig | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/sys/Makefile.dep b/sys/Makefile.dep index e59d3816a1..79a3a70ef8 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -10,6 +10,9 @@ ifneq (,$(filter arduino,$(USEMODULE))) USEMODULE += fmt USEMODULE += ztimer_usec USEMODULE += ztimer_msec + ifneq (,$(filter stdio_cdc_acm,$(USEMODULE))) + USEMODULE += arduino_serial_stdio + endif endif ifneq (,$(filter arduino_pwm,$(FEATURES_USED))) diff --git a/sys/arduino/Kconfig b/sys/arduino/Kconfig index 6c648e7b23..b1ffeff5f4 100644 --- a/sys/arduino/Kconfig +++ b/sys/arduino/Kconfig @@ -38,4 +38,5 @@ config MODULE_ARDUINO_PWM config MODULE_ARDUINO_SERIAL_STDIO bool "Use STDIO as Serial" depends on MODULE_ARDUINO - depends on TEST_KCONFIG \ No newline at end of file + depends on TEST_KCONFIG + default y if MODULE_STDIO_CDC_ACM From 332f97098780021d0258aa3894ced6795f6b4d3a Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Sat, 25 Dec 2021 13:32:29 +0100 Subject: [PATCH 3/5] boards/feather-m0: use UART_UNDEF to enable Serial over STDIO --- boards/feather-m0/include/arduino_board.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/boards/feather-m0/include/arduino_board.h b/boards/feather-m0/include/arduino_board.h index 513d5447b3..4e97255ff8 100644 --- a/boards/feather-m0/include/arduino_board.h +++ b/boards/feather-m0/include/arduino_board.h @@ -31,6 +31,13 @@ extern "C" { */ #define ARDUINO_LED (13) +/** + * @brief On-board serial port mapping, stdio is used for Serial + */ +#ifndef ARDUINO_UART_DEV +#define ARDUINO_UART_DEV UART_UNDEF +#endif + /** * @brief Look-up table for the Arduino's digital pins */ From 529c6fa5b4bcf403a48f1d7b58655d01b8f68063 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Sat, 25 Dec 2021 13:32:44 +0100 Subject: [PATCH 4/5] boards/arduino-mkr: use UART_UNDEF to enable Serial over STDIO --- boards/common/arduino-mkr/include/arduino_board.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/boards/common/arduino-mkr/include/arduino_board.h b/boards/common/arduino-mkr/include/arduino_board.h index f09234fe20..2704aa4cfe 100644 --- a/boards/common/arduino-mkr/include/arduino_board.h +++ b/boards/common/arduino-mkr/include/arduino_board.h @@ -28,6 +28,13 @@ extern "C" { #endif +/** + * @brief On-board serial port mapping, stdio is used for Serial + */ +#ifndef ARDUINO_UART_DEV +#define ARDUINO_UART_DEV UART_UNDEF +#endif + /** * @brief Look-up table for the Arduino's digital pins */ From a7dc4808b7315c4c40142e522a106b4c39e4bff8 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Thu, 6 Jan 2022 17:33:08 +0100 Subject: [PATCH 5/5] boards/sodaq-*: use UART_UNDEF to enable Serial over STDIO --- boards/sodaq-autonomo/include/arduino_board.h | 4 +++- boards/sodaq-explorer/include/arduino_board.h | 4 +++- boards/sodaq-one/include/arduino_board.h | 4 +++- boards/sodaq-sara-aff/include/arduino_board.h | 4 +++- boards/sodaq-sara-sff/include/arduino_board.h | 4 +++- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/boards/sodaq-autonomo/include/arduino_board.h b/boards/sodaq-autonomo/include/arduino_board.h index dbf200ed43..aacb468936 100644 --- a/boards/sodaq-autonomo/include/arduino_board.h +++ b/boards/sodaq-autonomo/include/arduino_board.h @@ -35,7 +35,9 @@ extern "C" { /** * @brief On-board serial port mapping */ -#define ARDUINO_UART_DEV UART_DEV(0) +#ifndef ARDUINO_UART_DEV +#define ARDUINO_UART_DEV UART_UNDEF +#endif /** * @brief Look-up table for the Arduino's digital pins diff --git a/boards/sodaq-explorer/include/arduino_board.h b/boards/sodaq-explorer/include/arduino_board.h index e4ceaa21b3..8fe4a317cc 100644 --- a/boards/sodaq-explorer/include/arduino_board.h +++ b/boards/sodaq-explorer/include/arduino_board.h @@ -34,7 +34,9 @@ extern "C" { /** * @brief On-board serial port mapping */ -#define ARDUINO_UART_DEV UART_DEV(0) +#ifndef ARDUINO_UART_DEV +#define ARDUINO_UART_DEV UART_UNDEF +#endif /** * @brief Look-up table for the Arduino's digital pins diff --git a/boards/sodaq-one/include/arduino_board.h b/boards/sodaq-one/include/arduino_board.h index a5fae805ca..19f8597fbf 100644 --- a/boards/sodaq-one/include/arduino_board.h +++ b/boards/sodaq-one/include/arduino_board.h @@ -34,7 +34,9 @@ extern "C" { /** * @brief On-board serial port mapping */ -#define ARDUINO_UART_DEV UART_DEV(0) +#ifndef ARDUINO_UART_DEV +#define ARDUINO_UART_DEV UART_UNDEF +#endif /** * @brief Look-up table for the Arduino's digital pins diff --git a/boards/sodaq-sara-aff/include/arduino_board.h b/boards/sodaq-sara-aff/include/arduino_board.h index 457dbdf256..b0a78981b1 100644 --- a/boards/sodaq-sara-aff/include/arduino_board.h +++ b/boards/sodaq-sara-aff/include/arduino_board.h @@ -34,7 +34,9 @@ extern "C" { /** * @brief On-board serial port mapping */ -#define ARDUINO_UART_DEV UART_DEV(0) +#ifndef ARDUINO_UART_DEV +#define ARDUINO_UART_DEV UART_UNDEF +#endif /** * @brief Look-up table for the Arduino's digital pins diff --git a/boards/sodaq-sara-sff/include/arduino_board.h b/boards/sodaq-sara-sff/include/arduino_board.h index 3edcb755c3..84fae71689 100644 --- a/boards/sodaq-sara-sff/include/arduino_board.h +++ b/boards/sodaq-sara-sff/include/arduino_board.h @@ -34,7 +34,9 @@ extern "C" { /** * @brief On-board serial port mapping */ -#define ARDUINO_UART_DEV UART_DEV(0) +#ifndef ARDUINO_UART_DEV +#define ARDUINO_UART_DEV UART_UNDEF +#endif /** * @brief Look-up table for the Arduino's digital pins