os_flag.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. *********************************************************************************************************
  3. * uC/OS-II
  4. * The Real-Time Kernel
  5. * EVENT FLAG MANAGEMENT
  6. *
  7. * (c) Copyright 1992-2013, Micrium, Weston, FL
  8. * All Rights Reserved
  9. *
  10. * File : OS_FLAG.C
  11. * By : Jean J. Labrosse
  12. * Version : V2.92.11
  13. *
  14. * LICENSING TERMS:
  15. * ---------------
  16. * uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
  17. * If you plan on using uC/OS-II in a commercial product you need to contact Micrium to properly license
  18. * its use in your product. We provide ALL the source code for your convenience and to help you experience
  19. * uC/OS-II. The fact that the source is provided does NOT mean that you can use it without paying a
  20. * licensing fee.
  21. *********************************************************************************************************
  22. */
  23. #define MICRIUM_SOURCE
  24. #ifndef OS_MASTER_FILE
  25. #include <ucos_ii.h>
  26. #endif
  27. #if (OS_FLAG_EN > 0u) && (OS_MAX_FLAGS > 0u)
  28. /*
  29. *********************************************************************************************************
  30. * LOCAL PROTOTYPES
  31. *********************************************************************************************************
  32. */
  33. static void OS_FlagBlock(OS_FLAG_GRP *pgrp, OS_FLAG_NODE *pnode, OS_FLAGS flags, INT8U wait_type, INT32U timeout);
  34. static BOOLEAN OS_FlagTaskRdy(OS_FLAG_NODE *pnode, OS_FLAGS flags_rdy, INT8U pend_stat);
  35. /*$PAGE*/
  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. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  97. OS_CPU_SR cpu_sr = 0u;
  98. #endif
  99. #ifdef OS_SAFETY_CRITICAL
  100. if (perr == (INT8U *)0) {
  101. OS_SAFETY_CRITICAL_EXCEPTION();
  102. return ((OS_FLAGS)0);
  103. }
  104. #endif
  105. #if OS_ARG_CHK_EN > 0u
  106. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  107. *perr = OS_ERR_FLAG_INVALID_PGRP;
  108. return ((OS_FLAGS)0);
  109. }
  110. #endif
  111. if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Validate event block type */
  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. /*$PAGE*/
  123. *perr = OS_ERR_NONE; /* Assume NO error until proven otherwise. */
  124. OS_ENTER_CRITICAL();
  125. switch (wait_type) {
  126. case OS_FLAG_WAIT_SET_ALL: /* See if all required flags are set */
  127. flags_rdy = (OS_FLAGS)(pgrp->OSFlagFlags & flags); /* Extract only the bits we want */
  128. if (flags_rdy == flags) { /* Must match ALL the bits that we want */
  129. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  130. pgrp->OSFlagFlags &= (OS_FLAGS)~flags_rdy; /* Clear ONLY the flags we wanted */
  131. }
  132. } else {
  133. *perr = OS_ERR_FLAG_NOT_RDY;
  134. }
  135. OS_EXIT_CRITICAL();
  136. break;
  137. case OS_FLAG_WAIT_SET_ANY:
  138. flags_rdy = (OS_FLAGS)(pgrp->OSFlagFlags & flags); /* Extract only the bits we want */
  139. if (flags_rdy != (OS_FLAGS)0) { /* See if any flag set */
  140. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  141. pgrp->OSFlagFlags &= (OS_FLAGS)~flags_rdy; /* Clear ONLY the flags we got */
  142. }
  143. } else {
  144. *perr = OS_ERR_FLAG_NOT_RDY;
  145. }
  146. OS_EXIT_CRITICAL();
  147. break;
  148. #if OS_FLAG_WAIT_CLR_EN > 0u
  149. case OS_FLAG_WAIT_CLR_ALL: /* See if all required flags are cleared */
  150. flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags; /* Extract only the bits we want */
  151. if (flags_rdy == flags) { /* Must match ALL the bits that we want */
  152. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  153. pgrp->OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we wanted */
  154. }
  155. } else {
  156. *perr = OS_ERR_FLAG_NOT_RDY;
  157. }
  158. OS_EXIT_CRITICAL();
  159. break;
  160. case OS_FLAG_WAIT_CLR_ANY:
  161. flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags; /* Extract only the bits we want */
  162. if (flags_rdy != (OS_FLAGS)0) { /* See if any flag cleared */
  163. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  164. pgrp->OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we got */
  165. }
  166. } else {
  167. *perr = OS_ERR_FLAG_NOT_RDY;
  168. }
  169. OS_EXIT_CRITICAL();
  170. break;
  171. #endif
  172. default:
  173. OS_EXIT_CRITICAL();
  174. flags_rdy = (OS_FLAGS)0;
  175. *perr = OS_ERR_FLAG_WAIT_TYPE;
  176. break;
  177. }
  178. return (flags_rdy);
  179. }
  180. #endif
  181. /*$PAGE*/
  182. /*
  183. *********************************************************************************************************
  184. * CREATE AN EVENT FLAG
  185. *
  186. * Description: This function is called to create an event flag group.
  187. *
  188. * Arguments : flags Contains the initial value to store in the event flag group.
  189. *
  190. * perr is a pointer to an error code which will be returned to your application:
  191. * OS_ERR_NONE if the call was successful.
  192. * OS_ERR_CREATE_ISR if you attempted to create an Event Flag from an
  193. * ISR.
  194. * OS_ERR_FLAG_GRP_DEPLETED if there are no more event flag groups
  195. *
  196. * Returns : A pointer to an event flag group or a NULL pointer if no more groups are available.
  197. *
  198. * Called from: Task ONLY
  199. *********************************************************************************************************
  200. */
  201. OS_FLAG_GRP *OSFlagCreate (OS_FLAGS flags,
  202. INT8U *perr)
  203. {
  204. OS_FLAG_GRP *pgrp;
  205. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  206. OS_CPU_SR cpu_sr = 0u;
  207. #endif
  208. #ifdef OS_SAFETY_CRITICAL
  209. if (perr == (INT8U *)0) {
  210. OS_SAFETY_CRITICAL_EXCEPTION();
  211. return ((OS_FLAG_GRP *)0);
  212. }
  213. #endif
  214. #ifdef OS_SAFETY_CRITICAL_IEC61508
  215. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  216. OS_SAFETY_CRITICAL_EXCEPTION();
  217. return ((OS_FLAG_GRP *)0);
  218. }
  219. #endif
  220. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  221. *perr = OS_ERR_CREATE_ISR; /* ... can't CREATE from an ISR */
  222. return ((OS_FLAG_GRP *)0);
  223. }
  224. OS_ENTER_CRITICAL();
  225. pgrp = OSFlagFreeList; /* Get next free event flag */
  226. if (pgrp != (OS_FLAG_GRP *)0) { /* See if we have event flag groups available */
  227. /* Adjust free list */
  228. OSFlagFreeList = (OS_FLAG_GRP *)OSFlagFreeList->OSFlagWaitList;
  229. pgrp->OSFlagType = OS_EVENT_TYPE_FLAG; /* Set to event flag group type */
  230. pgrp->OSFlagFlags = flags; /* Set to desired initial value */
  231. pgrp->OSFlagWaitList = (void *)0; /* Clear list of tasks waiting on flags */
  232. #if OS_FLAG_NAME_EN > 0u
  233. pgrp->OSFlagName = (INT8U *)(void *)"?";
  234. #endif
  235. OS_EXIT_CRITICAL();
  236. *perr = OS_ERR_NONE;
  237. } else {
  238. OS_EXIT_CRITICAL();
  239. *perr = OS_ERR_FLAG_GRP_DEPLETED;
  240. }
  241. return (pgrp); /* Return pointer to event flag group */
  242. }
  243. /*$PAGE*/
  244. /*
  245. *********************************************************************************************************
  246. * DELETE AN EVENT FLAG GROUP
  247. *
  248. * Description: This function deletes an event flag group and readies all tasks pending on the event flag
  249. * group.
  250. *
  251. * Arguments : pgrp is a pointer to the desired event flag group.
  252. *
  253. * opt determines delete options as follows:
  254. * opt == OS_DEL_NO_PEND Deletes the event flag group ONLY if no task pending
  255. * opt == OS_DEL_ALWAYS Deletes the event flag group even if tasks are
  256. * waiting. In this case, all the tasks pending will be
  257. * readied.
  258. *
  259. * perr is a pointer to an error code that can contain one of the following values:
  260. * OS_ERR_NONE The call was successful and the event flag group was
  261. * deleted
  262. * OS_ERR_DEL_ISR If you attempted to delete the event flag group from
  263. * an ISR
  264. * OS_ERR_FLAG_INVALID_PGRP If 'pgrp' is a NULL pointer.
  265. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to an event flag group
  266. * OS_ERR_INVALID_OPT An invalid option was specified
  267. * OS_ERR_TASK_WAITING One or more tasks were waiting on the event flag
  268. * group.
  269. *
  270. * Returns : pgrp upon error
  271. * (OS_EVENT *)0 if the event flag group was successfully deleted.
  272. *
  273. * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
  274. * the event flag group MUST check the return code of OSFlagAccept() and OSFlagPend().
  275. * 2) This call can potentially disable interrupts for a long time. The interrupt disable
  276. * time is directly proportional to the number of tasks waiting on the event flag group.
  277. * 3) All tasks that were waiting for the event flag will be readied and returned an
  278. * OS_ERR_PEND_ABORT if OSFlagDel() was called with OS_DEL_ALWAYS
  279. *********************************************************************************************************
  280. */
  281. #if OS_FLAG_DEL_EN > 0u
  282. OS_FLAG_GRP *OSFlagDel (OS_FLAG_GRP *pgrp,
  283. INT8U opt,
  284. INT8U *perr)
  285. {
  286. BOOLEAN tasks_waiting;
  287. OS_FLAG_NODE *pnode;
  288. OS_FLAG_GRP *pgrp_return;
  289. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  290. OS_CPU_SR cpu_sr = 0u;
  291. #endif
  292. #ifdef OS_SAFETY_CRITICAL
  293. if (perr == (INT8U *)0) {
  294. OS_SAFETY_CRITICAL_EXCEPTION();
  295. return ((OS_FLAG_GRP *)0);
  296. }
  297. #endif
  298. #if OS_ARG_CHK_EN > 0u
  299. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  300. *perr = OS_ERR_FLAG_INVALID_PGRP;
  301. return (pgrp);
  302. }
  303. #endif
  304. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  305. *perr = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
  306. return (pgrp);
  307. }
  308. if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Validate event group type */
  309. *perr = OS_ERR_EVENT_TYPE;
  310. return (pgrp);
  311. }
  312. OS_ENTER_CRITICAL();
  313. if (pgrp->OSFlagWaitList != (void *)0) { /* See if any tasks waiting on event flags */
  314. tasks_waiting = OS_TRUE; /* Yes */
  315. } else {
  316. tasks_waiting = OS_FALSE; /* No */
  317. }
  318. switch (opt) {
  319. case OS_DEL_NO_PEND: /* Delete group if no task waiting */
  320. if (tasks_waiting == OS_FALSE) {
  321. #if OS_FLAG_NAME_EN > 0u
  322. pgrp->OSFlagName = (INT8U *)(void *)"?";
  323. #endif
  324. pgrp->OSFlagType = OS_EVENT_TYPE_UNUSED;
  325. pgrp->OSFlagWaitList = (void *)OSFlagFreeList; /* Return group to free list */
  326. pgrp->OSFlagFlags = (OS_FLAGS)0;
  327. OSFlagFreeList = pgrp;
  328. OS_EXIT_CRITICAL();
  329. *perr = OS_ERR_NONE;
  330. pgrp_return = (OS_FLAG_GRP *)0; /* Event Flag Group has been deleted */
  331. } else {
  332. OS_EXIT_CRITICAL();
  333. *perr = OS_ERR_TASK_WAITING;
  334. pgrp_return = pgrp;
  335. }
  336. break;
  337. case OS_DEL_ALWAYS: /* Always delete the event flag group */
  338. pnode = (OS_FLAG_NODE *)pgrp->OSFlagWaitList;
  339. while (pnode != (OS_FLAG_NODE *)0) { /* Ready ALL tasks waiting for flags */
  340. (void)OS_FlagTaskRdy(pnode, (OS_FLAGS)0, OS_STAT_PEND_ABORT);
  341. pnode = (OS_FLAG_NODE *)pnode->OSFlagNodeNext;
  342. }
  343. #if OS_FLAG_NAME_EN > 0u
  344. pgrp->OSFlagName = (INT8U *)(void *)"?";
  345. #endif
  346. pgrp->OSFlagType = OS_EVENT_TYPE_UNUSED;
  347. pgrp->OSFlagWaitList = (void *)OSFlagFreeList;/* Return group to free list */
  348. pgrp->OSFlagFlags = (OS_FLAGS)0;
  349. OSFlagFreeList = pgrp;
  350. OS_EXIT_CRITICAL();
  351. if (tasks_waiting == OS_TRUE) { /* Reschedule only if task(s) were waiting */
  352. OS_Sched(); /* Find highest priority task ready to run */
  353. }
  354. *perr = OS_ERR_NONE;
  355. pgrp_return = (OS_FLAG_GRP *)0; /* Event Flag Group has been deleted */
  356. break;
  357. default:
  358. OS_EXIT_CRITICAL();
  359. *perr = OS_ERR_INVALID_OPT;
  360. pgrp_return = pgrp;
  361. break;
  362. }
  363. return (pgrp_return);
  364. }
  365. #endif
  366. /*$PAGE*/
  367. /*
  368. *********************************************************************************************************
  369. * GET THE NAME OF AN EVENT FLAG GROUP
  370. *
  371. * Description: This function is used to obtain the name assigned to an event flag group
  372. *
  373. * Arguments : pgrp is a pointer to the event flag group.
  374. *
  375. * pname is pointer to a pointer to an ASCII string that will receive the name of the event flag
  376. * group.
  377. *
  378. * perr is a pointer to an error code that can contain one of the following values:
  379. *
  380. * OS_ERR_NONE if the requested task is resumed
  381. * OS_ERR_EVENT_TYPE if 'pevent' is not pointing to an event flag group
  382. * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
  383. * OS_ERR_FLAG_INVALID_PGRP if you passed a NULL pointer for 'pgrp'
  384. * OS_ERR_NAME_GET_ISR if you called this function from an ISR
  385. *
  386. * Returns : The length of the string or 0 if the 'pgrp' is a NULL pointer.
  387. *********************************************************************************************************
  388. */
  389. #if OS_FLAG_NAME_EN > 0u
  390. INT8U OSFlagNameGet (OS_FLAG_GRP *pgrp,
  391. INT8U **pname,
  392. INT8U *perr)
  393. {
  394. INT8U len;
  395. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  396. OS_CPU_SR cpu_sr = 0u;
  397. #endif
  398. #ifdef OS_SAFETY_CRITICAL
  399. if (perr == (INT8U *)0) {
  400. OS_SAFETY_CRITICAL_EXCEPTION();
  401. return (0u);
  402. }
  403. #endif
  404. #if OS_ARG_CHK_EN > 0u
  405. if (pgrp == (OS_FLAG_GRP *)0) { /* Is 'pgrp' a NULL pointer? */
  406. *perr = OS_ERR_FLAG_INVALID_PGRP;
  407. return (0u);
  408. }
  409. if (pname == (INT8U **)0) { /* Is 'pname' a NULL pointer? */
  410. *perr = OS_ERR_PNAME_NULL;
  411. return (0u);
  412. }
  413. #endif
  414. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  415. *perr = OS_ERR_NAME_GET_ISR;
  416. return (0u);
  417. }
  418. OS_ENTER_CRITICAL();
  419. if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {
  420. OS_EXIT_CRITICAL();
  421. *perr = OS_ERR_EVENT_TYPE;
  422. return (0u);
  423. }
  424. *pname = pgrp->OSFlagName;
  425. len = OS_StrLen(*pname);
  426. OS_EXIT_CRITICAL();
  427. *perr = OS_ERR_NONE;
  428. return (len);
  429. }
  430. #endif
  431. /*$PAGE*/
  432. /*
  433. *********************************************************************************************************
  434. * ASSIGN A NAME TO AN EVENT FLAG GROUP
  435. *
  436. * Description: This function assigns a name to an event flag group.
  437. *
  438. * Arguments : pgrp is a pointer to the event flag group.
  439. *
  440. * pname is a pointer to an ASCII string that will be used as the name of the event flag
  441. * group.
  442. *
  443. * perr is a pointer to an error code that can contain one of the following values:
  444. *
  445. * OS_ERR_NONE if the requested task is resumed
  446. * OS_ERR_EVENT_TYPE if 'pevent' is not pointing to an event flag group
  447. * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
  448. * OS_ERR_FLAG_INVALID_PGRP if you passed a NULL pointer for 'pgrp'
  449. * OS_ERR_NAME_SET_ISR if you called this function from an ISR
  450. *
  451. * Returns : None
  452. *********************************************************************************************************
  453. */
  454. #if OS_FLAG_NAME_EN > 0u
  455. void OSFlagNameSet (OS_FLAG_GRP *pgrp,
  456. INT8U *pname,
  457. INT8U *perr)
  458. {
  459. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  460. OS_CPU_SR cpu_sr = 0u;
  461. #endif
  462. #ifdef OS_SAFETY_CRITICAL
  463. if (perr == (INT8U *)0) {
  464. OS_SAFETY_CRITICAL_EXCEPTION();
  465. return;
  466. }
  467. #endif
  468. #if OS_ARG_CHK_EN > 0u
  469. if (pgrp == (OS_FLAG_GRP *)0) { /* Is 'pgrp' a NULL pointer? */
  470. *perr = OS_ERR_FLAG_INVALID_PGRP;
  471. return;
  472. }
  473. if (pname == (INT8U *)0) { /* Is 'pname' a NULL pointer? */
  474. *perr = OS_ERR_PNAME_NULL;
  475. return;
  476. }
  477. #endif
  478. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  479. *perr = OS_ERR_NAME_SET_ISR;
  480. return;
  481. }
  482. OS_ENTER_CRITICAL();
  483. if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {
  484. OS_EXIT_CRITICAL();
  485. *perr = OS_ERR_EVENT_TYPE;
  486. return;
  487. }
  488. pgrp->OSFlagName = pname;
  489. OS_EXIT_CRITICAL();
  490. *perr = OS_ERR_NONE;
  491. return;
  492. }
  493. #endif
  494. /*$PAGE*/
  495. /*
  496. *********************************************************************************************************
  497. * WAIT ON AN EVENT FLAG GROUP
  498. *
  499. * Description: This function is called to wait for a combination of bits to be set in an event flag
  500. * group. Your application can wait for ANY bit to be set or ALL bits to be set.
  501. *
  502. * Arguments : pgrp is a pointer to the desired event flag group.
  503. *
  504. * flags Is a bit pattern indicating which bit(s) (i.e. flags) you wish to wait for.
  505. * The bits you want are specified by setting the corresponding bits in
  506. * 'flags'. e.g. if your application wants to wait for bits 0 and 1 then
  507. * 'flags' would contain 0x03.
  508. *
  509. * wait_type specifies whether you want ALL bits to be set or ANY of the bits to be set.
  510. * You can specify the following argument:
  511. *
  512. * OS_FLAG_WAIT_CLR_ALL You will wait for ALL bits in 'mask' to be clear (0)
  513. * OS_FLAG_WAIT_SET_ALL You will wait for ALL bits in 'mask' to be set (1)
  514. * OS_FLAG_WAIT_CLR_ANY You will wait for ANY bit in 'mask' to be clear (0)
  515. * OS_FLAG_WAIT_SET_ANY You will wait for ANY bit in 'mask' to be set (1)
  516. *
  517. * NOTE: Add OS_FLAG_CONSUME if you want the event flag to be 'consumed' by
  518. * the call. Example, to wait for any flag in a group AND then clear
  519. * the flags that are present, set 'wait_type' to:
  520. *
  521. * OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME
  522. *
  523. * timeout is an optional timeout (in clock ticks) that your task will wait for the
  524. * desired bit combination. If you specify 0, however, your task will wait
  525. * forever at the specified event flag group or, until a message arrives.
  526. *
  527. * perr is a pointer to an error code and can be:
  528. * OS_ERR_NONE The desired bits have been set within the specified
  529. * 'timeout'.
  530. * OS_ERR_PEND_ISR If you tried to PEND from an ISR
  531. * OS_ERR_FLAG_INVALID_PGRP If 'pgrp' is a NULL pointer.
  532. * OS_ERR_EVENT_TYPE You are not pointing to an event flag group
  533. * OS_ERR_TIMEOUT The bit(s) have not been set in the specified
  534. * 'timeout'.
  535. * OS_ERR_PEND_ABORT The wait on the flag was aborted.
  536. * OS_ERR_FLAG_WAIT_TYPE You didn't specify a proper 'wait_type' argument.
  537. *
  538. * Returns : The flags in the event flag group that made the task ready or, 0 if a timeout or an error
  539. * occurred.
  540. *
  541. * Called from: Task ONLY
  542. *
  543. * Note(s) : 1) IMPORTANT, the behavior of this function has changed from PREVIOUS versions. The
  544. * function NOW returns the flags that were ready INSTEAD of the current state of the
  545. * event flags.
  546. *********************************************************************************************************
  547. */
  548. OS_FLAGS OSFlagPend (OS_FLAG_GRP *pgrp,
  549. OS_FLAGS flags,
  550. INT8U wait_type,
  551. INT32U timeout,
  552. INT8U *perr)
  553. {
  554. OS_FLAG_NODE node;
  555. OS_FLAGS flags_rdy;
  556. INT8U result;
  557. INT8U pend_stat;
  558. BOOLEAN consume;
  559. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  560. OS_CPU_SR cpu_sr = 0u;
  561. #endif
  562. #ifdef OS_SAFETY_CRITICAL
  563. if (perr == (INT8U *)0) {
  564. OS_SAFETY_CRITICAL_EXCEPTION();
  565. return ((OS_FLAGS)0);
  566. }
  567. #endif
  568. #if OS_ARG_CHK_EN > 0u
  569. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  570. *perr = OS_ERR_FLAG_INVALID_PGRP;
  571. return ((OS_FLAGS)0);
  572. }
  573. #endif
  574. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  575. *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
  576. return ((OS_FLAGS)0);
  577. }
  578. if (OSLockNesting > 0u) { /* See if called with scheduler locked ... */
  579. *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
  580. return ((OS_FLAGS)0);
  581. }
  582. if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Validate event block type */
  583. *perr = OS_ERR_EVENT_TYPE;
  584. return ((OS_FLAGS)0);
  585. }
  586. result = (INT8U)(wait_type & OS_FLAG_CONSUME);
  587. if (result != (INT8U)0) { /* See if we need to consume the flags */
  588. wait_type &= (INT8U)~(INT8U)OS_FLAG_CONSUME;
  589. consume = OS_TRUE;
  590. } else {
  591. consume = OS_FALSE;
  592. }
  593. /*$PAGE*/
  594. OS_ENTER_CRITICAL();
  595. switch (wait_type) {
  596. case OS_FLAG_WAIT_SET_ALL: /* See if all required flags are set */
  597. flags_rdy = (OS_FLAGS)(pgrp->OSFlagFlags & flags); /* Extract only the bits we want */
  598. if (flags_rdy == flags) { /* Must match ALL the bits that we want */
  599. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  600. pgrp->OSFlagFlags &= (OS_FLAGS)~flags_rdy; /* Clear ONLY the flags we wanted */
  601. }
  602. OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */
  603. OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */
  604. *perr = OS_ERR_NONE;
  605. return (flags_rdy);
  606. } else { /* Block task until events occur or timeout */
  607. OS_FlagBlock(pgrp, &node, flags, wait_type, timeout);
  608. OS_EXIT_CRITICAL();
  609. }
  610. break;
  611. case OS_FLAG_WAIT_SET_ANY:
  612. flags_rdy = (OS_FLAGS)(pgrp->OSFlagFlags & flags); /* Extract only the bits we want */
  613. if (flags_rdy != (OS_FLAGS)0) { /* See if any flag set */
  614. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  615. pgrp->OSFlagFlags &= (OS_FLAGS)~flags_rdy; /* Clear ONLY the flags that we got */
  616. }
  617. OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */
  618. OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */
  619. *perr = OS_ERR_NONE;
  620. return (flags_rdy);
  621. } else { /* Block task until events occur or timeout */
  622. OS_FlagBlock(pgrp, &node, flags, wait_type, timeout);
  623. OS_EXIT_CRITICAL();
  624. }
  625. break;
  626. #if OS_FLAG_WAIT_CLR_EN > 0u
  627. case OS_FLAG_WAIT_CLR_ALL: /* See if all required flags are cleared */
  628. flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags; /* Extract only the bits we want */
  629. if (flags_rdy == flags) { /* Must match ALL the bits that we want */
  630. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  631. pgrp->OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we wanted */
  632. }
  633. OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */
  634. OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */
  635. *perr = OS_ERR_NONE;
  636. return (flags_rdy);
  637. } else { /* Block task until events occur or timeout */
  638. OS_FlagBlock(pgrp, &node, flags, wait_type, timeout);
  639. OS_EXIT_CRITICAL();
  640. }
  641. break;
  642. case OS_FLAG_WAIT_CLR_ANY:
  643. flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags; /* Extract only the bits we want */
  644. if (flags_rdy != (OS_FLAGS)0) { /* See if any flag cleared */
  645. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  646. pgrp->OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we got */
  647. }
  648. OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */
  649. OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */
  650. *perr = OS_ERR_NONE;
  651. return (flags_rdy);
  652. } else { /* Block task until events occur or timeout */
  653. OS_FlagBlock(pgrp, &node, flags, wait_type, timeout);
  654. OS_EXIT_CRITICAL();
  655. }
  656. break;
  657. #endif
  658. default:
  659. OS_EXIT_CRITICAL();
  660. flags_rdy = (OS_FLAGS)0;
  661. *perr = OS_ERR_FLAG_WAIT_TYPE;
  662. return (flags_rdy);
  663. }
  664. /*$PAGE*/
  665. OS_Sched(); /* Find next HPT ready to run */
  666. OS_ENTER_CRITICAL();
  667. if (OSTCBCur->OSTCBStatPend != OS_STAT_PEND_OK) { /* Have we timed-out or aborted? */
  668. pend_stat = OSTCBCur->OSTCBStatPend;
  669. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  670. OS_FlagUnlink(&node);
  671. OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Yes, make task ready-to-run */
  672. OS_EXIT_CRITICAL();
  673. flags_rdy = (OS_FLAGS)0;
  674. switch (pend_stat) {
  675. case OS_STAT_PEND_ABORT:
  676. *perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted waiting */
  677. break;
  678. case OS_STAT_PEND_TO:
  679. default:
  680. *perr = OS_ERR_TIMEOUT; /* Indicate that we timed-out waiting */
  681. break;
  682. }
  683. return (flags_rdy);
  684. }
  685. flags_rdy = OSTCBCur->OSTCBFlagsRdy;
  686. if (consume == OS_TRUE) { /* See if we need to consume the flags */
  687. switch (wait_type) {
  688. case OS_FLAG_WAIT_SET_ALL:
  689. case OS_FLAG_WAIT_SET_ANY: /* Clear ONLY the flags we got */
  690. pgrp->OSFlagFlags &= (OS_FLAGS)~flags_rdy;
  691. break;
  692. #if OS_FLAG_WAIT_CLR_EN > 0u
  693. case OS_FLAG_WAIT_CLR_ALL:
  694. case OS_FLAG_WAIT_CLR_ANY: /* Set ONLY the flags we got */
  695. pgrp->OSFlagFlags |= flags_rdy;
  696. break;
  697. #endif
  698. default:
  699. OS_EXIT_CRITICAL();
  700. *perr = OS_ERR_FLAG_WAIT_TYPE;
  701. return ((OS_FLAGS)0);
  702. }
  703. }
  704. OS_EXIT_CRITICAL();
  705. *perr = OS_ERR_NONE; /* Event(s) must have occurred */
  706. return (flags_rdy);
  707. }
  708. /*$PAGE*/
  709. /*
  710. *********************************************************************************************************
  711. * GET FLAGS WHO CAUSED TASK TO BECOME READY
  712. *
  713. * Description: This function is called to obtain the flags that caused the task to become ready to run.
  714. * In other words, this function allows you to tell "Who done it!".
  715. *
  716. * Arguments : None
  717. *
  718. * Returns : The flags that caused the task to be ready.
  719. *
  720. * Called from: Task ONLY
  721. *********************************************************************************************************
  722. */
  723. OS_FLAGS OSFlagPendGetFlagsRdy (void)
  724. {
  725. OS_FLAGS flags;
  726. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  727. OS_CPU_SR cpu_sr = 0u;
  728. #endif
  729. OS_ENTER_CRITICAL();
  730. flags = OSTCBCur->OSTCBFlagsRdy;
  731. OS_EXIT_CRITICAL();
  732. return (flags);
  733. }
  734. /*$PAGE*/
  735. /*
  736. *********************************************************************************************************
  737. * POST EVENT FLAG BIT(S)
  738. *
  739. * Description: This function is called to set or clear some bits in an event flag group. The bits to
  740. * set or clear are specified by a 'bit mask'.
  741. *
  742. * Arguments : pgrp is a pointer to the desired event flag group.
  743. *
  744. * flags If 'opt' (see below) is OS_FLAG_SET, each bit that is set in 'flags' will
  745. * set the corresponding bit in the event flag group. e.g. to set bits 0, 4
  746. * and 5 you would set 'flags' to:
  747. *
  748. * 0x31 (note, bit 0 is least significant bit)
  749. *
  750. * If 'opt' (see below) is OS_FLAG_CLR, each bit that is set in 'flags' will
  751. * CLEAR the corresponding bit in the event flag group. e.g. to clear bits 0,
  752. * 4 and 5 you would specify 'flags' as:
  753. *
  754. * 0x31 (note, bit 0 is least significant bit)
  755. *
  756. * opt indicates whether the flags will be:
  757. * set (OS_FLAG_SET) or
  758. * cleared (OS_FLAG_CLR)
  759. *
  760. * perr is a pointer to an error code and can be:
  761. * OS_ERR_NONE The call was successfull
  762. * OS_ERR_FLAG_INVALID_PGRP You passed a NULL pointer
  763. * OS_ERR_EVENT_TYPE You are not pointing to an event flag group
  764. * OS_ERR_FLAG_INVALID_OPT You specified an invalid option
  765. *
  766. * Returns : the new value of the event flags bits that are still set.
  767. *
  768. * Called From: Task or ISR
  769. *
  770. * WARNING(s) : 1) The execution time of this function depends on the number of tasks waiting on the event
  771. * flag group.
  772. * 2) The amount of time interrupts are DISABLED depends on the number of tasks waiting on
  773. * the event flag group.
  774. *********************************************************************************************************
  775. */
  776. OS_FLAGS OSFlagPost (OS_FLAG_GRP *pgrp,
  777. OS_FLAGS flags,
  778. INT8U opt,
  779. INT8U *perr)
  780. {
  781. OS_FLAG_NODE *pnode;
  782. BOOLEAN sched;
  783. OS_FLAGS flags_cur;
  784. OS_FLAGS flags_rdy;
  785. BOOLEAN rdy;
  786. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  787. OS_CPU_SR cpu_sr = 0u;
  788. #endif
  789. #ifdef OS_SAFETY_CRITICAL
  790. if (perr == (INT8U *)0) {
  791. OS_SAFETY_CRITICAL_EXCEPTION();
  792. return ((OS_FLAGS)0);
  793. }
  794. #endif
  795. #if OS_ARG_CHK_EN > 0u
  796. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  797. *perr = OS_ERR_FLAG_INVALID_PGRP;
  798. return ((OS_FLAGS)0);
  799. }
  800. #endif
  801. if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Make sure we are pointing to an event flag grp */
  802. *perr = OS_ERR_EVENT_TYPE;
  803. return ((OS_FLAGS)0);
  804. }
  805. /*$PAGE*/
  806. OS_ENTER_CRITICAL();
  807. switch (opt) {
  808. case OS_FLAG_CLR:
  809. pgrp->OSFlagFlags &= (OS_FLAGS)~flags; /* Clear the flags specified in the group */
  810. break;
  811. case OS_FLAG_SET:
  812. pgrp->OSFlagFlags |= flags; /* Set the flags specified in the group */
  813. break;
  814. default:
  815. OS_EXIT_CRITICAL(); /* INVALID option */
  816. *perr = OS_ERR_FLAG_INVALID_OPT;
  817. return ((OS_FLAGS)0);
  818. }
  819. sched = OS_FALSE; /* Indicate that we don't need rescheduling */
  820. pnode = (OS_FLAG_NODE *)pgrp->OSFlagWaitList;
  821. while (pnode != (OS_FLAG_NODE *)0) { /* Go through all tasks waiting on event flag(s) */
  822. switch (pnode->OSFlagNodeWaitType) {
  823. case OS_FLAG_WAIT_SET_ALL: /* See if all req. flags are set for current node */
  824. flags_rdy = (OS_FLAGS)(pgrp->OSFlagFlags & pnode->OSFlagNodeFlags);
  825. if (flags_rdy == pnode->OSFlagNodeFlags) { /* Make task RTR, event(s) Rx'd */
  826. rdy = OS_FlagTaskRdy(pnode, flags_rdy, OS_STAT_PEND_OK);
  827. if (rdy == OS_TRUE) {
  828. sched = OS_TRUE; /* When done we will reschedule */
  829. }
  830. }
  831. break;
  832. case OS_FLAG_WAIT_SET_ANY: /* See if any flag set */
  833. flags_rdy = (OS_FLAGS)(pgrp->OSFlagFlags & pnode->OSFlagNodeFlags);
  834. if (flags_rdy != (OS_FLAGS)0) { /* Make task RTR, event(s) Rx'd */
  835. rdy = OS_FlagTaskRdy(pnode, flags_rdy, OS_STAT_PEND_OK);
  836. if (rdy == OS_TRUE) {
  837. sched = OS_TRUE; /* When done we will reschedule */
  838. }
  839. }
  840. break;
  841. #if OS_FLAG_WAIT_CLR_EN > 0u
  842. case OS_FLAG_WAIT_CLR_ALL: /* See if all req. flags are set for current node */
  843. flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
  844. if (flags_rdy == pnode->OSFlagNodeFlags) { /* Make task RTR, event(s) Rx'd */
  845. rdy = OS_FlagTaskRdy(pnode, flags_rdy, OS_STAT_PEND_OK);
  846. if (rdy == OS_TRUE) {
  847. sched = OS_TRUE; /* When done we will reschedule */
  848. }
  849. }
  850. break;
  851. case OS_FLAG_WAIT_CLR_ANY: /* See if any flag set */
  852. flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
  853. if (flags_rdy != (OS_FLAGS)0) { /* Make task RTR, event(s) Rx'd */
  854. rdy = OS_FlagTaskRdy(pnode, flags_rdy, OS_STAT_PEND_OK);
  855. if (rdy == OS_TRUE) {
  856. sched = OS_TRUE; /* When done we will reschedule */
  857. }
  858. }
  859. break;
  860. #endif
  861. default:
  862. OS_EXIT_CRITICAL();
  863. *perr = OS_ERR_FLAG_WAIT_TYPE;
  864. return ((OS_FLAGS)0);
  865. }
  866. pnode = (OS_FLAG_NODE *)pnode->OSFlagNodeNext; /* Point to next task waiting for event flag(s) */
  867. }
  868. OS_EXIT_CRITICAL();
  869. if (sched == OS_TRUE) {
  870. OS_Sched();
  871. }
  872. OS_ENTER_CRITICAL();
  873. flags_cur = pgrp->OSFlagFlags;
  874. OS_EXIT_CRITICAL();
  875. *perr = OS_ERR_NONE;
  876. return (flags_cur);
  877. }
  878. /*$PAGE*/
  879. /*
  880. *********************************************************************************************************
  881. * QUERY EVENT FLAG
  882. *
  883. * Description: This function is used to check the value of the event flag group.
  884. *
  885. * Arguments : pgrp is a pointer to the desired event flag group.
  886. *
  887. * perr is a pointer to an error code returned to the called:
  888. * OS_ERR_NONE The call was successfull
  889. * OS_ERR_FLAG_INVALID_PGRP You passed a NULL pointer
  890. * OS_ERR_EVENT_TYPE You are not pointing to an event flag group
  891. *
  892. * Returns : The current value of the event flag group.
  893. *
  894. * Called From: Task or ISR
  895. *********************************************************************************************************
  896. */
  897. #if OS_FLAG_QUERY_EN > 0u
  898. OS_FLAGS OSFlagQuery (OS_FLAG_GRP *pgrp,
  899. INT8U *perr)
  900. {
  901. OS_FLAGS flags;
  902. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  903. OS_CPU_SR cpu_sr = 0u;
  904. #endif
  905. #ifdef OS_SAFETY_CRITICAL
  906. if (perr == (INT8U *)0) {
  907. OS_SAFETY_CRITICAL_EXCEPTION();
  908. return ((OS_FLAGS)0);
  909. }
  910. #endif
  911. #if OS_ARG_CHK_EN > 0u
  912. if (pgrp == (OS_FLAG_GRP *)0) { /* Validate 'pgrp' */
  913. *perr = OS_ERR_FLAG_INVALID_PGRP;
  914. return ((OS_FLAGS)0);
  915. }
  916. #endif
  917. if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Validate event block type */
  918. *perr = OS_ERR_EVENT_TYPE;
  919. return ((OS_FLAGS)0);
  920. }
  921. OS_ENTER_CRITICAL();
  922. flags = pgrp->OSFlagFlags;
  923. OS_EXIT_CRITICAL();
  924. *perr = OS_ERR_NONE;
  925. return (flags); /* Return the current value of the event flags */
  926. }
  927. #endif
  928. /*$PAGE*/
  929. /*
  930. *********************************************************************************************************
  931. * SUSPEND TASK UNTIL EVENT FLAG(s) RECEIVED OR TIMEOUT OCCURS
  932. *
  933. * Description: This function is internal to uC/OS-II and is used to put a task to sleep until the desired
  934. * event flag bit(s) are set.
  935. *
  936. * Arguments : pgrp is a pointer to the desired event flag group.
  937. *
  938. * pnode is a pointer to a structure which contains data about the task waiting for
  939. * event flag bit(s) to be set.
  940. *
  941. * flags Is a bit pattern indicating which bit(s) (i.e. flags) you wish to check.
  942. * The bits you want are specified by setting the corresponding bits in
  943. * 'flags'. e.g. if your application wants to wait for bits 0 and 1 then
  944. * 'flags' would contain 0x03.
  945. *
  946. * wait_type specifies whether you want ALL bits to be set/cleared or ANY of the bits
  947. * to be set/cleared.
  948. * You can specify the following argument:
  949. *
  950. * OS_FLAG_WAIT_CLR_ALL You will check ALL bits in 'mask' to be clear (0)
  951. * OS_FLAG_WAIT_CLR_ANY You will check ANY bit in 'mask' to be clear (0)
  952. * OS_FLAG_WAIT_SET_ALL You will check ALL bits in 'mask' to be set (1)
  953. * OS_FLAG_WAIT_SET_ANY You will check ANY bit in 'mask' to be set (1)
  954. *
  955. * timeout is the desired amount of time that the task will wait for the event flag
  956. * bit(s) to be set.
  957. *
  958. * Returns : none
  959. *
  960. * Called by : OSFlagPend() OS_FLAG.C
  961. *
  962. * Note(s) : This function is INTERNAL to uC/OS-II and your application should not call it.
  963. *********************************************************************************************************
  964. */
  965. static void OS_FlagBlock (OS_FLAG_GRP *pgrp,
  966. OS_FLAG_NODE *pnode,
  967. OS_FLAGS flags,
  968. INT8U wait_type,
  969. INT32U timeout)
  970. {
  971. OS_FLAG_NODE *pnode_next;
  972. INT8U y;
  973. OSTCBCur->OSTCBStat |= OS_STAT_FLAG;
  974. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  975. OSTCBCur->OSTCBDly = timeout; /* Store timeout in task's TCB */
  976. #if OS_TASK_DEL_EN > 0u
  977. OSTCBCur->OSTCBFlagNode = pnode; /* TCB to link to node */
  978. #endif
  979. pnode->OSFlagNodeFlags = flags; /* Save the flags that we need to wait for */
  980. pnode->OSFlagNodeWaitType = wait_type; /* Save the type of wait we are doing */
  981. pnode->OSFlagNodeTCB = (void *)OSTCBCur; /* Link to task's TCB */
  982. pnode->OSFlagNodeNext = pgrp->OSFlagWaitList; /* Add node at beginning of event flag wait list */
  983. pnode->OSFlagNodePrev = (void *)0;
  984. pnode->OSFlagNodeFlagGrp = (void *)pgrp; /* Link to Event Flag Group */
  985. pnode_next = (OS_FLAG_NODE *)pgrp->OSFlagWaitList;
  986. if (pnode_next != (void *)0) { /* Is this the first NODE to insert? */
  987. pnode_next->OSFlagNodePrev = pnode; /* No, link in doubly linked list */
  988. }
  989. pgrp->OSFlagWaitList = (void *)pnode;
  990. y = OSTCBCur->OSTCBY; /* Suspend current task until flag(s) received */
  991. OSRdyTbl[y] &= (OS_PRIO)~OSTCBCur->OSTCBBitX;
  992. if (OSRdyTbl[y] == 0x00u) {
  993. OSRdyGrp &= (OS_PRIO)~OSTCBCur->OSTCBBitY;
  994. }
  995. }
  996. /*$PAGE*/
  997. /*
  998. *********************************************************************************************************
  999. * INITIALIZE THE EVENT FLAG MODULE
  1000. *
  1001. * Description: This function is called by uC/OS-II to initialize the event flag module. Your application
  1002. * MUST NOT call this function. In other words, this function is internal to uC/OS-II.
  1003. *
  1004. * Arguments : none
  1005. *
  1006. * Returns : none
  1007. *
  1008. * WARNING : You MUST NOT call this function from your code. This is an INTERNAL function to uC/OS-II.
  1009. *********************************************************************************************************
  1010. */
  1011. void OS_FlagInit (void)
  1012. {
  1013. #if OS_MAX_FLAGS == 1u
  1014. OSFlagFreeList = (OS_FLAG_GRP *)&OSFlagTbl[0]; /* Only ONE event flag group! */
  1015. OSFlagFreeList->OSFlagType = OS_EVENT_TYPE_UNUSED;
  1016. OSFlagFreeList->OSFlagWaitList = (void *)0;
  1017. OSFlagFreeList->OSFlagFlags = (OS_FLAGS)0;
  1018. #if OS_FLAG_NAME_EN > 0u
  1019. OSFlagFreeList->OSFlagName = (INT8U *)"?";
  1020. #endif
  1021. #endif
  1022. #if OS_MAX_FLAGS >= 2u
  1023. INT16U ix;
  1024. INT16U ix_next;
  1025. OS_FLAG_GRP *pgrp1;
  1026. OS_FLAG_GRP *pgrp2;
  1027. OS_MemClr((INT8U *)&OSFlagTbl[0], sizeof(OSFlagTbl)); /* Clear the flag group table */
  1028. for (ix = 0u; ix < (OS_MAX_FLAGS - 1u); ix++) { /* Init. list of free EVENT FLAGS */
  1029. ix_next = ix + 1u;
  1030. pgrp1 = &OSFlagTbl[ix];
  1031. pgrp2 = &OSFlagTbl[ix_next];
  1032. pgrp1->OSFlagType = OS_EVENT_TYPE_UNUSED;
  1033. pgrp1->OSFlagWaitList = (void *)pgrp2;
  1034. #if OS_FLAG_NAME_EN > 0u
  1035. pgrp1->OSFlagName = (INT8U *)(void *)"?"; /* Unknown name */
  1036. #endif
  1037. }
  1038. pgrp1 = &OSFlagTbl[ix];
  1039. pgrp1->OSFlagType = OS_EVENT_TYPE_UNUSED;
  1040. pgrp1->OSFlagWaitList = (void *)0;
  1041. #if OS_FLAG_NAME_EN > 0u
  1042. pgrp1->OSFlagName = (INT8U *)(void *)"?"; /* Unknown name */
  1043. #endif
  1044. OSFlagFreeList = &OSFlagTbl[0];
  1045. #endif
  1046. }
  1047. /*$PAGE*/
  1048. /*
  1049. *********************************************************************************************************
  1050. * MAKE TASK READY-TO-RUN, EVENT(s) OCCURRED
  1051. *
  1052. * Description: This function is internal to uC/OS-II and is used to make a task ready-to-run because the
  1053. * desired event flag bits have been set.
  1054. *
  1055. * Arguments : pnode is a pointer to a structure which contains data about the task waiting for
  1056. * event flag bit(s) to be set.
  1057. *
  1058. * flags_rdy contains the bit pattern of the event flags that cause the task to become
  1059. * ready-to-run.
  1060. *
  1061. * pend_stat is used to indicate the readied task's pending status:
  1062. *
  1063. *
  1064. * Returns : OS_TRUE If the task has been placed in the ready list and thus needs scheduling
  1065. * OS_FALSE The task is still not ready to run and thus scheduling is not necessary
  1066. *
  1067. * Called by : OSFlagsPost() OS_FLAG.C
  1068. *
  1069. * Note(s) : 1) This function assumes that interrupts are disabled.
  1070. * 2) This function is INTERNAL to uC/OS-II and your application should not call it.
  1071. *********************************************************************************************************
  1072. */
  1073. static BOOLEAN OS_FlagTaskRdy (OS_FLAG_NODE *pnode,
  1074. OS_FLAGS flags_rdy,
  1075. INT8U pend_stat)
  1076. {
  1077. OS_TCB *ptcb;
  1078. BOOLEAN sched;
  1079. ptcb = (OS_TCB *)pnode->OSFlagNodeTCB; /* Point to TCB of waiting task */
  1080. ptcb->OSTCBDly = 0u;
  1081. ptcb->OSTCBFlagsRdy = flags_rdy;
  1082. ptcb->OSTCBStat &= (INT8U)~(INT8U)OS_STAT_FLAG;
  1083. ptcb->OSTCBStatPend = pend_stat;
  1084. if (ptcb->OSTCBStat == OS_STAT_RDY) { /* Task now ready? */
  1085. OSRdyGrp |= ptcb->OSTCBBitY; /* Put task into ready list */
  1086. OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
  1087. sched = OS_TRUE;
  1088. } else {
  1089. sched = OS_FALSE;
  1090. }
  1091. OS_FlagUnlink(pnode);
  1092. return (sched);
  1093. }
  1094. /*$PAGE*/
  1095. /*
  1096. *********************************************************************************************************
  1097. * UNLINK EVENT FLAG NODE FROM WAITING LIST
  1098. *
  1099. * Description: This function is internal to uC/OS-II and is used to unlink an event flag node from a
  1100. * list of tasks waiting for the event flag.
  1101. *
  1102. * Arguments : pnode is a pointer to a structure which contains data about the task waiting for
  1103. * event flag bit(s) to be set.
  1104. *
  1105. * Returns : none
  1106. *
  1107. * Called by : OS_FlagTaskRdy() OS_FLAG.C
  1108. * OSFlagPend() OS_FLAG.C
  1109. * OSTaskDel() OS_TASK.C
  1110. *
  1111. * Note(s) : 1) This function assumes that interrupts are disabled.
  1112. * 2) This function is INTERNAL to uC/OS-II and your application should not call it.
  1113. *********************************************************************************************************
  1114. */
  1115. void OS_FlagUnlink (OS_FLAG_NODE *pnode)
  1116. {
  1117. #if OS_TASK_DEL_EN > 0u
  1118. OS_TCB *ptcb;
  1119. #endif
  1120. OS_FLAG_GRP *pgrp;
  1121. OS_FLAG_NODE *pnode_prev;
  1122. OS_FLAG_NODE *pnode_next;
  1123. pnode_prev = (OS_FLAG_NODE *)pnode->OSFlagNodePrev;
  1124. pnode_next = (OS_FLAG_NODE *)pnode->OSFlagNodeNext;
  1125. if (pnode_prev == (OS_FLAG_NODE *)0) { /* Is it first node in wait list? */
  1126. pgrp = (OS_FLAG_GRP *)pnode->OSFlagNodeFlagGrp;
  1127. pgrp->OSFlagWaitList = (void *)pnode_next; /* Update list for new 1st node */
  1128. if (pnode_next != (OS_FLAG_NODE *)0) {
  1129. pnode_next->OSFlagNodePrev = (OS_FLAG_NODE *)0; /* Link new 1st node PREV to NULL */
  1130. }
  1131. } else { /* No, A node somewhere in the list */
  1132. pnode_prev->OSFlagNodeNext = pnode_next; /* Link around the node to unlink */
  1133. if (pnode_next != (OS_FLAG_NODE *)0) { /* Was this the LAST node? */
  1134. pnode_next->OSFlagNodePrev = pnode_prev; /* No, Link around current node */
  1135. }
  1136. }
  1137. #if OS_TASK_DEL_EN > 0u
  1138. ptcb = (OS_TCB *)pnode->OSFlagNodeTCB;
  1139. ptcb->OSTCBFlagNode = (OS_FLAG_NODE *)0;
  1140. #endif
  1141. }
  1142. #endif