Chapter 2. API Reference
SCLK Frequency Requirements The SPI slaves are designed to operate at up to 40 MHz. The data cannot be
recognized or received correctly if the clock is too fast or does not have a 50% duty cycle.
Restrictions and Known Issues
1. If DMA is enabled, the rx buffer should be word-aligned (starting from a 32-bit boundary and having a length
of multiples of 4 bytes). Otherwise, DMA may write incorrectly or not in a boundary aligned manner. The
driver reports an error if this condition is not satisfied.
Also, a Host should write lengths that are multiples of 4 bytes. The data with inappropriate lengths will be
discarded.
Application Example
The code example for Device/Host communication can be found in the peripherals/spi_slave directory of ESP-IDF
examples.
API Reference
Header File
• components/driver/include/driver/spi_slave.h
Functions
esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *bus_config,
const spi_slave_interface_config_t *slave_config,
spi_dma_chan_t dma_chan)
Initialize a SPI bus as a slave interface.
Warning SPI0/1 is not supported
Warning If a DMA channel is selected, any transmit and receive buffer used should be allocated in DMA-
capable memory.
Warning The ISR of SPI is always executed on the core which calls this function. Never starve the ISR on
this core or the SPI transactions will not be handled.
Return
• ESP_ERR_INVALID_ARG if configuration is invalid
• ESP_ERR_INVALID_STATE if host already is in use
• ESP_ERR_NOT_FOUND if there is no available DMA channel
• ESP_ERR_NO_MEM if out of memory
• ESP_OK on success
Parameters
• host: SPI peripheral to use as a SPI slave interface
• bus_config: Pointer to a spi_bus_config_t struct specifying how the host should be initialized
• slave_config: Pointer to a spi_slave_interface_config_t struct specifying the details for the slave
interface
• dma_chan: - Selecting a DMA channel for an SPI bus allows transactions on the bus with size only
limited by the amount of internal memory.
– Selecting SPI_DMA_DISABLED limits the size of transactions.
– Set to SPI_DMA_DISABLED if only the SPI flash uses this bus.
– Set to SPI_DMA_CH_AUTO to let the driver to allocate the DMA channel.
esp_err_t spi_slave_free(spi_host_device_t host)
Free a SPI bus claimed as a SPI slave interface.
Return
• ESP_ERR_INVALID_ARG if parameter is invalid
• ESP_ERR_INVALID_STATE if not all devices on the bus are freed
• ESP_OK on success
Parameters
Espressif Systems 385
Submit Document Feedback
Release v4.4