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
9 # pylint: disable=protected-access, no-self-use
11 """Custom SPI Class for RP2040"""
15 def __init__(self, clock, MOSI=None, MISO=None, *, baudrate=1000000):
16 self._frequency = baudrate
17 for portId, portSck, portMosi, portMiso in spiPorts:
20 and MOSI in (portMosi, None) # Clock is required!
21 and MISO in (portMiso, None) # But can do with just output
23 mosiPin = Pin(portMosi.id) if MOSI else None
24 misoPin = Pin(portMiso.id) if MISO else None
35 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".format(
36 (clock, MOSI, MISO), spiPorts
40 # pylint: disable=too-many-arguments,unused-argument
52 """Initialize the Port"""
53 self._frequency = baudrate
62 # pylint: enable=too-many-arguments
66 """Return the current frequency"""
67 return self._frequency
69 def write(self, buf, start=0, end=None):
70 """Write data from the buffer to SPI"""
73 def readinto(self, buf, start=0, end=None, write_value=0):
74 """Read data from SPI and into the buffer"""
75 self._spi.readinto(buf)
77 # pylint: disable=too-many-arguments
79 self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
81 """Perform a half-duplex write from buffer_out and then
82 read data into buffer_in
84 self._spi.write_readinto(
93 # pylint: enable=too-many-arguments