X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/b4c14d5d9aa07d174825d00786efb7230477c8de..cbaee77564e37047937a9c642d13f20b53821845:/src/adafruit_blinka/microcontroller/generic_linux/spi.py diff --git a/src/adafruit_blinka/microcontroller/generic_linux/spi.py b/src/adafruit_blinka/microcontroller/generic_linux/spi.py index b905873..a93c46b 100755 --- a/src/adafruit_blinka/microcontroller/generic_linux/spi.py +++ b/src/adafruit_blinka/microcontroller/generic_linux/spi.py @@ -1,5 +1,6 @@ import spidev import time +from adafruit_blinka.agnostic import detector class SPI: MSB = 0 @@ -29,6 +30,22 @@ class SPI: self.baudrate = baudrate self.mode = mode self.bits = bits + self.chip = detector.chip + + def set_no_cs(self): + # Linux SPI driver for AM33XX chip in BeagleBone and PocketBeagle + # does not support setting SPI_NO_CS mode bit (issue #104) + if not self.chip.AM33XX and not self.chip.IMX8MX and not self.chip.SAMA5 \ + and not self.chip.APQ8016 and not self.chip.T210 and not self.chip.T186 \ + and not self.chip.T194 and not self.chip.SUN8I: + try: + self._spi.no_cs = True # this doesn't work but try anyways + except AttributeError: + pass + + @property + def frequency(self): + return self.baudrate def write(self, buf, start=0, end=None): if not buf: @@ -37,34 +54,28 @@ class SPI: end = len(buf) try: self._spi.open(self._port, 0) - try: - self._spi.no_cs = True # this doesn't work but try anyways - except AttributeError: - pass + self.set_no_cs() self._spi.max_speed_hz = self.baudrate self._spi.mode = self.mode self._spi.bits_per_word = self.bits - self._spi.writebytes([x for x in buf[start:end]]) + self._spi.writebytes2(buf[start:end]) self._spi.close() except FileNotFoundError as not_found: print("Could not open SPI device - check if SPI is enabled in kernel!") raise - def readinto(self, buf, start=0, end=None): + def readinto(self, buf, start=0, end=None, write_value=0): if not buf: return if end is None: end = len(buf) try: self._spi.open(self._port, 0) - try: - self._spi.no_cs = True # this doesn't work but try anyways - except AttributeError: - pass + self.set_no_cs() self._spi.max_speed_hz = self.baudrate self._spi.mode = self.mode self._spi.bits_per_word = self.bits - data = self._spi.readbytes(end-start) + data = self._spi.xfer([write_value]*(end-start)) for i in range(end-start): # 'readinto' the given buffer buf[start+i] = data[i] self._spi.close() @@ -77,17 +88,14 @@ class SPI: if not buffer_out or not buffer_in: return if out_end is None: - out_end = len(buffer_out) + out_end = len(buffer_out) if in_end is None: in_end = len(buffer_in) if out_end - out_start != in_end - in_start: raise RuntimeError('Buffer slices must be of equal length.') try: self._spi.open(self._port, 0) - try: - self._spi.no_cs = True # this doesn't work but try anyways - except AttributeError: - pass + self.set_no_cs() self._spi.max_speed_hz = self.baudrate self._spi.mode = self.mode self._spi.bits_per_word = self.bits