os_flag.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * Copyright (c) 2021, Meco Jianting Man <jiantingman@foxmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-12-10 Meco Man first version
  9. */
  10. /*
  11. *********************************************************************************************************
  12. * uC/OS-II
  13. * The Real-Time Kernel
  14. *
  15. * Copyright 1992-2020 Silicon Laboratories Inc. www.silabs.com
  16. *
  17. * SPDX-License-Identifier: APACHE-2.0
  18. *
  19. * This software is subject to an open source license and is distributed by
  20. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  21. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  22. *
  23. *********************************************************************************************************
  24. */
  25. /*
  26. *********************************************************************************************************
  27. *
  28. * EVENT FLAG MANAGEMENT
  29. *
  30. * Filename : os_flag.c
  31. * Version : V2.93.00
  32. *********************************************************************************************************
  33. */
  34. #include "ucos_ii.h"
  35. #if (OS_FLAG_EN > 0u)
  36. /*
  37. *********************************************************************************************************
  38. * CHECK THE STATUS OF FLAGS IN AN EVENT FLAG GROUP
  39. *
  40. * Description: This function is called to check the status of a combination of bits to be set or cleared
  41. * in an event flag group. Your application can check for ANY bit to be set/cleared or ALL
  42. * bits to be set/cleared.
  43. *
  44. * This call does not block if the desired flags are not present.
  45. *
  46. * Arguments : pgrp is a pointer to the desired event flag group.
  47. *
  48. * flags Is a bit pattern indicating which bit(s) (i.e. flags) you wish to check.
  49. * The bits you want are specified by setting the corresponding bits in
  50. * 'flags'. e.g. if your application wants to wait for bits 0 and 1 then
  51. * 'flags' would contain 0x03.
  52. *
  53. * wait_type specifies whether you want ALL bits to be set/cleared or ANY of the bits
  54. * to be set/cleared.
  55. * You can specify the following argument:
  56. *
  57. * OS_FLAG_WAIT_CLR_ALL You will check ALL bits in 'flags' to be clear (0)
  58. * OS_FLAG_WAIT_CLR_ANY You will check ANY bit in 'flags' to be clear (0)
  59. * OS_FLAG_WAIT_SET_ALL You will check ALL bits in 'flags' to be set (1)
  60. * OS_FLAG_WAIT_SET_ANY You will check ANY bit in 'flags' to be set (1)
  61. *
  62. * NOTE: Add OS_FLAG_CONSUME if you want the event flag to be 'consumed' by
  63. * the call. Example, to wait for any flag in a group AND then clear
  64. * the flags that are present, set 'wait_type' to:
  65. *
  66. * OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME
  67. *
  68. * perr is a pointer to an error code and can be:
  69. * OS_ERR_NONE No error
  70. * OS_ERR_EVENT_TYPE You are not pointing to an event flag group
  71. * OS_ERR_FLAG_WAIT_TYPE You didn't specify a proper 'wait_type' argument.
  72. * OS_ERR_FLAG_INVALID_PGRP You passed a NULL pointer instead of the event flag
  73. * group handle.
  74. * OS_ERR_FLAG_NOT_RDY The desired flags you are waiting for are not
  75. * available.
  76. *
  77. * Returns : The flags in the event flag group that made the task ready or, 0 if a timeout or an error
  78. * occurred.
  79. *
  80. * Called from: Task or ISR
  81. *
  82. * Note(s) : 1) IMPORTANT, the behavior of this function has changed from PREVIOUS versions. The
  83. * function NOW returns the flags that were ready INSTEAD of the current state of the
  84. * event flags.
  85. *********************************************************************************************************
  86. */
  87. #if OS_FLAG_ACCEPT_EN > 0u
  88. OS_FLAGS OSFlagAccept (OS_FLAG_GRP *pgrp,
  89. OS_FLAGS flags,
  90. INT8U wait_type,
  91. INT8U *perr)
  92. {
  93. OS_FLAGS flags_rdy;
  94. INT8U result;
  95. BOOLEAN consume;
  96. rt_uint8_t rt_option = 0;
  97. rt_err_t rt_err;
  98. #ifdef OS_SAFETY_CRITICAL
  99. if (perr == (INT8U *)0) {
  100. OS_SAFETY_CRITICAL_EXCEPTION();
  101. return ((OS_FLAGS)0);
  102. }
  103. #endif
  104. #if OS_ARG_CHK_EN > 0u
  105. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  106. *perr = OS_ERR_FLAG_INVALID_PGRP;
  107. return ((OS_FLAGS)0);
  108. }
  109. #endif
  110. if (rt_object_get_type(&pgrp->pFlagGrp->parent.parent) /* Validate event block type */
  111. != RT_Object_Class_Event) {
  112. *perr = OS_ERR_EVENT_TYPE;
  113. return ((OS_FLAGS)0);
  114. }
  115. result = (INT8U)(wait_type & OS_FLAG_CONSUME);
  116. if (result != (INT8U)0) { /* See if we need to consume the flags */
  117. wait_type &= ~OS_FLAG_CONSUME;
  118. consume = OS_TRUE;
  119. } else {
  120. consume = OS_FALSE;
  121. }
  122. *perr = OS_ERR_NONE; /* Assume NO error until proven otherwise. */
  123. switch (wait_type) {
  124. case OS_FLAG_WAIT_SET_ALL: /* See if all required flags are set */
  125. rt_option = RT_EVENT_FLAG_AND;
  126. break;
  127. case OS_FLAG_WAIT_SET_ANY:
  128. rt_option = RT_EVENT_FLAG_OR;
  129. break;
  130. #if OS_FLAG_WAIT_CLR_EN > 0u
  131. case OS_FLAG_WAIT_CLR_ALL: /* See if all required flags are cleared */
  132. rt_option = RT_EVENT_FLAG_AND;
  133. break;
  134. case OS_FLAG_WAIT_CLR_ANY:
  135. rt_option = RT_EVENT_FLAG_OR;
  136. break;
  137. #endif
  138. default:
  139. *perr = OS_ERR_FLAG_WAIT_TYPE;
  140. return 0;
  141. }
  142. if (consume == OS_TRUE){
  143. rt_option |= RT_EVENT_FLAG_CLEAR;
  144. }
  145. rt_err = rt_event_recv(pgrp->pFlagGrp,
  146. flags,
  147. rt_option,
  148. RT_WAITING_NO,
  149. &flags_rdy);
  150. if (rt_err == -RT_ETIMEOUT) {
  151. *perr = OS_ERR_FLAG_NOT_RDY;
  152. flags_rdy = 0;
  153. } else {
  154. *perr = OS_ERR_NONE;
  155. }
  156. return (flags_rdy);
  157. }
  158. #endif
  159. /*
  160. *********************************************************************************************************
  161. * CREATE AN EVENT FLAG
  162. *
  163. * Description: This function is called to create an event flag group.
  164. *
  165. * Arguments : flags Contains the initial value to store in the event flag group.
  166. *
  167. * perr is a pointer to an error code which will be returned to your application:
  168. * OS_ERR_NONE if the call was successful.
  169. * OS_ERR_CREATE_ISR if you attempted to create an Event Flag from an
  170. * ISR.
  171. * OS_ERR_FLAG_GRP_DEPLETED if there are no more event flag groups
  172. * OS_ERR_ILLEGAL_CREATE_RUN_TIME if you tried to create an event flag after
  173. * safety critical operation started.
  174. *
  175. * Returns : A pointer to an event flag group or a NULL pointer if no more groups are available.
  176. *
  177. * Called from: Task ONLY
  178. *********************************************************************************************************
  179. */
  180. OS_FLAG_GRP *OSFlagCreate (OS_FLAGS flags,
  181. INT8U *perr)
  182. {
  183. OS_FLAG_GRP *pgrp;
  184. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  185. OS_CPU_SR cpu_sr = 0u;
  186. #endif
  187. rt_event_t rt_event;
  188. #ifdef OS_SAFETY_CRITICAL
  189. if (perr == (INT8U *)0) {
  190. OS_SAFETY_CRITICAL_EXCEPTION();
  191. return ((OS_FLAG_GRP *)0);
  192. }
  193. #endif
  194. #ifdef OS_SAFETY_CRITICAL_IEC61508
  195. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  196. OS_SAFETY_CRITICAL_EXCEPTION();
  197. *perr = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
  198. return ((OS_FLAG_GRP *)0);
  199. }
  200. #endif
  201. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  202. *perr = OS_ERR_CREATE_ISR; /* ... can't CREATE from an ISR */
  203. return ((OS_FLAG_GRP *)0);
  204. }
  205. pgrp = RT_KERNEL_MALLOC(sizeof(OS_FLAG_GRP));
  206. if (pgrp == (OS_FLAG_GRP *)0) { /* Get an event control block */
  207. return ((OS_FLAG_GRP *)0);
  208. }
  209. rt_event = rt_event_create("uCOS-II", RT_IPC_FLAG_PRIO);
  210. if(!rt_event) {
  211. *perr = OS_ERR_FLAG_GRP_DEPLETED;
  212. RT_KERNEL_FREE(pgrp);
  213. } else {
  214. *perr = OS_ERR_NONE;
  215. }
  216. OS_ENTER_CRITICAL();
  217. pgrp->pFlagGrp = rt_event;
  218. pgrp->OSFlagFlags = flags;
  219. OS_EXIT_CRITICAL();
  220. return (pgrp); /* Return pointer to event flag group */
  221. }
  222. /*
  223. *********************************************************************************************************
  224. * DELETE AN EVENT FLAG GROUP
  225. *
  226. * Description: This function deletes an event flag group and readies all tasks pending on the event flag
  227. * group.
  228. *
  229. * Arguments : pgrp is a pointer to the desired event flag group.
  230. *
  231. * opt determines delete options as follows:
  232. * opt == OS_DEL_NO_PEND Deletes the event flag group ONLY if no task pending
  233. * opt == OS_DEL_ALWAYS Deletes the event flag group even if tasks are
  234. * waiting. In this case, all the tasks pending will be
  235. * readied.
  236. *
  237. * perr is a pointer to an error code that can contain one of the following values:
  238. * OS_ERR_NONE The call was successful and the event flag group was
  239. * deleted
  240. * OS_ERR_DEL_ISR If you attempted to delete the event flag group from
  241. * an ISR
  242. * OS_ERR_FLAG_INVALID_PGRP If 'pgrp' is a NULL pointer.
  243. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to an event flag group
  244. * OS_ERR_ILLEGAL_DEL_RUN_TIME If you tried to delete an event flag after
  245. * safety critical operation started.
  246. * OS_ERR_INVALID_OPT An invalid option was specified
  247. * OS_ERR_TASK_WAITING One or more tasks were waiting on the event flag
  248. * group.
  249. *
  250. * Returns : pgrp upon error
  251. * (OS_EVENT *)0 if the event flag group was successfully deleted.
  252. *
  253. * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
  254. * the event flag group MUST check the return code of OSFlagAccept() and OSFlagPend().
  255. * 2) This call can potentially disable interrupts for a long time. The interrupt disable
  256. * time is directly proportional to the number of tasks waiting on the event flag group.
  257. * 3) All tasks that were waiting for the event flag will be readied and returned an
  258. * OS_ERR_PEND_ABORT if OSFlagDel() was called with OS_DEL_ALWAYS
  259. *********************************************************************************************************
  260. */
  261. #if OS_FLAG_DEL_EN > 0u
  262. OS_FLAG_GRP *OSFlagDel (OS_FLAG_GRP *pgrp,
  263. INT8U opt,
  264. INT8U *perr)
  265. {
  266. OS_FLAG_GRP *pgrp_return;
  267. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  268. OS_CPU_SR cpu_sr = 0u;
  269. #endif
  270. #ifdef OS_SAFETY_CRITICAL
  271. if (perr == (INT8U *)0) {
  272. OS_SAFETY_CRITICAL_EXCEPTION();
  273. return ((OS_FLAG_GRP *)0);
  274. }
  275. #endif
  276. #ifdef OS_SAFETY_CRITICAL_IEC61508
  277. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  278. OS_SAFETY_CRITICAL_EXCEPTION();
  279. *perr = OS_ERR_ILLEGAL_DEL_RUN_TIME;
  280. return ((OS_FLAG_GRP *)0);
  281. }
  282. #endif
  283. #if OS_ARG_CHK_EN > 0u
  284. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  285. *perr = OS_ERR_FLAG_INVALID_PGRP;
  286. return (pgrp);
  287. }
  288. #endif
  289. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  290. *perr = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
  291. return (pgrp);
  292. }
  293. if (rt_object_get_type(&pgrp->pFlagGrp->parent.parent) /* Validate event block type */
  294. != RT_Object_Class_Event) {
  295. *perr = OS_ERR_EVENT_TYPE;
  296. return (pgrp);
  297. }
  298. OS_ENTER_CRITICAL();
  299. switch (opt) {
  300. case OS_DEL_NO_PEND: /* Delete group if no task waiting */
  301. if (rt_list_isempty(&(pgrp->pFlagGrp->parent.suspend_thread))) { /* 若没有线程等待信号量 */
  302. pgrp->OSFlagFlags = (OS_FLAGS)0;
  303. OS_EXIT_CRITICAL();
  304. rt_event_detach(pgrp->pFlagGrp);
  305. RT_KERNEL_FREE(pgrp);
  306. *perr = OS_ERR_NONE;
  307. pgrp_return = (OS_FLAG_GRP *)0; /* Event Flag Group has been deleted */
  308. } else {
  309. OS_EXIT_CRITICAL();
  310. *perr = OS_ERR_TASK_WAITING;
  311. pgrp_return = pgrp;
  312. }
  313. break;
  314. case OS_DEL_ALWAYS: /* Always delete the event flag group */
  315. pgrp->OSFlagFlags = (OS_FLAGS)0;
  316. OS_EXIT_CRITICAL();
  317. rt_event_detach(pgrp->pFlagGrp);
  318. RT_KERNEL_FREE(pgrp);
  319. *perr = OS_ERR_NONE;
  320. pgrp_return = (OS_FLAG_GRP *)0; /* Event Flag Group has been deleted */
  321. break;
  322. default:
  323. OS_EXIT_CRITICAL();
  324. *perr = OS_ERR_INVALID_OPT;
  325. pgrp_return = pgrp;
  326. break;
  327. }
  328. return (pgrp_return);
  329. }
  330. #endif
  331. /*
  332. *********************************************************************************************************
  333. * ASSIGN A NAME TO AN EVENT FLAG GROUP
  334. *
  335. * Description: This function assigns a name to an event flag group.
  336. *
  337. * Arguments : pgrp is a pointer to the event flag group.
  338. *
  339. * pname is a pointer to an ASCII string that will be used as the name of the event flag
  340. * group.
  341. *
  342. * perr is a pointer to an error code that can contain one of the following values:
  343. *
  344. * OS_ERR_NONE if the requested task is resumed
  345. * OS_ERR_EVENT_TYPE if 'pevent' is not pointing to an event flag group
  346. * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
  347. * OS_ERR_FLAG_INVALID_PGRP if you passed a NULL pointer for 'pgrp'
  348. * OS_ERR_NAME_SET_ISR if you called this function from an ISR
  349. *
  350. * Returns : None
  351. *********************************************************************************************************
  352. */
  353. #if OS_FLAG_NAME_EN > 0u
  354. void OSFlagNameSet (OS_FLAG_GRP *pgrp,
  355. INT8U *pname,
  356. INT8U *perr)
  357. {
  358. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  359. OS_CPU_SR cpu_sr = 0u;
  360. #endif
  361. #ifdef OS_SAFETY_CRITICAL
  362. if (perr == (INT8U *)0) {
  363. OS_SAFETY_CRITICAL_EXCEPTION();
  364. return;
  365. }
  366. #endif
  367. #if OS_ARG_CHK_EN > 0u
  368. if (pgrp == (OS_FLAG_GRP *)0) { /* Is 'pgrp' a NULL pointer? */
  369. *perr = OS_ERR_FLAG_INVALID_PGRP;
  370. return;
  371. }
  372. if (pname == (INT8U *)0) { /* Is 'pname' a NULL pointer? */
  373. *perr = OS_ERR_PNAME_NULL;
  374. return;
  375. }
  376. #endif
  377. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  378. *perr = OS_ERR_NAME_SET_ISR;
  379. return;
  380. }
  381. OS_ENTER_CRITICAL();
  382. if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {
  383. OS_EXIT_CRITICAL();
  384. *perr = OS_ERR_EVENT_TYPE;
  385. return;
  386. }
  387. pgrp->OSFlagName = pname;
  388. OS_EXIT_CRITICAL();
  389. OS_TRACE_EVENT_NAME_SET(pgrp, pname);
  390. *perr = OS_ERR_NONE;
  391. return;
  392. }
  393. #endif
  394. /*
  395. *********************************************************************************************************
  396. * WAIT ON AN EVENT FLAG GROUP
  397. *
  398. * Description: This function is called to wait for a combination of bits to be set in an event flag
  399. * group. Your application can wait for ANY bit to be set or ALL bits to be set.
  400. *
  401. * Arguments : pgrp is a pointer to the desired event flag group.
  402. *
  403. * flags Is a bit pattern indicating which bit(s) (i.e. flags) you wish to wait for.
  404. * The bits you want are specified by setting the corresponding bits in
  405. * 'flags'. e.g. if your application wants to wait for bits 0 and 1 then
  406. * 'flags' would contain 0x03.
  407. *
  408. * wait_type specifies whether you want ALL bits to be set or ANY of the bits to be set.
  409. * You can specify the following argument:
  410. *
  411. * OS_FLAG_WAIT_CLR_ALL You will wait for ALL bits in 'mask' to be clear (0)
  412. * OS_FLAG_WAIT_SET_ALL You will wait for ALL bits in 'mask' to be set (1)
  413. * OS_FLAG_WAIT_CLR_ANY You will wait for ANY bit in 'mask' to be clear (0)
  414. * OS_FLAG_WAIT_SET_ANY You will wait for ANY bit in 'mask' to be set (1)
  415. *
  416. * NOTE: Add OS_FLAG_CONSUME if you want the event flag to be 'consumed' by
  417. * the call. Example, to wait for any flag in a group AND then clear
  418. * the flags that are present, set 'wait_type' to:
  419. *
  420. * OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME
  421. *
  422. * timeout is an optional timeout (in clock ticks) that your task will wait for the
  423. * desired bit combination. If you specify 0, however, your task will wait
  424. * forever at the specified event flag group or, until a message arrives.
  425. *
  426. * perr is a pointer to an error code and can be:
  427. * OS_ERR_NONE The desired bits have been set within the specified
  428. * 'timeout'.
  429. * OS_ERR_PEND_ISR If you tried to PEND from an ISR
  430. * OS_ERR_FLAG_INVALID_PGRP If 'pgrp' is a NULL pointer.
  431. * OS_ERR_EVENT_TYPE You are not pointing to an event flag group
  432. * OS_ERR_TIMEOUT The bit(s) have not been set in the specified
  433. * 'timeout'.
  434. * OS_ERR_PEND_ABORT The wait on the flag was aborted.
  435. * OS_ERR_FLAG_WAIT_TYPE You didn't specify a proper 'wait_type' argument.
  436. *
  437. * Returns : The flags in the event flag group that made the task ready or, 0 if a timeout or an error
  438. * occurred.
  439. *
  440. * Called from: Task ONLY
  441. *
  442. * Note(s) : 1) IMPORTANT, the behavior of this function has changed from PREVIOUS versions. The
  443. * function NOW returns the flags that were ready INSTEAD of the current state of the
  444. * event flags.
  445. *********************************************************************************************************
  446. */
  447. OS_FLAGS OSFlagPend (OS_FLAG_GRP *pgrp,
  448. OS_FLAGS flags,
  449. INT8U wait_type,
  450. INT32U timeout,
  451. INT8U *perr)
  452. {
  453. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  454. OS_CPU_SR cpu_sr = 0u;
  455. #endif
  456. rt_err_t rt_err;
  457. INT8U result;
  458. BOOLEAN consume;
  459. rt_uint8_t rt_option = 0;
  460. OS_FLAGS flags_rdy;
  461. #ifdef OS_SAFETY_CRITICAL
  462. if (perr == (INT8U *)0) {
  463. OS_SAFETY_CRITICAL_EXCEPTION();
  464. return ((OS_FLAGS)0);
  465. }
  466. #endif
  467. #if OS_ARG_CHK_EN > 0u
  468. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  469. *perr = OS_ERR_FLAG_INVALID_PGRP;
  470. return ((OS_FLAGS)0);
  471. }
  472. #endif
  473. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  474. *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
  475. return ((OS_FLAGS)0);
  476. }
  477. if (OSLockNesting > 0u) { /* See if called with scheduler locked ... */
  478. *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
  479. return ((OS_FLAGS)0);
  480. }
  481. if (rt_object_get_type(&pgrp->pFlagGrp->parent.parent) /* Validate event block type */
  482. != RT_Object_Class_Event) {
  483. *perr = OS_ERR_EVENT_TYPE;
  484. return ((OS_FLAGS)0);
  485. }
  486. result = (INT8U)(wait_type & OS_FLAG_CONSUME);
  487. if (result != (INT8U)0) { /* See if we need to consume the flags */
  488. wait_type &= (INT8U)~(INT8U)OS_FLAG_CONSUME;
  489. consume = OS_TRUE;
  490. } else {
  491. consume = OS_FALSE;
  492. }
  493. *perr = OS_ERR_NONE; /* Assume NO error until proven otherwise. */
  494. switch (wait_type) {
  495. case OS_FLAG_WAIT_SET_ALL: /* See if all required flags are set */
  496. rt_option = RT_EVENT_FLAG_AND;
  497. break;
  498. case OS_FLAG_WAIT_SET_ANY:
  499. rt_option = RT_EVENT_FLAG_OR;
  500. break;
  501. #if OS_FLAG_WAIT_CLR_EN > 0u
  502. case OS_FLAG_WAIT_CLR_ALL: /* See if all required flags are cleared */
  503. rt_option = RT_EVENT_FLAG_AND;
  504. break;
  505. case OS_FLAG_WAIT_CLR_ANY:
  506. rt_option = RT_EVENT_FLAG_OR;
  507. break;
  508. #endif
  509. default:
  510. *perr = OS_ERR_FLAG_WAIT_TYPE;
  511. return 0;
  512. }
  513. if (consume == OS_TRUE){
  514. rt_option |= RT_EVENT_FLAG_CLEAR;
  515. }
  516. OS_ENTER_CRITICAL(); /* Otherwise, must wait until event occurs */
  517. OSTCBCur->OSTCBStat |= OS_STAT_FLAG; /* Resource not available, pend on semaphore */
  518. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  519. #ifndef PKG_USING_UCOSII_WRAPPER_TINY
  520. OSTCBCur->OSTCBDly = timeout; /* Store pend timeout in TCB */
  521. OSTCBCur->OSTCBFlagsRdy = rt_thread_self()->event_set;
  522. #endif
  523. OS_EXIT_CRITICAL();
  524. if(timeout) { /* 0为永久等待 */
  525. rt_err = rt_event_recv(pgrp->pFlagGrp,
  526. flags,
  527. rt_option,
  528. timeout,
  529. &flags_rdy);
  530. if (rt_err == -RT_ETIMEOUT) {
  531. *perr = OS_ERR_TIMEOUT;
  532. } else {
  533. *perr = OS_ERR_NONE;
  534. }
  535. } else {
  536. rt_err = rt_event_recv(pgrp->pFlagGrp,
  537. flags,
  538. rt_option,
  539. RT_WAITING_FOREVER,
  540. &flags_rdy);
  541. *perr = OS_ERR_NONE;
  542. }
  543. OS_ENTER_CRITICAL();
  544. #ifndef PKG_USING_UCOSII_WRAPPER_TINY
  545. OSTCBCur->OSTCBFlagsRdy = rt_thread_self()->event_set;
  546. #endif
  547. OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set task status to ready */
  548. OS_EXIT_CRITICAL();
  549. return (flags_rdy);
  550. }
  551. /*
  552. *********************************************************************************************************
  553. * GET FLAGS WHO CAUSED TASK TO BECOME READY
  554. *
  555. * Description: This function is called to obtain the flags that caused the task to become ready to run.
  556. * In other words, this function allows you to tell "Who done it!".
  557. *
  558. * Arguments : None
  559. *
  560. * Returns : The flags that caused the task to be ready.
  561. *
  562. * Called from: Task ONLY
  563. *********************************************************************************************************
  564. */
  565. OS_FLAGS OSFlagPendGetFlagsRdy (void)
  566. {
  567. OS_FLAGS flags;
  568. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  569. OS_CPU_SR cpu_sr = 0u;
  570. #endif
  571. OS_ENTER_CRITICAL();
  572. flags = rt_thread_self()->event_set;
  573. #ifndef PKG_USING_UCOSII_WRAPPER_TINY
  574. OSTCBCur->OSTCBFlagsRdy = flags;
  575. #endif
  576. OS_EXIT_CRITICAL();
  577. return (flags);
  578. }
  579. /*
  580. *********************************************************************************************************
  581. * POST EVENT FLAG BIT(S)
  582. *
  583. * Description: This function is called to set or clear some bits in an event flag group. The bits to
  584. * set or clear are specified by a 'bit mask'.
  585. *
  586. * Arguments : pgrp is a pointer to the desired event flag group.
  587. *
  588. * flags If 'opt' (see below) is OS_FLAG_SET, each bit that is set in 'flags' will
  589. * set the corresponding bit in the event flag group. e.g. to set bits 0, 4
  590. * and 5 you would set 'flags' to:
  591. *
  592. * 0x31 (note, bit 0 is least significant bit)
  593. *
  594. * If 'opt' (see below) is OS_FLAG_CLR, each bit that is set in 'flags' will
  595. * CLEAR the corresponding bit in the event flag group. e.g. to clear bits 0,
  596. * 4 and 5 you would specify 'flags' as:
  597. *
  598. * 0x31 (note, bit 0 is least significant bit)
  599. *
  600. * opt indicates whether the flags will be:
  601. * set (OS_FLAG_SET) or
  602. * cleared (OS_FLAG_CLR)
  603. *
  604. * perr is a pointer to an error code and can be:
  605. * OS_ERR_NONE The call was successfull
  606. * OS_ERR_FLAG_INVALID_PGRP You passed a NULL pointer
  607. * OS_ERR_EVENT_TYPE You are not pointing to an event flag group
  608. * OS_ERR_FLAG_INVALID_OPT You specified an invalid option
  609. *
  610. * Returns : the new value of the event flags bits that are still set.
  611. *
  612. * Called From: Task or ISR
  613. *
  614. * WARNING(s) : 1) The execution time of this function depends on the number of tasks waiting on the event
  615. * flag group.
  616. * 2) The amount of time interrupts are DISABLED depends on the number of tasks waiting on
  617. * the event flag group.
  618. *********************************************************************************************************
  619. */
  620. OS_FLAGS OSFlagPost (OS_FLAG_GRP *pgrp,
  621. OS_FLAGS flags,
  622. INT8U opt,
  623. INT8U *perr)
  624. {
  625. rt_err_t rt_err;
  626. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  627. OS_CPU_SR cpu_sr = 0u;
  628. #endif
  629. #ifdef OS_SAFETY_CRITICAL
  630. if (perr == (INT8U *)0) {
  631. OS_SAFETY_CRITICAL_EXCEPTION();
  632. return ((OS_FLAGS)0);
  633. }
  634. #endif
  635. #if OS_ARG_CHK_EN > 0u
  636. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  637. *perr = OS_ERR_FLAG_INVALID_PGRP;
  638. return ((OS_FLAGS)0);
  639. }
  640. #endif
  641. if (rt_object_get_type(&pgrp->pFlagGrp->parent.parent) /* Validate event block type */
  642. != RT_Object_Class_Event) {
  643. *perr = OS_ERR_EVENT_TYPE;
  644. return ((OS_FLAGS)0);
  645. }
  646. rt_err = rt_event_send(pgrp->pFlagGrp, flags);
  647. if(rt_err == RT_EOK) {
  648. *perr = OS_ERR_NONE;
  649. } else {
  650. *perr = OS_ERR_FLAG_INVALID_PGRP;
  651. }
  652. OS_ENTER_CRITICAL();
  653. #ifndef PKG_USING_UCOSII_WRAPPER_TINY
  654. OSTCBCur->OSTCBFlagsRdy = rt_thread_self()->event_set;
  655. #endif
  656. flags = pgrp->pFlagGrp->set;
  657. OS_EXIT_CRITICAL();
  658. return (flags);
  659. }
  660. /*
  661. *********************************************************************************************************
  662. * QUERY EVENT FLAG
  663. *
  664. * Description: This function is used to check the value of the event flag group.
  665. *
  666. * Arguments : pgrp is a pointer to the desired event flag group.
  667. *
  668. * perr is a pointer to an error code returned to the called:
  669. * OS_ERR_NONE The call was successfull
  670. * OS_ERR_FLAG_INVALID_PGRP You passed a NULL pointer
  671. * OS_ERR_EVENT_TYPE You are not pointing to an event flag group
  672. *
  673. * Returns : The current value of the event flag group.
  674. *
  675. * Called From: Task or ISR
  676. *********************************************************************************************************
  677. */
  678. #if OS_FLAG_QUERY_EN > 0u
  679. OS_FLAGS OSFlagQuery (OS_FLAG_GRP *pgrp,
  680. INT8U *perr)
  681. {
  682. OS_FLAGS flags;
  683. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  684. OS_CPU_SR cpu_sr = 0u;
  685. #endif
  686. #ifdef OS_SAFETY_CRITICAL
  687. if (perr == (INT8U *)0) {
  688. OS_SAFETY_CRITICAL_EXCEPTION();
  689. return ((OS_FLAGS)0);
  690. }
  691. #endif
  692. #if OS_ARG_CHK_EN > 0u
  693. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  694. *perr = OS_ERR_FLAG_INVALID_PGRP;
  695. return ((OS_FLAGS)0);
  696. }
  697. #endif
  698. if (rt_object_get_type(&pgrp->pFlagGrp->parent.parent) /* Validate event block type */
  699. != RT_Object_Class_Event) {
  700. *perr = OS_ERR_EVENT_TYPE;
  701. return ((OS_FLAGS)0);
  702. }
  703. OS_ENTER_CRITICAL();
  704. flags = pgrp->pFlagGrp->set;
  705. OS_EXIT_CRITICAL();
  706. *perr = OS_ERR_NONE;
  707. return (flags); /* Return the current value of the event flags */
  708. }
  709. #endif
  710. #endif