Zephyr Project API  3.4.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
98#define GPIO_INT_DISABLE (1U << 21)
99
102/* Enables GPIO pin interrupt. */
103#define GPIO_INT_ENABLE (1U << 22)
104
105/* GPIO interrupt is sensitive to logical levels.
106 *
107 * This is a component flag that should be combined with other
108 * `GPIO_INT_*` flags to produce a meaningful configuration.
109 */
110#define GPIO_INT_LEVELS_LOGICAL (1U << 23)
111
112/* GPIO interrupt is edge sensitive.
113 *
114 * Note: by default interrupts are level sensitive.
115 *
116 * This is a component flag that should be combined with other
117 * `GPIO_INT_*` flags to produce a meaningful configuration.
118 */
119#define GPIO_INT_EDGE (1U << 24)
120
121/* Trigger detection when input state is (or transitions to) physical low or
122 * logical 0 level.
123 *
124 * This is a component flag that should be combined with other
125 * `GPIO_INT_*` flags to produce a meaningful configuration.
126 */
127#define GPIO_INT_LOW_0 (1U << 25)
128
129/* Trigger detection on input state is (or transitions to) physical high or
130 * logical 1 level.
131 *
132 * This is a component flag that should be combined with other
133 * `GPIO_INT_*` flags to produce a meaningful configuration.
134 */
135#define GPIO_INT_HIGH_1 (1U << 26)
136
137#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
138/* Disable/Enable interrupt functionality without changing other interrupt
139 * related register, such as clearing the pending register.
140 *
141 * This is a component flag that should be combined with `GPIO_INT_ENABLE` or
142 * `GPIO_INT_DISABLE` flags to produce a meaningful configuration.
143 */
144#define GPIO_INT_ENABLE_DISABLE_ONLY (1u << 27)
145#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
146
147#define GPIO_INT_MASK (GPIO_INT_DISABLE | \
148 GPIO_INT_ENABLE | \
149 GPIO_INT_LEVELS_LOGICAL | \
150 GPIO_INT_EDGE | \
151 GPIO_INT_LOW_0 | \
152 GPIO_INT_HIGH_1)
153
158#define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
159 GPIO_INT_EDGE | \
160 GPIO_INT_HIGH_1)
161
165#define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
166 GPIO_INT_EDGE | \
167 GPIO_INT_LOW_0)
168
172#define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
173 GPIO_INT_EDGE | \
174 GPIO_INT_LOW_0 | \
175 GPIO_INT_HIGH_1)
176
180#define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
181 GPIO_INT_LOW_0)
182
186#define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
187 GPIO_INT_HIGH_1)
188
192#define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
193 GPIO_INT_LEVELS_LOGICAL | \
194 GPIO_INT_EDGE | \
195 GPIO_INT_LOW_0)
196
200#define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
201 GPIO_INT_LEVELS_LOGICAL | \
202 GPIO_INT_EDGE | \
203 GPIO_INT_HIGH_1)
204
208#define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
209 GPIO_INT_LEVELS_LOGICAL | \
210 GPIO_INT_LOW_0)
211
215#define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
216 GPIO_INT_LEVELS_LOGICAL | \
217 GPIO_INT_HIGH_1)
218
222#define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
232
245
253
265
273
288 const struct device *port;
293};
294
329#define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
330 { \
331 .port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)),\
332 .pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
333 .dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
334 }
335
353#define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
354 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
355 (GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
356 (default_value))
357
366#define GPIO_DT_SPEC_GET(node_id, prop) \
367 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
368
379#define GPIO_DT_SPEC_GET_OR(node_id, prop, default_value) \
380 GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
381
392#define GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
393 GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
394
406#define GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
407 COND_CODE_1(DT_PROP_HAS_IDX(DT_DRV_INST(inst), prop, idx), \
408 (GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)), \
409 (default_value))
410
419#define GPIO_DT_SPEC_INST_GET(inst, prop) \
420 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
421
432#define GPIO_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
433 GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value)
434
438#define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
439
446 /* Mask identifying pins supported by the controller.
447 *
448 * Initialization of this mask is the responsibility of device
449 * instance generation in the driver.
450 */
452};
453
459 /* Mask identifying pins that are configured as active low.
460 *
461 * Management of this mask is the responsibility of the
462 * wrapper functions in this header.
463 */
465};
466
467struct gpio_callback;
468
481typedef void (*gpio_callback_handler_t)(const struct device *port,
482 struct gpio_callback *cb,
483 gpio_port_pins_t pins);
484
500
503
511};
512
519/* Used by driver api function pin_interrupt_configure, these are defined
520 * in terms of the public flags so we can just mask and pass them
521 * through to the driver api
522 */
523enum gpio_int_mode {
524 GPIO_INT_MODE_DISABLED = GPIO_INT_DISABLE,
525 GPIO_INT_MODE_LEVEL = GPIO_INT_ENABLE,
526 GPIO_INT_MODE_EDGE = GPIO_INT_ENABLE | GPIO_INT_EDGE,
527#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
528 GPIO_INT_MODE_DISABLE_ONLY = GPIO_INT_DISABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
529 GPIO_INT_MODE_ENABLE_ONLY = GPIO_INT_ENABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
530#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
531};
532
533enum gpio_int_trig {
534 /* Trigger detection when input state is (or transitions to)
535 * physical low. (Edge Falling or Active Low)
536 */
537 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
538 /* Trigger detection when input state is (or transitions to)
539 * physical high. (Edge Rising or Active High) */
540 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
541 /* Trigger detection on pin rising or falling edge. */
542 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
543};
544
545__subsystem struct gpio_driver_api {
546 int (*pin_configure)(const struct device *port, gpio_pin_t pin,
548#ifdef CONFIG_GPIO_GET_CONFIG
549 int (*pin_get_config)(const struct device *port, gpio_pin_t pin,
551#endif
552 int (*port_get_raw)(const struct device *port,
553 gpio_port_value_t *value);
554 int (*port_set_masked_raw)(const struct device *port,
555 gpio_port_pins_t mask,
556 gpio_port_value_t value);
557 int (*port_set_bits_raw)(const struct device *port,
558 gpio_port_pins_t pins);
559 int (*port_clear_bits_raw)(const struct device *port,
560 gpio_port_pins_t pins);
561 int (*port_toggle_bits)(const struct device *port,
562 gpio_port_pins_t pins);
563 int (*pin_interrupt_configure)(const struct device *port,
564 gpio_pin_t pin,
565 enum gpio_int_mode, enum gpio_int_trig);
566 int (*manage_callback)(const struct device *port,
567 struct gpio_callback *cb,
568 bool set);
569 uint32_t (*get_pending_int)(const struct device *dev);
570#ifdef CONFIG_GPIO_GET_DIRECTION
571 int (*port_get_direction)(const struct device *port, gpio_port_pins_t map,
572 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
573#endif /* CONFIG_GPIO_GET_DIRECTION */
574};
575
588static inline bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
589{
590 /* Validate port is ready */
591 return device_is_ready(spec->port);
592}
593
614__syscall int gpio_pin_interrupt_configure(const struct device *port,
615 gpio_pin_t pin,
617
618static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
619 gpio_pin_t pin,
621{
622 const struct gpio_driver_api *api =
623 (const struct gpio_driver_api *)port->api;
624 __unused const struct gpio_driver_config *const cfg =
625 (const struct gpio_driver_config *)port->config;
626 const struct gpio_driver_data *const data =
627 (const struct gpio_driver_data *)port->data;
628 enum gpio_int_trig trig;
629 enum gpio_int_mode mode;
630
631 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE))
632 != (GPIO_INT_DISABLE | GPIO_INT_ENABLE),
633 "Cannot both enable and disable interrupts");
634
635 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE)) != 0U,
636 "Must either enable or disable interrupts");
637
638 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
639 ((flags & GPIO_INT_EDGE) != 0) ||
640 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
641 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
642 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
643 "enabled for a level interrupt.");
644
645 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
646#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
647 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
648 (flags & GPIO_INT_ENABLE_DISABLE_ONLY) != 0,
649#else
650 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0),
651#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
652 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be "
653 "enabled.");
654
655 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
656 "Unsupported pin");
657
658 if (((flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
659 ((data->invert & (gpio_port_pins_t)BIT(pin)) != 0)) {
660 /* Invert signal bits */
661 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
662 }
663
664 trig = (enum gpio_int_trig)(flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1));
665#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
666 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE |
667 GPIO_INT_ENABLE_DISABLE_ONLY));
668#else
669 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE));
670#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
671
672 return api->pin_interrupt_configure(port, pin, mode, trig);
673}
674
688static inline int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec,
690{
691 return gpio_pin_interrupt_configure(spec->port, spec->pin, flags);
692}
693
709__syscall int gpio_pin_configure(const struct device *port,
710 gpio_pin_t pin,
712
713static inline int z_impl_gpio_pin_configure(const struct device *port,
714 gpio_pin_t pin,
716{
717 const struct gpio_driver_api *api =
718 (const struct gpio_driver_api *)port->api;
719 __unused const struct gpio_driver_config *const cfg =
720 (const struct gpio_driver_config *)port->config;
721 struct gpio_driver_data *data =
722 (struct gpio_driver_data *)port->data;
723
724 __ASSERT((flags & GPIO_INT_MASK) == 0,
725 "Interrupt flags are not supported");
726
727 __ASSERT((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) !=
729 "Pull Up and Pull Down should not be enabled simultaneously");
730
731 __ASSERT(!((flags & GPIO_INPUT) && !(flags & GPIO_OUTPUT) && (flags & GPIO_SINGLE_ENDED)),
732 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
733
734 __ASSERT_NO_MSG((flags & GPIO_SINGLE_ENDED) != 0 ||
735 (flags & GPIO_LINE_OPEN_DRAIN) == 0);
736
737 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
738 || (flags & GPIO_OUTPUT) != 0,
739 "Output needs to be enabled to be initialized low or high");
740
741 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
742 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
743 "Output cannot be initialized low and high");
744
745 if (((flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
746 && ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
747 && ((flags & GPIO_ACTIVE_LOW) != 0)) {
748 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
749 }
750
751 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
752
753 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
754 "Unsupported pin");
755
756 if ((flags & GPIO_ACTIVE_LOW) != 0) {
757 data->invert |= (gpio_port_pins_t)BIT(pin);
758 } else {
759 data->invert &= ~(gpio_port_pins_t)BIT(pin);
760 }
761
762 return api->pin_configure(port, pin, flags);
763}
764
776static inline int gpio_pin_configure_dt(const struct gpio_dt_spec *spec,
777 gpio_flags_t extra_flags)
778{
779 return gpio_pin_configure(spec->port,
780 spec->pin,
781 spec->dt_flags | extra_flags);
782}
783
784/*
785 * @brief Get direction of select pins in a port.
786 *
787 * Retrieve direction of each pin specified in @p map.
788 *
789 * If @p inputs or @p outputs is NULL, then this function does not get the
790 * respective input or output direction information.
791 *
792 * @param port Pointer to the device structure for the driver instance.
793 * @param map Bitmap of pin directions to query.
794 * @param inputs Pointer to a variable where input directions will be stored.
795 * @param outputs Pointer to a variable where output directions will be stored.
796 *
797 * @retval 0 If successful.
798 * @retval -ENOSYS if the underlying driver does not support this call.
799 * @retval -EIO I/O error when accessing an external GPIO chip.
800 * @retval -EWOULDBLOCK if operation would block.
801 */
802__syscall int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
803 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
804
805#ifdef CONFIG_GPIO_GET_DIRECTION
806static inline int z_impl_gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
807 gpio_port_pins_t *inputs,
808 gpio_port_pins_t *outputs)
809{
810 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
811
812 if (api->port_get_direction == NULL) {
813 return -ENOSYS;
814 }
815
816 return api->port_get_direction(port, map, inputs, outputs);
817}
818#endif /* CONFIG_GPIO_GET_DIRECTION */
819
832static inline int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
833{
834 int rv;
835 gpio_port_pins_t pins;
836 __unused const struct gpio_driver_config *cfg =
837 (const struct gpio_driver_config *)port->config;
838
839 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
840
841 rv = gpio_port_get_direction(port, BIT(pin), &pins, NULL);
842 if (rv < 0) {
843 return rv;
844 }
845
846 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
847}
848
860static inline int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
861{
862 return gpio_pin_is_input(spec->port, spec->pin);
863}
864
877static inline int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
878{
879 int rv;
880 gpio_port_pins_t pins;
881 __unused const struct gpio_driver_config *cfg =
882 (const struct gpio_driver_config *)port->config;
883
884 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
885
886 rv = gpio_port_get_direction(port, BIT(pin), NULL, &pins);
887 if (rv < 0) {
888 return rv;
889 }
890
891 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
892}
893
905static inline int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
906{
907 return gpio_pin_is_output(spec->port, spec->pin);
908}
909
925__syscall int gpio_pin_get_config(const struct device *port, gpio_pin_t pin,
927
928#ifdef CONFIG_GPIO_GET_CONFIG
929static inline int z_impl_gpio_pin_get_config(const struct device *port,
930 gpio_pin_t pin,
932{
933 const struct gpio_driver_api *api =
934 (const struct gpio_driver_api *)port->api;
935
936 if (api->pin_get_config == NULL)
937 return -ENOSYS;
938
939 return api->pin_get_config(port, pin, flags);
940}
941#endif
942
955static inline int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec,
957{
958 return gpio_pin_get_config(spec->port, spec->pin, flags);
959}
960
978__syscall int gpio_port_get_raw(const struct device *port,
979 gpio_port_value_t *value);
980
981static inline int z_impl_gpio_port_get_raw(const struct device *port,
982 gpio_port_value_t *value)
983{
984 const struct gpio_driver_api *api =
985 (const struct gpio_driver_api *)port->api;
986
987 return api->port_get_raw(port, value);
988}
989
1008static inline int gpio_port_get(const struct device *port,
1009 gpio_port_value_t *value)
1010{
1011 const struct gpio_driver_data *const data =
1012 (const struct gpio_driver_data *)port->data;
1013 int ret;
1014
1015 ret = gpio_port_get_raw(port, value);
1016 if (ret == 0) {
1017 *value ^= data->invert;
1018 }
1019
1020 return ret;
1021}
1022
1040__syscall int gpio_port_set_masked_raw(const struct device *port,
1041 gpio_port_pins_t mask,
1042 gpio_port_value_t value);
1043
1044static inline int z_impl_gpio_port_set_masked_raw(const struct device *port,
1045 gpio_port_pins_t mask,
1046 gpio_port_value_t value)
1047{
1048 const struct gpio_driver_api *api =
1049 (const struct gpio_driver_api *)port->api;
1050
1051 return api->port_set_masked_raw(port, mask, value);
1052}
1053
1074static inline int gpio_port_set_masked(const struct device *port,
1075 gpio_port_pins_t mask,
1076 gpio_port_value_t value)
1077{
1078 const struct gpio_driver_data *const data =
1079 (const struct gpio_driver_data *)port->data;
1080
1081 value ^= data->invert;
1082
1083 return gpio_port_set_masked_raw(port, mask, value);
1084}
1085
1096__syscall int gpio_port_set_bits_raw(const struct device *port,
1097 gpio_port_pins_t pins);
1098
1099static inline int z_impl_gpio_port_set_bits_raw(const struct device *port,
1100 gpio_port_pins_t pins)
1101{
1102 const struct gpio_driver_api *api =
1103 (const struct gpio_driver_api *)port->api;
1104
1105 return api->port_set_bits_raw(port, pins);
1106}
1107
1118static inline int gpio_port_set_bits(const struct device *port,
1119 gpio_port_pins_t pins)
1120{
1121 return gpio_port_set_masked(port, pins, pins);
1122}
1123
1134__syscall int gpio_port_clear_bits_raw(const struct device *port,
1135 gpio_port_pins_t pins);
1136
1137static inline int z_impl_gpio_port_clear_bits_raw(const struct device *port,
1138 gpio_port_pins_t pins)
1139{
1140 const struct gpio_driver_api *api =
1141 (const struct gpio_driver_api *)port->api;
1142
1143 return api->port_clear_bits_raw(port, pins);
1144}
1145
1156static inline int gpio_port_clear_bits(const struct device *port,
1157 gpio_port_pins_t pins)
1158{
1159 return gpio_port_set_masked(port, pins, 0);
1160}
1161
1172__syscall int gpio_port_toggle_bits(const struct device *port,
1173 gpio_port_pins_t pins);
1174
1175static inline int z_impl_gpio_port_toggle_bits(const struct device *port,
1176 gpio_port_pins_t pins)
1177{
1178 const struct gpio_driver_api *api =
1179 (const struct gpio_driver_api *)port->api;
1180
1181 return api->port_toggle_bits(port, pins);
1182}
1183
1195static inline int gpio_port_set_clr_bits_raw(const struct device *port,
1196 gpio_port_pins_t set_pins,
1197 gpio_port_pins_t clear_pins)
1198{
1199 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1200
1201 return gpio_port_set_masked_raw(port, set_pins | clear_pins, set_pins);
1202}
1203
1215static inline int gpio_port_set_clr_bits(const struct device *port,
1216 gpio_port_pins_t set_pins,
1217 gpio_port_pins_t clear_pins)
1218{
1219 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1220
1221 return gpio_port_set_masked(port, set_pins | clear_pins, set_pins);
1222}
1223
1239static inline int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
1240{
1241 __unused const struct gpio_driver_config *const cfg =
1242 (const struct gpio_driver_config *)port->config;
1243 gpio_port_value_t value;
1244 int ret;
1245
1246 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1247 "Unsupported pin");
1248
1249 ret = gpio_port_get_raw(port, &value);
1250 if (ret == 0) {
1251 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1252 }
1253
1254 return ret;
1255}
1256
1276static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)
1277{
1278 __unused const struct gpio_driver_config *const cfg =
1279 (const struct gpio_driver_config *)port->config;
1280 gpio_port_value_t value;
1281 int ret;
1282
1283 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1284 "Unsupported pin");
1285
1286 ret = gpio_port_get(port, &value);
1287 if (ret == 0) {
1288 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1289 }
1290
1291 return ret;
1292}
1293
1304static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
1305{
1306 return gpio_pin_get(spec->port, spec->pin);
1307}
1308
1324static inline int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin,
1325 int value)
1326{
1327 __unused const struct gpio_driver_config *const cfg =
1328 (const struct gpio_driver_config *)port->config;
1329 int ret;
1330
1331 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1332 "Unsupported pin");
1333
1334 if (value != 0) {
1336 } else {
1338 }
1339
1340 return ret;
1341}
1342
1364static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin,
1365 int value)
1366{
1367 __unused const struct gpio_driver_config *const cfg =
1368 (const struct gpio_driver_config *)port->config;
1369 const struct gpio_driver_data *const data =
1370 (const struct gpio_driver_data *)port->data;
1371
1372 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1373 "Unsupported pin");
1374
1375 if (data->invert & (gpio_port_pins_t)BIT(pin)) {
1376 value = (value != 0) ? 0 : 1;
1377 }
1378
1379 return gpio_pin_set_raw(port, pin, value);
1380}
1381
1393static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
1394{
1395 return gpio_pin_set(spec->port, spec->pin, value);
1396}
1397
1408static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
1409{
1410 __unused const struct gpio_driver_config *const cfg =
1411 (const struct gpio_driver_config *)port->config;
1412
1413 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1414 "Unsupported pin");
1415
1416 return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin));
1417}
1418
1429static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
1430{
1431 return gpio_pin_toggle(spec->port, spec->pin);
1432}
1433
1440static inline void gpio_init_callback(struct gpio_callback *callback,
1442 gpio_port_pins_t pin_mask)
1443{
1444 __ASSERT(callback, "Callback pointer should not be NULL");
1445 __ASSERT(handler, "Callback handler pointer should not be NULL");
1446
1447 callback->handler = handler;
1448 callback->pin_mask = pin_mask;
1449}
1450
1463static inline int gpio_add_callback(const struct device *port,
1464 struct gpio_callback *callback)
1465{
1466 const struct gpio_driver_api *api =
1467 (const struct gpio_driver_api *)port->api;
1468
1469 if (api->manage_callback == NULL) {
1470 return -ENOTSUP;
1471 }
1472
1473 return api->manage_callback(port, callback, true);
1474}
1475
1487static inline int gpio_add_callback_dt(const struct gpio_dt_spec *spec,
1488 struct gpio_callback *callback)
1489{
1490 return gpio_add_callback(spec->port, callback);
1491}
1492
1509static inline int gpio_remove_callback(const struct device *port,
1510 struct gpio_callback *callback)
1511{
1512 const struct gpio_driver_api *api =
1513 (const struct gpio_driver_api *)port->api;
1514
1515 if (api->manage_callback == NULL) {
1516 return -ENOTSUP;
1517 }
1518
1519 return api->manage_callback(port, callback, false);
1520}
1521
1533static inline int gpio_remove_callback_dt(const struct gpio_dt_spec *spec,
1534 struct gpio_callback *callback)
1535{
1536 return gpio_remove_callback(spec->port, callback);
1537}
1538
1552__syscall int gpio_get_pending_int(const struct device *dev);
1553
1554static inline int z_impl_gpio_get_pending_int(const struct device *dev)
1555{
1556 const struct gpio_driver_api *api =
1557 (const struct gpio_driver_api *)dev->api;
1558
1559 if (api->get_pending_int == NULL) {
1560 return -ENOTSUP;
1561 }
1562
1563 return api->get_pending_int(dev);
1564}
1565
1570#ifdef __cplusplus
1571}
1572#endif
1573
1574#include <syscalls/gpio.h>
1575
1576#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:1463
#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:1239
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:832
static int gpio_pin_get(const struct device *port, gpio_pin_t pin)
Get logical level of an input pin.
Definition: gpio.h:1276
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:905
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:688
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:481
static int gpio_remove_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Remove an application callback.
Definition: gpio.h:1533
static int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
Toggle pin level from a gpio_dt_spec.
Definition: gpio.h:1429
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition: gpio.h:252
static int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
Check if pin is configured for output.
Definition: gpio.h:877
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:776
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:1393
static int gpio_add_callback_dt(const struct gpio_dt_spec *spec, struct gpio_callback *callback)
Add an application callback.
Definition: gpio.h:1487
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:1118
uint32_t gpio_flags_t
Provides a type to hold GPIO configuration flags.
Definition: gpio.h:272
#define GPIO_ACTIVE_LOW
Definition: gpio.h:26
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:1195
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:1215
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:1440
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:860
#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:231
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:955
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:98
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: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:1304
static int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
Toggle pin level.
Definition: gpio.h:1408
static bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
Validate that GPIO port is ready.
Definition: gpio.h:588
uint32_t gpio_port_value_t
Provides values for a set of pins associated with a port.
Definition: gpio.h:244
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:1364
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:1156
static int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)
Remove an application callback.
Definition: gpio.h:1509
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:1074
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:264
#define GPIO_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:1324
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:1008
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:29
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:380
void * data
Definition: device.h:390
const void * api
Definition: device.h:386
const void * config
Definition: device.h:384
GPIO callback structure.
Definition: gpio.h:495
sys_snode_t node
Definition: gpio.h:499
gpio_port_pins_t pin_mask
Definition: gpio.h:510
gpio_callback_handler_t handler
Definition: gpio.h:502
Definition: gpio.h:445
gpio_port_pins_t port_pin_mask
Definition: gpio.h:451
Definition: gpio.h:458
gpio_port_pins_t invert
Definition: gpio.h:464
Container for GPIO pin information specified in devicetree.
Definition: gpio.h:286
const struct device * port
Definition: gpio.h:288
gpio_pin_t pin
Definition: gpio.h:290
gpio_dt_flags_t dt_flags
Definition: gpio.h:292
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static void handler(struct k_timer *timer)
Definition: main.c:19