py32f0xx_hal_tim.c 209 KB

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