tasks.c 191 KB

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