tasks.c 164 KB

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