Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5216

General • Need some help figuring out PIO code problem

$
0
0
I am trying to send a 9 bit byte by using the PIO to create a uart. The 9th bit (MSB) is either set to 0 for data or 1 for address. I took the PIO uart tx example and tried to modify it. I have a logic analyzer connected to the pin and all I ever get is 1 single byte as data (9th bit low). It never sends 2 bytes I've spent my afternoon trying to figure it. I hope someone here can help me with this. here's the code:

Code:

from machine import Pinfrom rp2 import PIO, StateMachine, asm_piofrom time import sleep, sleep_msUART_BAUD = 9600PIN_BASE = Pin(3, Pin.OUT, value=1)sleep(1)@asm_pio(sideset_init=PIO.OUT_HIGH, out_init=PIO.OUT_HIGH, out_shiftdir=PIO.SHIFT_RIGHT)def uart_tx_data():    # Block with TX deasserted until data available    pull()    # Initialise bit counter, assert start bit for 8 cycles    set(x, 7)  .side(0)       [7]    # Shift out 8 data bits, 8 execution cycles per bit    label("bitloop")    out(pins, 1)              [6]    jmp(x_dec, "bitloop")    # add 1 more bit to act as data bit Set x low for data    set(x, 0)    .side(0)    out(pins, y)              [6]    # Assert stop bit    set(x, 1)    .side(1)    out(pins, x) .side(1)     [6]@asm_pio(sideset_init=PIO.OUT_HIGH, out_init=PIO.OUT_HIGH, out_shiftdir=PIO.SHIFT_RIGHT)def uart_tx_address():    # Block with TX deasserted until data available    pull()    # Initialise bit counter, assert start bit for 8 cycles    set(x, 7)  .side(0)       [7]    # Shift out 8 data bits, 8 execution cycles per bit    label("bitloop")    out(pins, 1)              [6]    jmp(x_dec, "bitloop")    # add 1 more bit to act as address bit. Set x high for address    set(x, 1)    .side(0)    out(pins, x)              [6]    # Assert stop bit    set(x, 1)    .side(1)    out(pins, x) .side(1)     [6]sm_address = StateMachine(0, uart_tx_address, freq=8 * UART_BAUD, sideset_base=Pin(PIN_BASE), out_base=Pin(PIN_BASE))sm_data = StateMachine(1, uart_tx_data, freq=8 * UART_BAUD, sideset_base=Pin(PIN_BASE), out_base=Pin(PIN_BASE))    sm_address.active(1)sm_data.active(1)sm_address.put(0x0B)  # Send address bytesleep(1)sm_data.put(0x0B)  # Send data byte# Keep the program runningwhile True:    pass
Image
Screenshot 2024-09-16 at 5.54.01 PM.png

Statistics: Posted by donmerch — Mon Sep 16, 2024 11:04 pm



Viewing all articles
Browse latest Browse all 5216

Trending Articles