Skip to content

Commit 1d40a7e

Browse files
soburinashif
authored andcommitted
tests: lib: devicetree: add test for zephyr,memory-region-flags attr
Add a test for zephyr,memory-region-flags. This test tests the attributes of memory regions defined in the linker script, but this information cannot be obtained at runtime. It verifies that the expected memory region attributes are defined in the linker.cmd. Since no syntax analysis is performed, this is not an entirely rigorous test but sufficient for practical purposes. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 624e051 commit 1d40a7e

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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(memory_region_flags)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024, TOKITA Hiroshi <[email protected]>
3+
*/
4+
5+
/ {
6+
test {
7+
#address-cells = < 0x1 >;
8+
#size-cells = < 0x1 >;
9+
10+
test_region_r: sram@20010000 {
11+
compatible = "zephyr,memory-region";
12+
reg = < 0x20010000 0x100 >;
13+
zephyr,memory-region = "TEST_REGION_R";
14+
zephyr,memory-region-flags = "r";
15+
};
16+
17+
test_region_nrwxail: sram@20010100 {
18+
compatible = "zephyr,memory-region";
19+
reg = < 0x20010100 0x100 >;
20+
zephyr,memory-region = "TEST_REGION_NRWXAIL";
21+
zephyr,memory-region-flags = "!rwxail";
22+
};
23+
24+
test_region_no_flags: sram@20010200 {
25+
compatible = "zephyr,memory-region";
26+
reg = < 0x20010200 0x100 >;
27+
zephyr,memory-region = "TEST_REGION_NO_FLAGS";
28+
};
29+
30+
test_region_none: sram@20010300 {
31+
compatible = "zephyr,memory-region";
32+
reg = < 0x20010300 0x100 >;
33+
zephyr,memory-region = "TEST_REGION_NONE";
34+
zephyr,memory-region-flags = "";
35+
};
36+
};
37+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright (c) 2024, TOKITA Hiroshi <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import os
5+
import sys
6+
import logging
7+
import re
8+
from twister_harness import DeviceAdapter
9+
10+
ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
11+
sys.path.insert(
12+
0, os.path.join(ZEPHYR_BASE, "scripts", "dts", "python-devicetree", "src")
13+
)
14+
from devicetree import edtlib
15+
16+
logger = logging.getLogger(__name__)
17+
18+
19+
def smart_int(numstr):
20+
if numstr.startswith("0x"):
21+
return int(numstr[2:], 16)
22+
else:
23+
return int(numstr)
24+
25+
26+
def verify_memory_region_flags(build_dir, label, expect):
27+
logger.info(label)
28+
29+
linkercmd = os.path.join(build_dir, "zephyr", "linker.cmd")
30+
logger.info(linkercmd)
31+
32+
edt = edtlib.EDT(
33+
os.path.join(build_dir, "zephyr", "zephyr.dts"),
34+
[os.path.join(ZEPHYR_BASE, "dts", "bindings")],
35+
)
36+
37+
node = edt.label2node[label]
38+
logger.info(node)
39+
40+
region = node.props["zephyr,memory-region"].val
41+
logger.info(region)
42+
43+
origin = node.props["reg"].val[0]
44+
length = node.props["reg"].val[1]
45+
pattern = (
46+
region + r"\s*" + expect +
47+
r"\s*:\s*ORIGIN\s*=\s*\(?([0-9A-Fa-fx]*)\)?,\s*LENGTH\s*=\s*\(?([0-9A-Fa-fx]*)\)?"
48+
)
49+
50+
logger.info(pattern)
51+
52+
found = False
53+
54+
with open(linkercmd) as f:
55+
for line in f:
56+
m = re.search(pattern, line)
57+
if m and smart_int(m[1]) == origin and smart_int(m[2]) == length:
58+
found = True
59+
60+
assert found
61+
62+
63+
def test_region_r(dut: DeviceAdapter):
64+
verify_memory_region_flags(dut.device_config.build_dir, "test_region_r", r"\(\s*r\s*\)")
65+
66+
def test_region_nrwxail(dut: DeviceAdapter):
67+
verify_memory_region_flags(dut.device_config.build_dir, "test_region_nrwxail", r"\(\s*!rwxail\s*\)")
68+
69+
def test_region_no_flags(dut: DeviceAdapter):
70+
verify_memory_region_flags(dut.device_config.build_dir, "test_region_no_flags", r"\(\s*rw\s*\)")
71+
72+
def test_region_none(dut: DeviceAdapter):
73+
verify_memory_region_flags(dut.device_config.build_dir, "test_region_none", r"")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (c) 2024, TOKITA Hiroshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
int main(void)
8+
{
9+
return 0;
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests:
2+
3+
libraries.devicetree.memory_region_flags:
4+
harness: pytest
5+
platform_allow: qemu_cortex_m3
6+
tags:
7+
- devicetree
8+
- pytest
9+
10+
libraries.devicetree.memory_region_flags.linker_generator:
11+
harness: pytest
12+
platform_allow: qemu_cortex_m3
13+
tags:
14+
- devicetree
15+
- pytest
16+
extra_configs:
17+
- CONFIG_CMAKE_LINKER_GENERATOR=y

0 commit comments

Comments
 (0)