tasks.c 259 KB

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