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).
  • Loading branch information
michthom committed Sep 23, 2021
1 parent b869a4a commit 4b8955b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ repos:
name: pylint (examples code)
description: Run pylint rules on "examples/*.py" files
entry: /usr/bin/env bash -c
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name,consider-using-f-string $example; done)']
language: system
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

0 comments on commit 4b8955b

Please sign in to comment.