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>
22
23#include <zephyr/types.h>
24#include <stddef.h>
25#include <zephyr/device.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
47#define GPIO_INPUT (1U << 16)
48
50#define GPIO_OUTPUT (1U << 17)
51
53#define GPIO_DISCONNECTED 0
54
57/* Initializes output to a low state. */
58#define GPIO_OUTPUT_INIT_LOW (1U << 18)
59
60/* Initializes output to a high state. */
61#define GPIO_OUTPUT_INIT_HIGH (1U << 19)
62
63/* Initializes output based on logic level */
64#define GPIO_OUTPUT_INIT_LOGICAL (1U << 20)
65
69#define GPIO_OUTPUT_LOW (GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW)
71#define GPIO_OUTPUT_HIGH (GPIO_OUTPUT | GPIO_OUTPUT_INIT_HIGH)
73#define GPIO_OUTPUT_INACTIVE (GPIO_OUTPUT | \
74 GPIO_OUTPUT_INIT_LOW | \
75 GPIO_OUTPUT_INIT_LOGICAL)
77#define GPIO_OUTPUT_ACTIVE (GPIO_OUTPUT | \
78 GPIO_OUTPUT_INIT_HIGH | \
79 GPIO_OUTPUT_INIT_LOGICAL)
80
100#define GPIO_INT_DISABLE (1U << 21)
101
104/* Enables GPIO pin interrupt. */
105#define GPIO_INT_ENABLE (1U << 22)
106
107/* GPIO interrupt is sensitive to logical levels.
108 *
109 * This is a component flag that should be combined with other
110 * `GPIO_INT_*` flags to produce a meaningful configuration.
111 */
112#define GPIO_INT_LEVELS_LOGICAL (1U << 23)
113
114/* GPIO interrupt is edge sensitive.
115 *
116 * Note: by default interrupts are level sensitive.
117 *
118 * This is a component flag that should be combined with other
119 * `GPIO_INT_*` flags to produce a meaningful configuration.
120 */
121#define GPIO_INT_EDGE (1U << 24)
122
123/* Trigger detection when input state is (or transitions to) physical low or
124 * logical 0 level.
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_LOW_0 (1U << 25)
130
131/* Trigger detection on input state is (or transitions to) physical high or
132 * logical 1 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_HIGH_1 (1U << 26)
138
139#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
140/* Disable/Enable interrupt functionality without changing other interrupt
141 * related register, such as clearing the pending register.
142 *
143 * This is a component flag that should be combined with `GPIO_INT_ENABLE` or
144 * `GPIO_INT_DISABLE` flags to produce a meaningful configuration.
145 */
146#define GPIO_INT_ENABLE_DISABLE_ONLY (1u << 27)
147#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
148
149#define GPIO_INT_MASK (GPIO_INT_DISABLE | \
150 GPIO_INT_ENABLE | \
151 GPIO_INT_LEVELS_LOGICAL | \
152 GPIO_INT_EDGE | \
153 GPIO_INT_LOW_0 | \
154 GPIO_INT_HIGH_1)
155
160#define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
161 GPIO_INT_EDGE | \
162 GPIO_INT_HIGH_1)
163
167#define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
168 GPIO_INT_EDGE | \
169 GPIO_INT_LOW_0)
170
174#define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
175 GPIO_INT_EDGE | \
176 GPIO_INT_LOW_0 | \
177 GPIO_INT_HIGH_1)
178
182#define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
183 GPIO_INT_LOW_0)
184
188#define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
189 GPIO_INT_HIGH_1)
190
194#define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
195 GPIO_INT_LEVELS_LOGICAL | \
196 GPIO_INT_EDGE | \
197 GPIO_INT_LOW_0)
198
202#define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
203 GPIO_INT_LEVELS_LOGICAL | \
204 GPIO_INT_EDGE | \
205 GPIO_INT_HIGH_1)
206
210#define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
211 GPIO_INT_LEVELS_LOGICAL | \
212 GPIO_INT_LOW_0)
213
217#define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
218 GPIO_INT_LEVELS_LOGICAL | \
219 GPIO_INT_HIGH_1)
220
224#define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
234
247
255
267
275
296
331#define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
332 { \
333 .port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)),\
334 .pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
335 .dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
336 }
337
355#define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
356 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
357 (GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
358 (default_value))
359
368#define GPIO_DT_SPEC_GET(node_id, prop) \
369 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
370
381#define GPIO_DT_SPEC_GET_OR(node_id, prop, default_value) \
382 GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
383
394#define GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
395 GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
396
408#define GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
409 COND_CODE_1(DT_PROP_HAS_IDX(DT_DRV_INST(inst), prop, idx), \
410 (GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)), \
411 (default_value))
412
421#define GPIO_DT_SPEC_INST_GET(inst, prop) \
422 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
423
434#define GPIO_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
435 GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value)
436
437/*
438 * @cond INTERNAL_HIDDEN
439 */
440
451#define Z_GPIO_GEN_BITMASK_COND(node_id, prop, off_idx, sz_idx) \
452 COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, off_idx), \
453 (COND_CODE_0(DT_PROP_BY_IDX(node_id, prop, sz_idx), \
454 (0), \
455 (GENMASK64(DT_PROP_BY_IDX(node_id, prop, off_idx) + \
456 DT_PROP_BY_IDX(node_id, prop, sz_idx) - 1, \
457 DT_PROP_BY_IDX(node_id, prop, off_idx)))) \
458 ), (0))
459
467#define Z_GPIO_GEN_RESERVED_RANGES_COND(odd_it, node_id) \
468 COND_CODE_1(DT_PROP_HAS_IDX(node_id, gpio_reserved_ranges, odd_it), \
469 (Z_GPIO_GEN_BITMASK_COND(node_id, \
470 gpio_reserved_ranges, \
471 GET_ARG_N(odd_it, Z_SPARSE_LIST_EVEN_NUMBERS), \
472 odd_it)), \
473 (0))
474
566#define GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios) \
567 ((gpio_port_pins_t) \
568 COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
569 (GENMASK64(BITS_PER_LONG_LONG - 1, ngpios) \
570 | FOR_EACH_FIXED_ARG(Z_GPIO_GEN_RESERVED_RANGES_COND, \
571 (|), \
572 node_id, \
573 LIST_DROP_EMPTY(Z_SPARSE_LIST_ODD_NUMBERS))), \
574 (0)))
575
583#define GPIO_DT_RESERVED_RANGES(node_id) \
584 GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, DT_PROP(node_id, ngpios))
585
595#define GPIO_DT_INST_RESERVED_RANGES_NGPIOS(inst, ngpios) \
596 GPIO_DT_RESERVED_RANGES_NGPIOS(DT_DRV_INST(inst), ngpios)
597
606#define GPIO_DT_INST_RESERVED_RANGES(inst) \
607 GPIO_DT_RESERVED_RANGES(DT_DRV_INST(inst))
608
657#define GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(node_id, ngpios) \
658 ((gpio_port_pins_t) \
659 COND_CODE_0(ngpios, \
660 (0), \
661 (COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
662 ((GENMASK64(ngpios - 1, 0) & \
663 ~GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios))), \
664 (GENMASK64(ngpios - 1, 0))) \
665 ) \
666 ))
667
677#define GPIO_DT_INST_PORT_PIN_MASK_NGPIOS_EXC(inst, ngpios) \
678 GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(DT_DRV_INST(inst), ngpios)
679
683#define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
684
698
711
712struct gpio_callback;
713
726typedef void (*gpio_callback_handler_t)(const struct device *port,
727 struct gpio_callback *cb,
728 gpio_port_pins_t pins);
729
757
764/* Used by driver api function pin_interrupt_configure, these are defined
765 * in terms of the public flags so we can just mask and pass them
766 * through to the driver api
767 */
768enum gpio_int_mode {
769 GPIO_INT_MODE_DISABLED = GPIO_INT_DISABLE,
770 GPIO_INT_MODE_LEVEL = GPIO_INT_ENABLE,
771 GPIO_INT_MODE_EDGE = GPIO_INT_ENABLE | GPIO_INT_EDGE,
772#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
773 GPIO_INT_MODE_DISABLE_ONLY = GPIO_INT_DISABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
774 GPIO_INT_MODE_ENABLE_ONLY = GPIO_INT_ENABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
775#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
776};
777
778enum gpio_int_trig {
779 /* Trigger detection when input state is (or transitions to)
780 * physical low. (Edge Falling or Active Low)
781 */
782 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
783 /* Trigger detection when input state is (or transitions to)
784 * physical high. (Edge Rising or Active High) */
785 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
786 /* Trigger detection on pin rising or falling edge. */
787 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
788 /* Trigger a system wakeup. */
789 GPIO_INT_TRIG_WAKE = GPIO_INT_WAKEUP,
790};
791
792__subsystem struct gpio_driver_api {
793 int (*pin_configure)(const struct device *port, gpio_pin_t pin,
795#ifdef CONFIG_GPIO_GET_CONFIG
796 int (*pin_get_config)(const struct device *port, gpio_pin_t pin,
798#endif
799 int (*port_get_raw)(const struct device *port,
800 gpio_port_value_t *value);
801 int (*port_set_masked_raw)(const struct device *port,
802 gpio_port_pins_t mask,
803 gpio_port_value_t value);
804 int (*port_set_bits_raw)(const struct device *port,
805 gpio_port_pins_t pins);
806 int (*port_clear_bits_raw)(const struct device *port,
807 gpio_port_pins_t pins);
808 int (*port_toggle_bits)(const struct device *port,
809 gpio_port_pins_t pins);
810 int (*pin_interrupt_configure)(const struct device *port,
811 gpio_pin_t pin,
812 enum gpio_int_mode, enum gpio_int_trig);
813 int (*manage_callback)(const struct device *port,
814 struct gpio_callback *cb,
815 bool set);
816 uint32_t (*get_pending_int)(const struct device *dev);
817#ifdef CONFIG_GPIO_GET_DIRECTION
818 int (*port_get_direction)(const struct device *port, gpio_port_pins_t map,
819 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
820#endif /* CONFIG_GPIO_GET_DIRECTION */
821};
822
835static inline bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
836{
837 /* Validate port is ready */
838 return device_is_ready(spec->port);
839}
840
864__syscall int gpio_pin_interrupt_configure(const struct device *port,
865 gpio_pin_t pin,
867
868static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
869 gpio_pin_t pin,
871{
872 const struct gpio_driver_api *api =
873 (const struct gpio_driver_api *)port->api;
874 __unused const struct gpio_driver_config *const cfg =
875 (const struct gpio_driver_config *)port->config;
876 const struct gpio_driver_data *const data =
877 (const struct gpio_driver_data *)port->data;
878 enum gpio_int_trig trig;
879 enum gpio_int_mode mode;
880
881 if (api->pin_interrupt_configure == NULL) {
882 return -ENOSYS;
883 }
884
885 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE))
886 != (GPIO_INT_DISABLE | GPIO_INT_ENABLE),
887 "Cannot both enable and disable interrupts");
888
889 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE)) != 0U,
890 "Must either enable or disable interrupts");
891
892 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
893 ((flags & GPIO_INT_EDGE) != 0) ||
894 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
895 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
896 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
897 "enabled for a level interrupt.");
898
899 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
900#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
901 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
902 (flags & GPIO_INT_ENABLE_DISABLE_ONLY) != 0,
903#else
904 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0),
905#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
906 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be "
907 "enabled.");
908
909 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
910 "Unsupported pin");
911
912 if (((flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
913 ((data->invert & (gpio_port_pins_t)BIT(pin)) != 0)) {
914 /* Invert signal bits */
915 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
916 }
917
918 trig = (enum gpio_int_trig)(flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1 | GPIO_INT_WAKEUP));
919#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
920 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE |
921 GPIO_INT_ENABLE_DISABLE_ONLY));
922#else
923 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE));
924#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
925
926 return api->pin_interrupt_configure(port, pin, mode, trig);
927}
928
944static inline int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec,
946{
947 return gpio_pin_interrupt_configure(spec->port, spec->pin, flags);
948}
949
965__syscall int gpio_pin_configure(const struct device *port,
966 gpio_pin_t pin,
968
969static inline int z_impl_gpio_pin_configure(const struct device *port,
970 gpio_pin_t pin,
972{
973 const struct gpio_driver_api *api =
974 (const struct gpio_driver_api *)port->api;
975 __unused const struct gpio_driver_config *const cfg =
976 (const struct gpio_driver_config *)port->config;
977 struct gpio_driver_data *data =
978 (struct gpio_driver_data *)port->data;
979
980 __ASSERT((flags & GPIO_INT_MASK) == 0,
981 "Interrupt flags are not supported");
982
983 __ASSERT((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) !=
985 "Pull Up and Pull Down should not be enabled simultaneously");
986
987 __ASSERT(!((flags & GPIO_INPUT) && !(flags & GPIO_OUTPUT) && (flags & GPIO_SINGLE_ENDED)),
988 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
989
990 __ASSERT_NO_MSG((flags & GPIO_SINGLE_ENDED) != 0 ||
991 (flags & GPIO_LINE_OPEN_DRAIN) == 0);
992
993 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
994 || (flags & GPIO_OUTPUT) != 0,
995 "Output needs to be enabled to be initialized low or high");
996
997 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
998 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
999 "Output cannot be initialized low and high");
1000
1001 if (((flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
1002 && ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
1003 && ((flags & GPIO_ACTIVE_LOW) != 0)) {
1004 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
1005 }
1006
1007 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
1008
1009 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1010 "Unsupported pin");
1011
1012 if ((flags & GPIO_ACTIVE_LOW) != 0) {
1013 data->invert |= (gpio_port_pins_t)BIT(pin);
1014 } else {
1015 data->invert &= ~(gpio_port_pins_t)BIT(pin);
1016 }
1017
1018 return api->pin_configure(port, pin, flags);
1019}
1020
1032static inline int gpio_pin_configure_dt(const struct gpio_dt_spec *spec,
1033 gpio_flags_t extra_flags)
1034{
1035 return gpio_pin_configure(spec->port,
1036 spec->pin,
1037 spec->dt_flags | extra_flags);
1038}
1039
1058__syscall int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1059 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
1060
1061#ifdef CONFIG_GPIO_GET_DIRECTION
1062static inline int z_impl_gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1063 gpio_port_pins_t *inputs,
1064 gpio_port_pins_t *outputs)
1065{
1066 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1067
1068 if (api->port_get_direction == NULL) {
1069 return -ENOSYS;
1070 }
1071
1072 return api->port_get_direction(port, map, inputs, outputs);
1073}
1074#endif /* CONFIG_GPIO_GET_DIRECTION */
1075
1088static inline int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
1089{
1090 int rv;
1091 gpio_port_pins_t pins;
1092 __unused const struct gpio_driver_config *cfg =
1093 (const struct gpio_driver_config *)port->config;
1094
1095 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1096
1097 rv = gpio_port_get_direction(port, BIT(pin), &pins, NULL);
1098 if (rv < 0) {
1099 return rv;
1100 }
1101
1102 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1103}
1104
1116static inline int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
1117{
1118 return gpio_pin_is_input(spec->port, spec->pin);
1119}
1120
1133static inline int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
1134{
1135 int rv;
1136 gpio_port_pins_t pins;
1137 __unused const struct gpio_driver_config *cfg =
1138 (const struct gpio_driver_config *)port->config;
1139
1140 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1141
1142 rv = gpio_port_get_direction(port, BIT(pin), NULL, &pins);
1143 if (rv < 0) {
1144 return rv;
1145 }
1146
1147 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1148}
1149
1161static inline int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
1162{
1163 return gpio_pin_is_output(spec->port, spec->pin);
1164}
1165
1181__syscall int gpio_pin_get_config(const struct device *port, gpio_pin_t pin,
1183
1184#ifdef CONFIG_GPIO_GET_CONFIG
1185static inline int z_impl_gpio_pin_get_config(const struct device *port,
1186 gpio_pin_t pin,
1188{
1189 const struct gpio_driver_api *api =
1190 (const struct gpio_driver_api *)port->api;
1191
1192 if (api->pin_get_config == NULL)
1193 return -ENOSYS;
1194
1195 return api->pin_get_config(port, pin, flags);
1196}
1197#endif
1198
1211static inline int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec,
1213{
1214 return gpio_pin_get_config(spec->port, spec->pin, flags);
1215}
1216
1234__syscall int gpio_port_get_raw(const struct device *port,
1235 gpio_port_value_t *value);
1236
1237static inline int z_impl_gpio_port_get_raw(const struct device *port,
1238 gpio_port_value_t *value)
1239{
1240 const struct gpio_driver_api *api =
1241 (const struct gpio_driver_api *)port->api;
1242
1243 return api->port_get_raw(port, value);
1244}
1245
1264static inline int gpio_port_get(const struct device *port,
1265 gpio_port_value_t *value)
1266{
1267 const struct gpio_driver_data *const data =
1268 (const struct gpio_driver_data *)port->data;
1269 int ret;
1270
1271 ret = gpio_port_get_raw(port, value);
1272 if (ret == 0) {
1273 *value ^= data->invert;
1274 }
1275
1276 return ret;
1277}
1278
1296__syscall int gpio_port_set_masked_raw(const struct device *port,
1297 gpio_port_pins_t mask,
1298 gpio_port_value_t value);
1299
1300static inline int z_impl_gpio_port_set_masked_raw(const struct device *port,
1301 gpio_port_pins_t mask,
1302 gpio_port_value_t value)
1303{
1304 const struct gpio_driver_api *api =
1305 (const struct gpio_driver_api *)port->api;
1306
1307 return api->port_set_masked_raw(port, mask, value);
1308}
1309
1330static inline int gpio_port_set_masked(const struct device *port,
1331 gpio_port_pins_t mask,
1332 gpio_port_value_t value)
1333{
1334 const struct gpio_driver_data *const data =
1335 (const struct gpio_driver_data *)port->data;
1336
1337 value ^= data->invert;
1338
1339 return gpio_port_set_masked_raw(port, mask, value);
1340}
1341
1352__syscall int gpio_port_set_bits_raw(const struct device *port,
1353 gpio_port_pins_t pins);
1354
1355static inline int z_impl_gpio_port_set_bits_raw(const struct device *port,
1356 gpio_port_pins_t pins)
1357{
1358 const struct gpio_driver_api *api =
1359 (const struct gpio_driver_api *)port->api;
1360
1361 return api->port_set_bits_raw(port, pins);
1362}
1363
1374static inline int gpio_port_set_bits(const struct device *port,
1375 gpio_port_pins_t pins)
1376{
1377 return gpio_port_set_masked(port, pins, pins);
1378}
1379
1390__syscall int gpio_port_clear_bits_raw(const struct device *port,
1391 gpio_port_pins_t pins);
1392
1393static inline int z_impl_gpio_port_clear_bits_raw(const struct device *port,
1394 gpio_port_pins_t pins)
1395{
1396 const struct gpio_driver_api *api =
1397 (const struct gpio_driver_api *)port->api;
1398
1399 return api->port_clear_bits_raw(port, pins);
1400}
1401
1412static inline int gpio_port_clear_bits(const struct device *port,
1413 gpio_port_pins_t pins)
1414{
1415 return gpio_port_set_masked(port, pins, 0);
1416}
1417
1428__syscall int gpio_port_toggle_bits(const struct device *port,
1429 gpio_port_pins_t pins);
1430
1431static inline int z_impl_gpio_port_toggle_bits(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
1437 return api->port_toggle_bits(port, pins);
1438}
1439
1451static inline int gpio_port_set_clr_bits_raw(const struct device *port,
1452 gpio_port_pins_t set_pins,
1453 gpio_port_pins_t clear_pins)
1454{
1455 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1456
1457 return gpio_port_set_masked_raw(port, set_pins | clear_pins, set_pins);
1458}
1459
1471static inline int gpio_port_set_clr_bits(const struct device *port,
1472 gpio_port_pins_t set_pins,
1473 gpio_port_pins_t clear_pins)
1474{
1475 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1476
1477 return gpio_port_set_masked(port, set_pins | clear_pins, set_pins);
1478}
1479
1495static inline int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
1496{
1497 __unused const struct gpio_driver_config *const cfg =
1498 (const struct gpio_driver_config *)port->config;
1499 gpio_port_value_t value;
1500 int ret;
1501
1502 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1503 "Unsupported pin");
1504
1505 ret = gpio_port_get_raw(port, &value);
1506 if (ret == 0) {
1507 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1508 }
1509
1510 return ret;
1511}
1512
1532static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)
1533{
1534 __unused const struct gpio_driver_config *const cfg =
1535 (const struct gpio_driver_config *)port->config;
1536 gpio_port_value_t value;
1537 int ret;
1538
1539 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1540 "Unsupported pin");
1541
1542 ret = gpio_port_get(port, &value);
1543 if (ret == 0) {
1544 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1545 }
1546
1547 return ret;
1548}
1549
1560static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
1561{
1562 return gpio_pin_get(spec->port, spec->pin);
1563}
1564
1580static inline int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin,
1581 int value)
1582{
1583 __unused const struct gpio_driver_config *const cfg =
1584 (const struct gpio_driver_config *)port->config;
1585 int ret;
1586
1587 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1588 "Unsupported pin");
1589
1590 if (value != 0) {
1591 ret = gpio_port_set_bits_raw(port, (gpio_port_pins_t)BIT(pin));
1592 } else {
1594 }
1595
1596 return ret;
1597}
1598
1620static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin,
1621 int value)
1622{
1623 __unused const struct gpio_driver_config *const cfg =
1624 (const struct gpio_driver_config *)port->config;
1625 const struct gpio_driver_data *const data =
1626 (const struct gpio_driver_data *)port->data;
1627
1628 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1629 "Unsupported pin");
1630
1631 if (data->invert & (gpio_port_pins_t)BIT(pin)) {
1632 value = (value != 0) ? 0 : 1;
1633 }
1634
1635 return gpio_pin_set_raw(port, pin, value);
1636}
1637
1649static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
1650{
1651 return gpio_pin_set(spec->port, spec->pin, value);
1652}
1653
1664static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
1665{
1666 __unused const struct gpio_driver_config *const cfg =
1667 (const struct gpio_driver_config *)port->config;
1668
1669 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1670 "Unsupported pin");
1671
1672 return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin));
1673}
1674
1685static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
1686{
1687 return gpio_pin_toggle(spec->port, spec->pin);
1688}
1689
1696static inline void gpio_init_callback(struct gpio_callback *callback,
1698 gpio_port_pins_t pin_mask)
1699{
1700 __ASSERT(callback, "Callback pointer should not be NULL");
1701 __ASSERT(handler, "Callback handler pointer should not be NULL");
1702
1703 callback->handler = handler;
1704 callback->pin_mask = pin_mask;
1705}
1706
1721static inline int gpio_add_callback(const struct device *port,
1722 struct gpio_callback *callback)
1723{
1724 const struct gpio_driver_api *api =
1725 (const struct gpio_driver_api *)port->api;
1726
1727 if (api->manage_callback == NULL) {
1728 return -ENOSYS;
1729 }
1730
1731 return api->manage_callback(port, callback, true);
1732}
1733
1745static inline int gpio_add_callback_dt(const struct gpio_dt_spec *spec,
1746 struct gpio_callback *callback)
1747{
1748 return gpio_add_callback(spec->port, callback);
1749}
1750
1769static inline int gpio_remove_callback(const struct device *port,
1770 struct gpio_callback *callback)
1771{
1772 const struct gpio_driver_api *api =
1773 (const struct gpio_driver_api *)port->api;
1774
1775 if (api->manage_callback == NULL) {
1776 return -ENOSYS;
1777 }
1778
1779 return api->manage_callback(port, callback, false);
1780}
1781
1793static inline int gpio_remove_callback_dt(const struct gpio_dt_spec *spec,
1794 struct gpio_callback *callback)
1795{
1796 return gpio_remove_callback(spec->port, callback);
1797}
1798
1813__syscall int gpio_get_pending_int(const struct device *dev);
1814
1815static inline int z_impl_gpio_get_pending_int(const struct device *dev)
1816{
1817 const struct gpio_driver_api *api =
1818 (const struct gpio_driver_api *)dev->api;
1819
1820 if (api->get_pending_int == NULL) {
1821 return -ENOSYS;
1822 }
1823
1824 return api->get_pending_int(dev);
1825}
1826
1831#ifdef __cplusplus
1832}
1833#endif
1834
1835#include <zephyr/syscalls/gpio.h>
1836
1837#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:1721
#define GPIO_OUTPUT
Enables pin as output, no change to the output state.
Definition gpio.h:50
static int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
Get physical level of an input pin.
Definition gpio.h:1495
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:1088
static int gpio_pin_get(const struct device *port, gpio_pin_t pin)
Get logical level of an input pin.
Definition gpio.h:1532
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:1161
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:944
static int gpio_remove_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1793
static int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
Toggle pin level from a gpio_dt_spec.
Definition gpio.h:1685
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition gpio.h:254
static int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
Check if pin is configured for output.
Definition gpio.h:1133
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:1032
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:1649
static int gpio_add_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Add an application callback.
Definition gpio.h:1745
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:1374
uint32_t gpio_flags_t
Provides a type to hold GPIO configuration flags.
Definition gpio.h:274
#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:1451
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:1471
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:1696
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:1116
#define GPIO_INPUT
Enables pin as input.
Definition gpio.h:47
uint32_t gpio_port_pins_t
Identifies a set of pins associated with a port.
Definition gpio.h:233
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:1211
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:100
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:1560
static int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
Toggle pin level.
Definition gpio.h:1664
static bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
Validate that GPIO port is ready.
Definition gpio.h:835
uint32_t gpio_port_value_t
Provides values for a set of pins associated with a port.
Definition gpio.h:246
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:1620
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:1412
static int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)
Remove an application callback.
Definition gpio.h:1769
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:1330
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:266
#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:1580
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:1264
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:726
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
#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:412
void * data
Address of the device instance private data.
Definition device.h:422
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418
const void * config
Address of device instance config information.
Definition device.h:416
GPIO callback structure.
Definition gpio.h:740
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:744
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:755
gpio_callback_handler_t handler
Actual callback function being called when relevant.
Definition gpio.h:747
This structure is common to all GPIO drivers and is expected to be the first element in the object po...
Definition gpio.h:690
gpio_port_pins_t port_pin_mask
Mask identifying pins supported by the controller.
Definition gpio.h:696
This structure is common to all GPIO drivers and is expected to be the first element in the driver's ...
Definition gpio.h:703
gpio_port_pins_t invert
Mask identifying pins that are configured as active low.
Definition gpio.h:709
Container for GPIO pin information specified in devicetree.
Definition gpio.h:288
const struct device * port
GPIO device controlling the pin.
Definition gpio.h:290
gpio_pin_t pin
The pin's number on the device.
Definition gpio.h:292
gpio_dt_flags_t dt_flags
The pin's configuration flags as specified in devicetree.
Definition gpio.h:294