Skip to content

Commit c04278f

Browse files
nordic-seglrlubos
authored andcommitted
tests: benchmarks: multicore: idle_spim_loopback: Add test for SPIM120
Add test that combinest functional check (whether SPIM transmits data) with non-functional check (current consumption). Confirm that slow SPIM130 / fast SPIM120: - doesn't block entering low power states like IDLE or S2RAM; - work correclty after leaving low power state. Signed-off-by: Sebastian Głąb <[email protected]> (cherry picked from commit 99b0684)
1 parent f1e16de commit c04278f

File tree

11 files changed

+496
-0
lines changed

11 files changed

+496
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# Copyright (c) 2024 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+
11+
if(NOT SYSBUILD)
12+
message(FATAL_ERROR
13+
" This is a multi-image application that should be built using sysbuild.\n"
14+
" Add --sysbuild argument to west build command to prepare all the images.")
15+
endif()
16+
17+
project(idle_spim_loopback)
18+
19+
target_sources(app PRIVATE src/main.c)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
config TEST_SLEEP_DURATION_MS
8+
int "Amount of time (in miliseconds) the core is sleeping"
9+
default 1000
10+
help
11+
Set sleep duration to TEST_SLEEP_DURATION_MS miliseconds.
12+
Based on the value of 'min-residency-us' specified for each power state defined in the DTS,
13+
core enters the lowest possible power state.
14+
15+
config DATA_FIELD
16+
int "Number of bytes exchanged in single SPI transceive operation"
17+
default 4
18+
help
19+
Set number of bytes exchanged in single spi_transceive() operation.
20+
Values from 1 to 16 bytes are confirmed to work correctly.
21+
22+
source "Kconfig.zephyr"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
source "${ZEPHYR_BASE}/share/sysbuild/Kconfig"
8+
9+
config REMOTE_BOARD
10+
string "The board used for remote target"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
&pinctrl {
8+
spi130_default_alt: spi130_default_alt {
9+
group1 {
10+
psels = <NRF_PSEL(SPIM_SCK, 0, 0)>,
11+
<NRF_PSEL(SPIM_MOSI, 0, 7)>,
12+
<NRF_PSEL(SPIM_MISO, 0, 6)>;
13+
};
14+
};
15+
16+
spi130_sleep_alt: spi130_sleep_alt {
17+
group1 {
18+
psels = <NRF_PSEL(SPIM_SCK, 0, 0)>,
19+
<NRF_PSEL(SPIM_MISO, 0, 6)>,
20+
<NRF_PSEL(SPIM_MOSI, 0, 7)>;
21+
low-power-enable;
22+
};
23+
};
24+
};
25+
26+
&spi130 {
27+
compatible = "nordic,nrf-spim";
28+
status = "okay";
29+
pinctrl-0 = <&spi130_default_alt>;
30+
pinctrl-1 = <&spi130_sleep_alt>;
31+
pinctrl-names = "default", "sleep";
32+
overrun-character = <0x00>;
33+
memory-regions = <&cpuapp_dma_region>;
34+
zephyr,pm-device-runtime-auto;
35+
dut_spi_dt: test-spi-dev@0 {
36+
compatible = "vnd,spi-device";
37+
reg = <0>;
38+
spi-max-frequency = <DT_FREQ_M(4)>;
39+
};
40+
};
41+
42+
&exmif {
43+
status = "disabled";
44+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
&pinctrl {
8+
spi120_default: spi120_default {
9+
group1 {
10+
psels = <NRF_PSEL(SPIM_SCK, 7, 3)>,
11+
<NRF_PSEL(SPIM_MISO, 7, 6)>,
12+
<NRF_PSEL(SPIM_MOSI, 7, 7)>;
13+
};
14+
};
15+
16+
spi120_sleep: spi120_sleep {
17+
group1 {
18+
psels = <NRF_PSEL(SPIM_SCK, 7, 3)>,
19+
<NRF_PSEL(SPIM_MISO, 7, 6)>,
20+
<NRF_PSEL(SPIM_MOSI, 7, 7)>;
21+
low-power-enable;
22+
};
23+
};
24+
};
25+
26+
&spi130 {
27+
status = "disabled";
28+
/delete-property/ dut_spi_dt;
29+
};
30+
31+
&spi120 {
32+
compatible = "nordic,nrf-spim";
33+
status = "okay";
34+
pinctrl-0 = <&spi120_default>;
35+
pinctrl-1 = <&spi120_sleep>;
36+
pinctrl-names = "default", "sleep";
37+
overrun-character = <0x00>;
38+
memory-regions = <&dma_fast_region>;
39+
zephyr,pm-device-runtime-auto;
40+
dut_spi_dt: test-spi-dev@0 {
41+
compatible = "vnd,spi-device";
42+
reg = <0>;
43+
spi-max-frequency = <DT_FREQ_M(4)>;
44+
};
45+
};
46+
47+
&exmif {
48+
status = "disabled";
49+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_PRINTK=y
2+
CONFIG_LOG=y
3+
CONFIG_SPI=y
4+
5+
CONFIG_NRF_REGTOOL_VERBOSITY=1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CONFIG_PM=y
2+
CONFIG_PM_S2RAM=y
3+
CONFIG_PM_S2RAM_CUSTOM_MARKING=y
4+
CONFIG_POWEROFF=y
5+
CONFIG_NRFS_MRAM_SERVICE_ENABLED=n
6+
7+
CONFIG_PM_DEVICE=y
8+
CONFIG_PM_DEVICE_RUNTIME=y
9+
10+
# Enable for debugging purposes only
11+
CONFIG_BOOT_BANNER=n
12+
CONFIG_NCS_BOOT_BANNER=n
13+
CONFIG_PRINTK=n
14+
CONFIG_LOG=n
15+
CONFIG_CONSOLE=n
16+
CONFIG_UART_CONSOLE=n
17+
CONFIG_SERIAL=n
18+
19+
CONFIG_SPI=y
20+
CONFIG_ASSERT=y
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <zephyr/logging/log.h>
8+
LOG_MODULE_REGISTER(idle_spim_loopback, LOG_LEVEL_INF);
9+
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/drivers/spi.h>
12+
#include <zephyr/linker/devicetree_regions.h>
13+
#include <zephyr/pm/device_runtime.h>
14+
15+
#define DELTA (1)
16+
17+
#define SPI_MODE (SPI_OP_MODE_MASTER | SPI_WORD_SET(8) | SPI_LINES_SINGLE | SPI_TRANSFER_MSB \
18+
| SPI_MODE_CPHA | SPI_MODE_CPOL)
19+
20+
static struct spi_dt_spec spim_spec = SPI_DT_SPEC_GET(DT_NODELABEL(dut_spi_dt), SPI_MODE, 0);
21+
22+
#define MEMORY_SECTION(node) \
23+
COND_CODE_1(DT_NODE_HAS_PROP(node, memory_regions), \
24+
(__attribute__((__section__( \
25+
LINKER_DT_NODE_REGION_NAME(DT_PHANDLE(node, memory_regions)))))), \
26+
())
27+
28+
static uint8_t spim_buffer[2 * CONFIG_DATA_FIELD]
29+
MEMORY_SECTION(DT_BUS(DT_NODELABEL(dut_spi_dt)));
30+
31+
/* Variables used to make SPI active for ~1 second */
32+
static struct k_timer my_timer;
33+
static bool timer_expired;
34+
35+
void my_timer_handler(struct k_timer *dummy)
36+
{
37+
timer_expired = true;
38+
}
39+
40+
int main(void)
41+
{
42+
int ret;
43+
int counter = 0;
44+
uint8_t acc = 0;
45+
bool test_pass;
46+
47+
/* SPI buffer sets */
48+
struct spi_buf tx_spi_buf = {
49+
.buf = &spim_buffer[0],
50+
.len = CONFIG_DATA_FIELD
51+
};
52+
struct spi_buf_set tx_spi_buf_set = {
53+
.buffers = &tx_spi_buf,
54+
.count = 1
55+
};
56+
57+
struct spi_buf rx_spi_buf = {
58+
.buf = &spim_buffer[CONFIG_DATA_FIELD],
59+
.len = CONFIG_DATA_FIELD
60+
};
61+
struct spi_buf_set rx_spi_buf_set = {
62+
.buffers = &rx_spi_buf,
63+
.count = 1
64+
};
65+
66+
LOG_INF("%s runs as a SPI HOST", CONFIG_BOARD_TARGET);
67+
LOG_INF("%d bytes of data exchanged at once", CONFIG_DATA_FIELD);
68+
69+
ret = spi_is_ready_dt(&spim_spec);
70+
if (!ret) {
71+
LOG_ERR("Error: SPI device is not ready");
72+
}
73+
__ASSERT(ret, "Error: SPI device is not ready\n");
74+
75+
k_timer_init(&my_timer, my_timer_handler, NULL);
76+
77+
/* Run test forever */
78+
while (1) {
79+
test_pass = true;
80+
timer_expired = false;
81+
82+
/* start a one-shot timer that expires after 1 second */
83+
k_timer_start(&my_timer, K_MSEC(1000), K_NO_WAIT);
84+
85+
/* SPI active transmissions for ~ 1 second */
86+
while (!timer_expired) {
87+
/* Generate pseudo random tx_data for current test */
88+
for (int i = 0; i < CONFIG_DATA_FIELD; i++) {
89+
*((uint8_t *) tx_spi_buf.buf + i) = acc;
90+
acc += DELTA;
91+
}
92+
93+
/* Transmit data */
94+
ret = spi_transceive_dt(&spim_spec, &tx_spi_buf_set, &rx_spi_buf_set);
95+
if (ret != 0) {
96+
LOG_ERR("spi_transceive_dt, err: %d", ret);
97+
}
98+
__ASSERT(ret == 0, "Error: spi_transceive_dt, err: %d\n", ret);
99+
100+
/* Check if the received data is consistent with the data sent */
101+
for (int i = 0; i < CONFIG_DATA_FIELD; i++) {
102+
uint8_t received = *((uint8_t *) rx_spi_buf.buf + i);
103+
uint8_t transmitted = *((uint8_t *) tx_spi_buf.buf + i);
104+
105+
if (received != transmitted) {
106+
LOG_ERR("FAIL: rx[%d] = %d, expected %d",
107+
i, received, transmitted);
108+
test_pass = false;
109+
__ASSERT(false, "Run %d - FAILED\n", counter);
110+
}
111+
}
112+
} /* while (!timer_expired) */
113+
114+
/* Report if communication was successful */
115+
if (test_pass) {
116+
LOG_INF("Run %d - PASS", counter);
117+
} else {
118+
LOG_INF("Run %d - FAILED", counter);
119+
}
120+
counter++;
121+
122+
/* Sleep / enter low power state */
123+
k_msleep(CONFIG_TEST_SLEEP_DURATION_MS);
124+
}
125+
126+
return 0;
127+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
if("${SB_CONFIG_REMOTE_BOARD}" STREQUAL "")
8+
message(FATAL_ERROR "REMOTE_BOARD must be set to a valid board name")
9+
endif()
10+
11+
# Add remote project
12+
ExternalZephyrProject_Add(
13+
APPLICATION remote
14+
SOURCE_DIR ${APP_DIR}/../../power_consumption/common/remote_sleep_forever
15+
BOARD ${SB_CONFIG_REMOTE_BOARD}
16+
BOARD_REVISION ${BOARD_REVISION}
17+
)
18+
19+
# Add a dependency so that the remote image will be built and flashed first
20+
add_dependencies(idle_spim_loopback remote)
21+
# Add dependency so that the remote image is flashed first.
22+
sysbuild_add_dependencies(FLASH idle_spim_loopback remote)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SB_CONFIG_REMOTE_BOARD="nrf54h20dk/nrf54h20/cpurad"

0 commit comments

Comments
 (0)