tasks.c 166 KB

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