class SPI – a controller-driven serial protocol¶
SPI is a synchronous serial protocol driven by a controller. At the
physical level it uses three lines (SCK, MOSI, MISO) plus a
per-peripheral chip-select line.
Usage is similar to I2C; the main difference is the parameters
passed when initialising the bus:
from pyb import SPI
spi = SPI(2, SPI.CONTROLLER, baudrate=600000, polarity=1, phase=0, crc=0x7)
The only required parameter is mode (SPI.CONTROLLER or
SPI.PERIPHERAL). polarity is the idle level of SCK (0 or
1). phase selects whether data is sampled on the first (0) or
second (1) clock edge. crc is either None (no CRC) or a CRC
polynomial.
Transferring data:
data = spi.send_recv(b"1234") # send 4 bytes and receive 4 bytes
buf = bytearray(4)
spi.send_recv(b"1234", buf) # send 4 bytes and receive 4 into buf
spi.send_recv(buf, buf) # send/receive 4 bytes through buf
Constructors¶
- class pyb.SPI(bus: int | str, *args, **kwargs)¶
Construct an SPI object on the given
bus(an integer SPI peripheral index, e.g.2forSPI2). With no additional parameters the object is created but not initialised (it retains the previous bus settings, if any); if extra arguments are given the bus is initialised with them. Seeinit()for the available parameters.SPI(2)is wired to the same header pins on every STM32 OpenMV Cam; the OpenMV Cam N6 additionally exposesSPI(4):Bus
NSS
SCK
MISO
MOSI
SPI(2)(all STM32 OpenMV Cams)P3P2P1P0SPI(4)(OpenMV Cam N6 only)P15P16P17P18NSSis not driven by the SPI peripheral on either bus; it is free to use as a normal GPIO chip-select.Methods¶
- init(mode: int, baudrate: int = 328125, *, prescaler: int = -1, polarity: int = 1, phase: int = 0, bits: int = 8, firstbit: int = SPI.MSB, ti: bool = False, crc: int | None = None) None¶
Initialise the SPI bus with the given parameters:
modemust be eitherSPI.CONTROLLERorSPI.PERIPHERAL.baudrateis the SCK clock rate (only sensible for a controller).prescaleris the prescaler to use to derive SCK from the APB bus frequency; use ofprescaleroverridesbaudrate.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.bitscan be 8 or 16, and is the number of bits in each transferred word.firstbitcan beSPI.MSBorSPI.LSB.tiTrue indicates Texas Instruments, as opposed to Motorola, signal conventions.crccan be None for no CRC, or a polynomial specifier.
The SPI clock frequency may not match
baudrateexactly. The hardware only supports clocks that are the parent APB bus frequency divided by a power-of-two prescaler (2, 4, 8, 16, 32, 64, 128or256); the driver picks the highest one that does not exceed the requestedbaudrate.SPI(2)is on APB1. For precise control over the clock, setprescalerdirectly instead ofbaudrate.Printing the SPI object shows the computed baud rate and chosen prescaler.
- recv(recv: int | bytearray, *, timeout: int = 5000) bytes¶
Receive data on the bus:
recvcan be an integer, which is the number of bytes to receive, or a mutable buffer, which will be filled with received bytes.timeoutis the timeout in milliseconds to wait for the receive.
Return value: if
recvis an integer then a new buffer of the bytes received, otherwise the same buffer that was passed in torecv.
- send(send: int | bytes | bytearray, *, timeout: int = 5000) None¶
Send data on the bus:
sendis the data to send (an integer to send, or a buffer object).timeoutis the timeout in milliseconds to wait for the send.
- send_recv(send: int | bytes | bytearray, recv: bytearray | None = None, *, timeout: int = 5000) bytes¶
Send and receive data on the bus at the same time:
sendis the data to send (an integer to send, or a buffer object).recvis a mutable buffer which will be filled with received bytes. It can be the same assend, or omitted. If omitted, a new buffer will be created.timeoutis the timeout in milliseconds to wait for the receive.
Return value: the buffer with the received bytes.
Constants¶
- CONTROLLER: int¶
Initialise the SPI bus as master (controller) – the OpenMV Cam drives
SCKandMOSIand is in charge of the transaction.
- PERIPHERAL: int¶
Initialise the SPI bus as slave (peripheral) – the OpenMV Cam responds to clock pulses driven by a remote controller.