Line data Source code
1 1 : /*
2 : * Copyright (c) 2020 Friedt Professional Engineering Services, Inc
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : /**
8 : * @file
9 : * @brief Backend API for emulated GPIO
10 : */
11 :
12 : #ifndef ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_EMUL_H_
13 : #define ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_EMUL_H_
14 :
15 : #include <zephyr/types.h>
16 : #include <zephyr/drivers/gpio.h>
17 :
18 : #ifdef __cplusplus
19 : extern "C" {
20 : #endif
21 :
22 : /**
23 : * @brief Emulated GPIO backend API
24 : * @defgroup gpio_emul Emulated GPIO
25 : * @ingroup io_emulators
26 : * @ingroup gpio_interface
27 : * @{
28 : *
29 : * Behaviour of emulated GPIO is application-defined. As-such, each
30 : * application may
31 : *
32 : * - define a Device Tree overlay file to indicate the number of GPIO
33 : * controllers as well as the number of pins for each controller
34 : * - register a callback with the GPIO controller using
35 : * @ref gpio_add_callback to emulate "wiring"
36 : * - asynchronously call @ref gpio_emul_input_set and / or
37 : * @ref gpio_emul_input_set_masked in order to emulate GPIO events
38 : *
39 : * An example of an appropriate Device Tree overlay file is in
40 : * tests/drivers/gpio/gpio_basic_api/boards/native_sim.overlay.
41 : *
42 : * An example of registering a callback to emulate "wiring" as well as
43 : * an example of calling @ref gpio_emul_input_set is in the file
44 : * tests/drivers/gpio/gpio_basic_api/src/main.c .
45 : */
46 :
47 : /**
48 : * @brief Modify the values of one or more emulated GPIO input @p pins
49 : *
50 : * @param port The emulated GPIO port
51 : * @param pins The mask of pins that have changed
52 : * @param values New values to assign to @p pins
53 : *
54 : * @return 0 on success
55 : * @return -EINVAL if an invalid argument is provided
56 : */
57 1 : int gpio_emul_input_set_masked(const struct device *port, gpio_port_pins_t pins,
58 : gpio_port_value_t values);
59 :
60 : /**
61 : * @brief Modify the value of one emulated GPIO input @p pin
62 : *
63 : * @param port The emulated GPIO port
64 : * @param pin The pin to modify
65 : * @param value New values to assign to @p pin
66 : *
67 : * @return 0 on success
68 : * @return -EINVAL if an invalid argument is provided
69 : */
70 1 : static inline int gpio_emul_input_set(const struct device *port, gpio_pin_t pin,
71 : int value)
72 : {
73 : return gpio_emul_input_set_masked(port, BIT(pin), value ? BIT(pin) : 0);
74 : }
75 :
76 : /**
77 : * @brief Read the value of one or more emulated GPIO output @p pins
78 : *
79 : * @param port The emulated GPIO port
80 : * @param pins The mask of pins that have changed
81 : * @param values A pointer to where the value of @p pins will be stored
82 : *
83 : * @return 0 on success
84 : * @return -EINVAL if an invalid argument is provided
85 : */
86 1 : int gpio_emul_output_get_masked(const struct device *port, gpio_port_pins_t pins,
87 : gpio_port_value_t *values);
88 :
89 : /**
90 : * @brief Read the value of one emulated GPIO output @p pin
91 : *
92 : * @param port The emulated GPIO port
93 : * @param pin The pin to read
94 : *
95 : * @return 0 or 1 on success
96 : * @return -EINVAL if an invalid argument is provided
97 : */
98 1 : static inline int gpio_emul_output_get(const struct device *port, gpio_pin_t pin)
99 : {
100 : int ret;
101 : gpio_port_value_t values;
102 :
103 : ret = gpio_emul_output_get_masked(port, BIT(pin), &values);
104 : if (ret == 0) {
105 : ret = (values & BIT(pin)) ? 1 : 0;
106 : }
107 :
108 : return ret;
109 : }
110 :
111 : /**
112 : * @brief Get @p flags for a given emulated GPIO @p pin
113 : *
114 : * For more information on available flags, see @ref gpio_interface.
115 : *
116 : * @param port The emulated GPIO port
117 : * @param pin The pin to retrieve @p flags for
118 : * @param flags a pointer to where the flags for @p pin will be stored
119 : *
120 : * @return 0 on success
121 : * @return -EINVAL if an invalid argument is provided
122 : */
123 1 : int gpio_emul_flags_get(const struct device *port, gpio_pin_t pin, gpio_flags_t *flags);
124 :
125 : /**
126 : * @}
127 : */
128 :
129 : #ifdef __cplusplus
130 : }
131 : #endif
132 :
133 : #endif /* ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_EMUL_H_ */
|