Skip to content

Commit cd51657

Browse files
carlocaionembolivar-nordic
authored andcommitted
devicetree_regions: Fix fallback on token
Fix the wrong fallback on token and add a new test to catch this kind of errors early. Signed-off-by: Carlo Caione <[email protected]>
1 parent 4ff4991 commit cd51657

File tree

7 files changed

+171
-14
lines changed

7 files changed

+171
-14
lines changed

include/linker/devicetree_regions.h

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,40 @@
77
* Generate memory regions from devicetree nodes.
88
*/
99

10+
/**
11+
* @brief Get the linker memory-region name in a token form
12+
*
13+
* This attempts to use the zephyr,memory-region property (with
14+
* non-alphanumeric characters replaced with underscores) returning a token.
15+
*
16+
* Example devicetree fragment:
17+
*
18+
* @code{.dts}
19+
* / {
20+
* soc {
21+
* sram1: memory@2000000 {
22+
* zephyr,memory-region = "MY_NAME";
23+
* };
24+
* sram2: memory@2001000 {
25+
* zephyr,memory-region = "MY@OTHER@NAME";
26+
* };
27+
* };
28+
* };
29+
* @endcode
30+
*
31+
* Example usage:
32+
*
33+
* @code{.c}
34+
* LINKER_DT_NODE_REGION_NAME_TOKEN(DT_NODELABEL(sram1)) // MY_NAME
35+
* LINKER_DT_NODE_REGION_NAME_TOKEN(DT_NODELABEL(sram2)) // MY_OTHER_NAME
36+
* @endcode
37+
*
38+
* @param node_id node identifier
39+
* @return the name of the memory memory region the node will generate
40+
*/
41+
#define LINKER_DT_NODE_REGION_NAME_TOKEN(node_id) \
42+
DT_STRING_TOKEN(node_id, zephyr_memory_region)
43+
1044
/**
1145
* @brief Get the linker memory-region name
1246
*
@@ -15,6 +49,7 @@
1549
*
1650
* Example devicetree fragment:
1751
*
52+
* @code{.dts}
1853
* / {
1954
* soc {
2055
* sram1: memory@2000000 {
@@ -25,23 +60,26 @@
2560
* };
2661
* };
2762
* };
63+
* @endcode
2864
*
2965
* Example usage:
3066
*
67+
* @code{.c}
3168
* LINKER_DT_NODE_REGION_NAME(DT_NODELABEL(sram1)) // "MY_NAME"
3269
* LINKER_DT_NODE_REGION_NAME(DT_NODELABEL(sram2)) // "MY_OTHER_NAME"
70+
* @endcode
3371
*
3472
* @param node_id node identifier
3573
* @return the name of the memory memory region the node will generate
3674
*/
3775
#define LINKER_DT_NODE_REGION_NAME(node_id) \
38-
DT_STRING_TOKEN(node_id, zephyr_memory_region)
76+
STRINGIFY(LINKER_DT_NODE_REGION_NAME_TOKEN(node_id))
3977

4078
/** @cond INTERNAL_HIDDEN */
4179

4280
#define _DT_COMPATIBLE zephyr_memory_region
4381

44-
#define _DT_SECTION_PREFIX(node_id) UTIL_CAT(__, LINKER_DT_NODE_REGION_NAME(node_id))
82+
#define _DT_SECTION_PREFIX(node_id) UTIL_CAT(__, LINKER_DT_NODE_REGION_NAME_TOKEN(node_id))
4583
#define _DT_SECTION_START(node_id) UTIL_CAT(_DT_SECTION_PREFIX(node_id), _start)
4684
#define _DT_SECTION_END(node_id) UTIL_CAT(_DT_SECTION_PREFIX(node_id), _end)
4785
#define _DT_SECTION_SIZE(node_id) UTIL_CAT(_DT_SECTION_PREFIX(node_id), _size)
@@ -50,30 +88,70 @@
5088
/**
5189
* @brief Declare a memory region
5290
*
91+
* Example devicetree fragment:
92+
*
93+
* @code{.dts}
94+
* test_sram: sram@20010000 {
95+
* compatible = "zephyr,memory-region", "mmio-sram";
96+
* reg = < 0x20010000 0x1000 >;
97+
* zephyr,memory-region = "FOOBAR";
98+
* };
99+
* @endcode
100+
*
101+
* will result in:
102+
*
103+
* @code{.unparsed}
104+
* FOOBAR (rw) : ORIGIN = (0x20010000), LENGTH = (0x1000)
105+
* @endcode
106+
*
53107
* @param node_id devicetree node identifier
54108
* @param attr region attributes
55109
*/
56-
#define _REGION_DECLARE(node_id) \
57-
LINKER_DT_NODE_REGION_NAME(node_id) : \
58-
ORIGIN = DT_REG_ADDR(node_id), \
110+
#define _REGION_DECLARE(node_id) \
111+
LINKER_DT_NODE_REGION_NAME_TOKEN(node_id) : \
112+
ORIGIN = DT_REG_ADDR(node_id), \
59113
LENGTH = DT_REG_SIZE(node_id)
60114

61115
/**
62116
* @brief Declare a memory section from the device tree nodes with
63117
* compatible 'zephyr,memory-region'
64118
*
119+
* Example devicetree fragment:
120+
*
121+
* @code{.dts}
122+
* test_sram: sram@20010000 {
123+
* compatible = "zephyr,memory-region", "mmio-sram";
124+
* reg = < 0x20010000 0x1000 >;
125+
* zephyr,memory-region = "FOOBAR";
126+
* };
127+
* @endcode
128+
*
129+
* will result in:
130+
*
131+
* @code{.unparsed}
132+
* FOOBAR 0x20010000 (NOLOAD) :
133+
* {
134+
* __FOOBAR_start = .;
135+
* KEEP(*(FOOBAR))
136+
* KEEP(*(FOOBAR.*))
137+
* __FOOBAR_end = .;
138+
* } > FOOBAR
139+
* __FOOBAR_size = __FOOBAR_end - __FOOBAR_start;
140+
* __FOOBAR_load_start = LOADADDR(FOOBAR);
141+
* @endcode
142+
*
65143
* @param node_id devicetree node identifier
66144
*/
67145
#define _SECTION_DECLARE(node_id) \
68-
LINKER_DT_NODE_REGION_NAME(node_id) DT_REG_ADDR(node_id) (NOLOAD) : \
146+
LINKER_DT_NODE_REGION_NAME_TOKEN(node_id) DT_REG_ADDR(node_id) (NOLOAD) : \
69147
{ \
70148
_DT_SECTION_START(node_id) = .; \
71-
KEEP(*(LINKER_DT_NODE_REGION_NAME(node_id))) \
72-
KEEP(*(LINKER_DT_NODE_REGION_NAME(node_id).*)) \
149+
KEEP(*(LINKER_DT_NODE_REGION_NAME_TOKEN(node_id))) \
150+
KEEP(*(LINKER_DT_NODE_REGION_NAME_TOKEN(node_id).*)) \
73151
_DT_SECTION_END(node_id) = .; \
74-
} > LINKER_DT_NODE_REGION_NAME(node_id) \
152+
} > LINKER_DT_NODE_REGION_NAME_TOKEN(node_id) \
75153
_DT_SECTION_SIZE(node_id) = _DT_SECTION_END(node_id) - _DT_SECTION_START(node_id); \
76-
_DT_SECTION_LOAD(node_id) = LOADADDR(LINKER_DT_NODE_REGION_NAME(node_id));
154+
_DT_SECTION_LOAD(node_id) = LOADADDR(LINKER_DT_NODE_REGION_NAME_TOKEN(node_id));
77155

78156
/** @endcond */
79157

tests/lib/devicetree/api_ext/src/main.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
static void test_linker_regions(void)
1919
{
20-
zassert_true(!strcmp(STRINGIFY(LINKER_DT_NODE_REGION_NAME(TEST_SRAM1)),
21-
"SRAM_REGION"), "");
22-
zassert_true(!strcmp(STRINGIFY(LINKER_DT_NODE_REGION_NAME(TEST_SRAM2)),
23-
"SRAM_REGION_2"), "");
20+
zassert_true(!strcmp(LINKER_DT_NODE_REGION_NAME(TEST_SRAM1), "SRAM_REGION"), "");
21+
zassert_true(!strcmp(LINKER_DT_NODE_REGION_NAME(TEST_SRAM2), "SRAM_REGION_2"), "");
2422
}
2523

2624
void test_main(void)
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)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(memory_region)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2022, Carlo Caione <[email protected]>
3+
*/
4+
5+
/ {
6+
test {
7+
#address-cells = < 0x1 >;
8+
#size-cells = < 0x1 >;
9+
10+
test_sram: sram@20010000 {
11+
compatible = "zephyr,memory-region", "mmio-sram";
12+
reg = < 0x20010000 0x1000 >;
13+
zephyr,memory-region = "SRAM_REGION";
14+
};
15+
};
16+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2022, Carlo Caione <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ztest.h>
8+
#include <devicetree.h>
9+
#include <device.h>
10+
11+
#include <linker/devicetree_regions.h>
12+
13+
#define TEST_SRAM_NODE DT_NODELABEL(test_sram)
14+
#define TEST_SRAM_SECT LINKER_DT_NODE_REGION_NAME(TEST_SRAM_NODE)
15+
#define TEST_SRAM_ADDR DT_REG_ADDR(TEST_SRAM_NODE)
16+
#define TEST_SRAM_SIZE DT_REG_SIZE(TEST_SRAM_NODE)
17+
18+
uint8_t var_in_test_sram[TEST_SRAM_SIZE] Z_GENERIC_SECTION(TEST_SRAM_SECT);
19+
20+
extern char __SRAM_REGION_start[];
21+
extern char __SRAM_REGION_end[];
22+
extern char __SRAM_REGION_size[];
23+
extern char __SRAM_REGION_load_start[];
24+
25+
static void test_memory_region(void)
26+
{
27+
zassert_true(!strcmp(LINKER_DT_NODE_REGION_NAME(TEST_SRAM_NODE), "SRAM_REGION"), "");
28+
29+
zassert_equal_ptr(var_in_test_sram, TEST_SRAM_ADDR, "");
30+
31+
zassert_equal_ptr(__SRAM_REGION_start, TEST_SRAM_ADDR, "");
32+
zassert_equal_ptr(__SRAM_REGION_end, TEST_SRAM_ADDR + TEST_SRAM_SIZE, "");
33+
zassert_equal_ptr(__SRAM_REGION_load_start, TEST_SRAM_ADDR, "");
34+
35+
zassert_equal((unsigned long) __SRAM_REGION_size, TEST_SRAM_SIZE, "");
36+
}
37+
38+
void test_main(void)
39+
{
40+
ztest_test_suite(devicetree_memory_region,
41+
ztest_unit_test(test_memory_region)
42+
);
43+
ztest_run_test_suite(devicetree_memory_region);
44+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests:
2+
3+
libraries.devicetree.memory_region:
4+
platform_allow: qemu_cortex_m3
5+
tags: devicetree
6+
7+
libraries.devicetree.memory_region.linker_generator:
8+
platform_allow: qemu_cortex_m3
9+
tags: devicetree
10+
extra_configs:
11+
- CONFIG_CMAKE_LINKER_GENERATOR=y

0 commit comments

Comments
 (0)