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