15#ifndef ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
16#define ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
43#define GPIO_INPUT (1U << 8)
46#define GPIO_OUTPUT (1U << 9)
49#define GPIO_DISCONNECTED 0
54#define GPIO_OUTPUT_INIT_LOW (1U << 10)
57#define GPIO_OUTPUT_INIT_HIGH (1U << 11)
60#define GPIO_OUTPUT_INIT_LOGICAL (1U << 12)
65#define GPIO_OUTPUT_LOW (GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW)
67#define GPIO_OUTPUT_HIGH (GPIO_OUTPUT | GPIO_OUTPUT_INIT_HIGH)
69#define GPIO_OUTPUT_INACTIVE (GPIO_OUTPUT | \
70 GPIO_OUTPUT_INIT_LOW | \
71 GPIO_OUTPUT_INIT_LOGICAL)
73#define GPIO_OUTPUT_ACTIVE (GPIO_OUTPUT | \
74 GPIO_OUTPUT_INIT_HIGH | \
75 GPIO_OUTPUT_INIT_LOGICAL)
91#define GPIO_INT_DISABLE (1U << 13)
96#define GPIO_INT_ENABLE (1U << 14)
103#define GPIO_INT_LEVELS_LOGICAL (1U << 15)
112#define GPIO_INT_EDGE (1U << 16)
120#define GPIO_INT_LOW_0 (1U << 17)
128#define GPIO_INT_HIGH_1 (1U << 18)
130#define GPIO_INT_MASK (GPIO_INT_DISABLE | \
132 GPIO_INT_LEVELS_LOGICAL | \
141#define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
148#define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
155#define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
163#define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
169#define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
175#define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
176 GPIO_INT_LEVELS_LOGICAL | \
183#define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
184 GPIO_INT_LEVELS_LOGICAL | \
191#define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
192 GPIO_INT_LEVELS_LOGICAL | \
198#define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
199 GPIO_INT_LEVELS_LOGICAL | \
209#define GPIO_INT_DEBOUNCE (1U << 19)
234#define GPIO_DS_LOW_POS 20
235#define GPIO_DS_LOW_MASK (0x3U << GPIO_DS_LOW_POS)
240#define GPIO_DS_DFLT_LOW (0x0U << GPIO_DS_LOW_POS)
246#define GPIO_DS_ALT_LOW (0x1U << GPIO_DS_LOW_POS)
249#define GPIO_DS_HIGH_POS 22
250#define GPIO_DS_HIGH_MASK (0x3U << GPIO_DS_HIGH_POS)
255#define GPIO_DS_DFLT_HIGH (0x0U << GPIO_DS_HIGH_POS)
261#define GPIO_DS_ALT_HIGH (0x1U << GPIO_DS_HIGH_POS)
265#define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
361#define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
363 .port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)),\
364 .pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
365 .dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
385#define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
386 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
387 (GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
398#define GPIO_DT_SPEC_GET(node_id, prop) \
399 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
411#define GPIO_DT_SPEC_GET_OR(node_id, prop, default_value) \
412 GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
424#define GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
425 GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
438#define GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
439 GPIO_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), prop, idx, default_value)
449#define GPIO_DT_SPEC_INST_GET(inst, prop) \
450 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
462#define GPIO_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
463 GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value)
468#define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
555 GPIO_INT_MODE_LEVEL = GPIO_INT_ENABLE,
556 GPIO_INT_MODE_EDGE = GPIO_INT_ENABLE | GPIO_INT_EDGE,
562 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
565 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
567 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
570__subsystem
struct gpio_driver_api {
573 int (*port_get_raw)(
const struct device *port,
575 int (*port_set_masked_raw)(
const struct device *port,
578 int (*port_set_bits_raw)(
const struct device *port,
580 int (*port_clear_bits_raw)(
const struct device *port,
582 int (*port_toggle_bits)(
const struct device *port,
584 int (*pin_interrupt_configure)(
const struct device *port,
586 enum gpio_int_mode,
enum gpio_int_trig);
587 int (*manage_callback)(
const struct device *port,
621static inline int z_impl_gpio_pin_interrupt_configure(
const struct device *port,
625 const struct gpio_driver_api *api =
626 (
const struct gpio_driver_api *)port->
api;
631 enum gpio_int_trig trig;
632 enum gpio_int_mode mode;
638 "Cannot both enable and disable interrupts");
641 "Must either enable or disable interrupts");
643 __ASSERT(((
flags & GPIO_INT_ENABLE) == 0) ||
644 ((
flags & GPIO_INT_EDGE) != 0) ||
645 ((
flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
646 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
647 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
648 "enabled for a level interrupt.");
650 __ASSERT(((
flags & GPIO_INT_ENABLE) == 0) ||
651 ((
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 "
659 if (((
flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
662 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
665 trig = (
enum gpio_int_trig)(
flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1));
668 return api->pin_interrupt_configure(port, pin, mode, trig);
710static inline int z_impl_gpio_pin_configure(
const struct device *port,
714 const struct gpio_driver_api *api =
715 (
const struct gpio_driver_api *)port->
api;
721 __ASSERT((
flags & GPIO_INT_MASK) == 0,
722 "Interrupt flags are not supported");
726 "Pull Up and Pull Down should not be enabled simultaneously");
729 "Output needs to be enabled for 'Open Drain', 'Open Source' "
730 "mode to be supported");
732 __ASSERT_NO_MSG((
flags & GPIO_SINGLE_ENDED) != 0 ||
733 (
flags & GPIO_LINE_OPEN_DRAIN) == 0);
735 __ASSERT((
flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
737 "Output needs to be enabled to be initialized low or high");
739 __ASSERT((
flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
740 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
741 "Output cannot be initialized low and high");
743 if (((
flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
744 && ((
flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
746 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
749 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
761 return api->pin_configure(port, pin,
flags);
803static inline int z_impl_gpio_port_get_raw(
const struct device *port,
806 const struct gpio_driver_api *api =
807 (
const struct gpio_driver_api *)port->
api;
809 return api->port_get_raw(port, value);
839 *value ^=
data->invert;
866static inline int z_impl_gpio_port_set_masked_raw(
const struct device *port,
870 const struct gpio_driver_api *api =
871 (
const struct gpio_driver_api *)port->
api;
873 return api->port_set_masked_raw(port, mask, value);
903 value ^=
data->invert;
921static inline int z_impl_gpio_port_set_bits_raw(
const struct device *port,
924 const struct gpio_driver_api *api =
925 (
const struct gpio_driver_api *)port->
api;
927 return api->port_set_bits_raw(port, pins);
959static inline int z_impl_gpio_port_clear_bits_raw(
const struct device *port,
962 const struct gpio_driver_api *api =
963 (
const struct gpio_driver_api *)port->
api;
965 return api->port_clear_bits_raw(port, pins);
997static inline int z_impl_gpio_port_toggle_bits(
const struct device *port,
1000 const struct gpio_driver_api *api =
1001 (
const struct gpio_driver_api *)port->
api;
1003 return api->port_toggle_bits(port, pins);
1021 __ASSERT((set_pins & clear_pins) == 0,
"Set and Clear pins overlap");
1041 __ASSERT((set_pins & clear_pins) == 0,
"Set and Clear pins overlap");
1202 value = (value != 0) ? 0 : 1;
1271 __ASSERT(callback,
"Callback pointer should not be NULL");
1272 __ASSERT(
handler,
"Callback handler pointer should not be NULL");
1293 const struct gpio_driver_api *api =
1294 (
const struct gpio_driver_api *)port->
api;
1296 if (api->manage_callback == NULL) {
1300 return api->manage_callback(port, callback,
true);
1322 const struct gpio_driver_api *api =
1323 (
const struct gpio_driver_api *)port->
api;
1325 if (api->manage_callback == NULL) {
1329 return api->manage_callback(port, callback,
false);
1347static inline int z_impl_gpio_get_pending_int(
const struct device *dev)
1349 const struct gpio_driver_api *api =
1350 (
const struct gpio_driver_api *)dev->
api;
1352 if (api->get_pending_int == NULL) {
1356 return api->get_pending_int(dev);
1367#include <syscalls/gpio.h>
void
Definition: eswifi_shell.c:15
static int gpio_add_callback(const struct device *port, struct gpio_callback *callback)
Add an application callback.
Definition: gpio.h:1290
#define GPIO_OUTPUT
Definition: gpio.h:46
static int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
Get physical level of an input pin.
Definition: gpio.h:1061
static int gpio_pin_get(const struct device *port, gpio_pin_t pin)
Get logical level of an input pin.
Definition: gpio.h:1099
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_interrupt_configure_dt(const struct gpio_dt_spec *spec, gpio_flags_t flags)
Configure pin interrupts from a gpio_dt_spec.
Definition: gpio.h:684
uint8_t gpio_dt_flags_t
Provides a type to hold GPIO devicetree flags.
Definition: gpio.h:304
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:511
#define GPIO_INT_DEBOUNCE
Definition: gpio.h:209
static int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
Toggle pin level from a gpio_dt_spec.
Definition: gpio.h:1256
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition: gpio.h:295
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:775
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:1219
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:940
uint32_t gpio_flags_t
Provides a type to hold GPIO configuration flags.
Definition: gpio.h:312
#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:1017
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:1037
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:1267
uint32_t gpio_port_pins_t
Identifies a set of pins associated with a port.
Definition: gpio.h:274
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:91
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:1128
static int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
Toggle pin level.
Definition: gpio.h:1234
uint32_t gpio_port_value_t
Provides values for a set of pins associated with a port.
Definition: gpio.h:287
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:1189
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:978
static int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)
Remove an application callback.
Definition: gpio.h:1319
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:896
#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:1148
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:830
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 ENOTSUP
Definition: errno.h:115
flags
Definition: http_parser.h:131
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
Single-linked list implementation.
struct _snode sys_snode_t
Definition: slist.h:33
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
const void * api
Definition: device.h:373
void *const data
Definition: device.h:377
const void * config
Definition: device.h:371
GPIO callback structure.
Definition: gpio.h:525
sys_snode_t node
Definition: gpio.h:529
gpio_port_pins_t pin_mask
Definition: gpio.h:540
gpio_callback_handler_t handler
Definition: gpio.h:532
gpio_port_pins_t port_pin_mask
Definition: gpio.h:481
gpio_port_pins_t invert
Definition: gpio.h:494
Provides a type to hold GPIO information specified in devicetree.
Definition: gpio.h:321
const struct device * port
Definition: gpio.h:322
gpio_pin_t pin
Definition: gpio.h:323
gpio_dt_flags_t dt_flags
Definition: gpio.h:324
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static void handler(struct k_timer *timer)
Definition: main.c:19