1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
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
10 # pylint: disable=protected-access, no-self-use
12 """Custom SPI Class for RP2040"""
16 def __init__(self, clock, MOSI=None, MISO=None, *, baudrate=1000000):
17 self._frequency = baudrate
18 for portId, portSck, portMosi, portMiso in spiPorts:
21 and MOSI in (portMosi, None) # Clock is required!
22 and MISO in (portMiso, None) # But can do with just output
24 mosiPin = Pin(portMosi.id) if MOSI else None
25 misoPin = Pin(portMiso.id) if MISO else None
36 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".format(
37 (clock, MOSI, MISO), spiPorts
41 # pylint: disable=too-many-arguments,unused-argument
53 """Initialize the Port"""
54 self._frequency = baudrate
63 # pylint: enable=too-many-arguments
67 """Return the current frequency"""
68 return self._frequency
70 def write(self, buf, start=0, end=None):
71 """Write data from the buffer to SPI"""
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)
78 # pylint: disable=too-many-arguments
80 self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
82 """Perform a half-duplex write from buffer_out and then
83 read data into buffer_in
85 self._spi.write_readinto(
94 # pylint: enable=too-many-arguments