machine.SPI
SPI (Serial Peripheral Interface) is a synchronous serial protocol composed of a master and a slave.
The standard 4-wire mode consists of SCK (SCLK), CS (chip select), MOSI, MISO 4 wires connected to the master and slave
On K210, SPI has the following characteristics:
- There are 4 SPI devices. SPI0, SPI1, SPI3 can only work in master mode, and SPI2 can only work in slave mode. On MaixPy, SPI3 has been used to connect SPI Flash as a reserved hardware resource.
- Support 1/2/4/8 line full duplex mode. In MaixPy, currently only supports standard (Motorola) 4-wire full duplex mode (ie SCK, MOSI, MISO, CS four pins)
- The highest transmission rate 45M: 1/2 frequency, about 200Mbps
- Support DMA
- 4 hardware chip selects that can be configured with any pin
Constructor
spi = machine.SPI(id, mode=SPI.MODE_MASTER, baudrate=500000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck, mosi, miso, cs0, cs1, cs2, cs3)
Create a new SPI object with the specified parameters
Parameters
id: SPI ID, value range [0,4], currently only supports 0, 1, 4, and can only be in master mode, 2 can only be used as a slave, currently not implemented, 3 reserved, 4 uses soft Simulate SPI (.SPI_SOFT)mode: SPI mode,MODE_MASTERorMODE_MASTER_2orMODE_MASTER_4orMODE_MASTER_8orMODE_SLAVE, currently only supportsMODE_MASTERbaudrate: SPI baud rate (frequency)polarity: Polarity, the value is 0 or 1, which means the polarity of SPI when idle, 0 represents low level, 1 represents high levelphase: phase, the value bit is 0 or 1, indicating that the data is collected on the first or second edge of the clock, 0 means the first one, 1 means the secondbits: data width, the default value is 8, the value range is [4,32]firstbit: Specify whether the transmission is in MSB or LSB order, the default isSPI.MSBsck: SCK (clock) pin, the pin value can be passed directly, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.mosi: MOSI (host output) pin, the pin value can be directly passed, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.miso: MISO (host input) pin, the pin value can be directly passed, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.cs0: CS0 (chip select) pin, the pin value can be directly passed, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.cs1: CS1 (chip select) pin, the pin value can be directly passed, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.cs2: CS2 (chip select) pin, the pin value can be directly passed, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.cs3: CS3 (chip select) pin, the pin value can be directly passed, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.d0~d7: data pins, used in non-standard 4-wire mode, currently reserved. It is not necessary to set, but use fm to manage pin mapping in a unified manner.
Method
init
Similar constructor
spi.init(id, mode=SPI.MODE_MASTER, baudrate=500000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck, mosi, miso, cs0)
Parameters
Same as constructor
return value
no
read
Read data
spi.read(nbytes, write=0x00, cs=SPI.CS0)
Parameters
nbytes: the length to be readcs: select the chip select pin, the pins have been set forcs0~cs3during initialization, here only need to selectSPI.CS0~SPI.CS3, the default isSPI .CS0write: Because it is full duplex, set the value of theMOSIpin when reading, the default is0x00, that is, it is always low
return value
bytes type data
readinto
Read the data and put it in the specified variable
spi.readinto(buf, write=0x00, cs=SPI.CS0)
Parameters
buf:bytearraytype, the length is defined, the data is saved here after readingcs: select the chip select pin, the pins have been set forcs0~cs3during initialization, here only need to selectSPI.CS0~SPI.CS3, the default isSPI .CS0write: Because it is full duplex, set the value of theMOSIpin when reading, the default is0x00, that is, it is always low
return value
no
write
send data
spi.write(buf, cs=SPI.CS0)
Parameters
buf:bytearraytype, which defines the data and lengthcs: select the chip select pin, the pins have been set forcs0~cs3during initialization, here only need to selectSPI.CS0~SPI.CS3, the default isSPI .CS0
return value
no
write_readinto
Send data and read data to variables at the same time, that is, full duplex
spi.write(write_buf, read_buf, cs=SPI.CS0)
Parameters
write_buf:bytearraytype, which defines the data and length to be sentread_buf:bytearraytype, which defines the storage location of the received datacs: select the chip select pin, the pins have been set forcs0~cs3during initialization, here only need to selectSPI.CS0~SPI.CS3, the default isSPI .CS0
return value
no
deinit/__del__
Log off SPI, release hardware, turn off SPI clock
spi.deinit()
Parameters
no
return value
no
Examples
spi.deinit()
or
del spi
Constant
SPI0: SPI 0SPI1: SPI 1SPI2: SPI 2MODE_MASTER: as the master modeMODE_MASTER_2: as the master modeMODE_MASTER_4: as master modeMODE_MASTER_8: as the master modeMODE_SLAVE: as a slave modeMSB: MSB, that is, send the high or high byte firstLSB: LSB, that is, send the low or low byte firstCS0: Chip select 0CS1: Chip Select 1CS2: Chip Select 2CS3: Chip Select 3
Routine
Example 1: Basic read and write
from machine import SPI
spi = SPI(SPI.SPI1, mode=SPI.MODE_MASTER, baudrate=10000000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=28, mosi=29, miso=30, cs0= 27)
w = b'1234'
r = bytearray(4)
spi.write(w)
spi.write(w, cs=SPI.CS0)
spi.write_readinto(w, r)
spi.read(5, write=0x00)
spi.readinto(r, write=0x00)