15#ifndef ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
16#define ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
45#define GPIO_INPUT (1U << 16)
48#define GPIO_OUTPUT (1U << 17)
51#define GPIO_DISCONNECTED 0
56#define GPIO_OUTPUT_INIT_LOW (1U << 18)
59#define GPIO_OUTPUT_INIT_HIGH (1U << 19)
62#define GPIO_OUTPUT_INIT_LOGICAL (1U << 20)
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)
98#define GPIO_INT_DISABLE (1U << 21)
103#define GPIO_INT_ENABLE (1U << 22)
110#define GPIO_INT_LEVELS_LOGICAL (1U << 23)
119#define GPIO_INT_EDGE (1U << 24)
127#define GPIO_INT_LOW_0 (1U << 25)
135#define GPIO_INT_HIGH_1 (1U << 26)
137#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
144#define GPIO_INT_ENABLE_DISABLE_ONLY (1u << 27)
147#define GPIO_INT_MASK (GPIO_INT_DISABLE | \
149 GPIO_INT_LEVELS_LOGICAL | \
158#define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
165#define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
172#define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
180#define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
186#define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
192#define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
193 GPIO_INT_LEVELS_LOGICAL | \
200#define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
201 GPIO_INT_LEVELS_LOGICAL | \
208#define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
209 GPIO_INT_LEVELS_LOGICAL | \
215#define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
216 GPIO_INT_LEVELS_LOGICAL | \
222#define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
329#define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
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), \
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)), \
366#define GPIO_DT_SPEC_GET(node_id, prop) \
367 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
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)
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)
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)), \
419#define GPIO_DT_SPEC_INST_GET(inst, prop) \
420 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
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)
438#define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
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,
537 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
540 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
542 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
545__subsystem
struct gpio_driver_api {
548#ifdef CONFIG_GPIO_GET_CONFIG
552 int (*port_get_raw)(
const struct device *port,
554 int (*port_set_masked_raw)(
const struct device *port,
557 int (*port_set_bits_raw)(
const struct device *port,
559 int (*port_clear_bits_raw)(
const struct device *port,
561 int (*port_toggle_bits)(
const struct device *port,
563 int (*pin_interrupt_configure)(
const struct device *port,
565 enum gpio_int_mode,
enum gpio_int_trig);
566 int (*manage_callback)(
const struct device *port,
570#ifdef CONFIG_GPIO_GET_DIRECTION
618static inline int z_impl_gpio_pin_interrupt_configure(
const struct device *port,
622 const struct gpio_driver_api *api =
623 (
const struct gpio_driver_api *)port->
api;
628 enum gpio_int_trig trig;
629 enum gpio_int_mode mode;
633 "Cannot both enable and disable interrupts");
636 "Must either enable or disable interrupts");
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.");
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,
650 ((
flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0),
652 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be "
658 if (((
flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
661 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
664 trig = (
enum gpio_int_trig)(
flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1));
665#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
667 GPIO_INT_ENABLE_DISABLE_ONLY));
672 return api->pin_interrupt_configure(port, pin, mode, trig);
713static inline int z_impl_gpio_pin_configure(
const struct device *port,
717 const struct gpio_driver_api *api =
718 (
const struct gpio_driver_api *)port->
api;
724 __ASSERT((
flags & GPIO_INT_MASK) == 0,
725 "Interrupt flags are not supported");
729 "Pull Up and Pull Down should not be enabled simultaneously");
732 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
734 __ASSERT_NO_MSG((
flags & GPIO_SINGLE_ENDED) != 0 ||
735 (
flags & GPIO_LINE_OPEN_DRAIN) == 0);
737 __ASSERT((
flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
739 "Output needs to be enabled to be initialized low or high");
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");
745 if (((
flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
746 && ((
flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
748 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
751 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
762 return api->pin_configure(port, pin,
flags);
805#ifdef CONFIG_GPIO_GET_DIRECTION
810 const struct gpio_driver_api *api = (
const struct gpio_driver_api *)port->
api;
812 if (api->port_get_direction == NULL) {
816 return api->port_get_direction(port,
map, inputs, outputs);
928#ifdef CONFIG_GPIO_GET_CONFIG
929static inline int z_impl_gpio_pin_get_config(
const struct device *port,
933 const struct gpio_driver_api *api =
934 (
const struct gpio_driver_api *)port->
api;
936 if (api->pin_get_config == NULL)
939 return api->pin_get_config(port, pin,
flags);
981static inline int z_impl_gpio_port_get_raw(
const struct device *port,
984 const struct gpio_driver_api *api =
985 (
const struct gpio_driver_api *)port->
api;
987 return api->port_get_raw(port, value);
1017 *value ^=
data->invert;
1044static inline int z_impl_gpio_port_set_masked_raw(
const struct device *port,
1048 const struct gpio_driver_api *api =
1049 (
const struct gpio_driver_api *)port->
api;
1051 return api->port_set_masked_raw(port, mask, value);
1081 value ^=
data->invert;
1099static inline int z_impl_gpio_port_set_bits_raw(
const struct device *port,
1102 const struct gpio_driver_api *api =
1103 (
const struct gpio_driver_api *)port->
api;
1105 return api->port_set_bits_raw(port, pins);
1137static inline int z_impl_gpio_port_clear_bits_raw(
const struct device *port,
1140 const struct gpio_driver_api *api =
1141 (
const struct gpio_driver_api *)port->
api;
1143 return api->port_clear_bits_raw(port, pins);
1175static inline int z_impl_gpio_port_toggle_bits(
const struct device *port,
1178 const struct gpio_driver_api *api =
1179 (
const struct gpio_driver_api *)port->
api;
1181 return api->port_toggle_bits(port, pins);
1199 __ASSERT((set_pins & clear_pins) == 0,
"Set and Clear pins overlap");
1219 __ASSERT((set_pins & clear_pins) == 0,
"Set and Clear pins overlap");
1376 value = (value != 0) ? 0 : 1;
1444 __ASSERT(callback,
"Callback pointer should not be NULL");
1445 __ASSERT(
handler,
"Callback handler pointer should not be NULL");
1466 const struct gpio_driver_api *api =
1467 (
const struct gpio_driver_api *)port->
api;
1469 if (api->manage_callback == NULL) {
1473 return api->manage_callback(port, callback,
true);
1512 const struct gpio_driver_api *api =
1513 (
const struct gpio_driver_api *)port->
api;
1515 if (api->manage_callback == NULL) {
1519 return api->manage_callback(port, callback,
false);
1554static inline int z_impl_gpio_get_pending_int(
const struct device *dev)
1556 const struct gpio_driver_api *api =
1557 (
const struct gpio_driver_api *)dev->
api;
1559 if (api->get_pending_int == NULL) {
1563 return api->get_pending_int(dev);
1574#include <syscalls/gpio.h>
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
gpio_port_pins_t port_pin_mask
Definition: gpio.h:451
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