Skip to content

Commit 8144fe8

Browse files
committed
Fix ST7789 PIO driver pin modes
Needs to restore the mode and alt of the PIO pins every time _setup_sm_and_dma() is called
1 parent da81330 commit 8144fe8

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

cv2_drivers/displays/st7789_pio.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,10 @@ def __init__(
5050
self.dc = Pin(pin_dc) # Don't change mode/alt
5151
self.cs = Pin(pin_cs, Pin.OUT, value=1) if pin_cs else None
5252
self.freq = freq
53-
54-
# Get the current mode and alt of the pins so they can be restored
55-
txMode, txAlt = self.savePinModeAlt(self.tx)
56-
clkMode, clkAlt = self.savePinModeAlt(self.clk)
5753

5854
# Start the PIO state machine and DMA with 1 bytes per transfer
5955
self._setup_sm_and_dma(1)
6056

61-
# The tx and clk pins just got their mode and alt set for PIO0 or PIO1,
62-
# so we need to save them again to restore later when _write() is called
63-
self.txMode, self.txAlt = self.savePinModeAlt(self.tx)
64-
self.clkMode, self.clkAlt = self.savePinModeAlt(self.clk)
65-
66-
# Now restore the original mode and alt of the pins
67-
self.tx.init(mode=txMode, alt=txAlt)
68-
self.clk.init(mode=clkMode, alt=clkAlt)
69-
7057
# Call the parent class constructor
7158
super().__init__(width, height, rotation, color_order, reverse_bytes_in_word)
7259

@@ -78,6 +65,10 @@ def _setup_sm_and_dma(self, bytes_per_transfer):
7865
# Store the bytes per transfer for later use
7966
self.bytes_per_transfer = bytes_per_transfer
8067

68+
# Get the current mode and alt of the pins so they can be restored
69+
txMode, txAlt = self.savePinModeAlt(self.tx)
70+
clkMode, clkAlt = self.savePinModeAlt(self.clk)
71+
8172
# Initialize the PIO state machine
8273
self.sm = rp2.StateMachine(
8374
self.sm_id,
@@ -88,7 +79,18 @@ def _setup_sm_and_dma(self, bytes_per_transfer):
8879
pull_thresh = bytes_per_transfer * 8
8980
)
9081

91-
# Instantiate a DMA controller if not already done
82+
# The tx and clk pins just got their mode and alt set for PIO0 or PIO1.
83+
# We need to save them again to restore later when _write() is called,
84+
# if we haven't already
85+
if not hasattr(self, 'txMode'):
86+
self.txMode, self.txAlt = self.savePinModeAlt(self.tx)
87+
self.clkMode, self.clkAlt = self.savePinModeAlt(self.clk)
88+
89+
# Now restore the original mode and alt of the pins
90+
self.tx.init(mode=txMode, alt=txAlt)
91+
self.clk.init(mode=clkMode, alt=clkAlt)
92+
93+
# Instantiate a DMA controller if not already done
9294
if not hasattr(self, 'dma'):
9395
self.dma = rp2.DMA()
9496

0 commit comments

Comments
 (0)