---
title: "SPI Loopback"
description: "SPI master TX + RX loopback. Demonstrates the open / transfer / close pattern from ``. Wire MOSI to MISO on the board and run — on E1M-AEN, read the caution under Hardware first."
source: https://docs.alplab.ai/sdk/examples/spi-loopback
---

# `spi-loopback`

SPI master TX + RX loopback. Demonstrates the open / transfer / close pattern from [`<alp/peripheral.h>`](https://docs.alplab.ai/sdk/api/peripheral). Wire MOSI to MISO on the board and run — on E1M-AEN, read the caution under [Hardware](#hardware) first.

Source: [`examples/peripheral-io/spi-loopback/`](https://github.com/alplabai/alp-sdk/tree/main/examples/peripheral-io/spi-loopback).

## `board.yaml`

```yaml
som:
  sku: E1M-AEN801

preset: e1m-evk

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

diagnostics:
  log_level: info
```

## Source (abbreviated)

```c
#include "alp/peripheral.h"
#include "alp/board.h"   /* BOARD_SPI_ARDUINO */

int main(void)
{
    (void)alp_init();

    printf("[spi] open BOARD_SPI_ARDUINO @ 1 MHz mode 0\n");

    alp_spi_t *bus = alp_spi_open(&(alp_spi_config_t){
        .bus_id        = BOARD_SPI_ARDUINO,
        .freq_hz       = 1000000,
        .mode          = ALP_SPI_MODE_0,
        .bits_per_word = 8,
        .cs_pin_id     = ALP_SPI_NO_CS,   /* controller manages CS */
    });
    if (bus == NULL) {
        printf("[spi] open failed: alp_last_error=%d\n", (int)alp_last_error());
        printf("[spi] done\n");
        return 0;
    }

    uint8_t      tx[4] = { 0xAA, 0x55, 0x12, 0x34 };
    uint8_t      rx[4] = { 0 };
    alp_status_t s     = alp_spi_transceive(bus, tx, rx, sizeof tx);
    printf("[spi] transceive -> status=%d  rx={%02x %02x %02x %02x}\n",
           (int)s, rx[0], rx[1], rx[2], rx[3]);

    alp_spi_close(bus);
    printf("[spi] done\n");
    return 0;
}
```

## Hardware

:::danger[E1M-AEN: the Arduino SPI is the Wi-Fi module's bus]
On E1M-AEN, E1M `SPI1` (the EVK's Arduino UNO header SPI, `BOARD_SPI_ARDUINO`) runs at 1.8 V to the on-module CC3501E Wi-Fi / BLE module and is **not connected to the Ensemble SoC** (dispatch: `cc3501e`). The pads are valid only while Wi-Fi is powered: **never drive them while Wi-Fi is off.** Read this before you jumper the pins or wire a target to them.
:::

Short MOSI to MISO with a jumper (on E1M-AEN, only while Wi-Fi is powered); with the loop closed, `rx` reads back `aa 55 12 34`.

## See also

- [`<alp/peripheral.h>` reference — SPI](https://docs.alplab.ai/sdk/api/peripheral#spi)
- [Examples overview](https://docs.alplab.ai/sdk/examples/overview)
