Zephyr Project API 4.2.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019-2020 Nordic Semiconductor ASA
3 * Copyright (c) 2019 Piotr Mienkowski
4 * Copyright (c) 2017 ARM Ltd
5 * Copyright (c) 2015-2016 Intel Corporation.
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
16#ifndef ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
17#define ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
18
19#include <errno.h>
20
21#include <zephyr/sys/__assert.h>
22#include <zephyr/sys/slist.h>
24
25#include <zephyr/types.h>
26#include <stddef.h>
27#include <zephyr/device.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
54#define GPIO_INPUT BIT(16)
55
57#define GPIO_OUTPUT BIT(17)
58
60#define GPIO_DISCONNECTED 0
61
64/* Initializes output to a low state. */
65#define GPIO_OUTPUT_INIT_LOW BIT(18)
66
67/* Initializes output to a high state. */
68#define GPIO_OUTPUT_INIT_HIGH BIT(19)
69
70/* Initializes output based on logic level */
71#define GPIO_OUTPUT_INIT_LOGICAL BIT(20)
72
76#define GPIO_OUTPUT_LOW (GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW)
78#define GPIO_OUTPUT_HIGH (GPIO_OUTPUT | GPIO_OUTPUT_INIT_HIGH)
80#define GPIO_OUTPUT_INACTIVE (GPIO_OUTPUT | \
81 GPIO_OUTPUT_INIT_LOW | \
82 GPIO_OUTPUT_INIT_LOGICAL)
84#define GPIO_OUTPUT_ACTIVE (GPIO_OUTPUT | \
85 GPIO_OUTPUT_INIT_HIGH | \
86 GPIO_OUTPUT_INIT_LOGICAL)
87
107#define GPIO_INT_DISABLE BIT(21)
108
111/* Enables GPIO pin interrupt. */
112#define GPIO_INT_ENABLE BIT(22)
113
114/* GPIO interrupt is sensitive to logical levels.
115 *
116 * This is a component flag that should be combined with other
117 * `GPIO_INT_*` flags to produce a meaningful configuration.
118 */
119#define GPIO_INT_LEVELS_LOGICAL BIT(23)
120
121/* GPIO interrupt is edge sensitive.
122 *
123 * Note: by default interrupts are level sensitive.
124 *
125 * This is a component flag that should be combined with other
126 * `GPIO_INT_*` flags to produce a meaningful configuration.
127 */
128#define GPIO_INT_EDGE BIT(24)
129
130/* Trigger detection when input state is (or transitions to) physical low or
131 * logical 0 level.
132 *
133 * This is a component flag that should be combined with other
134 * `GPIO_INT_*` flags to produce a meaningful configuration.
135 */
136#define GPIO_INT_LOW_0 BIT(25)
137
138/* Trigger detection on input state is (or transitions to) physical high or
139 * logical 1 level.
140 *
141 * This is a component flag that should be combined with other
142 * `GPIO_INT_*` flags to produce a meaningful configuration.
143 */
144#define GPIO_INT_HIGH_1 BIT(26)
145
146#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
147/* Disable/Enable interrupt functionality without changing other interrupt
148 * related register, such as clearing the pending register.
149 *
150 * This is a component flag that should be combined with `GPIO_INT_ENABLE` or
151 * `GPIO_INT_DISABLE` flags to produce a meaningful configuration.
152 */
153#define GPIO_INT_ENABLE_DISABLE_ONLY BIT(27)
154#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
155
156#define GPIO_INT_MASK (GPIO_INT_DISABLE | \
157 GPIO_INT_ENABLE | \
158 GPIO_INT_LEVELS_LOGICAL | \
159 GPIO_INT_EDGE | \
160 GPIO_INT_LOW_0 | \
161 GPIO_INT_HIGH_1)
162
167#define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
168 GPIO_INT_EDGE | \
169 GPIO_INT_HIGH_1)
170
174#define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
175 GPIO_INT_EDGE | \
176 GPIO_INT_LOW_0)
177
181#define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
182 GPIO_INT_EDGE | \
183 GPIO_INT_LOW_0 | \
184 GPIO_INT_HIGH_1)
185
189#define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
190 GPIO_INT_LOW_0)
191
195#define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
196 GPIO_INT_HIGH_1)
197
201#define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
202 GPIO_INT_LEVELS_LOGICAL | \
203 GPIO_INT_EDGE | \
204 GPIO_INT_LOW_0)
205
209#define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
210 GPIO_INT_LEVELS_LOGICAL | \
211 GPIO_INT_EDGE | \
212 GPIO_INT_HIGH_1)
213
217#define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
218 GPIO_INT_LEVELS_LOGICAL | \
219 GPIO_INT_LOW_0)
220
224#define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
225 GPIO_INT_LEVELS_LOGICAL | \
226 GPIO_INT_HIGH_1)
227
231#define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
241
254
262
274
282
303
338#define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
339 { \
340 .port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)),\
341 .pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
342 .dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
343 }
344
362#define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
363 COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, idx), \
364 (GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
365 (default_value))
366
375#define GPIO_DT_SPEC_GET(node_id, prop) \
376 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
377
388#define GPIO_DT_SPEC_GET_OR(node_id, prop, default_value) \
389 GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
390
401#define GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
402 GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
403
415#define GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
416 COND_CODE_1(DT_PROP_HAS_IDX(DT_DRV_INST(inst), prop, idx), \
417 (GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)), \
418 (default_value))
419
428#define GPIO_DT_SPEC_INST_GET(inst, prop) \
429 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
430
441#define GPIO_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
442 GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value)
443
444/*
445 * @cond INTERNAL_HIDDEN
446 */
447
458#define Z_GPIO_GEN_BITMASK_COND(node_id, prop, off_idx, sz_idx) \
459 COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, off_idx), \
460 (COND_CODE_0(DT_PROP_BY_IDX(node_id, prop, sz_idx), \
461 (0), \
462 (GENMASK64(DT_PROP_BY_IDX(node_id, prop, off_idx) + \
463 DT_PROP_BY_IDX(node_id, prop, sz_idx) - 1, \
464 DT_PROP_BY_IDX(node_id, prop, off_idx)))) \
465 ), (0))
466
474#define Z_GPIO_GEN_RESERVED_RANGES_COND(odd_it, node_id) \
475 COND_CODE_1(DT_PROP_HAS_IDX(node_id, gpio_reserved_ranges, odd_it), \
476 (Z_GPIO_GEN_BITMASK_COND(node_id, \
477 gpio_reserved_ranges, \
478 GET_ARG_N(odd_it, Z_SPARSE_LIST_EVEN_NUMBERS), \
479 odd_it)), \
480 (0))
481
573#define GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios) \
574 ((gpio_port_pins_t) \
575 COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
576 (GENMASK64(BITS_PER_LONG_LONG - 1, ngpios) \
577 | FOR_EACH_FIXED_ARG(Z_GPIO_GEN_RESERVED_RANGES_COND, \
578 (|), \
579 node_id, \
580 LIST_DROP_EMPTY(Z_SPARSE_LIST_ODD_NUMBERS))), \
581 (0)))
582
590#define GPIO_DT_RESERVED_RANGES(node_id) \
591 GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, DT_PROP(node_id, ngpios))
592
602#define GPIO_DT_INST_RESERVED_RANGES_NGPIOS(inst, ngpios) \
603 GPIO_DT_RESERVED_RANGES_NGPIOS(DT_DRV_INST(inst), ngpios)
604
613#define GPIO_DT_INST_RESERVED_RANGES(inst) \
614 GPIO_DT_RESERVED_RANGES(DT_DRV_INST(inst))
615
664#define GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(node_id, ngpios) \
665 ((gpio_port_pins_t) \
666 COND_CODE_0(ngpios, \
667 (0), \
668 (COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
669 ((GENMASK64(ngpios - 1, 0) & \
670 ~GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios))), \
671 (GENMASK64(ngpios - 1, 0))) \
672 ) \
673 ))
674
684#define GPIO_DT_INST_PORT_PIN_MASK_NGPIOS_EXC(inst, ngpios) \
685 GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(DT_DRV_INST(inst), ngpios)
686
690#define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
691
705
718
719struct gpio_callback;
720
733typedef void (*gpio_callback_handler_t)(const struct device *port,
734 struct gpio_callback *cb,
735 gpio_port_pins_t pins);
736
764
771/* Used by driver api function pin_interrupt_configure, these are defined
772 * in terms of the public flags so we can just mask and pass them
773 * through to the driver api
774 */
775enum gpio_int_mode {
776 GPIO_INT_MODE_DISABLED = GPIO_INT_DISABLE,
777 GPIO_INT_MODE_LEVEL = GPIO_INT_ENABLE,
778 GPIO_INT_MODE_EDGE = GPIO_INT_ENABLE | GPIO_INT_EDGE,
779#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
780 GPIO_INT_MODE_DISABLE_ONLY = GPIO_INT_DISABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
781 GPIO_INT_MODE_ENABLE_ONLY = GPIO_INT_ENABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
782#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
783};
784
785enum gpio_int_trig {
786 /* Trigger detection when input state is (or transitions to)
787 * physical low. (Edge Falling or Active Low)
788 */
789 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
790 /* Trigger detection when input state is (or transitions to)
791 * physical high. (Edge Rising or Active High) */
792 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
793 /* Trigger detection on pin rising or falling edge. */
794 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
795 /* Trigger a system wakeup. */
796 GPIO_INT_TRIG_WAKE = GPIO_INT_WAKEUP,
797};
798
799__subsystem struct gpio_driver_api {
800 int (*pin_configure)(const struct device *port, gpio_pin_t pin,
802#ifdef CONFIG_GPIO_GET_CONFIG
803 int (*pin_get_config)(const struct device *port, gpio_pin_t pin,
805#endif
806 int (*port_get_raw)(const struct device *port,
807 gpio_port_value_t *value);
808 int (*port_set_masked_raw)(const struct device *port,
809 gpio_port_pins_t mask,
810 gpio_port_value_t value);
811 int (*port_set_bits_raw)(const struct device *port,
812 gpio_port_pins_t pins);
813 int (*port_clear_bits_raw)(const struct device *port,
814 gpio_port_pins_t pins);
815 int (*port_toggle_bits)(const struct device *port,
816 gpio_port_pins_t pins);
817 int (*pin_interrupt_configure)(const struct device *port,
818 gpio_pin_t pin,
819 enum gpio_int_mode mode,
820 enum gpio_int_trig trig);
821 int (*manage_callback)(const struct device *port,
822 struct gpio_callback *cb,
823 bool set);
824 uint32_t (*get_pending_int)(const struct device *dev);
825#ifdef CONFIG_GPIO_GET_DIRECTION
826 int (*port_get_direction)(const struct device *port, gpio_port_pins_t map,
827 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
828#endif /* CONFIG_GPIO_GET_DIRECTION */
829};
830
843static inline bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
844{
845 /* Validate port is ready */
846 return device_is_ready(spec->port);
847}
848
872__syscall int gpio_pin_interrupt_configure(const struct device *port,
873 gpio_pin_t pin,
875
876static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
877 gpio_pin_t pin,
879{
880 const struct gpio_driver_api *api =
881 (const struct gpio_driver_api *)port->api;
882 __unused const struct gpio_driver_config *const cfg =
883 (const struct gpio_driver_config *)port->config;
884 const struct gpio_driver_data *const data =
885 (const struct gpio_driver_data *)port->data;
886 enum gpio_int_trig trig;
887 enum gpio_int_mode mode;
888 int ret;
889
890 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, interrupt_configure, port, pin, flags);
891
892 if (api->pin_interrupt_configure == NULL) {
893 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, interrupt_configure, port, pin, -ENOSYS);
894 return -ENOSYS;
895 }
896
897 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE))
898 != (GPIO_INT_DISABLE | GPIO_INT_ENABLE),
899 "Cannot both enable and disable interrupts");
900
901 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE)) != 0U,
902 "Must either enable or disable interrupts");
903
904 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
905 ((flags & GPIO_INT_EDGE) != 0) ||
906 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
907 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
908 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
909 "enabled for a level interrupt.");
910
911#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
912#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE GPIO_INT_ENABLE_DISABLE_ONLY
913#else
914#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE 0
915#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
916
917 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
918 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
920 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be enabled.");
921#undef GPIO_INT_ENABLE_DISABLE_ONLY_VALUE
922 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
923 "Unsupported pin");
924
925 if (((flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
926 ((data->invert & (gpio_port_pins_t)BIT(pin)) != 0)) {
927 /* Invert signal bits */
928 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
929 }
930
931 trig = (enum gpio_int_trig)(flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1 | GPIO_INT_WAKEUP));
932#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
933 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE |
934 GPIO_INT_ENABLE_DISABLE_ONLY));
935#else
936 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE));
937#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
938
939 ret = api->pin_interrupt_configure(port, pin, mode, trig);
940 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, interrupt_configure, port, pin, ret);
941 return ret;
942}
943
959static inline int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec,
961{
962 return gpio_pin_interrupt_configure(spec->port, spec->pin, flags);
963}
964
980__syscall int gpio_pin_configure(const struct device *port,
981 gpio_pin_t pin,
983
984static inline int z_impl_gpio_pin_configure(const struct device *port,
985 gpio_pin_t pin,
987{
988 const struct gpio_driver_api *api =
989 (const struct gpio_driver_api *)port->api;
990 __unused const struct gpio_driver_config *const cfg =
991 (const struct gpio_driver_config *)port->config;
992 struct gpio_driver_data *data =
993 (struct gpio_driver_data *)port->data;
994 int ret;
995
996 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, configure, port, pin, flags);
997
998 __ASSERT((flags & GPIO_INT_MASK) == 0,
999 "Interrupt flags are not supported");
1000
1001 __ASSERT((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) !=
1003 "Pull Up and Pull Down should not be enabled simultaneously");
1004
1005 __ASSERT(!((flags & GPIO_INPUT) && !(flags & GPIO_OUTPUT) && (flags & GPIO_SINGLE_ENDED)),
1006 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
1007
1008 __ASSERT_NO_MSG((flags & GPIO_SINGLE_ENDED) != 0 ||
1009 (flags & GPIO_LINE_OPEN_DRAIN) == 0);
1010
1011 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
1012 || (flags & GPIO_OUTPUT) != 0,
1013 "Output needs to be enabled to be initialized low or high");
1014
1015 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
1016 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
1017 "Output cannot be initialized low and high");
1018
1019 if (((flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
1020 && ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
1021 && ((flags & GPIO_ACTIVE_LOW) != 0)) {
1022 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
1023 }
1024
1025 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
1026
1027 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1028 "Unsupported pin");
1029
1030 if ((flags & GPIO_ACTIVE_LOW) != 0) {
1031 data->invert |= (gpio_port_pins_t)BIT(pin);
1032 } else {
1033 data->invert &= ~(gpio_port_pins_t)BIT(pin);
1034 }
1035
1036 ret = api->pin_configure(port, pin, flags);
1037 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, configure, port, pin, ret);
1038 return ret;
1039}
1040
1052static inline int gpio_pin_configure_dt(const struct gpio_dt_spec *spec,
1053 gpio_flags_t extra_flags)
1054{
1055 return gpio_pin_configure(spec->port,
1056 spec->pin,
1057 spec->dt_flags | extra_flags);
1058}
1059
1078__syscall int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1079 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
1080
1081#ifdef CONFIG_GPIO_GET_DIRECTION
1082static inline int z_impl_gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1083 gpio_port_pins_t *inputs,
1084 gpio_port_pins_t *outputs)
1085{
1086 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1087 int ret;
1088
1089 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, get_direction, port, map, inputs, outputs);
1090
1091 if (api->port_get_direction == NULL) {
1092 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_direction, port, -ENOSYS);
1093 return -ENOSYS;
1094 }
1095
1096 ret = api->port_get_direction(port, map, inputs, outputs);
1097 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_direction, port, ret);
1098 return ret;
1099}
1100#endif /* CONFIG_GPIO_GET_DIRECTION */
1101
1114static inline int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
1115{
1116 int rv;
1117 gpio_port_pins_t pins;
1118 __unused const struct gpio_driver_config *cfg =
1119 (const struct gpio_driver_config *)port->config;
1120
1121 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1122
1123 rv = gpio_port_get_direction(port, BIT(pin), &pins, NULL);
1124 if (rv < 0) {
1125 return rv;
1126 }
1127
1128 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1129}
1130
1142static inline int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
1143{
1144 return gpio_pin_is_input(spec->port, spec->pin);
1145}
1146
1159static inline int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
1160{
1161 int rv;
1162 gpio_port_pins_t pins;
1163 __unused const struct gpio_driver_config *cfg =
1164 (const struct gpio_driver_config *)port->config;
1165
1166 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1167
1168 rv = gpio_port_get_direction(port, BIT(pin), NULL, &pins);
1169 if (rv < 0) {
1170 return rv;
1171 }
1172
1173 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1174}
1175
1187static inline int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
1188{
1189 return gpio_pin_is_output(spec->port, spec->pin);
1190}
1191
1207__syscall int gpio_pin_get_config(const struct device *port, gpio_pin_t pin,
1209
1210#ifdef CONFIG_GPIO_GET_CONFIG
1211static inline int z_impl_gpio_pin_get_config(const struct device *port,
1212 gpio_pin_t pin,
1214{
1215 const struct gpio_driver_api *api =
1216 (const struct gpio_driver_api *)port->api;
1217 int ret;
1218
1219 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, get_config, port, pin, *flags);
1220
1221 if (api->pin_get_config == NULL) {
1222 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, get_config, port, pin, -ENOSYS);
1223 return -ENOSYS;
1224 }
1225
1226 ret = api->pin_get_config(port, pin, flags);
1227 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, get_config, port, pin, ret);
1228 return ret;
1229}
1230#endif
1231
1244static inline int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec,
1246{
1247 return gpio_pin_get_config(spec->port, spec->pin, flags);
1248}
1249
1267__syscall int gpio_port_get_raw(const struct device *port,
1268 gpio_port_value_t *value);
1269
1270static inline int z_impl_gpio_port_get_raw(const struct device *port, gpio_port_value_t *value)
1271{
1272 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1273 int ret;
1274
1275 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, get_raw, port, value);
1276
1277 ret = api->port_get_raw(port, value);
1278 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_raw, port, ret);
1279 return ret;
1280}
1281
1300static inline int gpio_port_get(const struct device *port,
1301 gpio_port_value_t *value)
1302{
1303 const struct gpio_driver_data *const data =
1304 (const struct gpio_driver_data *)port->data;
1305 int ret;
1306
1307 ret = gpio_port_get_raw(port, value);
1308 if (ret == 0) {
1309 *value ^= data->invert;
1310 }
1311
1312 return ret;
1313}
1314
1332__syscall int gpio_port_set_masked_raw(const struct device *port,
1333 gpio_port_pins_t mask,
1334 gpio_port_value_t value);
1335
1336static inline int z_impl_gpio_port_set_masked_raw(const struct device *port,
1337 gpio_port_pins_t mask,
1338 gpio_port_value_t value)
1339{
1340 const struct gpio_driver_api *api =
1341 (const struct gpio_driver_api *)port->api;
1342 int ret;
1343
1344 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, set_masked_raw, port, mask, value);
1345
1346 ret = api->port_set_masked_raw(port, mask, value);
1347 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, set_masked_raw, port, ret);
1348 return ret;
1349}
1350
1371static inline int gpio_port_set_masked(const struct device *port,
1372 gpio_port_pins_t mask,
1373 gpio_port_value_t value)
1374{
1375 const struct gpio_driver_data *const data =
1376 (const struct gpio_driver_data *)port->data;
1377
1378 value ^= data->invert;
1379
1380 return gpio_port_set_masked_raw(port, mask, value);
1381}
1382
1393__syscall int gpio_port_set_bits_raw(const struct device *port,
1394 gpio_port_pins_t pins);
1395
1396static inline int z_impl_gpio_port_set_bits_raw(const struct device *port,
1397 gpio_port_pins_t pins)
1398{
1399 const struct gpio_driver_api *api =
1400 (const struct gpio_driver_api *)port->api;
1401 int ret;
1402
1403 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, set_bits_raw, port, pins);
1404
1405 ret = api->port_set_bits_raw(port, pins);
1406 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, set_bits_raw, port, ret);
1407 return ret;
1408}
1409
1420static inline int gpio_port_set_bits(const struct device *port,
1421 gpio_port_pins_t pins)
1422{
1423 return gpio_port_set_masked(port, pins, pins);
1424}
1425
1436__syscall int gpio_port_clear_bits_raw(const struct device *port,
1437 gpio_port_pins_t pins);
1438
1439static inline int z_impl_gpio_port_clear_bits_raw(const struct device *port,
1440 gpio_port_pins_t pins)
1441{
1442 const struct gpio_driver_api *api =
1443 (const struct gpio_driver_api *)port->api;
1444 int ret;
1445
1446 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, clear_bits_raw, port, pins);
1447
1448 ret = api->port_clear_bits_raw(port, pins);
1449 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, clear_bits_raw, port, ret);
1450 return ret;
1451}
1452
1463static inline int gpio_port_clear_bits(const struct device *port,
1464 gpio_port_pins_t pins)
1465{
1466 return gpio_port_set_masked(port, pins, 0);
1467}
1468
1479__syscall int gpio_port_toggle_bits(const struct device *port,
1480 gpio_port_pins_t pins);
1481
1482static inline int z_impl_gpio_port_toggle_bits(const struct device *port,
1483 gpio_port_pins_t pins)
1484{
1485 const struct gpio_driver_api *api =
1486 (const struct gpio_driver_api *)port->api;
1487 int ret;
1488
1489 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, toggle_bits, port, pins);
1490
1491 ret = api->port_toggle_bits(port, pins);
1492 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, toggle_bits, port, ret);
1493 return ret;
1494}
1495
1507static inline int gpio_port_set_clr_bits_raw(const struct device *port,
1508 gpio_port_pins_t set_pins,
1509 gpio_port_pins_t clear_pins)
1510{
1511 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1512
1513 return gpio_port_set_masked_raw(port, set_pins | clear_pins, set_pins);
1514}
1515
1527static inline int gpio_port_set_clr_bits(const struct device *port,
1528 gpio_port_pins_t set_pins,
1529 gpio_port_pins_t clear_pins)
1530{
1531 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1532
1533 return gpio_port_set_masked(port, set_pins | clear_pins, set_pins);
1534}
1535
1551static inline int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
1552{
1553 __unused const struct gpio_driver_config *const cfg =
1554 (const struct gpio_driver_config *)port->config;
1555 gpio_port_value_t value;
1556 int ret;
1557
1558 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1559 "Unsupported pin");
1560
1561 ret = gpio_port_get_raw(port, &value);
1562 if (ret == 0) {
1563 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1564 }
1565
1566 return ret;
1567}
1568
1588static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)
1589{
1590 __unused const struct gpio_driver_config *const cfg =
1591 (const struct gpio_driver_config *)port->config;
1592 gpio_port_value_t value;
1593 int ret;
1594
1595 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1596 "Unsupported pin");
1597
1598 ret = gpio_port_get(port, &value);
1599 if (ret == 0) {
1600 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1601 }
1602
1603 return ret;
1604}
1605
1616static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
1617{
1618 return gpio_pin_get(spec->port, spec->pin);
1619}
1620
1636static inline int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin,
1637 int value)
1638{
1639 __unused const struct gpio_driver_config *const cfg =
1640 (const struct gpio_driver_config *)port->config;
1641 int ret;
1642
1643 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1644 "Unsupported pin");
1645
1646 if (value != 0) {
1647 ret = gpio_port_set_bits_raw(port, (gpio_port_pins_t)BIT(pin));
1648 } else {
1650 }
1651
1652 return ret;
1653}
1654
1676static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin,
1677 int value)
1678{
1679 __unused const struct gpio_driver_config *const cfg =
1680 (const struct gpio_driver_config *)port->config;
1681 const struct gpio_driver_data *const data =
1682 (const struct gpio_driver_data *)port->data;
1683
1684 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1685 "Unsupported pin");
1686
1687 if (data->invert & (gpio_port_pins_t)BIT(pin)) {
1688 value = (value != 0) ? 0 : 1;
1689 }
1690
1691 return gpio_pin_set_raw(port, pin, value);
1692}
1693
1705static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
1706{
1707 return gpio_pin_set(spec->port, spec->pin, value);
1708}
1709
1720static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
1721{
1722 __unused const struct gpio_driver_config *const cfg =
1723 (const struct gpio_driver_config *)port->config;
1724
1725 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1726 "Unsupported pin");
1727
1728 return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin));
1729}
1730
1741static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
1742{
1743 return gpio_pin_toggle(spec->port, spec->pin);
1744}
1745
1752static inline void gpio_init_callback(struct gpio_callback *callback,
1754 gpio_port_pins_t pin_mask)
1755{
1756 SYS_PORT_TRACING_FUNC_ENTER(gpio, init_callback, callback, handler, pin_mask);
1757
1758 __ASSERT(callback, "Callback pointer should not be NULL");
1759 __ASSERT(handler, "Callback handler pointer should not be NULL");
1760
1761 callback->handler = handler;
1762 callback->pin_mask = pin_mask;
1763
1764 SYS_PORT_TRACING_FUNC_EXIT(gpio, init_callback, callback);
1765}
1766
1781static inline int gpio_add_callback(const struct device *port,
1782 struct gpio_callback *callback)
1783{
1784 const struct gpio_driver_api *api =
1785 (const struct gpio_driver_api *)port->api;
1786 int ret;
1787
1788 SYS_PORT_TRACING_FUNC_ENTER(gpio, add_callback, port, callback);
1789
1790 if (api->manage_callback == NULL) {
1791 SYS_PORT_TRACING_FUNC_EXIT(gpio, add_callback, port, -ENOSYS);
1792 return -ENOSYS;
1793 }
1794
1795 ret = api->manage_callback(port, callback, true);
1796 SYS_PORT_TRACING_FUNC_EXIT(gpio, add_callback, port, ret);
1797 return ret;
1798}
1799
1811static inline int gpio_add_callback_dt(const struct gpio_dt_spec *spec,
1812 struct gpio_callback *callback)
1813{
1814 return gpio_add_callback(spec->port, callback);
1815}
1816
1835static inline int gpio_remove_callback(const struct device *port,
1836 struct gpio_callback *callback)
1837{
1838 const struct gpio_driver_api *api =
1839 (const struct gpio_driver_api *)port->api;
1840 int ret;
1841
1842 SYS_PORT_TRACING_FUNC_ENTER(gpio, remove_callback, port, callback);
1843
1844 if (api->manage_callback == NULL) {
1845 SYS_PORT_TRACING_FUNC_EXIT(gpio, remove_callback, port, -ENOSYS);
1846 return -ENOSYS;
1847 }
1848
1849 ret = api->manage_callback(port, callback, false);
1850 SYS_PORT_TRACING_FUNC_EXIT(gpio, remove_callback, port, ret);
1851 return ret;
1852}
1853
1865static inline int gpio_remove_callback_dt(const struct gpio_dt_spec *spec,
1866 struct gpio_callback *callback)
1867{
1868 return gpio_remove_callback(spec->port, callback);
1869}
1870
1885__syscall int gpio_get_pending_int(const struct device *dev);
1886
1887static inline int z_impl_gpio_get_pending_int(const struct device *dev)
1888{
1889 const struct gpio_driver_api *api =
1890 (const struct gpio_driver_api *)dev->api;
1891 int ret;
1892
1893 SYS_PORT_TRACING_FUNC_ENTER(gpio, get_pending_int, dev);
1894
1895 if (api->get_pending_int == NULL) {
1896 SYS_PORT_TRACING_FUNC_EXIT(gpio, get_pending_int, dev, -ENOSYS);
1897 return -ENOSYS;
1898 }
1899
1900 ret = api->get_pending_int(dev);
1901 SYS_PORT_TRACING_FUNC_EXIT(gpio, get_pending_int, dev, ret);
1902 return ret;
1903}
1904
1909#ifdef __cplusplus
1910}
1911#endif
1912
1913#include <zephyr/syscalls/gpio.h>
1914
1915#endif /* ZEPHYR_INCLUDE_DRIVERS_GPIO_H_ */
#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE
System error numbers.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
static int gpio_add_callback(const struct device *port, struct gpio_callback *callback)
Add an application callback.
Definition gpio.h:1781
#define GPIO_OUTPUT
Enables pin as output, no change to the output state.
Definition gpio.h:57
static int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
Get physical level of an input pin.
Definition gpio.h:1551
int gpio_pin_get_config(const struct device *port, gpio_pin_t pin, gpio_flags_t *flags)
Get a configuration of a single pin.
static int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
Check if pin is configured for input.
Definition gpio.h:1114
static int gpio_pin_get(const struct device *port, gpio_pin_t pin)
Get logical level of an input pin.
Definition gpio.h:1588
int gpio_port_set_bits_raw(const struct device *port, gpio_port_pins_t pins)
Set physical level of selected output pins to high.
static int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
Check if a single pin from gpio_dt_spec is configured for output.
Definition gpio.h:1187
static int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec, gpio_flags_t flags)
Configure pin interrupts from a gpio_dt_spec.
Definition gpio.h:959
void(* gpio_callback_handler_t)(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins)
Define the application callback handler function signature.
Definition gpio.h:733
static int gpio_remove_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1865
static int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
Toggle pin level from a gpio_dt_spec.
Definition gpio.h:1741
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition gpio.h:261
static int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
Check if pin is configured for output.
Definition gpio.h:1159
int gpio_get_pending_int(const struct device *dev)
Function to get pending interrupts.
static int gpio_pin_configure_dt(const struct gpio_dt_spec *spec, gpio_flags_t extra_flags)
Configure a single pin from a gpio_dt_spec and some extra flags.
Definition gpio.h:1052
static int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
Set logical level of a output pin from a gpio_dt_spec.
Definition gpio.h:1705
static int gpio_add_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Add an application callback.
Definition gpio.h:1811
static int gpio_port_set_bits(const struct device *port, gpio_port_pins_t pins)
Set logical level of selected output pins to active.
Definition gpio.h:1420
uint32_t gpio_flags_t
Provides a type to hold GPIO configuration flags.
Definition gpio.h:281
#define GPIO_ACTIVE_LOW
GPIO pin is active (has logical value '1') in low state.
Definition gpio.h:24
#define GPIO_INT_WAKEUP
Configures GPIO interrupt to wakeup the system from low power mode.
Definition gpio.h:83
static int gpio_port_set_clr_bits_raw(const struct device *port, gpio_port_pins_t set_pins, gpio_port_pins_t clear_pins)
Set physical level of selected output pins.
Definition gpio.h:1507
static int gpio_port_set_clr_bits(const struct device *port, gpio_port_pins_t set_pins, gpio_port_pins_t clear_pins)
Set logical level of selected output pins.
Definition gpio.h:1527
static void gpio_init_callback(struct gpio_callback *callback, gpio_callback_handler_t handler, gpio_port_pins_t pin_mask)
Helper to initialize a struct gpio_callback properly.
Definition gpio.h:1752
static int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
Check if a single pin from gpio_dt_spec is configured for input.
Definition gpio.h:1142
#define GPIO_INPUT
Enables pin as input.
Definition gpio.h:54
uint32_t gpio_port_pins_t
Identifies a set of pins associated with a port.
Definition gpio.h:240
static int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec, gpio_flags_t *flags)
Get a configuration of a single pin from a gpio_dt_spec.
Definition gpio.h:1244
int gpio_port_toggle_bits(const struct device *port, gpio_port_pins_t pins)
Toggle level of selected output pins.
#define GPIO_INT_DISABLE
Disables GPIO pin interrupt.
Definition gpio.h:107
int gpio_pin_interrupt_configure(const struct device *port, gpio_pin_t pin, gpio_flags_t flags)
Configure pin interrupt.
int gpio_port_clear_bits_raw(const struct device *port, gpio_port_pins_t pins)
Set physical level of selected output pins to low.
int gpio_port_set_masked_raw(const struct device *port, gpio_port_pins_t mask, gpio_port_value_t value)
Set physical level of output pins in a port.
#define GPIO_PULL_UP
Enables GPIO pin pull-up.
Definition gpio.h:73
static int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
Get logical level of an input pin from a gpio_dt_spec.
Definition gpio.h:1616
static int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
Toggle pin level.
Definition gpio.h:1720
static bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
Validate that GPIO port is ready.
Definition gpio.h:843
uint32_t gpio_port_value_t
Provides values for a set of pins associated with a port.
Definition gpio.h:253
static int gpio_pin_set(const struct device *port, gpio_pin_t pin, int value)
Set logical level of an output pin.
Definition gpio.h:1676
static int gpio_port_clear_bits(const struct device *port, gpio_port_pins_t pins)
Set logical level of selected output pins to inactive.
Definition gpio.h:1463
static int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1835
static int gpio_port_set_masked(const struct device *port, gpio_port_pins_t mask, gpio_port_value_t value)
Set logical level of output pins in a port.
Definition gpio.h:1371
int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map, gpio_port_pins_t *inputs, gpio_port_pins_t *outputs)
Get direction of select pins in a port.
uint16_t gpio_dt_flags_t
Provides a type to hold GPIO devicetree flags.
Definition gpio.h:273
#define GPIO_PULL_DOWN
Enable GPIO pin pull-down.
Definition gpio.h:76
static int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin, int value)
Set physical level of an output pin.
Definition gpio.h:1636
int gpio_port_get_raw(const struct device *port, gpio_port_value_t *value)
Get physical level of all input pins in a port.
static int gpio_port_get(const struct device *port, gpio_port_value_t *value)
Get logical level of all input pins in a port.
Definition gpio.h:1300
int gpio_pin_configure(const struct device *port, gpio_pin_t pin, gpio_flags_t flags)
Configure a single pin.
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
#define SYS_PORT_TRACING_FUNC_ENTER(type, func,...)
Tracing macro for the entry into a function that might or might not return a value.
Definition tracing_macros.h:248
#define SYS_PORT_TRACING_FUNC_EXIT(type, func,...)
Tracing macro for when a function ends its execution.
Definition tracing_macros.h:274
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
flags
Definition parser.h:97
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
void * data
Address of the device instance private data.
Definition device.h:520
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
const void * config
Address of device instance config information.
Definition device.h:514
GPIO callback structure.
Definition gpio.h:747
sys_snode_t node
This is meant to be used in the driver and the user should not mess with it (see drivers/gpio/gpio_ut...
Definition gpio.h:751
gpio_port_pins_t pin_mask
A mask of pins the callback is interested in, if 0 the callback will never be called.
Definition gpio.h:762
gpio_callback_handler_t handler
Actual callback function being called when relevant.
Definition gpio.h:754
This structure is common to all GPIO drivers and is expected to be the first element in the object po...
Definition gpio.h:697
gpio_port_pins_t port_pin_mask
Mask identifying pins supported by the controller.
Definition gpio.h:703
This structure is common to all GPIO drivers and is expected to be the first element in the driver's ...
Definition gpio.h:710
gpio_port_pins_t invert
Mask identifying pins that are configured as active low.
Definition gpio.h:716
Container for GPIO pin information specified in devicetree.
Definition gpio.h:295
const struct device * port
GPIO device controlling the pin.
Definition gpio.h:297
gpio_pin_t pin
The pin's number on the device.
Definition gpio.h:299
gpio_dt_flags_t dt_flags
The pin's configuration flags as specified in devicetree.
Definition gpio.h:301