Skip to content

Enable CS "active-high" device support to resolve #71 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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