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

MicroPython • Re: Using DMA and PIO to stream to GPIO on Pico 2, like channel_irg.c example but continuously and in micropython

$
0
0
Here's how I did something similar for one of my projects:

Code:

from machine import Pinfrom rp2 import DMA, StateMachine, PIO, asm_piofrom array import arrayimport uctypesdata = bytes([0x0, 0x1, 0x2, 0x3])  # test dataaddr_buf = array('L', [uctypes.addressof(data)])  # get address of data buffer@asm_pio(out_init=(PIO.OUT_LOW,) * 8, out_shiftdir=PIO.SHIFT_RIGHT, autopull=True, pull_thresh=8)def set_pins():    out(pins, 8)    sm = StateMachine(0, set_pins, freq=100_000, out_base=Pin(2))sm.active(1)dma = DMA()dma2 = DMA()c = dma.pack_ctrl(size=0, inc_read=True, inc_write=False, treq_sel=0, chain_to=dma2.channel, enable=True)dma.config(ctrl=c)dma.read = datadma.write = smdma.count = len(data)c2 = dma2.pack_ctrl(size=2, inc_read=False, inc_write=False, chain_to=dma.channel, enable=True)dma2.config(ctrl=c2)dma2.read = addr_bufdma2.write = 0x50000000 + dma.channel * 0x40dma2.count = 1dma.active(1)
I don't know if that's the best approach, but it works fine for me. The main difference from what you posted is that it sends bytes to PIO, so everything is set up for byte transfers, but changing it to words should be trivial.

Statistics: Posted by horuable — Mon Jan 06, 2025 10:29 pm



Viewing all articles
Browse latest Browse all 4151

Trending Articles