Skip to content

Commit

Permalink
drivers: gpio: GPIO_DT_SPEC_GET_BY_IDX_OR: use default if idx invalid
Browse files Browse the repository at this point in the history
It is not sufficient to check if the given node_id has the requested
property in GPIO_DT_SPEC_GET_BY_IDX_OR(), the index also needs to valid.

Change the logic to test for a valid index (which, in turn, also checks if
the property is valid), otherwise return the default value provided by the
caller.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
  • Loading branch information
henrikbrixandersen committed Dec 30, 2024
1 parent 3c420a4 commit ee250a1
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/zephyr/drivers/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ struct gpio_dt_spec {
* @brief Like GPIO_DT_SPEC_GET_BY_IDX(), with a fallback to a default value
*
* If the devicetree node identifier 'node_id' refers to a node with a
* property 'prop', this expands to
* <tt>GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)</tt>. The @p
* property 'prop', and the index @p idx is valid for that property,
* this expands to <tt>GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)</tt>. The @p
* default_value parameter is not expanded in this case.
*
* Otherwise, this expands to @p default_value.
Expand All @@ -354,7 +354,7 @@ struct gpio_dt_spec {
* or default_value if the node or property do not exist
*/
#define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, idx), \
(GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
(default_value))

Expand Down

0 comments on commit ee250a1

Please sign in to comment.