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

sys/stdio_uart: add stdio_uart_onlcr (pseudo-) module #18731

Merged
merged 1 commit into from
Oct 15, 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
9 changes: 8 additions & 1 deletion makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,15 @@ PSEUDOMODULES += stdio_available
PSEUDOMODULES += stdio_cdc_acm
PSEUDOMODULES += stdio_ethos
PSEUDOMODULES += stdio_nimble_debug
PSEUDOMODULES += stdio_uart_rx
PSEUDOMODULES += stdio_telnet
## @defgroup sys_stdio_uart_onlcr Support for DOS line endings in STDIO-UART
## @ingroup sys_stdio_uart
## @{
## Enable this (pseudo-) module to emit DOS style line endings (`\r\n`) instead
## of UNIX style line endings (`\n`) via STDIO over UART.
PSEUDOMODULES += stdio_uart_onlcr
## @}
PSEUDOMODULES += stdio_uart_rx
PSEUDOMODULES += stm32_eth
PSEUDOMODULES += stm32_eth_auto
PSEUDOMODULES += stm32_eth_link_up
Expand Down
4 changes: 4 additions & 0 deletions sys/Kconfig.stdio
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ config MODULE_STDIO_UART
depends on HAS_PERIPH_UART
select MODULE_PERIPH_UART

config MODULE_STDIO_UART_ONLCR
bool "Emit DOS line endings for STDIO output via UART"
depends on MODULE_STDIO_UART

config MODULE_STDIO_NATIVE
bool "Native"
depends on CPU_ARCH_NATIVE
Expand Down
25 changes: 25 additions & 0 deletions sys/include/stdio_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,37 @@
*
* @brief Standard input/output backend using UART
*
* ## Input
*
* @warning Standard input is disabled by default on UART. To enable it, load
* the `stdin` module in your application:
* ```
* USEMODULE += stdin
* ```
*
* ## UART Configuration
*
* @note Running `make BOARD=<your_board> term` will launch `pyterm` with the
* correct parameters, so mostly you do not really need to care about
* the UART configuration.
*
* By convention RIOT boards used 8N1 encoding with a symbol rate of 115200 Bd
* for the UART used as stdio. However, some boards may have a different
* configuration due to hardware limitations. Most notably, many AVR boards use
* 9600 Bd as symbol rate instead, as they otherwise frequently loose an input
* character due to losing interrupts.
*
* By default UNIX style line endings (`\n`) are used. However, some terminal
* programs default to DOS style line endings (`\r\n`). It usually is better to
* configure the terminal program on the host to use UNIX style line endings.
* In scenarios this is not possible/desired, you can enable the (pseudo-)
* module @ref sys_stdio_uart_onlcr to emit DOS style line endings instead.
*
* RIOT's shell happily accepts both DOS and UNIX style line endings in any
* case, so typically no line ending conversion is needed on the input.
*
* ## STDIO from ISR
*
* @attention Using STDIO over UART from interrupt context should be avoided,
* except for debugging purposes
*
Expand Down
28 changes: 25 additions & 3 deletions sys/stdio_uart/stdio_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/

#include <errno.h>
#include <string.h>

#include "stdio_uart.h"

Expand Down Expand Up @@ -85,8 +86,29 @@ ssize_t stdio_read(void* buffer, size_t count)
#endif
}

ssize_t stdio_write(const void* buffer, size_t len)
ssize_t stdio_write(const void *buffer, size_t len)
{
uart_write(STDIO_UART_DEV, (const uint8_t *)buffer, len);
return len;
ssize_t result = len;
if (IS_USED(MODULE_STDIO_UART_ONLCR)) {
static const uint8_t crlf[2] = { (uint8_t)'\r', (uint8_t)'\n' };
const uint8_t *buf = buffer;
while (len) {
const uint8_t *pos = memchr(buf, '\n', len);
size_t chunk_len = (pos != NULL)
? (uintptr_t)pos - (uintptr_t)buf
: len;
uart_write(STDIO_UART_DEV, buf, chunk_len);
buf += chunk_len;
maribu marked this conversation as resolved.
Show resolved Hide resolved
len -= chunk_len;
if (len) {
uart_write(STDIO_UART_DEV, crlf, sizeof(crlf));
buf++;
len--;
}
}
}
else {
uart_write(STDIO_UART_DEV, (const uint8_t *)buffer, len);
}
return result;
}