2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
32 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
33 * all the API functions to use the MPU wrappers. That should only be done when
34 * task.h is included from an application file. */
35 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
37 /* FreeRTOS includes. */
41 #include "event_groups.h"
43 /* Lint e961, e750 and e9021 are suppressed as a MISRA exception justified
44 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
45 * for the header files above, but not in this file, in order to generate the
46 * correct privileged Vs unprivileged linkage and placement. */
47 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021 See comment above. */
49 typedef struct EventGroupDef_t
51 EventBits_t uxEventBits;
52 List_t xTasksWaitingForBits; /**< List of tasks waiting for a bit to be set. */
54 #if ( configUSE_TRACE_FACILITY == 1 )
55 UBaseType_t uxEventGroupNumber;
58 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
59 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the event group is statically allocated to ensure no attempt is made to free the memory. */
63 /*-----------------------------------------------------------*/
66 * Test the bits set in uxCurrentEventBits to see if the wait condition is met.
67 * The wait condition is defined by xWaitForAllBits. If xWaitForAllBits is
68 * pdTRUE then the wait condition is met if all the bits set in uxBitsToWaitFor
69 * are also set in uxCurrentEventBits. If xWaitForAllBits is pdFALSE then the
70 * wait condition is met if any of the bits set in uxBitsToWait for are also set
71 * in uxCurrentEventBits.
73 static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits,
74 const EventBits_t uxBitsToWaitFor,
75 const BaseType_t xWaitForAllBits ) PRIVILEGED_FUNCTION;
77 /*-----------------------------------------------------------*/
79 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
81 EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t * pxEventGroupBuffer )
83 EventGroup_t * pxEventBits;
85 traceENTER_xEventGroupCreateStatic( pxEventGroupBuffer );
87 /* A StaticEventGroup_t object must be provided. */
88 configASSERT( pxEventGroupBuffer );
90 #if ( configASSERT_DEFINED == 1 )
92 /* Sanity check that the size of the structure used to declare a
93 * variable of type StaticEventGroup_t equals the size of the real
94 * event group structure. */
95 volatile size_t xSize = sizeof( StaticEventGroup_t );
96 configASSERT( xSize == sizeof( EventGroup_t ) );
97 } /*lint !e529 xSize is referenced if configASSERT() is defined. */
98 #endif /* configASSERT_DEFINED */
100 /* The user has provided a statically allocated event group - use it. */
101 pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 !e9087 EventGroup_t and StaticEventGroup_t are deliberately aliased for data hiding purposes and guaranteed to have the same size and alignment requirement - checked by configASSERT(). */
103 if( pxEventBits != NULL )
105 pxEventBits->uxEventBits = 0;
106 vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
108 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
110 /* Both static and dynamic allocation can be used, so note that
111 * this event group was created statically in case the event group
112 * is later deleted. */
113 pxEventBits->ucStaticallyAllocated = pdTRUE;
115 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
117 traceEVENT_GROUP_CREATE( pxEventBits );
121 /* xEventGroupCreateStatic should only ever be called with
122 * pxEventGroupBuffer pointing to a pre-allocated (compile time
123 * allocated) StaticEventGroup_t variable. */
124 traceEVENT_GROUP_CREATE_FAILED();
127 traceRETURN_xEventGroupCreateStatic( pxEventBits );
132 #endif /* configSUPPORT_STATIC_ALLOCATION */
133 /*-----------------------------------------------------------*/
135 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
137 EventGroupHandle_t xEventGroupCreate( void )
139 EventGroup_t * pxEventBits;
141 traceENTER_xEventGroupCreate();
143 /* Allocate the event group. Justification for MISRA deviation as
144 * follows: pvPortMalloc() always ensures returned memory blocks are
145 * aligned per the requirements of the MCU stack. In this case
146 * pvPortMalloc() must return a pointer that is guaranteed to meet the
147 * alignment requirements of the EventGroup_t structure - which (if you
148 * follow it through) is the alignment requirements of the TickType_t type
149 * (EventBits_t being of TickType_t itself). Therefore, whenever the
150 * stack alignment requirements are greater than or equal to the
151 * TickType_t alignment requirements the cast is safe. In other cases,
152 * where the natural word size of the architecture is less than
153 * sizeof( TickType_t ), the TickType_t variables will be accessed in two
154 * or more reads operations, and the alignment requirements is only that
155 * of each individual read. */
156 pxEventBits = ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) ); /*lint !e9087 !e9079 see comment above. */
158 if( pxEventBits != NULL )
160 pxEventBits->uxEventBits = 0;
161 vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
163 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
165 /* Both static and dynamic allocation can be used, so note this
166 * event group was allocated statically in case the event group is
168 pxEventBits->ucStaticallyAllocated = pdFALSE;
170 #endif /* configSUPPORT_STATIC_ALLOCATION */
172 traceEVENT_GROUP_CREATE( pxEventBits );
176 traceEVENT_GROUP_CREATE_FAILED(); /*lint !e9063 Else branch only exists to allow tracing and does not generate code if trace macros are not defined. */
179 traceRETURN_xEventGroupCreate( pxEventBits );
184 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
185 /*-----------------------------------------------------------*/
187 EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
188 const EventBits_t uxBitsToSet,
189 const EventBits_t uxBitsToWaitFor,
190 TickType_t xTicksToWait )
192 EventBits_t uxOriginalBitValue, uxReturn;
193 EventGroup_t * pxEventBits = xEventGroup;
194 BaseType_t xAlreadyYielded;
195 BaseType_t xTimeoutOccurred = pdFALSE;
197 traceENTER_xEventGroupSync( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTicksToWait );
199 configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
200 configASSERT( uxBitsToWaitFor != 0 );
201 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
203 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
209 uxOriginalBitValue = pxEventBits->uxEventBits;
211 ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );
213 if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )
215 /* All the rendezvous bits are now set - no need to block. */
216 uxReturn = ( uxOriginalBitValue | uxBitsToSet );
218 /* Rendezvous always clear the bits. They will have been cleared
219 * already unless this is the only task in the rendezvous. */
220 pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
226 if( xTicksToWait != ( TickType_t ) 0 )
228 traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );
230 /* Store the bits that the calling task is waiting for in the
231 * task's event list item so the kernel knows when a match is
232 * found. Then enter the blocked state. */
233 vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );
235 /* This assignment is obsolete as uxReturn will get set after
236 * the task unblocks, but some compilers mistakenly generate a
237 * warning about uxReturn being returned without being set if the
238 * assignment is omitted. */
243 /* The rendezvous bits were not set, but no block time was
244 * specified - just return the current event bit value. */
245 uxReturn = pxEventBits->uxEventBits;
246 xTimeoutOccurred = pdTRUE;
250 xAlreadyYielded = xTaskResumeAll();
252 if( xTicksToWait != ( TickType_t ) 0 )
254 if( xAlreadyYielded == pdFALSE )
256 #if ( configNUMBER_OF_CORES == 1 )
258 portYIELD_WITHIN_API();
260 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
262 vTaskYieldWithinAPI();
264 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
268 mtCOVERAGE_TEST_MARKER();
271 /* The task blocked to wait for its required bits to be set - at this
272 * point either the required bits were set or the block time expired. If
273 * the required bits were set they will have been stored in the task's
274 * event list item, and they should now be retrieved then cleared. */
275 uxReturn = uxTaskResetEventItemValue();
277 if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
279 /* The task timed out, just return the current event bit value. */
280 taskENTER_CRITICAL();
282 uxReturn = pxEventBits->uxEventBits;
284 /* Although the task got here because it timed out before the
285 * bits it was waiting for were set, it is possible that since it
286 * unblocked another task has set the bits. If this is the case
287 * then it needs to clear the bits before exiting. */
288 if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )
290 pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
294 mtCOVERAGE_TEST_MARKER();
299 xTimeoutOccurred = pdTRUE;
303 /* The task unblocked because the bits were set. */
306 /* Control bits might be set as the task had blocked should not be
308 uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
311 traceEVENT_GROUP_SYNC_END( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred );
313 /* Prevent compiler warnings when trace macros are not used. */
314 ( void ) xTimeoutOccurred;
316 traceRETURN_xEventGroupSync( uxReturn );
320 /*-----------------------------------------------------------*/
322 EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
323 const EventBits_t uxBitsToWaitFor,
324 const BaseType_t xClearOnExit,
325 const BaseType_t xWaitForAllBits,
326 TickType_t xTicksToWait )
328 EventGroup_t * pxEventBits = xEventGroup;
329 EventBits_t uxReturn, uxControlBits = 0;
330 BaseType_t xWaitConditionMet, xAlreadyYielded;
331 BaseType_t xTimeoutOccurred = pdFALSE;
333 traceENTER_xEventGroupWaitBits( xEventGroup, uxBitsToWaitFor, xClearOnExit, xWaitForAllBits, xTicksToWait );
335 /* Check the user is not attempting to wait on the bits used by the kernel
336 * itself, and that at least one bit is being requested. */
337 configASSERT( xEventGroup );
338 configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
339 configASSERT( uxBitsToWaitFor != 0 );
340 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
342 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
348 const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
350 /* Check to see if the wait condition is already met or not. */
351 xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );
353 if( xWaitConditionMet != pdFALSE )
355 /* The wait condition has already been met so there is no need to
357 uxReturn = uxCurrentEventBits;
358 xTicksToWait = ( TickType_t ) 0;
360 /* Clear the wait bits if requested to do so. */
361 if( xClearOnExit != pdFALSE )
363 pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
367 mtCOVERAGE_TEST_MARKER();
370 else if( xTicksToWait == ( TickType_t ) 0 )
372 /* The wait condition has not been met, but no block time was
373 * specified, so just return the current value. */
374 uxReturn = uxCurrentEventBits;
375 xTimeoutOccurred = pdTRUE;
379 /* The task is going to block to wait for its required bits to be
380 * set. uxControlBits are used to remember the specified behaviour of
381 * this call to xEventGroupWaitBits() - for use when the event bits
382 * unblock the task. */
383 if( xClearOnExit != pdFALSE )
385 uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;
389 mtCOVERAGE_TEST_MARKER();
392 if( xWaitForAllBits != pdFALSE )
394 uxControlBits |= eventWAIT_FOR_ALL_BITS;
398 mtCOVERAGE_TEST_MARKER();
401 /* Store the bits that the calling task is waiting for in the
402 * task's event list item so the kernel knows when a match is
403 * found. Then enter the blocked state. */
404 vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );
406 /* This is obsolete as it will get set after the task unblocks, but
407 * some compilers mistakenly generate a warning about the variable
408 * being returned without being set if it is not done. */
411 traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );
414 xAlreadyYielded = xTaskResumeAll();
416 if( xTicksToWait != ( TickType_t ) 0 )
418 if( xAlreadyYielded == pdFALSE )
420 #if ( configNUMBER_OF_CORES == 1 )
422 portYIELD_WITHIN_API();
424 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
426 vTaskYieldWithinAPI();
428 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
432 mtCOVERAGE_TEST_MARKER();
435 /* The task blocked to wait for its required bits to be set - at this
436 * point either the required bits were set or the block time expired. If
437 * the required bits were set they will have been stored in the task's
438 * event list item, and they should now be retrieved then cleared. */
439 uxReturn = uxTaskResetEventItemValue();
441 if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
443 taskENTER_CRITICAL();
445 /* The task timed out, just return the current event bit value. */
446 uxReturn = pxEventBits->uxEventBits;
448 /* It is possible that the event bits were updated between this
449 * task leaving the Blocked state and running again. */
450 if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE )
452 if( xClearOnExit != pdFALSE )
454 pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
458 mtCOVERAGE_TEST_MARKER();
463 mtCOVERAGE_TEST_MARKER();
466 xTimeoutOccurred = pdTRUE;
472 /* The task unblocked because the bits were set. */
475 /* The task blocked so control bits may have been set. */
476 uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
479 traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );
481 /* Prevent compiler warnings when trace macros are not used. */
482 ( void ) xTimeoutOccurred;
484 traceRETURN_xEventGroupWaitBits( uxReturn );
488 /*-----------------------------------------------------------*/
490 EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup,
491 const EventBits_t uxBitsToClear )
493 EventGroup_t * pxEventBits = xEventGroup;
494 EventBits_t uxReturn;
496 traceENTER_xEventGroupClearBits( xEventGroup, uxBitsToClear );
498 /* Check the user is not attempting to clear the bits used by the kernel
500 configASSERT( xEventGroup );
501 configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
503 taskENTER_CRITICAL();
505 traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
507 /* The value returned is the event group value prior to the bits being
509 uxReturn = pxEventBits->uxEventBits;
511 /* Clear the bits. */
512 pxEventBits->uxEventBits &= ~uxBitsToClear;
516 traceRETURN_xEventGroupClearBits( uxReturn );
520 /*-----------------------------------------------------------*/
522 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
524 BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup,
525 const EventBits_t uxBitsToClear )
529 traceENTER_xEventGroupClearBitsFromISR( xEventGroup, uxBitsToClear );
531 traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );
532 xReturn = xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL ); /*lint !e9087 Can't avoid cast to void* as a generic callback function not specific to this use case. Callback casts back to original type so safe. */
534 traceRETURN_xEventGroupClearBitsFromISR( xReturn );
539 #endif /* if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) ) */
540 /*-----------------------------------------------------------*/
542 EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
544 UBaseType_t uxSavedInterruptStatus;
545 EventGroup_t const * const pxEventBits = xEventGroup;
546 EventBits_t uxReturn;
548 traceENTER_xEventGroupGetBitsFromISR( xEventGroup );
550 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
552 uxReturn = pxEventBits->uxEventBits;
554 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
556 traceRETURN_xEventGroupGetBitsFromISR( uxReturn );
559 } /*lint !e818 EventGroupHandle_t is a typedef used in other functions to so can't be pointer to const. */
560 /*-----------------------------------------------------------*/
562 EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
563 const EventBits_t uxBitsToSet )
565 ListItem_t * pxListItem;
567 ListItem_t const * pxListEnd;
568 List_t const * pxList;
569 EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;
570 EventGroup_t * pxEventBits = xEventGroup;
571 BaseType_t xMatchFound = pdFALSE;
573 traceENTER_xEventGroupSetBits( xEventGroup, uxBitsToSet );
575 /* Check the user is not attempting to set the bits used by the kernel
577 configASSERT( xEventGroup );
578 configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
580 pxList = &( pxEventBits->xTasksWaitingForBits );
581 pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */
584 traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );
586 pxListItem = listGET_HEAD_ENTRY( pxList );
589 pxEventBits->uxEventBits |= uxBitsToSet;
591 /* See if the new bit value should unblock any tasks. */
592 while( pxListItem != pxListEnd )
594 pxNext = listGET_NEXT( pxListItem );
595 uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );
596 xMatchFound = pdFALSE;
598 /* Split the bits waited for from the control bits. */
599 uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;
600 uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;
602 if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )
604 /* Just looking for single bit being set. */
605 if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )
607 xMatchFound = pdTRUE;
611 mtCOVERAGE_TEST_MARKER();
614 else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )
616 /* All bits are set. */
617 xMatchFound = pdTRUE;
621 /* Need all bits to be set, but not all the bits were set. */
624 if( xMatchFound != pdFALSE )
626 /* The bits match. Should the bits be cleared on exit? */
627 if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )
629 uxBitsToClear |= uxBitsWaitedFor;
633 mtCOVERAGE_TEST_MARKER();
636 /* Store the actual event flag value in the task's event list
637 * item before removing the task from the event list. The
638 * eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows
639 * that is was unblocked due to its required bits matching, rather
640 * than because it timed out. */
641 vTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );
644 /* Move onto the next list item. Note pxListItem->pxNext is not
645 * used here as the list item may have been removed from the event list
646 * and inserted into the ready/pending reading list. */
650 /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT
651 * bit was set in the control word. */
652 pxEventBits->uxEventBits &= ~uxBitsToClear;
654 ( void ) xTaskResumeAll();
656 traceRETURN_xEventGroupSetBits( pxEventBits->uxEventBits );
658 return pxEventBits->uxEventBits;
660 /*-----------------------------------------------------------*/
662 void vEventGroupDelete( EventGroupHandle_t xEventGroup )
664 EventGroup_t * pxEventBits = xEventGroup;
665 const List_t * pxTasksWaitingForBits;
667 traceENTER_vEventGroupDelete( xEventGroup );
669 configASSERT( pxEventBits );
671 pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
675 traceEVENT_GROUP_DELETE( xEventGroup );
677 while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )
679 /* Unblock the task, returning 0 as the event list is being deleted
680 * and cannot therefore have any bits set. */
681 configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( const ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );
682 vTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
685 ( void ) xTaskResumeAll();
687 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
689 /* The event group can only have been allocated dynamically - free
691 vPortFree( pxEventBits );
693 #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
695 /* The event group could have been allocated statically or
696 * dynamically, so check before attempting to free the memory. */
697 if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
699 vPortFree( pxEventBits );
703 mtCOVERAGE_TEST_MARKER();
706 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
708 traceRETURN_vEventGroupDelete();
710 /*-----------------------------------------------------------*/
712 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
713 BaseType_t xEventGroupGetStaticBuffer( EventGroupHandle_t xEventGroup,
714 StaticEventGroup_t ** ppxEventGroupBuffer )
717 EventGroup_t * pxEventBits = xEventGroup;
719 traceENTER_xEventGroupGetStaticBuffer( xEventGroup, ppxEventGroupBuffer );
721 configASSERT( pxEventBits );
722 configASSERT( ppxEventGroupBuffer );
724 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
726 /* Check if the event group was statically allocated. */
727 if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdTRUE )
729 *ppxEventGroupBuffer = ( StaticEventGroup_t * ) pxEventBits;
737 #else /* configSUPPORT_DYNAMIC_ALLOCATION */
739 /* Event group must have been statically allocated. */
740 *ppxEventGroupBuffer = ( StaticEventGroup_t * ) pxEventBits;
743 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
745 traceRETURN_xEventGroupGetStaticBuffer( xReturn );
749 #endif /* configSUPPORT_STATIC_ALLOCATION */
750 /*-----------------------------------------------------------*/
752 /* For internal use only - execute a 'set bits' command that was pended from
754 void vEventGroupSetBitsCallback( void * pvEventGroup,
755 const uint32_t ulBitsToSet )
757 traceENTER_vEventGroupSetBitsCallback( pvEventGroup, ulBitsToSet );
759 ( void ) xEventGroupSetBits( pvEventGroup, ( EventBits_t ) ulBitsToSet ); /*lint !e9079 Can't avoid cast to void* as a generic timer callback prototype. Callback casts back to original type so safe. */
761 traceRETURN_vEventGroupSetBitsCallback();
763 /*-----------------------------------------------------------*/
765 /* For internal use only - execute a 'clear bits' command that was pended from
767 void vEventGroupClearBitsCallback( void * pvEventGroup,
768 const uint32_t ulBitsToClear )
770 traceENTER_vEventGroupClearBitsCallback( pvEventGroup, ulBitsToClear );
772 ( void ) xEventGroupClearBits( pvEventGroup, ( EventBits_t ) ulBitsToClear ); /*lint !e9079 Can't avoid cast to void* as a generic timer callback prototype. Callback casts back to original type so safe. */
774 traceRETURN_vEventGroupClearBitsCallback();
776 /*-----------------------------------------------------------*/
778 static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits,
779 const EventBits_t uxBitsToWaitFor,
780 const BaseType_t xWaitForAllBits )
782 BaseType_t xWaitConditionMet = pdFALSE;
784 if( xWaitForAllBits == pdFALSE )
786 /* Task only has to wait for one bit within uxBitsToWaitFor to be
787 * set. Is one already set? */
788 if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )
790 xWaitConditionMet = pdTRUE;
794 mtCOVERAGE_TEST_MARKER();
799 /* Task has to wait for all the bits in uxBitsToWaitFor to be set.
800 * Are they set already? */
801 if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )
803 xWaitConditionMet = pdTRUE;
807 mtCOVERAGE_TEST_MARKER();
811 return xWaitConditionMet;
813 /*-----------------------------------------------------------*/
815 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
817 BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,
818 const EventBits_t uxBitsToSet,
819 BaseType_t * pxHigherPriorityTaskWoken )
823 traceENTER_xEventGroupSetBitsFromISR( xEventGroup, uxBitsToSet, pxHigherPriorityTaskWoken );
825 traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );
826 xReturn = xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken ); /*lint !e9087 Can't avoid cast to void* as a generic callback function not specific to this use case. Callback casts back to original type so safe. */
828 traceRETURN_xEventGroupSetBitsFromISR( xReturn );
833 #endif /* if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) ) */
834 /*-----------------------------------------------------------*/
836 #if ( configUSE_TRACE_FACILITY == 1 )
838 UBaseType_t uxEventGroupGetNumber( void * xEventGroup )
841 EventGroup_t const * pxEventBits = ( EventGroup_t * ) xEventGroup; /*lint !e9087 !e9079 EventGroupHandle_t is a pointer to an EventGroup_t, but EventGroupHandle_t is kept opaque outside of this file for data hiding purposes. */
843 traceENTER_uxEventGroupGetNumber( xEventGroup );
845 if( xEventGroup == NULL )
851 xReturn = pxEventBits->uxEventGroupNumber;
854 traceRETURN_uxEventGroupGetNumber( xReturn );
859 #endif /* configUSE_TRACE_FACILITY */
860 /*-----------------------------------------------------------*/
862 #if ( configUSE_TRACE_FACILITY == 1 )
864 void vEventGroupSetNumber( void * xEventGroup,
865 UBaseType_t uxEventGroupNumber )
867 traceENTER_vEventGroupSetNumber( xEventGroup, uxEventGroupNumber );
869 ( ( EventGroup_t * ) xEventGroup )->uxEventGroupNumber = uxEventGroupNumber; /*lint !e9087 !e9079 EventGroupHandle_t is a pointer to an EventGroup_t, but EventGroupHandle_t is kept opaque outside of this file for data hiding purposes. */
871 traceRETURN_vEventGroupSetNumber();
874 #endif /* configUSE_TRACE_FACILITY */
875 /*-----------------------------------------------------------*/