]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/rp2040/spi.py
07d4944d48d5d5b62df66570e8a57912cbfeb0e9
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / rp2040 / spi.py
1 """SPI Class for RP2040"""
2 from machine import SPI as _SPI
3 from machine import Pin
4 from microcontroller.pin import spiPorts
5
6 # pylint: disable=protected-access, no-self-use
7 class SPI:
8     """Custom SPI Class for RP2040"""
9     MSB = _SPI.MSB
10
11     def __init__(self, clock, MOSI=None, MISO=None, *, baudrate=1000000):
12         self._frequency = baudrate
13         for portId, portSck, portMosi, portMiso in spiPorts:
14             if (
15                 (clock == portSck)
16                 and MOSI in (portMosi, None)  # Clock is required!
17                 and MISO in (portMiso, None)  # But can do with just output
18             ):  # Or just input
19                 mosiPin = Pin(portMosi.id) if MOSI else None
20                 misoPin = Pin(portMiso.id) if MISO else None
21                 self._spi = _SPI(
22                     portId,
23                     sck=Pin(portSck.id),
24                     mosi=mosiPin,
25                     miso=misoPin,
26                     baudrate=baudrate,
27                 )
28                 break
29         else:
30             raise ValueError(
31                 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".format(
32                     (clock, MOSI, MISO), spiPorts
33                 )
34             )
35
36     # pylint: disable=too-many-arguments,unused-argument
37     def init(
38         self,
39         baudrate=1000000,
40         polarity=0,
41         phase=0,
42         bits=8,
43         firstbit=_SPI.MSB,
44         sck=None,
45         mosi=None,
46         miso=None,
47     ):
48         """Initialize the Port"""
49         self._frequency = baudrate
50         self._spi.init(
51             baudrate=baudrate,
52             polarity=polarity,
53             phase=phase,
54             bits=bits,
55             firstbit=firstbit,
56         )
57
58     # pylint: enable=too-many-arguments
59
60     @property
61     def frequency(self):
62         """Return the current frequency"""
63         return self._frequency
64
65     def write(self, buf, start=0, end=None):
66         """Write data from the buffer to SPI"""
67         self._spi.write(buf)
68
69     def readinto(self, buf, start=0, end=None, write_value=0):
70         """Read data from SPI and into the buffer"""
71         self._spi.readinto(buf)
72
73     # pylint: disable=too-many-arguments
74     def write_readinto(
75         self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
76     ):
77         """Perform a half-duplex write from buffer_out and then
78         read data into buffer_in
79         """
80         self._spi.write_readinto(
81             buffer_out,
82             buffer_in,
83             out_start=out_start,
84             out_end=out_end,
85             in_start=in_start,
86             in_end=in_end,
87         )
88
89     # pylint: enable=too-many-arguments