tasks.c 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319
  1. /*
  2. FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. /* Standard includes. */
  56. #include <stdlib.h>
  57. #include <string.h>
  58. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  59. all the API functions to use the MPU wrappers. That should only be done when
  60. task.h is included from an application file. */
  61. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  62. /* FreeRTOS includes. */
  63. #include "FreeRTOS.h"
  64. #include "task.h"
  65. #include "fr_timers.h"
  66. #include "StackMacros.h"
  67. /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
  68. MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
  69. header files above, but not in this file, in order to generate the correct
  70. privileged Vs unprivileged linkage and placement. */
  71. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
  72. /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
  73. functions but without including stdio.h here. */
  74. #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
  75. /* At the bottom of this file are two optional functions that can be used
  76. to generate human readable text from the raw data generated by the
  77. uxTaskGetSystemState() function. Note the formatting functions are provided
  78. for convenience only, and are NOT considered part of the kernel. */
  79. #include <stdio.h>
  80. #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
  81. /* Sanity check the configuration. */
  82. #if configUSE_TICKLESS_IDLE != 0
  83. #if INCLUDE_vTaskSuspend != 1
  84. #error INCLUDE_vTaskSuspend must be set to 1 if configUSE_TICKLESS_IDLE is not set to 0
  85. #endif /* INCLUDE_vTaskSuspend */
  86. #endif /* configUSE_TICKLESS_IDLE */
  87. /*
  88. * Defines the size, in words, of the stack allocated to the idle task.
  89. */
  90. #define tskIDLE_STACK_SIZE configMINIMAL_STACK_SIZE
  91. #if( configUSE_PREEMPTION == 0 )
  92. /* If the cooperative scheduler is being used then a yield should not be
  93. performed just because a higher priority task has been woken. */
  94. #define taskYIELD_IF_USING_PREEMPTION()
  95. #else
  96. #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
  97. #endif
  98. /* Value that can be assigned to the eNotifyState member of the TCB. */
  99. typedef enum
  100. {
  101. eNotWaitingNotification = 0,
  102. eWaitingNotification,
  103. eNotified
  104. } eNotifyValue;
  105. /*
  106. * Task control block. A task control block (TCB) is allocated for each task,
  107. * and stores task state information, including a pointer to the task's context
  108. * (the task's run time environment, including register values)
  109. */
  110. typedef struct tskTaskControlBlock
  111. {
  112. volatile StackType_t *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
  113. #if ( portUSING_MPU_WRAPPERS == 1 )
  114. xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
  115. BaseType_t xUsingStaticallyAllocatedStack; /* Set to pdTRUE if the stack is a statically allocated array, and pdFALSE if the stack is dynamically allocated. */
  116. #endif
  117. ListItem_t xGenericListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
  118. ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
  119. UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
  120. StackType_t *pxStack; /*< Points to the start of the stack. */
  121. char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  122. #if ( portSTACK_GROWTH > 0 )
  123. StackType_t *pxEndOfStack; /*< Points to the end of the stack on architectures where the stack grows up from low memory. */
  124. #endif
  125. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  126. UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
  127. #endif
  128. #if ( configUSE_TRACE_FACILITY == 1 )
  129. UBaseType_t 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. */
  130. UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
  131. #endif
  132. #if ( configUSE_MUTEXES == 1 )
  133. UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
  134. UBaseType_t uxMutexesHeld;
  135. #endif
  136. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  137. TaskHookFunction_t pxTaskTag;
  138. #endif
  139. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  140. uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
  141. #endif
  142. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  143. /* Allocate a Newlib reent structure that is specific to this task.
  144. Note Newlib support has been included by popular demand, but is not
  145. used by the FreeRTOS maintainers themselves. FreeRTOS is not
  146. responsible for resulting newlib operation. User must be familiar with
  147. newlib and must provide system-wide implementations of the necessary
  148. stubs. Be warned that (at the time of writing) the current newlib design
  149. implements a system-wide malloc() that must be provided with locks. */
  150. struct _reent xNewLib_reent;
  151. #endif
  152. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  153. volatile uint32_t ulNotifiedValue;
  154. volatile eNotifyValue eNotifyState;
  155. #endif
  156. } tskTCB;
  157. /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
  158. below to enable the use of older kernel aware debuggers. */
  159. typedef tskTCB TCB_t;
  160. /*
  161. * Some kernel aware debuggers require the data the debugger needs access to to
  162. * be global, rather than file scope.
  163. */
  164. #ifdef portREMOVE_STATIC_QUALIFIER
  165. #define static
  166. #endif
  167. /*lint -e956 A manual analysis and inspection has been used to determine which
  168. static variables must be declared volatile. */
  169. PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
  170. /* Lists for ready and blocked tasks. --------------------*/
  171. PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ];/*< Prioritised ready tasks. */
  172. PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
  173. PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
  174. PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
  175. PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
  176. PRIVILEGED_DATA static List_t xPendingReadyList; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
  177. #if ( INCLUDE_vTaskDelete == 1 )
  178. PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
  179. PRIVILEGED_DATA static volatile UBaseType_t uxTasksDeleted = ( UBaseType_t ) 0U;
  180. #endif
  181. #if ( INCLUDE_vTaskSuspend == 1 )
  182. PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
  183. #endif
  184. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  185. PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle = NULL; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
  186. #endif
  187. /* Other file private variables. --------------------------------*/
  188. PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
  189. PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) 0U;
  190. PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
  191. PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
  192. PRIVILEGED_DATA static volatile UBaseType_t uxPendedTicks = ( UBaseType_t ) 0U;
  193. PRIVILEGED_DATA static volatile BaseType_t xYieldPending = pdFALSE;
  194. PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
  195. PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
  196. PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = portMAX_DELAY;
  197. /* Context switches are held pending while the scheduler is suspended. Also,
  198. interrupts must not manipulate the xGenericListItem of a TCB, or any of the
  199. lists the xGenericListItem can be referenced from, if the scheduler is suspended.
  200. If an interrupt needs to unblock a task while the scheduler is suspended then it
  201. moves the task's event list item into the xPendingReadyList, ready for the
  202. kernel to move the task from the pending ready list into the real ready list
  203. when the scheduler is unsuspended. The pending ready list itself can only be
  204. accessed from a critical section. */
  205. PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
  206. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  207. PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
  208. PRIVILEGED_DATA static uint32_t ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
  209. #endif
  210. /*lint +e956 */
  211. /* Debugging and trace facilities private variables and macros. ------------*/
  212. /*
  213. * The value used to fill the stack of a task when the task is created. This
  214. * is used purely for checking the high water mark for tasks.
  215. */
  216. #define tskSTACK_FILL_BYTE ( 0xa5U )
  217. /*
  218. * Macros used by vListTask to indicate which state a task is in.
  219. */
  220. #define tskBLOCKED_CHAR ( 'B' )
  221. #define tskREADY_CHAR ( 'R' )
  222. #define tskDELETED_CHAR ( 'D' )
  223. #define tskSUSPENDED_CHAR ( 'S' )
  224. /*-----------------------------------------------------------*/
  225. #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
  226. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
  227. performed in a generic way that is not optimised to any particular
  228. microcontroller architecture. */
  229. /* uxTopReadyPriority holds the priority of the highest priority ready
  230. state task. */
  231. #define taskRECORD_READY_PRIORITY( uxPriority ) \
  232. { \
  233. if( ( uxPriority ) > uxTopReadyPriority ) \
  234. { \
  235. uxTopReadyPriority = ( uxPriority ); \
  236. } \
  237. } /* taskRECORD_READY_PRIORITY */
  238. /*-----------------------------------------------------------*/
  239. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  240. { \
  241. /* Find the highest priority queue that contains ready tasks. */ \
  242. while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) ) \
  243. { \
  244. configASSERT( uxTopReadyPriority ); \
  245. --uxTopReadyPriority; \
  246. } \
  247. \
  248. /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
  249. the same priority get an equal share of the processor time. */ \
  250. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) ); \
  251. } /* taskSELECT_HIGHEST_PRIORITY_TASK */
  252. /*-----------------------------------------------------------*/
  253. /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
  254. they are only required when a port optimised method of task selection is
  255. being used. */
  256. #define taskRESET_READY_PRIORITY( uxPriority )
  257. #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
  258. #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  259. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
  260. performed in a way that is tailored to the particular microcontroller
  261. architecture being used. */
  262. /* A port optimised version is provided. Call the port defined macros. */
  263. #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
  264. /*-----------------------------------------------------------*/
  265. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  266. { \
  267. UBaseType_t uxTopPriority; \
  268. \
  269. /* Find the highest priority queue that contains ready tasks. */ \
  270. portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
  271. configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
  272. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
  273. } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
  274. /*-----------------------------------------------------------*/
  275. /* A port optimised version is provided, call it only if the TCB being reset
  276. is being referenced from a ready list. If it is referenced from a delayed
  277. or suspended list then it won't be in a ready list. */
  278. #define taskRESET_READY_PRIORITY( uxPriority ) \
  279. { \
  280. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
  281. { \
  282. portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
  283. } \
  284. }
  285. #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  286. /*-----------------------------------------------------------*/
  287. /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
  288. count overflows. */
  289. #define taskSWITCH_DELAYED_LISTS() \
  290. { \
  291. List_t *pxTemp; \
  292. \
  293. /* The delayed tasks list should be empty when the lists are switched. */ \
  294. configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
  295. \
  296. pxTemp = pxDelayedTaskList; \
  297. pxDelayedTaskList = pxOverflowDelayedTaskList; \
  298. pxOverflowDelayedTaskList = pxTemp; \
  299. xNumOfOverflows++; \
  300. prvResetNextTaskUnblockTime(); \
  301. }
  302. /*-----------------------------------------------------------*/
  303. /*
  304. * Place the task represented by pxTCB into the appropriate ready list for
  305. * the task. It is inserted at the end of the list.
  306. */
  307. #define prvAddTaskToReadyList( pxTCB ) \
  308. traceMOVED_TASK_TO_READY_STATE( pxTCB ) \
  309. taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
  310. vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )
  311. /*-----------------------------------------------------------*/
  312. /*
  313. * Several functions take an TaskHandle_t parameter that can optionally be NULL,
  314. * where NULL is used to indicate that the handle of the currently executing
  315. * task should be used in place of the parameter. This macro simply checks to
  316. * see if the parameter is NULL and returns a pointer to the appropriate TCB.
  317. */
  318. #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( TCB_t * ) pxCurrentTCB : ( TCB_t * ) ( pxHandle ) )
  319. /* The item value of the event list item is normally used to hold the priority
  320. of the task to which it belongs (coded to allow it to be held in reverse
  321. priority order). However, it is occasionally borrowed for other purposes. It
  322. is important its value is not updated due to a task priority change while it is
  323. being used for another purpose. The following bit definition is used to inform
  324. the scheduler that the value should not be changed - in which case it is the
  325. responsibility of whichever module is using the value to ensure it gets set back
  326. to its original value when it is released. */
  327. #if configUSE_16_BIT_TICKS == 1
  328. #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
  329. #else
  330. #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
  331. #endif
  332. /* Callback function prototypes. --------------------------*/
  333. #if configCHECK_FOR_STACK_OVERFLOW > 0
  334. extern void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName );
  335. #endif
  336. #if configUSE_TICK_HOOK > 0
  337. extern void vApplicationTickHook( void );
  338. #endif
  339. /* File private functions. --------------------------------*/
  340. /*
  341. * Utility to ready a TCB for a given task. Mainly just copies the parameters
  342. * into the TCB structure.
  343. */
  344. static void prvInitialiseTCBVariables( TCB_t * const pxTCB, const char * const pcName, UBaseType_t uxPriority, const MemoryRegion_t * const xRegions, const uint16_t usStackDepth ) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  345. /**
  346. * Utility task that simply returns pdTRUE if the task referenced by xTask is
  347. * currently in the Suspended state, or pdFALSE if the task referenced by xTask
  348. * is in any other state.
  349. */
  350. #if ( INCLUDE_vTaskSuspend == 1 )
  351. static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
  352. #endif /* INCLUDE_vTaskSuspend */
  353. /*
  354. * Utility to ready all the lists used by the scheduler. This is called
  355. * automatically upon the creation of the first task.
  356. */
  357. static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
  358. /*
  359. * The idle task, which as all tasks is implemented as a never ending loop.
  360. * The idle task is automatically created and added to the ready lists upon
  361. * creation of the first user task.
  362. *
  363. * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
  364. * language extensions. The equivalent prototype for this function is:
  365. *
  366. * void prvIdleTask( void *pvParameters );
  367. *
  368. */
  369. static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
  370. /*
  371. * Utility to free all memory allocated by the scheduler to hold a TCB,
  372. * including the stack pointed to by the TCB.
  373. *
  374. * This does not free memory allocated by the task itself (i.e. memory
  375. * allocated by calls to pvPortMalloc from within the tasks application code).
  376. */
  377. #if ( INCLUDE_vTaskDelete == 1 )
  378. static void prvDeleteTCB( TCB_t *pxTCB ) PRIVILEGED_FUNCTION;
  379. #endif
  380. /*
  381. * Used only by the idle task. This checks to see if anything has been placed
  382. * in the list of tasks waiting to be deleted. If so the task is cleaned up
  383. * and its TCB deleted.
  384. */
  385. static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
  386. /*
  387. * The currently executing task is entering the Blocked state. Add the task to
  388. * either the current or the overflow delayed task list.
  389. */
  390. static void prvAddCurrentTaskToDelayedList( const TickType_t xTimeToWake ) PRIVILEGED_FUNCTION;
  391. /*
  392. * Allocates memory from the heap for a TCB and associated stack. Checks the
  393. * allocation was successful.
  394. */
  395. static TCB_t *prvAllocateTCBAndStack( const uint16_t usStackDepth, StackType_t * const puxStackBuffer ) PRIVILEGED_FUNCTION;
  396. /*
  397. * Fills an TaskStatus_t structure with information on each task that is
  398. * referenced from the pxList list (which may be a ready list, a delayed list,
  399. * a suspended list, etc.).
  400. *
  401. * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
  402. * NORMAL APPLICATION CODE.
  403. */
  404. #if ( configUSE_TRACE_FACILITY == 1 )
  405. static UBaseType_t prvListTaskWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState ) PRIVILEGED_FUNCTION;
  406. #endif
  407. /*
  408. * When a task is created, the stack of the task is filled with a known value.
  409. * This function determines the 'high water mark' of the task stack by
  410. * determining how much of the stack remains at the original preset value.
  411. */
  412. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
  413. static uint16_t prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
  414. #endif
  415. /*
  416. * Return the amount of time, in ticks, that will pass before the kernel will
  417. * next move a task from the Blocked state to the Running state.
  418. *
  419. * This conditional compilation should use inequality to 0, not equality to 1.
  420. * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
  421. * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
  422. * set to a value other than 1.
  423. */
  424. #if ( configUSE_TICKLESS_IDLE != 0 )
  425. static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
  426. #endif
  427. /*
  428. * Set xNextTaskUnblockTime to the time at which the next Blocked state task
  429. * will exit the Blocked state.
  430. */
  431. static void prvResetNextTaskUnblockTime( void );
  432. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  433. /*
  434. * Helper function used to pad task names with spaces when printing out
  435. * human readable tables of task information.
  436. */
  437. static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName );
  438. #endif
  439. /*-----------------------------------------------------------*/
  440. BaseType_t xTaskGenericCreate( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, StackType_t * const puxStackBuffer, const MemoryRegion_t * const xRegions ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  441. {
  442. BaseType_t xReturn;
  443. TCB_t * pxNewTCB;
  444. StackType_t *pxTopOfStack;
  445. configASSERT( pxTaskCode );
  446. configASSERT( ( ( uxPriority & ( ~portPRIVILEGE_BIT ) ) < configMAX_PRIORITIES ) );
  447. /* Allocate the memory required by the TCB and stack for the new task,
  448. checking that the allocation was successful. */
  449. pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );
  450. if( pxNewTCB != NULL )
  451. {
  452. #if( portUSING_MPU_WRAPPERS == 1 )
  453. /* Should the task be created in privileged mode? */
  454. BaseType_t xRunPrivileged;
  455. if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
  456. {
  457. xRunPrivileged = pdTRUE;
  458. }
  459. else
  460. {
  461. xRunPrivileged = pdFALSE;
  462. }
  463. uxPriority &= ~portPRIVILEGE_BIT;
  464. if( puxStackBuffer != NULL )
  465. {
  466. /* The application provided its own stack. Note this so no
  467. attempt is made to delete the stack should that task be
  468. deleted. */
  469. pxNewTCB->xUsingStaticallyAllocatedStack = pdTRUE;
  470. }
  471. else
  472. {
  473. /* The stack was allocated dynamically. Note this so it can be
  474. deleted again if the task is deleted. */
  475. pxNewTCB->xUsingStaticallyAllocatedStack = pdFALSE;
  476. }
  477. #endif /* portUSING_MPU_WRAPPERS == 1 */
  478. /* Calculate the top of stack address. This depends on whether the
  479. stack grows from high memory to low (as per the 80x86) or vice versa.
  480. portSTACK_GROWTH is used to make the result positive or negative as
  481. required by the port. */
  482. #if( portSTACK_GROWTH < 0 )
  483. {
  484. pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( uint16_t ) 1 );
  485. pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) ); /*lint !e923 MISRA exception. Avoiding casts between pointers and integers is not practical. Size differences accounted for using portPOINTER_SIZE_TYPE type. */
  486. /* Check the alignment of the calculated top of stack is correct. */
  487. configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
  488. }
  489. #else /* portSTACK_GROWTH */
  490. {
  491. pxTopOfStack = pxNewTCB->pxStack;
  492. /* Check the alignment of the stack buffer is correct. */
  493. configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
  494. /* If we want to use stack checking on architectures that use
  495. a positive stack growth direction then we also need to store the
  496. other extreme of the stack space. */
  497. pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );
  498. }
  499. #endif /* portSTACK_GROWTH */
  500. /* Setup the newly allocated TCB with the initial state of the task. */
  501. prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );
  502. /* Initialize the TCB stack to look as if the task was already running,
  503. but had been interrupted by the scheduler. The return address is set
  504. to the start of the task function. Once the stack has been initialised
  505. the top of stack variable is updated. */
  506. #if( portUSING_MPU_WRAPPERS == 1 )
  507. {
  508. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
  509. }
  510. #else /* portUSING_MPU_WRAPPERS */
  511. {
  512. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
  513. }
  514. #endif /* portUSING_MPU_WRAPPERS */
  515. if( ( void * ) pxCreatedTask != NULL )
  516. {
  517. /* Pass the TCB out - in an anonymous way. The calling function/
  518. task can use this as a handle to delete the task later if
  519. required.*/
  520. *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
  521. }
  522. else
  523. {
  524. mtCOVERAGE_TEST_MARKER();
  525. }
  526. /* Ensure interrupts don't access the task lists while they are being
  527. updated. */
  528. taskENTER_CRITICAL();
  529. {
  530. uxCurrentNumberOfTasks++;
  531. if( pxCurrentTCB == NULL )
  532. {
  533. /* There are no other tasks, or all the other tasks are in
  534. the suspended state - make this the current task. */
  535. pxCurrentTCB = pxNewTCB;
  536. if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
  537. {
  538. /* This is the first task to be created so do the preliminary
  539. initialisation required. We will not recover if this call
  540. fails, but we will report the failure. */
  541. prvInitialiseTaskLists();
  542. }
  543. else
  544. {
  545. mtCOVERAGE_TEST_MARKER();
  546. }
  547. }
  548. else
  549. {
  550. /* If the scheduler is not already running, make this task the
  551. current task if it is the highest priority task to be created
  552. so far. */
  553. if( xSchedulerRunning == pdFALSE )
  554. {
  555. if( pxCurrentTCB->uxPriority <= uxPriority )
  556. {
  557. pxCurrentTCB = pxNewTCB;
  558. }
  559. else
  560. {
  561. mtCOVERAGE_TEST_MARKER();
  562. }
  563. }
  564. else
  565. {
  566. mtCOVERAGE_TEST_MARKER();
  567. }
  568. }
  569. uxTaskNumber++;
  570. #if ( configUSE_TRACE_FACILITY == 1 )
  571. {
  572. /* Add a counter into the TCB for tracing only. */
  573. pxNewTCB->uxTCBNumber = uxTaskNumber;
  574. }
  575. #endif /* configUSE_TRACE_FACILITY */
  576. traceTASK_CREATE( pxNewTCB );
  577. prvAddTaskToReadyList( pxNewTCB );
  578. xReturn = pdPASS;
  579. portSETUP_TCB( pxNewTCB );
  580. }
  581. taskEXIT_CRITICAL();
  582. }
  583. else
  584. {
  585. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  586. traceTASK_CREATE_FAILED();
  587. }
  588. if( xReturn == pdPASS )
  589. {
  590. if( xSchedulerRunning != pdFALSE )
  591. {
  592. /* If the created task is of a higher priority than the current task
  593. then it should run now. */
  594. if( pxCurrentTCB->uxPriority < uxPriority )
  595. {
  596. taskYIELD_IF_USING_PREEMPTION();
  597. }
  598. else
  599. {
  600. mtCOVERAGE_TEST_MARKER();
  601. }
  602. }
  603. else
  604. {
  605. mtCOVERAGE_TEST_MARKER();
  606. }
  607. }
  608. return xReturn;
  609. }
  610. /*-----------------------------------------------------------*/
  611. #if ( INCLUDE_vTaskDelete == 1 )
  612. void vTaskDelete( TaskHandle_t xTaskToDelete )
  613. {
  614. TCB_t *pxTCB;
  615. taskENTER_CRITICAL();
  616. {
  617. /* If null is passed in here then it is the calling task that is
  618. being deleted. */
  619. pxTCB = prvGetTCBFromHandle( xTaskToDelete );
  620. /* Remove task from the ready list and place in the termination list.
  621. This will stop the task from be scheduled. The idle task will check
  622. the termination list and free up any memory allocated by the
  623. scheduler for the TCB and stack. */
  624. if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  625. {
  626. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  627. }
  628. else
  629. {
  630. mtCOVERAGE_TEST_MARKER();
  631. }
  632. /* Is the task waiting on an event also? */
  633. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  634. {
  635. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  636. }
  637. else
  638. {
  639. mtCOVERAGE_TEST_MARKER();
  640. }
  641. vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
  642. /* Increment the ucTasksDeleted variable so the idle task knows
  643. there is a task that has been deleted and that it should therefore
  644. check the xTasksWaitingTermination list. */
  645. ++uxTasksDeleted;
  646. /* Increment the uxTaskNumberVariable also so kernel aware debuggers
  647. can detect that the task lists need re-generating. */
  648. uxTaskNumber++;
  649. traceTASK_DELETE( pxTCB );
  650. }
  651. taskEXIT_CRITICAL();
  652. /* Force a reschedule if it is the currently running task that has just
  653. been deleted. */
  654. if( xSchedulerRunning != pdFALSE )
  655. {
  656. if( pxTCB == pxCurrentTCB )
  657. {
  658. configASSERT( uxSchedulerSuspended == 0 );
  659. /* The pre-delete hook is primarily for the Windows simulator,
  660. in which Windows specific clean up operations are performed,
  661. after which it is not possible to yield away from this task -
  662. hence xYieldPending is used to latch that a context switch is
  663. required. */
  664. portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
  665. portYIELD_WITHIN_API();
  666. }
  667. else
  668. {
  669. /* Reset the next expected unblock time in case it referred to
  670. the task that has just been deleted. */
  671. taskENTER_CRITICAL();
  672. {
  673. prvResetNextTaskUnblockTime();
  674. }
  675. taskEXIT_CRITICAL();
  676. }
  677. }
  678. }
  679. #endif /* INCLUDE_vTaskDelete */
  680. /*-----------------------------------------------------------*/
  681. #if ( INCLUDE_vTaskDelayUntil == 1 )
  682. void vTaskDelayUntil( TickType_t * const pxPreviousWakeTime, const TickType_t xTimeIncrement )
  683. {
  684. TickType_t xTimeToWake;
  685. BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
  686. configASSERT( pxPreviousWakeTime );
  687. configASSERT( ( xTimeIncrement > 0U ) );
  688. configASSERT( uxSchedulerSuspended == 0 );
  689. vTaskSuspendAll();
  690. {
  691. /* Minor optimisation. The tick count cannot change in this
  692. block. */
  693. const TickType_t xConstTickCount = xTickCount;
  694. /* Generate the tick time at which the task wants to wake. */
  695. xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
  696. if( xConstTickCount < *pxPreviousWakeTime )
  697. {
  698. /* The tick count has overflowed since this function was
  699. lasted called. In this case the only time we should ever
  700. actually delay is if the wake time has also overflowed,
  701. and the wake time is greater than the tick time. When this
  702. is the case it is as if neither time had overflowed. */
  703. if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
  704. {
  705. xShouldDelay = pdTRUE;
  706. }
  707. else
  708. {
  709. mtCOVERAGE_TEST_MARKER();
  710. }
  711. }
  712. else
  713. {
  714. /* The tick time has not overflowed. In this case we will
  715. delay if either the wake time has overflowed, and/or the
  716. tick time is less than the wake time. */
  717. if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
  718. {
  719. xShouldDelay = pdTRUE;
  720. }
  721. else
  722. {
  723. mtCOVERAGE_TEST_MARKER();
  724. }
  725. }
  726. /* Update the wake time ready for the next call. */
  727. *pxPreviousWakeTime = xTimeToWake;
  728. if( xShouldDelay != pdFALSE )
  729. {
  730. traceTASK_DELAY_UNTIL();
  731. /* Remove the task from the ready list before adding it to the
  732. blocked list as the same list item is used for both lists. */
  733. if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  734. {
  735. /* The current task must be in a ready list, so there is
  736. no need to check, and the port reset macro can be called
  737. directly. */
  738. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  739. }
  740. else
  741. {
  742. mtCOVERAGE_TEST_MARKER();
  743. }
  744. prvAddCurrentTaskToDelayedList( xTimeToWake );
  745. }
  746. else
  747. {
  748. mtCOVERAGE_TEST_MARKER();
  749. }
  750. }
  751. xAlreadyYielded = xTaskResumeAll();
  752. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  753. have put ourselves to sleep. */
  754. if( xAlreadyYielded == pdFALSE )
  755. {
  756. portYIELD_WITHIN_API();
  757. }
  758. else
  759. {
  760. mtCOVERAGE_TEST_MARKER();
  761. }
  762. }
  763. #endif /* INCLUDE_vTaskDelayUntil */
  764. /*-----------------------------------------------------------*/
  765. #if ( INCLUDE_vTaskDelay == 1 )
  766. void vTaskDelay( const TickType_t xTicksToDelay )
  767. {
  768. TickType_t xTimeToWake;
  769. BaseType_t xAlreadyYielded = pdFALSE;
  770. /* A delay time of zero just forces a reschedule. */
  771. if( xTicksToDelay > ( TickType_t ) 0U )
  772. {
  773. configASSERT( uxSchedulerSuspended == 0 );
  774. vTaskSuspendAll();
  775. {
  776. traceTASK_DELAY();
  777. /* A task that is removed from the event list while the
  778. scheduler is suspended will not get placed in the ready
  779. list or removed from the blocked list until the scheduler
  780. is resumed.
  781. This task cannot be in an event list as it is the currently
  782. executing task. */
  783. /* Calculate the time to wake - this may overflow but this is
  784. not a problem. */
  785. xTimeToWake = xTickCount + xTicksToDelay;
  786. /* We must remove ourselves from the ready list before adding
  787. ourselves to the blocked list as the same list item is used for
  788. both lists. */
  789. if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  790. {
  791. /* The current task must be in a ready list, so there is
  792. no need to check, and the port reset macro can be called
  793. directly. */
  794. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  795. }
  796. else
  797. {
  798. mtCOVERAGE_TEST_MARKER();
  799. }
  800. prvAddCurrentTaskToDelayedList( xTimeToWake );
  801. }
  802. xAlreadyYielded = xTaskResumeAll();
  803. }
  804. else
  805. {
  806. mtCOVERAGE_TEST_MARKER();
  807. }
  808. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  809. have put ourselves to sleep. */
  810. if( xAlreadyYielded == pdFALSE )
  811. {
  812. portYIELD_WITHIN_API();
  813. }
  814. else
  815. {
  816. mtCOVERAGE_TEST_MARKER();
  817. }
  818. }
  819. #endif /* INCLUDE_vTaskDelay */
  820. /*-----------------------------------------------------------*/
  821. #if ( INCLUDE_eTaskGetState == 1 )
  822. eTaskState eTaskGetState( TaskHandle_t xTask )
  823. {
  824. eTaskState eReturn;
  825. List_t *pxStateList;
  826. const TCB_t * const pxTCB = ( TCB_t * ) xTask;
  827. configASSERT( pxTCB );
  828. if( pxTCB == pxCurrentTCB )
  829. {
  830. /* The task calling this function is querying its own state. */
  831. eReturn = eRunning;
  832. }
  833. else
  834. {
  835. taskENTER_CRITICAL();
  836. {
  837. pxStateList = ( List_t * ) listLIST_ITEM_CONTAINER( &( pxTCB->xGenericListItem ) );
  838. }
  839. taskEXIT_CRITICAL();
  840. if( ( pxStateList == pxDelayedTaskList ) || ( pxStateList == pxOverflowDelayedTaskList ) )
  841. {
  842. /* The task being queried is referenced from one of the Blocked
  843. lists. */
  844. eReturn = eBlocked;
  845. }
  846. #if ( INCLUDE_vTaskSuspend == 1 )
  847. else if( pxStateList == &xSuspendedTaskList )
  848. {
  849. /* The task being queried is referenced from the suspended
  850. list. Is it genuinely suspended or is it block
  851. indefinitely? */
  852. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
  853. {
  854. eReturn = eSuspended;
  855. }
  856. else
  857. {
  858. eReturn = eBlocked;
  859. }
  860. }
  861. #endif
  862. #if ( INCLUDE_vTaskDelete == 1 )
  863. else if( pxStateList == &xTasksWaitingTermination )
  864. {
  865. /* The task being queried is referenced from the deleted
  866. tasks list. */
  867. eReturn = eDeleted;
  868. }
  869. #endif
  870. else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
  871. {
  872. /* If the task is not in any other state, it must be in the
  873. Ready (including pending ready) state. */
  874. eReturn = eReady;
  875. }
  876. }
  877. return eReturn;
  878. } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
  879. #endif /* INCLUDE_eTaskGetState */
  880. /*-----------------------------------------------------------*/
  881. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  882. UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask )
  883. {
  884. TCB_t *pxTCB;
  885. UBaseType_t uxReturn;
  886. taskENTER_CRITICAL();
  887. {
  888. /* If null is passed in here then we are changing the
  889. priority of the calling function. */
  890. pxTCB = prvGetTCBFromHandle( xTask );
  891. uxReturn = pxTCB->uxPriority;
  892. }
  893. taskEXIT_CRITICAL();
  894. return uxReturn;
  895. }
  896. #endif /* INCLUDE_uxTaskPriorityGet */
  897. /*-----------------------------------------------------------*/
  898. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  899. UBaseType_t uxTaskPriorityGetFromISR( TaskHandle_t xTask )
  900. {
  901. TCB_t *pxTCB;
  902. UBaseType_t uxReturn, uxSavedInterruptState;
  903. /* RTOS ports that support interrupt nesting have the concept of a
  904. maximum system call (or maximum API call) interrupt priority.
  905. Interrupts that are above the maximum system call priority are keep
  906. permanently enabled, even when the RTOS kernel is in a critical section,
  907. but cannot make any calls to FreeRTOS API functions. If configASSERT()
  908. is defined in FreeRTOSConfig.h then
  909. portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  910. failure if a FreeRTOS API function is called from an interrupt that has
  911. been assigned a priority above the configured maximum system call
  912. priority. Only FreeRTOS functions that end in FromISR can be called
  913. from interrupts that have been assigned a priority at or (logically)
  914. below the maximum system call interrupt priority. FreeRTOS maintains a
  915. separate interrupt safe API to ensure interrupt entry is as fast and as
  916. simple as possible. More information (albeit Cortex-M specific) is
  917. provided on the following link:
  918. http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  919. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  920. uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
  921. {
  922. /* If null is passed in here then it is the priority of the calling
  923. task that is being queried. */
  924. pxTCB = prvGetTCBFromHandle( xTask );
  925. uxReturn = pxTCB->uxPriority;
  926. }
  927. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
  928. return uxReturn;
  929. }
  930. #endif /* INCLUDE_uxTaskPriorityGet */
  931. /*-----------------------------------------------------------*/
  932. #if ( INCLUDE_vTaskPrioritySet == 1 )
  933. void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority )
  934. {
  935. TCB_t *pxTCB;
  936. UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
  937. BaseType_t xYieldRequired = pdFALSE;
  938. configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
  939. /* Ensure the new priority is valid. */
  940. if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  941. {
  942. uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  943. }
  944. else
  945. {
  946. mtCOVERAGE_TEST_MARKER();
  947. }
  948. taskENTER_CRITICAL();
  949. {
  950. /* If null is passed in here then it is the priority of the calling
  951. task that is being changed. */
  952. pxTCB = prvGetTCBFromHandle( xTask );
  953. traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
  954. #if ( configUSE_MUTEXES == 1 )
  955. {
  956. uxCurrentBasePriority = pxTCB->uxBasePriority;
  957. }
  958. #else
  959. {
  960. uxCurrentBasePriority = pxTCB->uxPriority;
  961. }
  962. #endif
  963. if( uxCurrentBasePriority != uxNewPriority )
  964. {
  965. /* The priority change may have readied a task of higher
  966. priority than the calling task. */
  967. if( uxNewPriority > uxCurrentBasePriority )
  968. {
  969. if( pxTCB != pxCurrentTCB )
  970. {
  971. /* The priority of a task other than the currently
  972. running task is being raised. Is the priority being
  973. raised above that of the running task? */
  974. if( uxNewPriority >= pxCurrentTCB->uxPriority )
  975. {
  976. xYieldRequired = pdTRUE;
  977. }
  978. else
  979. {
  980. mtCOVERAGE_TEST_MARKER();
  981. }
  982. }
  983. else
  984. {
  985. /* The priority of the running task is being raised,
  986. but the running task must already be the highest
  987. priority task able to run so no yield is required. */
  988. }
  989. }
  990. else if( pxTCB == pxCurrentTCB )
  991. {
  992. /* Setting the priority of the running task down means
  993. there may now be another task of higher priority that
  994. is ready to execute. */
  995. xYieldRequired = pdTRUE;
  996. }
  997. else
  998. {
  999. /* Setting the priority of any other task down does not
  1000. require a yield as the running task must be above the
  1001. new priority of the task being modified. */
  1002. }
  1003. /* Remember the ready list the task might be referenced from
  1004. before its uxPriority member is changed so the
  1005. taskRESET_READY_PRIORITY() macro can function correctly. */
  1006. uxPriorityUsedOnEntry = pxTCB->uxPriority;
  1007. #if ( configUSE_MUTEXES == 1 )
  1008. {
  1009. /* Only change the priority being used if the task is not
  1010. currently using an inherited priority. */
  1011. if( pxTCB->uxBasePriority == pxTCB->uxPriority )
  1012. {
  1013. pxTCB->uxPriority = uxNewPriority;
  1014. }
  1015. else
  1016. {
  1017. mtCOVERAGE_TEST_MARKER();
  1018. }
  1019. /* The base priority gets set whatever. */
  1020. pxTCB->uxBasePriority = uxNewPriority;
  1021. }
  1022. #else
  1023. {
  1024. pxTCB->uxPriority = uxNewPriority;
  1025. }
  1026. #endif
  1027. /* Only reset the event list item value if the value is not
  1028. being used for anything else. */
  1029. if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
  1030. {
  1031. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  1032. }
  1033. else
  1034. {
  1035. mtCOVERAGE_TEST_MARKER();
  1036. }
  1037. /* If the task is in the blocked or suspended list we need do
  1038. nothing more than change it's priority variable. However, if
  1039. the task is in a ready list it needs to be removed and placed
  1040. in the list appropriate to its new priority. */
  1041. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )
  1042. {
  1043. /* The task is currently in its ready list - remove before adding
  1044. it to it's new ready list. As we are in a critical section we
  1045. can do this even if the scheduler is suspended. */
  1046. if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  1047. {
  1048. /* It is known that the task is in its ready list so
  1049. there is no need to check again and the port level
  1050. reset macro can be called directly. */
  1051. portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
  1052. }
  1053. else
  1054. {
  1055. mtCOVERAGE_TEST_MARKER();
  1056. }
  1057. prvAddTaskToReadyList( pxTCB );
  1058. }
  1059. else
  1060. {
  1061. mtCOVERAGE_TEST_MARKER();
  1062. }
  1063. if( xYieldRequired == pdTRUE )
  1064. {
  1065. taskYIELD_IF_USING_PREEMPTION();
  1066. }
  1067. else
  1068. {
  1069. mtCOVERAGE_TEST_MARKER();
  1070. }
  1071. /* Remove compiler warning about unused variables when the port
  1072. optimised task selection is not being used. */
  1073. ( void ) uxPriorityUsedOnEntry;
  1074. }
  1075. }
  1076. taskEXIT_CRITICAL();
  1077. }
  1078. #endif /* INCLUDE_vTaskPrioritySet */
  1079. /*-----------------------------------------------------------*/
  1080. #if ( INCLUDE_vTaskSuspend == 1 )
  1081. void vTaskSuspend( TaskHandle_t xTaskToSuspend )
  1082. {
  1083. TCB_t *pxTCB;
  1084. taskENTER_CRITICAL();
  1085. {
  1086. /* If null is passed in here then it is the running task that is
  1087. being suspended. */
  1088. pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
  1089. traceTASK_SUSPEND( pxTCB );
  1090. /* Remove task from the ready/delayed list and place in the
  1091. suspended list. */
  1092. if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  1093. {
  1094. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  1095. }
  1096. else
  1097. {
  1098. mtCOVERAGE_TEST_MARKER();
  1099. }
  1100. /* Is the task waiting on an event also? */
  1101. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  1102. {
  1103. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  1104. }
  1105. else
  1106. {
  1107. mtCOVERAGE_TEST_MARKER();
  1108. }
  1109. vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
  1110. }
  1111. taskEXIT_CRITICAL();
  1112. if( pxTCB == pxCurrentTCB )
  1113. {
  1114. if( xSchedulerRunning != pdFALSE )
  1115. {
  1116. /* The current task has just been suspended. */
  1117. configASSERT( uxSchedulerSuspended == 0 );
  1118. portYIELD_WITHIN_API();
  1119. }
  1120. else
  1121. {
  1122. /* The scheduler is not running, but the task that was pointed
  1123. to by pxCurrentTCB has just been suspended and pxCurrentTCB
  1124. must be adjusted to point to a different task. */
  1125. if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks )
  1126. {
  1127. /* No other tasks are ready, so set pxCurrentTCB back to
  1128. NULL so when the next task is created pxCurrentTCB will
  1129. be set to point to it no matter what its relative priority
  1130. is. */
  1131. pxCurrentTCB = NULL;
  1132. }
  1133. else
  1134. {
  1135. vTaskSwitchContext();
  1136. }
  1137. }
  1138. }
  1139. else
  1140. {
  1141. if( xSchedulerRunning != pdFALSE )
  1142. {
  1143. /* A task other than the currently running task was suspended,
  1144. reset the next expected unblock time in case it referred to the
  1145. task that is now in the Suspended state. */
  1146. taskENTER_CRITICAL();
  1147. {
  1148. prvResetNextTaskUnblockTime();
  1149. }
  1150. taskEXIT_CRITICAL();
  1151. }
  1152. else
  1153. {
  1154. mtCOVERAGE_TEST_MARKER();
  1155. }
  1156. }
  1157. }
  1158. #endif /* INCLUDE_vTaskSuspend */
  1159. /*-----------------------------------------------------------*/
  1160. #if ( INCLUDE_vTaskSuspend == 1 )
  1161. static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
  1162. {
  1163. BaseType_t xReturn = pdFALSE;
  1164. const TCB_t * const pxTCB = ( TCB_t * ) xTask;
  1165. /* Accesses xPendingReadyList so must be called from a critical
  1166. section. */
  1167. /* It does not make sense to check if the calling task is suspended. */
  1168. configASSERT( xTask );
  1169. /* Is the task being resumed actually in the suspended list? */
  1170. if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
  1171. {
  1172. /* Has the task already been resumed from within an ISR? */
  1173. if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
  1174. {
  1175. /* Is it in the suspended list because it is in the Suspended
  1176. state, or because is is blocked with no timeout? */
  1177. if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
  1178. {
  1179. xReturn = pdTRUE;
  1180. }
  1181. else
  1182. {
  1183. mtCOVERAGE_TEST_MARKER();
  1184. }
  1185. }
  1186. else
  1187. {
  1188. mtCOVERAGE_TEST_MARKER();
  1189. }
  1190. }
  1191. else
  1192. {
  1193. mtCOVERAGE_TEST_MARKER();
  1194. }
  1195. return xReturn;
  1196. } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
  1197. #endif /* INCLUDE_vTaskSuspend */
  1198. /*-----------------------------------------------------------*/
  1199. #if ( INCLUDE_vTaskSuspend == 1 )
  1200. void vTaskResume( TaskHandle_t xTaskToResume )
  1201. {
  1202. TCB_t * const pxTCB = ( TCB_t * ) xTaskToResume;
  1203. /* It does not make sense to resume the calling task. */
  1204. configASSERT( xTaskToResume );
  1205. /* The parameter cannot be NULL as it is impossible to resume the
  1206. currently executing task. */
  1207. if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )
  1208. {
  1209. taskENTER_CRITICAL();
  1210. {
  1211. if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )
  1212. {
  1213. traceTASK_RESUME( pxTCB );
  1214. /* As we are in a critical section we can access the ready
  1215. lists even if the scheduler is suspended. */
  1216. ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
  1217. prvAddTaskToReadyList( pxTCB );
  1218. /* We may have just resumed a higher priority task. */
  1219. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1220. {
  1221. /* This yield may not cause the task just resumed to run,
  1222. but will leave the lists in the correct state for the
  1223. next yield. */
  1224. taskYIELD_IF_USING_PREEMPTION();
  1225. }
  1226. else
  1227. {
  1228. mtCOVERAGE_TEST_MARKER();
  1229. }
  1230. }
  1231. else
  1232. {
  1233. mtCOVERAGE_TEST_MARKER();
  1234. }
  1235. }
  1236. taskEXIT_CRITICAL();
  1237. }
  1238. else
  1239. {
  1240. mtCOVERAGE_TEST_MARKER();
  1241. }
  1242. }
  1243. #endif /* INCLUDE_vTaskSuspend */
  1244. /*-----------------------------------------------------------*/
  1245. #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
  1246. BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
  1247. {
  1248. BaseType_t xYieldRequired = pdFALSE;
  1249. TCB_t * const pxTCB = ( TCB_t * ) xTaskToResume;
  1250. UBaseType_t uxSavedInterruptStatus;
  1251. configASSERT( xTaskToResume );
  1252. /* RTOS ports that support interrupt nesting have the concept of a
  1253. maximum system call (or maximum API call) interrupt priority.
  1254. Interrupts that are above the maximum system call priority are keep
  1255. permanently enabled, even when the RTOS kernel is in a critical section,
  1256. but cannot make any calls to FreeRTOS API functions. If configASSERT()
  1257. is defined in FreeRTOSConfig.h then
  1258. portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1259. failure if a FreeRTOS API function is called from an interrupt that has
  1260. been assigned a priority above the configured maximum system call
  1261. priority. Only FreeRTOS functions that end in FromISR can be called
  1262. from interrupts that have been assigned a priority at or (logically)
  1263. below the maximum system call interrupt priority. FreeRTOS maintains a
  1264. separate interrupt safe API to ensure interrupt entry is as fast and as
  1265. simple as possible. More information (albeit Cortex-M specific) is
  1266. provided on the following link:
  1267. http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1268. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1269. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1270. {
  1271. if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )
  1272. {
  1273. traceTASK_RESUME_FROM_ISR( pxTCB );
  1274. /* Check the ready lists can be accessed. */
  1275. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  1276. {
  1277. /* Ready lists can be accessed so move the task from the
  1278. suspended list to the ready list directly. */
  1279. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1280. {
  1281. xYieldRequired = pdTRUE;
  1282. }
  1283. else
  1284. {
  1285. mtCOVERAGE_TEST_MARKER();
  1286. }
  1287. ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
  1288. prvAddTaskToReadyList( pxTCB );
  1289. }
  1290. else
  1291. {
  1292. /* The delayed or ready lists cannot be accessed so the task
  1293. is held in the pending ready list until the scheduler is
  1294. unsuspended. */
  1295. vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  1296. }
  1297. }
  1298. else
  1299. {
  1300. mtCOVERAGE_TEST_MARKER();
  1301. }
  1302. }
  1303. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1304. return xYieldRequired;
  1305. }
  1306. #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
  1307. /*-----------------------------------------------------------*/
  1308. void vTaskStartScheduler( void )
  1309. {
  1310. BaseType_t xReturn;
  1311. /* Add the idle task at the lowest priority. */
  1312. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  1313. {
  1314. /* Create the idle task, storing its handle in xIdleTaskHandle so it can
  1315. be returned by the xTaskGetIdleTaskHandle() function. */
  1316. xReturn = xTaskCreate( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
  1317. }
  1318. #else
  1319. {
  1320. /* Create the idle task without storing its handle. */
  1321. xReturn = xTaskCreate( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
  1322. }
  1323. #endif /* INCLUDE_xTaskGetIdleTaskHandle */
  1324. #if ( configUSE_TIMERS == 1 )
  1325. {
  1326. if( xReturn == pdPASS )
  1327. {
  1328. xReturn = xTimerCreateTimerTask();
  1329. }
  1330. else
  1331. {
  1332. mtCOVERAGE_TEST_MARKER();
  1333. }
  1334. }
  1335. #endif /* configUSE_TIMERS */
  1336. if( xReturn == pdPASS )
  1337. {
  1338. /* Interrupts are turned off here, to ensure a tick does not occur
  1339. before or during the call to xPortStartScheduler(). The stacks of
  1340. the created tasks contain a status word with interrupts switched on
  1341. so interrupts will automatically get re-enabled when the first task
  1342. starts to run. */
  1343. portDISABLE_INTERRUPTS();
  1344. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  1345. {
  1346. /* Switch Newlib's _impure_ptr variable to point to the _reent
  1347. structure specific to the task that will run first. */
  1348. _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
  1349. }
  1350. #endif /* configUSE_NEWLIB_REENTRANT */
  1351. xSchedulerRunning = pdTRUE;
  1352. xTickCount = ( TickType_t ) 0U;
  1353. /* If configGENERATE_RUN_TIME_STATS is defined then the following
  1354. macro must be defined to configure the timer/counter used to generate
  1355. the run time counter time base. */
  1356. portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
  1357. /* Setting up the timer tick is hardware specific and thus in the
  1358. portable interface. */
  1359. if( xPortStartScheduler() != pdFALSE )
  1360. {
  1361. /* Should not reach here as if the scheduler is running the
  1362. function will not return. */
  1363. }
  1364. else
  1365. {
  1366. /* Should only reach here if a task calls xTaskEndScheduler(). */
  1367. }
  1368. }
  1369. else
  1370. {
  1371. /* This line will only be reached if the kernel could not be started,
  1372. because there was not enough FreeRTOS heap to create the idle task
  1373. or the timer task. */
  1374. configASSERT( xReturn );
  1375. }
  1376. }
  1377. /*-----------------------------------------------------------*/
  1378. void vTaskEndScheduler( void )
  1379. {
  1380. /* Stop the scheduler interrupts and call the portable scheduler end
  1381. routine so the original ISRs can be restored if necessary. The port
  1382. layer must ensure interrupts enable bit is left in the correct state. */
  1383. portDISABLE_INTERRUPTS();
  1384. xSchedulerRunning = pdFALSE;
  1385. vPortEndScheduler();
  1386. }
  1387. /*----------------------------------------------------------*/
  1388. void vTaskSuspendAll( void )
  1389. {
  1390. /* A critical section is not required as the variable is of type
  1391. BaseType_t. Please read Richard Barry's reply in the following link to a
  1392. post in the FreeRTOS support forum before reporting this as a bug! -
  1393. http://goo.gl/wu4acr */
  1394. ++uxSchedulerSuspended;
  1395. }
  1396. /*----------------------------------------------------------*/
  1397. #if ( configUSE_TICKLESS_IDLE != 0 )
  1398. static TickType_t prvGetExpectedIdleTime( void )
  1399. {
  1400. TickType_t xReturn;
  1401. if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
  1402. {
  1403. xReturn = 0;
  1404. }
  1405. else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
  1406. {
  1407. /* There are other idle priority tasks in the ready state. If
  1408. time slicing is used then the very next tick interrupt must be
  1409. processed. */
  1410. xReturn = 0;
  1411. }
  1412. else
  1413. {
  1414. xReturn = xNextTaskUnblockTime - xTickCount;
  1415. }
  1416. return xReturn;
  1417. }
  1418. #endif /* configUSE_TICKLESS_IDLE */
  1419. /*----------------------------------------------------------*/
  1420. BaseType_t xTaskResumeAll( void )
  1421. {
  1422. TCB_t *pxTCB;
  1423. BaseType_t xAlreadyYielded = pdFALSE;
  1424. /* If uxSchedulerSuspended is zero then this function does not match a
  1425. previous call to vTaskSuspendAll(). */
  1426. configASSERT( uxSchedulerSuspended );
  1427. /* It is possible that an ISR caused a task to be removed from an event
  1428. list while the scheduler was suspended. If this was the case then the
  1429. removed task will have been added to the xPendingReadyList. Once the
  1430. scheduler has been resumed it is safe to move all the pending ready
  1431. tasks from this list into their appropriate ready list. */
  1432. taskENTER_CRITICAL();
  1433. {
  1434. --uxSchedulerSuspended;
  1435. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  1436. {
  1437. if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
  1438. {
  1439. /* Move any readied tasks from the pending list into the
  1440. appropriate ready list. */
  1441. while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
  1442. {
  1443. pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
  1444. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  1445. ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
  1446. prvAddTaskToReadyList( pxTCB );
  1447. /* If the moved task has a priority higher than the current
  1448. task then a yield must be performed. */
  1449. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1450. {
  1451. xYieldPending = pdTRUE;
  1452. }
  1453. else
  1454. {
  1455. mtCOVERAGE_TEST_MARKER();
  1456. }
  1457. }
  1458. /* If any ticks occurred while the scheduler was suspended then
  1459. they should be processed now. This ensures the tick count does
  1460. not slip, and that any delayed tasks are resumed at the correct
  1461. time. */
  1462. if( uxPendedTicks > ( UBaseType_t ) 0U )
  1463. {
  1464. while( uxPendedTicks > ( UBaseType_t ) 0U )
  1465. {
  1466. if( xTaskIncrementTick() != pdFALSE )
  1467. {
  1468. xYieldPending = pdTRUE;
  1469. }
  1470. else
  1471. {
  1472. mtCOVERAGE_TEST_MARKER();
  1473. }
  1474. --uxPendedTicks;
  1475. }
  1476. }
  1477. else
  1478. {
  1479. mtCOVERAGE_TEST_MARKER();
  1480. }
  1481. if( xYieldPending == pdTRUE )
  1482. {
  1483. #if( configUSE_PREEMPTION != 0 )
  1484. {
  1485. xAlreadyYielded = pdTRUE;
  1486. }
  1487. #endif
  1488. taskYIELD_IF_USING_PREEMPTION();
  1489. }
  1490. else
  1491. {
  1492. mtCOVERAGE_TEST_MARKER();
  1493. }
  1494. }
  1495. }
  1496. else
  1497. {
  1498. mtCOVERAGE_TEST_MARKER();
  1499. }
  1500. }
  1501. taskEXIT_CRITICAL();
  1502. return xAlreadyYielded;
  1503. }
  1504. /*-----------------------------------------------------------*/
  1505. TickType_t xTaskGetTickCount( void )
  1506. {
  1507. TickType_t xTicks;
  1508. /* Critical section required if running on a 16 bit processor. */
  1509. portTICK_TYPE_ENTER_CRITICAL();
  1510. {
  1511. xTicks = xTickCount;
  1512. }
  1513. portTICK_TYPE_EXIT_CRITICAL();
  1514. return xTicks;
  1515. }
  1516. /*-----------------------------------------------------------*/
  1517. TickType_t xTaskGetTickCountFromISR( void )
  1518. {
  1519. TickType_t xReturn;
  1520. UBaseType_t uxSavedInterruptStatus;
  1521. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1522. system call (or maximum API call) interrupt priority. Interrupts that are
  1523. above the maximum system call priority are kept permanently enabled, even
  1524. when the RTOS kernel is in a critical section, but cannot make any calls to
  1525. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1526. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1527. failure if a FreeRTOS API function is called from an interrupt that has been
  1528. assigned a priority above the configured maximum system call priority.
  1529. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1530. that have been assigned a priority at or (logically) below the maximum
  1531. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1532. safe API to ensure interrupt entry is as fast and as simple as possible.
  1533. More information (albeit Cortex-M specific) is provided on the following
  1534. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1535. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1536. uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
  1537. {
  1538. xReturn = xTickCount;
  1539. }
  1540. portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1541. return xReturn;
  1542. }
  1543. /*-----------------------------------------------------------*/
  1544. UBaseType_t uxTaskGetNumberOfTasks( void )
  1545. {
  1546. /* A critical section is not required because the variables are of type
  1547. BaseType_t. */
  1548. return uxCurrentNumberOfTasks;
  1549. }
  1550. /*-----------------------------------------------------------*/
  1551. #if ( INCLUDE_pcTaskGetTaskName == 1 )
  1552. char *pcTaskGetTaskName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  1553. {
  1554. TCB_t *pxTCB;
  1555. /* If null is passed in here then the name of the calling task is being queried. */
  1556. pxTCB = prvGetTCBFromHandle( xTaskToQuery );
  1557. configASSERT( pxTCB );
  1558. return &( pxTCB->pcTaskName[ 0 ] );
  1559. }
  1560. #endif /* INCLUDE_pcTaskGetTaskName */
  1561. /*-----------------------------------------------------------*/
  1562. #if ( configUSE_TRACE_FACILITY == 1 )
  1563. UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray, const UBaseType_t uxArraySize, uint32_t * const pulTotalRunTime )
  1564. {
  1565. UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
  1566. vTaskSuspendAll();
  1567. {
  1568. /* Is there a space in the array for each task in the system? */
  1569. if( uxArraySize >= uxCurrentNumberOfTasks )
  1570. {
  1571. /* Fill in an TaskStatus_t structure with information on each
  1572. task in the Ready state. */
  1573. do
  1574. {
  1575. uxQueue--;
  1576. uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
  1577. } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  1578. /* Fill in an TaskStatus_t structure with information on each
  1579. task in the Blocked state. */
  1580. uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
  1581. uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
  1582. #if( INCLUDE_vTaskDelete == 1 )
  1583. {
  1584. /* Fill in an TaskStatus_t structure with information on
  1585. each task that has been deleted but not yet cleaned up. */
  1586. uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
  1587. }
  1588. #endif
  1589. #if ( INCLUDE_vTaskSuspend == 1 )
  1590. {
  1591. /* Fill in an TaskStatus_t structure with information on
  1592. each task in the Suspended state. */
  1593. uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
  1594. }
  1595. #endif
  1596. #if ( configGENERATE_RUN_TIME_STATS == 1)
  1597. {
  1598. if( pulTotalRunTime != NULL )
  1599. {
  1600. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  1601. portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
  1602. #else
  1603. *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
  1604. #endif
  1605. }
  1606. }
  1607. #else
  1608. {
  1609. if( pulTotalRunTime != NULL )
  1610. {
  1611. *pulTotalRunTime = 0;
  1612. }
  1613. }
  1614. #endif
  1615. }
  1616. else
  1617. {
  1618. mtCOVERAGE_TEST_MARKER();
  1619. }
  1620. }
  1621. ( void ) xTaskResumeAll();
  1622. return uxTask;
  1623. }
  1624. #endif /* configUSE_TRACE_FACILITY */
  1625. /*----------------------------------------------------------*/
  1626. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  1627. TaskHandle_t xTaskGetIdleTaskHandle( void )
  1628. {
  1629. /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
  1630. started, then xIdleTaskHandle will be NULL. */
  1631. configASSERT( ( xIdleTaskHandle != NULL ) );
  1632. return xIdleTaskHandle;
  1633. }
  1634. #endif /* INCLUDE_xTaskGetIdleTaskHandle */
  1635. /*----------------------------------------------------------*/
  1636. /* This conditional compilation should use inequality to 0, not equality to 1.
  1637. This is to ensure vTaskStepTick() is available when user defined low power mode
  1638. implementations require configUSE_TICKLESS_IDLE to be set to a value other than
  1639. 1. */
  1640. #if ( configUSE_TICKLESS_IDLE != 0 )
  1641. void vTaskStepTick( const TickType_t xTicksToJump )
  1642. {
  1643. /* Correct the tick count value after a period during which the tick
  1644. was suppressed. Note this does *not* call the tick hook function for
  1645. each stepped tick. */
  1646. configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
  1647. xTickCount += xTicksToJump;
  1648. traceINCREASE_TICK_COUNT( xTicksToJump );
  1649. }
  1650. #endif /* configUSE_TICKLESS_IDLE */
  1651. /*----------------------------------------------------------*/
  1652. BaseType_t xTaskIncrementTick( void )
  1653. {
  1654. TCB_t * pxTCB;
  1655. TickType_t xItemValue;
  1656. BaseType_t xSwitchRequired = pdFALSE;
  1657. /* Called by the portable layer each time a tick interrupt occurs.
  1658. Increments the tick then checks to see if the new tick value will cause any
  1659. tasks to be unblocked. */
  1660. traceTASK_INCREMENT_TICK( xTickCount );
  1661. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  1662. {
  1663. /* Increment the RTOS tick, switching the delayed and overflowed
  1664. delayed lists if it wraps to 0. */
  1665. ++xTickCount;
  1666. {
  1667. /* Minor optimisation. The tick count cannot change in this
  1668. block. */
  1669. const TickType_t xConstTickCount = xTickCount;
  1670. if( xConstTickCount == ( TickType_t ) 0U )
  1671. {
  1672. taskSWITCH_DELAYED_LISTS();
  1673. }
  1674. else
  1675. {
  1676. mtCOVERAGE_TEST_MARKER();
  1677. }
  1678. /* See if this tick has made a timeout expire. Tasks are stored in
  1679. the queue in the order of their wake time - meaning once one task
  1680. has been found whose block time has not expired there is no need to
  1681. look any further down the list. */
  1682. if( xConstTickCount >= xNextTaskUnblockTime )
  1683. {
  1684. for( ;; )
  1685. {
  1686. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
  1687. {
  1688. /* The delayed list is empty. Set xNextTaskUnblockTime
  1689. to the maximum possible value so it is extremely
  1690. unlikely that the
  1691. if( xTickCount >= xNextTaskUnblockTime ) test will pass
  1692. next time through. */
  1693. xNextTaskUnblockTime = portMAX_DELAY;
  1694. break;
  1695. }
  1696. else
  1697. {
  1698. /* The delayed list is not empty, get the value of the
  1699. item at the head of the delayed list. This is the time
  1700. at which the task at the head of the delayed list must
  1701. be removed from the Blocked state. */
  1702. pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
  1703. xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );
  1704. if( xConstTickCount < xItemValue )
  1705. {
  1706. /* It is not time to unblock this item yet, but the
  1707. item value is the time at which the task at the head
  1708. of the blocked list must be removed from the Blocked
  1709. state - so record the item value in
  1710. xNextTaskUnblockTime. */
  1711. xNextTaskUnblockTime = xItemValue;
  1712. break;
  1713. }
  1714. else
  1715. {
  1716. mtCOVERAGE_TEST_MARKER();
  1717. }
  1718. /* It is time to remove the item from the Blocked state. */
  1719. ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
  1720. /* Is the task waiting on an event also? If so remove
  1721. it from the event list. */
  1722. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  1723. {
  1724. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  1725. }
  1726. else
  1727. {
  1728. mtCOVERAGE_TEST_MARKER();
  1729. }
  1730. /* Place the unblocked task into the appropriate ready
  1731. list. */
  1732. prvAddTaskToReadyList( pxTCB );
  1733. /* A task being unblocked cannot cause an immediate
  1734. context switch if preemption is turned off. */
  1735. #if ( configUSE_PREEMPTION == 1 )
  1736. {
  1737. /* Preemption is on, but a context switch should
  1738. only be performed if the unblocked task has a
  1739. priority that is equal to or higher than the
  1740. currently executing task. */
  1741. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1742. {
  1743. xSwitchRequired = pdTRUE;
  1744. }
  1745. else
  1746. {
  1747. mtCOVERAGE_TEST_MARKER();
  1748. }
  1749. }
  1750. #endif /* configUSE_PREEMPTION */
  1751. }
  1752. }
  1753. }
  1754. }
  1755. /* Tasks of equal priority to the currently running task will share
  1756. processing time (time slice) if preemption is on, and the application
  1757. writer has not explicitly turned time slicing off. */
  1758. #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
  1759. {
  1760. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
  1761. {
  1762. xSwitchRequired = pdTRUE;
  1763. }
  1764. else
  1765. {
  1766. mtCOVERAGE_TEST_MARKER();
  1767. }
  1768. }
  1769. #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
  1770. #if ( configUSE_TICK_HOOK == 1 )
  1771. {
  1772. /* Guard against the tick hook being called when the pended tick
  1773. count is being unwound (when the scheduler is being unlocked). */
  1774. if( uxPendedTicks == ( UBaseType_t ) 0U )
  1775. {
  1776. vApplicationTickHook();
  1777. }
  1778. else
  1779. {
  1780. mtCOVERAGE_TEST_MARKER();
  1781. }
  1782. }
  1783. #endif /* configUSE_TICK_HOOK */
  1784. }
  1785. else
  1786. {
  1787. ++uxPendedTicks;
  1788. /* The tick hook gets called at regular intervals, even if the
  1789. scheduler is locked. */
  1790. #if ( configUSE_TICK_HOOK == 1 )
  1791. {
  1792. vApplicationTickHook();
  1793. }
  1794. #endif
  1795. }
  1796. #if ( configUSE_PREEMPTION == 1 )
  1797. {
  1798. if( xYieldPending != pdFALSE )
  1799. {
  1800. xSwitchRequired = pdTRUE;
  1801. }
  1802. else
  1803. {
  1804. mtCOVERAGE_TEST_MARKER();
  1805. }
  1806. }
  1807. #endif /* configUSE_PREEMPTION */
  1808. return xSwitchRequired;
  1809. }
  1810. /*-----------------------------------------------------------*/
  1811. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  1812. void vTaskSetApplicationTaskTag( TaskHandle_t xTask, TaskHookFunction_t pxHookFunction )
  1813. {
  1814. TCB_t *xTCB;
  1815. /* If xTask is NULL then it is the task hook of the calling task that is
  1816. getting set. */
  1817. if( xTask == NULL )
  1818. {
  1819. xTCB = ( TCB_t * ) pxCurrentTCB;
  1820. }
  1821. else
  1822. {
  1823. xTCB = ( TCB_t * ) xTask;
  1824. }
  1825. /* Save the hook function in the TCB. A critical section is required as
  1826. the value can be accessed from an interrupt. */
  1827. taskENTER_CRITICAL();
  1828. xTCB->pxTaskTag = pxHookFunction;
  1829. taskEXIT_CRITICAL();
  1830. }
  1831. #endif /* configUSE_APPLICATION_TASK_TAG */
  1832. /*-----------------------------------------------------------*/
  1833. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  1834. TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
  1835. {
  1836. TCB_t *xTCB;
  1837. TaskHookFunction_t xReturn;
  1838. /* If xTask is NULL then we are setting our own task hook. */
  1839. if( xTask == NULL )
  1840. {
  1841. xTCB = ( TCB_t * ) pxCurrentTCB;
  1842. }
  1843. else
  1844. {
  1845. xTCB = ( TCB_t * ) xTask;
  1846. }
  1847. /* Save the hook function in the TCB. A critical section is required as
  1848. the value can be accessed from an interrupt. */
  1849. taskENTER_CRITICAL();
  1850. {
  1851. xReturn = xTCB->pxTaskTag;
  1852. }
  1853. taskEXIT_CRITICAL();
  1854. return xReturn;
  1855. }
  1856. #endif /* configUSE_APPLICATION_TASK_TAG */
  1857. /*-----------------------------------------------------------*/
  1858. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  1859. BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask, void *pvParameter )
  1860. {
  1861. TCB_t *xTCB;
  1862. BaseType_t xReturn;
  1863. /* If xTask is NULL then we are calling our own task hook. */
  1864. if( xTask == NULL )
  1865. {
  1866. xTCB = ( TCB_t * ) pxCurrentTCB;
  1867. }
  1868. else
  1869. {
  1870. xTCB = ( TCB_t * ) xTask;
  1871. }
  1872. if( xTCB->pxTaskTag != NULL )
  1873. {
  1874. xReturn = xTCB->pxTaskTag( pvParameter );
  1875. }
  1876. else
  1877. {
  1878. xReturn = pdFAIL;
  1879. }
  1880. return xReturn;
  1881. }
  1882. #endif /* configUSE_APPLICATION_TASK_TAG */
  1883. /*-----------------------------------------------------------*/
  1884. void vTaskSwitchContext( void )
  1885. {
  1886. if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
  1887. {
  1888. /* The scheduler is currently suspended - do not allow a context
  1889. switch. */
  1890. xYieldPending = pdTRUE;
  1891. }
  1892. else
  1893. {
  1894. xYieldPending = pdFALSE;
  1895. traceTASK_SWITCHED_OUT();
  1896. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  1897. {
  1898. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  1899. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
  1900. #else
  1901. ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
  1902. #endif
  1903. /* Add the amount of time the task has been running to the
  1904. accumulated time so far. The time the task started running was
  1905. stored in ulTaskSwitchedInTime. Note that there is no overflow
  1906. protection here so count values are only valid until the timer
  1907. overflows. The guard against negative values is to protect
  1908. against suspect run time stat counter implementations - which
  1909. are provided by the application, not the kernel. */
  1910. if( ulTotalRunTime > ulTaskSwitchedInTime )
  1911. {
  1912. pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
  1913. }
  1914. else
  1915. {
  1916. mtCOVERAGE_TEST_MARKER();
  1917. }
  1918. ulTaskSwitchedInTime = ulTotalRunTime;
  1919. }
  1920. #endif /* configGENERATE_RUN_TIME_STATS */
  1921. /* Check for stack overflow, if configured. */
  1922. taskFIRST_CHECK_FOR_STACK_OVERFLOW();
  1923. taskSECOND_CHECK_FOR_STACK_OVERFLOW();
  1924. /* Select a new task to run using either the generic C or port
  1925. optimised asm code. */
  1926. taskSELECT_HIGHEST_PRIORITY_TASK();
  1927. traceTASK_SWITCHED_IN();
  1928. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  1929. {
  1930. /* Switch Newlib's _impure_ptr variable to point to the _reent
  1931. structure specific to this task. */
  1932. _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
  1933. }
  1934. #endif /* configUSE_NEWLIB_REENTRANT */
  1935. }
  1936. }
  1937. /*-----------------------------------------------------------*/
  1938. void vTaskPlaceOnEventList( List_t * const pxEventList, const TickType_t xTicksToWait )
  1939. {
  1940. TickType_t xTimeToWake;
  1941. configASSERT( pxEventList );
  1942. /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
  1943. SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
  1944. /* Place the event list item of the TCB in the appropriate event list.
  1945. This is placed in the list in priority order so the highest priority task
  1946. is the first to be woken by the event. The queue that contains the event
  1947. list is locked, preventing simultaneous access from interrupts. */
  1948. vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  1949. /* The task must be removed from from the ready list before it is added to
  1950. the blocked list as the same list item is used for both lists. Exclusive
  1951. access to the ready lists guaranteed because the scheduler is locked. */
  1952. if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  1953. {
  1954. /* The current task must be in a ready list, so there is no need to
  1955. check, and the port reset macro can be called directly. */
  1956. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  1957. }
  1958. else
  1959. {
  1960. mtCOVERAGE_TEST_MARKER();
  1961. }
  1962. #if ( INCLUDE_vTaskSuspend == 1 )
  1963. {
  1964. if( xTicksToWait == portMAX_DELAY )
  1965. {
  1966. /* Add the task to the suspended task list instead of a delayed task
  1967. list to ensure the task is not woken by a timing event. It will
  1968. block indefinitely. */
  1969. vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );
  1970. }
  1971. else
  1972. {
  1973. /* Calculate the time at which the task should be woken if the event
  1974. does not occur. This may overflow but this doesn't matter, the
  1975. scheduler will handle it. */
  1976. xTimeToWake = xTickCount + xTicksToWait;
  1977. prvAddCurrentTaskToDelayedList( xTimeToWake );
  1978. }
  1979. }
  1980. #else /* INCLUDE_vTaskSuspend */
  1981. {
  1982. /* Calculate the time at which the task should be woken if the event does
  1983. not occur. This may overflow but this doesn't matter, the scheduler
  1984. will handle it. */
  1985. xTimeToWake = xTickCount + xTicksToWait;
  1986. prvAddCurrentTaskToDelayedList( xTimeToWake );
  1987. }
  1988. #endif /* INCLUDE_vTaskSuspend */
  1989. }
  1990. /*-----------------------------------------------------------*/
  1991. void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, const TickType_t xItemValue, const TickType_t xTicksToWait )
  1992. {
  1993. TickType_t xTimeToWake;
  1994. configASSERT( pxEventList );
  1995. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
  1996. the event groups implementation. */
  1997. configASSERT( uxSchedulerSuspended != 0 );
  1998. /* Store the item value in the event list item. It is safe to access the
  1999. event list item here as interrupts won't access the event list item of a
  2000. task that is not in the Blocked state. */
  2001. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
  2002. /* Place the event list item of the TCB at the end of the appropriate event
  2003. list. It is safe to access the event list here because it is part of an
  2004. event group implementation - and interrupts don't access event groups
  2005. directly (instead they access them indirectly by pending function calls to
  2006. the task level). */
  2007. vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  2008. /* The task must be removed from the ready list before it is added to the
  2009. blocked list. Exclusive access can be assured to the ready list as the
  2010. scheduler is locked. */
  2011. if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  2012. {
  2013. /* The current task must be in a ready list, so there is no need to
  2014. check, and the port reset macro can be called directly. */
  2015. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  2016. }
  2017. else
  2018. {
  2019. mtCOVERAGE_TEST_MARKER();
  2020. }
  2021. #if ( INCLUDE_vTaskSuspend == 1 )
  2022. {
  2023. if( xTicksToWait == portMAX_DELAY )
  2024. {
  2025. /* Add the task to the suspended task list instead of a delayed task
  2026. list to ensure it is not woken by a timing event. It will block
  2027. indefinitely. */
  2028. vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );
  2029. }
  2030. else
  2031. {
  2032. /* Calculate the time at which the task should be woken if the event
  2033. does not occur. This may overflow but this doesn't matter, the
  2034. kernel will manage it correctly. */
  2035. xTimeToWake = xTickCount + xTicksToWait;
  2036. prvAddCurrentTaskToDelayedList( xTimeToWake );
  2037. }
  2038. }
  2039. #else /* INCLUDE_vTaskSuspend */
  2040. {
  2041. /* Calculate the time at which the task should be woken if the event does
  2042. not occur. This may overflow but this doesn't matter, the kernel
  2043. will manage it correctly. */
  2044. xTimeToWake = xTickCount + xTicksToWait;
  2045. prvAddCurrentTaskToDelayedList( xTimeToWake );
  2046. }
  2047. #endif /* INCLUDE_vTaskSuspend */
  2048. }
  2049. /*-----------------------------------------------------------*/
  2050. #if configUSE_TIMERS == 1
  2051. void vTaskPlaceOnEventListRestricted( List_t * const pxEventList, const TickType_t xTicksToWait )
  2052. {
  2053. TickType_t xTimeToWake;
  2054. configASSERT( pxEventList );
  2055. /* This function should not be called by application code hence the
  2056. 'Restricted' in its name. It is not part of the public API. It is
  2057. designed for use by kernel code, and has special calling requirements -
  2058. it should be called from a critical section. */
  2059. /* Place the event list item of the TCB in the appropriate event list.
  2060. In this case it is assume that this is the only task that is going to
  2061. be waiting on this event list, so the faster vListInsertEnd() function
  2062. can be used in place of vListInsert. */
  2063. vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  2064. /* We must remove this task from the ready list before adding it to the
  2065. blocked list as the same list item is used for both lists. This
  2066. function is called form a critical section. */
  2067. if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  2068. {
  2069. /* The current task must be in a ready list, so there is no need to
  2070. check, and the port reset macro can be called directly. */
  2071. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  2072. }
  2073. else
  2074. {
  2075. mtCOVERAGE_TEST_MARKER();
  2076. }
  2077. /* Calculate the time at which the task should be woken if the event does
  2078. not occur. This may overflow but this doesn't matter. */
  2079. xTimeToWake = xTickCount + xTicksToWait;
  2080. traceTASK_DELAY_UNTIL();
  2081. prvAddCurrentTaskToDelayedList( xTimeToWake );
  2082. }
  2083. #endif /* configUSE_TIMERS */
  2084. /*-----------------------------------------------------------*/
  2085. BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
  2086. {
  2087. TCB_t *pxUnblockedTCB;
  2088. BaseType_t xReturn;
  2089. /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
  2090. called from a critical section within an ISR. */
  2091. /* The event list is sorted in priority order, so the first in the list can
  2092. be removed as it is known to be the highest priority. Remove the TCB from
  2093. the delayed list, and add it to the ready list.
  2094. If an event is for a queue that is locked then this function will never
  2095. get called - the lock count on the queue will get modified instead. This
  2096. means exclusive access to the event list is guaranteed here.
  2097. This function assumes that a check has already been made to ensure that
  2098. pxEventList is not empty. */
  2099. pxUnblockedTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
  2100. configASSERT( pxUnblockedTCB );
  2101. ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
  2102. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  2103. {
  2104. ( void ) uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );
  2105. prvAddTaskToReadyList( pxUnblockedTCB );
  2106. }
  2107. else
  2108. {
  2109. /* The delayed and ready lists cannot be accessed, so hold this task
  2110. pending until the scheduler is resumed. */
  2111. vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
  2112. }
  2113. if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
  2114. {
  2115. /* Return true if the task removed from the event list has a higher
  2116. priority than the calling task. This allows the calling task to know if
  2117. it should force a context switch now. */
  2118. xReturn = pdTRUE;
  2119. /* Mark that a yield is pending in case the user is not using the
  2120. "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
  2121. xYieldPending = pdTRUE;
  2122. }
  2123. else
  2124. {
  2125. xReturn = pdFALSE;
  2126. }
  2127. #if( configUSE_TICKLESS_IDLE == 1 )
  2128. {
  2129. /* If a task is blocked on a kernel object then xNextTaskUnblockTime
  2130. might be set to the blocked task's time out time. If the task is
  2131. unblocked for a reason other than a timeout xNextTaskUnblockTime is
  2132. normally left unchanged, because it is automatically get reset to a new
  2133. value when the tick count equals xNextTaskUnblockTime. However if
  2134. tickless idling is used it might be more important to enter sleep mode
  2135. at the earliest possible time - so reset xNextTaskUnblockTime here to
  2136. ensure it is updated at the earliest possible time. */
  2137. prvResetNextTaskUnblockTime();
  2138. }
  2139. #endif
  2140. return xReturn;
  2141. }
  2142. /*-----------------------------------------------------------*/
  2143. BaseType_t xTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, const TickType_t xItemValue )
  2144. {
  2145. TCB_t *pxUnblockedTCB;
  2146. BaseType_t xReturn;
  2147. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
  2148. the event flags implementation. */
  2149. configASSERT( uxSchedulerSuspended != pdFALSE );
  2150. /* Store the new item value in the event list. */
  2151. listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
  2152. /* Remove the event list form the event flag. Interrupts do not access
  2153. event flags. */
  2154. pxUnblockedTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxEventListItem );
  2155. configASSERT( pxUnblockedTCB );
  2156. ( void ) uxListRemove( pxEventListItem );
  2157. /* Remove the task from the delayed list and add it to the ready list. The
  2158. scheduler is suspended so interrupts will not be accessing the ready
  2159. lists. */
  2160. ( void ) uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );
  2161. prvAddTaskToReadyList( pxUnblockedTCB );
  2162. if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
  2163. {
  2164. /* Return true if the task removed from the event list has
  2165. a higher priority than the calling task. This allows
  2166. the calling task to know if it should force a context
  2167. switch now. */
  2168. xReturn = pdTRUE;
  2169. /* Mark that a yield is pending in case the user is not using the
  2170. "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
  2171. xYieldPending = pdTRUE;
  2172. }
  2173. else
  2174. {
  2175. xReturn = pdFALSE;
  2176. }
  2177. return xReturn;
  2178. }
  2179. /*-----------------------------------------------------------*/
  2180. void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
  2181. {
  2182. configASSERT( pxTimeOut );
  2183. pxTimeOut->xOverflowCount = xNumOfOverflows;
  2184. pxTimeOut->xTimeOnEntering = xTickCount;
  2185. }
  2186. /*-----------------------------------------------------------*/
  2187. BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, TickType_t * const pxTicksToWait )
  2188. {
  2189. BaseType_t xReturn;
  2190. configASSERT( pxTimeOut );
  2191. configASSERT( pxTicksToWait );
  2192. taskENTER_CRITICAL();
  2193. {
  2194. /* Minor optimisation. The tick count cannot change in this block. */
  2195. const TickType_t xConstTickCount = xTickCount;
  2196. #if ( INCLUDE_vTaskSuspend == 1 )
  2197. /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is
  2198. the maximum block time then the task should block indefinitely, and
  2199. therefore never time out. */
  2200. if( *pxTicksToWait == portMAX_DELAY )
  2201. {
  2202. xReturn = pdFALSE;
  2203. }
  2204. else /* We are not blocking indefinitely, perform the checks below. */
  2205. #endif
  2206. if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
  2207. {
  2208. /* The tick count is greater than the time at which vTaskSetTimeout()
  2209. was called, but has also overflowed since vTaskSetTimeOut() was called.
  2210. It must have wrapped all the way around and gone past us again. This
  2211. passed since vTaskSetTimeout() was called. */
  2212. xReturn = pdTRUE;
  2213. }
  2214. else if( ( xConstTickCount - pxTimeOut->xTimeOnEntering ) < *pxTicksToWait )
  2215. {
  2216. /* Not a genuine timeout. Adjust parameters for time remaining. */
  2217. *pxTicksToWait -= ( xConstTickCount - pxTimeOut->xTimeOnEntering );
  2218. vTaskSetTimeOutState( pxTimeOut );
  2219. xReturn = pdFALSE;
  2220. }
  2221. else
  2222. {
  2223. xReturn = pdTRUE;
  2224. }
  2225. }
  2226. taskEXIT_CRITICAL();
  2227. return xReturn;
  2228. }
  2229. /*-----------------------------------------------------------*/
  2230. void vTaskMissedYield( void )
  2231. {
  2232. xYieldPending = pdTRUE;
  2233. }
  2234. /*-----------------------------------------------------------*/
  2235. #if ( configUSE_TRACE_FACILITY == 1 )
  2236. UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
  2237. {
  2238. UBaseType_t uxReturn;
  2239. TCB_t *pxTCB;
  2240. if( xTask != NULL )
  2241. {
  2242. pxTCB = ( TCB_t * ) xTask;
  2243. uxReturn = pxTCB->uxTaskNumber;
  2244. }
  2245. else
  2246. {
  2247. uxReturn = 0U;
  2248. }
  2249. return uxReturn;
  2250. }
  2251. #endif /* configUSE_TRACE_FACILITY */
  2252. /*-----------------------------------------------------------*/
  2253. #if ( configUSE_TRACE_FACILITY == 1 )
  2254. void vTaskSetTaskNumber( TaskHandle_t xTask, const UBaseType_t uxHandle )
  2255. {
  2256. TCB_t *pxTCB;
  2257. if( xTask != NULL )
  2258. {
  2259. pxTCB = ( TCB_t * ) xTask;
  2260. pxTCB->uxTaskNumber = uxHandle;
  2261. }
  2262. }
  2263. #endif /* configUSE_TRACE_FACILITY */
  2264. /*
  2265. * -----------------------------------------------------------
  2266. * The Idle task.
  2267. * ----------------------------------------------------------
  2268. *
  2269. * The portTASK_FUNCTION() macro is used to allow port/compiler specific
  2270. * language extensions. The equivalent prototype for this function is:
  2271. *
  2272. * void prvIdleTask( void *pvParameters );
  2273. *
  2274. */
  2275. static portTASK_FUNCTION( prvIdleTask, pvParameters )
  2276. {
  2277. /* Stop warnings. */
  2278. ( void ) pvParameters;
  2279. for( ;; )
  2280. {
  2281. /* See if any tasks have been deleted. */
  2282. prvCheckTasksWaitingTermination();
  2283. #if ( configUSE_PREEMPTION == 0 )
  2284. {
  2285. /* If we are not using preemption we keep forcing a task switch to
  2286. see if any other task has become available. If we are using
  2287. preemption we don't need to do this as any task becoming available
  2288. will automatically get the processor anyway. */
  2289. taskYIELD();
  2290. }
  2291. #endif /* configUSE_PREEMPTION */
  2292. #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
  2293. {
  2294. /* When using preemption tasks of equal priority will be
  2295. timesliced. If a task that is sharing the idle priority is ready
  2296. to run then the idle task should yield before the end of the
  2297. timeslice.
  2298. A critical region is not required here as we are just reading from
  2299. the list, and an occasional incorrect value will not matter. If
  2300. the ready list at the idle priority contains more than one task
  2301. then a task other than the idle task is ready to execute. */
  2302. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
  2303. {
  2304. taskYIELD();
  2305. }
  2306. else
  2307. {
  2308. mtCOVERAGE_TEST_MARKER();
  2309. }
  2310. }
  2311. #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
  2312. #if ( configUSE_IDLE_HOOK == 1 )
  2313. {
  2314. extern void vApplicationIdleHook( void );
  2315. /* Call the user defined function from within the idle task. This
  2316. allows the application designer to add background functionality
  2317. without the overhead of a separate task.
  2318. NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
  2319. CALL A FUNCTION THAT MIGHT BLOCK. */
  2320. vApplicationIdleHook();
  2321. }
  2322. #endif /* configUSE_IDLE_HOOK */
  2323. /* This conditional compilation should use inequality to 0, not equality
  2324. to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
  2325. user defined low power mode implementations require
  2326. configUSE_TICKLESS_IDLE to be set to a value other than 1. */
  2327. #if ( configUSE_TICKLESS_IDLE != 0 )
  2328. {
  2329. TickType_t xExpectedIdleTime;
  2330. /* It is not desirable to suspend then resume the scheduler on
  2331. each iteration of the idle task. Therefore, a preliminary
  2332. test of the expected idle time is performed without the
  2333. scheduler suspended. The result here is not necessarily
  2334. valid. */
  2335. xExpectedIdleTime = prvGetExpectedIdleTime();
  2336. if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  2337. {
  2338. vTaskSuspendAll();
  2339. {
  2340. /* Now the scheduler is suspended, the expected idle
  2341. time can be sampled again, and this time its value can
  2342. be used. */
  2343. configASSERT( xNextTaskUnblockTime >= xTickCount );
  2344. xExpectedIdleTime = prvGetExpectedIdleTime();
  2345. if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  2346. {
  2347. traceLOW_POWER_IDLE_BEGIN();
  2348. portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
  2349. traceLOW_POWER_IDLE_END();
  2350. }
  2351. else
  2352. {
  2353. mtCOVERAGE_TEST_MARKER();
  2354. }
  2355. }
  2356. ( void ) xTaskResumeAll();
  2357. }
  2358. else
  2359. {
  2360. mtCOVERAGE_TEST_MARKER();
  2361. }
  2362. }
  2363. #endif /* configUSE_TICKLESS_IDLE */
  2364. }
  2365. }
  2366. /*-----------------------------------------------------------*/
  2367. #if configUSE_TICKLESS_IDLE != 0
  2368. eSleepModeStatus eTaskConfirmSleepModeStatus( void )
  2369. {
  2370. eSleepModeStatus eReturn = eStandardSleep;
  2371. if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
  2372. {
  2373. /* A task was made ready while the scheduler was suspended. */
  2374. eReturn = eAbortSleep;
  2375. }
  2376. else if( xYieldPending != pdFALSE )
  2377. {
  2378. /* A yield was pended while the scheduler was suspended. */
  2379. eReturn = eAbortSleep;
  2380. }
  2381. else
  2382. {
  2383. #if configUSE_TIMERS == 0
  2384. {
  2385. /* The idle task exists in addition to the application tasks. */
  2386. const UBaseType_t uxNonApplicationTasks = 1;
  2387. /* If timers are not being used and all the tasks are in the
  2388. suspended list (which might mean they have an infinite block
  2389. time rather than actually being suspended) then it is safe to
  2390. turn all clocks off and just wait for external interrupts. */
  2391. if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
  2392. {
  2393. eReturn = eNoTasksWaitingTimeout;
  2394. }
  2395. else
  2396. {
  2397. mtCOVERAGE_TEST_MARKER();
  2398. }
  2399. }
  2400. #endif /* configUSE_TIMERS */
  2401. }
  2402. return eReturn;
  2403. }
  2404. #endif /* configUSE_TICKLESS_IDLE */
  2405. /*-----------------------------------------------------------*/
  2406. static void prvInitialiseTCBVariables( TCB_t * const pxTCB, const char * const pcName, UBaseType_t uxPriority, const MemoryRegion_t * const xRegions, const uint16_t usStackDepth ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2407. {
  2408. UBaseType_t x;
  2409. /* Store the task name in the TCB. */
  2410. for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
  2411. {
  2412. pxTCB->pcTaskName[ x ] = pcName[ x ];
  2413. /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
  2414. configMAX_TASK_NAME_LEN characters just in case the memory after the
  2415. string is not accessible (extremely unlikely). */
  2416. if( pcName[ x ] == 0x00 )
  2417. {
  2418. break;
  2419. }
  2420. else
  2421. {
  2422. mtCOVERAGE_TEST_MARKER();
  2423. }
  2424. }
  2425. /* Ensure the name string is terminated in the case that the string length
  2426. was greater or equal to configMAX_TASK_NAME_LEN. */
  2427. pxTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
  2428. /* This is used as an array index so must ensure it's not too large. First
  2429. remove the privilege bit if one is present. */
  2430. if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  2431. {
  2432. uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  2433. }
  2434. else
  2435. {
  2436. mtCOVERAGE_TEST_MARKER();
  2437. }
  2438. pxTCB->uxPriority = uxPriority;
  2439. #if ( configUSE_MUTEXES == 1 )
  2440. {
  2441. pxTCB->uxBasePriority = uxPriority;
  2442. pxTCB->uxMutexesHeld = 0;
  2443. }
  2444. #endif /* configUSE_MUTEXES */
  2445. vListInitialiseItem( &( pxTCB->xGenericListItem ) );
  2446. vListInitialiseItem( &( pxTCB->xEventListItem ) );
  2447. /* Set the pxTCB as a link back from the ListItem_t. This is so we can get
  2448. back to the containing TCB from a generic item in a list. */
  2449. listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
  2450. /* Event lists are always in priority order. */
  2451. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  2452. listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
  2453. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  2454. {
  2455. pxTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
  2456. }
  2457. #endif /* portCRITICAL_NESTING_IN_TCB */
  2458. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  2459. {
  2460. pxTCB->pxTaskTag = NULL;
  2461. }
  2462. #endif /* configUSE_APPLICATION_TASK_TAG */
  2463. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  2464. {
  2465. pxTCB->ulRunTimeCounter = 0UL;
  2466. }
  2467. #endif /* configGENERATE_RUN_TIME_STATS */
  2468. #if ( portUSING_MPU_WRAPPERS == 1 )
  2469. {
  2470. vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );
  2471. }
  2472. #else /* portUSING_MPU_WRAPPERS */
  2473. {
  2474. ( void ) xRegions;
  2475. ( void ) usStackDepth;
  2476. }
  2477. #endif /* portUSING_MPU_WRAPPERS */
  2478. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  2479. {
  2480. pxTCB->ulNotifiedValue = 0;
  2481. pxTCB->eNotifyState = eNotWaitingNotification;
  2482. }
  2483. #endif
  2484. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  2485. {
  2486. /* Initialise this task's Newlib reent structure. */
  2487. _REENT_INIT_PTR( ( &( pxTCB->xNewLib_reent ) ) );
  2488. }
  2489. #endif /* configUSE_NEWLIB_REENTRANT */
  2490. }
  2491. /*-----------------------------------------------------------*/
  2492. #if ( portUSING_MPU_WRAPPERS == 1 )
  2493. void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify, const MemoryRegion_t * const xRegions )
  2494. {
  2495. TCB_t *pxTCB;
  2496. /* If null is passed in here then we are deleting ourselves. */
  2497. pxTCB = prvGetTCBFromHandle( xTaskToModify );
  2498. vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
  2499. }
  2500. #endif /* portUSING_MPU_WRAPPERS */
  2501. /*-----------------------------------------------------------*/
  2502. static void prvInitialiseTaskLists( void )
  2503. {
  2504. UBaseType_t uxPriority;
  2505. for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
  2506. {
  2507. vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
  2508. }
  2509. vListInitialise( &xDelayedTaskList1 );
  2510. vListInitialise( &xDelayedTaskList2 );
  2511. vListInitialise( &xPendingReadyList );
  2512. #if ( INCLUDE_vTaskDelete == 1 )
  2513. {
  2514. vListInitialise( &xTasksWaitingTermination );
  2515. }
  2516. #endif /* INCLUDE_vTaskDelete */
  2517. #if ( INCLUDE_vTaskSuspend == 1 )
  2518. {
  2519. vListInitialise( &xSuspendedTaskList );
  2520. }
  2521. #endif /* INCLUDE_vTaskSuspend */
  2522. /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
  2523. using list2. */
  2524. pxDelayedTaskList = &xDelayedTaskList1;
  2525. pxOverflowDelayedTaskList = &xDelayedTaskList2;
  2526. }
  2527. /*-----------------------------------------------------------*/
  2528. static void prvCheckTasksWaitingTermination( void )
  2529. {
  2530. #if ( INCLUDE_vTaskDelete == 1 )
  2531. {
  2532. BaseType_t xListIsEmpty;
  2533. /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
  2534. too often in the idle task. */
  2535. while( uxTasksDeleted > ( UBaseType_t ) 0U )
  2536. {
  2537. vTaskSuspendAll();
  2538. {
  2539. xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
  2540. }
  2541. ( void ) xTaskResumeAll();
  2542. if( xListIsEmpty == pdFALSE )
  2543. {
  2544. TCB_t *pxTCB;
  2545. taskENTER_CRITICAL();
  2546. {
  2547. pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
  2548. ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
  2549. --uxCurrentNumberOfTasks;
  2550. --uxTasksDeleted;
  2551. }
  2552. taskEXIT_CRITICAL();
  2553. prvDeleteTCB( pxTCB );
  2554. }
  2555. else
  2556. {
  2557. mtCOVERAGE_TEST_MARKER();
  2558. }
  2559. }
  2560. }
  2561. #endif /* vTaskDelete */
  2562. }
  2563. /*-----------------------------------------------------------*/
  2564. static void prvAddCurrentTaskToDelayedList( const TickType_t xTimeToWake )
  2565. {
  2566. /* The list item will be inserted in wake time order. */
  2567. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
  2568. if( xTimeToWake < xTickCount )
  2569. {
  2570. /* Wake time has overflowed. Place this item in the overflow list. */
  2571. vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xGenericListItem ) );
  2572. }
  2573. else
  2574. {
  2575. /* The wake time has not overflowed, so the current block list is used. */
  2576. vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xGenericListItem ) );
  2577. /* If the task entering the blocked state was placed at the head of the
  2578. list of blocked tasks then xNextTaskUnblockTime needs to be updated
  2579. too. */
  2580. if( xTimeToWake < xNextTaskUnblockTime )
  2581. {
  2582. xNextTaskUnblockTime = xTimeToWake;
  2583. }
  2584. else
  2585. {
  2586. mtCOVERAGE_TEST_MARKER();
  2587. }
  2588. }
  2589. }
  2590. /*-----------------------------------------------------------*/
  2591. static TCB_t *prvAllocateTCBAndStack( const uint16_t usStackDepth, StackType_t * const puxStackBuffer )
  2592. {
  2593. TCB_t *pxNewTCB;
  2594. /* If the stack grows down then allocate the stack then the TCB so the stack
  2595. does not grow into the TCB. Likewise if the stack grows up then allocate
  2596. the TCB then the stack. */
  2597. #if( portSTACK_GROWTH > 0 )
  2598. {
  2599. /* Allocate space for the TCB. Where the memory comes from depends on
  2600. the implementation of the port malloc function. */
  2601. pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
  2602. if( pxNewTCB != NULL )
  2603. {
  2604. /* Allocate space for the stack used by the task being created.
  2605. The base of the stack memory stored in the TCB so the task can
  2606. be deleted later if required. */
  2607. pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocAligned( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ), puxStackBuffer ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  2608. if( pxNewTCB->pxStack == NULL )
  2609. {
  2610. /* Could not allocate the stack. Delete the allocated TCB. */
  2611. vPortFree( pxNewTCB );
  2612. pxNewTCB = NULL;
  2613. }
  2614. }
  2615. }
  2616. #else /* portSTACK_GROWTH */
  2617. {
  2618. StackType_t *pxStack;
  2619. /* Allocate space for the stack used by the task being created. */
  2620. pxStack = ( StackType_t * ) pvPortMallocAligned( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ), puxStackBuffer ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  2621. if( pxStack != NULL )
  2622. {
  2623. /* Allocate space for the TCB. Where the memory comes from depends
  2624. on the implementation of the port malloc function. */
  2625. pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
  2626. if( pxNewTCB != NULL )
  2627. {
  2628. /* Store the stack location in the TCB. */
  2629. pxNewTCB->pxStack = pxStack;
  2630. }
  2631. else
  2632. {
  2633. /* The stack cannot be used as the TCB was not created. Free it
  2634. again. */
  2635. vPortFree( pxStack );
  2636. }
  2637. }
  2638. else
  2639. {
  2640. pxNewTCB = NULL;
  2641. }
  2642. }
  2643. #endif /* portSTACK_GROWTH */
  2644. if( pxNewTCB != NULL )
  2645. {
  2646. /* Avoid dependency on memset() if it is not required. */
  2647. #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
  2648. {
  2649. /* Just to help debugging. */
  2650. ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( StackType_t ) );
  2651. }
  2652. #endif /* ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) ) */
  2653. }
  2654. return pxNewTCB;
  2655. }
  2656. /*-----------------------------------------------------------*/
  2657. #if ( configUSE_TRACE_FACILITY == 1 )
  2658. static UBaseType_t prvListTaskWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState )
  2659. {
  2660. volatile TCB_t *pxNextTCB, *pxFirstTCB;
  2661. UBaseType_t uxTask = 0;
  2662. if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
  2663. {
  2664. listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
  2665. /* Populate an TaskStatus_t structure within the
  2666. pxTaskStatusArray array for each task that is referenced from
  2667. pxList. See the definition of TaskStatus_t in task.h for the
  2668. meaning of each TaskStatus_t structure member. */
  2669. do
  2670. {
  2671. listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
  2672. pxTaskStatusArray[ uxTask ].xHandle = ( TaskHandle_t ) pxNextTCB;
  2673. pxTaskStatusArray[ uxTask ].pcTaskName = ( const char * ) &( pxNextTCB->pcTaskName [ 0 ] );
  2674. pxTaskStatusArray[ uxTask ].xTaskNumber = pxNextTCB->uxTCBNumber;
  2675. pxTaskStatusArray[ uxTask ].eCurrentState = eState;
  2676. pxTaskStatusArray[ uxTask ].uxCurrentPriority = pxNextTCB->uxPriority;
  2677. #if ( INCLUDE_vTaskSuspend == 1 )
  2678. {
  2679. /* If the task is in the suspended list then there is a chance
  2680. it is actually just blocked indefinitely - so really it should
  2681. be reported as being in the Blocked state. */
  2682. if( eState == eSuspended )
  2683. {
  2684. if( listLIST_ITEM_CONTAINER( &( pxNextTCB->xEventListItem ) ) != NULL )
  2685. {
  2686. pxTaskStatusArray[ uxTask ].eCurrentState = eBlocked;
  2687. }
  2688. }
  2689. }
  2690. #endif /* INCLUDE_vTaskSuspend */
  2691. #if ( configUSE_MUTEXES == 1 )
  2692. {
  2693. pxTaskStatusArray[ uxTask ].uxBasePriority = pxNextTCB->uxBasePriority;
  2694. }
  2695. #else
  2696. {
  2697. pxTaskStatusArray[ uxTask ].uxBasePriority = 0;
  2698. }
  2699. #endif
  2700. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  2701. {
  2702. pxTaskStatusArray[ uxTask ].ulRunTimeCounter = pxNextTCB->ulRunTimeCounter;
  2703. }
  2704. #else
  2705. {
  2706. pxTaskStatusArray[ uxTask ].ulRunTimeCounter = 0;
  2707. }
  2708. #endif
  2709. #if ( portSTACK_GROWTH > 0 )
  2710. {
  2711. pxTaskStatusArray[ uxTask ].usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxNextTCB->pxEndOfStack );
  2712. }
  2713. #else
  2714. {
  2715. pxTaskStatusArray[ uxTask ].usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxNextTCB->pxStack );
  2716. }
  2717. #endif
  2718. uxTask++;
  2719. } while( pxNextTCB != pxFirstTCB );
  2720. }
  2721. else
  2722. {
  2723. mtCOVERAGE_TEST_MARKER();
  2724. }
  2725. return uxTask;
  2726. }
  2727. #endif /* configUSE_TRACE_FACILITY */
  2728. /*-----------------------------------------------------------*/
  2729. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
  2730. static uint16_t prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
  2731. {
  2732. uint32_t ulCount = 0U;
  2733. while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
  2734. {
  2735. pucStackByte -= portSTACK_GROWTH;
  2736. ulCount++;
  2737. }
  2738. ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
  2739. return ( uint16_t ) ulCount;
  2740. }
  2741. #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) */
  2742. /*-----------------------------------------------------------*/
  2743. #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
  2744. UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
  2745. {
  2746. TCB_t *pxTCB;
  2747. uint8_t *pucEndOfStack;
  2748. UBaseType_t uxReturn;
  2749. pxTCB = prvGetTCBFromHandle( xTask );
  2750. #if portSTACK_GROWTH < 0
  2751. {
  2752. pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
  2753. }
  2754. #else
  2755. {
  2756. pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
  2757. }
  2758. #endif
  2759. uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
  2760. return uxReturn;
  2761. }
  2762. #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
  2763. /*-----------------------------------------------------------*/
  2764. #if ( INCLUDE_vTaskDelete == 1 )
  2765. static void prvDeleteTCB( TCB_t *pxTCB )
  2766. {
  2767. /* This call is required specifically for the TriCore port. It must be
  2768. above the vPortFree() calls. The call is also used by ports/demos that
  2769. want to allocate and clean RAM statically. */
  2770. portCLEAN_UP_TCB( pxTCB );
  2771. /* Free up the memory allocated by the scheduler for the task. It is up
  2772. to the task to free any memory allocated at the application level. */
  2773. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  2774. {
  2775. _reclaim_reent( &( pxTCB->xNewLib_reent ) );
  2776. }
  2777. #endif /* configUSE_NEWLIB_REENTRANT */
  2778. #if( portUSING_MPU_WRAPPERS == 1 )
  2779. {
  2780. /* Only free the stack if it was allocated dynamically in the first
  2781. place. */
  2782. if( pxTCB->xUsingStaticallyAllocatedStack == pdFALSE )
  2783. {
  2784. vPortFreeAligned( pxTCB->pxStack );
  2785. }
  2786. }
  2787. #else
  2788. {
  2789. vPortFreeAligned( pxTCB->pxStack );
  2790. }
  2791. #endif
  2792. vPortFree( pxTCB );
  2793. }
  2794. #endif /* INCLUDE_vTaskDelete */
  2795. /*-----------------------------------------------------------*/
  2796. static void prvResetNextTaskUnblockTime( void )
  2797. {
  2798. TCB_t *pxTCB;
  2799. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
  2800. {
  2801. /* The new current delayed list is empty. Set
  2802. xNextTaskUnblockTime to the maximum possible value so it is
  2803. extremely unlikely that the
  2804. if( xTickCount >= xNextTaskUnblockTime ) test will pass until
  2805. there is an item in the delayed list. */
  2806. xNextTaskUnblockTime = portMAX_DELAY;
  2807. }
  2808. else
  2809. {
  2810. /* The new current delayed list is not empty, get the value of
  2811. the item at the head of the delayed list. This is the time at
  2812. which the task at the head of the delayed list should be removed
  2813. from the Blocked state. */
  2814. ( pxTCB ) = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
  2815. xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( ( pxTCB )->xGenericListItem ) );
  2816. }
  2817. }
  2818. /*-----------------------------------------------------------*/
  2819. #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
  2820. TaskHandle_t xTaskGetCurrentTaskHandle( void )
  2821. {
  2822. TaskHandle_t xReturn;
  2823. /* A critical section is not required as this is not called from
  2824. an interrupt and the current TCB will always be the same for any
  2825. individual execution thread. */
  2826. xReturn = pxCurrentTCB;
  2827. return xReturn;
  2828. }
  2829. #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
  2830. /*-----------------------------------------------------------*/
  2831. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  2832. BaseType_t xTaskGetSchedulerState( void )
  2833. {
  2834. BaseType_t xReturn;
  2835. if( xSchedulerRunning == pdFALSE )
  2836. {
  2837. xReturn = taskSCHEDULER_NOT_STARTED;
  2838. }
  2839. else
  2840. {
  2841. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  2842. {
  2843. xReturn = taskSCHEDULER_RUNNING;
  2844. }
  2845. else
  2846. {
  2847. xReturn = taskSCHEDULER_SUSPENDED;
  2848. }
  2849. }
  2850. return xReturn;
  2851. }
  2852. #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
  2853. /*-----------------------------------------------------------*/
  2854. #if ( configUSE_MUTEXES == 1 )
  2855. void vTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
  2856. {
  2857. TCB_t * const pxTCB = ( TCB_t * ) pxMutexHolder;
  2858. /* If the mutex was given back by an interrupt while the queue was
  2859. locked then the mutex holder might now be NULL. */
  2860. if( pxMutexHolder != NULL )
  2861. {
  2862. if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )
  2863. {
  2864. /* Adjust the mutex holder state to account for its new
  2865. priority. Only reset the event list item value if the value is
  2866. not being used for anything else. */
  2867. if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
  2868. {
  2869. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  2870. }
  2871. else
  2872. {
  2873. mtCOVERAGE_TEST_MARKER();
  2874. }
  2875. /* If the task being modified is in the ready state it will need to
  2876. be moved into a new list. */
  2877. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )
  2878. {
  2879. if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  2880. {
  2881. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  2882. }
  2883. else
  2884. {
  2885. mtCOVERAGE_TEST_MARKER();
  2886. }
  2887. /* Inherit the priority before being moved into the new list. */
  2888. pxTCB->uxPriority = pxCurrentTCB->uxPriority;
  2889. prvAddTaskToReadyList( pxTCB );
  2890. }
  2891. else
  2892. {
  2893. /* Just inherit the priority. */
  2894. pxTCB->uxPriority = pxCurrentTCB->uxPriority;
  2895. }
  2896. traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB->uxPriority );
  2897. }
  2898. else
  2899. {
  2900. mtCOVERAGE_TEST_MARKER();
  2901. }
  2902. }
  2903. else
  2904. {
  2905. mtCOVERAGE_TEST_MARKER();
  2906. }
  2907. }
  2908. #endif /* configUSE_MUTEXES */
  2909. /*-----------------------------------------------------------*/
  2910. #if ( configUSE_MUTEXES == 1 )
  2911. BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
  2912. {
  2913. TCB_t * const pxTCB = ( TCB_t * ) pxMutexHolder;
  2914. BaseType_t xReturn = pdFALSE;
  2915. if( pxMutexHolder != NULL )
  2916. {
  2917. configASSERT( pxTCB->uxMutexesHeld );
  2918. ( pxTCB->uxMutexesHeld )--;
  2919. if( pxTCB->uxPriority != pxTCB->uxBasePriority )
  2920. {
  2921. /* Only disinherit if no other mutexes are held. */
  2922. if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
  2923. {
  2924. /* A task can only have an inhertied priority if it holds
  2925. the mutex. If the mutex is held by a task then it cannot be
  2926. given from an interrupt, and if a mutex is given by the
  2927. holding task then it must be the running state task. Remove
  2928. the holding task from the ready list. */
  2929. if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  2930. {
  2931. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  2932. }
  2933. else
  2934. {
  2935. mtCOVERAGE_TEST_MARKER();
  2936. }
  2937. /* Disinherit the priority before adding the task into the
  2938. new ready list. */
  2939. traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
  2940. pxTCB->uxPriority = pxTCB->uxBasePriority;
  2941. /* Reset the event list item value. It cannot be in use for
  2942. any other purpose if this task is running, and it must be
  2943. running to give back the mutex. */
  2944. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  2945. prvAddTaskToReadyList( pxTCB );
  2946. /* Return true to indicate that a context switch is required.
  2947. This is only actually required in the corner case whereby
  2948. multiple mutexes were held and the mutexes were given back
  2949. in an order different to that in which they were taken.
  2950. If a context switch did not occur when the first mutex was
  2951. returned, even if a task was waiting on it, then a context
  2952. switch should occur when the last mutex is returned whether
  2953. a task is waiting on it or not. */
  2954. xReturn = pdTRUE;
  2955. }
  2956. else
  2957. {
  2958. mtCOVERAGE_TEST_MARKER();
  2959. }
  2960. }
  2961. else
  2962. {
  2963. mtCOVERAGE_TEST_MARKER();
  2964. }
  2965. }
  2966. else
  2967. {
  2968. mtCOVERAGE_TEST_MARKER();
  2969. }
  2970. return xReturn;
  2971. }
  2972. #endif /* configUSE_MUTEXES */
  2973. /*-----------------------------------------------------------*/
  2974. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  2975. void vTaskEnterCritical( void )
  2976. {
  2977. portDISABLE_INTERRUPTS();
  2978. if( xSchedulerRunning != pdFALSE )
  2979. {
  2980. ( pxCurrentTCB->uxCriticalNesting )++;
  2981. /* This is not the interrupt safe version of the enter critical
  2982. function so assert() if it is being called from an interrupt
  2983. context. Only API functions that end in "FromISR" can be used in an
  2984. interrupt. Only assert if the critical nesting count is 1 to
  2985. protect against recursive calls if the assert function also uses a
  2986. critical section. */
  2987. if( pxCurrentTCB->uxCriticalNesting == 1 )
  2988. {
  2989. portASSERT_IF_IN_ISR();
  2990. }
  2991. }
  2992. else
  2993. {
  2994. mtCOVERAGE_TEST_MARKER();
  2995. }
  2996. }
  2997. #endif /* portCRITICAL_NESTING_IN_TCB */
  2998. /*-----------------------------------------------------------*/
  2999. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  3000. void vTaskExitCritical( void )
  3001. {
  3002. if( xSchedulerRunning != pdFALSE )
  3003. {
  3004. if( pxCurrentTCB->uxCriticalNesting > 0U )
  3005. {
  3006. ( pxCurrentTCB->uxCriticalNesting )--;
  3007. if( pxCurrentTCB->uxCriticalNesting == 0U )
  3008. {
  3009. portENABLE_INTERRUPTS();
  3010. }
  3011. else
  3012. {
  3013. mtCOVERAGE_TEST_MARKER();
  3014. }
  3015. }
  3016. else
  3017. {
  3018. mtCOVERAGE_TEST_MARKER();
  3019. }
  3020. }
  3021. else
  3022. {
  3023. mtCOVERAGE_TEST_MARKER();
  3024. }
  3025. }
  3026. #endif /* portCRITICAL_NESTING_IN_TCB */
  3027. /*-----------------------------------------------------------*/
  3028. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  3029. static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName )
  3030. {
  3031. BaseType_t x;
  3032. /* Start by copying the entire string. */
  3033. strcpy( pcBuffer, pcTaskName );
  3034. /* Pad the end of the string with spaces to ensure columns line up when
  3035. printed out. */
  3036. for( x = strlen( pcBuffer ); x < ( configMAX_TASK_NAME_LEN - 1 ); x++ )
  3037. {
  3038. pcBuffer[ x ] = ' ';
  3039. }
  3040. /* Terminate. */
  3041. pcBuffer[ x ] = 0x00;
  3042. /* Return the new end of string. */
  3043. return &( pcBuffer[ x ] );
  3044. }
  3045. #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
  3046. /*-----------------------------------------------------------*/
  3047. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  3048. void vTaskList( char * pcWriteBuffer )
  3049. {
  3050. TaskStatus_t *pxTaskStatusArray;
  3051. volatile UBaseType_t uxArraySize, x;
  3052. char cStatus;
  3053. /*
  3054. * PLEASE NOTE:
  3055. *
  3056. * This function is provided for convenience only, and is used by many
  3057. * of the demo applications. Do not consider it to be part of the
  3058. * scheduler.
  3059. *
  3060. * vTaskList() calls uxTaskGetSystemState(), then formats part of the
  3061. * uxTaskGetSystemState() output into a human readable table that
  3062. * displays task names, states and stack usage.
  3063. *
  3064. * vTaskList() has a dependency on the sprintf() C library function that
  3065. * might bloat the code size, use a lot of stack, and provide different
  3066. * results on different platforms. An alternative, tiny, third party,
  3067. * and limited functionality implementation of sprintf() is provided in
  3068. * many of the FreeRTOS/Demo sub-directories in a file called
  3069. * printf-stdarg.c (note printf-stdarg.c does not provide a full
  3070. * snprintf() implementation!).
  3071. *
  3072. * It is recommended that production systems call uxTaskGetSystemState()
  3073. * directly to get access to raw stats data, rather than indirectly
  3074. * through a call to vTaskList().
  3075. */
  3076. /* Make sure the write buffer does not contain a string. */
  3077. *pcWriteBuffer = 0x00;
  3078. /* Take a snapshot of the number of tasks in case it changes while this
  3079. function is executing. */
  3080. uxArraySize = uxCurrentNumberOfTasks;
  3081. /* Allocate an array index for each task. */
  3082. pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
  3083. if( pxTaskStatusArray != NULL )
  3084. {
  3085. /* Generate the (binary) data. */
  3086. uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
  3087. /* Create a human readable table from the binary data. */
  3088. for( x = 0; x < uxArraySize; x++ )
  3089. {
  3090. switch( pxTaskStatusArray[ x ].eCurrentState )
  3091. {
  3092. case eReady: cStatus = tskREADY_CHAR;
  3093. break;
  3094. case eBlocked: cStatus = tskBLOCKED_CHAR;
  3095. break;
  3096. case eSuspended: cStatus = tskSUSPENDED_CHAR;
  3097. break;
  3098. case eDeleted: cStatus = tskDELETED_CHAR;
  3099. break;
  3100. default: /* Should not get here, but it is included
  3101. to prevent static checking errors. */
  3102. cStatus = 0x00;
  3103. break;
  3104. }
  3105. /* Write the task name to the string, padding with spaces so it
  3106. can be printed in tabular form more easily. */
  3107. pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
  3108. /* Write the rest of the string. */
  3109. sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
  3110. pcWriteBuffer += strlen( pcWriteBuffer );
  3111. }
  3112. /* Free the array again. */
  3113. vPortFree( pxTaskStatusArray );
  3114. }
  3115. else
  3116. {
  3117. mtCOVERAGE_TEST_MARKER();
  3118. }
  3119. }
  3120. #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
  3121. /*----------------------------------------------------------*/
  3122. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  3123. void vTaskGetRunTimeStats( char *pcWriteBuffer )
  3124. {
  3125. TaskStatus_t *pxTaskStatusArray;
  3126. volatile UBaseType_t uxArraySize, x;
  3127. uint32_t ulTotalTime, ulStatsAsPercentage;
  3128. #if( configUSE_TRACE_FACILITY != 1 )
  3129. {
  3130. #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
  3131. }
  3132. #endif
  3133. /*
  3134. * PLEASE NOTE:
  3135. *
  3136. * This function is provided for convenience only, and is used by many
  3137. * of the demo applications. Do not consider it to be part of the
  3138. * scheduler.
  3139. *
  3140. * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
  3141. * of the uxTaskGetSystemState() output into a human readable table that
  3142. * displays the amount of time each task has spent in the Running state
  3143. * in both absolute and percentage terms.
  3144. *
  3145. * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
  3146. * function that might bloat the code size, use a lot of stack, and
  3147. * provide different results on different platforms. An alternative,
  3148. * tiny, third party, and limited functionality implementation of
  3149. * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
  3150. * a file called printf-stdarg.c (note printf-stdarg.c does not provide
  3151. * a full snprintf() implementation!).
  3152. *
  3153. * It is recommended that production systems call uxTaskGetSystemState()
  3154. * directly to get access to raw stats data, rather than indirectly
  3155. * through a call to vTaskGetRunTimeStats().
  3156. */
  3157. /* Make sure the write buffer does not contain a string. */
  3158. *pcWriteBuffer = 0x00;
  3159. /* Take a snapshot of the number of tasks in case it changes while this
  3160. function is executing. */
  3161. uxArraySize = uxCurrentNumberOfTasks;
  3162. /* Allocate an array index for each task. */
  3163. pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
  3164. if( pxTaskStatusArray != NULL )
  3165. {
  3166. /* Generate the (binary) data. */
  3167. uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
  3168. /* For percentage calculations. */
  3169. ulTotalTime /= 100UL;
  3170. /* Avoid divide by zero errors. */
  3171. if( ulTotalTime > 0 )
  3172. {
  3173. /* Create a human readable table from the binary data. */
  3174. for( x = 0; x < uxArraySize; x++ )
  3175. {
  3176. /* What percentage of the total run time has the task used?
  3177. This will always be rounded down to the nearest integer.
  3178. ulTotalRunTimeDiv100 has already been divided by 100. */
  3179. ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
  3180. /* Write the task name to the string, padding with
  3181. spaces so it can be printed in tabular form more
  3182. easily. */
  3183. pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
  3184. if( ulStatsAsPercentage > 0UL )
  3185. {
  3186. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  3187. {
  3188. sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
  3189. }
  3190. #else
  3191. {
  3192. /* sizeof( int ) == sizeof( long ) so a smaller
  3193. printf() library can be used. */
  3194. sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
  3195. }
  3196. #endif
  3197. }
  3198. else
  3199. {
  3200. /* If the percentage is zero here then the task has
  3201. consumed less than 1% of the total run time. */
  3202. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  3203. {
  3204. sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
  3205. }
  3206. #else
  3207. {
  3208. /* sizeof( int ) == sizeof( long ) so a smaller
  3209. printf() library can be used. */
  3210. sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
  3211. }
  3212. #endif
  3213. }
  3214. pcWriteBuffer += strlen( pcWriteBuffer );
  3215. }
  3216. }
  3217. else
  3218. {
  3219. mtCOVERAGE_TEST_MARKER();
  3220. }
  3221. /* Free the array again. */
  3222. vPortFree( pxTaskStatusArray );
  3223. }
  3224. else
  3225. {
  3226. mtCOVERAGE_TEST_MARKER();
  3227. }
  3228. }
  3229. #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
  3230. /*-----------------------------------------------------------*/
  3231. TickType_t uxTaskResetEventItemValue( void )
  3232. {
  3233. TickType_t uxReturn;
  3234. uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
  3235. /* Reset the event list item to its normal value - so it can be used with
  3236. queues and semaphores. */
  3237. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  3238. return uxReturn;
  3239. }
  3240. /*-----------------------------------------------------------*/
  3241. #if ( configUSE_MUTEXES == 1 )
  3242. void *pvTaskIncrementMutexHeldCount( void )
  3243. {
  3244. /* If xSemaphoreCreateMutex() is called before any tasks have been created
  3245. then pxCurrentTCB will be NULL. */
  3246. if( pxCurrentTCB != NULL )
  3247. {
  3248. ( pxCurrentTCB->uxMutexesHeld )++;
  3249. }
  3250. return pxCurrentTCB;
  3251. }
  3252. #endif /* configUSE_MUTEXES */
  3253. /*-----------------------------------------------------------*/
  3254. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  3255. uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait )
  3256. {
  3257. TickType_t xTimeToWake;
  3258. uint32_t ulReturn;
  3259. taskENTER_CRITICAL();
  3260. {
  3261. /* Only block if the notification count is not already non-zero. */
  3262. if( pxCurrentTCB->ulNotifiedValue == 0UL )
  3263. {
  3264. /* Mark this task as waiting for a notification. */
  3265. pxCurrentTCB->eNotifyState = eWaitingNotification;
  3266. if( xTicksToWait > ( TickType_t ) 0 )
  3267. {
  3268. /* The task is going to block. First it must be removed
  3269. from the ready list. */
  3270. if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  3271. {
  3272. /* The current task must be in a ready list, so there is
  3273. no need to check, and the port reset macro can be called
  3274. directly. */
  3275. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  3276. }
  3277. else
  3278. {
  3279. mtCOVERAGE_TEST_MARKER();
  3280. }
  3281. #if ( INCLUDE_vTaskSuspend == 1 )
  3282. {
  3283. if( xTicksToWait == portMAX_DELAY )
  3284. {
  3285. /* Add the task to the suspended task list instead
  3286. of a delayed task list to ensure the task is not
  3287. woken by a timing event. It will block
  3288. indefinitely. */
  3289. vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );
  3290. }
  3291. else
  3292. {
  3293. /* Calculate the time at which the task should be
  3294. woken if no notification events occur. This may
  3295. overflow but this doesn't matter, the scheduler will
  3296. handle it. */
  3297. xTimeToWake = xTickCount + xTicksToWait;
  3298. prvAddCurrentTaskToDelayedList( xTimeToWake );
  3299. }
  3300. }
  3301. #else /* INCLUDE_vTaskSuspend */
  3302. {
  3303. /* Calculate the time at which the task should be
  3304. woken if the event does not occur. This may
  3305. overflow but this doesn't matter, the scheduler will
  3306. handle it. */
  3307. xTimeToWake = xTickCount + xTicksToWait;
  3308. prvAddCurrentTaskToDelayedList( xTimeToWake );
  3309. }
  3310. #endif /* INCLUDE_vTaskSuspend */
  3311. /* All ports are written to allow a yield in a critical
  3312. section (some will yield immediately, others wait until the
  3313. critical section exits) - but it is not something that
  3314. application code should ever do. */
  3315. portYIELD_WITHIN_API();
  3316. }
  3317. else
  3318. {
  3319. mtCOVERAGE_TEST_MARKER();
  3320. }
  3321. }
  3322. else
  3323. {
  3324. mtCOVERAGE_TEST_MARKER();
  3325. }
  3326. }
  3327. taskEXIT_CRITICAL();
  3328. taskENTER_CRITICAL();
  3329. {
  3330. ulReturn = pxCurrentTCB->ulNotifiedValue;
  3331. if( ulReturn != 0UL )
  3332. {
  3333. if( xClearCountOnExit != pdFALSE )
  3334. {
  3335. pxCurrentTCB->ulNotifiedValue = 0UL;
  3336. }
  3337. else
  3338. {
  3339. ( pxCurrentTCB->ulNotifiedValue )--;
  3340. }
  3341. }
  3342. else
  3343. {
  3344. mtCOVERAGE_TEST_MARKER();
  3345. }
  3346. pxCurrentTCB->eNotifyState = eNotWaitingNotification;
  3347. }
  3348. taskEXIT_CRITICAL();
  3349. return ulReturn;
  3350. }
  3351. #endif /* configUSE_TASK_NOTIFICATIONS */
  3352. /*-----------------------------------------------------------*/
  3353. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  3354. BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait )
  3355. {
  3356. TickType_t xTimeToWake;
  3357. BaseType_t xReturn;
  3358. taskENTER_CRITICAL();
  3359. {
  3360. /* Only block if a notification is not already pending. */
  3361. if( pxCurrentTCB->eNotifyState != eNotified )
  3362. {
  3363. /* Clear bits in the task's notification value as bits may get
  3364. set by the notifying task or interrupt. This can be used to
  3365. clear the value to zero. */
  3366. pxCurrentTCB->ulNotifiedValue &= ~ulBitsToClearOnEntry;
  3367. /* Mark this task as waiting for a notification. */
  3368. pxCurrentTCB->eNotifyState = eWaitingNotification;
  3369. if( xTicksToWait > ( TickType_t ) 0 )
  3370. {
  3371. /* The task is going to block. First it must be removed
  3372. from the ready list. */
  3373. if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
  3374. {
  3375. /* The current task must be in a ready list, so there is
  3376. no need to check, and the port reset macro can be called
  3377. directly. */
  3378. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  3379. }
  3380. else
  3381. {
  3382. mtCOVERAGE_TEST_MARKER();
  3383. }
  3384. #if ( INCLUDE_vTaskSuspend == 1 )
  3385. {
  3386. if( xTicksToWait == portMAX_DELAY )
  3387. {
  3388. /* Add the task to the suspended task list instead
  3389. of a delayed task list to ensure the task is not
  3390. woken by a timing event. It will block
  3391. indefinitely. */
  3392. vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );
  3393. }
  3394. else
  3395. {
  3396. /* Calculate the time at which the task should be
  3397. woken if no notification events occur. This may
  3398. overflow but this doesn't matter, the scheduler will
  3399. handle it. */
  3400. xTimeToWake = xTickCount + xTicksToWait;
  3401. prvAddCurrentTaskToDelayedList( xTimeToWake );
  3402. }
  3403. }
  3404. #else /* INCLUDE_vTaskSuspend */
  3405. {
  3406. /* Calculate the time at which the task should be
  3407. woken if the event does not occur. This may
  3408. overflow but this doesn't matter, the scheduler will
  3409. handle it. */
  3410. xTimeToWake = xTickCount + xTicksToWait;
  3411. prvAddCurrentTaskToDelayedList( xTimeToWake );
  3412. }
  3413. #endif /* INCLUDE_vTaskSuspend */
  3414. /* All ports are written to allow a yield in a critical
  3415. section (some will yield immediately, others wait until the
  3416. critical section exits) - but it is not something that
  3417. application code should ever do. */
  3418. portYIELD_WITHIN_API();
  3419. }
  3420. else
  3421. {
  3422. mtCOVERAGE_TEST_MARKER();
  3423. }
  3424. }
  3425. else
  3426. {
  3427. mtCOVERAGE_TEST_MARKER();
  3428. }
  3429. }
  3430. taskEXIT_CRITICAL();
  3431. taskENTER_CRITICAL();
  3432. {
  3433. if( pulNotificationValue != NULL )
  3434. {
  3435. /* Output the current notification value, which may or may not
  3436. have changed. */
  3437. *pulNotificationValue = pxCurrentTCB->ulNotifiedValue;
  3438. }
  3439. /* If eNotifyValue is set then either the task never entered the
  3440. blocked state (because a notification was already pending) or the
  3441. task unblocked because of a notification. Otherwise the task
  3442. unblocked because of a timeout. */
  3443. if( pxCurrentTCB->eNotifyState == eWaitingNotification )
  3444. {
  3445. /* A notification was not received. */
  3446. xReturn = pdFALSE;
  3447. }
  3448. else
  3449. {
  3450. /* A notification was already pending or a notification was
  3451. received while the task was waiting. */
  3452. pxCurrentTCB->ulNotifiedValue &= ~ulBitsToClearOnExit;
  3453. xReturn = pdTRUE;
  3454. }
  3455. pxCurrentTCB->eNotifyState = eNotWaitingNotification;
  3456. }
  3457. taskEXIT_CRITICAL();
  3458. return xReturn;
  3459. }
  3460. #endif /* configUSE_TASK_NOTIFICATIONS */
  3461. /*-----------------------------------------------------------*/
  3462. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  3463. BaseType_t xTaskNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction )
  3464. {
  3465. TCB_t * pxTCB;
  3466. eNotifyValue eOriginalNotifyState;
  3467. BaseType_t xReturn = pdPASS;
  3468. configASSERT( xTaskToNotify );
  3469. pxTCB = ( TCB_t * ) xTaskToNotify;
  3470. taskENTER_CRITICAL();
  3471. {
  3472. eOriginalNotifyState = pxTCB->eNotifyState;
  3473. pxTCB->eNotifyState = eNotified;
  3474. switch( eAction )
  3475. {
  3476. case eSetBits :
  3477. pxTCB->ulNotifiedValue |= ulValue;
  3478. break;
  3479. case eIncrement :
  3480. ( pxTCB->ulNotifiedValue )++;
  3481. break;
  3482. case eSetValueWithOverwrite :
  3483. pxTCB->ulNotifiedValue = ulValue;
  3484. break;
  3485. case eSetValueWithoutOverwrite :
  3486. if( eOriginalNotifyState != eNotified )
  3487. {
  3488. pxTCB->ulNotifiedValue = ulValue;
  3489. }
  3490. else
  3491. {
  3492. /* The value could not be written to the task. */
  3493. xReturn = pdFAIL;
  3494. }
  3495. break;
  3496. case eNoAction:
  3497. /* The task is being notified without its notify value being
  3498. updated. */
  3499. break;
  3500. }
  3501. /* If the task is in the blocked state specifically to wait for a
  3502. notification then unblock it now. */
  3503. if( eOriginalNotifyState == eWaitingNotification )
  3504. {
  3505. ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
  3506. prvAddTaskToReadyList( pxTCB );
  3507. /* The task should not have been on an event list. */
  3508. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  3509. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  3510. {
  3511. /* The notified task has a priority above the currently
  3512. executing task so a yield is required. */
  3513. portYIELD_WITHIN_API();
  3514. }
  3515. else
  3516. {
  3517. mtCOVERAGE_TEST_MARKER();
  3518. }
  3519. }
  3520. else
  3521. {
  3522. mtCOVERAGE_TEST_MARKER();
  3523. }
  3524. }
  3525. taskEXIT_CRITICAL();
  3526. return xReturn;
  3527. }
  3528. #endif /* configUSE_TASK_NOTIFICATIONS */
  3529. /*-----------------------------------------------------------*/
  3530. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  3531. BaseType_t xTaskNotifyFromISR( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, BaseType_t *pxHigherPriorityTaskWoken )
  3532. {
  3533. TCB_t * pxTCB;
  3534. eNotifyValue eOriginalNotifyState;
  3535. BaseType_t xReturn = pdPASS;
  3536. UBaseType_t uxSavedInterruptStatus;
  3537. configASSERT( xTaskToNotify );
  3538. /* RTOS ports that support interrupt nesting have the concept of a
  3539. maximum system call (or maximum API call) interrupt priority.
  3540. Interrupts that are above the maximum system call priority are keep
  3541. permanently enabled, even when the RTOS kernel is in a critical section,
  3542. but cannot make any calls to FreeRTOS API functions. If configASSERT()
  3543. is defined in FreeRTOSConfig.h then
  3544. portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  3545. failure if a FreeRTOS API function is called from an interrupt that has
  3546. been assigned a priority above the configured maximum system call
  3547. priority. Only FreeRTOS functions that end in FromISR can be called
  3548. from interrupts that have been assigned a priority at or (logically)
  3549. below the maximum system call interrupt priority. FreeRTOS maintains a
  3550. separate interrupt safe API to ensure interrupt entry is as fast and as
  3551. simple as possible. More information (albeit Cortex-M specific) is
  3552. provided on the following link:
  3553. http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  3554. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  3555. pxTCB = ( TCB_t * ) xTaskToNotify;
  3556. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  3557. {
  3558. eOriginalNotifyState = pxTCB->eNotifyState;
  3559. pxTCB->eNotifyState = eNotified;
  3560. switch( eAction )
  3561. {
  3562. case eSetBits :
  3563. pxTCB->ulNotifiedValue |= ulValue;
  3564. break;
  3565. case eIncrement :
  3566. ( pxTCB->ulNotifiedValue )++;
  3567. break;
  3568. case eSetValueWithOverwrite :
  3569. pxTCB->ulNotifiedValue = ulValue;
  3570. break;
  3571. case eSetValueWithoutOverwrite :
  3572. if( eOriginalNotifyState != eNotified )
  3573. {
  3574. pxTCB->ulNotifiedValue = ulValue;
  3575. }
  3576. else
  3577. {
  3578. /* The value could not be written to the task. */
  3579. xReturn = pdFAIL;
  3580. }
  3581. break;
  3582. case eNoAction :
  3583. /* The task is being notified without its notify value being
  3584. updated. */
  3585. break;
  3586. }
  3587. /* If the task is in the blocked state specifically to wait for a
  3588. notification then unblock it now. */
  3589. if( eOriginalNotifyState == eWaitingNotification )
  3590. {
  3591. /* The task should not have been on an event list. */
  3592. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  3593. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  3594. {
  3595. ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
  3596. prvAddTaskToReadyList( pxTCB );
  3597. }
  3598. else
  3599. {
  3600. /* The delayed and ready lists cannot be accessed, so hold
  3601. this task pending until the scheduler is resumed. */
  3602. vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  3603. }
  3604. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  3605. {
  3606. /* The notified task has a priority above the currently
  3607. executing task so a yield is required. */
  3608. if( pxHigherPriorityTaskWoken != NULL )
  3609. {
  3610. *pxHigherPriorityTaskWoken = pdTRUE;
  3611. }
  3612. }
  3613. else
  3614. {
  3615. mtCOVERAGE_TEST_MARKER();
  3616. }
  3617. }
  3618. }
  3619. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  3620. return xReturn;
  3621. }
  3622. #endif /* configUSE_TASK_NOTIFICATIONS */
  3623. /*-----------------------------------------------------------*/
  3624. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  3625. void vTaskNotifyGiveFromISR( TaskHandle_t xTaskToNotify, BaseType_t *pxHigherPriorityTaskWoken )
  3626. {
  3627. TCB_t * pxTCB;
  3628. eNotifyValue eOriginalNotifyState;
  3629. UBaseType_t uxSavedInterruptStatus;
  3630. configASSERT( xTaskToNotify );
  3631. /* RTOS ports that support interrupt nesting have the concept of a
  3632. maximum system call (or maximum API call) interrupt priority.
  3633. Interrupts that are above the maximum system call priority are keep
  3634. permanently enabled, even when the RTOS kernel is in a critical section,
  3635. but cannot make any calls to FreeRTOS API functions. If configASSERT()
  3636. is defined in FreeRTOSConfig.h then
  3637. portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  3638. failure if a FreeRTOS API function is called from an interrupt that has
  3639. been assigned a priority above the configured maximum system call
  3640. priority. Only FreeRTOS functions that end in FromISR can be called
  3641. from interrupts that have been assigned a priority at or (logically)
  3642. below the maximum system call interrupt priority. FreeRTOS maintains a
  3643. separate interrupt safe API to ensure interrupt entry is as fast and as
  3644. simple as possible. More information (albeit Cortex-M specific) is
  3645. provided on the following link:
  3646. http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  3647. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  3648. pxTCB = ( TCB_t * ) xTaskToNotify;
  3649. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  3650. {
  3651. eOriginalNotifyState = pxTCB->eNotifyState;
  3652. pxTCB->eNotifyState = eNotified;
  3653. /* 'Giving' is equivalent to incrementing a count in a counting
  3654. semaphore. */
  3655. ( pxTCB->ulNotifiedValue )++;
  3656. /* If the task is in the blocked state specifically to wait for a
  3657. notification then unblock it now. */
  3658. if( eOriginalNotifyState == eWaitingNotification )
  3659. {
  3660. /* The task should not have been on an event list. */
  3661. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  3662. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  3663. {
  3664. ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
  3665. prvAddTaskToReadyList( pxTCB );
  3666. }
  3667. else
  3668. {
  3669. /* The delayed and ready lists cannot be accessed, so hold
  3670. this task pending until the scheduler is resumed. */
  3671. vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  3672. }
  3673. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  3674. {
  3675. /* The notified task has a priority above the currently
  3676. executing task so a yield is required. */
  3677. if( pxHigherPriorityTaskWoken != NULL )
  3678. {
  3679. *pxHigherPriorityTaskWoken = pdTRUE;
  3680. }
  3681. }
  3682. else
  3683. {
  3684. mtCOVERAGE_TEST_MARKER();
  3685. }
  3686. }
  3687. }
  3688. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  3689. }
  3690. #endif /* configUSE_TASK_NOTIFICATIONS */
  3691. /*-----------------------------------------------------------*/
  3692. #ifdef FREERTOS_MODULE_TEST
  3693. #include "tasks_test_access_functions.h"
  3694. #endif