tasks.c 223 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /* Standard includes. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  32. * all the API functions to use the MPU wrappers. That should only be done when
  33. * task.h is included from an application file. */
  34. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  35. /* FreeRTOS includes. */
  36. #include "FreeRTOS.h"
  37. #include "task.h"
  38. #include "timers.h"
  39. #include "stack_macros.h"
  40. /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
  41. * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  42. * for the header files above, but not in this file, in order to generate the
  43. * correct privileged Vs unprivileged linkage and placement. */
  44. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
  45. /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
  46. * functions but without including stdio.h here. */
  47. #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
  48. /* At the bottom of this file are two optional functions that can be used
  49. * to generate human readable text from the raw data generated by the
  50. * uxTaskGetSystemState() function. Note the formatting functions are provided
  51. * for convenience only, and are NOT considered part of the kernel. */
  52. #include <stdio.h>
  53. #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
  54. #if ( configUSE_PREEMPTION == 0 )
  55. /* If the cooperative scheduler is being used then a yield should not be
  56. * performed just because a higher priority task has been woken. */
  57. #define taskYIELD_IF_USING_PREEMPTION()
  58. #else
  59. #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
  60. #endif
  61. /* Values that can be assigned to the ucNotifyState member of the TCB. */
  62. #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
  63. #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
  64. #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
  65. /*
  66. * The value used to fill the stack of a task when the task is created. This
  67. * is used purely for checking the high water mark for tasks.
  68. */
  69. #define tskSTACK_FILL_BYTE ( 0xa5U )
  70. /* Bits used to record how a task's stack and TCB were allocated. */
  71. #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
  72. #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
  73. #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
  74. /* If any of the following are set then task stacks are filled with a known
  75. * value so the high water mark can be determined. If none of the following are
  76. * set then don't fill the stack so there is no unnecessary dependency on memset. */
  77. #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
  78. #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
  79. #else
  80. #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
  81. #endif
  82. /*
  83. * Macros used by vListTask to indicate which state a task is in.
  84. */
  85. #define tskRUNNING_CHAR ( 'X' )
  86. #define tskBLOCKED_CHAR ( 'B' )
  87. #define tskREADY_CHAR ( 'R' )
  88. #define tskDELETED_CHAR ( 'D' )
  89. #define tskSUSPENDED_CHAR ( 'S' )
  90. /*
  91. * Some kernel aware debuggers require the data the debugger needs access to to
  92. * be global, rather than file scope.
  93. */
  94. #ifdef portREMOVE_STATIC_QUALIFIER
  95. #define static
  96. #endif
  97. /* The name allocated to the Idle task. This can be overridden by defining
  98. * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
  99. #ifndef configIDLE_TASK_NAME
  100. #define configIDLE_TASK_NAME "IDLE"
  101. #endif
  102. #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
  103. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
  104. * performed in a generic way that is not optimised to any particular
  105. * microcontroller architecture. */
  106. /* uxTopReadyPriority holds the priority of the highest priority ready
  107. * state task. */
  108. #define taskRECORD_READY_PRIORITY( uxPriority ) \
  109. { \
  110. if( ( uxPriority ) > uxTopReadyPriority ) \
  111. { \
  112. uxTopReadyPriority = ( uxPriority ); \
  113. } \
  114. } /* taskRECORD_READY_PRIORITY */
  115. /*-----------------------------------------------------------*/
  116. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  117. { \
  118. UBaseType_t uxTopPriority = uxTopReadyPriority; \
  119. \
  120. /* Find the highest priority queue that contains ready tasks. */ \
  121. while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
  122. { \
  123. configASSERT( uxTopPriority ); \
  124. --uxTopPriority; \
  125. } \
  126. \
  127. /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
  128. * the same priority get an equal share of the processor time. */ \
  129. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
  130. uxTopReadyPriority = uxTopPriority; \
  131. } /* taskSELECT_HIGHEST_PRIORITY_TASK */
  132. /*-----------------------------------------------------------*/
  133. /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
  134. * they are only required when a port optimised method of task selection is
  135. * being used. */
  136. #define taskRESET_READY_PRIORITY( uxPriority )
  137. #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
  138. #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  139. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
  140. * performed in a way that is tailored to the particular microcontroller
  141. * architecture being used. */
  142. /* A port optimised version is provided. Call the port defined macros. */
  143. #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
  144. /*-----------------------------------------------------------*/
  145. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  146. { \
  147. UBaseType_t uxTopPriority; \
  148. \
  149. /* Find the highest priority list that contains ready tasks. */ \
  150. portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
  151. configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
  152. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
  153. } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
  154. /*-----------------------------------------------------------*/
  155. /* A port optimised version is provided, call it only if the TCB being reset
  156. * is being referenced from a ready list. If it is referenced from a delayed
  157. * or suspended list then it won't be in a ready list. */
  158. #define taskRESET_READY_PRIORITY( uxPriority ) \
  159. { \
  160. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
  161. { \
  162. portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
  163. } \
  164. }
  165. #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  166. /*-----------------------------------------------------------*/
  167. /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
  168. * count overflows. */
  169. #define taskSWITCH_DELAYED_LISTS() \
  170. { \
  171. List_t * pxTemp; \
  172. \
  173. /* The delayed tasks list should be empty when the lists are switched. */ \
  174. configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
  175. \
  176. pxTemp = pxDelayedTaskList; \
  177. pxDelayedTaskList = pxOverflowDelayedTaskList; \
  178. pxOverflowDelayedTaskList = pxTemp; \
  179. xNumOfOverflows++; \
  180. prvResetNextTaskUnblockTime(); \
  181. }
  182. /*-----------------------------------------------------------*/
  183. /*
  184. * Place the task represented by pxTCB into the appropriate ready list for
  185. * the task. It is inserted at the end of the list.
  186. */
  187. #define prvAddTaskToReadyList( pxTCB ) \
  188. traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
  189. taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
  190. listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
  191. tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
  192. /*-----------------------------------------------------------*/
  193. /*
  194. * Several functions take a TaskHandle_t parameter that can optionally be NULL,
  195. * where NULL is used to indicate that the handle of the currently executing
  196. * task should be used in place of the parameter. This macro simply checks to
  197. * see if the parameter is NULL and returns a pointer to the appropriate TCB.
  198. */
  199. #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
  200. /* The item value of the event list item is normally used to hold the priority
  201. * of the task to which it belongs (coded to allow it to be held in reverse
  202. * priority order). However, it is occasionally borrowed for other purposes. It
  203. * is important its value is not updated due to a task priority change while it is
  204. * being used for another purpose. The following bit definition is used to inform
  205. * the scheduler that the value should not be changed - in which case it is the
  206. * responsibility of whichever module is using the value to ensure it gets set back
  207. * to its original value when it is released. */
  208. #if ( configUSE_16_BIT_TICKS == 1 )
  209. #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
  210. #else
  211. #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
  212. #endif
  213. /*
  214. * Task control block. A task control block (TCB) is allocated for each task,
  215. * and stores task state information, including a pointer to the task's context
  216. * (the task's run time environment, including register values)
  217. */
  218. typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
  219. {
  220. 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. */
  221. #if ( portUSING_MPU_WRAPPERS == 1 )
  222. xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
  223. #endif
  224. 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 ). */
  225. ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
  226. UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
  227. StackType_t * pxStack; /*< Points to the start of the stack. */
  228. 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. */
  229. #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
  230. StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
  231. #endif
  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. configRUN_TIME_COUNTER_TYPE 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 pdHV_TRUE 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 moved to function scope 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 = pdHV_FALSE;
  314. PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
  315. PRIVILEGED_DATA static volatile BaseType_t xYieldPending = pdHV_FALSE;
  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 ) pdHV_FALSE;
  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 configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
  337. PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE 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 pdHV_TRUE if the task referenced by xTask is
  344. * currently in the Suspended state, or pdHV_FALSE 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 * ) pvPortMallocStack( ( ( ( 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 = pvPortMallocStack( ( ( ( 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. vPortFreeStack( 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 = pdHV_TRUE;
  691. }
  692. else
  693. {
  694. xRunPrivileged = pdHV_FALSE;
  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. }
  723. #else /* portSTACK_GROWTH */
  724. {
  725. pxTopOfStack = pxNewTCB->pxStack;
  726. /* Check the alignment of the stack buffer is correct. */
  727. configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
  728. /* The other extreme of the stack space is required if stack checking is
  729. * performed. */
  730. pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
  731. }
  732. #endif /* portSTACK_GROWTH */
  733. /* Store the task name in the TCB. */
  734. if( pcName != NULL )
  735. {
  736. for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
  737. {
  738. pxNewTCB->pcTaskName[ x ] = pcName[ x ];
  739. /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
  740. * configMAX_TASK_NAME_LEN characters just in case the memory after the
  741. * string is not accessible (extremely unlikely). */
  742. if( pcName[ x ] == ( char ) 0x00 )
  743. {
  744. break;
  745. }
  746. else
  747. {
  748. mtCOVERAGE_TEST_MARKER();
  749. }
  750. }
  751. /* Ensure the name string is terminated in the case that the string length
  752. * was greater or equal to configMAX_TASK_NAME_LEN. */
  753. pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
  754. }
  755. else
  756. {
  757. /* The task has not been given a name, so just ensure there is a NULL
  758. * terminator when it is read out. */
  759. pxNewTCB->pcTaskName[ 0 ] = 0x00;
  760. }
  761. /* This is used as an array index so must ensure it's not too large. */
  762. configASSERT( uxPriority < configMAX_PRIORITIES );
  763. if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  764. {
  765. uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  766. }
  767. else
  768. {
  769. mtCOVERAGE_TEST_MARKER();
  770. }
  771. pxNewTCB->uxPriority = uxPriority;
  772. #if ( configUSE_MUTEXES == 1 )
  773. {
  774. pxNewTCB->uxBasePriority = uxPriority;
  775. pxNewTCB->uxMutexesHeld = 0;
  776. }
  777. #endif /* configUSE_MUTEXES */
  778. vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
  779. vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
  780. /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
  781. * back to the containing TCB from a generic item in a list. */
  782. listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
  783. /* Event lists are always in priority order. */
  784. 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. */
  785. listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
  786. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  787. {
  788. pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
  789. }
  790. #endif /* portCRITICAL_NESTING_IN_TCB */
  791. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  792. {
  793. pxNewTCB->pxTaskTag = NULL;
  794. }
  795. #endif /* configUSE_APPLICATION_TASK_TAG */
  796. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  797. {
  798. pxNewTCB->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
  799. }
  800. #endif /* configGENERATE_RUN_TIME_STATS */
  801. #if ( portUSING_MPU_WRAPPERS == 1 )
  802. {
  803. vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
  804. }
  805. #else
  806. {
  807. /* Avoid compiler warning about unreferenced parameter. */
  808. ( void ) xRegions;
  809. }
  810. #endif
  811. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
  812. {
  813. memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
  814. }
  815. #endif
  816. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  817. {
  818. memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
  819. memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
  820. }
  821. #endif
  822. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  823. {
  824. /* Initialise this task's Newlib reent structure.
  825. * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
  826. * for additional information. */
  827. _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
  828. }
  829. #endif
  830. #if ( INCLUDE_xTaskAbortDelay == 1 )
  831. {
  832. pxNewTCB->ucDelayAborted = pdHV_FALSE;
  833. }
  834. #endif
  835. /* Initialize the TCB stack to look as if the task was already running,
  836. * but had been interrupted by the scheduler. The return address is set
  837. * to the start of the task function. Once the stack has been initialised
  838. * the top of stack variable is updated. */
  839. #if ( portUSING_MPU_WRAPPERS == 1 )
  840. {
  841. /* If the port has capability to detect stack overflow,
  842. * pass the stack end address to the stack initialization
  843. * function as well. */
  844. #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
  845. {
  846. #if ( portSTACK_GROWTH < 0 )
  847. {
  848. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
  849. }
  850. #else /* portSTACK_GROWTH */
  851. {
  852. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
  853. }
  854. #endif /* portSTACK_GROWTH */
  855. }
  856. #else /* portHAS_STACK_OVERFLOW_CHECKING */
  857. {
  858. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
  859. }
  860. #endif /* portHAS_STACK_OVERFLOW_CHECKING */
  861. }
  862. #else /* portUSING_MPU_WRAPPERS */
  863. {
  864. /* If the port has capability to detect stack overflow,
  865. * pass the stack end address to the stack initialization
  866. * function as well. */
  867. #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
  868. {
  869. #if ( portSTACK_GROWTH < 0 )
  870. {
  871. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
  872. }
  873. #else /* portSTACK_GROWTH */
  874. {
  875. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
  876. }
  877. #endif /* portSTACK_GROWTH */
  878. }
  879. #else /* portHAS_STACK_OVERFLOW_CHECKING */
  880. {
  881. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
  882. }
  883. #endif /* portHAS_STACK_OVERFLOW_CHECKING */
  884. }
  885. #endif /* portUSING_MPU_WRAPPERS */
  886. if( pxCreatedTask != NULL )
  887. {
  888. /* Pass the handle out in an anonymous way. The handle can be used to
  889. * change the created task's priority, delete the created task, etc.*/
  890. *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
  891. }
  892. else
  893. {
  894. mtCOVERAGE_TEST_MARKER();
  895. }
  896. }
  897. /*-----------------------------------------------------------*/
  898. static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
  899. {
  900. /* Ensure interrupts don't access the task lists while the lists are being
  901. * updated. */
  902. taskENTER_CRITICAL();
  903. {
  904. uxCurrentNumberOfTasks++;
  905. if( pxCurrentTCB == NULL )
  906. {
  907. /* There are no other tasks, or all the other tasks are in
  908. * the suspended state - make this the current task. */
  909. pxCurrentTCB = pxNewTCB;
  910. if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
  911. {
  912. /* This is the first task to be created so do the preliminary
  913. * initialisation required. We will not recover if this call
  914. * fails, but we will report the failure. */
  915. prvInitialiseTaskLists();
  916. }
  917. else
  918. {
  919. mtCOVERAGE_TEST_MARKER();
  920. }
  921. }
  922. else
  923. {
  924. /* If the scheduler is not already running, make this task the
  925. * current task if it is the highest priority task to be created
  926. * so far. */
  927. if( xSchedulerRunning == pdHV_FALSE )
  928. {
  929. if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
  930. {
  931. pxCurrentTCB = pxNewTCB;
  932. }
  933. else
  934. {
  935. mtCOVERAGE_TEST_MARKER();
  936. }
  937. }
  938. else
  939. {
  940. mtCOVERAGE_TEST_MARKER();
  941. }
  942. }
  943. uxTaskNumber++;
  944. #if ( configUSE_TRACE_FACILITY == 1 )
  945. {
  946. /* Add a counter into the TCB for tracing only. */
  947. pxNewTCB->uxTCBNumber = uxTaskNumber;
  948. }
  949. #endif /* configUSE_TRACE_FACILITY */
  950. traceTASK_CREATE( pxNewTCB );
  951. prvAddTaskToReadyList( pxNewTCB );
  952. portSETUP_TCB( pxNewTCB );
  953. }
  954. taskEXIT_CRITICAL();
  955. if( xSchedulerRunning != pdHV_FALSE )
  956. {
  957. /* If the created task is of a higher priority than the current task
  958. * then it should run now. */
  959. if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
  960. {
  961. taskYIELD_IF_USING_PREEMPTION();
  962. }
  963. else
  964. {
  965. mtCOVERAGE_TEST_MARKER();
  966. }
  967. }
  968. else
  969. {
  970. mtCOVERAGE_TEST_MARKER();
  971. }
  972. }
  973. /*-----------------------------------------------------------*/
  974. #if ( INCLUDE_vTaskDelete == 1 )
  975. void vTaskDelete( TaskHandle_t xTaskToDelete )
  976. {
  977. TCB_t * pxTCB;
  978. taskENTER_CRITICAL();
  979. {
  980. /* If null is passed in here then it is the calling task that is
  981. * being deleted. */
  982. pxTCB = prvGetTCBFromHandle( xTaskToDelete );
  983. /* Remove task from the ready/delayed list. */
  984. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  985. {
  986. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  987. }
  988. else
  989. {
  990. mtCOVERAGE_TEST_MARKER();
  991. }
  992. /* Is the task waiting on an event also? */
  993. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  994. {
  995. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  996. }
  997. else
  998. {
  999. mtCOVERAGE_TEST_MARKER();
  1000. }
  1001. /* Increment the uxTaskNumber also so kernel aware debuggers can
  1002. * detect that the task lists need re-generating. This is done before
  1003. * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
  1004. * not return. */
  1005. uxTaskNumber++;
  1006. if( pxTCB == pxCurrentTCB )
  1007. {
  1008. /* A task is deleting itself. This cannot complete within the
  1009. * task itself, as a context switch to another task is required.
  1010. * Place the task in the termination list. The idle task will
  1011. * check the termination list and free up any memory allocated by
  1012. * the scheduler for the TCB and stack of the deleted task. */
  1013. vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
  1014. /* Increment the ucTasksDeleted variable so the idle task knows
  1015. * there is a task that has been deleted and that it should therefore
  1016. * check the xTasksWaitingTermination list. */
  1017. ++uxDeletedTasksWaitingCleanUp;
  1018. /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
  1019. * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
  1020. traceTASK_DELETE( pxTCB );
  1021. /* The pre-delete hook is primarily for the Windows simulator,
  1022. * in which Windows specific clean up operations are performed,
  1023. * after which it is not possible to yield away from this task -
  1024. * hence xYieldPending is used to latch that a context switch is
  1025. * required. */
  1026. portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
  1027. }
  1028. else
  1029. {
  1030. --uxCurrentNumberOfTasks;
  1031. traceTASK_DELETE( pxTCB );
  1032. /* Reset the next expected unblock time in case it referred to
  1033. * the task that has just been deleted. */
  1034. prvResetNextTaskUnblockTime();
  1035. }
  1036. }
  1037. taskEXIT_CRITICAL();
  1038. /* If the task is not deleting itself, call prvDeleteTCB from outside of
  1039. * critical section. If a task deletes itself, prvDeleteTCB is called
  1040. * from prvCheckTasksWaitingTermination which is called from Idle task. */
  1041. if( pxTCB != pxCurrentTCB )
  1042. {
  1043. prvDeleteTCB( pxTCB );
  1044. }
  1045. /* Force a reschedule if it is the currently running task that has just
  1046. * been deleted. */
  1047. if( xSchedulerRunning != pdHV_FALSE )
  1048. {
  1049. if( pxTCB == pxCurrentTCB )
  1050. {
  1051. configASSERT( uxSchedulerSuspended == 0 );
  1052. portYIELD_WITHIN_API();
  1053. }
  1054. else
  1055. {
  1056. mtCOVERAGE_TEST_MARKER();
  1057. }
  1058. }
  1059. }
  1060. #endif /* INCLUDE_vTaskDelete */
  1061. /*-----------------------------------------------------------*/
  1062. #if ( INCLUDE_xTaskDelayUntil == 1 )
  1063. BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
  1064. const TickType_t xTimeIncrement )
  1065. {
  1066. TickType_t xTimeToWake;
  1067. BaseType_t xAlreadyYielded, xShouldDelay = pdHV_FALSE;
  1068. configASSERT( pxPreviousWakeTime );
  1069. configASSERT( ( xTimeIncrement > 0U ) );
  1070. configASSERT( uxSchedulerSuspended == 0 );
  1071. vTaskSuspendAll();
  1072. {
  1073. /* Minor optimisation. The tick count cannot change in this
  1074. * block. */
  1075. const TickType_t xConstTickCount = xTickCount;
  1076. /* Generate the tick time at which the task wants to wake. */
  1077. xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
  1078. if( xConstTickCount < *pxPreviousWakeTime )
  1079. {
  1080. /* The tick count has overflowed since this function was
  1081. * lasted called. In this case the only time we should ever
  1082. * actually delay is if the wake time has also overflowed,
  1083. * and the wake time is greater than the tick time. When this
  1084. * is the case it is as if neither time had overflowed. */
  1085. if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
  1086. {
  1087. xShouldDelay = pdHV_TRUE;
  1088. }
  1089. else
  1090. {
  1091. mtCOVERAGE_TEST_MARKER();
  1092. }
  1093. }
  1094. else
  1095. {
  1096. /* The tick time has not overflowed. In this case we will
  1097. * delay if either the wake time has overflowed, and/or the
  1098. * tick time is less than the wake time. */
  1099. if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
  1100. {
  1101. xShouldDelay = pdHV_TRUE;
  1102. }
  1103. else
  1104. {
  1105. mtCOVERAGE_TEST_MARKER();
  1106. }
  1107. }
  1108. /* Update the wake time ready for the next call. */
  1109. *pxPreviousWakeTime = xTimeToWake;
  1110. if( xShouldDelay != pdHV_FALSE )
  1111. {
  1112. traceTASK_DELAY_UNTIL( xTimeToWake );
  1113. /* prvAddCurrentTaskToDelayedList() needs the block time, not
  1114. * the time to wake, so subtract the current tick count. */
  1115. prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdHV_FALSE );
  1116. }
  1117. else
  1118. {
  1119. mtCOVERAGE_TEST_MARKER();
  1120. }
  1121. }
  1122. xAlreadyYielded = xTaskResumeAll();
  1123. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  1124. * have put ourselves to sleep. */
  1125. if( xAlreadyYielded == pdHV_FALSE )
  1126. {
  1127. portYIELD_WITHIN_API();
  1128. }
  1129. else
  1130. {
  1131. mtCOVERAGE_TEST_MARKER();
  1132. }
  1133. return xShouldDelay;
  1134. }
  1135. #endif /* INCLUDE_xTaskDelayUntil */
  1136. /*-----------------------------------------------------------*/
  1137. #if ( INCLUDE_vTaskDelay == 1 )
  1138. void vTaskDelay( const TickType_t xTicksToDelay )
  1139. {
  1140. BaseType_t xAlreadyYielded = pdHV_FALSE;
  1141. /* A delay time of zero just forces a reschedule. */
  1142. if( xTicksToDelay > ( TickType_t ) 0U )
  1143. {
  1144. configASSERT( uxSchedulerSuspended == 0 );
  1145. vTaskSuspendAll();
  1146. {
  1147. traceTASK_DELAY();
  1148. /* A task that is removed from the event list while the
  1149. * scheduler is suspended will not get placed in the ready
  1150. * list or removed from the blocked list until the scheduler
  1151. * is resumed.
  1152. *
  1153. * This task cannot be in an event list as it is the currently
  1154. * executing task. */
  1155. prvAddCurrentTaskToDelayedList( xTicksToDelay, pdHV_FALSE );
  1156. }
  1157. xAlreadyYielded = xTaskResumeAll();
  1158. }
  1159. else
  1160. {
  1161. mtCOVERAGE_TEST_MARKER();
  1162. }
  1163. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  1164. * have put ourselves to sleep. */
  1165. if( xAlreadyYielded == pdHV_FALSE )
  1166. {
  1167. portYIELD_WITHIN_API();
  1168. }
  1169. else
  1170. {
  1171. mtCOVERAGE_TEST_MARKER();
  1172. }
  1173. }
  1174. #endif /* INCLUDE_vTaskDelay */
  1175. /*-----------------------------------------------------------*/
  1176. #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
  1177. eTaskState eTaskGetState( TaskHandle_t xTask )
  1178. {
  1179. eTaskState eReturn;
  1180. List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
  1181. const TCB_t * const pxTCB = xTask;
  1182. configASSERT( pxTCB );
  1183. if( pxTCB == pxCurrentTCB )
  1184. {
  1185. /* The task calling this function is querying its own state. */
  1186. eReturn = eRunning;
  1187. }
  1188. else
  1189. {
  1190. taskENTER_CRITICAL();
  1191. {
  1192. pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
  1193. pxDelayedList = pxDelayedTaskList;
  1194. pxOverflowedDelayedList = pxOverflowDelayedTaskList;
  1195. }
  1196. taskEXIT_CRITICAL();
  1197. if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
  1198. {
  1199. /* The task being queried is referenced from one of the Blocked
  1200. * lists. */
  1201. eReturn = eBlocked;
  1202. }
  1203. #if ( INCLUDE_vTaskSuspend == 1 )
  1204. else if( pxStateList == &xSuspendedTaskList )
  1205. {
  1206. /* The task being queried is referenced from the suspended
  1207. * list. Is it genuinely suspended or is it blocked
  1208. * indefinitely? */
  1209. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
  1210. {
  1211. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  1212. {
  1213. BaseType_t x;
  1214. /* The task does not appear on the event list item of
  1215. * and of the RTOS objects, but could still be in the
  1216. * blocked state if it is waiting on its notification
  1217. * rather than waiting on an object. If not, is
  1218. * suspended. */
  1219. eReturn = eSuspended;
  1220. for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
  1221. {
  1222. if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
  1223. {
  1224. eReturn = eBlocked;
  1225. break;
  1226. }
  1227. }
  1228. }
  1229. #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  1230. {
  1231. eReturn = eSuspended;
  1232. }
  1233. #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  1234. }
  1235. else
  1236. {
  1237. eReturn = eBlocked;
  1238. }
  1239. }
  1240. #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
  1241. #if ( INCLUDE_vTaskDelete == 1 )
  1242. else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
  1243. {
  1244. /* The task being queried is referenced from the deleted
  1245. * tasks list, or it is not referenced from any lists at
  1246. * all. */
  1247. eReturn = eDeleted;
  1248. }
  1249. #endif
  1250. else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
  1251. {
  1252. /* If the task is not in any other state, it must be in the
  1253. * Ready (including pending ready) state. */
  1254. eReturn = eReady;
  1255. }
  1256. }
  1257. return eReturn;
  1258. } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
  1259. #endif /* INCLUDE_eTaskGetState */
  1260. /*-----------------------------------------------------------*/
  1261. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  1262. UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
  1263. {
  1264. TCB_t const * pxTCB;
  1265. UBaseType_t uxReturn;
  1266. taskENTER_CRITICAL();
  1267. {
  1268. /* If null is passed in here then it is the priority of the task
  1269. * that called uxTaskPriorityGet() that is being queried. */
  1270. pxTCB = prvGetTCBFromHandle( xTask );
  1271. uxReturn = pxTCB->uxPriority;
  1272. }
  1273. taskEXIT_CRITICAL();
  1274. return uxReturn;
  1275. }
  1276. #endif /* INCLUDE_uxTaskPriorityGet */
  1277. /*-----------------------------------------------------------*/
  1278. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  1279. UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
  1280. {
  1281. TCB_t const * pxTCB;
  1282. UBaseType_t uxReturn, uxSavedInterruptState;
  1283. /* RTOS ports that support interrupt nesting have the concept of a
  1284. * maximum system call (or maximum API call) interrupt priority.
  1285. * Interrupts that are above the maximum system call priority are keep
  1286. * permanently enabled, even when the RTOS kernel is in a critical section,
  1287. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  1288. * is defined in FreeRTOSConfig.h then
  1289. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1290. * failure if a FreeRTOS API function is called from an interrupt that has
  1291. * been assigned a priority above the configured maximum system call
  1292. * priority. Only FreeRTOS functions that end in FromISR can be called
  1293. * from interrupts that have been assigned a priority at or (logically)
  1294. * below the maximum system call interrupt priority. FreeRTOS maintains a
  1295. * separate interrupt safe API to ensure interrupt entry is as fast and as
  1296. * simple as possible. More information (albeit Cortex-M specific) is
  1297. * provided on the following link:
  1298. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  1299. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1300. uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
  1301. {
  1302. /* If null is passed in here then it is the priority of the calling
  1303. * task that is being queried. */
  1304. pxTCB = prvGetTCBFromHandle( xTask );
  1305. uxReturn = pxTCB->uxPriority;
  1306. }
  1307. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
  1308. return uxReturn;
  1309. }
  1310. #endif /* INCLUDE_uxTaskPriorityGet */
  1311. /*-----------------------------------------------------------*/
  1312. #if ( INCLUDE_vTaskPrioritySet == 1 )
  1313. void vTaskPrioritySet( TaskHandle_t xTask,
  1314. UBaseType_t uxNewPriority )
  1315. {
  1316. TCB_t * pxTCB;
  1317. UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
  1318. BaseType_t xYieldRequired = pdHV_FALSE;
  1319. configASSERT( uxNewPriority < configMAX_PRIORITIES );
  1320. /* Ensure the new priority is valid. */
  1321. if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  1322. {
  1323. uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  1324. }
  1325. else
  1326. {
  1327. mtCOVERAGE_TEST_MARKER();
  1328. }
  1329. taskENTER_CRITICAL();
  1330. {
  1331. /* If null is passed in here then it is the priority of the calling
  1332. * task that is being changed. */
  1333. pxTCB = prvGetTCBFromHandle( xTask );
  1334. traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
  1335. #if ( configUSE_MUTEXES == 1 )
  1336. {
  1337. uxCurrentBasePriority = pxTCB->uxBasePriority;
  1338. }
  1339. #else
  1340. {
  1341. uxCurrentBasePriority = pxTCB->uxPriority;
  1342. }
  1343. #endif
  1344. if( uxCurrentBasePriority != uxNewPriority )
  1345. {
  1346. /* The priority change may have readied a task of higher
  1347. * priority than the calling task. */
  1348. if( uxNewPriority > uxCurrentBasePriority )
  1349. {
  1350. if( pxTCB != pxCurrentTCB )
  1351. {
  1352. /* The priority of a task other than the currently
  1353. * running task is being raised. Is the priority being
  1354. * raised above that of the running task? */
  1355. if( uxNewPriority >= pxCurrentTCB->uxPriority )
  1356. {
  1357. xYieldRequired = pdHV_TRUE;
  1358. }
  1359. else
  1360. {
  1361. mtCOVERAGE_TEST_MARKER();
  1362. }
  1363. }
  1364. else
  1365. {
  1366. /* The priority of the running task is being raised,
  1367. * but the running task must already be the highest
  1368. * priority task able to run so no yield is required. */
  1369. }
  1370. }
  1371. else if( pxTCB == pxCurrentTCB )
  1372. {
  1373. /* Setting the priority of the running task down means
  1374. * there may now be another task of higher priority that
  1375. * is ready to execute. */
  1376. xYieldRequired = pdHV_TRUE;
  1377. }
  1378. else
  1379. {
  1380. /* Setting the priority of any other task down does not
  1381. * require a yield as the running task must be above the
  1382. * new priority of the task being modified. */
  1383. }
  1384. /* Remember the ready list the task might be referenced from
  1385. * before its uxPriority member is changed so the
  1386. * taskRESET_READY_PRIORITY() macro can function correctly. */
  1387. uxPriorityUsedOnEntry = pxTCB->uxPriority;
  1388. #if ( configUSE_MUTEXES == 1 )
  1389. {
  1390. /* Only change the priority being used if the task is not
  1391. * currently using an inherited priority. */
  1392. if( pxTCB->uxBasePriority == pxTCB->uxPriority )
  1393. {
  1394. pxTCB->uxPriority = uxNewPriority;
  1395. }
  1396. else
  1397. {
  1398. mtCOVERAGE_TEST_MARKER();
  1399. }
  1400. /* The base priority gets set whatever. */
  1401. pxTCB->uxBasePriority = uxNewPriority;
  1402. }
  1403. #else /* if ( configUSE_MUTEXES == 1 ) */
  1404. {
  1405. pxTCB->uxPriority = uxNewPriority;
  1406. }
  1407. #endif /* if ( configUSE_MUTEXES == 1 ) */
  1408. /* Only reset the event list item value if the value is not
  1409. * being used for anything else. */
  1410. if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
  1411. {
  1412. 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. */
  1413. }
  1414. else
  1415. {
  1416. mtCOVERAGE_TEST_MARKER();
  1417. }
  1418. /* If the task is in the blocked or suspended list we need do
  1419. * nothing more than change its priority variable. However, if
  1420. * the task is in a ready list it needs to be removed and placed
  1421. * in the list appropriate to its new priority. */
  1422. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdHV_FALSE )
  1423. {
  1424. /* The task is currently in its ready list - remove before
  1425. * adding it to its new ready list. As we are in a critical
  1426. * section we can do this even if the scheduler is suspended. */
  1427. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  1428. {
  1429. /* It is known that the task is in its ready list so
  1430. * there is no need to check again and the port level
  1431. * reset macro can be called directly. */
  1432. portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
  1433. }
  1434. else
  1435. {
  1436. mtCOVERAGE_TEST_MARKER();
  1437. }
  1438. prvAddTaskToReadyList( pxTCB );
  1439. }
  1440. else
  1441. {
  1442. mtCOVERAGE_TEST_MARKER();
  1443. }
  1444. if( xYieldRequired != pdHV_FALSE )
  1445. {
  1446. taskYIELD_IF_USING_PREEMPTION();
  1447. }
  1448. else
  1449. {
  1450. mtCOVERAGE_TEST_MARKER();
  1451. }
  1452. /* Remove compiler warning about unused variables when the port
  1453. * optimised task selection is not being used. */
  1454. ( void ) uxPriorityUsedOnEntry;
  1455. }
  1456. }
  1457. taskEXIT_CRITICAL();
  1458. }
  1459. #endif /* INCLUDE_vTaskPrioritySet */
  1460. /*-----------------------------------------------------------*/
  1461. #if ( INCLUDE_vTaskSuspend == 1 )
  1462. void vTaskSuspend( TaskHandle_t xTaskToSuspend )
  1463. {
  1464. TCB_t * pxTCB;
  1465. taskENTER_CRITICAL();
  1466. {
  1467. /* If null is passed in here then it is the running task that is
  1468. * being suspended. */
  1469. pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
  1470. traceTASK_SUSPEND( pxTCB );
  1471. /* Remove task from the ready/delayed list and place in the
  1472. * suspended list. */
  1473. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  1474. {
  1475. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  1476. }
  1477. else
  1478. {
  1479. mtCOVERAGE_TEST_MARKER();
  1480. }
  1481. /* Is the task waiting on an event also? */
  1482. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  1483. {
  1484. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  1485. }
  1486. else
  1487. {
  1488. mtCOVERAGE_TEST_MARKER();
  1489. }
  1490. vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
  1491. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  1492. {
  1493. BaseType_t x;
  1494. for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
  1495. {
  1496. if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
  1497. {
  1498. /* The task was blocked to wait for a notification, but is
  1499. * now suspended, so no notification was received. */
  1500. pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
  1501. }
  1502. }
  1503. }
  1504. #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  1505. }
  1506. taskEXIT_CRITICAL();
  1507. if( xSchedulerRunning != pdHV_FALSE )
  1508. {
  1509. /* Reset the next expected unblock time in case it referred to the
  1510. * task that is now in the Suspended state. */
  1511. taskENTER_CRITICAL();
  1512. {
  1513. prvResetNextTaskUnblockTime();
  1514. }
  1515. taskEXIT_CRITICAL();
  1516. }
  1517. else
  1518. {
  1519. mtCOVERAGE_TEST_MARKER();
  1520. }
  1521. if( pxTCB == pxCurrentTCB )
  1522. {
  1523. if( xSchedulerRunning != pdHV_FALSE )
  1524. {
  1525. /* The current task has just been suspended. */
  1526. configASSERT( uxSchedulerSuspended == 0 );
  1527. portYIELD_WITHIN_API();
  1528. }
  1529. else
  1530. {
  1531. /* The scheduler is not running, but the task that was pointed
  1532. * to by pxCurrentTCB has just been suspended and pxCurrentTCB
  1533. * must be adjusted to point to a different task. */
  1534. if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
  1535. {
  1536. /* No other tasks are ready, so set pxCurrentTCB back to
  1537. * NULL so when the next task is created pxCurrentTCB will
  1538. * be set to point to it no matter what its relative priority
  1539. * is. */
  1540. pxCurrentTCB = NULL;
  1541. }
  1542. else
  1543. {
  1544. vTaskSwitchContext();
  1545. }
  1546. }
  1547. }
  1548. else
  1549. {
  1550. mtCOVERAGE_TEST_MARKER();
  1551. }
  1552. }
  1553. #endif /* INCLUDE_vTaskSuspend */
  1554. /*-----------------------------------------------------------*/
  1555. #if ( INCLUDE_vTaskSuspend == 1 )
  1556. static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
  1557. {
  1558. BaseType_t xReturn = pdHV_FALSE;
  1559. const TCB_t * const pxTCB = xTask;
  1560. /* Accesses xPendingReadyList so must be called from a critical
  1561. * section. */
  1562. /* It does not make sense to check if the calling task is suspended. */
  1563. configASSERT( xTask );
  1564. /* Is the task being resumed actually in the suspended list? */
  1565. if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdHV_FALSE )
  1566. {
  1567. /* Has the task already been resumed from within an ISR? */
  1568. if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdHV_FALSE )
  1569. {
  1570. /* Is it in the suspended list because it is in the Suspended
  1571. * state, or because is is blocked with no timeout? */
  1572. if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdHV_FALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
  1573. {
  1574. xReturn = pdHV_TRUE;
  1575. }
  1576. else
  1577. {
  1578. mtCOVERAGE_TEST_MARKER();
  1579. }
  1580. }
  1581. else
  1582. {
  1583. mtCOVERAGE_TEST_MARKER();
  1584. }
  1585. }
  1586. else
  1587. {
  1588. mtCOVERAGE_TEST_MARKER();
  1589. }
  1590. return xReturn;
  1591. } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
  1592. #endif /* INCLUDE_vTaskSuspend */
  1593. /*-----------------------------------------------------------*/
  1594. #if ( INCLUDE_vTaskSuspend == 1 )
  1595. void vTaskResume( TaskHandle_t xTaskToResume )
  1596. {
  1597. TCB_t * const pxTCB = xTaskToResume;
  1598. /* It does not make sense to resume the calling task. */
  1599. configASSERT( xTaskToResume );
  1600. /* The parameter cannot be NULL as it is impossible to resume the
  1601. * currently executing task. */
  1602. if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
  1603. {
  1604. taskENTER_CRITICAL();
  1605. {
  1606. if( prvTaskIsTaskSuspended( pxTCB ) != pdHV_FALSE )
  1607. {
  1608. traceTASK_RESUME( pxTCB );
  1609. /* The ready list can be accessed even if the scheduler is
  1610. * suspended because this is inside a critical section. */
  1611. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  1612. prvAddTaskToReadyList( pxTCB );
  1613. /* A higher priority task may have just been resumed. */
  1614. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1615. {
  1616. /* This yield may not cause the task just resumed to run,
  1617. * but will leave the lists in the correct state for the
  1618. * next yield. */
  1619. taskYIELD_IF_USING_PREEMPTION();
  1620. }
  1621. else
  1622. {
  1623. mtCOVERAGE_TEST_MARKER();
  1624. }
  1625. }
  1626. else
  1627. {
  1628. mtCOVERAGE_TEST_MARKER();
  1629. }
  1630. }
  1631. taskEXIT_CRITICAL();
  1632. }
  1633. else
  1634. {
  1635. mtCOVERAGE_TEST_MARKER();
  1636. }
  1637. }
  1638. #endif /* INCLUDE_vTaskSuspend */
  1639. /*-----------------------------------------------------------*/
  1640. #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
  1641. BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
  1642. {
  1643. BaseType_t xYieldRequired = pdHV_FALSE;
  1644. TCB_t * const pxTCB = xTaskToResume;
  1645. UBaseType_t uxSavedInterruptStatus;
  1646. configASSERT( xTaskToResume );
  1647. /* RTOS ports that support interrupt nesting have the concept of a
  1648. * maximum system call (or maximum API call) interrupt priority.
  1649. * Interrupts that are above the maximum system call priority are keep
  1650. * permanently enabled, even when the RTOS kernel is in a critical section,
  1651. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  1652. * is defined in FreeRTOSConfig.h then
  1653. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1654. * failure if a FreeRTOS API function is called from an interrupt that has
  1655. * been assigned a priority above the configured maximum system call
  1656. * priority. Only FreeRTOS functions that end in FromISR can be called
  1657. * from interrupts that have been assigned a priority at or (logically)
  1658. * below the maximum system call interrupt priority. FreeRTOS maintains a
  1659. * separate interrupt safe API to ensure interrupt entry is as fast and as
  1660. * simple as possible. More information (albeit Cortex-M specific) is
  1661. * provided on the following link:
  1662. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  1663. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1664. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1665. {
  1666. if( prvTaskIsTaskSuspended( pxTCB ) != pdHV_FALSE )
  1667. {
  1668. traceTASK_RESUME_FROM_ISR( pxTCB );
  1669. /* Check the ready lists can be accessed. */
  1670. if( uxSchedulerSuspended == ( UBaseType_t ) pdHV_FALSE )
  1671. {
  1672. /* Ready lists can be accessed so move the task from the
  1673. * suspended list to the ready list directly. */
  1674. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1675. {
  1676. xYieldRequired = pdHV_TRUE;
  1677. /* Mark that a yield is pending in case the user is not
  1678. * using the return value to initiate a context switch
  1679. * from the ISR using portYIELD_FROM_ISR. */
  1680. xYieldPending = pdHV_TRUE;
  1681. }
  1682. else
  1683. {
  1684. mtCOVERAGE_TEST_MARKER();
  1685. }
  1686. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  1687. prvAddTaskToReadyList( pxTCB );
  1688. }
  1689. else
  1690. {
  1691. /* The delayed or ready lists cannot be accessed so the task
  1692. * is held in the pending ready list until the scheduler is
  1693. * unsuspended. */
  1694. vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  1695. }
  1696. }
  1697. else
  1698. {
  1699. mtCOVERAGE_TEST_MARKER();
  1700. }
  1701. }
  1702. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1703. return xYieldRequired;
  1704. }
  1705. #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
  1706. /*-----------------------------------------------------------*/
  1707. void vTaskStartScheduler( void )
  1708. {
  1709. BaseType_t xReturn;
  1710. /* Add the idle task at the lowest priority. */
  1711. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  1712. {
  1713. StaticTask_t * pxIdleTaskTCBBuffer = NULL;
  1714. StackType_t * pxIdleTaskStackBuffer = NULL;
  1715. uint32_t ulIdleTaskStackSize;
  1716. /* The Idle task is created using user provided RAM - obtain the
  1717. * address of the RAM then create the idle task. */
  1718. vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
  1719. xIdleTaskHandle = xTaskCreateStatic( prvIdleTask,
  1720. configIDLE_TASK_NAME,
  1721. ulIdleTaskStackSize,
  1722. ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
  1723. portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
  1724. pxIdleTaskStackBuffer,
  1725. pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
  1726. if( xIdleTaskHandle != NULL )
  1727. {
  1728. xReturn = pdPASS;
  1729. }
  1730. else
  1731. {
  1732. xReturn = pdFAIL;
  1733. }
  1734. }
  1735. #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  1736. {
  1737. /* The Idle task is being created using dynamically allocated RAM. */
  1738. xReturn = xTaskCreate( prvIdleTask,
  1739. configIDLE_TASK_NAME,
  1740. configMINIMAL_STACK_SIZE,
  1741. ( void * ) NULL,
  1742. portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
  1743. &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
  1744. }
  1745. #endif /* configSUPPORT_STATIC_ALLOCATION */
  1746. #if ( configUSE_TIMERS == 1 )
  1747. {
  1748. if( xReturn == pdPASS )
  1749. {
  1750. xReturn = xTimerCreateTimerTask();
  1751. }
  1752. else
  1753. {
  1754. mtCOVERAGE_TEST_MARKER();
  1755. }
  1756. }
  1757. #endif /* configUSE_TIMERS */
  1758. if( xReturn == pdPASS )
  1759. {
  1760. /* freertos_tasks_c_additions_init() should only be called if the user
  1761. * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
  1762. * the only macro called by the function. */
  1763. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  1764. {
  1765. freertos_tasks_c_additions_init();
  1766. }
  1767. #endif
  1768. /* Interrupts are turned off here, to ensure a tick does not occur
  1769. * before or during the call to xPortStartScheduler(). The stacks of
  1770. * the created tasks contain a status word with interrupts switched on
  1771. * so interrupts will automatically get re-enabled when the first task
  1772. * starts to run. */
  1773. portDISABLE_INTERRUPTS();
  1774. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  1775. {
  1776. /* Switch Newlib's _impure_ptr variable to point to the _reent
  1777. * structure specific to the task that will run first.
  1778. * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
  1779. * for additional information. */
  1780. _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
  1781. }
  1782. #endif /* configUSE_NEWLIB_REENTRANT */
  1783. xNextTaskUnblockTime = portMAX_DELAY;
  1784. xSchedulerRunning = pdHV_TRUE;
  1785. xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
  1786. /* If configGENERATE_RUN_TIME_STATS is defined then the following
  1787. * macro must be defined to configure the timer/counter used to generate
  1788. * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
  1789. * is set to 0 and the following line fails to build then ensure you do not
  1790. * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
  1791. * FreeRTOSConfig.h file. */
  1792. portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
  1793. traceTASK_SWITCHED_IN();
  1794. /* Setting up the timer tick is hardware specific and thus in the
  1795. * portable interface. */
  1796. if( xPortStartScheduler() != pdHV_FALSE )
  1797. {
  1798. /* Should not reach here as if the scheduler is running the
  1799. * function will not return. */
  1800. }
  1801. else
  1802. {
  1803. /* Should only reach here if a task calls xTaskEndScheduler(). */
  1804. }
  1805. }
  1806. else
  1807. {
  1808. /* This line will only be reached if the kernel could not be started,
  1809. * because there was not enough FreeRTOS heap to create the idle task
  1810. * or the timer task. */
  1811. configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
  1812. }
  1813. /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
  1814. * meaning xIdleTaskHandle is not used anywhere else. */
  1815. ( void ) xIdleTaskHandle;
  1816. /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
  1817. * from getting optimized out as it is no longer used by the kernel. */
  1818. ( void ) uxTopUsedPriority;
  1819. }
  1820. /*-----------------------------------------------------------*/
  1821. void vTaskEndScheduler( void )
  1822. {
  1823. /* Stop the scheduler interrupts and call the portable scheduler end
  1824. * routine so the original ISRs can be restored if necessary. The port
  1825. * layer must ensure interrupts enable bit is left in the correct state. */
  1826. portDISABLE_INTERRUPTS();
  1827. xSchedulerRunning = pdHV_FALSE;
  1828. vPortEndScheduler();
  1829. }
  1830. /*----------------------------------------------------------*/
  1831. void vTaskSuspendAll( void )
  1832. {
  1833. /* A critical section is not required as the variable is of type
  1834. * BaseType_t. Please read Richard Barry's reply in the following link to a
  1835. * post in the FreeRTOS support forum before reporting this as a bug! -
  1836. * https://goo.gl/wu4acr */
  1837. /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
  1838. * do not otherwise exhibit real time behaviour. */
  1839. portSOFTWARE_BARRIER();
  1840. /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
  1841. * is used to allow calls to vTaskSuspendAll() to nest. */
  1842. ++uxSchedulerSuspended;
  1843. /* Enforces ordering for ports and optimised compilers that may otherwise place
  1844. * the above increment elsewhere. */
  1845. portMEMORY_BARRIER();
  1846. }
  1847. /*----------------------------------------------------------*/
  1848. #if ( configUSE_TICKLESS_IDLE != 0 )
  1849. static TickType_t prvGetExpectedIdleTime( void )
  1850. {
  1851. TickType_t xReturn;
  1852. UBaseType_t uxHigherPriorityReadyTasks = pdHV_FALSE;
  1853. /* uxHigherPriorityReadyTasks takes care of the case where
  1854. * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
  1855. * task that are in the Ready state, even though the idle task is
  1856. * running. */
  1857. #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
  1858. {
  1859. if( uxTopReadyPriority > tskIDLE_PRIORITY )
  1860. {
  1861. uxHigherPriorityReadyTasks = pdHV_TRUE;
  1862. }
  1863. }
  1864. #else
  1865. {
  1866. const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
  1867. /* When port optimised task selection is used the uxTopReadyPriority
  1868. * variable is used as a bit map. If bits other than the least
  1869. * significant bit are set then there are tasks that have a priority
  1870. * above the idle priority that are in the Ready state. This takes
  1871. * care of the case where the co-operative scheduler is in use. */
  1872. if( uxTopReadyPriority > uxLeastSignificantBit )
  1873. {
  1874. uxHigherPriorityReadyTasks = pdHV_TRUE;
  1875. }
  1876. }
  1877. #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
  1878. if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
  1879. {
  1880. xReturn = 0;
  1881. }
  1882. else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
  1883. {
  1884. /* There are other idle priority tasks in the ready state. If
  1885. * time slicing is used then the very next tick interrupt must be
  1886. * processed. */
  1887. xReturn = 0;
  1888. }
  1889. else if( uxHigherPriorityReadyTasks != pdHV_FALSE )
  1890. {
  1891. /* There are tasks in the Ready state that have a priority above the
  1892. * idle priority. This path can only be reached if
  1893. * configUSE_PREEMPTION is 0. */
  1894. xReturn = 0;
  1895. }
  1896. else
  1897. {
  1898. xReturn = xNextTaskUnblockTime - xTickCount;
  1899. }
  1900. return xReturn;
  1901. }
  1902. #endif /* configUSE_TICKLESS_IDLE */
  1903. /*----------------------------------------------------------*/
  1904. BaseType_t xTaskResumeAll( void )
  1905. {
  1906. TCB_t * pxTCB = NULL;
  1907. BaseType_t xAlreadyYielded = pdHV_FALSE;
  1908. /* If uxSchedulerSuspended is zero then this function does not match a
  1909. * previous call to vTaskSuspendAll(). */
  1910. configASSERT( uxSchedulerSuspended );
  1911. /* It is possible that an ISR caused a task to be removed from an event
  1912. * list while the scheduler was suspended. If this was the case then the
  1913. * removed task will have been added to the xPendingReadyList. Once the
  1914. * scheduler has been resumed it is safe to move all the pending ready
  1915. * tasks from this list into their appropriate ready list. */
  1916. taskENTER_CRITICAL();
  1917. {
  1918. --uxSchedulerSuspended;
  1919. if( uxSchedulerSuspended == ( UBaseType_t ) pdHV_FALSE )
  1920. {
  1921. if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
  1922. {
  1923. /* Move any readied tasks from the pending list into the
  1924. * appropriate ready list. */
  1925. while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdHV_FALSE )
  1926. {
  1927. 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. */
  1928. listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
  1929. portMEMORY_BARRIER();
  1930. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  1931. prvAddTaskToReadyList( pxTCB );
  1932. /* If the moved task has a priority higher than or equal to
  1933. * the current task then a yield must be performed. */
  1934. if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
  1935. {
  1936. xYieldPending = pdHV_TRUE;
  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() != pdHV_FALSE )
  1964. {
  1965. xYieldPending = pdHV_TRUE;
  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 != pdHV_FALSE )
  1981. {
  1982. #if ( configUSE_PREEMPTION != 0 )
  1983. {
  1984. xAlreadyYielded = pdHV_TRUE;
  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 = pdHV_FALSE;
  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 = pdHV_TRUE;
  2085. }
  2086. else if( cNextChar == ( char ) 0x00 )
  2087. {
  2088. /* Both strings terminated, a match must have been
  2089. * found. */
  2090. pxReturn = pxNextTCB;
  2091. xBreakLoop = pdHV_TRUE;
  2092. }
  2093. else
  2094. {
  2095. mtCOVERAGE_TEST_MARKER();
  2096. }
  2097. if( xBreakLoop != pdHV_FALSE )
  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. configRUN_TIME_COUNTER_TYPE * 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 = pdHV_TRUE;
  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. * 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 = pdHV_TRUE;
  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 = pdHV_FALSE;
  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 ) pdHV_FALSE )
  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 ) != pdHV_FALSE )
  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 deemed 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. listREMOVE_ITEM( &( 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. listREMOVE_ITEM( &( 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 = pdHV_TRUE;
  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 = pdHV_TRUE;
  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 != pdHV_FALSE )
  2478. {
  2479. xSwitchRequired = pdHV_TRUE;
  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 ) pdHV_FALSE )
  2594. {
  2595. /* The scheduler is currently suspended - do not allow a context
  2596. * switch. */
  2597. xYieldPending = pdHV_TRUE;
  2598. }
  2599. else
  2600. {
  2601. xYieldPending = pdHV_FALSE;
  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.
  2667. *
  2668. * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
  2669. * Normally, the xItemValue of a TCB's ListItem_t members is:
  2670. * xItemValue = ( configMAX_PRIORITIES - uxPriority )
  2671. * Therefore, the event list is sorted in descending priority order.
  2672. *
  2673. * The queue that contains the event list is locked, preventing
  2674. * simultaneous access from interrupts. */
  2675. vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  2676. prvAddCurrentTaskToDelayedList( xTicksToWait, pdHV_TRUE );
  2677. }
  2678. /*-----------------------------------------------------------*/
  2679. void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
  2680. const TickType_t xItemValue,
  2681. const TickType_t xTicksToWait )
  2682. {
  2683. configASSERT( pxEventList );
  2684. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
  2685. * the event groups implementation. */
  2686. configASSERT( uxSchedulerSuspended != 0 );
  2687. /* Store the item value in the event list item. It is safe to access the
  2688. * event list item here as interrupts won't access the event list item of a
  2689. * task that is not in the Blocked state. */
  2690. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
  2691. /* Place the event list item of the TCB at the end of the appropriate event
  2692. * list. It is safe to access the event list here because it is part of an
  2693. * event group implementation - and interrupts don't access event groups
  2694. * directly (instead they access them indirectly by pending function calls to
  2695. * the task level). */
  2696. listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  2697. prvAddCurrentTaskToDelayedList( xTicksToWait, pdHV_TRUE );
  2698. }
  2699. /*-----------------------------------------------------------*/
  2700. #if ( configUSE_TIMERS == 1 )
  2701. void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
  2702. TickType_t xTicksToWait,
  2703. const BaseType_t xWaitIndefinitely )
  2704. {
  2705. configASSERT( pxEventList );
  2706. /* This function should not be called by application code hence the
  2707. * 'Restricted' in its name. It is not part of the public API. It is
  2708. * designed for use by kernel code, and has special calling requirements -
  2709. * it should be called with the scheduler suspended. */
  2710. /* Place the event list item of the TCB in the appropriate event list.
  2711. * In this case it is assume that this is the only task that is going to
  2712. * be waiting on this event list, so the faster vListInsertEnd() function
  2713. * can be used in place of vListInsert. */
  2714. listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  2715. /* If the task should block indefinitely then set the block time to a
  2716. * value that will be recognised as an indefinite delay inside the
  2717. * prvAddCurrentTaskToDelayedList() function. */
  2718. if( xWaitIndefinitely != pdHV_FALSE )
  2719. {
  2720. xTicksToWait = portMAX_DELAY;
  2721. }
  2722. traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
  2723. prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
  2724. }
  2725. #endif /* configUSE_TIMERS */
  2726. /*-----------------------------------------------------------*/
  2727. BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
  2728. {
  2729. TCB_t * pxUnblockedTCB;
  2730. BaseType_t xReturn;
  2731. /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
  2732. * called from a critical section within an ISR. */
  2733. /* The event list is sorted in priority order, so the first in the list can
  2734. * be removed as it is known to be the highest priority. Remove the TCB from
  2735. * the delayed list, and add it to the ready list.
  2736. *
  2737. * If an event is for a queue that is locked then this function will never
  2738. * get called - the lock count on the queue will get modified instead. This
  2739. * means exclusive access to the event list is guaranteed here.
  2740. *
  2741. * This function assumes that a check has already been made to ensure that
  2742. * pxEventList is not empty. */
  2743. 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. */
  2744. configASSERT( pxUnblockedTCB );
  2745. listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
  2746. if( uxSchedulerSuspended == ( UBaseType_t ) pdHV_FALSE )
  2747. {
  2748. listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
  2749. prvAddTaskToReadyList( pxUnblockedTCB );
  2750. #if ( configUSE_TICKLESS_IDLE != 0 )
  2751. {
  2752. /* If a task is blocked on a kernel object then xNextTaskUnblockTime
  2753. * might be set to the blocked task's time out time. If the task is
  2754. * unblocked for a reason other than a timeout xNextTaskUnblockTime is
  2755. * normally left unchanged, because it is automatically reset to a new
  2756. * value when the tick count equals xNextTaskUnblockTime. However if
  2757. * tickless idling is used it might be more important to enter sleep mode
  2758. * at the earliest possible time - so reset xNextTaskUnblockTime here to
  2759. * ensure it is updated at the earliest possible time. */
  2760. prvResetNextTaskUnblockTime();
  2761. }
  2762. #endif
  2763. }
  2764. else
  2765. {
  2766. /* The delayed and ready lists cannot be accessed, so hold this task
  2767. * pending until the scheduler is resumed. */
  2768. listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
  2769. }
  2770. if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
  2771. {
  2772. /* Return true if the task removed from the event list has a higher
  2773. * priority than the calling task. This allows the calling task to know if
  2774. * it should force a context switch now. */
  2775. xReturn = pdHV_TRUE;
  2776. /* Mark that a yield is pending in case the user is not using the
  2777. * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
  2778. xYieldPending = pdHV_TRUE;
  2779. }
  2780. else
  2781. {
  2782. xReturn = pdHV_FALSE;
  2783. }
  2784. return xReturn;
  2785. }
  2786. /*-----------------------------------------------------------*/
  2787. void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
  2788. const TickType_t xItemValue )
  2789. {
  2790. TCB_t * pxUnblockedTCB;
  2791. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
  2792. * the event flags implementation. */
  2793. configASSERT( uxSchedulerSuspended != pdHV_FALSE );
  2794. /* Store the new item value in the event list. */
  2795. listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
  2796. /* Remove the event list form the event flag. Interrupts do not access
  2797. * event flags. */
  2798. 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. */
  2799. configASSERT( pxUnblockedTCB );
  2800. listREMOVE_ITEM( pxEventListItem );
  2801. #if ( configUSE_TICKLESS_IDLE != 0 )
  2802. {
  2803. /* If a task is blocked on a kernel object then xNextTaskUnblockTime
  2804. * might be set to the blocked task's time out time. If the task is
  2805. * unblocked for a reason other than a timeout xNextTaskUnblockTime is
  2806. * normally left unchanged, because it is automatically reset to a new
  2807. * value when the tick count equals xNextTaskUnblockTime. However if
  2808. * tickless idling is used it might be more important to enter sleep mode
  2809. * at the earliest possible time - so reset xNextTaskUnblockTime here to
  2810. * ensure it is updated at the earliest possible time. */
  2811. prvResetNextTaskUnblockTime();
  2812. }
  2813. #endif
  2814. /* Remove the task from the delayed list and add it to the ready list. The
  2815. * scheduler is suspended so interrupts will not be accessing the ready
  2816. * lists. */
  2817. listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
  2818. prvAddTaskToReadyList( pxUnblockedTCB );
  2819. if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
  2820. {
  2821. /* The unblocked task has a priority above that of the calling task, so
  2822. * a context switch is required. This function is called with the
  2823. * scheduler suspended so xYieldPending is set so the context switch
  2824. * occurs immediately that the scheduler is resumed (unsuspended). */
  2825. xYieldPending = pdHV_TRUE;
  2826. }
  2827. }
  2828. /*-----------------------------------------------------------*/
  2829. void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
  2830. {
  2831. configASSERT( pxTimeOut );
  2832. taskENTER_CRITICAL();
  2833. {
  2834. pxTimeOut->xOverflowCount = xNumOfOverflows;
  2835. pxTimeOut->xTimeOnEntering = xTickCount;
  2836. }
  2837. taskEXIT_CRITICAL();
  2838. }
  2839. /*-----------------------------------------------------------*/
  2840. void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
  2841. {
  2842. /* For internal use only as it does not use a critical section. */
  2843. pxTimeOut->xOverflowCount = xNumOfOverflows;
  2844. pxTimeOut->xTimeOnEntering = xTickCount;
  2845. }
  2846. /*-----------------------------------------------------------*/
  2847. BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
  2848. TickType_t * const pxTicksToWait )
  2849. {
  2850. BaseType_t xReturn;
  2851. configASSERT( pxTimeOut );
  2852. configASSERT( pxTicksToWait );
  2853. taskENTER_CRITICAL();
  2854. {
  2855. /* Minor optimisation. The tick count cannot change in this block. */
  2856. const TickType_t xConstTickCount = xTickCount;
  2857. const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
  2858. #if ( INCLUDE_xTaskAbortDelay == 1 )
  2859. if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdHV_FALSE )
  2860. {
  2861. /* The delay was aborted, which is not the same as a time out,
  2862. * but has the same result. */
  2863. pxCurrentTCB->ucDelayAborted = pdHV_FALSE;
  2864. xReturn = pdHV_TRUE;
  2865. }
  2866. else
  2867. #endif
  2868. #if ( INCLUDE_vTaskSuspend == 1 )
  2869. if( *pxTicksToWait == portMAX_DELAY )
  2870. {
  2871. /* If INCLUDE_vTaskSuspend is set to 1 and the block time
  2872. * specified is the maximum block time then the task should block
  2873. * indefinitely, and therefore never time out. */
  2874. xReturn = pdHV_FALSE;
  2875. }
  2876. else
  2877. #endif
  2878. if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
  2879. {
  2880. /* The tick count is greater than the time at which
  2881. * vTaskSetTimeout() was called, but has also overflowed since
  2882. * vTaskSetTimeOut() was called. It must have wrapped all the way
  2883. * around and gone past again. This passed since vTaskSetTimeout()
  2884. * was called. */
  2885. xReturn = pdHV_TRUE;
  2886. *pxTicksToWait = ( TickType_t ) 0;
  2887. }
  2888. else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
  2889. {
  2890. /* Not a genuine timeout. Adjust parameters for time remaining. */
  2891. *pxTicksToWait -= xElapsedTime;
  2892. vTaskInternalSetTimeOutState( pxTimeOut );
  2893. xReturn = pdHV_FALSE;
  2894. }
  2895. else
  2896. {
  2897. *pxTicksToWait = ( TickType_t ) 0;
  2898. xReturn = pdHV_TRUE;
  2899. }
  2900. }
  2901. taskEXIT_CRITICAL();
  2902. return xReturn;
  2903. }
  2904. /*-----------------------------------------------------------*/
  2905. void vTaskMissedYield( void )
  2906. {
  2907. xYieldPending = pdHV_TRUE;
  2908. }
  2909. /*-----------------------------------------------------------*/
  2910. #if ( configUSE_TRACE_FACILITY == 1 )
  2911. UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
  2912. {
  2913. UBaseType_t uxReturn;
  2914. TCB_t const * pxTCB;
  2915. if( xTask != NULL )
  2916. {
  2917. pxTCB = xTask;
  2918. uxReturn = pxTCB->uxTaskNumber;
  2919. }
  2920. else
  2921. {
  2922. uxReturn = 0U;
  2923. }
  2924. return uxReturn;
  2925. }
  2926. #endif /* configUSE_TRACE_FACILITY */
  2927. /*-----------------------------------------------------------*/
  2928. #if ( configUSE_TRACE_FACILITY == 1 )
  2929. void vTaskSetTaskNumber( TaskHandle_t xTask,
  2930. const UBaseType_t uxHandle )
  2931. {
  2932. TCB_t * pxTCB;
  2933. if( xTask != NULL )
  2934. {
  2935. pxTCB = xTask;
  2936. pxTCB->uxTaskNumber = uxHandle;
  2937. }
  2938. }
  2939. #endif /* configUSE_TRACE_FACILITY */
  2940. /*
  2941. * -----------------------------------------------------------
  2942. * The Idle task.
  2943. * ----------------------------------------------------------
  2944. *
  2945. * The portTASK_FUNCTION() macro is used to allow port/compiler specific
  2946. * language extensions. The equivalent prototype for this function is:
  2947. *
  2948. * void prvIdleTask( void *pvParameters );
  2949. *
  2950. */
  2951. static portTASK_FUNCTION( prvIdleTask, pvParameters )
  2952. {
  2953. /* Stop warnings. */
  2954. ( void ) pvParameters;
  2955. /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
  2956. * SCHEDULER IS STARTED. **/
  2957. /* In case a task that has a secure context deletes itself, in which case
  2958. * the idle task is responsible for deleting the task's secure context, if
  2959. * any. */
  2960. portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
  2961. for( ; ; )
  2962. {
  2963. /* See if any tasks have deleted themselves - if so then the idle task
  2964. * is responsible for freeing the deleted task's TCB and stack. */
  2965. prvCheckTasksWaitingTermination();
  2966. #if ( configUSE_PREEMPTION == 0 )
  2967. {
  2968. /* If we are not using preemption we keep forcing a task switch to
  2969. * see if any other task has become available. If we are using
  2970. * preemption we don't need to do this as any task becoming available
  2971. * will automatically get the processor anyway. */
  2972. taskYIELD();
  2973. }
  2974. #endif /* configUSE_PREEMPTION */
  2975. #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
  2976. {
  2977. /* When using preemption tasks of equal priority will be
  2978. * timesliced. If a task that is sharing the idle priority is ready
  2979. * to run then the idle task should yield before the end of the
  2980. * timeslice.
  2981. *
  2982. * A critical region is not required here as we are just reading from
  2983. * the list, and an occasional incorrect value will not matter. If
  2984. * the ready list at the idle priority contains more than one task
  2985. * then a task other than the idle task is ready to execute. */
  2986. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
  2987. {
  2988. taskYIELD();
  2989. }
  2990. else
  2991. {
  2992. mtCOVERAGE_TEST_MARKER();
  2993. }
  2994. }
  2995. #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
  2996. #if ( configUSE_IDLE_HOOK == 1 )
  2997. {
  2998. extern void vApplicationIdleHook( void );
  2999. /* Call the user defined function from within the idle task. This
  3000. * allows the application designer to add background functionality
  3001. * without the overhead of a separate task.
  3002. * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
  3003. * CALL A FUNCTION THAT MIGHT BLOCK. */
  3004. vApplicationIdleHook();
  3005. }
  3006. #endif /* configUSE_IDLE_HOOK */
  3007. /* This conditional compilation should use inequality to 0, not equality
  3008. * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
  3009. * user defined low power mode implementations require
  3010. * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
  3011. #if ( configUSE_TICKLESS_IDLE != 0 )
  3012. {
  3013. TickType_t xExpectedIdleTime;
  3014. /* It is not desirable to suspend then resume the scheduler on
  3015. * each iteration of the idle task. Therefore, a preliminary
  3016. * test of the expected idle time is performed without the
  3017. * scheduler suspended. The result here is not necessarily
  3018. * valid. */
  3019. xExpectedIdleTime = prvGetExpectedIdleTime();
  3020. if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  3021. {
  3022. vTaskSuspendAll();
  3023. {
  3024. /* Now the scheduler is suspended, the expected idle
  3025. * time can be sampled again, and this time its value can
  3026. * be used. */
  3027. configASSERT( xNextTaskUnblockTime >= xTickCount );
  3028. xExpectedIdleTime = prvGetExpectedIdleTime();
  3029. /* Define the following macro to set xExpectedIdleTime to 0
  3030. * if the application does not want
  3031. * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
  3032. configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
  3033. if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  3034. {
  3035. traceLOW_POWER_IDLE_BEGIN();
  3036. portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
  3037. traceLOW_POWER_IDLE_END();
  3038. }
  3039. else
  3040. {
  3041. mtCOVERAGE_TEST_MARKER();
  3042. }
  3043. }
  3044. ( void ) xTaskResumeAll();
  3045. }
  3046. else
  3047. {
  3048. mtCOVERAGE_TEST_MARKER();
  3049. }
  3050. }
  3051. #endif /* configUSE_TICKLESS_IDLE */
  3052. }
  3053. }
  3054. /*-----------------------------------------------------------*/
  3055. #if ( configUSE_TICKLESS_IDLE != 0 )
  3056. eSleepModeStatus eTaskConfirmSleepModeStatus( void )
  3057. {
  3058. /* The idle task exists in addition to the application tasks. */
  3059. const UBaseType_t uxNonApplicationTasks = 1;
  3060. eSleepModeStatus eReturn = eStandardSleep;
  3061. /* This function must be called from a critical section. */
  3062. if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
  3063. {
  3064. /* A task was made ready while the scheduler was suspended. */
  3065. eReturn = eAbortSleep;
  3066. }
  3067. else if( xYieldPending != pdHV_FALSE )
  3068. {
  3069. /* A yield was pended while the scheduler was suspended. */
  3070. eReturn = eAbortSleep;
  3071. }
  3072. else if( xPendedTicks != 0 )
  3073. {
  3074. /* A tick interrupt has already occurred but was held pending
  3075. * because the scheduler is suspended. */
  3076. eReturn = eAbortSleep;
  3077. }
  3078. else
  3079. {
  3080. /* If all the tasks are in the suspended list (which might mean they
  3081. * have an infinite block time rather than actually being suspended)
  3082. * then it is safe to turn all clocks off and just wait for external
  3083. * interrupts. */
  3084. if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
  3085. {
  3086. eReturn = eNoTasksWaitingTimeout;
  3087. }
  3088. else
  3089. {
  3090. mtCOVERAGE_TEST_MARKER();
  3091. }
  3092. }
  3093. return eReturn;
  3094. }
  3095. #endif /* configUSE_TICKLESS_IDLE */
  3096. /*-----------------------------------------------------------*/
  3097. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
  3098. void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
  3099. BaseType_t xIndex,
  3100. void * pvValue )
  3101. {
  3102. TCB_t * pxTCB;
  3103. if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
  3104. {
  3105. pxTCB = prvGetTCBFromHandle( xTaskToSet );
  3106. configASSERT( pxTCB != NULL );
  3107. pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
  3108. }
  3109. }
  3110. #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
  3111. /*-----------------------------------------------------------*/
  3112. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
  3113. void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
  3114. BaseType_t xIndex )
  3115. {
  3116. void * pvReturn = NULL;
  3117. TCB_t * pxTCB;
  3118. if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
  3119. {
  3120. pxTCB = prvGetTCBFromHandle( xTaskToQuery );
  3121. pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
  3122. }
  3123. else
  3124. {
  3125. pvReturn = NULL;
  3126. }
  3127. return pvReturn;
  3128. }
  3129. #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
  3130. /*-----------------------------------------------------------*/
  3131. #if ( portUSING_MPU_WRAPPERS == 1 )
  3132. void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
  3133. const MemoryRegion_t * const xRegions )
  3134. {
  3135. TCB_t * pxTCB;
  3136. /* If null is passed in here then we are modifying the MPU settings of
  3137. * the calling task. */
  3138. pxTCB = prvGetTCBFromHandle( xTaskToModify );
  3139. vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
  3140. }
  3141. #endif /* portUSING_MPU_WRAPPERS */
  3142. /*-----------------------------------------------------------*/
  3143. static void prvInitialiseTaskLists( void )
  3144. {
  3145. UBaseType_t uxPriority;
  3146. for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
  3147. {
  3148. vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
  3149. }
  3150. vListInitialise( &xDelayedTaskList1 );
  3151. vListInitialise( &xDelayedTaskList2 );
  3152. vListInitialise( &xPendingReadyList );
  3153. #if ( INCLUDE_vTaskDelete == 1 )
  3154. {
  3155. vListInitialise( &xTasksWaitingTermination );
  3156. }
  3157. #endif /* INCLUDE_vTaskDelete */
  3158. #if ( INCLUDE_vTaskSuspend == 1 )
  3159. {
  3160. vListInitialise( &xSuspendedTaskList );
  3161. }
  3162. #endif /* INCLUDE_vTaskSuspend */
  3163. /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
  3164. * using list2. */
  3165. pxDelayedTaskList = &xDelayedTaskList1;
  3166. pxOverflowDelayedTaskList = &xDelayedTaskList2;
  3167. }
  3168. /*-----------------------------------------------------------*/
  3169. static void prvCheckTasksWaitingTermination( void )
  3170. {
  3171. /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
  3172. #if ( INCLUDE_vTaskDelete == 1 )
  3173. {
  3174. TCB_t * pxTCB;
  3175. /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
  3176. * being called too often in the idle task. */
  3177. while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
  3178. {
  3179. taskENTER_CRITICAL();
  3180. {
  3181. 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. */
  3182. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  3183. --uxCurrentNumberOfTasks;
  3184. --uxDeletedTasksWaitingCleanUp;
  3185. }
  3186. taskEXIT_CRITICAL();
  3187. prvDeleteTCB( pxTCB );
  3188. }
  3189. }
  3190. #endif /* INCLUDE_vTaskDelete */
  3191. }
  3192. /*-----------------------------------------------------------*/
  3193. #if ( configUSE_TRACE_FACILITY == 1 )
  3194. void vTaskGetInfo( TaskHandle_t xTask,
  3195. TaskStatus_t * pxTaskStatus,
  3196. BaseType_t xGetFreeStackSpace,
  3197. eTaskState eState )
  3198. {
  3199. TCB_t * pxTCB;
  3200. /* xTask is NULL then get the state of the calling task. */
  3201. pxTCB = prvGetTCBFromHandle( xTask );
  3202. pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
  3203. pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
  3204. pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
  3205. pxTaskStatus->pxStackBase = pxTCB->pxStack;
  3206. pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
  3207. #if ( configUSE_MUTEXES == 1 )
  3208. {
  3209. pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
  3210. }
  3211. #else
  3212. {
  3213. pxTaskStatus->uxBasePriority = 0;
  3214. }
  3215. #endif
  3216. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  3217. {
  3218. pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
  3219. }
  3220. #else
  3221. {
  3222. pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
  3223. }
  3224. #endif
  3225. /* Obtaining the task state is a little fiddly, so is only done if the
  3226. * value of eState passed into this function is eInvalid - otherwise the
  3227. * state is just set to whatever is passed in. */
  3228. if( eState != eInvalid )
  3229. {
  3230. if( pxTCB == pxCurrentTCB )
  3231. {
  3232. pxTaskStatus->eCurrentState = eRunning;
  3233. }
  3234. else
  3235. {
  3236. pxTaskStatus->eCurrentState = eState;
  3237. #if ( INCLUDE_vTaskSuspend == 1 )
  3238. {
  3239. /* If the task is in the suspended list then there is a
  3240. * chance it is actually just blocked indefinitely - so really
  3241. * it should be reported as being in the Blocked state. */
  3242. if( eState == eSuspended )
  3243. {
  3244. vTaskSuspendAll();
  3245. {
  3246. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  3247. {
  3248. pxTaskStatus->eCurrentState = eBlocked;
  3249. }
  3250. }
  3251. ( void ) xTaskResumeAll();
  3252. }
  3253. }
  3254. #endif /* INCLUDE_vTaskSuspend */
  3255. }
  3256. }
  3257. else
  3258. {
  3259. pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
  3260. }
  3261. /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
  3262. * parameter is provided to allow it to be skipped. */
  3263. if( xGetFreeStackSpace != pdHV_FALSE )
  3264. {
  3265. #if ( portSTACK_GROWTH > 0 )
  3266. {
  3267. pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
  3268. }
  3269. #else
  3270. {
  3271. pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
  3272. }
  3273. #endif
  3274. }
  3275. else
  3276. {
  3277. pxTaskStatus->usStackHighWaterMark = 0;
  3278. }
  3279. }
  3280. #endif /* configUSE_TRACE_FACILITY */
  3281. /*-----------------------------------------------------------*/
  3282. #if ( configUSE_TRACE_FACILITY == 1 )
  3283. static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
  3284. List_t * pxList,
  3285. eTaskState eState )
  3286. {
  3287. configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
  3288. UBaseType_t uxTask = 0;
  3289. if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
  3290. {
  3291. 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. */
  3292. /* Populate an TaskStatus_t structure within the
  3293. * pxTaskStatusArray array for each task that is referenced from
  3294. * pxList. See the definition of TaskStatus_t in task.h for the
  3295. * meaning of each TaskStatus_t structure member. */
  3296. do
  3297. {
  3298. 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. */
  3299. vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdHV_TRUE, eState );
  3300. uxTask++;
  3301. } while( pxNextTCB != pxFirstTCB );
  3302. }
  3303. else
  3304. {
  3305. mtCOVERAGE_TEST_MARKER();
  3306. }
  3307. return uxTask;
  3308. }
  3309. #endif /* configUSE_TRACE_FACILITY */
  3310. /*-----------------------------------------------------------*/
  3311. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
  3312. static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
  3313. {
  3314. uint32_t ulCount = 0U;
  3315. while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
  3316. {
  3317. pucStackByte -= portSTACK_GROWTH;
  3318. ulCount++;
  3319. }
  3320. ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
  3321. return ( configSTACK_DEPTH_TYPE ) ulCount;
  3322. }
  3323. #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
  3324. /*-----------------------------------------------------------*/
  3325. #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
  3326. /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
  3327. * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
  3328. * user to determine the return type. It gets around the problem of the value
  3329. * overflowing on 8-bit types without breaking backward compatibility for
  3330. * applications that expect an 8-bit return type. */
  3331. configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
  3332. {
  3333. TCB_t * pxTCB;
  3334. uint8_t * pucEndOfStack;
  3335. configSTACK_DEPTH_TYPE uxReturn;
  3336. /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
  3337. * the same except for their return type. Using configSTACK_DEPTH_TYPE
  3338. * allows the user to determine the return type. It gets around the
  3339. * problem of the value overflowing on 8-bit types without breaking
  3340. * backward compatibility for applications that expect an 8-bit return
  3341. * type. */
  3342. pxTCB = prvGetTCBFromHandle( xTask );
  3343. #if portSTACK_GROWTH < 0
  3344. {
  3345. pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
  3346. }
  3347. #else
  3348. {
  3349. pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
  3350. }
  3351. #endif
  3352. uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
  3353. return uxReturn;
  3354. }
  3355. #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
  3356. /*-----------------------------------------------------------*/
  3357. #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
  3358. UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
  3359. {
  3360. TCB_t * pxTCB;
  3361. uint8_t * pucEndOfStack;
  3362. UBaseType_t uxReturn;
  3363. pxTCB = prvGetTCBFromHandle( xTask );
  3364. #if portSTACK_GROWTH < 0
  3365. {
  3366. pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
  3367. }
  3368. #else
  3369. {
  3370. pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
  3371. }
  3372. #endif
  3373. uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
  3374. return uxReturn;
  3375. }
  3376. #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
  3377. /*-----------------------------------------------------------*/
  3378. #if ( INCLUDE_vTaskDelete == 1 )
  3379. static void prvDeleteTCB( TCB_t * pxTCB )
  3380. {
  3381. /* This call is required specifically for the TriCore port. It must be
  3382. * above the vPortFree() calls. The call is also used by ports/demos that
  3383. * want to allocate and clean RAM statically. */
  3384. portCLEAN_UP_TCB( pxTCB );
  3385. /* Free up the memory allocated by the scheduler for the task. It is up
  3386. * to the task to free any memory allocated at the application level.
  3387. * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
  3388. * for additional information. */
  3389. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  3390. {
  3391. _reclaim_reent( &( pxTCB->xNewLib_reent ) );
  3392. }
  3393. #endif /* configUSE_NEWLIB_REENTRANT */
  3394. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
  3395. {
  3396. /* The task can only have been allocated dynamically - free both
  3397. * the stack and TCB. */
  3398. vPortFreeStack( pxTCB->pxStack );
  3399. vPortFree( pxTCB );
  3400. }
  3401. #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
  3402. {
  3403. /* The task could have been allocated statically or dynamically, so
  3404. * check what was statically allocated before trying to free the
  3405. * memory. */
  3406. if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
  3407. {
  3408. /* Both the stack and TCB were allocated dynamically, so both
  3409. * must be freed. */
  3410. vPortFreeStack( pxTCB->pxStack );
  3411. vPortFree( pxTCB );
  3412. }
  3413. else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
  3414. {
  3415. /* Only the stack was statically allocated, so the TCB is the
  3416. * only memory that must be freed. */
  3417. vPortFree( pxTCB );
  3418. }
  3419. else
  3420. {
  3421. /* Neither the stack nor the TCB were allocated dynamically, so
  3422. * nothing needs to be freed. */
  3423. configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
  3424. mtCOVERAGE_TEST_MARKER();
  3425. }
  3426. }
  3427. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  3428. }
  3429. #endif /* INCLUDE_vTaskDelete */
  3430. /*-----------------------------------------------------------*/
  3431. static void prvResetNextTaskUnblockTime( void )
  3432. {
  3433. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdHV_FALSE )
  3434. {
  3435. /* The new current delayed list is empty. Set xNextTaskUnblockTime to
  3436. * the maximum possible value so it is extremely unlikely that the
  3437. * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
  3438. * there is an item in the delayed list. */
  3439. xNextTaskUnblockTime = portMAX_DELAY;
  3440. }
  3441. else
  3442. {
  3443. /* The new current delayed list is not empty, get the value of
  3444. * the item at the head of the delayed list. This is the time at
  3445. * which the task at the head of the delayed list should be removed
  3446. * from the Blocked state. */
  3447. xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
  3448. }
  3449. }
  3450. /*-----------------------------------------------------------*/
  3451. #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
  3452. TaskHandle_t xTaskGetCurrentTaskHandle( void )
  3453. {
  3454. TaskHandle_t xReturn;
  3455. /* A critical section is not required as this is not called from
  3456. * an interrupt and the current TCB will always be the same for any
  3457. * individual execution thread. */
  3458. xReturn = pxCurrentTCB;
  3459. return xReturn;
  3460. }
  3461. #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
  3462. /*-----------------------------------------------------------*/
  3463. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  3464. BaseType_t xTaskGetSchedulerState( void )
  3465. {
  3466. BaseType_t xReturn;
  3467. if( xSchedulerRunning == pdHV_FALSE )
  3468. {
  3469. xReturn = taskSCHEDULER_NOT_STARTED;
  3470. }
  3471. else
  3472. {
  3473. if( uxSchedulerSuspended == ( UBaseType_t ) pdHV_FALSE )
  3474. {
  3475. xReturn = taskSCHEDULER_RUNNING;
  3476. }
  3477. else
  3478. {
  3479. xReturn = taskSCHEDULER_SUSPENDED;
  3480. }
  3481. }
  3482. return xReturn;
  3483. }
  3484. #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
  3485. /*-----------------------------------------------------------*/
  3486. #if ( configUSE_MUTEXES == 1 )
  3487. BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
  3488. {
  3489. TCB_t * const pxMutexHolderTCB = pxMutexHolder;
  3490. BaseType_t xReturn = pdHV_FALSE;
  3491. /* If the mutex was given back by an interrupt while the queue was
  3492. * locked then the mutex holder might now be NULL. _RB_ Is this still
  3493. * needed as interrupts can no longer use mutexes? */
  3494. if( pxMutexHolder != NULL )
  3495. {
  3496. /* If the holder of the mutex has a priority below the priority of
  3497. * the task attempting to obtain the mutex then it will temporarily
  3498. * inherit the priority of the task attempting to obtain the mutex. */
  3499. if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
  3500. {
  3501. /* Adjust the mutex holder state to account for its new
  3502. * priority. Only reset the event list item value if the value is
  3503. * not being used for anything else. */
  3504. if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
  3505. {
  3506. 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. */
  3507. }
  3508. else
  3509. {
  3510. mtCOVERAGE_TEST_MARKER();
  3511. }
  3512. /* If the task being modified is in the ready state it will need
  3513. * to be moved into a new list. */
  3514. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdHV_FALSE )
  3515. {
  3516. if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  3517. {
  3518. /* It is known that the task is in its ready list so
  3519. * there is no need to check again and the port level
  3520. * reset macro can be called directly. */
  3521. portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
  3522. }
  3523. else
  3524. {
  3525. mtCOVERAGE_TEST_MARKER();
  3526. }
  3527. /* Inherit the priority before being moved into the new list. */
  3528. pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
  3529. prvAddTaskToReadyList( pxMutexHolderTCB );
  3530. }
  3531. else
  3532. {
  3533. /* Just inherit the priority. */
  3534. pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
  3535. }
  3536. traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
  3537. /* Inheritance occurred. */
  3538. xReturn = pdHV_TRUE;
  3539. }
  3540. else
  3541. {
  3542. if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
  3543. {
  3544. /* The base priority of the mutex holder is lower than the
  3545. * priority of the task attempting to take the mutex, but the
  3546. * current priority of the mutex holder is not lower than the
  3547. * priority of the task attempting to take the mutex.
  3548. * Therefore the mutex holder must have already inherited a
  3549. * priority, but inheritance would have occurred if that had
  3550. * not been the case. */
  3551. xReturn = pdHV_TRUE;
  3552. }
  3553. else
  3554. {
  3555. mtCOVERAGE_TEST_MARKER();
  3556. }
  3557. }
  3558. }
  3559. else
  3560. {
  3561. mtCOVERAGE_TEST_MARKER();
  3562. }
  3563. return xReturn;
  3564. }
  3565. #endif /* configUSE_MUTEXES */
  3566. /*-----------------------------------------------------------*/
  3567. #if ( configUSE_MUTEXES == 1 )
  3568. BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
  3569. {
  3570. TCB_t * const pxTCB = pxMutexHolder;
  3571. BaseType_t xReturn = pdHV_FALSE;
  3572. if( pxMutexHolder != NULL )
  3573. {
  3574. /* A task can only have an inherited priority if it holds the mutex.
  3575. * If the mutex is held by a task then it cannot be given from an
  3576. * interrupt, and if a mutex is given by the holding task then it must
  3577. * be the running state task. */
  3578. configASSERT( pxTCB == pxCurrentTCB );
  3579. configASSERT( pxTCB->uxMutexesHeld );
  3580. ( pxTCB->uxMutexesHeld )--;
  3581. /* Has the holder of the mutex inherited the priority of another
  3582. * task? */
  3583. if( pxTCB->uxPriority != pxTCB->uxBasePriority )
  3584. {
  3585. /* Only disinherit if no other mutexes are held. */
  3586. if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
  3587. {
  3588. /* A task can only have an inherited priority if it holds
  3589. * the mutex. If the mutex is held by a task then it cannot be
  3590. * given from an interrupt, and if a mutex is given by the
  3591. * holding task then it must be the running state task. Remove
  3592. * the holding task from the ready list. */
  3593. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  3594. {
  3595. portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
  3596. }
  3597. else
  3598. {
  3599. mtCOVERAGE_TEST_MARKER();
  3600. }
  3601. /* Disinherit the priority before adding the task into the
  3602. * new ready list. */
  3603. traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
  3604. pxTCB->uxPriority = pxTCB->uxBasePriority;
  3605. /* Reset the event list item value. It cannot be in use for
  3606. * any other purpose if this task is running, and it must be
  3607. * running to give back the mutex. */
  3608. 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. */
  3609. prvAddTaskToReadyList( pxTCB );
  3610. /* Return true to indicate that a context switch is required.
  3611. * This is only actually required in the corner case whereby
  3612. * multiple mutexes were held and the mutexes were given back
  3613. * in an order different to that in which they were taken.
  3614. * If a context switch did not occur when the first mutex was
  3615. * returned, even if a task was waiting on it, then a context
  3616. * switch should occur when the last mutex is returned whether
  3617. * a task is waiting on it or not. */
  3618. xReturn = pdHV_TRUE;
  3619. }
  3620. else
  3621. {
  3622. mtCOVERAGE_TEST_MARKER();
  3623. }
  3624. }
  3625. else
  3626. {
  3627. mtCOVERAGE_TEST_MARKER();
  3628. }
  3629. }
  3630. else
  3631. {
  3632. mtCOVERAGE_TEST_MARKER();
  3633. }
  3634. return xReturn;
  3635. }
  3636. #endif /* configUSE_MUTEXES */
  3637. /*-----------------------------------------------------------*/
  3638. #if ( configUSE_MUTEXES == 1 )
  3639. void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
  3640. UBaseType_t uxHighestPriorityWaitingTask )
  3641. {
  3642. TCB_t * const pxTCB = pxMutexHolder;
  3643. UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
  3644. const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
  3645. if( pxMutexHolder != NULL )
  3646. {
  3647. /* If pxMutexHolder is not NULL then the holder must hold at least
  3648. * one mutex. */
  3649. configASSERT( pxTCB->uxMutexesHeld );
  3650. /* Determine the priority to which the priority of the task that
  3651. * holds the mutex should be set. This will be the greater of the
  3652. * holding task's base priority and the priority of the highest
  3653. * priority task that is waiting to obtain the mutex. */
  3654. if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
  3655. {
  3656. uxPriorityToUse = uxHighestPriorityWaitingTask;
  3657. }
  3658. else
  3659. {
  3660. uxPriorityToUse = pxTCB->uxBasePriority;
  3661. }
  3662. /* Does the priority need to change? */
  3663. if( pxTCB->uxPriority != uxPriorityToUse )
  3664. {
  3665. /* Only disinherit if no other mutexes are held. This is a
  3666. * simplification in the priority inheritance implementation. If
  3667. * the task that holds the mutex is also holding other mutexes then
  3668. * the other mutexes may have caused the priority inheritance. */
  3669. if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
  3670. {
  3671. /* If a task has timed out because it already holds the
  3672. * mutex it was trying to obtain then it cannot of inherited
  3673. * its own priority. */
  3674. configASSERT( pxTCB != pxCurrentTCB );
  3675. /* Disinherit the priority, remembering the previous
  3676. * priority to facilitate determining the subject task's
  3677. * state. */
  3678. traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
  3679. uxPriorityUsedOnEntry = pxTCB->uxPriority;
  3680. pxTCB->uxPriority = uxPriorityToUse;
  3681. /* Only reset the event list item value if the value is not
  3682. * being used for anything else. */
  3683. if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
  3684. {
  3685. 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. */
  3686. }
  3687. else
  3688. {
  3689. mtCOVERAGE_TEST_MARKER();
  3690. }
  3691. /* If the running task is not the task that holds the mutex
  3692. * then the task that holds the mutex could be in either the
  3693. * Ready, Blocked or Suspended states. Only remove the task
  3694. * from its current state list if it is in the Ready state as
  3695. * the task's priority is going to change and there is one
  3696. * Ready list per priority. */
  3697. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdHV_FALSE )
  3698. {
  3699. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  3700. {
  3701. /* It is known that the task is in its ready list so
  3702. * there is no need to check again and the port level
  3703. * reset macro can be called directly. */
  3704. portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
  3705. }
  3706. else
  3707. {
  3708. mtCOVERAGE_TEST_MARKER();
  3709. }
  3710. prvAddTaskToReadyList( pxTCB );
  3711. }
  3712. else
  3713. {
  3714. mtCOVERAGE_TEST_MARKER();
  3715. }
  3716. }
  3717. else
  3718. {
  3719. mtCOVERAGE_TEST_MARKER();
  3720. }
  3721. }
  3722. else
  3723. {
  3724. mtCOVERAGE_TEST_MARKER();
  3725. }
  3726. }
  3727. else
  3728. {
  3729. mtCOVERAGE_TEST_MARKER();
  3730. }
  3731. }
  3732. #endif /* configUSE_MUTEXES */
  3733. /*-----------------------------------------------------------*/
  3734. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  3735. void vTaskEnterCritical( void )
  3736. {
  3737. portDISABLE_INTERRUPTS();
  3738. if( xSchedulerRunning != pdHV_FALSE )
  3739. {
  3740. ( pxCurrentTCB->uxCriticalNesting )++;
  3741. /* This is not the interrupt safe version of the enter critical
  3742. * function so assert() if it is being called from an interrupt
  3743. * context. Only API functions that end in "FromISR" can be used in an
  3744. * interrupt. Only assert if the critical nesting count is 1 to
  3745. * protect against recursive calls if the assert function also uses a
  3746. * critical section. */
  3747. if( pxCurrentTCB->uxCriticalNesting == 1 )
  3748. {
  3749. portASSERT_IF_IN_ISR();
  3750. }
  3751. }
  3752. else
  3753. {
  3754. mtCOVERAGE_TEST_MARKER();
  3755. }
  3756. }
  3757. #endif /* portCRITICAL_NESTING_IN_TCB */
  3758. /*-----------------------------------------------------------*/
  3759. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  3760. void vTaskExitCritical( void )
  3761. {
  3762. if( xSchedulerRunning != pdHV_FALSE )
  3763. {
  3764. if( pxCurrentTCB->uxCriticalNesting > 0U )
  3765. {
  3766. ( pxCurrentTCB->uxCriticalNesting )--;
  3767. if( pxCurrentTCB->uxCriticalNesting == 0U )
  3768. {
  3769. portENABLE_INTERRUPTS();
  3770. }
  3771. else
  3772. {
  3773. mtCOVERAGE_TEST_MARKER();
  3774. }
  3775. }
  3776. else
  3777. {
  3778. mtCOVERAGE_TEST_MARKER();
  3779. }
  3780. }
  3781. else
  3782. {
  3783. mtCOVERAGE_TEST_MARKER();
  3784. }
  3785. }
  3786. #endif /* portCRITICAL_NESTING_IN_TCB */
  3787. /*-----------------------------------------------------------*/
  3788. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  3789. static char * prvWriteNameToBuffer( char * pcBuffer,
  3790. const char * pcTaskName )
  3791. {
  3792. size_t x;
  3793. /* Start by copying the entire string. */
  3794. strcpy( pcBuffer, pcTaskName );
  3795. /* Pad the end of the string with spaces to ensure columns line up when
  3796. * printed out. */
  3797. for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
  3798. {
  3799. pcBuffer[ x ] = ' ';
  3800. }
  3801. /* Terminate. */
  3802. pcBuffer[ x ] = ( char ) 0x00;
  3803. /* Return the new end of string. */
  3804. return &( pcBuffer[ x ] );
  3805. }
  3806. #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
  3807. /*-----------------------------------------------------------*/
  3808. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  3809. void vTaskList( char * pcWriteBuffer )
  3810. {
  3811. TaskStatus_t * pxTaskStatusArray;
  3812. UBaseType_t uxArraySize, x;
  3813. char cStatus;
  3814. /*
  3815. * PLEASE NOTE:
  3816. *
  3817. * This function is provided for convenience only, and is used by many
  3818. * of the demo applications. Do not consider it to be part of the
  3819. * scheduler.
  3820. *
  3821. * vTaskList() calls uxTaskGetSystemState(), then formats part of the
  3822. * uxTaskGetSystemState() output into a human readable table that
  3823. * displays task: names, states, priority, stack usage and task number.
  3824. * Stack usage specified as the number of unused StackType_t words stack can hold
  3825. * on top of stack - not the number of bytes.
  3826. *
  3827. * vTaskList() has a dependency on the sprintf() C library function that
  3828. * might bloat the code size, use a lot of stack, and provide different
  3829. * results on different platforms. An alternative, tiny, third party,
  3830. * and limited functionality implementation of sprintf() is provided in
  3831. * many of the FreeRTOS/Demo sub-directories in a file called
  3832. * printf-stdarg.c (note printf-stdarg.c does not provide a full
  3833. * snprintf() implementation!).
  3834. *
  3835. * It is recommended that production systems call uxTaskGetSystemState()
  3836. * directly to get access to raw stats data, rather than indirectly
  3837. * through a call to vTaskList().
  3838. */
  3839. /* Make sure the write buffer does not contain a string. */
  3840. *pcWriteBuffer = ( char ) 0x00;
  3841. /* Take a snapshot of the number of tasks in case it changes while this
  3842. * function is executing. */
  3843. uxArraySize = uxCurrentNumberOfTasks;
  3844. /* Allocate an array index for each task. NOTE! if
  3845. * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
  3846. * equate to NULL. */
  3847. 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. */
  3848. if( pxTaskStatusArray != NULL )
  3849. {
  3850. /* Generate the (binary) data. */
  3851. uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
  3852. /* Create a human readable table from the binary data. */
  3853. for( x = 0; x < uxArraySize; x++ )
  3854. {
  3855. switch( pxTaskStatusArray[ x ].eCurrentState )
  3856. {
  3857. case eRunning:
  3858. cStatus = tskRUNNING_CHAR;
  3859. break;
  3860. case eReady:
  3861. cStatus = tskREADY_CHAR;
  3862. break;
  3863. case eBlocked:
  3864. cStatus = tskBLOCKED_CHAR;
  3865. break;
  3866. case eSuspended:
  3867. cStatus = tskSUSPENDED_CHAR;
  3868. break;
  3869. case eDeleted:
  3870. cStatus = tskDELETED_CHAR;
  3871. break;
  3872. case eInvalid: /* Fall through. */
  3873. default: /* Should not get here, but it is included
  3874. * to prevent static checking errors. */
  3875. cStatus = ( char ) 0x00;
  3876. break;
  3877. }
  3878. /* Write the task name to the string, padding with spaces so it
  3879. * can be printed in tabular form more easily. */
  3880. pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
  3881. /* Write the rest of the string. */
  3882. 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. */
  3883. 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. */
  3884. }
  3885. /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
  3886. * is 0 then vPortFree() will be #defined to nothing. */
  3887. vPortFree( pxTaskStatusArray );
  3888. }
  3889. else
  3890. {
  3891. mtCOVERAGE_TEST_MARKER();
  3892. }
  3893. }
  3894. #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  3895. /*----------------------------------------------------------*/
  3896. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  3897. void vTaskGetRunTimeStats( char * pcWriteBuffer )
  3898. {
  3899. TaskStatus_t * pxTaskStatusArray;
  3900. UBaseType_t uxArraySize, x;
  3901. configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
  3902. #if ( configUSE_TRACE_FACILITY != 1 )
  3903. {
  3904. #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
  3905. }
  3906. #endif
  3907. /*
  3908. * PLEASE NOTE:
  3909. *
  3910. * This function is provided for convenience only, and is used by many
  3911. * of the demo applications. Do not consider it to be part of the
  3912. * scheduler.
  3913. *
  3914. * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
  3915. * of the uxTaskGetSystemState() output into a human readable table that
  3916. * displays the amount of time each task has spent in the Running state
  3917. * in both absolute and percentage terms.
  3918. *
  3919. * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
  3920. * function that might bloat the code size, use a lot of stack, and
  3921. * provide different results on different platforms. An alternative,
  3922. * tiny, third party, and limited functionality implementation of
  3923. * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
  3924. * a file called printf-stdarg.c (note printf-stdarg.c does not provide
  3925. * a full snprintf() implementation!).
  3926. *
  3927. * It is recommended that production systems call uxTaskGetSystemState()
  3928. * directly to get access to raw stats data, rather than indirectly
  3929. * through a call to vTaskGetRunTimeStats().
  3930. */
  3931. /* Make sure the write buffer does not contain a string. */
  3932. *pcWriteBuffer = ( char ) 0x00;
  3933. /* Take a snapshot of the number of tasks in case it changes while this
  3934. * function is executing. */
  3935. uxArraySize = uxCurrentNumberOfTasks;
  3936. /* Allocate an array index for each task. NOTE! If
  3937. * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
  3938. * equate to NULL. */
  3939. 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. */
  3940. if( pxTaskStatusArray != NULL )
  3941. {
  3942. /* Generate the (binary) data. */
  3943. uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
  3944. /* For percentage calculations. */
  3945. ulTotalTime /= 100UL;
  3946. /* Avoid divide by zero errors. */
  3947. if( ulTotalTime > 0UL )
  3948. {
  3949. /* Create a human readable table from the binary data. */
  3950. for( x = 0; x < uxArraySize; x++ )
  3951. {
  3952. /* What percentage of the total run time has the task used?
  3953. * This will always be rounded down to the nearest integer.
  3954. * ulTotalRunTime has already been divided by 100. */
  3955. ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
  3956. /* Write the task name to the string, padding with
  3957. * spaces so it can be printed in tabular form more
  3958. * easily. */
  3959. pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
  3960. if( ulStatsAsPercentage > 0UL )
  3961. {
  3962. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  3963. {
  3964. sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
  3965. }
  3966. #else
  3967. {
  3968. /* sizeof( int ) == sizeof( long ) so a smaller
  3969. * printf() library can be used. */
  3970. 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. */
  3971. }
  3972. #endif
  3973. }
  3974. else
  3975. {
  3976. /* If the percentage is zero here then the task has
  3977. * consumed less than 1% of the total run time. */
  3978. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  3979. {
  3980. sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
  3981. }
  3982. #else
  3983. {
  3984. /* sizeof( int ) == sizeof( long ) so a smaller
  3985. * printf() library can be used. */
  3986. 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. */
  3987. }
  3988. #endif
  3989. }
  3990. 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. */
  3991. }
  3992. }
  3993. else
  3994. {
  3995. mtCOVERAGE_TEST_MARKER();
  3996. }
  3997. /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
  3998. * is 0 then vPortFree() will be #defined to nothing. */
  3999. vPortFree( pxTaskStatusArray );
  4000. }
  4001. else
  4002. {
  4003. mtCOVERAGE_TEST_MARKER();
  4004. }
  4005. }
  4006. #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
  4007. /*-----------------------------------------------------------*/
  4008. TickType_t uxTaskResetEventItemValue( void )
  4009. {
  4010. TickType_t uxReturn;
  4011. uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
  4012. /* Reset the event list item to its normal value - so it can be used with
  4013. * queues and semaphores. */
  4014. 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. */
  4015. return uxReturn;
  4016. }
  4017. /*-----------------------------------------------------------*/
  4018. #if ( configUSE_MUTEXES == 1 )
  4019. TaskHandle_t pvTaskIncrementMutexHeldCount( void )
  4020. {
  4021. /* If xSemaphoreCreateMutex() is called before any tasks have been created
  4022. * then pxCurrentTCB will be NULL. */
  4023. if( pxCurrentTCB != NULL )
  4024. {
  4025. ( pxCurrentTCB->uxMutexesHeld )++;
  4026. }
  4027. return pxCurrentTCB;
  4028. }
  4029. #endif /* configUSE_MUTEXES */
  4030. /*-----------------------------------------------------------*/
  4031. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4032. uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
  4033. BaseType_t xClearCountOnExit,
  4034. TickType_t xTicksToWait )
  4035. {
  4036. uint32_t ulReturn;
  4037. configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4038. taskENTER_CRITICAL();
  4039. {
  4040. /* Only block if the notification count is not already non-zero. */
  4041. if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
  4042. {
  4043. /* Mark this task as waiting for a notification. */
  4044. pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
  4045. if( xTicksToWait > ( TickType_t ) 0 )
  4046. {
  4047. prvAddCurrentTaskToDelayedList( xTicksToWait, pdHV_TRUE );
  4048. traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
  4049. /* All ports are written to allow a yield in a critical
  4050. * section (some will yield immediately, others wait until the
  4051. * critical section exits) - but it is not something that
  4052. * application code should ever do. */
  4053. portYIELD_WITHIN_API();
  4054. }
  4055. else
  4056. {
  4057. mtCOVERAGE_TEST_MARKER();
  4058. }
  4059. }
  4060. else
  4061. {
  4062. mtCOVERAGE_TEST_MARKER();
  4063. }
  4064. }
  4065. taskEXIT_CRITICAL();
  4066. taskENTER_CRITICAL();
  4067. {
  4068. traceTASK_NOTIFY_TAKE( uxIndexToWait );
  4069. ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
  4070. if( ulReturn != 0UL )
  4071. {
  4072. if( xClearCountOnExit != pdHV_FALSE )
  4073. {
  4074. pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
  4075. }
  4076. else
  4077. {
  4078. pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
  4079. }
  4080. }
  4081. else
  4082. {
  4083. mtCOVERAGE_TEST_MARKER();
  4084. }
  4085. pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
  4086. }
  4087. taskEXIT_CRITICAL();
  4088. return ulReturn;
  4089. }
  4090. #endif /* configUSE_TASK_NOTIFICATIONS */
  4091. /*-----------------------------------------------------------*/
  4092. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4093. BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
  4094. uint32_t ulBitsToClearOnEntry,
  4095. uint32_t ulBitsToClearOnExit,
  4096. uint32_t * pulNotificationValue,
  4097. TickType_t xTicksToWait )
  4098. {
  4099. BaseType_t xReturn;
  4100. configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4101. taskENTER_CRITICAL();
  4102. {
  4103. /* Only block if a notification is not already pending. */
  4104. if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
  4105. {
  4106. /* Clear bits in the task's notification value as bits may get
  4107. * set by the notifying task or interrupt. This can be used to
  4108. * clear the value to zero. */
  4109. pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
  4110. /* Mark this task as waiting for a notification. */
  4111. pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
  4112. if( xTicksToWait > ( TickType_t ) 0 )
  4113. {
  4114. prvAddCurrentTaskToDelayedList( xTicksToWait, pdHV_TRUE );
  4115. traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
  4116. /* All ports are written to allow a yield in a critical
  4117. * section (some will yield immediately, others wait until the
  4118. * critical section exits) - but it is not something that
  4119. * application code should ever do. */
  4120. portYIELD_WITHIN_API();
  4121. }
  4122. else
  4123. {
  4124. mtCOVERAGE_TEST_MARKER();
  4125. }
  4126. }
  4127. else
  4128. {
  4129. mtCOVERAGE_TEST_MARKER();
  4130. }
  4131. }
  4132. taskEXIT_CRITICAL();
  4133. taskENTER_CRITICAL();
  4134. {
  4135. traceTASK_NOTIFY_WAIT( uxIndexToWait );
  4136. if( pulNotificationValue != NULL )
  4137. {
  4138. /* Output the current notification value, which may or may not
  4139. * have changed. */
  4140. *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
  4141. }
  4142. /* If ucNotifyValue is set then either the task never entered the
  4143. * blocked state (because a notification was already pending) or the
  4144. * task unblocked because of a notification. Otherwise the task
  4145. * unblocked because of a timeout. */
  4146. if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
  4147. {
  4148. /* A notification was not received. */
  4149. xReturn = pdHV_FALSE;
  4150. }
  4151. else
  4152. {
  4153. /* A notification was already pending or a notification was
  4154. * received while the task was waiting. */
  4155. pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
  4156. xReturn = pdHV_TRUE;
  4157. }
  4158. pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
  4159. }
  4160. taskEXIT_CRITICAL();
  4161. return xReturn;
  4162. }
  4163. #endif /* configUSE_TASK_NOTIFICATIONS */
  4164. /*-----------------------------------------------------------*/
  4165. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4166. BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
  4167. UBaseType_t uxIndexToNotify,
  4168. uint32_t ulValue,
  4169. eNotifyAction eAction,
  4170. uint32_t * pulPreviousNotificationValue )
  4171. {
  4172. TCB_t * pxTCB;
  4173. BaseType_t xReturn = pdPASS;
  4174. uint8_t ucOriginalNotifyState;
  4175. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4176. configASSERT( xTaskToNotify );
  4177. pxTCB = xTaskToNotify;
  4178. taskENTER_CRITICAL();
  4179. {
  4180. if( pulPreviousNotificationValue != NULL )
  4181. {
  4182. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
  4183. }
  4184. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  4185. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  4186. switch( eAction )
  4187. {
  4188. case eSetBits:
  4189. pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
  4190. break;
  4191. case eIncrement:
  4192. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  4193. break;
  4194. case eSetValueWithOverwrite:
  4195. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  4196. break;
  4197. case eSetValueWithoutOverwrite:
  4198. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  4199. {
  4200. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  4201. }
  4202. else
  4203. {
  4204. /* The value could not be written to the task. */
  4205. xReturn = pdFAIL;
  4206. }
  4207. break;
  4208. case eNoAction:
  4209. /* The task is being notified without its notify value being
  4210. * updated. */
  4211. break;
  4212. default:
  4213. /* Should not get here if all enums are handled.
  4214. * Artificially force an assert by testing a value the
  4215. * compiler can't assume is const. */
  4216. configASSERT( xTickCount == ( TickType_t ) 0 );
  4217. break;
  4218. }
  4219. traceTASK_NOTIFY( uxIndexToNotify );
  4220. /* If the task is in the blocked state specifically to wait for a
  4221. * notification then unblock it now. */
  4222. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  4223. {
  4224. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  4225. prvAddTaskToReadyList( pxTCB );
  4226. /* The task should not have been on an event list. */
  4227. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  4228. #if ( configUSE_TICKLESS_IDLE != 0 )
  4229. {
  4230. /* If a task is blocked waiting for a notification then
  4231. * xNextTaskUnblockTime might be set to the blocked task's time
  4232. * out time. If the task is unblocked for a reason other than
  4233. * a timeout xNextTaskUnblockTime is normally left unchanged,
  4234. * because it will automatically get reset to a new value when
  4235. * the tick count equals xNextTaskUnblockTime. However if
  4236. * tickless idling is used it might be more important to enter
  4237. * sleep mode at the earliest possible time - so reset
  4238. * xNextTaskUnblockTime here to ensure it is updated at the
  4239. * earliest possible time. */
  4240. prvResetNextTaskUnblockTime();
  4241. }
  4242. #endif
  4243. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  4244. {
  4245. /* The notified task has a priority above the currently
  4246. * executing task so a yield is required. */
  4247. taskYIELD_IF_USING_PREEMPTION();
  4248. }
  4249. else
  4250. {
  4251. mtCOVERAGE_TEST_MARKER();
  4252. }
  4253. }
  4254. else
  4255. {
  4256. mtCOVERAGE_TEST_MARKER();
  4257. }
  4258. }
  4259. taskEXIT_CRITICAL();
  4260. return xReturn;
  4261. }
  4262. #endif /* configUSE_TASK_NOTIFICATIONS */
  4263. /*-----------------------------------------------------------*/
  4264. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4265. BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
  4266. UBaseType_t uxIndexToNotify,
  4267. uint32_t ulValue,
  4268. eNotifyAction eAction,
  4269. uint32_t * pulPreviousNotificationValue,
  4270. BaseType_t * pxHigherPriorityTaskWoken )
  4271. {
  4272. TCB_t * pxTCB;
  4273. uint8_t ucOriginalNotifyState;
  4274. BaseType_t xReturn = pdPASS;
  4275. UBaseType_t uxSavedInterruptStatus;
  4276. configASSERT( xTaskToNotify );
  4277. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4278. /* RTOS ports that support interrupt nesting have the concept of a
  4279. * maximum system call (or maximum API call) interrupt priority.
  4280. * Interrupts that are above the maximum system call priority are keep
  4281. * permanently enabled, even when the RTOS kernel is in a critical section,
  4282. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  4283. * is defined in FreeRTOSConfig.h then
  4284. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  4285. * failure if a FreeRTOS API function is called from an interrupt that has
  4286. * been assigned a priority above the configured maximum system call
  4287. * priority. Only FreeRTOS functions that end in FromISR can be called
  4288. * from interrupts that have been assigned a priority at or (logically)
  4289. * below the maximum system call interrupt priority. FreeRTOS maintains a
  4290. * separate interrupt safe API to ensure interrupt entry is as fast and as
  4291. * simple as possible. More information (albeit Cortex-M specific) is
  4292. * provided on the following link:
  4293. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  4294. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  4295. pxTCB = xTaskToNotify;
  4296. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  4297. {
  4298. if( pulPreviousNotificationValue != NULL )
  4299. {
  4300. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
  4301. }
  4302. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  4303. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  4304. switch( eAction )
  4305. {
  4306. case eSetBits:
  4307. pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
  4308. break;
  4309. case eIncrement:
  4310. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  4311. break;
  4312. case eSetValueWithOverwrite:
  4313. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  4314. break;
  4315. case eSetValueWithoutOverwrite:
  4316. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  4317. {
  4318. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  4319. }
  4320. else
  4321. {
  4322. /* The value could not be written to the task. */
  4323. xReturn = pdFAIL;
  4324. }
  4325. break;
  4326. case eNoAction:
  4327. /* The task is being notified without its notify value being
  4328. * updated. */
  4329. break;
  4330. default:
  4331. /* Should not get here if all enums are handled.
  4332. * Artificially force an assert by testing a value the
  4333. * compiler can't assume is const. */
  4334. configASSERT( xTickCount == ( TickType_t ) 0 );
  4335. break;
  4336. }
  4337. traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
  4338. /* If the task is in the blocked state specifically to wait for a
  4339. * notification then unblock it now. */
  4340. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  4341. {
  4342. /* The task should not have been on an event list. */
  4343. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  4344. if( uxSchedulerSuspended == ( UBaseType_t ) pdHV_FALSE )
  4345. {
  4346. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  4347. prvAddTaskToReadyList( pxTCB );
  4348. }
  4349. else
  4350. {
  4351. /* The delayed and ready lists cannot be accessed, so hold
  4352. * this task pending until the scheduler is resumed. */
  4353. listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  4354. }
  4355. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  4356. {
  4357. /* The notified task has a priority above the currently
  4358. * executing task so a yield is required. */
  4359. if( pxHigherPriorityTaskWoken != NULL )
  4360. {
  4361. *pxHigherPriorityTaskWoken = pdHV_TRUE;
  4362. }
  4363. /* Mark that a yield is pending in case the user is not
  4364. * using the "xHigherPriorityTaskWoken" parameter to an ISR
  4365. * safe FreeRTOS function. */
  4366. xYieldPending = pdHV_TRUE;
  4367. }
  4368. else
  4369. {
  4370. mtCOVERAGE_TEST_MARKER();
  4371. }
  4372. }
  4373. }
  4374. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  4375. return xReturn;
  4376. }
  4377. #endif /* configUSE_TASK_NOTIFICATIONS */
  4378. /*-----------------------------------------------------------*/
  4379. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4380. void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
  4381. UBaseType_t uxIndexToNotify,
  4382. BaseType_t * pxHigherPriorityTaskWoken )
  4383. {
  4384. TCB_t * pxTCB;
  4385. uint8_t ucOriginalNotifyState;
  4386. UBaseType_t uxSavedInterruptStatus;
  4387. configASSERT( xTaskToNotify );
  4388. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4389. /* RTOS ports that support interrupt nesting have the concept of a
  4390. * maximum system call (or maximum API call) interrupt priority.
  4391. * Interrupts that are above the maximum system call priority are keep
  4392. * permanently enabled, even when the RTOS kernel is in a critical section,
  4393. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  4394. * is defined in FreeRTOSConfig.h then
  4395. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  4396. * failure if a FreeRTOS API function is called from an interrupt that has
  4397. * been assigned a priority above the configured maximum system call
  4398. * priority. Only FreeRTOS functions that end in FromISR can be called
  4399. * from interrupts that have been assigned a priority at or (logically)
  4400. * below the maximum system call interrupt priority. FreeRTOS maintains a
  4401. * separate interrupt safe API to ensure interrupt entry is as fast and as
  4402. * simple as possible. More information (albeit Cortex-M specific) is
  4403. * provided on the following link:
  4404. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  4405. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  4406. pxTCB = xTaskToNotify;
  4407. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  4408. {
  4409. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  4410. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  4411. /* 'Giving' is equivalent to incrementing a count in a counting
  4412. * semaphore. */
  4413. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  4414. traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
  4415. /* If the task is in the blocked state specifically to wait for a
  4416. * notification then unblock it now. */
  4417. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  4418. {
  4419. /* The task should not have been on an event list. */
  4420. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  4421. if( uxSchedulerSuspended == ( UBaseType_t ) pdHV_FALSE )
  4422. {
  4423. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  4424. prvAddTaskToReadyList( pxTCB );
  4425. }
  4426. else
  4427. {
  4428. /* The delayed and ready lists cannot be accessed, so hold
  4429. * this task pending until the scheduler is resumed. */
  4430. listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  4431. }
  4432. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  4433. {
  4434. /* The notified task has a priority above the currently
  4435. * executing task so a yield is required. */
  4436. if( pxHigherPriorityTaskWoken != NULL )
  4437. {
  4438. *pxHigherPriorityTaskWoken = pdHV_TRUE;
  4439. }
  4440. /* Mark that a yield is pending in case the user is not
  4441. * using the "xHigherPriorityTaskWoken" parameter in an ISR
  4442. * safe FreeRTOS function. */
  4443. xYieldPending = pdHV_TRUE;
  4444. }
  4445. else
  4446. {
  4447. mtCOVERAGE_TEST_MARKER();
  4448. }
  4449. }
  4450. }
  4451. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  4452. }
  4453. #endif /* configUSE_TASK_NOTIFICATIONS */
  4454. /*-----------------------------------------------------------*/
  4455. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4456. BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
  4457. UBaseType_t uxIndexToClear )
  4458. {
  4459. TCB_t * pxTCB;
  4460. BaseType_t xReturn;
  4461. configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4462. /* If null is passed in here then it is the calling task that is having
  4463. * its notification state cleared. */
  4464. pxTCB = prvGetTCBFromHandle( xTask );
  4465. taskENTER_CRITICAL();
  4466. {
  4467. if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
  4468. {
  4469. pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
  4470. xReturn = pdPASS;
  4471. }
  4472. else
  4473. {
  4474. xReturn = pdFAIL;
  4475. }
  4476. }
  4477. taskEXIT_CRITICAL();
  4478. return xReturn;
  4479. }
  4480. #endif /* configUSE_TASK_NOTIFICATIONS */
  4481. /*-----------------------------------------------------------*/
  4482. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4483. uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
  4484. UBaseType_t uxIndexToClear,
  4485. uint32_t ulBitsToClear )
  4486. {
  4487. TCB_t * pxTCB;
  4488. uint32_t ulReturn;
  4489. /* If null is passed in here then it is the calling task that is having
  4490. * its notification state cleared. */
  4491. pxTCB = prvGetTCBFromHandle( xTask );
  4492. taskENTER_CRITICAL();
  4493. {
  4494. /* Return the notification as it was before the bits were cleared,
  4495. * then clear the bit mask. */
  4496. ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
  4497. pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
  4498. }
  4499. taskEXIT_CRITICAL();
  4500. return ulReturn;
  4501. }
  4502. #endif /* configUSE_TASK_NOTIFICATIONS */
  4503. /*-----------------------------------------------------------*/
  4504. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
  4505. configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
  4506. {
  4507. return xIdleTaskHandle->ulRunTimeCounter;
  4508. }
  4509. #endif
  4510. /*-----------------------------------------------------------*/
  4511. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
  4512. configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
  4513. {
  4514. configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
  4515. ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE();
  4516. /* For percentage calculations. */
  4517. ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
  4518. /* Avoid divide by zero errors. */
  4519. if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
  4520. {
  4521. ulReturn = xIdleTaskHandle->ulRunTimeCounter / ulTotalTime;
  4522. }
  4523. else
  4524. {
  4525. ulReturn = 0;
  4526. }
  4527. return ulReturn;
  4528. }
  4529. #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
  4530. /*-----------------------------------------------------------*/
  4531. static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
  4532. const BaseType_t xCanBlockIndefinitely )
  4533. {
  4534. TickType_t xTimeToWake;
  4535. const TickType_t xConstTickCount = xTickCount;
  4536. #if ( INCLUDE_xTaskAbortDelay == 1 )
  4537. {
  4538. /* About to enter a delayed list, so ensure the ucDelayAborted flag is
  4539. * reset to pdHV_FALSE so it can be detected as having been set to pdHV_TRUE
  4540. * when the task leaves the Blocked state. */
  4541. pxCurrentTCB->ucDelayAborted = pdHV_FALSE;
  4542. }
  4543. #endif
  4544. /* Remove the task from the ready list before adding it to the blocked list
  4545. * as the same list item is used for both lists. */
  4546. if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  4547. {
  4548. /* The current task must be in a ready list, so there is no need to
  4549. * check, and the port reset macro can be called directly. */
  4550. 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. */
  4551. }
  4552. else
  4553. {
  4554. mtCOVERAGE_TEST_MARKER();
  4555. }
  4556. #if ( INCLUDE_vTaskSuspend == 1 )
  4557. {
  4558. if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdHV_FALSE ) )
  4559. {
  4560. /* Add the task to the suspended task list instead of a delayed task
  4561. * list to ensure it is not woken by a timing event. It will block
  4562. * indefinitely. */
  4563. listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
  4564. }
  4565. else
  4566. {
  4567. /* Calculate the time at which the task should be woken if the event
  4568. * does not occur. This may overflow but this doesn't matter, the
  4569. * kernel will manage it correctly. */
  4570. xTimeToWake = xConstTickCount + xTicksToWait;
  4571. /* The list item will be inserted in wake time order. */
  4572. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
  4573. if( xTimeToWake < xConstTickCount )
  4574. {
  4575. /* Wake time has overflowed. Place this item in the overflow
  4576. * list. */
  4577. vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
  4578. }
  4579. else
  4580. {
  4581. /* The wake time has not overflowed, so the current block list
  4582. * is used. */
  4583. vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
  4584. /* If the task entering the blocked state was placed at the
  4585. * head of the list of blocked tasks then xNextTaskUnblockTime
  4586. * needs to be updated too. */
  4587. if( xTimeToWake < xNextTaskUnblockTime )
  4588. {
  4589. xNextTaskUnblockTime = xTimeToWake;
  4590. }
  4591. else
  4592. {
  4593. mtCOVERAGE_TEST_MARKER();
  4594. }
  4595. }
  4596. }
  4597. }
  4598. #else /* INCLUDE_vTaskSuspend */
  4599. {
  4600. /* Calculate the time at which the task should be woken if the event
  4601. * does not occur. This may overflow but this doesn't matter, the kernel
  4602. * will manage it correctly. */
  4603. xTimeToWake = xConstTickCount + xTicksToWait;
  4604. /* The list item will be inserted in wake time order. */
  4605. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
  4606. if( xTimeToWake < xConstTickCount )
  4607. {
  4608. /* Wake time has overflowed. Place this item in the overflow list. */
  4609. vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
  4610. }
  4611. else
  4612. {
  4613. /* The wake time has not overflowed, so the current block list is used. */
  4614. vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
  4615. /* If the task entering the blocked state was placed at the head of the
  4616. * list of blocked tasks then xNextTaskUnblockTime needs to be updated
  4617. * too. */
  4618. if( xTimeToWake < xNextTaskUnblockTime )
  4619. {
  4620. xNextTaskUnblockTime = xTimeToWake;
  4621. }
  4622. else
  4623. {
  4624. mtCOVERAGE_TEST_MARKER();
  4625. }
  4626. }
  4627. /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
  4628. ( void ) xCanBlockIndefinitely;
  4629. }
  4630. #endif /* INCLUDE_vTaskSuspend */
  4631. }
  4632. /* Code below here allows additional code to be inserted into this source file,
  4633. * especially where access to file scope functions and data is needed (for example
  4634. * when performing module tests). */
  4635. #ifdef FREERTOS_MODULE_TEST
  4636. #include "tasks_test_access_functions.h"
  4637. #endif
  4638. #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
  4639. #include "freertos_tasks_c_additions.h"
  4640. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  4641. static void freertos_tasks_c_additions_init( void )
  4642. {
  4643. FREERTOS_TASKS_C_ADDITIONS_INIT();
  4644. }
  4645. #endif
  4646. #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */