tasks.c 213 KB

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