You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_tim.c 195KB

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