tasks.c 166 KB

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