tasks.c 268 KB

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