How do I read from serial?

I’m using a Nucleo STM32F411. I can’t print to serial, but I can’t figure out how to read a char. getc() and stdio_read() doesn’t seem to work.

The closest I can seem to get is to call uart_init(). It takes an argument rx_cb that is called for every byte received. It states that the RX buffer is filled, but the docs don’t tell me how to access the buffer.

What do I need to do? A complete example would be useful.

To be honest, the documentation for Riot is a bit lacking.

So I think in my Makefile I should add: USEMODULE += stdio_uart_rx

Then in main(): int c= getchar();

The read is blocking, which is OK for now.

Ah, OK, it appears that if I want non-blocking, I can use use uart_init() with a callback, which takes a void *arg (not sure what practical use that would be), as well as the byte received.

I haven’t tried the non-blocking out, though.

Some kind of cookbook would be useful so that we’re not trying to figure this all out by looking at the source code.

Hi @blippy, sorry for the late answer. Your question is quite broad, but I’ll try to answer as best as I can:

I can’t print to serial, but I can’t figure out how to read a char. getc() and stdio_read() doesn’t seem to work.

RIOT is a very very modular system you only pull in what you need/want to use. But that means you actually to pull it in, so depending on what application you are testing or running that might or not be the case. Here you seem to be using the stdio library. This is not really related to RIOT, stdin has standard functions to print to stdout and read from stdin, so exactly the ones you mention.

But by default as you found out stdio is included but stdio_uart is a special case where stdin has tobe pulled in (pulling stdio_uart_rx also works but stdin is actually the one that selects stdio_uart_rx. But other stdio modules do not need to do this. This I agree could use some more love with documentation.

The closest I can seem to get is to call uart_init(). It takes an argument rx_cb that is called for every byte received. It states that the RX buffer is filled, but the docs don’t tell me how to access the buffer. What do I need to do? A complete example would be useful.

You can also choose not to include stdio and tap into the uart peripheral directly. iotlab-training has an uart example you could get inspired by. There are also examples in the RIOT tree.

Ah, OK, it appears that if I want non-blocking, I can use use uart_init() with a callback, which takes a void *arg (not sure what practical use that would be), as well as the byte received.

Well the [stdio uart implemntation](Ah, OK, it appears that if I want non-blocking, I can use use uart_init() with a callback, which takes a void *arg (not sure what practical use that would be), as well as the byte received.) uses this to pass the descriptor to the pipe.

I haven’t tried the non-blocking out, though.

stdin function you are using are blocking, you can either put those into a thread, or you can use uart directly and read from that buffer at your own speed. tsrb and ringbuffer could be of use here.