Skip to content

Commit 2167ce2

Browse files
nika-nordicjonathannilsen
authored andcommitted
[nrf fromtree] tests: boards: nrf: add tests for dmm component
Added tests verify output and input buffers allocation using dmm component. Signed-off-by: Nikodem Kastelik <[email protected]> (cherry picked from commit d67abdd)
1 parent df8cb42 commit 2167ce2

File tree

6 files changed

+365
-0
lines changed

6 files changed

+365
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(dmm)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
10+
target_sources(app PRIVATE ${app_sources})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/ {
2+
aliases {
3+
dut-cache = &spi1;
4+
dut-nocache= &spi3;
5+
};
6+
};
7+
8+
&pinctrl {
9+
spi1_default_alt: spi1_default_alt {
10+
group1 {
11+
psels = <NRF_PSEL(SPIM_MOSI, 0, 1)>,
12+
<NRF_PSEL(SPIM_SCK, 0, 2)>;
13+
};
14+
};
15+
16+
spi1_sleep_alt: spi1_sleep_alt {
17+
group1 {
18+
psels = <NRF_PSEL(SPIM_MOSI, 0, 1)>,
19+
<NRF_PSEL(SPIM_SCK, 0, 2)>;
20+
low-power-enable;
21+
};
22+
};
23+
24+
spi3_default_alt: spi3_default_alt {
25+
group1 {
26+
psels = <NRF_PSEL(SPIM_MOSI, 0, 3)>,
27+
<NRF_PSEL(SPIM_SCK, 0, 4)>;
28+
};
29+
};
30+
31+
spi3_sleep_alt: spi3_sleep_alt {
32+
group1 {
33+
psels = <NRF_PSEL(SPIM_MOSI, 0, 3)>,
34+
<NRF_PSEL(SPIM_SCK, 0, 4)>;
35+
low-power-enable;
36+
};
37+
};
38+
};
39+
40+
&spi1
41+
{
42+
compatible = "nordic,nrf-spim";
43+
status = "okay";
44+
pinctrl-0 = <&spi1_default_alt>;
45+
pinctrl-1 = <&spi1_sleep_alt>;
46+
pinctrl-names = "default", "sleep";
47+
};
48+
49+
&spi3
50+
{
51+
compatible = "nordic,nrf-spim";
52+
status = "okay";
53+
pinctrl-0 = <&spi3_default_alt>;
54+
pinctrl-1 = <&spi3_sleep_alt>;
55+
pinctrl-names = "default", "sleep";
56+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/ {
2+
aliases {
3+
dut-cache = &spi120;
4+
dut-nocache = &spi130;
5+
};
6+
};
7+
8+
&pinctrl {
9+
spi130_default_alt: spi130_default_alt {
10+
group1 {
11+
psels = <NRF_PSEL(SPIM_MOSI, 2, 8)>,
12+
<NRF_PSEL(SPIM_SCK, 1, 2)>;
13+
};
14+
};
15+
16+
spi130_sleep_alt: spi130_sleep_alt {
17+
group1 {
18+
psels = <NRF_PSEL(SPIM_MOSI, 2, 8)>,
19+
<NRF_PSEL(SPIM_SCK, 1, 2)>;
20+
low-power-enable;
21+
};
22+
};
23+
24+
spi120_default_alt: spi120_default_alt {
25+
group1 {
26+
psels = <NRF_PSEL(SPIM_MOSI, 7, 1)>,
27+
<NRF_PSEL(SPIM_SCK, 7, 2)>;
28+
};
29+
};
30+
31+
spi120_sleep_alt: spi120_sleep_alt {
32+
group1 {
33+
psels = <NRF_PSEL(SPIM_MOSI, 7, 1)>,
34+
<NRF_PSEL(SPIM_SCK, 7, 2)>;
35+
low-power-enable;
36+
};
37+
};
38+
};
39+
40+
&spi130
41+
{
42+
compatible = "nordic,nrf-spim";
43+
status = "okay";
44+
pinctrl-0 = <&spi130_default_alt>;
45+
pinctrl-1 = <&spi130_sleep_alt>;
46+
pinctrl-names = "default", "sleep";
47+
memory-regions = <&cpuapp_dma_region>;
48+
};
49+
50+
&spi120
51+
{
52+
compatible = "nordic,nrf-spim";
53+
status = "okay";
54+
pinctrl-0 = <&spi120_default_alt>;
55+
pinctrl-1 = <&spi120_sleep_alt>;
56+
pinctrl-names = "default", "sleep";
57+
memory-regions = <&dma_fast_region>;
58+
};

tests/boards/nrf/dmm/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y

tests/boards/nrf/dmm/src/main.c

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
#include <string.h>
9+
#include <zephyr/cache.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/ztest.h>
12+
13+
#include <dmm.h>
14+
15+
#define DUT_CACHE DT_ALIAS(dut_cache)
16+
#define DUT_NOCACHE DT_ALIAS(dut_nocache)
17+
18+
#define DMM_TEST_GET_REG_START(node_id) \
19+
COND_CODE_1(DT_NODE_HAS_PROP(node_id, memory_regions), \
20+
(DT_REG_ADDR(DT_PHANDLE(node_id, memory_regions))), (0))
21+
22+
#define DMM_TEST_GET_REG_SIZE(node_id) \
23+
COND_CODE_1(DT_NODE_HAS_PROP(node_id, memory_regions), \
24+
(DT_REG_SIZE(DT_PHANDLE(node_id, memory_regions))), (0))
25+
26+
struct dmm_test_region {
27+
void *mem_reg;
28+
uintptr_t start;
29+
size_t size;
30+
};
31+
32+
enum {
33+
DMM_TEST_REGION_CACHE,
34+
DMM_TEST_REGION_NOCACHE,
35+
DMM_TEST_REGION_COUNT
36+
};
37+
38+
struct dmm_fixture {
39+
struct dmm_test_region regions[DMM_TEST_REGION_COUNT];
40+
uint32_t fill_value;
41+
};
42+
43+
static const struct dmm_test_region dmm_test_regions[DMM_TEST_REGION_COUNT] = {
44+
[DMM_TEST_REGION_CACHE] = {
45+
.mem_reg = DMM_DEV_TO_REG(DUT_CACHE),
46+
.start = DMM_TEST_GET_REG_START(DUT_CACHE),
47+
.size = DMM_TEST_GET_REG_SIZE(DUT_CACHE)
48+
},
49+
[DMM_TEST_REGION_NOCACHE] = {
50+
.mem_reg = DMM_DEV_TO_REG(DUT_NOCACHE),
51+
.start = DMM_TEST_GET_REG_START(DUT_NOCACHE),
52+
.size = DMM_TEST_GET_REG_SIZE(DUT_NOCACHE)
53+
},
54+
};
55+
56+
static void *test_setup(void)
57+
{
58+
static struct dmm_fixture fixture;
59+
60+
memcpy(fixture.regions, dmm_test_regions, sizeof(dmm_test_regions));
61+
fixture.fill_value = 0x1;
62+
return &fixture;
63+
}
64+
65+
static void test_cleanup(void *argc)
66+
{
67+
}
68+
69+
static bool dmm_buffer_in_region_check(struct dmm_test_region *dtr, void *buf, size_t size)
70+
{
71+
uintptr_t start = (uintptr_t)buf;
72+
73+
return ((start >= dtr->start) && ((start + size) <= (dtr->start + dtr->size)));
74+
}
75+
76+
static void dmm_check_output_buffer(struct dmm_test_region *dtr, uint32_t *fill_value,
77+
void *data, size_t size, bool was_prealloc)
78+
{
79+
void *buf;
80+
int retval;
81+
82+
memset(data, (*fill_value)++, size);
83+
retval = dmm_buffer_out_prepare(dtr->mem_reg, data, size, &buf);
84+
zassert_ok(retval);
85+
zassert_true(IS_ALIGNED(buf, DMM_DCACHE_LINE_SIZE));
86+
87+
if (IS_ENABLED(CONFIG_HAS_NORDIC_DMM)) {
88+
if (was_prealloc) {
89+
zassert_equal(data, buf);
90+
} else {
91+
zassert_not_equal(data, buf);
92+
}
93+
zassert_true(dmm_buffer_in_region_check(dtr, buf, size));
94+
} else {
95+
zassert_equal(data, buf);
96+
}
97+
sys_cache_data_invd_range(buf, size);
98+
zassert_mem_equal(buf, data, size);
99+
100+
retval = dmm_buffer_out_release(dtr->mem_reg, buf);
101+
zassert_ok(retval);
102+
}
103+
104+
static void dmm_check_input_buffer(struct dmm_test_region *dtr, uint32_t *fill_value,
105+
void *data, size_t size, bool was_prealloc, bool is_cached)
106+
{
107+
void *buf;
108+
int retval;
109+
uint8_t intermediate_buf[128];
110+
111+
zassert_true(size < sizeof(intermediate_buf));
112+
113+
retval = dmm_buffer_in_prepare(dtr->mem_reg, data, size, &buf);
114+
zassert_ok(retval);
115+
zassert_true(IS_ALIGNED(buf, DMM_DCACHE_LINE_SIZE));
116+
117+
if (IS_ENABLED(CONFIG_HAS_NORDIC_DMM)) {
118+
if (was_prealloc) {
119+
zassert_equal(data, buf);
120+
} else {
121+
zassert_not_equal(data, buf);
122+
}
123+
zassert_true(dmm_buffer_in_region_check(dtr, buf, size));
124+
} else {
125+
zassert_equal(data, buf);
126+
}
127+
128+
/* Simulate external bus master writing to memory region */
129+
memset(buf, (*fill_value)++, size);
130+
sys_cache_data_flush_range(buf, size);
131+
/* Preserve actual memory region contents before polluting the cache */
132+
memcpy(intermediate_buf, buf, size);
133+
if (IS_ENABLED(CONFIG_DCACHE) && is_cached) {
134+
/* Purposefully pollute the cache to make sure library manages cache properly */
135+
memset(buf, (*fill_value)++, size);
136+
}
137+
138+
retval = dmm_buffer_in_release(dtr->mem_reg, data, size, buf);
139+
zassert_ok(retval);
140+
141+
zassert_mem_equal(data, intermediate_buf, size);
142+
}
143+
144+
ZTEST_USER_F(dmm, test_check_dev_cache_in_allocate)
145+
{
146+
uint8_t user_data[16];
147+
148+
dmm_check_input_buffer(&fixture->regions[DMM_TEST_REGION_CACHE], &fixture->fill_value,
149+
user_data, sizeof(user_data), false, true);
150+
}
151+
152+
ZTEST_USER_F(dmm, test_check_dev_cache_in_preallocate)
153+
{
154+
static uint8_t user_data[16] DMM_MEMORY_SECTION(DUT_CACHE);
155+
156+
dmm_check_input_buffer(&fixture->regions[DMM_TEST_REGION_CACHE], &fixture->fill_value,
157+
user_data, sizeof(user_data), true, true);
158+
}
159+
160+
ZTEST_USER_F(dmm, test_check_dev_cache_out_allocate)
161+
{
162+
uint8_t user_data[16];
163+
164+
dmm_check_output_buffer(&fixture->regions[DMM_TEST_REGION_CACHE], &fixture->fill_value,
165+
user_data, sizeof(user_data), false);
166+
}
167+
168+
ZTEST_USER_F(dmm, test_check_dev_cache_out_preallocate)
169+
{
170+
static uint8_t user_data[16] DMM_MEMORY_SECTION(DUT_CACHE);
171+
172+
dmm_check_output_buffer(&fixture->regions[DMM_TEST_REGION_CACHE], &fixture->fill_value,
173+
user_data, sizeof(user_data), true);
174+
}
175+
176+
ZTEST_USER_F(dmm, test_check_dev_nocache_in_allocate)
177+
{
178+
uint8_t user_data[16];
179+
180+
dmm_check_input_buffer(&fixture->regions[DMM_TEST_REGION_NOCACHE], &fixture->fill_value,
181+
user_data, sizeof(user_data), false, false);
182+
}
183+
184+
ZTEST_USER_F(dmm, test_check_dev_nocache_in_preallocate)
185+
{
186+
static uint8_t user_data[16] DMM_MEMORY_SECTION(DUT_NOCACHE);
187+
188+
dmm_check_input_buffer(&fixture->regions[DMM_TEST_REGION_NOCACHE], &fixture->fill_value,
189+
user_data, sizeof(user_data), true, false);
190+
}
191+
192+
ZTEST_USER_F(dmm, test_check_dev_nocache_out_allocate)
193+
{
194+
uint8_t user_data[16];
195+
196+
dmm_check_output_buffer(&fixture->regions[DMM_TEST_REGION_NOCACHE], &fixture->fill_value,
197+
user_data, sizeof(user_data), false);
198+
}
199+
200+
ZTEST_USER_F(dmm, test_check_dev_nocache_out_preallocate)
201+
{
202+
static uint8_t user_data[16] DMM_MEMORY_SECTION(DUT_NOCACHE);
203+
204+
dmm_check_output_buffer(&fixture->regions[DMM_TEST_REGION_NOCACHE], &fixture->fill_value,
205+
user_data, sizeof(user_data), true);
206+
}
207+
208+
ZTEST_SUITE(dmm, NULL, test_setup, NULL, test_cleanup, NULL);
209+
210+
int dmm_test_prepare(void)
211+
{
212+
const struct dmm_test_region *dtr;
213+
214+
for (size_t i = 0; i < ARRAY_SIZE(dmm_test_regions); i++) {
215+
dtr = &dmm_test_regions[i];
216+
memset((void *)dtr->start, 0x00, dtr->size);
217+
}
218+
219+
return 0;
220+
}
221+
222+
SYS_INIT(dmm_test_prepare, PRE_KERNEL_1, 0);

tests/boards/nrf/dmm/testcase.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
common:
2+
tags: drivers
3+
harness: ztest
4+
5+
tests:
6+
boards.nrf.dmm:
7+
platform_allow:
8+
- nrf54h20dk/nrf54h20/cpuapp
9+
- nrf5340dk/nrf5340/cpuapp
10+
integration_platforms:
11+
- nrf5340dk/nrf5340/cpuapp
12+
- nrf54h20dk/nrf54h20/cpuapp
13+
14+
boards.nrf.dmm.cache_disabled:
15+
extra_configs:
16+
- CONFIG_DCACHE=n
17+
platform_allow:
18+
- nrf54h20dk/nrf54h20/cpuapp

0 commit comments

Comments
 (0)