tasks.c 166 KB

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