os_task.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845
  1. /*
  2. *********************************************************************************************************
  3. * uC/OS-III
  4. * The Real-Time Kernel
  5. *
  6. * Copyright 2009-2022 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. * TASK MANAGEMENT
  19. *
  20. * File : os_task.c
  21. * Version : V3.08.02
  22. *********************************************************************************************************
  23. */
  24. #define MICRIUM_SOURCE
  25. #include "os.h"
  26. #ifdef VSC_INCLUDE_SOURCE_FILE_NAMES
  27. const CPU_CHAR *os_task__c = "$Id: $";
  28. #endif
  29. /*
  30. ************************************************************************************************************************
  31. * CHANGE PRIORITY OF A TASK
  32. *
  33. * Description: This function allows you to change the priority of a task dynamically. Note that the new
  34. * priority MUST be available.
  35. *
  36. * Arguments : p_tcb is the TCB of the tack to change the priority for
  37. *
  38. * prio_new is the new priority
  39. *
  40. * p_err is a pointer to an error code returned by this function:
  41. *
  42. * OS_ERR_NONE Is the call was successful
  43. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  44. * OS_ERR_PRIO_INVALID If the priority you specify is higher that the maximum allowed
  45. * (i.e. >= (OS_CFG_PRIO_MAX-1)) or already in use by a kernel
  46. * task
  47. * OS_ERR_STATE_INVALID If the task is in an invalid state
  48. * OS_ERR_TASK_CHANGE_PRIO_ISR If you tried to change the task's priority from an ISR
  49. *
  50. * Returns : none
  51. *
  52. * Note(s) : none
  53. ************************************************************************************************************************
  54. */
  55. #if (OS_CFG_TASK_CHANGE_PRIO_EN > 0u)
  56. void OSTaskChangePrio (OS_TCB *p_tcb,
  57. OS_PRIO prio_new,
  58. OS_ERR *p_err)
  59. {
  60. #if (OS_CFG_MUTEX_EN > 0u)
  61. OS_PRIO prio_high;
  62. #endif
  63. CPU_SR_ALLOC();
  64. #ifdef OS_SAFETY_CRITICAL
  65. if (p_err == (OS_ERR *)0) {
  66. OS_SAFETY_CRITICAL_EXCEPTION();
  67. return;
  68. }
  69. #endif
  70. #if (OS_CFG_ARG_CHK_EN > 0u)
  71. if ((p_tcb != (OS_TCB *)0) && (p_tcb->TaskState == OS_TASK_STATE_DEL)) {
  72. *p_err = OS_ERR_STATE_INVALID;
  73. return;
  74. }
  75. #endif
  76. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  77. if (OSIntNestingCtr > 0u) { /* Not allowed to call from an ISR */
  78. *p_err = OS_ERR_TASK_CHANGE_PRIO_ISR;
  79. return;
  80. }
  81. #endif
  82. if (prio_new >= (OS_CFG_PRIO_MAX - 1u)) { /* Cannot set to Idle Task priority */
  83. *p_err = OS_ERR_PRIO_INVALID;
  84. return;
  85. }
  86. CPU_CRITICAL_ENTER();
  87. if (p_tcb == (OS_TCB *)0) { /* Are we changing the priority of 'self'? */
  88. if (OSRunning != OS_STATE_OS_RUNNING) {
  89. CPU_CRITICAL_EXIT();
  90. *p_err = OS_ERR_OS_NOT_RUNNING;
  91. return;
  92. }
  93. p_tcb = OSTCBCurPtr;
  94. }
  95. #if (OS_CFG_MUTEX_EN > 0u)
  96. p_tcb->BasePrio = prio_new; /* Update base priority */
  97. if (p_tcb->MutexGrpHeadPtr != (OS_MUTEX *)0) { /* Owning a mutex? */
  98. if (prio_new > p_tcb->Prio) {
  99. prio_high = OS_MutexGrpPrioFindHighest(p_tcb);
  100. if (prio_new > prio_high) {
  101. prio_new = prio_high;
  102. }
  103. }
  104. }
  105. #endif
  106. OS_TaskChangePrio(p_tcb, prio_new);
  107. OS_TRACE_TASK_PRIO_CHANGE(p_tcb, prio_new);
  108. CPU_CRITICAL_EXIT();
  109. if (OSRunning == OS_STATE_OS_RUNNING) {
  110. OSSched(); /* Run highest priority task ready */
  111. }
  112. *p_err = OS_ERR_NONE;
  113. }
  114. #endif
  115. /*
  116. ************************************************************************************************************************
  117. * CREATE A TASK
  118. *
  119. * Description: This function is used to have uC/OS-III manage the execution of a task. Tasks can either be created
  120. * prior to the start of multitasking or by a running task. A task cannot be created by an ISR.
  121. *
  122. * Arguments : p_tcb is a pointer to the task's TCB
  123. *
  124. * p_name is a pointer to an ASCII string to provide a name to the task.
  125. *
  126. * p_task is a pointer to the task's code
  127. *
  128. * p_arg is a pointer to an optional data area which can be used to pass parameters to
  129. * the task when the task first executes. Where the task is concerned it thinks
  130. * it was invoked and passed the argument 'p_arg' as follows:
  131. *
  132. * void Task (void *p_arg)
  133. * {
  134. * for (;;) {
  135. * Task code;
  136. * }
  137. * }
  138. *
  139. * prio is the task's priority. A unique priority MUST be assigned to each task and the
  140. * lower the number, the higher the priority.
  141. *
  142. * p_stk_base is a pointer to the base address of the stack (i.e. low address).
  143. *
  144. * stk_limit is the number of stack elements to set as 'watermark' limit for the stack. This value
  145. * represents the number of CPU_STK entries left before the stack is full. For example,
  146. * specifying 10% of the 'stk_size' value indicates that the stack limit will be reached
  147. * when the stack reaches 90% full.
  148. *
  149. * stk_size is the size of the stack in number of elements. If CPU_STK is set to CPU_INT08U,
  150. * 'stk_size' corresponds to the number of bytes available. If CPU_STK is set to
  151. * CPU_INT16U, 'stk_size' contains the number of 16-bit entries available. Finally, if
  152. * CPU_STK is set to CPU_INT32U, 'stk_size' contains the number of 32-bit entries
  153. * available on the stack.
  154. *
  155. * q_size is the maximum number of messages that can be sent to the task
  156. *
  157. * time_quanta amount of time (in ticks) for time slice when round-robin between tasks. Specify 0 to use
  158. * the default.
  159. *
  160. * p_ext is a pointer to a user supplied memory location which is used as a TCB extension.
  161. * For example, this user memory can hold the contents of floating-point registers
  162. * during a context switch, the time each task takes to execute, the number of times
  163. * the task has been switched-in, etc.
  164. *
  165. * opt contains additional information (or options) about the behavior of the task.
  166. * See OS_OPT_TASK_xxx in OS.H. Current choices are:
  167. *
  168. * OS_OPT_TASK_NONE No option selected
  169. * OS_OPT_TASK_STK_CHK Stack checking to be allowed for the task
  170. * OS_OPT_TASK_STK_CLR Clear the stack when the task is created
  171. * OS_OPT_TASK_SAVE_FP If the CPU has floating-point registers, save them
  172. * during a context switch.
  173. * OS_OPT_TASK_NO_TLS If the caller doesn't want or need TLS (Thread Local
  174. * Storage) support for the task. If you do not include this
  175. * option, TLS will be supported by default.
  176. *
  177. * p_err is a pointer to an error code that will be set during this call. The value pointer
  178. * to by 'p_err' can be:
  179. *
  180. * OS_ERR_NONE If the function was successful
  181. * OS_ERR_ILLEGAL_CREATE_RUN_TIME If you are trying to create the task after you called
  182. * OSSafetyCriticalStart()
  183. * OS_ERR_PRIO_INVALID If the priority you specify is higher that the maximum
  184. * allowed (i.e. >= OS_CFG_PRIO_MAX-1) or,
  185. * OS_ERR_STK_OVF If the stack was overflowed during stack init
  186. * OS_ERR_STK_INVALID If you specified a NULL pointer for 'p_stk_base'
  187. * OS_ERR_STK_SIZE_INVALID If you specified zero for the 'stk_size'
  188. * OS_ERR_STK_LIMIT_INVALID If you specified a 'stk_limit' greater than or equal
  189. * to 'stk_size'
  190. * OS_ERR_TASK_CREATE_ISR If you tried to create a task from an ISR
  191. * OS_ERR_TASK_INVALID If you specified a NULL pointer for 'p_task'
  192. * OS_ERR_TCB_INVALID If you specified a NULL pointer for 'p_tcb'
  193. *
  194. * Returns : none
  195. *
  196. * Note(s) : 1) OSTaskCreate() will return with the error OS_ERR_STK_OVF when a stack overflow is detected
  197. * during stack initialization. In that specific case some memory may have been corrupted. It is
  198. * therefore recommended to treat OS_ERR_STK_OVF as a fatal error.
  199. ************************************************************************************************************************
  200. */
  201. void OSTaskCreate (OS_TCB *p_tcb,
  202. CPU_CHAR *p_name,
  203. OS_TASK_PTR p_task,
  204. void *p_arg,
  205. OS_PRIO prio,
  206. CPU_STK *p_stk_base,
  207. CPU_STK_SIZE stk_limit,
  208. CPU_STK_SIZE stk_size,
  209. OS_MSG_QTY q_size,
  210. OS_TICK time_quanta,
  211. void *p_ext,
  212. OS_OPT opt,
  213. OS_ERR *p_err)
  214. {
  215. CPU_STK_SIZE i;
  216. #if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
  217. OS_REG_ID reg_nbr;
  218. #endif
  219. #if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
  220. OS_TLS_ID id;
  221. #endif
  222. CPU_STK *p_sp;
  223. CPU_STK *p_stk_limit;
  224. CPU_SR_ALLOC();
  225. #ifdef OS_SAFETY_CRITICAL
  226. if (p_err == (OS_ERR *)0) {
  227. OS_SAFETY_CRITICAL_EXCEPTION();
  228. return;
  229. }
  230. #endif
  231. #ifdef OS_SAFETY_CRITICAL_IEC61508
  232. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  233. *p_err = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
  234. return;
  235. }
  236. #endif
  237. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  238. if (OSIntNestingCtr > 0u) { /* --------- CANNOT CREATE A TASK FROM AN ISR --------- */
  239. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  240. *p_err = OS_ERR_TASK_CREATE_ISR;
  241. return;
  242. }
  243. #endif
  244. #if (OS_CFG_ARG_CHK_EN > 0u) /* ---------------- VALIDATE ARGUMENTS ---------------- */
  245. if (p_tcb == (OS_TCB *)0) { /* User must supply a valid OS_TCB */
  246. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  247. *p_err = OS_ERR_TCB_INVALID;
  248. return;
  249. }
  250. if (p_task == (OS_TASK_PTR)0u) { /* User must supply a valid task */
  251. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  252. *p_err = OS_ERR_TASK_INVALID;
  253. return;
  254. }
  255. if (p_stk_base == (CPU_STK *)0) { /* User must supply a valid stack base address */
  256. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  257. *p_err = OS_ERR_STK_INVALID;
  258. return;
  259. }
  260. if (stk_size < OSCfg_StkSizeMin) { /* User must supply a valid minimum stack size */
  261. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  262. *p_err = OS_ERR_STK_SIZE_INVALID;
  263. return;
  264. }
  265. if (stk_limit >= stk_size) { /* User must supply a valid stack limit */
  266. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  267. *p_err = OS_ERR_STK_LIMIT_INVALID;
  268. return;
  269. }
  270. if ((prio > (OS_CFG_PRIO_MAX - 2u)) && /* Priority must be within 0 and OS_CFG_PRIO_MAX-1 */
  271. (prio != (OS_CFG_PRIO_MAX - 1u))) {
  272. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  273. *p_err = OS_ERR_PRIO_INVALID;
  274. return;
  275. }
  276. #endif
  277. if (prio == (OS_CFG_PRIO_MAX - 1u)) {
  278. #if (OS_CFG_TASK_IDLE_EN > 0u)
  279. if (p_tcb != &OSIdleTaskTCB) {
  280. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  281. *p_err = OS_ERR_PRIO_INVALID; /* Not allowed to use same priority as idle task */
  282. return;
  283. }
  284. #else
  285. OS_TRACE_TASK_CREATE_FAILED(p_tcb);
  286. *p_err = OS_ERR_PRIO_INVALID; /* Not allowed to use same priority as idle task */
  287. return;
  288. #endif
  289. }
  290. OS_TaskInitTCB(p_tcb); /* Initialize the TCB to default values */
  291. *p_err = OS_ERR_NONE;
  292. /* -------------- CLEAR THE TASK'S STACK -------------- */
  293. if (((opt & OS_OPT_TASK_STK_CHK) != 0u) || /* See if stack checking has been enabled */
  294. ((opt & OS_OPT_TASK_STK_CLR) != 0u)) { /* See if stack needs to be cleared */
  295. if ((opt & OS_OPT_TASK_STK_CLR) != 0u) {
  296. p_sp = p_stk_base;
  297. for (i = 0u; i < stk_size; i++) { /* Stack grows from HIGH to LOW memory */
  298. *p_sp = 0u; /* Clear from bottom of stack and up! */
  299. p_sp++;
  300. }
  301. }
  302. }
  303. /* ------ INITIALIZE THE STACK FRAME OF THE TASK ------ */
  304. #if (CPU_CFG_STK_GROWTH == CPU_STK_GROWTH_HI_TO_LO)
  305. p_stk_limit = p_stk_base + stk_limit;
  306. #else
  307. p_stk_limit = p_stk_base + (stk_size - 1u) - stk_limit;
  308. #endif
  309. p_sp = OSTaskStkInit(p_task,
  310. p_arg,
  311. p_stk_base,
  312. p_stk_limit,
  313. stk_size,
  314. opt);
  315. #if (CPU_CFG_STK_GROWTH == CPU_STK_GROWTH_HI_TO_LO) /* Check if we overflown the stack during init */
  316. if (p_sp < p_stk_base) {
  317. *p_err = OS_ERR_STK_OVF;
  318. return;
  319. }
  320. #else
  321. if (p_sp > (p_stk_base + stk_size)) {
  322. *p_err = OS_ERR_STK_OVF;
  323. return;
  324. }
  325. #endif
  326. #if (OS_CFG_TASK_STK_REDZONE_EN > 0u) /* Initialize Redzoned stack */
  327. OS_TaskStkRedzoneInit(p_stk_base, stk_size);
  328. #endif
  329. /* ------------ INITIALIZE THE TCB FIELDS ------------- */
  330. #if (OS_CFG_DBG_EN > 0u)
  331. p_tcb->TaskEntryAddr = p_task; /* Save task entry point address */
  332. p_tcb->TaskEntryArg = p_arg; /* Save task entry argument */
  333. #endif
  334. #if (OS_CFG_DBG_EN > 0u)
  335. p_tcb->NamePtr = p_name; /* Save task name */
  336. #else
  337. (void)p_name;
  338. #endif
  339. p_tcb->Prio = prio; /* Save the task's priority */
  340. #if (OS_CFG_MUTEX_EN > 0u)
  341. p_tcb->BasePrio = prio; /* Set the base priority */
  342. #endif
  343. p_tcb->StkPtr = p_sp; /* Save the new top-of-stack pointer */
  344. p_tcb->StkLimitPtr = p_stk_limit; /* Save the stack limit pointer */
  345. #if (OS_CFG_SCHED_ROUND_ROBIN_EN > 0u)
  346. p_tcb->TimeQuanta = time_quanta; /* Save the #ticks for time slice (0 means not sliced) */
  347. if (time_quanta == 0u) {
  348. p_tcb->TimeQuantaCtr = OSSchedRoundRobinDfltTimeQuanta;
  349. } else {
  350. p_tcb->TimeQuantaCtr = time_quanta;
  351. }
  352. #else
  353. (void)time_quanta;
  354. #endif
  355. p_tcb->ExtPtr = p_ext; /* Save pointer to TCB extension */
  356. #if ((OS_CFG_DBG_EN > 0u) || (OS_CFG_STAT_TASK_STK_CHK_EN > 0u) || (OS_CFG_TASK_STK_REDZONE_EN > 0u))
  357. p_tcb->StkBasePtr = p_stk_base; /* Save pointer to the base address of the stack */
  358. p_tcb->StkSize = stk_size; /* Save the stack size (in number of CPU_STK elements) */
  359. #endif
  360. p_tcb->Opt = opt; /* Save task options */
  361. #if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
  362. for (reg_nbr = 0u; reg_nbr < OS_CFG_TASK_REG_TBL_SIZE; reg_nbr++) {
  363. p_tcb->RegTbl[reg_nbr] = 0u;
  364. }
  365. #endif
  366. #if (OS_CFG_TASK_Q_EN > 0u)
  367. OS_MsgQInit(&p_tcb->MsgQ, /* Initialize the task's message queue */
  368. q_size);
  369. #else
  370. (void)q_size;
  371. #endif
  372. OSTaskCreateHook(p_tcb); /* Call user defined hook */
  373. OS_TRACE_TASK_CREATE(p_tcb);
  374. OS_TRACE_TASK_SEM_CREATE(p_tcb, p_name);
  375. #if (OS_CFG_TASK_Q_EN > 0u)
  376. OS_TRACE_TASK_MSG_Q_CREATE(&p_tcb->MsgQ, p_name);
  377. #endif
  378. #if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
  379. for (id = 0u; id < OS_CFG_TLS_TBL_SIZE; id++) {
  380. p_tcb->TLS_Tbl[id] = 0u;
  381. }
  382. OS_TLS_TaskCreate(p_tcb); /* Call TLS hook */
  383. #endif
  384. /* -------------- ADD TASK TO READY LIST -------------- */
  385. CPU_CRITICAL_ENTER();
  386. OS_PrioInsert(p_tcb->Prio);
  387. OS_RdyListInsertTail(p_tcb);
  388. #if (OS_CFG_DBG_EN > 0u)
  389. OS_TaskDbgListAdd(p_tcb);
  390. #endif
  391. OSTaskQty++; /* Increment the #tasks counter */
  392. if (OSRunning != OS_STATE_OS_RUNNING) { /* Return if multitasking has not started */
  393. CPU_CRITICAL_EXIT();
  394. return;
  395. }
  396. CPU_CRITICAL_EXIT();
  397. OSSched();
  398. }
  399. /*
  400. ************************************************************************************************************************
  401. * DELETE A TASK
  402. *
  403. * Description: This function allows you to delete a task. The calling task can delete itself by specifying a NULL
  404. * pointer for 'p_tcb'. The deleted task is returned to the dormant state and can be re-activated by
  405. * creating the deleted task again.
  406. *
  407. * Arguments : p_tcb is the TCB of the tack to delete
  408. *
  409. * p_err is a pointer to an error code returned by this function:
  410. *
  411. * OS_ERR_NONE If the call is successful
  412. * OS_ERR_ILLEGAL_DEL_RUN_TIME If you are trying to delete the task after you called
  413. * OSStart()
  414. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  415. * OS_ERR_STATE_INVALID If the state of the task is invalid
  416. * OS_ERR_TASK_DEL_IDLE If you attempted to delete uC/OS-III's idle task
  417. * OS_ERR_TASK_DEL_INVALID If you attempted to delete uC/OS-III's ISR handler task
  418. * OS_ERR_TASK_DEL_ISR If you tried to delete a task from an ISR
  419. *
  420. * Returns : none
  421. *
  422. * Note(s) : 1) 'p_err' gets set to OS_ERR_NONE before OSSched() to allow the returned err or code to be monitored even
  423. * for a task that is deleting itself. In this case, 'p_err' MUST point to a global variable that can be
  424. * accessed by another task.
  425. ************************************************************************************************************************
  426. */
  427. #if (OS_CFG_TASK_DEL_EN > 0u)
  428. void OSTaskDel (OS_TCB *p_tcb,
  429. OS_ERR *p_err)
  430. {
  431. #if (OS_CFG_MUTEX_EN > 0u)
  432. OS_TCB *p_tcb_owner;
  433. OS_PRIO prio_new;
  434. #endif
  435. CPU_SR_ALLOC();
  436. #ifdef OS_SAFETY_CRITICAL
  437. if (p_err == (OS_ERR *)0) {
  438. OS_SAFETY_CRITICAL_EXCEPTION();
  439. return;
  440. }
  441. #endif
  442. #ifdef OS_SAFETY_CRITICAL_IEC61508
  443. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  444. *p_err = OS_ERR_ILLEGAL_DEL_RUN_TIME;
  445. return;
  446. }
  447. #endif
  448. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  449. if (OSIntNestingCtr > 0u) { /* See if trying to delete from ISR */
  450. *p_err = OS_ERR_TASK_DEL_ISR;
  451. return;
  452. }
  453. #endif
  454. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  455. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  456. *p_err = OS_ERR_OS_NOT_RUNNING;
  457. return;
  458. }
  459. #endif
  460. #if (OS_CFG_TASK_IDLE_EN > 0u)
  461. if (p_tcb == &OSIdleTaskTCB) { /* Not allowed to delete the idle task */
  462. *p_err = OS_ERR_TASK_DEL_IDLE;
  463. return;
  464. }
  465. #endif
  466. if (p_tcb == (OS_TCB *)0) { /* Delete 'Self'? */
  467. CPU_CRITICAL_ENTER();
  468. p_tcb = OSTCBCurPtr; /* Yes. */
  469. CPU_CRITICAL_EXIT();
  470. }
  471. CPU_CRITICAL_ENTER();
  472. switch (p_tcb->TaskState) {
  473. case OS_TASK_STATE_RDY:
  474. OS_RdyListRemove(p_tcb);
  475. break;
  476. case OS_TASK_STATE_SUSPENDED:
  477. break;
  478. case OS_TASK_STATE_DLY: /* Task is only delayed, not on any wait list */
  479. case OS_TASK_STATE_DLY_SUSPENDED:
  480. #if (OS_CFG_TICK_EN > 0u)
  481. OS_TickListRemove(p_tcb);
  482. #endif
  483. break;
  484. case OS_TASK_STATE_PEND:
  485. case OS_TASK_STATE_PEND_SUSPENDED:
  486. case OS_TASK_STATE_PEND_TIMEOUT:
  487. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  488. switch (p_tcb->PendOn) { /* See what we are pending on */
  489. case OS_TASK_PEND_ON_NOTHING:
  490. case OS_TASK_PEND_ON_TASK_Q: /* There is no wait list for these two */
  491. case OS_TASK_PEND_ON_TASK_SEM:
  492. break;
  493. case OS_TASK_PEND_ON_FLAG: /* Remove from pend list */
  494. case OS_TASK_PEND_ON_Q:
  495. case OS_TASK_PEND_ON_SEM:
  496. OS_PendListRemove(p_tcb);
  497. break;
  498. #if (OS_CFG_MUTEX_EN > 0u)
  499. case OS_TASK_PEND_ON_MUTEX:
  500. p_tcb_owner = ((OS_MUTEX *)((void *)p_tcb->PendObjPtr))->OwnerTCBPtr;
  501. prio_new = p_tcb_owner->Prio;
  502. OS_PendListRemove(p_tcb);
  503. if ((p_tcb_owner->Prio != p_tcb_owner->BasePrio) &&
  504. (p_tcb_owner->Prio == p_tcb->Prio)) { /* Has the owner inherited a priority? */
  505. prio_new = OS_MutexGrpPrioFindHighest(p_tcb_owner);
  506. prio_new = (prio_new > p_tcb_owner->BasePrio) ? p_tcb_owner->BasePrio : prio_new;
  507. }
  508. p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING;
  509. if (prio_new != p_tcb_owner->Prio) {
  510. OS_TaskChangePrio(p_tcb_owner, prio_new);
  511. OS_TRACE_MUTEX_TASK_PRIO_DISINHERIT(p_tcb_owner, p_tcb_owner->Prio);
  512. }
  513. break;
  514. #endif
  515. default:
  516. /* Default case. */
  517. break;
  518. }
  519. #if (OS_CFG_TICK_EN > 0u)
  520. if ((p_tcb->TaskState == OS_TASK_STATE_PEND_TIMEOUT) ||
  521. (p_tcb->TaskState == OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED)) {
  522. OS_TickListRemove(p_tcb);
  523. }
  524. #endif
  525. break;
  526. default:
  527. CPU_CRITICAL_EXIT();
  528. *p_err = OS_ERR_STATE_INVALID;
  529. return;
  530. }
  531. #if (OS_CFG_MUTEX_EN > 0u)
  532. if(p_tcb->MutexGrpHeadPtr != (OS_MUTEX *)0) {
  533. OS_MutexGrpPostAll(p_tcb);
  534. }
  535. #endif
  536. #if (OS_CFG_TASK_Q_EN > 0u)
  537. (void)OS_MsgQFreeAll(&p_tcb->MsgQ); /* Free task's message queue messages */
  538. #endif
  539. OSTaskDelHook(p_tcb); /* Call user defined hook */
  540. #if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
  541. OS_TLS_TaskDel(p_tcb); /* Call TLS hook */
  542. #endif
  543. #if (OS_CFG_DBG_EN > 0u)
  544. OS_TaskDbgListRemove(p_tcb);
  545. #endif
  546. OSTaskQty--; /* One less task being managed */
  547. OS_TRACE_TASK_DEL(p_tcb);
  548. #if (OS_CFG_TASK_STK_REDZONE_EN == 0u) /* Don't clear the TCB before checking the red-zone */
  549. OS_TaskInitTCB(p_tcb); /* Initialize the TCB to default values */
  550. #endif
  551. p_tcb->TaskState = (OS_STATE)OS_TASK_STATE_DEL; /* Indicate that the task was deleted */
  552. *p_err = OS_ERR_NONE; /* See Note #1. */
  553. CPU_CRITICAL_EXIT();
  554. OSSched(); /* Find new highest priority task */
  555. }
  556. #endif
  557. /*
  558. ************************************************************************************************************************
  559. * FLUSH TASK's QUEUE
  560. *
  561. * Description: This function is used to flush the task's internal message queue.
  562. *
  563. * Arguments : p_tcb is a pointer to the task's OS_TCB. Specifying a NULL pointer indicates that you wish to
  564. * flush the message queue of the calling task.
  565. *
  566. * p_err is a pointer to a variable that will contain an error code returned by this function.
  567. *
  568. * OS_ERR_NONE Upon success
  569. * OS_ERR_FLUSH_ISR If you called this function from an ISR
  570. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  571. *
  572. * Returns : The number of entries freed from the queue
  573. *
  574. * Note(s) : 1) You should use this function with great care because, when to flush the queue, you LOOSE the
  575. * references to what the queue entries are pointing to and thus, you could cause 'memory leaks'. In
  576. * other words, the data you are pointing to that's being referenced by the queue entries should, most
  577. * likely, need to be de-allocated (i.e. freed).
  578. ************************************************************************************************************************
  579. */
  580. #if (OS_CFG_TASK_Q_EN > 0u)
  581. OS_MSG_QTY OSTaskQFlush (OS_TCB *p_tcb,
  582. OS_ERR *p_err)
  583. {
  584. OS_MSG_QTY entries;
  585. CPU_SR_ALLOC();
  586. #ifdef OS_SAFETY_CRITICAL
  587. if (p_err == (OS_ERR *)0) {
  588. OS_SAFETY_CRITICAL_EXCEPTION();
  589. return (0u);
  590. }
  591. #endif
  592. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  593. if (OSIntNestingCtr > 0u) { /* Can't flush a message queue from an ISR */
  594. *p_err = OS_ERR_FLUSH_ISR;
  595. return (0u);
  596. }
  597. #endif
  598. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  599. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  600. *p_err = OS_ERR_OS_NOT_RUNNING;
  601. return (0u);
  602. }
  603. #endif
  604. if (p_tcb == (OS_TCB *)0) { /* Flush message queue of calling task? */
  605. CPU_CRITICAL_ENTER();
  606. p_tcb = OSTCBCurPtr;
  607. CPU_CRITICAL_EXIT();
  608. }
  609. CPU_CRITICAL_ENTER();
  610. entries = OS_MsgQFreeAll(&p_tcb->MsgQ); /* Return all OS_MSGs to the OS_MSG pool */
  611. CPU_CRITICAL_EXIT();
  612. *p_err = OS_ERR_NONE;
  613. return (entries);
  614. }
  615. #endif
  616. /*
  617. ************************************************************************************************************************
  618. * WAIT FOR A MESSAGE
  619. *
  620. * Description: This function causes the current task to wait for a message to be posted to it.
  621. *
  622. * Arguments : timeout is an optional timeout period (in clock ticks). If non-zero, your task will wait for a
  623. * message to arrive up to the amount of time specified by this argument.
  624. * If you specify 0, however, your task will wait forever or, until a message arrives.
  625. *
  626. * opt determines whether the user wants to block if the task's queue is empty or not:
  627. *
  628. * OS_OPT_PEND_BLOCKING
  629. * OS_OPT_PEND_NON_BLOCKING
  630. *
  631. * p_msg_size is a pointer to a variable that will receive the size of the message
  632. *
  633. * p_ts is a pointer to a variable that will receive the timestamp of when the message was
  634. * received. If you pass a NULL pointer (i.e. (CPU_TS *)0) then you will not get the
  635. * timestamp. In other words, passing a NULL pointer is valid and indicates that you don't
  636. * need the timestamp.
  637. *
  638. * p_err is a pointer to where an error message will be deposited. Possible error
  639. * messages are:
  640. *
  641. * OS_ERR_NONE The call was successful and your task received a message.
  642. * OS_ERR_OPT_INVALID If you specified an invalid option
  643. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  644. * OS_ERR_PEND_ABORT If the pend was aborted
  645. * OS_ERR_PEND_ISR If you called this function from an ISR and the result
  646. * OS_ERR_PEND_WOULD_BLOCK If you specified non-blocking but the queue was not empty
  647. * OS_ERR_PTR_INVALID If 'p_msg_size' is NULL
  648. * OS_ERR_SCHED_LOCKED If the scheduler is locked
  649. * OS_ERR_TIMEOUT A message was not received within the specified timeout
  650. * would lead to a suspension
  651. * OS_ERR_TICK_DISABLED If kernel ticks are disabled and a timeout is specified
  652. *
  653. * Returns : A pointer to the message received or a NULL pointer upon error.
  654. *
  655. * Note(s) : 1) It is possible to receive NULL pointers when there are no errors.
  656. *
  657. * : 2) This API 'MUST NOT' be called from a timer callback function.
  658. ************************************************************************************************************************
  659. */
  660. #if (OS_CFG_TASK_Q_EN > 0u)
  661. void *OSTaskQPend (OS_TICK timeout,
  662. OS_OPT opt,
  663. OS_MSG_SIZE *p_msg_size,
  664. CPU_TS *p_ts,
  665. OS_ERR *p_err)
  666. {
  667. OS_MSG_Q *p_msg_q;
  668. void *p_void;
  669. CPU_SR_ALLOC();
  670. #ifdef OS_SAFETY_CRITICAL
  671. if (p_err == (OS_ERR *)0) {
  672. OS_SAFETY_CRITICAL_EXCEPTION();
  673. return ((void *)0);
  674. }
  675. #endif
  676. OS_TRACE_TASK_MSG_Q_PEND_ENTER(&OSTCBCurPtr->MsgQ, timeout, opt, p_msg_size, p_ts);
  677. #if (OS_CFG_TICK_EN == 0u)
  678. if (timeout != 0u) {
  679. *p_err = OS_ERR_TICK_DISABLED;
  680. OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_TICK_DISABLED);
  681. return ((void *)0);
  682. }
  683. #endif
  684. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  685. if (OSIntNestingCtr > 0u) { /* Can't Pend from an ISR */
  686. OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_PEND_ISR);
  687. *p_err = OS_ERR_PEND_ISR;
  688. return ((void *)0);
  689. }
  690. #endif
  691. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  692. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  693. OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_OS_NOT_RUNNING);
  694. *p_err = OS_ERR_OS_NOT_RUNNING;
  695. return ((void *)0);
  696. }
  697. #endif
  698. #if (OS_CFG_ARG_CHK_EN > 0u) /* ---------------- VALIDATE ARGUMENTS ---------------- */
  699. if (p_msg_size == (OS_MSG_SIZE *)0) { /* User must supply a valid destination for msg size */
  700. OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_PTR_INVALID);
  701. *p_err = OS_ERR_PTR_INVALID;
  702. return ((void *)0);
  703. }
  704. switch (opt) { /* User must supply a valid option */
  705. case OS_OPT_PEND_BLOCKING:
  706. case OS_OPT_PEND_NON_BLOCKING:
  707. break;
  708. default:
  709. OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_OPT_INVALID);
  710. *p_err = OS_ERR_OPT_INVALID;
  711. return ((void *)0);
  712. }
  713. #endif
  714. if (p_ts != (CPU_TS *)0) {
  715. *p_ts = 0u; /* Initialize the returned timestamp */
  716. }
  717. CPU_CRITICAL_ENTER();
  718. p_msg_q = &OSTCBCurPtr->MsgQ; /* Any message waiting in the message queue? */
  719. p_void = OS_MsgQGet(p_msg_q,
  720. p_msg_size,
  721. p_ts,
  722. p_err);
  723. if (*p_err == OS_ERR_NONE) {
  724. #if (OS_CFG_TASK_PROFILE_EN > 0u)
  725. #if (OS_CFG_TS_EN > 0u)
  726. if (p_ts != (CPU_TS *)0) {
  727. OSTCBCurPtr->MsgQPendTime = OS_TS_GET() - *p_ts;
  728. if (OSTCBCurPtr->MsgQPendTimeMax < OSTCBCurPtr->MsgQPendTime) {
  729. OSTCBCurPtr->MsgQPendTimeMax = OSTCBCurPtr->MsgQPendTime;
  730. }
  731. }
  732. #endif
  733. #endif
  734. OS_TRACE_TASK_MSG_Q_PEND(p_msg_q);
  735. CPU_CRITICAL_EXIT();
  736. OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_NONE);
  737. return (p_void); /* Yes, Return oldest message received */
  738. }
  739. if ((opt & OS_OPT_PEND_NON_BLOCKING) != 0u) { /* Caller wants to block if not available? */
  740. *p_err = OS_ERR_PEND_WOULD_BLOCK; /* No */
  741. CPU_CRITICAL_EXIT();
  742. OS_TRACE_TASK_MSG_Q_PEND_FAILED(p_msg_q);
  743. OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_PEND_WOULD_BLOCK);
  744. return ((void *)0);
  745. } else { /* Yes */
  746. if (OSSchedLockNestingCtr > 0u) { /* Can't block when the scheduler is locked */
  747. CPU_CRITICAL_EXIT();
  748. OS_TRACE_TASK_MSG_Q_PEND_FAILED(p_msg_q);
  749. OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_SCHED_LOCKED);
  750. *p_err = OS_ERR_SCHED_LOCKED;
  751. return ((void *)0);
  752. }
  753. }
  754. OS_Pend((OS_PEND_OBJ *)0, /* Block task pending on Message */
  755. OSTCBCurPtr,
  756. OS_TASK_PEND_ON_TASK_Q,
  757. timeout);
  758. CPU_CRITICAL_EXIT();
  759. OS_TRACE_TASK_MSG_Q_PEND_BLOCK(p_msg_q);
  760. OSSched(); /* Find the next highest priority task ready to run */
  761. CPU_CRITICAL_ENTER();
  762. switch (OSTCBCurPtr->PendStatus) {
  763. case OS_STATUS_PEND_OK: /* Extract message from TCB (Put there by Post) */
  764. p_void = OSTCBCurPtr->MsgPtr;
  765. *p_msg_size = OSTCBCurPtr->MsgSize;
  766. #if (OS_CFG_TS_EN > 0u)
  767. if (p_ts != (CPU_TS *)0) {
  768. *p_ts = OSTCBCurPtr->TS;
  769. #if (OS_CFG_TASK_PROFILE_EN > 0u)
  770. OSTCBCurPtr->MsgQPendTime = OS_TS_GET() - OSTCBCurPtr->TS;
  771. if (OSTCBCurPtr->MsgQPendTimeMax < OSTCBCurPtr->MsgQPendTime) {
  772. OSTCBCurPtr->MsgQPendTimeMax = OSTCBCurPtr->MsgQPendTime;
  773. }
  774. #endif
  775. }
  776. #endif
  777. OS_TRACE_TASK_MSG_Q_PEND(p_msg_q);
  778. *p_err = OS_ERR_NONE;
  779. break;
  780. case OS_STATUS_PEND_ABORT: /* Indicate that we aborted */
  781. p_void = (void *)0;
  782. *p_msg_size = 0u;
  783. if (p_ts != (CPU_TS *)0) {
  784. *p_ts = 0u;
  785. }
  786. OS_TRACE_TASK_MSG_Q_PEND_FAILED(p_msg_q);
  787. *p_err = OS_ERR_PEND_ABORT;
  788. break;
  789. case OS_STATUS_PEND_TIMEOUT: /* Indicate that we didn't get event within TO */
  790. default:
  791. p_void = (void *)0;
  792. *p_msg_size = 0u;
  793. #if (OS_CFG_TS_EN > 0u)
  794. if (p_ts != (CPU_TS *)0) {
  795. *p_ts = OSTCBCurPtr->TS;
  796. }
  797. #endif
  798. OS_TRACE_TASK_MSG_Q_PEND_FAILED(p_msg_q);
  799. *p_err = OS_ERR_TIMEOUT;
  800. break;
  801. }
  802. CPU_CRITICAL_EXIT();
  803. OS_TRACE_TASK_MSG_Q_PEND_EXIT(*p_err);
  804. return (p_void); /* Return received message */
  805. }
  806. #endif
  807. /*
  808. ************************************************************************************************************************
  809. * ABORT WAITING FOR A MESSAGE
  810. *
  811. * Description: This function aborts & readies the task specified. This function should be used to fault-abort the wait
  812. * for a message, rather than to normally post the message to the task via OSTaskQPost().
  813. *
  814. * Arguments : p_tcb is a pointer to the task to pend abort
  815. *
  816. * opt provides options for this function:
  817. *
  818. * OS_OPT_POST_NONE No option specified
  819. * OS_OPT_POST_NO_SCHED Indicates that the scheduler will not be called.
  820. *
  821. * p_err is a pointer to a variable that will contain an error code returned by this function.
  822. *
  823. * OS_ERR_NONE If the task was readied and informed of the aborted wait
  824. * OS_ERR_OPT_INVALID If you specified an invalid option
  825. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  826. * OS_ERR_PEND_ABORT_ISR If you called this function from an ISR
  827. * OS_ERR_PEND_ABORT_NONE If task was not pending on a message and thus there is nothing to
  828. * abort
  829. * OS_ERR_PEND_ABORT_SELF If you passed a NULL pointer for 'p_tcb'
  830. *
  831. * Returns : == OS_FALSE if task was not waiting for a message, or upon error.
  832. * == OS_TRUE if task was waiting for a message and was readied and informed.
  833. *
  834. * Note(s) : none
  835. ************************************************************************************************************************
  836. */
  837. #if (OS_CFG_TASK_Q_EN > 0u) && (OS_CFG_TASK_Q_PEND_ABORT_EN > 0u)
  838. CPU_BOOLEAN OSTaskQPendAbort (OS_TCB *p_tcb,
  839. OS_OPT opt,
  840. OS_ERR *p_err)
  841. {
  842. CPU_TS ts;
  843. CPU_SR_ALLOC();
  844. #ifdef OS_SAFETY_CRITICAL
  845. if (p_err == (OS_ERR *)0) {
  846. OS_SAFETY_CRITICAL_EXCEPTION();
  847. return (OS_FALSE);
  848. }
  849. #endif
  850. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  851. if (OSIntNestingCtr > 0u) { /* See if called from ISR ... */
  852. *p_err = OS_ERR_PEND_ABORT_ISR; /* ... can't Pend Abort from an ISR */
  853. return (OS_FALSE);
  854. }
  855. #endif
  856. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  857. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  858. *p_err = OS_ERR_OS_NOT_RUNNING;
  859. return (OS_FALSE);
  860. }
  861. #endif
  862. #if (OS_CFG_ARG_CHK_EN > 0u) /* ---------------- VALIDATE ARGUMENTS ---------------- */
  863. switch (opt) { /* User must supply a valid option */
  864. case OS_OPT_POST_NONE:
  865. case OS_OPT_POST_NO_SCHED:
  866. break;
  867. default:
  868. *p_err = OS_ERR_OPT_INVALID;
  869. return (OS_FALSE);
  870. }
  871. #endif
  872. CPU_CRITICAL_ENTER();
  873. #if (OS_CFG_ARG_CHK_EN > 0u)
  874. if ((p_tcb == (OS_TCB *)0) || /* Pend abort self? */
  875. (p_tcb == OSTCBCurPtr)) {
  876. CPU_CRITICAL_EXIT();
  877. *p_err = OS_ERR_PEND_ABORT_SELF; /* ... doesn't make sense */
  878. return (OS_FALSE);
  879. }
  880. #endif
  881. if (p_tcb->PendOn != OS_TASK_PEND_ON_TASK_Q) { /* Is task waiting for a message? */
  882. CPU_CRITICAL_EXIT(); /* No */
  883. *p_err = OS_ERR_PEND_ABORT_NONE;
  884. return (OS_FALSE);
  885. }
  886. #if (OS_CFG_TS_EN > 0u)
  887. ts = OS_TS_GET(); /* Get timestamp of when the abort occurred */
  888. #else
  889. ts = 0u;
  890. #endif
  891. OS_PendAbort(p_tcb, /* Abort the pend */
  892. ts,
  893. OS_STATUS_PEND_ABORT);
  894. CPU_CRITICAL_EXIT();
  895. if ((opt & OS_OPT_POST_NO_SCHED) == 0u) {
  896. OSSched(); /* Run the scheduler */
  897. }
  898. *p_err = OS_ERR_NONE;
  899. return (OS_TRUE);
  900. }
  901. #endif
  902. /*
  903. ************************************************************************************************************************
  904. * POST MESSAGE TO A TASK
  905. *
  906. * Description: This function sends a message to a task.
  907. *
  908. * Arguments : p_tcb is a pointer to the TCB of the task receiving a message. If you specify a NULL pointer then
  909. * the message will be posted to the task's queue of the calling task. In other words, you'd be
  910. * posting a message to yourself.
  911. *
  912. * p_void is a pointer to the message to send.
  913. *
  914. * msg_size is the size of the message sent (in bytes)
  915. *
  916. * opt specifies whether the post will be FIFO or LIFO:
  917. *
  918. * OS_OPT_POST_FIFO Post at the end of the queue
  919. * OS_OPT_POST_LIFO Post at the front of the queue
  920. *
  921. * OS_OPT_POST_NO_SCHED Do not run the scheduler after the post
  922. *
  923. * Note(s): 1) OS_OPT_POST_NO_SCHED can be added with one of the other options.
  924. *
  925. *
  926. * p_err is a pointer to a variable that will hold the error code associated
  927. * with the outcome of this call. Errors can be:
  928. *
  929. * OS_ERR_NONE The call was successful and the message was sent
  930. * OS_ERR_MSG_POOL_EMPTY If there are no more OS_MSGs available from the pool
  931. * OS_ERR_OPT_INVALID If you specified an invalid option
  932. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  933. * OS_ERR_Q_MAX If the queue is full
  934. * OS_ERR_STATE_INVALID If the task is in an invalid state. This should never happen
  935. * and if it does, would be considered a system failure
  936. *
  937. * Returns : none
  938. *
  939. * Note(s) : none
  940. ************************************************************************************************************************
  941. */
  942. #if (OS_CFG_TASK_Q_EN > 0u)
  943. void OSTaskQPost (OS_TCB *p_tcb,
  944. void *p_void,
  945. OS_MSG_SIZE msg_size,
  946. OS_OPT opt,
  947. OS_ERR *p_err)
  948. {
  949. CPU_TS ts;
  950. CPU_SR_ALLOC();
  951. #ifdef OS_SAFETY_CRITICAL
  952. if (p_err == (OS_ERR *)0) {
  953. OS_SAFETY_CRITICAL_EXCEPTION();
  954. return;
  955. }
  956. #endif
  957. OS_TRACE_TASK_MSG_Q_POST_ENTER(&p_tcb->MsgQ, p_void, msg_size, opt);
  958. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  959. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  960. OS_TRACE_TASK_MSG_Q_POST_EXIT(OS_ERR_OS_NOT_RUNNING);
  961. *p_err = OS_ERR_OS_NOT_RUNNING;
  962. return;
  963. }
  964. #endif
  965. #if (OS_CFG_ARG_CHK_EN > 0u) /* ---------------- VALIDATE ARGUMENTS ---------------- */
  966. switch (opt) { /* User must supply a valid option */
  967. case OS_OPT_POST_FIFO:
  968. case OS_OPT_POST_LIFO:
  969. case OS_OPT_POST_FIFO | OS_OPT_POST_NO_SCHED:
  970. case OS_OPT_POST_LIFO | OS_OPT_POST_NO_SCHED:
  971. break;
  972. default:
  973. OS_TRACE_TASK_MSG_Q_POST_FAILED(&p_tcb->MsgQ);
  974. OS_TRACE_TASK_MSG_Q_POST_EXIT(OS_ERR_OPT_INVALID);
  975. *p_err = OS_ERR_OPT_INVALID;
  976. return;
  977. }
  978. #endif
  979. #if (OS_CFG_TS_EN > 0u)
  980. ts = OS_TS_GET(); /* Get timestamp */
  981. #else
  982. ts = 0u;
  983. #endif
  984. OS_TRACE_TASK_MSG_Q_POST(&p_tcb->MsgQ);
  985. *p_err = OS_ERR_NONE; /* Assume we won't have any errors */
  986. CPU_CRITICAL_ENTER();
  987. if (p_tcb == (OS_TCB *)0) { /* Post msg to 'self'? */
  988. p_tcb = OSTCBCurPtr;
  989. }
  990. switch (p_tcb->TaskState) {
  991. case OS_TASK_STATE_RDY:
  992. case OS_TASK_STATE_DLY:
  993. case OS_TASK_STATE_SUSPENDED:
  994. case OS_TASK_STATE_DLY_SUSPENDED:
  995. OS_MsgQPut(&p_tcb->MsgQ, /* Deposit the message in the queue */
  996. p_void,
  997. msg_size,
  998. opt,
  999. ts,
  1000. p_err);
  1001. CPU_CRITICAL_EXIT();
  1002. break;
  1003. case OS_TASK_STATE_PEND:
  1004. case OS_TASK_STATE_PEND_TIMEOUT:
  1005. case OS_TASK_STATE_PEND_SUSPENDED:
  1006. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  1007. if (p_tcb->PendOn == OS_TASK_PEND_ON_TASK_Q) { /* Is task waiting for a message to be sent to it? */
  1008. OS_Post((OS_PEND_OBJ *)0,
  1009. p_tcb,
  1010. p_void,
  1011. msg_size,
  1012. ts);
  1013. CPU_CRITICAL_EXIT();
  1014. if ((opt & OS_OPT_POST_NO_SCHED) == 0u) {
  1015. OSSched(); /* Run the scheduler */
  1016. }
  1017. } else {
  1018. OS_MsgQPut(&p_tcb->MsgQ, /* No, Task is pending on something else ... */
  1019. p_void, /* ... Deposit the message in the task's queue */
  1020. msg_size,
  1021. opt,
  1022. ts,
  1023. p_err);
  1024. CPU_CRITICAL_EXIT();
  1025. }
  1026. break;
  1027. default:
  1028. CPU_CRITICAL_EXIT();
  1029. *p_err = OS_ERR_STATE_INVALID;
  1030. break;
  1031. }
  1032. OS_TRACE_TASK_MSG_Q_POST_EXIT(*p_err);
  1033. }
  1034. #endif
  1035. /*
  1036. ************************************************************************************************************************
  1037. * GET THE CURRENT VALUE OF A TASK REGISTER
  1038. *
  1039. * Description: This function is called to obtain the current value of a task register. Task registers are application
  1040. * specific and can be used to store task specific values such as 'error numbers' (i.e. errno), statistics,
  1041. * etc.
  1042. *
  1043. * Arguments : p_tcb is a pointer to the OS_TCB of the task you want to read the register from. If 'p_tcb' is a
  1044. * NULL pointer then you will get the register of the current task.
  1045. *
  1046. * id is the 'id' of the desired task variable. Note that the 'id' must be less than
  1047. * OS_CFG_TASK_REG_TBL_SIZE
  1048. *
  1049. * p_err is a pointer to a variable that will hold an error code related to this call.
  1050. *
  1051. * OS_ERR_NONE If the call was successful
  1052. * OS_ERR_REG_ID_INVALID If the 'id' is not between 0 and OS_CFG_TASK_REG_TBL_SIZE-1
  1053. *
  1054. * Returns : The current value of the task's register or 0 if an error is detected.
  1055. *
  1056. * Note(s) : none
  1057. ************************************************************************************************************************
  1058. */
  1059. #if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
  1060. OS_REG OSTaskRegGet (OS_TCB *p_tcb,
  1061. OS_REG_ID id,
  1062. OS_ERR *p_err)
  1063. {
  1064. OS_REG value;
  1065. CPU_SR_ALLOC();
  1066. #ifdef OS_SAFETY_CRITICAL
  1067. if (p_err == (OS_ERR *)0) {
  1068. OS_SAFETY_CRITICAL_EXCEPTION();
  1069. return (0u);
  1070. }
  1071. #endif
  1072. #if (OS_CFG_ARG_CHK_EN > 0u)
  1073. if (id >= OS_CFG_TASK_REG_TBL_SIZE) {
  1074. *p_err = OS_ERR_REG_ID_INVALID;
  1075. return (0u);
  1076. }
  1077. #endif
  1078. CPU_CRITICAL_ENTER();
  1079. if (p_tcb == (OS_TCB *)0) {
  1080. p_tcb = OSTCBCurPtr;
  1081. }
  1082. value = p_tcb->RegTbl[id];
  1083. CPU_CRITICAL_EXIT();
  1084. *p_err = OS_ERR_NONE;
  1085. return (value);
  1086. }
  1087. #endif
  1088. /*
  1089. ************************************************************************************************************************
  1090. * ALLOCATE THE NEXT AVAILABLE TASK REGISTER ID
  1091. *
  1092. * Description: This function is called to obtain a task register ID. This function thus allows task registers IDs to be
  1093. * allocated dynamically instead of statically.
  1094. *
  1095. * Arguments : p_err is a pointer to a variable that will hold an error code related to this call.
  1096. *
  1097. * OS_ERR_NONE If the call was successful
  1098. * OS_ERR_NO_MORE_ID_AVAIL If you are attempting to assign more task register IDs than you
  1099. * have available through OS_CFG_TASK_REG_TBL_SIZE
  1100. *
  1101. * Returns : The next available task register 'id' or OS_CFG_TASK_REG_TBL_SIZE if an error is detected.
  1102. *
  1103. * Note(s) : none
  1104. ************************************************************************************************************************
  1105. */
  1106. #if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
  1107. OS_REG_ID OSTaskRegGetID (OS_ERR *p_err)
  1108. {
  1109. OS_REG_ID id;
  1110. CPU_SR_ALLOC();
  1111. #ifdef OS_SAFETY_CRITICAL
  1112. if (p_err == (OS_ERR *)0) {
  1113. OS_SAFETY_CRITICAL_EXCEPTION();
  1114. return ((OS_REG_ID)OS_CFG_TASK_REG_TBL_SIZE);
  1115. }
  1116. #endif
  1117. CPU_CRITICAL_ENTER();
  1118. if (OSTaskRegNextAvailID >= OS_CFG_TASK_REG_TBL_SIZE) { /* See if we exceeded the number of IDs available */
  1119. *p_err = OS_ERR_NO_MORE_ID_AVAIL; /* Yes, cannot allocate more task register IDs */
  1120. CPU_CRITICAL_EXIT();
  1121. return (OS_CFG_TASK_REG_TBL_SIZE);
  1122. }
  1123. id = OSTaskRegNextAvailID; /* Assign the next available ID */
  1124. OSTaskRegNextAvailID++; /* Increment available ID for next request */
  1125. CPU_CRITICAL_EXIT();
  1126. *p_err = OS_ERR_NONE;
  1127. return (id);
  1128. }
  1129. #endif
  1130. /*
  1131. ************************************************************************************************************************
  1132. * SET THE CURRENT VALUE OF A TASK REGISTER
  1133. *
  1134. * Description: This function is called to change the current value of a task register. Task registers are application
  1135. * specific and can be used to store task specific values such as 'error numbers' (i.e. errno), statistics,
  1136. * etc.
  1137. *
  1138. * Arguments : p_tcb is a pointer to the OS_TCB of the task you want to set the register for. If 'p_tcb' is a NULL
  1139. * pointer then you will change the register of the current task.
  1140. *
  1141. * id is the 'id' of the desired task register. Note that the 'id' must be less than
  1142. * OS_CFG_TASK_REG_TBL_SIZE
  1143. *
  1144. * value is the desired value for the task register.
  1145. *
  1146. * p_err is a pointer to a variable that will hold an error code related to this call.
  1147. *
  1148. * OS_ERR_NONE If the call was successful
  1149. * OS_ERR_REG_ID_INVALID If the 'id' is not between 0 and OS_CFG_TASK_REG_TBL_SIZE-1
  1150. *
  1151. * Returns : none
  1152. *
  1153. * Note(s) : none
  1154. ************************************************************************************************************************
  1155. */
  1156. #if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
  1157. void OSTaskRegSet (OS_TCB *p_tcb,
  1158. OS_REG_ID id,
  1159. OS_REG value,
  1160. OS_ERR *p_err)
  1161. {
  1162. CPU_SR_ALLOC();
  1163. #ifdef OS_SAFETY_CRITICAL
  1164. if (p_err == (OS_ERR *)0) {
  1165. OS_SAFETY_CRITICAL_EXCEPTION();
  1166. return;
  1167. }
  1168. #endif
  1169. #if (OS_CFG_ARG_CHK_EN > 0u)
  1170. if (id >= OS_CFG_TASK_REG_TBL_SIZE) {
  1171. *p_err = OS_ERR_REG_ID_INVALID;
  1172. return;
  1173. }
  1174. #endif
  1175. CPU_CRITICAL_ENTER();
  1176. if (p_tcb == (OS_TCB *)0) {
  1177. p_tcb = OSTCBCurPtr;
  1178. }
  1179. p_tcb->RegTbl[id] = value;
  1180. CPU_CRITICAL_EXIT();
  1181. *p_err = OS_ERR_NONE;
  1182. }
  1183. #endif
  1184. /*
  1185. ************************************************************************************************************************
  1186. * RESUME A SUSPENDED TASK
  1187. *
  1188. * Description: This function is called to resume a previously suspended task. This is the only call that will remove an
  1189. * explicit task suspension.
  1190. *
  1191. * Arguments : p_tcb Is a pointer to the task's OS_TCB to resume
  1192. *
  1193. * p_err Is a pointer to a variable that will contain an error code returned by this function
  1194. *
  1195. * OS_ERR_NONE If the requested task is resumed
  1196. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  1197. * OS_ERR_STATE_INVALID If the task is in an invalid state
  1198. * OS_ERR_TASK_NOT_SUSPENDED If the task to resume has not been suspended
  1199. * OS_ERR_TASK_RESUME_ISR If you called this function from an ISR
  1200. * OS_ERR_TASK_RESUME_SELF You cannot resume 'self'
  1201. *
  1202. * Returns : none
  1203. *
  1204. * Note(s) : none
  1205. ************************************************************************************************************************
  1206. */
  1207. #if (OS_CFG_TASK_SUSPEND_EN > 0u)
  1208. void OSTaskResume (OS_TCB *p_tcb,
  1209. OS_ERR *p_err)
  1210. {
  1211. CPU_SR_ALLOC();
  1212. OS_TRACE_TASK_RESUME_ENTER(p_tcb);
  1213. #ifdef OS_SAFETY_CRITICAL
  1214. if (p_err == (OS_ERR *)0) {
  1215. OS_SAFETY_CRITICAL_EXCEPTION();
  1216. return;
  1217. }
  1218. #endif
  1219. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  1220. if (OSIntNestingCtr > 0u) { /* Not allowed to call from an ISR */
  1221. *p_err = OS_ERR_TASK_RESUME_ISR;
  1222. OS_TRACE_TASK_RESUME_EXIT(OS_ERR_TASK_RESUME_ISR);
  1223. return;
  1224. }
  1225. #endif
  1226. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  1227. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  1228. *p_err = OS_ERR_OS_NOT_RUNNING;
  1229. OS_TRACE_TASK_RESUME_EXIT(OS_ERR_OS_NOT_RUNNING);
  1230. return;
  1231. }
  1232. #endif
  1233. #if (OS_CFG_ARG_CHK_EN > 0u)
  1234. CPU_CRITICAL_ENTER();
  1235. if ((p_tcb == (OS_TCB *)0) || /* We cannot resume 'self' */
  1236. (p_tcb == OSTCBCurPtr)) {
  1237. CPU_CRITICAL_EXIT();
  1238. *p_err = OS_ERR_TASK_RESUME_SELF;
  1239. OS_TRACE_TASK_RESUME_EXIT(OS_ERR_TASK_RESUME_SELF);
  1240. return;
  1241. }
  1242. CPU_CRITICAL_EXIT();
  1243. #endif
  1244. CPU_CRITICAL_ENTER();
  1245. *p_err = OS_ERR_NONE;
  1246. switch (p_tcb->TaskState) {
  1247. case OS_TASK_STATE_RDY:
  1248. case OS_TASK_STATE_DLY:
  1249. case OS_TASK_STATE_PEND:
  1250. case OS_TASK_STATE_PEND_TIMEOUT:
  1251. CPU_CRITICAL_EXIT();
  1252. *p_err = OS_ERR_TASK_NOT_SUSPENDED;
  1253. OS_TRACE_TASK_RESUME_EXIT(OS_ERR_TASK_NOT_SUSPENDED);
  1254. break;
  1255. case OS_TASK_STATE_SUSPENDED:
  1256. p_tcb->SuspendCtr--;
  1257. if (p_tcb->SuspendCtr == 0u) {
  1258. p_tcb->TaskState = OS_TASK_STATE_RDY;
  1259. OS_RdyListInsert(p_tcb); /* Insert the task in the ready list */
  1260. OS_TRACE_TASK_RESUME(p_tcb);
  1261. }
  1262. CPU_CRITICAL_EXIT();
  1263. break;
  1264. case OS_TASK_STATE_DLY_SUSPENDED:
  1265. p_tcb->SuspendCtr--;
  1266. if (p_tcb->SuspendCtr == 0u) {
  1267. p_tcb->TaskState = OS_TASK_STATE_DLY;
  1268. }
  1269. CPU_CRITICAL_EXIT();
  1270. break;
  1271. case OS_TASK_STATE_PEND_SUSPENDED:
  1272. p_tcb->SuspendCtr--;
  1273. if (p_tcb->SuspendCtr == 0u) {
  1274. p_tcb->TaskState = OS_TASK_STATE_PEND;
  1275. }
  1276. CPU_CRITICAL_EXIT();
  1277. break;
  1278. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  1279. p_tcb->SuspendCtr--;
  1280. if (p_tcb->SuspendCtr == 0u) {
  1281. p_tcb->TaskState = OS_TASK_STATE_PEND_TIMEOUT;
  1282. }
  1283. CPU_CRITICAL_EXIT();
  1284. break;
  1285. default:
  1286. CPU_CRITICAL_EXIT();
  1287. *p_err = OS_ERR_STATE_INVALID;
  1288. OS_TRACE_TASK_RESUME_EXIT(OS_ERR_STATE_INVALID);
  1289. break;
  1290. }
  1291. if (*p_err != OS_ERR_NONE) { /* Don't schedule if task wasn't in a suspend state. */
  1292. return;
  1293. }
  1294. OSSched();
  1295. OS_TRACE_TASK_RESUME_EXIT(OS_ERR_NONE);
  1296. }
  1297. #endif
  1298. /*
  1299. ************************************************************************************************************************
  1300. * WAIT FOR A TASK SEMAPHORE
  1301. *
  1302. * Description: This function is called to block the current task until a signal is sent by another task or ISR.
  1303. *
  1304. * Arguments : timeout is the amount of time you are will to wait for the signal
  1305. *
  1306. * opt determines whether the user wants to block if a semaphore post was not received:
  1307. *
  1308. * OS_OPT_PEND_BLOCKING
  1309. * OS_OPT_PEND_NON_BLOCKING
  1310. *
  1311. * p_ts is a pointer to a variable that will receive the timestamp of when the semaphore was posted
  1312. * or pend aborted. If you pass a NULL pointer (i.e. (CPU_TS *)0) then you will not get the
  1313. * timestamp. In other words, passing a NULL pointer is valid and indicates that you don't
  1314. * need the timestamp.
  1315. *
  1316. * p_err is a pointer to an error code that will be set by this function
  1317. *
  1318. * OS_ERR_NONE The call was successful and your task received a message
  1319. * OS_ERR_OPT_INVALID You specified an invalid option
  1320. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  1321. * OS_ERR_PEND_ABORT If the pend was aborted
  1322. * OS_ERR_PEND_ISR If you called this function from an ISR
  1323. * OS_ERR_PEND_WOULD_BLOCK If you specified non-blocking but no signal was received
  1324. * OS_ERR_SCHED_LOCKED If the scheduler is locked
  1325. * OS_ERR_STATUS_INVALID If the pend status is invalid
  1326. * OS_ERR_TIMEOUT A message was not received within the specified timeout
  1327. *
  1328. * Returns : The current count of signals the task received, 0 if none.
  1329. *
  1330. * Note(s) : This API 'MUST NOT' be called from a timer callback function.
  1331. ************************************************************************************************************************
  1332. */
  1333. OS_SEM_CTR OSTaskSemPend (OS_TICK timeout,
  1334. OS_OPT opt,
  1335. CPU_TS *p_ts,
  1336. OS_ERR *p_err)
  1337. {
  1338. OS_SEM_CTR ctr;
  1339. CPU_SR_ALLOC();
  1340. #if (OS_CFG_TS_EN == 0u)
  1341. (void)p_ts; /* Prevent compiler warning for not using 'ts' */
  1342. #endif
  1343. #ifdef OS_SAFETY_CRITICAL
  1344. if (p_err == (OS_ERR *)0) {
  1345. OS_SAFETY_CRITICAL_EXCEPTION();
  1346. return (0u);
  1347. }
  1348. #endif
  1349. OS_TRACE_TASK_SEM_PEND_ENTER(OSTCBCurPtr, timeout, opt, p_ts);
  1350. #if (OS_CFG_TICK_EN == 0u)
  1351. if (timeout != 0u) {
  1352. OS_TRACE_TASK_SEM_PEND_FAILED(OSTCBCurPtr);
  1353. OS_TRACE_TASK_SEM_PEND_EXIT(OS_ERR_TICK_DISABLED);
  1354. *p_err = OS_ERR_TICK_DISABLED;
  1355. return (0u);
  1356. }
  1357. #endif
  1358. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  1359. if (OSIntNestingCtr > 0u) { /* Not allowed to call from an ISR */
  1360. OS_TRACE_TASK_SEM_PEND_FAILED(OSTCBCurPtr);
  1361. OS_TRACE_TASK_SEM_PEND_EXIT(OS_ERR_PEND_ISR);
  1362. *p_err = OS_ERR_PEND_ISR;
  1363. return (0u);
  1364. }
  1365. #endif
  1366. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  1367. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  1368. OS_TRACE_TASK_SEM_PEND_EXIT(OS_ERR_OS_NOT_RUNNING);
  1369. *p_err = OS_ERR_OS_NOT_RUNNING;
  1370. return (0u);
  1371. }
  1372. #endif
  1373. #if (OS_CFG_ARG_CHK_EN > 0u)
  1374. switch (opt) { /* Validate 'opt' */
  1375. case OS_OPT_PEND_BLOCKING:
  1376. case OS_OPT_PEND_NON_BLOCKING:
  1377. break;
  1378. default:
  1379. OS_TRACE_TASK_SEM_PEND_FAILED(OSTCBCurPtr);
  1380. OS_TRACE_TASK_SEM_PEND_EXIT(OS_ERR_OPT_INVALID);
  1381. *p_err = OS_ERR_OPT_INVALID;
  1382. return (0u);
  1383. }
  1384. #endif
  1385. CPU_CRITICAL_ENTER();
  1386. if (OSTCBCurPtr->SemCtr > 0u) { /* See if task already been signaled */
  1387. OSTCBCurPtr->SemCtr--;
  1388. ctr = OSTCBCurPtr->SemCtr;
  1389. #if (OS_CFG_TS_EN > 0u)
  1390. if (p_ts != (CPU_TS *)0) {
  1391. *p_ts = OSTCBCurPtr->TS;
  1392. }
  1393. #if (OS_CFG_TASK_PROFILE_EN > 0u)
  1394. #if (OS_CFG_TS_EN > 0u)
  1395. OSTCBCurPtr->SemPendTime = OS_TS_GET() - OSTCBCurPtr->TS;
  1396. if (OSTCBCurPtr->SemPendTimeMax < OSTCBCurPtr->SemPendTime) {
  1397. OSTCBCurPtr->SemPendTimeMax = OSTCBCurPtr->SemPendTime;
  1398. }
  1399. #endif
  1400. #endif
  1401. #endif
  1402. OS_TRACE_TASK_SEM_PEND(OSTCBCurPtr);
  1403. CPU_CRITICAL_EXIT();
  1404. OS_TRACE_TASK_SEM_PEND_EXIT(OS_ERR_NONE);
  1405. *p_err = OS_ERR_NONE;
  1406. return (ctr);
  1407. }
  1408. if ((opt & OS_OPT_PEND_NON_BLOCKING) != 0u) { /* Caller wants to block if not available? */
  1409. CPU_CRITICAL_EXIT();
  1410. #if (OS_CFG_TS_EN > 0u)
  1411. if (p_ts != (CPU_TS *)0) {
  1412. *p_ts = 0u;
  1413. }
  1414. #endif
  1415. OS_TRACE_TASK_SEM_PEND_FAILED(OSTCBCurPtr);
  1416. OS_TRACE_TASK_SEM_PEND_EXIT(OS_ERR_PEND_WOULD_BLOCK);
  1417. *p_err = OS_ERR_PEND_WOULD_BLOCK; /* No */
  1418. return (0u);
  1419. } else { /* Yes */
  1420. if (OSSchedLockNestingCtr > 0u) { /* Can't pend when the scheduler is locked */
  1421. #if (OS_CFG_TS_EN > 0u)
  1422. if (p_ts != (CPU_TS *)0) {
  1423. *p_ts = 0u;
  1424. }
  1425. #endif
  1426. CPU_CRITICAL_EXIT();
  1427. OS_TRACE_TASK_SEM_PEND_FAILED(OSTCBCurPtr);
  1428. OS_TRACE_TASK_SEM_PEND_EXIT(OS_ERR_SCHED_LOCKED);
  1429. *p_err = OS_ERR_SCHED_LOCKED;
  1430. return (0u);
  1431. }
  1432. }
  1433. OS_Pend((OS_PEND_OBJ *)0, /* Block task pending on Signal */
  1434. OSTCBCurPtr,
  1435. OS_TASK_PEND_ON_TASK_SEM,
  1436. timeout);
  1437. CPU_CRITICAL_EXIT();
  1438. OS_TRACE_TASK_SEM_PEND_BLOCK(OSTCBCurPtr);
  1439. OSSched(); /* Find next highest priority task ready to run */
  1440. CPU_CRITICAL_ENTER();
  1441. switch (OSTCBCurPtr->PendStatus) { /* See if we timed-out or aborted */
  1442. case OS_STATUS_PEND_OK:
  1443. #if (OS_CFG_TS_EN > 0u)
  1444. if (p_ts != (CPU_TS *)0) {
  1445. *p_ts = OSTCBCurPtr->TS;
  1446. #if (OS_CFG_TASK_PROFILE_EN > 0u)
  1447. #if (OS_CFG_TS_EN > 0u)
  1448. OSTCBCurPtr->SemPendTime = OS_TS_GET() - OSTCBCurPtr->TS;
  1449. if (OSTCBCurPtr->SemPendTimeMax < OSTCBCurPtr->SemPendTime) {
  1450. OSTCBCurPtr->SemPendTimeMax = OSTCBCurPtr->SemPendTime;
  1451. }
  1452. #endif
  1453. #endif
  1454. }
  1455. #endif
  1456. OS_TRACE_TASK_SEM_PEND(OSTCBCurPtr);
  1457. *p_err = OS_ERR_NONE;
  1458. break;
  1459. case OS_STATUS_PEND_ABORT:
  1460. #if (OS_CFG_TS_EN > 0u)
  1461. if (p_ts != (CPU_TS *)0) {
  1462. *p_ts = OSTCBCurPtr->TS;
  1463. }
  1464. #endif
  1465. OS_TRACE_TASK_SEM_PEND_FAILED(OSTCBCurPtr);
  1466. *p_err = OS_ERR_PEND_ABORT; /* Indicate that we aborted */
  1467. break;
  1468. case OS_STATUS_PEND_TIMEOUT:
  1469. #if (OS_CFG_TS_EN > 0u)
  1470. if (p_ts != (CPU_TS *)0) {
  1471. *p_ts = 0u;
  1472. }
  1473. #endif
  1474. OS_TRACE_TASK_SEM_PEND_FAILED(OSTCBCurPtr);
  1475. *p_err = OS_ERR_TIMEOUT; /* Indicate that we didn't get event within TO */
  1476. break;
  1477. default:
  1478. OS_TRACE_TASK_SEM_PEND_FAILED(OSTCBCurPtr);
  1479. *p_err = OS_ERR_STATUS_INVALID;
  1480. break;
  1481. }
  1482. ctr = OSTCBCurPtr->SemCtr;
  1483. CPU_CRITICAL_EXIT();
  1484. OS_TRACE_TASK_SEM_PEND_EXIT(*p_err);
  1485. return (ctr);
  1486. }
  1487. /*
  1488. ************************************************************************************************************************
  1489. * ABORT WAITING FOR A SIGNAL
  1490. *
  1491. * Description: This function aborts & readies the task specified. This function should be used to fault-abort the wait
  1492. * for a signal, rather than to normally post the signal to the task via OSTaskSemPost().
  1493. *
  1494. * Arguments : p_tcb is a pointer to the task to pend abort
  1495. *
  1496. * opt provides options for this function:
  1497. *
  1498. * OS_OPT_POST_NONE No option selected
  1499. * OS_OPT_POST_NO_SCHED Indicates that the scheduler will not be called.
  1500. *
  1501. * p_err is a pointer to a variable that will contain an error code returned by this function.
  1502. *
  1503. * OS_ERR_NONE If the task was readied and informed of the aborted wait
  1504. * OS_ERR_OPT_INVALID You specified an invalid option
  1505. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  1506. * OS_ERR_PEND_ABORT_ISR If you tried calling this function from an ISR
  1507. * OS_ERR_PEND_ABORT_NONE If the task was not waiting for a signal
  1508. * OS_ERR_PEND_ABORT_SELF If you attempted to pend abort the calling task. This is not
  1509. * possible since the calling task cannot be pending because it's
  1510. * running
  1511. *
  1512. * Returns : == OS_FALSE if task was not waiting for a message, or upon error.
  1513. * == OS_TRUE if task was waiting for a message and was readied and informed.
  1514. *
  1515. * Note(s) : none
  1516. ************************************************************************************************************************
  1517. */
  1518. #if (OS_CFG_TASK_SEM_PEND_ABORT_EN > 0u)
  1519. CPU_BOOLEAN OSTaskSemPendAbort (OS_TCB *p_tcb,
  1520. OS_OPT opt,
  1521. OS_ERR *p_err)
  1522. {
  1523. CPU_TS ts;
  1524. CPU_SR_ALLOC();
  1525. #ifdef OS_SAFETY_CRITICAL
  1526. if (p_err == (OS_ERR *)0) {
  1527. OS_SAFETY_CRITICAL_EXCEPTION();
  1528. return (OS_FALSE);
  1529. }
  1530. #endif
  1531. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  1532. if (OSIntNestingCtr > 0u) { /* See if called from ISR ... */
  1533. *p_err = OS_ERR_PEND_ABORT_ISR; /* ... can't Pend Abort from an ISR */
  1534. return (OS_FALSE);
  1535. }
  1536. #endif
  1537. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  1538. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  1539. *p_err = OS_ERR_OS_NOT_RUNNING;
  1540. return (OS_FALSE);
  1541. }
  1542. #endif
  1543. #if (OS_CFG_ARG_CHK_EN > 0u)
  1544. switch (opt) { /* Validate 'opt' */
  1545. case OS_OPT_POST_NONE:
  1546. case OS_OPT_POST_NO_SCHED:
  1547. break;
  1548. default:
  1549. *p_err = OS_ERR_OPT_INVALID;
  1550. return (OS_FALSE);
  1551. }
  1552. #endif
  1553. CPU_CRITICAL_ENTER();
  1554. if ((p_tcb == (OS_TCB *)0) || /* Pend abort self? */
  1555. (p_tcb == OSTCBCurPtr)) {
  1556. CPU_CRITICAL_EXIT(); /* ... doesn't make sense! */
  1557. *p_err = OS_ERR_PEND_ABORT_SELF;
  1558. return (OS_FALSE);
  1559. }
  1560. if (p_tcb->PendOn != OS_TASK_PEND_ON_TASK_SEM) { /* Is task waiting for a signal? */
  1561. CPU_CRITICAL_EXIT();
  1562. *p_err = OS_ERR_PEND_ABORT_NONE;
  1563. return (OS_FALSE);
  1564. }
  1565. CPU_CRITICAL_EXIT();
  1566. CPU_CRITICAL_ENTER();
  1567. #if (OS_CFG_TS_EN > 0u)
  1568. ts = OS_TS_GET();
  1569. #else
  1570. ts = 0u;
  1571. #endif
  1572. OS_PendAbort(p_tcb, /* Abort the pend */
  1573. ts,
  1574. OS_STATUS_PEND_ABORT);
  1575. CPU_CRITICAL_EXIT();
  1576. if ((opt & OS_OPT_POST_NO_SCHED) == 0u) {
  1577. OSSched(); /* Run the scheduler */
  1578. }
  1579. *p_err = OS_ERR_NONE;
  1580. return (OS_TRUE);
  1581. }
  1582. #endif
  1583. /*
  1584. ************************************************************************************************************************
  1585. * SIGNAL A TASK
  1586. *
  1587. * Description: This function is called to signal a task waiting for a signal.
  1588. *
  1589. * Arguments : p_tcb is the pointer to the TCB of the task to signal. A NULL pointer indicates that you are sending
  1590. * a signal to yourself.
  1591. *
  1592. * opt determines the type of POST performed:
  1593. *
  1594. * OS_OPT_POST_NONE No option
  1595. * OS_OPT_POST_NO_SCHED Do not call the scheduler
  1596. *
  1597. * p_err is a pointer to an error code returned by this function:
  1598. *
  1599. * OS_ERR_NONE If the requested task is signaled
  1600. * OS_ERR_OPT_INVALID If you specified an invalid option
  1601. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  1602. * OS_ERR_SEM_OVF If the post would cause the semaphore count to overflow
  1603. * OS_ERR_STATE_INVALID If the task is in an invalid state. This should never happen
  1604. * and if it does, would be considered a system failure
  1605. *
  1606. * Returns : The current value of the task's signal counter or 0 if called from an ISR
  1607. *
  1608. * Note(s) : none
  1609. ************************************************************************************************************************
  1610. */
  1611. OS_SEM_CTR OSTaskSemPost (OS_TCB *p_tcb,
  1612. OS_OPT opt,
  1613. OS_ERR *p_err)
  1614. {
  1615. OS_SEM_CTR ctr;
  1616. CPU_TS ts;
  1617. CPU_SR_ALLOC();
  1618. #ifdef OS_SAFETY_CRITICAL
  1619. if (p_err == (OS_ERR *)0) {
  1620. OS_SAFETY_CRITICAL_EXCEPTION();
  1621. return (0u);
  1622. }
  1623. #endif
  1624. OS_TRACE_TASK_SEM_POST_ENTER(p_tcb, opt);
  1625. #if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
  1626. if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
  1627. OS_TRACE_TASK_SEM_POST_EXIT(OS_ERR_OS_NOT_RUNNING);
  1628. *p_err = OS_ERR_OS_NOT_RUNNING;
  1629. return (0u);
  1630. }
  1631. #endif
  1632. #if (OS_CFG_ARG_CHK_EN > 0u)
  1633. switch (opt) { /* Validate 'opt' */
  1634. case OS_OPT_POST_NONE:
  1635. case OS_OPT_POST_NO_SCHED:
  1636. break;
  1637. default:
  1638. OS_TRACE_TASK_SEM_POST_FAILED(p_tcb);
  1639. OS_TRACE_TASK_SEM_POST_EXIT(OS_ERR_OPT_INVALID);
  1640. *p_err = OS_ERR_OPT_INVALID;
  1641. return (0u);
  1642. }
  1643. #endif
  1644. #if (OS_CFG_TS_EN > 0u)
  1645. ts = OS_TS_GET(); /* Get timestamp */
  1646. #else
  1647. ts = 0u;
  1648. #endif
  1649. OS_TRACE_TASK_SEM_POST(p_tcb);
  1650. CPU_CRITICAL_ENTER();
  1651. if (p_tcb == (OS_TCB *)0) { /* Post signal to 'self'? */
  1652. p_tcb = OSTCBCurPtr;
  1653. }
  1654. #if (OS_CFG_TS_EN > 0u)
  1655. p_tcb->TS = ts;
  1656. #endif
  1657. *p_err = OS_ERR_NONE; /* Assume we won't have any errors */
  1658. switch (p_tcb->TaskState) {
  1659. case OS_TASK_STATE_RDY:
  1660. case OS_TASK_STATE_DLY:
  1661. case OS_TASK_STATE_SUSPENDED:
  1662. case OS_TASK_STATE_DLY_SUSPENDED:
  1663. if (p_tcb->SemCtr == (OS_SEM_CTR)-1) {
  1664. CPU_CRITICAL_EXIT();
  1665. *p_err = OS_ERR_SEM_OVF;
  1666. OS_TRACE_SEM_POST_EXIT(*p_err);
  1667. return (0u);
  1668. }
  1669. p_tcb->SemCtr++; /* Task signaled is not pending on anything */
  1670. ctr = p_tcb->SemCtr;
  1671. CPU_CRITICAL_EXIT();
  1672. break;
  1673. case OS_TASK_STATE_PEND:
  1674. case OS_TASK_STATE_PEND_TIMEOUT:
  1675. case OS_TASK_STATE_PEND_SUSPENDED:
  1676. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  1677. if (p_tcb->PendOn == OS_TASK_PEND_ON_TASK_SEM) { /* Is task signaled waiting for a signal? */
  1678. OS_Post((OS_PEND_OBJ *)0, /* Task is pending on signal */
  1679. p_tcb,
  1680. (void *)0,
  1681. 0u,
  1682. ts);
  1683. ctr = p_tcb->SemCtr;
  1684. CPU_CRITICAL_EXIT();
  1685. if ((opt & OS_OPT_POST_NO_SCHED) == 0u) {
  1686. OSSched(); /* Run the scheduler */
  1687. }
  1688. } else {
  1689. if (p_tcb->SemCtr == (OS_SEM_CTR)-1) {
  1690. CPU_CRITICAL_EXIT();
  1691. *p_err = OS_ERR_SEM_OVF;
  1692. OS_TRACE_SEM_POST_EXIT(*p_err);
  1693. return (0u);
  1694. }
  1695. p_tcb->SemCtr++; /* No, Task signaled is NOT pending on semaphore ... */
  1696. ctr = p_tcb->SemCtr; /* ... it must be waiting on something else */
  1697. CPU_CRITICAL_EXIT();
  1698. }
  1699. break;
  1700. default:
  1701. CPU_CRITICAL_EXIT();
  1702. *p_err = OS_ERR_STATE_INVALID;
  1703. ctr = 0u;
  1704. break;
  1705. }
  1706. OS_TRACE_TASK_SEM_POST_EXIT(*p_err);
  1707. return (ctr);
  1708. }
  1709. /*
  1710. ************************************************************************************************************************
  1711. * SET THE SIGNAL COUNTER OF A TASK
  1712. *
  1713. * Description: This function is called to clear the signal counter
  1714. *
  1715. * Arguments : p_tcb is the pointer to the TCB of the task to clear the counter. If you specify a NULL pointer
  1716. * then the signal counter of the current task will be cleared.
  1717. *
  1718. * cnt is the desired value of the semaphore counter
  1719. *
  1720. * p_err is a pointer to an error code returned by this function
  1721. *
  1722. * OS_ERR_NONE If the signal counter of the requested task is set
  1723. * OS_ERR_SET_ISR If the function was called from an ISR
  1724. * OS_ERR_TASK_WAITING One or more tasks were waiting on the semaphore
  1725. *
  1726. * Returns : none
  1727. *
  1728. * Note(s) : none
  1729. ************************************************************************************************************************
  1730. */
  1731. OS_SEM_CTR OSTaskSemSet (OS_TCB *p_tcb,
  1732. OS_SEM_CTR cnt,
  1733. OS_ERR *p_err)
  1734. {
  1735. OS_SEM_CTR ctr;
  1736. CPU_SR_ALLOC();
  1737. #ifdef OS_SAFETY_CRITICAL
  1738. if (p_err == (OS_ERR *)0) {
  1739. OS_SAFETY_CRITICAL_EXCEPTION();
  1740. return (0u);
  1741. }
  1742. #endif
  1743. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  1744. if (OSIntNestingCtr > 0u) { /* Not allowed to call from an ISR */
  1745. *p_err = OS_ERR_SET_ISR;
  1746. return (0u);
  1747. }
  1748. #endif
  1749. CPU_CRITICAL_ENTER();
  1750. if (p_tcb == (OS_TCB *)0) {
  1751. p_tcb = OSTCBCurPtr;
  1752. }
  1753. if (((p_tcb->TaskState & OS_TASK_STATE_PEND) != 0u) && /* Not allowed when a task is waiting. */
  1754. (p_tcb->PendOn == OS_TASK_PEND_ON_TASK_SEM)) {
  1755. CPU_CRITICAL_EXIT();
  1756. *p_err = OS_ERR_TASK_WAITING;
  1757. return (0u);
  1758. }
  1759. ctr = p_tcb->SemCtr;
  1760. p_tcb->SemCtr = (OS_SEM_CTR)cnt;
  1761. CPU_CRITICAL_EXIT();
  1762. *p_err = OS_ERR_NONE;
  1763. return (ctr);
  1764. }
  1765. /*
  1766. ************************************************************************************************************************
  1767. * STACK CHECKING
  1768. *
  1769. * Description: This function is called to calculate the amount of free memory left on the specified task's stack.
  1770. *
  1771. * Arguments : p_tcb is a pointer to the TCB of the task to check. If you specify a NULL pointer then
  1772. * you are specifying that you want to check the stack of the current task.
  1773. *
  1774. * p_free is a pointer to a variable that will receive the number of free 'entries' on the task's stack.
  1775. *
  1776. * p_used is a pointer to a variable that will receive the number of used 'entries' on the task's stack.
  1777. *
  1778. * p_err is a pointer to a variable that will contain an error code.
  1779. *
  1780. * OS_ERR_NONE Upon success
  1781. * OS_ERR_PTR_INVALID If either 'p_free' or 'p_used' are NULL pointers
  1782. * OS_ERR_TASK_NOT_EXIST If the stack pointer of the task is a NULL pointer
  1783. * OS_ERR_TASK_OPT If you did NOT specified OS_OPT_TASK_STK_CHK when the task
  1784. * was created
  1785. * OS_ERR_TASK_STK_CHK_ISR You called this function from an ISR
  1786. *
  1787. * Returns : none
  1788. *
  1789. * Note(s) : none
  1790. ************************************************************************************************************************
  1791. */
  1792. #if (OS_CFG_STAT_TASK_STK_CHK_EN > 0u)
  1793. void OSTaskStkChk (OS_TCB *p_tcb,
  1794. CPU_STK_SIZE *p_free,
  1795. CPU_STK_SIZE *p_used,
  1796. OS_ERR *p_err)
  1797. {
  1798. CPU_STK_SIZE free_stk;
  1799. CPU_STK_SIZE stk_size;
  1800. CPU_STK *p_stk;
  1801. CPU_SR_ALLOC();
  1802. #ifdef OS_SAFETY_CRITICAL
  1803. if (p_err == (OS_ERR *)0) {
  1804. OS_SAFETY_CRITICAL_EXCEPTION();
  1805. return;
  1806. }
  1807. #endif
  1808. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  1809. if (OSIntNestingCtr > 0u) { /* See if trying to check stack from ISR */
  1810. *p_err = OS_ERR_TASK_STK_CHK_ISR;
  1811. return;
  1812. }
  1813. #endif
  1814. #if (OS_CFG_ARG_CHK_EN > 0u)
  1815. if (p_free == (CPU_STK_SIZE *)0) { /* User must specify valid destinations for the sizes */
  1816. *p_err = OS_ERR_PTR_INVALID;
  1817. return;
  1818. }
  1819. if (p_used == (CPU_STK_SIZE *)0) {
  1820. *p_err = OS_ERR_PTR_INVALID;
  1821. return;
  1822. }
  1823. #endif
  1824. CPU_CRITICAL_ENTER();
  1825. if (p_tcb == (OS_TCB *)0) { /* Check the stack of the current task? */
  1826. p_tcb = OSTCBCurPtr; /* Yes */
  1827. }
  1828. if (p_tcb->StkPtr == (CPU_STK *)0) { /* Make sure task exist */
  1829. CPU_CRITICAL_EXIT();
  1830. *p_free = 0u;
  1831. *p_used = 0u;
  1832. *p_err = OS_ERR_TASK_NOT_EXIST;
  1833. return;
  1834. }
  1835. if ((p_tcb->Opt & OS_OPT_TASK_STK_CHK) == 0u) { /* Make sure stack checking option is set */
  1836. CPU_CRITICAL_EXIT();
  1837. *p_free = 0u;
  1838. *p_used = 0u;
  1839. *p_err = OS_ERR_TASK_OPT;
  1840. return;
  1841. }
  1842. #if (CPU_CFG_STK_GROWTH == CPU_STK_GROWTH_HI_TO_LO)
  1843. p_stk = p_tcb->StkBasePtr; /* Start at the lowest memory and go up */
  1844. #if (OS_CFG_TASK_STK_REDZONE_EN > 0u)
  1845. p_stk += OS_CFG_TASK_STK_REDZONE_DEPTH;
  1846. #endif
  1847. #else
  1848. p_stk = p_tcb->StkBasePtr + p_tcb->StkSize - 1u; /* Start at the highest memory and go down */
  1849. #if (OS_CFG_TASK_STK_REDZONE_EN > 0u)
  1850. p_stk -= OS_CFG_TASK_STK_REDZONE_DEPTH;
  1851. #endif
  1852. #endif
  1853. stk_size = p_tcb->StkSize;
  1854. CPU_CRITICAL_EXIT();
  1855. free_stk = 0u;
  1856. /* Compute the number of zero entries on the stk */
  1857. #if (CPU_CFG_STK_GROWTH == CPU_STK_GROWTH_HI_TO_LO)
  1858. while ((free_stk < stk_size) &&
  1859. (*p_stk == 0u)) {
  1860. p_stk++;
  1861. free_stk++;
  1862. }
  1863. #else
  1864. while ((free_stk < stk_size) &&
  1865. (*p_stk == 0u)) {
  1866. free_stk++;
  1867. p_stk--;
  1868. }
  1869. #endif
  1870. *p_free = free_stk;
  1871. *p_used = (stk_size - free_stk); /* Compute number of entries used on the stack */
  1872. *p_err = OS_ERR_NONE;
  1873. }
  1874. #endif
  1875. /*
  1876. ************************************************************************************************************************
  1877. * CHECK THE STACK REDZONE OF A TASK
  1878. *
  1879. * Description: Verify a task's stack redzone.
  1880. *
  1881. * Arguments : p_tcb is a pointer to the TCB of the task to check or null for the current task.
  1882. *
  1883. * Returns : If the stack is corrupted (OS_FALSE) or not (OS_TRUE).
  1884. *
  1885. * Note(s) : These functions are INTERNAL to uC/OS-III and your application should not call it.
  1886. ************************************************************************************************************************
  1887. */
  1888. #if (OS_CFG_TASK_STK_REDZONE_EN > 0u)
  1889. CPU_BOOLEAN OSTaskStkRedzoneChk (OS_TCB *p_tcb)
  1890. {
  1891. CPU_BOOLEAN stk_status;
  1892. if (p_tcb == (OS_TCB *)0) {
  1893. p_tcb = OSTCBCurPtr;
  1894. }
  1895. /* Check if SP is valid: */
  1896. /* StkBase <= SP < (StkBase + StkSize) */
  1897. if ((p_tcb->StkPtr < p_tcb->StkBasePtr) ||
  1898. (p_tcb->StkPtr >= (p_tcb->StkBasePtr + p_tcb->StkSize))) {
  1899. return (OS_FALSE);
  1900. }
  1901. stk_status = OS_TaskStkRedzoneChk(p_tcb->StkBasePtr, p_tcb->StkSize);
  1902. return (stk_status);
  1903. }
  1904. #endif
  1905. /*
  1906. ************************************************************************************************************************
  1907. * SUSPEND A TASK
  1908. *
  1909. * Description: This function is called to suspend a task. The task can be the calling task if 'p_tcb' is a NULL pointer
  1910. * or the pointer to the TCB of the calling task.
  1911. *
  1912. * Arguments : p_tcb is a pointer to the TCB to suspend.
  1913. * If p_tcb is a NULL pointer then, suspend the current task.
  1914. *
  1915. * p_err is a pointer to a variable that will receive an error code from this function.
  1916. *
  1917. * OS_ERR_NONE If the requested task is suspended
  1918. * OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
  1919. * OS_ERR_SCHED_LOCKED You can't suspend the current task is the scheduler is
  1920. * locked
  1921. * OS_ERR_STATE_INVALID If the task is in an invalid state
  1922. * OS_ERR_TASK_SUSPEND_CTR_OVF If the nesting counter overflowed.
  1923. * OS_ERR_TASK_SUSPEND_ISR If you called this function from an ISR
  1924. * OS_ERR_TASK_SUSPEND_IDLE If you attempted to suspend the idle task which is not
  1925. * allowed
  1926. * OS_ERR_TASK_SUSPEND_INT_HANDLER If you attempted to suspend the idle task which is not
  1927. * allowed
  1928. *
  1929. * Returns : none
  1930. *
  1931. * Note(s) : 1) You should use this function with great care. If you suspend a task that is waiting for an event
  1932. * (i.e. a message, a semaphore, a queue ...) you will prevent this task from running when the event
  1933. * arrives.
  1934. ************************************************************************************************************************
  1935. */
  1936. #if (OS_CFG_TASK_SUSPEND_EN > 0u)
  1937. void OSTaskSuspend (OS_TCB *p_tcb,
  1938. OS_ERR *p_err)
  1939. {
  1940. CPU_SR_ALLOC();
  1941. OS_TRACE_TASK_SUSPEND_ENTER(p_tcb);
  1942. #ifdef OS_SAFETY_CRITICAL
  1943. if (p_err == (OS_ERR *)0) {
  1944. OS_SAFETY_CRITICAL_EXCEPTION();
  1945. return;
  1946. }
  1947. #endif
  1948. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  1949. if (OSIntNestingCtr > 0u) { /* Not allowed to call from an ISR */
  1950. *p_err = OS_ERR_TASK_SUSPEND_ISR;
  1951. OS_TRACE_TASK_SUSPEND_EXIT(OS_ERR_TASK_SUSPEND_ISR);
  1952. return;
  1953. }
  1954. #endif
  1955. #if (OS_CFG_TASK_IDLE_EN > 0u)
  1956. if (p_tcb == &OSIdleTaskTCB) { /* Make sure not suspending the idle task */
  1957. *p_err = OS_ERR_TASK_SUSPEND_IDLE;
  1958. OS_TRACE_TASK_SUSPEND_EXIT(OS_ERR_TASK_SUSPEND_IDLE);
  1959. return;
  1960. }
  1961. #endif
  1962. CPU_CRITICAL_ENTER();
  1963. if (p_tcb == (OS_TCB *)0) { /* See if specified to suspend self */
  1964. if (OSRunning != OS_STATE_OS_RUNNING) { /* Can't suspend self when the kernel isn't running */
  1965. CPU_CRITICAL_EXIT();
  1966. *p_err = OS_ERR_OS_NOT_RUNNING;
  1967. OS_TRACE_TASK_SUSPEND_EXIT(OS_ERR_OS_NOT_RUNNING);
  1968. return;
  1969. }
  1970. p_tcb = OSTCBCurPtr;
  1971. }
  1972. if (p_tcb == OSTCBCurPtr) {
  1973. if (OSSchedLockNestingCtr > 0u) { /* Can't suspend when the scheduler is locked */
  1974. CPU_CRITICAL_EXIT();
  1975. *p_err = OS_ERR_SCHED_LOCKED;
  1976. OS_TRACE_TASK_SUSPEND_EXIT(OS_ERR_SCHED_LOCKED);
  1977. return;
  1978. }
  1979. }
  1980. *p_err = OS_ERR_NONE;
  1981. switch (p_tcb->TaskState) {
  1982. case OS_TASK_STATE_RDY:
  1983. p_tcb->TaskState = OS_TASK_STATE_SUSPENDED;
  1984. p_tcb->SuspendCtr = 1u;
  1985. OS_RdyListRemove(p_tcb);
  1986. OS_TRACE_TASK_SUSPEND(p_tcb);
  1987. CPU_CRITICAL_EXIT();
  1988. break;
  1989. case OS_TASK_STATE_DLY:
  1990. p_tcb->TaskState = OS_TASK_STATE_DLY_SUSPENDED;
  1991. p_tcb->SuspendCtr = 1u;
  1992. CPU_CRITICAL_EXIT();
  1993. break;
  1994. case OS_TASK_STATE_PEND:
  1995. p_tcb->TaskState = OS_TASK_STATE_PEND_SUSPENDED;
  1996. p_tcb->SuspendCtr = 1u;
  1997. CPU_CRITICAL_EXIT();
  1998. break;
  1999. case OS_TASK_STATE_PEND_TIMEOUT:
  2000. p_tcb->TaskState = OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED;
  2001. p_tcb->SuspendCtr = 1u;
  2002. CPU_CRITICAL_EXIT();
  2003. break;
  2004. case OS_TASK_STATE_SUSPENDED:
  2005. case OS_TASK_STATE_DLY_SUSPENDED:
  2006. case OS_TASK_STATE_PEND_SUSPENDED:
  2007. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  2008. if (p_tcb->SuspendCtr == (OS_NESTING_CTR)-1) {
  2009. CPU_CRITICAL_EXIT();
  2010. *p_err = OS_ERR_TASK_SUSPEND_CTR_OVF;
  2011. OS_TRACE_TASK_SUSPEND_EXIT(OS_ERR_TASK_SUSPEND_CTR_OVF);
  2012. return;
  2013. }
  2014. p_tcb->SuspendCtr++;
  2015. CPU_CRITICAL_EXIT();
  2016. break;
  2017. default:
  2018. CPU_CRITICAL_EXIT();
  2019. *p_err = OS_ERR_STATE_INVALID;
  2020. OS_TRACE_TASK_SUSPEND_EXIT(OS_ERR_STATE_INVALID);
  2021. return;
  2022. }
  2023. if (OSRunning == OS_STATE_OS_RUNNING) { /* Only schedule when the kernel is running */
  2024. OSSched();
  2025. OS_TRACE_TASK_SUSPEND_EXIT(OS_ERR_NONE);
  2026. }
  2027. }
  2028. #endif
  2029. /*
  2030. ************************************************************************************************************************
  2031. * CHANGE A TASK'S TIME SLICE
  2032. *
  2033. * Description: This function is called to change the value of the task's specific time slice.
  2034. *
  2035. * Arguments : p_tcb is the pointer to the TCB of the task to change. If you specify an NULL pointer, the current
  2036. * task is assumed.
  2037. *
  2038. * time_quanta is the number of ticks before the CPU is taken away when round-robin scheduling is enabled.
  2039. *
  2040. * p_err is a pointer to an error code returned by this function:
  2041. *
  2042. * OS_ERR_NONE Upon success
  2043. * OS_ERR_SET_ISR If you called this function from an ISR
  2044. *
  2045. * Returns : none
  2046. *
  2047. * Note(s) : none
  2048. ************************************************************************************************************************
  2049. */
  2050. #if (OS_CFG_SCHED_ROUND_ROBIN_EN > 0u)
  2051. void OSTaskTimeQuantaSet (OS_TCB *p_tcb,
  2052. OS_TICK time_quanta,
  2053. OS_ERR *p_err)
  2054. {
  2055. CPU_SR_ALLOC();
  2056. #ifdef OS_SAFETY_CRITICAL
  2057. if (p_err == (OS_ERR *)0) {
  2058. OS_SAFETY_CRITICAL_EXCEPTION();
  2059. return;
  2060. }
  2061. #endif
  2062. #if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
  2063. if (OSIntNestingCtr > 0u) { /* Can't call this function from an ISR */
  2064. *p_err = OS_ERR_SET_ISR;
  2065. return;
  2066. }
  2067. #endif
  2068. CPU_CRITICAL_ENTER();
  2069. if (p_tcb == (OS_TCB *)0) {
  2070. p_tcb = OSTCBCurPtr;
  2071. }
  2072. if (time_quanta == 0u) {
  2073. p_tcb->TimeQuanta = OSSchedRoundRobinDfltTimeQuanta;
  2074. } else {
  2075. p_tcb->TimeQuanta = time_quanta;
  2076. }
  2077. if (p_tcb->TimeQuanta > p_tcb->TimeQuantaCtr) {
  2078. p_tcb->TimeQuantaCtr = p_tcb->TimeQuanta;
  2079. }
  2080. CPU_CRITICAL_EXIT();
  2081. *p_err = OS_ERR_NONE;
  2082. }
  2083. #endif
  2084. /*
  2085. ************************************************************************************************************************
  2086. * ADD/REMOVE TASK TO/FROM DEBUG LIST
  2087. *
  2088. * Description: These functions are called by uC/OS-III to add or remove an OS_TCB from the debug list.
  2089. *
  2090. * Arguments : p_tcb is a pointer to the OS_TCB to add/remove
  2091. *
  2092. * Returns : none
  2093. *
  2094. * Note(s) : These functions are INTERNAL to uC/OS-III and your application should not call it.
  2095. ************************************************************************************************************************
  2096. */
  2097. #if (OS_CFG_DBG_EN > 0u)
  2098. void OS_TaskDbgListAdd (OS_TCB *p_tcb)
  2099. {
  2100. p_tcb->DbgPrevPtr = (OS_TCB *)0;
  2101. if (OSTaskDbgListPtr == (OS_TCB *)0) {
  2102. p_tcb->DbgNextPtr = (OS_TCB *)0;
  2103. } else {
  2104. p_tcb->DbgNextPtr = OSTaskDbgListPtr;
  2105. OSTaskDbgListPtr->DbgPrevPtr = p_tcb;
  2106. }
  2107. OSTaskDbgListPtr = p_tcb;
  2108. }
  2109. void OS_TaskDbgListRemove (OS_TCB *p_tcb)
  2110. {
  2111. OS_TCB *p_tcb_next;
  2112. OS_TCB *p_tcb_prev;
  2113. p_tcb_prev = p_tcb->DbgPrevPtr;
  2114. p_tcb_next = p_tcb->DbgNextPtr;
  2115. if (p_tcb_prev == (OS_TCB *)0) {
  2116. OSTaskDbgListPtr = p_tcb_next;
  2117. if (p_tcb_next != (OS_TCB *)0) {
  2118. p_tcb_next->DbgPrevPtr = (OS_TCB *)0;
  2119. }
  2120. p_tcb->DbgNextPtr = (OS_TCB *)0;
  2121. } else if (p_tcb_next == (OS_TCB *)0) {
  2122. p_tcb_prev->DbgNextPtr = (OS_TCB *)0;
  2123. p_tcb->DbgPrevPtr = (OS_TCB *)0;
  2124. } else {
  2125. p_tcb_prev->DbgNextPtr = p_tcb_next;
  2126. p_tcb_next->DbgPrevPtr = p_tcb_prev;
  2127. p_tcb->DbgNextPtr = (OS_TCB *)0;
  2128. p_tcb->DbgPrevPtr = (OS_TCB *)0;
  2129. }
  2130. }
  2131. #endif
  2132. /*
  2133. ************************************************************************************************************************
  2134. * TASK MANAGER INITIALIZATION
  2135. *
  2136. * Description: This function is called by OSInit() to initialize the task management.
  2137. *
  2138. * Argument(s): p_err is a pointer to a variable that will contain an error code returned by this function.
  2139. *
  2140. * OS_ERR_NONE the call was successful
  2141. *
  2142. * Returns : none
  2143. *
  2144. * Note(s) : This function is INTERNAL to uC/OS-III and your application should not call it.
  2145. ************************************************************************************************************************
  2146. */
  2147. void OS_TaskInit (OS_ERR *p_err)
  2148. {
  2149. #if (OS_CFG_DBG_EN > 0u)
  2150. OSTaskDbgListPtr = (OS_TCB *)0;
  2151. #endif
  2152. OSTaskQty = 0u; /* Clear the number of tasks */
  2153. #if ((OS_CFG_TASK_PROFILE_EN > 0u) || (OS_CFG_DBG_EN > 0u))
  2154. OSTaskCtxSwCtr = 0u; /* Clear the context switch counter */
  2155. #endif
  2156. *p_err = OS_ERR_NONE;
  2157. }
  2158. /*
  2159. ************************************************************************************************************************
  2160. * INITIALIZE TCB FIELDS
  2161. *
  2162. * Description: This function is called to initialize a TCB to default values
  2163. *
  2164. * Arguments : p_tcb is a pointer to the TCB to initialize
  2165. *
  2166. * Returns : none
  2167. *
  2168. * Note(s) : This function is INTERNAL to uC/OS-III and your application should not call it.
  2169. ************************************************************************************************************************
  2170. */
  2171. void OS_TaskInitTCB (OS_TCB *p_tcb)
  2172. {
  2173. #if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
  2174. OS_REG_ID reg_id;
  2175. #endif
  2176. #if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
  2177. OS_TLS_ID id;
  2178. #endif
  2179. p_tcb->StkPtr = (CPU_STK *)0;
  2180. p_tcb->StkLimitPtr = (CPU_STK *)0;
  2181. p_tcb->ExtPtr = (void *)0;
  2182. p_tcb->NextPtr = (OS_TCB *)0;
  2183. p_tcb->PrevPtr = (OS_TCB *)0;
  2184. #if (OS_CFG_TICK_EN > 0u)
  2185. p_tcb->TickNextPtr = (OS_TCB *)0;
  2186. p_tcb->TickPrevPtr = (OS_TCB *)0;
  2187. #endif
  2188. #if (OS_CFG_DBG_EN > 0u)
  2189. p_tcb->NamePtr = (CPU_CHAR *)((void *)"?Task");
  2190. #endif
  2191. #if ((OS_CFG_DBG_EN > 0u) || (OS_CFG_STAT_TASK_STK_CHK_EN > 0u))
  2192. p_tcb->StkBasePtr = (CPU_STK *)0;
  2193. #endif
  2194. #if (OS_CFG_DBG_EN > 0u)
  2195. p_tcb->TaskEntryAddr = (OS_TASK_PTR )0;
  2196. p_tcb->TaskEntryArg = (void *)0;
  2197. #endif
  2198. #if (OS_CFG_TS_EN > 0u)
  2199. p_tcb->TS = 0u;
  2200. #endif
  2201. #if (OS_MSG_EN > 0u)
  2202. p_tcb->MsgPtr = (void *)0;
  2203. p_tcb->MsgSize = 0u;
  2204. #endif
  2205. #if (OS_CFG_TASK_Q_EN > 0u)
  2206. OS_MsgQInit(&p_tcb->MsgQ,
  2207. 0u);
  2208. #if (OS_CFG_TASK_PROFILE_EN > 0u)
  2209. p_tcb->MsgQPendTime = 0u;
  2210. p_tcb->MsgQPendTimeMax = 0u;
  2211. #endif
  2212. #endif
  2213. #if (OS_CFG_FLAG_EN > 0u)
  2214. p_tcb->FlagsPend = 0u;
  2215. p_tcb->FlagsOpt = 0u;
  2216. p_tcb->FlagsRdy = 0u;
  2217. #endif
  2218. #if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
  2219. for (reg_id = 0u; reg_id < OS_CFG_TASK_REG_TBL_SIZE; reg_id++) {
  2220. p_tcb->RegTbl[reg_id] = 0u;
  2221. }
  2222. #endif
  2223. #if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
  2224. for (id = 0u; id < OS_CFG_TLS_TBL_SIZE; id++) {
  2225. p_tcb->TLS_Tbl[id] = 0u;
  2226. }
  2227. #endif
  2228. p_tcb->SemCtr = 0u;
  2229. #if (OS_CFG_TASK_PROFILE_EN > 0u)
  2230. p_tcb->SemPendTime = 0u;
  2231. p_tcb->SemPendTimeMax = 0u;
  2232. #endif
  2233. #if ((OS_CFG_DBG_EN > 0u) || (OS_CFG_STAT_TASK_STK_CHK_EN > 0u))
  2234. p_tcb->StkSize = 0u;
  2235. #endif
  2236. #if (OS_CFG_TASK_SUSPEND_EN > 0u)
  2237. p_tcb->SuspendCtr = 0u;
  2238. #endif
  2239. #if (OS_CFG_STAT_TASK_STK_CHK_EN > 0u)
  2240. p_tcb->StkFree = 0u;
  2241. p_tcb->StkUsed = 0u;
  2242. #endif
  2243. p_tcb->Opt = 0u;
  2244. #if (OS_CFG_TICK_EN > 0u)
  2245. p_tcb->TickRemain = 0u;
  2246. p_tcb->TickCtrPrev = 0u;
  2247. #endif
  2248. #if (OS_CFG_SCHED_ROUND_ROBIN_EN > 0u)
  2249. p_tcb->TimeQuanta = 0u;
  2250. p_tcb->TimeQuantaCtr = 0u;
  2251. #endif
  2252. #if (OS_CFG_TASK_PROFILE_EN > 0u)
  2253. p_tcb->CPUUsage = 0u;
  2254. p_tcb->CPUUsageMax = 0u;
  2255. p_tcb->CtxSwCtr = 0u;
  2256. p_tcb->CyclesDelta = 0u;
  2257. #if (OS_CFG_TS_EN > 0u)
  2258. p_tcb->CyclesStart = OS_TS_GET(); /* Read the current timestamp and save */
  2259. #else
  2260. p_tcb->CyclesStart = 0u;
  2261. #endif
  2262. p_tcb->CyclesTotal = 0u;
  2263. #endif
  2264. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  2265. p_tcb->IntDisTimeMax = 0u;
  2266. #endif
  2267. #if (OS_CFG_SCHED_LOCK_TIME_MEAS_EN > 0u)
  2268. p_tcb->SchedLockTimeMax = 0u;
  2269. #endif
  2270. p_tcb->PendNextPtr = (OS_TCB *)0;
  2271. p_tcb->PendPrevPtr = (OS_TCB *)0;
  2272. p_tcb->PendObjPtr = (OS_PEND_OBJ *)0;
  2273. p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING;
  2274. p_tcb->PendStatus = OS_STATUS_PEND_OK;
  2275. p_tcb->TaskState = OS_TASK_STATE_RDY;
  2276. p_tcb->Prio = OS_PRIO_INIT;
  2277. #if (OS_CFG_MUTEX_EN > 0u)
  2278. p_tcb->BasePrio = OS_PRIO_INIT;
  2279. p_tcb->MutexGrpHeadPtr = (OS_MUTEX *)0;
  2280. #endif
  2281. #if (OS_CFG_DBG_EN > 0u)
  2282. p_tcb->DbgPrevPtr = (OS_TCB *)0;
  2283. p_tcb->DbgNextPtr = (OS_TCB *)0;
  2284. p_tcb->DbgNamePtr = (CPU_CHAR *)((void *)" ");
  2285. #endif
  2286. }
  2287. /*
  2288. ************************************************************************************************************************
  2289. * CATCH ACCIDENTAL TASK RETURN
  2290. *
  2291. * Description: This function is called if a task accidentally returns without deleting itself. In other words, a task
  2292. * should either be an infinite loop or delete itself if it's done.
  2293. *
  2294. * Arguments : none
  2295. *
  2296. * Returns : none
  2297. *
  2298. * Note(s) : This function is INTERNAL to uC/OS-III and your application should not call it.
  2299. ************************************************************************************************************************
  2300. */
  2301. void OS_TaskReturn (void)
  2302. {
  2303. OS_ERR err;
  2304. OSTaskReturnHook(OSTCBCurPtr); /* Call hook to let user decide on what to do */
  2305. #if (OS_CFG_TASK_DEL_EN > 0u)
  2306. OSTaskDel((OS_TCB *)0, /* Delete task if it accidentally returns! */
  2307. &err);
  2308. #else
  2309. for (;;) {
  2310. OSTimeDly(OSCfg_TickRate_Hz,
  2311. OS_OPT_TIME_DLY,
  2312. &err);
  2313. }
  2314. #endif
  2315. }
  2316. /*
  2317. ************************************************************************************************************************
  2318. * CHECK THE STACK REDZONE OF A TASK
  2319. *
  2320. * Description: Verify a task's stack redzone.
  2321. *
  2322. * Arguments : p_tcb is a pointer to the base of the stack.
  2323. *
  2324. * stk_size is the size of the stack.
  2325. *
  2326. * Returns : If the stack is corrupted (OS_FALSE) or not (OS_TRUE).
  2327. *
  2328. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application should not call it.
  2329. ************************************************************************************************************************
  2330. */
  2331. #if (OS_CFG_TASK_STK_REDZONE_EN > 0u)
  2332. CPU_BOOLEAN OS_TaskStkRedzoneChk (CPU_STK *p_base,
  2333. CPU_STK_SIZE stk_size)
  2334. {
  2335. CPU_INT32U i;
  2336. #if (CPU_CFG_STK_GROWTH == CPU_STK_GROWTH_HI_TO_LO)
  2337. (void)stk_size; /* Prevent compiler warning for not using 'stk_size' */
  2338. for (i = 0u; i < OS_CFG_TASK_STK_REDZONE_DEPTH; i++) {
  2339. if (*p_base != (CPU_DATA)OS_STACK_CHECK_VAL) {
  2340. return (OS_FALSE);
  2341. }
  2342. p_base++;
  2343. }
  2344. #else
  2345. p_base = p_base + stk_size - 1u;
  2346. for (i = 0u; i < OS_CFG_TASK_STK_REDZONE_DEPTH; i++) {
  2347. if (*p_base != (CPU_DATA)OS_STACK_CHECK_VAL) {
  2348. return (OS_FALSE);
  2349. }
  2350. p_base--;
  2351. }
  2352. #endif
  2353. return (OS_TRUE);
  2354. }
  2355. #endif
  2356. /*
  2357. ************************************************************************************************************************
  2358. * INITIALIZE A REDZONE ENABLED STACK
  2359. *
  2360. * Description: This functions is used to initialize a stack with Redzone checking.
  2361. *
  2362. * Arguments : p_tcb is a pointer to the base of the stack.
  2363. *
  2364. * stk_size is the size of the stack.
  2365. *
  2366. * Returns : none.
  2367. *
  2368. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application should not call it.
  2369. ************************************************************************************************************************
  2370. */
  2371. #if (OS_CFG_TASK_STK_REDZONE_EN > 0u)
  2372. void OS_TaskStkRedzoneInit (CPU_STK *p_base,
  2373. CPU_STK_SIZE stk_size)
  2374. {
  2375. CPU_STK_SIZE i;
  2376. #if (CPU_CFG_STK_GROWTH == CPU_STK_GROWTH_HI_TO_LO)
  2377. (void)stk_size; /* Prevent compiler warning for not using 'stk_size' */
  2378. for (i = 0u; i < OS_CFG_TASK_STK_REDZONE_DEPTH; i++) {
  2379. *(p_base + i) = (CPU_DATA)OS_STACK_CHECK_VAL;
  2380. }
  2381. #else
  2382. for (i = 0u; i < OS_CFG_TASK_STK_REDZONE_DEPTH; i++) {
  2383. *(p_base + stk_size - 1u - i) = (CPU_DATA)OS_STACK_CHECK_VAL;
  2384. }
  2385. #endif
  2386. }
  2387. #endif
  2388. /*
  2389. ************************************************************************************************************************
  2390. * CHANGE PRIORITY OF A TASK
  2391. *
  2392. * Description: This function is called by the kernel to perform the actual operation of changing a task's priority.
  2393. * Priority inheritance is updated if necessary.
  2394. *
  2395. *
  2396. *
  2397. * Argument(s): p_tcb is a pointer to the tcb of the task to change the priority.
  2398. *
  2399. * prio_new is the new priority to give to the task.
  2400. *
  2401. *
  2402. * Returns : none.
  2403. *
  2404. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application MUST NOT call it.
  2405. ************************************************************************************************************************
  2406. */
  2407. void OS_TaskChangePrio(OS_TCB *p_tcb,
  2408. OS_PRIO prio_new)
  2409. {
  2410. OS_TCB *p_tcb_owner;
  2411. #if (OS_CFG_MUTEX_EN > 0u)
  2412. OS_PRIO prio_cur;
  2413. #endif
  2414. do {
  2415. p_tcb_owner = (OS_TCB *)0;
  2416. #if (OS_CFG_MUTEX_EN > 0u)
  2417. prio_cur = p_tcb->Prio;
  2418. #endif
  2419. switch (p_tcb->TaskState) {
  2420. case OS_TASK_STATE_RDY:
  2421. OS_RdyListRemove(p_tcb); /* Remove from current priority */
  2422. p_tcb->Prio = prio_new; /* Set new task priority */
  2423. OS_PrioInsert(p_tcb->Prio);
  2424. if (p_tcb == OSTCBCurPtr) {
  2425. OS_RdyListInsertHead(p_tcb);
  2426. } else {
  2427. OS_RdyListInsertTail(p_tcb);
  2428. }
  2429. break;
  2430. case OS_TASK_STATE_DLY: /* Nothing to do except change the priority in the OS_TCB*/
  2431. case OS_TASK_STATE_SUSPENDED:
  2432. case OS_TASK_STATE_DLY_SUSPENDED:
  2433. p_tcb->Prio = prio_new; /* Set new task priority */
  2434. break;
  2435. case OS_TASK_STATE_PEND:
  2436. case OS_TASK_STATE_PEND_TIMEOUT:
  2437. case OS_TASK_STATE_PEND_SUSPENDED:
  2438. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  2439. p_tcb->Prio = prio_new; /* Set new task priority */
  2440. switch (p_tcb->PendOn) { /* What to do depends on what we are pending on */
  2441. case OS_TASK_PEND_ON_FLAG:
  2442. case OS_TASK_PEND_ON_Q:
  2443. case OS_TASK_PEND_ON_SEM:
  2444. OS_PendListChangePrio(p_tcb);
  2445. break;
  2446. case OS_TASK_PEND_ON_MUTEX:
  2447. #if (OS_CFG_MUTEX_EN > 0u)
  2448. OS_PendListChangePrio(p_tcb);
  2449. p_tcb_owner = ((OS_MUTEX *)((void *)p_tcb->PendObjPtr))->OwnerTCBPtr;
  2450. if (prio_cur > prio_new) { /* Are we increasing the priority? */
  2451. if (p_tcb_owner->Prio <= prio_new) { /* Yes, do we need to give this prio to the owner? */
  2452. p_tcb_owner = (OS_TCB *)0;
  2453. } else {
  2454. /* Block is empty when trace is disabled. */
  2455. OS_TRACE_MUTEX_TASK_PRIO_INHERIT(p_tcb_owner, prio_new);
  2456. }
  2457. } else {
  2458. if (p_tcb_owner->Prio == prio_cur) { /* No, is it required to check for a lower prio? */
  2459. prio_new = OS_MutexGrpPrioFindHighest(p_tcb_owner);
  2460. prio_new = (prio_new > p_tcb_owner->BasePrio) ? p_tcb_owner->BasePrio : prio_new;
  2461. if (prio_new == p_tcb_owner->Prio) {
  2462. p_tcb_owner = (OS_TCB *)0;
  2463. } else {
  2464. /* Block is empty when trace is disabled. */
  2465. OS_TRACE_MUTEX_TASK_PRIO_DISINHERIT(p_tcb_owner, prio_new);
  2466. }
  2467. } else {
  2468. p_tcb_owner = (OS_TCB *)0;
  2469. }
  2470. }
  2471. #endif
  2472. break;
  2473. case OS_TASK_PEND_ON_TASK_Q:
  2474. case OS_TASK_PEND_ON_TASK_SEM:
  2475. default:
  2476. /* Default case. */
  2477. break;
  2478. }
  2479. break;
  2480. default:
  2481. return;
  2482. }
  2483. p_tcb = p_tcb_owner;
  2484. } while (p_tcb != (OS_TCB *)0);
  2485. }