From b6fb701f894812aa2f024308e1345480c80140c0 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Sat, 15 Apr 2023 11:35:40 +1000 Subject: [PATCH] devicetree: add `DT_ENUM_HAS_VALUE` Add a macro for checking if an enumeration matches a given value. This enables code to directly check whether a string enumeration is a specific value, without needing to construct an intermediate variable, and without checking against a index value which may change. ``` /* Enables this */ if (DT_INST_ENUM_HAS_VALUE(0, power_amplifier_output, rfo_hp)) {} /* Instead of this */ if (DT_INST_ENUM_IDX(0, power_amplifier_output) == 0) {} ``` Signed-off-by: Jordan Yates --- include/zephyr/devicetree.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/zephyr/devicetree.h b/include/zephyr/devicetree.h index e77652f7d0..8111717c02 100644 --- a/include/zephyr/devicetree.h +++ b/include/zephyr/devicetree.h @@ -39,6 +39,7 @@ * are missing from this list, please add them. It should be complete. * * _ENUM_IDX: property's value as an index into bindings enum + * _ENUM_VAL__EXISTS property's value as a token exists * _ENUM_TOKEN: property's value as a token into bindings enum (string * enum values are identifiers) [deprecated, use _STRING_TOKEN] * _ENUM_UPPER_TOKEN: like _ENUM_TOKEN, but uppercased [deprecated, use @@ -827,6 +828,17 @@ COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \ (DT_ENUM_IDX(node_id, prop)), (default_idx_value)) +/** + * @brief Does a node enumeration property have a given value? + * + * @param node_id node identifier + * @param prop lowercase-and-underscores property name + * @param value lowercase-and-underscores enumeration value + * @return 1 if the node property has the value @a value, 0 otherwise. + */ +#define DT_ENUM_HAS_VALUE(node_id, prop, value) \ + IS_ENABLED(DT_CAT6(node_id, _P_, prop, _ENUM_VAL_, value, _EXISTS)) + /** * @brief Get a string property's value as a token. * @@ -3336,6 +3348,17 @@ #define DT_INST_ENUM_IDX_OR(inst, prop, default_idx_value) \ DT_ENUM_IDX_OR(DT_DRV_INST(inst), prop, default_idx_value) +/** + * @brief Does a `DT_DRV_COMPAT` enumeration property have a given value? + * + * @param inst instance number + * @param prop lowercase-and-underscores property name + * @param value lowercase-and-underscores enumeration value + * @return 1 if the node property has the value @a value, 0 otherwise. + */ +#define DT_INST_ENUM_HAS_VALUE(inst, prop, value) \ + DT_ENUM_HAS_VALUE(DT_DRV_INST(inst), prop, value) + /** * @brief Get a `DT_DRV_COMPAT` instance property * @param inst instance number