Skip to content

Commit eb946e4

Browse files
samples: add SPI master/slave peripheral sample
Add SPI sample with SPIM and SPIS support. Button 2 triggers a master transfer, LED 2 toggles on slave reception. Includes: - SPIM and SPIS initialization and transfer handling - Board configuration with default pin mappings for all supported DKs - Kconfig option for configurable message string - Documentation with wiring guide, pin conflict notes, and testing steps Signed-off-by: Martynas Smilingis <martynas.smilingis@nordicsemi.no>
1 parent 677efd0 commit eb946e4

13 files changed

Lines changed: 532 additions & 2 deletions

File tree

boards/nordic/bm_nrf54l15dk/include/board-config.h

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ extern "C" {
6363
#define BOARD_CONSOLE_UARTE_PIN_CTS NRF_PIN_PORT_TO_PIN_NUMBER(7, 1)
6464
#endif
6565

66+
/* Application SPI master configuration */
67+
#ifndef BOARD_APP_SPIM_INST
68+
#define BOARD_APP_SPIM_INST NRF_SPIM21
69+
#endif
70+
71+
#ifndef BOARD_APP_SPIM_PIN_SCK
72+
#define BOARD_APP_SPIM_PIN_SCK NRF_PIN_PORT_TO_PIN_NUMBER(11, 1)
73+
#endif
74+
#ifndef BOARD_APP_SPIM_PIN_MOSI
75+
#define BOARD_APP_SPIM_PIN_MOSI NRF_PIN_PORT_TO_PIN_NUMBER(12, 1)
76+
#endif
77+
#ifndef BOARD_APP_SPIM_PIN_MISO
78+
#define BOARD_APP_SPIM_PIN_MISO NRF_PIN_PORT_TO_PIN_NUMBER(13, 1)
79+
#endif
80+
#ifndef BOARD_APP_SPIM_PIN_CSN
81+
#define BOARD_APP_SPIM_PIN_CSN NRF_PIN_PORT_TO_PIN_NUMBER(14, 1)
82+
#endif
83+
84+
/* Application SPI slave configuration */
85+
#ifndef BOARD_APP_SPIS_INST
86+
#define BOARD_APP_SPIS_INST NRF_SPIS30
87+
#endif
88+
89+
#ifndef BOARD_APP_SPIS_PIN_SCK
90+
#define BOARD_APP_SPIS_PIN_SCK NRF_PIN_PORT_TO_PIN_NUMBER(0, 0)
91+
#endif
92+
#ifndef BOARD_APP_SPIS_PIN_MOSI
93+
#define BOARD_APP_SPIS_PIN_MOSI NRF_PIN_PORT_TO_PIN_NUMBER(1, 0)
94+
#endif
95+
#ifndef BOARD_APP_SPIS_PIN_MISO
96+
#define BOARD_APP_SPIS_PIN_MISO NRF_PIN_PORT_TO_PIN_NUMBER(2, 0)
97+
#endif
98+
#ifndef BOARD_APP_SPIS_PIN_CSN
99+
#define BOARD_APP_SPIS_PIN_CSN NRF_PIN_PORT_TO_PIN_NUMBER(3, 0)
100+
#endif
101+
66102
/* Application UART configuration */
67103
#ifndef BOARD_APP_UARTE_INST
68104
#define BOARD_APP_UARTE_INST NRF_UARTE30
@@ -104,17 +140,20 @@ extern "C" {
104140
#define BOARD_APP_LPUARTE_INST NRF_UARTE21
105141
#endif
106142

143+
/* Shared with SPI master SCK. */
107144
#ifndef BOARD_APP_LPUARTE_PIN_TX
108145
#define BOARD_APP_LPUARTE_PIN_TX NRF_PIN_PORT_TO_PIN_NUMBER(11, 1)
109146
#endif
147+
/* Shared with SPI master MOSI. */
110148
#ifndef BOARD_APP_LPUARTE_PIN_RX
111149
#define BOARD_APP_LPUARTE_PIN_RX NRF_PIN_PORT_TO_PIN_NUMBER(12, 1)
112150
#endif
113151
#ifndef BOARD_APP_LPUARTE_PIN_REQ
114152
#define BOARD_APP_LPUARTE_PIN_REQ NRF_PIN_PORT_TO_PIN_NUMBER(4, 0) /* Shared with button 3. */
115153
#endif
154+
/* Shared with LED 3 and SPI master CSN. */
116155
#ifndef BOARD_APP_LPUARTE_PIN_RDY
117-
#define BOARD_APP_LPUARTE_PIN_RDY NRF_PIN_PORT_TO_PIN_NUMBER(14, 1) /* Shared with LED 3. */
156+
#define BOARD_APP_LPUARTE_PIN_RDY NRF_PIN_PORT_TO_PIN_NUMBER(14, 1)
118157
#endif
119158

120159
#ifdef __cplusplus

boards/nordic/bm_nrf54lm20dk/include/board-config.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ extern "C" {
6363
#define BOARD_CONSOLE_UARTE_PIN_CTS NRF_PIN_PORT_TO_PIN_NUMBER(19, 1)
6464
#endif
6565

66+
/* Application SPI master configuration */
67+
#ifndef BOARD_APP_SPIM_INST
68+
#define BOARD_APP_SPIM_INST NRF_SPIM21
69+
#endif
70+
71+
#ifndef BOARD_APP_SPIM_PIN_SCK
72+
#define BOARD_APP_SPIM_PIN_SCK NRF_PIN_PORT_TO_PIN_NUMBER(11, 1)
73+
#endif
74+
#ifndef BOARD_APP_SPIM_PIN_MOSI
75+
#define BOARD_APP_SPIM_PIN_MOSI NRF_PIN_PORT_TO_PIN_NUMBER(12, 1)
76+
#endif
77+
#ifndef BOARD_APP_SPIM_PIN_MISO
78+
#define BOARD_APP_SPIM_PIN_MISO NRF_PIN_PORT_TO_PIN_NUMBER(13, 1)
79+
#endif
80+
#ifndef BOARD_APP_SPIM_PIN_CSN
81+
#define BOARD_APP_SPIM_PIN_CSN NRF_PIN_PORT_TO_PIN_NUMBER(14, 1)
82+
#endif
83+
84+
/* Application SPI slave configuration */
85+
#ifndef BOARD_APP_SPIS_INST
86+
#define BOARD_APP_SPIS_INST NRF_SPIS30
87+
#endif
88+
89+
#ifndef BOARD_APP_SPIS_PIN_SCK
90+
#define BOARD_APP_SPIS_PIN_SCK NRF_PIN_PORT_TO_PIN_NUMBER(0, 0)
91+
#endif
92+
#ifndef BOARD_APP_SPIS_PIN_MOSI
93+
#define BOARD_APP_SPIS_PIN_MOSI NRF_PIN_PORT_TO_PIN_NUMBER(1, 0)
94+
#endif
95+
#ifndef BOARD_APP_SPIS_PIN_MISO
96+
#define BOARD_APP_SPIS_PIN_MISO NRF_PIN_PORT_TO_PIN_NUMBER(2, 0)
97+
#endif
98+
#ifndef BOARD_APP_SPIS_PIN_CSN
99+
#define BOARD_APP_SPIS_PIN_CSN NRF_PIN_PORT_TO_PIN_NUMBER(3, 0)
100+
#endif
101+
66102
/* Application UART configuration */
67103
#ifndef BOARD_APP_UARTE_INST
68104
#define BOARD_APP_UARTE_INST NRF_UARTE30

boards/nordic/bm_nrf54ls05dk/include/board-config.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ extern "C" {
6363
#define BOARD_CONSOLE_UARTE_PIN_CTS NRF_PIN_PORT_TO_PIN_NUMBER(7, 1)
6464
#endif
6565

66+
/* Application SPI master configuration */
67+
#ifndef BOARD_APP_SPIM_INST
68+
#define BOARD_APP_SPIM_INST NRF_SPIM21
69+
#endif
70+
71+
#ifndef BOARD_APP_SPIM_PIN_SCK
72+
#define BOARD_APP_SPIM_PIN_SCK NRF_PIN_PORT_TO_PIN_NUMBER(2, 1)
73+
#endif
74+
#ifndef BOARD_APP_SPIM_PIN_MOSI
75+
#define BOARD_APP_SPIM_PIN_MOSI NRF_PIN_PORT_TO_PIN_NUMBER(3, 1)
76+
#endif
77+
#ifndef BOARD_APP_SPIM_PIN_MISO
78+
#define BOARD_APP_SPIM_PIN_MISO NRF_PIN_PORT_TO_PIN_NUMBER(5, 1)
79+
#endif
80+
#ifndef BOARD_APP_SPIM_PIN_CSN
81+
#define BOARD_APP_SPIM_PIN_CSN NRF_PIN_PORT_TO_PIN_NUMBER(6, 1)
82+
#endif
83+
84+
/* Application SPI slave configuration */
85+
#ifndef BOARD_APP_SPIS_INST
86+
#define BOARD_APP_SPIS_INST NRF_SPIS22
87+
#endif
88+
89+
#ifndef BOARD_APP_SPIS_PIN_SCK
90+
#define BOARD_APP_SPIS_PIN_SCK NRF_PIN_PORT_TO_PIN_NUMBER(11, 1)
91+
#endif
92+
#ifndef BOARD_APP_SPIS_PIN_MOSI
93+
#define BOARD_APP_SPIS_PIN_MOSI NRF_PIN_PORT_TO_PIN_NUMBER(12, 1)
94+
#endif
95+
#ifndef BOARD_APP_SPIS_PIN_MISO
96+
#define BOARD_APP_SPIS_PIN_MISO NRF_PIN_PORT_TO_PIN_NUMBER(21, 1)
97+
#endif
98+
#ifndef BOARD_APP_SPIS_PIN_CSN
99+
#define BOARD_APP_SPIS_PIN_CSN NRF_PIN_PORT_TO_PIN_NUMBER(22, 1)
100+
#endif
101+
66102
/* Application UART configuration */
67103
#ifndef BOARD_APP_UARTE_INST
68104
#define BOARD_APP_UARTE_INST NRF_UARTE21

boards/nordic/bm_nrf54lv10dk/include/board-config.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ extern "C" {
6363
#define BOARD_CONSOLE_UARTE_PIN_CTS NRF_PIN_PORT_TO_PIN_NUMBER(7, 1)
6464
#endif
6565

66+
/* Application SPI master configuration */
67+
#ifndef BOARD_APP_SPIM_INST
68+
#define BOARD_APP_SPIM_INST NRF_SPIM21
69+
#endif
70+
71+
#ifndef BOARD_APP_SPIM_PIN_SCK
72+
#define BOARD_APP_SPIM_PIN_SCK NRF_PIN_PORT_TO_PIN_NUMBER(0, 1)
73+
#endif
74+
#ifndef BOARD_APP_SPIM_PIN_MOSI
75+
#define BOARD_APP_SPIM_PIN_MOSI NRF_PIN_PORT_TO_PIN_NUMBER(1, 1)
76+
#endif
77+
#ifndef BOARD_APP_SPIM_PIN_MISO
78+
#define BOARD_APP_SPIM_PIN_MISO NRF_PIN_PORT_TO_PIN_NUMBER(2, 1)
79+
#endif
80+
#ifndef BOARD_APP_SPIM_PIN_CSN
81+
#define BOARD_APP_SPIM_PIN_CSN NRF_PIN_PORT_TO_PIN_NUMBER(3, 1)
82+
#endif
83+
84+
/* Application SPI slave configuration */
85+
#ifndef BOARD_APP_SPIS_INST
86+
#define BOARD_APP_SPIS_INST NRF_SPIS30
87+
#endif
88+
89+
#ifndef BOARD_APP_SPIS_PIN_SCK
90+
#define BOARD_APP_SPIS_PIN_SCK NRF_PIN_PORT_TO_PIN_NUMBER(0, 0)
91+
#endif
92+
#ifndef BOARD_APP_SPIS_PIN_MOSI
93+
#define BOARD_APP_SPIS_PIN_MOSI NRF_PIN_PORT_TO_PIN_NUMBER(1, 0)
94+
#endif
95+
#ifndef BOARD_APP_SPIS_PIN_MISO
96+
#define BOARD_APP_SPIS_PIN_MISO NRF_PIN_PORT_TO_PIN_NUMBER(2, 0)
97+
#endif
98+
#ifndef BOARD_APP_SPIS_PIN_CSN
99+
#define BOARD_APP_SPIS_PIN_CSN NRF_PIN_PORT_TO_PIN_NUMBER(3, 0)
100+
#endif
101+
66102
/* Application UART configuration */
67103
#ifndef BOARD_APP_UARTE_INST
68104
#define BOARD_APP_UARTE_INST NRF_UARTE30
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. list-table:: SPIM and SPIS pin connections (board-config.h).
2+
:widths: auto
3+
:header-rows: 1
4+
5+
* - Development Kit
6+
- nRF54L15 DK
7+
- nRF54LM20 DK
8+
- nRF54LS05 DK
9+
- nRF54LV10 DK
10+
* - SPIM SCK
11+
- P1.11
12+
- P1.11
13+
- P1.02
14+
- P1.00
15+
* - SPIM MOSI
16+
- P1.12
17+
- P1.12
18+
- P1.03
19+
- P1.01
20+
* - SPIM MISO
21+
- P1.13
22+
- P1.13
23+
- P1.05
24+
- P1.02
25+
* - SPIM CSN
26+
- P1.14
27+
- P1.14
28+
- P1.06
29+
- P1.03
30+
* - SPIS SCK
31+
- P0.00
32+
- P0.00
33+
- P1.11
34+
- P0.00
35+
* - SPIS MOSI
36+
- P0.01
37+
- P0.01
38+
- P1.12
39+
- P0.01
40+
* - SPIS MISO
41+
- P0.02
42+
- P0.02
43+
- P1.21
44+
- P0.02
45+
* - SPIS CSN
46+
- P0.03
47+
- P0.03
48+
- P1.22
49+
- P0.03

doc/nrf-bm/links.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
.. _`How to flash an application`: https://docs.nordicsemi.com/bundle/nrf-connect-vscode/page/get_started/quick_debug.html#how-to-flash-an-application
9292
.. _`How to connect to the terminal`: https://docs.nordicsemi.com/bundle/nrf-connect-vscode/page/get_started/quick_debug.html#how-to-connect-to-the-terminal
9393
.. _`nRF Connect for Desktop`: https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Connect-for-desktop
94+
.. _`Board Configurator`: https://docs.nordicsemi.com/bundle/nrf-connect-board-configurator/page/index.html
9495
.. _`additional requirements`: https://docs.nordicsemi.com/bundle/nrf-connect-desktop/page/download_cfd.html
9596
.. _`nRF Connect Device Manager`: https://www.nordicsemi.com/Products/Development-tools/nRF-Connect-Device-Manager
9697
.. _`nRF Toolbox`: https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Toolbox

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ No changes since the latest nRF Connect SDK Bare Metal release.
108108
Peripheral samples
109109
------------------
110110

111-
No changes since the latest nRF Connect SDK Bare Metal release.
111+
* Added the :ref:`bm_spi_sample` sample.
112112

113113
DFU samples
114114
-----------
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2026 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
project(spi)
11+
12+
target_sources(app PRIVATE src/main.c)

samples/peripherals/spi/Kconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2026 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
menu "SPI sample"
8+
9+
config SAMPLE_SPI_MSG
10+
string "Message to send over SPI"
11+
default "Hello World!"
12+
help
13+
The message that the SPI master sends to the SPI slave when the button is pressed.
14+
15+
module=SAMPLE_SPI
16+
module-str=SPI Sample
17+
source "$(ZEPHYR_BASE)/subsys/logging/Kconfig.template.log_config"
18+
19+
endmenu # "SPI sample"
20+
21+
source "Kconfig.zephyr"

samples/peripherals/spi/README.rst

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
.. _bm_spi_sample:
2+
3+
SPI
4+
###
5+
6+
.. contents::
7+
:local:
8+
:depth: 2
9+
10+
The SPI sample demonstrates how to configure and use the SPIM and SPIS peripherals with the nrfx drivers.
11+
12+
Requirements
13+
************
14+
15+
The sample supports the following development kits:
16+
17+
.. tabs::
18+
19+
.. group-tab:: Simple board variants
20+
21+
The following board variants do **not** have DFU capabilities:
22+
23+
.. include:: /includes/supported_boards_all_non-mcuboot_variants_s115.txt
24+
25+
.. include:: /includes/supported_boards_all_non-mcuboot_variants_s145.txt
26+
27+
.. group-tab:: MCUboot board variants
28+
29+
The following board variants have DFU capabilities:
30+
31+
.. include:: /includes/supported_boards_all_mcuboot_variants_s115.txt
32+
33+
.. include:: /includes/supported_boards_all_mcuboot_variants_s145.txt
34+
35+
Overview
36+
********
37+
38+
The sample initializes the **SPIM** and **SPIS** instances with the pins configured in the board's :file:`board-config.h`.
39+
When **Button 2** is pressed, the master sends a configurable string (:kconfig:option:`CONFIG_SAMPLE_SPI_MSG`) to the slave.
40+
The slave receives the data, logs it, and toggles **LED 2**.
41+
42+
User interface
43+
**************
44+
45+
LED 0
46+
Lit when the sample is initialized and ready.
47+
48+
LED 2
49+
Toggles each time the SPI slave completes a reception.
50+
51+
Button 2
52+
Sends a message from the local SPIM to the connected SPIS.
53+
54+
.. _bm_spi_wiring:
55+
56+
Wiring
57+
======
58+
59+
The sample uses the **SPIM** and **SPIS** pins defined in the board's :file:`board-config.h` header (``BOARD_APP_SPIM_*`` and ``BOARD_APP_SPIS_*``).
60+
61+
.. include:: /includes/spi_board_connections.txt
62+
63+
Connect 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).
64+
65+
.. note:: Board-specific behavior
66+
67+
* **nRF54L15 DK** — SPI Master pins overlap with **LED 3** (P1.14, flickers during transfers) and **Button 0** (P1.13, do not press during transfers).
68+
* **nRF54LV10 DK** — The SPI Slave pins (P0.00-P0.03) are shared with one of the debugger's virtual serial ports.
69+
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.
70+
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.
71+
72+
.. tip:: Two-board setup
73+
74+
If single-board loopback is not supported on your board by default, you can also wire the SPIM signals of one board to the SPIS signals of a second board running the same sample.
75+
In that case, make sure to connect a common **GND** between the two boards and keep the wiring short to avoid signal integrity issues.
76+
77+
Building and running
78+
********************
79+
80+
This sample can be found under :file:`samples/peripherals/spi/` in the |BMshort| folder structure.
81+
82+
For details on how to create, configure, and program a sample, see :ref:`getting_started_with_the_samples`.
83+
84+
Testing
85+
=======
86+
87+
After building and flashing the sample, test it as follows:
88+
89+
1. Connect the four SPIM→SPIS signal pairs on the same board (see :ref:`Wiring <bm_spi_wiring>`).
90+
#. Open the console log and verify that ``SPI sample initialized`` appears in the log.
91+
#. Press **Button 2**.
92+
#. Observe that the log shows a master "sent" entry and a slave "received" entry, both containing the :kconfig:option:`CONFIG_SAMPLE_SPI_MSG` string.
93+
#. Observe that **LED 2** toggles on each completed reception.

0 commit comments

Comments
 (0)