Skip to content

Commit 601c51d

Browse files
committed
Add the ability to clock extra after a transaction. Used for SD cards.
1 parent 0021da2 commit 601c51d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Diff for: adafruit_bus_device/spi_device.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class SPIDevice:
2727
2828
:param ~nativeio.SPI spi: The SPI bus the device is on
2929
:param ~microcontroller.Pin chip_select: The chip select pin
30+
:param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high. (Used for SD cards.)
3031
3132
.. note:: This class is **NOT** built into CircuitPython. See
3233
:ref:`here for install instructions <bus_device_installation>`.
@@ -48,11 +49,12 @@ class SPIDevice:
4849
with device as spi:
4950
spi.write(bytes_read)
5051
"""
51-
def __init__(self, spi, chip_select, baudrate=100000, polarity=0, phase=0):
52+
def __init__(self, spi, chip_select, baudrate=100000, polarity=0, phase=0, extra_clocks=0):
5253
self.spi = spi
5354
self.baudrate = baudrate
5455
self.polarity = polarity
5556
self.phase = phase
57+
self.extra_clocks = extra_clocks
5658
self.chip_select = chip_select
5759
self.chip_select.switch_to_output(value=True)
5860

@@ -66,5 +68,13 @@ def __enter__(self):
6668

6769
def __exit__(self, *exc):
6870
self.chip_select.value = True
71+
if self.extra_clocks > 0:
72+
buf = bytearray(1)
73+
buf[0] = 0xff
74+
clocks = self.extra_clocks // 8
75+
if self.extra_clocks % 8 != 0:
76+
clocks += 1
77+
for i in range(clocks):
78+
self.spi.write(buf)
6979
self.spi.unlock()
7080
return False

0 commit comments

Comments
 (0)