tasks.c 166 KB

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