tasks.c 149 KB

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