event_groups.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * FreeRTOS Kernel V10.5.1
  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. /* Standard includes. */
  29. #include <stdlib.h>
  30. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  31. * all the API functions to use the MPU wrappers. That should only be done when
  32. * task.h is included from an application file. */
  33. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  34. /* FreeRTOS includes. */
  35. #include "FreeRTOS.h"
  36. #include "task.h"
  37. #include "timers.h"
  38. #include "event_groups.h"
  39. /* Lint e961, e750 and e9021 are suppressed as a MISRA exception justified
  40. * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  41. * for the header files above, but not in this file, in order to generate the
  42. * correct privileged Vs unprivileged linkage and placement. */
  43. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021 See comment above. */
  44. /* The following bit fields convey control information in a task's event list
  45. * item value. It is important they don't clash with the
  46. * taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */
  47. #if configUSE_16_BIT_TICKS == 1
  48. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x0100U
  49. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x0200U
  50. #define eventWAIT_FOR_ALL_BITS 0x0400U
  51. #define eventEVENT_BITS_CONTROL_BYTES 0xff00U /* 只有8位用于事件组 */
  52. #else
  53. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x01000000UL
  54. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x02000000UL
  55. #define eventWAIT_FOR_ALL_BITS 0x04000000UL
  56. #define eventEVENT_BITS_CONTROL_BYTES 0xff000000UL /* 只有24位用于事件组 */
  57. #endif
  58. typedef struct EventGroupDef_t
  59. {
  60. EventBits_t uxEventBits; /* 事件标志位 */
  61. List_t xTasksWaitingForBits; /* 等待事件发生的任务列表 */
  62. #if ( configUSE_TRACE_FACILITY == 1 )
  63. UBaseType_t uxEventGroupNumber;
  64. #endif
  65. } EventGroup_t;
  66. /*
  67. * Test the bits set in uxCurrentEventBits to see if the wait condition is met.
  68. * The wait condition is defined by xWaitForAllBits. If xWaitForAllBits is
  69. * pdTRUE then the wait condition is met if all the bits set in uxBitsToWaitFor
  70. * are also set in uxCurrentEventBits. If xWaitForAllBits is pdFALSE then the
  71. * wait condition is met if any of the bits set in uxBitsToWait for are also set
  72. * in uxCurrentEventBits.
  73. */
  74. static BaseType_t prvTestWaitCondition(const EventBits_t uxCurrentEventBits,
  75. const EventBits_t uxBitsToWaitFor,
  76. const BaseType_t xWaitForAllBits ) PRIVILEGED_FUNCTION;
  77. /**
  78. * 创建事件标志组
  79. */
  80. EventGroupHandle_t xEventGroupCreate(void)
  81. {
  82. EventGroup_t * pxEventBits;
  83. /* Allocate the event group. Justification for MISRA deviation as
  84. * follows: pvPortMalloc() always ensures returned memory blocks are
  85. * aligned per the requirements of the MCU stack. In this case
  86. * pvPortMalloc() must return a pointer that is guaranteed to meet the
  87. * alignment requirements of the EventGroup_t structure - which (if you
  88. * follow it through) is the alignment requirements of the TickType_t type
  89. * (EventBits_t being of TickType_t itself). Therefore, whenever the
  90. * stack alignment requirements are greater than or equal to the
  91. * TickType_t alignment requirements the cast is safe. In other cases,
  92. * where the natural word size of the architecture is less than
  93. * sizeof( TickType_t ), the TickType_t variables will be accessed in two
  94. * or more reads operations, and the alignment requirements is only that
  95. * of each individual read. */
  96. pxEventBits = (EventGroup_t *) pvPortMalloc(sizeof(EventGroup_t));
  97. if (pxEventBits != NULL)
  98. {
  99. /* 事件位全部清零 */
  100. pxEventBits->uxEventBits = 0;
  101. vListInitialise(&(pxEventBits->xTasksWaitingForBits));
  102. traceEVENT_GROUP_CREATE(pxEventBits);
  103. }
  104. else
  105. {
  106. traceEVENT_GROUP_CREATE_FAILED();
  107. }
  108. return pxEventBits;
  109. }
  110. EventBits_t xEventGroupSync(EventGroupHandle_t xEventGroup,
  111. const EventBits_t uxBitsToSet,
  112. const EventBits_t uxBitsToWaitFor,
  113. TickType_t xTicksToWait)
  114. {
  115. EventBits_t uxOriginalBitValue, uxReturn;
  116. EventGroup_t * pxEventBits = xEventGroup;
  117. BaseType_t xAlreadyYielded;
  118. BaseType_t xTimeoutOccurred = pdFALSE;
  119. configASSERT((uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES) == 0);
  120. configASSERT(uxBitsToWaitFor != 0 );
  121. #if ((INCLUDE_xTaskGetSchedulerState == 1) || (configUSE_TIMERS == 1))
  122. {
  123. configASSERT(!((xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED) && (xTicksToWait != 0)));
  124. }
  125. #endif
  126. vTaskSuspendAll();
  127. {
  128. uxOriginalBitValue = pxEventBits->uxEventBits;
  129. (void) xEventGroupSetBits(xEventGroup, uxBitsToSet);
  130. if (((uxOriginalBitValue | uxBitsToSet) & uxBitsToWaitFor) == uxBitsToWaitFor)
  131. {
  132. /* All the rendezvous bits are now set - no need to block. */
  133. uxReturn = ( uxOriginalBitValue | uxBitsToSet );
  134. /* Rendezvous always clear the bits. They will have been cleared
  135. * already unless this is the only task in the rendezvous. */
  136. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  137. xTicksToWait = 0;
  138. }
  139. else
  140. {
  141. if ( xTicksToWait != ( TickType_t ) 0 )
  142. {
  143. traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );
  144. /* Store the bits that the calling task is waiting for in the
  145. * task's event list item so the kernel knows when a match is
  146. * found. Then enter the blocked state. */
  147. vTaskPlaceOnUnorderedEventList(&(pxEventBits->xTasksWaitingForBits), (uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait);
  148. /* This assignment is obsolete as uxReturn will get set after
  149. * the task unblocks, but some compilers mistakenly generate a
  150. * warning about uxReturn being returned without being set if the
  151. * assignment is omitted. */
  152. uxReturn = 0;
  153. }
  154. else
  155. {
  156. /* The rendezvous bits were not set, but no block time was
  157. * specified - just return the current event bit value. */
  158. uxReturn = pxEventBits->uxEventBits;
  159. xTimeoutOccurred = pdTRUE;
  160. }
  161. }
  162. }
  163. xAlreadyYielded = xTaskResumeAll();
  164. if (xTicksToWait != (TickType_t)0)
  165. {
  166. if (xAlreadyYielded == pdFALSE)
  167. {
  168. portYIELD_WITHIN_API();
  169. }
  170. /* The task blocked to wait for its required bits to be set - at this
  171. * point either the required bits were set or the block time expired. If
  172. * the required bits were set they will have been stored in the task's
  173. * event list item, and they should now be retrieved then cleared. */
  174. uxReturn = uxTaskResetEventItemValue();
  175. if ((uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == (EventBits_t )0)
  176. {
  177. /* The task timed out, just return the current event bit value. */
  178. taskENTER_CRITICAL();
  179. {
  180. uxReturn = pxEventBits->uxEventBits;
  181. /* Although the task got here because it timed out before the
  182. * bits it was waiting for were set, it is possible that since it
  183. * unblocked another task has set the bits. If this is the case
  184. * then it needs to clear the bits before exiting. */
  185. if ( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )
  186. {
  187. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  188. }
  189. }
  190. taskEXIT_CRITICAL();
  191. xTimeoutOccurred = pdTRUE;
  192. }
  193. else
  194. {
  195. /* The task unblocked because the bits were set. */
  196. }
  197. /* Control bits might be set as the task had blocked should not be
  198. * returned. */
  199. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  200. }
  201. traceEVENT_GROUP_SYNC_END(xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred);
  202. /* Prevent compiler warnings when trace macros are not used. */
  203. ( void ) xTimeoutOccurred;
  204. return uxReturn;
  205. }
  206. /**
  207. * 等待指定的事件位
  208. * @uxBitsToWaitFor: 任务等待的事件位
  209. * @xTicksToWait: 任务等待的节拍数
  210. * @xWaitForAllBits : 是否等待uxBitsToWaitFor中的所有事件发生才返回
  211. */
  212. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  213. const EventBits_t uxBitsToWaitFor,
  214. const BaseType_t xClearOnExit,
  215. const BaseType_t xWaitForAllBits,
  216. TickType_t xTicksToWait)
  217. {
  218. EventGroup_t * pxEventBits = xEventGroup;
  219. EventBits_t uxReturn, uxControlBits = 0;
  220. BaseType_t xWaitConditionMet, xAlreadyYielded;
  221. BaseType_t xTimeoutOccurred = pdFALSE;
  222. /* Check the user is not attempting to wait on the bits used by the kernel
  223. * itself, and that at least one bit is being requested. */
  224. configASSERT(xEventGroup);
  225. configASSERT((uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES) == 0);
  226. configASSERT(uxBitsToWaitFor != 0);
  227. #if ((INCLUDE_xTaskGetSchedulerState == 1) || (configUSE_TIMERS == 1))
  228. {
  229. configASSERT(!((xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED) && (xTicksToWait != 0)));
  230. }
  231. #endif
  232. vTaskSuspendAll();
  233. {
  234. const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
  235. /* Check to see if the wait condition is already met or not. */
  236. xWaitConditionMet = prvTestWaitCondition(uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits);
  237. /* 等待的事件发生 */
  238. if (xWaitConditionMet != pdFALSE)
  239. {
  240. /* The wait condition has already been met so there is no need to
  241. * block.
  242. */
  243. uxReturn = uxCurrentEventBits;
  244. xTicksToWait = (TickType_t)0;
  245. /* Clear the wait bits if requested to do so. */
  246. if (xClearOnExit != pdFALSE)
  247. {
  248. /* 事件发生时,清空等待的事件 */
  249. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  250. }
  251. }
  252. else if (xTicksToWait == (TickType_t)0)
  253. {
  254. /* The wait condition has not been met, but no block time was
  255. * specified, so just return the current value.
  256. * 事件未发生,但是不进行等待
  257. */
  258. uxReturn = uxCurrentEventBits;
  259. xTimeoutOccurred = pdTRUE;
  260. }
  261. else
  262. {
  263. /* The task is going to block to wait for its required bits to be
  264. * set. uxControlBits are used to remember the specified behaviour of
  265. * this call to xEventGroupWaitBits() - for use when the event bits
  266. * unblock the task. */
  267. if (xClearOnExit != pdFALSE)
  268. {
  269. uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;
  270. }
  271. /* 等待所有的事件发生 */
  272. if (xWaitForAllBits != pdFALSE)
  273. {
  274. uxControlBits |= eventWAIT_FOR_ALL_BITS;
  275. }
  276. /* Store the bits that the calling task is waiting for in the
  277. * task's event list item so the kernel knows when a match is
  278. * found. Then enter the blocked state.
  279. * 任务等待的事件,没有发生,加入阻塞队列
  280. */
  281. vTaskPlaceOnUnorderedEventList(&(pxEventBits->xTasksWaitingForBits), (uxBitsToWaitFor|uxControlBits), xTicksToWait);
  282. /* This is obsolete as it will get set after the task unblocks, but
  283. * some compilers mistakenly generate a warning about the variable
  284. * being returned without being set if it is not done. */
  285. uxReturn = 0;
  286. traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );
  287. }
  288. }
  289. xAlreadyYielded = xTaskResumeAll();
  290. /* 任务阻塞等待 */
  291. if (xTicksToWait != (TickType_t)0)
  292. {
  293. /* 任务进入阻塞等待,触发任务调度 */
  294. if (xAlreadyYielded == pdFALSE)
  295. {
  296. portYIELD_WITHIN_API();
  297. }
  298. /* The task blocked to wait for its required bits to be set - at this
  299. * point either the required bits were set or the block time expired. If
  300. * the required bits were set they will have been stored in the task's
  301. * event list item, and they should now be retrieved then cleared.
  302. * 被阻塞的任务,恢复执行
  303. */
  304. uxReturn = uxTaskResetEventItemValue();
  305. /* 恢复执行,但是等待的事件没发生(等待超时) */
  306. if ((uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET) == (EventBits_t)0)
  307. {
  308. taskENTER_CRITICAL();
  309. {
  310. /* The task timed out, just return the current event bit value. */
  311. uxReturn = pxEventBits->uxEventBits;
  312. /* It is possible that the event bits were updated between this
  313. * task leaving the Blocked state and running again. */
  314. if (prvTestWaitCondition(uxReturn, uxBitsToWaitFor, xWaitForAllBits) != pdFALSE)
  315. {
  316. if (xClearOnExit != pdFALSE )
  317. {
  318. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  319. }
  320. }
  321. xTimeoutOccurred = pdTRUE;
  322. }
  323. taskEXIT_CRITICAL();
  324. }
  325. else
  326. {
  327. /* The task unblocked because the bits were set. */
  328. }
  329. /* The task blocked so control bits may have been set. */
  330. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  331. }
  332. traceEVENT_GROUP_WAIT_BITS_END(xEventGroup, uxBitsToWaitFor, xTimeoutOccurred);
  333. /* Prevent compiler warnings when trace macros are not used. */
  334. (void)xTimeoutOccurred;
  335. return uxReturn;
  336. }
  337. /**
  338. * 将指定的事件位清零
  339. * @uxBitsToClear: 需要清零的事件位
  340. */
  341. EventBits_t xEventGroupClearBits(EventGroupHandle_t xEventGroup,
  342. const EventBits_t uxBitsToClear)
  343. {
  344. EventGroup_t * pxEventBits = xEventGroup;
  345. EventBits_t uxReturn;
  346. /* Check the user is not attempting to clear the bits used by the kernel
  347. * itself. */
  348. configASSERT(xEventGroup);
  349. configASSERT((uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES) == 0);
  350. taskENTER_CRITICAL();
  351. {
  352. traceEVENT_GROUP_CLEAR_BITS(xEventGroup, uxBitsToClear);
  353. /**
  354. * The value returned is the event group value prior to the bits being
  355. * cleared.
  356. * 获取事件组标记位
  357. */
  358. uxReturn = pxEventBits->uxEventBits;
  359. /**
  360. * Clear the bits
  361. * 清理掉标记位
  362. */
  363. pxEventBits->uxEventBits &= ~uxBitsToClear;
  364. }
  365. taskEXIT_CRITICAL();
  366. /* 返回旧的事件位 */
  367. return uxReturn;
  368. }
  369. #if ((configUSE_TRACE_FACILITY == 1) && (INCLUDE_xTimerPendFunctionCall == 1) && (configUSE_TIMERS == 1))
  370. BaseType_t xEventGroupClearBitsFromISR(EventGroupHandle_t xEventGroup,
  371. const EventBits_t uxBitsToClear)
  372. {
  373. BaseType_t xReturn;
  374. traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear);
  375. xReturn = xTimerPendFunctionCallFromISR(vEventGroupClearBitsCallback, (void *)xEventGroup, (uint32_t)uxBitsToClear, NULL);
  376. return xReturn;
  377. }
  378. #endif
  379. EventBits_t xEventGroupGetBitsFromISR(EventGroupHandle_t xEventGroup)
  380. {
  381. UBaseType_t uxSavedInterruptStatus;
  382. EventGroup_t const * const pxEventBits = xEventGroup;
  383. EventBits_t uxReturn;
  384. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  385. {
  386. uxReturn = pxEventBits->uxEventBits;
  387. }
  388. portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedInterruptStatus);
  389. return uxReturn;
  390. }
  391. /**
  392. * 将指定的事件位设置为1
  393. * @uxBitsToSet: 待设置的bit位置
  394. */
  395. EventBits_t xEventGroupSetBits(EventGroupHandle_t xEventGroup,
  396. const EventBits_t uxBitsToSet)
  397. {
  398. ListItem_t * pxListItem;
  399. ListItem_t * pxNext;
  400. ListItem_t const * pxListEnd;
  401. List_t const * pxList;
  402. EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;
  403. EventGroup_t * pxEventBits = xEventGroup;
  404. BaseType_t xMatchFound = pdFALSE;
  405. /* Check the user is not attempting to set the bits used by the kernel
  406. * itself. */
  407. configASSERT(xEventGroup);
  408. configASSERT((uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES) == 0);
  409. pxList = &(pxEventBits->xTasksWaitingForBits);
  410. pxListEnd = listGET_END_MARKER(pxList);
  411. vTaskSuspendAll();
  412. {
  413. traceEVENT_GROUP_SET_BITS(xEventGroup, uxBitsToSet);
  414. pxListItem = listGET_HEAD_ENTRY(pxList);
  415. /**
  416. * Set the bits.
  417. * 设置事件标记位
  418. */
  419. pxEventBits->uxEventBits |= uxBitsToSet;
  420. /**
  421. * See if the new bit value should unblock any tasks.
  422. * 有任务在等待事件位被设置
  423. */
  424. while(pxListItem != pxListEnd)
  425. {
  426. pxNext = listGET_NEXT(pxListItem);
  427. uxBitsWaitedFor = listGET_LIST_ITEM_VALUE(pxListItem);
  428. xMatchFound = pdFALSE;
  429. /* Split the bits waited for from the control bits. */
  430. uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;
  431. /* 计算出任务等待的事件组 */
  432. uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;
  433. if ((uxControlBits & eventWAIT_FOR_ALL_BITS) == (EventBits_t)0)
  434. {
  435. /* Just looking for single bit being set. */
  436. if ((uxBitsWaitedFor & pxEventBits->uxEventBits) != (EventBits_t)0)
  437. {
  438. xMatchFound = pdTRUE;
  439. }
  440. }
  441. else if ((uxBitsWaitedFor & pxEventBits->uxEventBits) == uxBitsWaitedFor)
  442. {
  443. /**
  444. * All bits are set.
  445. */
  446. xMatchFound = pdTRUE;
  447. }
  448. else
  449. {
  450. /* Need all bits to be set, but not all the bits were set. */
  451. }
  452. /* 等待的事件发生 */
  453. if (xMatchFound != pdFALSE)
  454. {
  455. /* The bits match. Should the bits be cleared on exit? */
  456. if ((uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT) != (EventBits_t)0)
  457. {
  458. /* 事件发生后,需要被清空 */
  459. uxBitsToClear |= uxBitsWaitedFor;
  460. }
  461. /* Store the actual event flag value in the task's event list
  462. * item before removing the task from the event list. The
  463. * eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows
  464. * that is was unblocked due to its required bits matching, rather
  465. * than because it timed out.
  466. * 任务从等待队列中删除
  467. */
  468. vTaskRemoveFromUnorderedEventList(pxListItem, pxEventBits->uxEventBits|eventUNBLOCKED_DUE_TO_BIT_SET);
  469. }
  470. /* Move onto the next list item. Note pxListItem->pxNext is not
  471. * used here as the list item may have been removed from the event list
  472. * and inserted into the ready/pending reading list.
  473. */
  474. pxListItem = pxNext;
  475. }
  476. /**
  477. * Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT
  478. * bit was set in the control word.
  479. */
  480. pxEventBits->uxEventBits &= ~uxBitsToClear;
  481. }
  482. (void) xTaskResumeAll();
  483. /* 返回剩余的事件位 */
  484. return pxEventBits->uxEventBits;
  485. }
  486. void vEventGroupDelete(EventGroupHandle_t xEventGroup)
  487. {
  488. EventGroup_t * pxEventBits = xEventGroup;
  489. const List_t * pxTasksWaitingForBits;
  490. configASSERT(pxEventBits);
  491. pxTasksWaitingForBits = &(pxEventBits->xTasksWaitingForBits);
  492. vTaskSuspendAll();
  493. {
  494. traceEVENT_GROUP_DELETE(xEventGroup);
  495. while(listCURRENT_LIST_LENGTH(pxTasksWaitingForBits) > (UBaseType_t)0)
  496. {
  497. /* Unblock the task, returning 0 as the event list is being deleted
  498. * and cannot therefore have any bits set. */
  499. configASSERT(pxTasksWaitingForBits->xListEnd.pxNext != (const ListItem_t *) &(pxTasksWaitingForBits->xListEnd));
  500. vTaskRemoveFromUnorderedEventList(pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET);
  501. }
  502. }
  503. (void) xTaskResumeAll();
  504. /* The event group can only have been allocated dynamically - free
  505. * it again. */
  506. vPortFree(pxEventBits);
  507. }
  508. /* For internal use only - execute a 'set bits' command that was pended from
  509. * an interrupt.
  510. */
  511. void vEventGroupSetBitsCallback(void * pvEventGroup,
  512. const uint32_t ulBitsToSet)
  513. {
  514. (void)xEventGroupSetBits(pvEventGroup, (EventBits_t) ulBitsToSet);
  515. }
  516. /* For internal use only - execute a 'clear bits' command that was pended from
  517. * an interrupt.
  518. */
  519. void vEventGroupClearBitsCallback(void * pvEventGroup,
  520. const uint32_t ulBitsToClear)
  521. {
  522. (void)xEventGroupClearBits(pvEventGroup, (EventBits_t)ulBitsToClear);
  523. }
  524. /**
  525. * 是否有等待的事件发生
  526. */
  527. static BaseType_t prvTestWaitCondition(const EventBits_t uxCurrentEventBits,
  528. const EventBits_t uxBitsToWaitFor,
  529. const BaseType_t xWaitForAllBits)
  530. {
  531. BaseType_t xWaitConditionMet = pdFALSE;
  532. /* 等待任意一个事件发生即可 */
  533. if (xWaitForAllBits == pdFALSE)
  534. {
  535. /* Task only has to wait for one bit within uxBitsToWaitFor to be
  536. * set. Is one already set? */
  537. if ((uxCurrentEventBits & uxBitsToWaitFor) != (EventBits_t)0)
  538. {
  539. xWaitConditionMet = pdTRUE;
  540. }
  541. }
  542. else
  543. {
  544. /* Task has to wait for all the bits in uxBitsToWaitFor to be set.
  545. * Are they set already?
  546. * 任务等待所有的事件都发生
  547. */
  548. if ((uxCurrentEventBits & uxBitsToWaitFor) == uxBitsToWaitFor)
  549. {
  550. xWaitConditionMet = pdTRUE;
  551. }
  552. }
  553. return xWaitConditionMet;
  554. }
  555. #if ((configUSE_TRACE_FACILITY == 1) && (INCLUDE_xTimerPendFunctionCall == 1) && (configUSE_TIMERS == 1))
  556. BaseType_t xEventGroupSetBitsFromISR(EventGroupHandle_t xEventGroup,
  557. const EventBits_t uxBitsToSet,
  558. BaseType_t * pxHigherPriorityTaskWoken)
  559. {
  560. BaseType_t xReturn;
  561. traceEVENT_GROUP_SET_BITS_FROM_ISR(xEventGroup, uxBitsToSet);
  562. xReturn = xTimerPendFunctionCallFromISR(vEventGroupSetBitsCallback, (void *)xEventGroup, (uint32_t) uxBitsToSet, pxHigherPriorityTaskWoken);
  563. return xReturn;
  564. }
  565. #endif
  566. #if (configUSE_TRACE_FACILITY == 1)
  567. UBaseType_t uxEventGroupGetNumber(void * xEventGroup)
  568. {
  569. UBaseType_t xReturn;
  570. EventGroup_t const * pxEventBits = (EventGroup_t *)xEventGroup;
  571. if (xEventGroup == NULL)
  572. {
  573. xReturn = 0;
  574. }
  575. else
  576. {
  577. xReturn = pxEventBits->uxEventGroupNumber;
  578. }
  579. return xReturn;
  580. }
  581. #endif
  582. #if (configUSE_TRACE_FACILITY == 1)
  583. void vEventGroupSetNumber(void * xEventGroup,
  584. UBaseType_t uxEventGroupNumber)
  585. {
  586. ((EventGroup_t *) xEventGroup)->uxEventGroupNumber = uxEventGroupNumber;
  587. }
  588. #endif