tasks.c 266 KB

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