Skip to content

Commit aeee230

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 1c4f0fc commit aeee230

File tree

11 files changed

+231
-0
lines changed

11 files changed

+231
-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+
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
nvmem-layout {
20+
compatible = "fixed-layout";
21+
#address-cells = <1>;
22+
#size-cells = <1>;
23+
24+
cell0: cell@0 {
25+
reg = <0x0 0x10>;
26+
#nvmem-cell-cells = <0>;
27+
};
28+
29+
cell10: cell@10 {
30+
reg = <0x10 0x10>;
31+
read-only;
32+
#nvmem-cell-cells = <0>;
33+
};
34+
};
35+
};

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
nvmem-layout {
15+
compatible = "fixed-layout";
16+
#address-cells = <1>;
17+
#size-cells = <1>;
18+
19+
cell0: cell@0 {
20+
reg = <0x0 0x10>;
21+
#nvmem-cell-cells = <0>;
22+
};
23+
24+
cell10: cell@10 {
25+
reg = <0x10 0x10>;
26+
read-only;
27+
#nvmem-cell-cells = <0>;
28+
};
29+
};
30+
};
31+
};
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
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
11+
ZTEST(nvmem_devictree, test_nvmem_devictree)
12+
{
13+
zexpect_equal(DT_NVMEM_CELLS_HAS_IDX(consumer0, 0), 1);
14+
zexpect_equal(DT_NVMEM_CELLS_HAS_IDX(consumer0, 1), 1);
15+
zexpect_equal(DT_NVMEM_CELLS_HAS_IDX(consumer0, 2), 0);
16+
17+
zexpect_equal(DT_NVMEM_CELLS_HAS_NAME(consumer0, cell0), 1);
18+
zexpect_equal(DT_NVMEM_CELLS_HAS_NAME(consumer0, cell10), 1);
19+
zexpect_equal(DT_NVMEM_CELLS_HAS_NAME(consumer0, missing), 0);
20+
21+
zexpect_equal(DT_NUM_NVMEM_CELLS(consumer0), 2);
22+
23+
zexpect_str_equal(DT_NODE_PATH(DT_NVMEM_CELL_BY_IDX(consumer0, 0)),
24+
"/test-provider0/nvmem-layout/cell@0");
25+
zexpect_str_equal(DT_NODE_PATH(DT_NVMEM_CELL_BY_IDX(consumer0, 1)),
26+
"/test-provider0/nvmem-layout/cell@10");
27+
28+
zexpect_str_equal(DT_NODE_PATH(DT_NVMEM_CELL_BY_NAME(consumer0, cell0)),
29+
"/test-provider0/nvmem-layout/cell@0");
30+
zexpect_str_equal(DT_NODE_PATH(DT_NVMEM_CELL_BY_NAME(consumer0, cell10)),
31+
"/test-provider0/nvmem-layout/cell@10");
32+
33+
zexpect_str_equal(DT_NODE_PATH(DT_MTD_FROM_NVMEM_CELL(DT_NVMEM_CELL(consumer0))),
34+
"/test-provider0");
35+
}
36+
37+
#define DT_DRV_COMPAT vnd_nvmem_consumer
38+
ZTEST(nvmem_devictree, test_nvmem_devictree_inst)
39+
{
40+
zexpect_equal(DT_INST_NVMEM_CELLS_HAS_IDX(0, 0), 1);
41+
zexpect_equal(DT_INST_NVMEM_CELLS_HAS_IDX(0, 1), 1);
42+
zexpect_equal(DT_INST_NVMEM_CELLS_HAS_IDX(0, 2), 0);
43+
44+
zexpect_equal(DT_INST_NVMEM_CELLS_HAS_NAME(0, cell0), 1);
45+
zexpect_equal(DT_INST_NVMEM_CELLS_HAS_NAME(0, cell10), 1);
46+
zexpect_equal(DT_INST_NVMEM_CELLS_HAS_NAME(0, missing), 0);
47+
48+
zexpect_equal(DT_INST_NUM_NVMEM_CELLS(0), 2);
49+
50+
zexpect_str_equal(DT_NODE_PATH(DT_INST_NVMEM_CELL_BY_IDX(0, 0)),
51+
"/test-provider0/nvmem-layout/cell@0");
52+
zexpect_str_equal(DT_NODE_PATH(DT_INST_NVMEM_CELL_BY_IDX(0, 1)),
53+
"/test-provider0/nvmem-layout/cell@10");
54+
55+
zexpect_str_equal(DT_NODE_PATH(DT_INST_NVMEM_CELL_BY_NAME(0, cell0)),
56+
"/test-provider0/nvmem-layout/cell@0");
57+
zexpect_str_equal(DT_NODE_PATH(DT_INST_NVMEM_CELL_BY_NAME(0, cell10)),
58+
"/test-provider0/nvmem-layout/cell@10");
59+
60+
zexpect_str_equal(DT_NODE_PATH(DT_MTD_FROM_NVMEM_CELL(DT_INST_NVMEM_CELL(0))),
61+
"/test-provider0");
62+
}
63+
#undef DT_DRV_COMPAT
64+
65+
ZTEST_SUITE(nvmem_devictree, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)