tasks.c 247 KB

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