-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
6 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(memory_region_flags) | ||
|
||
FILE(GLOB app_sources src/*.c) | ||
target_sources(app PRIVATE ${app_sources}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2024, TOKITA Hiroshi <[email protected]> | ||
*/ | ||
|
||
/ { | ||
test { | ||
#address-cells = < 0x1 >; | ||
#size-cells = < 0x1 >; | ||
|
||
test_region_r: sram@20010000 { | ||
compatible = "zephyr,memory-region"; | ||
reg = < 0x20010000 0x100 >; | ||
zephyr,memory-region = "TEST_REGION_R"; | ||
zephyr,memory-region-flags = "r"; | ||
}; | ||
|
||
test_region_nrwxail: sram@20010100 { | ||
compatible = "zephyr,memory-region"; | ||
reg = < 0x20010100 0x100 >; | ||
zephyr,memory-region = "TEST_REGION_NRWXAIL"; | ||
zephyr,memory-region-flags = "!rwxail"; | ||
}; | ||
|
||
test_region_no_flags: sram@20010200 { | ||
compatible = "zephyr,memory-region"; | ||
reg = < 0x20010200 0x100 >; | ||
zephyr,memory-region = "TEST_REGION_NO_FLAGS"; | ||
}; | ||
|
||
test_region_none: sram@20010300 { | ||
compatible = "zephyr,memory-region"; | ||
reg = < 0x20010300 0x100 >; | ||
zephyr,memory-region = "TEST_REGION_NONE"; | ||
zephyr,memory-region-flags = ""; | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# empty |
73 changes: 73 additions & 0 deletions
73
tests/lib/devicetree/memory_region_flags/pytest/test_memory_region_flags.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright (c) 2024, TOKITA Hiroshi <[email protected]> | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import sys | ||
import logging | ||
import re | ||
from twister_harness import DeviceAdapter | ||
|
||
ZEPHYR_BASE = os.getenv("ZEPHYR_BASE") | ||
sys.path.insert( | ||
0, os.path.join(ZEPHYR_BASE, "scripts", "dts", "python-devicetree", "src") | ||
) | ||
from devicetree import edtlib | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def smart_int(numstr): | ||
if numstr.startswith("0x"): | ||
return int(numstr[2:], 16) | ||
else: | ||
return int(numstr) | ||
|
||
|
||
def verify_memory_region_flags(build_dir, label, expect): | ||
logger.info(label) | ||
|
||
linkercmd = os.path.join(build_dir, "zephyr", "linker.cmd") | ||
logger.info(linkercmd) | ||
|
||
edt = edtlib.EDT( | ||
os.path.join(build_dir, "zephyr", "zephyr.dts"), | ||
[os.path.join(ZEPHYR_BASE, "dts", "bindings")], | ||
) | ||
|
||
node = edt.label2node[label] | ||
logger.info(node) | ||
|
||
region = node.props["zephyr,memory-region"].val | ||
logger.info(region) | ||
|
||
origin = node.props["reg"].val[0] | ||
length = node.props["reg"].val[1] | ||
pattern = ( | ||
region + r"\s*" + expect + | ||
r"\s*:\s*ORIGIN\s*=\s*\(?([0-9A-Fa-fx]*)\)?,\s*LENGTH\s*=\s*\(?([0-9A-Fa-fx]*)\)?" | ||
) | ||
|
||
logger.info(pattern) | ||
|
||
found = False | ||
|
||
with open(linkercmd) as f: | ||
for line in f: | ||
m = re.search(pattern, line) | ||
if m and smart_int(m[1]) == origin and smart_int(m[2]) == length: | ||
found = True | ||
|
||
assert found | ||
|
||
|
||
def test_region_r(dut: DeviceAdapter): | ||
verify_memory_region_flags(dut.device_config.build_dir, "test_region_r", r"\(\s*r\s*\)") | ||
|
||
def test_region_nrwxail(dut: DeviceAdapter): | ||
verify_memory_region_flags(dut.device_config.build_dir, "test_region_nrwxail", r"\(\s*!rwxail\s*\)") | ||
|
||
def test_region_no_flags(dut: DeviceAdapter): | ||
verify_memory_region_flags(dut.device_config.build_dir, "test_region_no_flags", r"\(\s*rw\s*\)") | ||
|
||
def test_region_none(dut: DeviceAdapter): | ||
verify_memory_region_flags(dut.device_config.build_dir, "test_region_none", r"") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright (c) 2024, TOKITA Hiroshi <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
int main(void) | ||
{ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
tests: | ||
|
||
libraries.devicetree.memory_region_flags: | ||
harness: pytest | ||
platform_allow: qemu_cortex_m3 | ||
tags: | ||
- devicetree | ||
- pytest | ||
|
||
libraries.devicetree.memory_region_flags.linker_generator: | ||
harness: pytest | ||
platform_allow: qemu_cortex_m3 | ||
tags: | ||
- devicetree | ||
- pytest | ||
extra_configs: | ||
- CONFIG_CMAKE_LINKER_GENERATOR=y |