tasks.c 252 KB

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