SPI API
The ALP SDK SPI driver provides a CMSIS-Driver compatible interface for SPI master and slave communication across all E1M modules.
Header
#include "alp_spi.h"
Initialization
#include "alp_spi.h"
/* Get the SPI driver instance for bus 0 */
ALP_SPI_Driver_t *spi = ALP_SPI_GetDriver(0);
/* Initialize and power on */
spi->Initialize(spi_event_callback);
spi->PowerControl(ALP_POWER_FULL);
/* Configure as master, CPOL=0, CPHA=0, 8-bit, 1 MHz */
spi->Control(ALP_SPI_MODE_MASTER |
ALP_SPI_CPOL0_CPHA0 |
ALP_SPI_DATA_BITS(8),
1000000);
Sending Data
uint8_t tx_buf[] = {0x01, 0x02, 0x03, 0x04};
spi->Send(tx_buf, sizeof(tx_buf));
/* Wait for completion (polling) */
while (spi->GetStatus().busy);
Receiving Data
uint8_t rx_buf[4];
spi->Receive(rx_buf, sizeof(rx_buf));
while (spi->GetStatus().busy);
/* rx_buf now contains 4 bytes from the slave */
Full-Duplex Transfer
uint8_t tx_buf[] = {0xAA, 0xBB, 0xCC};
uint8_t rx_buf[3];
spi->Transfer(tx_buf, rx_buf, 3);
while (spi->GetStatus().busy);
Chip Select Control
The ALP SDK supports hardware-managed and software-managed chip select:
/* Hardware CS (active low, directly from SPI peripheral) */
spi->Control(ALP_SPI_SS_MASTER_HW_OUTPUT, 0);
/* Software CS using GPIO */
gpio->SetValue(CS_PIN, 0); /* Assert CS */
spi->Transfer(tx_buf, rx_buf, len);
while (spi->GetStatus().busy);
gpio->SetValue(CS_PIN, 1); /* De-assert CS */
Event Callback
void spi_event_callback(uint32_t event)
{
if (event & ALP_SPI_EVENT_TRANSFER_COMPLETE) {
/* Transfer finished */
}
if (event & ALP_SPI_EVENT_DATA_LOST) {
/* Overrun error */
}
}
API Reference
Functions
| Function | Description |
|---|---|
ALP_SPI_GetDriver() | Get driver instance for an SPI bus |
Initialize() | Initialize the SPI driver with event callback |
Uninitialize() | De-initialize and release resources |
PowerControl() | Set power state |
Control() | Configure mode, speed, and frame format |
Send() | Transmit data (output only) |
Receive() | Receive data (input only) |
Transfer() | Full-duplex send and receive |
GetDataCount() | Get number of bytes transferred |
GetStatus() | Get current driver status |
Abort() | Abort an in-progress transfer |
Events
| Event | Description |
|---|---|
ALP_SPI_EVENT_TRANSFER_COMPLETE | Transfer completed successfully |
ALP_SPI_EVENT_DATA_LOST | Receive overrun or transmit underflow |
ALP_SPI_EVENT_MODE_FAULT | Multi-master mode fault detected |
SPI Modes
| Mode | CPOL | CPHA | Description |
|---|---|---|---|
ALP_SPI_CPOL0_CPHA0 | 0 | 0 | Clock idle low, sample on leading edge |
ALP_SPI_CPOL0_CPHA1 | 0 | 1 | Clock idle low, sample on trailing edge |
ALP_SPI_CPOL1_CPHA0 | 1 | 0 | Clock idle high, sample on leading edge |
ALP_SPI_CPOL1_CPHA1 | 1 | 1 | Clock idle high, sample on trailing edge |