]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/event_groups.c
4cd89919c14814fa01dfd2194a738b7a77fc4661
[freertos] / FreeRTOS / Source / event_groups.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Standard includes. */\r
71 #include <stdlib.h>\r
72 \r
73 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
74 all the API functions to use the MPU wrappers.  That should only be done when\r
75 task.h is included from an application file. */\r
76 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
77 \r
78 /* FreeRTOS includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "timers.h"\r
82 #include "event_groups.h"\r
83 \r
84 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the\r
85 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the\r
86 header files above, but not in this file, in order to generate the correct\r
87 privileged Vs unprivileged linkage and placement. */\r
88 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */\r
89 \r
90 /* The following bit fields convey control information in a task's event list\r
91 item value.  It is important they don't clash with the\r
92 taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */\r
93 #if configUSE_16_BIT_TICKS == 1\r
94         #define eventCLEAR_EVENTS_ON_EXIT_BIT   0x0100U\r
95         #define eventUNBLOCKED_DUE_TO_BIT_SET   0x0200U\r
96         #define eventWAIT_FOR_ALL_BITS                  0x0400U\r
97         #define eventEVENT_BITS_CONTROL_BYTES   0xff00U\r
98 #else\r
99         #define eventCLEAR_EVENTS_ON_EXIT_BIT   0x01000000UL\r
100         #define eventUNBLOCKED_DUE_TO_BIT_SET   0x02000000UL\r
101         #define eventWAIT_FOR_ALL_BITS                  0x04000000UL\r
102         #define eventEVENT_BITS_CONTROL_BYTES   0xff000000UL\r
103 #endif\r
104 \r
105 typedef struct xEventGroupDefinition\r
106 {\r
107         EventBits_t uxEventBits;\r
108         List_t xTasksWaitingForBits;            /*< List of tasks waiting for a bit to be set. */\r
109 \r
110         #if( configUSE_TRACE_FACILITY == 1 )\r
111                 UBaseType_t uxEventGroupNumber;\r
112         #endif\r
113 \r
114         #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
115                 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the event group is statically allocated to ensure no attempt is made to free the memory. */\r
116         #endif\r
117 } EventGroup_t;\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /*\r
122  * Test the bits set in uxCurrentEventBits to see if the wait condition is met.\r
123  * The wait condition is defined by xWaitForAllBits.  If xWaitForAllBits is\r
124  * pdTRUE then the wait condition is met if all the bits set in uxBitsToWaitFor\r
125  * are also set in uxCurrentEventBits.  If xWaitForAllBits is pdFALSE then the\r
126  * wait condition is met if any of the bits set in uxBitsToWait for are also set\r
127  * in uxCurrentEventBits.\r
128  */\r
129 static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits ) PRIVILEGED_FUNCTION;\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
134 \r
135         EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t *pxEventGroupBuffer )\r
136         {\r
137         EventGroup_t *pxEventBits;\r
138 \r
139                 /* A StaticEventGroup_t object must be provided. */\r
140                 configASSERT( pxEventGroupBuffer );\r
141 \r
142                 /* The user has provided a statically allocated event group - use it. */\r
143                 pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 EventGroup_t and StaticEventGroup_t are guaranteed to have the same size and alignment requirement - checked by configASSERT(). */\r
144 \r
145                 if( pxEventBits != NULL )\r
146                 {\r
147                         pxEventBits->uxEventBits = 0;\r
148                         vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );\r
149 \r
150                         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
151                         {\r
152                                 /* Both static and dynamic allocation can be used, so note that\r
153                                 this event group was created statically in case the event group\r
154                                 is later deleted. */\r
155                                 pxEventBits->ucStaticallyAllocated = pdTRUE;\r
156                         }\r
157                         #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
158 \r
159                         traceEVENT_GROUP_CREATE( pxEventBits );\r
160                 }\r
161                 else\r
162                 {\r
163                         traceEVENT_GROUP_CREATE_FAILED();\r
164                 }\r
165 \r
166                 return ( EventGroupHandle_t ) pxEventBits;\r
167         }\r
168 \r
169 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
173 \r
174         EventGroupHandle_t xEventGroupCreate( void )\r
175         {\r
176         EventGroup_t *pxEventBits;\r
177 \r
178                 /* Allocate the event group. */\r
179                 pxEventBits = ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) );\r
180 \r
181                 if( pxEventBits != NULL )\r
182                 {\r
183                         pxEventBits->uxEventBits = 0;\r
184                         vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );\r
185 \r
186                         #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
187                         {\r
188                                 /* Both static and dynamic allocation can be used, so note this\r
189                                 event group was allocated statically in case the event group is\r
190                                 later deleted. */\r
191                                 pxEventBits->ucStaticallyAllocated = pdFALSE;\r
192                         }\r
193                         #endif /* configSUPPORT_STATIC_ALLOCATION */\r
194 \r
195                         traceEVENT_GROUP_CREATE( pxEventBits );\r
196                 }\r
197                 else\r
198                 {\r
199                         traceEVENT_GROUP_CREATE_FAILED();\r
200                 }\r
201 \r
202                 return ( EventGroupHandle_t ) pxEventBits;\r
203         }\r
204 \r
205 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait )\r
209 {\r
210 EventBits_t uxOriginalBitValue, uxReturn;\r
211 EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;\r
212 BaseType_t xAlreadyYielded;\r
213 BaseType_t xTimeoutOccurred = pdFALSE;\r
214 \r
215         configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );\r
216         configASSERT( uxBitsToWaitFor != 0 );\r
217         #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
218         {\r
219                 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
220         }\r
221         #endif\r
222 \r
223         vTaskSuspendAll();\r
224         {\r
225                 uxOriginalBitValue = pxEventBits->uxEventBits;\r
226 \r
227                 ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );\r
228 \r
229                 if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
230                 {\r
231                         /* All the rendezvous bits are now set - no need to block. */\r
232                         uxReturn = ( uxOriginalBitValue | uxBitsToSet );\r
233 \r
234                         /* Rendezvous always clear the bits.  They will have been cleared\r
235                         already unless this is the only task in the rendezvous. */\r
236                         pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
237 \r
238                         xTicksToWait = 0;\r
239                 }\r
240                 else\r
241                 {\r
242                         if( xTicksToWait != ( TickType_t ) 0 )\r
243                         {\r
244                                 traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );\r
245 \r
246                                 /* Store the bits that the calling task is waiting for in the\r
247                                 task's event list item so the kernel knows when a match is\r
248                                 found.  Then enter the blocked state. */\r
249                                 vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );\r
250 \r
251                                 /* This assignment is obsolete as uxReturn will get set after\r
252                                 the task unblocks, but some compilers mistakenly generate a\r
253                                 warning about uxReturn being returned without being set if the\r
254                                 assignment is omitted. */\r
255                                 uxReturn = 0;\r
256                         }\r
257                         else\r
258                         {\r
259                                 /* The rendezvous bits were not set, but no block time was\r
260                                 specified - just return the current event bit value. */\r
261                                 uxReturn = pxEventBits->uxEventBits;\r
262                         }\r
263                 }\r
264         }\r
265         xAlreadyYielded = xTaskResumeAll();\r
266 \r
267         if( xTicksToWait != ( TickType_t ) 0 )\r
268         {\r
269                 if( xAlreadyYielded == pdFALSE )\r
270                 {\r
271                         portYIELD_WITHIN_API();\r
272                 }\r
273                 else\r
274                 {\r
275                         mtCOVERAGE_TEST_MARKER();\r
276                 }\r
277 \r
278                 /* The task blocked to wait for its required bits to be set - at this\r
279                 point either the required bits were set or the block time expired.  If\r
280                 the required bits were set they will have been stored in the task's\r
281                 event list item, and they should now be retrieved then cleared. */\r
282                 uxReturn = uxTaskResetEventItemValue();\r
283 \r
284                 if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )\r
285                 {\r
286                         /* The task timed out, just return the current event bit value. */\r
287                         taskENTER_CRITICAL();\r
288                         {\r
289                                 uxReturn = pxEventBits->uxEventBits;\r
290 \r
291                                 /* Although the task got here because it timed out before the\r
292                                 bits it was waiting for were set, it is possible that since it\r
293                                 unblocked another task has set the bits.  If this is the case\r
294                                 then it needs to clear the bits before exiting. */\r
295                                 if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
296                                 {\r
297                                         pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
298                                 }\r
299                                 else\r
300                                 {\r
301                                         mtCOVERAGE_TEST_MARKER();\r
302                                 }\r
303                         }\r
304                         taskEXIT_CRITICAL();\r
305 \r
306                         xTimeoutOccurred = pdTRUE;\r
307                 }\r
308                 else\r
309                 {\r
310                         /* The task unblocked because the bits were set. */\r
311                 }\r
312 \r
313                 /* Control bits might be set as the task had blocked should not be\r
314                 returned. */\r
315                 uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;\r
316         }\r
317 \r
318         traceEVENT_GROUP_SYNC_END( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred );\r
319 \r
320         return uxReturn;\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait )\r
325 {\r
326 EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;\r
327 EventBits_t uxReturn, uxControlBits = 0;\r
328 BaseType_t xWaitConditionMet, xAlreadyYielded;\r
329 BaseType_t xTimeoutOccurred = pdFALSE;\r
330 \r
331         /* Check the user is not attempting to wait on the bits used by the kernel\r
332         itself, and that at least one bit is being requested. */\r
333         configASSERT( xEventGroup );\r
334         configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );\r
335         configASSERT( uxBitsToWaitFor != 0 );\r
336         #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
337         {\r
338                 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
339         }\r
340         #endif\r
341 \r
342         vTaskSuspendAll();\r
343         {\r
344                 const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;\r
345 \r
346                 /* Check to see if the wait condition is already met or not. */\r
347                 xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );\r
348 \r
349                 if( xWaitConditionMet != pdFALSE )\r
350                 {\r
351                         /* The wait condition has already been met so there is no need to\r
352                         block. */\r
353                         uxReturn = uxCurrentEventBits;\r
354                         xTicksToWait = ( TickType_t ) 0;\r
355 \r
356                         /* Clear the wait bits if requested to do so. */\r
357                         if( xClearOnExit != pdFALSE )\r
358                         {\r
359                                 pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
360                         }\r
361                         else\r
362                         {\r
363                                 mtCOVERAGE_TEST_MARKER();\r
364                         }\r
365                 }\r
366                 else if( xTicksToWait == ( TickType_t ) 0 )\r
367                 {\r
368                         /* The wait condition has not been met, but no block time was\r
369                         specified, so just return the current value. */\r
370                         uxReturn = uxCurrentEventBits;\r
371                 }\r
372                 else\r
373                 {\r
374                         /* The task is going to block to wait for its required bits to be\r
375                         set.  uxControlBits are used to remember the specified behaviour of\r
376                         this call to xEventGroupWaitBits() - for use when the event bits\r
377                         unblock the task. */\r
378                         if( xClearOnExit != pdFALSE )\r
379                         {\r
380                                 uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;\r
381                         }\r
382                         else\r
383                         {\r
384                                 mtCOVERAGE_TEST_MARKER();\r
385                         }\r
386 \r
387                         if( xWaitForAllBits != pdFALSE )\r
388                         {\r
389                                 uxControlBits |= eventWAIT_FOR_ALL_BITS;\r
390                         }\r
391                         else\r
392                         {\r
393                                 mtCOVERAGE_TEST_MARKER();\r
394                         }\r
395 \r
396                         /* Store the bits that the calling task is waiting for in the\r
397                         task's event list item so the kernel knows when a match is\r
398                         found.  Then enter the blocked state. */\r
399                         vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );\r
400 \r
401                         /* This is obsolete as it will get set after the task unblocks, but\r
402                         some compilers mistakenly generate a warning about the variable\r
403                         being returned without being set if it is not done. */\r
404                         uxReturn = 0;\r
405 \r
406                         traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );\r
407                 }\r
408         }\r
409         xAlreadyYielded = xTaskResumeAll();\r
410 \r
411         if( xTicksToWait != ( TickType_t ) 0 )\r
412         {\r
413                 if( xAlreadyYielded == pdFALSE )\r
414                 {\r
415                         portYIELD_WITHIN_API();\r
416                 }\r
417                 else\r
418                 {\r
419                         mtCOVERAGE_TEST_MARKER();\r
420                 }\r
421 \r
422                 /* The task blocked to wait for its required bits to be set - at this\r
423                 point either the required bits were set or the block time expired.  If\r
424                 the required bits were set they will have been stored in the task's\r
425                 event list item, and they should now be retrieved then cleared. */\r
426                 uxReturn = uxTaskResetEventItemValue();\r
427 \r
428                 if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )\r
429                 {\r
430                         taskENTER_CRITICAL();\r
431                         {\r
432                                 /* The task timed out, just return the current event bit value. */\r
433                                 uxReturn = pxEventBits->uxEventBits;\r
434 \r
435                                 /* It is possible that the event bits were updated between this\r
436                                 task leaving the Blocked state and running again. */\r
437                                 if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE )\r
438                                 {\r
439                                         if( xClearOnExit != pdFALSE )\r
440                                         {\r
441                                                 pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
442                                         }\r
443                                         else\r
444                                         {\r
445                                                 mtCOVERAGE_TEST_MARKER();\r
446                                         }\r
447                                 }\r
448                                 else\r
449                                 {\r
450                                         mtCOVERAGE_TEST_MARKER();\r
451                                 }\r
452                         }\r
453                         taskEXIT_CRITICAL();\r
454 \r
455                         /* Prevent compiler warnings when trace macros are not used. */\r
456                         xTimeoutOccurred = pdFALSE;\r
457                 }\r
458                 else\r
459                 {\r
460                         /* The task unblocked because the bits were set. */\r
461                 }\r
462 \r
463                 /* The task blocked so control bits may have been set. */\r
464                 uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;\r
465         }\r
466         traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );\r
467 \r
468         return uxReturn;\r
469 }\r
470 /*-----------------------------------------------------------*/\r
471 \r
472 EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )\r
473 {\r
474 EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;\r
475 EventBits_t uxReturn;\r
476 \r
477         /* Check the user is not attempting to clear the bits used by the kernel\r
478         itself. */\r
479         configASSERT( xEventGroup );\r
480         configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );\r
481 \r
482         taskENTER_CRITICAL();\r
483         {\r
484                 traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );\r
485 \r
486                 /* The value returned is the event group value prior to the bits being\r
487                 cleared. */\r
488                 uxReturn = pxEventBits->uxEventBits;\r
489 \r
490                 /* Clear the bits. */\r
491                 pxEventBits->uxEventBits &= ~uxBitsToClear;\r
492         }\r
493         taskEXIT_CRITICAL();\r
494 \r
495         return uxReturn;\r
496 }\r
497 /*-----------------------------------------------------------*/\r
498 \r
499 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )\r
500 \r
501         BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )\r
502         {\r
503                 BaseType_t xReturn;\r
504 \r
505                 traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );\r
506                 xReturn = xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL );\r
507 \r
508                 return xReturn;\r
509         }\r
510 \r
511 #endif\r
512 /*-----------------------------------------------------------*/\r
513 \r
514 EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )\r
515 {\r
516 UBaseType_t uxSavedInterruptStatus;\r
517 EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;\r
518 EventBits_t uxReturn;\r
519 \r
520         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
521         {\r
522                 uxReturn = pxEventBits->uxEventBits;\r
523         }\r
524         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
525 \r
526         return uxReturn;\r
527 }\r
528 /*-----------------------------------------------------------*/\r
529 \r
530 EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet )\r
531 {\r
532 ListItem_t *pxListItem, *pxNext;\r
533 ListItem_t const *pxListEnd;\r
534 List_t *pxList;\r
535 EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;\r
536 EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;\r
537 BaseType_t xMatchFound = pdFALSE;\r
538 \r
539         /* Check the user is not attempting to set the bits used by the kernel\r
540         itself. */\r
541         configASSERT( xEventGroup );\r
542         configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );\r
543 \r
544         pxList = &( pxEventBits->xTasksWaitingForBits );\r
545         pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */\r
546         vTaskSuspendAll();\r
547         {\r
548                 traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );\r
549 \r
550                 pxListItem = listGET_HEAD_ENTRY( pxList );\r
551 \r
552                 /* Set the bits. */\r
553                 pxEventBits->uxEventBits |= uxBitsToSet;\r
554 \r
555                 /* See if the new bit value should unblock any tasks. */\r
556                 while( pxListItem != pxListEnd )\r
557                 {\r
558                         pxNext = listGET_NEXT( pxListItem );\r
559                         uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );\r
560                         xMatchFound = pdFALSE;\r
561 \r
562                         /* Split the bits waited for from the control bits. */\r
563                         uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;\r
564                         uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;\r
565 \r
566                         if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )\r
567                         {\r
568                                 /* Just looking for single bit being set. */\r
569                                 if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )\r
570                                 {\r
571                                         xMatchFound = pdTRUE;\r
572                                 }\r
573                                 else\r
574                                 {\r
575                                         mtCOVERAGE_TEST_MARKER();\r
576                                 }\r
577                         }\r
578                         else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )\r
579                         {\r
580                                 /* All bits are set. */\r
581                                 xMatchFound = pdTRUE;\r
582                         }\r
583                         else\r
584                         {\r
585                                 /* Need all bits to be set, but not all the bits were set. */\r
586                         }\r
587 \r
588                         if( xMatchFound != pdFALSE )\r
589                         {\r
590                                 /* The bits match.  Should the bits be cleared on exit? */\r
591                                 if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )\r
592                                 {\r
593                                         uxBitsToClear |= uxBitsWaitedFor;\r
594                                 }\r
595                                 else\r
596                                 {\r
597                                         mtCOVERAGE_TEST_MARKER();\r
598                                 }\r
599 \r
600                                 /* Store the actual event flag value in the task's event list\r
601                                 item before removing the task from the event list.  The\r
602                                 eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows\r
603                                 that is was unblocked due to its required bits matching, rather\r
604                                 than because it timed out. */\r
605                                 ( void ) xTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );\r
606                         }\r
607 \r
608                         /* Move onto the next list item.  Note pxListItem->pxNext is not\r
609                         used here as the list item may have been removed from the event list\r
610                         and inserted into the ready/pending reading list. */\r
611                         pxListItem = pxNext;\r
612                 }\r
613 \r
614                 /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT\r
615                 bit was set in the control word. */\r
616                 pxEventBits->uxEventBits &= ~uxBitsToClear;\r
617         }\r
618         ( void ) xTaskResumeAll();\r
619 \r
620         return pxEventBits->uxEventBits;\r
621 }\r
622 /*-----------------------------------------------------------*/\r
623 \r
624 void vEventGroupDelete( EventGroupHandle_t xEventGroup )\r
625 {\r
626 EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;\r
627 const List_t *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );\r
628 \r
629         vTaskSuspendAll();\r
630         {\r
631                 traceEVENT_GROUP_DELETE( xEventGroup );\r
632 \r
633                 while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )\r
634                 {\r
635                         /* Unblock the task, returning 0 as the event list is being deleted\r
636                         and     cannot therefore have any bits set. */\r
637                         configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );\r
638                         ( void ) xTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );\r
639                 }\r
640 \r
641                 #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )\r
642                 {\r
643                         /* The event group can only have been allocated dynamically - free\r
644                         it again. */\r
645                         vPortFree( pxEventBits );\r
646                 }\r
647                 #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
648                 {\r
649                         /* The event group could have been allocated statically or\r
650                         dynamically, so check before attempting to free the memory. */\r
651                         if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdFALSE )\r
652                         {\r
653                                 vPortFree( pxEventBits );\r
654                         }\r
655                         else\r
656                         {\r
657                                 mtCOVERAGE_TEST_MARKER();\r
658                         }\r
659                 }\r
660                 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
661         }\r
662         ( void ) xTaskResumeAll();\r
663 }\r
664 /*-----------------------------------------------------------*/\r
665 \r
666 /* For internal use only - execute a 'set bits' command that was pended from\r
667 an interrupt. */\r
668 void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet )\r
669 {\r
670         ( void ) xEventGroupSetBits( pvEventGroup, ( EventBits_t ) ulBitsToSet );\r
671 }\r
672 /*-----------------------------------------------------------*/\r
673 \r
674 /* For internal use only - execute a 'clear bits' command that was pended from\r
675 an interrupt. */\r
676 void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear )\r
677 {\r
678         ( void ) xEventGroupClearBits( pvEventGroup, ( EventBits_t ) ulBitsToClear );\r
679 }\r
680 /*-----------------------------------------------------------*/\r
681 \r
682 static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits )\r
683 {\r
684 BaseType_t xWaitConditionMet = pdFALSE;\r
685 \r
686         if( xWaitForAllBits == pdFALSE )\r
687         {\r
688                 /* Task only has to wait for one bit within uxBitsToWaitFor to be\r
689                 set.  Is one already set? */\r
690                 if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )\r
691                 {\r
692                         xWaitConditionMet = pdTRUE;\r
693                 }\r
694                 else\r
695                 {\r
696                         mtCOVERAGE_TEST_MARKER();\r
697                 }\r
698         }\r
699         else\r
700         {\r
701                 /* Task has to wait for all the bits in uxBitsToWaitFor to be set.\r
702                 Are they set already? */\r
703                 if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
704                 {\r
705                         xWaitConditionMet = pdTRUE;\r
706                 }\r
707                 else\r
708                 {\r
709                         mtCOVERAGE_TEST_MARKER();\r
710                 }\r
711         }\r
712 \r
713         return xWaitConditionMet;\r
714 }\r
715 /*-----------------------------------------------------------*/\r
716 \r
717 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )\r
718 \r
719         BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken )\r
720         {\r
721         BaseType_t xReturn;\r
722 \r
723                 traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );\r
724                 xReturn = xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken );\r
725 \r
726                 return xReturn;\r
727         }\r
728 \r
729 #endif\r
730 /*-----------------------------------------------------------*/\r
731 \r
732 #if (configUSE_TRACE_FACILITY == 1)\r
733 \r
734         UBaseType_t uxEventGroupGetNumber( void* xEventGroup )\r
735         {\r
736         UBaseType_t xReturn;\r
737         EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;\r
738 \r
739                 if( xEventGroup == NULL )\r
740                 {\r
741                         xReturn = 0;\r
742                 }\r
743                 else\r
744                 {\r
745                         xReturn = pxEventBits->uxEventGroupNumber;\r
746                 }\r
747 \r
748                 return xReturn;\r
749         }\r
750 \r
751 #endif\r
752 \r