mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Simplify SPI bit-banging implementation
And add some comments to make stuff clearer
This commit is contained in:
@@ -64,6 +64,11 @@ class SPISoftwareBus(SharedMixin, Device):
|
|||||||
"""
|
"""
|
||||||
result = []
|
result = []
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
# See https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
|
||||||
|
# (specifically the section "Example of bit-banging the master
|
||||||
|
# protocol") for a simpler C implementation of this which ignores
|
||||||
|
# clock polarity, phase, variable word-size, and multiple input
|
||||||
|
# words
|
||||||
if lsb_first:
|
if lsb_first:
|
||||||
shift = operator.lshift
|
shift = operator.lshift
|
||||||
init_mask = 1
|
init_mask = 1
|
||||||
@@ -76,13 +81,15 @@ class SPISoftwareBus(SharedMixin, Device):
|
|||||||
for _ in range(bits_per_word):
|
for _ in range(bits_per_word):
|
||||||
if self.mosi is not None:
|
if self.mosi is not None:
|
||||||
self.mosi.value = bool(write_word & mask)
|
self.mosi.value = bool(write_word & mask)
|
||||||
|
# read bit on clock activation
|
||||||
self.clock.on()
|
self.clock.on()
|
||||||
if self.miso is not None and not clock_phase:
|
if not clock_phase:
|
||||||
if self.miso.value:
|
if self.miso is not None and self.miso.value:
|
||||||
read_word |= mask
|
read_word |= mask
|
||||||
|
# read bit on clock deactivation
|
||||||
self.clock.off()
|
self.clock.off()
|
||||||
if self.miso is not None and clock_phase:
|
if clock_phase:
|
||||||
if self.miso.value:
|
if self.miso is not None and self.miso.value:
|
||||||
read_word |= mask
|
read_word |= mask
|
||||||
mask = shift(mask, 1)
|
mask = shift(mask, 1)
|
||||||
result.append(read_word)
|
result.append(read_word)
|
||||||
|
|||||||
Reference in New Issue
Block a user