tasks.c 260 KB

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