Boris Lovosevic edited this page Jun 5, 2018 · 8 revisions

Editing in progress...

machine Module

Class SPI


This class includes full support for using ESP32 SPI peripheral in master mode

Only SPI master mode is supported for now.

Python exception wil be raised if the requested spihost is used by SD Card driver (sdcard in spi mode).
If the requested spihost is VSPI and the psRAM is used at 80 MHz, the exception will be raised.
The exception will be raised if SPI cannot be configured for given configurations.



Create the SPI instance object

spi = machine.spi(spihost, baudrate, polarity, phase, firstbit, sck, mosi, miso, cs, duplex, bits)

Argument Description
spihost The hardware SPI host
machine-SPI.HSPI (1) or machine.SPI.VSPI (2) can be used
Default: 1
baudrate SPI clock speed in Hz; Default: 1000000
polarity SPI polarity; 0 or 1; Default: 0
phase SPI phase; 0 or 1; Default: 0
polarity & phase defines the SPI operating mode (0-3):
mode = (polarity<<1) + phase
firstbit Send MSB or LSB bit first; default: MSB
use constants machine.spi.MSB or machine.spi.LSB
sck SPI SCK pin; Can be entered as integer pin number or machine.Pin object
moosi SPI MISI pin; Can be entered as integer pin number or machine.Pin object
miso SPI MISO pin; Can be entered as integer pin number or machine.Pin object
cs Optional; SPI CS pin; Can be entered as integer pin number or machine.Pin object.
If set, CS activation and deactivation is handled by the driver, if not set, SPI select and deselect must be handled by user
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
Default: True
bits Not used, ignored if given

Only sck, mosi and miso are required, all the other arguments are optional and will be set to the default values if not given.
All arguments, except for spihost are KW arguments and must be entered as arg=value.


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)

Write 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.
Returns True on success, False ion error


spi.select()

Activates the CS pin if it was configured when the spi object was created.


spi.deselect()

Deactivates the CS pin if it was configured when the spi object was created.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.