]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/ft232h/spi.py
Update spi.py
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / ft232h / spi.py
index 26e1bbb877caee6bf4096c27805755e58a9f883b..9ae57512233c1f6dce07f512e6194a7e753dc8c1 100644 (file)
@@ -8,6 +8,7 @@ class SPI:
     bits = 8
 
     def __init__(self):
+        # change GPIO controller to SPI
         from pyftdi.spi import SpiController
         self._spi = SpiController(cs_count=1)
         self._spi.configure('ftdi:///1')
@@ -31,7 +32,13 @@ class SPI:
     def write(self, buf, start=0, end=None):
         end = end if end else len(buf)
         port = self._spi.get_port(self.cs, self.freq, self.mode)
-        port.write(buf[start:end])
+        chunks, rest = divmod(end - start, self._spi.PAYLOAD_MAX_LENGTH)
+        for i in range(chunks):
+            chunk_start = start + i * self._spi.PAYLOAD_MAX_LENGTH
+            chunk_end = chunk_start + self._spi.PAYLOAD_MAX_LENGTH
+            port.write(buf[chunk_start:chunk_end])
+        if rest:
+            port.write(buf[-1*rest:])
 
     def readinto(self, buf, start=0, end=None, write_value=0):
         end = end if end else len(buf)
@@ -45,6 +52,6 @@ class SPI:
         in_end = in_end if in_end else len(buffer_in)
         port = self._spi.get_port(self.cs, self.freq, self.mode)
         result = port.exchange(buffer_out[out_start:out_end],
-                               in_end-in_start)
+                               in_end-in_start, duplex=True)
         for i, b in enumerate(result):
-            buffer_in[in_start+i] = b
\ No newline at end of file
+            buffer_in[in_start+i] = b