2016-03-23 19:01:06 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation.
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-03-23 19:01:06 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file Header where utility code can be found for GPIO drivers
|
|
|
|
*/
|
|
|
|
|
2018-09-15 01:43:44 +08:00
|
|
|
#ifndef ZEPHYR_DRIVERS_GPIO_GPIO_UTILS_H_
|
|
|
|
#define ZEPHYR_DRIVERS_GPIO_GPIO_UTILS_H_
|
2016-03-23 19:01:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generic function to insert or remove a callback from a callback list
|
|
|
|
*
|
|
|
|
* @param callbacks A pointer to the original list of callbacks (can be NULL)
|
|
|
|
* @param callback A pointer of the callback to insert or remove from the list
|
|
|
|
* @param set A boolean indicating insertion or removal of the callback
|
2018-11-15 16:45:54 +08:00
|
|
|
*
|
|
|
|
* @return 0 on success, negative errno otherwise.
|
2016-03-23 19:01:06 +08:00
|
|
|
*/
|
2019-03-13 05:15:42 +08:00
|
|
|
static inline int gpio_manage_callback(sys_slist_t *callbacks,
|
2018-11-15 16:45:54 +08:00
|
|
|
struct gpio_callback *callback,
|
|
|
|
bool set)
|
2016-03-23 19:01:06 +08:00
|
|
|
{
|
|
|
|
__ASSERT(callback, "No callback!");
|
|
|
|
__ASSERT(callback->handler, "No callback handler!");
|
|
|
|
|
2018-11-15 16:45:54 +08:00
|
|
|
if (!sys_slist_is_empty(callbacks)) {
|
|
|
|
if (!sys_slist_find_and_remove(callbacks, &callback->node)) {
|
|
|
|
if (!set) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 19:01:06 +08:00
|
|
|
if (set) {
|
|
|
|
sys_slist_prepend(callbacks, &callback->node);
|
|
|
|
}
|
2018-11-15 16:45:54 +08:00
|
|
|
|
|
|
|
return 0;
|
2016-03-23 19:01:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generic function to go through and fire callback from a callback list
|
|
|
|
*
|
|
|
|
* @param list A pointer on the gpio callback list
|
|
|
|
* @param port A pointer on the gpio driver instance
|
|
|
|
* @param pins The actual pin mask that triggered the interrupt
|
|
|
|
*/
|
2019-03-13 05:15:42 +08:00
|
|
|
static inline void gpio_fire_callbacks(sys_slist_t *list,
|
2016-03-23 19:01:06 +08:00
|
|
|
struct device *port,
|
2017-04-21 23:03:20 +08:00
|
|
|
u32_t pins)
|
2016-03-23 19:01:06 +08:00
|
|
|
{
|
2019-01-16 21:20:12 +08:00
|
|
|
struct gpio_callback *cb, *tmp;
|
2016-03-23 19:01:06 +08:00
|
|
|
|
2019-01-16 21:20:12 +08:00
|
|
|
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(list, cb, tmp, node) {
|
2016-03-23 19:01:06 +08:00
|
|
|
if (cb->pin_mask & pins) {
|
|
|
|
__ASSERT(cb->handler, "No callback handler!");
|
|
|
|
cb->handler(port, cb, pins);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 01:43:44 +08:00
|
|
|
#endif /* ZEPHYR_DRIVERS_GPIO_GPIO_UTILS_H_ */
|