tasks.c 264 KB

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