event_groups.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. #ifndef EVENT_GROUPS_H
  56. #define EVENT_GROUPS_H
  57. #ifndef INC_FREERTOS_H
  58. #error "include FreeRTOS.h" must appear in source files before "include event_groups.h"
  59. #endif
  60. #include "timers.h"
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. /**
  65. * An event group is a collection of bits to which an application can assign a
  66. * meaning. For example, an application may create an event group to convey
  67. * the status of various CAN bus related events in which bit 0 might mean "A CAN
  68. * message has been received and is ready for processing", bit 1 might mean "The
  69. * application has queued a message that is ready for sending onto the CAN
  70. * network", and bit 2 might mean "It is time to send a SYNC message onto the
  71. * CAN network" etc. A task can then test the bit values to see which events
  72. * are active, and optionally enter the Blocked state to wait for a specified
  73. * bit or a group of specified bits to be active. To continue the CAN bus
  74. * example, a CAN controlling task can enter the Blocked state (and therefore
  75. * not consume any processing time) until either bit 0, bit 1 or bit 2 are
  76. * active, at which time the bit that was actually active would inform the task
  77. * which action it had to take (process a received message, send a message, or
  78. * send a SYNC).
  79. *
  80. * The event groups implementation contains intelligence to avoid race
  81. * conditions that would otherwise occur were an application to use a simple
  82. * variable for the same purpose. This is particularly important with respect
  83. * to when a bit within an event group is to be cleared, and when bits have to
  84. * be set and then tested atomically - as is the case where event groups are
  85. * used to create a synchronisation point between multiple tasks (a
  86. * 'rendezvous').
  87. *
  88. * \defgroup EventGroup
  89. */
  90. /**
  91. * event_groups.h
  92. *
  93. * Type by which event groups are referenced. For example, a call to
  94. * xEventGroupCreate() returns an EventGroupHandle_t variable that can then
  95. * be used as a parameter to other event group functions.
  96. *
  97. * \defgroup EventGroupHandle_t EventGroupHandle_t
  98. * \ingroup EventGroup
  99. */
  100. typedef void * EventGroupHandle_t;
  101. /*
  102. * The type that holds event bits always matches TickType_t - therefore the
  103. * number of bits it holds is set by configUSE_16_BIT_TICKS (16 bits if set to 1,
  104. * 32 bits if set to 0.
  105. *
  106. * \defgroup EventBits_t EventBits_t
  107. * \ingroup EventGroup
  108. */
  109. typedef TickType_t EventBits_t;
  110. /**
  111. * event_groups.h
  112. *<pre>
  113. EventGroupHandle_t xEventGroupCreate( void );
  114. </pre>
  115. *
  116. * Create a new event group. This function cannot be called from an interrupt.
  117. *
  118. * Although event groups are not related to ticks, for internal implementation
  119. * reasons the number of bits available for use in an event group is dependent
  120. * on the configUSE_16_BIT_TICKS setting in FreeRTOSConfig.h. If
  121. * configUSE_16_BIT_TICKS is 1 then each event group contains 8 usable bits (bit
  122. * 0 to bit 7). If configUSE_16_BIT_TICKS is set to 0 then each event group has
  123. * 24 usable bits (bit 0 to bit 23). The EventBits_t type is used to store
  124. * event bits within an event group.
  125. *
  126. * @return If the event group was created then a handle to the event group is
  127. * returned. If there was insufficient FreeRTOS heap available to create the
  128. * event group then NULL is returned. See http://www.freertos.org/a00111.html
  129. *
  130. * Example usage:
  131. <pre>
  132. // Declare a variable to hold the created event group.
  133. EventGroupHandle_t xCreatedEventGroup;
  134. // Attempt to create the event group.
  135. xCreatedEventGroup = xEventGroupCreate();
  136. // Was the event group created successfully?
  137. if( xCreatedEventGroup == NULL )
  138. {
  139. // The event group was not created because there was insufficient
  140. // FreeRTOS heap available.
  141. }
  142. else
  143. {
  144. // The event group was created.
  145. }
  146. </pre>
  147. * \defgroup xEventGroupCreate xEventGroupCreate
  148. * \ingroup EventGroup
  149. */
  150. EventGroupHandle_t xEventGroupCreate( void ) PRIVILEGED_FUNCTION;
  151. /**
  152. * event_groups.h
  153. *<pre>
  154. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  155. const EventBits_t uxBitsToWaitFor,
  156. const BaseType_t xClearOnExit,
  157. const BaseType_t xWaitForAllBits,
  158. const TickType_t xTicksToWait );
  159. </pre>
  160. *
  161. * [Potentially] block to wait for one or more bits to be set within a
  162. * previously created event group.
  163. *
  164. * This function cannot be called from an interrupt.
  165. *
  166. * @param xEventGroup The event group in which the bits are being tested. The
  167. * event group must have previously been created using a call to
  168. * xEventGroupCreate().
  169. *
  170. * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
  171. * inside the event group. For example, to wait for bit 0 and/or bit 2 set
  172. * uxBitsToWaitFor to 0x05. To wait for bits 0 and/or bit 1 and/or bit 2 set
  173. * uxBitsToWaitFor to 0x07. Etc.
  174. *
  175. * @param xClearOnExit If xClearOnExit is set to pdTRUE then any bits within
  176. * uxBitsToWaitFor that are set within the event group will be cleared before
  177. * xEventGroupWaitBits() returns if the wait condition was met (if the function
  178. * returns for a reason other than a timeout). If xClearOnExit is set to
  179. * pdFALSE then the bits set in the event group are not altered when the call to
  180. * xEventGroupWaitBits() returns.
  181. *
  182. * @param xWaitForAllBits If xWaitForAllBits is set to pdTRUE then
  183. * xEventGroupWaitBits() will return when either all the bits in uxBitsToWaitFor
  184. * are set or the specified block time expires. If xWaitForAllBits is set to
  185. * pdFALSE then xEventGroupWaitBits() will return when any one of the bits set
  186. * in uxBitsToWaitFor is set or the specified block time expires. The block
  187. * time is specified by the xTicksToWait parameter.
  188. *
  189. * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
  190. * for one/all (depending on the xWaitForAllBits value) of the bits specified by
  191. * uxBitsToWaitFor to become set.
  192. *
  193. * @return The value of the event group at the time either the bits being waited
  194. * for became set, or the block time expired. Test the return value to know
  195. * which bits were set. If xEventGroupWaitBits() returned because its timeout
  196. * expired then not all the bits being waited for will be set. If
  197. * xEventGroupWaitBits() returned because the bits it was waiting for were set
  198. * then the returned value is the event group value before any bits were
  199. * automatically cleared in the case that xClearOnExit parameter was set to
  200. * pdTRUE.
  201. *
  202. * Example usage:
  203. <pre>
  204. #define BIT_0 ( 1 << 0 )
  205. #define BIT_4 ( 1 << 4 )
  206. void aFunction( EventGroupHandle_t xEventGroup )
  207. {
  208. EventBits_t uxBits;
  209. const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
  210. // Wait a maximum of 100ms for either bit 0 or bit 4 to be set within
  211. // the event group. Clear the bits before exiting.
  212. uxBits = xEventGroupWaitBits(
  213. xEventGroup, // The event group being tested.
  214. BIT_0 | BIT_4, // The bits within the event group to wait for.
  215. pdTRUE, // BIT_0 and BIT_4 should be cleared before returning.
  216. pdFALSE, // Don't wait for both bits, either bit will do.
  217. xTicksToWait ); // Wait a maximum of 100ms for either bit to be set.
  218. if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  219. {
  220. // xEventGroupWaitBits() returned because both bits were set.
  221. }
  222. else if( ( uxBits & BIT_0 ) != 0 )
  223. {
  224. // xEventGroupWaitBits() returned because just BIT_0 was set.
  225. }
  226. else if( ( uxBits & BIT_4 ) != 0 )
  227. {
  228. // xEventGroupWaitBits() returned because just BIT_4 was set.
  229. }
  230. else
  231. {
  232. // xEventGroupWaitBits() returned because xTicksToWait ticks passed
  233. // without either BIT_0 or BIT_4 becoming set.
  234. }
  235. }
  236. </pre>
  237. * \defgroup xEventGroupWaitBits xEventGroupWaitBits
  238. * \ingroup EventGroup
  239. */
  240. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  241. /**
  242. * event_groups.h
  243. *<pre>
  244. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear );
  245. </pre>
  246. *
  247. * Clear bits within an event group. This function cannot be called from an
  248. * interrupt.
  249. *
  250. * @param xEventGroup The event group in which the bits are to be cleared.
  251. *
  252. * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear
  253. * in the event group. For example, to clear bit 3 only, set uxBitsToClear to
  254. * 0x08. To clear bit 3 and bit 0 set uxBitsToClear to 0x09.
  255. *
  256. * @return The value of the event group before the specified bits were cleared.
  257. *
  258. * Example usage:
  259. <pre>
  260. #define BIT_0 ( 1 << 0 )
  261. #define BIT_4 ( 1 << 4 )
  262. void aFunction( EventGroupHandle_t xEventGroup )
  263. {
  264. EventBits_t uxBits;
  265. // Clear bit 0 and bit 4 in xEventGroup.
  266. uxBits = xEventGroupClearBits(
  267. xEventGroup, // The event group being updated.
  268. BIT_0 | BIT_4 );// The bits being cleared.
  269. if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  270. {
  271. // Both bit 0 and bit 4 were set before xEventGroupClearBits() was
  272. // called. Both will now be clear (not set).
  273. }
  274. else if( ( uxBits & BIT_0 ) != 0 )
  275. {
  276. // Bit 0 was set before xEventGroupClearBits() was called. It will
  277. // now be clear.
  278. }
  279. else if( ( uxBits & BIT_4 ) != 0 )
  280. {
  281. // Bit 4 was set before xEventGroupClearBits() was called. It will
  282. // now be clear.
  283. }
  284. else
  285. {
  286. // Neither bit 0 nor bit 4 were set in the first place.
  287. }
  288. }
  289. </pre>
  290. * \defgroup xEventGroupClearBits xEventGroupClearBits
  291. * \ingroup EventGroup
  292. */
  293. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear ) PRIVILEGED_FUNCTION;
  294. /**
  295. * event_groups.h
  296. *<pre>
  297. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
  298. </pre>
  299. *
  300. * A version of xEventGroupClearBits() that can be called from an interrupt.
  301. *
  302. * Setting bits in an event group is not a deterministic operation because there
  303. * are an unknown number of tasks that may be waiting for the bit or bits being
  304. * set. FreeRTOS does not allow nondeterministic operations to be performed
  305. * while interrupts are disabled, so protects event groups that are accessed
  306. * from tasks by suspending the scheduler rather than disabling interrupts. As
  307. * a result event groups cannot be accessed directly from an interrupt service
  308. * routine. Therefore xEventGroupClearBitsFromISR() sends a message to the
  309. * timer task to have the clear operation performed in the context of the timer
  310. * task.
  311. *
  312. * @param xEventGroup The event group in which the bits are to be cleared.
  313. *
  314. * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear.
  315. * For example, to clear bit 3 only, set uxBitsToClear to 0x08. To clear bit 3
  316. * and bit 0 set uxBitsToClear to 0x09.
  317. *
  318. * @return If the request to execute the function was posted successfully then
  319. * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
  320. * if the timer service queue was full.
  321. *
  322. * Example usage:
  323. <pre>
  324. #define BIT_0 ( 1 << 0 )
  325. #define BIT_4 ( 1 << 4 )
  326. // An event group which it is assumed has already been created by a call to
  327. // xEventGroupCreate().
  328. EventGroupHandle_t xEventGroup;
  329. void anInterruptHandler( void )
  330. {
  331. // Clear bit 0 and bit 4 in xEventGroup.
  332. xResult = xEventGroupClearBitsFromISR(
  333. xEventGroup, // The event group being updated.
  334. BIT_0 | BIT_4 ); // The bits being set.
  335. if( xResult == pdPASS )
  336. {
  337. // The message was posted successfully.
  338. }
  339. }
  340. </pre>
  341. * \defgroup xEventGroupSetBitsFromISR xEventGroupSetBitsFromISR
  342. * \ingroup EventGroup
  343. */
  344. #if( configUSE_TRACE_FACILITY == 1 )
  345. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
  346. #else
  347. #define xEventGroupClearBitsFromISR( xEventGroup, uxBitsToClear ) xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL )
  348. #endif
  349. /**
  350. * event_groups.h
  351. *<pre>
  352. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
  353. </pre>
  354. *
  355. * Set bits within an event group.
  356. * This function cannot be called from an interrupt. xEventGroupSetBitsFromISR()
  357. * is a version that can be called from an interrupt.
  358. *
  359. * Setting bits in an event group will automatically unblock tasks that are
  360. * blocked waiting for the bits.
  361. *
  362. * @param xEventGroup The event group in which the bits are to be set.
  363. *
  364. * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
  365. * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
  366. * and bit 0 set uxBitsToSet to 0x09.
  367. *
  368. * @return The value of the event group at the time the call to
  369. * xEventGroupSetBits() returns. There are two reasons why the returned value
  370. * might have the bits specified by the uxBitsToSet parameter cleared. First,
  371. * if setting a bit results in a task that was waiting for the bit leaving the
  372. * blocked state then it is possible the bit will be cleared automatically
  373. * (see the xClearBitOnExit parameter of xEventGroupWaitBits()). Second, any
  374. * unblocked (or otherwise Ready state) task that has a priority above that of
  375. * the task that called xEventGroupSetBits() will execute and may change the
  376. * event group value before the call to xEventGroupSetBits() returns.
  377. *
  378. * Example usage:
  379. <pre>
  380. #define BIT_0 ( 1 << 0 )
  381. #define BIT_4 ( 1 << 4 )
  382. void aFunction( EventGroupHandle_t xEventGroup )
  383. {
  384. EventBits_t uxBits;
  385. // Set bit 0 and bit 4 in xEventGroup.
  386. uxBits = xEventGroupSetBits(
  387. xEventGroup, // The event group being updated.
  388. BIT_0 | BIT_4 );// The bits being set.
  389. if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  390. {
  391. // Both bit 0 and bit 4 remained set when the function returned.
  392. }
  393. else if( ( uxBits & BIT_0 ) != 0 )
  394. {
  395. // Bit 0 remained set when the function returned, but bit 4 was
  396. // cleared. It might be that bit 4 was cleared automatically as a
  397. // task that was waiting for bit 4 was removed from the Blocked
  398. // state.
  399. }
  400. else if( ( uxBits & BIT_4 ) != 0 )
  401. {
  402. // Bit 4 remained set when the function returned, but bit 0 was
  403. // cleared. It might be that bit 0 was cleared automatically as a
  404. // task that was waiting for bit 0 was removed from the Blocked
  405. // state.
  406. }
  407. else
  408. {
  409. // Neither bit 0 nor bit 4 remained set. It might be that a task
  410. // was waiting for both of the bits to be set, and the bits were
  411. // cleared as the task left the Blocked state.
  412. }
  413. }
  414. </pre>
  415. * \defgroup xEventGroupSetBits xEventGroupSetBits
  416. * \ingroup EventGroup
  417. */
  418. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
  419. /**
  420. * event_groups.h
  421. *<pre>
  422. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken );
  423. </pre>
  424. *
  425. * A version of xEventGroupSetBits() that can be called from an interrupt.
  426. *
  427. * Setting bits in an event group is not a deterministic operation because there
  428. * are an unknown number of tasks that may be waiting for the bit or bits being
  429. * set. FreeRTOS does not allow nondeterministic operations to be performed in
  430. * interrupts or from critical sections. Therefore xEventGroupSetBitFromISR()
  431. * sends a message to the timer task to have the set operation performed in the
  432. * context of the timer task - where a scheduler lock is used in place of a
  433. * critical section.
  434. *
  435. * @param xEventGroup The event group in which the bits are to be set.
  436. *
  437. * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
  438. * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
  439. * and bit 0 set uxBitsToSet to 0x09.
  440. *
  441. * @param pxHigherPriorityTaskWoken As mentioned above, calling this function
  442. * will result in a message being sent to the timer daemon task. If the
  443. * priority of the timer daemon task is higher than the priority of the
  444. * currently running task (the task the interrupt interrupted) then
  445. * *pxHigherPriorityTaskWoken will be set to pdTRUE by
  446. * xEventGroupSetBitsFromISR(), indicating that a context switch should be
  447. * requested before the interrupt exits. For that reason
  448. * *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the
  449. * example code below.
  450. *
  451. * @return If the request to execute the function was posted successfully then
  452. * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
  453. * if the timer service queue was full.
  454. *
  455. * Example usage:
  456. <pre>
  457. #define BIT_0 ( 1 << 0 )
  458. #define BIT_4 ( 1 << 4 )
  459. // An event group which it is assumed has already been created by a call to
  460. // xEventGroupCreate().
  461. EventGroupHandle_t xEventGroup;
  462. void anInterruptHandler( void )
  463. {
  464. BaseType_t xHigherPriorityTaskWoken, xResult;
  465. // xHigherPriorityTaskWoken must be initialised to pdFALSE.
  466. xHigherPriorityTaskWoken = pdFALSE;
  467. // Set bit 0 and bit 4 in xEventGroup.
  468. xResult = xEventGroupSetBitsFromISR(
  469. xEventGroup, // The event group being updated.
  470. BIT_0 | BIT_4 // The bits being set.
  471. &xHigherPriorityTaskWoken );
  472. // Was the message posted successfully?
  473. if( xResult == pdPASS )
  474. {
  475. // If xHigherPriorityTaskWoken is now set to pdTRUE then a context
  476. // switch should be requested. The macro used is port specific and
  477. // will be either portYIELD_FROM_ISR() or portEND_SWITCHING_ISR() -
  478. // refer to the documentation page for the port being used.
  479. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  480. }
  481. }
  482. </pre>
  483. * \defgroup xEventGroupSetBitsFromISR xEventGroupSetBitsFromISR
  484. * \ingroup EventGroup
  485. */
  486. #if( configUSE_TRACE_FACILITY == 1 )
  487. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
  488. #else
  489. #define xEventGroupSetBitsFromISR( xEventGroup, uxBitsToSet, pxHigherPriorityTaskWoken ) xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken )
  490. #endif
  491. /**
  492. * event_groups.h
  493. *<pre>
  494. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
  495. const EventBits_t uxBitsToSet,
  496. const EventBits_t uxBitsToWaitFor,
  497. TickType_t xTicksToWait );
  498. </pre>
  499. *
  500. * Atomically set bits within an event group, then wait for a combination of
  501. * bits to be set within the same event group. This functionality is typically
  502. * used to synchronise multiple tasks, where each task has to wait for the other
  503. * tasks to reach a synchronisation point before proceeding.
  504. *
  505. * This function cannot be used from an interrupt.
  506. *
  507. * The function will return before its block time expires if the bits specified
  508. * by the uxBitsToWait parameter are set, or become set within that time. In
  509. * this case all the bits specified by uxBitsToWait will be automatically
  510. * cleared before the function returns.
  511. *
  512. * @param xEventGroup The event group in which the bits are being tested. The
  513. * event group must have previously been created using a call to
  514. * xEventGroupCreate().
  515. *
  516. * @param uxBitsToSet The bits to set in the event group before determining
  517. * if, and possibly waiting for, all the bits specified by the uxBitsToWait
  518. * parameter are set.
  519. *
  520. * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
  521. * inside the event group. For example, to wait for bit 0 and bit 2 set
  522. * uxBitsToWaitFor to 0x05. To wait for bits 0 and bit 1 and bit 2 set
  523. * uxBitsToWaitFor to 0x07. Etc.
  524. *
  525. * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
  526. * for all of the bits specified by uxBitsToWaitFor to become set.
  527. *
  528. * @return The value of the event group at the time either the bits being waited
  529. * for became set, or the block time expired. Test the return value to know
  530. * which bits were set. If xEventGroupSync() returned because its timeout
  531. * expired then not all the bits being waited for will be set. If
  532. * xEventGroupSync() returned because all the bits it was waiting for were
  533. * set then the returned value is the event group value before any bits were
  534. * automatically cleared.
  535. *
  536. * Example usage:
  537. <pre>
  538. // Bits used by the three tasks.
  539. #define TASK_0_BIT ( 1 << 0 )
  540. #define TASK_1_BIT ( 1 << 1 )
  541. #define TASK_2_BIT ( 1 << 2 )
  542. #define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )
  543. // Use an event group to synchronise three tasks. It is assumed this event
  544. // group has already been created elsewhere.
  545. EventGroupHandle_t xEventBits;
  546. void vTask0( void *pvParameters )
  547. {
  548. EventBits_t uxReturn;
  549. TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
  550. for( ;; )
  551. {
  552. // Perform task functionality here.
  553. // Set bit 0 in the event flag to note this task has reached the
  554. // sync point. The other two tasks will set the other two bits defined
  555. // by ALL_SYNC_BITS. All three tasks have reached the synchronisation
  556. // point when all the ALL_SYNC_BITS are set. Wait a maximum of 100ms
  557. // for this to happen.
  558. uxReturn = xEventGroupSync( xEventBits, TASK_0_BIT, ALL_SYNC_BITS, xTicksToWait );
  559. if( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
  560. {
  561. // All three tasks reached the synchronisation point before the call
  562. // to xEventGroupSync() timed out.
  563. }
  564. }
  565. }
  566. void vTask1( void *pvParameters )
  567. {
  568. for( ;; )
  569. {
  570. // Perform task functionality here.
  571. // Set bit 1 in the event flag to note this task has reached the
  572. // synchronisation point. The other two tasks will set the other two
  573. // bits defined by ALL_SYNC_BITS. All three tasks have reached the
  574. // synchronisation point when all the ALL_SYNC_BITS are set. Wait
  575. // indefinitely for this to happen.
  576. xEventGroupSync( xEventBits, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  577. // xEventGroupSync() was called with an indefinite block time, so
  578. // this task will only reach here if the syncrhonisation was made by all
  579. // three tasks, so there is no need to test the return value.
  580. }
  581. }
  582. void vTask2( void *pvParameters )
  583. {
  584. for( ;; )
  585. {
  586. // Perform task functionality here.
  587. // Set bit 2 in the event flag to note this task has reached the
  588. // synchronisation point. The other two tasks will set the other two
  589. // bits defined by ALL_SYNC_BITS. All three tasks have reached the
  590. // synchronisation point when all the ALL_SYNC_BITS are set. Wait
  591. // indefinitely for this to happen.
  592. xEventGroupSync( xEventBits, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  593. // xEventGroupSync() was called with an indefinite block time, so
  594. // this task will only reach here if the syncrhonisation was made by all
  595. // three tasks, so there is no need to test the return value.
  596. }
  597. }
  598. </pre>
  599. * \defgroup xEventGroupSync xEventGroupSync
  600. * \ingroup EventGroup
  601. */
  602. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  603. /**
  604. * event_groups.h
  605. *<pre>
  606. EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup );
  607. </pre>
  608. *
  609. * Returns the current value of the bits in an event group. This function
  610. * cannot be used from an interrupt.
  611. *
  612. * @param xEventGroup The event group being queried.
  613. *
  614. * @return The event group bits at the time xEventGroupGetBits() was called.
  615. *
  616. * \defgroup xEventGroupGetBits xEventGroupGetBits
  617. * \ingroup EventGroup
  618. */
  619. #define xEventGroupGetBits( xEventGroup ) xEventGroupClearBits( xEventGroup, 0 )
  620. /**
  621. * event_groups.h
  622. *<pre>
  623. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup );
  624. </pre>
  625. *
  626. * A version of xEventGroupGetBits() that can be called from an ISR.
  627. *
  628. * @param xEventGroup The event group being queried.
  629. *
  630. * @return The event group bits at the time xEventGroupGetBitsFromISR() was called.
  631. *
  632. * \defgroup xEventGroupGetBitsFromISR xEventGroupGetBitsFromISR
  633. * \ingroup EventGroup
  634. */
  635. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
  636. /**
  637. * event_groups.h
  638. *<pre>
  639. void xEventGroupDelete( EventGroupHandle_t xEventGroup );
  640. </pre>
  641. *
  642. * Delete an event group that was previously created by a call to
  643. * xEventGroupCreate(). Tasks that are blocked on the event group will be
  644. * unblocked and obtain 0 as the event group's value.
  645. *
  646. * @param xEventGroup The event group being deleted.
  647. */
  648. void vEventGroupDelete( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
  649. /* For internal use only. */
  650. void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet ) PRIVILEGED_FUNCTION;
  651. void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear ) PRIVILEGED_FUNCTION;
  652. #if (configUSE_TRACE_FACILITY == 1)
  653. UBaseType_t uxEventGroupGetNumber( void* xEventGroup ) PRIVILEGED_FUNCTION;
  654. #endif
  655. #ifdef __cplusplus
  656. }
  657. #endif
  658. #endif /* EVENT_GROUPS_H */