tasks.c 255 KB

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