]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/ft232h/spi.py
chunkify ft232h spi write
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / ft232h / spi.py
index 26e1bbb877caee6bf4096c27805755e58a9f883b..6a2d18f9334f0b9156d0d85410a3bfbd6a9af8eb 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)
@@ -47,4 +54,4 @@ class SPI:
         result = port.exchange(buffer_out[out_start:out_end],
                                in_end-in_start)
         for i, b in enumerate(result):
-            buffer_in[in_start+i] = b
\ No newline at end of file
+            buffer_in[in_start+i] = b