Skip to content

PIO UART unexpected in_waiting and read results #4

Open
@anecdata

Description

@anecdata

CircuitPython version and board name

Adafruit CircuitPython 9.2.4 on 2025-01-29; Pimoroni Pico Plus 2 W with rp2350b
Adafruit CircuitPython 9.2.4 on 2025-01-29; Raspberry Pi Pico W with rp2040

Code/REPL

import time
import random
import board
import busio
import adafruit_pio_uart

time.sleep(3)  # wait for serial

# hardware setup: GP0 connected to GP27; GP1 connected to GP26
uart_busio = busio.UART(board.GP0, board.GP1)
uart_pio = adafruit_pio_uart.UART(board.GP26, board.GP27)

for buflen in (1, 2, 3, 7, 8, 9 ,10, 15, 16, 17, 18,):
    buffer = bytearray()
    for _ in range(0, buflen):
        buffer.append(random.choice(b'abcdefghijklmnopqrstuvwxyz'))
    print(f"\n{buflen=} {buffer=}")

    # PIO to busio
    uart_busio.reset_input_buffer()
    print(f"{uart_pio.write(buffer)=}")
    print(f"{uart_busio.in_waiting=}")
    print(f"{uart_busio.read(buflen)=}")

    # busio to PIO
    uart_pio.reset_input_buffer()
    print(f"{uart_busio.write(buffer)=}")
    time.sleep(0.25)  # w/o some wait, .in_waiting = 0
    print(f"{uart_pio.in_waiting=}")
    print(f"{uart_pio.read(buflen)=}")

Behavior

Code above compares busio UART behavior to PIO UART behavior (using the adafruit_pio_uart library).

busio correctly shows in_waiting bytes matching the number of bytes sent. read correctly reads the number of bytes sent.

For PIO, in_waiting maxxes out at 8, and read maxxes out at 9 bytes regardless of how many bytes (>9) are written to the UART.

Description

No response

Additional information

As noted in the code comments above, .in_waiting for the PIO will return 0 unless there is some delay before checking it. This extra blocking could be a problem for efficient user code, especially asyncio.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions