Zephyr Project API  3.3.0
A Scalable Open Source RTOS
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
45#define GPIO_INPUT (1U << 16)
46
48#define GPIO_OUTPUT (1U << 17)
49
51#define GPIO_DISCONNECTED 0
52
55/* Initializes output to a low state. */
56#define GPIO_OUTPUT_INIT_LOW (1U << 18)
57
58/* Initializes output to a high state. */
59#define GPIO_OUTPUT_INIT_HIGH (1U << 19)
60
61/* Initializes output based on logic level */
62#define GPIO_OUTPUT_INIT_LOGICAL (1U << 20)
63
67#define GPIO_OUTPUT_LOW (GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW)
69#define GPIO_OUTPUT_HIGH (GPIO_OUTPUT | GPIO_OUTPUT_INIT_HIGH)
71#define GPIO_OUTPUT_INACTIVE (GPIO_OUTPUT | \
72 GPIO_OUTPUT_INIT_LOW | \
73 GPIO_OUTPUT_INIT_LOGICAL)
75#define GPIO_OUTPUT_ACTIVE (GPIO_OUTPUT | \
76 GPIO_OUTPUT_INIT_HIGH | \
77 GPIO_OUTPUT_INIT_LOGICAL)
78
93#define GPIO_INT_DISABLE (1U << 21)
94
97/* Enables GPIO pin interrupt. */
98#define GPIO_INT_ENABLE (1U << 22)
99
100/* GPIO interrupt is sensitive to logical levels.
101 *
102 * This is a component flag that should be combined with other
103 * `GPIO_INT_*` flags to produce a meaningful configuration.
104 */
105#define GPIO_INT_LEVELS_LOGICAL (1U << 23)
106
107/* GPIO interrupt is edge sensitive.
108 *
109 * Note: by default interrupts are level sensitive.
110 *
111 * This is a component flag that should be combined with other
112 * `GPIO_INT_*` flags to produce a meaningful configuration.
113 */
114#define GPIO_INT_EDGE (1U << 24)
115
116/* Trigger detection when input state is (or transitions to) physical low or
117 * logical 0 level.
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_LOW_0 (1U << 25)
123
124/* Trigger detection on input state is (or transitions to) physical high or
125 * logical 1 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_HIGH_1 (1U << 26)
131
132#define GPIO_INT_MASK (GPIO_INT_DISABLE | \
133 GPIO_INT_ENABLE | \
134 GPIO_INT_LEVELS_LOGICAL | \
135 GPIO_INT_EDGE | \
136 GPIO_INT_LOW_0 | \
137 GPIO_INT_HIGH_1)
138
143#define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
144 GPIO_INT_EDGE | \
145 GPIO_INT_HIGH_1)
146
150#define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
151 GPIO_INT_EDGE | \
152 GPIO_INT_LOW_0)
153
157#define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
158 GPIO_INT_EDGE | \
159 GPIO_INT_LOW_0 | \
160 GPIO_INT_HIGH_1)
161
165#define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
166 GPIO_INT_LOW_0)
167
171#define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
172 GPIO_INT_HIGH_1)
173
177#define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
178 GPIO_INT_LEVELS_LOGICAL | \
179 GPIO_INT_EDGE | \
180 GPIO_INT_LOW_0)
181
185#define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
186 GPIO_INT_LEVELS_LOGICAL | \
187 GPIO_INT_EDGE | \
188 GPIO_INT_HIGH_1)
189
193#define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
194 GPIO_INT_LEVELS_LOGICAL | \
195 GPIO_INT_LOW_0)
196
200#define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
201 GPIO_INT_LEVELS_LOGICAL | \
202 GPIO_INT_HIGH_1)
203
207#define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
217
230
238
250
258
273 const struct device *port;
278};
279
314#define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
315 { \
316 .port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)),\
317 .pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
318 .dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
319 }
320
338#define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
339 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
340 (GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
341 (default_value))
342
351#define GPIO_DT_SPEC_GET(node_id, prop) \
352 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
353
364#define GPIO_DT_SPEC_GET_OR(node_id, prop, default_value) \
365 GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
366
377#define GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
378 GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
379
391#define GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
392 COND_CODE_1(DT_PROP_HAS_IDX(DT_DRV_INST(inst), prop, idx), \
393 (GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)), \
394 (default_value))
395
404#define GPIO_DT_SPEC_INST_GET(inst, prop) \
405 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
406
417#define GPIO_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
418 GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value)
419
423#define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
424
431 /* Mask identifying pins supported by the controller.
432 *
433 * Initialization of this mask is the responsibility of device
434 * instance generation in the driver.
435 */
437};
438
444 /* Mask identifying pins that are configured as active low.
445 *
446 * Management of this mask is the responsibility of the
447 * wrapper functions in this header.
448 */
450};
451
452struct gpio_callback;
453
466typedef void (*gpio_callback_handler_t)(const struct device *port,
467 struct gpio_callback *cb,
468 gpio_port_pins_t pins);
469
485
488
496};
497
504/* Used by driver api function pin_interrupt_configure, these are defined
505 * in terms of the public flags so we can just mask and pass them
506 * through to the driver api
507 */
508enum gpio_int_mode {
509 GPIO_INT_MODE_DISABLED = GPIO_INT_DISABLE,
510 GPIO_INT_MODE_LEVEL = GPIO_INT_ENABLE,
511 GPIO_INT_MODE_EDGE = GPIO_INT_ENABLE | GPIO_INT_EDGE,
512};
513
514enum gpio_int_trig {
515 /* Trigger detection when input state is (or transitions to)
516 * physical low. (Edge Failing or Active Low) */
517 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
518 /* Trigger detection when input state is (or transitions to)
519 * physical high. (Edge Rising or Active High) */
520 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
521 /* Trigger detection on pin rising or falling edge. */
522 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
523};
524
525__subsystem struct gpio_driver_api {
526 int (*pin_configure)(const struct device *port, gpio_pin_t pin,
528#ifdef CONFIG_GPIO_GET_CONFIG
529 int (*pin_get_config)(const struct device *port, gpio_pin_t pin,
531#endif
532 int (*port_get_raw)(const struct device *port,
533 gpio_port_value_t *value);
534 int (*port_set_masked_raw)(const struct device *port,
535 gpio_port_pins_t mask,
536 gpio_port_value_t value);
537 int (*port_set_bits_raw)(const struct device *port,
538 gpio_port_pins_t pins);
539 int (*port_clear_bits_raw)(const struct device *port,
540 gpio_port_pins_t pins);
541 int (*port_toggle_bits)(const struct device *port,
542 gpio_port_pins_t pins);
543 int (*pin_interrupt_configure)(const struct device *port,
544 gpio_pin_t pin,
545 enum gpio_int_mode, enum gpio_int_trig);
546 int (*manage_callback)(const struct device *port,
547 struct gpio_callback *cb,
548 bool set);
549 uint32_t (*get_pending_int)(const struct device *dev);
550#ifdef CONFIG_GPIO_GET_DIRECTION
551 int (*port_get_direction)(const struct device *port, gpio_port_pins_t map,
552 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
553#endif /* CONFIG_GPIO_GET_DIRECTION */
554};
555
568static inline bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
569{
570 /* Validate port is ready */
571 return device_is_ready(spec->port);
572}
573
594__syscall int gpio_pin_interrupt_configure(const struct device *port,
595 gpio_pin_t pin,
597
598static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
599 gpio_pin_t pin,
601{
602 const struct gpio_driver_api *api =
603 (const struct gpio_driver_api *)port->api;
604 __unused const struct gpio_driver_config *const cfg =
605 (const struct gpio_driver_config *)port->config;
606 const struct gpio_driver_data *const data =
607 (const struct gpio_driver_data *)port->data;
608 enum gpio_int_trig trig;
609 enum gpio_int_mode mode;
610
611 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE))
612 != (GPIO_INT_DISABLE | GPIO_INT_ENABLE),
613 "Cannot both enable and disable interrupts");
614
615 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE)) != 0U,
616 "Must either enable or disable interrupts");
617
618 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
619 ((flags & GPIO_INT_EDGE) != 0) ||
620 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
621 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
622 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
623 "enabled for a level interrupt.");
624
625 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
626 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0),
627 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be "
628 "enabled.");
629
630 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
631 "Unsupported pin");
632
633 if (((flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
634 ((data->invert & (gpio_port_pins_t)BIT(pin)) != 0)) {
635 /* Invert signal bits */
636 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
637 }
638
639 trig = (enum gpio_int_trig)(flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1));
640 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE));
641
642 return api->pin_interrupt_configure(port, pin, mode, trig);
643}
644
658static inline int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec,
660{
661 return gpio_pin_interrupt_configure(spec->port, spec->pin, flags);
662}
663
679__syscall int gpio_pin_configure(const struct device *port,
680 gpio_pin_t pin,
682
683static inline int z_impl_gpio_pin_configure(const struct device *port,
684 gpio_pin_t pin,
686{
687 const struct gpio_driver_api *api =
688 (const struct gpio_driver_api *)port->api;
689 __unused const struct gpio_driver_config *const cfg =
690 (const struct gpio_driver_config *)port->config;
691 struct gpio_driver_data *data =
692 (struct gpio_driver_data *)port->data;
693
694 __ASSERT((flags & GPIO_INT_MASK) == 0,
695 "Interrupt flags are not supported");
696
697 __ASSERT((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) !=
699 "Pull Up and Pull Down should not be enabled simultaneously");
700
701 __ASSERT(!((flags & GPIO_INPUT) && !(flags & GPIO_OUTPUT) && (flags & GPIO_SINGLE_ENDED)),
702 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
703
704 __ASSERT_NO_MSG((flags & GPIO_SINGLE_ENDED) != 0 ||
705 (flags & GPIO_LINE_OPEN_DRAIN) == 0);
706
707 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
708 || (flags & GPIO_OUTPUT) != 0,
709 "Output needs to be enabled to be initialized low or high");
710
711 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
712 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
713 "Output cannot be initialized low and high");
714
715 if (((flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
716 && ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
717 && ((flags & GPIO_ACTIVE_LOW) != 0)) {
718 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
719 }
720
721 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
722
723 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
724 "Unsupported pin");
725
726 if ((flags & GPIO_ACTIVE_LOW) != 0) {
727 data->invert |= (gpio_port_pins_t)BIT(pin);
728 } else {
729 data->invert &= ~(gpio_port_pins_t)BIT(pin);
730 }
731
732 return api->pin_configure(port, pin, flags);
733}
734
746static inline int gpio_pin_configure_dt(const struct gpio_dt_spec *spec,
747 gpio_flags_t extra_flags)
748{
749 return gpio_pin_configure(spec->port,
750 spec->pin,
751 spec->dt_flags | extra_flags);
752}
753
754/*
755 * @brief Get direction of select pins in a port.
756 *
757 * Retrieve direction of each pin specified in @p map.
758 *
759 * If @p inputs or @p outputs is NULL, then this function does not get the
760 * respective input or output direction information.
761 *
762 * @param port Pointer to the device structure for the driver instance.
763 * @param map Bitmap of pin directions to query.
764 * @param inputs Pointer to a variable where input directions will be stored.
765 * @param outputs Pointer to a variable where output directions will be stored.
766 *
767 * @retval 0 If successful.
768 * @retval -ENOSYS if the underlying driver does not support this call.
769 * @retval -EIO I/O error when accessing an external GPIO chip.
770 * @retval -EWOULDBLOCK if operation would block.
771 */
772__syscall int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
773 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
774
775#ifdef CONFIG_GPIO_GET_DIRECTION
776static inline int z_impl_gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
777 gpio_port_pins_t *inputs,
778 gpio_port_pins_t *outputs)
779{
780 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
781
782 if (api->port_get_direction == NULL) {
783 return -ENOSYS;
784 }
785
786 return api->port_get_direction(port, map, inputs, outputs);
787}
788#endif /* CONFIG_GPIO_GET_DIRECTION */
789
802static inline int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
803{
804 int rv;
805 gpio_port_pins_t pins;
806 __unused const struct gpio_driver_config *cfg =
807 (const struct gpio_driver_config *)port->config;
808
809 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
810
811 rv = gpio_port_get_direction(port, BIT(pin), &pins, NULL);
812 if (rv < 0) {
813 return rv;
814 }
815
816 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
817}
818
830static inline int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
831{
832 return gpio_pin_is_input(spec->port, spec->pin);
833}
834
847static inline int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
848{
849 int rv;
850 gpio_port_pins_t pins;
851 __unused const struct gpio_driver_config *cfg =
852 (const struct gpio_driver_config *)port->config;
853
854 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
855
856 rv = gpio_port_get_direction(port, BIT(pin), NULL, &pins);
857 if (rv < 0) {
858 return rv;
859 }
860
861 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
862}
863
875static inline int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
876{
877 return gpio_pin_is_output(spec->port, spec->pin);
878}
879
895__syscall int gpio_pin_get_config(const struct device *port, gpio_pin_t pin,
897
898#ifdef CONFIG_GPIO_GET_CONFIG
899static inline int z_impl_gpio_pin_get_config(const struct device *port,
900 gpio_pin_t pin,
902{
903 const struct gpio_driver_api *api =
904 (const struct gpio_driver_api *)port->api;
905
906 if (api->pin_get_config == NULL)
907 return -ENOSYS;
908
909 return api->pin_get_config(port, pin, flags);
910}
911#endif
912
925static inline int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec,
927{
928 return gpio_pin_get_config(spec->port, spec->pin, flags);
929}
930
948__syscall int gpio_port_get_raw(const struct device *port,
949 gpio_port_value_t *value);
950
951static inline int z_impl_gpio_port_get_raw(const struct device *port,
952 gpio_port_value_t *value)
953{
954 const struct gpio_driver_api *api =
955 (const struct gpio_driver_api *)port->api;
956
957 return api->port_get_raw(port, value);
958}
959
978static inline int gpio_port_get(const struct device *port,
979 gpio_port_value_t *value)
980{
981 const struct gpio_driver_data *const data =
982 (const struct gpio_driver_data *)port->data;
983 int ret;
984
985 ret = gpio_port_get_raw(port, value);
986 if (ret == 0) {
987 *value ^= data->invert;
988 }
989
990 return ret;
991}
992
1010__syscall int gpio_port_set_masked_raw(const struct device *port,
1011 gpio_port_pins_t mask,
1012 gpio_port_value_t value);
1013
1014static inline int z_impl_gpio_port_set_masked_raw(const struct device *port,
1015 gpio_port_pins_t mask,
1016 gpio_port_value_t value)
1017{
1018 const struct gpio_driver_api *api =
1019 (const struct gpio_driver_api *)port->api;
1020
1021 return api->port_set_masked_raw(port, mask, value);
1022}
1023
1044static inline int gpio_port_set_masked(const struct device *port,
1045 gpio_port_pins_t mask,
1046 gpio_port_value_t value)
1047{
1048 const struct gpio_driver_data *const data =
1049 (const struct gpio_driver_data *)port->data;
1050
1051 value ^= data->invert;
1052
1053 return gpio_port_set_masked_raw(port, mask, value);
1054}
1055
1066__syscall int gpio_port_set_bits_raw(const struct device *port,
1067 gpio_port_pins_t pins);
1068
1069static inline int z_impl_gpio_port_set_bits_raw(const struct device *port,
1070 gpio_port_pins_t pins)
1071{
1072 const struct gpio_driver_api *api =
1073 (const struct gpio_driver_api *)port->api;
1074
1075 return api->port_set_bits_raw(port, pins);
1076}
1077
1088static inline int gpio_port_set_bits(const struct device *port,
1089 gpio_port_pins_t pins)
1090{
1091 return gpio_port_set_masked(port, pins, pins);
1092}
1093
1104__syscall int gpio_port_clear_bits_raw(const struct device *port,
1105 gpio_port_pins_t pins);
1106
1107static inline int z_impl_gpio_port_clear_bits_raw(const struct device *port,
1108 gpio_port_pins_t pins)
1109{
1110 const struct gpio_driver_api *api =
1111 (const struct gpio_driver_api *)port->api;
1112
1113 return api->port_clear_bits_raw(port, pins);
1114}
1115
1126static inline int gpio_port_clear_bits(const struct device *port,
1127 gpio_port_pins_t pins)
1128{
1129 return gpio_port_set_masked(port, pins, 0);
1130}
1131
1142__syscall int gpio_port_toggle_bits(const struct device *port,
1143 gpio_port_pins_t pins);
1144
1145static inline int z_impl_gpio_port_toggle_bits(const struct device *port,
1146 gpio_port_pins_t pins)
1147{
1148 const struct gpio_driver_api *api =
1149 (const struct gpio_driver_api *)port->api;
1150
1151 return api->port_toggle_bits(port, pins);
1152}
1153
1165static inline int gpio_port_set_clr_bits_raw(const struct device *port,
1166 gpio_port_pins_t set_pins,
1167 gpio_port_pins_t clear_pins)
1168{
1169 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1170
1171 return gpio_port_set_masked_raw(port, set_pins | clear_pins, set_pins);
1172}
1173
1185static inline int gpio_port_set_clr_bits(const struct device *port,
1186 gpio_port_pins_t set_pins,
1187 gpio_port_pins_t clear_pins)
1188{
1189 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1190
1191 return gpio_port_set_masked(port, set_pins | clear_pins, set_pins);
1192}
1193
1209static inline int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
1210{
1211 __unused const struct gpio_driver_config *const cfg =
1212 (const struct gpio_driver_config *)port->config;
1213 gpio_port_value_t value;
1214 int ret;
1215
1216 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1217 "Unsupported pin");
1218
1219 ret = gpio_port_get_raw(port, &value);
1220 if (ret == 0) {
1221 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1222 }
1223
1224 return ret;
1225}
1226
1246static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)
1247{
1248 __unused const struct gpio_driver_config *const cfg =
1249 (const struct gpio_driver_config *)port->config;
1250 gpio_port_value_t value;
1251 int ret;
1252
1253 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1254 "Unsupported pin");
1255
1256 ret = gpio_port_get(port, &value);
1257 if (ret == 0) {
1258 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1259 }
1260
1261 return ret;
1262}
1263
1274static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
1275{
1276 return gpio_pin_get(spec->port, spec->pin);
1277}
1278
1294static inline int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin,
1295 int value)
1296{
1297 __unused const struct gpio_driver_config *const cfg =
1298 (const struct gpio_driver_config *)port->config;
1299 int ret;
1300
1301 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1302 "Unsupported pin");
1303
1304 if (value != 0) {
1306 } else {
1308 }
1309
1310 return ret;
1311}
1312
1334static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin,
1335 int value)
1336{
1337 __unused const struct gpio_driver_config *const cfg =
1338 (const struct gpio_driver_config *)port->config;
1339 const struct gpio_driver_data *const data =
1340 (const struct gpio_driver_data *)port->data;
1341
1342 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1343 "Unsupported pin");
1344
1345 if (data->invert & (gpio_port_pins_t)BIT(pin)) {
1346 value = (value != 0) ? 0 : 1;
1347 }
1348
1349 return gpio_pin_set_raw(port, pin, value);
1350}
1351
1363static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
1364{
1365 return gpio_pin_set(spec->port, spec->pin, value);
1366}
1367
1378static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
1379{
1380 __unused const struct gpio_driver_config *const cfg =
1381 (const struct gpio_driver_config *)port->config;
1382
1383 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1384 "Unsupported pin");
1385
1386 return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin));
1387}
1388
1399static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
1400{
1401 return gpio_pin_toggle(spec->port, spec->pin);
1402}
1403
1410static inline void gpio_init_callback(struct gpio_callback *callback,
1412 gpio_port_pins_t pin_mask)
1413{
1414 __ASSERT(callback, "Callback pointer should not be NULL");
1415 __ASSERT(handler, "Callback handler pointer should not be NULL");
1416
1417 callback->handler = handler;
1418 callback->pin_mask = pin_mask;
1419}
1420
1433static inline int gpio_add_callback(const struct device *port,
1434 struct gpio_callback *callback)
1435{
1436 const struct gpio_driver_api *api =
1437 (const struct gpio_driver_api *)port->api;
1438
1439 if (api->manage_callback == NULL) {
1440 return -ENOTSUP;
1441 }
1442
1443 return api->manage_callback(port, callback, true);
1444}
1445
1462static inline int gpio_remove_callback(const struct device *port,
1463 struct gpio_callback *callback)
1464{
1465 const struct gpio_driver_api *api =
1466 (const struct gpio_driver_api *)port->api;
1467
1468 if (api->manage_callback == NULL) {
1469 return -ENOTSUP;
1470 }
1471
1472 return api->manage_callback(port, callback, false);
1473}
1474
1488__syscall int gpio_get_pending_int(const struct device *dev);
1489
1490static inline int z_impl_gpio_get_pending_int(const struct device *dev)
1491{
1492 const struct gpio_driver_api *api =
1493 (const struct gpio_driver_api *)dev->api;
1494
1495 if (api->get_pending_int == NULL) {
1496 return -ENOTSUP;
1497 }
1498
1499 return api->get_pending_int(dev);
1500}
1501
1506#ifdef __cplusplus
1507}
1508#endif
1509
1510#include <syscalls/gpio.h>
1511
1512#endif /* ZEPHYR_INCLUDE_DRIVERS_GPIO_H_ */
System error numbers.
volatile int rv
Definition: main.c:45
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:1433
#define GPIO_OUTPUT
Definition: gpio.h:48
static int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
Get physical level of an input pin.
Definition: gpio.h:1209
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:802
static int gpio_pin_get(const struct device *port, gpio_pin_t pin)
Get logical level of an input pin.
Definition: gpio.h:1246
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:875
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:658
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:466
static int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
Toggle pin level from a gpio_dt_spec.
Definition: gpio.h:1399
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition: gpio.h:237
static int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
Check if pin is configured for output.
Definition: gpio.h:847
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:746
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:1363
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:1088
uint32_t gpio_flags_t
Provides a type to hold GPIO configuration flags.
Definition: gpio.h:257
#define GPIO_ACTIVE_LOW
Definition: gpio.h:23
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:1165
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:1185
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:1410
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:830
#define GPIO_INPUT
Definition: gpio.h:45
uint32_t gpio_port_pins_t
Identifies a set of pins associated with a port.
Definition: gpio.h:216
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:925
int gpio_port_toggle_bits(const struct device *port, gpio_port_pins_t pins)
Toggle level of selected output pins.
#define GPIO_INT_DISABLE
Definition: gpio.h:93
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
Definition: gpio.h:72
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:1274
static int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
Toggle pin level.
Definition: gpio.h:1378
static bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
Validate that GPIO port is ready.
Definition: gpio.h:568
uint32_t gpio_port_value_t
Provides values for a set of pins associated with a port.
Definition: gpio.h:229
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:1334
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:1126
static int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)
Remove an application callback.
Definition: gpio.h:1462
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:1044
int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map, gpio_port_pins_t *inputs, gpio_port_pins_t *outputs)
uint16_t gpio_dt_flags_t
Provides a type to hold GPIO devicetree flags.
Definition: gpio.h:249
#define GPIO_PULL_DOWN
Definition: gpio.h:75
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:1294
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:978
int gpio_pin_configure(const struct device *port, gpio_pin_t pin, gpio_flags_t flags)
Configure a single pin.
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition: util_macro.h:44
#define ENOSYS
Definition: errno.h:83
#define ENOTSUP
Definition: errno.h:115
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
struct region_map map[]
Definition: main.c:35
flags
Definition: parser.h:96
Single-linked list implementation.
struct _snode sys_snode_t
Definition: slist.h:33
__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:378
void * data
Definition: device.h:388
const void * api
Definition: device.h:384
const void * config
Definition: device.h:382
GPIO callback structure.
Definition: gpio.h:480
sys_snode_t node
Definition: gpio.h:484
gpio_port_pins_t pin_mask
Definition: gpio.h:495
gpio_callback_handler_t handler
Definition: gpio.h:487
Definition: gpio.h:430
gpio_port_pins_t port_pin_mask
Definition: gpio.h:436
Definition: gpio.h:443
gpio_port_pins_t invert
Definition: gpio.h:449
Container for GPIO pin information specified in devicetree.
Definition: gpio.h:271
const struct device * port
Definition: gpio.h:273
gpio_pin_t pin
Definition: gpio.h:275
gpio_dt_flags_t dt_flags
Definition: gpio.h:277
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static void handler(struct k_timer *timer)
Definition: main.c:19