Prusa MINI Firmware overview
event_groups.h
Go to the documentation of this file.
1 /*
2  FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3  All rights reserved
4 
5  VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6 
7  This file is part of the FreeRTOS distribution.
8 
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.
12 
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  ***************************************************************************
19 
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
24 
25  ***************************************************************************
26  * *
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. *
31  * *
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 *
36  * *
37  ***************************************************************************
38 
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()?
42 
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.
46 
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.
51 
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.
55 
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.
58 
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.
62 
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.
66 
67  1 tab == 4 spaces!
68 */
69 
70 #ifndef EVENT_GROUPS_H
71 #define EVENT_GROUPS_H
72 
73 #ifndef INC_FREERTOS_H
74  #error "include FreeRTOS.h" must appear in source files before "include event_groups.h"
75 #endif
76 
77 /* FreeRTOS includes. */
78 #include "timers.h"
79 
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
83 
84 /**
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
98  * send a SYNC).
99  *
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
106  * 'rendezvous').
107  *
108  * \defgroup EventGroup
109  */
110 
111 
112 
113 /**
114  * event_groups.h
115  *
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.
119  *
120  * \defgroup EventGroupHandle_t EventGroupHandle_t
121  * \ingroup EventGroup
122  */
123 typedef void * EventGroupHandle_t;
124 
125 /*
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.
129  *
130  * \defgroup EventBits_t EventBits_t
131  * \ingroup EventGroup
132  */
134 
135 /**
136  * event_groups.h
137  *<pre>
138  EventGroupHandle_t xEventGroupCreate( void );
139  </pre>
140  *
141  * Create a new event group.
142  *
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.
152  *
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.
160  *
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
164  *
165  * Example usage:
166  <pre>
167  // Declare a variable to hold the created event group.
168  EventGroupHandle_t xCreatedEventGroup;
169 
170  // Attempt to create the event group.
171  xCreatedEventGroup = xEventGroupCreate();
172 
173  // Was the event group created successfully?
174  if( xCreatedEventGroup == NULL )
175  {
176  // The event group was not created because there was insufficient
177  // FreeRTOS heap available.
178  }
179  else
180  {
181  // The event group was created.
182  }
183  </pre>
184  * \defgroup xEventGroupCreate xEventGroupCreate
185  * \ingroup EventGroup
186  */
187 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
188 PRIVILEGED_FUNCTION EventGroupHandle_t xEventGroupCreate( void ) ;
189 #endif
190 
191 /**
192  * event_groups.h
193  *<pre>
194  EventGroupHandle_t xEventGroupCreateStatic( EventGroupHandle_t * pxEventGroupBuffer );
195  </pre>
196  *
197  * Create a new event group.
198  *
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.
208  *
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.
216  *
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.
220  *
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.
223  *
224  * Example usage:
225  <pre>
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;
235 
236  // Create the event group without dynamically allocating any memory.
237  xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );
238  </pre>
239  */
240 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
241 PRIVILEGED_FUNCTION EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t *pxEventGroupBuffer );
242 #endif
243 
244 /**
245  * event_groups.h
246  *<pre>
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 );
252  </pre>
253  *
254  * [Potentially] block to wait for one or more bits to be set within a
255  * previously created event group.
256  *
257  * This function cannot be called from an interrupt.
258  *
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().
262  *
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.
267  *
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.
274  *
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.
281  *
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.
285  *
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
293  * pdTRUE.
294  *
295  * Example usage:
296  <pre>
297  #define BIT_0 ( 1 << 0 )
298  #define BIT_4 ( 1 << 4 )
299 
300  void aFunction( EventGroupHandle_t xEventGroup )
301  {
302  EventBits_t uxBits;
303  const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
304 
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.
313 
314  if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
315  {
316  // xEventGroupWaitBits() returned because both bits were set.
317  }
318  else if( ( uxBits & BIT_0 ) != 0 )
319  {
320  // xEventGroupWaitBits() returned because just BIT_0 was set.
321  }
322  else if( ( uxBits & BIT_4 ) != 0 )
323  {
324  // xEventGroupWaitBits() returned because just BIT_4 was set.
325  }
326  else
327  {
328  // xEventGroupWaitBits() returned because xTicksToWait ticks passed
329  // without either BIT_0 or BIT_4 becoming set.
330  }
331  }
332  </pre>
333  * \defgroup xEventGroupWaitBits xEventGroupWaitBits
334  * \ingroup EventGroup
335  */
336 PRIVILEGED_FUNCTION EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait );
337 
338 /**
339  * event_groups.h
340  *<pre>
341  EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear );
342  </pre>
343  *
344  * Clear bits within an event group. This function cannot be called from an
345  * interrupt.
346  *
347  * @param xEventGroup The event group in which the bits are to be cleared.
348  *
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.
352  *
353  * @return The value of the event group before the specified bits were cleared.
354  *
355  * Example usage:
356  <pre>
357  #define BIT_0 ( 1 << 0 )
358  #define BIT_4 ( 1 << 4 )
359 
360  void aFunction( EventGroupHandle_t xEventGroup )
361  {
362  EventBits_t uxBits;
363 
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.
368 
369  if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
370  {
371  // Both bit 0 and bit 4 were set before xEventGroupClearBits() was
372  // called. Both will now be clear (not set).
373  }
374  else if( ( uxBits & BIT_0 ) != 0 )
375  {
376  // Bit 0 was set before xEventGroupClearBits() was called. It will
377  // now be clear.
378  }
379  else if( ( uxBits & BIT_4 ) != 0 )
380  {
381  // Bit 4 was set before xEventGroupClearBits() was called. It will
382  // now be clear.
383  }
384  else
385  {
386  // Neither bit 0 nor bit 4 were set in the first place.
387  }
388  }
389  </pre>
390  * \defgroup xEventGroupClearBits xEventGroupClearBits
391  * \ingroup EventGroup
392  */
394 
395 /**
396  * event_groups.h
397  *<pre>
398  BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
399  </pre>
400  *
401  * A version of xEventGroupClearBits() that can be called from an interrupt.
402  *
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
411  * task.
412  *
413  * @param xEventGroup The event group in which the bits are to be cleared.
414  *
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.
418  *
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.
422  *
423  * Example usage:
424  <pre>
425  #define BIT_0 ( 1 << 0 )
426  #define BIT_4 ( 1 << 4 )
427 
428  // An event group which it is assumed has already been created by a call to
429  // xEventGroupCreate().
430  EventGroupHandle_t xEventGroup;
431 
432  void anInterruptHandler( void )
433  {
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.
438 
439  if( xResult == pdPASS )
440  {
441  // The message was posted successfully.
442  }
443  }
444  </pre>
445  * \defgroup xEventGroupClearBitsFromISR xEventGroupClearBitsFromISR
446  * \ingroup EventGroup
447  */
448 #if( configUSE_TRACE_FACILITY == 1 )
450 #else
451  #define xEventGroupClearBitsFromISR( xEventGroup, uxBitsToClear ) xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL )
452 #endif
453 
454 /**
455  * event_groups.h
456  *<pre>
457  EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
458  </pre>
459  *
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.
463  *
464  * Setting bits in an event group will automatically unblock tasks that are
465  * blocked waiting for the bits.
466  *
467  * @param xEventGroup The event group in which the bits are to be set.
468  *
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.
472  *
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.
482  *
483  * Example usage:
484  <pre>
485  #define BIT_0 ( 1 << 0 )
486  #define BIT_4 ( 1 << 4 )
487 
488  void aFunction( EventGroupHandle_t xEventGroup )
489  {
490  EventBits_t uxBits;
491 
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.
496 
497  if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
498  {
499  // Both bit 0 and bit 4 remained set when the function returned.
500  }
501  else if( ( uxBits & BIT_0 ) != 0 )
502  {
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
506  // state.
507  }
508  else if( ( uxBits & BIT_4 ) != 0 )
509  {
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
513  // state.
514  }
515  else
516  {
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.
520  }
521  }
522  </pre>
523  * \defgroup xEventGroupSetBits xEventGroupSetBits
524  * \ingroup EventGroup
525  */
527 
528 /**
529  * event_groups.h
530  *<pre>
531  BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken );
532  </pre>
533  *
534  * A version of xEventGroupSetBits() that can be called from an interrupt.
535  *
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
542  * critical section.
543  *
544  * @param xEventGroup The event group in which the bits are to be set.
545  *
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.
549  *
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.
559  *
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.
563  *
564  * Example usage:
565  <pre>
566  #define BIT_0 ( 1 << 0 )
567  #define BIT_4 ( 1 << 4 )
568 
569  // An event group which it is assumed has already been created by a call to
570  // xEventGroupCreate().
571  EventGroupHandle_t xEventGroup;
572 
573  void anInterruptHandler( void )
574  {
575  BaseType_t xHigherPriorityTaskWoken, xResult;
576 
577  // xHigherPriorityTaskWoken must be initialised to pdFALSE.
578  xHigherPriorityTaskWoken = pdFALSE;
579 
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 );
585 
586  // Was the message posted successfully?
587  if( xResult == pdPASS )
588  {
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 );
594  }
595  }
596  </pre>
597  * \defgroup xEventGroupSetBitsFromISR xEventGroupSetBitsFromISR
598  * \ingroup EventGroup
599  */
600 #if( configUSE_TRACE_FACILITY == 1 )
601  PRIVILEGED_FUNCTION BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken );
602 #else
603  #define xEventGroupSetBitsFromISR( xEventGroup, uxBitsToSet, pxHigherPriorityTaskWoken ) xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken )
604 #endif
605 
606 /**
607  * event_groups.h
608  *<pre>
609  EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
610  const EventBits_t uxBitsToSet,
611  const EventBits_t uxBitsToWaitFor,
612  TickType_t xTicksToWait );
613  </pre>
614  *
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.
619  *
620  * This function cannot be used from an interrupt.
621  *
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.
626  *
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().
630  *
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
633  * parameter are set.
634  *
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.
639  *
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.
642  *
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.
650  *
651  * Example usage:
652  <pre>
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 )
657 
658  #define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )
659 
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;
663 
664  void vTask0( void *pvParameters )
665  {
666  EventBits_t uxReturn;
667  TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
668 
669  for( ;; )
670  {
671  // Perform task functionality here.
672 
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 );
679 
680  if( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
681  {
682  // All three tasks reached the synchronisation point before the call
683  // to xEventGroupSync() timed out.
684  }
685  }
686  }
687 
688  void vTask1( void *pvParameters )
689  {
690  for( ;; )
691  {
692  // Perform task functionality here.
693 
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 );
700 
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.
704  }
705  }
706 
707  void vTask2( void *pvParameters )
708  {
709  for( ;; )
710  {
711  // Perform task functionality here.
712 
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 );
719 
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.
723  }
724  }
725 
726  </pre>
727  * \defgroup xEventGroupSync xEventGroupSync
728  * \ingroup EventGroup
729  */
730 PRIVILEGED_FUNCTION EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait );
731 
732 
733 /**
734  * event_groups.h
735  *<pre>
736  EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup );
737  </pre>
738  *
739  * Returns the current value of the bits in an event group. This function
740  * cannot be used from an interrupt.
741  *
742  * @param xEventGroup The event group being queried.
743  *
744  * @return The event group bits at the time xEventGroupGetBits() was called.
745  *
746  * \defgroup xEventGroupGetBits xEventGroupGetBits
747  * \ingroup EventGroup
748  */
749 #define xEventGroupGetBits( xEventGroup ) xEventGroupClearBits( xEventGroup, 0 )
750 
751 /**
752  * event_groups.h
753  *<pre>
754  EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup );
755  </pre>
756  *
757  * A version of xEventGroupGetBits() that can be called from an ISR.
758  *
759  * @param xEventGroup The event group being queried.
760  *
761  * @return The event group bits at the time xEventGroupGetBitsFromISR() was called.
762  *
763  * \defgroup xEventGroupGetBitsFromISR xEventGroupGetBitsFromISR
764  * \ingroup EventGroup
765  */
767 
768 /**
769  * event_groups.h
770  *<pre>
771  void xEventGroupDelete( EventGroupHandle_t xEventGroup );
772  </pre>
773  *
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.
777  *
778  * @param xEventGroup The event group being deleted.
779  */
781 
782 /* For internal use only. */
783 PRIVILEGED_FUNCTION void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet );
784 PRIVILEGED_FUNCTION void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear );
785 
786 
787 #if (configUSE_TRACE_FACILITY == 1)
788  PRIVILEGED_FUNCTION UBaseType_t uxEventGroupGetNumber( void* xEventGroup );
789 #endif
790 
791 #ifdef __cplusplus
792 }
793 #endif
794 
795 #endif /* EVENT_GROUPS_H */
vEventGroupClearBitsCallback
PRIVILEGED_FUNCTION void vEventGroupClearBitsCallback(void *pvEventGroup, const uint32_t ulBitsToClear)
Definition: event_groups.c:676
xEventGroupSync
PRIVILEGED_FUNCTION EventBits_t xEventGroupSync(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait)
Definition: event_groups.c:208
xEventGroupClearBits
PRIVILEGED_FUNCTION EventBits_t xEventGroupClearBits(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear)
Definition: event_groups.c:472
vEventGroupDelete
PRIVILEGED_FUNCTION void vEventGroupDelete(EventGroupHandle_t xEventGroup)
Definition: event_groups.c:624
xEventGroupSetBitsFromISR
#define xEventGroupSetBitsFromISR(xEventGroup, uxBitsToSet, pxHigherPriorityTaskWoken)
Definition: event_groups.h:603
timers.h
EventBits_t
TickType_t EventBits_t
Definition: event_groups.h:133
EventGroupHandle_t
void * EventGroupHandle_t
Definition: event_groups.h:123
PRIVILEGED_FUNCTION
#define PRIVILEGED_FUNCTION
Definition: mpu_wrappers.h:204
TickType_t
uint32_t TickType_t
Definition: portmacro.h:105
xEventGroupClearBitsFromISR
#define xEventGroupClearBitsFromISR(xEventGroup, uxBitsToClear)
Definition: event_groups.h:451
xEventGroupSetBits
PRIVILEGED_FUNCTION EventBits_t xEventGroupSetBits(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet)
Definition: event_groups.c:530
UBaseType_t
unsigned long UBaseType_t
Definition: portmacro.h:99
vEventGroupSetBitsCallback
PRIVILEGED_FUNCTION void vEventGroupSetBitsCallback(void *pvEventGroup, const uint32_t ulBitsToSet)
Definition: event_groups.c:668
xSTATIC_EVENT_GROUP
Definition: FreeRTOS.h:1012
BaseType_t
long BaseType_t
Definition: portmacro.h:98
xEventGroupGetBitsFromISR
PRIVILEGED_FUNCTION EventBits_t xEventGroupGetBitsFromISR(EventGroupHandle_t xEventGroup)
Definition: event_groups.c:514
xEventGroupWaitBits
PRIVILEGED_FUNCTION EventBits_t xEventGroupWaitBits(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait)
Definition: event_groups.c:324