tasks.c 215 KB

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