| 
1 | 1 | from .st7789 import ST7789  | 
2 | 2 | from machine import Pin  | 
3 | 3 | import rp2  | 
 | 4 | +# import time  | 
4 | 5 | 
 
  | 
5 | 6 | # Derived from:  | 
6 | 7 | # https://github.com/raspberrypi/pico-examples/tree/master/pio/st7789_lcd  | 
@@ -67,11 +68,24 @@ def __init__(  | 
67 | 68 |         # so we need to save them again to restore later when _write() is called  | 
68 | 69 |         self.txMode, self.txAlt = self.savePinModeAlt(self.tx)  | 
69 | 70 |         self.clkMode, self.clkAlt = self.savePinModeAlt(self.clk)  | 
70 |  | -          | 
 | 71 | + | 
71 | 72 |         # Now restore the original mode and alt of the pins  | 
72 | 73 |         self.tx.init(mode=txMode, alt=txAlt)  | 
73 | 74 |         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 | + | 
75 | 89 |         # Call the parent class constructor  | 
76 | 90 |         super().__init__(width, height, rotation, color_order, reverse_bytes_in_word)  | 
77 | 91 | 
 
  | 
@@ -109,9 +123,16 @@ def _pio_write(self, data):  | 
109 | 123 |         """Write data to the display using PIO."""  | 
110 | 124 |         # Start the state machine  | 
111 | 125 |         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  | 
115 | 136 | 
 
  | 
116 | 137 |         # Stop the state machine  | 
117 | 138 |         self.sm.active(0)  | 
 | 
0 commit comments