tasks.c 140 KB

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