tasks.c 265 KB

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