Skip to content

Commit 03c8deb

Browse files
MaureenHelmmbolivar-nordic
authored andcommitted
devicetree: Add model name helpers based on compat
Follow up to e1e1664 adding model name helpers to access generated macros based on a compatible's matching entries in vendor prefixes. Signed-off-by: Maureen Helm <maureen.helm@intel.com>
1 parent e73c363 commit 03c8deb

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

include/zephyr/devicetree.h

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1864,7 +1864,7 @@
18641864
*/
18651865

18661866
/**
1867-
* @defgroup devicetree-generic-vendor Vendor name helpers
1867+
* @defgroup devicetree-generic-vendor Vendor and model name helpers
18681868
* @ingroup devicetree
18691869
* @{
18701870
*/
@@ -1951,6 +1951,82 @@
19511951
#define DT_NODE_VENDOR_OR(node_id, default_value) \
19521952
DT_NODE_VENDOR_BY_IDX_OR(node_id, 0, default_value)
19531953

1954+
/**
1955+
* @brief Get the model at index "idx" as a string literal
1956+
*
1957+
* The model is a string extracted from the compatible after the vendor prefix.
1958+
*
1959+
* Example vendor-prefixes.txt:
1960+
*
1961+
* vnd A stand-in for a real vendor
1962+
* zephyr Zephyr-specific binding
1963+
*
1964+
* Example devicetree fragment:
1965+
*
1966+
* n1: node-1 {
1967+
* compatible = "vnd,model1", "gpio", "zephyr,model2";
1968+
* };
1969+
*
1970+
* Example usage:
1971+
*
1972+
* DT_NODE_MODEL_BY_IDX(DT_NODELABEL(n1), 0) // "model1"
1973+
* DT_NODE_MODEL_BY_IDX(DT_NODELABEL(n1), 2) // "model2"
1974+
*
1975+
* Notice that the compatible at index 1 doesn't match any entries in the
1976+
* vendor prefix file and therefore index 1 is not a valid model index. Use
1977+
* DT_NODE_MODEL_HAS_IDX(node_id, idx) to determine if an index is valid.
1978+
*
1979+
* @param node_id node identifier
1980+
* @param idx index of the model to return
1981+
* @return string literal of the idx-th model
1982+
*/
1983+
#define DT_NODE_MODEL_BY_IDX(node_id, idx) \
1984+
DT_CAT3(node_id, _COMPAT_MODEL_IDX_, idx)
1985+
1986+
/**
1987+
* @brief Does a node's compatible property have a model at an index?
1988+
*
1989+
* If this returns 1, then DT_NODE_MODEL_BY_IDX(node_id, idx) is valid. If it
1990+
* returns 0, it is an error to use DT_NODE_MODEL_BY_IDX(node_id, idx) with
1991+
* index "idx".
1992+
*
1993+
* @param node_id node identifier
1994+
* @param idx index of the model to check
1995+
* @return 1 if "idx" is a valid model index,
1996+
* 0 otherwise.
1997+
*/
1998+
#define DT_NODE_MODEL_HAS_IDX(node_id, idx) \
1999+
IS_ENABLED(DT_CAT4(node_id, _COMPAT_MODEL_IDX_, idx, _EXISTS))
2000+
2001+
/**
2002+
* @brief Like DT_NODE_MODEL_BY_IDX(), but with a fallback to default_value.
2003+
*
2004+
* If the value exists, this expands to DT_NODE_MODEL_BY_IDX(node_id, idx).
2005+
* The default_value parameter is not expanded in this case.
2006+
*
2007+
* Otherwise, this expands to default_value.
2008+
*
2009+
* @param node_id node identifier
2010+
* @param idx index of the model to return
2011+
* @return string literal of the idx-th model
2012+
* @param default_value a fallback value to expand to
2013+
* @return string literal of the idx-th model or "default_value"
2014+
*/
2015+
#define DT_NODE_MODEL_BY_IDX_OR(node_id, idx, default_value) \
2016+
COND_CODE_1(DT_NODE_MODEL_HAS_IDX(node_id, idx), \
2017+
(DT_NODE_MODEL_BY_IDX(node_id, idx)), (default_value))
2018+
2019+
/**
2020+
* @brief Get the node's (only) model as a string literal
2021+
*
2022+
* Equivalent to DT_NODE_MODEL_BY_IDX_OR(node_id, 0, default_value).
2023+
*
2024+
* @param node_id node identifier
2025+
* @param default_value a fallback value to expand to
2026+
*/
2027+
#define DT_NODE_MODEL_OR(node_id, default_value) \
2028+
DT_NODE_MODEL_BY_IDX_OR(node_id, 0, default_value)
2029+
19542030
/**
19552031
* @}
19562032
*/

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define TEST_TEMP DT_NODELABEL(test_temp_sensor)
3838
#define TEST_REG DT_NODELABEL(test_reg)
3939
#define TEST_VENDOR DT_NODELABEL(test_vendor)
40+
#define TEST_MODEL DT_NODELABEL(test_vendor)
4041
#define TEST_ENUM_0 DT_NODELABEL(test_enum_0)
4142

4243
#define TEST_I2C DT_NODELABEL(test_i2c)
@@ -475,6 +476,34 @@ ZTEST(devicetree_api, test_vendor)
475476
zassert_true(!strcmp(DT_NODE_VENDOR_OR(TEST_VENDOR, NULL), VND_VENDOR), "");
476477
}
477478

479+
#define VND_MODEL "model1"
480+
#define ZEP_MODEL "model2"
481+
482+
ZTEST(devicetree_api, test_model)
483+
{
484+
/* DT_NODE_MODEL_HAS_IDX */
485+
zassert_true(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 0), "");
486+
zassert_false(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 1), "");
487+
zassert_true(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 2), "");
488+
zassert_false(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 3), "");
489+
490+
/* DT_NODE_MODEL_BY_IDX */
491+
zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX(TEST_MODEL, 0), VND_MODEL), "");
492+
zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX(TEST_MODEL, 2), ZEP_MODEL), "");
493+
494+
/* DT_NODE_MODEL_BY_IDX_OR */
495+
zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 0, NULL), VND_MODEL), "");
496+
zassert_is_null(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 1, NULL), "");
497+
zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 2, NULL), ZEP_MODEL), "");
498+
zassert_is_null(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 3, NULL), "");
499+
500+
/* DT_NODE_MODEL_OR */
501+
zassert_true(!strcmp(DT_NODE_MODEL_OR(TEST_MODEL, NULL), VND_MODEL), "");
502+
}
503+
504+
#undef ZEP_MODEL
505+
#undef VND_MODEL
506+
478507
#undef DT_DRV_COMPAT
479508
#define DT_DRV_COMPAT vnd_reg_holder
480509
ZTEST(devicetree_api, test_reg)

0 commit comments

Comments
 (0)