class SPI – a Serial Peripheral Interface bus protocol (master side)¶
SPI is a synchronous serial protocol that is driven by a master. At the physical level, a bus consists of 3 lines: SCK, MOSI, MISO. Multiple devices can share the same bus. Each device should have a separate, 4th signal, SS (Slave Select), to select a particular device on a bus with which communication takes place. Management of an SS signal should happen in user code (via machine.Pin class).
Construct an SPI object on the given bus,
id. Values of
iddepend on a particular port and its hardware. Values 0, 1, etc. are commonly used to select hardware SPI block #0, #1, etc. Value -1 can be used for bitbanging (software) implementation of SPI (if supported by a port).
With no additional parameters, the SPI object is created but not initialised (it has the settings from the last initialisation of the bus, if any). If extra arguments are given, the bus is initialised. See
initfor parameters of initialisation.
init(baudrate=1000000, *, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None, pins=(SCK, MOSI, MISO))¶
Initialise the SPI bus with the given parameters:
baudrateis the SCK clock rate.
polaritycan be 0 or 1, and is the level the idle clock line sits at.
phasecan be 0 or 1 to sample data on the first or second clock edge respectively.
bitsis the width in bits of each transfer. Only 8 is guaranteed to be supported by all hardware.
misoare pins (machine.Pin) objects to use for bus signals. For most hardware SPI blocks (as selected by
idparameter to the constructor), pins are fixed and cannot be changed. In some cases, hardware blocks allow 2-3 alternative pin sets for a hardware SPI block. Arbitrary pin assignments are possible only for a bitbanging SPI driver (
pins- WiPy port doesn’t
misoarguments, and instead allows to specify them as a tuple of
In the case of hardware SPI the actual clock frequency may be lower than the requested baudrate. This is dependant on the platform hardware. The actual rate may be determined by printing the SPI object.
Turn off the SPI bus.
Read a number of bytes specified by
nbyteswhile continuously writing the single byte given by
write. Returns a
bytesobject with the data that was read.
Read into the buffer specified by
bufwhile continuously writing the single byte given by
Write the bytes contained in
Write the bytes from
write_bufwhile reading into
read_buf. The buffers can be the same or different, but both buffers must have the same length. Returns