tasks.c 214 KB

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