Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devicetree: Add DT_FOREACH_ANCESTOR macro #81338

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

rruuaanng
Copy link
Collaborator

@rruuaanng rruuaanng commented Nov 13, 2024

Add 'DT_FOREACH_ANCESTOR' macro to get a list of
ancestor node of a given node_id.

I've considered that one could use DT_PROP_BY_IDX(node_id, reg, 0) in place of DT_REG_RAW_ADDR(node_id), but only for 32-bit addresses. This could be generalized by adding a different API for retrieving #address-cells and #size-cells, but I think that would be a bit clunky.
I've also thought about using the existing DT_RANGES_* macros to translate and validate addresses manually. This would still require at least the following additions:
DT_REG_RAW_ADDR()
Something like DT_FOREACH_ANCESTOR() for walking up the tree.

from #58860

@rruuaanng rruuaanng force-pushed the dt-parent branch 2 times, most recently from eaecc55 to f653237 Compare November 13, 2024 13:05
@rruuaanng rruuaanng added area: Devicetree RFC Request For Comments: want input from the community labels Nov 13, 2024
@rruuaanng
Copy link
Collaborator Author

rruuaanng commented Nov 13, 2024

I noticed that some parts of the documentation have conflicts with the changes. I will fix them after merging this PR.

There is currently only one "generic" for-each macro,

And, there are also some inappropriate func tags.

:c:func:DT_FOREACH_CHILD, which allows iterating over the children of a
devicetree node.

# Writes helper macros for dealing with node's parent.
out_dt_define(f"{node.z_path_id}_FOREACH_ANCESTOR(fn)",
" ".join(f"fn(DT_{parent.z_path_id})" for parent in
_visit_parent_node(node) if parent is not None))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if here cannot be omitted. The generator will traverse to the root node so that it has a None.

ZTEST(devicetree_api, test_parent_nodes_list)
{
/* When traversing upwards, there are no fixed attributes and labels */
#define TEST_FUNC(parent) { /* No operation */ }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to add too many properties for testing in the test dts, so I chose to do this. Make it only used to fill in the macro parameters, and then calculate whether the number of nodes it traverses matches.

Comment on lines +2246 to +2247
zassert_equal(ARRAY_SIZE(vals_a), 3, "");
zassert_equal(ARRAY_SIZE(vals_b), 4, "");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is here.

@rruuaanng
Copy link
Collaborator Author

I ran the test on my PC and it performed fine.

 - PASS - [devicetree_api.test_model] duration = 0.001 seconds
 - PASS - [devicetree_api.test_node_child_idx] duration = 0.001 seconds
 - PASS - [devicetree_api.test_node_name] duration = 0.001 seconds
 - PASS - [devicetree_api.test_nodelabel_props] duration = 0.001 seconds
 - PASS - [devicetree_api.test_parent] duration = 0.001 seconds
 - PASS - [devicetree_api.test_parent_nodes_list] duration = 0.001 seconds
 - PASS - [devicetree_api.test_path] duration = 0.001 seconds
 - PASS - [devicetree_api.test_path_props] duration = 0.003 seconds
 - PASS - [devicetree_api.test_phandles] duration = 0.007 seconds
 - PASS - [devicetree_api.test_pinctrl] duration = 0.004 seconds
 - PASS - [devicetree_api.test_pwms] duration = 0.003 seconds
 - PASS - [devicetree_api.test_ranges_empty] duration = 0.002 seconds
 - PASS - [devicetree_api.test_ranges_other] duration = 0.003 seconds
 - PASS - [devicetree_api.test_ranges_pcie] duration = 0.002 seconds
 - PASS - [devicetree_api.test_reg] duration = 0.003 seconds
 - PASS - [devicetree_api.test_reg_64] duration = 0.001 seconds
 - PASS - [devicetree_api.test_reset] duration = 0.002 seconds
 - PASS - [devicetree_api.test_same_node] duration = 0.001 seconds
 - PASS - [devicetree_api.test_string_array_escape] duration = 0.001 seconds
 - PASS - [devicetree_api.test_string_escape] duration = 0.003 seconds
 - PASS - [devicetree_api.test_string_idx_token] duration = 0.003 seconds
 - PASS - [devicetree_api.test_string_idx_unquoted] duration = 0.002 seconds
 - PASS - [devicetree_api.test_string_token] duration = 0.003 seconds
 - PASS - [devicetree_api.test_string_unquoted] duration = 0.003 seconds
 - PASS - [devicetree_api.test_vendor] duration = 0.001 seconds

------ TESTSUITE SUMMARY END ------

===================================================================
PROJECT EXECUTION SUCCESSFUL

@rruuaanng rruuaanng marked this pull request as draft November 13, 2024 15:12
@rruuaanng
Copy link
Collaborator Author

To my surprise, it worked fine in my PC, I converted it to a draft and tried to find the cause of the error :(

@rruuaanng rruuaanng marked this pull request as ready for review November 14, 2024 06:23
Add 'DT_FOREACH_ANCESTOR' macro to get a list of
ancestor node of a given node_id.

Signed-off-by: James Roy <[email protected]>
@kartben kartben merged commit 4553a21 into zephyrproject-rtos:main Dec 18, 2024
35 checks passed
@rruuaanng rruuaanng deleted the dt-parent branch December 18, 2024 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Build System area: Devicetree RFC Request For Comments: want input from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants