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