tasks.c 270 KB

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