Skip to content

Commit 6150a9e

Browse files
committed
Change HM01B0 PIO driver to use DMA transfers
Transfers now complete in ~17.6ms (theoretical limit is ~16.4ms) Transfers used to take ~30ms without DMA
1 parent e9e992e commit 6150a9e

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

cv2_drivers/displays/st7789_pio.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .st7789 import ST7789
22
from machine import Pin
33
import rp2
4+
# import time
45

56
# Derived from:
67
# https://github.com/raspberrypi/pico-examples/tree/master/pio/st7789_lcd
@@ -67,11 +68,24 @@ def __init__(
6768
# so we need to save them again to restore later when _write() is called
6869
self.txMode, self.txAlt = self.savePinModeAlt(self.tx)
6970
self.clkMode, self.clkAlt = self.savePinModeAlt(self.clk)
70-
71+
7172
# Now restore the original mode and alt of the pins
7273
self.tx.init(mode=txMode, alt=txAlt)
7374
self.clk.init(mode=clkMode, alt=clkAlt)
74-
75+
76+
# Set up DMA to transfer to the PIO state machine
77+
self.dma = rp2.DMA()
78+
req_num = ((self.sm_id // 4) << 3) + (self.sm_id % 4)
79+
dma_ctrl = self.dma.pack_ctrl(
80+
size = 0, # 0 = 8-bit, 1 = 16-bit, 2 = 32-bit
81+
inc_write = False,
82+
treq_sel = req_num
83+
)
84+
self.dma.config(
85+
write = self.sm,
86+
ctrl = dma_ctrl
87+
)
88+
7589
# Call the parent class constructor
7690
super().__init__(width, height, rotation, color_order, reverse_bytes_in_word)
7791

@@ -109,9 +123,16 @@ def _pio_write(self, data):
109123
"""Write data to the display using PIO."""
110124
# Start the state machine
111125
self.sm.active(1)
112-
113-
# Write data to the state machine
114-
self.sm.put(data, 24)
126+
127+
# Configure DMA to read from the buffer and write to the state machine
128+
self.dma.read = data
129+
count = len(data) if isinstance(data, (bytes, bytearray)) else data.size
130+
self.dma.count = count
131+
132+
# Start the DMA transfer and wait for it to finish
133+
self.dma.active(True)
134+
while self.dma.active():
135+
pass
115136

116137
# Stop the state machine
117138
self.sm.active(0)

0 commit comments

Comments
 (0)