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"""
10 def __init__(self, clock, MOSI=None, MISO=None, *, baudrate=1000000):
11 self._frequency = baudrate
12 for portId, portSck, portMosi, portMiso in spiPorts:
15 and MOSI in (portMosi, None) # Clock is required!
16 and MISO in (portMiso, None) # But can do with just output
18 mosiPin = Pin(portMosi.id) if MOSI else None
19 misoPin = Pin(portMiso.id) if MISO else None
30 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".format(
31 (clock, MOSI, MISO), spiPorts
35 # pylint: disable=too-many-arguments,unused-argument
47 """Initialize the Port"""
48 self._frequency = baudrate
57 # pylint: enable=too-many-arguments
61 """Return the current frequency"""
62 return self._frequency
64 def write(self, buf, start=0, end=None):
65 """Write data from the buffer to SPI"""
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)
72 # pylint: disable=too-many-arguments
74 self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
76 """Perform a half-duplex write from buffer_out and then
77 read data into buffer_in
79 self._spi.write_readinto(
88 # pylint: enable=too-many-arguments