tasks.c 167 KB

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