Zephyr API Documentation 4.2.0-rc1
A Scalable Open Source RTOS
 4.2.0-rc1
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 BIT(16)
49
51#define GPIO_OUTPUT BIT(17)
52
54#define GPIO_DISCONNECTED 0
55
58/* Initializes output to a low state. */
59#define GPIO_OUTPUT_INIT_LOW BIT(18)
60
61/* Initializes output to a high state. */
62#define GPIO_OUTPUT_INIT_HIGH BIT(19)
63
64/* Initializes output based on logic level */
65#define GPIO_OUTPUT_INIT_LOGICAL BIT(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 BIT(21)
102
105/* Enables GPIO pin interrupt. */
106#define GPIO_INT_ENABLE BIT(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 BIT(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 BIT(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 BIT(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 BIT(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 BIT(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_PROP_HAS_IDX(node_id, prop, idx), \
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 mode,
814 enum gpio_int_trig trig);
815 int (*manage_callback)(const struct device *port,
816 struct gpio_callback *cb,
817 bool set);
818 uint32_t (*get_pending_int)(const struct device *dev);
819#ifdef CONFIG_GPIO_GET_DIRECTION
820 int (*port_get_direction)(const struct device *port, gpio_port_pins_t map,
821 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
822#endif /* CONFIG_GPIO_GET_DIRECTION */
823};
824
837static inline bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
838{
839 /* Validate port is ready */
840 return device_is_ready(spec->port);
841}
842
866__syscall int gpio_pin_interrupt_configure(const struct device *port,
867 gpio_pin_t pin,
869
870static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
871 gpio_pin_t pin,
873{
874 const struct gpio_driver_api *api =
875 (const struct gpio_driver_api *)port->api;
876 __unused const struct gpio_driver_config *const cfg =
877 (const struct gpio_driver_config *)port->config;
878 const struct gpio_driver_data *const data =
879 (const struct gpio_driver_data *)port->data;
880 enum gpio_int_trig trig;
881 enum gpio_int_mode mode;
882 int ret;
883
884 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, interrupt_configure, port, pin, flags);
885
886 if (api->pin_interrupt_configure == NULL) {
887 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, interrupt_configure, port, pin, -ENOSYS);
888 return -ENOSYS;
889 }
890
891 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE))
892 != (GPIO_INT_DISABLE | GPIO_INT_ENABLE),
893 "Cannot both enable and disable interrupts");
894
895 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE)) != 0U,
896 "Must either enable or disable interrupts");
897
898 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
899 ((flags & GPIO_INT_EDGE) != 0) ||
900 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
901 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
902 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
903 "enabled for a level interrupt.");
904
905#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
906#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE GPIO_INT_ENABLE_DISABLE_ONLY
907#else
908#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE 0
909#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
910
911 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
912 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
914 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be enabled.");
915#undef GPIO_INT_ENABLE_DISABLE_ONLY_VALUE
916 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
917 "Unsupported pin");
918
919 if (((flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
920 ((data->invert & (gpio_port_pins_t)BIT(pin)) != 0)) {
921 /* Invert signal bits */
922 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
923 }
924
925 trig = (enum gpio_int_trig)(flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1 | GPIO_INT_WAKEUP));
926#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
927 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE |
928 GPIO_INT_ENABLE_DISABLE_ONLY));
929#else
930 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE));
931#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
932
933 ret = api->pin_interrupt_configure(port, pin, mode, trig);
934 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, interrupt_configure, port, pin, ret);
935 return ret;
936}
937
953static inline int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec,
955{
956 return gpio_pin_interrupt_configure(spec->port, spec->pin, flags);
957}
958
974__syscall int gpio_pin_configure(const struct device *port,
975 gpio_pin_t pin,
977
978static inline int z_impl_gpio_pin_configure(const struct device *port,
979 gpio_pin_t pin,
981{
982 const struct gpio_driver_api *api =
983 (const struct gpio_driver_api *)port->api;
984 __unused const struct gpio_driver_config *const cfg =
985 (const struct gpio_driver_config *)port->config;
986 struct gpio_driver_data *data =
987 (struct gpio_driver_data *)port->data;
988 int ret;
989
990 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, configure, port, pin, flags);
991
992 __ASSERT((flags & GPIO_INT_MASK) == 0,
993 "Interrupt flags are not supported");
994
995 __ASSERT((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) !=
997 "Pull Up and Pull Down should not be enabled simultaneously");
998
999 __ASSERT(!((flags & GPIO_INPUT) && !(flags & GPIO_OUTPUT) && (flags & GPIO_SINGLE_ENDED)),
1000 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
1001
1002 __ASSERT_NO_MSG((flags & GPIO_SINGLE_ENDED) != 0 ||
1003 (flags & GPIO_LINE_OPEN_DRAIN) == 0);
1004
1005 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
1006 || (flags & GPIO_OUTPUT) != 0,
1007 "Output needs to be enabled to be initialized low or high");
1008
1009 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
1010 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
1011 "Output cannot be initialized low and high");
1012
1013 if (((flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
1014 && ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
1015 && ((flags & GPIO_ACTIVE_LOW) != 0)) {
1016 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
1017 }
1018
1019 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
1020
1021 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1022 "Unsupported pin");
1023
1024 if ((flags & GPIO_ACTIVE_LOW) != 0) {
1025 data->invert |= (gpio_port_pins_t)BIT(pin);
1026 } else {
1027 data->invert &= ~(gpio_port_pins_t)BIT(pin);
1028 }
1029
1030 ret = api->pin_configure(port, pin, flags);
1031 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, configure, port, pin, ret);
1032 return ret;
1033}
1034
1046static inline int gpio_pin_configure_dt(const struct gpio_dt_spec *spec,
1047 gpio_flags_t extra_flags)
1048{
1049 return gpio_pin_configure(spec->port,
1050 spec->pin,
1051 spec->dt_flags | extra_flags);
1052}
1053
1072__syscall int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1073 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
1074
1075#ifdef CONFIG_GPIO_GET_DIRECTION
1076static inline int z_impl_gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1077 gpio_port_pins_t *inputs,
1078 gpio_port_pins_t *outputs)
1079{
1080 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1081 int ret;
1082
1083 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, get_direction, port, map, inputs, outputs);
1084
1085 if (api->port_get_direction == NULL) {
1086 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_direction, port, -ENOSYS);
1087 return -ENOSYS;
1088 }
1089
1090 ret = api->port_get_direction(port, map, inputs, outputs);
1091 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_direction, port, ret);
1092 return ret;
1093}
1094#endif /* CONFIG_GPIO_GET_DIRECTION */
1095
1108static inline int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
1109{
1110 int rv;
1111 gpio_port_pins_t pins;
1112 __unused const struct gpio_driver_config *cfg =
1113 (const struct gpio_driver_config *)port->config;
1114
1115 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1116
1117 rv = gpio_port_get_direction(port, BIT(pin), &pins, NULL);
1118 if (rv < 0) {
1119 return rv;
1120 }
1121
1122 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1123}
1124
1136static inline int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
1137{
1138 return gpio_pin_is_input(spec->port, spec->pin);
1139}
1140
1153static inline int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
1154{
1155 int rv;
1156 gpio_port_pins_t pins;
1157 __unused const struct gpio_driver_config *cfg =
1158 (const struct gpio_driver_config *)port->config;
1159
1160 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1161
1162 rv = gpio_port_get_direction(port, BIT(pin), NULL, &pins);
1163 if (rv < 0) {
1164 return rv;
1165 }
1166
1167 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1168}
1169
1181static inline int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
1182{
1183 return gpio_pin_is_output(spec->port, spec->pin);
1184}
1185
1201__syscall int gpio_pin_get_config(const struct device *port, gpio_pin_t pin,
1203
1204#ifdef CONFIG_GPIO_GET_CONFIG
1205static inline int z_impl_gpio_pin_get_config(const struct device *port,
1206 gpio_pin_t pin,
1208{
1209 const struct gpio_driver_api *api =
1210 (const struct gpio_driver_api *)port->api;
1211 int ret;
1212
1213 SYS_PORT_TRACING_FUNC_ENTER(gpio_pin, get_config, port, pin, *flags);
1214
1215 if (api->pin_get_config == NULL) {
1216 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, get_config, port, pin, -ENOSYS);
1217 return -ENOSYS;
1218 }
1219
1220 ret = api->pin_get_config(port, pin, flags);
1221 SYS_PORT_TRACING_FUNC_EXIT(gpio_pin, get_config, port, pin, ret);
1222 return ret;
1223}
1224#endif
1225
1238static inline int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec,
1240{
1241 return gpio_pin_get_config(spec->port, spec->pin, flags);
1242}
1243
1261__syscall int gpio_port_get_raw(const struct device *port,
1262 gpio_port_value_t *value);
1263
1264static inline int z_impl_gpio_port_get_raw(const struct device *port, gpio_port_value_t *value)
1265{
1266 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1267 int ret;
1268
1269 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, get_raw, port, value);
1270
1271 ret = api->port_get_raw(port, value);
1272 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, get_raw, port, ret);
1273 return ret;
1274}
1275
1294static inline int gpio_port_get(const struct device *port,
1295 gpio_port_value_t *value)
1296{
1297 const struct gpio_driver_data *const data =
1298 (const struct gpio_driver_data *)port->data;
1299 int ret;
1300
1301 ret = gpio_port_get_raw(port, value);
1302 if (ret == 0) {
1303 *value ^= data->invert;
1304 }
1305
1306 return ret;
1307}
1308
1326__syscall int gpio_port_set_masked_raw(const struct device *port,
1327 gpio_port_pins_t mask,
1328 gpio_port_value_t value);
1329
1330static inline int z_impl_gpio_port_set_masked_raw(const struct device *port,
1331 gpio_port_pins_t mask,
1332 gpio_port_value_t value)
1333{
1334 const struct gpio_driver_api *api =
1335 (const struct gpio_driver_api *)port->api;
1336 int ret;
1337
1338 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, set_masked_raw, port, mask, value);
1339
1340 ret = api->port_set_masked_raw(port, mask, value);
1341 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, set_masked_raw, port, ret);
1342 return ret;
1343}
1344
1365static inline int gpio_port_set_masked(const struct device *port,
1366 gpio_port_pins_t mask,
1367 gpio_port_value_t value)
1368{
1369 const struct gpio_driver_data *const data =
1370 (const struct gpio_driver_data *)port->data;
1371
1372 value ^= data->invert;
1373
1374 return gpio_port_set_masked_raw(port, mask, value);
1375}
1376
1387__syscall int gpio_port_set_bits_raw(const struct device *port,
1388 gpio_port_pins_t pins);
1389
1390static inline int z_impl_gpio_port_set_bits_raw(const struct device *port,
1391 gpio_port_pins_t pins)
1392{
1393 const struct gpio_driver_api *api =
1394 (const struct gpio_driver_api *)port->api;
1395 int ret;
1396
1397 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, set_bits_raw, port, pins);
1398
1399 ret = api->port_set_bits_raw(port, pins);
1400 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, set_bits_raw, port, ret);
1401 return ret;
1402}
1403
1414static inline int gpio_port_set_bits(const struct device *port,
1415 gpio_port_pins_t pins)
1416{
1417 return gpio_port_set_masked(port, pins, pins);
1418}
1419
1430__syscall int gpio_port_clear_bits_raw(const struct device *port,
1431 gpio_port_pins_t pins);
1432
1433static inline int z_impl_gpio_port_clear_bits_raw(const struct device *port,
1434 gpio_port_pins_t pins)
1435{
1436 const struct gpio_driver_api *api =
1437 (const struct gpio_driver_api *)port->api;
1438 int ret;
1439
1440 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, clear_bits_raw, port, pins);
1441
1442 ret = api->port_clear_bits_raw(port, pins);
1443 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, clear_bits_raw, port, ret);
1444 return ret;
1445}
1446
1457static inline int gpio_port_clear_bits(const struct device *port,
1458 gpio_port_pins_t pins)
1459{
1460 return gpio_port_set_masked(port, pins, 0);
1461}
1462
1473__syscall int gpio_port_toggle_bits(const struct device *port,
1474 gpio_port_pins_t pins);
1475
1476static inline int z_impl_gpio_port_toggle_bits(const struct device *port,
1477 gpio_port_pins_t pins)
1478{
1479 const struct gpio_driver_api *api =
1480 (const struct gpio_driver_api *)port->api;
1481 int ret;
1482
1483 SYS_PORT_TRACING_FUNC_ENTER(gpio_port, toggle_bits, port, pins);
1484
1485 ret = api->port_toggle_bits(port, pins);
1486 SYS_PORT_TRACING_FUNC_EXIT(gpio_port, toggle_bits, port, ret);
1487 return ret;
1488}
1489
1501static inline int gpio_port_set_clr_bits_raw(const struct device *port,
1502 gpio_port_pins_t set_pins,
1503 gpio_port_pins_t clear_pins)
1504{
1505 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1506
1507 return gpio_port_set_masked_raw(port, set_pins | clear_pins, set_pins);
1508}
1509
1521static inline int gpio_port_set_clr_bits(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(port, set_pins | clear_pins, set_pins);
1528}
1529
1545static inline int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
1546{
1547 __unused const struct gpio_driver_config *const cfg =
1548 (const struct gpio_driver_config *)port->config;
1549 gpio_port_value_t value;
1550 int ret;
1551
1552 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1553 "Unsupported pin");
1554
1555 ret = gpio_port_get_raw(port, &value);
1556 if (ret == 0) {
1557 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1558 }
1559
1560 return ret;
1561}
1562
1582static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)
1583{
1584 __unused const struct gpio_driver_config *const cfg =
1585 (const struct gpio_driver_config *)port->config;
1586 gpio_port_value_t value;
1587 int ret;
1588
1589 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1590 "Unsupported pin");
1591
1592 ret = gpio_port_get(port, &value);
1593 if (ret == 0) {
1594 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1595 }
1596
1597 return ret;
1598}
1599
1610static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
1611{
1612 return gpio_pin_get(spec->port, spec->pin);
1613}
1614
1630static inline int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin,
1631 int value)
1632{
1633 __unused const struct gpio_driver_config *const cfg =
1634 (const struct gpio_driver_config *)port->config;
1635 int ret;
1636
1637 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1638 "Unsupported pin");
1639
1640 if (value != 0) {
1641 ret = gpio_port_set_bits_raw(port, (gpio_port_pins_t)BIT(pin));
1642 } else {
1644 }
1645
1646 return ret;
1647}
1648
1670static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin,
1671 int value)
1672{
1673 __unused const struct gpio_driver_config *const cfg =
1674 (const struct gpio_driver_config *)port->config;
1675 const struct gpio_driver_data *const data =
1676 (const struct gpio_driver_data *)port->data;
1677
1678 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1679 "Unsupported pin");
1680
1681 if (data->invert & (gpio_port_pins_t)BIT(pin)) {
1682 value = (value != 0) ? 0 : 1;
1683 }
1684
1685 return gpio_pin_set_raw(port, pin, value);
1686}
1687
1699static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
1700{
1701 return gpio_pin_set(spec->port, spec->pin, value);
1702}
1703
1714static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
1715{
1716 __unused const struct gpio_driver_config *const cfg =
1717 (const struct gpio_driver_config *)port->config;
1718
1719 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1720 "Unsupported pin");
1721
1722 return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin));
1723}
1724
1735static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
1736{
1737 return gpio_pin_toggle(spec->port, spec->pin);
1738}
1739
1746static inline void gpio_init_callback(struct gpio_callback *callback,
1748 gpio_port_pins_t pin_mask)
1749{
1750 SYS_PORT_TRACING_FUNC_ENTER(gpio, init_callback, callback, handler, pin_mask);
1751
1752 __ASSERT(callback, "Callback pointer should not be NULL");
1753 __ASSERT(handler, "Callback handler pointer should not be NULL");
1754
1755 callback->handler = handler;
1756 callback->pin_mask = pin_mask;
1757
1758 SYS_PORT_TRACING_FUNC_EXIT(gpio, init_callback, callback);
1759}
1760
1775static inline int gpio_add_callback(const struct device *port,
1776 struct gpio_callback *callback)
1777{
1778 const struct gpio_driver_api *api =
1779 (const struct gpio_driver_api *)port->api;
1780 int ret;
1781
1782 SYS_PORT_TRACING_FUNC_ENTER(gpio, add_callback, port, callback);
1783
1784 if (api->manage_callback == NULL) {
1785 SYS_PORT_TRACING_FUNC_EXIT(gpio, add_callback, port, -ENOSYS);
1786 return -ENOSYS;
1787 }
1788
1789 ret = api->manage_callback(port, callback, true);
1790 SYS_PORT_TRACING_FUNC_EXIT(gpio, add_callback, port, ret);
1791 return ret;
1792}
1793
1805static inline int gpio_add_callback_dt(const struct gpio_dt_spec *spec,
1806 struct gpio_callback *callback)
1807{
1808 return gpio_add_callback(spec->port, callback);
1809}
1810
1829static inline int gpio_remove_callback(const struct device *port,
1830 struct gpio_callback *callback)
1831{
1832 const struct gpio_driver_api *api =
1833 (const struct gpio_driver_api *)port->api;
1834 int ret;
1835
1836 SYS_PORT_TRACING_FUNC_ENTER(gpio, remove_callback, port, callback);
1837
1838 if (api->manage_callback == NULL) {
1839 SYS_PORT_TRACING_FUNC_EXIT(gpio, remove_callback, port, -ENOSYS);
1840 return -ENOSYS;
1841 }
1842
1843 ret = api->manage_callback(port, callback, false);
1844 SYS_PORT_TRACING_FUNC_EXIT(gpio, remove_callback, port, ret);
1845 return ret;
1846}
1847
1859static inline int gpio_remove_callback_dt(const struct gpio_dt_spec *spec,
1860 struct gpio_callback *callback)
1861{
1862 return gpio_remove_callback(spec->port, callback);
1863}
1864
1879__syscall int gpio_get_pending_int(const struct device *dev);
1880
1881static inline int z_impl_gpio_get_pending_int(const struct device *dev)
1882{
1883 const struct gpio_driver_api *api =
1884 (const struct gpio_driver_api *)dev->api;
1885 int ret;
1886
1887 SYS_PORT_TRACING_FUNC_ENTER(gpio, get_pending_int, dev);
1888
1889 if (api->get_pending_int == NULL) {
1890 SYS_PORT_TRACING_FUNC_EXIT(gpio, get_pending_int, dev, -ENOSYS);
1891 return -ENOSYS;
1892 }
1893
1894 ret = api->get_pending_int(dev);
1895 SYS_PORT_TRACING_FUNC_EXIT(gpio, get_pending_int, dev, ret);
1896 return ret;
1897}
1898
1903#ifdef __cplusplus
1904}
1905#endif
1906
1907#include <zephyr/syscalls/gpio.h>
1908
1909#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:1775
#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:1545
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:1108
static int gpio_pin_get(const struct device *port, gpio_pin_t pin)
Get logical level of an input pin.
Definition gpio.h:1582
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:1181
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:953
static int gpio_remove_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1859
static int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
Toggle pin level from a gpio_dt_spec.
Definition gpio.h:1735
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:1153
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:1046
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:1699
static int gpio_add_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Add an application callback.
Definition gpio.h:1805
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:1414
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:1501
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:1521
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:1746
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:1136
#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:1238
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:1610
static int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
Toggle pin level.
Definition gpio.h:1714
static bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
Validate that GPIO port is ready.
Definition gpio.h:837
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:1670
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:1457
static int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1829
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:1365
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:1630
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:1294
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
#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: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