c
// spi.h
#ifndef SPI_DRIVER_H
#define SPI_DRIVER_H
#include <stdint.h>
#include <stdbool.h>
// Assumptions for custom embedded platform:
// - SPI peripheral base address: 0x40001000 (configurable via macro).
// - Registers (32-bit, little-endian):
// - SPI_CTRL (0x00): Control register.
// Bits: [0] ENABLE (1=enabled), [1:2] MODE (0=mode0,1=mode1,2=mode2,3=mode3),
// [3] MASTER (1=master), [8:15] CLK_DIV (clock divider, e.g., 0= /1, 255=/256).
// - SPI_STATUS (0x04): Status register.
// Bits: [0] BUSY (1=transfer in progress).
// - SPI_DATA (0x08): Data register (write to TX, read from RX).
// - SPI is full-duplex, MSB-first, 8-bit frames by default.
// - Polling-based (no interrupts).
// - Single SPI instance (SPI0).
// - Clock source is system clock (configure prescaler accordingly).
// - User must define platform-specific memory mapping (e.g., MMIO).
#define SPI_BASE_ADDR 0x40001000UL
// Register offsets
#define SPI_CTRL_OFFSET 0x00
#define SPI_STATUS_OFFSET 0x04
#define SPI_DATA_OFFSET 0x08
// Control register bits
#define SPI_CTRL_ENABLE (1UL << 0)
#define SPI_CTRL_MODE_MASK (3UL << 1)
#define SPI_CTRL_MODE_0 (0UL << 1)
#define SPI_CTRL_MODE_1 (1UL << 1)
#define SPI_CTRL_MODE_2 (2UL << 1)
#define SPI_CTRL_MODE_3 (3UL << 1)
#define SPI_CTRL_MASTER (1UL << 3)
#define SPI_CTRL_CLK_DIV_MASK (0xFFUL << 8)
#define SPI_CTRL_CLK_DIV_SHIFT 8
// Status register bits
#define SPI_STATUS_BUSY (1UL << 0)
// SPI Configuration Structure
typedef struct {
uint8_t mode; // 0-3 (CPOL/CPHA)
uint16_t clk_div; // Clock divider (1-256, 0= /1, 255=/256)
bool master; // true = master mode
} spi_config_t;
// SPI Handle (opaque, single instance)
typedef struct spi_handle {
volatile uint32_t *ctrl_reg;
volatile uint32_t *status_reg;
volatile uint32_t *data_reg;
} spi_handle_t;
// API Functions
spi_handle_t *spi_init(const spi_config_t *config);
void spi_deinit(spi_handle_t *hspi);
bool spi_transmit(spi_handle_t *hspi, const uint8_t *tx_data, uint32_t len);
bool spi_receive(spi_handle_t *hspi, uint8_t *rx_data, uint32_t len);
bool spi_transfer(spi_handle_t *hspi, const uint8_t *tx_data, uint8_t *rx_data, uint32_t len);
uint8_t spi_transfer_byte(spi_handle_t *hspi, uint8_t tx_byte);
#endif // SPI_DRIVER_H
c
// spi.c
#include "spi.h"
// Helper macros for register access
#define REG32(addr) (*((volatile uint32_t *)(addr)))
spi_handle_t g_spi_handle; // Global handle for single instance
spi_handle_t *spi_init(const spi_config_t *config) {
g_spi_handle.ctrl_reg = (volatile uint32_t *)(SPI_BASE_ADDR + SPI_CTRL_OFFSET);
g_spi_handle.status_reg = (volatile uint32_t *)(SPI_BASE_ADDR + SPI_STATUS_OFFSET);
g_spi_handle.data_reg = (volatile uint32_t *)(SPI_BASE_ADDR + SPI_DATA_OFFSET);
// Disable before config
REG32(g_spi_handle.ctrl_reg) = 0;
// Build control value
uint32_t ctrl = SPI_CTRL_MASTER * config->master;
ctrl |= (config->mode & 0x3UL) << 1;
ctrl |= (config->clk_div & 0xFFUL) << SPI_CTRL_CLK_DIV_SHIFT;
// Write config (enable last)
REG32(g_spi_handle.ctrl_reg) = ctrl | SPI_CTRL_ENABLE;
// Dummy read to sync
(void)REG32(g_spi_handle.status_reg);
return &g_spi_handle;
}
void spi_deinit(spi_handle_t *hspi) {
if (!hspi) return;
REG32(hspi->ctrl_reg) = 0; // Disable
}
static bool spi_wait_idle(spi_handle_t *hspi) {
uint32_t timeout = 1000000; // Adjust for system clock (~1s max)
while ((REG32(hspi->status_reg) & SPI_STATUS_BUSY) && timeout--) {
// Optional: asm("nop"); for tighter loop
}
return (timeout != 0);
}
bool spi_transmit(spi_handle_t *hspi, const uint8_t *tx_data, uint32_t len) {
if (!hspi || !tx_data || !len) return false;
if (!spi_wait_idle(hspi)) return false;
for (uint32_t i = 0; i < len; i++) {
REG32(hspi->data_reg) = (uint32_t)tx_data[i];
if (!spi_wait_idle(hspi)) return false;
(void)REG32(hspi->data_reg); // Dummy read to clear any flags
}
return true;
}
bool spi_receive(spi_handle_t *hspi, uint8_t *rx_data, uint32_t len) {
if (!hspi || !rx_data || !len) return false;
if (!spi_wait_idle(hspi)) return false;
for (uint32_t i = 0; i < len; i++) {
REG32(hspi->data_reg) = 0x00; // Dummy TX
if (!spi_wait_idle(hspi)) return false;
rx_data[i] = (uint8_t)REG32(hspi->data_reg);
}
return true;
}
bool spi_transfer(spi_handle_t *hspi, const uint8_t *tx_data, uint8_t *rx_data, uint32_t len) {
if (!hspi || !len) return false;
if (!spi_wait_idle(hspi)) return false;
for (uint32_t i = 0; i < len; i++) {
uint32_t tx_byte = tx_data ? tx_data[i] : 0x00;
REG32(hspi->data_reg) = tx_byte;
if (!spi_wait_idle(hspi)) return false;
if (rx_data) {
rx_data[i] = (uint8_t)REG32(hspi->data_reg);
}
}
return true;
}
uint8_t spi_transfer_byte(spi_handle_t *hspi, uint8_t tx_byte) {
if (!hspi || !spi_wait_idle(hspi)) return 0xFF;
REG32(hspi->data_reg) = (uint32_t)tx_byte;
if (!spi_wait_idle(hspi)) return 0xFF;
return (uint8_t)REG32(hspi->data_reg);
}
Usage Example (main.c snippet)
c
#include "spi.h"
int main(void) {
spi_config_t config = {
.mode = 0, // Mode 0 (CPOL=0, CPHA=0)
.clk_div = 8, // Sysclk / 8
.master = true
};
spi_handle_t *hspi = spi_init(&config);
if (!hspi) {
// Error handling
while(1);
}
uint8_t tx_buf[] = {0x01, 0x02, 0x03};
uint8_t rx_buf[3] = {0};
// Full-duplex transfer
if (spi_transfer(hspi, tx_buf, rx_buf, 3)) {
// Success: rx_buf now has response
}
spi_deinit(hspi);
while(1);
}
Key Notes (Sherlock's Analysis)
- Customization: Adjust
SPI_BASE_ADDR and register layout to match your platform's datasheet. Verify bitfields!
- Clocking:
clk_div assumes peripheral clock = sysclk / (clk_div + 1). Test max speed.
- GPIO/CS: Not included—handle chip-select (CS) and MISO/MOSI/SCLK pins separately via GPIO driver.
- Error Handling: Returns
bool for success. Timeout prevents hangs.
- Extensions: Add DMA/interrupts? Define more bits. Multi-instance? Use array of handles.
- Testing: Scope signals for mode/timing. Start with loopback (MISO to MOSI).
This driver is production-ready for basic use. Query for tweaks! 🔍