devicetree: add DT_PARENT()

This macro takes a node identifier, and returns the parent node's
identifier.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-04-08 15:04:15 -07:00 committed by Kumar Gala
commit 6e27343e7b
4 changed files with 47 additions and 3 deletions

View file

@ -41,6 +41,8 @@ node-macro =/ %s"DT_N" path-id %s"_IRQ_NAME_" dt-name
%s"_VAL_" dt-name [ %s"_EXISTS" ]
; Macros are generated for each of the node's compatibles.
node-macro =/ %s"DT_N" path-id %s"_COMPAT_MATCHES_" dt-name
; The node identifier for the node's parent in the devicetree.
node-macro =/ %s"DT_N" path-id %s"_PARENT"
; --------------------------------------------------------------------
; property-macro: a macro related to a node property

View file

@ -212,6 +212,26 @@
*/
#define DT_INST(inst, compat) UTIL_CAT(DT_N_INST, DT_DASH(inst, compat))
/**
* @brief Get a node identifier for a parent node
*
* Example devicetree fragment:
*
* parent: parent-node {
* child: child-node {
* ...
* };
* };
*
* The following generate equivalent node identifiers:
*
* DT_NODELABEL(parent)
* DT_PARENT(DT_NODELABEL(child))
*
* @param node_id node identifier
*/
#define DT_PARENT(node_id) UTIL_CAT(node_id, _PARENT)
/**
* @brief Get a node identifier for a child node
*

View file

@ -51,11 +51,16 @@ def main():
node.z_path_id = node_z_path_id(node)
write_node_comment(node)
if node.parent is not None:
out_comment(f"Node parent ({node.parent.path}) identifier:")
out_dt_define(f"{node.z_path_id}_PARENT",
f"DT_{node.parent.z_path_id}")
if not node.enabled:
out_comment("No macros: node is disabled")
out_comment("No node macros: node is disabled")
continue
if not node.matching_compat:
out_comment("No macros: node has no matching binding")
out_comment("No node macros: node has no matching binding")
continue
write_idents_and_existence(node)

View file

@ -1218,6 +1218,22 @@ static void test_clocks(void)
"fixed clk freq");
}
static void test_parent(void)
{
/* The label of a child node's parent is the label of the parent. */
zassert_true(!strcmp(DT_LABEL(DT_PARENT(TEST_SPI_DEV_0)),
DT_LABEL(TEST_SPI_BUS_0)),
"dev 0 parent");
/*
* We should be able to use DT_PARENT() even with nodes, like /test,
* that have no matching compatible.
*/
zassert_true(!strcmp(DT_LABEL(DT_CHILD(DT_PARENT(TEST_SPI_BUS_0),
spi_33334444)),
DT_LABEL(TEST_SPI_BUS_0)),
"round trip through node with no compatible");
}
void test_main(void)
{
ztest_test_suite(devicetree_api,
@ -1243,7 +1259,8 @@ void test_main(void)
ztest_unit_test(test_cs_gpios),
ztest_unit_test(test_chosen),
ztest_unit_test(test_enums),
ztest_unit_test(test_clocks)
ztest_unit_test(test_clocks),
ztest_unit_test(test_parent)
);
ztest_run_test_suite(devicetree_api);
}