tasks.c 217 KB

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