os_core.c 87 KB

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