tasks.c 222 KB

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