1 /*
2  * Copyright (c) 2021-2024, Texas Instruments Incorporated - http://www.ti.com
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 /** ============================================================================
34  *  @file       EventP.h
35  *
36  *  @brief      Event Group support
37  *
38  * Events are a collection of bits with an application-defined meaning,
39  * typically used for messaging or synchronisation. A task may check the state
40  * of a set of bits or pend on an EventP object to react to changes when they
41  * are posted from another context.
42  *
43  * Only one Task may pend on a single EventP object at any given time.
44  *
45  * Events are synchronous in nature, meaning that a receiving task will block or
46  * pend while waiting for the events to occur. When the desired events are
47  * received, the pending task continues its execution, as it would after a call
48  * to Semaphore_pend(), for example.
49  *
50  * EventP_pend is used to wait for events. The eventMask determine which
51  * event(s) must occur before returning from EventP_pend. The timeout parameter
52  * allows the task to wait until a timeout, wait indefinitely, or not wait at
53  * all. If waitForAll is true, the pend call will not return until all of the
54  * bits in eventMask are set. If it is false, any of the bits in eventMask will
55  * be returned. A return value of zero indicates that a timeout has occurred. A
56  * non-zero return value is the set of events that were active at the time the
57  * task was unblocked. Event bits that caused a return (either the whole
58  * eventMask or any individual bit, depending on waitForAll) will be cleared
59  * when EventP_pend returns.
60  *
61  *  ============================================================================
62  */
63 
64 #ifndef ti_dpl_EventP__include
65 #define ti_dpl_EventP__include
66 
67 #include <stdint.h>
68 #include <stdbool.h>
69 #include <stddef.h>
70 
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74 
75 /*!
76  *  @brief    Number of bytes greater than or equal to the size of any RTOS Event object.
77  *
78  *  Zephyr: 16
79  */
80 #define EventP_STRUCT_SIZE (16)
81 
82 /*!
83  *  @brief    EventP structure.
84  *
85  *  Opaque structure that should be large enough to hold any of the
86  *  RTOS specific EventP objects.
87  */
88 typedef union EventP_Struct
89 {
90     uint32_t dummy; /*!< Align object */
91     uint8_t data[EventP_STRUCT_SIZE];
92 } EventP_Struct;
93 
94 /*!
95  *  @brief    Wait forever define
96  */
97 #define EventP_WAIT_FOREVER ~(0)
98 
99 /*!
100  *  @brief    No wait define
101  */
102 #define EventP_NO_WAIT (0)
103 
104 /*!
105  *  @brief Opaque client reference to an instance of a EventP
106  *
107  *  A EventP_Handle returned from create or construct represents that instance.
108  */
109 typedef EventP_Struct *EventP_Handle;
110 
111 /*!
112  * @brief Create an EventP, allocating memory on the heap.
113  *
114  * EventP_create creates a new event object. EventP_create returns the
115  * handle of the new task object or NULL if the event could not be created.
116  *
117  *
118  * This API cannot be called from interrupt contexts.
119  *
120  * @retval EventP handle (NULL on failure)
121  */
122 extern EventP_Handle EventP_create(void);
123 
124 /*!
125  *  @brief  Function to delete an EventP.
126  *
127  *  @param  handle  A EventP_Handle returned from EventP_create
128  */
129 extern void EventP_delete(EventP_Handle handle);
130 
131 /*!
132  * @brief Construct an EventP, using statically allocated memory.
133  *
134  * EventP_construct creates a new event object. EventP_construct returns the
135  * handle of the new task object or NULL if the event could not be created.
136  *
137  *
138  * This API cannot be called from interrupt contexts.
139  *
140  * @retval EventP handle (NULL on failure)
141  */
142 extern EventP_Handle EventP_construct(EventP_Struct *obj);
143 
144 /*!
145  *  @brief  Function to destruct an EventP
146  *
147  *  @param  obj     Pointer to a EventP_Struct object that was passed to
148  *                  EventP_construct().
149  *
150  *  @return
151  */
152 extern void EventP_destruct(EventP_Struct *obj);
153 
154 /*!
155  * @brief Wait for the events listed in eventMask.
156  *
157  * EventP_pend is used to wait for events. The eventMask determine which event(s)
158  * must occur before returning from EventP_pend. The timeout parameter allows the
159  * task to wait until a timeout, wait indefinitely, or not wait at all. If
160  * waitForAll is true, the pend call will not return until all of the bits in
161  * eventMask are set. If it is false, any of the bits in eventMask will be
162  * returned. A return value of zero indicates that a timeout has occurred. A
163  * non-zero return value is the set of events in the eventMask that were active
164  * at the time the task was unblocked.
165  *
166  * Event bits that caused a return (either the whole eventMask or any individual
167  * bit, depending on waitForAll) will be cleared when EventP_pend returns.
168  *
169  * A timeout value of EventP_WAIT_FOREVER causes the task to wait indefinitely
170  * for matching events to be posted. A timeout value of EventP_NO_WAIT causes
171  * EventP_pend to return immediately.
172  *
173  * This API cannot be called from interrupt contexts.
174  *
175  * @param event         Event handle
176  * @param eventMask     Match against the events in this bitmask.
177  * @param waitForAll    If true, only return when all matching bits are set
178  * @param timeout       Return after this many ClockP ticks, even if there is no match
179  *
180  * @retval A bitmask containing all consumed events, or zero on timeout.
181  */
182 extern uint32_t EventP_pend(EventP_Handle event, uint32_t eventMask, bool waitForAll, uint32_t timeout);
183 
184 /*!
185  * @brief Post events to an event object.
186  *
187  * EventP_post() is used to signal events. If a task is waiting for the event
188  * and the event conditions are met, EventP_post() unblocks the task. If no
189  * tasks are waiting, EventP_post() simply registers the event with the event
190  * object and returns.
191  *
192  * @param event         Event handle
193  * @param eventMask     Mask of eventIds to post (this must be non-zero).
194  */
195 extern void EventP_post(EventP_Handle event, uint32_t eventMask);
196 
197 /*!
198  * @brief Clear events from an event object.
199  *
200  * Clears the bits in eventMask from the EventP.
201  *
202  * @param event         Event handle
203  * @param eventMask     Mask of eventIds to clear (this must be non-zero).
204  */
205 extern void EventP_clear(EventP_Handle event, uint32_t eventMask);
206 
207 /*!
208  * @brief Get the current events from an event object.
209  *
210  * Returns the currently active events in an EventP without clearing them.
211  *
212  * @param event Event handle
213  *
214  * @retval Currently active events
215  */
216 extern uint32_t EventP_get(EventP_Handle event);
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 #endif /* ti_dpl_EventP__include */