Skip to content
Boris Lovosevic edited this page Jul 27, 2019 · 3 revisions

machine Module

Class SPI


This class includes full support for using K210 SPI peripheral in master and slave mode
WS2812 (neopixel) support is also included in this module as it uses the SPI peripheral.

K210 SPI interface can operate in 4-line or 3-line mode.
4-line mode is standard SPI operating mode, mosi, miso, sck anf cs are used.
3-line mode does not use miso line. Data are sent and received over the same line (mosi). In this mode SPI can operate only as half-duplex.



Create the SPI instance object

spi = machine.spi(spihost, args)


Argument Description
id SPI ID; following id's can be used:
machine.SPI.SPI0 hardware SPI #0
machine.SPI.SPI1 hardware SPI #1
machine.SPI.SPI_SLAVE hardware SPI slave
machine.SPI.WS2812_0 hardware SPI #0 in WS2812 mode
machine.SPI.WS2812_1 hardware SPI #1 in WS2812 mode
Default: machine.SPI.SPI1
mosi SPI MOSI pin; Any valid and used K210 pin
mandatory in all operating modes
miso SPI MISO pin; Any valid and used K210 pin
not used in WS2812 operating mode
mandatory in MASTER operating mode if duplex=True
optional in MASTER operating mode if duplex=False. If not set SPI master operates in 3-line mode
optional in SLAVE operating mode. If not set SPI slave operates in 3-line mode
sck SPI SCK pin; Any valid and used K210 pin
not used in WS2812 operating mode
mandatory in MASTER and SLAVE operating modes
cs SPI CS; Any valid and used K210 pin
not used in WS2812 operating mode
mandatory in SLAVE operating mode
Optional in MASTER operating mode. If set, CS activation and deactivation is handled by the driver, if not set, SPI select and deselect must be handled by user
baudrate SPI clock speed in Hz; Range: 1000000 ~ 40000000
Default: 1000000
in WS2812 operating mode fixed at 20000000 Hz
not used in SLAVE operating mode
mode defines the SPI operating mode (0-3)
Default: 0
polarity SPI polarity; 0 or 1
only used if mode is not set
Default: 0
phase SPI phase; 0 or 1
polarity & phase defines the SPI operating mode (0-3):
mode = (polarity<<1) + phase
only used if mode is not set
Default: 0
firstbit Send MSB or LSB bit first; default: MSB
use constants machine.spi.MSB or machine.spi.LSB
Default: machine.spi.MSB
duplex Select wether SPI operates in fullduplex or halfduplex mode
In fullduplex mode SPI writes and reads data at the same time
In halfduplex mode SPI writes data first, then reads data. If miso is not set, data are read and written over the same pin (mosi)
Default: True
not used in SLAVE operating mode, SPI always operates in half-duplex mode
bits Number of bits; range: 4 - 32
Default: 8
slave_buffer Slave buffer
Optional, if not provided default slave buffer of 1024 bytes will be created
If integer is provided, slave buffer of requested size will be created; range: 512B ~ 1MB
If MicroPython buffer object is provided it will be used as slave buffer
Only used in SLAVE operating mode
slave_rolen size of the read only area at the end of the slave buffer
Only used in SLAVE operating mode
ws2812_num Optional; number of pixels used
Default: 1
only used in WS2812 operating mode
ws2812_lo Optional; LOW time in ns
Default: 400
only used in WS2812 operating mode
ws2812_hi Optional; HI time in ns
Default: 850
only used in WS2812 operating mode
ws2812_reset Optional; RESET time in ns
Default: 80000
only used in WS2812 operating mode
ws2812_white Optional; RGBW neopixel is used
Default: False
only used in WS2812 operating mode


All arguments, except for id are KW arguments and must be entered as arg=value.


spi.init(args)

Change the SPI operating parameters.
Only the following subset of arguments is accepted:
baudrate, firstbit, duplex
ws2812_num, ws2812_lo, ws2812_hi, ws2812_reset, ws2812_white


spi.deinit()

Deinitialize the SPI object, free all used resources.


spi.read(len [,val])

Read len bytes from SPI device.
Returns the string of read bytes.
If the optional val argument is given, outputs val byte on mosi during read (if duplex mode is used).


spi.readinto(buf [,val])

Read bytes from SPI device into buffer object buf. Length of buf bytes are read.
If the optional val argument is given, outputs val byte on mosi during read (if duplex mode is used).


spi.readfrom_mem(address, length [,addrlen])

Writes address to the spi device and reads length bytes.
The number of the address bytes to write is determined from the address value (1 byte for 0-255, 2 bytes for 256-65535, ...).
The number of address bytes to be written can also be set by the optional argument addrlen (1-4).
Returns the string of read bytes.


spi.write(buf)

Write bytes from buffer object buf to the SPI device.
Returns True on success, False ion error


spi.write_readinto(wr_buf, rd_buf [, delay])

Writes bytes from buffer object wr_buf to the SPI device and reads from SPI device into buffer object rd_buf.
The lenghts of wr_buf and rd_buf can be different.
In fullduplex mode write and read are simultaneous.
In halfduplex mode the data are first written to the device, then read from it.
If the optional argument delay is provided, a delay of delay us is inserted between write and read (in half-duplex mode).
Returns True on success, False ion error

Clone this wiki locally