stm32f1xx_hal_tim.c 238 KB

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