tasks.c 242 KB

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