tasks.c 194 KB

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