Driver API for Synaptics SPI peripheral.
The SPI (Serial Peripheral Interface) protocol is a synchronous serial communication protocol that supports full-duplex or half-duplex data exchange between a controller and one or more peripherals.
Features:
- Configurable clock polarity and phase (CPOL, CPHA)
- Support for controller and peripheral modes
- Programmable data frame size
- Hardware TX and RX FIFOs
- Blocking and non-blocking transfer APIs
- Note
- SPI APIs are divided into two categories:
- High-level APIs: These provide blocking and non-blocking transfer support using data transfer structures. Ideal for application-layer SPI communication.
- Low-level APIs: These provide direct control of SPI hardware and registers, suitable for bootloaders, low-level initialization, and fine-tuned control.
- Warning
- Mixing high-level and low-level SPI APIs for the same instance is not recommended. Each has different expectations for FIFO management and interrupt handling. Use only one model per instance to avoid undefined behavior.
Configuration Considerations
To initialize and use the SPI driver:
spi_init
Initialize the SPI hardware and internal driver structures for the specified SPI instance.
spi_deinit
Deinitializes the SPI hardware and releases any resources held by the driver for the specified SPI instance.
spi_set_config
Sets the SPI hardware configuration for the specified SPI instance, including FRF, NDF, Clk polarity, phase and baud rate. Programs the SPI registers accordingly and stores the baud rate achieved.
.spi_controller = true,
.frame_size = SPI_FRAME_SIZE,
.toggle_peripheral_select = true,
.disable_peripheral_out = false,
.num_rx_frames = SPI_TRANSFER_LENGTH,
.spi_clk_freq_khz = SPI_CLOCK_FREQ,
.rx_sample_delay = 0,
};
uint32_t actual_clk = 0;
}
Controller PINMUX Configuration for SPI
pinmux_set_pin_func(
SPI_MSTR_MISO, SPI_MSTR_MISO__SPI_MSTR_MISO);
pinmux_set_pin_func(
SPI_MSTR_MOSI, SPI_MSTR_MOSI__SPI_MSTR_MOSI);
pinmux_set_pin_func(
SPI_MSTR_CLK, SPI_MSTR_CLK__SPI_MSTR_CLK);
pinmux_set_pin_func(
SPI_MSTR_CS, SPI_MSTR_CS__SPI_MSTR_CS);
Peripheral PINMUX Configuration for SPI
pinmux_set_pin_func(
SPI_SLV_MISO, SPI_SLV_MISO__SPI_SLV_MISO);
pinmux_set_pin_func(
SPI_SLV_MOSI, SPI_SLV_MOSI__SPI_SLV_MOSI);
pinmux_set_pin_func(
SPI_SLV_CLK, SPI_SLV_CLK__SPI_SLV_CLK);
pinmux_set_pin_func(
SPI_SLV_CS, SPI_SLV_CS__SPI_SLV_CS);
High-level Functions.
spi_transfer_non_blocking
Initiates a non-blocking transmit operation over SPI for the specified instance. Data is written to the SPI transmit FIFO and the function returns immediately. Completion is handled via callback.
.tx_buff = tx_buffer,
.rx_buff = rx_buffer,
.xfer_size = transfer_size,
.tx_dummy = 0xFF,
.callback = spi_callback_handler,
};
Low-level Functions.
spi_write_tx_data
Writes a single word to the SPI transmit FIFO. Should be used only when FIFO has available space. Non-blocking; does not wait for transmission to complete.