event_groups.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. FreeRTOS V9.0.0 - Copyright (C) 2016 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. /* FreeRTOS includes. */
  61. #include "timers.h"
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /**
  66. * An event group is a collection of bits to which an application can assign a
  67. * meaning. For example, an application may create an event group to convey
  68. * the status of various CAN bus related events in which bit 0 might mean "A CAN
  69. * message has been received and is ready for processing", bit 1 might mean "The
  70. * application has queued a message that is ready for sending onto the CAN
  71. * network", and bit 2 might mean "It is time to send a SYNC message onto the
  72. * CAN network" etc. A task can then test the bit values to see which events
  73. * are active, and optionally enter the Blocked state to wait for a specified
  74. * bit or a group of specified bits to be active. To continue the CAN bus
  75. * example, a CAN controlling task can enter the Blocked state (and therefore
  76. * not consume any processing time) until either bit 0, bit 1 or bit 2 are
  77. * active, at which time the bit that was actually active would inform the task
  78. * which action it had to take (process a received message, send a message, or
  79. * send a SYNC).
  80. *
  81. * The event groups implementation contains intelligence to avoid race
  82. * conditions that would otherwise occur were an application to use a simple
  83. * variable for the same purpose. This is particularly important with respect
  84. * to when a bit within an event group is to be cleared, and when bits have to
  85. * be set and then tested atomically - as is the case where event groups are
  86. * used to create a synchronisation point between multiple tasks (a
  87. * 'rendezvous').
  88. *
  89. * \defgroup EventGroup
  90. */
  91. /**
  92. * event_groups.h
  93. *
  94. * Type by which event groups are referenced. For example, a call to
  95. * xEventGroupCreate() returns an EventGroupHandle_t variable that can then
  96. * be used as a parameter to other event group functions.
  97. *
  98. * \defgroup EventGroupHandle_t EventGroupHandle_t
  99. * \ingroup EventGroup
  100. */
  101. typedef void * EventGroupHandle_t;
  102. /*
  103. * The type that holds event bits always matches TickType_t - therefore the
  104. * number of bits it holds is set by configUSE_16_BIT_TICKS (16 bits if set to 1,
  105. * 32 bits if set to 0.
  106. *
  107. * \defgroup EventBits_t EventBits_t
  108. * \ingroup EventGroup
  109. */
  110. typedef TickType_t EventBits_t;
  111. /**
  112. * event_groups.h
  113. *<pre>
  114. EventGroupHandle_t xEventGroupCreate( void );
  115. </pre>
  116. *
  117. * Create a new event group.
  118. *
  119. * Internally, within the FreeRTOS implementation, event groups use a [small]
  120. * block of memory, in which the event group's structure is stored. If an event
  121. * groups is created using xEventGropuCreate() then the required memory is
  122. * automatically dynamically allocated inside the xEventGroupCreate() function.
  123. * (see http://www.freertos.org/a00111.html). If an event group is created
  124. * using xEventGropuCreateStatic() then the application writer must instead
  125. * provide the memory that will get used by the event group.
  126. * xEventGroupCreateStatic() therefore allows an event group to be created
  127. * without using any dynamic memory allocation.
  128. *
  129. * Although event groups are not related to ticks, for internal implementation
  130. * reasons the number of bits available for use in an event group is dependent
  131. * on the configUSE_16_BIT_TICKS setting in FreeRTOSConfig.h. If
  132. * configUSE_16_BIT_TICKS is 1 then each event group contains 8 usable bits (bit
  133. * 0 to bit 7). If configUSE_16_BIT_TICKS is set to 0 then each event group has
  134. * 24 usable bits (bit 0 to bit 23). The EventBits_t type is used to store
  135. * event bits within an event group.
  136. *
  137. * @return If the event group was created then a handle to the event group is
  138. * returned. If there was insufficient FreeRTOS heap available to create the
  139. * event group then NULL is returned. See http://www.freertos.org/a00111.html
  140. *
  141. * Example usage:
  142. <pre>
  143. // Declare a variable to hold the created event group.
  144. EventGroupHandle_t xCreatedEventGroup;
  145. // Attempt to create the event group.
  146. xCreatedEventGroup = xEventGroupCreate();
  147. // Was the event group created successfully?
  148. if( xCreatedEventGroup == NULL )
  149. {
  150. // The event group was not created because there was insufficient
  151. // FreeRTOS heap available.
  152. }
  153. else
  154. {
  155. // The event group was created.
  156. }
  157. </pre>
  158. * \defgroup xEventGroupCreate xEventGroupCreate
  159. * \ingroup EventGroup
  160. */
  161. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  162. EventGroupHandle_t xEventGroupCreate( void ) PRIVILEGED_FUNCTION;
  163. #endif
  164. /**
  165. * event_groups.h
  166. *<pre>
  167. EventGroupHandle_t xEventGroupCreateStatic( EventGroupHandle_t * pxEventGroupBuffer );
  168. </pre>
  169. *
  170. * Create a new event group.
  171. *
  172. * Internally, within the FreeRTOS implementation, event groups use a [small]
  173. * block of memory, in which the event group's structure is stored. If an event
  174. * groups is created using xEventGropuCreate() then the required memory is
  175. * automatically dynamically allocated inside the xEventGroupCreate() function.
  176. * (see http://www.freertos.org/a00111.html). If an event group is created
  177. * using xEventGropuCreateStatic() then the application writer must instead
  178. * provide the memory that will get used by the event group.
  179. * xEventGroupCreateStatic() therefore allows an event group to be created
  180. * without using any dynamic memory allocation.
  181. *
  182. * Although event groups are not related to ticks, for internal implementation
  183. * reasons the number of bits available for use in an event group is dependent
  184. * on the configUSE_16_BIT_TICKS setting in FreeRTOSConfig.h. If
  185. * configUSE_16_BIT_TICKS is 1 then each event group contains 8 usable bits (bit
  186. * 0 to bit 7). If configUSE_16_BIT_TICKS is set to 0 then each event group has
  187. * 24 usable bits (bit 0 to bit 23). The EventBits_t type is used to store
  188. * event bits within an event group.
  189. *
  190. * @param pxEventGroupBuffer pxEventGroupBuffer must point to a variable of type
  191. * StaticEventGroup_t, which will be then be used to hold the event group's data
  192. * structures, removing the need for the memory to be allocated dynamically.
  193. *
  194. * @return If the event group was created then a handle to the event group is
  195. * returned. If pxEventGroupBuffer was NULL then NULL is returned.
  196. *
  197. * Example usage:
  198. <pre>
  199. // StaticEventGroup_t is a publicly accessible structure that has the same
  200. // size and alignment requirements as the real event group structure. It is
  201. // provided as a mechanism for applications to know the size of the event
  202. // group (which is dependent on the architecture and configuration file
  203. // settings) without breaking the strict data hiding policy by exposing the
  204. // real event group internals. This StaticEventGroup_t variable is passed
  205. // into the xSemaphoreCreateEventGroupStatic() function and is used to store
  206. // the event group's data structures
  207. StaticEventGroup_t xEventGroupBuffer;
  208. // Create the event group without dynamically allocating any memory.
  209. xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );
  210. </pre>
  211. */
  212. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  213. EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t *pxEventGroupBuffer ) PRIVILEGED_FUNCTION;
  214. #endif
  215. /**
  216. * event_groups.h
  217. *<pre>
  218. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  219. const EventBits_t uxBitsToWaitFor,
  220. const BaseType_t xClearOnExit,
  221. const BaseType_t xWaitForAllBits,
  222. const TickType_t xTicksToWait );
  223. </pre>
  224. *
  225. * [Potentially] block to wait for one or more bits to be set within a
  226. * previously created event group.
  227. *
  228. * This function cannot be called from an interrupt.
  229. *
  230. * @param xEventGroup The event group in which the bits are being tested. The
  231. * event group must have previously been created using a call to
  232. * xEventGroupCreate().
  233. *
  234. * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
  235. * inside the event group. For example, to wait for bit 0 and/or bit 2 set
  236. * uxBitsToWaitFor to 0x05. To wait for bits 0 and/or bit 1 and/or bit 2 set
  237. * uxBitsToWaitFor to 0x07. Etc.
  238. *
  239. * @param xClearOnExit If xClearOnExit is set to pdTRUE then any bits within
  240. * uxBitsToWaitFor that are set within the event group will be cleared before
  241. * xEventGroupWaitBits() returns if the wait condition was met (if the function
  242. * returns for a reason other than a timeout). If xClearOnExit is set to
  243. * pdFALSE then the bits set in the event group are not altered when the call to
  244. * xEventGroupWaitBits() returns.
  245. *
  246. * @param xWaitForAllBits If xWaitForAllBits is set to pdTRUE then
  247. * xEventGroupWaitBits() will return when either all the bits in uxBitsToWaitFor
  248. * are set or the specified block time expires. If xWaitForAllBits is set to
  249. * pdFALSE then xEventGroupWaitBits() will return when any one of the bits set
  250. * in uxBitsToWaitFor is set or the specified block time expires. The block
  251. * time is specified by the xTicksToWait parameter.
  252. *
  253. * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
  254. * for one/all (depending on the xWaitForAllBits value) of the bits specified by
  255. * uxBitsToWaitFor to become set.
  256. *
  257. * @return The value of the event group at the time either the bits being waited
  258. * for became set, or the block time expired. Test the return value to know
  259. * which bits were set. If xEventGroupWaitBits() returned because its timeout
  260. * expired then not all the bits being waited for will be set. If
  261. * xEventGroupWaitBits() returned because the bits it was waiting for were set
  262. * then the returned value is the event group value before any bits were
  263. * automatically cleared in the case that xClearOnExit parameter was set to
  264. * pdTRUE.
  265. *
  266. * Example usage:
  267. <pre>
  268. #define BIT_0 ( 1 << 0 )
  269. #define BIT_4 ( 1 << 4 )
  270. void aFunction( EventGroupHandle_t xEventGroup )
  271. {
  272. EventBits_t uxBits;
  273. const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
  274. // Wait a maximum of 100ms for either bit 0 or bit 4 to be set within
  275. // the event group. Clear the bits before exiting.
  276. uxBits = xEventGroupWaitBits(
  277. xEventGroup, // The event group being tested.
  278. BIT_0 | BIT_4, // The bits within the event group to wait for.
  279. pdTRUE, // BIT_0 and BIT_4 should be cleared before returning.
  280. pdFALSE, // Don't wait for both bits, either bit will do.
  281. xTicksToWait ); // Wait a maximum of 100ms for either bit to be set.
  282. if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  283. {
  284. // xEventGroupWaitBits() returned because both bits were set.
  285. }
  286. else if( ( uxBits & BIT_0 ) != 0 )
  287. {
  288. // xEventGroupWaitBits() returned because just BIT_0 was set.
  289. }
  290. else if( ( uxBits & BIT_4 ) != 0 )
  291. {
  292. // xEventGroupWaitBits() returned because just BIT_4 was set.
  293. }
  294. else
  295. {
  296. // xEventGroupWaitBits() returned because xTicksToWait ticks passed
  297. // without either BIT_0 or BIT_4 becoming set.
  298. }
  299. }
  300. </pre>
  301. * \defgroup xEventGroupWaitBits xEventGroupWaitBits
  302. * \ingroup EventGroup
  303. */
  304. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  305. /**
  306. * event_groups.h
  307. *<pre>
  308. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear );
  309. </pre>
  310. *
  311. * Clear bits within an event group. This function cannot be called from an
  312. * interrupt.
  313. *
  314. * @param xEventGroup The event group in which the bits are to be cleared.
  315. *
  316. * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear
  317. * in the event group. For example, to clear bit 3 only, set uxBitsToClear to
  318. * 0x08. To clear bit 3 and bit 0 set uxBitsToClear to 0x09.
  319. *
  320. * @return The value of the event group before the specified bits were cleared.
  321. *
  322. * Example usage:
  323. <pre>
  324. #define BIT_0 ( 1 << 0 )
  325. #define BIT_4 ( 1 << 4 )
  326. void aFunction( EventGroupHandle_t xEventGroup )
  327. {
  328. EventBits_t uxBits;
  329. // Clear bit 0 and bit 4 in xEventGroup.
  330. uxBits = xEventGroupClearBits(
  331. xEventGroup, // The event group being updated.
  332. BIT_0 | BIT_4 );// The bits being cleared.
  333. if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  334. {
  335. // Both bit 0 and bit 4 were set before xEventGroupClearBits() was
  336. // called. Both will now be clear (not set).
  337. }
  338. else if( ( uxBits & BIT_0 ) != 0 )
  339. {
  340. // Bit 0 was set before xEventGroupClearBits() was called. It will
  341. // now be clear.
  342. }
  343. else if( ( uxBits & BIT_4 ) != 0 )
  344. {
  345. // Bit 4 was set before xEventGroupClearBits() was called. It will
  346. // now be clear.
  347. }
  348. else
  349. {
  350. // Neither bit 0 nor bit 4 were set in the first place.
  351. }
  352. }
  353. </pre>
  354. * \defgroup xEventGroupClearBits xEventGroupClearBits
  355. * \ingroup EventGroup
  356. */
  357. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear ) PRIVILEGED_FUNCTION;
  358. /**
  359. * event_groups.h
  360. *<pre>
  361. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
  362. </pre>
  363. *
  364. * A version of xEventGroupClearBits() that can be called from an interrupt.
  365. *
  366. * Setting bits in an event group is not a deterministic operation because there
  367. * are an unknown number of tasks that may be waiting for the bit or bits being
  368. * set. FreeRTOS does not allow nondeterministic operations to be performed
  369. * while interrupts are disabled, so protects event groups that are accessed
  370. * from tasks by suspending the scheduler rather than disabling interrupts. As
  371. * a result event groups cannot be accessed directly from an interrupt service
  372. * routine. Therefore xEventGroupClearBitsFromISR() sends a message to the
  373. * timer task to have the clear operation performed in the context of the timer
  374. * task.
  375. *
  376. * @param xEventGroup The event group in which the bits are to be cleared.
  377. *
  378. * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear.
  379. * For example, to clear bit 3 only, set uxBitsToClear to 0x08. To clear bit 3
  380. * and bit 0 set uxBitsToClear to 0x09.
  381. *
  382. * @return If the request to execute the function was posted successfully then
  383. * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
  384. * if the timer service queue was full.
  385. *
  386. * Example usage:
  387. <pre>
  388. #define BIT_0 ( 1 << 0 )
  389. #define BIT_4 ( 1 << 4 )
  390. // An event group which it is assumed has already been created by a call to
  391. // xEventGroupCreate().
  392. EventGroupHandle_t xEventGroup;
  393. void anInterruptHandler( void )
  394. {
  395. // Clear bit 0 and bit 4 in xEventGroup.
  396. xResult = xEventGroupClearBitsFromISR(
  397. xEventGroup, // The event group being updated.
  398. BIT_0 | BIT_4 ); // The bits being set.
  399. if( xResult == pdPASS )
  400. {
  401. // The message was posted successfully.
  402. }
  403. }
  404. </pre>
  405. * \defgroup xEventGroupClearBitsFromISR xEventGroupClearBitsFromISR
  406. * \ingroup EventGroup
  407. */
  408. #if( configUSE_TRACE_FACILITY == 1 )
  409. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
  410. #else
  411. #define xEventGroupClearBitsFromISR( xEventGroup, uxBitsToClear ) xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL )
  412. #endif
  413. /**
  414. * event_groups.h
  415. *<pre>
  416. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
  417. </pre>
  418. *
  419. * Set bits within an event group.
  420. * This function cannot be called from an interrupt. xEventGroupSetBitsFromISR()
  421. * is a version that can be called from an interrupt.
  422. *
  423. * Setting bits in an event group will automatically unblock tasks that are
  424. * blocked waiting for the bits.
  425. *
  426. * @param xEventGroup The event group in which the bits are to be set.
  427. *
  428. * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
  429. * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
  430. * and bit 0 set uxBitsToSet to 0x09.
  431. *
  432. * @return The value of the event group at the time the call to
  433. * xEventGroupSetBits() returns. There are two reasons why the returned value
  434. * might have the bits specified by the uxBitsToSet parameter cleared. First,
  435. * if setting a bit results in a task that was waiting for the bit leaving the
  436. * blocked state then it is possible the bit will be cleared automatically
  437. * (see the xClearBitOnExit parameter of xEventGroupWaitBits()). Second, any
  438. * unblocked (or otherwise Ready state) task that has a priority above that of
  439. * the task that called xEventGroupSetBits() will execute and may change the
  440. * event group value before the call to xEventGroupSetBits() returns.
  441. *
  442. * Example usage:
  443. <pre>
  444. #define BIT_0 ( 1 << 0 )
  445. #define BIT_4 ( 1 << 4 )
  446. void aFunction( EventGroupHandle_t xEventGroup )
  447. {
  448. EventBits_t uxBits;
  449. // Set bit 0 and bit 4 in xEventGroup.
  450. uxBits = xEventGroupSetBits(
  451. xEventGroup, // The event group being updated.
  452. BIT_0 | BIT_4 );// The bits being set.
  453. if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  454. {
  455. // Both bit 0 and bit 4 remained set when the function returned.
  456. }
  457. else if( ( uxBits & BIT_0 ) != 0 )
  458. {
  459. // Bit 0 remained set when the function returned, but bit 4 was
  460. // cleared. It might be that bit 4 was cleared automatically as a
  461. // task that was waiting for bit 4 was removed from the Blocked
  462. // state.
  463. }
  464. else if( ( uxBits & BIT_4 ) != 0 )
  465. {
  466. // Bit 4 remained set when the function returned, but bit 0 was
  467. // cleared. It might be that bit 0 was cleared automatically as a
  468. // task that was waiting for bit 0 was removed from the Blocked
  469. // state.
  470. }
  471. else
  472. {
  473. // Neither bit 0 nor bit 4 remained set. It might be that a task
  474. // was waiting for both of the bits to be set, and the bits were
  475. // cleared as the task left the Blocked state.
  476. }
  477. }
  478. </pre>
  479. * \defgroup xEventGroupSetBits xEventGroupSetBits
  480. * \ingroup EventGroup
  481. */
  482. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
  483. /**
  484. * event_groups.h
  485. *<pre>
  486. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken );
  487. </pre>
  488. *
  489. * A version of xEventGroupSetBits() that can be called from an interrupt.
  490. *
  491. * Setting bits in an event group is not a deterministic operation because there
  492. * are an unknown number of tasks that may be waiting for the bit or bits being
  493. * set. FreeRTOS does not allow nondeterministic operations to be performed in
  494. * interrupts or from critical sections. Therefore xEventGroupSetBitsFromISR()
  495. * sends a message to the timer task to have the set operation performed in the
  496. * context of the timer task - where a scheduler lock is used in place of a
  497. * critical section.
  498. *
  499. * @param xEventGroup The event group in which the bits are to be set.
  500. *
  501. * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
  502. * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
  503. * and bit 0 set uxBitsToSet to 0x09.
  504. *
  505. * @param pxHigherPriorityTaskWoken As mentioned above, calling this function
  506. * will result in a message being sent to the timer daemon task. If the
  507. * priority of the timer daemon task is higher than the priority of the
  508. * currently running task (the task the interrupt interrupted) then
  509. * *pxHigherPriorityTaskWoken will be set to pdTRUE by
  510. * xEventGroupSetBitsFromISR(), indicating that a context switch should be
  511. * requested before the interrupt exits. For that reason
  512. * *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the
  513. * example code below.
  514. *
  515. * @return If the request to execute the function was posted successfully then
  516. * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
  517. * if the timer service queue was full.
  518. *
  519. * Example usage:
  520. <pre>
  521. #define BIT_0 ( 1 << 0 )
  522. #define BIT_4 ( 1 << 4 )
  523. // An event group which it is assumed has already been created by a call to
  524. // xEventGroupCreate().
  525. EventGroupHandle_t xEventGroup;
  526. void anInterruptHandler( void )
  527. {
  528. BaseType_t xHigherPriorityTaskWoken, xResult;
  529. // xHigherPriorityTaskWoken must be initialised to pdFALSE.
  530. xHigherPriorityTaskWoken = pdFALSE;
  531. // Set bit 0 and bit 4 in xEventGroup.
  532. xResult = xEventGroupSetBitsFromISR(
  533. xEventGroup, // The event group being updated.
  534. BIT_0 | BIT_4 // The bits being set.
  535. &xHigherPriorityTaskWoken );
  536. // Was the message posted successfully?
  537. if( xResult == pdPASS )
  538. {
  539. // If xHigherPriorityTaskWoken is now set to pdTRUE then a context
  540. // switch should be requested. The macro used is port specific and
  541. // will be either portYIELD_FROM_ISR() or portEND_SWITCHING_ISR() -
  542. // refer to the documentation page for the port being used.
  543. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  544. }
  545. }
  546. </pre>
  547. * \defgroup xEventGroupSetBitsFromISR xEventGroupSetBitsFromISR
  548. * \ingroup EventGroup
  549. */
  550. #if( configUSE_TRACE_FACILITY == 1 )
  551. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
  552. #else
  553. #define xEventGroupSetBitsFromISR( xEventGroup, uxBitsToSet, pxHigherPriorityTaskWoken ) xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken )
  554. #endif
  555. /**
  556. * event_groups.h
  557. *<pre>
  558. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
  559. const EventBits_t uxBitsToSet,
  560. const EventBits_t uxBitsToWaitFor,
  561. TickType_t xTicksToWait );
  562. </pre>
  563. *
  564. * Atomically set bits within an event group, then wait for a combination of
  565. * bits to be set within the same event group. This functionality is typically
  566. * used to synchronise multiple tasks, where each task has to wait for the other
  567. * tasks to reach a synchronisation point before proceeding.
  568. *
  569. * This function cannot be used from an interrupt.
  570. *
  571. * The function will return before its block time expires if the bits specified
  572. * by the uxBitsToWait parameter are set, or become set within that time. In
  573. * this case all the bits specified by uxBitsToWait will be automatically
  574. * cleared before the function returns.
  575. *
  576. * @param xEventGroup The event group in which the bits are being tested. The
  577. * event group must have previously been created using a call to
  578. * xEventGroupCreate().
  579. *
  580. * @param uxBitsToSet The bits to set in the event group before determining
  581. * if, and possibly waiting for, all the bits specified by the uxBitsToWait
  582. * parameter are set.
  583. *
  584. * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
  585. * inside the event group. For example, to wait for bit 0 and bit 2 set
  586. * uxBitsToWaitFor to 0x05. To wait for bits 0 and bit 1 and bit 2 set
  587. * uxBitsToWaitFor to 0x07. Etc.
  588. *
  589. * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
  590. * for all of the bits specified by uxBitsToWaitFor to become set.
  591. *
  592. * @return The value of the event group at the time either the bits being waited
  593. * for became set, or the block time expired. Test the return value to know
  594. * which bits were set. If xEventGroupSync() returned because its timeout
  595. * expired then not all the bits being waited for will be set. If
  596. * xEventGroupSync() returned because all the bits it was waiting for were
  597. * set then the returned value is the event group value before any bits were
  598. * automatically cleared.
  599. *
  600. * Example usage:
  601. <pre>
  602. // Bits used by the three tasks.
  603. #define TASK_0_BIT ( 1 << 0 )
  604. #define TASK_1_BIT ( 1 << 1 )
  605. #define TASK_2_BIT ( 1 << 2 )
  606. #define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )
  607. // Use an event group to synchronise three tasks. It is assumed this event
  608. // group has already been created elsewhere.
  609. EventGroupHandle_t xEventBits;
  610. void vTask0( void *pvParameters )
  611. {
  612. EventBits_t uxReturn;
  613. TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
  614. for( ;; )
  615. {
  616. // Perform task functionality here.
  617. // Set bit 0 in the event flag to note this task has reached the
  618. // sync point. The other two tasks will set the other two bits defined
  619. // by ALL_SYNC_BITS. All three tasks have reached the synchronisation
  620. // point when all the ALL_SYNC_BITS are set. Wait a maximum of 100ms
  621. // for this to happen.
  622. uxReturn = xEventGroupSync( xEventBits, TASK_0_BIT, ALL_SYNC_BITS, xTicksToWait );
  623. if( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
  624. {
  625. // All three tasks reached the synchronisation point before the call
  626. // to xEventGroupSync() timed out.
  627. }
  628. }
  629. }
  630. void vTask1( void *pvParameters )
  631. {
  632. for( ;; )
  633. {
  634. // Perform task functionality here.
  635. // Set bit 1 in the event flag to note this task has reached the
  636. // synchronisation point. The other two tasks will set the other two
  637. // bits defined by ALL_SYNC_BITS. All three tasks have reached the
  638. // synchronisation point when all the ALL_SYNC_BITS are set. Wait
  639. // indefinitely for this to happen.
  640. xEventGroupSync( xEventBits, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  641. // xEventGroupSync() was called with an indefinite block time, so
  642. // this task will only reach here if the syncrhonisation was made by all
  643. // three tasks, so there is no need to test the return value.
  644. }
  645. }
  646. void vTask2( void *pvParameters )
  647. {
  648. for( ;; )
  649. {
  650. // Perform task functionality here.
  651. // Set bit 2 in the event flag to note this task has reached the
  652. // synchronisation point. The other two tasks will set the other two
  653. // bits defined by ALL_SYNC_BITS. All three tasks have reached the
  654. // synchronisation point when all the ALL_SYNC_BITS are set. Wait
  655. // indefinitely for this to happen.
  656. xEventGroupSync( xEventBits, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  657. // xEventGroupSync() was called with an indefinite block time, so
  658. // this task will only reach here if the syncrhonisation was made by all
  659. // three tasks, so there is no need to test the return value.
  660. }
  661. }
  662. </pre>
  663. * \defgroup xEventGroupSync xEventGroupSync
  664. * \ingroup EventGroup
  665. */
  666. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  667. /**
  668. * event_groups.h
  669. *<pre>
  670. EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup );
  671. </pre>
  672. *
  673. * Returns the current value of the bits in an event group. This function
  674. * cannot be used from an interrupt.
  675. *
  676. * @param xEventGroup The event group being queried.
  677. *
  678. * @return The event group bits at the time xEventGroupGetBits() was called.
  679. *
  680. * \defgroup xEventGroupGetBits xEventGroupGetBits
  681. * \ingroup EventGroup
  682. */
  683. #define xEventGroupGetBits( xEventGroup ) xEventGroupClearBits( xEventGroup, 0 )
  684. /**
  685. * event_groups.h
  686. *<pre>
  687. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup );
  688. </pre>
  689. *
  690. * A version of xEventGroupGetBits() that can be called from an ISR.
  691. *
  692. * @param xEventGroup The event group being queried.
  693. *
  694. * @return The event group bits at the time xEventGroupGetBitsFromISR() was called.
  695. *
  696. * \defgroup xEventGroupGetBitsFromISR xEventGroupGetBitsFromISR
  697. * \ingroup EventGroup
  698. */
  699. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
  700. /**
  701. * event_groups.h
  702. *<pre>
  703. void xEventGroupDelete( EventGroupHandle_t xEventGroup );
  704. </pre>
  705. *
  706. * Delete an event group that was previously created by a call to
  707. * xEventGroupCreate(). Tasks that are blocked on the event group will be
  708. * unblocked and obtain 0 as the event group's value.
  709. *
  710. * @param xEventGroup The event group being deleted.
  711. */
  712. void vEventGroupDelete( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
  713. /* For internal use only. */
  714. void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet ) PRIVILEGED_FUNCTION;
  715. void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear ) PRIVILEGED_FUNCTION;
  716. #if (configUSE_TRACE_FACILITY == 1)
  717. UBaseType_t uxEventGroupGetNumber( void* xEventGroup ) PRIVILEGED_FUNCTION;
  718. #endif
  719. #ifdef __cplusplus
  720. }
  721. #endif
  722. #endif /* EVENT_GROUPS_H */