Page 1 of 1

UART code under the hood

Posted: Sun Sep 15, 2019 2:47 am
by smhodge
Several UART methods imply that under the hood the code can accumulated multiple bytes received, for example, UART.any() returns "the number of bytes waiting". Based on other languages and microcontrollers I interpret that to mean that the underlying UART code is interrupt-driven and accumulate bytes into an internal buffer, maybe a circular buffer perhaps 256 bytes long.

1. Is that correct?
2. If so, what is the length of the buffer, i.e., how many bytes can arrive before the buffer is full?
3. Does the interrupt qualify as an "external" interrupt to wake up from a pyb.wfi() or pyb.stop() state?

Thanks

Re: UART code under the hood

Posted: Sun Sep 15, 2019 3:56 am
by jimmo
Yes. There's a circular buffer.

The simple answer is that if you care about the receive buffer, you should set the size explicitly using the rxbuf kwarg to the UART constructor (or uart.init). See https://docs.micropython.org/en/latest/ ... .UART.init

In detail though, by default the UART will have a 64 byte rx buffer. (The code is slightly complicated because this argument was originally called read_buf_len, which init() still accepts). But you can look in ports/stm32/machine_uart.c to see where it calls uart_set_rxbuf.

Additionally, the UART assigned to the REPL defaults to 260 bytes.
smhodge wrote:
Sun Sep 15, 2019 2:47 am
3. Does the interrupt qualify as an "external" interrupt to wake up from a pyb.wfi() or pyb.stop() state?
wfi, yes. stop, fairly sure. Easy to test though.

Re: UART code under the hood

Posted: Mon Sep 16, 2019 4:51 pm
by smhodge
Thanks for the info and link to the source code. I just upgraded the firmware to 1.11.