RP2 UART timeout is incorrect
To repro link pins 0 and 1 and paste the following:
from machine import UART, Pin
uart = UART(0, 115200, rx=Pin(1, Pin.IN), tx=Pin(0, Pin.OUT), timeout=1000_000, timeout_char=1000_000)
uart.readline()
It times out after 15s rather than 16.6minutes.
Setting a long timeout is a hack to resolve https://github.com/micropython/micropython/issues/10867. In my opinion it should be possible to disable the timeout (set it to infinity). In uasyncio systems, uasyncio should provide any timeouts required by the application.
machine.UART: Fix the double timeout with uart.read(), uart.write() and uart.readinto().
Summary
When uart.read(), uart.write() and uart_readinto() timed out because less data was transferred than expected, but some, the delay was uart.timeout + uart.timeout.char instead of just uart,timeout_char().
This PR changes the behavior into the expected one. It consists of 3 commits. The first changes uart.read() and uart.write(). The impact of that change is limited to the machine.UART class. The second commit changes stream.readinto() and may affect all uses of that function.
The third commit changes the ESP32's machine_uart.c to deal properly with the timeout_char argument for uart.read().
uart.readline() is unchanged.
This PR addresses issue #17611.
Testing
Tested with STM32(Pyboard V1.1 and PYBD_SF6), RP2 Pico, MIMXRT(Teensy 4.1), SAMD51, ESP32, NRF (nrf52840) and a CC3200 board using strings of varying length. Typical results:
RP2
UART(0, baudrate=38400, bits=8, parity=None, stop=1, tx=0, rx=1, txbuf=256, rxbuf=256, timeout=200, timeout_char=50, invert=None, irq=0)
uart.read() msg="" timeout=201 ms
uart.read() msg="123" timeout=52 ms
uart.read() msg="12345" timeout=1 ms
uart.readline() msg="" timeout=201 ms
uart.readline() msg="12345" timeout=202 ms
uart.readline() msg="12345\n" timeout=2 ms
uart.readinto(bytearray(5)) msg="" timeout=201 ms
uart.readinto(bytearray(5)) msg="123" timeout=52 ms
uart.readinto(bytearray(5)) msg="12345" timeout=1 ms
CC3200
UART(1, pins=(Pin("GP16"), Pin("GP17")))
uart.read() msg="" timeout=36 ms
uart.read() msg="123" timeout=38 ms (from 74 ms without the change)
uart.read() msg="12345" timeout=5 ms
uart.readline() msg="" timeout=35 ms
uart.readline() msg="12345" timeout=41 ms
uart.readline() msg="12345\n" timeout=6 ms
uart.readinto(bytearray(5)) msg="" timeout=35 ms
uart.readinto(bytearray(5)) msg="123" timeout=39 ms
uart.readinto(bytearray(5)) msg="12345" timeout=5 ms
Trade-offs and Alternatives
If the impact of the changes to a stream module seems unsafe, the second commit may be omitted, causing the behavior of uart.readinto() unchanged.