stm32l0xx_hal_tim.c 152 KB

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