Skip to content

Commit

Permalink
Merge pull request #19738 from benpicco/stdio_dispatch
Browse files Browse the repository at this point in the history
stdio_dispatch: allow to select multiple stdio methods at the same time
  • Loading branch information
benpicco authored Feb 9, 2024
2 parents 6c96952 + 018b732 commit 55b6728
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 347 deletions.
37 changes: 11 additions & 26 deletions cpu/esp32/stdio_usb_serial_jtag/usb_serial_jtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
#include <errno.h>
#include <sys/types.h>

#include "isrpipe.h"
#include "tsrb.h"
#include "stdio_base.h"

#include "irq_arch.h"
#include "esp_attr.h"
Expand All @@ -33,41 +32,20 @@
#include "soc/periph_defs.h"
#include "rom/ets_sys.h"

static uint8_t _rx_buf_mem[USB_SERIAL_JTAG_PACKET_SZ_BYTES];
static isrpipe_t stdio_serial_isrpipe = ISRPIPE_INIT(_rx_buf_mem);

static tsrb_t serial_tx_rb;
static uint8_t serial_tx_rb_buf[USB_SERIAL_JTAG_PACKET_SZ_BYTES];

#define IRQ_MASK (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | \
(IS_USED(MODULE_STDIO_USB_SERIAL_JTAG_RX) * USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT))

Check warning on line 39 in cpu/esp32/stdio_usb_serial_jtag/usb_serial_jtag.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters

ssize_t stdio_write(const void *buffer, size_t len)
static ssize_t _write(const void *buffer, size_t len)
{
tsrb_add(&serial_tx_rb, buffer, len);
USB_SERIAL_JTAG.int_ena.val = IRQ_MASK;

return len;
}

ssize_t stdio_read(void* buffer, size_t count)
{
if (IS_USED(MODULE_STDIO_USB_SERIAL_JTAG_RX)) {
return (ssize_t)isrpipe_read(&stdio_serial_isrpipe, buffer, count);
}

return -ENOTSUP;
}

int stdio_available(void)
{
if (IS_USED(MODULE_STDIO_AVAILABLE)) {
return tsrb_avail(&stdio_serial_isrpipe.tsrb);
}

return -ENOTSUP;
}

IRAM_ATTR
static void _serial_intr_handler(void *arg)
{
Expand All @@ -80,7 +58,7 @@ static void _serial_intr_handler(void *arg)
/* read data if available */
while (IS_USED(MODULE_STDIO_USB_SERIAL_JTAG_RX) &&
usb_serial_jtag_ll_rxfifo_data_available()) {
isrpipe_write_one(&stdio_serial_isrpipe, USB_SERIAL_JTAG.ep1.rdwr_byte);
isrpipe_write_one(&stdin_isrpipe, USB_SERIAL_JTAG.ep1.rdwr_byte);
}

/* write data if there is a free stop */
Expand All @@ -104,7 +82,7 @@ static void _serial_intr_handler(void *arg)
irq_isr_exit();
}

void stdio_init(void)
static void _init(void)
{
tsrb_init(&serial_tx_rb, serial_tx_rb_buf, sizeof(serial_tx_rb_buf));

Expand All @@ -128,4 +106,11 @@ void stdio_init(void)
intr_cntrl_ll_set_int_level(CPU_INUM_SERIAL_JTAG, 1);
#endif
}

static void _detach(void)
{
intr_cntrl_ll_disable_interrupts(BIT(CPU_INUM_SERIAL_JTAG));
}

STDIO_PROVIDER(STDIO_ESP32_SERIAL_JTAG, _init, _detach, _write)
/**@}*/
4 changes: 4 additions & 0 deletions drivers/ethos/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include "isrpipe.h"
#include "stdio_uart.h"

#ifndef STDIO_UART_RX_BUFSIZE
#define STDIO_UART_RX_BUFSIZE STDIO_RX_BUFSIZE
#endif

extern ethos_t ethos;

static uint8_t _rx_buf_mem[STDIO_UART_RX_BUFSIZE];
Expand Down
7 changes: 3 additions & 4 deletions drivers/slipdev/slipdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ static void _slip_rx_cb(void *arg, uint8_t byte)
break;
case SLIPDEV_END:
dev->state = SLIPDEV_STATE_NONE;
byte = 0;
/* fall-through */
break;
default:
isrpipe_write_one(&slipdev_stdio_isrpipe, byte);
isrpipe_write_one(&stdin_isrpipe, byte);
break;
}
return;
Expand All @@ -79,7 +78,7 @@ static void _slip_rx_cb(void *arg, uint8_t byte)
break;
}
dev->state = SLIPDEV_STATE_STDIN;
isrpipe_write_one(&slipdev_stdio_isrpipe, byte);
isrpipe_write_one(&stdin_isrpipe, byte);
return;
#endif
case SLIPDEV_STATE_NONE:
Expand Down
32 changes: 5 additions & 27 deletions drivers/slipdev/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,22 @@
#include "stdio_base.h"
#include "stdio_uart.h"

static uint8_t _rx_buf_mem[STDIO_UART_RX_BUFSIZE];

isrpipe_t slipdev_stdio_isrpipe = ISRPIPE_INIT(_rx_buf_mem);
mutex_t slipdev_mutex = MUTEX_INIT;

static void _isrpipe_write(void *arg, uint8_t data)
{
isrpipe_write_one(arg, (char)data);
}

void stdio_init(void)
static void _init(void)
{
/* intentionally overwritten in netdev init so we have stdio before
* the network device is initialized is initialized */
uart_init(slipdev_params[0].uart, slipdev_params[0].baudrate,
_isrpipe_write, &slipdev_stdio_isrpipe);
_isrpipe_write, &stdin_isrpipe);
}

ssize_t stdio_read(void *buffer, size_t len)
{
uint8_t *ptr = buffer;

while (len) {
int read = isrpipe_read(&slipdev_stdio_isrpipe, ptr, 1);

if (read == 0) {
continue;
}

if (*ptr == 0) {
break;
}

++ptr;
--len;
}
return ptr - (uint8_t *)buffer;
}

ssize_t stdio_write(const void *buffer, size_t len)
static ssize_t _write(const void *buffer, size_t len)
{
mutex_lock(&slipdev_mutex);
slipdev_write_byte(slipdev_params[0].uart, SLIPDEV_STDIO_START);
Expand All @@ -73,4 +49,6 @@ ssize_t stdio_write(const void *buffer, size_t len)
return len;
}

STDIO_PROVIDER(STDIO_SLIP, _init, NULL, _write)

/** @} */
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ PSEUDOMODULES += soft_uart_modecfg
PSEUDOMODULES += stdin
PSEUDOMODULES += stdio_available
PSEUDOMODULES += stdio_cdc_acm
PSEUDOMODULES += stdio_dispatch
PSEUDOMODULES += stdio_ethos
PSEUDOMODULES += stdio_nimble_debug
PSEUDOMODULES += stdio_telnet
Expand Down
19 changes: 19 additions & 0 deletions makefiles/stdio.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,29 @@ STDIO_MODULES = \
stdio_usb_serial_jtag \
#

STDIO_LEGACY_MODULES = \
ethos_stdio \
stdio_ethos \
stdio_native # requires #19002 \
#

# select stdio_uart if no other stdio module is slected
ifeq (,$(filter $(STDIO_MODULES),$(USEMODULE)))
USEMODULE += stdio_uart
endif

ifeq (,$(filter $(STDIO_LEGACY_MODULES),$(USEMODULE)))
USEMODULE += stdio
endif

ifneq (,$(filter stdin,$(USEMODULE)))
USEMODULE += isrpipe
endif

ifneq (1, $(words $(filter $(STDIO_MODULES),$(USEMODULE))))
USEMODULE += stdio_dispatch
endif

ifneq (,$(filter stdio_cdc_acm,$(USEMODULE)))
USEMODULE += usbus_cdc_acm
USEMODULE += isrpipe
Expand Down Expand Up @@ -78,6 +96,7 @@ endif

ifneq (,$(filter stdio_udp,$(USEMODULE)))
USEMODULE += sock_udp
USEMODULE += sock_async
endif

# enable stdout buffering for modules that benefit from sending out buffers in larger chunks
Expand Down
30 changes: 9 additions & 21 deletions pkg/tinyusb/cdc_acm_stdio/cdc_acm_stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,12 @@

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

#include "tusb.h"
#include "tinyusb.h"

static mutex_t data_lock = MUTEX_INIT_LOCKED;

void stdio_init(void)
{
}

#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)
static ssize_t _write(const void* buffer, size_t len)
{
const char *start = buffer;

Expand All @@ -62,9 +44,15 @@ ssize_t stdio_write(const void* buffer, size_t len)
return (char *)buffer - start;
}

#ifdef MODULE_STDIN
void tud_cdc_rx_cb(uint8_t itf)
{
(void)itf;

mutex_unlock(&data_lock);
uint8_t buffer[64];
unsigned res = tud_cdc_read(buffer, sizeof(buffer));
isrpipe_write(&stdin_isrpipe, buffer, res);
}
#endif

STDIO_PROVIDER(STDIO_TINYUSB_CDC_ACM, NULL, NULL, _write)
3 changes: 2 additions & 1 deletion sys/fmt/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

#include "kernel_defines.h"
#include "fmt.h"
#include "stdio_base.h"

extern ssize_t stdio_write(const void* buffer, size_t len);

static const char _hex_chars[16] = "0123456789ABCDEF";

Expand Down
Loading

0 comments on commit 55b6728

Please sign in to comment.