tasks.c 258 KB

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