2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
70 #ifndef EVENT_GROUPS_H
71 #define EVENT_GROUPS_H
73 #ifndef INC_FREERTOS_H
74 #error "include FreeRTOS.h" must appear in source files before "include event_groups.h"
77 /* FreeRTOS includes. */
85 * An event group is a collection of bits to which an application can assign a
86 * meaning. For example, an application may create an event group to convey
87 * the status of various CAN bus related events in which bit 0 might mean "A CAN
88 * message has been received and is ready for processing", bit 1 might mean "The
89 * application has queued a message that is ready for sending onto the CAN
90 * network", and bit 2 might mean "It is time to send a SYNC message onto the
91 * CAN network" etc. A task can then test the bit values to see which events
92 * are active, and optionally enter the Blocked state to wait for a specified
93 * bit or a group of specified bits to be active. To continue the CAN bus
94 * example, a CAN controlling task can enter the Blocked state (and therefore
95 * not consume any processing time) until either bit 0, bit 1 or bit 2 are
96 * active, at which time the bit that was actually active would inform the task
97 * which action it had to take (process a received message, send a message, or
100 * The event groups implementation contains intelligence to avoid race
101 * conditions that would otherwise occur were an application to use a simple
102 * variable for the same purpose. This is particularly important with respect
103 * to when a bit within an event group is to be cleared, and when bits have to
104 * be set and then tested atomically - as is the case where event groups are
105 * used to create a synchronisation point between multiple tasks (a
108 * \defgroup EventGroup
116 * Type by which event groups are referenced. For example, a call to
117 * xEventGroupCreate() returns an EventGroupHandle_t variable that can then
118 * be used as a parameter to other event group functions.
120 * \defgroup EventGroupHandle_t EventGroupHandle_t
121 * \ingroup EventGroup
123 typedef void * EventGroupHandle_t;
126 * The type that holds event bits always matches TickType_t - therefore the
127 * number of bits it holds is set by configUSE_16_BIT_TICKS (16 bits if set to 1,
128 * 32 bits if set to 0.
130 * \defgroup EventBits_t EventBits_t
131 * \ingroup EventGroup
133 typedef TickType_t EventBits_t;
138 EventGroupHandle_t xEventGroupCreate( void );
141 * Create a new event group.
143 * Internally, within the FreeRTOS implementation, event groups use a [small]
144 * block of memory, in which the event group's structure is stored. If an event
145 * groups is created using xEventGropuCreate() then the required memory is
146 * automatically dynamically allocated inside the xEventGroupCreate() function.
147 * (see http://www.freertos.org/a00111.html). If an event group is created
148 * using xEventGropuCreateStatic() then the application writer must instead
149 * provide the memory that will get used by the event group.
150 * xEventGroupCreateStatic() therefore allows an event group to be created
151 * without using any dynamic memory allocation.
153 * Although event groups are not related to ticks, for internal implementation
154 * reasons the number of bits available for use in an event group is dependent
155 * on the configUSE_16_BIT_TICKS setting in FreeRTOSConfig.h. If
156 * configUSE_16_BIT_TICKS is 1 then each event group contains 8 usable bits (bit
157 * 0 to bit 7). If configUSE_16_BIT_TICKS is set to 0 then each event group has
158 * 24 usable bits (bit 0 to bit 23). The EventBits_t type is used to store
159 * event bits within an event group.
161 * @return If the event group was created then a handle to the event group is
162 * returned. If there was insufficient FreeRTOS heap available to create the
163 * event group then NULL is returned. See http://www.freertos.org/a00111.html
167 // Declare a variable to hold the created event group.
168 EventGroupHandle_t xCreatedEventGroup;
170 // Attempt to create the event group.
171 xCreatedEventGroup = xEventGroupCreate();
173 // Was the event group created successfully?
174 if( xCreatedEventGroup == NULL )
176 // The event group was not created because there was insufficient
177 // FreeRTOS heap available.
181 // The event group was created.
184 * \defgroup xEventGroupCreate xEventGroupCreate
185 * \ingroup EventGroup
187 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
188 EventGroupHandle_t xEventGroupCreate( void ) PRIVILEGED_FUNCTION;
194 EventGroupHandle_t xEventGroupCreateStatic( EventGroupHandle_t * pxEventGroupBuffer );
197 * Create a new event group.
199 * Internally, within the FreeRTOS implementation, event groups use a [small]
200 * block of memory, in which the event group's structure is stored. If an event
201 * groups is created using xEventGropuCreate() then the required memory is
202 * automatically dynamically allocated inside the xEventGroupCreate() function.
203 * (see http://www.freertos.org/a00111.html). If an event group is created
204 * using xEventGropuCreateStatic() then the application writer must instead
205 * provide the memory that will get used by the event group.
206 * xEventGroupCreateStatic() therefore allows an event group to be created
207 * without using any dynamic memory allocation.
209 * Although event groups are not related to ticks, for internal implementation
210 * reasons the number of bits available for use in an event group is dependent
211 * on the configUSE_16_BIT_TICKS setting in FreeRTOSConfig.h. If
212 * configUSE_16_BIT_TICKS is 1 then each event group contains 8 usable bits (bit
213 * 0 to bit 7). If configUSE_16_BIT_TICKS is set to 0 then each event group has
214 * 24 usable bits (bit 0 to bit 23). The EventBits_t type is used to store
215 * event bits within an event group.
217 * @param pxEventGroupBuffer pxEventGroupBuffer must point to a variable of type
218 * StaticEventGroup_t, which will be then be used to hold the event group's data
219 * structures, removing the need for the memory to be allocated dynamically.
221 * @return If the event group was created then a handle to the event group is
222 * returned. If pxEventGroupBuffer was NULL then NULL is returned.
226 // StaticEventGroup_t is a publicly accessible structure that has the same
227 // size and alignment requirements as the real event group structure. It is
228 // provided as a mechanism for applications to know the size of the event
229 // group (which is dependent on the architecture and configuration file
230 // settings) without breaking the strict data hiding policy by exposing the
231 // real event group internals. This StaticEventGroup_t variable is passed
232 // into the xSemaphoreCreateEventGroupStatic() function and is used to store
233 // the event group's data structures
234 StaticEventGroup_t xEventGroupBuffer;
236 // Create the event group without dynamically allocating any memory.
237 xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );
240 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
241 EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t *pxEventGroupBuffer ) PRIVILEGED_FUNCTION;
247 EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
248 const EventBits_t uxBitsToWaitFor,
249 const BaseType_t xClearOnExit,
250 const BaseType_t xWaitForAllBits,
251 const TickType_t xTicksToWait );
254 * [Potentially] block to wait for one or more bits to be set within a
255 * previously created event group.
257 * This function cannot be called from an interrupt.
259 * @param xEventGroup The event group in which the bits are being tested. The
260 * event group must have previously been created using a call to
261 * xEventGroupCreate().
263 * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
264 * inside the event group. For example, to wait for bit 0 and/or bit 2 set
265 * uxBitsToWaitFor to 0x05. To wait for bits 0 and/or bit 1 and/or bit 2 set
266 * uxBitsToWaitFor to 0x07. Etc.
268 * @param xClearOnExit If xClearOnExit is set to pdTRUE then any bits within
269 * uxBitsToWaitFor that are set within the event group will be cleared before
270 * xEventGroupWaitBits() returns if the wait condition was met (if the function
271 * returns for a reason other than a timeout). If xClearOnExit is set to
272 * pdFALSE then the bits set in the event group are not altered when the call to
273 * xEventGroupWaitBits() returns.
275 * @param xWaitForAllBits If xWaitForAllBits is set to pdTRUE then
276 * xEventGroupWaitBits() will return when either all the bits in uxBitsToWaitFor
277 * are set or the specified block time expires. If xWaitForAllBits is set to
278 * pdFALSE then xEventGroupWaitBits() will return when any one of the bits set
279 * in uxBitsToWaitFor is set or the specified block time expires. The block
280 * time is specified by the xTicksToWait parameter.
282 * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
283 * for one/all (depending on the xWaitForAllBits value) of the bits specified by
284 * uxBitsToWaitFor to become set.
286 * @return The value of the event group at the time either the bits being waited
287 * for became set, or the block time expired. Test the return value to know
288 * which bits were set. If xEventGroupWaitBits() returned because its timeout
289 * expired then not all the bits being waited for will be set. If
290 * xEventGroupWaitBits() returned because the bits it was waiting for were set
291 * then the returned value is the event group value before any bits were
292 * automatically cleared in the case that xClearOnExit parameter was set to
297 #define BIT_0 ( 1 << 0 )
298 #define BIT_4 ( 1 << 4 )
300 void aFunction( EventGroupHandle_t xEventGroup )
303 const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
305 // Wait a maximum of 100ms for either bit 0 or bit 4 to be set within
306 // the event group. Clear the bits before exiting.
307 uxBits = xEventGroupWaitBits(
308 xEventGroup, // The event group being tested.
309 BIT_0 | BIT_4, // The bits within the event group to wait for.
310 pdTRUE, // BIT_0 and BIT_4 should be cleared before returning.
311 pdFALSE, // Don't wait for both bits, either bit will do.
312 xTicksToWait ); // Wait a maximum of 100ms for either bit to be set.
314 if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
316 // xEventGroupWaitBits() returned because both bits were set.
318 else if( ( uxBits & BIT_0 ) != 0 )
320 // xEventGroupWaitBits() returned because just BIT_0 was set.
322 else if( ( uxBits & BIT_4 ) != 0 )
324 // xEventGroupWaitBits() returned because just BIT_4 was set.
328 // xEventGroupWaitBits() returned because xTicksToWait ticks passed
329 // without either BIT_0 or BIT_4 becoming set.
333 * \defgroup xEventGroupWaitBits xEventGroupWaitBits
334 * \ingroup EventGroup
336 EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
341 EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear );
344 * Clear bits within an event group. This function cannot be called from an
347 * @param xEventGroup The event group in which the bits are to be cleared.
349 * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear
350 * in the event group. For example, to clear bit 3 only, set uxBitsToClear to
351 * 0x08. To clear bit 3 and bit 0 set uxBitsToClear to 0x09.
353 * @return The value of the event group before the specified bits were cleared.
357 #define BIT_0 ( 1 << 0 )
358 #define BIT_4 ( 1 << 4 )
360 void aFunction( EventGroupHandle_t xEventGroup )
364 // Clear bit 0 and bit 4 in xEventGroup.
365 uxBits = xEventGroupClearBits(
366 xEventGroup, // The event group being updated.
367 BIT_0 | BIT_4 );// The bits being cleared.
369 if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
371 // Both bit 0 and bit 4 were set before xEventGroupClearBits() was
372 // called. Both will now be clear (not set).
374 else if( ( uxBits & BIT_0 ) != 0 )
376 // Bit 0 was set before xEventGroupClearBits() was called. It will
379 else if( ( uxBits & BIT_4 ) != 0 )
381 // Bit 4 was set before xEventGroupClearBits() was called. It will
386 // Neither bit 0 nor bit 4 were set in the first place.
390 * \defgroup xEventGroupClearBits xEventGroupClearBits
391 * \ingroup EventGroup
393 EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear ) PRIVILEGED_FUNCTION;
398 BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
401 * A version of xEventGroupClearBits() that can be called from an interrupt.
403 * Setting bits in an event group is not a deterministic operation because there
404 * are an unknown number of tasks that may be waiting for the bit or bits being
405 * set. FreeRTOS does not allow nondeterministic operations to be performed
406 * while interrupts are disabled, so protects event groups that are accessed
407 * from tasks by suspending the scheduler rather than disabling interrupts. As
408 * a result event groups cannot be accessed directly from an interrupt service
409 * routine. Therefore xEventGroupClearBitsFromISR() sends a message to the
410 * timer task to have the clear operation performed in the context of the timer
413 * @param xEventGroup The event group in which the bits are to be cleared.
415 * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear.
416 * For example, to clear bit 3 only, set uxBitsToClear to 0x08. To clear bit 3
417 * and bit 0 set uxBitsToClear to 0x09.
419 * @return If the request to execute the function was posted successfully then
420 * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
421 * if the timer service queue was full.
425 #define BIT_0 ( 1 << 0 )
426 #define BIT_4 ( 1 << 4 )
428 // An event group which it is assumed has already been created by a call to
429 // xEventGroupCreate().
430 EventGroupHandle_t xEventGroup;
432 void anInterruptHandler( void )
434 // Clear bit 0 and bit 4 in xEventGroup.
435 xResult = xEventGroupClearBitsFromISR(
436 xEventGroup, // The event group being updated.
437 BIT_0 | BIT_4 ); // The bits being set.
439 if( xResult == pdPASS )
441 // The message was posted successfully.
445 * \defgroup xEventGroupClearBitsFromISR xEventGroupClearBitsFromISR
446 * \ingroup EventGroup
448 #if( configUSE_TRACE_FACILITY == 1 )
449 BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
451 #define xEventGroupClearBitsFromISR( xEventGroup, uxBitsToClear ) xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL )
457 EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
460 * Set bits within an event group.
461 * This function cannot be called from an interrupt. xEventGroupSetBitsFromISR()
462 * is a version that can be called from an interrupt.
464 * Setting bits in an event group will automatically unblock tasks that are
465 * blocked waiting for the bits.
467 * @param xEventGroup The event group in which the bits are to be set.
469 * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
470 * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
471 * and bit 0 set uxBitsToSet to 0x09.
473 * @return The value of the event group at the time the call to
474 * xEventGroupSetBits() returns. There are two reasons why the returned value
475 * might have the bits specified by the uxBitsToSet parameter cleared. First,
476 * if setting a bit results in a task that was waiting for the bit leaving the
477 * blocked state then it is possible the bit will be cleared automatically
478 * (see the xClearBitOnExit parameter of xEventGroupWaitBits()). Second, any
479 * unblocked (or otherwise Ready state) task that has a priority above that of
480 * the task that called xEventGroupSetBits() will execute and may change the
481 * event group value before the call to xEventGroupSetBits() returns.
485 #define BIT_0 ( 1 << 0 )
486 #define BIT_4 ( 1 << 4 )
488 void aFunction( EventGroupHandle_t xEventGroup )
492 // Set bit 0 and bit 4 in xEventGroup.
493 uxBits = xEventGroupSetBits(
494 xEventGroup, // The event group being updated.
495 BIT_0 | BIT_4 );// The bits being set.
497 if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
499 // Both bit 0 and bit 4 remained set when the function returned.
501 else if( ( uxBits & BIT_0 ) != 0 )
503 // Bit 0 remained set when the function returned, but bit 4 was
504 // cleared. It might be that bit 4 was cleared automatically as a
505 // task that was waiting for bit 4 was removed from the Blocked
508 else if( ( uxBits & BIT_4 ) != 0 )
510 // Bit 4 remained set when the function returned, but bit 0 was
511 // cleared. It might be that bit 0 was cleared automatically as a
512 // task that was waiting for bit 0 was removed from the Blocked
517 // Neither bit 0 nor bit 4 remained set. It might be that a task
518 // was waiting for both of the bits to be set, and the bits were
519 // cleared as the task left the Blocked state.
523 * \defgroup xEventGroupSetBits xEventGroupSetBits
524 * \ingroup EventGroup
526 EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
531 BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken );
534 * A version of xEventGroupSetBits() that can be called from an interrupt.
536 * Setting bits in an event group is not a deterministic operation because there
537 * are an unknown number of tasks that may be waiting for the bit or bits being
538 * set. FreeRTOS does not allow nondeterministic operations to be performed in
539 * interrupts or from critical sections. Therefore xEventGroupSetBitsFromISR()
540 * sends a message to the timer task to have the set operation performed in the
541 * context of the timer task - where a scheduler lock is used in place of a
544 * @param xEventGroup The event group in which the bits are to be set.
546 * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
547 * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
548 * and bit 0 set uxBitsToSet to 0x09.
550 * @param pxHigherPriorityTaskWoken As mentioned above, calling this function
551 * will result in a message being sent to the timer daemon task. If the
552 * priority of the timer daemon task is higher than the priority of the
553 * currently running task (the task the interrupt interrupted) then
554 * *pxHigherPriorityTaskWoken will be set to pdTRUE by
555 * xEventGroupSetBitsFromISR(), indicating that a context switch should be
556 * requested before the interrupt exits. For that reason
557 * *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the
558 * example code below.
560 * @return If the request to execute the function was posted successfully then
561 * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
562 * if the timer service queue was full.
566 #define BIT_0 ( 1 << 0 )
567 #define BIT_4 ( 1 << 4 )
569 // An event group which it is assumed has already been created by a call to
570 // xEventGroupCreate().
571 EventGroupHandle_t xEventGroup;
573 void anInterruptHandler( void )
575 BaseType_t xHigherPriorityTaskWoken, xResult;
577 // xHigherPriorityTaskWoken must be initialised to pdFALSE.
578 xHigherPriorityTaskWoken = pdFALSE;
580 // Set bit 0 and bit 4 in xEventGroup.
581 xResult = xEventGroupSetBitsFromISR(
582 xEventGroup, // The event group being updated.
583 BIT_0 | BIT_4 // The bits being set.
584 &xHigherPriorityTaskWoken );
586 // Was the message posted successfully?
587 if( xResult == pdPASS )
589 // If xHigherPriorityTaskWoken is now set to pdTRUE then a context
590 // switch should be requested. The macro used is port specific and
591 // will be either portYIELD_FROM_ISR() or portEND_SWITCHING_ISR() -
592 // refer to the documentation page for the port being used.
593 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
597 * \defgroup xEventGroupSetBitsFromISR xEventGroupSetBitsFromISR
598 * \ingroup EventGroup
600 #if( configUSE_TRACE_FACILITY == 1 )
601 BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
603 #define xEventGroupSetBitsFromISR( xEventGroup, uxBitsToSet, pxHigherPriorityTaskWoken ) xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken )
609 EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
610 const EventBits_t uxBitsToSet,
611 const EventBits_t uxBitsToWaitFor,
612 TickType_t xTicksToWait );
615 * Atomically set bits within an event group, then wait for a combination of
616 * bits to be set within the same event group. This functionality is typically
617 * used to synchronise multiple tasks, where each task has to wait for the other
618 * tasks to reach a synchronisation point before proceeding.
620 * This function cannot be used from an interrupt.
622 * The function will return before its block time expires if the bits specified
623 * by the uxBitsToWait parameter are set, or become set within that time. In
624 * this case all the bits specified by uxBitsToWait will be automatically
625 * cleared before the function returns.
627 * @param xEventGroup The event group in which the bits are being tested. The
628 * event group must have previously been created using a call to
629 * xEventGroupCreate().
631 * @param uxBitsToSet The bits to set in the event group before determining
632 * if, and possibly waiting for, all the bits specified by the uxBitsToWait
635 * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
636 * inside the event group. For example, to wait for bit 0 and bit 2 set
637 * uxBitsToWaitFor to 0x05. To wait for bits 0 and bit 1 and bit 2 set
638 * uxBitsToWaitFor to 0x07. Etc.
640 * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
641 * for all of the bits specified by uxBitsToWaitFor to become set.
643 * @return The value of the event group at the time either the bits being waited
644 * for became set, or the block time expired. Test the return value to know
645 * which bits were set. If xEventGroupSync() returned because its timeout
646 * expired then not all the bits being waited for will be set. If
647 * xEventGroupSync() returned because all the bits it was waiting for were
648 * set then the returned value is the event group value before any bits were
649 * automatically cleared.
653 // Bits used by the three tasks.
654 #define TASK_0_BIT ( 1 << 0 )
655 #define TASK_1_BIT ( 1 << 1 )
656 #define TASK_2_BIT ( 1 << 2 )
658 #define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )
660 // Use an event group to synchronise three tasks. It is assumed this event
661 // group has already been created elsewhere.
662 EventGroupHandle_t xEventBits;
664 void vTask0( void *pvParameters )
666 EventBits_t uxReturn;
667 TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
671 // Perform task functionality here.
673 // Set bit 0 in the event flag to note this task has reached the
674 // sync point. The other two tasks will set the other two bits defined
675 // by ALL_SYNC_BITS. All three tasks have reached the synchronisation
676 // point when all the ALL_SYNC_BITS are set. Wait a maximum of 100ms
677 // for this to happen.
678 uxReturn = xEventGroupSync( xEventBits, TASK_0_BIT, ALL_SYNC_BITS, xTicksToWait );
680 if( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
682 // All three tasks reached the synchronisation point before the call
683 // to xEventGroupSync() timed out.
688 void vTask1( void *pvParameters )
692 // Perform task functionality here.
694 // Set bit 1 in the event flag to note this task has reached the
695 // synchronisation point. The other two tasks will set the other two
696 // bits defined by ALL_SYNC_BITS. All three tasks have reached the
697 // synchronisation point when all the ALL_SYNC_BITS are set. Wait
698 // indefinitely for this to happen.
699 xEventGroupSync( xEventBits, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );
701 // xEventGroupSync() was called with an indefinite block time, so
702 // this task will only reach here if the syncrhonisation was made by all
703 // three tasks, so there is no need to test the return value.
707 void vTask2( void *pvParameters )
711 // Perform task functionality here.
713 // Set bit 2 in the event flag to note this task has reached the
714 // synchronisation point. The other two tasks will set the other two
715 // bits defined by ALL_SYNC_BITS. All three tasks have reached the
716 // synchronisation point when all the ALL_SYNC_BITS are set. Wait
717 // indefinitely for this to happen.
718 xEventGroupSync( xEventBits, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );
720 // xEventGroupSync() was called with an indefinite block time, so
721 // this task will only reach here if the syncrhonisation was made by all
722 // three tasks, so there is no need to test the return value.
727 * \defgroup xEventGroupSync xEventGroupSync
728 * \ingroup EventGroup
730 EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
736 EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup );
739 * Returns the current value of the bits in an event group. This function
740 * cannot be used from an interrupt.
742 * @param xEventGroup The event group being queried.
744 * @return The event group bits at the time xEventGroupGetBits() was called.
746 * \defgroup xEventGroupGetBits xEventGroupGetBits
747 * \ingroup EventGroup
749 #define xEventGroupGetBits( xEventGroup ) xEventGroupClearBits( xEventGroup, 0 )
754 EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup );
757 * A version of xEventGroupGetBits() that can be called from an ISR.
759 * @param xEventGroup The event group being queried.
761 * @return The event group bits at the time xEventGroupGetBitsFromISR() was called.
763 * \defgroup xEventGroupGetBitsFromISR xEventGroupGetBitsFromISR
764 * \ingroup EventGroup
766 EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
771 void xEventGroupDelete( EventGroupHandle_t xEventGroup );
774 * Delete an event group that was previously created by a call to
775 * xEventGroupCreate(). Tasks that are blocked on the event group will be
776 * unblocked and obtain 0 as the event group's value.
778 * @param xEventGroup The event group being deleted.
780 void vEventGroupDelete( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
782 /* For internal use only. */
783 void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet ) PRIVILEGED_FUNCTION;
784 void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear ) PRIVILEGED_FUNCTION;
787 #if (configUSE_TRACE_FACILITY == 1)
788 UBaseType_t uxEventGroupGetNumber( void* xEventGroup ) PRIVILEGED_FUNCTION;
795 #endif /* EVENT_GROUPS_H */