@@ -16,6 +16,7 @@ class ST7789_PIO(ST7789):
16
16
pin_tx (Pin): Transmit pin number **Required**
17
17
pin_dc (Pin): Data/Command pin number **Required**
18
18
pin_cs (Pin): Chip Select pin number
19
+ freq (int): State machine frequency in Hz, default -1 (system clock)
19
20
rotation (int): Orientation of display
20
21
- 0-Portrait, default
21
22
- 1-Landscape
@@ -36,47 +37,56 @@ def __init__(
36
37
pin_tx ,
37
38
pin_dc ,
38
39
pin_cs = None ,
40
+ freq = - 1 ,
39
41
rotation = 0 ,
40
42
color_order = ST7789 .BGR ,
41
43
reverse_bytes_in_word = True ,
42
44
):
43
45
# Store PIO arguments
44
46
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
55
50
self .cs = Pin (pin_cs , Pin .OUT , value = 1 ) if pin_cs else None
51
+ self .freq = freq
56
52
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 )
61
56
57
+ # Initialize the PIO state machine
62
58
self .sm = rp2 .StateMachine (
63
59
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 ,
68
64
)
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 )
69
74
75
+ # Call the parent class constructor
70
76
super ().__init__ (width , height , rotation , color_order , reverse_bytes_in_word )
71
77
72
78
def _write (self , command = None , data = None ):
73
79
"""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
75
81
# 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 )
77
85
78
- # Temporarily set the DC pin to output mode
86
+ # Temporarily set the SPI pins to the correct mode and alt for PIO
79
87
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 )
80
90
81
91
# Write to the display
82
92
if self .cs :
@@ -90,8 +100,10 @@ def _write(self, command=None, data=None):
90
100
if self .cs :
91
101
self .cs .on ()
92
102
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 )
95
107
96
108
def _pio_write (self , data ):
97
109
"""Write data to the display using PIO."""
@@ -105,7 +117,6 @@ def _pio_write(self, data):
105
117
self .sm .active (0 )
106
118
107
119
@rp2 .asm_pio (
108
- # fifo_join = rp2.PIO.JOIN_TX,
109
120
out_init = rp2 .PIO .OUT_LOW ,
110
121
sideset_init = rp2 .PIO .OUT_LOW ,
111
122
out_shiftdir = rp2 .PIO .SHIFT_LEFT ,
@@ -114,10 +125,4 @@ def _pio_write(self, data):
114
125
)
115
126
def _pio_write_spi ():
116
127
out (pins , 1 ).side (0 )
117
- nop ()
118
- nop ()
119
- nop ()
120
128
nop ().side (1 )
121
- nop ()
122
- nop ()
123
- nop ()
0 commit comments