stm32l4xx_hal_tim.c 214 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_tim.c
  4. * @author MCD Application Team
  5. * @brief TIM HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Timer (TIM) peripheral:
  8. * + TIM Time Base Initialization
  9. * + TIM Time Base Start
  10. * + TIM Time Base Start Interruption
  11. * + TIM Time Base Start DMA
  12. * + TIM Output Compare/PWM Initialization
  13. * + TIM Output Compare/PWM Channel Configuration
  14. * + TIM Output Compare/PWM Start
  15. * + TIM Output Compare/PWM Start Interruption
  16. * + TIM Output Compare/PWM Start DMA
  17. * + TIM Input Capture Initialization
  18. * + TIM Input Capture Channel Configuration
  19. * + TIM Input Capture Start
  20. * + TIM Input Capture Start Interruption
  21. * + TIM Input Capture Start DMA
  22. * + TIM One Pulse Initialization
  23. * + TIM One Pulse Channel Configuration
  24. * + TIM One Pulse Start
  25. * + TIM Encoder Interface Initialization
  26. * + TIM Encoder Interface Start
  27. * + TIM Encoder Interface Start Interruption
  28. * + TIM Encoder Interface Start DMA
  29. * + Commutation Event configuration with Interruption and DMA
  30. * + TIM OCRef clear configuration
  31. * + TIM External Clock configuration
  32. @verbatim
  33. ==============================================================================
  34. ##### TIMER Generic features #####
  35. ==============================================================================
  36. [..] The Timer features include:
  37. (#) 16-bit up, down, up/down auto-reload counter.
  38. (#) 16-bit programmable prescaler allowing dividing (also on the fly) the
  39. counter clock frequency either by any factor between 1 and 65536.
  40. (#) Up to 4 independent channels for:
  41. (++) Input Capture
  42. (++) Output Compare
  43. (++) PWM generation (Edge and Center-aligned Mode)
  44. (++) One-pulse mode output
  45. (#) Synchronization circuit to control the timer with external signals and to interconnect
  46. several timers together.
  47. (#) Supports incremental encoder for positioning purposes
  48. ##### How to use this driver #####
  49. ==============================================================================
  50. [..]
  51. (#) Initialize the TIM low level resources by implementing the following functions
  52. depending on the selected feature:
  53. (++) Time Base : HAL_TIM_Base_MspInit()
  54. (++) Input Capture : HAL_TIM_IC_MspInit()
  55. (++) Output Compare : HAL_TIM_OC_MspInit()
  56. (++) PWM generation : HAL_TIM_PWM_MspInit()
  57. (++) One-pulse mode output : HAL_TIM_OnePulse_MspInit()
  58. (++) Encoder mode output : HAL_TIM_Encoder_MspInit()
  59. (#) Initialize the TIM low level resources :
  60. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  61. (##) TIM pins configuration
  62. (+++) Enable the clock for the TIM GPIOs using the following function:
  63. __HAL_RCC_GPIOx_CLK_ENABLE();
  64. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  65. (#) The external Clock can be configured, if needed (the default clock is the
  66. internal clock from the APBx), using the following function:
  67. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  68. any start function.
  69. (#) Configure the TIM in the desired functioning mode using one of the
  70. Initialization function of this driver:
  71. (++) HAL_TIM_Base_Init: to use the Timer to generate a simple time base
  72. (++) HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an
  73. Output Compare signal.
  74. (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a
  75. PWM signal.
  76. (++) HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an
  77. external signal.
  78. (++) HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer
  79. in One Pulse Mode.
  80. (++) HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
  81. (#) Activate the TIM peripheral using one of the start functions depending from the feature used:
  82. (++) Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
  83. (++) Input Capture : HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
  84. (++) Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
  85. (++) PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
  86. (++) One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
  87. (++) Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(), HAL_TIM_Encoder_Start_IT().
  88. (#) The DMA Burst is managed with the two following functions:
  89. HAL_TIM_DMABurst_WriteStart()
  90. HAL_TIM_DMABurst_ReadStart()
  91. *** Callback registration ***
  92. =============================================
  93. [..]
  94. The compilation define USE_HAL_TIM_REGISTER_CALLBACKS when set to 1
  95. allows the user to configure dynamically the driver callbacks.
  96. [..]
  97. Use Function @ref HAL_TIM_RegisterCallback() to register a callback.
  98. @ref HAL_TIM_RegisterCallback() takes as parameters the HAL peripheral handle,
  99. the Callback ID and a pointer to the user callback function.
  100. [..]
  101. Use function @ref HAL_TIM_UnRegisterCallback() to reset a callback to the default
  102. weak function.
  103. @ref HAL_TIM_UnRegisterCallback takes as parameters the HAL peripheral handle,
  104. and the Callback ID.
  105. [..]
  106. These functions allow to register/unregister following callbacks:
  107. (+) Base_MspInitCallback : TIM Base Msp Init Callback.
  108. (+) Base_MspDeInitCallback : TIM Base Msp DeInit Callback.
  109. (+) IC_MspInitCallback : TIM IC Msp Init Callback.
  110. (+) IC_MspDeInitCallback : TIM IC Msp DeInit Callback.
  111. (+) OC_MspInitCallback : TIM OC Msp Init Callback.
  112. (+) OC_MspDeInitCallback : TIM OC Msp DeInit Callback.
  113. (+) PWM_MspInitCallback : TIM PWM Msp Init Callback.
  114. (+) PWM_MspDeInitCallback : TIM PWM Msp DeInit Callback.
  115. (+) OnePulse_MspInitCallback : TIM One Pulse Msp Init Callback.
  116. (+) OnePulse_MspDeInitCallback : TIM One Pulse Msp DeInit Callback.
  117. (+) Encoder_MspInitCallback : TIM Encoder Msp Init Callback.
  118. (+) Encoder_MspDeInitCallback : TIM Encoder Msp DeInit Callback.
  119. (+) HallSensor_MspInitCallback : TIM Hall Sensor Msp Init Callback.
  120. (+) HallSensor_MspDeInitCallback : TIM Hall Sensor Msp DeInit Callback.
  121. (+) PeriodElapsedCallback : TIM Period Elapsed Callback.
  122. (+) PeriodElapsedHalfCpltCallback : TIM Period Elapsed half complete Callback.
  123. (+) TriggerCallback : TIM Trigger Callback.
  124. (+) TriggerHalfCpltCallback : TIM Trigger half complete Callback.
  125. (+) IC_CaptureCallback : TIM Input Capture Callback.
  126. (+) IC_CaptureHalfCpltCallback : TIM Input Capture half complete Callback.
  127. (+) OC_DelayElapsedCallback : TIM Output Compare Delay Elapsed Callback.
  128. (+) PWM_PulseFinishedCallback : TIM PWM Pulse Finished Callback.
  129. (+) PWM_PulseFinishedHalfCpltCallback : TIM PWM Pulse Finished half complete Callback.
  130. (+) ErrorCallback : TIM Error Callback.
  131. (+) CommutationCallback : TIM Commutation Callback.
  132. (+) CommutationHalfCpltCallback : TIM Commutation half complete Callback.
  133. (+) BreakCallback : TIM Break Callback.
  134. (+) Break2Callback : TIM Break2 Callback.
  135. [..]
  136. By default, after the Init and when the state is HAL_TIM_STATE_RESET
  137. all interrupt callbacks are set to the corresponding weak functions:
  138. examples @ref HAL_TIM_TriggerCallback(), @ref HAL_TIM_ErrorCallback().
  139. [..]
  140. Exception done for MspInit and MspDeInit functions that are reset to the legacy weak
  141. functionalities in the Init / DeInit only when these callbacks are null
  142. (not registered beforehand). If not, MspInit or MspDeInit are not null, the Init / DeInit
  143. keep and use the user MspInit / MspDeInit callbacks(registered beforehand)
  144. [..]
  145. Callbacks can be registered / unregistered in HAL_TIM_STATE_READY state only.
  146. Exception done MspInit / MspDeInit that can be registered / unregistered
  147. in HAL_TIM_STATE_READY or HAL_TIM_STATE_RESET state,
  148. thus registered(user) MspInit / DeInit callbacks can be used during the Init / DeInit.
  149. In that case first register the MspInit/MspDeInit user callbacks
  150. using @ref HAL_TIM_RegisterCallback() before calling DeInit or Init function.
  151. [..]
  152. When The compilation define USE_HAL_TIM_REGISTER_CALLBACKS is set to 0 or
  153. not defined, the callback registration feature is not available and all callbacks
  154. are set to the corresponding weak functions.
  155. @endverbatim
  156. ******************************************************************************
  157. * @attention
  158. *
  159. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  160. * All rights reserved.</center></h2>
  161. *
  162. * This software component is licensed by ST under BSD 3-Clause license,
  163. * the "License"; You may not use this file except in compliance with the
  164. * License. You may obtain a copy of the License at:
  165. * opensource.org/licenses/BSD-3-Clause
  166. *
  167. ******************************************************************************
  168. */
  169. /* Includes ------------------------------------------------------------------*/
  170. #include "stm32l4xx_hal.h"
  171. /** @addtogroup STM32L4xx_HAL_Driver
  172. * @{
  173. */
  174. /** @defgroup TIM TIM
  175. * @brief TIM HAL module driver
  176. * @{
  177. */
  178. #ifdef HAL_TIM_MODULE_ENABLED
  179. /* Private typedef -----------------------------------------------------------*/
  180. /* Private define ------------------------------------------------------------*/
  181. /* Private macro -------------------------------------------------------------*/
  182. /* Private variables ---------------------------------------------------------*/
  183. /* Private function prototypes -----------------------------------------------*/
  184. /** @addtogroup TIM_Private_Functions
  185. * @{
  186. */
  187. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  188. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  189. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  190. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  191. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  192. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
  193. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  194. uint32_t TIM_ICFilter);
  195. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
  196. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  197. uint32_t TIM_ICFilter);
  198. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  199. uint32_t TIM_ICFilter);
  200. static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource);
  201. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma);
  202. static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma);
  203. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);
  204. static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma);
  205. static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  206. TIM_SlaveConfigTypeDef *sSlaveConfig);
  207. /**
  208. * @}
  209. */
  210. /* Exported functions --------------------------------------------------------*/
  211. /** @defgroup TIM_Exported_Functions TIM Exported Functions
  212. * @{
  213. */
  214. /** @defgroup TIM_Exported_Functions_Group1 TIM Time Base functions
  215. * @brief Time Base functions
  216. *
  217. @verbatim
  218. ==============================================================================
  219. ##### Time Base functions #####
  220. ==============================================================================
  221. [..]
  222. This section provides functions allowing to:
  223. (+) Initialize and configure the TIM base.
  224. (+) De-initialize the TIM base.
  225. (+) Start the Time Base.
  226. (+) Stop the Time Base.
  227. (+) Start the Time Base and enable interrupt.
  228. (+) Stop the Time Base and disable interrupt.
  229. (+) Start the Time Base and enable DMA transfer.
  230. (+) Stop the Time Base and disable DMA transfer.
  231. @endverbatim
  232. * @{
  233. */
  234. /**
  235. * @brief Initializes the TIM Time base Unit according to the specified
  236. * parameters in the TIM_HandleTypeDef and initialize the associated handle.
  237. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  238. * requires a timer reset to avoid unexpected direction
  239. * due to DIR bit readonly in center aligned mode.
  240. * Ex: call @ref HAL_TIM_Base_DeInit() before HAL_TIM_Base_Init()
  241. * @param htim TIM Base handle
  242. * @retval HAL status
  243. */
  244. HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
  245. {
  246. /* Check the TIM handle allocation */
  247. if (htim == NULL)
  248. {
  249. return HAL_ERROR;
  250. }
  251. /* Check the parameters */
  252. assert_param(IS_TIM_INSTANCE(htim->Instance));
  253. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  254. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  255. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  256. if (htim->State == HAL_TIM_STATE_RESET)
  257. {
  258. /* Allocate lock resource and initialize it */
  259. htim->Lock = HAL_UNLOCKED;
  260. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  261. /* Reset interrupt callbacks to legacy weak callbacks */
  262. TIM_ResetCallback(htim);
  263. if (htim->Base_MspInitCallback == NULL)
  264. {
  265. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
  266. }
  267. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  268. htim->Base_MspInitCallback(htim);
  269. #else
  270. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  271. HAL_TIM_Base_MspInit(htim);
  272. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  273. }
  274. /* Set the TIM state */
  275. htim->State = HAL_TIM_STATE_BUSY;
  276. /* Set the Time Base configuration */
  277. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  278. /* Initialize the TIM state*/
  279. htim->State = HAL_TIM_STATE_READY;
  280. return HAL_OK;
  281. }
  282. /**
  283. * @brief DeInitializes the TIM Base peripheral
  284. * @param htim TIM Base handle
  285. * @retval HAL status
  286. */
  287. HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)
  288. {
  289. /* Check the parameters */
  290. assert_param(IS_TIM_INSTANCE(htim->Instance));
  291. htim->State = HAL_TIM_STATE_BUSY;
  292. /* Disable the TIM Peripheral Clock */
  293. __HAL_TIM_DISABLE(htim);
  294. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  295. if (htim->Base_MspDeInitCallback == NULL)
  296. {
  297. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
  298. }
  299. /* DeInit the low level hardware */
  300. htim->Base_MspDeInitCallback(htim);
  301. #else
  302. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  303. HAL_TIM_Base_MspDeInit(htim);
  304. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  305. /* Change TIM state */
  306. htim->State = HAL_TIM_STATE_RESET;
  307. /* Release Lock */
  308. __HAL_UNLOCK(htim);
  309. return HAL_OK;
  310. }
  311. /**
  312. * @brief Initializes the TIM Base MSP.
  313. * @param htim TIM Base handle
  314. * @retval None
  315. */
  316. __weak void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
  317. {
  318. /* Prevent unused argument(s) compilation warning */
  319. UNUSED(htim);
  320. /* NOTE : This function should not be modified, when the callback is needed,
  321. the HAL_TIM_Base_MspInit could be implemented in the user file
  322. */
  323. }
  324. /**
  325. * @brief DeInitializes TIM Base MSP.
  326. * @param htim TIM Base handle
  327. * @retval None
  328. */
  329. __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
  330. {
  331. /* Prevent unused argument(s) compilation warning */
  332. UNUSED(htim);
  333. /* NOTE : This function should not be modified, when the callback is needed,
  334. the HAL_TIM_Base_MspDeInit could be implemented in the user file
  335. */
  336. }
  337. /**
  338. * @brief Starts the TIM Base generation.
  339. * @param htim TIM Base handle
  340. * @retval HAL status
  341. */
  342. HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)
  343. {
  344. uint32_t tmpsmcr;
  345. /* Check the parameters */
  346. assert_param(IS_TIM_INSTANCE(htim->Instance));
  347. /* Set the TIM state */
  348. htim->State = HAL_TIM_STATE_BUSY;
  349. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  350. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  351. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  352. {
  353. __HAL_TIM_ENABLE(htim);
  354. }
  355. /* Change the TIM state*/
  356. htim->State = HAL_TIM_STATE_READY;
  357. /* Return function status */
  358. return HAL_OK;
  359. }
  360. /**
  361. * @brief Stops the TIM Base generation.
  362. * @param htim TIM Base handle
  363. * @retval HAL status
  364. */
  365. HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)
  366. {
  367. /* Check the parameters */
  368. assert_param(IS_TIM_INSTANCE(htim->Instance));
  369. /* Set the TIM state */
  370. htim->State = HAL_TIM_STATE_BUSY;
  371. /* Disable the Peripheral */
  372. __HAL_TIM_DISABLE(htim);
  373. /* Change the TIM state*/
  374. htim->State = HAL_TIM_STATE_READY;
  375. /* Return function status */
  376. return HAL_OK;
  377. }
  378. /**
  379. * @brief Starts the TIM Base generation in interrupt mode.
  380. * @param htim TIM Base handle
  381. * @retval HAL status
  382. */
  383. HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
  384. {
  385. uint32_t tmpsmcr;
  386. /* Check the parameters */
  387. assert_param(IS_TIM_INSTANCE(htim->Instance));
  388. /* Enable the TIM Update interrupt */
  389. __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
  390. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  391. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  392. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  393. {
  394. __HAL_TIM_ENABLE(htim);
  395. }
  396. /* Return function status */
  397. return HAL_OK;
  398. }
  399. /**
  400. * @brief Stops the TIM Base generation in interrupt mode.
  401. * @param htim TIM Base handle
  402. * @retval HAL status
  403. */
  404. HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)
  405. {
  406. /* Check the parameters */
  407. assert_param(IS_TIM_INSTANCE(htim->Instance));
  408. /* Disable the TIM Update interrupt */
  409. __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);
  410. /* Disable the Peripheral */
  411. __HAL_TIM_DISABLE(htim);
  412. /* Return function status */
  413. return HAL_OK;
  414. }
  415. /**
  416. * @brief Starts the TIM Base generation in DMA mode.
  417. * @param htim TIM Base handle
  418. * @param pData The source Buffer address.
  419. * @param Length The length of data to be transferred from memory to peripheral.
  420. * @retval HAL status
  421. */
  422. HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  423. {
  424. uint32_t tmpsmcr;
  425. /* Check the parameters */
  426. assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
  427. if ((htim->State == HAL_TIM_STATE_BUSY))
  428. {
  429. return HAL_BUSY;
  430. }
  431. else if ((htim->State == HAL_TIM_STATE_READY))
  432. {
  433. if ((pData == NULL) && (Length > 0U))
  434. {
  435. return HAL_ERROR;
  436. }
  437. else
  438. {
  439. htim->State = HAL_TIM_STATE_BUSY;
  440. }
  441. }
  442. else
  443. {
  444. /* nothing to do */
  445. }
  446. /* Set the DMA Period elapsed callbacks */
  447. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  448. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  449. /* Set the DMA error callback */
  450. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  451. /* Enable the DMA channel */
  452. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR, Length) != HAL_OK)
  453. {
  454. return HAL_ERROR;
  455. }
  456. /* Enable the TIM Update DMA request */
  457. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);
  458. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  459. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  460. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  461. {
  462. __HAL_TIM_ENABLE(htim);
  463. }
  464. /* Return function status */
  465. return HAL_OK;
  466. }
  467. /**
  468. * @brief Stops the TIM Base generation in DMA mode.
  469. * @param htim TIM Base handle
  470. * @retval HAL status
  471. */
  472. HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)
  473. {
  474. /* Check the parameters */
  475. assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
  476. /* Disable the TIM Update DMA request */
  477. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);
  478. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  479. /* Disable the Peripheral */
  480. __HAL_TIM_DISABLE(htim);
  481. /* Change the htim state */
  482. htim->State = HAL_TIM_STATE_READY;
  483. /* Return function status */
  484. return HAL_OK;
  485. }
  486. /**
  487. * @}
  488. */
  489. /** @defgroup TIM_Exported_Functions_Group2 TIM Output Compare functions
  490. * @brief TIM Output Compare functions
  491. *
  492. @verbatim
  493. ==============================================================================
  494. ##### TIM Output Compare functions #####
  495. ==============================================================================
  496. [..]
  497. This section provides functions allowing to:
  498. (+) Initialize and configure the TIM Output Compare.
  499. (+) De-initialize the TIM Output Compare.
  500. (+) Start the TIM Output Compare.
  501. (+) Stop the TIM Output Compare.
  502. (+) Start the TIM Output Compare and enable interrupt.
  503. (+) Stop the TIM Output Compare and disable interrupt.
  504. (+) Start the TIM Output Compare and enable DMA transfer.
  505. (+) Stop the TIM Output Compare and disable DMA transfer.
  506. @endverbatim
  507. * @{
  508. */
  509. /**
  510. * @brief Initializes the TIM Output Compare according to the specified
  511. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  512. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  513. * requires a timer reset to avoid unexpected direction
  514. * due to DIR bit readonly in center aligned mode.
  515. * Ex: call @ref HAL_TIM_OC_DeInit() before HAL_TIM_OC_Init()
  516. * @param htim TIM Output Compare handle
  517. * @retval HAL status
  518. */
  519. HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim)
  520. {
  521. /* Check the TIM handle allocation */
  522. if (htim == NULL)
  523. {
  524. return HAL_ERROR;
  525. }
  526. /* Check the parameters */
  527. assert_param(IS_TIM_INSTANCE(htim->Instance));
  528. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  529. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  530. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  531. if (htim->State == HAL_TIM_STATE_RESET)
  532. {
  533. /* Allocate lock resource and initialize it */
  534. htim->Lock = HAL_UNLOCKED;
  535. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  536. /* Reset interrupt callbacks to legacy weak callbacks */
  537. TIM_ResetCallback(htim);
  538. if (htim->OC_MspInitCallback == NULL)
  539. {
  540. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
  541. }
  542. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  543. htim->OC_MspInitCallback(htim);
  544. #else
  545. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  546. HAL_TIM_OC_MspInit(htim);
  547. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  548. }
  549. /* Set the TIM state */
  550. htim->State = HAL_TIM_STATE_BUSY;
  551. /* Init the base time for the Output Compare */
  552. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  553. /* Initialize the TIM state*/
  554. htim->State = HAL_TIM_STATE_READY;
  555. return HAL_OK;
  556. }
  557. /**
  558. * @brief DeInitializes the TIM peripheral
  559. * @param htim TIM Output Compare handle
  560. * @retval HAL status
  561. */
  562. HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)
  563. {
  564. /* Check the parameters */
  565. assert_param(IS_TIM_INSTANCE(htim->Instance));
  566. htim->State = HAL_TIM_STATE_BUSY;
  567. /* Disable the TIM Peripheral Clock */
  568. __HAL_TIM_DISABLE(htim);
  569. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  570. if (htim->OC_MspDeInitCallback == NULL)
  571. {
  572. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
  573. }
  574. /* DeInit the low level hardware */
  575. htim->OC_MspDeInitCallback(htim);
  576. #else
  577. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  578. HAL_TIM_OC_MspDeInit(htim);
  579. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  580. /* Change TIM state */
  581. htim->State = HAL_TIM_STATE_RESET;
  582. /* Release Lock */
  583. __HAL_UNLOCK(htim);
  584. return HAL_OK;
  585. }
  586. /**
  587. * @brief Initializes the TIM Output Compare MSP.
  588. * @param htim TIM Output Compare handle
  589. * @retval None
  590. */
  591. __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
  592. {
  593. /* Prevent unused argument(s) compilation warning */
  594. UNUSED(htim);
  595. /* NOTE : This function should not be modified, when the callback is needed,
  596. the HAL_TIM_OC_MspInit could be implemented in the user file
  597. */
  598. }
  599. /**
  600. * @brief DeInitializes TIM Output Compare MSP.
  601. * @param htim TIM Output Compare handle
  602. * @retval None
  603. */
  604. __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
  605. {
  606. /* Prevent unused argument(s) compilation warning */
  607. UNUSED(htim);
  608. /* NOTE : This function should not be modified, when the callback is needed,
  609. the HAL_TIM_OC_MspDeInit could be implemented in the user file
  610. */
  611. }
  612. /**
  613. * @brief Starts the TIM Output Compare signal generation.
  614. * @param htim TIM Output Compare handle
  615. * @param Channel TIM Channel to be enabled
  616. * This parameter can be one of the following values:
  617. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  618. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  619. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  620. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  621. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  622. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  623. * @retval HAL status
  624. */
  625. HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  626. {
  627. uint32_t tmpsmcr;
  628. /* Check the parameters */
  629. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  630. /* Enable the Output compare channel */
  631. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  632. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  633. {
  634. /* Enable the main output */
  635. __HAL_TIM_MOE_ENABLE(htim);
  636. }
  637. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  638. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  639. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  640. {
  641. __HAL_TIM_ENABLE(htim);
  642. }
  643. /* Return function status */
  644. return HAL_OK;
  645. }
  646. /**
  647. * @brief Stops the TIM Output Compare signal generation.
  648. * @param htim TIM Output Compare handle
  649. * @param Channel TIM Channel to be disabled
  650. * This parameter can be one of the following values:
  651. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  652. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  653. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  654. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  655. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  656. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  657. * @retval HAL status
  658. */
  659. HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  660. {
  661. /* Check the parameters */
  662. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  663. /* Disable the Output compare channel */
  664. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  665. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  666. {
  667. /* Disable the Main Output */
  668. __HAL_TIM_MOE_DISABLE(htim);
  669. }
  670. /* Disable the Peripheral */
  671. __HAL_TIM_DISABLE(htim);
  672. /* Return function status */
  673. return HAL_OK;
  674. }
  675. /**
  676. * @brief Starts the TIM Output Compare signal generation in interrupt mode.
  677. * @param htim TIM Output Compare handle
  678. * @param Channel TIM Channel to be enabled
  679. * This parameter can be one of the following values:
  680. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  681. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  682. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  683. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  684. * @retval HAL status
  685. */
  686. HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  687. {
  688. uint32_t tmpsmcr;
  689. /* Check the parameters */
  690. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  691. switch (Channel)
  692. {
  693. case TIM_CHANNEL_1:
  694. {
  695. /* Enable the TIM Capture/Compare 1 interrupt */
  696. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  697. break;
  698. }
  699. case TIM_CHANNEL_2:
  700. {
  701. /* Enable the TIM Capture/Compare 2 interrupt */
  702. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  703. break;
  704. }
  705. case TIM_CHANNEL_3:
  706. {
  707. /* Enable the TIM Capture/Compare 3 interrupt */
  708. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  709. break;
  710. }
  711. case TIM_CHANNEL_4:
  712. {
  713. /* Enable the TIM Capture/Compare 4 interrupt */
  714. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  715. break;
  716. }
  717. default:
  718. break;
  719. }
  720. /* Enable the Output compare channel */
  721. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  722. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  723. {
  724. /* Enable the main output */
  725. __HAL_TIM_MOE_ENABLE(htim);
  726. }
  727. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  728. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  729. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  730. {
  731. __HAL_TIM_ENABLE(htim);
  732. }
  733. /* Return function status */
  734. return HAL_OK;
  735. }
  736. /**
  737. * @brief Stops the TIM Output Compare signal generation in interrupt mode.
  738. * @param htim TIM Output Compare handle
  739. * @param Channel TIM Channel to be disabled
  740. * This parameter can be one of the following values:
  741. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  742. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  743. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  744. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  745. * @retval HAL status
  746. */
  747. HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  748. {
  749. /* Check the parameters */
  750. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  751. switch (Channel)
  752. {
  753. case TIM_CHANNEL_1:
  754. {
  755. /* Disable the TIM Capture/Compare 1 interrupt */
  756. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  757. break;
  758. }
  759. case TIM_CHANNEL_2:
  760. {
  761. /* Disable the TIM Capture/Compare 2 interrupt */
  762. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  763. break;
  764. }
  765. case TIM_CHANNEL_3:
  766. {
  767. /* Disable the TIM Capture/Compare 3 interrupt */
  768. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  769. break;
  770. }
  771. case TIM_CHANNEL_4:
  772. {
  773. /* Disable the TIM Capture/Compare 4 interrupt */
  774. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  775. break;
  776. }
  777. default:
  778. break;
  779. }
  780. /* Disable the Output compare channel */
  781. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  782. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  783. {
  784. /* Disable the Main Output */
  785. __HAL_TIM_MOE_DISABLE(htim);
  786. }
  787. /* Disable the Peripheral */
  788. __HAL_TIM_DISABLE(htim);
  789. /* Return function status */
  790. return HAL_OK;
  791. }
  792. /**
  793. * @brief Starts the TIM Output Compare signal generation in DMA mode.
  794. * @param htim TIM Output Compare handle
  795. * @param Channel TIM Channel to be enabled
  796. * This parameter can be one of the following values:
  797. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  798. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  799. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  800. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  801. * @param pData The source Buffer address.
  802. * @param Length The length of data to be transferred from memory to TIM peripheral
  803. * @retval HAL status
  804. */
  805. HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  806. {
  807. uint32_t tmpsmcr;
  808. /* Check the parameters */
  809. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  810. if ((htim->State == HAL_TIM_STATE_BUSY))
  811. {
  812. return HAL_BUSY;
  813. }
  814. else if ((htim->State == HAL_TIM_STATE_READY))
  815. {
  816. if ((pData == NULL) && (Length > 0U))
  817. {
  818. return HAL_ERROR;
  819. }
  820. else
  821. {
  822. htim->State = HAL_TIM_STATE_BUSY;
  823. }
  824. }
  825. else
  826. {
  827. /* nothing to do */
  828. }
  829. switch (Channel)
  830. {
  831. case TIM_CHANNEL_1:
  832. {
  833. /* Set the DMA compare callbacks */
  834. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  835. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  836. /* Set the DMA error callback */
  837. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  838. /* Enable the DMA channel */
  839. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
  840. {
  841. return HAL_ERROR;
  842. }
  843. /* Enable the TIM Capture/Compare 1 DMA request */
  844. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  845. break;
  846. }
  847. case TIM_CHANNEL_2:
  848. {
  849. /* Set the DMA compare callbacks */
  850. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  851. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  852. /* Set the DMA error callback */
  853. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  854. /* Enable the DMA channel */
  855. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
  856. {
  857. return HAL_ERROR;
  858. }
  859. /* Enable the TIM Capture/Compare 2 DMA request */
  860. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  861. break;
  862. }
  863. case TIM_CHANNEL_3:
  864. {
  865. /* Set the DMA compare callbacks */
  866. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  867. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  868. /* Set the DMA error callback */
  869. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  870. /* Enable the DMA channel */
  871. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
  872. {
  873. return HAL_ERROR;
  874. }
  875. /* Enable the TIM Capture/Compare 3 DMA request */
  876. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  877. break;
  878. }
  879. case TIM_CHANNEL_4:
  880. {
  881. /* Set the DMA compare callbacks */
  882. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  883. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  884. /* Set the DMA error callback */
  885. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  886. /* Enable the DMA channel */
  887. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length) != HAL_OK)
  888. {
  889. return HAL_ERROR;
  890. }
  891. /* Enable the TIM Capture/Compare 4 DMA request */
  892. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  893. break;
  894. }
  895. default:
  896. break;
  897. }
  898. /* Enable the Output compare channel */
  899. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  900. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  901. {
  902. /* Enable the main output */
  903. __HAL_TIM_MOE_ENABLE(htim);
  904. }
  905. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  906. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  907. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  908. {
  909. __HAL_TIM_ENABLE(htim);
  910. }
  911. /* Return function status */
  912. return HAL_OK;
  913. }
  914. /**
  915. * @brief Stops the TIM Output Compare signal generation in DMA mode.
  916. * @param htim TIM Output Compare handle
  917. * @param Channel TIM Channel to be disabled
  918. * This parameter can be one of the following values:
  919. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  920. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  921. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  922. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  923. * @retval HAL status
  924. */
  925. HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  926. {
  927. /* Check the parameters */
  928. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  929. switch (Channel)
  930. {
  931. case TIM_CHANNEL_1:
  932. {
  933. /* Disable the TIM Capture/Compare 1 DMA request */
  934. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  935. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  936. break;
  937. }
  938. case TIM_CHANNEL_2:
  939. {
  940. /* Disable the TIM Capture/Compare 2 DMA request */
  941. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  942. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  943. break;
  944. }
  945. case TIM_CHANNEL_3:
  946. {
  947. /* Disable the TIM Capture/Compare 3 DMA request */
  948. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  949. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  950. break;
  951. }
  952. case TIM_CHANNEL_4:
  953. {
  954. /* Disable the TIM Capture/Compare 4 interrupt */
  955. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  956. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  957. break;
  958. }
  959. default:
  960. break;
  961. }
  962. /* Disable the Output compare channel */
  963. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  964. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  965. {
  966. /* Disable the Main Output */
  967. __HAL_TIM_MOE_DISABLE(htim);
  968. }
  969. /* Disable the Peripheral */
  970. __HAL_TIM_DISABLE(htim);
  971. /* Change the htim state */
  972. htim->State = HAL_TIM_STATE_READY;
  973. /* Return function status */
  974. return HAL_OK;
  975. }
  976. /**
  977. * @}
  978. */
  979. /** @defgroup TIM_Exported_Functions_Group3 TIM PWM functions
  980. * @brief TIM PWM functions
  981. *
  982. @verbatim
  983. ==============================================================================
  984. ##### TIM PWM functions #####
  985. ==============================================================================
  986. [..]
  987. This section provides functions allowing to:
  988. (+) Initialize and configure the TIM PWM.
  989. (+) De-initialize the TIM PWM.
  990. (+) Start the TIM PWM.
  991. (+) Stop the TIM PWM.
  992. (+) Start the TIM PWM and enable interrupt.
  993. (+) Stop the TIM PWM and disable interrupt.
  994. (+) Start the TIM PWM and enable DMA transfer.
  995. (+) Stop the TIM PWM and disable DMA transfer.
  996. @endverbatim
  997. * @{
  998. */
  999. /**
  1000. * @brief Initializes the TIM PWM Time Base according to the specified
  1001. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  1002. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1003. * requires a timer reset to avoid unexpected direction
  1004. * due to DIR bit readonly in center aligned mode.
  1005. * Ex: call @ref HAL_TIM_PWM_DeInit() before HAL_TIM_PWM_Init()
  1006. * @param htim TIM PWM handle
  1007. * @retval HAL status
  1008. */
  1009. HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)
  1010. {
  1011. /* Check the TIM handle allocation */
  1012. if (htim == NULL)
  1013. {
  1014. return HAL_ERROR;
  1015. }
  1016. /* Check the parameters */
  1017. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1018. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1019. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1020. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1021. if (htim->State == HAL_TIM_STATE_RESET)
  1022. {
  1023. /* Allocate lock resource and initialize it */
  1024. htim->Lock = HAL_UNLOCKED;
  1025. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1026. /* Reset interrupt callbacks to legacy weak callbacks */
  1027. TIM_ResetCallback(htim);
  1028. if (htim->PWM_MspInitCallback == NULL)
  1029. {
  1030. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
  1031. }
  1032. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  1033. htim->PWM_MspInitCallback(htim);
  1034. #else
  1035. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1036. HAL_TIM_PWM_MspInit(htim);
  1037. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1038. }
  1039. /* Set the TIM state */
  1040. htim->State = HAL_TIM_STATE_BUSY;
  1041. /* Init the base time for the PWM */
  1042. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1043. /* Initialize the TIM state*/
  1044. htim->State = HAL_TIM_STATE_READY;
  1045. return HAL_OK;
  1046. }
  1047. /**
  1048. * @brief DeInitializes the TIM peripheral
  1049. * @param htim TIM PWM handle
  1050. * @retval HAL status
  1051. */
  1052. HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)
  1053. {
  1054. /* Check the parameters */
  1055. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1056. htim->State = HAL_TIM_STATE_BUSY;
  1057. /* Disable the TIM Peripheral Clock */
  1058. __HAL_TIM_DISABLE(htim);
  1059. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1060. if (htim->PWM_MspDeInitCallback == NULL)
  1061. {
  1062. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
  1063. }
  1064. /* DeInit the low level hardware */
  1065. htim->PWM_MspDeInitCallback(htim);
  1066. #else
  1067. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  1068. HAL_TIM_PWM_MspDeInit(htim);
  1069. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1070. /* Change TIM state */
  1071. htim->State = HAL_TIM_STATE_RESET;
  1072. /* Release Lock */
  1073. __HAL_UNLOCK(htim);
  1074. return HAL_OK;
  1075. }
  1076. /**
  1077. * @brief Initializes the TIM PWM MSP.
  1078. * @param htim TIM PWM handle
  1079. * @retval None
  1080. */
  1081. __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
  1082. {
  1083. /* Prevent unused argument(s) compilation warning */
  1084. UNUSED(htim);
  1085. /* NOTE : This function should not be modified, when the callback is needed,
  1086. the HAL_TIM_PWM_MspInit could be implemented in the user file
  1087. */
  1088. }
  1089. /**
  1090. * @brief DeInitializes TIM PWM MSP.
  1091. * @param htim TIM PWM handle
  1092. * @retval None
  1093. */
  1094. __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)
  1095. {
  1096. /* Prevent unused argument(s) compilation warning */
  1097. UNUSED(htim);
  1098. /* NOTE : This function should not be modified, when the callback is needed,
  1099. the HAL_TIM_PWM_MspDeInit could be implemented in the user file
  1100. */
  1101. }
  1102. /**
  1103. * @brief Starts the PWM signal generation.
  1104. * @param htim TIM handle
  1105. * @param Channel TIM Channels to be enabled
  1106. * This parameter can be one of the following values:
  1107. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1108. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1109. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1110. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1111. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  1112. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  1113. * @retval HAL status
  1114. */
  1115. HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  1116. {
  1117. uint32_t tmpsmcr;
  1118. /* Check the parameters */
  1119. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1120. /* Enable the Capture compare channel */
  1121. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1122. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1123. {
  1124. /* Enable the main output */
  1125. __HAL_TIM_MOE_ENABLE(htim);
  1126. }
  1127. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1128. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1129. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1130. {
  1131. __HAL_TIM_ENABLE(htim);
  1132. }
  1133. /* Return function status */
  1134. return HAL_OK;
  1135. }
  1136. /**
  1137. * @brief Stops the PWM signal generation.
  1138. * @param htim TIM PWM handle
  1139. * @param Channel TIM Channels to be disabled
  1140. * This parameter can be one of the following values:
  1141. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1142. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1143. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1144. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1145. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  1146. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  1147. * @retval HAL status
  1148. */
  1149. HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  1150. {
  1151. /* Check the parameters */
  1152. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1153. /* Disable the Capture compare channel */
  1154. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1155. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1156. {
  1157. /* Disable the Main Output */
  1158. __HAL_TIM_MOE_DISABLE(htim);
  1159. }
  1160. /* Disable the Peripheral */
  1161. __HAL_TIM_DISABLE(htim);
  1162. /* Change the htim state */
  1163. htim->State = HAL_TIM_STATE_READY;
  1164. /* Return function status */
  1165. return HAL_OK;
  1166. }
  1167. /**
  1168. * @brief Starts the PWM signal generation in interrupt mode.
  1169. * @param htim TIM PWM handle
  1170. * @param Channel TIM Channel to be enabled
  1171. * This parameter can be one of the following values:
  1172. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1173. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1174. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1175. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1176. * @retval HAL status
  1177. */
  1178. HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1179. {
  1180. uint32_t tmpsmcr;
  1181. /* Check the parameters */
  1182. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1183. switch (Channel)
  1184. {
  1185. case TIM_CHANNEL_1:
  1186. {
  1187. /* Enable the TIM Capture/Compare 1 interrupt */
  1188. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1189. break;
  1190. }
  1191. case TIM_CHANNEL_2:
  1192. {
  1193. /* Enable the TIM Capture/Compare 2 interrupt */
  1194. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1195. break;
  1196. }
  1197. case TIM_CHANNEL_3:
  1198. {
  1199. /* Enable the TIM Capture/Compare 3 interrupt */
  1200. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1201. break;
  1202. }
  1203. case TIM_CHANNEL_4:
  1204. {
  1205. /* Enable the TIM Capture/Compare 4 interrupt */
  1206. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1207. break;
  1208. }
  1209. default:
  1210. break;
  1211. }
  1212. /* Enable the Capture compare channel */
  1213. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1214. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1215. {
  1216. /* Enable the main output */
  1217. __HAL_TIM_MOE_ENABLE(htim);
  1218. }
  1219. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1220. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1221. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1222. {
  1223. __HAL_TIM_ENABLE(htim);
  1224. }
  1225. /* Return function status */
  1226. return HAL_OK;
  1227. }
  1228. /**
  1229. * @brief Stops the PWM signal generation in interrupt mode.
  1230. * @param htim TIM PWM handle
  1231. * @param Channel TIM Channels to be disabled
  1232. * This parameter can be one of the following values:
  1233. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1234. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1235. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1236. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1237. * @retval HAL status
  1238. */
  1239. HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1240. {
  1241. /* Check the parameters */
  1242. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1243. switch (Channel)
  1244. {
  1245. case TIM_CHANNEL_1:
  1246. {
  1247. /* Disable the TIM Capture/Compare 1 interrupt */
  1248. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1249. break;
  1250. }
  1251. case TIM_CHANNEL_2:
  1252. {
  1253. /* Disable the TIM Capture/Compare 2 interrupt */
  1254. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1255. break;
  1256. }
  1257. case TIM_CHANNEL_3:
  1258. {
  1259. /* Disable the TIM Capture/Compare 3 interrupt */
  1260. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1261. break;
  1262. }
  1263. case TIM_CHANNEL_4:
  1264. {
  1265. /* Disable the TIM Capture/Compare 4 interrupt */
  1266. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1267. break;
  1268. }
  1269. default:
  1270. break;
  1271. }
  1272. /* Disable the Capture compare channel */
  1273. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1274. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1275. {
  1276. /* Disable the Main Output */
  1277. __HAL_TIM_MOE_DISABLE(htim);
  1278. }
  1279. /* Disable the Peripheral */
  1280. __HAL_TIM_DISABLE(htim);
  1281. /* Return function status */
  1282. return HAL_OK;
  1283. }
  1284. /**
  1285. * @brief Starts the TIM PWM signal generation in DMA mode.
  1286. * @param htim TIM PWM handle
  1287. * @param Channel TIM Channels to be enabled
  1288. * This parameter can be one of the following values:
  1289. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1290. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1291. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1292. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1293. * @param pData The source Buffer address.
  1294. * @param Length The length of data to be transferred from memory to TIM peripheral
  1295. * @retval HAL status
  1296. */
  1297. HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  1298. {
  1299. uint32_t tmpsmcr;
  1300. /* Check the parameters */
  1301. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1302. if ((htim->State == HAL_TIM_STATE_BUSY))
  1303. {
  1304. return HAL_BUSY;
  1305. }
  1306. else if ((htim->State == HAL_TIM_STATE_READY))
  1307. {
  1308. if ((pData == NULL) && (Length > 0U))
  1309. {
  1310. return HAL_ERROR;
  1311. }
  1312. else
  1313. {
  1314. htim->State = HAL_TIM_STATE_BUSY;
  1315. }
  1316. }
  1317. else
  1318. {
  1319. /* nothing to do */
  1320. }
  1321. switch (Channel)
  1322. {
  1323. case TIM_CHANNEL_1:
  1324. {
  1325. /* Set the DMA compare callbacks */
  1326. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1327. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1328. /* Set the DMA error callback */
  1329. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  1330. /* Enable the DMA channel */
  1331. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
  1332. {
  1333. return HAL_ERROR;
  1334. }
  1335. /* Enable the TIM Capture/Compare 1 DMA request */
  1336. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1337. break;
  1338. }
  1339. case TIM_CHANNEL_2:
  1340. {
  1341. /* Set the DMA compare callbacks */
  1342. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1343. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1344. /* Set the DMA error callback */
  1345. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  1346. /* Enable the DMA channel */
  1347. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
  1348. {
  1349. return HAL_ERROR;
  1350. }
  1351. /* Enable the TIM Capture/Compare 2 DMA request */
  1352. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1353. break;
  1354. }
  1355. case TIM_CHANNEL_3:
  1356. {
  1357. /* Set the DMA compare callbacks */
  1358. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1359. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1360. /* Set the DMA error callback */
  1361. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  1362. /* Enable the DMA channel */
  1363. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
  1364. {
  1365. return HAL_ERROR;
  1366. }
  1367. /* Enable the TIM Output Capture/Compare 3 request */
  1368. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1369. break;
  1370. }
  1371. case TIM_CHANNEL_4:
  1372. {
  1373. /* Set the DMA compare callbacks */
  1374. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1375. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1376. /* Set the DMA error callback */
  1377. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  1378. /* Enable the DMA channel */
  1379. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length) != HAL_OK)
  1380. {
  1381. return HAL_ERROR;
  1382. }
  1383. /* Enable the TIM Capture/Compare 4 DMA request */
  1384. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1385. break;
  1386. }
  1387. default:
  1388. break;
  1389. }
  1390. /* Enable the Capture compare channel */
  1391. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1392. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1393. {
  1394. /* Enable the main output */
  1395. __HAL_TIM_MOE_ENABLE(htim);
  1396. }
  1397. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1398. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1399. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1400. {
  1401. __HAL_TIM_ENABLE(htim);
  1402. }
  1403. /* Return function status */
  1404. return HAL_OK;
  1405. }
  1406. /**
  1407. * @brief Stops the TIM PWM signal generation in DMA mode.
  1408. * @param htim TIM PWM handle
  1409. * @param Channel TIM Channels to be disabled
  1410. * This parameter can be one of the following values:
  1411. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1412. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1413. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1414. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1415. * @retval HAL status
  1416. */
  1417. HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1418. {
  1419. /* Check the parameters */
  1420. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1421. switch (Channel)
  1422. {
  1423. case TIM_CHANNEL_1:
  1424. {
  1425. /* Disable the TIM Capture/Compare 1 DMA request */
  1426. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1427. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1428. break;
  1429. }
  1430. case TIM_CHANNEL_2:
  1431. {
  1432. /* Disable the TIM Capture/Compare 2 DMA request */
  1433. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1434. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1435. break;
  1436. }
  1437. case TIM_CHANNEL_3:
  1438. {
  1439. /* Disable the TIM Capture/Compare 3 DMA request */
  1440. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1441. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1442. break;
  1443. }
  1444. case TIM_CHANNEL_4:
  1445. {
  1446. /* Disable the TIM Capture/Compare 4 interrupt */
  1447. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1448. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  1449. break;
  1450. }
  1451. default:
  1452. break;
  1453. }
  1454. /* Disable the Capture compare channel */
  1455. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1456. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1457. {
  1458. /* Disable the Main Output */
  1459. __HAL_TIM_MOE_DISABLE(htim);
  1460. }
  1461. /* Disable the Peripheral */
  1462. __HAL_TIM_DISABLE(htim);
  1463. /* Change the htim state */
  1464. htim->State = HAL_TIM_STATE_READY;
  1465. /* Return function status */
  1466. return HAL_OK;
  1467. }
  1468. /**
  1469. * @}
  1470. */
  1471. /** @defgroup TIM_Exported_Functions_Group4 TIM Input Capture functions
  1472. * @brief TIM Input Capture functions
  1473. *
  1474. @verbatim
  1475. ==============================================================================
  1476. ##### TIM Input Capture functions #####
  1477. ==============================================================================
  1478. [..]
  1479. This section provides functions allowing to:
  1480. (+) Initialize and configure the TIM Input Capture.
  1481. (+) De-initialize the TIM Input Capture.
  1482. (+) Start the TIM Input Capture.
  1483. (+) Stop the TIM Input Capture.
  1484. (+) Start the TIM Input Capture and enable interrupt.
  1485. (+) Stop the TIM Input Capture and disable interrupt.
  1486. (+) Start the TIM Input Capture and enable DMA transfer.
  1487. (+) Stop the TIM Input Capture and disable DMA transfer.
  1488. @endverbatim
  1489. * @{
  1490. */
  1491. /**
  1492. * @brief Initializes the TIM Input Capture Time base according to the specified
  1493. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  1494. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1495. * requires a timer reset to avoid unexpected direction
  1496. * due to DIR bit readonly in center aligned mode.
  1497. * Ex: call @ref HAL_TIM_IC_DeInit() before HAL_TIM_IC_Init()
  1498. * @param htim TIM Input Capture handle
  1499. * @retval HAL status
  1500. */
  1501. HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)
  1502. {
  1503. /* Check the TIM handle allocation */
  1504. if (htim == NULL)
  1505. {
  1506. return HAL_ERROR;
  1507. }
  1508. /* Check the parameters */
  1509. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1510. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1511. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1512. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1513. if (htim->State == HAL_TIM_STATE_RESET)
  1514. {
  1515. /* Allocate lock resource and initialize it */
  1516. htim->Lock = HAL_UNLOCKED;
  1517. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1518. /* Reset interrupt callbacks to legacy weak callbacks */
  1519. TIM_ResetCallback(htim);
  1520. if (htim->IC_MspInitCallback == NULL)
  1521. {
  1522. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
  1523. }
  1524. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  1525. htim->IC_MspInitCallback(htim);
  1526. #else
  1527. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1528. HAL_TIM_IC_MspInit(htim);
  1529. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1530. }
  1531. /* Set the TIM state */
  1532. htim->State = HAL_TIM_STATE_BUSY;
  1533. /* Init the base time for the input capture */
  1534. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1535. /* Initialize the TIM state*/
  1536. htim->State = HAL_TIM_STATE_READY;
  1537. return HAL_OK;
  1538. }
  1539. /**
  1540. * @brief DeInitializes the TIM peripheral
  1541. * @param htim TIM Input Capture handle
  1542. * @retval HAL status
  1543. */
  1544. HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)
  1545. {
  1546. /* Check the parameters */
  1547. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1548. htim->State = HAL_TIM_STATE_BUSY;
  1549. /* Disable the TIM Peripheral Clock */
  1550. __HAL_TIM_DISABLE(htim);
  1551. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1552. if (htim->IC_MspDeInitCallback == NULL)
  1553. {
  1554. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
  1555. }
  1556. /* DeInit the low level hardware */
  1557. htim->IC_MspDeInitCallback(htim);
  1558. #else
  1559. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  1560. HAL_TIM_IC_MspDeInit(htim);
  1561. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1562. /* Change TIM state */
  1563. htim->State = HAL_TIM_STATE_RESET;
  1564. /* Release Lock */
  1565. __HAL_UNLOCK(htim);
  1566. return HAL_OK;
  1567. }
  1568. /**
  1569. * @brief Initializes the TIM Input Capture MSP.
  1570. * @param htim TIM Input Capture handle
  1571. * @retval None
  1572. */
  1573. __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
  1574. {
  1575. /* Prevent unused argument(s) compilation warning */
  1576. UNUSED(htim);
  1577. /* NOTE : This function should not be modified, when the callback is needed,
  1578. the HAL_TIM_IC_MspInit could be implemented in the user file
  1579. */
  1580. }
  1581. /**
  1582. * @brief DeInitializes TIM Input Capture MSP.
  1583. * @param htim TIM handle
  1584. * @retval None
  1585. */
  1586. __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)
  1587. {
  1588. /* Prevent unused argument(s) compilation warning */
  1589. UNUSED(htim);
  1590. /* NOTE : This function should not be modified, when the callback is needed,
  1591. the HAL_TIM_IC_MspDeInit could be implemented in the user file
  1592. */
  1593. }
  1594. /**
  1595. * @brief Starts the TIM Input Capture measurement.
  1596. * @param htim TIM Input Capture handle
  1597. * @param Channel TIM Channels to be enabled
  1598. * This parameter can be one of the following values:
  1599. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1600. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1601. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1602. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1603. * @retval HAL status
  1604. */
  1605. HAL_StatusTypeDef HAL_TIM_IC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  1606. {
  1607. uint32_t tmpsmcr;
  1608. /* Check the parameters */
  1609. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1610. /* Enable the Input Capture channel */
  1611. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1612. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1613. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1614. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1615. {
  1616. __HAL_TIM_ENABLE(htim);
  1617. }
  1618. /* Return function status */
  1619. return HAL_OK;
  1620. }
  1621. /**
  1622. * @brief Stops the TIM Input Capture measurement.
  1623. * @param htim TIM Input Capture handle
  1624. * @param Channel TIM Channels to be disabled
  1625. * This parameter can be one of the following values:
  1626. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1627. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1628. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1629. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1630. * @retval HAL status
  1631. */
  1632. HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  1633. {
  1634. /* Check the parameters */
  1635. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1636. /* Disable the Input Capture channel */
  1637. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1638. /* Disable the Peripheral */
  1639. __HAL_TIM_DISABLE(htim);
  1640. /* Return function status */
  1641. return HAL_OK;
  1642. }
  1643. /**
  1644. * @brief Starts the TIM Input Capture measurement in interrupt mode.
  1645. * @param htim TIM Input Capture handle
  1646. * @param Channel TIM Channels to be enabled
  1647. * This parameter can be one of the following values:
  1648. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1649. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1650. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1651. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1652. * @retval HAL status
  1653. */
  1654. HAL_StatusTypeDef HAL_TIM_IC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1655. {
  1656. uint32_t tmpsmcr;
  1657. /* Check the parameters */
  1658. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1659. switch (Channel)
  1660. {
  1661. case TIM_CHANNEL_1:
  1662. {
  1663. /* Enable the TIM Capture/Compare 1 interrupt */
  1664. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1665. break;
  1666. }
  1667. case TIM_CHANNEL_2:
  1668. {
  1669. /* Enable the TIM Capture/Compare 2 interrupt */
  1670. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1671. break;
  1672. }
  1673. case TIM_CHANNEL_3:
  1674. {
  1675. /* Enable the TIM Capture/Compare 3 interrupt */
  1676. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1677. break;
  1678. }
  1679. case TIM_CHANNEL_4:
  1680. {
  1681. /* Enable the TIM Capture/Compare 4 interrupt */
  1682. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1683. break;
  1684. }
  1685. default:
  1686. break;
  1687. }
  1688. /* Enable the Input Capture channel */
  1689. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1690. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1691. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1692. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1693. {
  1694. __HAL_TIM_ENABLE(htim);
  1695. }
  1696. /* Return function status */
  1697. return HAL_OK;
  1698. }
  1699. /**
  1700. * @brief Stops the TIM Input Capture measurement in interrupt mode.
  1701. * @param htim TIM Input Capture handle
  1702. * @param Channel TIM Channels to be disabled
  1703. * This parameter can be one of the following values:
  1704. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1705. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1706. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1707. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1708. * @retval HAL status
  1709. */
  1710. HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1711. {
  1712. /* Check the parameters */
  1713. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1714. switch (Channel)
  1715. {
  1716. case TIM_CHANNEL_1:
  1717. {
  1718. /* Disable the TIM Capture/Compare 1 interrupt */
  1719. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1720. break;
  1721. }
  1722. case TIM_CHANNEL_2:
  1723. {
  1724. /* Disable the TIM Capture/Compare 2 interrupt */
  1725. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1726. break;
  1727. }
  1728. case TIM_CHANNEL_3:
  1729. {
  1730. /* Disable the TIM Capture/Compare 3 interrupt */
  1731. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1732. break;
  1733. }
  1734. case TIM_CHANNEL_4:
  1735. {
  1736. /* Disable the TIM Capture/Compare 4 interrupt */
  1737. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1738. break;
  1739. }
  1740. default:
  1741. break;
  1742. }
  1743. /* Disable the Input Capture channel */
  1744. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1745. /* Disable the Peripheral */
  1746. __HAL_TIM_DISABLE(htim);
  1747. /* Return function status */
  1748. return HAL_OK;
  1749. }
  1750. /**
  1751. * @brief Starts the TIM Input Capture measurement in DMA mode.
  1752. * @param htim TIM Input Capture handle
  1753. * @param Channel TIM Channels to be enabled
  1754. * This parameter can be one of the following values:
  1755. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1756. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1757. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1758. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1759. * @param pData The destination Buffer address.
  1760. * @param Length The length of data to be transferred from TIM peripheral to memory.
  1761. * @retval HAL status
  1762. */
  1763. HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  1764. {
  1765. uint32_t tmpsmcr;
  1766. /* Check the parameters */
  1767. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1768. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  1769. if ((htim->State == HAL_TIM_STATE_BUSY))
  1770. {
  1771. return HAL_BUSY;
  1772. }
  1773. else if ((htim->State == HAL_TIM_STATE_READY))
  1774. {
  1775. if ((pData == NULL) && (Length > 0U))
  1776. {
  1777. return HAL_ERROR;
  1778. }
  1779. else
  1780. {
  1781. htim->State = HAL_TIM_STATE_BUSY;
  1782. }
  1783. }
  1784. else
  1785. {
  1786. /* nothing to do */
  1787. }
  1788. switch (Channel)
  1789. {
  1790. case TIM_CHANNEL_1:
  1791. {
  1792. /* Set the DMA capture callbacks */
  1793. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  1794. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1795. /* Set the DMA error callback */
  1796. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  1797. /* Enable the DMA channel */
  1798. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
  1799. {
  1800. return HAL_ERROR;
  1801. }
  1802. /* Enable the TIM Capture/Compare 1 DMA request */
  1803. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1804. break;
  1805. }
  1806. case TIM_CHANNEL_2:
  1807. {
  1808. /* Set the DMA capture callbacks */
  1809. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  1810. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1811. /* Set the DMA error callback */
  1812. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  1813. /* Enable the DMA channel */
  1814. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData, Length) != HAL_OK)
  1815. {
  1816. return HAL_ERROR;
  1817. }
  1818. /* Enable the TIM Capture/Compare 2 DMA request */
  1819. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1820. break;
  1821. }
  1822. case TIM_CHANNEL_3:
  1823. {
  1824. /* Set the DMA capture callbacks */
  1825. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  1826. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1827. /* Set the DMA error callback */
  1828. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  1829. /* Enable the DMA channel */
  1830. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData, Length) != HAL_OK)
  1831. {
  1832. return HAL_ERROR;
  1833. }
  1834. /* Enable the TIM Capture/Compare 3 DMA request */
  1835. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1836. break;
  1837. }
  1838. case TIM_CHANNEL_4:
  1839. {
  1840. /* Set the DMA capture callbacks */
  1841. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  1842. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1843. /* Set the DMA error callback */
  1844. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  1845. /* Enable the DMA channel */
  1846. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData, Length) != HAL_OK)
  1847. {
  1848. return HAL_ERROR;
  1849. }
  1850. /* Enable the TIM Capture/Compare 4 DMA request */
  1851. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1852. break;
  1853. }
  1854. default:
  1855. break;
  1856. }
  1857. /* Enable the Input Capture channel */
  1858. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1859. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1860. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1861. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1862. {
  1863. __HAL_TIM_ENABLE(htim);
  1864. }
  1865. /* Return function status */
  1866. return HAL_OK;
  1867. }
  1868. /**
  1869. * @brief Stops the TIM Input Capture measurement in DMA mode.
  1870. * @param htim TIM Input Capture handle
  1871. * @param Channel TIM Channels to be disabled
  1872. * This parameter can be one of the following values:
  1873. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1874. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1875. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1876. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1877. * @retval HAL status
  1878. */
  1879. HAL_StatusTypeDef HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1880. {
  1881. /* Check the parameters */
  1882. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1883. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  1884. switch (Channel)
  1885. {
  1886. case TIM_CHANNEL_1:
  1887. {
  1888. /* Disable the TIM Capture/Compare 1 DMA request */
  1889. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1890. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1891. break;
  1892. }
  1893. case TIM_CHANNEL_2:
  1894. {
  1895. /* Disable the TIM Capture/Compare 2 DMA request */
  1896. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1897. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1898. break;
  1899. }
  1900. case TIM_CHANNEL_3:
  1901. {
  1902. /* Disable the TIM Capture/Compare 3 DMA request */
  1903. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1904. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1905. break;
  1906. }
  1907. case TIM_CHANNEL_4:
  1908. {
  1909. /* Disable the TIM Capture/Compare 4 DMA request */
  1910. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1911. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  1912. break;
  1913. }
  1914. default:
  1915. break;
  1916. }
  1917. /* Disable the Input Capture channel */
  1918. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1919. /* Disable the Peripheral */
  1920. __HAL_TIM_DISABLE(htim);
  1921. /* Change the htim state */
  1922. htim->State = HAL_TIM_STATE_READY;
  1923. /* Return function status */
  1924. return HAL_OK;
  1925. }
  1926. /**
  1927. * @}
  1928. */
  1929. /** @defgroup TIM_Exported_Functions_Group5 TIM One Pulse functions
  1930. * @brief TIM One Pulse functions
  1931. *
  1932. @verbatim
  1933. ==============================================================================
  1934. ##### TIM One Pulse functions #####
  1935. ==============================================================================
  1936. [..]
  1937. This section provides functions allowing to:
  1938. (+) Initialize and configure the TIM One Pulse.
  1939. (+) De-initialize the TIM One Pulse.
  1940. (+) Start the TIM One Pulse.
  1941. (+) Stop the TIM One Pulse.
  1942. (+) Start the TIM One Pulse and enable interrupt.
  1943. (+) Stop the TIM One Pulse and disable interrupt.
  1944. (+) Start the TIM One Pulse and enable DMA transfer.
  1945. (+) Stop the TIM One Pulse and disable DMA transfer.
  1946. @endverbatim
  1947. * @{
  1948. */
  1949. /**
  1950. * @brief Initializes the TIM One Pulse Time Base according to the specified
  1951. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  1952. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1953. * requires a timer reset to avoid unexpected direction
  1954. * due to DIR bit readonly in center aligned mode.
  1955. * Ex: call @ref HAL_TIM_OnePulse_DeInit() before HAL_TIM_OnePulse_Init()
  1956. * @param htim TIM One Pulse handle
  1957. * @param OnePulseMode Select the One pulse mode.
  1958. * This parameter can be one of the following values:
  1959. * @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
  1960. * @arg TIM_OPMODE_REPETITIVE: Repetitive pulses will be generated.
  1961. * @retval HAL status
  1962. */
  1963. HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)
  1964. {
  1965. /* Check the TIM handle allocation */
  1966. if (htim == NULL)
  1967. {
  1968. return HAL_ERROR;
  1969. }
  1970. /* Check the parameters */
  1971. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1972. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1973. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1974. assert_param(IS_TIM_OPM_MODE(OnePulseMode));
  1975. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1976. if (htim->State == HAL_TIM_STATE_RESET)
  1977. {
  1978. /* Allocate lock resource and initialize it */
  1979. htim->Lock = HAL_UNLOCKED;
  1980. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1981. /* Reset interrupt callbacks to legacy weak callbacks */
  1982. TIM_ResetCallback(htim);
  1983. if (htim->OnePulse_MspInitCallback == NULL)
  1984. {
  1985. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
  1986. }
  1987. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  1988. htim->OnePulse_MspInitCallback(htim);
  1989. #else
  1990. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1991. HAL_TIM_OnePulse_MspInit(htim);
  1992. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1993. }
  1994. /* Set the TIM state */
  1995. htim->State = HAL_TIM_STATE_BUSY;
  1996. /* Configure the Time base in the One Pulse Mode */
  1997. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1998. /* Reset the OPM Bit */
  1999. htim->Instance->CR1 &= ~TIM_CR1_OPM;
  2000. /* Configure the OPM Mode */
  2001. htim->Instance->CR1 |= OnePulseMode;
  2002. /* Initialize the TIM state*/
  2003. htim->State = HAL_TIM_STATE_READY;
  2004. return HAL_OK;
  2005. }
  2006. /**
  2007. * @brief DeInitializes the TIM One Pulse
  2008. * @param htim TIM One Pulse handle
  2009. * @retval HAL status
  2010. */
  2011. HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)
  2012. {
  2013. /* Check the parameters */
  2014. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2015. htim->State = HAL_TIM_STATE_BUSY;
  2016. /* Disable the TIM Peripheral Clock */
  2017. __HAL_TIM_DISABLE(htim);
  2018. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2019. if (htim->OnePulse_MspDeInitCallback == NULL)
  2020. {
  2021. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
  2022. }
  2023. /* DeInit the low level hardware */
  2024. htim->OnePulse_MspDeInitCallback(htim);
  2025. #else
  2026. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  2027. HAL_TIM_OnePulse_MspDeInit(htim);
  2028. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2029. /* Change TIM state */
  2030. htim->State = HAL_TIM_STATE_RESET;
  2031. /* Release Lock */
  2032. __HAL_UNLOCK(htim);
  2033. return HAL_OK;
  2034. }
  2035. /**
  2036. * @brief Initializes the TIM One Pulse MSP.
  2037. * @param htim TIM One Pulse handle
  2038. * @retval None
  2039. */
  2040. __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
  2041. {
  2042. /* Prevent unused argument(s) compilation warning */
  2043. UNUSED(htim);
  2044. /* NOTE : This function should not be modified, when the callback is needed,
  2045. the HAL_TIM_OnePulse_MspInit could be implemented in the user file
  2046. */
  2047. }
  2048. /**
  2049. * @brief DeInitializes TIM One Pulse MSP.
  2050. * @param htim TIM One Pulse handle
  2051. * @retval None
  2052. */
  2053. __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)
  2054. {
  2055. /* Prevent unused argument(s) compilation warning */
  2056. UNUSED(htim);
  2057. /* NOTE : This function should not be modified, when the callback is needed,
  2058. the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
  2059. */
  2060. }
  2061. /**
  2062. * @brief Starts the TIM One Pulse signal generation.
  2063. * @param htim TIM One Pulse handle
  2064. * @param OutputChannel TIM Channels to be enabled
  2065. * This parameter can be one of the following values:
  2066. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2067. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2068. * @retval HAL status
  2069. */
  2070. HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2071. {
  2072. /* Prevent unused argument(s) compilation warning */
  2073. UNUSED(OutputChannel);
  2074. /* Enable the Capture compare and the Input Capture channels
  2075. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2076. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2077. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2078. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
  2079. No need to enable the counter, it's enabled automatically by hardware
  2080. (the counter starts in response to a stimulus and generate a pulse */
  2081. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2082. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2083. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  2084. {
  2085. /* Enable the main output */
  2086. __HAL_TIM_MOE_ENABLE(htim);
  2087. }
  2088. /* Return function status */
  2089. return HAL_OK;
  2090. }
  2091. /**
  2092. * @brief Stops the TIM One Pulse signal generation.
  2093. * @param htim TIM One Pulse handle
  2094. * @param OutputChannel TIM Channels to be disable
  2095. * This parameter can be one of the following values:
  2096. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2097. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2098. * @retval HAL status
  2099. */
  2100. HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2101. {
  2102. /* Prevent unused argument(s) compilation warning */
  2103. UNUSED(OutputChannel);
  2104. /* Disable the Capture compare and the Input Capture channels
  2105. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2106. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2107. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2108. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  2109. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2110. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2111. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  2112. {
  2113. /* Disable the Main Output */
  2114. __HAL_TIM_MOE_DISABLE(htim);
  2115. }
  2116. /* Disable the Peripheral */
  2117. __HAL_TIM_DISABLE(htim);
  2118. /* Return function status */
  2119. return HAL_OK;
  2120. }
  2121. /**
  2122. * @brief Starts the TIM One Pulse signal generation in interrupt mode.
  2123. * @param htim TIM One Pulse handle
  2124. * @param OutputChannel TIM Channels to be enabled
  2125. * This parameter can be one of the following values:
  2126. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2127. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2128. * @retval HAL status
  2129. */
  2130. HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2131. {
  2132. /* Prevent unused argument(s) compilation warning */
  2133. UNUSED(OutputChannel);
  2134. /* Enable the Capture compare and the Input Capture channels
  2135. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2136. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2137. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2138. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
  2139. No need to enable the counter, it's enabled automatically by hardware
  2140. (the counter starts in response to a stimulus and generate a pulse */
  2141. /* Enable the TIM Capture/Compare 1 interrupt */
  2142. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2143. /* Enable the TIM Capture/Compare 2 interrupt */
  2144. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2145. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2146. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2147. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  2148. {
  2149. /* Enable the main output */
  2150. __HAL_TIM_MOE_ENABLE(htim);
  2151. }
  2152. /* Return function status */
  2153. return HAL_OK;
  2154. }
  2155. /**
  2156. * @brief Stops the TIM One Pulse signal generation in interrupt mode.
  2157. * @param htim TIM One Pulse handle
  2158. * @param OutputChannel TIM Channels to be enabled
  2159. * This parameter can be one of the following values:
  2160. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2161. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2162. * @retval HAL status
  2163. */
  2164. HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2165. {
  2166. /* Prevent unused argument(s) compilation warning */
  2167. UNUSED(OutputChannel);
  2168. /* Disable the TIM Capture/Compare 1 interrupt */
  2169. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2170. /* Disable the TIM Capture/Compare 2 interrupt */
  2171. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2172. /* Disable the Capture compare and the Input Capture channels
  2173. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2174. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2175. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2176. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  2177. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2178. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2179. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  2180. {
  2181. /* Disable the Main Output */
  2182. __HAL_TIM_MOE_DISABLE(htim);
  2183. }
  2184. /* Disable the Peripheral */
  2185. __HAL_TIM_DISABLE(htim);
  2186. /* Return function status */
  2187. return HAL_OK;
  2188. }
  2189. /**
  2190. * @}
  2191. */
  2192. /** @defgroup TIM_Exported_Functions_Group6 TIM Encoder functions
  2193. * @brief TIM Encoder functions
  2194. *
  2195. @verbatim
  2196. ==============================================================================
  2197. ##### TIM Encoder functions #####
  2198. ==============================================================================
  2199. [..]
  2200. This section provides functions allowing to:
  2201. (+) Initialize and configure the TIM Encoder.
  2202. (+) De-initialize the TIM Encoder.
  2203. (+) Start the TIM Encoder.
  2204. (+) Stop the TIM Encoder.
  2205. (+) Start the TIM Encoder and enable interrupt.
  2206. (+) Stop the TIM Encoder and disable interrupt.
  2207. (+) Start the TIM Encoder and enable DMA transfer.
  2208. (+) Stop the TIM Encoder and disable DMA transfer.
  2209. @endverbatim
  2210. * @{
  2211. */
  2212. /**
  2213. * @brief Initializes the TIM Encoder Interface and initialize the associated handle.
  2214. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  2215. * requires a timer reset to avoid unexpected direction
  2216. * due to DIR bit readonly in center aligned mode.
  2217. * Ex: call @ref HAL_TIM_Encoder_DeInit() before HAL_TIM_Encoder_Init()
  2218. * @note Encoder mode and External clock mode 2 are not compatible and must not be selected together
  2219. * Ex: A call for @ref HAL_TIM_Encoder_Init will erase the settings of @ref HAL_TIM_ConfigClockSource
  2220. * using TIM_CLOCKSOURCE_ETRMODE2 and vice versa
  2221. * @param htim TIM Encoder Interface handle
  2222. * @param sConfig TIM Encoder Interface configuration structure
  2223. * @retval HAL status
  2224. */
  2225. HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim, TIM_Encoder_InitTypeDef *sConfig)
  2226. {
  2227. uint32_t tmpsmcr;
  2228. uint32_t tmpccmr1;
  2229. uint32_t tmpccer;
  2230. /* Check the TIM handle allocation */
  2231. if (htim == NULL)
  2232. {
  2233. return HAL_ERROR;
  2234. }
  2235. /* Check the parameters */
  2236. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  2237. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  2238. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  2239. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2240. assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));
  2241. assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));
  2242. assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));
  2243. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  2244. assert_param(IS_TIM_IC_POLARITY(sConfig->IC2Polarity));
  2245. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  2246. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));
  2247. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  2248. assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));
  2249. if (htim->State == HAL_TIM_STATE_RESET)
  2250. {
  2251. /* Allocate lock resource and initialize it */
  2252. htim->Lock = HAL_UNLOCKED;
  2253. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2254. /* Reset interrupt callbacks to legacy weak callbacks */
  2255. TIM_ResetCallback(htim);
  2256. if (htim->Encoder_MspInitCallback == NULL)
  2257. {
  2258. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
  2259. }
  2260. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  2261. htim->Encoder_MspInitCallback(htim);
  2262. #else
  2263. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  2264. HAL_TIM_Encoder_MspInit(htim);
  2265. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2266. }
  2267. /* Set the TIM state */
  2268. htim->State = HAL_TIM_STATE_BUSY;
  2269. /* Reset the SMS and ECE bits */
  2270. htim->Instance->SMCR &= ~(TIM_SMCR_SMS | TIM_SMCR_ECE);
  2271. /* Configure the Time base in the Encoder Mode */
  2272. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  2273. /* Get the TIMx SMCR register value */
  2274. tmpsmcr = htim->Instance->SMCR;
  2275. /* Get the TIMx CCMR1 register value */
  2276. tmpccmr1 = htim->Instance->CCMR1;
  2277. /* Get the TIMx CCER register value */
  2278. tmpccer = htim->Instance->CCER;
  2279. /* Set the encoder Mode */
  2280. tmpsmcr |= sConfig->EncoderMode;
  2281. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  2282. tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);
  2283. tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8U));
  2284. /* Set the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
  2285. tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);
  2286. tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);
  2287. tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8U);
  2288. tmpccmr1 |= (sConfig->IC1Filter << 4U) | (sConfig->IC2Filter << 12U);
  2289. /* Set the TI1 and the TI2 Polarities */
  2290. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
  2291. tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);
  2292. tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4U);
  2293. /* Write to TIMx SMCR */
  2294. htim->Instance->SMCR = tmpsmcr;
  2295. /* Write to TIMx CCMR1 */
  2296. htim->Instance->CCMR1 = tmpccmr1;
  2297. /* Write to TIMx CCER */
  2298. htim->Instance->CCER = tmpccer;
  2299. /* Initialize the TIM state*/
  2300. htim->State = HAL_TIM_STATE_READY;
  2301. return HAL_OK;
  2302. }
  2303. /**
  2304. * @brief DeInitializes the TIM Encoder interface
  2305. * @param htim TIM Encoder Interface handle
  2306. * @retval HAL status
  2307. */
  2308. HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)
  2309. {
  2310. /* Check the parameters */
  2311. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2312. htim->State = HAL_TIM_STATE_BUSY;
  2313. /* Disable the TIM Peripheral Clock */
  2314. __HAL_TIM_DISABLE(htim);
  2315. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2316. if (htim->Encoder_MspDeInitCallback == NULL)
  2317. {
  2318. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
  2319. }
  2320. /* DeInit the low level hardware */
  2321. htim->Encoder_MspDeInitCallback(htim);
  2322. #else
  2323. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  2324. HAL_TIM_Encoder_MspDeInit(htim);
  2325. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2326. /* Change TIM state */
  2327. htim->State = HAL_TIM_STATE_RESET;
  2328. /* Release Lock */
  2329. __HAL_UNLOCK(htim);
  2330. return HAL_OK;
  2331. }
  2332. /**
  2333. * @brief Initializes the TIM Encoder Interface MSP.
  2334. * @param htim TIM Encoder Interface handle
  2335. * @retval None
  2336. */
  2337. __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
  2338. {
  2339. /* Prevent unused argument(s) compilation warning */
  2340. UNUSED(htim);
  2341. /* NOTE : This function should not be modified, when the callback is needed,
  2342. the HAL_TIM_Encoder_MspInit could be implemented in the user file
  2343. */
  2344. }
  2345. /**
  2346. * @brief DeInitializes TIM Encoder Interface MSP.
  2347. * @param htim TIM Encoder Interface handle
  2348. * @retval None
  2349. */
  2350. __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)
  2351. {
  2352. /* Prevent unused argument(s) compilation warning */
  2353. UNUSED(htim);
  2354. /* NOTE : This function should not be modified, when the callback is needed,
  2355. the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
  2356. */
  2357. }
  2358. /**
  2359. * @brief Starts the TIM Encoder Interface.
  2360. * @param htim TIM Encoder Interface handle
  2361. * @param Channel TIM Channels to be enabled
  2362. * This parameter can be one of the following values:
  2363. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2364. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2365. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2366. * @retval HAL status
  2367. */
  2368. HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  2369. {
  2370. /* Check the parameters */
  2371. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2372. /* Enable the encoder interface channels */
  2373. switch (Channel)
  2374. {
  2375. case TIM_CHANNEL_1:
  2376. {
  2377. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2378. break;
  2379. }
  2380. case TIM_CHANNEL_2:
  2381. {
  2382. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2383. break;
  2384. }
  2385. default :
  2386. {
  2387. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2388. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2389. break;
  2390. }
  2391. }
  2392. /* Enable the Peripheral */
  2393. __HAL_TIM_ENABLE(htim);
  2394. /* Return function status */
  2395. return HAL_OK;
  2396. }
  2397. /**
  2398. * @brief Stops the TIM Encoder Interface.
  2399. * @param htim TIM Encoder Interface handle
  2400. * @param Channel TIM Channels to be disabled
  2401. * This parameter can be one of the following values:
  2402. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2403. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2404. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2405. * @retval HAL status
  2406. */
  2407. HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  2408. {
  2409. /* Check the parameters */
  2410. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2411. /* Disable the Input Capture channels 1 and 2
  2412. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2413. switch (Channel)
  2414. {
  2415. case TIM_CHANNEL_1:
  2416. {
  2417. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2418. break;
  2419. }
  2420. case TIM_CHANNEL_2:
  2421. {
  2422. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2423. break;
  2424. }
  2425. default :
  2426. {
  2427. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2428. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2429. break;
  2430. }
  2431. }
  2432. /* Disable the Peripheral */
  2433. __HAL_TIM_DISABLE(htim);
  2434. /* Return function status */
  2435. return HAL_OK;
  2436. }
  2437. /**
  2438. * @brief Starts the TIM Encoder Interface in interrupt mode.
  2439. * @param htim TIM Encoder Interface handle
  2440. * @param Channel TIM Channels to be enabled
  2441. * This parameter can be one of the following values:
  2442. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2443. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2444. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2445. * @retval HAL status
  2446. */
  2447. HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  2448. {
  2449. /* Check the parameters */
  2450. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2451. /* Enable the encoder interface channels */
  2452. /* Enable the capture compare Interrupts 1 and/or 2 */
  2453. switch (Channel)
  2454. {
  2455. case TIM_CHANNEL_1:
  2456. {
  2457. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2458. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2459. break;
  2460. }
  2461. case TIM_CHANNEL_2:
  2462. {
  2463. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2464. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2465. break;
  2466. }
  2467. default :
  2468. {
  2469. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2470. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2471. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2472. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2473. break;
  2474. }
  2475. }
  2476. /* Enable the Peripheral */
  2477. __HAL_TIM_ENABLE(htim);
  2478. /* Return function status */
  2479. return HAL_OK;
  2480. }
  2481. /**
  2482. * @brief Stops the TIM Encoder Interface in interrupt mode.
  2483. * @param htim TIM Encoder Interface handle
  2484. * @param Channel TIM Channels to be disabled
  2485. * This parameter can be one of the following values:
  2486. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2487. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2488. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2489. * @retval HAL status
  2490. */
  2491. HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  2492. {
  2493. /* Check the parameters */
  2494. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2495. /* Disable the Input Capture channels 1 and 2
  2496. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2497. if (Channel == TIM_CHANNEL_1)
  2498. {
  2499. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2500. /* Disable the capture compare Interrupts 1 */
  2501. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2502. }
  2503. else if (Channel == TIM_CHANNEL_2)
  2504. {
  2505. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2506. /* Disable the capture compare Interrupts 2 */
  2507. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2508. }
  2509. else
  2510. {
  2511. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2512. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2513. /* Disable the capture compare Interrupts 1 and 2 */
  2514. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2515. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2516. }
  2517. /* Disable the Peripheral */
  2518. __HAL_TIM_DISABLE(htim);
  2519. /* Change the htim state */
  2520. htim->State = HAL_TIM_STATE_READY;
  2521. /* Return function status */
  2522. return HAL_OK;
  2523. }
  2524. /**
  2525. * @brief Starts the TIM Encoder Interface in DMA mode.
  2526. * @param htim TIM Encoder Interface handle
  2527. * @param Channel TIM Channels to be enabled
  2528. * This parameter can be one of the following values:
  2529. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2530. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2531. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2532. * @param pData1 The destination Buffer address for IC1.
  2533. * @param pData2 The destination Buffer address for IC2.
  2534. * @param Length The length of data to be transferred from TIM peripheral to memory.
  2535. * @retval HAL status
  2536. */
  2537. HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1,
  2538. uint32_t *pData2, uint16_t Length)
  2539. {
  2540. /* Check the parameters */
  2541. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  2542. if ((htim->State == HAL_TIM_STATE_BUSY))
  2543. {
  2544. return HAL_BUSY;
  2545. }
  2546. else if ((htim->State == HAL_TIM_STATE_READY))
  2547. {
  2548. if ((((pData1 == NULL) || (pData2 == NULL))) && (Length > 0U))
  2549. {
  2550. return HAL_ERROR;
  2551. }
  2552. else
  2553. {
  2554. htim->State = HAL_TIM_STATE_BUSY;
  2555. }
  2556. }
  2557. else
  2558. {
  2559. /* nothing to do */
  2560. }
  2561. switch (Channel)
  2562. {
  2563. case TIM_CHANNEL_1:
  2564. {
  2565. /* Set the DMA capture callbacks */
  2566. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  2567. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2568. /* Set the DMA error callback */
  2569. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  2570. /* Enable the DMA channel */
  2571. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length) != HAL_OK)
  2572. {
  2573. return HAL_ERROR;
  2574. }
  2575. /* Enable the TIM Input Capture DMA request */
  2576. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  2577. /* Enable the Peripheral */
  2578. __HAL_TIM_ENABLE(htim);
  2579. /* Enable the Capture compare channel */
  2580. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2581. break;
  2582. }
  2583. case TIM_CHANNEL_2:
  2584. {
  2585. /* Set the DMA capture callbacks */
  2586. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  2587. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2588. /* Set the DMA error callback */
  2589. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError;
  2590. /* Enable the DMA channel */
  2591. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length) != HAL_OK)
  2592. {
  2593. return HAL_ERROR;
  2594. }
  2595. /* Enable the TIM Input Capture DMA request */
  2596. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  2597. /* Enable the Peripheral */
  2598. __HAL_TIM_ENABLE(htim);
  2599. /* Enable the Capture compare channel */
  2600. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2601. break;
  2602. }
  2603. case TIM_CHANNEL_ALL:
  2604. {
  2605. /* Set the DMA capture callbacks */
  2606. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  2607. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2608. /* Set the DMA error callback */
  2609. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  2610. /* Enable the DMA channel */
  2611. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length) != HAL_OK)
  2612. {
  2613. return HAL_ERROR;
  2614. }
  2615. /* Set the DMA capture callbacks */
  2616. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  2617. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2618. /* Set the DMA error callback */
  2619. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  2620. /* Enable the DMA channel */
  2621. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length) != HAL_OK)
  2622. {
  2623. return HAL_ERROR;
  2624. }
  2625. /* Enable the Peripheral */
  2626. __HAL_TIM_ENABLE(htim);
  2627. /* Enable the Capture compare channel */
  2628. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2629. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2630. /* Enable the TIM Input Capture DMA request */
  2631. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  2632. /* Enable the TIM Input Capture DMA request */
  2633. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  2634. break;
  2635. }
  2636. default:
  2637. break;
  2638. }
  2639. /* Return function status */
  2640. return HAL_OK;
  2641. }
  2642. /**
  2643. * @brief Stops the TIM Encoder Interface in DMA mode.
  2644. * @param htim TIM Encoder Interface handle
  2645. * @param Channel TIM Channels to be enabled
  2646. * This parameter can be one of the following values:
  2647. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2648. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2649. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2650. * @retval HAL status
  2651. */
  2652. HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  2653. {
  2654. /* Check the parameters */
  2655. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  2656. /* Disable the Input Capture channels 1 and 2
  2657. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2658. if (Channel == TIM_CHANNEL_1)
  2659. {
  2660. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2661. /* Disable the capture compare DMA Request 1 */
  2662. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  2663. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  2664. }
  2665. else if (Channel == TIM_CHANNEL_2)
  2666. {
  2667. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2668. /* Disable the capture compare DMA Request 2 */
  2669. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  2670. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  2671. }
  2672. else
  2673. {
  2674. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2675. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2676. /* Disable the capture compare DMA Request 1 and 2 */
  2677. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  2678. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  2679. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  2680. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  2681. }
  2682. /* Disable the Peripheral */
  2683. __HAL_TIM_DISABLE(htim);
  2684. /* Change the htim state */
  2685. htim->State = HAL_TIM_STATE_READY;
  2686. /* Return function status */
  2687. return HAL_OK;
  2688. }
  2689. /**
  2690. * @}
  2691. */
  2692. /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
  2693. * @brief TIM IRQ handler management
  2694. *
  2695. @verbatim
  2696. ==============================================================================
  2697. ##### IRQ handler management #####
  2698. ==============================================================================
  2699. [..]
  2700. This section provides Timer IRQ handler function.
  2701. @endverbatim
  2702. * @{
  2703. */
  2704. /**
  2705. * @brief This function handles TIM interrupts requests.
  2706. * @param htim TIM handle
  2707. * @retval None
  2708. */
  2709. void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
  2710. {
  2711. /* Capture compare 1 event */
  2712. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)
  2713. {
  2714. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) != RESET)
  2715. {
  2716. {
  2717. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
  2718. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2719. /* Input capture event */
  2720. if ((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U)
  2721. {
  2722. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2723. htim->IC_CaptureCallback(htim);
  2724. #else
  2725. HAL_TIM_IC_CaptureCallback(htim);
  2726. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2727. }
  2728. /* Output compare event */
  2729. else
  2730. {
  2731. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2732. htim->OC_DelayElapsedCallback(htim);
  2733. htim->PWM_PulseFinishedCallback(htim);
  2734. #else
  2735. HAL_TIM_OC_DelayElapsedCallback(htim);
  2736. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2737. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2738. }
  2739. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2740. }
  2741. }
  2742. }
  2743. /* Capture compare 2 event */
  2744. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET)
  2745. {
  2746. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) != RESET)
  2747. {
  2748. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);
  2749. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2750. /* Input capture event */
  2751. if ((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U)
  2752. {
  2753. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2754. htim->IC_CaptureCallback(htim);
  2755. #else
  2756. HAL_TIM_IC_CaptureCallback(htim);
  2757. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2758. }
  2759. /* Output compare event */
  2760. else
  2761. {
  2762. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2763. htim->OC_DelayElapsedCallback(htim);
  2764. htim->PWM_PulseFinishedCallback(htim);
  2765. #else
  2766. HAL_TIM_OC_DelayElapsedCallback(htim);
  2767. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2768. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2769. }
  2770. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2771. }
  2772. }
  2773. /* Capture compare 3 event */
  2774. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET)
  2775. {
  2776. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) != RESET)
  2777. {
  2778. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);
  2779. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2780. /* Input capture event */
  2781. if ((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U)
  2782. {
  2783. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2784. htim->IC_CaptureCallback(htim);
  2785. #else
  2786. HAL_TIM_IC_CaptureCallback(htim);
  2787. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2788. }
  2789. /* Output compare event */
  2790. else
  2791. {
  2792. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2793. htim->OC_DelayElapsedCallback(htim);
  2794. htim->PWM_PulseFinishedCallback(htim);
  2795. #else
  2796. HAL_TIM_OC_DelayElapsedCallback(htim);
  2797. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2798. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2799. }
  2800. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2801. }
  2802. }
  2803. /* Capture compare 4 event */
  2804. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET)
  2805. {
  2806. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) != RESET)
  2807. {
  2808. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
  2809. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  2810. /* Input capture event */
  2811. if ((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U)
  2812. {
  2813. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2814. htim->IC_CaptureCallback(htim);
  2815. #else
  2816. HAL_TIM_IC_CaptureCallback(htim);
  2817. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2818. }
  2819. /* Output compare event */
  2820. else
  2821. {
  2822. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2823. htim->OC_DelayElapsedCallback(htim);
  2824. htim->PWM_PulseFinishedCallback(htim);
  2825. #else
  2826. HAL_TIM_OC_DelayElapsedCallback(htim);
  2827. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2828. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2829. }
  2830. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2831. }
  2832. }
  2833. /* TIM Update event */
  2834. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)
  2835. {
  2836. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_UPDATE) != RESET)
  2837. {
  2838. __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
  2839. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2840. htim->PeriodElapsedCallback(htim);
  2841. #else
  2842. HAL_TIM_PeriodElapsedCallback(htim);
  2843. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2844. }
  2845. }
  2846. /* TIM Break input event */
  2847. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK) != RESET)
  2848. {
  2849. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) != RESET)
  2850. {
  2851. __HAL_TIM_CLEAR_IT(htim, TIM_IT_BREAK);
  2852. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2853. htim->BreakCallback(htim);
  2854. #else
  2855. HAL_TIMEx_BreakCallback(htim);
  2856. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2857. }
  2858. }
  2859. /* TIM Break2 input event */
  2860. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK2) != RESET)
  2861. {
  2862. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) != RESET)
  2863. {
  2864. __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_BREAK2);
  2865. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2866. htim->Break2Callback(htim);
  2867. #else
  2868. HAL_TIMEx_Break2Callback(htim);
  2869. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2870. }
  2871. }
  2872. /* TIM Trigger detection event */
  2873. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_TRIGGER) != RESET)
  2874. {
  2875. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_TRIGGER) != RESET)
  2876. {
  2877. __HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);
  2878. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2879. htim->TriggerCallback(htim);
  2880. #else
  2881. HAL_TIM_TriggerCallback(htim);
  2882. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2883. }
  2884. }
  2885. /* TIM commutation event */
  2886. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_COM) != RESET)
  2887. {
  2888. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_COM) != RESET)
  2889. {
  2890. __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_COM);
  2891. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2892. htim->CommutationCallback(htim);
  2893. #else
  2894. HAL_TIMEx_CommutCallback(htim);
  2895. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2896. }
  2897. }
  2898. }
  2899. /**
  2900. * @}
  2901. */
  2902. /** @defgroup TIM_Exported_Functions_Group8 TIM Peripheral Control functions
  2903. * @brief TIM Peripheral Control functions
  2904. *
  2905. @verbatim
  2906. ==============================================================================
  2907. ##### Peripheral Control functions #####
  2908. ==============================================================================
  2909. [..]
  2910. This section provides functions allowing to:
  2911. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  2912. (+) Configure External Clock source.
  2913. (+) Configure Complementary channels, break features and dead time.
  2914. (+) Configure Master and the Slave synchronization.
  2915. (+) Configure the DMA Burst Mode.
  2916. @endverbatim
  2917. * @{
  2918. */
  2919. /**
  2920. * @brief Initializes the TIM Output Compare Channels according to the specified
  2921. * parameters in the TIM_OC_InitTypeDef.
  2922. * @param htim TIM Output Compare handle
  2923. * @param sConfig TIM Output Compare configuration structure
  2924. * @param Channel TIM Channels to configure
  2925. * This parameter can be one of the following values:
  2926. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2927. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2928. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  2929. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  2930. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  2931. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  2932. * @retval HAL status
  2933. */
  2934. HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim,
  2935. TIM_OC_InitTypeDef *sConfig,
  2936. uint32_t Channel)
  2937. {
  2938. /* Check the parameters */
  2939. assert_param(IS_TIM_CHANNELS(Channel));
  2940. assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
  2941. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  2942. /* Process Locked */
  2943. __HAL_LOCK(htim);
  2944. htim->State = HAL_TIM_STATE_BUSY;
  2945. switch (Channel)
  2946. {
  2947. case TIM_CHANNEL_1:
  2948. {
  2949. /* Check the parameters */
  2950. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  2951. /* Configure the TIM Channel 1 in Output Compare */
  2952. TIM_OC1_SetConfig(htim->Instance, sConfig);
  2953. break;
  2954. }
  2955. case TIM_CHANNEL_2:
  2956. {
  2957. /* Check the parameters */
  2958. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2959. /* Configure the TIM Channel 2 in Output Compare */
  2960. TIM_OC2_SetConfig(htim->Instance, sConfig);
  2961. break;
  2962. }
  2963. case TIM_CHANNEL_3:
  2964. {
  2965. /* Check the parameters */
  2966. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  2967. /* Configure the TIM Channel 3 in Output Compare */
  2968. TIM_OC3_SetConfig(htim->Instance, sConfig);
  2969. break;
  2970. }
  2971. case TIM_CHANNEL_4:
  2972. {
  2973. /* Check the parameters */
  2974. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  2975. /* Configure the TIM Channel 4 in Output Compare */
  2976. TIM_OC4_SetConfig(htim->Instance, sConfig);
  2977. break;
  2978. }
  2979. case TIM_CHANNEL_5:
  2980. {
  2981. /* Check the parameters */
  2982. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  2983. /* Configure the TIM Channel 5 in Output Compare */
  2984. TIM_OC5_SetConfig(htim->Instance, sConfig);
  2985. break;
  2986. }
  2987. case TIM_CHANNEL_6:
  2988. {
  2989. /* Check the parameters */
  2990. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  2991. /* Configure the TIM Channel 6 in Output Compare */
  2992. TIM_OC6_SetConfig(htim->Instance, sConfig);
  2993. break;
  2994. }
  2995. default:
  2996. break;
  2997. }
  2998. htim->State = HAL_TIM_STATE_READY;
  2999. __HAL_UNLOCK(htim);
  3000. return HAL_OK;
  3001. }
  3002. /**
  3003. * @brief Initializes the TIM Input Capture Channels according to the specified
  3004. * parameters in the TIM_IC_InitTypeDef.
  3005. * @param htim TIM IC handle
  3006. * @param sConfig TIM Input Capture configuration structure
  3007. * @param Channel TIM Channel to configure
  3008. * This parameter can be one of the following values:
  3009. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3010. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3011. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3012. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3013. * @retval HAL status
  3014. */
  3015. HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_IC_InitTypeDef *sConfig, uint32_t Channel)
  3016. {
  3017. /* Check the parameters */
  3018. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3019. assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
  3020. assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
  3021. assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
  3022. assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
  3023. /* Process Locked */
  3024. __HAL_LOCK(htim);
  3025. htim->State = HAL_TIM_STATE_BUSY;
  3026. if (Channel == TIM_CHANNEL_1)
  3027. {
  3028. /* TI1 Configuration */
  3029. TIM_TI1_SetConfig(htim->Instance,
  3030. sConfig->ICPolarity,
  3031. sConfig->ICSelection,
  3032. sConfig->ICFilter);
  3033. /* Reset the IC1PSC Bits */
  3034. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  3035. /* Set the IC1PSC value */
  3036. htim->Instance->CCMR1 |= sConfig->ICPrescaler;
  3037. }
  3038. else if (Channel == TIM_CHANNEL_2)
  3039. {
  3040. /* TI2 Configuration */
  3041. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3042. TIM_TI2_SetConfig(htim->Instance,
  3043. sConfig->ICPolarity,
  3044. sConfig->ICSelection,
  3045. sConfig->ICFilter);
  3046. /* Reset the IC2PSC Bits */
  3047. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  3048. /* Set the IC2PSC value */
  3049. htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8U);
  3050. }
  3051. else if (Channel == TIM_CHANNEL_3)
  3052. {
  3053. /* TI3 Configuration */
  3054. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3055. TIM_TI3_SetConfig(htim->Instance,
  3056. sConfig->ICPolarity,
  3057. sConfig->ICSelection,
  3058. sConfig->ICFilter);
  3059. /* Reset the IC3PSC Bits */
  3060. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
  3061. /* Set the IC3PSC value */
  3062. htim->Instance->CCMR2 |= sConfig->ICPrescaler;
  3063. }
  3064. else
  3065. {
  3066. /* TI4 Configuration */
  3067. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3068. TIM_TI4_SetConfig(htim->Instance,
  3069. sConfig->ICPolarity,
  3070. sConfig->ICSelection,
  3071. sConfig->ICFilter);
  3072. /* Reset the IC4PSC Bits */
  3073. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
  3074. /* Set the IC4PSC value */
  3075. htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8U);
  3076. }
  3077. htim->State = HAL_TIM_STATE_READY;
  3078. __HAL_UNLOCK(htim);
  3079. return HAL_OK;
  3080. }
  3081. /**
  3082. * @brief Initializes the TIM PWM channels according to the specified
  3083. * parameters in the TIM_OC_InitTypeDef.
  3084. * @param htim TIM PWM handle
  3085. * @param sConfig TIM PWM configuration structure
  3086. * @param Channel TIM Channels to be configured
  3087. * This parameter can be one of the following values:
  3088. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3089. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3090. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3091. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3092. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  3093. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  3094. * @retval HAL status
  3095. */
  3096. HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
  3097. TIM_OC_InitTypeDef *sConfig,
  3098. uint32_t Channel)
  3099. {
  3100. /* Check the parameters */
  3101. assert_param(IS_TIM_CHANNELS(Channel));
  3102. assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
  3103. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  3104. assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
  3105. /* Process Locked */
  3106. __HAL_LOCK(htim);
  3107. htim->State = HAL_TIM_STATE_BUSY;
  3108. switch (Channel)
  3109. {
  3110. case TIM_CHANNEL_1:
  3111. {
  3112. /* Check the parameters */
  3113. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3114. /* Configure the Channel 1 in PWM mode */
  3115. TIM_OC1_SetConfig(htim->Instance, sConfig);
  3116. /* Set the Preload enable bit for channel1 */
  3117. htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
  3118. /* Configure the Output Fast mode */
  3119. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
  3120. htim->Instance->CCMR1 |= sConfig->OCFastMode;
  3121. break;
  3122. }
  3123. case TIM_CHANNEL_2:
  3124. {
  3125. /* Check the parameters */
  3126. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3127. /* Configure the Channel 2 in PWM mode */
  3128. TIM_OC2_SetConfig(htim->Instance, sConfig);
  3129. /* Set the Preload enable bit for channel2 */
  3130. htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
  3131. /* Configure the Output Fast mode */
  3132. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
  3133. htim->Instance->CCMR1 |= sConfig->OCFastMode << 8U;
  3134. break;
  3135. }
  3136. case TIM_CHANNEL_3:
  3137. {
  3138. /* Check the parameters */
  3139. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3140. /* Configure the Channel 3 in PWM mode */
  3141. TIM_OC3_SetConfig(htim->Instance, sConfig);
  3142. /* Set the Preload enable bit for channel3 */
  3143. htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
  3144. /* Configure the Output Fast mode */
  3145. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
  3146. htim->Instance->CCMR2 |= sConfig->OCFastMode;
  3147. break;
  3148. }
  3149. case TIM_CHANNEL_4:
  3150. {
  3151. /* Check the parameters */
  3152. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3153. /* Configure the Channel 4 in PWM mode */
  3154. TIM_OC4_SetConfig(htim->Instance, sConfig);
  3155. /* Set the Preload enable bit for channel4 */
  3156. htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
  3157. /* Configure the Output Fast mode */
  3158. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
  3159. htim->Instance->CCMR2 |= sConfig->OCFastMode << 8U;
  3160. break;
  3161. }
  3162. case TIM_CHANNEL_5:
  3163. {
  3164. /* Check the parameters */
  3165. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  3166. /* Configure the Channel 5 in PWM mode */
  3167. TIM_OC5_SetConfig(htim->Instance, sConfig);
  3168. /* Set the Preload enable bit for channel5*/
  3169. htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
  3170. /* Configure the Output Fast mode */
  3171. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;
  3172. htim->Instance->CCMR3 |= sConfig->OCFastMode;
  3173. break;
  3174. }
  3175. case TIM_CHANNEL_6:
  3176. {
  3177. /* Check the parameters */
  3178. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  3179. /* Configure the Channel 6 in PWM mode */
  3180. TIM_OC6_SetConfig(htim->Instance, sConfig);
  3181. /* Set the Preload enable bit for channel6 */
  3182. htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
  3183. /* Configure the Output Fast mode */
  3184. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;
  3185. htim->Instance->CCMR3 |= sConfig->OCFastMode << 8U;
  3186. break;
  3187. }
  3188. default:
  3189. break;
  3190. }
  3191. htim->State = HAL_TIM_STATE_READY;
  3192. __HAL_UNLOCK(htim);
  3193. return HAL_OK;
  3194. }
  3195. /**
  3196. * @brief Initializes the TIM One Pulse Channels according to the specified
  3197. * parameters in the TIM_OnePulse_InitTypeDef.
  3198. * @param htim TIM One Pulse handle
  3199. * @param sConfig TIM One Pulse configuration structure
  3200. * @param OutputChannel TIM output channel to configure
  3201. * This parameter can be one of the following values:
  3202. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3203. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3204. * @param InputChannel TIM input Channel to configure
  3205. * This parameter can be one of the following values:
  3206. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3207. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3208. * @retval HAL status
  3209. */
  3210. HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef *sConfig,
  3211. uint32_t OutputChannel, uint32_t InputChannel)
  3212. {
  3213. TIM_OC_InitTypeDef temp1;
  3214. /* Check the parameters */
  3215. assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
  3216. assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
  3217. if (OutputChannel != InputChannel)
  3218. {
  3219. /* Process Locked */
  3220. __HAL_LOCK(htim);
  3221. htim->State = HAL_TIM_STATE_BUSY;
  3222. /* Extract the Output compare configuration from sConfig structure */
  3223. temp1.OCMode = sConfig->OCMode;
  3224. temp1.Pulse = sConfig->Pulse;
  3225. temp1.OCPolarity = sConfig->OCPolarity;
  3226. temp1.OCNPolarity = sConfig->OCNPolarity;
  3227. temp1.OCIdleState = sConfig->OCIdleState;
  3228. temp1.OCNIdleState = sConfig->OCNIdleState;
  3229. switch (OutputChannel)
  3230. {
  3231. case TIM_CHANNEL_1:
  3232. {
  3233. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3234. TIM_OC1_SetConfig(htim->Instance, &temp1);
  3235. break;
  3236. }
  3237. case TIM_CHANNEL_2:
  3238. {
  3239. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3240. TIM_OC2_SetConfig(htim->Instance, &temp1);
  3241. break;
  3242. }
  3243. default:
  3244. break;
  3245. }
  3246. switch (InputChannel)
  3247. {
  3248. case TIM_CHANNEL_1:
  3249. {
  3250. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3251. TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
  3252. sConfig->ICSelection, sConfig->ICFilter);
  3253. /* Reset the IC1PSC Bits */
  3254. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  3255. /* Select the Trigger source */
  3256. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  3257. htim->Instance->SMCR |= TIM_TS_TI1FP1;
  3258. /* Select the Slave Mode */
  3259. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3260. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  3261. break;
  3262. }
  3263. case TIM_CHANNEL_2:
  3264. {
  3265. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3266. TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
  3267. sConfig->ICSelection, sConfig->ICFilter);
  3268. /* Reset the IC2PSC Bits */
  3269. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  3270. /* Select the Trigger source */
  3271. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  3272. htim->Instance->SMCR |= TIM_TS_TI2FP2;
  3273. /* Select the Slave Mode */
  3274. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3275. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  3276. break;
  3277. }
  3278. default:
  3279. break;
  3280. }
  3281. htim->State = HAL_TIM_STATE_READY;
  3282. __HAL_UNLOCK(htim);
  3283. return HAL_OK;
  3284. }
  3285. else
  3286. {
  3287. return HAL_ERROR;
  3288. }
  3289. }
  3290. /**
  3291. * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
  3292. * @param htim TIM handle
  3293. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
  3294. * This parameter can be one of the following values:
  3295. * @arg TIM_DMABASE_CR1
  3296. * @arg TIM_DMABASE_CR2
  3297. * @arg TIM_DMABASE_SMCR
  3298. * @arg TIM_DMABASE_DIER
  3299. * @arg TIM_DMABASE_SR
  3300. * @arg TIM_DMABASE_EGR
  3301. * @arg TIM_DMABASE_CCMR1
  3302. * @arg TIM_DMABASE_CCMR2
  3303. * @arg TIM_DMABASE_CCER
  3304. * @arg TIM_DMABASE_CNT
  3305. * @arg TIM_DMABASE_PSC
  3306. * @arg TIM_DMABASE_ARR
  3307. * @arg TIM_DMABASE_RCR
  3308. * @arg TIM_DMABASE_CCR1
  3309. * @arg TIM_DMABASE_CCR2
  3310. * @arg TIM_DMABASE_CCR3
  3311. * @arg TIM_DMABASE_CCR4
  3312. * @arg TIM_DMABASE_BDTR
  3313. * @arg TIM_DMABASE_OR1
  3314. * @arg TIM_DMABASE_CCMR3
  3315. * @arg TIM_DMABASE_CCR5
  3316. * @arg TIM_DMABASE_CCR6
  3317. * @arg TIM_DMABASE_OR2
  3318. * @arg TIM_DMABASE_OR3
  3319. * @param BurstRequestSrc TIM DMA Request sources
  3320. * This parameter can be one of the following values:
  3321. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3322. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3323. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3324. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3325. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3326. * @arg TIM_DMA_COM: TIM Commutation DMA source
  3327. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3328. * @param BurstBuffer The Buffer address.
  3329. * @param BurstLength DMA Burst length. This parameter can be one value
  3330. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3331. * @note This function should be used only when BurstLength is equal to DMA data transfer length.
  3332. * @retval HAL status
  3333. */
  3334. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress, uint32_t BurstRequestSrc,
  3335. uint32_t *BurstBuffer, uint32_t BurstLength)
  3336. {
  3337. /* Check the parameters */
  3338. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  3339. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  3340. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3341. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  3342. if ((htim->State == HAL_TIM_STATE_BUSY))
  3343. {
  3344. return HAL_BUSY;
  3345. }
  3346. else if ((htim->State == HAL_TIM_STATE_READY))
  3347. {
  3348. if ((BurstBuffer == NULL) && (BurstLength > 0U))
  3349. {
  3350. return HAL_ERROR;
  3351. }
  3352. else
  3353. {
  3354. htim->State = HAL_TIM_STATE_BUSY;
  3355. }
  3356. }
  3357. else
  3358. {
  3359. /* nothing to do */
  3360. }
  3361. switch (BurstRequestSrc)
  3362. {
  3363. case TIM_DMA_UPDATE:
  3364. {
  3365. /* Set the DMA Period elapsed callbacks */
  3366. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  3367. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  3368. /* Set the DMA error callback */
  3369. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  3370. /* Enable the DMA channel */
  3371. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3372. {
  3373. return HAL_ERROR;
  3374. }
  3375. break;
  3376. }
  3377. case TIM_DMA_CC1:
  3378. {
  3379. /* Set the DMA compare callbacks */
  3380. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3381. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3382. /* Set the DMA error callback */
  3383. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  3384. /* Enable the DMA channel */
  3385. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer,
  3386. (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3387. {
  3388. return HAL_ERROR;
  3389. }
  3390. break;
  3391. }
  3392. case TIM_DMA_CC2:
  3393. {
  3394. /* Set the DMA compare callbacks */
  3395. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3396. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3397. /* Set the DMA error callback */
  3398. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  3399. /* Enable the DMA channel */
  3400. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer,
  3401. (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3402. {
  3403. return HAL_ERROR;
  3404. }
  3405. break;
  3406. }
  3407. case TIM_DMA_CC3:
  3408. {
  3409. /* Set the DMA compare callbacks */
  3410. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3411. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3412. /* Set the DMA error callback */
  3413. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  3414. /* Enable the DMA channel */
  3415. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer,
  3416. (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3417. {
  3418. return HAL_ERROR;
  3419. }
  3420. break;
  3421. }
  3422. case TIM_DMA_CC4:
  3423. {
  3424. /* Set the DMA compare callbacks */
  3425. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3426. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3427. /* Set the DMA error callback */
  3428. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  3429. /* Enable the DMA channel */
  3430. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer,
  3431. (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3432. {
  3433. return HAL_ERROR;
  3434. }
  3435. break;
  3436. }
  3437. case TIM_DMA_COM:
  3438. {
  3439. /* Set the DMA commutation callbacks */
  3440. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  3441. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  3442. /* Set the DMA error callback */
  3443. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
  3444. /* Enable the DMA channel */
  3445. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)BurstBuffer,
  3446. (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3447. {
  3448. return HAL_ERROR;
  3449. }
  3450. break;
  3451. }
  3452. case TIM_DMA_TRIGGER:
  3453. {
  3454. /* Set the DMA trigger callbacks */
  3455. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  3456. htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
  3457. /* Set the DMA error callback */
  3458. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  3459. /* Enable the DMA channel */
  3460. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer,
  3461. (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3462. {
  3463. return HAL_ERROR;
  3464. }
  3465. break;
  3466. }
  3467. default:
  3468. break;
  3469. }
  3470. /* configure the DMA Burst Mode */
  3471. htim->Instance->DCR = (BurstBaseAddress | BurstLength);
  3472. /* Enable the TIM DMA Request */
  3473. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  3474. htim->State = HAL_TIM_STATE_READY;
  3475. /* Return function status */
  3476. return HAL_OK;
  3477. }
  3478. /**
  3479. * @brief Stops the TIM DMA Burst mode
  3480. * @param htim TIM handle
  3481. * @param BurstRequestSrc TIM DMA Request sources to disable
  3482. * @retval HAL status
  3483. */
  3484. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  3485. {
  3486. HAL_StatusTypeDef status = HAL_OK;
  3487. /* Check the parameters */
  3488. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3489. /* Abort the DMA transfer (at least disable the DMA channel) */
  3490. switch (BurstRequestSrc)
  3491. {
  3492. case TIM_DMA_UPDATE:
  3493. {
  3494. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  3495. break;
  3496. }
  3497. case TIM_DMA_CC1:
  3498. {
  3499. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  3500. break;
  3501. }
  3502. case TIM_DMA_CC2:
  3503. {
  3504. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  3505. break;
  3506. }
  3507. case TIM_DMA_CC3:
  3508. {
  3509. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  3510. break;
  3511. }
  3512. case TIM_DMA_CC4:
  3513. {
  3514. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  3515. break;
  3516. }
  3517. case TIM_DMA_COM:
  3518. {
  3519. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
  3520. break;
  3521. }
  3522. case TIM_DMA_TRIGGER:
  3523. {
  3524. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
  3525. break;
  3526. }
  3527. default:
  3528. break;
  3529. }
  3530. if (HAL_OK == status)
  3531. {
  3532. /* Disable the TIM Update DMA request */
  3533. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  3534. }
  3535. /* Return function status */
  3536. return status;
  3537. }
  3538. /**
  3539. * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
  3540. * @param htim TIM handle
  3541. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
  3542. * This parameter can be one of the following values:
  3543. * @arg TIM_DMABASE_CR1
  3544. * @arg TIM_DMABASE_CR2
  3545. * @arg TIM_DMABASE_SMCR
  3546. * @arg TIM_DMABASE_DIER
  3547. * @arg TIM_DMABASE_SR
  3548. * @arg TIM_DMABASE_EGR
  3549. * @arg TIM_DMABASE_CCMR1
  3550. * @arg TIM_DMABASE_CCMR2
  3551. * @arg TIM_DMABASE_CCER
  3552. * @arg TIM_DMABASE_CNT
  3553. * @arg TIM_DMABASE_PSC
  3554. * @arg TIM_DMABASE_ARR
  3555. * @arg TIM_DMABASE_RCR
  3556. * @arg TIM_DMABASE_CCR1
  3557. * @arg TIM_DMABASE_CCR2
  3558. * @arg TIM_DMABASE_CCR3
  3559. * @arg TIM_DMABASE_CCR4
  3560. * @arg TIM_DMABASE_BDTR
  3561. * @arg TIM_DMABASE_OR1
  3562. * @arg TIM_DMABASE_CCMR3
  3563. * @arg TIM_DMABASE_CCR5
  3564. * @arg TIM_DMABASE_CCR6
  3565. * @arg TIM_DMABASE_OR2
  3566. * @arg TIM_DMABASE_OR3
  3567. * @param BurstRequestSrc TIM DMA Request sources
  3568. * This parameter can be one of the following values:
  3569. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3570. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3571. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3572. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3573. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3574. * @arg TIM_DMA_COM: TIM Commutation DMA source
  3575. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3576. * @param BurstBuffer The Buffer address.
  3577. * @param BurstLength DMA Burst length. This parameter can be one value
  3578. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3579. * @note This function should be used only when BurstLength is equal to DMA data transfer length.
  3580. * @retval HAL status
  3581. */
  3582. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3583. uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
  3584. {
  3585. /* Check the parameters */
  3586. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  3587. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  3588. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3589. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  3590. if ((htim->State == HAL_TIM_STATE_BUSY))
  3591. {
  3592. return HAL_BUSY;
  3593. }
  3594. else if ((htim->State == HAL_TIM_STATE_READY))
  3595. {
  3596. if ((BurstBuffer == NULL) && (BurstLength > 0U))
  3597. {
  3598. return HAL_ERROR;
  3599. }
  3600. else
  3601. {
  3602. htim->State = HAL_TIM_STATE_BUSY;
  3603. }
  3604. }
  3605. else
  3606. {
  3607. /* nothing to do */
  3608. }
  3609. switch (BurstRequestSrc)
  3610. {
  3611. case TIM_DMA_UPDATE:
  3612. {
  3613. /* Set the DMA Period elapsed callbacks */
  3614. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  3615. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  3616. /* Set the DMA error callback */
  3617. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  3618. /* Enable the DMA channel */
  3619. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3620. {
  3621. return HAL_ERROR;
  3622. }
  3623. break;
  3624. }
  3625. case TIM_DMA_CC1:
  3626. {
  3627. /* Set the DMA capture callbacks */
  3628. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  3629. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3630. /* Set the DMA error callback */
  3631. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  3632. /* Enable the DMA channel */
  3633. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3634. {
  3635. return HAL_ERROR;
  3636. }
  3637. break;
  3638. }
  3639. case TIM_DMA_CC2:
  3640. {
  3641. /* Set the DMA capture/compare callbacks */
  3642. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  3643. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3644. /* Set the DMA error callback */
  3645. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  3646. /* Enable the DMA channel */
  3647. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3648. {
  3649. return HAL_ERROR;
  3650. }
  3651. break;
  3652. }
  3653. case TIM_DMA_CC3:
  3654. {
  3655. /* Set the DMA capture callbacks */
  3656. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  3657. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3658. /* Set the DMA error callback */
  3659. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  3660. /* Enable the DMA channel */
  3661. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3662. {
  3663. return HAL_ERROR;
  3664. }
  3665. break;
  3666. }
  3667. case TIM_DMA_CC4:
  3668. {
  3669. /* Set the DMA capture callbacks */
  3670. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  3671. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3672. /* Set the DMA error callback */
  3673. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  3674. /* Enable the DMA channel */
  3675. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3676. {
  3677. return HAL_ERROR;
  3678. }
  3679. break;
  3680. }
  3681. case TIM_DMA_COM:
  3682. {
  3683. /* Set the DMA commutation callbacks */
  3684. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  3685. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  3686. /* Set the DMA error callback */
  3687. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
  3688. /* Enable the DMA channel */
  3689. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3690. {
  3691. return HAL_ERROR;
  3692. }
  3693. break;
  3694. }
  3695. case TIM_DMA_TRIGGER:
  3696. {
  3697. /* Set the DMA trigger callbacks */
  3698. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  3699. htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
  3700. /* Set the DMA error callback */
  3701. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  3702. /* Enable the DMA channel */
  3703. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8U) + 1U) != HAL_OK)
  3704. {
  3705. return HAL_ERROR;
  3706. }
  3707. break;
  3708. }
  3709. default:
  3710. break;
  3711. }
  3712. /* configure the DMA Burst Mode */
  3713. htim->Instance->DCR = (BurstBaseAddress | BurstLength);
  3714. /* Enable the TIM DMA Request */
  3715. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  3716. htim->State = HAL_TIM_STATE_READY;
  3717. /* Return function status */
  3718. return HAL_OK;
  3719. }
  3720. /**
  3721. * @brief Stop the DMA burst reading
  3722. * @param htim TIM handle
  3723. * @param BurstRequestSrc TIM DMA Request sources to disable.
  3724. * @retval HAL status
  3725. */
  3726. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  3727. {
  3728. HAL_StatusTypeDef status = HAL_OK;
  3729. /* Check the parameters */
  3730. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3731. /* Abort the DMA transfer (at least disable the DMA channel) */
  3732. switch (BurstRequestSrc)
  3733. {
  3734. case TIM_DMA_UPDATE:
  3735. {
  3736. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  3737. break;
  3738. }
  3739. case TIM_DMA_CC1:
  3740. {
  3741. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  3742. break;
  3743. }
  3744. case TIM_DMA_CC2:
  3745. {
  3746. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  3747. break;
  3748. }
  3749. case TIM_DMA_CC3:
  3750. {
  3751. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  3752. break;
  3753. }
  3754. case TIM_DMA_CC4:
  3755. {
  3756. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  3757. break;
  3758. }
  3759. case TIM_DMA_COM:
  3760. {
  3761. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
  3762. break;
  3763. }
  3764. case TIM_DMA_TRIGGER:
  3765. {
  3766. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
  3767. break;
  3768. }
  3769. default:
  3770. break;
  3771. }
  3772. if (HAL_OK == status)
  3773. {
  3774. /* Disable the TIM Update DMA request */
  3775. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  3776. }
  3777. /* Return function status */
  3778. return status;
  3779. }
  3780. /**
  3781. * @brief Generate a software event
  3782. * @param htim TIM handle
  3783. * @param EventSource specifies the event source.
  3784. * This parameter can be one of the following values:
  3785. * @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
  3786. * @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
  3787. * @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
  3788. * @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
  3789. * @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
  3790. * @arg TIM_EVENTSOURCE_COM: Timer COM event source
  3791. * @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
  3792. * @arg TIM_EVENTSOURCE_BREAK: Timer Break event source
  3793. * @arg TIM_EVENTSOURCE_BREAK2: Timer Break2 event source
  3794. * @note Basic timers can only generate an update event.
  3795. * @note TIM_EVENTSOURCE_COM is relevant only with advanced timer instances.
  3796. * @note TIM_EVENTSOURCE_BREAK and TIM_EVENTSOURCE_BREAK2 are relevant
  3797. * only for timer instances supporting break input(s).
  3798. * @retval HAL status
  3799. */
  3800. HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
  3801. {
  3802. /* Check the parameters */
  3803. assert_param(IS_TIM_INSTANCE(htim->Instance));
  3804. assert_param(IS_TIM_EVENT_SOURCE(EventSource));
  3805. /* Process Locked */
  3806. __HAL_LOCK(htim);
  3807. /* Change the TIM state */
  3808. htim->State = HAL_TIM_STATE_BUSY;
  3809. /* Set the event sources */
  3810. htim->Instance->EGR = EventSource;
  3811. /* Change the TIM state */
  3812. htim->State = HAL_TIM_STATE_READY;
  3813. __HAL_UNLOCK(htim);
  3814. /* Return function status */
  3815. return HAL_OK;
  3816. }
  3817. /**
  3818. * @brief Configures the OCRef clear feature
  3819. * @param htim TIM handle
  3820. * @param sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
  3821. * contains the OCREF clear feature and parameters for the TIM peripheral.
  3822. * @param Channel specifies the TIM Channel
  3823. * This parameter can be one of the following values:
  3824. * @arg TIM_CHANNEL_1: TIM Channel 1
  3825. * @arg TIM_CHANNEL_2: TIM Channel 2
  3826. * @arg TIM_CHANNEL_3: TIM Channel 3
  3827. * @arg TIM_CHANNEL_4: TIM Channel 4
  3828. * @arg TIM_CHANNEL_5: TIM Channel 5
  3829. * @arg TIM_CHANNEL_6: TIM Channel 6
  3830. * @retval HAL status
  3831. */
  3832. HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
  3833. TIM_ClearInputConfigTypeDef *sClearInputConfig,
  3834. uint32_t Channel)
  3835. {
  3836. /* Check the parameters */
  3837. assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
  3838. assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
  3839. /* Process Locked */
  3840. __HAL_LOCK(htim);
  3841. htim->State = HAL_TIM_STATE_BUSY;
  3842. switch (sClearInputConfig->ClearInputSource)
  3843. {
  3844. case TIM_CLEARINPUTSOURCE_NONE:
  3845. {
  3846. /* Clear the OCREF clear selection bit and the the ETR Bits */
  3847. CLEAR_BIT(htim->Instance->SMCR, (TIM_SMCR_OCCS | TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP));
  3848. break;
  3849. }
  3850. case TIM_CLEARINPUTSOURCE_OCREFCLR:
  3851. {
  3852. /* Clear the OCREF clear selection bit */
  3853. CLEAR_BIT(htim->Instance->SMCR, TIM_SMCR_OCCS);
  3854. }
  3855. break;
  3856. case TIM_CLEARINPUTSOURCE_ETR:
  3857. {
  3858. /* Check the parameters */
  3859. assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
  3860. assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
  3861. assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
  3862. /* When OCRef clear feature is used with ETR source, ETR prescaler must be off */
  3863. if (sClearInputConfig->ClearInputPrescaler != TIM_CLEARINPUTPRESCALER_DIV1)
  3864. {
  3865. htim->State = HAL_TIM_STATE_READY;
  3866. __HAL_UNLOCK(htim);
  3867. return HAL_ERROR;
  3868. }
  3869. TIM_ETR_SetConfig(htim->Instance,
  3870. sClearInputConfig->ClearInputPrescaler,
  3871. sClearInputConfig->ClearInputPolarity,
  3872. sClearInputConfig->ClearInputFilter);
  3873. /* Set the OCREF clear selection bit */
  3874. SET_BIT(htim->Instance->SMCR, TIM_SMCR_OCCS);
  3875. break;
  3876. }
  3877. default:
  3878. break;
  3879. }
  3880. switch (Channel)
  3881. {
  3882. case TIM_CHANNEL_1:
  3883. {
  3884. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  3885. {
  3886. /* Enable the OCREF clear feature for Channel 1 */
  3887. SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
  3888. }
  3889. else
  3890. {
  3891. /* Disable the OCREF clear feature for Channel 1 */
  3892. CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
  3893. }
  3894. break;
  3895. }
  3896. case TIM_CHANNEL_2:
  3897. {
  3898. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  3899. {
  3900. /* Enable the OCREF clear feature for Channel 2 */
  3901. SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
  3902. }
  3903. else
  3904. {
  3905. /* Disable the OCREF clear feature for Channel 2 */
  3906. CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
  3907. }
  3908. break;
  3909. }
  3910. case TIM_CHANNEL_3:
  3911. {
  3912. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  3913. {
  3914. /* Enable the OCREF clear feature for Channel 3 */
  3915. SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
  3916. }
  3917. else
  3918. {
  3919. /* Disable the OCREF clear feature for Channel 3 */
  3920. CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
  3921. }
  3922. break;
  3923. }
  3924. case TIM_CHANNEL_4:
  3925. {
  3926. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  3927. {
  3928. /* Enable the OCREF clear feature for Channel 4 */
  3929. SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
  3930. }
  3931. else
  3932. {
  3933. /* Disable the OCREF clear feature for Channel 4 */
  3934. CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
  3935. }
  3936. break;
  3937. }
  3938. case TIM_CHANNEL_5:
  3939. {
  3940. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  3941. {
  3942. /* Enable the OCREF clear feature for Channel 5 */
  3943. SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
  3944. }
  3945. else
  3946. {
  3947. /* Disable the OCREF clear feature for Channel 5 */
  3948. CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
  3949. }
  3950. break;
  3951. }
  3952. case TIM_CHANNEL_6:
  3953. {
  3954. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  3955. {
  3956. /* Enable the OCREF clear feature for Channel 6 */
  3957. SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
  3958. }
  3959. else
  3960. {
  3961. /* Disable the OCREF clear feature for Channel 6 */
  3962. CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
  3963. }
  3964. break;
  3965. }
  3966. default:
  3967. break;
  3968. }
  3969. htim->State = HAL_TIM_STATE_READY;
  3970. __HAL_UNLOCK(htim);
  3971. return HAL_OK;
  3972. }
  3973. /**
  3974. * @brief Configures the clock source to be used
  3975. * @param htim TIM handle
  3976. * @param sClockSourceConfig pointer to a TIM_ClockConfigTypeDef structure that
  3977. * contains the clock source information for the TIM peripheral.
  3978. * @retval HAL status
  3979. */
  3980. HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, TIM_ClockConfigTypeDef *sClockSourceConfig)
  3981. {
  3982. uint32_t tmpsmcr;
  3983. /* Process Locked */
  3984. __HAL_LOCK(htim);
  3985. htim->State = HAL_TIM_STATE_BUSY;
  3986. /* Check the parameters */
  3987. assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
  3988. /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
  3989. tmpsmcr = htim->Instance->SMCR;
  3990. tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
  3991. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  3992. htim->Instance->SMCR = tmpsmcr;
  3993. switch (sClockSourceConfig->ClockSource)
  3994. {
  3995. case TIM_CLOCKSOURCE_INTERNAL:
  3996. {
  3997. assert_param(IS_TIM_INSTANCE(htim->Instance));
  3998. break;
  3999. }
  4000. case TIM_CLOCKSOURCE_ETRMODE1:
  4001. {
  4002. /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
  4003. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  4004. /* Check ETR input conditioning related parameters */
  4005. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  4006. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4007. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4008. /* Configure the ETR Clock source */
  4009. TIM_ETR_SetConfig(htim->Instance,
  4010. sClockSourceConfig->ClockPrescaler,
  4011. sClockSourceConfig->ClockPolarity,
  4012. sClockSourceConfig->ClockFilter);
  4013. /* Select the External clock mode1 and the ETRF trigger */
  4014. tmpsmcr = htim->Instance->SMCR;
  4015. tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
  4016. /* Write to TIMx SMCR */
  4017. htim->Instance->SMCR = tmpsmcr;
  4018. break;
  4019. }
  4020. case TIM_CLOCKSOURCE_ETRMODE2:
  4021. {
  4022. /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
  4023. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
  4024. /* Check ETR input conditioning related parameters */
  4025. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  4026. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4027. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4028. /* Configure the ETR Clock source */
  4029. TIM_ETR_SetConfig(htim->Instance,
  4030. sClockSourceConfig->ClockPrescaler,
  4031. sClockSourceConfig->ClockPolarity,
  4032. sClockSourceConfig->ClockFilter);
  4033. /* Enable the External clock mode2 */
  4034. htim->Instance->SMCR |= TIM_SMCR_ECE;
  4035. break;
  4036. }
  4037. case TIM_CLOCKSOURCE_TI1:
  4038. {
  4039. /* Check whether or not the timer instance supports external clock mode 1 */
  4040. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4041. /* Check TI1 input conditioning related parameters */
  4042. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4043. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4044. TIM_TI1_ConfigInputStage(htim->Instance,
  4045. sClockSourceConfig->ClockPolarity,
  4046. sClockSourceConfig->ClockFilter);
  4047. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
  4048. break;
  4049. }
  4050. case TIM_CLOCKSOURCE_TI2:
  4051. {
  4052. /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
  4053. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4054. /* Check TI2 input conditioning related parameters */
  4055. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4056. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4057. TIM_TI2_ConfigInputStage(htim->Instance,
  4058. sClockSourceConfig->ClockPolarity,
  4059. sClockSourceConfig->ClockFilter);
  4060. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
  4061. break;
  4062. }
  4063. case TIM_CLOCKSOURCE_TI1ED:
  4064. {
  4065. /* Check whether or not the timer instance supports external clock mode 1 */
  4066. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4067. /* Check TI1 input conditioning related parameters */
  4068. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4069. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4070. TIM_TI1_ConfigInputStage(htim->Instance,
  4071. sClockSourceConfig->ClockPolarity,
  4072. sClockSourceConfig->ClockFilter);
  4073. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
  4074. break;
  4075. }
  4076. case TIM_CLOCKSOURCE_ITR0:
  4077. case TIM_CLOCKSOURCE_ITR1:
  4078. case TIM_CLOCKSOURCE_ITR2:
  4079. case TIM_CLOCKSOURCE_ITR3:
  4080. {
  4081. /* Check whether or not the timer instance supports internal trigger input */
  4082. assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
  4083. TIM_ITRx_SetConfig(htim->Instance, sClockSourceConfig->ClockSource);
  4084. break;
  4085. }
  4086. default:
  4087. break;
  4088. }
  4089. htim->State = HAL_TIM_STATE_READY;
  4090. __HAL_UNLOCK(htim);
  4091. return HAL_OK;
  4092. }
  4093. /**
  4094. * @brief Selects the signal connected to the TI1 input: direct from CH1_input
  4095. * or a XOR combination between CH1_input, CH2_input & CH3_input
  4096. * @param htim TIM handle.
  4097. * @param TI1_Selection Indicate whether or not channel 1 is connected to the
  4098. * output of a XOR gate.
  4099. * This parameter can be one of the following values:
  4100. * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
  4101. * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
  4102. * pins are connected to the TI1 input (XOR combination)
  4103. * @retval HAL status
  4104. */
  4105. HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
  4106. {
  4107. uint32_t tmpcr2;
  4108. /* Check the parameters */
  4109. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  4110. assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
  4111. /* Get the TIMx CR2 register value */
  4112. tmpcr2 = htim->Instance->CR2;
  4113. /* Reset the TI1 selection */
  4114. tmpcr2 &= ~TIM_CR2_TI1S;
  4115. /* Set the TI1 selection */
  4116. tmpcr2 |= TI1_Selection;
  4117. /* Write to TIMxCR2 */
  4118. htim->Instance->CR2 = tmpcr2;
  4119. return HAL_OK;
  4120. }
  4121. /**
  4122. * @brief Configures the TIM in Slave mode
  4123. * @param htim TIM handle.
  4124. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  4125. * contains the selected trigger (internal trigger input, filtered
  4126. * timer input or external trigger input) and the Slave mode
  4127. * (Disable, Reset, Gated, Trigger, External clock mode 1).
  4128. * @retval HAL status
  4129. */
  4130. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro(TIM_HandleTypeDef *htim, TIM_SlaveConfigTypeDef *sSlaveConfig)
  4131. {
  4132. /* Check the parameters */
  4133. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  4134. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  4135. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  4136. __HAL_LOCK(htim);
  4137. htim->State = HAL_TIM_STATE_BUSY;
  4138. if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
  4139. {
  4140. htim->State = HAL_TIM_STATE_READY;
  4141. __HAL_UNLOCK(htim);
  4142. return HAL_ERROR;
  4143. }
  4144. /* Disable Trigger Interrupt */
  4145. __HAL_TIM_DISABLE_IT(htim, TIM_IT_TRIGGER);
  4146. /* Disable Trigger DMA request */
  4147. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  4148. htim->State = HAL_TIM_STATE_READY;
  4149. __HAL_UNLOCK(htim);
  4150. return HAL_OK;
  4151. }
  4152. /**
  4153. * @brief Configures the TIM in Slave mode in interrupt mode
  4154. * @param htim TIM handle.
  4155. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  4156. * contains the selected trigger (internal trigger input, filtered
  4157. * timer input or external trigger input) and the Slave mode
  4158. * (Disable, Reset, Gated, Trigger, External clock mode 1).
  4159. * @retval HAL status
  4160. */
  4161. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro_IT(TIM_HandleTypeDef *htim,
  4162. TIM_SlaveConfigTypeDef *sSlaveConfig)
  4163. {
  4164. /* Check the parameters */
  4165. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  4166. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  4167. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  4168. __HAL_LOCK(htim);
  4169. htim->State = HAL_TIM_STATE_BUSY;
  4170. if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
  4171. {
  4172. htim->State = HAL_TIM_STATE_READY;
  4173. __HAL_UNLOCK(htim);
  4174. return HAL_ERROR;
  4175. }
  4176. /* Enable Trigger Interrupt */
  4177. __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);
  4178. /* Disable Trigger DMA request */
  4179. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  4180. htim->State = HAL_TIM_STATE_READY;
  4181. __HAL_UNLOCK(htim);
  4182. return HAL_OK;
  4183. }
  4184. /**
  4185. * @brief Read the captured value from Capture Compare unit
  4186. * @param htim TIM handle.
  4187. * @param Channel TIM Channels to be enabled
  4188. * This parameter can be one of the following values:
  4189. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  4190. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  4191. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  4192. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  4193. * @retval Captured value
  4194. */
  4195. uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)
  4196. {
  4197. uint32_t tmpreg = 0U;
  4198. switch (Channel)
  4199. {
  4200. case TIM_CHANNEL_1:
  4201. {
  4202. /* Check the parameters */
  4203. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  4204. /* Return the capture 1 value */
  4205. tmpreg = htim->Instance->CCR1;
  4206. break;
  4207. }
  4208. case TIM_CHANNEL_2:
  4209. {
  4210. /* Check the parameters */
  4211. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4212. /* Return the capture 2 value */
  4213. tmpreg = htim->Instance->CCR2;
  4214. break;
  4215. }
  4216. case TIM_CHANNEL_3:
  4217. {
  4218. /* Check the parameters */
  4219. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  4220. /* Return the capture 3 value */
  4221. tmpreg = htim->Instance->CCR3;
  4222. break;
  4223. }
  4224. case TIM_CHANNEL_4:
  4225. {
  4226. /* Check the parameters */
  4227. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  4228. /* Return the capture 4 value */
  4229. tmpreg = htim->Instance->CCR4;
  4230. break;
  4231. }
  4232. default:
  4233. break;
  4234. }
  4235. return tmpreg;
  4236. }
  4237. /**
  4238. * @}
  4239. */
  4240. /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
  4241. * @brief TIM Callbacks functions
  4242. *
  4243. @verbatim
  4244. ==============================================================================
  4245. ##### TIM Callbacks functions #####
  4246. ==============================================================================
  4247. [..]
  4248. This section provides TIM callback functions:
  4249. (+) TIM Period elapsed callback
  4250. (+) TIM Output Compare callback
  4251. (+) TIM Input capture callback
  4252. (+) TIM Trigger callback
  4253. (+) TIM Error callback
  4254. @endverbatim
  4255. * @{
  4256. */
  4257. /**
  4258. * @brief Period elapsed callback in non-blocking mode
  4259. * @param htim TIM handle
  4260. * @retval None
  4261. */
  4262. __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  4263. {
  4264. /* Prevent unused argument(s) compilation warning */
  4265. UNUSED(htim);
  4266. /* NOTE : This function should not be modified, when the callback is needed,
  4267. the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
  4268. */
  4269. }
  4270. /**
  4271. * @brief Period elapsed half complete callback in non-blocking mode
  4272. * @param htim TIM handle
  4273. * @retval None
  4274. */
  4275. __weak void HAL_TIM_PeriodElapsedHalfCpltCallback(TIM_HandleTypeDef *htim)
  4276. {
  4277. /* Prevent unused argument(s) compilation warning */
  4278. UNUSED(htim);
  4279. /* NOTE : This function should not be modified, when the callback is needed,
  4280. the HAL_TIM_PeriodElapsedHalfCpltCallback could be implemented in the user file
  4281. */
  4282. }
  4283. /**
  4284. * @brief Output Compare callback in non-blocking mode
  4285. * @param htim TIM OC handle
  4286. * @retval None
  4287. */
  4288. __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
  4289. {
  4290. /* Prevent unused argument(s) compilation warning */
  4291. UNUSED(htim);
  4292. /* NOTE : This function should not be modified, when the callback is needed,
  4293. the HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
  4294. */
  4295. }
  4296. /**
  4297. * @brief Input Capture callback in non-blocking mode
  4298. * @param htim TIM IC handle
  4299. * @retval None
  4300. */
  4301. __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  4302. {
  4303. /* Prevent unused argument(s) compilation warning */
  4304. UNUSED(htim);
  4305. /* NOTE : This function should not be modified, when the callback is needed,
  4306. the HAL_TIM_IC_CaptureCallback could be implemented in the user file
  4307. */
  4308. }
  4309. /**
  4310. * @brief Input Capture half complete callback in non-blocking mode
  4311. * @param htim TIM IC handle
  4312. * @retval None
  4313. */
  4314. __weak void HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef *htim)
  4315. {
  4316. /* Prevent unused argument(s) compilation warning */
  4317. UNUSED(htim);
  4318. /* NOTE : This function should not be modified, when the callback is needed,
  4319. the HAL_TIM_IC_CaptureHalfCpltCallback could be implemented in the user file
  4320. */
  4321. }
  4322. /**
  4323. * @brief PWM Pulse finished callback in non-blocking mode
  4324. * @param htim TIM handle
  4325. * @retval None
  4326. */
  4327. __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  4328. {
  4329. /* Prevent unused argument(s) compilation warning */
  4330. UNUSED(htim);
  4331. /* NOTE : This function should not be modified, when the callback is needed,
  4332. the HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
  4333. */
  4334. }
  4335. /**
  4336. * @brief PWM Pulse finished half complete callback in non-blocking mode
  4337. * @param htim TIM handle
  4338. * @retval None
  4339. */
  4340. __weak void HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef *htim)
  4341. {
  4342. /* Prevent unused argument(s) compilation warning */
  4343. UNUSED(htim);
  4344. /* NOTE : This function should not be modified, when the callback is needed,
  4345. the HAL_TIM_PWM_PulseFinishedHalfCpltCallback could be implemented in the user file
  4346. */
  4347. }
  4348. /**
  4349. * @brief Hall Trigger detection callback in non-blocking mode
  4350. * @param htim TIM handle
  4351. * @retval None
  4352. */
  4353. __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
  4354. {
  4355. /* Prevent unused argument(s) compilation warning */
  4356. UNUSED(htim);
  4357. /* NOTE : This function should not be modified, when the callback is needed,
  4358. the HAL_TIM_TriggerCallback could be implemented in the user file
  4359. */
  4360. }
  4361. /**
  4362. * @brief Hall Trigger detection half complete callback in non-blocking mode
  4363. * @param htim TIM handle
  4364. * @retval None
  4365. */
  4366. __weak void HAL_TIM_TriggerHalfCpltCallback(TIM_HandleTypeDef *htim)
  4367. {
  4368. /* Prevent unused argument(s) compilation warning */
  4369. UNUSED(htim);
  4370. /* NOTE : This function should not be modified, when the callback is needed,
  4371. the HAL_TIM_TriggerHalfCpltCallback could be implemented in the user file
  4372. */
  4373. }
  4374. /**
  4375. * @brief Timer error callback in non-blocking mode
  4376. * @param htim TIM handle
  4377. * @retval None
  4378. */
  4379. __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
  4380. {
  4381. /* Prevent unused argument(s) compilation warning */
  4382. UNUSED(htim);
  4383. /* NOTE : This function should not be modified, when the callback is needed,
  4384. the HAL_TIM_ErrorCallback could be implemented in the user file
  4385. */
  4386. }
  4387. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4388. /**
  4389. * @brief Register a User TIM callback to be used instead of the weak predefined callback
  4390. * @param htim tim handle
  4391. * @param CallbackID ID of the callback to be registered
  4392. * This parameter can be one of the following values:
  4393. * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
  4394. * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
  4395. * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
  4396. * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
  4397. * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
  4398. * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
  4399. * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
  4400. * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
  4401. * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
  4402. * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
  4403. * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
  4404. * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
  4405. * @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
  4406. * @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
  4407. * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
  4408. * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
  4409. * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
  4410. * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
  4411. * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
  4412. * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
  4413. * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
  4414. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
  4415. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
  4416. * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
  4417. * @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
  4418. * @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
  4419. * @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
  4420. * @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
  4421. * @param pCallback pointer to the callback function
  4422. * @retval status
  4423. */
  4424. HAL_StatusTypeDef HAL_TIM_RegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID,
  4425. pTIM_CallbackTypeDef pCallback)
  4426. {
  4427. HAL_StatusTypeDef status = HAL_OK;
  4428. if (pCallback == NULL)
  4429. {
  4430. return HAL_ERROR;
  4431. }
  4432. /* Process locked */
  4433. __HAL_LOCK(htim);
  4434. if (htim->State == HAL_TIM_STATE_READY)
  4435. {
  4436. switch (CallbackID)
  4437. {
  4438. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4439. htim->Base_MspInitCallback = pCallback;
  4440. break;
  4441. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4442. htim->Base_MspDeInitCallback = pCallback;
  4443. break;
  4444. case HAL_TIM_IC_MSPINIT_CB_ID :
  4445. htim->IC_MspInitCallback = pCallback;
  4446. break;
  4447. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4448. htim->IC_MspDeInitCallback = pCallback;
  4449. break;
  4450. case HAL_TIM_OC_MSPINIT_CB_ID :
  4451. htim->OC_MspInitCallback = pCallback;
  4452. break;
  4453. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4454. htim->OC_MspDeInitCallback = pCallback;
  4455. break;
  4456. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4457. htim->PWM_MspInitCallback = pCallback;
  4458. break;
  4459. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4460. htim->PWM_MspDeInitCallback = pCallback;
  4461. break;
  4462. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4463. htim->OnePulse_MspInitCallback = pCallback;
  4464. break;
  4465. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4466. htim->OnePulse_MspDeInitCallback = pCallback;
  4467. break;
  4468. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4469. htim->Encoder_MspInitCallback = pCallback;
  4470. break;
  4471. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4472. htim->Encoder_MspDeInitCallback = pCallback;
  4473. break;
  4474. case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
  4475. htim->HallSensor_MspInitCallback = pCallback;
  4476. break;
  4477. case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
  4478. htim->HallSensor_MspDeInitCallback = pCallback;
  4479. break;
  4480. case HAL_TIM_PERIOD_ELAPSED_CB_ID :
  4481. htim->PeriodElapsedCallback = pCallback;
  4482. break;
  4483. case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
  4484. htim->PeriodElapsedHalfCpltCallback = pCallback;
  4485. break;
  4486. case HAL_TIM_TRIGGER_CB_ID :
  4487. htim->TriggerCallback = pCallback;
  4488. break;
  4489. case HAL_TIM_TRIGGER_HALF_CB_ID :
  4490. htim->TriggerHalfCpltCallback = pCallback;
  4491. break;
  4492. case HAL_TIM_IC_CAPTURE_CB_ID :
  4493. htim->IC_CaptureCallback = pCallback;
  4494. break;
  4495. case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
  4496. htim->IC_CaptureHalfCpltCallback = pCallback;
  4497. break;
  4498. case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
  4499. htim->OC_DelayElapsedCallback = pCallback;
  4500. break;
  4501. case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
  4502. htim->PWM_PulseFinishedCallback = pCallback;
  4503. break;
  4504. case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
  4505. htim->PWM_PulseFinishedHalfCpltCallback = pCallback;
  4506. break;
  4507. case HAL_TIM_ERROR_CB_ID :
  4508. htim->ErrorCallback = pCallback;
  4509. break;
  4510. case HAL_TIM_COMMUTATION_CB_ID :
  4511. htim->CommutationCallback = pCallback;
  4512. break;
  4513. case HAL_TIM_COMMUTATION_HALF_CB_ID :
  4514. htim->CommutationHalfCpltCallback = pCallback;
  4515. break;
  4516. case HAL_TIM_BREAK_CB_ID :
  4517. htim->BreakCallback = pCallback;
  4518. break;
  4519. case HAL_TIM_BREAK2_CB_ID :
  4520. htim->Break2Callback = pCallback;
  4521. break;
  4522. default :
  4523. /* Return error status */
  4524. status = HAL_ERROR;
  4525. break;
  4526. }
  4527. }
  4528. else if (htim->State == HAL_TIM_STATE_RESET)
  4529. {
  4530. switch (CallbackID)
  4531. {
  4532. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4533. htim->Base_MspInitCallback = pCallback;
  4534. break;
  4535. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4536. htim->Base_MspDeInitCallback = pCallback;
  4537. break;
  4538. case HAL_TIM_IC_MSPINIT_CB_ID :
  4539. htim->IC_MspInitCallback = pCallback;
  4540. break;
  4541. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4542. htim->IC_MspDeInitCallback = pCallback;
  4543. break;
  4544. case HAL_TIM_OC_MSPINIT_CB_ID :
  4545. htim->OC_MspInitCallback = pCallback;
  4546. break;
  4547. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4548. htim->OC_MspDeInitCallback = pCallback;
  4549. break;
  4550. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4551. htim->PWM_MspInitCallback = pCallback;
  4552. break;
  4553. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4554. htim->PWM_MspDeInitCallback = pCallback;
  4555. break;
  4556. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4557. htim->OnePulse_MspInitCallback = pCallback;
  4558. break;
  4559. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4560. htim->OnePulse_MspDeInitCallback = pCallback;
  4561. break;
  4562. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4563. htim->Encoder_MspInitCallback = pCallback;
  4564. break;
  4565. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4566. htim->Encoder_MspDeInitCallback = pCallback;
  4567. break;
  4568. case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
  4569. htim->HallSensor_MspInitCallback = pCallback;
  4570. break;
  4571. case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
  4572. htim->HallSensor_MspDeInitCallback = pCallback;
  4573. break;
  4574. default :
  4575. /* Return error status */
  4576. status = HAL_ERROR;
  4577. break;
  4578. }
  4579. }
  4580. else
  4581. {
  4582. /* Return error status */
  4583. status = HAL_ERROR;
  4584. }
  4585. /* Release Lock */
  4586. __HAL_UNLOCK(htim);
  4587. return status;
  4588. }
  4589. /**
  4590. * @brief Unregister a TIM callback
  4591. * TIM callback is redirected to the weak predefined callback
  4592. * @param htim tim handle
  4593. * @param CallbackID ID of the callback to be unregistered
  4594. * This parameter can be one of the following values:
  4595. * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
  4596. * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
  4597. * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
  4598. * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
  4599. * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
  4600. * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
  4601. * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
  4602. * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
  4603. * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
  4604. * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
  4605. * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
  4606. * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
  4607. * @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
  4608. * @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
  4609. * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
  4610. * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
  4611. * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
  4612. * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
  4613. * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
  4614. * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
  4615. * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
  4616. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
  4617. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
  4618. * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
  4619. * @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
  4620. * @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
  4621. * @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
  4622. * @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
  4623. * @retval status
  4624. */
  4625. HAL_StatusTypeDef HAL_TIM_UnRegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID)
  4626. {
  4627. HAL_StatusTypeDef status = HAL_OK;
  4628. /* Process locked */
  4629. __HAL_LOCK(htim);
  4630. if (htim->State == HAL_TIM_STATE_READY)
  4631. {
  4632. switch (CallbackID)
  4633. {
  4634. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4635. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit; /* Legacy weak Base MspInit Callback */
  4636. break;
  4637. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4638. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit; /* Legacy weak Base Msp DeInit Callback */
  4639. break;
  4640. case HAL_TIM_IC_MSPINIT_CB_ID :
  4641. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit; /* Legacy weak IC Msp Init Callback */
  4642. break;
  4643. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4644. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit; /* Legacy weak IC Msp DeInit Callback */
  4645. break;
  4646. case HAL_TIM_OC_MSPINIT_CB_ID :
  4647. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit; /* Legacy weak OC Msp Init Callback */
  4648. break;
  4649. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4650. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit; /* Legacy weak OC Msp DeInit Callback */
  4651. break;
  4652. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4653. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit; /* Legacy weak PWM Msp Init Callback */
  4654. break;
  4655. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4656. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit; /* Legacy weak PWM Msp DeInit Callback */
  4657. break;
  4658. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4659. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit; /* Legacy weak One Pulse Msp Init Callback */
  4660. break;
  4661. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4662. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit; /* Legacy weak One Pulse Msp DeInit Callback */
  4663. break;
  4664. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4665. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit; /* Legacy weak Encoder Msp Init Callback */
  4666. break;
  4667. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4668. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit; /* Legacy weak Encoder Msp DeInit Callback */
  4669. break;
  4670. case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
  4671. htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit; /* Legacy weak Hall Sensor Msp Init Callback */
  4672. break;
  4673. case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
  4674. htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit; /* Legacy weak Hall Sensor Msp DeInit Callback */
  4675. break;
  4676. case HAL_TIM_PERIOD_ELAPSED_CB_ID :
  4677. htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback; /* Legacy weak Period Elapsed Callback */
  4678. break;
  4679. case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
  4680. htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback; /* Legacy weak Period Elapsed half complete Callback */
  4681. break;
  4682. case HAL_TIM_TRIGGER_CB_ID :
  4683. htim->TriggerCallback = HAL_TIM_TriggerCallback; /* Legacy weak Trigger Callback */
  4684. break;
  4685. case HAL_TIM_TRIGGER_HALF_CB_ID :
  4686. htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback; /* Legacy weak Trigger half complete Callback */
  4687. break;
  4688. case HAL_TIM_IC_CAPTURE_CB_ID :
  4689. htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback; /* Legacy weak IC Capture Callback */
  4690. break;
  4691. case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
  4692. htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback; /* Legacy weak IC Capture half complete Callback */
  4693. break;
  4694. case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
  4695. htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback; /* Legacy weak OC Delay Elapsed Callback */
  4696. break;
  4697. case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
  4698. htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback; /* Legacy weak PWM Pulse Finished Callback */
  4699. break;
  4700. case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
  4701. htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback; /* Legacy weak PWM Pulse Finished half complete Callback */
  4702. break;
  4703. case HAL_TIM_ERROR_CB_ID :
  4704. htim->ErrorCallback = HAL_TIM_ErrorCallback; /* Legacy weak Error Callback */
  4705. break;
  4706. case HAL_TIM_COMMUTATION_CB_ID :
  4707. htim->CommutationCallback = HAL_TIMEx_CommutCallback; /* Legacy weak Commutation Callback */
  4708. break;
  4709. case HAL_TIM_COMMUTATION_HALF_CB_ID :
  4710. htim->CommutationHalfCpltCallback = HAL_TIMEx_CommutHalfCpltCallback; /* Legacy weak Commutation half complete Callback */
  4711. break;
  4712. case HAL_TIM_BREAK_CB_ID :
  4713. htim->BreakCallback = HAL_TIMEx_BreakCallback; /* Legacy weak Break Callback */
  4714. break;
  4715. case HAL_TIM_BREAK2_CB_ID :
  4716. htim->Break2Callback = HAL_TIMEx_Break2Callback; /* Legacy weak Break2 Callback */
  4717. break;
  4718. default :
  4719. /* Return error status */
  4720. status = HAL_ERROR;
  4721. break;
  4722. }
  4723. }
  4724. else if (htim->State == HAL_TIM_STATE_RESET)
  4725. {
  4726. switch (CallbackID)
  4727. {
  4728. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4729. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit; /* Legacy weak Base MspInit Callback */
  4730. break;
  4731. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4732. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit; /* Legacy weak Base Msp DeInit Callback */
  4733. break;
  4734. case HAL_TIM_IC_MSPINIT_CB_ID :
  4735. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit; /* Legacy weak IC Msp Init Callback */
  4736. break;
  4737. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4738. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit; /* Legacy weak IC Msp DeInit Callback */
  4739. break;
  4740. case HAL_TIM_OC_MSPINIT_CB_ID :
  4741. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit; /* Legacy weak OC Msp Init Callback */
  4742. break;
  4743. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4744. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit; /* Legacy weak OC Msp DeInit Callback */
  4745. break;
  4746. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4747. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit; /* Legacy weak PWM Msp Init Callback */
  4748. break;
  4749. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4750. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit; /* Legacy weak PWM Msp DeInit Callback */
  4751. break;
  4752. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4753. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit; /* Legacy weak One Pulse Msp Init Callback */
  4754. break;
  4755. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4756. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit; /* Legacy weak One Pulse Msp DeInit Callback */
  4757. break;
  4758. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4759. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit; /* Legacy weak Encoder Msp Init Callback */
  4760. break;
  4761. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4762. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit; /* Legacy weak Encoder Msp DeInit Callback */
  4763. break;
  4764. case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
  4765. htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit; /* Legacy weak Hall Sensor Msp Init Callback */
  4766. break;
  4767. case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
  4768. htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit; /* Legacy weak Hall Sensor Msp DeInit Callback */
  4769. break;
  4770. default :
  4771. /* Return error status */
  4772. status = HAL_ERROR;
  4773. break;
  4774. }
  4775. }
  4776. else
  4777. {
  4778. /* Return error status */
  4779. status = HAL_ERROR;
  4780. }
  4781. /* Release Lock */
  4782. __HAL_UNLOCK(htim);
  4783. return status;
  4784. }
  4785. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  4786. /**
  4787. * @}
  4788. */
  4789. /** @defgroup TIM_Exported_Functions_Group10 TIM Peripheral State functions
  4790. * @brief TIM Peripheral State functions
  4791. *
  4792. @verbatim
  4793. ==============================================================================
  4794. ##### Peripheral State functions #####
  4795. ==============================================================================
  4796. [..]
  4797. This subsection permits to get in run-time the status of the peripheral
  4798. and the data flow.
  4799. @endverbatim
  4800. * @{
  4801. */
  4802. /**
  4803. * @brief Return the TIM Base handle state.
  4804. * @param htim TIM Base handle
  4805. * @retval HAL state
  4806. */
  4807. HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(TIM_HandleTypeDef *htim)
  4808. {
  4809. return htim->State;
  4810. }
  4811. /**
  4812. * @brief Return the TIM OC handle state.
  4813. * @param htim TIM Output Compare handle
  4814. * @retval HAL state
  4815. */
  4816. HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(TIM_HandleTypeDef *htim)
  4817. {
  4818. return htim->State;
  4819. }
  4820. /**
  4821. * @brief Return the TIM PWM handle state.
  4822. * @param htim TIM handle
  4823. * @retval HAL state
  4824. */
  4825. HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(TIM_HandleTypeDef *htim)
  4826. {
  4827. return htim->State;
  4828. }
  4829. /**
  4830. * @brief Return the TIM Input Capture handle state.
  4831. * @param htim TIM IC handle
  4832. * @retval HAL state
  4833. */
  4834. HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(TIM_HandleTypeDef *htim)
  4835. {
  4836. return htim->State;
  4837. }
  4838. /**
  4839. * @brief Return the TIM One Pulse Mode handle state.
  4840. * @param htim TIM OPM handle
  4841. * @retval HAL state
  4842. */
  4843. HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(TIM_HandleTypeDef *htim)
  4844. {
  4845. return htim->State;
  4846. }
  4847. /**
  4848. * @brief Return the TIM Encoder Mode handle state.
  4849. * @param htim TIM Encoder Interface handle
  4850. * @retval HAL state
  4851. */
  4852. HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(TIM_HandleTypeDef *htim)
  4853. {
  4854. return htim->State;
  4855. }
  4856. /**
  4857. * @}
  4858. */
  4859. /**
  4860. * @}
  4861. */
  4862. /** @defgroup TIM_Private_Functions TIM Private Functions
  4863. * @{
  4864. */
  4865. /**
  4866. * @brief TIM DMA error callback
  4867. * @param hdma pointer to DMA handle.
  4868. * @retval None
  4869. */
  4870. void TIM_DMAError(DMA_HandleTypeDef *hdma)
  4871. {
  4872. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  4873. htim->State = HAL_TIM_STATE_READY;
  4874. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4875. htim->ErrorCallback(htim);
  4876. #else
  4877. HAL_TIM_ErrorCallback(htim);
  4878. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  4879. }
  4880. /**
  4881. * @brief TIM DMA Delay Pulse complete callback.
  4882. * @param hdma pointer to DMA handle.
  4883. * @retval None
  4884. */
  4885. void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
  4886. {
  4887. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  4888. htim->State = HAL_TIM_STATE_READY;
  4889. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  4890. {
  4891. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  4892. }
  4893. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  4894. {
  4895. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  4896. }
  4897. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  4898. {
  4899. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  4900. }
  4901. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  4902. {
  4903. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  4904. }
  4905. else
  4906. {
  4907. /* nothing to do */
  4908. }
  4909. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4910. htim->PWM_PulseFinishedCallback(htim);
  4911. #else
  4912. HAL_TIM_PWM_PulseFinishedCallback(htim);
  4913. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  4914. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  4915. }
  4916. /**
  4917. * @brief TIM DMA Delay Pulse half complete callback.
  4918. * @param hdma pointer to DMA handle.
  4919. * @retval None
  4920. */
  4921. void TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef *hdma)
  4922. {
  4923. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  4924. htim->State = HAL_TIM_STATE_READY;
  4925. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  4926. {
  4927. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  4928. }
  4929. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  4930. {
  4931. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  4932. }
  4933. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  4934. {
  4935. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  4936. }
  4937. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  4938. {
  4939. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  4940. }
  4941. else
  4942. {
  4943. /* nothing to do */
  4944. }
  4945. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4946. htim->PWM_PulseFinishedHalfCpltCallback(htim);
  4947. #else
  4948. HAL_TIM_PWM_PulseFinishedHalfCpltCallback(htim);
  4949. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  4950. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  4951. }
  4952. /**
  4953. * @brief TIM DMA Capture complete callback.
  4954. * @param hdma pointer to DMA handle.
  4955. * @retval None
  4956. */
  4957. void TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
  4958. {
  4959. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  4960. htim->State = HAL_TIM_STATE_READY;
  4961. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  4962. {
  4963. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  4964. }
  4965. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  4966. {
  4967. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  4968. }
  4969. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  4970. {
  4971. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  4972. }
  4973. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  4974. {
  4975. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  4976. }
  4977. else
  4978. {
  4979. /* nothing to do */
  4980. }
  4981. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4982. htim->IC_CaptureCallback(htim);
  4983. #else
  4984. HAL_TIM_IC_CaptureCallback(htim);
  4985. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  4986. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  4987. }
  4988. /**
  4989. * @brief TIM DMA Capture half complete callback.
  4990. * @param hdma pointer to DMA handle.
  4991. * @retval None
  4992. */
  4993. void TIM_DMACaptureHalfCplt(DMA_HandleTypeDef *hdma)
  4994. {
  4995. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  4996. htim->State = HAL_TIM_STATE_READY;
  4997. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  4998. {
  4999. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5000. }
  5001. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5002. {
  5003. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5004. }
  5005. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5006. {
  5007. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5008. }
  5009. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5010. {
  5011. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5012. }
  5013. else
  5014. {
  5015. /* nothing to do */
  5016. }
  5017. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5018. htim->IC_CaptureHalfCpltCallback(htim);
  5019. #else
  5020. HAL_TIM_IC_CaptureHalfCpltCallback(htim);
  5021. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5022. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5023. }
  5024. /**
  5025. * @brief TIM DMA Period Elapse complete callback.
  5026. * @param hdma pointer to DMA handle.
  5027. * @retval None
  5028. */
  5029. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
  5030. {
  5031. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5032. htim->State = HAL_TIM_STATE_READY;
  5033. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5034. htim->PeriodElapsedCallback(htim);
  5035. #else
  5036. HAL_TIM_PeriodElapsedCallback(htim);
  5037. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5038. }
  5039. /**
  5040. * @brief TIM DMA Period Elapse half complete callback.
  5041. * @param hdma pointer to DMA handle.
  5042. * @retval None
  5043. */
  5044. static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma)
  5045. {
  5046. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5047. htim->State = HAL_TIM_STATE_READY;
  5048. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5049. htim->PeriodElapsedHalfCpltCallback(htim);
  5050. #else
  5051. HAL_TIM_PeriodElapsedHalfCpltCallback(htim);
  5052. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5053. }
  5054. /**
  5055. * @brief TIM DMA Trigger callback.
  5056. * @param hdma pointer to DMA handle.
  5057. * @retval None
  5058. */
  5059. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
  5060. {
  5061. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5062. htim->State = HAL_TIM_STATE_READY;
  5063. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5064. htim->TriggerCallback(htim);
  5065. #else
  5066. HAL_TIM_TriggerCallback(htim);
  5067. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5068. }
  5069. /**
  5070. * @brief TIM DMA Trigger half complete callback.
  5071. * @param hdma pointer to DMA handle.
  5072. * @retval None
  5073. */
  5074. static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma)
  5075. {
  5076. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5077. htim->State = HAL_TIM_STATE_READY;
  5078. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5079. htim->TriggerHalfCpltCallback(htim);
  5080. #else
  5081. HAL_TIM_TriggerHalfCpltCallback(htim);
  5082. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5083. }
  5084. /**
  5085. * @brief Time Base configuration
  5086. * @param TIMx TIM peripheral
  5087. * @param Structure TIM Base configuration structure
  5088. * @retval None
  5089. */
  5090. void TIM_Base_SetConfig(TIM_TypeDef *TIMx, TIM_Base_InitTypeDef *Structure)
  5091. {
  5092. uint32_t tmpcr1;
  5093. tmpcr1 = TIMx->CR1;
  5094. /* Set TIM Time Base Unit parameters ---------------------------------------*/
  5095. if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
  5096. {
  5097. /* Select the Counter Mode */
  5098. tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
  5099. tmpcr1 |= Structure->CounterMode;
  5100. }
  5101. if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
  5102. {
  5103. /* Set the clock division */
  5104. tmpcr1 &= ~TIM_CR1_CKD;
  5105. tmpcr1 |= (uint32_t)Structure->ClockDivision;
  5106. }
  5107. /* Set the auto-reload preload */
  5108. MODIFY_REG(tmpcr1, TIM_CR1_ARPE, Structure->AutoReloadPreload);
  5109. TIMx->CR1 = tmpcr1;
  5110. /* Set the Autoreload value */
  5111. TIMx->ARR = (uint32_t)Structure->Period ;
  5112. /* Set the Prescaler value */
  5113. TIMx->PSC = Structure->Prescaler;
  5114. if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
  5115. {
  5116. /* Set the Repetition Counter value */
  5117. TIMx->RCR = Structure->RepetitionCounter;
  5118. }
  5119. /* Generate an update event to reload the Prescaler
  5120. and the repetition counter (only for advanced timer) value immediately */
  5121. TIMx->EGR = TIM_EGR_UG;
  5122. }
  5123. /**
  5124. * @brief Timer Output Compare 1 configuration
  5125. * @param TIMx to select the TIM peripheral
  5126. * @param OC_Config The ouput configuration structure
  5127. * @retval None
  5128. */
  5129. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  5130. {
  5131. uint32_t tmpccmrx;
  5132. uint32_t tmpccer;
  5133. uint32_t tmpcr2;
  5134. /* Disable the Channel 1: Reset the CC1E Bit */
  5135. TIMx->CCER &= ~TIM_CCER_CC1E;
  5136. /* Get the TIMx CCER register value */
  5137. tmpccer = TIMx->CCER;
  5138. /* Get the TIMx CR2 register value */
  5139. tmpcr2 = TIMx->CR2;
  5140. /* Get the TIMx CCMR1 register value */
  5141. tmpccmrx = TIMx->CCMR1;
  5142. /* Reset the Output Compare Mode Bits */
  5143. tmpccmrx &= ~TIM_CCMR1_OC1M;
  5144. tmpccmrx &= ~TIM_CCMR1_CC1S;
  5145. /* Select the Output Compare Mode */
  5146. tmpccmrx |= OC_Config->OCMode;
  5147. /* Reset the Output Polarity level */
  5148. tmpccer &= ~TIM_CCER_CC1P;
  5149. /* Set the Output Compare Polarity */
  5150. tmpccer |= OC_Config->OCPolarity;
  5151. if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_1))
  5152. {
  5153. /* Check parameters */
  5154. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  5155. /* Reset the Output N Polarity level */
  5156. tmpccer &= ~TIM_CCER_CC1NP;
  5157. /* Set the Output N Polarity */
  5158. tmpccer |= OC_Config->OCNPolarity;
  5159. /* Reset the Output N State */
  5160. tmpccer &= ~TIM_CCER_CC1NE;
  5161. }
  5162. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5163. {
  5164. /* Check parameters */
  5165. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  5166. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  5167. /* Reset the Output Compare and Output Compare N IDLE State */
  5168. tmpcr2 &= ~TIM_CR2_OIS1;
  5169. tmpcr2 &= ~TIM_CR2_OIS1N;
  5170. /* Set the Output Idle state */
  5171. tmpcr2 |= OC_Config->OCIdleState;
  5172. /* Set the Output N Idle state */
  5173. tmpcr2 |= OC_Config->OCNIdleState;
  5174. }
  5175. /* Write to TIMx CR2 */
  5176. TIMx->CR2 = tmpcr2;
  5177. /* Write to TIMx CCMR1 */
  5178. TIMx->CCMR1 = tmpccmrx;
  5179. /* Set the Capture Compare Register value */
  5180. TIMx->CCR1 = OC_Config->Pulse;
  5181. /* Write to TIMx CCER */
  5182. TIMx->CCER = tmpccer;
  5183. }
  5184. /**
  5185. * @brief Timer Output Compare 2 configuration
  5186. * @param TIMx to select the TIM peripheral
  5187. * @param OC_Config The ouput configuration structure
  5188. * @retval None
  5189. */
  5190. void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  5191. {
  5192. uint32_t tmpccmrx;
  5193. uint32_t tmpccer;
  5194. uint32_t tmpcr2;
  5195. /* Disable the Channel 2: Reset the CC2E Bit */
  5196. TIMx->CCER &= ~TIM_CCER_CC2E;
  5197. /* Get the TIMx CCER register value */
  5198. tmpccer = TIMx->CCER;
  5199. /* Get the TIMx CR2 register value */
  5200. tmpcr2 = TIMx->CR2;
  5201. /* Get the TIMx CCMR1 register value */
  5202. tmpccmrx = TIMx->CCMR1;
  5203. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5204. tmpccmrx &= ~TIM_CCMR1_OC2M;
  5205. tmpccmrx &= ~TIM_CCMR1_CC2S;
  5206. /* Select the Output Compare Mode */
  5207. tmpccmrx |= (OC_Config->OCMode << 8U);
  5208. /* Reset the Output Polarity level */
  5209. tmpccer &= ~TIM_CCER_CC2P;
  5210. /* Set the Output Compare Polarity */
  5211. tmpccer |= (OC_Config->OCPolarity << 4U);
  5212. if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_2))
  5213. {
  5214. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  5215. /* Reset the Output N Polarity level */
  5216. tmpccer &= ~TIM_CCER_CC2NP;
  5217. /* Set the Output N Polarity */
  5218. tmpccer |= (OC_Config->OCNPolarity << 4U);
  5219. /* Reset the Output N State */
  5220. tmpccer &= ~TIM_CCER_CC2NE;
  5221. }
  5222. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5223. {
  5224. /* Check parameters */
  5225. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  5226. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  5227. /* Reset the Output Compare and Output Compare N IDLE State */
  5228. tmpcr2 &= ~TIM_CR2_OIS2;
  5229. tmpcr2 &= ~TIM_CR2_OIS2N;
  5230. /* Set the Output Idle state */
  5231. tmpcr2 |= (OC_Config->OCIdleState << 2U);
  5232. /* Set the Output N Idle state */
  5233. tmpcr2 |= (OC_Config->OCNIdleState << 2U);
  5234. }
  5235. /* Write to TIMx CR2 */
  5236. TIMx->CR2 = tmpcr2;
  5237. /* Write to TIMx CCMR1 */
  5238. TIMx->CCMR1 = tmpccmrx;
  5239. /* Set the Capture Compare Register value */
  5240. TIMx->CCR2 = OC_Config->Pulse;
  5241. /* Write to TIMx CCER */
  5242. TIMx->CCER = tmpccer;
  5243. }
  5244. /**
  5245. * @brief Timer Output Compare 3 configuration
  5246. * @param TIMx to select the TIM peripheral
  5247. * @param OC_Config The ouput configuration structure
  5248. * @retval None
  5249. */
  5250. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  5251. {
  5252. uint32_t tmpccmrx;
  5253. uint32_t tmpccer;
  5254. uint32_t tmpcr2;
  5255. /* Disable the Channel 3: Reset the CC2E Bit */
  5256. TIMx->CCER &= ~TIM_CCER_CC3E;
  5257. /* Get the TIMx CCER register value */
  5258. tmpccer = TIMx->CCER;
  5259. /* Get the TIMx CR2 register value */
  5260. tmpcr2 = TIMx->CR2;
  5261. /* Get the TIMx CCMR2 register value */
  5262. tmpccmrx = TIMx->CCMR2;
  5263. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5264. tmpccmrx &= ~TIM_CCMR2_OC3M;
  5265. tmpccmrx &= ~TIM_CCMR2_CC3S;
  5266. /* Select the Output Compare Mode */
  5267. tmpccmrx |= OC_Config->OCMode;
  5268. /* Reset the Output Polarity level */
  5269. tmpccer &= ~TIM_CCER_CC3P;
  5270. /* Set the Output Compare Polarity */
  5271. tmpccer |= (OC_Config->OCPolarity << 8U);
  5272. if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_3))
  5273. {
  5274. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  5275. /* Reset the Output N Polarity level */
  5276. tmpccer &= ~TIM_CCER_CC3NP;
  5277. /* Set the Output N Polarity */
  5278. tmpccer |= (OC_Config->OCNPolarity << 8U);
  5279. /* Reset the Output N State */
  5280. tmpccer &= ~TIM_CCER_CC3NE;
  5281. }
  5282. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5283. {
  5284. /* Check parameters */
  5285. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  5286. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  5287. /* Reset the Output Compare and Output Compare N IDLE State */
  5288. tmpcr2 &= ~TIM_CR2_OIS3;
  5289. tmpcr2 &= ~TIM_CR2_OIS3N;
  5290. /* Set the Output Idle state */
  5291. tmpcr2 |= (OC_Config->OCIdleState << 4U);
  5292. /* Set the Output N Idle state */
  5293. tmpcr2 |= (OC_Config->OCNIdleState << 4U);
  5294. }
  5295. /* Write to TIMx CR2 */
  5296. TIMx->CR2 = tmpcr2;
  5297. /* Write to TIMx CCMR2 */
  5298. TIMx->CCMR2 = tmpccmrx;
  5299. /* Set the Capture Compare Register value */
  5300. TIMx->CCR3 = OC_Config->Pulse;
  5301. /* Write to TIMx CCER */
  5302. TIMx->CCER = tmpccer;
  5303. }
  5304. /**
  5305. * @brief Timer Output Compare 4 configuration
  5306. * @param TIMx to select the TIM peripheral
  5307. * @param OC_Config The ouput configuration structure
  5308. * @retval None
  5309. */
  5310. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  5311. {
  5312. uint32_t tmpccmrx;
  5313. uint32_t tmpccer;
  5314. uint32_t tmpcr2;
  5315. /* Disable the Channel 4: Reset the CC4E Bit */
  5316. TIMx->CCER &= ~TIM_CCER_CC4E;
  5317. /* Get the TIMx CCER register value */
  5318. tmpccer = TIMx->CCER;
  5319. /* Get the TIMx CR2 register value */
  5320. tmpcr2 = TIMx->CR2;
  5321. /* Get the TIMx CCMR2 register value */
  5322. tmpccmrx = TIMx->CCMR2;
  5323. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5324. tmpccmrx &= ~TIM_CCMR2_OC4M;
  5325. tmpccmrx &= ~TIM_CCMR2_CC4S;
  5326. /* Select the Output Compare Mode */
  5327. tmpccmrx |= (OC_Config->OCMode << 8U);
  5328. /* Reset the Output Polarity level */
  5329. tmpccer &= ~TIM_CCER_CC4P;
  5330. /* Set the Output Compare Polarity */
  5331. tmpccer |= (OC_Config->OCPolarity << 12U);
  5332. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5333. {
  5334. /* Check parameters */
  5335. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  5336. /* Reset the Output Compare IDLE State */
  5337. tmpcr2 &= ~TIM_CR2_OIS4;
  5338. /* Set the Output Idle state */
  5339. tmpcr2 |= (OC_Config->OCIdleState << 6U);
  5340. }
  5341. /* Write to TIMx CR2 */
  5342. TIMx->CR2 = tmpcr2;
  5343. /* Write to TIMx CCMR2 */
  5344. TIMx->CCMR2 = tmpccmrx;
  5345. /* Set the Capture Compare Register value */
  5346. TIMx->CCR4 = OC_Config->Pulse;
  5347. /* Write to TIMx CCER */
  5348. TIMx->CCER = tmpccer;
  5349. }
  5350. /**
  5351. * @brief Timer Output Compare 5 configuration
  5352. * @param TIMx to select the TIM peripheral
  5353. * @param OC_Config The ouput configuration structure
  5354. * @retval None
  5355. */
  5356. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx,
  5357. TIM_OC_InitTypeDef *OC_Config)
  5358. {
  5359. uint32_t tmpccmrx;
  5360. uint32_t tmpccer;
  5361. uint32_t tmpcr2;
  5362. /* Disable the output: Reset the CCxE Bit */
  5363. TIMx->CCER &= ~TIM_CCER_CC5E;
  5364. /* Get the TIMx CCER register value */
  5365. tmpccer = TIMx->CCER;
  5366. /* Get the TIMx CR2 register value */
  5367. tmpcr2 = TIMx->CR2;
  5368. /* Get the TIMx CCMR1 register value */
  5369. tmpccmrx = TIMx->CCMR3;
  5370. /* Reset the Output Compare Mode Bits */
  5371. tmpccmrx &= ~(TIM_CCMR3_OC5M);
  5372. /* Select the Output Compare Mode */
  5373. tmpccmrx |= OC_Config->OCMode;
  5374. /* Reset the Output Polarity level */
  5375. tmpccer &= ~TIM_CCER_CC5P;
  5376. /* Set the Output Compare Polarity */
  5377. tmpccer |= (OC_Config->OCPolarity << 16U);
  5378. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5379. {
  5380. /* Reset the Output Compare IDLE State */
  5381. tmpcr2 &= ~TIM_CR2_OIS5;
  5382. /* Set the Output Idle state */
  5383. tmpcr2 |= (OC_Config->OCIdleState << 8U);
  5384. }
  5385. /* Write to TIMx CR2 */
  5386. TIMx->CR2 = tmpcr2;
  5387. /* Write to TIMx CCMR3 */
  5388. TIMx->CCMR3 = tmpccmrx;
  5389. /* Set the Capture Compare Register value */
  5390. TIMx->CCR5 = OC_Config->Pulse;
  5391. /* Write to TIMx CCER */
  5392. TIMx->CCER = tmpccer;
  5393. }
  5394. /**
  5395. * @brief Timer Output Compare 6 configuration
  5396. * @param TIMx to select the TIM peripheral
  5397. * @param OC_Config The ouput configuration structure
  5398. * @retval None
  5399. */
  5400. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx,
  5401. TIM_OC_InitTypeDef *OC_Config)
  5402. {
  5403. uint32_t tmpccmrx;
  5404. uint32_t tmpccer;
  5405. uint32_t tmpcr2;
  5406. /* Disable the output: Reset the CCxE Bit */
  5407. TIMx->CCER &= ~TIM_CCER_CC6E;
  5408. /* Get the TIMx CCER register value */
  5409. tmpccer = TIMx->CCER;
  5410. /* Get the TIMx CR2 register value */
  5411. tmpcr2 = TIMx->CR2;
  5412. /* Get the TIMx CCMR1 register value */
  5413. tmpccmrx = TIMx->CCMR3;
  5414. /* Reset the Output Compare Mode Bits */
  5415. tmpccmrx &= ~(TIM_CCMR3_OC6M);
  5416. /* Select the Output Compare Mode */
  5417. tmpccmrx |= (OC_Config->OCMode << 8U);
  5418. /* Reset the Output Polarity level */
  5419. tmpccer &= (uint32_t)~TIM_CCER_CC6P;
  5420. /* Set the Output Compare Polarity */
  5421. tmpccer |= (OC_Config->OCPolarity << 20U);
  5422. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5423. {
  5424. /* Reset the Output Compare IDLE State */
  5425. tmpcr2 &= ~TIM_CR2_OIS6;
  5426. /* Set the Output Idle state */
  5427. tmpcr2 |= (OC_Config->OCIdleState << 10U);
  5428. }
  5429. /* Write to TIMx CR2 */
  5430. TIMx->CR2 = tmpcr2;
  5431. /* Write to TIMx CCMR3 */
  5432. TIMx->CCMR3 = tmpccmrx;
  5433. /* Set the Capture Compare Register value */
  5434. TIMx->CCR6 = OC_Config->Pulse;
  5435. /* Write to TIMx CCER */
  5436. TIMx->CCER = tmpccer;
  5437. }
  5438. /**
  5439. * @brief Slave Timer configuration function
  5440. * @param htim TIM handle
  5441. * @param sSlaveConfig Slave timer configuration
  5442. * @retval None
  5443. */
  5444. static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  5445. TIM_SlaveConfigTypeDef *sSlaveConfig)
  5446. {
  5447. uint32_t tmpsmcr;
  5448. uint32_t tmpccmr1;
  5449. uint32_t tmpccer;
  5450. /* Get the TIMx SMCR register value */
  5451. tmpsmcr = htim->Instance->SMCR;
  5452. /* Reset the Trigger Selection Bits */
  5453. tmpsmcr &= ~TIM_SMCR_TS;
  5454. /* Set the Input Trigger source */
  5455. tmpsmcr |= sSlaveConfig->InputTrigger;
  5456. /* Reset the slave mode Bits */
  5457. tmpsmcr &= ~TIM_SMCR_SMS;
  5458. /* Set the slave mode */
  5459. tmpsmcr |= sSlaveConfig->SlaveMode;
  5460. /* Write to TIMx SMCR */
  5461. htim->Instance->SMCR = tmpsmcr;
  5462. /* Configure the trigger prescaler, filter, and polarity */
  5463. switch (sSlaveConfig->InputTrigger)
  5464. {
  5465. case TIM_TS_ETRF:
  5466. {
  5467. /* Check the parameters */
  5468. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  5469. assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
  5470. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5471. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5472. /* Configure the ETR Trigger source */
  5473. TIM_ETR_SetConfig(htim->Instance,
  5474. sSlaveConfig->TriggerPrescaler,
  5475. sSlaveConfig->TriggerPolarity,
  5476. sSlaveConfig->TriggerFilter);
  5477. break;
  5478. }
  5479. case TIM_TS_TI1F_ED:
  5480. {
  5481. /* Check the parameters */
  5482. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  5483. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5484. if(sSlaveConfig->SlaveMode == TIM_SLAVEMODE_GATED)
  5485. {
  5486. return HAL_ERROR;
  5487. }
  5488. /* Disable the Channel 1: Reset the CC1E Bit */
  5489. tmpccer = htim->Instance->CCER;
  5490. htim->Instance->CCER &= ~TIM_CCER_CC1E;
  5491. tmpccmr1 = htim->Instance->CCMR1;
  5492. /* Set the filter */
  5493. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5494. tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4U);
  5495. /* Write to TIMx CCMR1 and CCER registers */
  5496. htim->Instance->CCMR1 = tmpccmr1;
  5497. htim->Instance->CCER = tmpccer;
  5498. break;
  5499. }
  5500. case TIM_TS_TI1FP1:
  5501. {
  5502. /* Check the parameters */
  5503. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  5504. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5505. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5506. /* Configure TI1 Filter and Polarity */
  5507. TIM_TI1_ConfigInputStage(htim->Instance,
  5508. sSlaveConfig->TriggerPolarity,
  5509. sSlaveConfig->TriggerFilter);
  5510. break;
  5511. }
  5512. case TIM_TS_TI2FP2:
  5513. {
  5514. /* Check the parameters */
  5515. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  5516. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5517. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5518. /* Configure TI2 Filter and Polarity */
  5519. TIM_TI2_ConfigInputStage(htim->Instance,
  5520. sSlaveConfig->TriggerPolarity,
  5521. sSlaveConfig->TriggerFilter);
  5522. break;
  5523. }
  5524. case TIM_TS_ITR0:
  5525. case TIM_TS_ITR1:
  5526. case TIM_TS_ITR2:
  5527. case TIM_TS_ITR3:
  5528. {
  5529. /* Check the parameter */
  5530. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  5531. break;
  5532. }
  5533. default:
  5534. break;
  5535. }
  5536. return HAL_OK;
  5537. }
  5538. /**
  5539. * @brief Configure the TI1 as Input.
  5540. * @param TIMx to select the TIM peripheral.
  5541. * @param TIM_ICPolarity The Input Polarity.
  5542. * This parameter can be one of the following values:
  5543. * @arg TIM_ICPOLARITY_RISING
  5544. * @arg TIM_ICPOLARITY_FALLING
  5545. * @arg TIM_ICPOLARITY_BOTHEDGE
  5546. * @param TIM_ICSelection specifies the input to be used.
  5547. * This parameter can be one of the following values:
  5548. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 1 is selected to be connected to IC1.
  5549. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 1 is selected to be connected to IC2.
  5550. * @arg TIM_ICSELECTION_TRC: TIM Input 1 is selected to be connected to TRC.
  5551. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5552. * This parameter must be a value between 0x00 and 0x0F.
  5553. * @retval None
  5554. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
  5555. * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
  5556. * protected against un-initialized filter and polarity values.
  5557. */
  5558. void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5559. uint32_t TIM_ICFilter)
  5560. {
  5561. uint32_t tmpccmr1;
  5562. uint32_t tmpccer;
  5563. /* Disable the Channel 1: Reset the CC1E Bit */
  5564. TIMx->CCER &= ~TIM_CCER_CC1E;
  5565. tmpccmr1 = TIMx->CCMR1;
  5566. tmpccer = TIMx->CCER;
  5567. /* Select the Input */
  5568. if (IS_TIM_CC2_INSTANCE(TIMx) != RESET)
  5569. {
  5570. tmpccmr1 &= ~TIM_CCMR1_CC1S;
  5571. tmpccmr1 |= TIM_ICSelection;
  5572. }
  5573. else
  5574. {
  5575. tmpccmr1 |= TIM_CCMR1_CC1S_0;
  5576. }
  5577. /* Set the filter */
  5578. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5579. tmpccmr1 |= ((TIM_ICFilter << 4U) & TIM_CCMR1_IC1F);
  5580. /* Select the Polarity and set the CC1E Bit */
  5581. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  5582. tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
  5583. /* Write to TIMx CCMR1 and CCER registers */
  5584. TIMx->CCMR1 = tmpccmr1;
  5585. TIMx->CCER = tmpccer;
  5586. }
  5587. /**
  5588. * @brief Configure the Polarity and Filter for TI1.
  5589. * @param TIMx to select the TIM peripheral.
  5590. * @param TIM_ICPolarity The Input Polarity.
  5591. * This parameter can be one of the following values:
  5592. * @arg TIM_ICPOLARITY_RISING
  5593. * @arg TIM_ICPOLARITY_FALLING
  5594. * @arg TIM_ICPOLARITY_BOTHEDGE
  5595. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5596. * This parameter must be a value between 0x00 and 0x0F.
  5597. * @retval None
  5598. */
  5599. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  5600. {
  5601. uint32_t tmpccmr1;
  5602. uint32_t tmpccer;
  5603. /* Disable the Channel 1: Reset the CC1E Bit */
  5604. tmpccer = TIMx->CCER;
  5605. TIMx->CCER &= ~TIM_CCER_CC1E;
  5606. tmpccmr1 = TIMx->CCMR1;
  5607. /* Set the filter */
  5608. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5609. tmpccmr1 |= (TIM_ICFilter << 4U);
  5610. /* Select the Polarity and set the CC1E Bit */
  5611. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  5612. tmpccer |= TIM_ICPolarity;
  5613. /* Write to TIMx CCMR1 and CCER registers */
  5614. TIMx->CCMR1 = tmpccmr1;
  5615. TIMx->CCER = tmpccer;
  5616. }
  5617. /**
  5618. * @brief Configure the TI2 as Input.
  5619. * @param TIMx to select the TIM peripheral
  5620. * @param TIM_ICPolarity The Input Polarity.
  5621. * This parameter can be one of the following values:
  5622. * @arg TIM_ICPOLARITY_RISING
  5623. * @arg TIM_ICPOLARITY_FALLING
  5624. * @arg TIM_ICPOLARITY_BOTHEDGE
  5625. * @param TIM_ICSelection specifies the input to be used.
  5626. * This parameter can be one of the following values:
  5627. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 2 is selected to be connected to IC2.
  5628. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 2 is selected to be connected to IC1.
  5629. * @arg TIM_ICSELECTION_TRC: TIM Input 2 is selected to be connected to TRC.
  5630. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5631. * This parameter must be a value between 0x00 and 0x0F.
  5632. * @retval None
  5633. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
  5634. * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
  5635. * protected against un-initialized filter and polarity values.
  5636. */
  5637. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5638. uint32_t TIM_ICFilter)
  5639. {
  5640. uint32_t tmpccmr1;
  5641. uint32_t tmpccer;
  5642. /* Disable the Channel 2: Reset the CC2E Bit */
  5643. TIMx->CCER &= ~TIM_CCER_CC2E;
  5644. tmpccmr1 = TIMx->CCMR1;
  5645. tmpccer = TIMx->CCER;
  5646. /* Select the Input */
  5647. tmpccmr1 &= ~TIM_CCMR1_CC2S;
  5648. tmpccmr1 |= (TIM_ICSelection << 8U);
  5649. /* Set the filter */
  5650. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  5651. tmpccmr1 |= ((TIM_ICFilter << 12U) & TIM_CCMR1_IC2F);
  5652. /* Select the Polarity and set the CC2E Bit */
  5653. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  5654. tmpccer |= ((TIM_ICPolarity << 4U) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
  5655. /* Write to TIMx CCMR1 and CCER registers */
  5656. TIMx->CCMR1 = tmpccmr1 ;
  5657. TIMx->CCER = tmpccer;
  5658. }
  5659. /**
  5660. * @brief Configure the Polarity and Filter for TI2.
  5661. * @param TIMx to select the TIM peripheral.
  5662. * @param TIM_ICPolarity The Input Polarity.
  5663. * This parameter can be one of the following values:
  5664. * @arg TIM_ICPOLARITY_RISING
  5665. * @arg TIM_ICPOLARITY_FALLING
  5666. * @arg TIM_ICPOLARITY_BOTHEDGE
  5667. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5668. * This parameter must be a value between 0x00 and 0x0F.
  5669. * @retval None
  5670. */
  5671. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  5672. {
  5673. uint32_t tmpccmr1;
  5674. uint32_t tmpccer;
  5675. /* Disable the Channel 2: Reset the CC2E Bit */
  5676. TIMx->CCER &= ~TIM_CCER_CC2E;
  5677. tmpccmr1 = TIMx->CCMR1;
  5678. tmpccer = TIMx->CCER;
  5679. /* Set the filter */
  5680. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  5681. tmpccmr1 |= (TIM_ICFilter << 12U);
  5682. /* Select the Polarity and set the CC2E Bit */
  5683. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  5684. tmpccer |= (TIM_ICPolarity << 4U);
  5685. /* Write to TIMx CCMR1 and CCER registers */
  5686. TIMx->CCMR1 = tmpccmr1 ;
  5687. TIMx->CCER = tmpccer;
  5688. }
  5689. /**
  5690. * @brief Configure the TI3 as Input.
  5691. * @param TIMx to select the TIM peripheral
  5692. * @param TIM_ICPolarity The Input Polarity.
  5693. * This parameter can be one of the following values:
  5694. * @arg TIM_ICPOLARITY_RISING
  5695. * @arg TIM_ICPOLARITY_FALLING
  5696. * @arg TIM_ICPOLARITY_BOTHEDGE
  5697. * @param TIM_ICSelection specifies the input to be used.
  5698. * This parameter can be one of the following values:
  5699. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 3 is selected to be connected to IC3.
  5700. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 3 is selected to be connected to IC4.
  5701. * @arg TIM_ICSELECTION_TRC: TIM Input 3 is selected to be connected to TRC.
  5702. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5703. * This parameter must be a value between 0x00 and 0x0F.
  5704. * @retval None
  5705. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
  5706. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  5707. * protected against un-initialized filter and polarity values.
  5708. */
  5709. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5710. uint32_t TIM_ICFilter)
  5711. {
  5712. uint32_t tmpccmr2;
  5713. uint32_t tmpccer;
  5714. /* Disable the Channel 3: Reset the CC3E Bit */
  5715. TIMx->CCER &= ~TIM_CCER_CC3E;
  5716. tmpccmr2 = TIMx->CCMR2;
  5717. tmpccer = TIMx->CCER;
  5718. /* Select the Input */
  5719. tmpccmr2 &= ~TIM_CCMR2_CC3S;
  5720. tmpccmr2 |= TIM_ICSelection;
  5721. /* Set the filter */
  5722. tmpccmr2 &= ~TIM_CCMR2_IC3F;
  5723. tmpccmr2 |= ((TIM_ICFilter << 4U) & TIM_CCMR2_IC3F);
  5724. /* Select the Polarity and set the CC3E Bit */
  5725. tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
  5726. tmpccer |= ((TIM_ICPolarity << 8U) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
  5727. /* Write to TIMx CCMR2 and CCER registers */
  5728. TIMx->CCMR2 = tmpccmr2;
  5729. TIMx->CCER = tmpccer;
  5730. }
  5731. /**
  5732. * @brief Configure the TI4 as Input.
  5733. * @param TIMx to select the TIM peripheral
  5734. * @param TIM_ICPolarity The Input Polarity.
  5735. * This parameter can be one of the following values:
  5736. * @arg TIM_ICPOLARITY_RISING
  5737. * @arg TIM_ICPOLARITY_FALLING
  5738. * @arg TIM_ICPOLARITY_BOTHEDGE
  5739. * @param TIM_ICSelection specifies the input to be used.
  5740. * This parameter can be one of the following values:
  5741. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 4 is selected to be connected to IC4.
  5742. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 4 is selected to be connected to IC3.
  5743. * @arg TIM_ICSELECTION_TRC: TIM Input 4 is selected to be connected to TRC.
  5744. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5745. * This parameter must be a value between 0x00 and 0x0F.
  5746. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
  5747. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  5748. * protected against un-initialized filter and polarity values.
  5749. * @retval None
  5750. */
  5751. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5752. uint32_t TIM_ICFilter)
  5753. {
  5754. uint32_t tmpccmr2;
  5755. uint32_t tmpccer;
  5756. /* Disable the Channel 4: Reset the CC4E Bit */
  5757. TIMx->CCER &= ~TIM_CCER_CC4E;
  5758. tmpccmr2 = TIMx->CCMR2;
  5759. tmpccer = TIMx->CCER;
  5760. /* Select the Input */
  5761. tmpccmr2 &= ~TIM_CCMR2_CC4S;
  5762. tmpccmr2 |= (TIM_ICSelection << 8U);
  5763. /* Set the filter */
  5764. tmpccmr2 &= ~TIM_CCMR2_IC4F;
  5765. tmpccmr2 |= ((TIM_ICFilter << 12U) & TIM_CCMR2_IC4F);
  5766. /* Select the Polarity and set the CC4E Bit */
  5767. tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
  5768. tmpccer |= ((TIM_ICPolarity << 12U) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
  5769. /* Write to TIMx CCMR2 and CCER registers */
  5770. TIMx->CCMR2 = tmpccmr2;
  5771. TIMx->CCER = tmpccer ;
  5772. }
  5773. /**
  5774. * @brief Selects the Input Trigger source
  5775. * @param TIMx to select the TIM peripheral
  5776. * @param InputTriggerSource The Input Trigger source.
  5777. * This parameter can be one of the following values:
  5778. * @arg TIM_TS_ITR0: Internal Trigger 0
  5779. * @arg TIM_TS_ITR1: Internal Trigger 1
  5780. * @arg TIM_TS_ITR2: Internal Trigger 2
  5781. * @arg TIM_TS_ITR3: Internal Trigger 3
  5782. * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  5783. * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  5784. * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  5785. * @arg TIM_TS_ETRF: External Trigger input
  5786. * @retval None
  5787. */
  5788. static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource)
  5789. {
  5790. uint32_t tmpsmcr;
  5791. /* Get the TIMx SMCR register value */
  5792. tmpsmcr = TIMx->SMCR;
  5793. /* Reset the TS Bits */
  5794. tmpsmcr &= ~TIM_SMCR_TS;
  5795. /* Set the Input Trigger source and the slave mode*/
  5796. tmpsmcr |= (InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1);
  5797. /* Write to TIMx SMCR */
  5798. TIMx->SMCR = tmpsmcr;
  5799. }
  5800. /**
  5801. * @brief Configures the TIMx External Trigger (ETR).
  5802. * @param TIMx to select the TIM peripheral
  5803. * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
  5804. * This parameter can be one of the following values:
  5805. * @arg TIM_ETRPRESCALER_DIV1: ETRP Prescaler OFF.
  5806. * @arg TIM_ETRPRESCALER_DIV2: ETRP frequency divided by 2.
  5807. * @arg TIM_ETRPRESCALER_DIV4: ETRP frequency divided by 4.
  5808. * @arg TIM_ETRPRESCALER_DIV8: ETRP frequency divided by 8.
  5809. * @param TIM_ExtTRGPolarity The external Trigger Polarity.
  5810. * This parameter can be one of the following values:
  5811. * @arg TIM_ETRPOLARITY_INVERTED: active low or falling edge active.
  5812. * @arg TIM_ETRPOLARITY_NONINVERTED: active high or rising edge active.
  5813. * @param ExtTRGFilter External Trigger Filter.
  5814. * This parameter must be a value between 0x00 and 0x0F
  5815. * @retval None
  5816. */
  5817. void TIM_ETR_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler,
  5818. uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
  5819. {
  5820. uint32_t tmpsmcr;
  5821. tmpsmcr = TIMx->SMCR;
  5822. /* Reset the ETR Bits */
  5823. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  5824. /* Set the Prescaler, the Filter value and the Polarity */
  5825. tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8U)));
  5826. /* Write to TIMx SMCR */
  5827. TIMx->SMCR = tmpsmcr;
  5828. }
  5829. /**
  5830. * @brief Enables or disables the TIM Capture Compare Channel x.
  5831. * @param TIMx to select the TIM peripheral
  5832. * @param Channel specifies the TIM Channel
  5833. * This parameter can be one of the following values:
  5834. * @arg TIM_CHANNEL_1: TIM Channel 1
  5835. * @arg TIM_CHANNEL_2: TIM Channel 2
  5836. * @arg TIM_CHANNEL_3: TIM Channel 3
  5837. * @arg TIM_CHANNEL_4: TIM Channel 4
  5838. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  5839. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  5840. * @param ChannelState specifies the TIM Channel CCxE bit new state.
  5841. * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
  5842. * @retval None
  5843. */
  5844. void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
  5845. {
  5846. uint32_t tmp;
  5847. /* Check the parameters */
  5848. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  5849. assert_param(IS_TIM_CHANNELS(Channel));
  5850. tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
  5851. /* Reset the CCxE Bit */
  5852. TIMx->CCER &= ~tmp;
  5853. /* Set or reset the CCxE Bit */
  5854. TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
  5855. }
  5856. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5857. /**
  5858. * @brief Reset interrupt callbacks to the legacy weak callbacks.
  5859. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  5860. * the configuration information for TIM module.
  5861. * @retval None
  5862. */
  5863. void TIM_ResetCallback(TIM_HandleTypeDef *htim)
  5864. {
  5865. /* Reset the TIM callback to the legacy weak callbacks */
  5866. htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback; /* Legacy weak PeriodElapsedCallback */
  5867. htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback; /* Legacy weak PeriodElapsedHalfCpltCallback */
  5868. htim->TriggerCallback = HAL_TIM_TriggerCallback; /* Legacy weak TriggerCallback */
  5869. htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback; /* Legacy weak TriggerHalfCpltCallback */
  5870. htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback; /* Legacy weak IC_CaptureCallback */
  5871. htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback; /* Legacy weak IC_CaptureHalfCpltCallback */
  5872. htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback; /* Legacy weak OC_DelayElapsedCallback */
  5873. htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback; /* Legacy weak PWM_PulseFinishedCallback */
  5874. htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback; /* Legacy weak PWM_PulseFinishedHalfCpltCallback */
  5875. htim->ErrorCallback = HAL_TIM_ErrorCallback; /* Legacy weak ErrorCallback */
  5876. htim->CommutationCallback = HAL_TIMEx_CommutCallback; /* Legacy weak CommutationCallback */
  5877. htim->CommutationHalfCpltCallback = HAL_TIMEx_CommutHalfCpltCallback; /* Legacy weak CommutationHalfCpltCallback */
  5878. htim->BreakCallback = HAL_TIMEx_BreakCallback; /* Legacy weak BreakCallback */
  5879. htim->Break2Callback = HAL_TIMEx_Break2Callback; /* Legacy weak Break2Callback */
  5880. }
  5881. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5882. /**
  5883. * @}
  5884. */
  5885. #endif /* HAL_TIM_MODULE_ENABLED */
  5886. /**
  5887. * @}
  5888. */
  5889. /**
  5890. * @}
  5891. */
  5892. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/