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