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