]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/ftdi_mpsse/mpsse/spi.py
Add support for FT2232H "microcontroller"
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / ftdi_mpsse / mpsse / spi.py
1 """SPI Class for FTDI MPSSE"""
2 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.pin import Pin
3
4 # pylint: disable=protected-access
5 class SPI:
6     """Custom SPI Class for FTDI MPSSE"""
7
8     MSB = 0
9
10     def __init__(self, id=None):
11         # pylint: disable=import-outside-toplevel
12         from pyftdi.spi import SpiController
13
14         # pylint: enable=import-outside-toplevel
15
16         self._spi = SpiController(cs_count=1)
17         if id is None:
18             self._spi.configure("ftdi://ftdi:ft232h/1", frequency=frequency)
19         else
20             self._spi.configure("ftdi://ftdi:ft2232h/{}".format(id+1), frequency=frequency)
21         self._port = self._spi.get_port(0)
22         self._port.set_frequency(100000)
23         self._port._cpol = 0
24         self._port._cpha = 0
25         # Change GPIO controller to SPI
26         Pin.mpsse_gpio = self._spi.get_gpio()
27
28     # pylint: disable=too-many-arguments,unused-argument
29     def init(
30         self,
31         baudrate=100000,
32         polarity=0,
33         phase=0,
34         bits=8,
35         firstbit=MSB,
36         sck=None,
37         mosi=None,
38         miso=None,
39     ):
40         """Initialize the Port"""
41         self._port.set_frequency(baudrate)
42         # FTDI device can only support mode 0 and mode 2
43         # due to the limitation of MPSSE engine.
44         # This means CPHA must = 0
45         self._port._cpol = polarity
46         if phase != 0:
47             raise ValueError("Only SPI phase 0 is supported by FT232H.")
48         self._port._cpha = phase
49
50     # pylint: enable=too-many-arguments
51
52     @property
53     def frequency(self):
54         """Return the current frequency"""
55         return self._port.frequency
56
57     def write(self, buf, start=0, end=None):
58         """Write data from the buffer to SPI"""
59         end = end if end else len(buf)
60         chunks, rest = divmod(end - start, self._spi.PAYLOAD_MAX_LENGTH)
61         for i in range(chunks):
62             chunk_start = start + i * self._spi.PAYLOAD_MAX_LENGTH
63             chunk_end = chunk_start + self._spi.PAYLOAD_MAX_LENGTH
64             self._port.write(buf[chunk_start:chunk_end])
65         if rest:
66             self._port.write(buf[-1 * rest :])
67
68     # pylint: disable=unused-argument
69     def readinto(self, buf, start=0, end=None, write_value=0):
70         """Read data from SPI and into the buffer"""
71         end = end if end else len(buf)
72         result = self._port.read(end - start)
73         for i, b in enumerate(result):
74             buf[start + i] = b
75
76     # pylint: enable=unused-argument
77
78     # pylint: disable=too-many-arguments
79     def write_readinto(
80         self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
81     ):
82         """Perform a half-duplex write from buffer_out and then
83         read data into buffer_in
84         """
85         out_end = out_end if out_end else len(buffer_out)
86         in_end = in_end if in_end else len(buffer_in)
87         result = self._port.exchange(
88             buffer_out[out_start:out_end], in_end - in_start, duplex=True
89         )
90         for i, b in enumerate(result):
91             buffer_in[in_start + i] = b
92
93     # pylint: enable=too-many-arguments