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
55#define GPIO_INPUT BIT(16)
56
58#define GPIO_OUTPUT BIT(17)
59
61#define GPIO_DISCONNECTED 0
62
65/* Initializes output to a low state. */
66#define GPIO_OUTPUT_INIT_LOW BIT(18)
67
68/* Initializes output to a high state. */
69#define GPIO_OUTPUT_INIT_HIGH BIT(19)
70
71/* Initializes output based on logic level */
72#define GPIO_OUTPUT_INIT_LOGICAL BIT(20)
73
77#define GPIO_OUTPUT_LOW (GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW)
79#define GPIO_OUTPUT_HIGH (GPIO_OUTPUT | GPIO_OUTPUT_INIT_HIGH)
81#define GPIO_OUTPUT_INACTIVE (GPIO_OUTPUT | \
82 GPIO_OUTPUT_INIT_LOW | \
83 GPIO_OUTPUT_INIT_LOGICAL)
85#define GPIO_OUTPUT_ACTIVE (GPIO_OUTPUT | \
86 GPIO_OUTPUT_INIT_HIGH | \
87 GPIO_OUTPUT_INIT_LOGICAL)
88
108#define GPIO_INT_DISABLE BIT(21)
109
112/* Enables GPIO pin interrupt. */
113#define GPIO_INT_ENABLE BIT(22)
114
115/* GPIO interrupt is sensitive to logical levels.
116 *
117 * This is a component flag that should be combined with other
118 * `GPIO_INT_*` flags to produce a meaningful configuration.
119 */
120#define GPIO_INT_LEVELS_LOGICAL BIT(23)
121
122/* GPIO interrupt is edge sensitive.
123 *
124 * Note: by default interrupts are level sensitive.
125 *
126 * This is a component flag that should be combined with other
127 * `GPIO_INT_*` flags to produce a meaningful configuration.
128 */
129#define GPIO_INT_EDGE BIT(24)
130
131/* Trigger detection when input state is (or transitions to) physical low or
132 * logical 0 level.
133 *
134 * This is a component flag that should be combined with other
135 * `GPIO_INT_*` flags to produce a meaningful configuration.
136 */
137#define GPIO_INT_LOW_0 BIT(25)
138
139/* Trigger detection on input state is (or transitions to) physical high or
140 * logical 1 level.
141 *
142 * This is a component flag that should be combined with other
143 * `GPIO_INT_*` flags to produce a meaningful configuration.
144 */
145#define GPIO_INT_HIGH_1 BIT(26)
146
147#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
148/* Disable/Enable interrupt functionality without changing other interrupt
149 * related register, such as clearing the pending register.
150 *
151 * This is a component flag that should be combined with `GPIO_INT_ENABLE` or
152 * `GPIO_INT_DISABLE` flags to produce a meaningful configuration.
153 */
154#define GPIO_INT_ENABLE_DISABLE_ONLY BIT(27)
155#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
156
157#define GPIO_INT_MASK (GPIO_INT_DISABLE | \
158 GPIO_INT_ENABLE | \
159 GPIO_INT_LEVELS_LOGICAL | \
160 GPIO_INT_EDGE | \
161 GPIO_INT_LOW_0 | \
162 GPIO_INT_HIGH_1)
163
168#define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
169 GPIO_INT_EDGE | \
170 GPIO_INT_HIGH_1)
171
175#define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
176 GPIO_INT_EDGE | \
177 GPIO_INT_LOW_0)
178
182#define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
183 GPIO_INT_EDGE | \
184 GPIO_INT_LOW_0 | \
185 GPIO_INT_HIGH_1)
186
190#define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
191 GPIO_INT_LOW_0)
192
196#define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
197 GPIO_INT_HIGH_1)
198
202#define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
203 GPIO_INT_LEVELS_LOGICAL | \
204 GPIO_INT_EDGE | \
205 GPIO_INT_LOW_0)
206
210#define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
211 GPIO_INT_LEVELS_LOGICAL | \
212 GPIO_INT_EDGE | \
213 GPIO_INT_HIGH_1)
214
218#define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
219 GPIO_INT_LEVELS_LOGICAL | \
220 GPIO_INT_LOW_0)
221
225#define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
226 GPIO_INT_LEVELS_LOGICAL | \
227 GPIO_INT_HIGH_1)
228
232#define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
242
255
263
275
283
304
339#define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
340 { \
341 .port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)),\
342 .pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
343 .dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
344 }
345
363#define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
364 COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, idx), \
365 (GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
366 (default_value))
367
376#define GPIO_DT_SPEC_GET(node_id, prop) \
377 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
378
389#define GPIO_DT_SPEC_GET_OR(node_id, prop, default_value) \
390 GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
391
402#define GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
403 GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
404
416#define GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
417 COND_CODE_1(DT_PROP_HAS_IDX(DT_DRV_INST(inst), prop, idx), \
418 (GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)), \
419 (default_value))
420
429#define GPIO_DT_SPEC_INST_GET(inst, prop) \
430 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
431
442#define GPIO_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
443 GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value)
444
445/*
446 * @cond INTERNAL_HIDDEN
447 */
448
459#define Z_GPIO_GEN_BITMASK_COND(node_id, prop, off_idx, sz_idx) \
460 COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, off_idx), \
461 (COND_CODE_0(DT_PROP_BY_IDX(node_id, prop, sz_idx), \
462 (0), \
463 (GENMASK64(DT_PROP_BY_IDX(node_id, prop, off_idx) + \
464 DT_PROP_BY_IDX(node_id, prop, sz_idx) - 1, \
465 DT_PROP_BY_IDX(node_id, prop, off_idx)))) \
466 ), (0))
467
475#define Z_GPIO_GEN_RESERVED_RANGES_COND(odd_it, node_id) \
476 COND_CODE_1(DT_PROP_HAS_IDX(node_id, gpio_reserved_ranges, odd_it), \
477 (Z_GPIO_GEN_BITMASK_COND(node_id, \
478 gpio_reserved_ranges, \
479 GET_ARG_N(odd_it, Z_SPARSE_LIST_EVEN_NUMBERS), \
480 odd_it)), \
481 (0))
482
574#define GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios) \
575 ((gpio_port_pins_t) \
576 COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
577 (GENMASK64(BITS_PER_LONG_LONG - 1, ngpios) \
578 | FOR_EACH_FIXED_ARG(Z_GPIO_GEN_RESERVED_RANGES_COND, \
579 (|), \
580 node_id, \
581 LIST_DROP_EMPTY(Z_SPARSE_LIST_ODD_NUMBERS))), \
582 (0)))
583
591#define GPIO_DT_RESERVED_RANGES(node_id) \
592 GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, DT_PROP(node_id, ngpios))
593
603#define GPIO_DT_INST_RESERVED_RANGES_NGPIOS(inst, ngpios) \
604 GPIO_DT_RESERVED_RANGES_NGPIOS(DT_DRV_INST(inst), ngpios)
605
614#define GPIO_DT_INST_RESERVED_RANGES(inst) \
615 GPIO_DT_RESERVED_RANGES(DT_DRV_INST(inst))
616
665#define GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(node_id, ngpios) \
666 ((gpio_port_pins_t) \
667 COND_CODE_0(ngpios, \
668 (0), \
669 (COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
670 ((GENMASK64(ngpios - 1, 0) & \
671 ~GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios))), \
672 (GENMASK64(ngpios - 1, 0))) \
673 ) \
674 ))
675
685#define GPIO_DT_INST_PORT_PIN_MASK_NGPIOS_EXC(inst, ngpios) \
686 GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(DT_DRV_INST(inst), ngpios)
687
691#define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
692
706
719
720struct gpio_callback;
721
734typedef void (*gpio_callback_handler_t)(const struct device *port,
735 struct gpio_callback *cb,
736 gpio_port_pins_t pins);
737
765
772/* Used by driver api function pin_interrupt_configure, these are defined
773 * in terms of the public flags so we can just mask and pass them
774 * through to the driver api
775 */
776enum gpio_int_mode {
777 GPIO_INT_MODE_DISABLED = GPIO_INT_DISABLE,
778 GPIO_INT_MODE_LEVEL = GPIO_INT_ENABLE,
779 GPIO_INT_MODE_EDGE = GPIO_INT_ENABLE | GPIO_INT_EDGE,
780#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
781 GPIO_INT_MODE_DISABLE_ONLY = GPIO_INT_DISABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
782 GPIO_INT_MODE_ENABLE_ONLY = GPIO_INT_ENABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
783#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
784};
785
786enum gpio_int_trig {
787 /* Trigger detection when input state is (or transitions to)
788 * physical low. (Edge Falling or Active Low)
789 */
790 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
791 /* Trigger detection when input state is (or transitions to)
792 * physical high. (Edge Rising or Active High) */
793 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
794 /* Trigger detection on pin rising or falling edge. */
795 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
796 /* Trigger a system wakeup. */
797 GPIO_INT_TRIG_WAKE = GPIO_INT_WAKEUP,
798 /* Trigger a system wakeup when input state is (or transitions to)
799 * physical low. (Edge Falling or Active Low)
800 */
801 GPIO_INT_TRIG_WAKE_LOW = GPIO_INT_LOW_0 | GPIO_INT_WAKEUP,
802 /* Trigger a system wakeup when input state is (or transitions to)
803 * physical high. (Edge Rising or Active High)
804 */
805 GPIO_INT_TRIG_WAKE_HIGH = GPIO_INT_HIGH_1 | GPIO_INT_WAKEUP,
806 /* Trigger a system wakeup on pin rising or falling edge. */
807 GPIO_INT_TRIG_WAKE_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1 | GPIO_INT_WAKEUP,
808};
809
810__subsystem struct gpio_driver_api {
811 int (*pin_configure)(const struct device *port, gpio_pin_t pin,
813#ifdef CONFIG_GPIO_GET_CONFIG
814 int (*pin_get_config)(const struct device *port, gpio_pin_t pin,
816#endif
817 int (*port_get_raw)(const struct device *port,
818 gpio_port_value_t *value);
819 int (*port_set_masked_raw)(const struct device *port,
820 gpio_port_pins_t mask,
821 gpio_port_value_t value);
822 int (*port_set_bits_raw)(const struct device *port,
823 gpio_port_pins_t pins);
824 int (*port_clear_bits_raw)(const struct device *port,
825 gpio_port_pins_t pins);
826 int (*port_toggle_bits)(const struct device *port,
827 gpio_port_pins_t pins);
828 int (*pin_interrupt_configure)(const struct device *port,
829 gpio_pin_t pin,
830 enum gpio_int_mode mode,
831 enum gpio_int_trig trig);
832 int (*manage_callback)(const struct device *port,
833 struct gpio_callback *cb,
834 bool set);
835 uint32_t (*get_pending_int)(const struct device *dev);
836#ifdef CONFIG_GPIO_GET_DIRECTION
837 int (*port_get_direction)(const struct device *port, gpio_port_pins_t map,
838 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
839#endif /* CONFIG_GPIO_GET_DIRECTION */
840};
841
854static inline bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
855{
856 /* Validate port is ready */
857 return device_is_ready(spec->port);
858}
859
883__syscall int gpio_pin_interrupt_configure(const struct device *port,
884 gpio_pin_t pin,
886
887static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
888 gpio_pin_t pin,
890{
891 const struct gpio_driver_api *api =
892 (const struct gpio_driver_api *)port->api;
893 __unused const struct gpio_driver_config *const cfg =
894 (const struct gpio_driver_config *)port->config;
895 const struct gpio_driver_data *const data =
896 (const struct gpio_driver_data *)port->data;
897 enum gpio_int_trig trig;
898 enum gpio_int_mode mode;
899 int ret;
900
901 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, interrupt_configure, port, pin, flags);
902
903 if (api->pin_interrupt_configure == NULL) {
904 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, interrupt_configure, port, pin, -ENOSYS);
905 return -ENOSYS;
906 }
907
908 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE))
909 != (GPIO_INT_DISABLE | GPIO_INT_ENABLE),
910 "Cannot both enable and disable interrupts");
911
912 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE)) != 0U,
913 "Must either enable or disable interrupts");
914
915 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
916 ((flags & GPIO_INT_EDGE) != 0) ||
917 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
918 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
919 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
920 "enabled for a level interrupt.");
921
922#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
923#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE GPIO_INT_ENABLE_DISABLE_ONLY
924#else
925#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE 0
926#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
927
928 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
929 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
931 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be enabled.");
932#undef GPIO_INT_ENABLE_DISABLE_ONLY_VALUE
933 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
934 "Unsupported pin");
935
936 if (((flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
937 ((data->invert & (gpio_port_pins_t)BIT(pin)) != 0)) {
938 /* Invert signal bits */
939 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
940 }
941
942 trig = (enum gpio_int_trig)(flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1 | GPIO_INT_WAKEUP));
943#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
944 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE |
945 GPIO_INT_ENABLE_DISABLE_ONLY));
946#else
947 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE));
948#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
949
950 ret = api->pin_interrupt_configure(port, pin, mode, trig);
951 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, interrupt_configure, port, pin, ret);
952 return ret;
953}
954
972static inline int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec,
974{
975 return gpio_pin_interrupt_configure(spec->port, spec->pin,
976 flags | (spec->dt_flags & GPIO_INT_WAKEUP));
977}
978
994__syscall int gpio_pin_configure(const struct device *port,
995 gpio_pin_t pin,
997
998static inline int z_impl_gpio_pin_configure(const struct device *port,
999 gpio_pin_t pin,
1001{
1002 const struct gpio_driver_api *api =
1003 (const struct gpio_driver_api *)port->api;
1004 __unused const struct gpio_driver_config *const cfg =
1005 (const struct gpio_driver_config *)port->config;
1006 struct gpio_driver_data *data =
1007 (struct gpio_driver_data *)port->data;
1008 int ret;
1009
1010 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, configure, port, pin, flags);
1011
1012 __ASSERT((flags & GPIO_INT_MASK) == 0,
1013 "Interrupt flags are not supported");
1014
1015 __ASSERT((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) !=
1017 "Pull Up and Pull Down should not be enabled simultaneously");
1018
1019 __ASSERT(!((flags & GPIO_INPUT) && !(flags & GPIO_OUTPUT) && (flags & GPIO_SINGLE_ENDED)),
1020 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
1021
1022 __ASSERT_NO_MSG((flags & GPIO_SINGLE_ENDED) != 0 ||
1023 (flags & GPIO_LINE_OPEN_DRAIN) == 0);
1024
1025 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
1026 || (flags & GPIO_OUTPUT) != 0,
1027 "Output needs to be enabled to be initialized low or high");
1028
1029 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
1030 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
1031 "Output cannot be initialized low and high");
1032
1033 if (((flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
1034 && ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
1035 && ((flags & GPIO_ACTIVE_LOW) != 0)) {
1036 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
1037 }
1038
1039 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
1040
1041 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1042 "Unsupported pin");
1043
1044 if ((flags & GPIO_ACTIVE_LOW) != 0) {
1045 data->invert |= (gpio_port_pins_t)BIT(pin);
1046 } else {
1047 data->invert &= ~(gpio_port_pins_t)BIT(pin);
1048 }
1049
1050 ret = api->pin_configure(port, pin, flags);
1051 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, configure, port, pin, ret);
1052 return ret;
1053}
1054
1066static inline int gpio_pin_configure_dt(const struct gpio_dt_spec *spec,
1067 gpio_flags_t extra_flags)
1068{
1069 return gpio_pin_configure(spec->port,
1070 spec->pin,
1071 spec->dt_flags | extra_flags);
1072}
1073
1092__syscall int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1093 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
1094
1095#ifdef CONFIG_GPIO_GET_DIRECTION
1096static inline int z_impl_gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1097 gpio_port_pins_t *inputs,
1098 gpio_port_pins_t *outputs)
1099{
1100 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1101 int ret;
1102
1103 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, get_direction, port, map, inputs, outputs);
1104
1105 if (api->port_get_direction == NULL) {
1106 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_direction, port, -ENOSYS);
1107 return -ENOSYS;
1108 }
1109
1110 ret = api->port_get_direction(port, map, inputs, outputs);
1111 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_direction, port, ret);
1112 return ret;
1113}
1114#endif /* CONFIG_GPIO_GET_DIRECTION */
1115
1128static inline int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
1129{
1130 int rv;
1131 gpio_port_pins_t pins;
1132 __unused const struct gpio_driver_config *cfg =
1133 (const struct gpio_driver_config *)port->config;
1134
1135 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1136
1137 rv = gpio_port_get_direction(port, BIT(pin), &pins, NULL);
1138 if (rv < 0) {
1139 return rv;
1140 }
1141
1142 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1143}
1144
1156static inline int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
1157{
1158 return gpio_pin_is_input(spec->port, spec->pin);
1159}
1160
1173static inline int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
1174{
1175 int rv;
1176 gpio_port_pins_t pins;
1177 __unused const struct gpio_driver_config *cfg =
1178 (const struct gpio_driver_config *)port->config;
1179
1180 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1181
1182 rv = gpio_port_get_direction(port, BIT(pin), NULL, &pins);
1183 if (rv < 0) {
1184 return rv;
1185 }
1186
1187 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1188}
1189
1201static inline int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
1202{
1203 return gpio_pin_is_output(spec->port, spec->pin);
1204}
1205
1221__syscall int gpio_pin_get_config(const struct device *port, gpio_pin_t pin,
1223
1224#ifdef CONFIG_GPIO_GET_CONFIG
1225static inline int z_impl_gpio_pin_get_config(const struct device *port,
1226 gpio_pin_t pin,
1228{
1229 const struct gpio_driver_api *api =
1230 (const struct gpio_driver_api *)port->api;
1231 int ret;
1232
1233 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, get_config, port, pin, *flags);
1234
1235 if (api->pin_get_config == NULL) {
1236 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, get_config, port, pin, -ENOSYS);
1237 return -ENOSYS;
1238 }
1239
1240 ret = api->pin_get_config(port, pin, flags);
1241 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, get_config, port, pin, ret);
1242 return ret;
1243}
1244#endif
1245
1258static inline int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec,
1260{
1261 return gpio_pin_get_config(spec->port, spec->pin, flags);
1262}
1263
1281__syscall int gpio_port_get_raw(const struct device *port,
1282 gpio_port_value_t *value);
1283
1284static inline int z_impl_gpio_port_get_raw(const struct device *port, gpio_port_value_t *value)
1285{
1286 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1287 int ret;
1288
1289 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, get_raw, port, value);
1290
1291 ret = api->port_get_raw(port, value);
1292 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_raw, port, ret);
1293 return ret;
1294}
1295
1314static inline int gpio_port_get(const struct device *port,
1315 gpio_port_value_t *value)
1316{
1317 const struct gpio_driver_data *const data =
1318 (const struct gpio_driver_data *)port->data;
1319 int ret;
1320
1321 ret = gpio_port_get_raw(port, value);
1322 if (ret == 0) {
1323 *value ^= data->invert;
1324 }
1325
1326 return ret;
1327}
1328
1346__syscall int gpio_port_set_masked_raw(const struct device *port,
1347 gpio_port_pins_t mask,
1348 gpio_port_value_t value);
1349
1350static inline int z_impl_gpio_port_set_masked_raw(const struct device *port,
1351 gpio_port_pins_t mask,
1352 gpio_port_value_t value)
1353{
1354 const struct gpio_driver_api *api =
1355 (const struct gpio_driver_api *)port->api;
1356 int ret;
1357
1358 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, set_masked_raw, port, mask, value);
1359
1360 ret = api->port_set_masked_raw(port, mask, value);
1361 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, set_masked_raw, port, ret);
1362 return ret;
1363}
1364
1385static inline int gpio_port_set_masked(const struct device *port,
1386 gpio_port_pins_t mask,
1387 gpio_port_value_t value)
1388{
1389 const struct gpio_driver_data *const data =
1390 (const struct gpio_driver_data *)port->data;
1391
1392 value ^= data->invert;
1393
1394 return gpio_port_set_masked_raw(port, mask, value);
1395}
1396
1407__syscall int gpio_port_set_bits_raw(const struct device *port,
1408 gpio_port_pins_t pins);
1409
1410static inline int z_impl_gpio_port_set_bits_raw(const struct device *port,
1411 gpio_port_pins_t pins)
1412{
1413 const struct gpio_driver_api *api =
1414 (const struct gpio_driver_api *)port->api;
1415 int ret;
1416
1417 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, set_bits_raw, port, pins);
1418
1419 ret = api->port_set_bits_raw(port, pins);
1420 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, set_bits_raw, port, ret);
1421 return ret;
1422}
1423
1434static inline int gpio_port_set_bits(const struct device *port,
1435 gpio_port_pins_t pins)
1436{
1437 return gpio_port_set_masked(port, pins, pins);
1438}
1439
1450__syscall int gpio_port_clear_bits_raw(const struct device *port,
1451 gpio_port_pins_t pins);
1452
1453static inline int z_impl_gpio_port_clear_bits_raw(const struct device *port,
1454 gpio_port_pins_t pins)
1455{
1456 const struct gpio_driver_api *api =
1457 (const struct gpio_driver_api *)port->api;
1458 int ret;
1459
1460 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, clear_bits_raw, port, pins);
1461
1462 ret = api->port_clear_bits_raw(port, pins);
1463 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, clear_bits_raw, port, ret);
1464 return ret;
1465}
1466
1477static inline int gpio_port_clear_bits(const struct device *port,
1478 gpio_port_pins_t pins)
1479{
1480 return gpio_port_set_masked(port, pins, 0);
1481}
1482
1493__syscall int gpio_port_toggle_bits(const struct device *port,
1494 gpio_port_pins_t pins);
1495
1496static inline int z_impl_gpio_port_toggle_bits(const struct device *port,
1497 gpio_port_pins_t pins)
1498{
1499 const struct gpio_driver_api *api =
1500 (const struct gpio_driver_api *)port->api;
1501 int ret;
1502
1503 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, toggle_bits, port, pins);
1504
1505 ret = api->port_toggle_bits(port, pins);
1506 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, toggle_bits, port, ret);
1507 return ret;
1508}
1509
1521static inline int gpio_port_set_clr_bits_raw(const struct device *port,
1522 gpio_port_pins_t set_pins,
1523 gpio_port_pins_t clear_pins)
1524{
1525 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1526
1527 return gpio_port_set_masked_raw(port, set_pins | clear_pins, set_pins);
1528}
1529
1541static inline int gpio_port_set_clr_bits(const struct device *port,
1542 gpio_port_pins_t set_pins,
1543 gpio_port_pins_t clear_pins)
1544{
1545 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1546
1547 return gpio_port_set_masked(port, set_pins | clear_pins, set_pins);
1548}
1549
1565static inline int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
1566{
1567 __unused const struct gpio_driver_config *const cfg =
1568 (const struct gpio_driver_config *)port->config;
1569 gpio_port_value_t value;
1570 int ret;
1571
1572 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1573 "Unsupported pin");
1574
1575 ret = gpio_port_get_raw(port, &value);
1576 if (ret == 0) {
1577 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1578 }
1579
1580 return ret;
1581}
1582
1602static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)
1603{
1604 __unused const struct gpio_driver_config *const cfg =
1605 (const struct gpio_driver_config *)port->config;
1606 gpio_port_value_t value;
1607 int ret;
1608
1609 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1610 "Unsupported pin");
1611
1612 ret = gpio_port_get(port, &value);
1613 if (ret == 0) {
1614 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1615 }
1616
1617 return ret;
1618}
1619
1630static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
1631{
1632 return gpio_pin_get(spec->port, spec->pin);
1633}
1634
1650static inline int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin,
1651 int value)
1652{
1653 __unused const struct gpio_driver_config *const cfg =
1654 (const struct gpio_driver_config *)port->config;
1655 int ret;
1656
1657 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1658 "Unsupported pin");
1659
1660 if (value != 0) {
1661 ret = gpio_port_set_bits_raw(port, (gpio_port_pins_t)BIT(pin));
1662 } else {
1664 }
1665
1666 return ret;
1667}
1668
1690static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin,
1691 int value)
1692{
1693 __unused const struct gpio_driver_config *const cfg =
1694 (const struct gpio_driver_config *)port->config;
1695 const struct gpio_driver_data *const data =
1696 (const struct gpio_driver_data *)port->data;
1697
1698 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1699 "Unsupported pin");
1700
1701 if (data->invert & (gpio_port_pins_t)BIT(pin)) {
1702 value = (value != 0) ? 0 : 1;
1703 }
1704
1705 return gpio_pin_set_raw(port, pin, value);
1706}
1707
1719static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
1720{
1721 return gpio_pin_set(spec->port, spec->pin, value);
1722}
1723
1734static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
1735{
1736 __unused const struct gpio_driver_config *const cfg =
1737 (const struct gpio_driver_config *)port->config;
1738
1739 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1740 "Unsupported pin");
1741
1742 return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin));
1743}
1744
1755static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
1756{
1757 return gpio_pin_toggle(spec->port, spec->pin);
1758}
1759
1766static inline void gpio_init_callback(struct gpio_callback *callback,
1768 gpio_port_pins_t pin_mask)
1769{
1770 SYS_PORT_TRACING_FUNC_ENTER(gpio, init_callback, callback, handler, pin_mask);
1771
1772 __ASSERT(callback, "Callback pointer should not be NULL");
1773 __ASSERT(handler, "Callback handler pointer should not be NULL");
1774
1775 callback->handler = handler;
1776 callback->pin_mask = pin_mask;
1777
1778 SYS_PORT_TRACING_FUNC_EXIT(gpio, init_callback, callback);
1779}
1780
1795static inline int gpio_add_callback(const struct device *port,
1796 struct gpio_callback *callback)
1797{
1798 const struct gpio_driver_api *api =
1799 (const struct gpio_driver_api *)port->api;
1800 int ret;
1801
1802 SYS_PORT_TRACING_FUNC_ENTER(gpio, add_callback, port, callback);
1803
1804 if (api->manage_callback == NULL) {
1805 SYS_PORT_TRACING_FUNC_EXIT(gpio, add_callback, port, -ENOSYS);
1806 return -ENOSYS;
1807 }
1808
1809 ret = api->manage_callback(port, callback, true);
1810 SYS_PORT_TRACING_FUNC_EXIT(gpio, add_callback, port, ret);
1811 return ret;
1812}
1813
1825static inline int gpio_add_callback_dt(const struct gpio_dt_spec *spec,
1826 struct gpio_callback *callback)
1827{
1828 return gpio_add_callback(spec->port, callback);
1829}
1830
1849static inline int gpio_remove_callback(const struct device *port,
1850 struct gpio_callback *callback)
1851{
1852 const struct gpio_driver_api *api =
1853 (const struct gpio_driver_api *)port->api;
1854 int ret;
1855
1856 SYS_PORT_TRACING_FUNC_ENTER(gpio, remove_callback, port, callback);
1857
1858 if (api->manage_callback == NULL) {
1859 SYS_PORT_TRACING_FUNC_EXIT(gpio, remove_callback, port, -ENOSYS);
1860 return -ENOSYS;
1861 }
1862
1863 ret = api->manage_callback(port, callback, false);
1864 SYS_PORT_TRACING_FUNC_EXIT(gpio, remove_callback, port, ret);
1865 return ret;
1866}
1867
1879static inline int gpio_remove_callback_dt(const struct gpio_dt_spec *spec,
1880 struct gpio_callback *callback)
1881{
1882 return gpio_remove_callback(spec->port, callback);
1883}
1884
1899__syscall int gpio_get_pending_int(const struct device *dev);
1900
1901static inline int z_impl_gpio_get_pending_int(const struct device *dev)
1902{
1903 const struct gpio_driver_api *api =
1904 (const struct gpio_driver_api *)dev->api;
1905 int ret;
1906
1907 SYS_PORT_TRACING_FUNC_ENTER(gpio, get_pending_int, dev);
1908
1909 if (api->get_pending_int == NULL) {
1910 SYS_PORT_TRACING_FUNC_EXIT(gpio, get_pending_int, dev, -ENOSYS);
1911 return -ENOSYS;
1912 }
1913
1914 ret = api->get_pending_int(dev);
1915 SYS_PORT_TRACING_FUNC_EXIT(gpio, get_pending_int, dev, ret);
1916 return ret;
1917}
1918
1923#ifdef __cplusplus
1924}
1925#endif
1926
1927#include <zephyr/syscalls/gpio.h>
1928
1929#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:1795
#define GPIO_OUTPUT
Enables pin as output, no change to the output state.
Definition gpio.h:58
static int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
Get physical level of an input pin.
Definition gpio.h:1565
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:1128
static int gpio_pin_get(const struct device *port, gpio_pin_t pin)
Get logical level of an input pin.
Definition gpio.h:1602
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:1201
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:972
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:734
static int gpio_remove_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1879
static int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
Toggle pin level from a gpio_dt_spec.
Definition gpio.h:1755
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition gpio.h:262
static int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
Check if pin is configured for output.
Definition gpio.h:1173
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:1066
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:1719
static int gpio_add_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Add an application callback.
Definition gpio.h:1825
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:1434
uint32_t gpio_flags_t
Provides a type to hold GPIO configuration flags.
Definition gpio.h:282
#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:1521
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:1541
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:1766
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:1156
#define GPIO_INPUT
Enables pin as input.
Definition gpio.h:55
uint32_t gpio_port_pins_t
Identifies a set of pins associated with a port.
Definition gpio.h:241
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:1258
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:108
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:1630
static int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
Toggle pin level.
Definition gpio.h:1734
static bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
Validate that GPIO port is ready.
Definition gpio.h:854
uint32_t gpio_port_value_t
Provides values for a set of pins associated with a port.
Definition gpio.h:254
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:1690
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:1477
static int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1849
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:1385
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:274
#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:1650
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:1314
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:748
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:752
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:763
gpio_callback_handler_t handler
Actual callback function being called when relevant.
Definition gpio.h:755
This structure is common to all GPIO drivers and is expected to be the first element in the object po...
Definition gpio.h:698
gpio_port_pins_t port_pin_mask
Mask identifying pins supported by the controller.
Definition gpio.h:704
This structure is common to all GPIO drivers and is expected to be the first element in the driver's ...
Definition gpio.h:711
gpio_port_pins_t invert
Mask identifying pins that are configured as active low.
Definition gpio.h:717
Container for GPIO pin information specified in devicetree.
Definition gpio.h:296
const struct device * port
GPIO device controlling the pin.
Definition gpio.h:298
gpio_pin_t pin
The pin's number on the device.
Definition gpio.h:300
gpio_dt_flags_t dt_flags
The pin's configuration flags as specified in devicetree.
Definition gpio.h:302