SPI Module¶
| Since | Origin / Contributor | Maintainer | Source |
|---|---|---|---|
| 2017-05-04 | Arnim Läuger | Arnim Läuger | spi.c |
SPI Bus¶
The ESP32 contains 4 SPI bus hosts called SPI, SPI1, HSPI, and VSPI. SPI is locked to flash communication and is not available for the application. SPI1 is currently also tied to flash support, but might be available in the future. Applications can currently only use the HSPI and VSPI hosts.
The host signals can be mapped to any suitable GPIO pins.
Note
The API on ESP32 differs from the API on ESP8266. For backwards compatibility please refer to lua_compat/spi_compat.lua.
spi.master()¶
Initializes a bus in master mode and returns a bus master object.
Syntax¶
spi.master(host[, config[, dma]])
Parameters¶
hostid, one ofspi.SPI1. not supported yetspi.HSPIspi.VSPI
configtable listing the assigned GPIOs. All signal assignment are optional.sclkmosimisoquadwpquadhd
dmaset DMA channel (1 or 2) or disable DMA (0), defaults to 1 if omitted. Enabling DMA allows sending and receiving an unlimited amount of bytes but has restrictions in halfduplex mode (seespi.master:device()). Disabling DMA limits a transaction to 32 bytes max.
Not
Omitting the config table returns an SPI bus master object without further initialization. This enables sharing the same SPI host with sdmmc in SD-SPI mode. First call sdmmc.init(sdmmc.VSPI, ...), then spi.master(spi.VSPI).
Returns¶
SPI bus master object
Example¶
busmaster_config = {sclk = 19, mosi = 23, miso = 25}
busmaster = spi.master(spi.HSPI, busmaster_config)
Bus Master Object¶
spi.master:close()¶
Close the bus host. This fails if there are still devices registered on this bus.
Caution
The bus is also closed when the bus master object is automatically destroyed during garbage collection. Registered devices inherently prevent garbage collection of the bus master object.
Syntax¶
busmaster:close()
Parameters¶
none
Returns¶
nil
spi.master:device()¶
Adds a device on the given master bus. Up to three devices per bus are supported.
Note
Due to restrictions of the ESP IDF, halfduplex mode does not support DMA with both MOSI and MISO phases. Disable DMA during the call to spi.master() in this case and ensure that transaction data is not larger than 32 bytes.
Syntax¶
busmaster:device(config)
Parameters¶
config table describing the device parameters:
csGPIO connected to device's chip-select pin, optionalmodeSPI mode used for this device (0-3), mandatoryfreqclock frequency used for this device [Hz], mandatorycommand_bitsamount of bits in command phase (0-16), defaults to 0 if omittedaddress_bitsamount of bits in address phase (0-64), defaults to 0 if omitteddummy_bitsamount of dummy bits to insert address and data phase, defaults to 0 if omittedcs_ena_pretrans, optionalcs_ena_posttrans, optionalduty_cycle_pos, optionaltx_lsb_firsttransmit command/address/data LSB first iftrue, MSB first otherwise (or if omitted)rx_lsb_firstreceive data LSB first iftrue, MSB first otherwise (or if omitted)wire3use spiq for both transmit and receive iftrue, use mosi and miso otherwise (or if omitted)positive_cschip-select is active high during a transaction iftrue, cs is active low otherwise (or if omitted)halfduplextransmit data before receiving data iftrue, transmit and receive simultaneously otherwise (or if omitted)clk_as_csoutput clock on cs line when cs is active iftrue, defaults tofalseif omitted
Returns¶
SPI device object
Example¶
device_config = {mode = 0, freq = 1000000}
device_config.cs = 22
dev1 = busmaster:device(device_config)
device_config.cs = 4
dev2 = busmaster:device(device_config)
Device Object¶
spi.master:device:remove()¶
Removes a device from the related bus master.
Caution
The device is also removed when the device object is automatically destroyed during garbage collection.
Syntax¶
device:remove()
Parameters¶
none
Returns¶
nil
spi.master:device:transfer()¶
Initiate an SPI transaction consisting of
- assertion of cs signal
- optional sending of command bits
- optional sending of address bits
- optional sending of dummy bits
- sending of tx data
- concurrent or appended reception of rx data, optional
- de-assertion of cs signal
The function returns after the transaction is completed.
Syntax¶
device:transfer(trans)
device:transfer(txdata)
Parameters¶
trans table containing the elements of the transaction:
commanddata for command phase, amount of bits was defined during device creation, optionaladdressdata for address phase, amount of bits was defined during device creation, optionaltxdatastring of data to be sent to the device, optionalrxlennumber of bytes to be received, optionalmodeoptional, one ofsiotransmit in SIO mode, default if omitteddiotransmit in DIO modeqiotransmit in QIO mode
addr_modetransmit address also in selectedmodeiftrue, transmit address in SIO otherwise.
txdata string of data to be sent to the device
Returns¶
String of rxlen length, or #txdata length if rxlen is omitted.
spi.slave()¶
Initializes a bus in slave mode and returns a slave object. Not yet supported.