tasks.c 216 KB

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