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