-
Notifications
You must be signed in to change notification settings - Fork 3
/
zephyr-gpio-get-direction.patch
68 lines (63 loc) · 1.93 KB
/
zephyr-gpio-get-direction.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
commit a1a2733f88044ae1ef81be5cc5852dda1ffeda42
Author: Christopher Friedt <chrisfriedt@gmail.com>
Date: Fri Oct 9 12:52:28 2020 -0400
gpio: query pin direction
diff --git a/include/drivers/gpio.h b/include/drivers/gpio.h
index 44f893a615..43fbf7d01e 100644
--- a/include/drivers/gpio.h
+++ b/include/drivers/gpio.h
@@ -334,6 +334,13 @@ struct gpio_driver_data {
* wrapper functions in this header.
*/
gpio_port_pins_t invert;
+ /*
+ * Mask identifying pins that are configured as output
+ *
+ * Management of this mask is the responsibility of the
+ * wrapper functions in this header.
+ */
+ gpio_port_pins_t output;
};
struct gpio_callback;
@@ -585,6 +592,12 @@ static inline int gpio_pin_configure(const struct device *port,
return ret;
}
+ if (flags & GPIO_OUTPUT) {
+ data->output |= BIT(pin);
+ } else if (flags & GPIO_INPUT) {
+ data->output &= ~BIT(pin);
+ }
+
if ((flags & GPIO_ACTIVE_LOW) != 0) {
data->invert |= (gpio_port_pins_t)BIT(pin);
} else {
@@ -599,6 +612,30 @@ static inline int gpio_pin_configure(const struct device *port,
return ret;
}
+/**
+ * @brief Get physical direction of a single @p pin in a @p port
+ *
+ * This function returns true if the given pin is configured as
+ * @ref GPIO_OUTPUT. Otherwise, this function returns false.
+ *
+ * @param port Pointer to device structure for the driver instance.
+ * @param pin Pin number to query the direction of
+ * @return the direction of the pin
+ */
+static inline bool gpio_pin_get_direction(const struct device *port, gpio_pin_t pin)
+{
+ const struct gpio_driver_config *const cfg =
+ (const struct gpio_driver_config *)port->config;
+ struct gpio_driver_data *const data =
+ (struct gpio_driver_data *)port->data;
+
+ (void)cfg;
+ __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
+ "Unsupported pin");
+
+ return (bool)!!(BIT(pin) & data->output);
+}
+
/**
* @brief Get physical level of all input pins in a port.
*