from machine import Pin
from rp2 import PIO, StateMachine, asm_pio
# Constants
UART_BAUD = 9600 # UART baud rate
PIN_BASE = 10 # Starting pin number
NUM_UARTS = 8 # Number of UART TXs
# PIO program for UART TX
@asm_pio(sideset_init=PIO.OUT_HIGH, out_init=PIO.OUT_HIGH, out_shiftdir=PIO.SHIFT_RIGHT)
def uart_tx():
pull() # Wait for data in TX FIFO
set(x, 7) .side(0) [7] # Start bit (LOW for 8 cycles)
label("bitloop")
out(pins, 1) [6] # Shift out 8 data bits, each bit lasts 8 cycles
jmp(x_dec, "bitloop") # Decrement bit counter, loop until done
nop() .side(1) [6] # Stop bit (HIGH for 8 cycles)
# Initialize 8 UART TXs on pins 10 to 17
uarts = []
for i in range(NUM_UARTS):
sm = StateMachine(
i, uart_tx, freq=8 * UART_BAUD,
sideset_base=Pin(PIN_BASE + i),
out_base=Pin(PIN_BASE + i)
)
sm.active(1)
uarts.append(sm)
# Function to send a string over a specific State Machine
def pio_uart_print(sm, s):
for c in s:
while sm.tx_fifo() >= 4: # Wait if the FIFO is full
pass
sm.put(ord(c)) # Send ASCII value of each character
# Send "hello" 100 times on all 8 UART TX lines
for _ in range(100): # Repeat 100 times
for u in uarts: # Send through all UARTs
pio_uart_print(u, "hello\n")