stm32f3xx_hal_tim.c 173 KB

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