Zephyr API Documentation 4.0.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
15#ifndef ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
16#define ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
17
18#include <errno.h>
19
20#include <zephyr/sys/__assert.h>
21#include <zephyr/sys/slist.h>
23
24#include <zephyr/types.h>
25#include <stddef.h>
26#include <zephyr/device.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
48#define GPIO_INPUT (1U << 16)
49
51#define GPIO_OUTPUT (1U << 17)
52
54#define GPIO_DISCONNECTED 0
55
58/* Initializes output to a low state. */
59#define GPIO_OUTPUT_INIT_LOW (1U << 18)
60
61/* Initializes output to a high state. */
62#define GPIO_OUTPUT_INIT_HIGH (1U << 19)
63
64/* Initializes output based on logic level */
65#define GPIO_OUTPUT_INIT_LOGICAL (1U << 20)
66
70#define GPIO_OUTPUT_LOW (GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW)
72#define GPIO_OUTPUT_HIGH (GPIO_OUTPUT | GPIO_OUTPUT_INIT_HIGH)
74#define GPIO_OUTPUT_INACTIVE (GPIO_OUTPUT | \
75 GPIO_OUTPUT_INIT_LOW | \
76 GPIO_OUTPUT_INIT_LOGICAL)
78#define GPIO_OUTPUT_ACTIVE (GPIO_OUTPUT | \
79 GPIO_OUTPUT_INIT_HIGH | \
80 GPIO_OUTPUT_INIT_LOGICAL)
81
101#define GPIO_INT_DISABLE (1U << 21)
102
105/* Enables GPIO pin interrupt. */
106#define GPIO_INT_ENABLE (1U << 22)
107
108/* GPIO interrupt is sensitive to logical levels.
109 *
110 * This is a component flag that should be combined with other
111 * `GPIO_INT_*` flags to produce a meaningful configuration.
112 */
113#define GPIO_INT_LEVELS_LOGICAL (1U << 23)
114
115/* GPIO interrupt is edge sensitive.
116 *
117 * Note: by default interrupts are level sensitive.
118 *
119 * This is a component flag that should be combined with other
120 * `GPIO_INT_*` flags to produce a meaningful configuration.
121 */
122#define GPIO_INT_EDGE (1U << 24)
123
124/* Trigger detection when input state is (or transitions to) physical low or
125 * logical 0 level.
126 *
127 * This is a component flag that should be combined with other
128 * `GPIO_INT_*` flags to produce a meaningful configuration.
129 */
130#define GPIO_INT_LOW_0 (1U << 25)
131
132/* Trigger detection on input state is (or transitions to) physical high or
133 * logical 1 level.
134 *
135 * This is a component flag that should be combined with other
136 * `GPIO_INT_*` flags to produce a meaningful configuration.
137 */
138#define GPIO_INT_HIGH_1 (1U << 26)
139
140#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
141/* Disable/Enable interrupt functionality without changing other interrupt
142 * related register, such as clearing the pending register.
143 *
144 * This is a component flag that should be combined with `GPIO_INT_ENABLE` or
145 * `GPIO_INT_DISABLE` flags to produce a meaningful configuration.
146 */
147#define GPIO_INT_ENABLE_DISABLE_ONLY (1u << 27)
148#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
149
150#define GPIO_INT_MASK (GPIO_INT_DISABLE | \
151 GPIO_INT_ENABLE | \
152 GPIO_INT_LEVELS_LOGICAL | \
153 GPIO_INT_EDGE | \
154 GPIO_INT_LOW_0 | \
155 GPIO_INT_HIGH_1)
156
161#define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
162 GPIO_INT_EDGE | \
163 GPIO_INT_HIGH_1)
164
168#define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
169 GPIO_INT_EDGE | \
170 GPIO_INT_LOW_0)
171
175#define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
176 GPIO_INT_EDGE | \
177 GPIO_INT_LOW_0 | \
178 GPIO_INT_HIGH_1)
179
183#define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
184 GPIO_INT_LOW_0)
185
189#define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
190 GPIO_INT_HIGH_1)
191
195#define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
196 GPIO_INT_LEVELS_LOGICAL | \
197 GPIO_INT_EDGE | \
198 GPIO_INT_LOW_0)
199
203#define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
204 GPIO_INT_LEVELS_LOGICAL | \
205 GPIO_INT_EDGE | \
206 GPIO_INT_HIGH_1)
207
211#define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
212 GPIO_INT_LEVELS_LOGICAL | \
213 GPIO_INT_LOW_0)
214
218#define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
219 GPIO_INT_LEVELS_LOGICAL | \
220 GPIO_INT_HIGH_1)
221
225#define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
235
248
256
268
276
297
332#define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
333 { \
334 .port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)),\
335 .pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
336 .dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
337 }
338
356#define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
357 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
358 (GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
359 (default_value))
360
369#define GPIO_DT_SPEC_GET(node_id, prop) \
370 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
371
382#define GPIO_DT_SPEC_GET_OR(node_id, prop, default_value) \
383 GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
384
395#define GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
396 GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
397
409#define GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
410 COND_CODE_1(DT_PROP_HAS_IDX(DT_DRV_INST(inst), prop, idx), \
411 (GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)), \
412 (default_value))
413
422#define GPIO_DT_SPEC_INST_GET(inst, prop) \
423 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
424
435#define GPIO_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
436 GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value)
437
438/*
439 * @cond INTERNAL_HIDDEN
440 */
441
452#define Z_GPIO_GEN_BITMASK_COND(node_id, prop, off_idx, sz_idx) \
453 COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, off_idx), \
454 (COND_CODE_0(DT_PROP_BY_IDX(node_id, prop, sz_idx), \
455 (0), \
456 (GENMASK64(DT_PROP_BY_IDX(node_id, prop, off_idx) + \
457 DT_PROP_BY_IDX(node_id, prop, sz_idx) - 1, \
458 DT_PROP_BY_IDX(node_id, prop, off_idx)))) \
459 ), (0))
460
468#define Z_GPIO_GEN_RESERVED_RANGES_COND(odd_it, node_id) \
469 COND_CODE_1(DT_PROP_HAS_IDX(node_id, gpio_reserved_ranges, odd_it), \
470 (Z_GPIO_GEN_BITMASK_COND(node_id, \
471 gpio_reserved_ranges, \
472 GET_ARG_N(odd_it, Z_SPARSE_LIST_EVEN_NUMBERS), \
473 odd_it)), \
474 (0))
475
567#define GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios) \
568 ((gpio_port_pins_t) \
569 COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
570 (GENMASK64(BITS_PER_LONG_LONG - 1, ngpios) \
571 | FOR_EACH_FIXED_ARG(Z_GPIO_GEN_RESERVED_RANGES_COND, \
572 (|), \
573 node_id, \
574 LIST_DROP_EMPTY(Z_SPARSE_LIST_ODD_NUMBERS))), \
575 (0)))
576
584#define GPIO_DT_RESERVED_RANGES(node_id) \
585 GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, DT_PROP(node_id, ngpios))
586
596#define GPIO_DT_INST_RESERVED_RANGES_NGPIOS(inst, ngpios) \
597 GPIO_DT_RESERVED_RANGES_NGPIOS(DT_DRV_INST(inst), ngpios)
598
607#define GPIO_DT_INST_RESERVED_RANGES(inst) \
608 GPIO_DT_RESERVED_RANGES(DT_DRV_INST(inst))
609
658#define GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(node_id, ngpios) \
659 ((gpio_port_pins_t) \
660 COND_CODE_0(ngpios, \
661 (0), \
662 (COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
663 ((GENMASK64(ngpios - 1, 0) & \
664 ~GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios))), \
665 (GENMASK64(ngpios - 1, 0))) \
666 ) \
667 ))
668
678#define GPIO_DT_INST_PORT_PIN_MASK_NGPIOS_EXC(inst, ngpios) \
679 GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(DT_DRV_INST(inst), ngpios)
680
684#define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
685
699
712
713struct gpio_callback;
714
727typedef void (*gpio_callback_handler_t)(const struct device *port,
728 struct gpio_callback *cb,
729 gpio_port_pins_t pins);
730
758
765/* Used by driver api function pin_interrupt_configure, these are defined
766 * in terms of the public flags so we can just mask and pass them
767 * through to the driver api
768 */
769enum gpio_int_mode {
770 GPIO_INT_MODE_DISABLED = GPIO_INT_DISABLE,
771 GPIO_INT_MODE_LEVEL = GPIO_INT_ENABLE,
772 GPIO_INT_MODE_EDGE = GPIO_INT_ENABLE | GPIO_INT_EDGE,
773#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
774 GPIO_INT_MODE_DISABLE_ONLY = GPIO_INT_DISABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
775 GPIO_INT_MODE_ENABLE_ONLY = GPIO_INT_ENABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
776#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
777};
778
779enum gpio_int_trig {
780 /* Trigger detection when input state is (or transitions to)
781 * physical low. (Edge Falling or Active Low)
782 */
783 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
784 /* Trigger detection when input state is (or transitions to)
785 * physical high. (Edge Rising or Active High) */
786 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
787 /* Trigger detection on pin rising or falling edge. */
788 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
789 /* Trigger a system wakeup. */
790 GPIO_INT_TRIG_WAKE = GPIO_INT_WAKEUP,
791};
792
793__subsystem struct gpio_driver_api {
794 int (*pin_configure)(const struct device *port, gpio_pin_t pin,
796#ifdef CONFIG_GPIO_GET_CONFIG
797 int (*pin_get_config)(const struct device *port, gpio_pin_t pin,
799#endif
800 int (*port_get_raw)(const struct device *port,
801 gpio_port_value_t *value);
802 int (*port_set_masked_raw)(const struct device *port,
803 gpio_port_pins_t mask,
804 gpio_port_value_t value);
805 int (*port_set_bits_raw)(const struct device *port,
806 gpio_port_pins_t pins);
807 int (*port_clear_bits_raw)(const struct device *port,
808 gpio_port_pins_t pins);
809 int (*port_toggle_bits)(const struct device *port,
810 gpio_port_pins_t pins);
811 int (*pin_interrupt_configure)(const struct device *port,
812 gpio_pin_t pin,
813 enum gpio_int_mode, enum gpio_int_trig);
814 int (*manage_callback)(const struct device *port,
815 struct gpio_callback *cb,
816 bool set);
817 uint32_t (*get_pending_int)(const struct device *dev);
818#ifdef CONFIG_GPIO_GET_DIRECTION
819 int (*port_get_direction)(const struct device *port, gpio_port_pins_t map,
820 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
821#endif /* CONFIG_GPIO_GET_DIRECTION */
822};
823
836static inline bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
837{
838 /* Validate port is ready */
839 return device_is_ready(spec->port);
840}
841
865__syscall int gpio_pin_interrupt_configure(const struct device *port,
866 gpio_pin_t pin,
868
869static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
870 gpio_pin_t pin,
872{
873 const struct gpio_driver_api *api =
874 (const struct gpio_driver_api *)port->api;
875 __unused const struct gpio_driver_config *const cfg =
876 (const struct gpio_driver_config *)port->config;
877 const struct gpio_driver_data *const data =
878 (const struct gpio_driver_data *)port->data;
879 enum gpio_int_trig trig;
880 enum gpio_int_mode mode;
881 int ret;
882
883 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, interrupt_configure, port, pin, flags);
884
885 if (api->pin_interrupt_configure == NULL) {
886 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, interrupt_configure, port, pin, -ENOSYS);
887 return -ENOSYS;
888 }
889
890 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE))
891 != (GPIO_INT_DISABLE | GPIO_INT_ENABLE),
892 "Cannot both enable and disable interrupts");
893
894 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE)) != 0U,
895 "Must either enable or disable interrupts");
896
897 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
898 ((flags & GPIO_INT_EDGE) != 0) ||
899 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
900 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
901 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
902 "enabled for a level interrupt.");
903
904 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
905#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
906 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
907 (flags & GPIO_INT_ENABLE_DISABLE_ONLY) != 0,
908#else
909 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0),
910#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
911 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be "
912 "enabled.");
913
914 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
915 "Unsupported pin");
916
917 if (((flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
918 ((data->invert & (gpio_port_pins_t)BIT(pin)) != 0)) {
919 /* Invert signal bits */
920 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
921 }
922
923 trig = (enum gpio_int_trig)(flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1 | GPIO_INT_WAKEUP));
924#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
925 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE |
926 GPIO_INT_ENABLE_DISABLE_ONLY));
927#else
928 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE));
929#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
930
931 ret = api->pin_interrupt_configure(port, pin, mode, trig);
932 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, interrupt_configure, port, pin, ret);
933 return ret;
934}
935
951static inline int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec,
953{
954 return gpio_pin_interrupt_configure(spec->port, spec->pin, flags);
955}
956
972__syscall int gpio_pin_configure(const struct device *port,
973 gpio_pin_t pin,
975
976static inline int z_impl_gpio_pin_configure(const struct device *port,
977 gpio_pin_t pin,
979{
980 const struct gpio_driver_api *api =
981 (const struct gpio_driver_api *)port->api;
982 __unused const struct gpio_driver_config *const cfg =
983 (const struct gpio_driver_config *)port->config;
984 struct gpio_driver_data *data =
985 (struct gpio_driver_data *)port->data;
986 int ret;
987
988 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, configure, port, pin, flags);
989
990 __ASSERT((flags & GPIO_INT_MASK) == 0,
991 "Interrupt flags are not supported");
992
993 __ASSERT((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) !=
995 "Pull Up and Pull Down should not be enabled simultaneously");
996
997 __ASSERT(!((flags & GPIO_INPUT) && !(flags & GPIO_OUTPUT) && (flags & GPIO_SINGLE_ENDED)),
998 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
999
1000 __ASSERT_NO_MSG((flags & GPIO_SINGLE_ENDED) != 0 ||
1001 (flags & GPIO_LINE_OPEN_DRAIN) == 0);
1002
1003 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
1004 || (flags & GPIO_OUTPUT) != 0,
1005 "Output needs to be enabled to be initialized low or high");
1006
1007 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
1008 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
1009 "Output cannot be initialized low and high");
1010
1011 if (((flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
1012 && ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
1013 && ((flags & GPIO_ACTIVE_LOW) != 0)) {
1014 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
1015 }
1016
1017 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
1018
1019 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1020 "Unsupported pin");
1021
1022 if ((flags & GPIO_ACTIVE_LOW) != 0) {
1023 data->invert |= (gpio_port_pins_t)BIT(pin);
1024 } else {
1025 data->invert &= ~(gpio_port_pins_t)BIT(pin);
1026 }
1027
1028 ret = api->pin_configure(port, pin, flags);
1029 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, configure, port, pin, ret);
1030 return ret;
1031}
1032
1044static inline int gpio_pin_configure_dt(const struct gpio_dt_spec *spec,
1045 gpio_flags_t extra_flags)
1046{
1047 return gpio_pin_configure(spec->port,
1048 spec->pin,
1049 spec->dt_flags | extra_flags);
1050}
1051
1070__syscall int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1071 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
1072
1073#ifdef CONFIG_GPIO_GET_DIRECTION
1074static inline int z_impl_gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1075 gpio_port_pins_t *inputs,
1076 gpio_port_pins_t *outputs)
1077{
1078 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1079 int ret;
1080
1081 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, get_direction, port, map, inputs, outputs);
1082
1083 if (api->port_get_direction == NULL) {
1084 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_direction, port, -ENOSYS);
1085 return -ENOSYS;
1086 }
1087
1088 ret = api->port_get_direction(port, map, inputs, outputs);
1089 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_direction, port, ret);
1090 return ret;
1091}
1092#endif /* CONFIG_GPIO_GET_DIRECTION */
1093
1106static inline int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
1107{
1108 int rv;
1109 gpio_port_pins_t pins;
1110 __unused const struct gpio_driver_config *cfg =
1111 (const struct gpio_driver_config *)port->config;
1112
1113 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1114
1115 rv = gpio_port_get_direction(port, BIT(pin), &pins, NULL);
1116 if (rv < 0) {
1117 return rv;
1118 }
1119
1120 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1121}
1122
1134static inline int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
1135{
1136 return gpio_pin_is_input(spec->port, spec->pin);
1137}
1138
1151static inline int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
1152{
1153 int rv;
1154 gpio_port_pins_t pins;
1155 __unused const struct gpio_driver_config *cfg =
1156 (const struct gpio_driver_config *)port->config;
1157
1158 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1159
1160 rv = gpio_port_get_direction(port, BIT(pin), NULL, &pins);
1161 if (rv < 0) {
1162 return rv;
1163 }
1164
1165 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1166}
1167
1179static inline int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
1180{
1181 return gpio_pin_is_output(spec->port, spec->pin);
1182}
1183
1199__syscall int gpio_pin_get_config(const struct device *port, gpio_pin_t pin,
1201
1202#ifdef CONFIG_GPIO_GET_CONFIG
1203static inline int z_impl_gpio_pin_get_config(const struct device *port,
1204 gpio_pin_t pin,
1206{
1207 const struct gpio_driver_api *api =
1208 (const struct gpio_driver_api *)port->api;
1209 int ret;
1210
1211 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, get_config, port, pin, *flags);
1212
1213 if (api->pin_get_config == NULL) {
1214 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, get_config, port, pin, -ENOSYS);
1215 return -ENOSYS;
1216 }
1217
1218 ret = api->pin_get_config(port, pin, flags);
1219 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, get_config, port, pin, ret);
1220 return ret;
1221}
1222#endif
1223
1236static inline int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec,
1238{
1239 return gpio_pin_get_config(spec->port, spec->pin, flags);
1240}
1241
1259__syscall int gpio_port_get_raw(const struct device *port,
1260 gpio_port_value_t *value);
1261
1262static inline int z_impl_gpio_port_get_raw(const struct device *port, gpio_port_value_t *value)
1263{
1264 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1265 int ret;
1266
1267 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, get_raw, port, value);
1268
1269 ret = api->port_get_raw(port, value);
1270 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_raw, port, ret);
1271 return ret;
1272}
1273
1292static inline int gpio_port_get(const struct device *port,
1293 gpio_port_value_t *value)
1294{
1295 const struct gpio_driver_data *const data =
1296 (const struct gpio_driver_data *)port->data;
1297 int ret;
1298
1299 ret = gpio_port_get_raw(port, value);
1300 if (ret == 0) {
1301 *value ^= data->invert;
1302 }
1303
1304 return ret;
1305}
1306
1324__syscall int gpio_port_set_masked_raw(const struct device *port,
1325 gpio_port_pins_t mask,
1326 gpio_port_value_t value);
1327
1328static inline int z_impl_gpio_port_set_masked_raw(const struct device *port,
1329 gpio_port_pins_t mask,
1330 gpio_port_value_t value)
1331{
1332 const struct gpio_driver_api *api =
1333 (const struct gpio_driver_api *)port->api;
1334 int ret;
1335
1336 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, set_masked_raw, port, mask, value);
1337
1338 ret = api->port_set_masked_raw(port, mask, value);
1339 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, set_masked_raw, port, ret);
1340 return ret;
1341}
1342
1363static inline int gpio_port_set_masked(const struct device *port,
1364 gpio_port_pins_t mask,
1365 gpio_port_value_t value)
1366{
1367 const struct gpio_driver_data *const data =
1368 (const struct gpio_driver_data *)port->data;
1369
1370 value ^= data->invert;
1371
1372 return gpio_port_set_masked_raw(port, mask, value);
1373}
1374
1385__syscall int gpio_port_set_bits_raw(const struct device *port,
1386 gpio_port_pins_t pins);
1387
1388static inline int z_impl_gpio_port_set_bits_raw(const struct device *port,
1389 gpio_port_pins_t pins)
1390{
1391 const struct gpio_driver_api *api =
1392 (const struct gpio_driver_api *)port->api;
1393 int ret;
1394
1395 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, set_bits_raw, port, pins);
1396
1397 ret = api->port_set_bits_raw(port, pins);
1398 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, set_bits_raw, port, ret);
1399 return ret;
1400}
1401
1412static inline int gpio_port_set_bits(const struct device *port,
1413 gpio_port_pins_t pins)
1414{
1415 return gpio_port_set_masked(port, pins, pins);
1416}
1417
1428__syscall int gpio_port_clear_bits_raw(const struct device *port,
1429 gpio_port_pins_t pins);
1430
1431static inline int z_impl_gpio_port_clear_bits_raw(const struct device *port,
1432 gpio_port_pins_t pins)
1433{
1434 const struct gpio_driver_api *api =
1435 (const struct gpio_driver_api *)port->api;
1436 int ret;
1437
1438 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, clear_bits_raw, port, pins);
1439
1440 ret = api->port_clear_bits_raw(port, pins);
1441 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, clear_bits_raw, port, ret);
1442 return ret;
1443}
1444
1455static inline int gpio_port_clear_bits(const struct device *port,
1456 gpio_port_pins_t pins)
1457{
1458 return gpio_port_set_masked(port, pins, 0);
1459}
1460
1471__syscall int gpio_port_toggle_bits(const struct device *port,
1472 gpio_port_pins_t pins);
1473
1474static inline int z_impl_gpio_port_toggle_bits(const struct device *port,
1475 gpio_port_pins_t pins)
1476{
1477 const struct gpio_driver_api *api =
1478 (const struct gpio_driver_api *)port->api;
1479 int ret;
1480
1481 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, toggle_bits, port, pins);
1482
1483 ret = api->port_toggle_bits(port, pins);
1484 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, toggle_bits, port, ret);
1485 return ret;
1486}
1487
1499static inline int gpio_port_set_clr_bits_raw(const struct device *port,
1500 gpio_port_pins_t set_pins,
1501 gpio_port_pins_t clear_pins)
1502{
1503 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1504
1505 return gpio_port_set_masked_raw(port, set_pins | clear_pins, set_pins);
1506}
1507
1519static inline int gpio_port_set_clr_bits(const struct device *port,
1520 gpio_port_pins_t set_pins,
1521 gpio_port_pins_t clear_pins)
1522{
1523 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1524
1525 return gpio_port_set_masked(port, set_pins | clear_pins, set_pins);
1526}
1527
1543static inline int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
1544{
1545 __unused const struct gpio_driver_config *const cfg =
1546 (const struct gpio_driver_config *)port->config;
1547 gpio_port_value_t value;
1548 int ret;
1549
1550 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1551 "Unsupported pin");
1552
1553 ret = gpio_port_get_raw(port, &value);
1554 if (ret == 0) {
1555 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1556 }
1557
1558 return ret;
1559}
1560
1580static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)
1581{
1582 __unused const struct gpio_driver_config *const cfg =
1583 (const struct gpio_driver_config *)port->config;
1584 gpio_port_value_t value;
1585 int ret;
1586
1587 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1588 "Unsupported pin");
1589
1590 ret = gpio_port_get(port, &value);
1591 if (ret == 0) {
1592 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1593 }
1594
1595 return ret;
1596}
1597
1608static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
1609{
1610 return gpio_pin_get(spec->port, spec->pin);
1611}
1612
1628static inline int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin,
1629 int value)
1630{
1631 __unused const struct gpio_driver_config *const cfg =
1632 (const struct gpio_driver_config *)port->config;
1633 int ret;
1634
1635 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1636 "Unsupported pin");
1637
1638 if (value != 0) {
1639 ret = gpio_port_set_bits_raw(port, (gpio_port_pins_t)BIT(pin));
1640 } else {
1642 }
1643
1644 return ret;
1645}
1646
1668static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin,
1669 int value)
1670{
1671 __unused const struct gpio_driver_config *const cfg =
1672 (const struct gpio_driver_config *)port->config;
1673 const struct gpio_driver_data *const data =
1674 (const struct gpio_driver_data *)port->data;
1675
1676 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1677 "Unsupported pin");
1678
1679 if (data->invert & (gpio_port_pins_t)BIT(pin)) {
1680 value = (value != 0) ? 0 : 1;
1681 }
1682
1683 return gpio_pin_set_raw(port, pin, value);
1684}
1685
1697static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
1698{
1699 return gpio_pin_set(spec->port, spec->pin, value);
1700}
1701
1712static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
1713{
1714 __unused const struct gpio_driver_config *const cfg =
1715 (const struct gpio_driver_config *)port->config;
1716
1717 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1718 "Unsupported pin");
1719
1720 return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin));
1721}
1722
1733static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
1734{
1735 return gpio_pin_toggle(spec->port, spec->pin);
1736}
1737
1744static inline void gpio_init_callback(struct gpio_callback *callback,
1746 gpio_port_pins_t pin_mask)
1747{
1748 SYS_PORT_TRACING_FUNC_ENTER(gpio, init_callback, callback, handler, pin_mask);
1749
1750 __ASSERT(callback, "Callback pointer should not be NULL");
1751 __ASSERT(handler, "Callback handler pointer should not be NULL");
1752
1753 callback->handler = handler;
1754 callback->pin_mask = pin_mask;
1755
1756 SYS_PORT_TRACING_FUNC_EXIT(gpio, init_callback, callback);
1757}
1758
1773static inline int gpio_add_callback(const struct device *port,
1774 struct gpio_callback *callback)
1775{
1776 const struct gpio_driver_api *api =
1777 (const struct gpio_driver_api *)port->api;
1778 int ret;
1779
1780 SYS_PORT_TRACING_FUNC_ENTER(gpio, add_callback, port, callback);
1781
1782 if (api->manage_callback == NULL) {
1783 SYS_PORT_TRACING_FUNC_EXIT(gpio, add_callback, port, -ENOSYS);
1784 return -ENOSYS;
1785 }
1786
1787 ret = api->manage_callback(port, callback, true);
1788 SYS_PORT_TRACING_FUNC_EXIT(gpio, add_callback, port, ret);
1789 return ret;
1790}
1791
1803static inline int gpio_add_callback_dt(const struct gpio_dt_spec *spec,
1804 struct gpio_callback *callback)
1805{
1806 return gpio_add_callback(spec->port, callback);
1807}
1808
1827static inline int gpio_remove_callback(const struct device *port,
1828 struct gpio_callback *callback)
1829{
1830 const struct gpio_driver_api *api =
1831 (const struct gpio_driver_api *)port->api;
1832 int ret;
1833
1834 SYS_PORT_TRACING_FUNC_ENTER(gpio, remove_callback, port, callback);
1835
1836 if (api->manage_callback == NULL) {
1837 SYS_PORT_TRACING_FUNC_EXIT(gpio, remove_callback, port, -ENOSYS);
1838 return -ENOSYS;
1839 }
1840
1841 ret = api->manage_callback(port, callback, false);
1842 SYS_PORT_TRACING_FUNC_EXIT(gpio, remove_callback, port, ret);
1843 return ret;
1844}
1845
1857static inline int gpio_remove_callback_dt(const struct gpio_dt_spec *spec,
1858 struct gpio_callback *callback)
1859{
1860 return gpio_remove_callback(spec->port, callback);
1861}
1862
1877__syscall int gpio_get_pending_int(const struct device *dev);
1878
1879static inline int z_impl_gpio_get_pending_int(const struct device *dev)
1880{
1881 const struct gpio_driver_api *api =
1882 (const struct gpio_driver_api *)dev->api;
1883 int ret;
1884
1885 SYS_PORT_TRACING_FUNC_ENTER(gpio, get_pending_int, dev);
1886
1887 if (api->get_pending_int == NULL) {
1888 SYS_PORT_TRACING_FUNC_EXIT(gpio, get_pending_int, dev, -ENOSYS);
1889 return -ENOSYS;
1890 }
1891
1892 ret = api->get_pending_int(dev);
1893 SYS_PORT_TRACING_FUNC_EXIT(gpio, get_pending_int, dev, ret);
1894 return ret;
1895}
1896
1901#ifdef __cplusplus
1902}
1903#endif
1904
1905#include <zephyr/syscalls/gpio.h>
1906
1907#endif /* ZEPHYR_INCLUDE_DRIVERS_GPIO_H_ */
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:1773
#define GPIO_OUTPUT
Enables pin as output, no change to the output state.
Definition gpio.h:51
static int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
Get physical level of an input pin.
Definition gpio.h:1543
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:1106
static int gpio_pin_get(const struct device *port, gpio_pin_t pin)
Get logical level of an input pin.
Definition gpio.h:1580
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:1179
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:951
static int gpio_remove_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1857
static int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
Toggle pin level from a gpio_dt_spec.
Definition gpio.h:1733
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition gpio.h:255
static int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
Check if pin is configured for output.
Definition gpio.h:1151
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:1044
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:1697
static int gpio_add_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Add an application callback.
Definition gpio.h:1803
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:1412
uint32_t gpio_flags_t
Provides a type to hold GPIO configuration flags.
Definition gpio.h:275
#define GPIO_ACTIVE_LOW
GPIO pin is active (has logical value '1') in low state.
Definition gpio.h:26
#define GPIO_INT_WAKEUP
Configures GPIO interrupt to wakeup the system from low power mode.
Definition gpio.h:85
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:1499
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:1519
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:1744
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:1134
#define GPIO_INPUT
Enables pin as input.
Definition gpio.h:48
uint32_t gpio_port_pins_t
Identifies a set of pins associated with a port.
Definition gpio.h:234
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:1236
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:101
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:75
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:1608
static int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
Toggle pin level.
Definition gpio.h:1712
static bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
Validate that GPIO port is ready.
Definition gpio.h:836
uint32_t gpio_port_value_t
Provides values for a set of pins associated with a port.
Definition gpio.h:247
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:1668
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:1455
static int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1827
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:1363
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:267
#define GPIO_PULL_DOWN
Enable GPIO pin pull-down.
Definition gpio.h:78
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:1628
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:1292
int gpio_pin_configure(const struct device *port, gpio_pin_t pin, gpio_flags_t flags)
Configure a single pin.
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:727
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
flags
Definition parser.h:96
__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:411
void * data
Address of the device instance private data.
Definition device.h:421
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:417
const void * config
Address of device instance config information.
Definition device.h:415
GPIO callback structure.
Definition gpio.h:741
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:745
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:756
gpio_callback_handler_t handler
Actual callback function being called when relevant.
Definition gpio.h:748
This structure is common to all GPIO drivers and is expected to be the first element in the object po...
Definition gpio.h:691
gpio_port_pins_t port_pin_mask
Mask identifying pins supported by the controller.
Definition gpio.h:697
This structure is common to all GPIO drivers and is expected to be the first element in the driver's ...
Definition gpio.h:704
gpio_port_pins_t invert
Mask identifying pins that are configured as active low.
Definition gpio.h:710
Container for GPIO pin information specified in devicetree.
Definition gpio.h:289
const struct device * port
GPIO device controlling the pin.
Definition gpio.h:291
gpio_pin_t pin
The pin's number on the device.
Definition gpio.h:293
gpio_dt_flags_t dt_flags
The pin's configuration flags as specified in devicetree.
Definition gpio.h:295