Skip to content

Commit 1ae57e1

Browse files
committed
tests: nvmem: api: Add test suite for NVMEM devicetree API
Add basic testing to validate devictree macros for the NVMEM subsystem. Add testing with an EEPROM backend. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent dd8de62 commit 1ae57e1

File tree

12 files changed

+234
-0
lines changed

12 files changed

+234
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 Basalte bv
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Test NVMEM consumer node
5+
6+
compatible: "vnd,nvmem-consumer"
7+
8+
include: [nvmem-consumer.yaml]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 Basalte bv
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Test NVMEM provider node
5+
6+
compatible: "vnd,nvmem-provider"
7+
8+
include: [nvmem-provider.yaml]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 Basalte bv
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(nvmem_eeprom)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2025, Basalte bv
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/ {
7+
aliases {
8+
nvmem0 = &eeprom0;
9+
};
10+
11+
test_consumer0: test-consumer0 {
12+
compatible = "vnd,nvmem-consumer";
13+
nvmem-cells = <&cell0>, <&cell10>;
14+
nvmem-cell-names = "cell0", "cell10";
15+
};
16+
};
17+
18+
&eeprom0 {
19+
#address-cells = <1>;
20+
#size-cells = <1>;
21+
22+
cell0: cell@0 {
23+
reg = <0x0 0x10>;
24+
#nvmem-cell-cells = <0>;
25+
};
26+
27+
cell10: cell@10 {
28+
reg = <0x10 0x10>;
29+
read-only;
30+
#nvmem-cell-cells = <0>;
31+
};
32+
};

tests/subsys/nvmem/api/prj.conf

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

tests/subsys/nvmem/api/src/main.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) 2025 Basalte bv
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/ztest.h>
7+
#include <zephyr/nvmem.h>
8+
9+
#define consumer0 DT_NODELABEL(test_consumer0)
10+
#define nvmem0 DT_ALIAS(nvmem0)
11+
12+
static const struct nvmem_cell cell0 = NVMEM_CELL_GET_BY_IDX(consumer0, 0);
13+
static const struct nvmem_cell cell10 = NVMEM_CELL_GET_BY_NAME(consumer0, cell10);
14+
15+
ZTEST(nvmem_api, test_nvmem_api)
16+
{
17+
uint8_t buf[0x10];
18+
int ret;
19+
20+
zexpect_equal_ptr(cell0.dev, DEVICE_DT_GET(nvmem0));
21+
zexpect_equal(cell0.offset, 0);
22+
zexpect_equal(cell0.size, 0x10);
23+
zexpect_false(cell0.read_only);
24+
25+
zexpect_equal_ptr(cell10.dev, DEVICE_DT_GET(nvmem0));
26+
zexpect_equal(cell10.offset, 0x10);
27+
zexpect_equal(cell10.size, 0x10);
28+
zexpect_true(cell10.read_only);
29+
30+
for (size_t i = 0; i < sizeof(buf); ++i) {
31+
buf[i] = 2 * i;
32+
}
33+
34+
ret = nvmem_cell_write(&cell0, buf, 0, sizeof(buf));
35+
zassert_ok(ret, "Failed to write NVMEM");
36+
37+
memset(buf, 0, sizeof(buf));
38+
39+
ret = nvmem_cell_read(&cell0, buf, 0, sizeof(buf));
40+
zassert_ok(ret, "Failed to read NVMEM");
41+
42+
for (size_t i = 0; i < sizeof(buf); ++i) {
43+
zexpect_equal(buf[i], 2 * i);
44+
}
45+
46+
ret = nvmem_cell_write(&cell10, buf, 0, sizeof(buf));
47+
zassert_equal(ret, -EROFS, "Expected read-only NVMEM");
48+
}
49+
50+
ZTEST_SUITE(nvmem_api, NULL, NULL, NULL, NULL, NULL);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
common:
2+
platform_allow:
3+
- native_sim
4+
integration_platforms:
5+
- native_sim
6+
tags:
7+
- nvmem
8+
tests:
9+
subsys.nvmem.eeprom:
10+
extra_configs:
11+
- CONFIG_EEPROM=y
12+
extra_dtc_overlay_files:
13+
- eeprom.overlay
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 Basalte bv
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(nvmem_devictree)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2025, Basalte bv
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/ {
7+
test_consumer0: test-consumer0 {
8+
compatible = "vnd,nvmem-consumer";
9+
nvmem-cells = <&cell0>, <&cell10>;
10+
nvmem-cell-names = "cell0", "cell10";
11+
};
12+
13+
test_provider0: test-provider0 {
14+
compatible = "vnd,nvmem-provider";
15+
#address-cells = <1>;
16+
#size-cells = <1>;
17+
18+
cell0: cell@0 {
19+
reg = <0x0 0x10>;
20+
#nvmem-cell-cells = <0>;
21+
};
22+
23+
cell10: cell@10 {
24+
reg = <0x10 0x10>;
25+
read-only;
26+
#nvmem-cell-cells = <0>;
27+
};
28+
};
29+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_NVMEM=y

0 commit comments

Comments
 (0)