1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """am65xx SPI class using PureIO's SPI class"""
6 from Adafruit_PureIO import spi
8 # import Adafruit_PureIO.spi as spi
9 from adafruit_blinka.agnostic import detector
24 def __init__(self, portid):
25 if isinstance(portid, tuple):
26 self._spi = spi.SPI(device=portid)
28 self._spi = spi.SPI(device=(portid, 0))
34 # pylint: disable=too-many-arguments,unused-argument
52 self.baudrate = baudrate
55 self.chip = detector.chip
62 # pylint: enable=too-many-arguments,unused-argument
64 # pylint: disable=unnecessary-pass
66 """Setting so that SPI doesn't automatically set the CS pin"""
67 # No kernel seems to support this, so we're just going to pass
70 # pylint: enable=unnecessary-pass
74 """Return the current baudrate"""
77 def write(self, buf, start=0, end=None):
78 """Write data from the buffer to SPI"""
84 # self._spi.open(self._port, 0)
86 self._spi.max_speed_hz = self.baudrate
87 self._spi.mode = self.mode
88 self._spi.bits_per_word = self.bits
89 self._spi.writebytes(buf[start:end])
91 except FileNotFoundError:
92 print("Could not open SPI device - check if SPI is enabled in kernel!")
95 def readinto(self, buf, start=0, end=None, write_value=0):
96 """Read data from SPI and into the buffer"""
102 # self._spi.open(self._port, 0)
104 self._spi.max_speed_hz = self.baudrate
105 self._spi.mode = self.mode
106 self._spi.bits_per_word = self.bits
107 data = self._spi.transfer([write_value] * (end - start))
108 for i in range(end - start): # 'readinto' the given buffer
109 buf[start + i] = data[i]
111 except FileNotFoundError:
112 print("Could not open SPI device - check if SPI is enabled in kernel!")
115 # pylint: disable=too-many-arguments
117 self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
119 """Perform a half-duplex write from buffer_out and then
120 read data into buffer_in
122 if not buffer_out or not buffer_in:
125 out_end = len(buffer_out)
127 in_end = len(buffer_in)
128 if out_end - out_start != in_end - in_start:
129 raise RuntimeError("Buffer slices must be of equal length.")
131 # self._spi.open(self._port, 0)
133 self._spi.max_speed_hz = self.baudrate
134 self._spi.mode = self.mode
135 self._spi.bits_per_word = self.bits
136 data = self._spi.transfer(list(buffer_out[out_start : out_end + 1]))
137 for i in range((in_end - in_start)):
138 buffer_in[i + in_start] = data[i]
140 except FileNotFoundError:
141 print("Could not open SPI device - check if SPI is enabled in kernel!")
144 # pylint: enable=too-many-arguments