1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """SPI Class for FTDI MPSSE"""
5 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.pin import Pin
6 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.url import (
11 # pylint: disable=protected-access
13 """Custom SPI Class for FTDI MPSSE"""
17 def __init__(self, spi_id=None):
18 # pylint: disable=import-outside-toplevel
19 from pyftdi.spi import SpiController
21 # pylint: enable=import-outside-toplevel
23 self._spi = SpiController(cs_count=1)
25 self._spi.configure(get_ft232h_url())
27 self._spi.configure(get_ft2232h_url(spi_id + 1))
28 self._port = self._spi.get_port(0)
29 self._port.set_frequency(100000)
32 # Change GPIO controller to SPI
33 Pin.mpsse_gpio = self._spi.get_gpio()
35 # pylint: disable=too-many-arguments,unused-argument
47 """Initialize the Port"""
48 self._port.set_frequency(baudrate)
49 # FTDI device can only support mode 0 and mode 2
50 # due to the limitation of MPSSE engine.
51 # This means CPHA must = 0
52 self._port._cpol = polarity
54 raise ValueError("Only SPI phase 0 is supported by FT232H.")
55 self._port._cpha = phase
57 # pylint: enable=too-many-arguments
61 """Return the current frequency"""
62 return self._port.frequency
64 def write(self, buf, start=0, end=None):
65 """Write data from the buffer to SPI"""
66 end = end if end else len(buf)
67 chunks, rest = divmod(end - start, self._spi.PAYLOAD_MAX_LENGTH)
68 for i in range(chunks):
69 chunk_start = start + i * self._spi.PAYLOAD_MAX_LENGTH
70 chunk_end = chunk_start + self._spi.PAYLOAD_MAX_LENGTH
71 self._port.write(buf[chunk_start:chunk_end])
73 rest_start = start + chunks * self._spi.PAYLOAD_MAX_LENGTH
74 self._port.write(buf[rest_start:end])
76 # pylint: disable=unused-argument
77 def readinto(self, buf, start=0, end=None, write_value=0):
78 """Read data from SPI and into the buffer"""
79 end = end if end else len(buf)
80 buffer_out = [write_value] * (end - start)
81 result = self._port.exchange(buffer_out, end - start, duplex=True)
82 for i, b in enumerate(result):
85 # pylint: enable=unused-argument
87 # pylint: disable=too-many-arguments
89 self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
91 """Perform a half-duplex write from buffer_out and then
92 read data into buffer_in
94 out_end = out_end if out_end else len(buffer_out)
95 in_end = in_end if in_end else len(buffer_in)
96 result = self._port.exchange(
97 buffer_out[out_start:out_end], in_end - in_start, duplex=True
99 for i, b in enumerate(result):
100 buffer_in[in_start + i] = b
102 # pylint: enable=too-many-arguments