1 """SPI Class for FTDI MPSSE"""
2 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.pin import Pin
3 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.url import (
8 # pylint: disable=protected-access
10 """Custom SPI Class for FTDI MPSSE"""
14 def __init__(self, spi_id=None):
15 # pylint: disable=import-outside-toplevel
16 from pyftdi.spi import SpiController
18 # pylint: enable=import-outside-toplevel
20 self._spi = SpiController(cs_count=1)
22 self._spi.configure(get_ft232h_url())
24 self._spi.configure(get_ft2232h_url(spi_id + 1))
25 self._port = self._spi.get_port(0)
26 self._port.set_frequency(100000)
29 # Change GPIO controller to SPI
30 Pin.mpsse_gpio = self._spi.get_gpio()
32 # pylint: disable=too-many-arguments,unused-argument
44 """Initialize the Port"""
45 self._port.set_frequency(baudrate)
46 # FTDI device can only support mode 0 and mode 2
47 # due to the limitation of MPSSE engine.
48 # This means CPHA must = 0
49 self._port._cpol = polarity
51 raise ValueError("Only SPI phase 0 is supported by FT232H.")
52 self._port._cpha = phase
54 # pylint: enable=too-many-arguments
58 """Return the current frequency"""
59 return self._port.frequency
61 def write(self, buf, start=0, end=None):
62 """Write data from the buffer to SPI"""
63 end = end if end else len(buf)
64 chunks, rest = divmod(end - start, self._spi.PAYLOAD_MAX_LENGTH)
65 for i in range(chunks):
66 chunk_start = start + i * self._spi.PAYLOAD_MAX_LENGTH
67 chunk_end = chunk_start + self._spi.PAYLOAD_MAX_LENGTH
68 self._port.write(buf[chunk_start:chunk_end])
70 rest_start = start + chunks * self._spi.PAYLOAD_MAX_LENGTH
71 self._port.write(buf[rest_start:end])
73 # pylint: disable=unused-argument
74 def readinto(self, buf, start=0, end=None, write_value=0):
75 """Read data from SPI and into the buffer"""
76 end = end if end else len(buf)
77 buffer_out = [write_value] * (end - start)
78 result = self._port.exchange(buffer_out, end - start, duplex=True)
79 for i, b in enumerate(result):
82 # pylint: enable=unused-argument
84 # pylint: disable=too-many-arguments
86 self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
88 """Perform a half-duplex write from buffer_out and then
89 read data into buffer_in
91 out_end = out_end if out_end else len(buffer_out)
92 in_end = in_end if in_end else len(buffer_in)
93 result = self._port.exchange(
94 buffer_out[out_start:out_end], in_end - in_start, duplex=True
96 for i, b in enumerate(result):
97 buffer_in[in_start + i] = b
99 # pylint: enable=too-many-arguments