event_groups.h 31 KB

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