tasks.c 157 KB

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