UART code under the hood

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
smhodge
Posts: 86
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

UART code under the hood

Post by smhodge » Sun Sep 15, 2019 2:47 am

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

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: UART code under the hood

Post by jimmo » Sun Sep 15, 2019 3:56 am

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.

smhodge
Posts: 86
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

Re: UART code under the hood

Post by smhodge » Mon Sep 16, 2019 4:51 pm

Thanks for the info and link to the source code. I just upgraded the firmware to 1.11.

Post Reply