mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-26 15:03:53 +01:00
Merge pull request #17447 from gschorcht/sys/arduino_serial_stdio
sys/arduino: add Serial over stdio support
This commit is contained in:
commit
3676b63583
@ -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
|
||||
*/
|
||||
|
||||
@ -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
|
||||
*/
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)))
|
||||
|
||||
@ -34,3 +34,9 @@ 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
|
||||
default y if MODULE_STDIO_CDC_ACM
|
||||
|
||||
@ -26,3 +26,4 @@ INCLUDES += -I$(RIOTBASE)/sys/arduino/include
|
||||
CXXEXFLAGS += -std=c++11
|
||||
|
||||
PSEUDOMODULES += arduino_pwm
|
||||
PSEUDOMODULES += arduino_serial_stdio
|
||||
|
||||
@ -22,8 +22,14 @@ extern "C" {
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
3
tests/sys_arduino/app.config.test
Normal file
3
tests/sys_arduino/app.config.test
Normal file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user