tasks.c 159 KB

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