]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/ft232h/spi.py
Removed files added by updating submodules
[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
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             self._port.write(buf[-1 * rest :])
64
65     # pylint: disable=unused-argument
66     def readinto(self, buf, start=0, end=None, write_value=0):
67         """Read data from SPI and into the buffer"""
68         end = end if end else len(buf)
69         result = self._port.read(end - start)
70         for i, b in enumerate(result):
71             buf[start + i] = b
72
73     # pylint: enable=unused-argument
74
75     # pylint: disable=too-many-arguments
76     def write_readinto(
77         self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
78     ):
79         """Perform a half-duplex write from buffer_out and then
80         read data into buffer_in
81         """
82         out_end = out_end if out_end else len(buffer_out)
83         in_end = in_end if in_end else len(buffer_in)
84         result = self._port.exchange(
85             buffer_out[out_start:out_end], in_end - in_start, duplex=True
86         )
87         for i, b in enumerate(result):
88             buffer_in[in_start + i] = b
89
90     # pylint: enable=too-many-arguments