Class SPI – Serial Peripheral Interface Bus Protocol (Master)
What is SPI?
SPI is the abbreviation for Serial Peripheral Interface. It is a synchronous serial interface technology introduced by Motorola, a high-speed, full-duplex, synchronous communication bus. The SPI protocol is mainly used in short-distance communication systems, especially embedded systems. Many chip peripherals, such as LED display drivers, I/O interface chips, UART transceivers, etc., widely adopt the SPI bus protocol.
SPI is a synchronous serial protocol driven by a master device. At the physical level, the 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) used to select a specific device on the bus to communicate with.
Management of the SS signal should be done in user code (via the machine.Pin class).
Constructors
- class machine.SPI(id, ...)
Construct an SPI object on the given bus.
id- When set to 1, it is hardware SPI. When omitted, software SPI is constructed.
Hardware SPI construction:
>>> from machine import SPI
>>> hspi = SPI(1)
>>> hspi
SPI(id=1, baudrate=500000, polarity=0, phase=0, bits=8, firstbit=0, sck=-1, mosi=-1, miso=-1)
>>>
Software SPI construction:
from machine import SPI, Pin
spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(17), mosi=Pin(27), miso=Pin(18))
Methods
- SPI.init(baudrate=1000000, \*, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None, pins=(SCK, MOSI, MISO))
Initialize the SPI bus with the given parameters:
baudrateSCK clock frequency.polarity0 or 1, the idle clock line level.phase0 or 1 to sample data on the first or second clock edge respectively.bitsWidth of each transfer (in bits). All hardware is guaranteed to support only 8.firstbitCan beSPI.MSBorSPI.LSB.sck,mosi,misoare pins (machine.Pin) objects for the bus signals. For most hardware SPI blocks (selected by theidparameter when constructing the object), the pins are fixed and cannot be changed. In some cases, hardware modules allow 2-3 alternative pin groups for hardware SPI modules. Arbitrary pin assignment is only applicable to the bitbanging SPI driver (id= -1).pins- ESP32 does not havesck,mosi,misoparameters, but instead allows specifying them as a tuplepinsparameter.
- SPI.deinit()
Turn off the SPI bus.
- SPI.read(nbytes, write=0x00)
Read the specified number of bytes
nbyteswhile continuously writing the given single bytewrite. Returns abytesobject containing the read data.
- SPI.readinto(buf, write=0x00)
Read into the buffer specified by
bufwhile continuously writing the single byte given bywrite.Returns
None.Note: On
esp32, this function returns the number of bytes read.
- SPI.write(buf)
Write the bytes in
buf.Returns
None.Note: On
esp32, this function returns the number of bytes written.
- SPI.write_readinto(write_buf, read_buf)
Write bytes from
write_bufwhile reading intoread_buf. The buffers can be the same or different, but both buffers must have the same length.Returns
None.Note: On
esp32, this function returns the number of bytes written.
Constants
- SPI.MASTER
Used to initialize the SPI bus to master mode; this is only for
esp32.
- SPI.MSB
Set the first bit to be the most significant bit.
- SPI.LSB
Set the first bit to be the least significant bit.