1 """SPI Class for RP2040"""
2 from machine import SPI as _SPI
3 from machine import Pin
4 from microcontroller.pin import spiPorts
6 # pylint: disable=protected-access, no-self-use
8 """Custom SPI Class for RP2040"""
12 def __init__(self, clock, MOSI=None, MISO=None, *, baudrate=1000000):
13 self._frequency = baudrate
14 for portId, portSck, portMosi, portMiso in spiPorts:
17 and MOSI in (portMosi, None) # Clock is required!
18 and MISO in (portMiso, None) # But can do with just output
20 mosiPin = Pin(portMosi.id) if MOSI else None
21 misoPin = Pin(portMiso.id) if MISO else None
32 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".format(
33 (clock, MOSI, MISO), spiPorts
37 # pylint: disable=too-many-arguments,unused-argument
49 """Initialize the Port"""
50 self._frequency = baudrate
59 # pylint: enable=too-many-arguments
63 """Return the current frequency"""
64 return self._frequency
66 def write(self, buf, start=0, end=None):
67 """Write data from the buffer to SPI"""
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)
74 # pylint: disable=too-many-arguments
76 self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
78 """Perform a half-duplex write from buffer_out and then
79 read data into buffer_in
81 self._spi.write_readinto(
90 # pylint: enable=too-many-arguments