Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
pwm_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Basalte bv
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_DRIVERS_PWM_PWM_UTILS_H_
8#define ZEPHYR_INCLUDE_DRIVERS_PWM_PWM_UTILS_H_
9
10#include <stdbool.h>
11#include <stdint.h>
12#include <errno.h>
13
14#include <zephyr/devicetree.h>
15#include <zephyr/drivers/pwm.h>
16#include <zephyr/sys/__assert.h>
17#include <zephyr/sys/slist.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
33static inline int pwm_manage_event_callback(sys_slist_t *callbacks,
34 struct pwm_event_callback *callback, bool set)
35{
36 __ASSERT_NO_MSG(callback != NULL);
37 __ASSERT_NO_MSG(callback->handler != NULL);
38
39 if (!sys_slist_is_empty(callbacks)) {
40 if (!sys_slist_find_and_remove(callbacks, &callback->node)) {
41 if (!set) {
42 return -EINVAL;
43 }
44 }
45 } else if (!set) {
46 return -EINVAL;
47 }
48
49 if (set) {
50 sys_slist_prepend(callbacks, &callback->node);
51 }
52
53 return 0;
54}
55
64static inline void pwm_fire_event_callbacks(sys_slist_t *list, const struct device *dev,
65 uint32_t channel, pwm_events_t events)
66{
67 struct pwm_event_callback *cb, *tmp;
68
69 SYS_SLIST_FOR_EACH_CONTAINER_SAFE(list, cb, tmp, node) {
70 if (cb->channel == channel) {
71 __ASSERT_NO_MSG(cb->handler != NULL);
72
73 cb->handler(dev, cb, channel, events);
74 }
75 }
76}
77
78#ifdef __cplusplus
79}
80#endif
81
82#endif /* ZEPHYR_INCLUDE_DRIVERS_PWM_PWM_UTILS_H_ */
Devicetree main header.
Main header file for PWM (Pulse Width Modulation) driver API.
System error numbers.
uint16_t pwm_events_t
Provides a type to hold PWM events.
Definition pwm.h:118
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:448
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:49
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
static bool sys_slist_is_empty(const sys_slist_t *list)
Test if the given list is empty.
Definition slist.h:268
#define EINVAL
Invalid argument.
Definition errno.h:60
#define NULL
Definition iar_missing_defs.h:20
static void pwm_fire_event_callbacks(sys_slist_t *list, const struct device *dev, uint32_t channel, pwm_events_t events)
Generic function to go through and fire callback from a callback lists.
Definition pwm_utils.h:64
static int pwm_manage_event_callback(sys_slist_t *callbacks, struct pwm_event_callback *callback, bool set)
Generic function to insert or remove a callback from a callback list.
Definition pwm_utils.h:33
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
PWM event callback structure.
Definition pwm.h:465
uint32_t channel
Channel the callback is interested in.
Definition pwm.h:474
pwm_event_callback_handler_t handler
Actual callback function being called when relevant.
Definition pwm.h:471