@@ -16,6 +16,7 @@ class ST7789_PIO(ST7789):
1616 pin_tx (Pin): Transmit pin number **Required**
1717 pin_dc (Pin): Data/Command pin number **Required**
1818 pin_cs (Pin): Chip Select pin number
19+ freq (int): State machine frequency in Hz, default -1 (system clock)
1920 rotation (int): Orientation of display
2021 - 0-Portrait, default
2122 - 1-Landscape
@@ -36,47 +37,56 @@ def __init__(
3637 pin_tx ,
3738 pin_dc ,
3839 pin_cs = None ,
40+ freq = - 1 ,
3941 rotation = 0 ,
4042 color_order = ST7789 .BGR ,
4143 reverse_bytes_in_word = True ,
4244 ):
4345 # Store PIO arguments
4446 self .sm_id = sm_id
45- self .pin_clk = pin_clk
46- self .pin_tx = pin_tx
47- # self.pin_dc = pin_dc
48- # self.pin_cs = pin_cs
49-
50- self .clk = Pin (pin_clk , Pin .OUT ) # Don't change mode/alt
51- self .tx = Pin (pin_tx , Pin .OUT ) # Don't change mode/alt
52- self .clk = Pin (pin_clk , Pin .ALT , alt = Pin .ALT_PIO0 ) # Don't change mode/alt
53- self .tx = Pin (pin_tx , Pin .ALT , alt = Pin .ALT_PIO0 ) # Don't change mode/alt
54- self .dc = Pin (pin_dc , Pin .OUT ) # Don't change mode/alt
47+ self .clk = Pin (pin_clk ) # Don't change mode/alt
48+ self .tx = Pin (pin_tx ) # Don't change mode/alt
49+ self .dc = Pin (pin_dc ) # Don't change mode/alt
5550 self .cs = Pin (pin_cs , Pin .OUT , value = 1 ) if pin_cs else None
51+ self .freq = freq
5652
57- program = self ._pio_write_spi
58- # program[0][0]=0x6001
59- # program[0][4]=0xb042
60- print (program )
53+ # Get the current mode and alt of the pins so they can be restored
54+ txMode , txAlt = self .savePinModeAlt (self .tx )
55+ clkMode , clkAlt = self .savePinModeAlt (self .clk )
6156
57+ # Initialize the PIO state machine
6258 self .sm = rp2 .StateMachine (
6359 self .sm_id ,
64- program ,
65- out_base = self .pin_tx ,
66- sideset_base = self .pin_clk ,
67- # out_shiftdir = rp2.PIO.SHIFT_LEFT ,
60+ self . _pio_write_spi ,
61+ freq = self .freq ,
62+ out_base = self .tx ,
63+ sideset_base = self . clk ,
6864 )
65+
66+ # The tx and clk pins just got their mode and alt set for PIO0 or PIO1,
67+ # so we need to save them again to restore later when _write() is called
68+ self .txMode , self .txAlt = self .savePinModeAlt (self .tx )
69+ self .clkMode , self .clkAlt = self .savePinModeAlt (self .clk )
70+
71+ # Now restore the original mode and alt of the pins
72+ self .tx .init (mode = txMode , alt = txAlt )
73+ self .clk .init (mode = clkMode , alt = clkAlt )
6974
75+ # Call the parent class constructor
7076 super ().__init__ (width , height , rotation , color_order , reverse_bytes_in_word )
7177
7278 def _write (self , command = None , data = None ):
7379 """SPI write to the device: commands and data."""
74- # Save the current mode and alt of the DC pin in case it's used by
80+ # Save the current mode and alt of the spi pins in case they're used by
7581 # another device on the same SPI bus
76- # dcMode, dcAlt = self.savePinModeAlt(self.dc)
82+ dcMode , dcAlt = self .savePinModeAlt (self .dc )
83+ txMode , txAlt = self .savePinModeAlt (self .tx )
84+ clkMode , clkAlt = self .savePinModeAlt (self .clk )
7785
78- # Temporarily set the DC pin to output mode
86+ # Temporarily set the SPI pins to the correct mode and alt for PIO
7987 self .dc .init (mode = Pin .OUT )
88+ self .tx .init (mode = self .txMode , alt = self .txAlt )
89+ self .clk .init (mode = self .clkMode , alt = self .clkAlt )
8090
8191 # Write to the display
8292 if self .cs :
@@ -90,8 +100,10 @@ def _write(self, command=None, data=None):
90100 if self .cs :
91101 self .cs .on ()
92102
93- # Restore the DC pin to its original mode and alt
94- # self.dc.init(mode=dcMode, alt=dcAlt)
103+ # Restore the SPI pins to their original mode and alt
104+ self .dc .init (mode = dcMode , alt = dcAlt )
105+ self .tx .init (mode = txMode , alt = txAlt )
106+ self .clk .init (mode = clkMode , alt = clkAlt )
95107
96108 def _pio_write (self , data ):
97109 """Write data to the display using PIO."""
@@ -105,7 +117,6 @@ def _pio_write(self, data):
105117 self .sm .active (0 )
106118
107119 @rp2 .asm_pio (
108- # fifo_join = rp2.PIO.JOIN_TX,
109120 out_init = rp2 .PIO .OUT_LOW ,
110121 sideset_init = rp2 .PIO .OUT_LOW ,
111122 out_shiftdir = rp2 .PIO .SHIFT_LEFT ,
@@ -114,10 +125,4 @@ def _pio_write(self, data):
114125 )
115126 def _pio_write_spi ():
116127 out (pins , 1 ).side (0 )
117- nop ()
118- nop ()
119- nop ()
120128 nop ().side (1 )
121- nop ()
122- nop ()
123- nop ()
0 commit comments