Skip to content

Commit da0e6dd

Browse files
committed
Enable CS "active-high" device support
Reference adafruit#71 Enables SPIDevice to be used for things like the Sitronix ST7920 LCD display which requires CS to be pulled high during commands or data transfers. Adds a new attribute (cs_active_value) to set the preferred logic for the CS line. Default false (active low).
1 parent b869a4a commit da0e6dd

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

adafruit_bus_device/spi_device.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SPIDevice:
2121
:param ~busio.SPI spi: The SPI bus the device is on
2222
:param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the
2323
DigitalInOut API.
24+
:param bool cs_active_value: Set to true if your device requires CS to be active high. Defaults to false.
2425
:param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high.
2526
(Used for SD cards.)
2627
@@ -55,6 +56,7 @@ def __init__(
5556
spi,
5657
chip_select=None,
5758
*,
59+
cs_active_value=False
5860
baudrate=100000,
5961
polarity=0,
6062
phase=0,
@@ -66,6 +68,7 @@ def __init__(
6668
self.phase = phase
6769
self.extra_clocks = extra_clocks
6870
self.chip_select = chip_select
71+
self.cs_active_value = cs_active_value
6972
if self.chip_select:
7073
self.chip_select.switch_to_output(value=True)
7174

@@ -76,12 +79,12 @@ def __enter__(self):
7679
baudrate=self.baudrate, polarity=self.polarity, phase=self.phase
7780
)
7881
if self.chip_select:
79-
self.chip_select.value = False
82+
self.chip_select.value = self.cs_active_value
8083
return self.spi
8184

8285
def __exit__(self, exc_type, exc_val, exc_tb):
8386
if self.chip_select:
84-
self.chip_select.value = True
87+
self.chip_select.value = not self.cs_active_value
8588
if self.extra_clocks > 0:
8689
buf = bytearray(1)
8790
buf[0] = 0xFF

0 commit comments

Comments
 (0)