tasks.c 217 KB

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