From 3259adb186a88eeb3e10a6581d002901ab8cd271 Mon Sep 17 00:00:00 2001 From: Michael Thomson Date: Tue, 21 Sep 2021 20:13:00 +0100 Subject: [PATCH] Enable CS "active-high" device support Reference #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). --- adafruit_bus_device/spi_device.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/adafruit_bus_device/spi_device.py b/adafruit_bus_device/spi_device.py index 57d47f4..3e20115 100644 --- a/adafruit_bus_device/spi_device.py +++ b/adafruit_bus_device/spi_device.py @@ -21,6 +21,8 @@ class SPIDevice: :param ~busio.SPI spi: The SPI bus the device is on :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the DigitalInOut API. + :param bool cs_active_value: Set to true if your device requires CS to be active high. + Defaults to false. :param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high. (Used for SD cards.) @@ -55,6 +57,7 @@ def __init__( spi, chip_select=None, *, + cs_active_value=False, baudrate=100000, polarity=0, phase=0, @@ -66,6 +69,7 @@ def __init__( self.phase = phase self.extra_clocks = extra_clocks self.chip_select = chip_select + self.cs_active_value = cs_active_value if self.chip_select: self.chip_select.switch_to_output(value=True) @@ -76,12 +80,12 @@ def __enter__(self): baudrate=self.baudrate, polarity=self.polarity, phase=self.phase ) if self.chip_select: - self.chip_select.value = False + self.chip_select.value = self.cs_active_value return self.spi def __exit__(self, exc_type, exc_val, exc_tb): if self.chip_select: - self.chip_select.value = True + self.chip_select.value = not self.cs_active_value if self.extra_clocks > 0: buf = bytearray(1) buf[0] = 0xFF