Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
gpio_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8#ifndef ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_UTILS_H_
9#define ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_UTILS_H_
10
11#include <stdbool.h>
12#include <stdint.h>
13#include <errno.h>
14
15#include <zephyr/devicetree.h>
16#include <zephyr/drivers/gpio.h>
17#include <zephyr/sys/__assert.h>
18#include <zephyr/sys/slist.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#define GPIO_PORT_PIN_MASK_FROM_NGPIOS(ngpios) \
25 ((gpio_port_pins_t)(((uint64_t)1 << (ngpios)) - 1U))
26
35#define GPIO_PORT_PIN_MASK_FROM_DT_NODE(node_id) \
36 GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(node_id, DT_PROP(node_id, ngpios))
37
46#define GPIO_PORT_PIN_MASK_FROM_DT_INST(inst) \
47 GPIO_PORT_PIN_MASK_FROM_DT_NODE(DT_DRV_INST(inst))
48
58static inline int gpio_manage_callback(sys_slist_t *callbacks,
59 struct gpio_callback *callback,
60 bool set)
61{
62 __ASSERT(callback, "No callback!");
63 __ASSERT(callback->handler, "No callback handler!");
64
65 if (!sys_slist_is_empty(callbacks)) {
66 if (!sys_slist_find_and_remove(callbacks, &callback->node)) {
67 if (!set) {
68 return -EINVAL;
69 }
70 }
71 } else if (!set) {
72 return -EINVAL;
73 }
74
75 if (set) {
76 sys_slist_prepend(callbacks, &callback->node);
77 }
78
79 return 0;
80}
81
89static inline void gpio_fire_callbacks(sys_slist_t *list,
90 const struct device *port,
91 uint32_t pins)
92{
93 struct gpio_callback *cb, *tmp;
94
96 if (cb->pin_mask & pins) {
97 __ASSERT(cb->handler, "No callback handler!");
98 cb->handler(port, cb, cb->pin_mask & pins);
99 }
100 }
101}
102
103#ifdef __cplusplus
104}
105#endif
106
107#endif /* ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_UTILS_H_ */
Devicetree main header.
Public APIs for GPIO drivers.
System error numbers.
static void gpio_fire_callbacks(sys_slist_t *list, const struct device *port, uint32_t pins)
Generic function to go through and fire callback from a callback list.
Definition gpio_utils.h:89
static int gpio_manage_callback(sys_slist_t *callbacks, struct gpio_callback *callback, bool set)
Generic function to insert or remove a callback from a callback list.
Definition gpio_utils.h:58
static bool sys_slist_find_and_remove(sys_slist_t *list, sys_snode_t *node)
Find and remove a node from a list.
Definition slist.h:450
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:49
static bool sys_slist_is_empty(sys_slist_t *list)
Test if the given list is empty.
Definition slist.h:268
static void sys_slist_prepend(sys_slist_t *list, sys_snode_t *node)
Prepend a node to the given list.
Definition slist.h:305
#define SYS_SLIST_FOR_EACH_CONTAINER_SAFE(__sl, __cn, __cns, __n)
Provide the primitive to safely iterate on a list under a container Note: __cn can be detached,...
Definition slist.h:183
#define EINVAL
Invalid argument.
Definition errno.h:60
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
Runtime device structure (in ROM) per driver instance.
Definition device.h:412
GPIO callback structure.
Definition gpio.h:740
sys_snode_t node
This is meant to be used in the driver and the user should not mess with it (see drivers/gpio/gpio_ut...
Definition gpio.h:744
gpio_port_pins_t pin_mask
A mask of pins the callback is interested in, if 0 the callback will never be called.
Definition gpio.h:755
gpio_callback_handler_t handler
Actual callback function being called when relevant.
Definition gpio.h:747