Skip to main content

spi-loopback

SPI master TX + RX loopback. Demonstrates the open / transfer / close pattern from <alp/peripheral.h>. Wire MOSI to MISO on the carrier and run.

Source: examples/spi-loopback/.

board.yaml

schema_version: 2

som:
sku: E1M-AEN701

carrier:
name: E1M-EVK

cores:
m55_hp:
os: zephyr
app: ./src
peripherals: [spi]

diagnostics:
log_level: info

Source (abbreviated)

#include <alp/peripheral.h>
#include <alp/e1m_pinout.h>

int main(void) {
alp_spi_t *spi = alp_spi_open(&(alp_spi_config_t){
.bus_id = E1M_SPI0,
.freq_hz = 1000000u,
.mode = ALP_SPI_MODE_0,
.bits_per_word = 8,
.cs_pin_id = E1M_GPIO_IO5,
});
if (spi == NULL) return -1;

uint8_t tx[4] = { 0xAA, 0x55, 0xC3, 0x3C };
uint8_t rx[4] = { 0 };
alp_spi_transfer(spi, tx, rx, sizeof(tx));

for (size_t i = 0; i < sizeof(tx); i++) {
printk("[spi] tx=0x%02x rx=0x%02x %s\n",
tx[i], rx[i], tx[i] == rx[i] ? "OK" : "MISMATCH");
}

alp_spi_close(spi);
return 0;
}

Hardware

Short MOSI to MISO with a jumper. The chip-select line toggles around the transfer; the rest of the transaction reads back what was sent.

See also

Questions about this page? Discuss in Community Forum