Skip to main content

SPI API

The ALP SDK SPI driver provides a CMSIS-Driver compatible interface for SPI master and slave communication across all E1M modules.

#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

FunctionDescription
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

EventDescription
ALP_SPI_EVENT_TRANSFER_COMPLETETransfer completed successfully
ALP_SPI_EVENT_DATA_LOSTReceive overrun or transmit underflow
ALP_SPI_EVENT_MODE_FAULTMulti-master mode fault detected

SPI Modes

ModeCPOLCPHADescription
ALP_SPI_CPOL0_CPHA000Clock idle low, sample on leading edge
ALP_SPI_CPOL0_CPHA101Clock idle low, sample on trailing edge
ALP_SPI_CPOL1_CPHA010Clock idle high, sample on leading edge
ALP_SPI_CPOL1_CPHA111Clock idle high, sample on trailing edge