tasks.c 264 KB

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