SPI

The SPI sample demonstrates how to configure and use the SPIM and SPIS peripherals with the nrfx drivers.

Requirements

The sample supports the following development kits:

The following board variants do not have DFU capabilities:

S115:

Hardware platform

PCA

Board target

nRF54L15 DK

PCA10156

bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice

nRF54L15 DK (emulating nRF54L10)

PCA10156

bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice

nRF54L15 DK (emulating nRF54L05)

PCA10156

bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice

nRF54LM20 DK

PCA10184

bm_nrf54lm20dk/nrf54lm20a/cpuapp/s115_softdevice

nRF54LS05 DK

PCA10214

bm_nrf54ls05dk/nrf54ls05b/cpuapp/s115_softdevice

nRF54LV10 DK

PCA10188

bm_nrf54lv10dk/nrf54lv10a/cpuapp/s115_softdevice

S145:

Hardware platform

PCA

Board target

nRF54L15 DK

PCA10156

bm_nrf54l15dk/nrf54l15/cpuapp/s145_softdevice

nRF54L15 DK (emulating nRF54L10)

PCA10156

bm_nrf54l15dk/nrf54l10/cpuapp/s145_softdevice

nRF54L15 DK (emulating nRF54L05)

PCA10156

bm_nrf54l15dk/nrf54l05/cpuapp/s145_softdevice

nRF54LM20 DK

PCA10184

bm_nrf54lm20dk/nrf54lm20a/cpuapp/s145_softdevice

nRF54LS05 DK

PCA10214

bm_nrf54ls05dk/nrf54ls05b/cpuapp/s145_softdevice

nRF54LV10 DK

PCA10188

bm_nrf54lv10dk/nrf54lv10a/cpuapp/s145_softdevice

Overview

The sample initializes the SPIM and SPIS instances with the pins configured in the board-config.h header. When Button 2 is pressed, a configurable string (CONFIG_SAMPLE_SPI_MSG) is sent from the local SPI controller to a connected SPI target. The local SPI target receives data from a connected SPI controller, logs the received data and toggles LED 2.

Wiring

The sample requires the following pin wiring:

Single-device loopback setup:

Wire the four SPIM pins to the SPIS pins on the same board, matching each signal by name (SCK→SCK, MOSI→MOSI, MISO→MISO, CSN→CSN).

Two-device setup:

Wire the four SPIM pins on device 1 to the SPIS pins on device 2, matching each signal by name (SCK→SCK, MOSI→MOSI, MISO→MISO, CSN→CSN). Optionally, wire the four SPIS pins on device 1 to the SPIM pins on device 2 to test SPIM and SPIS on both devices.

Note

The sample configures the SPIM peripheral with an extra delay between chip select (CSN) and the first SCK edge (see the CONFIG_SAMPLE_SPI_CSN_TO_CLK_DELAY Kconfig option). The delay is required when talking to an SPI target which needs more time to wake up, like the SPIS peripheral on nRF54L devices, where the high-frequency clock is powered down during sleep and must be running before data can be transferred correctly. For more information, see the SPIS peripheral and electrical specification chapters in the product specification of your SoC.

SPIM and SPIS pin connections (board-config.h).

Development Kit

nRF54L15 DK

nRF54LM20 DK

nRF54LS05 DK

nRF54LV10 DK

SPIM SCK

P1.11

P1.11

P1.02

P1.00

SPIM MOSI

P1.12

P1.12

P1.03

P1.01

SPIM MISO

P1.13

P1.13

P1.05

P1.02

SPIM CSN

P1.14

P1.14

P1.06

P1.03

SPIS SCK

P0.00

P0.00

P1.11

P0.00

SPIS MOSI

P0.01

P0.01

P1.12

P0.01

SPIS MISO

P0.02

P0.02

P1.21

P0.02

SPIS CSN

P0.03

P0.03

P1.22

P0.03

Note

Board-specific behavior

  • nRF54L15 DK — The SPI controller pins overlap with LED 3 (P1.14, flickers during transfers) and Button 0 (P1.13, do not press during transfers).

  • nRF54LV10 DK — The SPI target pins (P0.00-P0.03) are shared with one of the debugger’s virtual serial ports. Before running the sample, open the Board Configurator app in nRF Connect for Desktop and disable the Connect port VCOM entry that is mapped to pins P0.00-P0.03, to release these pins from the debugger. Leave the other Connect port VCOM entry (mapped to pins P1.04-P1.07) enabled, since it is the virtual serial port used to read the sample’s log output.

User interface

LED 0:

Lit when the device is initialized.

LED 2:

Toggles when the SPIS peripheral completes a reception.

Button 2:

Send string from the local SPIM to a connected SPI target.

Configuration

You can modify the following options (available in the Kconfig file at samples/peripherals/spi):

  • CONFIG_SAMPLE_SPI_MSG - Set the message that the SPI controller sends to the SPI target when Button 2 is pressed.

  • CONFIG_SAMPLE_SPI_RX_BUF_SIZE - Set the size, in bytes, of the buffer used by the SPI target to receive data.

  • CONFIG_SAMPLE_SPI_CSN_TO_CLK_DELAY - Set the delay, in SPIM core clock periods, between chip select (CSN) going active and the first SCK edge. The required delay depends on how long the connected SPI target takes to wake up and become ready to receive data, which varies between devices. Refer to the electrical specification chapter of the SPI target device’s datasheet for its wake-up time.

Building and running

This sample can be found under samples/peripherals/spi/ in the Bare Metal folder structure.

For details on how to create, configure, and program a sample, see Getting Started with the samples.

Testing

You can test the sample in two ways, depending on the selected wiring: the single-device loopback setup or the two-device setup.

Test the sample on one development kit, with its SPI controller wired to its own SPI target.

  1. Wire the SPIM pins to the SPIS pins on the same kit, as described in Wiring.

  2. Compile and program the application.

  3. Connect to the kit with a terminal emulator (for example, the Serial Terminal app).

  4. Observe that the SPI sample initialized message is printed.

  5. Press Button 2.

  6. Observe that the terminal prints Message sent followed by the string set by the CONFIG_SAMPLE_SPI_MSG Kconfig option (Hello World! by default).

  7. Observe that the terminal prints Message received, the number of received bytes, and the same string.

  8. Observe that LED 2 toggles.