tasks.c 264 KB

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