stm32: Implement Asynchronous UART TX
I am attempting to write large blocks (eg: 2048 bytes) of data to the UART at low baudrates whilst attempting to execute other code. I've discovered that the UART TX is actually blocking until the write is completed. This is a bit of a pain...
This issue is for creating an asynchronous write option for UART. Related to #1533
Probably either interrupt or DMA driven. Should ideally behave like the UART RX does, with a definable buffer in python code, or block as it currently does.
Reading related issues it seems like #1422 has had some success using DMA. Personally, I don't require the callback as I'm not streaming from a file, just an existing bytearray in memory.
I haven't had a chance to read the huge threads on some of the issues I've found so I'm not 100% up to date yet...
stm32/uart: Fix UART timeout issue with low baudrate.
MicroPython Version
v1.20.0-261-g813d559bc
Environment
NUCLEO-G474RE
How to reproduce
Execute this code:
from pyb import UART
uart = UART(1, 4800, bits=8, parity=None, stop=1)
print(uart)
print(uart.any())
print(uart.write("123"))
print(uart.write(b"abcd"))
print(uart.writechar(1))
Got result:
UART(1, baudrate=4800, bits=8, parity=None, stop=1, flow=0, timeout=0, timeout_char=4, rxbuf=64)
0
3
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
OSError: [Errno 110] ETIMEDOUT
Detail of changes
timeout_char is calculated at
https://github.com/micropython/micropython/blob/813d559bc098eeaa1c6e0fa1deff92e666c0b458/ports/stm32/machine_uart.c#L253
However, if 13000 / baudrate is not divisible by baudrate (such as 4800, 9600, ...), calculated timeout will be less than actual timeout required.
To solve this issue, this PR rounds up timeout_char if 13000 / baudrate is not divisible by baudrate.
Since this PR affects test results using tests/pyb/uart.py, I also modified expected test result.
Effect of changes
Be able to write values with low baudlate.
UART(1, baudrate=4800, bits=8, parity=None, stop=1, flow=0, timeout=0, timeout_char=5, rxbuf=64)
0
3
4
None