Skip to content

Commit

Permalink
Enable CS "active-high" device support
Browse files Browse the repository at this point in the history
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).

Make examples use f-strings to resolve build failure in unrelated code
  • Loading branch information
michthom committed Sep 23, 2021
1 parent b869a4a commit 43728fa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions adafruit_bus_device/spi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down Expand Up @@ -55,6 +57,7 @@ def __init__(
spi,
chip_select=None,
*,
cs_active_value=False,
baudrate=100000,
polarity=0,
phase=0,
Expand All @@ -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)

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/busdevice_read_register_i2c_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
result = bytearray(1)
bus_device.readinto(result)

print("".join("{:02x}".format(x) for x in result))
x = "" # Suppress "undefined variable" error in pylint check
print("".join([f"{x:02x} for x in result"]))
3 changes: 2 additions & 1 deletion examples/busdevice_read_register_spi_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
result = bytearray(1)
bus_device.readinto(result)

print("".join("{:02x}".format(x) for x in result))
x = "" # Suppress "undefined variable" error in pylint check
print("".join([f"{x:02x} for x in result"]))

0 comments on commit 43728fa

Please sign in to comment.