Skip to content

Commit f6d7c38

Browse files
fabiobaltiericfriedt
authored andcommitted
doc: iterable_sections: add an API example
Expand the iterable sections documentation by adding a usage example. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent 29585ee commit f6d7c38

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

doc/reference/iterable_sections/index.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,62 @@ This page contains the reference documentation for the iterable sections APIs,
77
which can be used for defining iterable areas of equally-sized data structures,
88
that can be iterated on using `STRUCT_SECTION_FOREACH()`.
99

10+
Usage
11+
*****
12+
13+
Iterable section elements are typically used by defining the data structure and
14+
associated initializer in a common header file, so that they can be
15+
instantiated anywhere in the code base.
16+
17+
.. code-block:: c
18+
19+
struct my_data {
20+
int a, b;
21+
};
22+
23+
#define DEFINE_DATA(name, _a, _b) \
24+
STRUCT_SECTION_ITERABLE(my_data, name) = { \
25+
.a = _a, \
26+
.b = _b, \
27+
}
28+
29+
...
30+
31+
DEFINE_DATA(d1, 1, 2);
32+
DEFINE_DATA(d2, 3, 4);
33+
DEFINE_DATA(d3, 5, 6);
34+
35+
Then the linker has to be setup to place the place the structure in a
36+
contiguous segment using one of the linker macros such as
37+
`ITERABLE_SECTION_RAM()` or `ITERABLE_SECTION_ROM()`. Custom linker snippets
38+
are normally declared using one of the ``zephyr_linker_sources()`` CMake
39+
functions, using the appropriate section identifier, ``DATA_SECTIONS`` for RAM
40+
structures and ``SECTIONS`` for ROM ones.
41+
42+
.. code-block:: cmake
43+
44+
# CMakeLists.txt
45+
zephyr_linker_sources(DATA_SECTIONS iterables.ld)
46+
47+
.. code-block:: c
48+
49+
# iterables.ld
50+
ITERABLE_SECTION_RAM(my_data, 4)
51+
52+
The data can then be accessed using `STRUCT_SECTION_FOREACH()`.
53+
54+
.. code-block:: c
55+
56+
STRUCT_SECTION_FOREACH(my_data, data) {
57+
printk("%p: a: %d, b: %d\n", data, data->a, data->b);
58+
}
59+
60+
.. note::
61+
The linker is going to place the entries sorted by name, so the example
62+
above would visit ``d1``, ``d2`` and ``d3`` in that order, regardless of how
63+
they were defined in the code.
64+
65+
API Reference
66+
*************
67+
1068
.. doxygengroup:: iterable_section_apis

0 commit comments

Comments
 (0)