X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka.git/blobdiff_plain/a9dce262feed0b44f2b441e6e0236dfbc4a189c2..9f302982b22b1ee160bad6d0408a01f4b220b934:/src/adafruit_blinka/microcontroller/raspi_23/spi.py diff --git a/src/adafruit_blinka/microcontroller/raspi_23/spi.py b/src/adafruit_blinka/microcontroller/raspi_23/spi.py old mode 100644 new mode 100755 index 7e3eec5..1b6bdc0 --- a/src/adafruit_blinka/microcontroller/raspi_23/spi.py +++ b/src/adafruit_blinka/microcontroller/raspi_23/spi.py @@ -64,6 +64,31 @@ class SPI: for i in range(len(buf)): # 'readinto' the given buffer buf[i] = data[i] self._spi.close() + except FileNotFoundError as not_found: + print("Could not open SPI device - check if SPI is enabled in kernel!") + raise + + def write_readinto(self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None): + if not buffer_out or not buffer_in: + return + if out_end is None or in_end is None: + out_end = len(buffer_out) + 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._spi.max_speed_hz = self.baudrate + self._spi.mode = self.mode + self._spi.bits_per_word = self.bits + data = self._spi.xfer(list(buffer_out[out_start:out_end])) + for i in range(len(buffer_in[in_start:in_end])): # 'readinto' the given buffer + buffer_in[i] = data[i] + self._spi.close() except FileNotFoundError as not_found: print("Could not open SPI device - check if SPI is enabled in kernel!") raise \ No newline at end of file