ESP Hardware Abstraction Layer for SPI Peripheral
Note
This component is currently in beta. Its API, behavior, and compatibility may change at any time and without notice; backward compatibility is not guaranteed. Use caution when integrating into production systems.
Overview
The esp_hal_spi component provides a Hardware Abstraction Layer for the General Purpose SPI (GPSPI) peripherals across all ESP-IDF supported targets. It serves as a foundation for the higher-level SPI drivers, offering a consistent interface to interact with SPI hardware while hiding the complexities of chip-specific implementations.
Architecture
The HAL architecture consists of two primary layers:
-
HAL Layer (Upper): Defines the operational sequences and data structures required to interact with SPI peripherals, including:
- Initialization and de-initialization
- Clock configuration and timing calculations
- Device and transaction setup
- Master, slave, and slave HD (Half Duplex) mode operations
-
Low-Level Layer (Bottom): Acts as a translation layer between the HAL and the register definitions in the
soccomponent, handling:- Register access abstractions
- Chip-specific register configurations
- Hardware feature compatibility
Features
- Unified SPI interface across all ESP chip families
- Support for multiple operation modes:
- Master mode: Full-duplex and half-duplex communication
- Slave mode: Standard slave operation
- Slave HD mode: Half Duplex slave mode with segment-based transactions (on supported chips)
- Flexible SPI line configurations (1/2/4-line modes)
- Configurable clock sources and frequency settings
- Support for various transaction formats (command, address, dummy, data phases)
Usage
This component is primarily used by ESP-IDF peripheral drivers such as esp_driver_spi. It provides the low-level hardware abstraction needed for SPI communication with external devices.
For advanced developers implementing custom SPI solutions, the HAL functions can be used directly. However, please note that the interfaces provided by this component are internal to ESP-IDF and are subject to change.
Typical Usage Flow
Master Mode (without DMA):
- Initialize the SPI bus
- Setup clock speed configuration
- Call
setup_deviceto update parameters for a specific device - Call
setup_transto update parameters for a specific transaction - Prepare data to send into hardware registers
- Trigger the SPI transaction to start
- Wait until the transaction is complete
- Fetch the received data
Slave Mode (without DMA):
- Initialize the SPI bus with
spi_slave_hal_init - Configure device parameters (mode, bit order, etc.) in the HAL context
- Call
spi_slave_hal_setup_deviceto update parameters for the device - Prepare data to send and receiving buffer
- Call
spi_slave_hal_set_trans_bitlento set transaction bit length - Call
spi_slave_hal_user_startto trigger the SPI transaction to start - Wait until the transaction is done with
spi_slave_hal_usr_is_done - Call
spi_slave_hal_store_resultto store the received data - Call
spi_slave_hal_get_rcv_bitlento get the received data length
Dependencies
soc: Provides chip-specific register definitionshal: Core hardware abstraction utilities and macrosesp_hal_gpio: Required for ESP32 to access GPIO matrix delay information