tasks.c 174 KB

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