tasks.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755
  1. /*
  2. FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
  3. FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
  4. http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. ***************************************************************************
  6. * *
  7. * FreeRTOS tutorial books are available in pdf and paperback. *
  8. * Complete, revised, and edited pdf reference manuals are also *
  9. * available. *
  10. * *
  11. * Purchasing FreeRTOS documentation will not only help you, by *
  12. * ensuring you get running as quickly as possible and with an *
  13. * in-depth knowledge of how to use FreeRTOS, it will also help *
  14. * the FreeRTOS project to continue with its mission of providing *
  15. * professional grade, cross platform, de facto standard solutions *
  16. * for microcontrollers - completely free of charge! *
  17. * *
  18. * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
  19. * *
  20. * Thank you for using FreeRTOS, and thank you for your support! *
  21. * *
  22. ***************************************************************************
  23. This file is part of the FreeRTOS distribution.
  24. FreeRTOS is free software; you can redistribute it and/or modify it under
  25. the terms of the GNU General Public License (version 2) as published by the
  26. Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
  27. >>>NOTE<<< The modification to the GPL is included to allow you to
  28. distribute a combined work that includes FreeRTOS without being obliged to
  29. provide the source code for proprietary components outside of the FreeRTOS
  30. kernel. FreeRTOS is distributed in the hope that it will be useful, but
  31. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  32. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  33. more details. You should have received a copy of the GNU General Public
  34. License and the FreeRTOS license exception along with FreeRTOS; if not it
  35. can be viewed here: http://www.freertos.org/a00114.html and also obtained
  36. by writing to Richard Barry, contact details for whom are available on the
  37. FreeRTOS WEB site.
  38. 1 tab == 4 spaces!
  39. ***************************************************************************
  40. * *
  41. * Having a problem? Start by reading the FAQ "My application does *
  42. * not run, what could be wrong?" *
  43. * *
  44. * http://www.FreeRTOS.org/FAQHelp.html *
  45. * *
  46. ***************************************************************************
  47. http://www.FreeRTOS.org - Documentation, training, latest versions, license
  48. and contact details.
  49. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  50. including FreeRTOS+Trace - an indispensable productivity tool.
  51. Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
  52. the code with commercial support, indemnification, and middleware, under
  53. the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
  54. provide a safety engineered and independently SIL3 certified version under
  55. the SafeRTOS brand: http://www.SafeRTOS.com.
  56. */
  57. /* Standard includes. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  62. all the API functions to use the MPU wrappers. That should only be done when
  63. task.h is included from an application file. */
  64. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  65. /* FreeRTOS includes. */
  66. #include "FreeRTOS.h"
  67. #include "task.h"
  68. #include "timers.h"
  69. #include "StackMacros.h"
  70. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  71. /*
  72. * Defines the size, in words, of the stack allocated to the idle task.
  73. */
  74. #define tskIDLE_STACK_SIZE configMINIMAL_STACK_SIZE
  75. /*
  76. * Task control block. A task control block (TCB) is allocated for each task,
  77. * and stores task state information, including a pointer to the task's context
  78. * (the task's run time environment, including register values)
  79. */
  80. typedef struct tskTaskControlBlock
  81. {
  82. volatile portSTACK_TYPE *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
  83. #if ( portUSING_MPU_WRAPPERS == 1 )
  84. xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
  85. #endif
  86. xListItem xGenericListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
  87. xListItem xEventListItem; /*< Used to reference a task from an event list. */
  88. unsigned portBASE_TYPE uxPriority; /*< The priority of the task. 0 is the lowest priority. */
  89. portSTACK_TYPE *pxStack; /*< Points to the start of the stack. */
  90. signed char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */
  91. #if ( portSTACK_GROWTH > 0 )
  92. portSTACK_TYPE *pxEndOfStack; /*< Points to the end of the stack on architectures where the stack grows up from low memory. */
  93. #endif
  94. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  95. unsigned portBASE_TYPE uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
  96. #endif
  97. #if ( configUSE_TRACE_FACILITY == 1 )
  98. unsigned portBASE_TYPE uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
  99. unsigned portBASE_TYPE uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
  100. #endif
  101. #if ( configUSE_MUTEXES == 1 )
  102. unsigned portBASE_TYPE uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
  103. #endif
  104. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  105. pdTASK_HOOK_CODE pxTaskTag;
  106. #endif
  107. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  108. unsigned long ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
  109. #endif
  110. } tskTCB;
  111. /*
  112. * Some kernel aware debuggers require the data the debugger needs access to to
  113. * be global, rather than file scope.
  114. */
  115. #ifdef portREMOVE_STATIC_QUALIFIER
  116. #define static
  117. #endif
  118. /*lint -e956 */
  119. PRIVILEGED_DATA tskTCB * volatile pxCurrentTCB = NULL;
  120. /* Lists for ready and blocked tasks. --------------------*/
  121. PRIVILEGED_DATA static xList pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
  122. PRIVILEGED_DATA static xList xDelayedTaskList1; /*< Delayed tasks. */
  123. PRIVILEGED_DATA static xList xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
  124. PRIVILEGED_DATA static xList * volatile pxDelayedTaskList ; /*< Points to the delayed task list currently being used. */
  125. PRIVILEGED_DATA static xList * volatile pxOverflowDelayedTaskList; /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
  126. PRIVILEGED_DATA static xList xPendingReadyList; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready queue when the scheduler is resumed. */
  127. #if ( INCLUDE_vTaskDelete == 1 )
  128. PRIVILEGED_DATA static xList xTasksWaitingTermination; /*< Tasks that have been deleted - but the their memory not yet freed. */
  129. PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0U;
  130. #endif
  131. #if ( INCLUDE_vTaskSuspend == 1 )
  132. PRIVILEGED_DATA static xList xSuspendedTaskList; /*< Tasks that are currently suspended. */
  133. #endif
  134. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  135. PRIVILEGED_DATA static xTaskHandle xIdleTaskHandle = NULL; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
  136. #endif
  137. /* File private variables. --------------------------------*/
  138. PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks = ( unsigned portBASE_TYPE ) 0U;
  139. PRIVILEGED_DATA static volatile portTickType xTickCount = ( portTickType ) 0U;
  140. PRIVILEGED_DATA static unsigned portBASE_TYPE uxTopUsedPriority = tskIDLE_PRIORITY;
  141. PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTopReadyPriority = tskIDLE_PRIORITY;
  142. PRIVILEGED_DATA static volatile signed portBASE_TYPE xSchedulerRunning = pdFALSE;
  143. PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxSchedulerSuspended = ( unsigned portBASE_TYPE ) pdFALSE;
  144. PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxMissedTicks = ( unsigned portBASE_TYPE ) 0U;
  145. PRIVILEGED_DATA static volatile portBASE_TYPE xMissedYield = ( portBASE_TYPE ) pdFALSE;
  146. PRIVILEGED_DATA static volatile portBASE_TYPE xNumOfOverflows = ( portBASE_TYPE ) 0;
  147. PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber = ( unsigned portBASE_TYPE ) 0U;
  148. PRIVILEGED_DATA static volatile portTickType xNextTaskUnblockTime = ( portTickType ) portMAX_DELAY;
  149. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  150. PRIVILEGED_DATA static char pcStatsString[ 50 ] ;
  151. PRIVILEGED_DATA static unsigned long ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
  152. static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime ) PRIVILEGED_FUNCTION;
  153. #endif
  154. /* Debugging and trace facilities private variables and macros. ------------*/
  155. /*
  156. * The value used to fill the stack of a task when the task is created. This
  157. * is used purely for checking the high water mark for tasks.
  158. */
  159. #define tskSTACK_FILL_BYTE ( 0xa5U )
  160. /*
  161. * Macros used by vListTask to indicate which state a task is in.
  162. */
  163. #define tskBLOCKED_CHAR ( ( signed char ) 'B' )
  164. #define tskREADY_CHAR ( ( signed char ) 'R' )
  165. #define tskDELETED_CHAR ( ( signed char ) 'D' )
  166. #define tskSUSPENDED_CHAR ( ( signed char ) 'S' )
  167. /*-----------------------------------------------------------*/
  168. #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 0
  169. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
  170. performed in a generic way that is not optimised to any particular
  171. microcontroller architecture. */
  172. /* uxTopReadyPriority holds the priority of the highest priority ready
  173. state task. */
  174. #define taskRECORD_READY_PRIORITY( uxPriority ) \
  175. { \
  176. if( ( uxPriority ) > uxTopReadyPriority ) \
  177. { \
  178. uxTopReadyPriority = ( uxPriority ); \
  179. } \
  180. } /* taskRECORD_READY_PRIORITY */
  181. /*-----------------------------------------------------------*/
  182. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  183. { \
  184. /* Find the highest priority queue that contains ready tasks. */ \
  185. while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) ) \
  186. { \
  187. configASSERT( uxTopReadyPriority ); \
  188. --uxTopReadyPriority; \
  189. } \
  190. \
  191. /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
  192. the same priority get an equal share of the processor time. */ \
  193. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) ); \
  194. } /* taskSELECT_HIGHEST_PRIORITY_TASK */
  195. /*-----------------------------------------------------------*/
  196. /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
  197. they are only required when a port optimised method of task selection is
  198. being used. */
  199. #define taskRESET_READY_PRIORITY( uxPriority )
  200. #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
  201. #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  202. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
  203. performed in a way that is tailored to the particular microcontroller
  204. architecture being used. */
  205. /* A port optimised version is provided. Call the port defined macros. */
  206. #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
  207. /*-----------------------------------------------------------*/
  208. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  209. { \
  210. unsigned portBASE_TYPE uxTopPriority; \
  211. \
  212. /* Find the highest priority queue that contains ready tasks. */ \
  213. portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
  214. configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
  215. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
  216. } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
  217. /*-----------------------------------------------------------*/
  218. /* A port optimised version is provided, call it only if the TCB being reset
  219. is being referenced from a ready list. If it is referenced from a delayed
  220. or suspended list then it won't be in a ready list. */
  221. #define taskRESET_READY_PRIORITY( uxPriority ) \
  222. { \
  223. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == 0 ) \
  224. { \
  225. portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
  226. } \
  227. }
  228. #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  229. /*
  230. * Place the task represented by pxTCB into the appropriate ready queue for
  231. * the task. It is inserted at the end of the list. One quirk of this is
  232. * that if the task being inserted is at the same priority as the currently
  233. * executing task, then it will only be rescheduled after the currently
  234. * executing task has been rescheduled.
  235. */
  236. #define prvAddTaskToReadyQueue( pxTCB ) \
  237. traceMOVED_TASK_TO_READY_STATE( pxTCB ) \
  238. taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
  239. vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )
  240. /*-----------------------------------------------------------*/
  241. /*
  242. * Macro that looks at the list of tasks that are currently delayed to see if
  243. * any require waking.
  244. *
  245. * Tasks are stored in the queue in the order of their wake time - meaning
  246. * once one tasks has been found whose timer has not expired we need not look
  247. * any further down the list.
  248. */
  249. #define prvCheckDelayedTasks() \
  250. { \
  251. portTickType xItemValue; \
  252. \
  253. /* Is the tick count greater than or equal to the wake time of the first \
  254. task referenced from the delayed tasks list? */ \
  255. if( xTickCount >= xNextTaskUnblockTime ) \
  256. { \
  257. for( ;; ) \
  258. { \
  259. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE ) \
  260. { \
  261. /* The delayed list is empty. Set xNextTaskUnblockTime to the \
  262. maximum possible value so it is extremely unlikely that the \
  263. if( xTickCount >= xNextTaskUnblockTime ) test will pass next \
  264. time through. */ \
  265. xNextTaskUnblockTime = portMAX_DELAY; \
  266. break; \
  267. } \
  268. else \
  269. { \
  270. /* The delayed list is not empty, get the value of the item at \
  271. the head of the delayed list. This is the time at which the \
  272. task at the head of the delayed list should be removed from \
  273. the Blocked state. */ \
  274. pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); \
  275. xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) ); \
  276. \
  277. if( xTickCount < xItemValue ) \
  278. { \
  279. /* It is not time to unblock this item yet, but the item \
  280. value is the time at which the task at the head of the \
  281. blocked list should be removed from the Blocked state - \
  282. so record the item value in xNextTaskUnblockTime. */ \
  283. xNextTaskUnblockTime = xItemValue; \
  284. break; \
  285. } \
  286. \
  287. /* It is time to remove the item from the Blocked state. */ \
  288. uxListRemove( &( pxTCB->xGenericListItem ) ); \
  289. \
  290. /* Is the task waiting on an event also? */ \
  291. if( pxTCB->xEventListItem.pvContainer != NULL ) \
  292. { \
  293. uxListRemove( &( pxTCB->xEventListItem ) ); \
  294. } \
  295. prvAddTaskToReadyQueue( pxTCB ); \
  296. } \
  297. } \
  298. } \
  299. }
  300. /*-----------------------------------------------------------*/
  301. /*
  302. * Several functions take an xTaskHandle parameter that can optionally be NULL,
  303. * where NULL is used to indicate that the handle of the currently executing
  304. * task should be used in place of the parameter. This macro simply checks to
  305. * see if the parameter is NULL and returns a pointer to the appropriate TCB.
  306. */
  307. #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) ( pxHandle ) )
  308. /* Callback function prototypes. --------------------------*/
  309. extern void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName );
  310. extern void vApplicationTickHook( void );
  311. /* File private functions. --------------------------------*/
  312. /*
  313. * Utility to ready a TCB for a given task. Mainly just copies the parameters
  314. * into the TCB structure.
  315. */
  316. static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth ) PRIVILEGED_FUNCTION;
  317. /*
  318. * Utility to ready all the lists used by the scheduler. This is called
  319. * automatically upon the creation of the first task.
  320. */
  321. static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
  322. /*
  323. * The idle task, which as all tasks is implemented as a never ending loop.
  324. * The idle task is automatically created and added to the ready lists upon
  325. * creation of the first user task.
  326. *
  327. * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
  328. * language extensions. The equivalent prototype for this function is:
  329. *
  330. * void prvIdleTask( void *pvParameters );
  331. *
  332. */
  333. static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
  334. /*
  335. * Utility to free all memory allocated by the scheduler to hold a TCB,
  336. * including the stack pointed to by the TCB.
  337. *
  338. * This does not free memory allocated by the task itself (i.e. memory
  339. * allocated by calls to pvPortMalloc from within the tasks application code).
  340. */
  341. #if ( INCLUDE_vTaskDelete == 1 )
  342. static void prvDeleteTCB( tskTCB *pxTCB ) PRIVILEGED_FUNCTION;
  343. #endif
  344. /*
  345. * Used only by the idle task. This checks to see if anything has been placed
  346. * in the list of tasks waiting to be deleted. If so the task is cleaned up
  347. * and its TCB deleted.
  348. */
  349. static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
  350. /*
  351. * The currently executing task is entering the Blocked state. Add the task to
  352. * either the current or the overflow delayed task list.
  353. */
  354. static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake ) PRIVILEGED_FUNCTION;
  355. /*
  356. * Allocates memory from the heap for a TCB and associated stack. Checks the
  357. * allocation was successful.
  358. */
  359. static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer ) PRIVILEGED_FUNCTION;
  360. /*
  361. * Called from vTaskList. vListTasks details all the tasks currently under
  362. * control of the scheduler. The tasks may be in one of a number of lists.
  363. * prvListTaskWithinSingleList accepts a list and details the tasks from
  364. * within just that list.
  365. *
  366. * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
  367. * NORMAL APPLICATION CODE.
  368. */
  369. #if ( configUSE_TRACE_FACILITY == 1 )
  370. static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus ) PRIVILEGED_FUNCTION;
  371. #endif
  372. /*
  373. * When a task is created, the stack of the task is filled with a known value.
  374. * This function determines the 'high water mark' of the task stack by
  375. * determining how much of the stack remains at the original preset value.
  376. */
  377. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
  378. static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte ) PRIVILEGED_FUNCTION;
  379. #endif
  380. /*
  381. * Return the amount of time, in ticks, that will pass before the kernel will
  382. * next move a task from the Blocked state to the Running state.
  383. *
  384. * This conditional compilation should use inequality to 0, not equality to 1.
  385. * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
  386. * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
  387. * set to a value other than 1.
  388. */
  389. #if ( configUSE_TICKLESS_IDLE != 0 )
  390. static portTickType prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
  391. #endif
  392. /*lint +e956 */
  393. /*-----------------------------------------------------------
  394. * TASK CREATION API documented in task.h
  395. *----------------------------------------------------------*/
  396. signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed char * const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions )
  397. {
  398. signed portBASE_TYPE xReturn;
  399. tskTCB * pxNewTCB;
  400. configASSERT( pxTaskCode );
  401. configASSERT( ( ( uxPriority & ( ~portPRIVILEGE_BIT ) ) < configMAX_PRIORITIES ) );
  402. /* Allocate the memory required by the TCB and stack for the new task,
  403. checking that the allocation was successful. */
  404. pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );
  405. if( pxNewTCB != NULL )
  406. {
  407. portSTACK_TYPE *pxTopOfStack;
  408. #if( portUSING_MPU_WRAPPERS == 1 )
  409. /* Should the task be created in privileged mode? */
  410. portBASE_TYPE xRunPrivileged;
  411. if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
  412. {
  413. xRunPrivileged = pdTRUE;
  414. }
  415. else
  416. {
  417. xRunPrivileged = pdFALSE;
  418. }
  419. uxPriority &= ~portPRIVILEGE_BIT;
  420. #endif /* portUSING_MPU_WRAPPERS == 1 */
  421. /* Calculate the top of stack address. This depends on whether the
  422. stack grows from high memory to low (as per the 80x86) or visa versa.
  423. portSTACK_GROWTH is used to make the result positive or negative as
  424. required by the port. */
  425. #if( portSTACK_GROWTH < 0 )
  426. {
  427. pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 );
  428. pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );
  429. /* Check the alignment of the calculated top of stack is correct. */
  430. configASSERT( ( ( ( unsigned long ) pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
  431. }
  432. #else
  433. {
  434. pxTopOfStack = pxNewTCB->pxStack;
  435. /* Check the alignment of the stack buffer is correct. */
  436. configASSERT( ( ( ( unsigned long ) pxNewTCB->pxStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
  437. /* If we want to use stack checking on architectures that use
  438. a positive stack growth direction then we also need to store the
  439. other extreme of the stack space. */
  440. pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );
  441. }
  442. #endif
  443. /* Setup the newly allocated TCB with the initial state of the task. */
  444. prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );
  445. /* Initialize the TCB stack to look as if the task was already running,
  446. but had been interrupted by the scheduler. The return address is set
  447. to the start of the task function. Once the stack has been initialised
  448. the top of stack variable is updated. */
  449. #if( portUSING_MPU_WRAPPERS == 1 )
  450. {
  451. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
  452. }
  453. #else
  454. {
  455. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
  456. }
  457. #endif
  458. /* Check the alignment of the initialised stack. */
  459. portALIGNMENT_ASSERT_pxCurrentTCB( ( ( ( unsigned long ) pxNewTCB->pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
  460. if( ( void * ) pxCreatedTask != NULL )
  461. {
  462. /* Pass the TCB out - in an anonymous way. The calling function/
  463. task can use this as a handle to delete the task later if
  464. required.*/
  465. *pxCreatedTask = ( xTaskHandle ) pxNewTCB;
  466. }
  467. /* We are going to manipulate the task queues to add this task to a
  468. ready list, so must make sure no interrupts occur. */
  469. taskENTER_CRITICAL();
  470. {
  471. uxCurrentNumberOfTasks++;
  472. if( pxCurrentTCB == NULL )
  473. {
  474. /* There are no other tasks, or all the other tasks are in
  475. the suspended state - make this the current task. */
  476. pxCurrentTCB = pxNewTCB;
  477. if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )
  478. {
  479. /* This is the first task to be created so do the preliminary
  480. initialisation required. We will not recover if this call
  481. fails, but we will report the failure. */
  482. prvInitialiseTaskLists();
  483. }
  484. }
  485. else
  486. {
  487. /* If the scheduler is not already running, make this task the
  488. current task if it is the highest priority task to be created
  489. so far. */
  490. if( xSchedulerRunning == pdFALSE )
  491. {
  492. if( pxCurrentTCB->uxPriority <= uxPriority )
  493. {
  494. pxCurrentTCB = pxNewTCB;
  495. }
  496. }
  497. }
  498. /* Remember the top priority to make context switching faster. Use
  499. the priority in pxNewTCB as this has been capped to a valid value. */
  500. if( pxNewTCB->uxPriority > uxTopUsedPriority )
  501. {
  502. uxTopUsedPriority = pxNewTCB->uxPriority;
  503. }
  504. #if ( configUSE_TRACE_FACILITY == 1 )
  505. {
  506. /* Add a counter into the TCB for tracing only. */
  507. pxNewTCB->uxTCBNumber = uxTaskNumber;
  508. }
  509. #endif
  510. uxTaskNumber++;
  511. prvAddTaskToReadyQueue( pxNewTCB );
  512. xReturn = pdPASS;
  513. portSETUP_TCB( pxNewTCB );
  514. traceTASK_CREATE( pxNewTCB );
  515. }
  516. taskEXIT_CRITICAL();
  517. }
  518. else
  519. {
  520. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  521. traceTASK_CREATE_FAILED();
  522. }
  523. if( xReturn == pdPASS )
  524. {
  525. if( xSchedulerRunning != pdFALSE )
  526. {
  527. /* If the created task is of a higher priority than the current task
  528. then it should run now. */
  529. if( pxCurrentTCB->uxPriority < uxPriority )
  530. {
  531. portYIELD_WITHIN_API();
  532. }
  533. }
  534. }
  535. return xReturn;
  536. }
  537. /*-----------------------------------------------------------*/
  538. #if ( INCLUDE_vTaskDelete == 1 )
  539. void vTaskDelete( xTaskHandle pxTaskToDelete )
  540. {
  541. tskTCB *pxTCB;
  542. taskENTER_CRITICAL();
  543. {
  544. /* Ensure a yield is performed if the current task is being
  545. deleted. */
  546. if( pxTaskToDelete == pxCurrentTCB )
  547. {
  548. pxTaskToDelete = NULL;
  549. }
  550. /* If null is passed in here then we are deleting ourselves. */
  551. pxTCB = prvGetTCBFromHandle( pxTaskToDelete );
  552. /* Remove task from the ready list and place in the termination list.
  553. This will stop the task from be scheduled. The idle task will check
  554. the termination list and free up any memory allocated by the
  555. scheduler for the TCB and stack. */
  556. if( uxListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) ) == 0 )
  557. {
  558. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  559. }
  560. /* Is the task waiting on an event also? */
  561. if( pxTCB->xEventListItem.pvContainer != NULL )
  562. {
  563. uxListRemove( &( pxTCB->xEventListItem ) );
  564. }
  565. vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
  566. /* Increment the ucTasksDeleted variable so the idle task knows
  567. there is a task that has been deleted and that it should therefore
  568. check the xTasksWaitingTermination list. */
  569. ++uxTasksDeleted;
  570. /* Increment the uxTaskNumberVariable also so kernel aware debuggers
  571. can detect that the task lists need re-generating. */
  572. uxTaskNumber++;
  573. traceTASK_DELETE( pxTCB );
  574. }
  575. taskEXIT_CRITICAL();
  576. /* Force a reschedule if we have just deleted the current task. */
  577. if( xSchedulerRunning != pdFALSE )
  578. {
  579. if( ( void * ) pxTaskToDelete == NULL )
  580. {
  581. portYIELD_WITHIN_API();
  582. }
  583. }
  584. }
  585. #endif
  586. /*-----------------------------------------------------------
  587. * TASK CONTROL API documented in task.h
  588. *----------------------------------------------------------*/
  589. #if ( INCLUDE_vTaskDelayUntil == 1 )
  590. void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement )
  591. {
  592. portTickType xTimeToWake;
  593. portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;
  594. configASSERT( pxPreviousWakeTime );
  595. configASSERT( ( xTimeIncrement > 0U ) );
  596. vTaskSuspendAll();
  597. {
  598. /* Generate the tick time at which the task wants to wake. */
  599. xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
  600. if( xTickCount < *pxPreviousWakeTime )
  601. {
  602. /* The tick count has overflowed since this function was
  603. lasted called. In this case the only time we should ever
  604. actually delay is if the wake time has also overflowed,
  605. and the wake time is greater than the tick time. When this
  606. is the case it is as if neither time had overflowed. */
  607. if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )
  608. {
  609. xShouldDelay = pdTRUE;
  610. }
  611. }
  612. else
  613. {
  614. /* The tick time has not overflowed. In this case we will
  615. delay if either the wake time has overflowed, and/or the
  616. tick time is less than the wake time. */
  617. if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )
  618. {
  619. xShouldDelay = pdTRUE;
  620. }
  621. }
  622. /* Update the wake time ready for the next call. */
  623. *pxPreviousWakeTime = xTimeToWake;
  624. if( xShouldDelay != pdFALSE )
  625. {
  626. traceTASK_DELAY_UNTIL();
  627. /* We must remove ourselves from the ready list before adding
  628. ourselves to the blocked list as the same list item is used for
  629. both lists. */
  630. if( uxListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) ) == 0 )
  631. {
  632. /* The current task must be in a ready list, so there is
  633. no need to check, and the port reset macro can be called
  634. directly. */
  635. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  636. }
  637. prvAddCurrentTaskToDelayedList( xTimeToWake );
  638. }
  639. }
  640. xAlreadyYielded = xTaskResumeAll();
  641. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  642. have put ourselves to sleep. */
  643. if( xAlreadyYielded == pdFALSE )
  644. {
  645. portYIELD_WITHIN_API();
  646. }
  647. }
  648. #endif
  649. /*-----------------------------------------------------------*/
  650. #if ( INCLUDE_vTaskDelay == 1 )
  651. void vTaskDelay( portTickType xTicksToDelay )
  652. {
  653. portTickType xTimeToWake;
  654. signed portBASE_TYPE xAlreadyYielded = pdFALSE;
  655. /* A delay time of zero just forces a reschedule. */
  656. if( xTicksToDelay > ( portTickType ) 0U )
  657. {
  658. vTaskSuspendAll();
  659. {
  660. traceTASK_DELAY();
  661. /* A task that is removed from the event list while the
  662. scheduler is suspended will not get placed in the ready
  663. list or removed from the blocked list until the scheduler
  664. is resumed.
  665. This task cannot be in an event list as it is the currently
  666. executing task. */
  667. /* Calculate the time to wake - this may overflow but this is
  668. not a problem. */
  669. xTimeToWake = xTickCount + xTicksToDelay;
  670. /* We must remove ourselves from the ready list before adding
  671. ourselves to the blocked list as the same list item is used for
  672. both lists. */
  673. if( uxListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) ) == 0 )
  674. {
  675. /* The current task must be in a ready list, so there is
  676. no need to check, and the port reset macro can be called
  677. directly. */
  678. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  679. }
  680. prvAddCurrentTaskToDelayedList( xTimeToWake );
  681. }
  682. xAlreadyYielded = xTaskResumeAll();
  683. }
  684. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  685. have put ourselves to sleep. */
  686. if( xAlreadyYielded == pdFALSE )
  687. {
  688. portYIELD_WITHIN_API();
  689. }
  690. }
  691. #endif
  692. /*-----------------------------------------------------------*/
  693. #if ( INCLUDE_eTaskStateGet == 1 )
  694. eTaskState eTaskStateGet( xTaskHandle pxTask )
  695. {
  696. eTaskState eReturn;
  697. xList *pxStateList;
  698. tskTCB *pxTCB;
  699. pxTCB = ( tskTCB * ) pxTask;
  700. if( pxTCB == pxCurrentTCB )
  701. {
  702. /* The task calling this function is querying its own state. */
  703. eReturn = eRunning;
  704. }
  705. else
  706. {
  707. taskENTER_CRITICAL();
  708. {
  709. pxStateList = ( xList * ) listLIST_ITEM_CONTAINER( &( pxTCB->xGenericListItem ) );
  710. }
  711. taskEXIT_CRITICAL();
  712. if( ( pxStateList == pxDelayedTaskList ) || ( pxStateList == pxOverflowDelayedTaskList ) )
  713. {
  714. /* The task being queried is referenced from one of the Blocked
  715. lists. */
  716. eReturn = eBlocked;
  717. }
  718. #if ( INCLUDE_vTaskSuspend == 1 )
  719. else if( pxStateList == &xSuspendedTaskList )
  720. {
  721. /* The task being queried is referenced from the suspended
  722. list. */
  723. eReturn = eSuspended;
  724. }
  725. #endif
  726. #if ( INCLUDE_vTaskDelete == 1 )
  727. else if( pxStateList == &xTasksWaitingTermination )
  728. {
  729. /* The task being queried is referenced from the deleted
  730. tasks list. */
  731. eReturn = eDeleted;
  732. }
  733. #endif
  734. else
  735. {
  736. /* If the task is not in any other state, it must be in the
  737. Ready (including pending ready) state. */
  738. eReturn = eReady;
  739. }
  740. }
  741. return eReturn;
  742. }
  743. #endif
  744. /*-----------------------------------------------------------*/
  745. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  746. unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )
  747. {
  748. tskTCB *pxTCB;
  749. unsigned portBASE_TYPE uxReturn;
  750. taskENTER_CRITICAL();
  751. {
  752. /* If null is passed in here then we are changing the
  753. priority of the calling function. */
  754. pxTCB = prvGetTCBFromHandle( pxTask );
  755. uxReturn = pxTCB->uxPriority;
  756. }
  757. taskEXIT_CRITICAL();
  758. return uxReturn;
  759. }
  760. #endif
  761. /*-----------------------------------------------------------*/
  762. #if ( INCLUDE_vTaskPrioritySet == 1 )
  763. void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )
  764. {
  765. tskTCB *pxTCB;
  766. unsigned portBASE_TYPE uxCurrentPriority, uxPriorityUsedOnEntry;
  767. portBASE_TYPE xYieldRequired = pdFALSE;
  768. configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
  769. /* Ensure the new priority is valid. */
  770. if( uxNewPriority >= configMAX_PRIORITIES )
  771. {
  772. uxNewPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;
  773. }
  774. taskENTER_CRITICAL();
  775. {
  776. if( pxTask == pxCurrentTCB )
  777. {
  778. pxTask = NULL;
  779. }
  780. /* If null is passed in here then we are changing the
  781. priority of the calling function. */
  782. pxTCB = prvGetTCBFromHandle( pxTask );
  783. traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
  784. #if ( configUSE_MUTEXES == 1 )
  785. {
  786. uxCurrentPriority = pxTCB->uxBasePriority;
  787. }
  788. #else
  789. {
  790. uxCurrentPriority = pxTCB->uxPriority;
  791. }
  792. #endif
  793. if( uxCurrentPriority != uxNewPriority )
  794. {
  795. /* The priority change may have readied a task of higher
  796. priority than the calling task. */
  797. if( uxNewPriority > uxCurrentPriority )
  798. {
  799. if( pxTask != NULL )
  800. {
  801. /* The priority of another task is being raised. If we
  802. were raising the priority of the currently running task
  803. there would be no need to switch as it must have already
  804. been the highest priority task. */
  805. xYieldRequired = pdTRUE;
  806. }
  807. }
  808. else if( pxTask == NULL )
  809. {
  810. /* Setting our own priority down means there may now be another
  811. task of higher priority that is ready to execute. */
  812. xYieldRequired = pdTRUE;
  813. }
  814. /* Remember the ready list the task might be referenced from
  815. before its uxPriority member is changed so the
  816. taskRESET_READY_PRIORITY() macro can function correctly. */
  817. uxPriorityUsedOnEntry = pxTCB->uxPriority;
  818. #if ( configUSE_MUTEXES == 1 )
  819. {
  820. /* Only change the priority being used if the task is not
  821. currently using an inherited priority. */
  822. if( pxTCB->uxBasePriority == pxTCB->uxPriority )
  823. {
  824. pxTCB->uxPriority = uxNewPriority;
  825. }
  826. /* The base priority gets set whatever. */
  827. pxTCB->uxBasePriority = uxNewPriority;
  828. }
  829. #else
  830. {
  831. pxTCB->uxPriority = uxNewPriority;
  832. }
  833. #endif
  834. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );
  835. /* If the task is in the blocked or suspended list we need do
  836. nothing more than change it's priority variable. However, if
  837. the task is in a ready list it needs to be removed and placed
  838. in the queue appropriate to its new priority. */
  839. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )
  840. {
  841. /* The task is currently in its ready list - remove before adding
  842. it to it's new ready list. As we are in a critical section we
  843. can do this even if the scheduler is suspended. */
  844. if( uxListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) ) == 0 )
  845. {
  846. taskRESET_READY_PRIORITY( uxPriorityUsedOnEntry );
  847. }
  848. prvAddTaskToReadyQueue( pxTCB );
  849. }
  850. if( xYieldRequired == pdTRUE )
  851. {
  852. portYIELD_WITHIN_API();
  853. }
  854. }
  855. }
  856. taskEXIT_CRITICAL();
  857. /* Remove compiler warning about unused parameter when the port
  858. optimised task selection is not being used. */
  859. ( void ) uxPriorityUsedOnEntry;
  860. }
  861. #endif
  862. /*-----------------------------------------------------------*/
  863. #if ( INCLUDE_vTaskSuspend == 1 )
  864. void vTaskSuspend( xTaskHandle pxTaskToSuspend )
  865. {
  866. tskTCB *pxTCB;
  867. taskENTER_CRITICAL();
  868. {
  869. /* Ensure a yield is performed if the current task is being
  870. suspended. */
  871. if( pxTaskToSuspend == pxCurrentTCB )
  872. {
  873. pxTaskToSuspend = NULL;
  874. }
  875. /* If null is passed in here then we are suspending ourselves. */
  876. pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
  877. traceTASK_SUSPEND( pxTCB );
  878. /* Remove task from the ready/delayed list and place in the suspended list. */
  879. if( uxListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) ) == 0 )
  880. {
  881. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  882. }
  883. /* Is the task waiting on an event also? */
  884. if( pxTCB->xEventListItem.pvContainer != NULL )
  885. {
  886. uxListRemove( &( pxTCB->xEventListItem ) );
  887. }
  888. vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
  889. }
  890. taskEXIT_CRITICAL();
  891. if( ( void * ) pxTaskToSuspend == NULL )
  892. {
  893. if( xSchedulerRunning != pdFALSE )
  894. {
  895. /* We have just suspended the current task. */
  896. portYIELD_WITHIN_API();
  897. }
  898. else
  899. {
  900. /* The scheduler is not running, but the task that was pointed
  901. to by pxCurrentTCB has just been suspended and pxCurrentTCB
  902. must be adjusted to point to a different task. */
  903. if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks )
  904. {
  905. /* No other tasks are ready, so set pxCurrentTCB back to
  906. NULL so when the next task is created pxCurrentTCB will
  907. be set to point to it no matter what its relative priority
  908. is. */
  909. pxCurrentTCB = NULL;
  910. }
  911. else
  912. {
  913. vTaskSwitchContext();
  914. }
  915. }
  916. }
  917. }
  918. #endif
  919. /*-----------------------------------------------------------*/
  920. #if ( INCLUDE_vTaskSuspend == 1 )
  921. signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask )
  922. {
  923. portBASE_TYPE xReturn = pdFALSE;
  924. const tskTCB * const pxTCB = ( tskTCB * ) xTask;
  925. /* It does not make sense to check if the calling task is suspended. */
  926. configASSERT( xTask );
  927. /* Is the task we are attempting to resume actually in the
  928. suspended list? */
  929. if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
  930. {
  931. /* Has the task already been resumed from within an ISR? */
  932. if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
  933. {
  934. /* Is it in the suspended list because it is in the
  935. Suspended state? It is possible to be in the suspended
  936. list because it is blocked on a task with no timeout
  937. specified. */
  938. if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )
  939. {
  940. xReturn = pdTRUE;
  941. }
  942. }
  943. }
  944. return xReturn;
  945. }
  946. #endif
  947. /*-----------------------------------------------------------*/
  948. #if ( INCLUDE_vTaskSuspend == 1 )
  949. void vTaskResume( xTaskHandle pxTaskToResume )
  950. {
  951. tskTCB *pxTCB;
  952. /* It does not make sense to resume the calling task. */
  953. configASSERT( pxTaskToResume );
  954. /* Remove the task from whichever list it is currently in, and place
  955. it in the ready list. */
  956. pxTCB = ( tskTCB * ) pxTaskToResume;
  957. /* The parameter cannot be NULL as it is impossible to resume the
  958. currently executing task. */
  959. if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )
  960. {
  961. taskENTER_CRITICAL();
  962. {
  963. if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
  964. {
  965. traceTASK_RESUME( pxTCB );
  966. /* As we are in a critical section we can access the ready
  967. lists even if the scheduler is suspended. */
  968. uxListRemove( &( pxTCB->xGenericListItem ) );
  969. prvAddTaskToReadyQueue( pxTCB );
  970. /* We may have just resumed a higher priority task. */
  971. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  972. {
  973. /* This yield may not cause the task just resumed to run, but
  974. will leave the lists in the correct state for the next yield. */
  975. portYIELD_WITHIN_API();
  976. }
  977. }
  978. }
  979. taskEXIT_CRITICAL();
  980. }
  981. }
  982. #endif
  983. /*-----------------------------------------------------------*/
  984. #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
  985. portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )
  986. {
  987. portBASE_TYPE xYieldRequired = pdFALSE;
  988. tskTCB *pxTCB;
  989. unsigned portBASE_TYPE uxSavedInterruptStatus;
  990. configASSERT( pxTaskToResume );
  991. pxTCB = ( tskTCB * ) pxTaskToResume;
  992. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  993. {
  994. if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
  995. {
  996. traceTASK_RESUME_FROM_ISR( pxTCB );
  997. if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
  998. {
  999. xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );
  1000. uxListRemove( &( pxTCB->xGenericListItem ) );
  1001. prvAddTaskToReadyQueue( pxTCB );
  1002. }
  1003. else
  1004. {
  1005. /* We cannot access the delayed or ready lists, so will hold this
  1006. task pending until the scheduler is resumed, at which point a
  1007. yield will be performed if necessary. */
  1008. vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  1009. }
  1010. }
  1011. }
  1012. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1013. return xYieldRequired;
  1014. }
  1015. #endif
  1016. /*-----------------------------------------------------------
  1017. * PUBLIC SCHEDULER CONTROL documented in task.h
  1018. *----------------------------------------------------------*/
  1019. void vTaskStartScheduler( void )
  1020. {
  1021. portBASE_TYPE xReturn;
  1022. /* Add the idle task at the lowest priority. */
  1023. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  1024. {
  1025. /* Create the idle task, storing its handle in xIdleTaskHandle so it can
  1026. be returned by the xTaskGetIdleTaskHandle() function. */
  1027. xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle );
  1028. }
  1029. #else
  1030. {
  1031. /* Create the idle task without storing its handle. */
  1032. xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL );
  1033. }
  1034. #endif
  1035. #if ( configUSE_TIMERS == 1 )
  1036. {
  1037. if( xReturn == pdPASS )
  1038. {
  1039. xReturn = xTimerCreateTimerTask();
  1040. }
  1041. }
  1042. #endif
  1043. if( xReturn == pdPASS )
  1044. {
  1045. /* Interrupts are turned off here, to ensure a tick does not occur
  1046. before or during the call to xPortStartScheduler(). The stacks of
  1047. the created tasks contain a status word with interrupts switched on
  1048. so interrupts will automatically get re-enabled when the first task
  1049. starts to run.
  1050. STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE
  1051. DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */
  1052. portDISABLE_INTERRUPTS();
  1053. xSchedulerRunning = pdTRUE;
  1054. xTickCount = ( portTickType ) 0U;
  1055. /* If configGENERATE_RUN_TIME_STATS is defined then the following
  1056. macro must be defined to configure the timer/counter used to generate
  1057. the run time counter time base. */
  1058. portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
  1059. /* Setting up the timer tick is hardware specific and thus in the
  1060. portable interface. */
  1061. if( xPortStartScheduler() != pdFALSE )
  1062. {
  1063. /* Should not reach here as if the scheduler is running the
  1064. function will not return. */
  1065. }
  1066. else
  1067. {
  1068. /* Should only reach here if a task calls xTaskEndScheduler(). */
  1069. }
  1070. }
  1071. /* This line will only be reached if the kernel could not be started. */
  1072. configASSERT( xReturn );
  1073. }
  1074. /*-----------------------------------------------------------*/
  1075. void vTaskEndScheduler( void )
  1076. {
  1077. /* Stop the scheduler interrupts and call the portable scheduler end
  1078. routine so the original ISRs can be restored if necessary. The port
  1079. layer must ensure interrupts enable bit is left in the correct state. */
  1080. portDISABLE_INTERRUPTS();
  1081. xSchedulerRunning = pdFALSE;
  1082. vPortEndScheduler();
  1083. }
  1084. /*----------------------------------------------------------*/
  1085. void vTaskSuspendAll( void )
  1086. {
  1087. /* A critical section is not required as the variable is of type
  1088. portBASE_TYPE. */
  1089. ++uxSchedulerSuspended;
  1090. }
  1091. /*----------------------------------------------------------*/
  1092. #if ( configUSE_TICKLESS_IDLE != 0 )
  1093. portTickType prvGetExpectedIdleTime( void )
  1094. {
  1095. portTickType xReturn;
  1096. if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
  1097. {
  1098. xReturn = 0;
  1099. }
  1100. else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
  1101. {
  1102. /* There are other idle priority tasks in the ready state. If
  1103. time slicing is used then the very next tick interrupt must be
  1104. processed. */
  1105. xReturn = 0;
  1106. }
  1107. else
  1108. {
  1109. xReturn = xNextTaskUnblockTime - xTickCount;
  1110. }
  1111. return xReturn;
  1112. }
  1113. #endif /* configUSE_TICKLESS_IDLE != 0 */
  1114. /*----------------------------------------------------------*/
  1115. signed portBASE_TYPE xTaskResumeAll( void )
  1116. {
  1117. register tskTCB *pxTCB;
  1118. signed portBASE_TYPE xAlreadyYielded = pdFALSE;
  1119. /* If uxSchedulerSuspended is zero then this function does not match a
  1120. previous call to vTaskSuspendAll(). */
  1121. configASSERT( uxSchedulerSuspended );
  1122. /* It is possible that an ISR caused a task to be removed from an event
  1123. list while the scheduler was suspended. If this was the case then the
  1124. removed task will have been added to the xPendingReadyList. Once the
  1125. scheduler has been resumed it is safe to move all the pending ready
  1126. tasks from this list into their appropriate ready list. */
  1127. taskENTER_CRITICAL();
  1128. {
  1129. --uxSchedulerSuspended;
  1130. if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
  1131. {
  1132. if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0U )
  1133. {
  1134. portBASE_TYPE xYieldRequired = pdFALSE;
  1135. /* Move any readied tasks from the pending list into the
  1136. appropriate ready list. */
  1137. while( listLIST_IS_EMPTY( ( xList * ) &xPendingReadyList ) == pdFALSE )
  1138. {
  1139. pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xPendingReadyList ) );
  1140. uxListRemove( &( pxTCB->xEventListItem ) );
  1141. uxListRemove( &( pxTCB->xGenericListItem ) );
  1142. prvAddTaskToReadyQueue( pxTCB );
  1143. /* If we have moved a task that has a priority higher than
  1144. the current task then we should yield. */
  1145. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1146. {
  1147. xYieldRequired = pdTRUE;
  1148. }
  1149. }
  1150. /* If any ticks occurred while the scheduler was suspended then
  1151. they should be processed now. This ensures the tick count does not
  1152. slip, and that any delayed tasks are resumed at the correct time. */
  1153. if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )
  1154. {
  1155. while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )
  1156. {
  1157. vTaskIncrementTick();
  1158. --uxMissedTicks;
  1159. }
  1160. /* As we have processed some ticks it is appropriate to yield
  1161. to ensure the highest priority task that is ready to run is
  1162. the task actually running. */
  1163. #if configUSE_PREEMPTION == 1
  1164. {
  1165. xYieldRequired = pdTRUE;
  1166. }
  1167. #endif
  1168. }
  1169. if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )
  1170. {
  1171. xAlreadyYielded = pdTRUE;
  1172. xMissedYield = pdFALSE;
  1173. portYIELD_WITHIN_API();
  1174. }
  1175. }
  1176. }
  1177. }
  1178. taskEXIT_CRITICAL();
  1179. return xAlreadyYielded;
  1180. }
  1181. /*-----------------------------------------------------------
  1182. * PUBLIC TASK UTILITIES documented in task.h
  1183. *----------------------------------------------------------*/
  1184. portTickType xTaskGetTickCount( void )
  1185. {
  1186. portTickType xTicks;
  1187. /* Critical section required if running on a 16 bit processor. */
  1188. taskENTER_CRITICAL();
  1189. {
  1190. xTicks = xTickCount;
  1191. }
  1192. taskEXIT_CRITICAL();
  1193. return xTicks;
  1194. }
  1195. /*-----------------------------------------------------------*/
  1196. portTickType xTaskGetTickCountFromISR( void )
  1197. {
  1198. portTickType xReturn;
  1199. unsigned portBASE_TYPE uxSavedInterruptStatus;
  1200. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1201. xReturn = xTickCount;
  1202. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1203. return xReturn;
  1204. }
  1205. /*-----------------------------------------------------------*/
  1206. unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
  1207. {
  1208. /* A critical section is not required because the variables are of type
  1209. portBASE_TYPE. */
  1210. return uxCurrentNumberOfTasks;
  1211. }
  1212. /*-----------------------------------------------------------*/
  1213. #if ( INCLUDE_pcTaskGetTaskName == 1 )
  1214. signed char *pcTaskGetTaskName( xTaskHandle xTaskToQuery )
  1215. {
  1216. tskTCB *pxTCB;
  1217. /* If null is passed in here then the name of the calling task is being queried. */
  1218. pxTCB = prvGetTCBFromHandle( xTaskToQuery );
  1219. configASSERT( pxTCB );
  1220. return &( pxTCB->pcTaskName[ 0 ] );
  1221. }
  1222. #endif
  1223. /*-----------------------------------------------------------*/
  1224. #if ( configUSE_TRACE_FACILITY == 1 )
  1225. void vTaskList( signed char *pcWriteBuffer )
  1226. {
  1227. unsigned portBASE_TYPE uxQueue;
  1228. /* This is a VERY costly function that should be used for debug only.
  1229. It leaves interrupts disabled for a LONG time. */
  1230. vTaskSuspendAll();
  1231. {
  1232. /* Run through all the lists that could potentially contain a TCB and
  1233. report the task name, state and stack high water mark. */
  1234. *pcWriteBuffer = ( signed char ) 0x00;
  1235. strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
  1236. uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;
  1237. do
  1238. {
  1239. uxQueue--;
  1240. if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )
  1241. {
  1242. prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
  1243. }
  1244. }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
  1245. if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )
  1246. {
  1247. prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
  1248. }
  1249. if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )
  1250. {
  1251. prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
  1252. }
  1253. #if( INCLUDE_vTaskDelete == 1 )
  1254. {
  1255. if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
  1256. {
  1257. prvListTaskWithinSingleList( pcWriteBuffer, &xTasksWaitingTermination, tskDELETED_CHAR );
  1258. }
  1259. }
  1260. #endif
  1261. #if ( INCLUDE_vTaskSuspend == 1 )
  1262. {
  1263. if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
  1264. {
  1265. prvListTaskWithinSingleList( pcWriteBuffer, &xSuspendedTaskList, tskSUSPENDED_CHAR );
  1266. }
  1267. }
  1268. #endif
  1269. }
  1270. xTaskResumeAll();
  1271. }
  1272. #endif
  1273. /*----------------------------------------------------------*/
  1274. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  1275. void vTaskGetRunTimeStats( signed char *pcWriteBuffer )
  1276. {
  1277. unsigned portBASE_TYPE uxQueue;
  1278. unsigned long ulTotalRunTime;
  1279. /* This is a VERY costly function that should be used for debug only.
  1280. It leaves interrupts disabled for a LONG time. */
  1281. vTaskSuspendAll();
  1282. {
  1283. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  1284. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
  1285. #else
  1286. ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
  1287. #endif
  1288. /* Divide ulTotalRunTime by 100 to make the percentage caluclations
  1289. simpler in the prvGenerateRunTimeStatsForTasksInList() function. */
  1290. ulTotalRunTime /= 100UL;
  1291. /* Run through all the lists that could potentially contain a TCB,
  1292. generating a table of run timer percentages in the provided
  1293. buffer. */
  1294. *pcWriteBuffer = ( signed char ) 0x00;
  1295. strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
  1296. uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;
  1297. do
  1298. {
  1299. uxQueue--;
  1300. if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )
  1301. {
  1302. prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );
  1303. }
  1304. }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
  1305. if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )
  1306. {
  1307. prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );
  1308. }
  1309. if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )
  1310. {
  1311. prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );
  1312. }
  1313. #if ( INCLUDE_vTaskDelete == 1 )
  1314. {
  1315. if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
  1316. {
  1317. prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xTasksWaitingTermination, ulTotalRunTime );
  1318. }
  1319. }
  1320. #endif
  1321. #if ( INCLUDE_vTaskSuspend == 1 )
  1322. {
  1323. if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
  1324. {
  1325. prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xSuspendedTaskList, ulTotalRunTime );
  1326. }
  1327. }
  1328. #endif
  1329. }
  1330. xTaskResumeAll();
  1331. }
  1332. #endif
  1333. /*----------------------------------------------------------*/
  1334. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  1335. xTaskHandle xTaskGetIdleTaskHandle( void )
  1336. {
  1337. /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
  1338. started, then xIdleTaskHandle will be NULL. */
  1339. configASSERT( ( xIdleTaskHandle != NULL ) );
  1340. return xIdleTaskHandle;
  1341. }
  1342. #endif
  1343. /*----------------------------------------------------------*/
  1344. /* This conditional compilation should use inequality to 0, not equality to 1.
  1345. This is to ensure vTaskStepTick() is available when user defined low power mode
  1346. implementations require configUSE_TICKLESS_IDLE to be set to a value other than
  1347. 1. */
  1348. #if ( configUSE_TICKLESS_IDLE != 0 )
  1349. void vTaskStepTick( portTickType xTicksToJump )
  1350. {
  1351. configASSERT( xTicksToJump <= xNextTaskUnblockTime );
  1352. xTickCount += xTicksToJump;
  1353. }
  1354. #endif
  1355. /*-----------------------------------------------------------
  1356. * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
  1357. * documented in task.h
  1358. *----------------------------------------------------------*/
  1359. void vTaskIncrementTick( void )
  1360. {
  1361. tskTCB * pxTCB;
  1362. /* Called by the portable layer each time a tick interrupt occurs.
  1363. Increments the tick then checks to see if the new tick value will cause any
  1364. tasks to be unblocked. */
  1365. traceTASK_INCREMENT_TICK( xTickCount );
  1366. if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
  1367. {
  1368. ++xTickCount;
  1369. if( xTickCount == ( portTickType ) 0U )
  1370. {
  1371. xList *pxTemp;
  1372. /* Tick count has overflowed so we need to swap the delay lists.
  1373. If there are any items in pxDelayedTaskList here then there is
  1374. an error! */
  1375. configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );
  1376. pxTemp = pxDelayedTaskList;
  1377. pxDelayedTaskList = pxOverflowDelayedTaskList;
  1378. pxOverflowDelayedTaskList = pxTemp;
  1379. xNumOfOverflows++;
  1380. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
  1381. {
  1382. /* The new current delayed list is empty. Set
  1383. xNextTaskUnblockTime to the maximum possible value so it is
  1384. extremely unlikely that the
  1385. if( xTickCount >= xNextTaskUnblockTime ) test will pass until
  1386. there is an item in the delayed list. */
  1387. xNextTaskUnblockTime = portMAX_DELAY;
  1388. }
  1389. else
  1390. {
  1391. /* The new current delayed list is not empty, get the value of
  1392. the item at the head of the delayed list. This is the time at
  1393. which the task at the head of the delayed list should be removed
  1394. from the Blocked state. */
  1395. pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
  1396. xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );
  1397. }
  1398. }
  1399. /* See if this tick has made a timeout expire. */
  1400. prvCheckDelayedTasks();
  1401. }
  1402. else
  1403. {
  1404. ++uxMissedTicks;
  1405. /* The tick hook gets called at regular intervals, even if the
  1406. scheduler is locked. */
  1407. #if ( configUSE_TICK_HOOK == 1 )
  1408. {
  1409. vApplicationTickHook();
  1410. }
  1411. #endif
  1412. }
  1413. #if ( configUSE_TICK_HOOK == 1 )
  1414. {
  1415. /* Guard against the tick hook being called when the missed tick
  1416. count is being unwound (when the scheduler is being unlocked. */
  1417. if( uxMissedTicks == ( unsigned portBASE_TYPE ) 0U )
  1418. {
  1419. vApplicationTickHook();
  1420. }
  1421. }
  1422. #endif
  1423. }
  1424. /*-----------------------------------------------------------*/
  1425. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  1426. void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction )
  1427. {
  1428. tskTCB *xTCB;
  1429. /* If xTask is NULL then we are setting our own task hook. */
  1430. if( xTask == NULL )
  1431. {
  1432. xTCB = ( tskTCB * ) pxCurrentTCB;
  1433. }
  1434. else
  1435. {
  1436. xTCB = ( tskTCB * ) xTask;
  1437. }
  1438. /* Save the hook function in the TCB. A critical section is required as
  1439. the value can be accessed from an interrupt. */
  1440. taskENTER_CRITICAL();
  1441. xTCB->pxTaskTag = pxHookFunction;
  1442. taskEXIT_CRITICAL();
  1443. }
  1444. #endif
  1445. /*-----------------------------------------------------------*/
  1446. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  1447. pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask )
  1448. {
  1449. tskTCB *xTCB;
  1450. pdTASK_HOOK_CODE xReturn;
  1451. /* If xTask is NULL then we are setting our own task hook. */
  1452. if( xTask == NULL )
  1453. {
  1454. xTCB = ( tskTCB * ) pxCurrentTCB;
  1455. }
  1456. else
  1457. {
  1458. xTCB = ( tskTCB * ) xTask;
  1459. }
  1460. /* Save the hook function in the TCB. A critical section is required as
  1461. the value can be accessed from an interrupt. */
  1462. taskENTER_CRITICAL();
  1463. xReturn = xTCB->pxTaskTag;
  1464. taskEXIT_CRITICAL();
  1465. return xReturn;
  1466. }
  1467. #endif
  1468. /*-----------------------------------------------------------*/
  1469. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  1470. portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter )
  1471. {
  1472. tskTCB *xTCB;
  1473. portBASE_TYPE xReturn;
  1474. /* If xTask is NULL then we are calling our own task hook. */
  1475. if( xTask == NULL )
  1476. {
  1477. xTCB = ( tskTCB * ) pxCurrentTCB;
  1478. }
  1479. else
  1480. {
  1481. xTCB = ( tskTCB * ) xTask;
  1482. }
  1483. if( xTCB->pxTaskTag != NULL )
  1484. {
  1485. xReturn = xTCB->pxTaskTag( pvParameter );
  1486. }
  1487. else
  1488. {
  1489. xReturn = pdFAIL;
  1490. }
  1491. return xReturn;
  1492. }
  1493. #endif
  1494. /*-----------------------------------------------------------*/
  1495. void vTaskSwitchContext( void )
  1496. {
  1497. if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
  1498. {
  1499. /* The scheduler is currently suspended - do not allow a context
  1500. switch. */
  1501. xMissedYield = pdTRUE;
  1502. }
  1503. else
  1504. {
  1505. traceTASK_SWITCHED_OUT();
  1506. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  1507. {
  1508. unsigned long ulTempCounter;
  1509. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  1510. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTempCounter );
  1511. #else
  1512. ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();
  1513. #endif
  1514. /* Add the amount of time the task has been running to the accumulated
  1515. time so far. The time the task started running was stored in
  1516. ulTaskSwitchedInTime. Note that there is no overflow protection here
  1517. so count values are only valid until the timer overflows. Generally
  1518. this will be about 1 hour assuming a 1uS timer increment. */
  1519. pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );
  1520. ulTaskSwitchedInTime = ulTempCounter;
  1521. }
  1522. #endif
  1523. taskFIRST_CHECK_FOR_STACK_OVERFLOW();
  1524. taskSECOND_CHECK_FOR_STACK_OVERFLOW();
  1525. taskSELECT_HIGHEST_PRIORITY_TASK();
  1526. traceTASK_SWITCHED_IN();
  1527. }
  1528. }
  1529. /*-----------------------------------------------------------*/
  1530. void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )
  1531. {
  1532. portTickType xTimeToWake;
  1533. configASSERT( pxEventList );
  1534. /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
  1535. SCHEDULER SUSPENDED. */
  1536. /* Place the event list item of the TCB in the appropriate event list.
  1537. This is placed in the list in priority order so the highest priority task
  1538. is the first to be woken by the event. */
  1539. vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
  1540. /* We must remove ourselves from the ready list before adding ourselves
  1541. to the blocked list as the same list item is used for both lists. We have
  1542. exclusive access to the ready lists as the scheduler is locked. */
  1543. if( uxListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) ) == 0 )
  1544. {
  1545. /* The current task must be in a ready list, so there is no need to
  1546. check, and the port reset macro can be called directly. */
  1547. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  1548. }
  1549. #if ( INCLUDE_vTaskSuspend == 1 )
  1550. {
  1551. if( xTicksToWait == portMAX_DELAY )
  1552. {
  1553. /* Add ourselves to the suspended task list instead of a delayed task
  1554. list to ensure we are not woken by a timing event. We will block
  1555. indefinitely. */
  1556. vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
  1557. }
  1558. else
  1559. {
  1560. /* Calculate the time at which the task should be woken if the event does
  1561. not occur. This may overflow but this doesn't matter. */
  1562. xTimeToWake = xTickCount + xTicksToWait;
  1563. prvAddCurrentTaskToDelayedList( xTimeToWake );
  1564. }
  1565. }
  1566. #else
  1567. {
  1568. /* Calculate the time at which the task should be woken if the event does
  1569. not occur. This may overflow but this doesn't matter. */
  1570. xTimeToWake = xTickCount + xTicksToWait;
  1571. prvAddCurrentTaskToDelayedList( xTimeToWake );
  1572. }
  1573. #endif
  1574. }
  1575. /*-----------------------------------------------------------*/
  1576. #if configUSE_TIMERS == 1
  1577. void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )
  1578. {
  1579. portTickType xTimeToWake;
  1580. configASSERT( pxEventList );
  1581. /* This function should not be called by application code hence the
  1582. 'Restricted' in its name. It is not part of the public API. It is
  1583. designed for use by kernel code, and has special calling requirements -
  1584. it should be called from a critical section. */
  1585. /* Place the event list item of the TCB in the appropriate event list.
  1586. In this case it is assume that this is the only task that is going to
  1587. be waiting on this event list, so the faster vListInsertEnd() function
  1588. can be used in place of vListInsert. */
  1589. vListInsertEnd( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
  1590. /* We must remove this task from the ready list before adding it to the
  1591. blocked list as the same list item is used for both lists. This
  1592. function is called form a critical section. */
  1593. if( uxListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) ) == 0 )
  1594. {
  1595. /* The current task must be in a ready list, so there is no need to
  1596. check, and the port reset macro can be called directly. */
  1597. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  1598. }
  1599. /* Calculate the time at which the task should be woken if the event does
  1600. not occur. This may overflow but this doesn't matter. */
  1601. xTimeToWake = xTickCount + xTicksToWait;
  1602. traceTASK_DELAY_UNTIL();
  1603. prvAddCurrentTaskToDelayedList( xTimeToWake );
  1604. }
  1605. #endif /* configUSE_TIMERS */
  1606. /*-----------------------------------------------------------*/
  1607. signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )
  1608. {
  1609. tskTCB *pxUnblockedTCB;
  1610. portBASE_TYPE xReturn;
  1611. /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
  1612. SCHEDULER SUSPENDED. It can also be called from within an ISR. */
  1613. /* The event list is sorted in priority order, so we can remove the
  1614. first in the list, remove the TCB from the delayed list, and add
  1615. it to the ready list.
  1616. If an event is for a queue that is locked then this function will never
  1617. get called - the lock count on the queue will get modified instead. This
  1618. means we can always expect exclusive access to the event list here.
  1619. This function assumes that a check has already been made to ensure that
  1620. pxEventList is not empty. */
  1621. pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
  1622. configASSERT( pxUnblockedTCB );
  1623. uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
  1624. if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
  1625. {
  1626. uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );
  1627. prvAddTaskToReadyQueue( pxUnblockedTCB );
  1628. }
  1629. else
  1630. {
  1631. /* We cannot access the delayed or ready lists, so will hold this
  1632. task pending until the scheduler is resumed. */
  1633. vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
  1634. }
  1635. if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1636. {
  1637. /* Return true if the task removed from the event list has
  1638. a higher priority than the calling task. This allows
  1639. the calling task to know if it should force a context
  1640. switch now. */
  1641. xReturn = pdTRUE;
  1642. }
  1643. else
  1644. {
  1645. xReturn = pdFALSE;
  1646. }
  1647. return xReturn;
  1648. }
  1649. /*-----------------------------------------------------------*/
  1650. void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )
  1651. {
  1652. configASSERT( pxTimeOut );
  1653. pxTimeOut->xOverflowCount = xNumOfOverflows;
  1654. pxTimeOut->xTimeOnEntering = xTickCount;
  1655. }
  1656. /*-----------------------------------------------------------*/
  1657. portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait )
  1658. {
  1659. portBASE_TYPE xReturn;
  1660. configASSERT( pxTimeOut );
  1661. configASSERT( pxTicksToWait );
  1662. taskENTER_CRITICAL();
  1663. {
  1664. #if ( INCLUDE_vTaskSuspend == 1 )
  1665. /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is
  1666. the maximum block time then the task should block indefinitely, and
  1667. therefore never time out. */
  1668. if( *pxTicksToWait == portMAX_DELAY )
  1669. {
  1670. xReturn = pdFALSE;
  1671. }
  1672. else /* We are not blocking indefinitely, perform the checks below. */
  1673. #endif
  1674. if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( ( portTickType ) xTickCount >= ( portTickType ) pxTimeOut->xTimeOnEntering ) )
  1675. {
  1676. /* The tick count is greater than the time at which vTaskSetTimeout()
  1677. was called, but has also overflowed since vTaskSetTimeOut() was called.
  1678. It must have wrapped all the way around and gone past us again. This
  1679. passed since vTaskSetTimeout() was called. */
  1680. xReturn = pdTRUE;
  1681. }
  1682. else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )
  1683. {
  1684. /* Not a genuine timeout. Adjust parameters for time remaining. */
  1685. *pxTicksToWait -= ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering );
  1686. vTaskSetTimeOutState( pxTimeOut );
  1687. xReturn = pdFALSE;
  1688. }
  1689. else
  1690. {
  1691. xReturn = pdTRUE;
  1692. }
  1693. }
  1694. taskEXIT_CRITICAL();
  1695. return xReturn;
  1696. }
  1697. /*-----------------------------------------------------------*/
  1698. void vTaskMissedYield( void )
  1699. {
  1700. xMissedYield = pdTRUE;
  1701. }
  1702. /*-----------------------------------------------------------*/
  1703. #if ( configUSE_TRACE_FACILITY == 1 )
  1704. unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask )
  1705. {
  1706. unsigned portBASE_TYPE uxReturn;
  1707. tskTCB *pxTCB;
  1708. if( xTask != NULL )
  1709. {
  1710. pxTCB = ( tskTCB * ) xTask;
  1711. uxReturn = pxTCB->uxTaskNumber;
  1712. }
  1713. else
  1714. {
  1715. uxReturn = 0U;
  1716. }
  1717. return uxReturn;
  1718. }
  1719. #endif
  1720. /*-----------------------------------------------------------*/
  1721. #if ( configUSE_TRACE_FACILITY == 1 )
  1722. void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle )
  1723. {
  1724. tskTCB *pxTCB;
  1725. if( xTask != NULL )
  1726. {
  1727. pxTCB = ( tskTCB * ) xTask;
  1728. pxTCB->uxTaskNumber = uxHandle;
  1729. }
  1730. }
  1731. #endif
  1732. /*
  1733. * -----------------------------------------------------------
  1734. * The Idle task.
  1735. * ----------------------------------------------------------
  1736. *
  1737. * The portTASK_FUNCTION() macro is used to allow port/compiler specific
  1738. * language extensions. The equivalent prototype for this function is:
  1739. *
  1740. * void prvIdleTask( void *pvParameters );
  1741. *
  1742. */
  1743. static portTASK_FUNCTION( prvIdleTask, pvParameters )
  1744. {
  1745. /* Stop warnings. */
  1746. ( void ) pvParameters;
  1747. for( ;; )
  1748. {
  1749. /* See if any tasks have been deleted. */
  1750. prvCheckTasksWaitingTermination();
  1751. #if ( configUSE_PREEMPTION == 0 )
  1752. {
  1753. /* If we are not using preemption we keep forcing a task switch to
  1754. see if any other task has become available. If we are using
  1755. preemption we don't need to do this as any task becoming available
  1756. will automatically get the processor anyway. */
  1757. taskYIELD();
  1758. }
  1759. #endif
  1760. #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
  1761. {
  1762. /* When using preemption tasks of equal priority will be
  1763. timesliced. If a task that is sharing the idle priority is ready
  1764. to run then the idle task should yield before the end of the
  1765. timeslice.
  1766. A critical region is not required here as we are just reading from
  1767. the list, and an occasional incorrect value will not matter. If
  1768. the ready list at the idle priority contains more than one task
  1769. then a task other than the idle task is ready to execute. */
  1770. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )
  1771. {
  1772. taskYIELD();
  1773. }
  1774. }
  1775. #endif
  1776. #if ( configUSE_IDLE_HOOK == 1 )
  1777. {
  1778. extern void vApplicationIdleHook( void );
  1779. /* Call the user defined function from within the idle task. This
  1780. allows the application designer to add background functionality
  1781. without the overhead of a separate task.
  1782. NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
  1783. CALL A FUNCTION THAT MIGHT BLOCK. */
  1784. vApplicationIdleHook();
  1785. }
  1786. #endif
  1787. /* This conditional compilation should use inequality to 0, not equality
  1788. to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
  1789. user defined low power mode implementations require
  1790. configUSE_TICKLESS_IDLE to be set to a value other than 1. */
  1791. #if ( configUSE_TICKLESS_IDLE != 0 )
  1792. {
  1793. portTickType xExpectedIdleTime;
  1794. /* If the expected idle time is 1 then the idle time would end at
  1795. the end of the current time slice. The idle time must be at least
  1796. 2 to ensure any pended ticks between this point and the tick being
  1797. stopped can be legitimately stepped over when the tick suppression
  1798. routines returns. */
  1799. const portTickType xMinimumExpectedIdleTime = ( portTickType ) 2;
  1800. /* It is not desirable to suspend then resume the scheduler on
  1801. each iteration of the idle task. Therefore, a preliminary
  1802. test of the expected idle time is performed without the
  1803. scheduler suspended. The result here is not necessarily
  1804. valid. */
  1805. xExpectedIdleTime = prvGetExpectedIdleTime();
  1806. if( xExpectedIdleTime >= xMinimumExpectedIdleTime )
  1807. {
  1808. vTaskSuspendAll();
  1809. {
  1810. /* Now the scheduler is suspended, the expected idle
  1811. time can be sampled again, and this time its value can
  1812. be used. */
  1813. configASSERT( xNextTaskUnblockTime >= xTickCount );
  1814. xExpectedIdleTime = prvGetExpectedIdleTime();
  1815. if( xExpectedIdleTime >= xMinimumExpectedIdleTime )
  1816. {
  1817. portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
  1818. }
  1819. }
  1820. xTaskResumeAll();
  1821. }
  1822. }
  1823. #endif
  1824. }
  1825. } /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
  1826. /*-----------------------------------------------------------
  1827. * File private functions documented at the top of the file.
  1828. *----------------------------------------------------------*/
  1829. static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )
  1830. {
  1831. /* Store the function name in the TCB. */
  1832. #if configMAX_TASK_NAME_LEN > 1
  1833. {
  1834. /* Don't bring strncpy into the build unnecessarily. */
  1835. strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );
  1836. }
  1837. #endif
  1838. pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = ( signed char ) '\0';
  1839. /* This is used as an array index so must ensure it's not too large. First
  1840. remove the privilege bit if one is present. */
  1841. if( uxPriority >= configMAX_PRIORITIES )
  1842. {
  1843. uxPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;
  1844. }
  1845. pxTCB->uxPriority = uxPriority;
  1846. #if ( configUSE_MUTEXES == 1 )
  1847. {
  1848. pxTCB->uxBasePriority = uxPriority;
  1849. }
  1850. #endif
  1851. vListInitialiseItem( &( pxTCB->xGenericListItem ) );
  1852. vListInitialiseItem( &( pxTCB->xEventListItem ) );
  1853. /* Set the pxTCB as a link back from the xListItem. This is so we can get
  1854. back to the containing TCB from a generic item in a list. */
  1855. listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
  1856. /* Event lists are always in priority order. */
  1857. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
  1858. listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
  1859. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  1860. {
  1861. pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0U;
  1862. }
  1863. #endif
  1864. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  1865. {
  1866. pxTCB->pxTaskTag = NULL;
  1867. }
  1868. #endif
  1869. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  1870. {
  1871. pxTCB->ulRunTimeCounter = 0UL;
  1872. }
  1873. #endif
  1874. #if ( portUSING_MPU_WRAPPERS == 1 )
  1875. {
  1876. vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );
  1877. }
  1878. #else
  1879. {
  1880. ( void ) xRegions;
  1881. ( void ) usStackDepth;
  1882. }
  1883. #endif
  1884. }
  1885. /*-----------------------------------------------------------*/
  1886. #if ( portUSING_MPU_WRAPPERS == 1 )
  1887. void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )
  1888. {
  1889. tskTCB *pxTCB;
  1890. if( xTaskToModify == pxCurrentTCB )
  1891. {
  1892. xTaskToModify = NULL;
  1893. }
  1894. /* If null is passed in here then we are deleting ourselves. */
  1895. pxTCB = prvGetTCBFromHandle( xTaskToModify );
  1896. vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
  1897. }
  1898. /*-----------------------------------------------------------*/
  1899. #endif
  1900. static void prvInitialiseTaskLists( void )
  1901. {
  1902. unsigned portBASE_TYPE uxPriority;
  1903. for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < configMAX_PRIORITIES; uxPriority++ )
  1904. {
  1905. vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );
  1906. }
  1907. vListInitialise( ( xList * ) &xDelayedTaskList1 );
  1908. vListInitialise( ( xList * ) &xDelayedTaskList2 );
  1909. vListInitialise( ( xList * ) &xPendingReadyList );
  1910. #if ( INCLUDE_vTaskDelete == 1 )
  1911. {
  1912. vListInitialise( ( xList * ) &xTasksWaitingTermination );
  1913. }
  1914. #endif
  1915. #if ( INCLUDE_vTaskSuspend == 1 )
  1916. {
  1917. vListInitialise( ( xList * ) &xSuspendedTaskList );
  1918. }
  1919. #endif
  1920. /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
  1921. using list2. */
  1922. pxDelayedTaskList = &xDelayedTaskList1;
  1923. pxOverflowDelayedTaskList = &xDelayedTaskList2;
  1924. }
  1925. /*-----------------------------------------------------------*/
  1926. static void prvCheckTasksWaitingTermination( void )
  1927. {
  1928. #if ( INCLUDE_vTaskDelete == 1 )
  1929. {
  1930. portBASE_TYPE xListIsEmpty;
  1931. /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
  1932. too often in the idle task. */
  1933. while( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0U )
  1934. {
  1935. vTaskSuspendAll();
  1936. xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
  1937. xTaskResumeAll();
  1938. if( xListIsEmpty == pdFALSE )
  1939. {
  1940. tskTCB *pxTCB;
  1941. taskENTER_CRITICAL();
  1942. {
  1943. pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );
  1944. uxListRemove( &( pxTCB->xGenericListItem ) );
  1945. --uxCurrentNumberOfTasks;
  1946. --uxTasksDeleted;
  1947. }
  1948. taskEXIT_CRITICAL();
  1949. prvDeleteTCB( pxTCB );
  1950. }
  1951. }
  1952. }
  1953. #endif
  1954. }
  1955. /*-----------------------------------------------------------*/
  1956. static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake )
  1957. {
  1958. /* The list item will be inserted in wake time order. */
  1959. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
  1960. if( xTimeToWake < xTickCount )
  1961. {
  1962. /* Wake time has overflowed. Place this item in the overflow list. */
  1963. vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
  1964. }
  1965. else
  1966. {
  1967. /* The wake time has not overflowed, so we can use the current block list. */
  1968. vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
  1969. /* If the task entering the blocked state was placed at the head of the
  1970. list of blocked tasks then xNextTaskUnblockTime needs to be updated
  1971. too. */
  1972. if( xTimeToWake < xNextTaskUnblockTime )
  1973. {
  1974. xNextTaskUnblockTime = xTimeToWake;
  1975. }
  1976. }
  1977. }
  1978. /*-----------------------------------------------------------*/
  1979. static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )
  1980. {
  1981. tskTCB *pxNewTCB;
  1982. /* Allocate space for the TCB. Where the memory comes from depends on
  1983. the implementation of the port malloc function. */
  1984. pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );
  1985. if( pxNewTCB != NULL )
  1986. {
  1987. /* Allocate space for the stack used by the task being created.
  1988. The base of the stack memory stored in the TCB so the task can
  1989. be deleted later if required. */
  1990. pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );
  1991. if( pxNewTCB->pxStack == NULL )
  1992. {
  1993. /* Could not allocate the stack. Delete the allocated TCB. */
  1994. vPortFree( pxNewTCB );
  1995. pxNewTCB = NULL;
  1996. }
  1997. else
  1998. {
  1999. /* Just to help debugging. */
  2000. memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( portSTACK_TYPE ) );
  2001. }
  2002. }
  2003. return pxNewTCB;
  2004. }
  2005. /*-----------------------------------------------------------*/
  2006. #if ( configUSE_TRACE_FACILITY == 1 )
  2007. static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )
  2008. {
  2009. volatile tskTCB *pxNextTCB, *pxFirstTCB;
  2010. unsigned short usStackRemaining;
  2011. PRIVILEGED_DATA static char pcStatusString[ configMAX_TASK_NAME_LEN + 30 ];
  2012. /* Write the details of all the TCB's in pxList into the buffer. */
  2013. listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
  2014. do
  2015. {
  2016. listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
  2017. #if ( portSTACK_GROWTH > 0 )
  2018. {
  2019. usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );
  2020. }
  2021. #else
  2022. {
  2023. usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );
  2024. }
  2025. #endif
  2026. sprintf( pcStatusString, ( char * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, ( unsigned int ) usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
  2027. strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatusString );
  2028. } while( pxNextTCB != pxFirstTCB );
  2029. }
  2030. #endif
  2031. /*-----------------------------------------------------------*/
  2032. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  2033. static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime )
  2034. {
  2035. volatile tskTCB *pxNextTCB, *pxFirstTCB;
  2036. unsigned long ulStatsAsPercentage;
  2037. /* Write the run time stats of all the TCB's in pxList into the buffer. */
  2038. listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
  2039. do
  2040. {
  2041. /* Get next TCB in from the list. */
  2042. listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
  2043. /* Divide by zero check. */
  2044. if( ulTotalRunTime > 0UL )
  2045. {
  2046. /* Has the task run at all? */
  2047. if( pxNextTCB->ulRunTimeCounter == 0UL )
  2048. {
  2049. /* The task has used no CPU time at all. */
  2050. sprintf( pcStatsString, ( char * ) "%s\t\t0\t\t0%%\r\n", pxNextTCB->pcTaskName );
  2051. }
  2052. else
  2053. {
  2054. /* What percentage of the total run time has the task used?
  2055. This will always be rounded down to the nearest integer.
  2056. ulTotalRunTime has already been divided by 100. */
  2057. ulStatsAsPercentage = pxNextTCB->ulRunTimeCounter / ulTotalRunTime;
  2058. if( ulStatsAsPercentage > 0UL )
  2059. {
  2060. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  2061. {
  2062. sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );
  2063. }
  2064. #else
  2065. {
  2066. /* sizeof( int ) == sizeof( long ) so a smaller
  2067. printf() library can be used. */
  2068. sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
  2069. }
  2070. #endif
  2071. }
  2072. else
  2073. {
  2074. /* If the percentage is zero here then the task has
  2075. consumed less than 1% of the total run time. */
  2076. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  2077. {
  2078. sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );
  2079. }
  2080. #else
  2081. {
  2082. /* sizeof( int ) == sizeof( long ) so a smaller
  2083. printf() library can be used. */
  2084. sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );
  2085. }
  2086. #endif
  2087. }
  2088. }
  2089. strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatsString );
  2090. }
  2091. } while( pxNextTCB != pxFirstTCB );
  2092. }
  2093. #endif
  2094. /*-----------------------------------------------------------*/
  2095. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
  2096. static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte )
  2097. {
  2098. register unsigned short usCount = 0U;
  2099. while( *pucStackByte == tskSTACK_FILL_BYTE )
  2100. {
  2101. pucStackByte -= portSTACK_GROWTH;
  2102. usCount++;
  2103. }
  2104. usCount /= sizeof( portSTACK_TYPE );
  2105. return usCount;
  2106. }
  2107. #endif
  2108. /*-----------------------------------------------------------*/
  2109. #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
  2110. unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask )
  2111. {
  2112. tskTCB *pxTCB;
  2113. unsigned char *pcEndOfStack;
  2114. unsigned portBASE_TYPE uxReturn;
  2115. pxTCB = prvGetTCBFromHandle( xTask );
  2116. #if portSTACK_GROWTH < 0
  2117. {
  2118. pcEndOfStack = ( unsigned char * ) pxTCB->pxStack;
  2119. }
  2120. #else
  2121. {
  2122. pcEndOfStack = ( unsigned char * ) pxTCB->pxEndOfStack;
  2123. }
  2124. #endif
  2125. uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );
  2126. return uxReturn;
  2127. }
  2128. #endif
  2129. /*-----------------------------------------------------------*/
  2130. #if ( INCLUDE_vTaskDelete == 1 )
  2131. static void prvDeleteTCB( tskTCB *pxTCB )
  2132. {
  2133. /* This call is required specifically for the TriCore port. It must be
  2134. above the vPortFree() calls. The call is also used by ports/demos that
  2135. want to allocate and clean RAM statically. */
  2136. portCLEAN_UP_TCB( pxTCB );
  2137. /* Free up the memory allocated by the scheduler for the task. It is up to
  2138. the task to free any memory allocated at the application level. */
  2139. vPortFreeAligned( pxTCB->pxStack );
  2140. vPortFree( pxTCB );
  2141. }
  2142. #endif
  2143. /*-----------------------------------------------------------*/
  2144. #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
  2145. xTaskHandle xTaskGetCurrentTaskHandle( void )
  2146. {
  2147. xTaskHandle xReturn;
  2148. /* A critical section is not required as this is not called from
  2149. an interrupt and the current TCB will always be the same for any
  2150. individual execution thread. */
  2151. xReturn = pxCurrentTCB;
  2152. return xReturn;
  2153. }
  2154. #endif
  2155. /*-----------------------------------------------------------*/
  2156. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  2157. portBASE_TYPE xTaskGetSchedulerState( void )
  2158. {
  2159. portBASE_TYPE xReturn;
  2160. if( xSchedulerRunning == pdFALSE )
  2161. {
  2162. xReturn = taskSCHEDULER_NOT_STARTED;
  2163. }
  2164. else
  2165. {
  2166. if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
  2167. {
  2168. xReturn = taskSCHEDULER_RUNNING;
  2169. }
  2170. else
  2171. {
  2172. xReturn = taskSCHEDULER_SUSPENDED;
  2173. }
  2174. }
  2175. return xReturn;
  2176. }
  2177. #endif
  2178. /*-----------------------------------------------------------*/
  2179. #if ( configUSE_MUTEXES == 1 )
  2180. void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder )
  2181. {
  2182. tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
  2183. /* If the mutex was given back by an interrupt while the queue was
  2184. locked then the mutex holder might now be NULL. */
  2185. if( pxMutexHolder != NULL )
  2186. {
  2187. if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )
  2188. {
  2189. /* Adjust the mutex holder state to account for its new priority. */
  2190. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority );
  2191. /* If the task being modified is in the ready state it will need to
  2192. be moved into a new list. */
  2193. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )
  2194. {
  2195. if( uxListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) ) == 0 )
  2196. {
  2197. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  2198. }
  2199. /* Inherit the priority before being moved into the new list. */
  2200. pxTCB->uxPriority = pxCurrentTCB->uxPriority;
  2201. prvAddTaskToReadyQueue( pxTCB );
  2202. }
  2203. else
  2204. {
  2205. /* Just inherit the priority. */
  2206. pxTCB->uxPriority = pxCurrentTCB->uxPriority;
  2207. }
  2208. traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB->uxPriority );
  2209. }
  2210. }
  2211. }
  2212. #endif
  2213. /*-----------------------------------------------------------*/
  2214. #if ( configUSE_MUTEXES == 1 )
  2215. void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder )
  2216. {
  2217. tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
  2218. if( pxMutexHolder != NULL )
  2219. {
  2220. if( pxTCB->uxPriority != pxTCB->uxBasePriority )
  2221. {
  2222. /* We must be the running task to be able to give the mutex back.
  2223. Remove ourselves from the ready list we currently appear in. */
  2224. if( uxListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) ) == 0 )
  2225. {
  2226. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  2227. }
  2228. /* Disinherit the priority before adding the task into the new
  2229. ready list. */
  2230. traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
  2231. pxTCB->uxPriority = pxTCB->uxBasePriority;
  2232. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority );
  2233. prvAddTaskToReadyQueue( pxTCB );
  2234. }
  2235. }
  2236. }
  2237. #endif
  2238. /*-----------------------------------------------------------*/
  2239. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  2240. void vTaskEnterCritical( void )
  2241. {
  2242. portDISABLE_INTERRUPTS();
  2243. if( xSchedulerRunning != pdFALSE )
  2244. {
  2245. ( pxCurrentTCB->uxCriticalNesting )++;
  2246. }
  2247. }
  2248. #endif
  2249. /*-----------------------------------------------------------*/
  2250. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  2251. void vTaskExitCritical( void )
  2252. {
  2253. if( xSchedulerRunning != pdFALSE )
  2254. {
  2255. if( pxCurrentTCB->uxCriticalNesting > 0U )
  2256. {
  2257. ( pxCurrentTCB->uxCriticalNesting )--;
  2258. if( pxCurrentTCB->uxCriticalNesting == 0U )
  2259. {
  2260. portENABLE_INTERRUPTS();
  2261. }
  2262. }
  2263. }
  2264. }
  2265. #endif
  2266. /*-----------------------------------------------------------*/