tasks.c 258 KB

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