|
1864 | 1864 | */
|
1865 | 1865 |
|
1866 | 1866 | /**
|
1867 |
| - * @defgroup devicetree-generic-vendor Vendor name helpers |
| 1867 | + * @defgroup devicetree-generic-vendor Vendor and model name helpers |
1868 | 1868 | * @ingroup devicetree
|
1869 | 1869 | * @{
|
1870 | 1870 | */
|
|
1951 | 1951 | #define DT_NODE_VENDOR_OR(node_id, default_value) \
|
1952 | 1952 | DT_NODE_VENDOR_BY_IDX_OR(node_id, 0, default_value)
|
1953 | 1953 |
|
| 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 | + |
1954 | 2030 | /**
|
1955 | 2031 | * @}
|
1956 | 2032 | */
|
|
0 commit comments