tasks.c 258 KB

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