Skip to content

PIO UART unexpected in_waiting and read results #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
anecdata opened this issue Mar 13, 2025 · 0 comments
Open

PIO UART unexpected in_waiting and read results #4

anecdata opened this issue Mar 13, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@anecdata
Copy link
Member

anecdata commented Mar 13, 2025

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.

@anecdata anecdata added the bug Something isn't working label Mar 13, 2025
@anecdata anecdata transferred this issue from adafruit/circuitpython Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant