From 7ea37c9bb7b56c2ea3b795c51541b2f3867d5e02 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Wed, 23 Jun 2021 20:47:18 +1000 Subject: [PATCH] gpio: add additional `gpio_spec_dt` helpers Add helpers to allow providing a `struct gpio_spec_dt` directly to `gpio_pin_get`, `gpio_pin_set` and `gpio_pin_toggle` functions. Signed-off-by: Jordan Yates --- include/drivers/gpio.h | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/include/drivers/gpio.h b/include/drivers/gpio.h index 021a4e5e018..25899f9a85d 100644 --- a/include/drivers/gpio.h +++ b/include/drivers/gpio.h @@ -1115,6 +1115,21 @@ static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin) return ret; } +/** + * @brief Get logical level of an input pin from a @p gpio_dt_spec. + * + * This is equivalent to: + * + * gpio_pin_get(spec->port, spec->pin); + * + * @param spec GPIO specification from devicetree + * @retval a value from gpio_pin_get() + */ +static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec) +{ + return gpio_pin_get(spec->port, spec->pin); +} + /** * @brief Set physical level of an output pin. * @@ -1190,6 +1205,22 @@ static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin, return gpio_pin_set_raw(port, pin, value); } +/** + * @brief Set logical level of a output pin from a @p gpio_dt_spec. + * + * This is equivalent to: + * + * gpio_pin_set(spec->port, spec->pin, value); + * + * @param spec GPIO specification from devicetree + * @param value Value assigned to the pin. + * @retval a value from gpio_pin_set() + */ +static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value) +{ + return gpio_pin_set(spec->port, spec->pin, value); +} + /** * @brief Toggle pin level. * @@ -1212,6 +1243,21 @@ static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin) return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin)); } +/** + * @brief Toggle pin level from a @p gpio_dt_spec. + * + * This is equivalent to: + * + * gpio_pin_toggle(spec->port, spec->pin); + * + * @param spec GPIO specification from devicetree + * @retval a value from gpio_pin_toggle() + */ +static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec) +{ + return gpio_pin_toggle(spec->port, spec->pin); +} + /** * @brief Helper to initialize a struct gpio_callback properly * @param callback A valid Application's callback structure pointer.