Skip to content

Support stdio drivers with blocking character input #1020

@alastairpatrick

Description

@alastairpatrick

The signature of stdio_driver_t::in_chars is:

int (*in_chars)(char *buf, int len)

I would like to add another entry point to stdio_driver_t:

int (*in_chars_timeout_us)(char *buf, int len, uint64_t until)

The reason is, the former makes it awkward to implement a blocking stdio driver for an RTOS. Here is what I would like to write for qOS:

int in_chars_timeout_us(char *buf, int len, uint64_t until) {
  int i = 0;
  for (; i < len; ++i) {
    while (uart0_hw->fr & UART_UARTFR_RXFE_BITS) {
      if (!qos_await_irq(UART0_IRQ, &uart0_hw->imsc, UART_UARTIMSC_RXIM_BITS | UART_UARTIMSC_RTIM_BITS, until)) {
        goto timeout;
      }
    }
    buf[i] = uart0_hw->dr;
  }

timeout:
  return i ? i : PICO_ERROR_NO_DATA;
}

Without the added until parameter, the options are to have qos_await_irq() not block, in which case stdio input busy waits or to have qos_await_irq() timeout after an arbitrary amount of time, such as one second, which reduces the precision of the timeout passed to getchar_timeout_us().

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions