stm32f1xx_hal_tim.c 235 KB

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