stm32f1xx_hal_tim.c 161 KB

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