stm32f4xx_hal_tim.c 170 KB

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