1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 
   3 # SPDX-License-Identifier: MIT
 
   4 """Generic Linux SPI class using PureIO's SPI class"""
 
   5 from Adafruit_PureIO import spi
 
   6 from adafruit_blinka.agnostic import detector
 
  21     def __init__(self, portid):
 
  22         if isinstance(portid, tuple):
 
  23             self._spi = spi.SPI(device=portid)
 
  25             self._spi = spi.SPI(device=(portid, 0))
 
  31     # pylint: disable=too-many-arguments,unused-argument
 
  49         self.baudrate = baudrate
 
  52         self.chip = detector.chip
 
  59     # pylint: enable=too-many-arguments,unused-argument
 
  61     # pylint: disable=unnecessary-pass
 
  63         """Setting so that SPI doesn't automatically set the CS pin"""
 
  64         # No kernel seems to support this, so we're just going to pass
 
  67     # pylint: enable=unnecessary-pass
 
  71         """Return the current baudrate"""
 
  74     def write(self, buf, start=0, end=None):
 
  75         """Write data from the buffer to SPI"""
 
  76         if buf is None or len(buf) < 1:
 
  81             # self._spi.open(self._port, 0)
 
  83             self._spi.max_speed_hz = self.baudrate
 
  84             self._spi.mode = self.mode
 
  85             self._spi.bits_per_word = self.bits
 
  86             self._spi.writebytes(buf[start:end])
 
  88         except FileNotFoundError:
 
  89             print("Could not open SPI device - check if SPI is enabled in kernel!")
 
  92     def readinto(self, buf, start=0, end=None, write_value=0):
 
  93         """Read data from SPI and into the buffer"""
 
  94         if buf is None or len(buf) < 1:
 
  99             # self._spi.open(self._port, 0)
 
 101             self._spi.max_speed_hz = self.baudrate
 
 102             self._spi.mode = self.mode
 
 103             self._spi.bits_per_word = self.bits
 
 104             data = self._spi.transfer([write_value] * (end - start))
 
 105             for i in range(end - start):  # 'readinto' the given buffer
 
 106                 buf[start + i] = data[i]
 
 108         except FileNotFoundError:
 
 109             print("Could not open SPI device - check if SPI is enabled in kernel!")
 
 112     # pylint: disable=too-many-arguments
 
 114         self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
 
 116         """Perform a half-duplex write from buffer_out and then
 
 117         read data into buffer_in
 
 119         if buffer_out is None or buffer_in is None:
 
 121         if len(buffer_out) < 1 or len(buffer_in) < 1:
 
 124             out_end = len(buffer_out)
 
 126             in_end = len(buffer_in)
 
 127         if out_end - out_start != in_end - in_start:
 
 128             raise RuntimeError("Buffer slices must be of equal length.")
 
 130             # self._spi.open(self._port, 0)
 
 132             self._spi.max_speed_hz = self.baudrate
 
 133             self._spi.mode = self.mode
 
 134             self._spi.bits_per_word = self.bits
 
 135             data = self._spi.transfer(list(buffer_out[out_start : out_end + 1]))
 
 136             for i in range((in_end - in_start)):
 
 137                 buffer_in[i + in_start] = data[i]
 
 139         except FileNotFoundError:
 
 140             print("Could not open SPI device - check if SPI is enabled in kernel!")
 
 143     # pylint: enable=too-many-arguments