os_core.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. /*
  2. *********************************************************************************************************
  3. * uC/OS-II
  4. * The Real-Time Kernel
  5. * CORE FUNCTIONS
  6. *
  7. * (c) Copyright 1992-2013, Micrium, Weston, FL
  8. * All Rights Reserved
  9. *
  10. * File : OS_CORE.C
  11. * By : Jean J. Labrosse
  12. * Version : V2.92.10
  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. #define OS_GLOBALS
  26. #include <ucos_ii.h>
  27. #endif
  28. /*
  29. *********************************************************************************************************
  30. * PRIORITY RESOLUTION TABLE
  31. *
  32. * Note: Index into table is bit pattern to resolve highest priority
  33. * Indexed value corresponds to highest priority bit position (i.e. 0..7)
  34. *********************************************************************************************************
  35. */
  36. INT8U const OSUnMapTbl[256] = {
  37. 0u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x00 to 0x0F */
  38. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x10 to 0x1F */
  39. 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x20 to 0x2F */
  40. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x30 to 0x3F */
  41. 6u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x40 to 0x4F */
  42. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x50 to 0x5F */
  43. 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x60 to 0x6F */
  44. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x70 to 0x7F */
  45. 7u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x80 to 0x8F */
  46. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x90 to 0x9F */
  47. 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xA0 to 0xAF */
  48. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xB0 to 0xBF */
  49. 6u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xC0 to 0xCF */
  50. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xD0 to 0xDF */
  51. 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xE0 to 0xEF */
  52. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u /* 0xF0 to 0xFF */
  53. };
  54. /*$PAGE*/
  55. /*
  56. *********************************************************************************************************
  57. * FUNCTION PROTOTYPES
  58. *********************************************************************************************************
  59. */
  60. static void OS_InitEventList(void);
  61. static void OS_InitMisc(void);
  62. static void OS_InitRdyList(void);
  63. static void OS_InitTaskIdle(void);
  64. #if OS_TASK_STAT_EN > 0u
  65. static void OS_InitTaskStat(void);
  66. #endif
  67. static void OS_InitTCBList(void);
  68. static void OS_SchedNew(void);
  69. /*$PAGE*/
  70. /*
  71. *********************************************************************************************************
  72. * GET THE NAME OF A SEMAPHORE, MUTEX, MAILBOX or QUEUE
  73. *
  74. * Description: This function is used to obtain the name assigned to a semaphore, mutex, mailbox or queue.
  75. *
  76. * Arguments : pevent is a pointer to the event group. 'pevent' can point either to a semaphore,
  77. * a mutex, a mailbox or a queue. Where this function is concerned, the actual
  78. * type is irrelevant.
  79. *
  80. * pname is a pointer to a pointer to an ASCII string that will receive the name of the semaphore,
  81. * mutex, mailbox or queue.
  82. *
  83. * perr is a pointer to an error code that can contain one of the following values:
  84. *
  85. * OS_ERR_NONE if the name was copied to 'pname'
  86. * OS_ERR_EVENT_TYPE if 'pevent' is not pointing to the proper event
  87. * control block type.
  88. * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
  89. * OS_ERR_PEVENT_NULL if you passed a NULL pointer for 'pevent'
  90. * OS_ERR_NAME_GET_ISR if you are trying to call this function from an ISR
  91. *
  92. * Returns : The length of the string or 0 if the 'pevent' is a NULL pointer.
  93. *********************************************************************************************************
  94. */
  95. #if (OS_EVENT_EN) && (OS_EVENT_NAME_EN > 0u)
  96. INT8U OSEventNameGet (OS_EVENT *pevent,
  97. INT8U **pname,
  98. INT8U *perr)
  99. {
  100. INT8U len;
  101. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  102. OS_CPU_SR cpu_sr = 0u;
  103. #endif
  104. #ifdef OS_SAFETY_CRITICAL
  105. if (perr == (INT8U *)0) {
  106. OS_SAFETY_CRITICAL_EXCEPTION();
  107. return (0u);
  108. }
  109. #endif
  110. #if OS_ARG_CHK_EN > 0u
  111. if (pevent == (OS_EVENT *)0) { /* Is 'pevent' a NULL pointer? */
  112. *perr = OS_ERR_PEVENT_NULL;
  113. return (0u);
  114. }
  115. if (pname == (INT8U **)0) { /* Is 'pname' a NULL pointer? */
  116. *perr = OS_ERR_PNAME_NULL;
  117. return (0u);
  118. }
  119. #endif
  120. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  121. *perr = OS_ERR_NAME_GET_ISR;
  122. return (0u);
  123. }
  124. switch (pevent->OSEventType) {
  125. case OS_EVENT_TYPE_SEM:
  126. case OS_EVENT_TYPE_MUTEX:
  127. case OS_EVENT_TYPE_MBOX:
  128. case OS_EVENT_TYPE_Q:
  129. break;
  130. default:
  131. *perr = OS_ERR_EVENT_TYPE;
  132. return (0u);
  133. }
  134. OS_ENTER_CRITICAL();
  135. *pname = pevent->OSEventName;
  136. len = OS_StrLen(*pname);
  137. OS_EXIT_CRITICAL();
  138. *perr = OS_ERR_NONE;
  139. return (len);
  140. }
  141. #endif
  142. /*$PAGE*/
  143. /*
  144. *********************************************************************************************************
  145. * ASSIGN A NAME TO A SEMAPHORE, MUTEX, MAILBOX or QUEUE
  146. *
  147. * Description: This function assigns a name to a semaphore, mutex, mailbox or queue.
  148. *
  149. * Arguments : pevent is a pointer to the event group. 'pevent' can point either to a semaphore,
  150. * a mutex, a mailbox or a queue. Where this function is concerned, it doesn't
  151. * matter the actual type.
  152. *
  153. * pname is a pointer to an ASCII string that will be used as the name of the semaphore,
  154. * mutex, mailbox or queue.
  155. *
  156. * perr is a pointer to an error code that can contain one of the following values:
  157. *
  158. * OS_ERR_NONE if the requested task is resumed
  159. * OS_ERR_EVENT_TYPE if 'pevent' is not pointing to the proper event
  160. * control block type.
  161. * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
  162. * OS_ERR_PEVENT_NULL if you passed a NULL pointer for 'pevent'
  163. * OS_ERR_NAME_SET_ISR if you called this function from an ISR
  164. *
  165. * Returns : None
  166. *********************************************************************************************************
  167. */
  168. #if (OS_EVENT_EN) && (OS_EVENT_NAME_EN > 0u)
  169. void OSEventNameSet (OS_EVENT *pevent,
  170. INT8U *pname,
  171. INT8U *perr)
  172. {
  173. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  174. OS_CPU_SR cpu_sr = 0u;
  175. #endif
  176. #ifdef OS_SAFETY_CRITICAL
  177. if (perr == (INT8U *)0) {
  178. OS_SAFETY_CRITICAL_EXCEPTION();
  179. return;
  180. }
  181. #endif
  182. #if OS_ARG_CHK_EN > 0u
  183. if (pevent == (OS_EVENT *)0) { /* Is 'pevent' a NULL pointer? */
  184. *perr = OS_ERR_PEVENT_NULL;
  185. return;
  186. }
  187. if (pname == (INT8U *)0) { /* Is 'pname' a NULL pointer? */
  188. *perr = OS_ERR_PNAME_NULL;
  189. return;
  190. }
  191. #endif
  192. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  193. *perr = OS_ERR_NAME_SET_ISR;
  194. return;
  195. }
  196. switch (pevent->OSEventType) {
  197. case OS_EVENT_TYPE_SEM:
  198. case OS_EVENT_TYPE_MUTEX:
  199. case OS_EVENT_TYPE_MBOX:
  200. case OS_EVENT_TYPE_Q:
  201. break;
  202. default:
  203. *perr = OS_ERR_EVENT_TYPE;
  204. return;
  205. }
  206. OS_ENTER_CRITICAL();
  207. pevent->OSEventName = pname;
  208. OS_EXIT_CRITICAL();
  209. *perr = OS_ERR_NONE;
  210. }
  211. #endif
  212. /*$PAGE*/
  213. /*
  214. *********************************************************************************************************
  215. * PEND ON MULTIPLE EVENTS
  216. *
  217. * Description: This function waits for multiple events. If multiple events are ready at the start of the
  218. * pend call, then all available events are returned as ready. If the task must pend on the
  219. * multiple events, then only the first posted or aborted event is returned as ready.
  220. *
  221. * Arguments : pevents_pend is a pointer to a NULL-terminated array of event control blocks to wait for.
  222. *
  223. * pevents_rdy is a pointer to an array to return which event control blocks are available
  224. * or ready. The size of the array MUST be greater than or equal to the size
  225. * of the 'pevents_pend' array, including terminating NULL.
  226. *
  227. * pmsgs_rdy is a pointer to an array to return messages from any available message-type
  228. * events. The size of the array MUST be greater than or equal to the size of
  229. * the 'pevents_pend' array, excluding the terminating NULL. Since NULL
  230. * messages are valid messages, this array cannot be NULL-terminated. Instead,
  231. * every available message-type event returns its messages in the 'pmsgs_rdy'
  232. * array at the same index as the event is returned in the 'pevents_rdy' array.
  233. * All other 'pmsgs_rdy' array indices are filled with NULL messages.
  234. *
  235. * timeout is an optional timeout period (in clock ticks). If non-zero, your task will
  236. * wait for the resources up to the amount of time specified by this argument.
  237. * If you specify 0, however, your task will wait forever for the specified
  238. * events or, until the resources becomes available (or the events occur).
  239. *
  240. * perr is a pointer to where an error message will be deposited. Possible error
  241. * messages are:
  242. *
  243. * OS_ERR_NONE The call was successful and your task owns the resources
  244. * or, the events you are waiting for occurred; check the
  245. * 'pevents_rdy' array for which events are available.
  246. * OS_ERR_PEND_ABORT The wait on the events was aborted; check the
  247. * 'pevents_rdy' array for which events were aborted.
  248. * OS_ERR_TIMEOUT The events were not received within the specified
  249. * 'timeout'.
  250. * OS_ERR_PEVENT_NULL If 'pevents_pend', 'pevents_rdy', or 'pmsgs_rdy' is a
  251. * NULL pointer.
  252. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to an array of semaphores,
  253. * mailboxes, and/or queues.
  254. * OS_ERR_PEND_ISR If you called this function from an ISR and the result
  255. * would lead to a suspension.
  256. * OS_ERR_PEND_LOCKED If you called this function when the scheduler is locked.
  257. *
  258. * Returns : > 0 the number of events returned as ready or aborted.
  259. * == 0 if no events are returned as ready because of timeout or upon error.
  260. *
  261. * Notes : 1) a. Validate 'pevents_pend' array as valid OS_EVENTs :
  262. *
  263. * semaphores, mailboxes, queues
  264. *
  265. * b. Return ALL available events and messages, if any
  266. *
  267. * c. Add current task priority as pending to each events's wait list
  268. * Performed in OS_EventTaskWaitMulti()
  269. *
  270. * d. Wait on any of multiple events
  271. *
  272. * e. Remove current task priority as pending from each events's wait list
  273. * Performed in OS_EventTaskRdy(), if events posted or aborted
  274. *
  275. * f. Return any event posted or aborted, if any
  276. * else
  277. * Return timeout
  278. *
  279. * 2) 'pevents_rdy' initialized to NULL PRIOR to all other validation or function handling in
  280. * case of any error(s).
  281. *********************************************************************************************************
  282. */
  283. /*$PAGE*/
  284. #if ((OS_EVENT_EN) && (OS_EVENT_MULTI_EN > 0u))
  285. INT16U OSEventPendMulti (OS_EVENT **pevents_pend,
  286. OS_EVENT **pevents_rdy,
  287. void **pmsgs_rdy,
  288. INT32U timeout,
  289. INT8U *perr)
  290. {
  291. OS_EVENT **pevents;
  292. OS_EVENT *pevent;
  293. #if ((OS_Q_EN > 0u) && (OS_MAX_QS > 0u))
  294. OS_Q *pq;
  295. #endif
  296. BOOLEAN events_rdy;
  297. INT16U events_rdy_nbr;
  298. INT8U events_stat;
  299. #if (OS_CRITICAL_METHOD == 3u) /* Allocate storage for CPU status register */
  300. OS_CPU_SR cpu_sr = 0u;
  301. #endif
  302. #ifdef OS_SAFETY_CRITICAL
  303. if (perr == (INT8U *)0) {
  304. OS_SAFETY_CRITICAL_EXCEPTION();
  305. return (0u);
  306. }
  307. #endif
  308. #if (OS_ARG_CHK_EN > 0u)
  309. if (pevents_pend == (OS_EVENT **)0) { /* Validate 'pevents_pend' */
  310. *perr = OS_ERR_PEVENT_NULL;
  311. return (0u);
  312. }
  313. if (*pevents_pend == (OS_EVENT *)0) { /* Validate 'pevents_pend' */
  314. *perr = OS_ERR_PEVENT_NULL;
  315. return (0u);
  316. }
  317. if (pevents_rdy == (OS_EVENT **)0) { /* Validate 'pevents_rdy' */
  318. *perr = OS_ERR_PEVENT_NULL;
  319. return (0u);
  320. }
  321. if (pmsgs_rdy == (void **)0) { /* Validate 'pmsgs_rdy' */
  322. *perr = OS_ERR_PEVENT_NULL;
  323. return (0u);
  324. }
  325. #endif
  326. *pevents_rdy = (OS_EVENT *)0; /* Init array to NULL in case of errors */
  327. pevents = pevents_pend;
  328. pevent = *pevents;
  329. while (pevent != (OS_EVENT *)0) {
  330. switch (pevent->OSEventType) { /* Validate event block types */
  331. #if (OS_SEM_EN > 0u)
  332. case OS_EVENT_TYPE_SEM:
  333. break;
  334. #endif
  335. #if (OS_MBOX_EN > 0u)
  336. case OS_EVENT_TYPE_MBOX:
  337. break;
  338. #endif
  339. #if ((OS_Q_EN > 0u) && (OS_MAX_QS > 0u))
  340. case OS_EVENT_TYPE_Q:
  341. break;
  342. #endif
  343. case OS_EVENT_TYPE_MUTEX:
  344. case OS_EVENT_TYPE_FLAG:
  345. default:
  346. *perr = OS_ERR_EVENT_TYPE;
  347. return (0u);
  348. }
  349. pevents++;
  350. pevent = *pevents;
  351. }
  352. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  353. *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
  354. return (0u);
  355. }
  356. if (OSLockNesting > 0u) { /* See if called with scheduler locked ... */
  357. *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
  358. return (0u);
  359. }
  360. /*$PAGE*/
  361. events_rdy = OS_FALSE;
  362. events_rdy_nbr = 0u;
  363. events_stat = OS_STAT_RDY;
  364. pevents = pevents_pend;
  365. pevent = *pevents;
  366. OS_ENTER_CRITICAL();
  367. while (pevent != (OS_EVENT *)0) { /* See if any events already available */
  368. switch (pevent->OSEventType) {
  369. #if (OS_SEM_EN > 0u)
  370. case OS_EVENT_TYPE_SEM:
  371. if (pevent->OSEventCnt > 0u) { /* If semaphore count > 0, resource available; */
  372. pevent->OSEventCnt--; /* ... decrement semaphore, ... */
  373. *pevents_rdy++ = pevent; /* ... and return available semaphore event */
  374. events_rdy = OS_TRUE;
  375. *pmsgs_rdy++ = (void *)0; /* NO message returned for semaphores */
  376. events_rdy_nbr++;
  377. } else {
  378. events_stat |= OS_STAT_SEM; /* Configure multi-pend for semaphore events */
  379. }
  380. break;
  381. #endif
  382. #if (OS_MBOX_EN > 0u)
  383. case OS_EVENT_TYPE_MBOX:
  384. if (pevent->OSEventPtr != (void *)0) { /* If mailbox NOT empty; ... */
  385. /* ... return available message, ... */
  386. *pmsgs_rdy++ = (void *)pevent->OSEventPtr;
  387. pevent->OSEventPtr = (void *)0;
  388. *pevents_rdy++ = pevent; /* ... and return available mailbox event */
  389. events_rdy = OS_TRUE;
  390. events_rdy_nbr++;
  391. } else {
  392. events_stat |= OS_STAT_MBOX; /* Configure multi-pend for mailbox events */
  393. }
  394. break;
  395. #endif
  396. #if ((OS_Q_EN > 0u) && (OS_MAX_QS > 0u))
  397. case OS_EVENT_TYPE_Q:
  398. pq = (OS_Q *)pevent->OSEventPtr;
  399. if (pq->OSQEntries > 0u) { /* If queue NOT empty; ... */
  400. /* ... return available message, ... */
  401. *pmsgs_rdy++ = (void *)*pq->OSQOut++;
  402. if (pq->OSQOut == pq->OSQEnd) { /* If OUT ptr at queue end, ... */
  403. pq->OSQOut = pq->OSQStart; /* ... wrap to queue start */
  404. }
  405. pq->OSQEntries--; /* Update number of queue entries */
  406. *pevents_rdy++ = pevent; /* ... and return available queue event */
  407. events_rdy = OS_TRUE;
  408. events_rdy_nbr++;
  409. } else {
  410. events_stat |= OS_STAT_Q; /* Configure multi-pend for queue events */
  411. }
  412. break;
  413. #endif
  414. case OS_EVENT_TYPE_MUTEX:
  415. case OS_EVENT_TYPE_FLAG:
  416. default:
  417. OS_EXIT_CRITICAL();
  418. *pevents_rdy = (OS_EVENT *)0; /* NULL terminate return event array */
  419. *perr = OS_ERR_EVENT_TYPE;
  420. return (events_rdy_nbr);
  421. }
  422. pevents++;
  423. pevent = *pevents;
  424. }
  425. if ( events_rdy == OS_TRUE) { /* Return any events already available */
  426. *pevents_rdy = (OS_EVENT *)0; /* NULL terminate return event array */
  427. OS_EXIT_CRITICAL();
  428. *perr = OS_ERR_NONE;
  429. return (events_rdy_nbr);
  430. }
  431. /*$PAGE*/
  432. /* Otherwise, must wait until any event occurs */
  433. OSTCBCur->OSTCBStat |= events_stat | /* Resource not available, ... */
  434. OS_STAT_MULTI; /* ... pend on multiple events */
  435. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  436. OSTCBCur->OSTCBDly = timeout; /* Store pend timeout in TCB */
  437. OS_EventTaskWaitMulti(pevents_pend); /* Suspend task until events or timeout occurs */
  438. OS_EXIT_CRITICAL();
  439. OS_Sched(); /* Find next highest priority task ready */
  440. OS_ENTER_CRITICAL();
  441. switch (OSTCBCur->OSTCBStatPend) { /* Handle event posted, aborted, or timed-out */
  442. case OS_STAT_PEND_OK:
  443. case OS_STAT_PEND_ABORT:
  444. pevent = OSTCBCur->OSTCBEventPtr;
  445. if (pevent != (OS_EVENT *)0) { /* If task event ptr != NULL, ... */
  446. *pevents_rdy++ = pevent; /* ... return available event ... */
  447. *pevents_rdy = (OS_EVENT *)0; /* ... & NULL terminate return event array */
  448. events_rdy_nbr = 1;
  449. } else { /* Else NO event available, handle as timeout */
  450. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_TO;
  451. OS_EventTaskRemoveMulti(OSTCBCur, pevents_pend);
  452. }
  453. break;
  454. case OS_STAT_PEND_TO: /* If events timed out, ... */
  455. default: /* ... remove task from events' wait lists */
  456. OS_EventTaskRemoveMulti(OSTCBCur, pevents_pend);
  457. break;
  458. }
  459. switch (OSTCBCur->OSTCBStatPend) {
  460. case OS_STAT_PEND_OK:
  461. switch (pevent->OSEventType) { /* Return event's message */
  462. #if (OS_SEM_EN > 0u)
  463. case OS_EVENT_TYPE_SEM:
  464. *pmsgs_rdy++ = (void *)0; /* NO message returned for semaphores */
  465. break;
  466. #endif
  467. #if ((OS_MBOX_EN > 0u) || \
  468. ((OS_Q_EN > 0u) && (OS_MAX_QS > 0u)))
  469. case OS_EVENT_TYPE_MBOX:
  470. case OS_EVENT_TYPE_Q:
  471. *pmsgs_rdy++ = (void *)OSTCBCur->OSTCBMsg; /* Return received message */
  472. break;
  473. #endif
  474. case OS_EVENT_TYPE_MUTEX:
  475. case OS_EVENT_TYPE_FLAG:
  476. default:
  477. OS_EXIT_CRITICAL();
  478. *pevents_rdy = (OS_EVENT *)0; /* NULL terminate return event array */
  479. *perr = OS_ERR_EVENT_TYPE;
  480. return (events_rdy_nbr);
  481. }
  482. *perr = OS_ERR_NONE;
  483. break;
  484. case OS_STAT_PEND_ABORT:
  485. *pmsgs_rdy++ = (void *)0; /* NO message returned for abort */
  486. *perr = OS_ERR_PEND_ABORT; /* Indicate that event aborted */
  487. break;
  488. case OS_STAT_PEND_TO:
  489. default:
  490. *pmsgs_rdy++ = (void *)0; /* NO message returned for timeout */
  491. *perr = OS_ERR_TIMEOUT; /* Indicate that events timed out */
  492. break;
  493. }
  494. OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set task status to ready */
  495. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */
  496. OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* Clear event pointers */
  497. OSTCBCur->OSTCBEventMultiPtr = (OS_EVENT **)0;
  498. #if ((OS_MBOX_EN > 0u) || \
  499. ((OS_Q_EN > 0u) && (OS_MAX_QS > 0u)))
  500. OSTCBCur->OSTCBMsg = (void *)0; /* Clear task message */
  501. #endif
  502. OS_EXIT_CRITICAL();
  503. return (events_rdy_nbr);
  504. }
  505. #endif
  506. /*$PAGE*/
  507. /*
  508. *********************************************************************************************************
  509. * INITIALIZATION
  510. *
  511. * Description: This function is used to initialize the internals of uC/OS-II and MUST be called prior to
  512. * creating any uC/OS-II object and, prior to calling OSStart().
  513. *
  514. * Arguments : none
  515. *
  516. * Returns : none
  517. *********************************************************************************************************
  518. */
  519. void OSInit (void)
  520. {
  521. #if OS_TASK_CREATE_EXT_EN > 0u
  522. #if defined(OS_TLS_TBL_SIZE) && (OS_TLS_TBL_SIZE > 0u)
  523. INT8U err;
  524. #endif
  525. #endif
  526. OSInitHookBegin(); /* Call port specific initialization code */
  527. OS_InitMisc(); /* Initialize miscellaneous variables */
  528. OS_InitRdyList(); /* Initialize the Ready List */
  529. OS_InitTCBList(); /* Initialize the free list of OS_TCBs */
  530. OS_InitEventList(); /* Initialize the free list of OS_EVENTs */
  531. #if (OS_FLAG_EN > 0u) && (OS_MAX_FLAGS > 0u)
  532. OS_FlagInit(); /* Initialize the event flag structures */
  533. #endif
  534. #if (OS_MEM_EN > 0u) && (OS_MAX_MEM_PART > 0u)
  535. OS_MemInit(); /* Initialize the memory manager */
  536. #endif
  537. #if (OS_Q_EN > 0u) && (OS_MAX_QS > 0u)
  538. OS_QInit(); /* Initialize the message queue structures */
  539. #endif
  540. #if OS_TASK_CREATE_EXT_EN > 0u
  541. #if defined(OS_TLS_TBL_SIZE) && (OS_TLS_TBL_SIZE > 0u)
  542. OS_TLS_Init(&err); /* Initialize TLS, before creating tasks */
  543. if (err != OS_ERR_NONE) {
  544. return;
  545. }
  546. #endif
  547. #endif
  548. OS_InitTaskIdle(); /* Create the Idle Task */
  549. #if OS_TASK_STAT_EN > 0u
  550. OS_InitTaskStat(); /* Create the Statistic Task */
  551. #endif
  552. #if OS_TMR_EN > 0u
  553. OSTmr_Init(); /* Initialize the Timer Manager */
  554. #endif
  555. OSInitHookEnd(); /* Call port specific init. code */
  556. #if OS_DEBUG_EN > 0u
  557. OSDebugInit();
  558. #endif
  559. }
  560. /*$PAGE*/
  561. /*
  562. *********************************************************************************************************
  563. * ENTER ISR
  564. *
  565. * Description: This function is used to notify uC/OS-II that you are about to service an interrupt
  566. * service routine (ISR). This allows uC/OS-II to keep track of interrupt nesting and thus
  567. * only perform rescheduling at the last nested ISR.
  568. *
  569. * Arguments : none
  570. *
  571. * Returns : none
  572. *
  573. * Notes : 1) This function should be called with interrupts already disabled
  574. * 2) Your ISR can directly increment OSIntNesting without calling this function because
  575. * OSIntNesting has been declared 'global'.
  576. * 3) You MUST still call OSIntExit() even though you increment OSIntNesting directly.
  577. * 4) You MUST invoke OSIntEnter() and OSIntExit() in pair. In other words, for every call
  578. * to OSIntEnter() at the beginning of the ISR you MUST have a call to OSIntExit() at the
  579. * end of the ISR.
  580. * 5) You are allowed to nest interrupts up to 255 levels deep.
  581. * 6) I removed the OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL() around the increment because
  582. * OSIntEnter() is always called with interrupts disabled.
  583. *********************************************************************************************************
  584. */
  585. void OSIntEnter (void)
  586. {
  587. if (OSRunning == OS_TRUE) {
  588. if (OSIntNesting < 255u) {
  589. OSIntNesting++; /* Increment ISR nesting level */
  590. }
  591. }
  592. }
  593. /*$PAGE*/
  594. /*
  595. *********************************************************************************************************
  596. * EXIT ISR
  597. *
  598. * Description: This function is used to notify uC/OS-II that you have completed servicing an ISR. When
  599. * the last nested ISR has completed, uC/OS-II will call the scheduler to determine whether
  600. * a new, high-priority task, is ready to run.
  601. *
  602. * Arguments : none
  603. *
  604. * Returns : none
  605. *
  606. * Notes : 1) You MUST invoke OSIntEnter() and OSIntExit() in pair. In other words, for every call
  607. * to OSIntEnter() at the beginning of the ISR you MUST have a call to OSIntExit() at the
  608. * end of the ISR.
  609. * 2) Rescheduling is prevented when the scheduler is locked (see OS_SchedLock())
  610. *********************************************************************************************************
  611. */
  612. void OSIntExit (void)
  613. {
  614. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  615. OS_CPU_SR cpu_sr = 0u;
  616. #endif
  617. if (OSRunning == OS_TRUE) {
  618. OS_ENTER_CRITICAL();
  619. if (OSIntNesting > 0u) { /* Prevent OSIntNesting from wrapping */
  620. OSIntNesting--;
  621. }
  622. if (OSIntNesting == 0u) { /* Reschedule only if all ISRs complete ... */
  623. if (OSLockNesting == 0u) { /* ... and not locked. */
  624. OS_SchedNew();
  625. OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
  626. if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
  627. #if OS_TASK_PROFILE_EN > 0u
  628. OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
  629. #endif
  630. OSCtxSwCtr++; /* Keep track of the number of ctx switches */
  631. #if OS_TASK_CREATE_EXT_EN > 0u
  632. #if defined(OS_TLS_TBL_SIZE) && (OS_TLS_TBL_SIZE > 0u)
  633. OS_TLS_TaskSw();
  634. #endif
  635. #endif
  636. OSIntCtxSw(); /* Perform interrupt level ctx switch */
  637. }
  638. }
  639. }
  640. OS_EXIT_CRITICAL();
  641. }
  642. }
  643. /*$PAGE*/
  644. /*
  645. *********************************************************************************************************
  646. * INDICATE THAT IT'S NO LONGER SAFE TO CREATE OBJECTS
  647. *
  648. * Description: This function is called by the application code to indicate that all initialization has
  649. * been completed and that kernel objects are no longer allowed to be created.
  650. *
  651. * Arguments : none
  652. *
  653. * Returns : none
  654. *
  655. * Note(s) : 1) You should call this function when you no longer want to allow application code to
  656. * create kernel objects.
  657. * 2) You need to define the macro 'OS_SAFETY_CRITICAL_IEC61508'
  658. *********************************************************************************************************
  659. */
  660. #ifdef OS_SAFETY_CRITICAL_IEC61508
  661. void OSSafetyCriticalStart (void)
  662. {
  663. OSSafetyCriticalStartFlag = OS_TRUE;
  664. }
  665. #endif
  666. /*$PAGE*/
  667. /*
  668. *********************************************************************************************************
  669. * PREVENT SCHEDULING
  670. *
  671. * Description: This function is used to prevent rescheduling to take place. This allows your application
  672. * to prevent context switches until you are ready to permit context switching.
  673. *
  674. * Arguments : none
  675. *
  676. * Returns : none
  677. *
  678. * Notes : 1) You MUST invoke OSSchedLock() and OSSchedUnlock() in pair. In other words, for every
  679. * call to OSSchedLock() you MUST have a call to OSSchedUnlock().
  680. *********************************************************************************************************
  681. */
  682. #if OS_SCHED_LOCK_EN > 0u
  683. void OSSchedLock (void)
  684. {
  685. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  686. OS_CPU_SR cpu_sr = 0u;
  687. #endif
  688. if (OSRunning == OS_TRUE) { /* Make sure multitasking is running */
  689. OS_ENTER_CRITICAL();
  690. if (OSIntNesting == 0u) { /* Can't call from an ISR */
  691. if (OSLockNesting < 255u) { /* Prevent OSLockNesting from wrapping back to 0 */
  692. OSLockNesting++; /* Increment lock nesting level */
  693. }
  694. }
  695. OS_EXIT_CRITICAL();
  696. }
  697. }
  698. #endif
  699. /*$PAGE*/
  700. /*
  701. *********************************************************************************************************
  702. * ENABLE SCHEDULING
  703. *
  704. * Description: This function is used to re-allow rescheduling.
  705. *
  706. * Arguments : none
  707. *
  708. * Returns : none
  709. *
  710. * Notes : 1) You MUST invoke OSSchedLock() and OSSchedUnlock() in pair. In other words, for every
  711. * call to OSSchedLock() you MUST have a call to OSSchedUnlock().
  712. *********************************************************************************************************
  713. */
  714. #if OS_SCHED_LOCK_EN > 0u
  715. void OSSchedUnlock (void)
  716. {
  717. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  718. OS_CPU_SR cpu_sr = 0u;
  719. #endif
  720. if (OSRunning == OS_TRUE) { /* Make sure multitasking is running */
  721. OS_ENTER_CRITICAL();
  722. if (OSIntNesting == 0u) { /* Can't call from an ISR */
  723. if (OSLockNesting > 0u) { /* Do not decrement if already 0 */
  724. OSLockNesting--; /* Decrement lock nesting level */
  725. if (OSLockNesting == 0u) { /* See if scheduler is enabled */
  726. OS_EXIT_CRITICAL();
  727. OS_Sched(); /* See if a HPT is ready */
  728. } else {
  729. OS_EXIT_CRITICAL();
  730. }
  731. } else {
  732. OS_EXIT_CRITICAL();
  733. }
  734. } else {
  735. OS_EXIT_CRITICAL();
  736. }
  737. }
  738. }
  739. #endif
  740. /*$PAGE*/
  741. /*
  742. *********************************************************************************************************
  743. * START MULTITASKING
  744. *
  745. * Description: This function is used to start the multitasking process which lets uC/OS-II manages the
  746. * task that you have created. Before you can call OSStart(), you MUST have called OSInit()
  747. * and you MUST have created at least one task.
  748. *
  749. * Arguments : none
  750. *
  751. * Returns : none
  752. *
  753. * Note : OSStartHighRdy() MUST:
  754. * a) Call OSTaskSwHook() then,
  755. * b) Set OSRunning to OS_TRUE.
  756. * c) Load the context of the task pointed to by OSTCBHighRdy.
  757. * d_ Execute the task.
  758. *********************************************************************************************************
  759. */
  760. void OSStart (void)
  761. {
  762. if (OSRunning == OS_FALSE) {
  763. OS_SchedNew(); /* Find highest priority's task priority number */
  764. OSPrioCur = OSPrioHighRdy;
  765. OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy]; /* Point to highest priority task ready to run */
  766. OSTCBCur = OSTCBHighRdy;
  767. OSStartHighRdy(); /* Execute target specific code to start task */
  768. }
  769. }
  770. /*$PAGE*/
  771. /*
  772. *********************************************************************************************************
  773. * STATISTICS INITIALIZATION
  774. *
  775. * Description: This function is called by your application to establish CPU usage by first determining
  776. * how high a 32-bit counter would count to in 1 second if no other tasks were to execute
  777. * during that time. CPU usage is then determined by a low priority task which keeps track
  778. * of this 32-bit counter every second but this time, with other tasks running. CPU usage is
  779. * determined by:
  780. *
  781. * OSIdleCtr
  782. * CPU Usage (%) = 100 * (1 - ------------)
  783. * OSIdleCtrMax
  784. *
  785. * Arguments : none
  786. *
  787. * Returns : none
  788. *********************************************************************************************************
  789. */
  790. #if OS_TASK_STAT_EN > 0u
  791. void OSStatInit (void)
  792. {
  793. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  794. OS_CPU_SR cpu_sr = 0u;
  795. #endif
  796. OSTimeDly(2u); /* Synchronize with clock tick */
  797. OS_ENTER_CRITICAL();
  798. OSIdleCtr = 0uL; /* Clear idle counter */
  799. OS_EXIT_CRITICAL();
  800. OSTimeDly(OS_TICKS_PER_SEC / 10u); /* Determine MAX. idle counter value for 1/10 second */
  801. OS_ENTER_CRITICAL();
  802. OSIdleCtrMax = OSIdleCtr; /* Store maximum idle counter count in 1/10 second */
  803. OSStatRdy = OS_TRUE;
  804. OS_EXIT_CRITICAL();
  805. }
  806. #endif
  807. /*$PAGE*/
  808. /*
  809. *********************************************************************************************************
  810. * PROCESS SYSTEM TICK
  811. *
  812. * Description: This function is used to signal to uC/OS-II the occurrence of a 'system tick' (also known
  813. * as a 'clock tick'). This function should be called by the ticker ISR but, can also be
  814. * called by a high priority task.
  815. *
  816. * Arguments : none
  817. *
  818. * Returns : none
  819. *********************************************************************************************************
  820. */
  821. void OSTimeTick (void)
  822. {
  823. OS_TCB *ptcb;
  824. #if OS_TICK_STEP_EN > 0u
  825. BOOLEAN step;
  826. #endif
  827. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  828. OS_CPU_SR cpu_sr = 0u;
  829. #endif
  830. #if OS_TIME_TICK_HOOK_EN > 0u
  831. OSTimeTickHook(); /* Call user definable hook */
  832. #endif
  833. #if OS_TIME_GET_SET_EN > 0u
  834. OS_ENTER_CRITICAL(); /* Update the 32-bit tick counter */
  835. OSTime++;
  836. OS_EXIT_CRITICAL();
  837. #endif
  838. if (OSRunning == OS_TRUE) {
  839. #if OS_TICK_STEP_EN > 0u
  840. switch (OSTickStepState) { /* Determine whether we need to process a tick */
  841. case OS_TICK_STEP_DIS: /* Yes, stepping is disabled */
  842. step = OS_TRUE;
  843. break;
  844. case OS_TICK_STEP_WAIT: /* No, waiting for uC/OS-View to set ... */
  845. step = OS_FALSE; /* .. OSTickStepState to OS_TICK_STEP_ONCE */
  846. break;
  847. case OS_TICK_STEP_ONCE: /* Yes, process tick once and wait for next ... */
  848. step = OS_TRUE; /* ... step command from uC/OS-View */
  849. OSTickStepState = OS_TICK_STEP_WAIT;
  850. break;
  851. default: /* Invalid case, correct situation */
  852. step = OS_TRUE;
  853. OSTickStepState = OS_TICK_STEP_DIS;
  854. break;
  855. }
  856. if (step == OS_FALSE) { /* Return if waiting for step command */
  857. return;
  858. }
  859. #endif
  860. ptcb = OSTCBList; /* Point at first TCB in TCB list */
  861. while (ptcb->OSTCBPrio != OS_TASK_IDLE_PRIO) { /* Go through all TCBs in TCB list */
  862. OS_ENTER_CRITICAL();
  863. if (ptcb->OSTCBDly != 0u) { /* No, Delayed or waiting for event with TO */
  864. ptcb->OSTCBDly--; /* Decrement nbr of ticks to end of delay */
  865. if (ptcb->OSTCBDly == 0u) { /* Check for timeout */
  866. if ((ptcb->OSTCBStat & OS_STAT_PEND_ANY) != OS_STAT_RDY) {
  867. ptcb->OSTCBStat &= (INT8U)~(INT8U)OS_STAT_PEND_ANY; /* Yes, Clear status flag */
  868. ptcb->OSTCBStatPend = OS_STAT_PEND_TO; /* Indicate PEND timeout */
  869. } else {
  870. ptcb->OSTCBStatPend = OS_STAT_PEND_OK;
  871. }
  872. if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) == OS_STAT_RDY) { /* Is task suspended? */
  873. OSRdyGrp |= ptcb->OSTCBBitY; /* No, Make ready */
  874. OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
  875. }
  876. }
  877. }
  878. ptcb = ptcb->OSTCBNext; /* Point at next TCB in TCB list */
  879. OS_EXIT_CRITICAL();
  880. }
  881. }
  882. }
  883. /*$PAGE*/
  884. /*
  885. *********************************************************************************************************
  886. * GET VERSION
  887. *
  888. * Description: This function is used to return the version number of uC/OS-II. The returned value
  889. * corresponds to uC/OS-II's version number multiplied by 10000. In other words, version
  890. * 2.01.00 would be returned as 20100.
  891. *
  892. * Arguments : none
  893. *
  894. * Returns : The version number of uC/OS-II multiplied by 10000.
  895. *********************************************************************************************************
  896. */
  897. INT16U OSVersion (void)
  898. {
  899. return (OS_VERSION);
  900. }
  901. /*$PAGE*/
  902. /*
  903. *********************************************************************************************************
  904. * DUMMY FUNCTION
  905. *
  906. * Description: This function doesn't do anything. It is called by OSTaskDel().
  907. *
  908. * Arguments : none
  909. *
  910. * Returns : none
  911. *********************************************************************************************************
  912. */
  913. #if OS_TASK_DEL_EN > 0u
  914. void OS_Dummy (void)
  915. {
  916. }
  917. #endif
  918. /*$PAGE*/
  919. /*
  920. *********************************************************************************************************
  921. * MAKE TASK READY TO RUN BASED ON EVENT OCCURING
  922. *
  923. * Description: This function is called by other uC/OS-II services and is used to ready a task that was
  924. * waiting for an event to occur.
  925. *
  926. * Arguments : pevent is a pointer to the event control block corresponding to the event.
  927. *
  928. * pmsg is a pointer to a message. This pointer is used by message oriented services
  929. * such as MAILBOXEs and QUEUEs. The pointer is not used when called by other
  930. * service functions.
  931. *
  932. * msk is a mask that is used to clear the status byte of the TCB. For example,
  933. * OSSemPost() will pass OS_STAT_SEM, OSMboxPost() will pass OS_STAT_MBOX etc.
  934. *
  935. * pend_stat is used to indicate the readied task's pending status:
  936. *
  937. * OS_STAT_PEND_OK Task ready due to a post (or delete), not a timeout or
  938. * an abort.
  939. * OS_STAT_PEND_ABORT Task ready due to an abort.
  940. *
  941. * Returns : none
  942. *
  943. * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
  944. *********************************************************************************************************
  945. */
  946. #if (OS_EVENT_EN)
  947. INT8U OS_EventTaskRdy (OS_EVENT *pevent,
  948. void *pmsg,
  949. INT8U msk,
  950. INT8U pend_stat)
  951. {
  952. OS_TCB *ptcb;
  953. INT8U y;
  954. INT8U x;
  955. INT8U prio;
  956. #if OS_LOWEST_PRIO > 63u
  957. OS_PRIO *ptbl;
  958. #endif
  959. #if OS_LOWEST_PRIO <= 63u
  960. y = OSUnMapTbl[pevent->OSEventGrp]; /* Find HPT waiting for message */
  961. x = OSUnMapTbl[pevent->OSEventTbl[y]];
  962. prio = (INT8U)((y << 3u) + x); /* Find priority of task getting the msg */
  963. #else
  964. if ((pevent->OSEventGrp & 0xFFu) != 0u) { /* Find HPT waiting for message */
  965. y = OSUnMapTbl[ pevent->OSEventGrp & 0xFFu];
  966. } else {
  967. y = OSUnMapTbl[(OS_PRIO)(pevent->OSEventGrp >> 8u) & 0xFFu] + 8u;
  968. }
  969. ptbl = &pevent->OSEventTbl[y];
  970. if ((*ptbl & 0xFFu) != 0u) {
  971. x = OSUnMapTbl[*ptbl & 0xFFu];
  972. } else {
  973. x = OSUnMapTbl[(OS_PRIO)(*ptbl >> 8u) & 0xFFu] + 8u;
  974. }
  975. prio = (INT8U)((y << 4u) + x); /* Find priority of task getting the msg */
  976. #endif
  977. ptcb = OSTCBPrioTbl[prio]; /* Point to this task's OS_TCB */
  978. ptcb->OSTCBDly = 0u; /* Prevent OSTimeTick() from readying task */
  979. #if ((OS_Q_EN > 0u) && (OS_MAX_QS > 0u)) || (OS_MBOX_EN > 0u)
  980. ptcb->OSTCBMsg = pmsg; /* Send message directly to waiting task */
  981. #else
  982. pmsg = pmsg; /* Prevent compiler warning if not used */
  983. #endif
  984. ptcb->OSTCBStat &= (INT8U)~msk; /* Clear bit associated with event type */
  985. ptcb->OSTCBStatPend = pend_stat; /* Set pend status of post or abort */
  986. /* See if task is ready (could be susp'd) */
  987. if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) == OS_STAT_RDY) {
  988. OSRdyGrp |= ptcb->OSTCBBitY; /* Put task in the ready to run list */
  989. OSRdyTbl[y] |= ptcb->OSTCBBitX;
  990. }
  991. OS_EventTaskRemove(ptcb, pevent); /* Remove this task from event wait list */
  992. #if (OS_EVENT_MULTI_EN > 0u)
  993. if (ptcb->OSTCBEventMultiPtr != (OS_EVENT **)0) { /* Remove this task from events' wait lists */
  994. OS_EventTaskRemoveMulti(ptcb, ptcb->OSTCBEventMultiPtr);
  995. ptcb->OSTCBEventPtr = (OS_EVENT *)pevent;/* Return event as first multi-pend event ready*/
  996. }
  997. #endif
  998. return (prio);
  999. }
  1000. #endif
  1001. /*$PAGE*/
  1002. /*
  1003. *********************************************************************************************************
  1004. * MAKE TASK WAIT FOR EVENT TO OCCUR
  1005. *
  1006. * Description: This function is called by other uC/OS-II services to suspend a task because an event has
  1007. * not occurred.
  1008. *
  1009. * Arguments : pevent is a pointer to the event control block for which the task will be waiting for.
  1010. *
  1011. * Returns : none
  1012. *
  1013. * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
  1014. *********************************************************************************************************
  1015. */
  1016. #if (OS_EVENT_EN)
  1017. void OS_EventTaskWait (OS_EVENT *pevent)
  1018. {
  1019. INT8U y;
  1020. OSTCBCur->OSTCBEventPtr = pevent; /* Store ptr to ECB in TCB */
  1021. pevent->OSEventTbl[OSTCBCur->OSTCBY] |= OSTCBCur->OSTCBBitX; /* Put task in waiting list */
  1022. pevent->OSEventGrp |= OSTCBCur->OSTCBBitY;
  1023. y = OSTCBCur->OSTCBY; /* Task no longer ready */
  1024. OSRdyTbl[y] &= (OS_PRIO)~OSTCBCur->OSTCBBitX;
  1025. if (OSRdyTbl[y] == 0u) { /* Clear event grp bit if this was only task pending */
  1026. OSRdyGrp &= (OS_PRIO)~OSTCBCur->OSTCBBitY;
  1027. }
  1028. }
  1029. #endif
  1030. /*$PAGE*/
  1031. /*
  1032. *********************************************************************************************************
  1033. * MAKE TASK WAIT FOR ANY OF MULTIPLE EVENTS TO OCCUR
  1034. *
  1035. * Description: This function is called by other uC/OS-II services to suspend a task because any one of
  1036. * multiple events has not occurred.
  1037. *
  1038. * Arguments : pevents_wait is a pointer to an array of event control blocks, NULL-terminated, for
  1039. * which the task will be waiting for.
  1040. *
  1041. * Returns : none.
  1042. *
  1043. * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
  1044. *********************************************************************************************************
  1045. */
  1046. #if ((OS_EVENT_EN) && (OS_EVENT_MULTI_EN > 0u))
  1047. void OS_EventTaskWaitMulti (OS_EVENT **pevents_wait)
  1048. {
  1049. OS_EVENT **pevents;
  1050. OS_EVENT *pevent;
  1051. INT8U y;
  1052. OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0;
  1053. OSTCBCur->OSTCBEventMultiPtr = (OS_EVENT **)pevents_wait; /* Store ptr to ECBs in TCB */
  1054. pevents = pevents_wait;
  1055. pevent = *pevents;
  1056. while (pevent != (OS_EVENT *)0) { /* Put task in waiting lists */
  1057. pevent->OSEventTbl[OSTCBCur->OSTCBY] |= OSTCBCur->OSTCBBitX;
  1058. pevent->OSEventGrp |= OSTCBCur->OSTCBBitY;
  1059. pevents++;
  1060. pevent = *pevents;
  1061. }
  1062. y = OSTCBCur->OSTCBY; /* Task no longer ready */
  1063. OSRdyTbl[y] &= (OS_PRIO)~OSTCBCur->OSTCBBitX;
  1064. if (OSRdyTbl[y] == 0u) { /* Clear event grp bit if this was only task pending */
  1065. OSRdyGrp &= (OS_PRIO)~OSTCBCur->OSTCBBitY;
  1066. }
  1067. }
  1068. #endif
  1069. /*$PAGE*/
  1070. /*
  1071. *********************************************************************************************************
  1072. * REMOVE TASK FROM EVENT WAIT LIST
  1073. *
  1074. * Description: Remove a task from an event's wait list.
  1075. *
  1076. * Arguments : ptcb is a pointer to the task to remove.
  1077. *
  1078. * pevent is a pointer to the event control block.
  1079. *
  1080. * Returns : none
  1081. *
  1082. * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
  1083. *********************************************************************************************************
  1084. */
  1085. #if (OS_EVENT_EN)
  1086. void OS_EventTaskRemove (OS_TCB *ptcb,
  1087. OS_EVENT *pevent)
  1088. {
  1089. INT8U y;
  1090. y = ptcb->OSTCBY;
  1091. pevent->OSEventTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX; /* Remove task from wait list */
  1092. if (pevent->OSEventTbl[y] == 0u) {
  1093. pevent->OSEventGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
  1094. }
  1095. ptcb->OSTCBEventPtr = (OS_EVENT *)0; /* Unlink OS_EVENT from OS_TCB */
  1096. }
  1097. #endif
  1098. /*$PAGE*/
  1099. /*
  1100. *********************************************************************************************************
  1101. * REMOVE TASK FROM MULTIPLE EVENTS WAIT LISTS
  1102. *
  1103. * Description: Remove a task from multiple events' wait lists.
  1104. *
  1105. * Arguments : ptcb is a pointer to the task to remove.
  1106. *
  1107. * pevents_multi is a pointer to the array of event control blocks, NULL-terminated.
  1108. *
  1109. * Returns : none
  1110. *
  1111. * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
  1112. *********************************************************************************************************
  1113. */
  1114. #if ((OS_EVENT_EN) && (OS_EVENT_MULTI_EN > 0u))
  1115. void OS_EventTaskRemoveMulti (OS_TCB *ptcb,
  1116. OS_EVENT **pevents_multi)
  1117. {
  1118. OS_EVENT **pevents;
  1119. OS_EVENT *pevent;
  1120. INT8U y;
  1121. OS_PRIO bity;
  1122. OS_PRIO bitx;
  1123. y = ptcb->OSTCBY;
  1124. bity = ptcb->OSTCBBitY;
  1125. bitx = ptcb->OSTCBBitX;
  1126. pevents = pevents_multi;
  1127. pevent = *pevents;
  1128. while (pevent != (OS_EVENT *)0) { /* Remove task from all events' wait lists */
  1129. pevent->OSEventTbl[y] &= (OS_PRIO)~bitx;
  1130. if (pevent->OSEventTbl[y] == 0u) {
  1131. pevent->OSEventGrp &= (OS_PRIO)~bity;
  1132. }
  1133. pevents++;
  1134. pevent = *pevents;
  1135. }
  1136. }
  1137. #endif
  1138. /*$PAGE*/
  1139. /*
  1140. *********************************************************************************************************
  1141. * INITIALIZE EVENT CONTROL BLOCK'S WAIT LIST
  1142. *
  1143. * Description: This function is called by other uC/OS-II services to initialize the event wait list.
  1144. *
  1145. * Arguments : pevent is a pointer to the event control block allocated to the event.
  1146. *
  1147. * Returns : none
  1148. *
  1149. * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
  1150. *********************************************************************************************************
  1151. */
  1152. #if (OS_EVENT_EN)
  1153. void OS_EventWaitListInit (OS_EVENT *pevent)
  1154. {
  1155. INT8U i;
  1156. pevent->OSEventGrp = 0u; /* No task waiting on event */
  1157. for (i = 0u; i < OS_EVENT_TBL_SIZE; i++) {
  1158. pevent->OSEventTbl[i] = 0u;
  1159. }
  1160. }
  1161. #endif
  1162. /*$PAGE*/
  1163. /*
  1164. *********************************************************************************************************
  1165. * INITIALIZATION
  1166. * INITIALIZE THE FREE LIST OF EVENT CONTROL BLOCKS
  1167. *
  1168. * Description: This function is called by OSInit() to initialize the free list of event control blocks.
  1169. *
  1170. * Arguments : none
  1171. *
  1172. * Returns : none
  1173. *********************************************************************************************************
  1174. */
  1175. static void OS_InitEventList (void)
  1176. {
  1177. #if (OS_EVENT_EN) && (OS_MAX_EVENTS > 0u)
  1178. #if (OS_MAX_EVENTS > 1u)
  1179. INT16U ix;
  1180. INT16U ix_next;
  1181. OS_EVENT *pevent1;
  1182. OS_EVENT *pevent2;
  1183. OS_MemClr((INT8U *)&OSEventTbl[0], sizeof(OSEventTbl)); /* Clear the event table */
  1184. for (ix = 0u; ix < (OS_MAX_EVENTS - 1u); ix++) { /* Init. list of free EVENT control blocks */
  1185. ix_next = ix + 1u;
  1186. pevent1 = &OSEventTbl[ix];
  1187. pevent2 = &OSEventTbl[ix_next];
  1188. pevent1->OSEventType = OS_EVENT_TYPE_UNUSED;
  1189. pevent1->OSEventPtr = pevent2;
  1190. #if OS_EVENT_NAME_EN > 0u
  1191. pevent1->OSEventName = (INT8U *)(void *)"?"; /* Unknown name */
  1192. #endif
  1193. }
  1194. pevent1 = &OSEventTbl[ix];
  1195. pevent1->OSEventType = OS_EVENT_TYPE_UNUSED;
  1196. pevent1->OSEventPtr = (OS_EVENT *)0;
  1197. #if OS_EVENT_NAME_EN > 0u
  1198. pevent1->OSEventName = (INT8U *)(void *)"?"; /* Unknown name */
  1199. #endif
  1200. OSEventFreeList = &OSEventTbl[0];
  1201. #else
  1202. OSEventFreeList = &OSEventTbl[0]; /* Only have ONE event control block */
  1203. OSEventFreeList->OSEventType = OS_EVENT_TYPE_UNUSED;
  1204. OSEventFreeList->OSEventPtr = (OS_EVENT *)0;
  1205. #if OS_EVENT_NAME_EN > 0u
  1206. OSEventFreeList->OSEventName = (INT8U *)"?"; /* Unknown name */
  1207. #endif
  1208. #endif
  1209. #endif
  1210. }
  1211. /*$PAGE*/
  1212. /*
  1213. *********************************************************************************************************
  1214. * INITIALIZATION
  1215. * INITIALIZE MISCELLANEOUS VARIABLES
  1216. *
  1217. * Description: This function is called by OSInit() to initialize miscellaneous variables.
  1218. *
  1219. * Arguments : none
  1220. *
  1221. * Returns : none
  1222. *********************************************************************************************************
  1223. */
  1224. static void OS_InitMisc (void)
  1225. {
  1226. #if OS_TIME_GET_SET_EN > 0u
  1227. OSTime = 0uL; /* Clear the 32-bit system clock */
  1228. #endif
  1229. OSIntNesting = 0u; /* Clear the interrupt nesting counter */
  1230. OSLockNesting = 0u; /* Clear the scheduling lock counter */
  1231. OSTaskCtr = 0u; /* Clear the number of tasks */
  1232. OSRunning = OS_FALSE; /* Indicate that multitasking not started */
  1233. OSCtxSwCtr = 0u; /* Clear the context switch counter */
  1234. OSIdleCtr = 0uL; /* Clear the 32-bit idle counter */
  1235. #if OS_TASK_STAT_EN > 0u
  1236. OSIdleCtrRun = 0uL;
  1237. OSIdleCtrMax = 0uL;
  1238. OSStatRdy = OS_FALSE; /* Statistic task is not ready */
  1239. #endif
  1240. #ifdef OS_SAFETY_CRITICAL_IEC61508
  1241. OSSafetyCriticalStartFlag = OS_FALSE; /* Still allow creation of objects */
  1242. #endif
  1243. #if OS_TASK_REG_TBL_SIZE > 0u
  1244. OSTaskRegNextAvailID = 0u; /* Initialize the task register ID */
  1245. #endif
  1246. }
  1247. /*$PAGE*/
  1248. /*
  1249. *********************************************************************************************************
  1250. * INITIALIZATION
  1251. * INITIALIZE THE READY LIST
  1252. *
  1253. * Description: This function is called by OSInit() to initialize the Ready List.
  1254. *
  1255. * Arguments : none
  1256. *
  1257. * Returns : none
  1258. *********************************************************************************************************
  1259. */
  1260. static void OS_InitRdyList (void)
  1261. {
  1262. INT8U i;
  1263. OSRdyGrp = 0u; /* Clear the ready list */
  1264. for (i = 0u; i < OS_RDY_TBL_SIZE; i++) {
  1265. OSRdyTbl[i] = 0u;
  1266. }
  1267. OSPrioCur = 0u;
  1268. OSPrioHighRdy = 0u;
  1269. OSTCBHighRdy = (OS_TCB *)0;
  1270. OSTCBCur = (OS_TCB *)0;
  1271. }
  1272. /*$PAGE*/
  1273. /*
  1274. *********************************************************************************************************
  1275. * INITIALIZATION
  1276. * CREATING THE IDLE TASK
  1277. *
  1278. * Description: This function creates the Idle Task.
  1279. *
  1280. * Arguments : none
  1281. *
  1282. * Returns : none
  1283. *********************************************************************************************************
  1284. */
  1285. static void OS_InitTaskIdle (void)
  1286. {
  1287. #if OS_TASK_NAME_EN > 0u
  1288. INT8U err;
  1289. #endif
  1290. #if OS_TASK_CREATE_EXT_EN > 0u
  1291. #if OS_STK_GROWTH == 1u
  1292. (void)OSTaskCreateExt(OS_TaskIdle,
  1293. (void *)0, /* No arguments passed to OS_TaskIdle() */
  1294. &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1u],/* Set Top-Of-Stack */
  1295. OS_TASK_IDLE_PRIO, /* Lowest priority level */
  1296. OS_TASK_IDLE_ID,
  1297. &OSTaskIdleStk[0], /* Set Bottom-Of-Stack */
  1298. OS_TASK_IDLE_STK_SIZE,
  1299. (void *)0, /* No TCB extension */
  1300. OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack */
  1301. #else
  1302. (void)OSTaskCreateExt(OS_TaskIdle,
  1303. (void *)0, /* No arguments passed to OS_TaskIdle() */
  1304. &OSTaskIdleStk[0], /* Set Top-Of-Stack */
  1305. OS_TASK_IDLE_PRIO, /* Lowest priority level */
  1306. OS_TASK_IDLE_ID,
  1307. &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1u],/* Set Bottom-Of-Stack */
  1308. OS_TASK_IDLE_STK_SIZE,
  1309. (void *)0, /* No TCB extension */
  1310. OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack */
  1311. #endif
  1312. #else
  1313. #if OS_STK_GROWTH == 1u
  1314. (void)OSTaskCreate(OS_TaskIdle,
  1315. (void *)0,
  1316. &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1u],
  1317. OS_TASK_IDLE_PRIO);
  1318. #else
  1319. (void)OSTaskCreate(OS_TaskIdle,
  1320. (void *)0,
  1321. &OSTaskIdleStk[0],
  1322. OS_TASK_IDLE_PRIO);
  1323. #endif
  1324. #endif
  1325. #if OS_TASK_NAME_EN > 0u
  1326. OSTaskNameSet(OS_TASK_IDLE_PRIO, (INT8U *)(void *)"uC/OS-II Idle", &err);
  1327. #endif
  1328. }
  1329. /*$PAGE*/
  1330. /*
  1331. *********************************************************************************************************
  1332. * INITIALIZATION
  1333. * CREATING THE STATISTIC TASK
  1334. *
  1335. * Description: This function creates the Statistic Task.
  1336. *
  1337. * Arguments : none
  1338. *
  1339. * Returns : none
  1340. *********************************************************************************************************
  1341. */
  1342. #if OS_TASK_STAT_EN > 0u
  1343. static void OS_InitTaskStat (void)
  1344. {
  1345. #if OS_TASK_NAME_EN > 0u
  1346. INT8U err;
  1347. #endif
  1348. #if OS_TASK_CREATE_EXT_EN > 0u
  1349. #if OS_STK_GROWTH == 1u
  1350. (void)OSTaskCreateExt(OS_TaskStat,
  1351. (void *)0, /* No args passed to OS_TaskStat()*/
  1352. &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1u], /* Set Top-Of-Stack */
  1353. OS_TASK_STAT_PRIO, /* One higher than the idle task */
  1354. OS_TASK_STAT_ID,
  1355. &OSTaskStatStk[0], /* Set Bottom-Of-Stack */
  1356. OS_TASK_STAT_STK_SIZE,
  1357. (void *)0, /* No TCB extension */
  1358. OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear */
  1359. #else
  1360. (void)OSTaskCreateExt(OS_TaskStat,
  1361. (void *)0, /* No args passed to OS_TaskStat()*/
  1362. &OSTaskStatStk[0], /* Set Top-Of-Stack */
  1363. OS_TASK_STAT_PRIO, /* One higher than the idle task */
  1364. OS_TASK_STAT_ID,
  1365. &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1u], /* Set Bottom-Of-Stack */
  1366. OS_TASK_STAT_STK_SIZE,
  1367. (void *)0, /* No TCB extension */
  1368. OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear */
  1369. #endif
  1370. #else
  1371. #if OS_STK_GROWTH == 1u
  1372. (void)OSTaskCreate(OS_TaskStat,
  1373. (void *)0, /* No args passed to OS_TaskStat()*/
  1374. &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1u], /* Set Top-Of-Stack */
  1375. OS_TASK_STAT_PRIO); /* One higher than the idle task */
  1376. #else
  1377. (void)OSTaskCreate(OS_TaskStat,
  1378. (void *)0, /* No args passed to OS_TaskStat()*/
  1379. &OSTaskStatStk[0], /* Set Top-Of-Stack */
  1380. OS_TASK_STAT_PRIO); /* One higher than the idle task */
  1381. #endif
  1382. #endif
  1383. #if OS_TASK_NAME_EN > 0u
  1384. OSTaskNameSet(OS_TASK_STAT_PRIO, (INT8U *)(void *)"uC/OS-II Stat", &err);
  1385. #endif
  1386. }
  1387. #endif
  1388. /*$PAGE*/
  1389. /*
  1390. *********************************************************************************************************
  1391. * INITIALIZATION
  1392. * INITIALIZE THE FREE LIST OF TASK CONTROL BLOCKS
  1393. *
  1394. * Description: This function is called by OSInit() to initialize the free list of OS_TCBs.
  1395. *
  1396. * Arguments : none
  1397. *
  1398. * Returns : none
  1399. *********************************************************************************************************
  1400. */
  1401. static void OS_InitTCBList (void)
  1402. {
  1403. INT8U ix;
  1404. INT8U ix_next;
  1405. OS_TCB *ptcb1;
  1406. OS_TCB *ptcb2;
  1407. OS_MemClr((INT8U *)&OSTCBTbl[0], sizeof(OSTCBTbl)); /* Clear all the TCBs */
  1408. OS_MemClr((INT8U *)&OSTCBPrioTbl[0], sizeof(OSTCBPrioTbl)); /* Clear the priority table */
  1409. for (ix = 0u; ix < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1u); ix++) { /* Init. list of free TCBs */
  1410. ix_next = ix + 1u;
  1411. ptcb1 = &OSTCBTbl[ix];
  1412. ptcb2 = &OSTCBTbl[ix_next];
  1413. ptcb1->OSTCBNext = ptcb2;
  1414. #if OS_TASK_NAME_EN > 0u
  1415. ptcb1->OSTCBTaskName = (INT8U *)(void *)"?"; /* Unknown name */
  1416. #endif
  1417. }
  1418. ptcb1 = &OSTCBTbl[ix];
  1419. ptcb1->OSTCBNext = (OS_TCB *)0; /* Last OS_TCB */
  1420. #if OS_TASK_NAME_EN > 0u
  1421. ptcb1->OSTCBTaskName = (INT8U *)(void *)"?"; /* Unknown name */
  1422. #endif
  1423. OSTCBList = (OS_TCB *)0; /* TCB lists initializations */
  1424. OSTCBFreeList = &OSTCBTbl[0];
  1425. }
  1426. /*$PAGE*/
  1427. /*
  1428. *********************************************************************************************************
  1429. * CLEAR A SECTION OF MEMORY
  1430. *
  1431. * Description: This function is called by other uC/OS-II services to clear a contiguous block of RAM.
  1432. *
  1433. * Arguments : pdest is the start of the RAM to clear (i.e. write 0x00 to)
  1434. *
  1435. * size is the number of bytes to clear.
  1436. *
  1437. * Returns : none
  1438. *
  1439. * Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
  1440. * 2) Note that we can only clear up to 64K bytes of RAM. This is not an issue because none
  1441. * of the uses of this function gets close to this limit.
  1442. * 3) The clear is done one byte at a time since this will work on any processor irrespective
  1443. * of the alignment of the destination.
  1444. *********************************************************************************************************
  1445. */
  1446. void OS_MemClr (INT8U *pdest,
  1447. INT16U size)
  1448. {
  1449. while (size > 0u) {
  1450. *pdest++ = (INT8U)0;
  1451. size--;
  1452. }
  1453. }
  1454. /*$PAGE*/
  1455. /*
  1456. *********************************************************************************************************
  1457. * COPY A BLOCK OF MEMORY
  1458. *
  1459. * Description: This function is called by other uC/OS-II services to copy a block of memory from one
  1460. * location to another.
  1461. *
  1462. * Arguments : pdest is a pointer to the 'destination' memory block
  1463. *
  1464. * psrc is a pointer to the 'source' memory block
  1465. *
  1466. * size is the number of bytes to copy.
  1467. *
  1468. * Returns : none
  1469. *
  1470. * Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it. There is
  1471. * no provision to handle overlapping memory copy. However, that's not a problem since this
  1472. * is not a situation that will happen.
  1473. * 2) Note that we can only copy up to 64K bytes of RAM
  1474. * 3) The copy is done one byte at a time since this will work on any processor irrespective
  1475. * of the alignment of the source and destination.
  1476. *********************************************************************************************************
  1477. */
  1478. void OS_MemCopy (INT8U *pdest,
  1479. INT8U *psrc,
  1480. INT16U size)
  1481. {
  1482. while (size > 0u) {
  1483. *pdest++ = *psrc++;
  1484. size--;
  1485. }
  1486. }
  1487. /*$PAGE*/
  1488. /*
  1489. *********************************************************************************************************
  1490. * SCHEDULER
  1491. *
  1492. * Description: This function is called by other uC/OS-II services to determine whether a new, high
  1493. * priority task has been made ready to run. This function is invoked by TASK level code
  1494. * and is not used to reschedule tasks from ISRs (see OSIntExit() for ISR rescheduling).
  1495. *
  1496. * Arguments : none
  1497. *
  1498. * Returns : none
  1499. *
  1500. * Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
  1501. * 2) Rescheduling is prevented when the scheduler is locked (see OS_SchedLock())
  1502. *********************************************************************************************************
  1503. */
  1504. void OS_Sched (void)
  1505. {
  1506. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  1507. OS_CPU_SR cpu_sr = 0u;
  1508. #endif
  1509. OS_ENTER_CRITICAL();
  1510. if (OSIntNesting == 0u) { /* Schedule only if all ISRs done and ... */
  1511. if (OSLockNesting == 0u) { /* ... scheduler is not locked */
  1512. OS_SchedNew();
  1513. OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
  1514. if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
  1515. #if OS_TASK_PROFILE_EN > 0u
  1516. OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
  1517. #endif
  1518. OSCtxSwCtr++; /* Increment context switch counter */
  1519. #if OS_TASK_CREATE_EXT_EN > 0u
  1520. #if defined(OS_TLS_TBL_SIZE) && (OS_TLS_TBL_SIZE > 0u)
  1521. OS_TLS_TaskSw();
  1522. #endif
  1523. #endif
  1524. OS_TASK_SW(); /* Perform a context switch */
  1525. }
  1526. }
  1527. }
  1528. OS_EXIT_CRITICAL();
  1529. }
  1530. /*
  1531. *********************************************************************************************************
  1532. * FIND HIGHEST PRIORITY TASK READY TO RUN
  1533. *
  1534. * Description: This function is called by other uC/OS-II services to determine the highest priority task
  1535. * that is ready to run. The global variable 'OSPrioHighRdy' is changed accordingly.
  1536. *
  1537. * Arguments : none
  1538. *
  1539. * Returns : none
  1540. *
  1541. * Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
  1542. * 2) Interrupts are assumed to be disabled when this function is called.
  1543. *********************************************************************************************************
  1544. */
  1545. static void OS_SchedNew (void)
  1546. {
  1547. #if OS_LOWEST_PRIO <= 63u /* See if we support up to 64 tasks */
  1548. INT8U y;
  1549. y = OSUnMapTbl[OSRdyGrp];
  1550. OSPrioHighRdy = (INT8U)((y << 3u) + OSUnMapTbl[OSRdyTbl[y]]);
  1551. #else /* We support up to 256 tasks */
  1552. INT8U y;
  1553. OS_PRIO *ptbl;
  1554. if ((OSRdyGrp & 0xFFu) != 0u) {
  1555. y = OSUnMapTbl[OSRdyGrp & 0xFFu];
  1556. } else {
  1557. y = OSUnMapTbl[(OS_PRIO)(OSRdyGrp >> 8u) & 0xFFu] + 8u;
  1558. }
  1559. ptbl = &OSRdyTbl[y];
  1560. if ((*ptbl & 0xFFu) != 0u) {
  1561. OSPrioHighRdy = (INT8U)((y << 4u) + OSUnMapTbl[(*ptbl & 0xFFu)]);
  1562. } else {
  1563. OSPrioHighRdy = (INT8U)((y << 4u) + OSUnMapTbl[(OS_PRIO)(*ptbl >> 8u) & 0xFFu] + 8u);
  1564. }
  1565. #endif
  1566. }
  1567. /*$PAGE*/
  1568. /*
  1569. *********************************************************************************************************
  1570. * DETERMINE THE LENGTH OF AN ASCII STRING
  1571. *
  1572. * Description: This function is called by other uC/OS-II services to determine the size of an ASCII string
  1573. * (excluding the NUL character).
  1574. *
  1575. * Arguments : psrc is a pointer to the string for which we need to know the size.
  1576. *
  1577. * Returns : The size of the string (excluding the NUL terminating character)
  1578. *
  1579. * Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
  1580. * 2) The string to check must be less than 255 characters long.
  1581. *********************************************************************************************************
  1582. */
  1583. #if (OS_EVENT_NAME_EN > 0u) || (OS_FLAG_NAME_EN > 0u) || (OS_MEM_NAME_EN > 0u) || (OS_TASK_NAME_EN > 0u) || (OS_TMR_CFG_NAME_EN > 0u)
  1584. INT8U OS_StrLen (INT8U *psrc)
  1585. {
  1586. INT8U len;
  1587. #if OS_ARG_CHK_EN > 0u
  1588. if (psrc == (INT8U *)0) {
  1589. return (0u);
  1590. }
  1591. #endif
  1592. len = 0u;
  1593. while (*psrc != OS_ASCII_NUL) {
  1594. psrc++;
  1595. len++;
  1596. }
  1597. return (len);
  1598. }
  1599. #endif
  1600. /*$PAGE*/
  1601. /*
  1602. *********************************************************************************************************
  1603. * IDLE TASK
  1604. *
  1605. * Description: This task is internal to uC/OS-II and executes whenever no other higher priority tasks
  1606. * executes because they are ALL waiting for event(s) to occur.
  1607. *
  1608. * Arguments : none
  1609. *
  1610. * Returns : none
  1611. *
  1612. * Note(s) : 1) OSTaskIdleHook() is called after the critical section to ensure that interrupts will be
  1613. * enabled for at least a few instructions. On some processors (ex. Philips XA), enabling
  1614. * and then disabling interrupts didn't allow the processor enough time to have interrupts
  1615. * enabled before they were disabled again. uC/OS-II would thus never recognize
  1616. * interrupts.
  1617. * 2) This hook has been added to allow you to do such things as STOP the CPU to conserve
  1618. * power.
  1619. *********************************************************************************************************
  1620. */
  1621. void OS_TaskIdle (void *p_arg)
  1622. {
  1623. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  1624. OS_CPU_SR cpu_sr = 0u;
  1625. #endif
  1626. p_arg = p_arg; /* Prevent compiler warning for not using 'p_arg' */
  1627. for (;;) {
  1628. OS_ENTER_CRITICAL();
  1629. OSIdleCtr++;
  1630. OS_EXIT_CRITICAL();
  1631. OSTaskIdleHook(); /* Call user definable HOOK */
  1632. }
  1633. }
  1634. /*$PAGE*/
  1635. /*
  1636. *********************************************************************************************************
  1637. * STATISTICS TASK
  1638. *
  1639. * Description: This task is internal to uC/OS-II and is used to compute some statistics about the
  1640. * multitasking environment. Specifically, OS_TaskStat() computes the CPU usage.
  1641. * CPU usage is determined by:
  1642. *
  1643. * OSIdleCtr
  1644. * OSCPUUsage = 100 * (1 - ------------) (units are in %)
  1645. * OSIdleCtrMax
  1646. *
  1647. * Arguments : parg this pointer is not used at this time.
  1648. *
  1649. * Returns : none
  1650. *
  1651. * Notes : 1) This task runs at a priority level higher than the idle task. In fact, it runs at the
  1652. * next higher priority, OS_TASK_IDLE_PRIO-1.
  1653. * 2) You can disable this task by setting the configuration #define OS_TASK_STAT_EN to 0.
  1654. * 3) You MUST have at least a delay of 2/10 seconds to allow for the system to establish the
  1655. * maximum value for the idle counter.
  1656. *********************************************************************************************************
  1657. */
  1658. #if OS_TASK_STAT_EN > 0u
  1659. void OS_TaskStat (void *p_arg)
  1660. {
  1661. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  1662. OS_CPU_SR cpu_sr = 0u;
  1663. #endif
  1664. p_arg = p_arg; /* Prevent compiler warning for not using 'p_arg' */
  1665. while (OSStatRdy == OS_FALSE) {
  1666. OSTimeDly(2u * OS_TICKS_PER_SEC / 10u); /* Wait until statistic task is ready */
  1667. }
  1668. OSIdleCtrMax /= 100uL;
  1669. if (OSIdleCtrMax == 0uL) {
  1670. OSCPUUsage = 0u;
  1671. #if OS_TASK_SUSPEND_EN > 0u
  1672. (void)OSTaskSuspend(OS_PRIO_SELF);
  1673. #else
  1674. for (;;) {
  1675. OSTimeDly(OS_TICKS_PER_SEC);
  1676. }
  1677. #endif
  1678. }
  1679. OS_ENTER_CRITICAL();
  1680. OSIdleCtr = OSIdleCtrMax * 100uL; /* Set initial CPU usage as 0% */
  1681. OS_EXIT_CRITICAL();
  1682. for (;;) {
  1683. OS_ENTER_CRITICAL();
  1684. OSIdleCtrRun = OSIdleCtr; /* Obtain the of the idle counter for the past second */
  1685. OSIdleCtr = 0uL; /* Reset the idle counter for the next second */
  1686. OS_EXIT_CRITICAL();
  1687. OSCPUUsage = (INT8U)(100uL - OSIdleCtrRun / OSIdleCtrMax);
  1688. OSTaskStatHook(); /* Invoke user definable hook */
  1689. #if (OS_TASK_STAT_STK_CHK_EN > 0u) && (OS_TASK_CREATE_EXT_EN > 0u)
  1690. OS_TaskStatStkChk(); /* Check the stacks for each task */
  1691. #endif
  1692. OSTimeDly(OS_TICKS_PER_SEC / 10u); /* Accumulate OSIdleCtr for the next 1/10 second */
  1693. }
  1694. }
  1695. #endif
  1696. /*$PAGE*/
  1697. /*
  1698. *********************************************************************************************************
  1699. * CHECK ALL TASK STACKS
  1700. *
  1701. * Description: This function is called by OS_TaskStat() to check the stacks of each active task.
  1702. *
  1703. * Arguments : none
  1704. *
  1705. * Returns : none
  1706. *********************************************************************************************************
  1707. */
  1708. #if (OS_TASK_STAT_STK_CHK_EN > 0u) && (OS_TASK_CREATE_EXT_EN > 0u)
  1709. void OS_TaskStatStkChk (void)
  1710. {
  1711. OS_TCB *ptcb;
  1712. OS_STK_DATA stk_data;
  1713. INT8U err;
  1714. INT8U prio;
  1715. for (prio = 0u; prio <= OS_TASK_IDLE_PRIO; prio++) {
  1716. err = OSTaskStkChk(prio, &stk_data);
  1717. if (err == OS_ERR_NONE) {
  1718. ptcb = OSTCBPrioTbl[prio];
  1719. if (ptcb != (OS_TCB *)0) { /* Make sure task 'ptcb' is ... */
  1720. if (ptcb != OS_TCB_RESERVED) { /* ... still valid. */
  1721. #if OS_TASK_PROFILE_EN > 0u
  1722. #if OS_STK_GROWTH == 1u
  1723. ptcb->OSTCBStkBase = ptcb->OSTCBStkBottom + ptcb->OSTCBStkSize;
  1724. #else
  1725. ptcb->OSTCBStkBase = ptcb->OSTCBStkBottom - ptcb->OSTCBStkSize;
  1726. #endif
  1727. ptcb->OSTCBStkUsed = stk_data.OSUsed; /* Store number of entries used */
  1728. #endif
  1729. }
  1730. }
  1731. }
  1732. }
  1733. }
  1734. #endif
  1735. /*$PAGE*/
  1736. /*
  1737. *********************************************************************************************************
  1738. * INITIALIZE TCB
  1739. *
  1740. * Description: This function is internal to uC/OS-II and is used to initialize a Task Control Block when
  1741. * a task is created (see OSTaskCreate() and OSTaskCreateExt()).
  1742. *
  1743. * Arguments : prio is the priority of the task being created
  1744. *
  1745. * ptos is a pointer to the task's top-of-stack assuming that the CPU registers
  1746. * have been placed on the stack. Note that the top-of-stack corresponds to a
  1747. * 'high' memory location is OS_STK_GROWTH is set to 1 and a 'low' memory
  1748. * location if OS_STK_GROWTH is set to 0. Note that stack growth is CPU
  1749. * specific.
  1750. *
  1751. * pbos is a pointer to the bottom of stack. A NULL pointer is passed if called by
  1752. * 'OSTaskCreate()'.
  1753. *
  1754. * id is the task's ID (0..65535)
  1755. *
  1756. * stk_size is the size of the stack (in 'stack units'). If the stack units are INT8Us
  1757. * then, 'stk_size' contains the number of bytes for the stack. If the stack
  1758. * units are INT32Us then, the stack contains '4 * stk_size' bytes. The stack
  1759. * units are established by the #define constant OS_STK which is CPU
  1760. * specific. 'stk_size' is 0 if called by 'OSTaskCreate()'.
  1761. *
  1762. * pext is a pointer to a user supplied memory area that is used to extend the task
  1763. * control block. This allows you to store the contents of floating-point
  1764. * registers, MMU registers or anything else you could find useful during a
  1765. * context switch. You can even assign a name to each task and store this name
  1766. * in this TCB extension. A NULL pointer is passed if called by OSTaskCreate().
  1767. *
  1768. * opt options as passed to 'OSTaskCreateExt()' or,
  1769. * 0 if called from 'OSTaskCreate()'.
  1770. *
  1771. * Returns : OS_ERR_NONE if the call was successful
  1772. * OS_ERR_TASK_NO_MORE_TCB if there are no more free TCBs to be allocated and thus, the task
  1773. * cannot be created.
  1774. *
  1775. * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
  1776. *********************************************************************************************************
  1777. */
  1778. INT8U OS_TCBInit (INT8U prio,
  1779. OS_STK *ptos,
  1780. OS_STK *pbos,
  1781. INT16U id,
  1782. INT32U stk_size,
  1783. void *pext,
  1784. INT16U opt)
  1785. {
  1786. OS_TCB *ptcb;
  1787. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  1788. OS_CPU_SR cpu_sr = 0u;
  1789. #endif
  1790. #if OS_TASK_REG_TBL_SIZE > 0u
  1791. INT8U i;
  1792. #endif
  1793. #if OS_TASK_CREATE_EXT_EN > 0u
  1794. #if defined(OS_TLS_TBL_SIZE) && (OS_TLS_TBL_SIZE > 0u)
  1795. INT8U j;
  1796. #endif
  1797. #endif
  1798. OS_ENTER_CRITICAL();
  1799. ptcb = OSTCBFreeList; /* Get a free TCB from the free TCB list */
  1800. if (ptcb != (OS_TCB *)0) {
  1801. OSTCBFreeList = ptcb->OSTCBNext; /* Update pointer to free TCB list */
  1802. OS_EXIT_CRITICAL();
  1803. ptcb->OSTCBStkPtr = ptos; /* Load Stack pointer in TCB */
  1804. ptcb->OSTCBPrio = prio; /* Load task priority into TCB */
  1805. ptcb->OSTCBStat = OS_STAT_RDY; /* Task is ready to run */
  1806. ptcb->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */
  1807. ptcb->OSTCBDly = 0u; /* Task is not delayed */
  1808. #if OS_TASK_CREATE_EXT_EN > 0u
  1809. ptcb->OSTCBExtPtr = pext; /* Store pointer to TCB extension */
  1810. ptcb->OSTCBStkSize = stk_size; /* Store stack size */
  1811. ptcb->OSTCBStkBottom = pbos; /* Store pointer to bottom of stack */
  1812. ptcb->OSTCBOpt = opt; /* Store task options */
  1813. ptcb->OSTCBId = id; /* Store task ID */
  1814. #else
  1815. pext = pext; /* Prevent compiler warning if not used */
  1816. stk_size = stk_size;
  1817. pbos = pbos;
  1818. opt = opt;
  1819. id = id;
  1820. #endif
  1821. #if OS_TASK_DEL_EN > 0u
  1822. ptcb->OSTCBDelReq = OS_ERR_NONE;
  1823. #endif
  1824. #if OS_LOWEST_PRIO <= 63u /* Pre-compute X, Y */
  1825. ptcb->OSTCBY = (INT8U)(prio >> 3u);
  1826. ptcb->OSTCBX = (INT8U)(prio & 0x07u);
  1827. #else /* Pre-compute X, Y */
  1828. ptcb->OSTCBY = (INT8U)((INT8U)(prio >> 4u) & 0xFFu);
  1829. ptcb->OSTCBX = (INT8U) (prio & 0x0Fu);
  1830. #endif
  1831. /* Pre-compute BitX and BitY */
  1832. ptcb->OSTCBBitY = (OS_PRIO)(1uL << ptcb->OSTCBY);
  1833. ptcb->OSTCBBitX = (OS_PRIO)(1uL << ptcb->OSTCBX);
  1834. #if (OS_EVENT_EN)
  1835. ptcb->OSTCBEventPtr = (OS_EVENT *)0; /* Task is not pending on an event */
  1836. #if (OS_EVENT_MULTI_EN > 0u)
  1837. ptcb->OSTCBEventMultiPtr = (OS_EVENT **)0; /* Task is not pending on any events */
  1838. #endif
  1839. #endif
  1840. #if (OS_FLAG_EN > 0u) && (OS_MAX_FLAGS > 0u) && (OS_TASK_DEL_EN > 0u)
  1841. ptcb->OSTCBFlagNode = (OS_FLAG_NODE *)0; /* Task is not pending on an event flag */
  1842. #endif
  1843. #if (OS_MBOX_EN > 0u) || ((OS_Q_EN > 0u) && (OS_MAX_QS > 0u))
  1844. ptcb->OSTCBMsg = (void *)0; /* No message received */
  1845. #endif
  1846. #if OS_TASK_PROFILE_EN > 0u
  1847. ptcb->OSTCBCtxSwCtr = 0uL; /* Initialize profiling variables */
  1848. ptcb->OSTCBCyclesStart = 0uL;
  1849. ptcb->OSTCBCyclesTot = 0uL;
  1850. ptcb->OSTCBStkBase = (OS_STK *)0;
  1851. ptcb->OSTCBStkUsed = 0uL;
  1852. #endif
  1853. #if OS_TASK_NAME_EN > 0u
  1854. ptcb->OSTCBTaskName = (INT8U *)(void *)"?";
  1855. #endif
  1856. #if OS_TASK_REG_TBL_SIZE > 0u /* Initialize the task variables */
  1857. for (i = 0u; i < OS_TASK_REG_TBL_SIZE; i++) {
  1858. ptcb->OSTCBRegTbl[i] = 0u;
  1859. }
  1860. #endif
  1861. OSTCBInitHook(ptcb);
  1862. OS_ENTER_CRITICAL();
  1863. OSTCBPrioTbl[prio] = ptcb;
  1864. OS_EXIT_CRITICAL();
  1865. OSTaskCreateHook(ptcb); /* Call user defined hook */
  1866. #if OS_TASK_CREATE_EXT_EN > 0u
  1867. #if defined(OS_TLS_TBL_SIZE) && (OS_TLS_TBL_SIZE > 0u)
  1868. for (j = 0u; j < OS_TLS_TBL_SIZE; j++) {
  1869. ptcb->OSTCBTLSTbl[j] = (OS_TLS)0;
  1870. }
  1871. OS_TLS_TaskCreate(ptcb); /* Call TLS hook */
  1872. #endif
  1873. #endif
  1874. OS_ENTER_CRITICAL();
  1875. ptcb->OSTCBNext = OSTCBList; /* Link into TCB chain */
  1876. ptcb->OSTCBPrev = (OS_TCB *)0;
  1877. if (OSTCBList != (OS_TCB *)0) {
  1878. OSTCBList->OSTCBPrev = ptcb;
  1879. }
  1880. OSTCBList = ptcb;
  1881. OSRdyGrp |= ptcb->OSTCBBitY; /* Make task ready to run */
  1882. OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
  1883. OSTaskCtr++; /* Increment the #tasks counter */
  1884. OS_EXIT_CRITICAL();
  1885. return (OS_ERR_NONE);
  1886. }
  1887. OS_EXIT_CRITICAL();
  1888. return (OS_ERR_TASK_NO_MORE_TCB);
  1889. }