# Inspired by https://github.com/pimoroni/pimoroni-pico/blob/8ca47d640523cfb6c3ab262992574dfcfa8527fd/drivers/st7789/st7789_parallel.pio
# rp2.dma docs at https://github.com/micropython/micropython/pull/7670/files
# https://docs.micropython.org/en/latest/library/rp2.PIO.html
import rp2
from machine import Pin
d0 = 0
wr = 8 # GPIO pin number of the write strobe
iterations = 0
def tx_done(arg=None):
global iterations
iterations +=1
print(iterations)
@rp2.asm_pio(out_init=[rp2.PIO.OUT_LOW]*8, sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_RIGHT, autopull=True, pull_thresh=8)
def _program():
wrap_target()
out(pins, 8) .side(0) [4] # Output the next 8 bits in the fifo and strobe the WR pin
nop() .side(1) [4]
wrap()
# The state machine will automatically pull the next byte from the FIFO, and push it to the output shift register.
# This will continue until the FIFO is empty, at which point the state machine will stop.
# pio_num is index of the PIO block being used, sm_num is the state machine in that block.
# my_state_machine is an rp2.PIO() instance.
pio_num = 0
sm_num = 0
# my_state_machine = rp2.PIO(pio_num)
sm = rp2.StateMachine((pio_num*4)+sm_num, _program, freq=2000, sideset_base=Pin(wr), out_base=Pin(d0))
sm.active(True)
def transmit(data):
sm.put(data)
while sm.tx_fifo():
pass
tx_done()
###############################################
tx_fifo_reg = 0x50200000 + 0x010
DATA_REQUEST_INDEX = (pio_num << 3) + sm_num
d = rp2.DMA()
ctrl_noswap = d.pack_ctrl(
size=0, # Transfer bytes, rather than words,
inc_write=False, # don't increment the write address
treq_sel=DATA_REQUEST_INDEX, # pace the transfer.
irq_quiet=False, # Generate IRQs when the transfer is complete.
bswap=False, # Whether to byte-swap the data. Defaults to True.
# The following fields are defaults, and don't need to be specified.
enable=True,
high_pri=False,
inc_read=True,
ring_size=0,
ring_sel=0,
chain_to=0,
sniff_en=False,
write_err=False,
read_err=False,
)
ctrl_swap = d.pack_ctrl(default=ctrl_noswap, bswap=True)
def transmit_dma(data):
d.config(read=src_data, write=tx_fifo_reg, count=len(src_data), ctrl=ctrl_noswap, trigger=False)
d.irq(handler=tx_done, hard=False)
d.active(True)
###############################################
src_data = bytearray([i for i in range(256)])
transmit(src_data)