stm32f4xx_tim.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_tim.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the TIM peripheral:
  9. * + TimeBase management
  10. * + Output Compare management
  11. * + Input Capture management
  12. * + Advanced-control timers (TIM1 and TIM8) specific features
  13. * + Interrupts, DMA and flags management
  14. * + Clocks management
  15. * + Synchronization management
  16. * + Specific interface management
  17. * + Specific remapping management
  18. *
  19. @verbatim
  20. ===============================================================================
  21. ##### How to use this driver #####
  22. ===============================================================================
  23. [..]
  24. This driver provides functions to configure and program the TIM
  25. of all STM32F4xx devices.
  26. These functions are split in 9 groups:
  27. (#) TIM TimeBase management: this group includes all needed functions
  28. to configure the TM Timebase unit:
  29. (++) Set/Get Prescaler
  30. (++) Set/Get Autoreload
  31. (++) Counter modes configuration
  32. (++) Set Clock division
  33. (++) Select the One Pulse mode
  34. (++) Update Request Configuration
  35. (++) Update Disable Configuration
  36. (++) Auto-Preload Configuration
  37. (++) Enable/Disable the counter
  38. (#) TIM Output Compare management: this group includes all needed
  39. functions to configure the Capture/Compare unit used in Output
  40. compare mode:
  41. (++) Configure each channel, independently, in Output Compare mode
  42. (++) Select the output compare modes
  43. (++) Select the Polarities of each channel
  44. (++) Set/Get the Capture/Compare register values
  45. (++) Select the Output Compare Fast mode
  46. (++) Select the Output Compare Forced mode
  47. (++) Output Compare-Preload Configuration
  48. (++) Clear Output Compare Reference
  49. (++) Select the OCREF Clear signal
  50. (++) Enable/Disable the Capture/Compare Channels
  51. (#) TIM Input Capture management: this group includes all needed
  52. functions to configure the Capture/Compare unit used in
  53. Input Capture mode:
  54. (++) Configure each channel in input capture mode
  55. (++) Configure Channel1/2 in PWM Input mode
  56. (++) Set the Input Capture Prescaler
  57. (++) Get the Capture/Compare values
  58. (#) Advanced-control timers (TIM1 and TIM8) specific features
  59. (++) Configures the Break input, dead time, Lock level, the OSSI,
  60. the OSSR State and the AOE(automatic output enable)
  61. (++) Enable/Disable the TIM peripheral Main Outputs
  62. (++) Select the Commutation event
  63. (++) Set/Reset the Capture Compare Preload Control bit
  64. (#) TIM interrupts, DMA and flags management
  65. (++) Enable/Disable interrupt sources
  66. (++) Get flags status
  67. (++) Clear flags/ Pending bits
  68. (++) Enable/Disable DMA requests
  69. (++) Configure DMA burst mode
  70. (++) Select CaptureCompare DMA request
  71. (#) TIM clocks management: this group includes all needed functions
  72. to configure the clock controller unit:
  73. (++) Select internal/External clock
  74. (++) Select the external clock mode: ETR(Mode1/Mode2), TIx or ITRx
  75. (#) TIM synchronization management: this group includes all needed
  76. functions to configure the Synchronization unit:
  77. (++) Select Input Trigger
  78. (++) Select Output Trigger
  79. (++) Select Master Slave Mode
  80. (++) ETR Configuration when used as external trigger
  81. (#) TIM specific interface management, this group includes all
  82. needed functions to use the specific TIM interface:
  83. (++) Encoder Interface Configuration
  84. (++) Select Hall Sensor
  85. (#) TIM specific remapping management includes the Remapping
  86. configuration of specific timers
  87. @endverbatim
  88. ******************************************************************************
  89. * @attention
  90. *
  91. * Copyright (c) 2016 STMicroelectronics.
  92. * All rights reserved.
  93. *
  94. * This software is licensed under terms that can be found in the LICENSE file
  95. * in the root directory of this software component.
  96. * If no LICENSE file comes with this software, it is provided AS-IS.
  97. *
  98. ******************************************************************************
  99. */
  100. /* Includes ------------------------------------------------------------------*/
  101. #include "stm32f4xx_tim.h"
  102. #include "stm32f4xx_rcc.h"
  103. /** @addtogroup STM32F4xx_StdPeriph_Driver
  104. * @{
  105. */
  106. /** @defgroup TIM
  107. * @brief TIM driver modules
  108. * @{
  109. */
  110. /* Private typedef -----------------------------------------------------------*/
  111. /* Private define ------------------------------------------------------------*/
  112. /* ---------------------- TIM registers bit mask ------------------------ */
  113. #define SMCR_ETR_MASK ((uint16_t)0x00FF)
  114. #define CCMR_OFFSET ((uint16_t)0x0018)
  115. #define CCER_CCE_SET ((uint16_t)0x0001)
  116. #define CCER_CCNE_SET ((uint16_t)0x0004)
  117. #define CCMR_OC13M_MASK ((uint16_t)0xFF8F)
  118. #define CCMR_OC24M_MASK ((uint16_t)0x8FFF)
  119. /* Private macro -------------------------------------------------------------*/
  120. /* Private variables ---------------------------------------------------------*/
  121. /* Private function prototypes -----------------------------------------------*/
  122. static void TI1_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  123. uint16_t TIM_ICFilter);
  124. static void TI2_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  125. uint16_t TIM_ICFilter);
  126. static void TI3_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  127. uint16_t TIM_ICFilter);
  128. static void TI4_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  129. uint16_t TIM_ICFilter);
  130. /* Private functions ---------------------------------------------------------*/
  131. /** @defgroup TIM_Private_Functions
  132. * @{
  133. */
  134. /** @defgroup TIM_Group1 TimeBase management functions
  135. * @brief TimeBase management functions
  136. *
  137. @verbatim
  138. ===============================================================================
  139. ##### TimeBase management functions #####
  140. ===============================================================================
  141. ##### TIM Driver: how to use it in Timing(Time base) Mode #####
  142. ===============================================================================
  143. [..]
  144. To use the Timer in Timing(Time base) mode, the following steps are mandatory:
  145. (#) Enable TIM clock using RCC_APBxPeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE) function
  146. (#) Fill the TIM_TimeBaseInitStruct with the desired parameters.
  147. (#) Call TIM_TimeBaseInit(TIMx, &TIM_TimeBaseInitStruct) to configure the Time Base unit
  148. with the corresponding configuration
  149. (#) Enable the NVIC if you need to generate the update interrupt.
  150. (#) Enable the corresponding interrupt using the function TIM_ITConfig(TIMx, TIM_IT_Update)
  151. (#) Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
  152. -@- All other functions can be used separately to modify, if needed,
  153. a specific feature of the Timer.
  154. @endverbatim
  155. * @{
  156. */
  157. /**
  158. * @brief Deinitializes the TIMx peripheral registers to their default reset values.
  159. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  160. * @retval None
  161. */
  162. void TIM_DeInit(TIM_TypeDef* TIMx)
  163. {
  164. /* Check the parameters */
  165. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  166. if (TIMx == TIM1)
  167. {
  168. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, ENABLE);
  169. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, DISABLE);
  170. }
  171. else if (TIMx == TIM2)
  172. {
  173. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
  174. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, DISABLE);
  175. }
  176. else if (TIMx == TIM3)
  177. {
  178. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);
  179. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, DISABLE);
  180. }
  181. else if (TIMx == TIM4)
  182. {
  183. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);
  184. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, DISABLE);
  185. }
  186. else if (TIMx == TIM5)
  187. {
  188. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM5, ENABLE);
  189. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM5, DISABLE);
  190. }
  191. else if (TIMx == TIM6)
  192. {
  193. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM6, ENABLE);
  194. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM6, DISABLE);
  195. }
  196. else if (TIMx == TIM7)
  197. {
  198. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM7, ENABLE);
  199. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM7, DISABLE);
  200. }
  201. else if (TIMx == TIM8)
  202. {
  203. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM8, ENABLE);
  204. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM8, DISABLE);
  205. }
  206. else if (TIMx == TIM9)
  207. {
  208. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM9, ENABLE);
  209. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM9, DISABLE);
  210. }
  211. else if (TIMx == TIM10)
  212. {
  213. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM10, ENABLE);
  214. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM10, DISABLE);
  215. }
  216. else if (TIMx == TIM11)
  217. {
  218. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM11, ENABLE);
  219. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM11, DISABLE);
  220. }
  221. else if (TIMx == TIM12)
  222. {
  223. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM12, ENABLE);
  224. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM12, DISABLE);
  225. }
  226. else if (TIMx == TIM13)
  227. {
  228. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM13, ENABLE);
  229. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM13, DISABLE);
  230. }
  231. else
  232. {
  233. if (TIMx == TIM14)
  234. {
  235. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM14, ENABLE);
  236. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM14, DISABLE);
  237. }
  238. }
  239. }
  240. /**
  241. * @brief Initializes the TIMx Time Base Unit peripheral according to
  242. * the specified parameters in the TIM_TimeBaseInitStruct.
  243. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  244. * @param TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef structure
  245. * that contains the configuration information for the specified TIM peripheral.
  246. * @retval None
  247. */
  248. void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
  249. {
  250. uint16_t tmpcr1 = 0;
  251. /* Check the parameters */
  252. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  253. assert_param(IS_TIM_COUNTER_MODE(TIM_TimeBaseInitStruct->TIM_CounterMode));
  254. assert_param(IS_TIM_CKD_DIV(TIM_TimeBaseInitStruct->TIM_ClockDivision));
  255. tmpcr1 = TIMx->CR1;
  256. if((TIMx == TIM1) || (TIMx == TIM8)||
  257. (TIMx == TIM2) || (TIMx == TIM3)||
  258. (TIMx == TIM4) || (TIMx == TIM5))
  259. {
  260. /* Select the Counter Mode */
  261. tmpcr1 &= (uint16_t)(~(TIM_CR1_DIR | TIM_CR1_CMS));
  262. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_CounterMode;
  263. }
  264. if((TIMx != TIM6) && (TIMx != TIM7))
  265. {
  266. /* Set the clock division */
  267. tmpcr1 &= (uint16_t)(~TIM_CR1_CKD);
  268. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_ClockDivision;
  269. }
  270. TIMx->CR1 = tmpcr1;
  271. /* Set the Autoreload value */
  272. TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ;
  273. /* Set the Prescaler value */
  274. TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;
  275. if ((TIMx == TIM1) || (TIMx == TIM8))
  276. {
  277. /* Set the Repetition Counter value */
  278. TIMx->RCR = TIM_TimeBaseInitStruct->TIM_RepetitionCounter;
  279. }
  280. /* Generate an update event to reload the Prescaler
  281. and the repetition counter(only for TIM1 and TIM8) value immediately */
  282. TIMx->EGR = TIM_PSCReloadMode_Immediate;
  283. }
  284. /**
  285. * @brief Fills each TIM_TimeBaseInitStruct member with its default value.
  286. * @param TIM_TimeBaseInitStruct : pointer to a TIM_TimeBaseInitTypeDef
  287. * structure which will be initialized.
  288. * @retval None
  289. */
  290. void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
  291. {
  292. /* Set the default configuration */
  293. TIM_TimeBaseInitStruct->TIM_Period = 0xFFFFFFFF;
  294. TIM_TimeBaseInitStruct->TIM_Prescaler = 0x0000;
  295. TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;
  296. TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;
  297. TIM_TimeBaseInitStruct->TIM_RepetitionCounter = 0x0000;
  298. }
  299. /**
  300. * @brief Configures the TIMx Prescaler.
  301. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  302. * @param Prescaler: specifies the Prescaler Register value
  303. * @param TIM_PSCReloadMode: specifies the TIM Prescaler Reload mode
  304. * This parameter can be one of the following values:
  305. * @arg TIM_PSCReloadMode_Update: The Prescaler is loaded at the update event.
  306. * @arg TIM_PSCReloadMode_Immediate: The Prescaler is loaded immediately.
  307. * @retval None
  308. */
  309. void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode)
  310. {
  311. /* Check the parameters */
  312. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  313. assert_param(IS_TIM_PRESCALER_RELOAD(TIM_PSCReloadMode));
  314. /* Set the Prescaler value */
  315. TIMx->PSC = Prescaler;
  316. /* Set or reset the UG Bit */
  317. TIMx->EGR = TIM_PSCReloadMode;
  318. }
  319. /**
  320. * @brief Specifies the TIMx Counter Mode to be used.
  321. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  322. * @param TIM_CounterMode: specifies the Counter Mode to be used
  323. * This parameter can be one of the following values:
  324. * @arg TIM_CounterMode_Up: TIM Up Counting Mode
  325. * @arg TIM_CounterMode_Down: TIM Down Counting Mode
  326. * @arg TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1
  327. * @arg TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2
  328. * @arg TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3
  329. * @retval None
  330. */
  331. void TIM_CounterModeConfig(TIM_TypeDef* TIMx, uint16_t TIM_CounterMode)
  332. {
  333. uint16_t tmpcr1 = 0;
  334. /* Check the parameters */
  335. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  336. assert_param(IS_TIM_COUNTER_MODE(TIM_CounterMode));
  337. tmpcr1 = TIMx->CR1;
  338. /* Reset the CMS and DIR Bits */
  339. tmpcr1 &= (uint16_t)~(TIM_CR1_DIR | TIM_CR1_CMS);
  340. /* Set the Counter Mode */
  341. tmpcr1 |= TIM_CounterMode;
  342. /* Write to TIMx CR1 register */
  343. TIMx->CR1 = tmpcr1;
  344. }
  345. /**
  346. * @brief Sets the TIMx Counter Register value
  347. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  348. * @param Counter: specifies the Counter register new value.
  349. * @retval None
  350. */
  351. void TIM_SetCounter(TIM_TypeDef* TIMx, uint32_t Counter)
  352. {
  353. /* Check the parameters */
  354. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  355. /* Set the Counter Register value */
  356. TIMx->CNT = Counter;
  357. }
  358. /**
  359. * @brief Sets the TIMx Autoreload Register value
  360. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  361. * @param Autoreload: specifies the Autoreload register new value.
  362. * @retval None
  363. */
  364. void TIM_SetAutoreload(TIM_TypeDef* TIMx, uint32_t Autoreload)
  365. {
  366. /* Check the parameters */
  367. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  368. /* Set the Autoreload Register value */
  369. TIMx->ARR = Autoreload;
  370. }
  371. /**
  372. * @brief Gets the TIMx Counter value.
  373. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  374. * @retval Counter Register value
  375. */
  376. uint32_t TIM_GetCounter(TIM_TypeDef* TIMx)
  377. {
  378. /* Check the parameters */
  379. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  380. /* Get the Counter Register value */
  381. return TIMx->CNT;
  382. }
  383. /**
  384. * @brief Gets the TIMx Prescaler value.
  385. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  386. * @retval Prescaler Register value.
  387. */
  388. uint16_t TIM_GetPrescaler(TIM_TypeDef* TIMx)
  389. {
  390. /* Check the parameters */
  391. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  392. /* Get the Prescaler Register value */
  393. return TIMx->PSC;
  394. }
  395. /**
  396. * @brief Enables or Disables the TIMx Update event.
  397. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  398. * @param NewState: new state of the TIMx UDIS bit
  399. * This parameter can be: ENABLE or DISABLE.
  400. * @retval None
  401. */
  402. void TIM_UpdateDisableConfig(TIM_TypeDef* TIMx, FunctionalState NewState)
  403. {
  404. /* Check the parameters */
  405. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  406. assert_param(IS_FUNCTIONAL_STATE(NewState));
  407. if (NewState != DISABLE)
  408. {
  409. /* Set the Update Disable Bit */
  410. TIMx->CR1 |= TIM_CR1_UDIS;
  411. }
  412. else
  413. {
  414. /* Reset the Update Disable Bit */
  415. TIMx->CR1 &= (uint16_t)~TIM_CR1_UDIS;
  416. }
  417. }
  418. /**
  419. * @brief Configures the TIMx Update Request Interrupt source.
  420. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  421. * @param TIM_UpdateSource: specifies the Update source.
  422. * This parameter can be one of the following values:
  423. * @arg TIM_UpdateSource_Global: Source of update is the counter
  424. * overflow/underflow or the setting of UG bit, or an update
  425. * generation through the slave mode controller.
  426. * @arg TIM_UpdateSource_Regular: Source of update is counter overflow/underflow.
  427. * @retval None
  428. */
  429. void TIM_UpdateRequestConfig(TIM_TypeDef* TIMx, uint16_t TIM_UpdateSource)
  430. {
  431. /* Check the parameters */
  432. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  433. assert_param(IS_TIM_UPDATE_SOURCE(TIM_UpdateSource));
  434. if (TIM_UpdateSource != TIM_UpdateSource_Global)
  435. {
  436. /* Set the URS Bit */
  437. TIMx->CR1 |= TIM_CR1_URS;
  438. }
  439. else
  440. {
  441. /* Reset the URS Bit */
  442. TIMx->CR1 &= (uint16_t)~TIM_CR1_URS;
  443. }
  444. }
  445. /**
  446. * @brief Enables or disables TIMx peripheral Preload register on ARR.
  447. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  448. * @param NewState: new state of the TIMx peripheral Preload register
  449. * This parameter can be: ENABLE or DISABLE.
  450. * @retval None
  451. */
  452. void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState NewState)
  453. {
  454. /* Check the parameters */
  455. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  456. assert_param(IS_FUNCTIONAL_STATE(NewState));
  457. if (NewState != DISABLE)
  458. {
  459. /* Set the ARR Preload Bit */
  460. TIMx->CR1 |= TIM_CR1_ARPE;
  461. }
  462. else
  463. {
  464. /* Reset the ARR Preload Bit */
  465. TIMx->CR1 &= (uint16_t)~TIM_CR1_ARPE;
  466. }
  467. }
  468. /**
  469. * @brief Selects the TIMx's One Pulse Mode.
  470. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  471. * @param TIM_OPMode: specifies the OPM Mode to be used.
  472. * This parameter can be one of the following values:
  473. * @arg TIM_OPMode_Single
  474. * @arg TIM_OPMode_Repetitive
  475. * @retval None
  476. */
  477. void TIM_SelectOnePulseMode(TIM_TypeDef* TIMx, uint16_t TIM_OPMode)
  478. {
  479. /* Check the parameters */
  480. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  481. assert_param(IS_TIM_OPM_MODE(TIM_OPMode));
  482. /* Reset the OPM Bit */
  483. TIMx->CR1 &= (uint16_t)~TIM_CR1_OPM;
  484. /* Configure the OPM Mode */
  485. TIMx->CR1 |= TIM_OPMode;
  486. }
  487. /**
  488. * @brief Sets the TIMx Clock Division value.
  489. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  490. * @param TIM_CKD: specifies the clock division value.
  491. * This parameter can be one of the following value:
  492. * @arg TIM_CKD_DIV1: TDTS = Tck_tim
  493. * @arg TIM_CKD_DIV2: TDTS = 2*Tck_tim
  494. * @arg TIM_CKD_DIV4: TDTS = 4*Tck_tim
  495. * @retval None
  496. */
  497. void TIM_SetClockDivision(TIM_TypeDef* TIMx, uint16_t TIM_CKD)
  498. {
  499. /* Check the parameters */
  500. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  501. assert_param(IS_TIM_CKD_DIV(TIM_CKD));
  502. /* Reset the CKD Bits */
  503. TIMx->CR1 &= (uint16_t)(~TIM_CR1_CKD);
  504. /* Set the CKD value */
  505. TIMx->CR1 |= TIM_CKD;
  506. }
  507. /**
  508. * @brief Enables or disables the specified TIM peripheral.
  509. * @param TIMx: where x can be 1 to 14 to select the TIMx peripheral.
  510. * @param NewState: new state of the TIMx peripheral.
  511. * This parameter can be: ENABLE or DISABLE.
  512. * @retval None
  513. */
  514. void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
  515. {
  516. /* Check the parameters */
  517. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  518. assert_param(IS_FUNCTIONAL_STATE(NewState));
  519. if (NewState != DISABLE)
  520. {
  521. /* Enable the TIM Counter */
  522. TIMx->CR1 |= TIM_CR1_CEN;
  523. }
  524. else
  525. {
  526. /* Disable the TIM Counter */
  527. TIMx->CR1 &= (uint16_t)~TIM_CR1_CEN;
  528. }
  529. }
  530. /**
  531. * @}
  532. */
  533. /** @defgroup TIM_Group2 Output Compare management functions
  534. * @brief Output Compare management functions
  535. *
  536. @verbatim
  537. ===============================================================================
  538. ##### Output Compare management functions #####
  539. ===============================================================================
  540. ##### TIM Driver: how to use it in Output Compare Mode #####
  541. ===============================================================================
  542. [..]
  543. To use the Timer in Output Compare mode, the following steps are mandatory:
  544. (#) Enable TIM clock using RCC_APBxPeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE)
  545. function
  546. (#) Configure the TIM pins by configuring the corresponding GPIO pins
  547. (#) Configure the Time base unit as described in the first part of this driver,
  548. (++) if needed, else the Timer will run with the default configuration:
  549. Autoreload value = 0xFFFF
  550. (++) Prescaler value = 0x0000
  551. (++) Counter mode = Up counting
  552. (++) Clock Division = TIM_CKD_DIV1
  553. (#) Fill the TIM_OCInitStruct with the desired parameters including:
  554. (++) The TIM Output Compare mode: TIM_OCMode
  555. (++) TIM Output State: TIM_OutputState
  556. (++) TIM Pulse value: TIM_Pulse
  557. (++) TIM Output Compare Polarity : TIM_OCPolarity
  558. (#) Call TIM_OCxInit(TIMx, &TIM_OCInitStruct) to configure the desired
  559. channel with the corresponding configuration
  560. (#) Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
  561. -@- All other functions can be used separately to modify, if needed,
  562. a specific feature of the Timer.
  563. -@- In case of PWM mode, this function is mandatory:
  564. TIM_OCxPreloadConfig(TIMx, TIM_OCPreload_ENABLE);
  565. -@- If the corresponding interrupt or DMA request are needed, the user should:
  566. (+@) Enable the NVIC (or the DMA) to use the TIM interrupts (or DMA requests).
  567. (+@) Enable the corresponding interrupt (or DMA request) using the function
  568. TIM_ITConfig(TIMx, TIM_IT_CCx) (or TIM_DMA_Cmd(TIMx, TIM_DMA_CCx))
  569. @endverbatim
  570. * @{
  571. */
  572. /**
  573. * @brief Initializes the TIMx Channel1 according to the specified parameters in
  574. * the TIM_OCInitStruct.
  575. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  576. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  577. * the configuration information for the specified TIM peripheral.
  578. * @retval None
  579. */
  580. void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  581. {
  582. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  583. /* Check the parameters */
  584. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  585. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  586. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  587. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  588. /* Disable the Channel 1: Reset the CC1E Bit */
  589. TIMx->CCER &= (uint16_t)~TIM_CCER_CC1E;
  590. /* Get the TIMx CCER register value */
  591. tmpccer = TIMx->CCER;
  592. /* Get the TIMx CR2 register value */
  593. tmpcr2 = TIMx->CR2;
  594. /* Get the TIMx CCMR1 register value */
  595. tmpccmrx = TIMx->CCMR1;
  596. /* Reset the Output Compare Mode Bits */
  597. tmpccmrx &= (uint16_t)~TIM_CCMR1_OC1M;
  598. tmpccmrx &= (uint16_t)~TIM_CCMR1_CC1S;
  599. /* Select the Output Compare Mode */
  600. tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  601. /* Reset the Output Polarity level */
  602. tmpccer &= (uint16_t)~TIM_CCER_CC1P;
  603. /* Set the Output Compare Polarity */
  604. tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;
  605. /* Set the Output State */
  606. tmpccer |= TIM_OCInitStruct->TIM_OutputState;
  607. if((TIMx == TIM1) || (TIMx == TIM8))
  608. {
  609. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  610. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  611. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  612. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  613. /* Reset the Output N Polarity level */
  614. tmpccer &= (uint16_t)~TIM_CCER_CC1NP;
  615. /* Set the Output N Polarity */
  616. tmpccer |= TIM_OCInitStruct->TIM_OCNPolarity;
  617. /* Reset the Output N State */
  618. tmpccer &= (uint16_t)~TIM_CCER_CC1NE;
  619. /* Set the Output N State */
  620. tmpccer |= TIM_OCInitStruct->TIM_OutputNState;
  621. /* Reset the Output Compare and Output Compare N IDLE State */
  622. tmpcr2 &= (uint16_t)~TIM_CR2_OIS1;
  623. tmpcr2 &= (uint16_t)~TIM_CR2_OIS1N;
  624. /* Set the Output Idle state */
  625. tmpcr2 |= TIM_OCInitStruct->TIM_OCIdleState;
  626. /* Set the Output N Idle state */
  627. tmpcr2 |= TIM_OCInitStruct->TIM_OCNIdleState;
  628. }
  629. /* Write to TIMx CR2 */
  630. TIMx->CR2 = tmpcr2;
  631. /* Write to TIMx CCMR1 */
  632. TIMx->CCMR1 = tmpccmrx;
  633. /* Set the Capture Compare Register value */
  634. TIMx->CCR1 = TIM_OCInitStruct->TIM_Pulse;
  635. /* Write to TIMx CCER */
  636. TIMx->CCER = tmpccer;
  637. }
  638. /**
  639. * @brief Initializes the TIMx Channel2 according to the specified parameters
  640. * in the TIM_OCInitStruct.
  641. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  642. * peripheral.
  643. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  644. * the configuration information for the specified TIM peripheral.
  645. * @retval None
  646. */
  647. void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  648. {
  649. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  650. /* Check the parameters */
  651. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  652. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  653. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  654. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  655. /* Disable the Channel 2: Reset the CC2E Bit */
  656. TIMx->CCER &= (uint16_t)~TIM_CCER_CC2E;
  657. /* Get the TIMx CCER register value */
  658. tmpccer = TIMx->CCER;
  659. /* Get the TIMx CR2 register value */
  660. tmpcr2 = TIMx->CR2;
  661. /* Get the TIMx CCMR1 register value */
  662. tmpccmrx = TIMx->CCMR1;
  663. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  664. tmpccmrx &= (uint16_t)~TIM_CCMR1_OC2M;
  665. tmpccmrx &= (uint16_t)~TIM_CCMR1_CC2S;
  666. /* Select the Output Compare Mode */
  667. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
  668. /* Reset the Output Polarity level */
  669. tmpccer &= (uint16_t)~TIM_CCER_CC2P;
  670. /* Set the Output Compare Polarity */
  671. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 4);
  672. /* Set the Output State */
  673. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 4);
  674. if((TIMx == TIM1) || (TIMx == TIM8))
  675. {
  676. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  677. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  678. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  679. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  680. /* Reset the Output N Polarity level */
  681. tmpccer &= (uint16_t)~TIM_CCER_CC2NP;
  682. /* Set the Output N Polarity */
  683. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 4);
  684. /* Reset the Output N State */
  685. tmpccer &= (uint16_t)~TIM_CCER_CC2NE;
  686. /* Set the Output N State */
  687. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 4);
  688. /* Reset the Output Compare and Output Compare N IDLE State */
  689. tmpcr2 &= (uint16_t)~TIM_CR2_OIS2;
  690. tmpcr2 &= (uint16_t)~TIM_CR2_OIS2N;
  691. /* Set the Output Idle state */
  692. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 2);
  693. /* Set the Output N Idle state */
  694. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 2);
  695. }
  696. /* Write to TIMx CR2 */
  697. TIMx->CR2 = tmpcr2;
  698. /* Write to TIMx CCMR1 */
  699. TIMx->CCMR1 = tmpccmrx;
  700. /* Set the Capture Compare Register value */
  701. TIMx->CCR2 = TIM_OCInitStruct->TIM_Pulse;
  702. /* Write to TIMx CCER */
  703. TIMx->CCER = tmpccer;
  704. }
  705. /**
  706. * @brief Initializes the TIMx Channel3 according to the specified parameters
  707. * in the TIM_OCInitStruct.
  708. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  709. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  710. * the configuration information for the specified TIM peripheral.
  711. * @retval None
  712. */
  713. void TIM_OC3Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  714. {
  715. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  716. /* Check the parameters */
  717. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  718. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  719. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  720. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  721. /* Disable the Channel 3: Reset the CC2E Bit */
  722. TIMx->CCER &= (uint16_t)~TIM_CCER_CC3E;
  723. /* Get the TIMx CCER register value */
  724. tmpccer = TIMx->CCER;
  725. /* Get the TIMx CR2 register value */
  726. tmpcr2 = TIMx->CR2;
  727. /* Get the TIMx CCMR2 register value */
  728. tmpccmrx = TIMx->CCMR2;
  729. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  730. tmpccmrx &= (uint16_t)~TIM_CCMR2_OC3M;
  731. tmpccmrx &= (uint16_t)~TIM_CCMR2_CC3S;
  732. /* Select the Output Compare Mode */
  733. tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  734. /* Reset the Output Polarity level */
  735. tmpccer &= (uint16_t)~TIM_CCER_CC3P;
  736. /* Set the Output Compare Polarity */
  737. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 8);
  738. /* Set the Output State */
  739. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 8);
  740. if((TIMx == TIM1) || (TIMx == TIM8))
  741. {
  742. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  743. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  744. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  745. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  746. /* Reset the Output N Polarity level */
  747. tmpccer &= (uint16_t)~TIM_CCER_CC3NP;
  748. /* Set the Output N Polarity */
  749. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 8);
  750. /* Reset the Output N State */
  751. tmpccer &= (uint16_t)~TIM_CCER_CC3NE;
  752. /* Set the Output N State */
  753. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 8);
  754. /* Reset the Output Compare and Output Compare N IDLE State */
  755. tmpcr2 &= (uint16_t)~TIM_CR2_OIS3;
  756. tmpcr2 &= (uint16_t)~TIM_CR2_OIS3N;
  757. /* Set the Output Idle state */
  758. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 4);
  759. /* Set the Output N Idle state */
  760. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 4);
  761. }
  762. /* Write to TIMx CR2 */
  763. TIMx->CR2 = tmpcr2;
  764. /* Write to TIMx CCMR2 */
  765. TIMx->CCMR2 = tmpccmrx;
  766. /* Set the Capture Compare Register value */
  767. TIMx->CCR3 = TIM_OCInitStruct->TIM_Pulse;
  768. /* Write to TIMx CCER */
  769. TIMx->CCER = tmpccer;
  770. }
  771. /**
  772. * @brief Initializes the TIMx Channel4 according to the specified parameters
  773. * in the TIM_OCInitStruct.
  774. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  775. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  776. * the configuration information for the specified TIM peripheral.
  777. * @retval None
  778. */
  779. void TIM_OC4Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  780. {
  781. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  782. /* Check the parameters */
  783. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  784. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  785. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  786. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  787. /* Disable the Channel 4: Reset the CC4E Bit */
  788. TIMx->CCER &= (uint16_t)~TIM_CCER_CC4E;
  789. /* Get the TIMx CCER register value */
  790. tmpccer = TIMx->CCER;
  791. /* Get the TIMx CR2 register value */
  792. tmpcr2 = TIMx->CR2;
  793. /* Get the TIMx CCMR2 register value */
  794. tmpccmrx = TIMx->CCMR2;
  795. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  796. tmpccmrx &= (uint16_t)~TIM_CCMR2_OC4M;
  797. tmpccmrx &= (uint16_t)~TIM_CCMR2_CC4S;
  798. /* Select the Output Compare Mode */
  799. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
  800. /* Reset the Output Polarity level */
  801. tmpccer &= (uint16_t)~TIM_CCER_CC4P;
  802. /* Set the Output Compare Polarity */
  803. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 12);
  804. /* Set the Output State */
  805. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 12);
  806. if((TIMx == TIM1) || (TIMx == TIM8))
  807. {
  808. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  809. /* Reset the Output Compare IDLE State */
  810. tmpcr2 &=(uint16_t) ~TIM_CR2_OIS4;
  811. /* Set the Output Idle state */
  812. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 6);
  813. }
  814. /* Write to TIMx CR2 */
  815. TIMx->CR2 = tmpcr2;
  816. /* Write to TIMx CCMR2 */
  817. TIMx->CCMR2 = tmpccmrx;
  818. /* Set the Capture Compare Register value */
  819. TIMx->CCR4 = TIM_OCInitStruct->TIM_Pulse;
  820. /* Write to TIMx CCER */
  821. TIMx->CCER = tmpccer;
  822. }
  823. /**
  824. * @brief Fills each TIM_OCInitStruct member with its default value.
  825. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure which will
  826. * be initialized.
  827. * @retval None
  828. */
  829. void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct)
  830. {
  831. /* Set the default configuration */
  832. TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;
  833. TIM_OCInitStruct->TIM_OutputState = TIM_OutputState_Disable;
  834. TIM_OCInitStruct->TIM_OutputNState = TIM_OutputNState_Disable;
  835. TIM_OCInitStruct->TIM_Pulse = 0x00000000;
  836. TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;
  837. TIM_OCInitStruct->TIM_OCNPolarity = TIM_OCPolarity_High;
  838. TIM_OCInitStruct->TIM_OCIdleState = TIM_OCIdleState_Reset;
  839. TIM_OCInitStruct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  840. }
  841. /**
  842. * @brief Selects the TIM Output Compare Mode.
  843. * @note This function disables the selected channel before changing the Output
  844. * Compare Mode. If needed, user has to enable this channel using
  845. * TIM_CCxCmd() and TIM_CCxNCmd() functions.
  846. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  847. * @param TIM_Channel: specifies the TIM Channel
  848. * This parameter can be one of the following values:
  849. * @arg TIM_Channel_1: TIM Channel 1
  850. * @arg TIM_Channel_2: TIM Channel 2
  851. * @arg TIM_Channel_3: TIM Channel 3
  852. * @arg TIM_Channel_4: TIM Channel 4
  853. * @param TIM_OCMode: specifies the TIM Output Compare Mode.
  854. * This parameter can be one of the following values:
  855. * @arg TIM_OCMode_Timing
  856. * @arg TIM_OCMode_Active
  857. * @arg TIM_OCMode_Toggle
  858. * @arg TIM_OCMode_PWM1
  859. * @arg TIM_OCMode_PWM2
  860. * @arg TIM_ForcedAction_Active
  861. * @arg TIM_ForcedAction_InActive
  862. * @retval None
  863. */
  864. void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode)
  865. {
  866. uint32_t tmp = 0;
  867. uint16_t tmp1 = 0;
  868. /* Check the parameters */
  869. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  870. assert_param(IS_TIM_CHANNEL(TIM_Channel));
  871. assert_param(IS_TIM_OCM(TIM_OCMode));
  872. tmp = (uint32_t) TIMx;
  873. tmp += CCMR_OFFSET;
  874. tmp1 = CCER_CCE_SET << (uint16_t)TIM_Channel;
  875. /* Disable the Channel: Reset the CCxE Bit */
  876. TIMx->CCER &= (uint16_t) ~tmp1;
  877. if((TIM_Channel == TIM_Channel_1) ||(TIM_Channel == TIM_Channel_3))
  878. {
  879. tmp += (TIM_Channel>>1);
  880. /* Reset the OCxM bits in the CCMRx register */
  881. *(__IO uint32_t *) tmp &= CCMR_OC13M_MASK;
  882. /* Configure the OCxM bits in the CCMRx register */
  883. *(__IO uint32_t *) tmp |= TIM_OCMode;
  884. }
  885. else
  886. {
  887. tmp += (uint16_t)(TIM_Channel - (uint16_t)4)>> (uint16_t)1;
  888. /* Reset the OCxM bits in the CCMRx register */
  889. *(__IO uint32_t *) tmp &= CCMR_OC24M_MASK;
  890. /* Configure the OCxM bits in the CCMRx register */
  891. *(__IO uint32_t *) tmp |= (uint16_t)(TIM_OCMode << 8);
  892. }
  893. }
  894. /**
  895. * @brief Sets the TIMx Capture Compare1 Register value
  896. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  897. * @param Compare1: specifies the Capture Compare1 register new value.
  898. * @retval None
  899. */
  900. void TIM_SetCompare1(TIM_TypeDef* TIMx, uint32_t Compare1)
  901. {
  902. /* Check the parameters */
  903. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  904. /* Set the Capture Compare1 Register value */
  905. TIMx->CCR1 = Compare1;
  906. }
  907. /**
  908. * @brief Sets the TIMx Capture Compare2 Register value
  909. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  910. * peripheral.
  911. * @param Compare2: specifies the Capture Compare2 register new value.
  912. * @retval None
  913. */
  914. void TIM_SetCompare2(TIM_TypeDef* TIMx, uint32_t Compare2)
  915. {
  916. /* Check the parameters */
  917. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  918. /* Set the Capture Compare2 Register value */
  919. TIMx->CCR2 = Compare2;
  920. }
  921. /**
  922. * @brief Sets the TIMx Capture Compare3 Register value
  923. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  924. * @param Compare3: specifies the Capture Compare3 register new value.
  925. * @retval None
  926. */
  927. void TIM_SetCompare3(TIM_TypeDef* TIMx, uint32_t Compare3)
  928. {
  929. /* Check the parameters */
  930. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  931. /* Set the Capture Compare3 Register value */
  932. TIMx->CCR3 = Compare3;
  933. }
  934. /**
  935. * @brief Sets the TIMx Capture Compare4 Register value
  936. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  937. * @param Compare4: specifies the Capture Compare4 register new value.
  938. * @retval None
  939. */
  940. void TIM_SetCompare4(TIM_TypeDef* TIMx, uint32_t Compare4)
  941. {
  942. /* Check the parameters */
  943. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  944. /* Set the Capture Compare4 Register value */
  945. TIMx->CCR4 = Compare4;
  946. }
  947. /**
  948. * @brief Forces the TIMx output 1 waveform to active or inactive level.
  949. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  950. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  951. * This parameter can be one of the following values:
  952. * @arg TIM_ForcedAction_Active: Force active level on OC1REF
  953. * @arg TIM_ForcedAction_InActive: Force inactive level on OC1REF.
  954. * @retval None
  955. */
  956. void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  957. {
  958. uint16_t tmpccmr1 = 0;
  959. /* Check the parameters */
  960. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  961. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  962. tmpccmr1 = TIMx->CCMR1;
  963. /* Reset the OC1M Bits */
  964. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC1M;
  965. /* Configure The Forced output Mode */
  966. tmpccmr1 |= TIM_ForcedAction;
  967. /* Write to TIMx CCMR1 register */
  968. TIMx->CCMR1 = tmpccmr1;
  969. }
  970. /**
  971. * @brief Forces the TIMx output 2 waveform to active or inactive level.
  972. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  973. * peripheral.
  974. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  975. * This parameter can be one of the following values:
  976. * @arg TIM_ForcedAction_Active: Force active level on OC2REF
  977. * @arg TIM_ForcedAction_InActive: Force inactive level on OC2REF.
  978. * @retval None
  979. */
  980. void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  981. {
  982. uint16_t tmpccmr1 = 0;
  983. /* Check the parameters */
  984. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  985. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  986. tmpccmr1 = TIMx->CCMR1;
  987. /* Reset the OC2M Bits */
  988. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC2M;
  989. /* Configure The Forced output Mode */
  990. tmpccmr1 |= (uint16_t)(TIM_ForcedAction << 8);
  991. /* Write to TIMx CCMR1 register */
  992. TIMx->CCMR1 = tmpccmr1;
  993. }
  994. /**
  995. * @brief Forces the TIMx output 3 waveform to active or inactive level.
  996. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  997. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  998. * This parameter can be one of the following values:
  999. * @arg TIM_ForcedAction_Active: Force active level on OC3REF
  1000. * @arg TIM_ForcedAction_InActive: Force inactive level on OC3REF.
  1001. * @retval None
  1002. */
  1003. void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  1004. {
  1005. uint16_t tmpccmr2 = 0;
  1006. /* Check the parameters */
  1007. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1008. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  1009. tmpccmr2 = TIMx->CCMR2;
  1010. /* Reset the OC1M Bits */
  1011. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC3M;
  1012. /* Configure The Forced output Mode */
  1013. tmpccmr2 |= TIM_ForcedAction;
  1014. /* Write to TIMx CCMR2 register */
  1015. TIMx->CCMR2 = tmpccmr2;
  1016. }
  1017. /**
  1018. * @brief Forces the TIMx output 4 waveform to active or inactive level.
  1019. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1020. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  1021. * This parameter can be one of the following values:
  1022. * @arg TIM_ForcedAction_Active: Force active level on OC4REF
  1023. * @arg TIM_ForcedAction_InActive: Force inactive level on OC4REF.
  1024. * @retval None
  1025. */
  1026. void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  1027. {
  1028. uint16_t tmpccmr2 = 0;
  1029. /* Check the parameters */
  1030. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1031. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  1032. tmpccmr2 = TIMx->CCMR2;
  1033. /* Reset the OC2M Bits */
  1034. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC4M;
  1035. /* Configure The Forced output Mode */
  1036. tmpccmr2 |= (uint16_t)(TIM_ForcedAction << 8);
  1037. /* Write to TIMx CCMR2 register */
  1038. TIMx->CCMR2 = tmpccmr2;
  1039. }
  1040. /**
  1041. * @brief Enables or disables the TIMx peripheral Preload register on CCR1.
  1042. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1043. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1044. * This parameter can be one of the following values:
  1045. * @arg TIM_OCPreload_Enable
  1046. * @arg TIM_OCPreload_Disable
  1047. * @retval None
  1048. */
  1049. void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1050. {
  1051. uint16_t tmpccmr1 = 0;
  1052. /* Check the parameters */
  1053. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1054. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1055. tmpccmr1 = TIMx->CCMR1;
  1056. /* Reset the OC1PE Bit */
  1057. tmpccmr1 &= (uint16_t)(~TIM_CCMR1_OC1PE);
  1058. /* Enable or Disable the Output Compare Preload feature */
  1059. tmpccmr1 |= TIM_OCPreload;
  1060. /* Write to TIMx CCMR1 register */
  1061. TIMx->CCMR1 = tmpccmr1;
  1062. }
  1063. /**
  1064. * @brief Enables or disables the TIMx peripheral Preload register on CCR2.
  1065. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1066. * peripheral.
  1067. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1068. * This parameter can be one of the following values:
  1069. * @arg TIM_OCPreload_Enable
  1070. * @arg TIM_OCPreload_Disable
  1071. * @retval None
  1072. */
  1073. void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1074. {
  1075. uint16_t tmpccmr1 = 0;
  1076. /* Check the parameters */
  1077. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1078. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1079. tmpccmr1 = TIMx->CCMR1;
  1080. /* Reset the OC2PE Bit */
  1081. tmpccmr1 &= (uint16_t)(~TIM_CCMR1_OC2PE);
  1082. /* Enable or Disable the Output Compare Preload feature */
  1083. tmpccmr1 |= (uint16_t)(TIM_OCPreload << 8);
  1084. /* Write to TIMx CCMR1 register */
  1085. TIMx->CCMR1 = tmpccmr1;
  1086. }
  1087. /**
  1088. * @brief Enables or disables the TIMx peripheral Preload register on CCR3.
  1089. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1090. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1091. * This parameter can be one of the following values:
  1092. * @arg TIM_OCPreload_Enable
  1093. * @arg TIM_OCPreload_Disable
  1094. * @retval None
  1095. */
  1096. void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1097. {
  1098. uint16_t tmpccmr2 = 0;
  1099. /* Check the parameters */
  1100. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1101. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1102. tmpccmr2 = TIMx->CCMR2;
  1103. /* Reset the OC3PE Bit */
  1104. tmpccmr2 &= (uint16_t)(~TIM_CCMR2_OC3PE);
  1105. /* Enable or Disable the Output Compare Preload feature */
  1106. tmpccmr2 |= TIM_OCPreload;
  1107. /* Write to TIMx CCMR2 register */
  1108. TIMx->CCMR2 = tmpccmr2;
  1109. }
  1110. /**
  1111. * @brief Enables or disables the TIMx peripheral Preload register on CCR4.
  1112. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1113. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1114. * This parameter can be one of the following values:
  1115. * @arg TIM_OCPreload_Enable
  1116. * @arg TIM_OCPreload_Disable
  1117. * @retval None
  1118. */
  1119. void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1120. {
  1121. uint16_t tmpccmr2 = 0;
  1122. /* Check the parameters */
  1123. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1124. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1125. tmpccmr2 = TIMx->CCMR2;
  1126. /* Reset the OC4PE Bit */
  1127. tmpccmr2 &= (uint16_t)(~TIM_CCMR2_OC4PE);
  1128. /* Enable or Disable the Output Compare Preload feature */
  1129. tmpccmr2 |= (uint16_t)(TIM_OCPreload << 8);
  1130. /* Write to TIMx CCMR2 register */
  1131. TIMx->CCMR2 = tmpccmr2;
  1132. }
  1133. /**
  1134. * @brief Configures the TIMx Output Compare 1 Fast feature.
  1135. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1136. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1137. * This parameter can be one of the following values:
  1138. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1139. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1140. * @retval None
  1141. */
  1142. void TIM_OC1FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1143. {
  1144. uint16_t tmpccmr1 = 0;
  1145. /* Check the parameters */
  1146. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1147. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1148. /* Get the TIMx CCMR1 register value */
  1149. tmpccmr1 = TIMx->CCMR1;
  1150. /* Reset the OC1FE Bit */
  1151. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC1FE;
  1152. /* Enable or Disable the Output Compare Fast Bit */
  1153. tmpccmr1 |= TIM_OCFast;
  1154. /* Write to TIMx CCMR1 */
  1155. TIMx->CCMR1 = tmpccmr1;
  1156. }
  1157. /**
  1158. * @brief Configures the TIMx Output Compare 2 Fast feature.
  1159. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1160. * peripheral.
  1161. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1162. * This parameter can be one of the following values:
  1163. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1164. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1165. * @retval None
  1166. */
  1167. void TIM_OC2FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1168. {
  1169. uint16_t tmpccmr1 = 0;
  1170. /* Check the parameters */
  1171. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1172. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1173. /* Get the TIMx CCMR1 register value */
  1174. tmpccmr1 = TIMx->CCMR1;
  1175. /* Reset the OC2FE Bit */
  1176. tmpccmr1 &= (uint16_t)(~TIM_CCMR1_OC2FE);
  1177. /* Enable or Disable the Output Compare Fast Bit */
  1178. tmpccmr1 |= (uint16_t)(TIM_OCFast << 8);
  1179. /* Write to TIMx CCMR1 */
  1180. TIMx->CCMR1 = tmpccmr1;
  1181. }
  1182. /**
  1183. * @brief Configures the TIMx Output Compare 3 Fast feature.
  1184. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1185. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1186. * This parameter can be one of the following values:
  1187. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1188. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1189. * @retval None
  1190. */
  1191. void TIM_OC3FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1192. {
  1193. uint16_t tmpccmr2 = 0;
  1194. /* Check the parameters */
  1195. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1196. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1197. /* Get the TIMx CCMR2 register value */
  1198. tmpccmr2 = TIMx->CCMR2;
  1199. /* Reset the OC3FE Bit */
  1200. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC3FE;
  1201. /* Enable or Disable the Output Compare Fast Bit */
  1202. tmpccmr2 |= TIM_OCFast;
  1203. /* Write to TIMx CCMR2 */
  1204. TIMx->CCMR2 = tmpccmr2;
  1205. }
  1206. /**
  1207. * @brief Configures the TIMx Output Compare 4 Fast feature.
  1208. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1209. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1210. * This parameter can be one of the following values:
  1211. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1212. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1213. * @retval None
  1214. */
  1215. void TIM_OC4FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1216. {
  1217. uint16_t tmpccmr2 = 0;
  1218. /* Check the parameters */
  1219. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1220. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1221. /* Get the TIMx CCMR2 register value */
  1222. tmpccmr2 = TIMx->CCMR2;
  1223. /* Reset the OC4FE Bit */
  1224. tmpccmr2 &= (uint16_t)(~TIM_CCMR2_OC4FE);
  1225. /* Enable or Disable the Output Compare Fast Bit */
  1226. tmpccmr2 |= (uint16_t)(TIM_OCFast << 8);
  1227. /* Write to TIMx CCMR2 */
  1228. TIMx->CCMR2 = tmpccmr2;
  1229. }
  1230. /**
  1231. * @brief Clears or safeguards the OCREF1 signal on an external event
  1232. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1233. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1234. * This parameter can be one of the following values:
  1235. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1236. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1237. * @retval None
  1238. */
  1239. void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1240. {
  1241. uint16_t tmpccmr1 = 0;
  1242. /* Check the parameters */
  1243. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1244. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1245. tmpccmr1 = TIMx->CCMR1;
  1246. /* Reset the OC1CE Bit */
  1247. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC1CE;
  1248. /* Enable or Disable the Output Compare Clear Bit */
  1249. tmpccmr1 |= TIM_OCClear;
  1250. /* Write to TIMx CCMR1 register */
  1251. TIMx->CCMR1 = tmpccmr1;
  1252. }
  1253. /**
  1254. * @brief Clears or safeguards the OCREF2 signal on an external event
  1255. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1256. * peripheral.
  1257. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1258. * This parameter can be one of the following values:
  1259. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1260. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1261. * @retval None
  1262. */
  1263. void TIM_ClearOC2Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1264. {
  1265. uint16_t tmpccmr1 = 0;
  1266. /* Check the parameters */
  1267. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1268. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1269. tmpccmr1 = TIMx->CCMR1;
  1270. /* Reset the OC2CE Bit */
  1271. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC2CE;
  1272. /* Enable or Disable the Output Compare Clear Bit */
  1273. tmpccmr1 |= (uint16_t)(TIM_OCClear << 8);
  1274. /* Write to TIMx CCMR1 register */
  1275. TIMx->CCMR1 = tmpccmr1;
  1276. }
  1277. /**
  1278. * @brief Clears or safeguards the OCREF3 signal on an external event
  1279. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1280. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1281. * This parameter can be one of the following values:
  1282. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1283. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1284. * @retval None
  1285. */
  1286. void TIM_ClearOC3Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1287. {
  1288. uint16_t tmpccmr2 = 0;
  1289. /* Check the parameters */
  1290. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1291. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1292. tmpccmr2 = TIMx->CCMR2;
  1293. /* Reset the OC3CE Bit */
  1294. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC3CE;
  1295. /* Enable or Disable the Output Compare Clear Bit */
  1296. tmpccmr2 |= TIM_OCClear;
  1297. /* Write to TIMx CCMR2 register */
  1298. TIMx->CCMR2 = tmpccmr2;
  1299. }
  1300. /**
  1301. * @brief Clears or safeguards the OCREF4 signal on an external event
  1302. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1303. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1304. * This parameter can be one of the following values:
  1305. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1306. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1307. * @retval None
  1308. */
  1309. void TIM_ClearOC4Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1310. {
  1311. uint16_t tmpccmr2 = 0;
  1312. /* Check the parameters */
  1313. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1314. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1315. tmpccmr2 = TIMx->CCMR2;
  1316. /* Reset the OC4CE Bit */
  1317. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC4CE;
  1318. /* Enable or Disable the Output Compare Clear Bit */
  1319. tmpccmr2 |= (uint16_t)(TIM_OCClear << 8);
  1320. /* Write to TIMx CCMR2 register */
  1321. TIMx->CCMR2 = tmpccmr2;
  1322. }
  1323. /**
  1324. * @brief Configures the TIMx channel 1 polarity.
  1325. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1326. * @param TIM_OCPolarity: specifies the OC1 Polarity
  1327. * This parameter can be one of the following values:
  1328. * @arg TIM_OCPolarity_High: Output Compare active high
  1329. * @arg TIM_OCPolarity_Low: Output Compare active low
  1330. * @retval None
  1331. */
  1332. void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1333. {
  1334. uint16_t tmpccer = 0;
  1335. /* Check the parameters */
  1336. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1337. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1338. tmpccer = TIMx->CCER;
  1339. /* Set or Reset the CC1P Bit */
  1340. tmpccer &= (uint16_t)(~TIM_CCER_CC1P);
  1341. tmpccer |= TIM_OCPolarity;
  1342. /* Write to TIMx CCER register */
  1343. TIMx->CCER = tmpccer;
  1344. }
  1345. /**
  1346. * @brief Configures the TIMx Channel 1N polarity.
  1347. * @param TIMx: where x can be 1 or 8 to select the TIM peripheral.
  1348. * @param TIM_OCNPolarity: specifies the OC1N Polarity
  1349. * This parameter can be one of the following values:
  1350. * @arg TIM_OCNPolarity_High: Output Compare active high
  1351. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1352. * @retval None
  1353. */
  1354. void TIM_OC1NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1355. {
  1356. uint16_t tmpccer = 0;
  1357. /* Check the parameters */
  1358. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1359. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1360. tmpccer = TIMx->CCER;
  1361. /* Set or Reset the CC1NP Bit */
  1362. tmpccer &= (uint16_t)~TIM_CCER_CC1NP;
  1363. tmpccer |= TIM_OCNPolarity;
  1364. /* Write to TIMx CCER register */
  1365. TIMx->CCER = tmpccer;
  1366. }
  1367. /**
  1368. * @brief Configures the TIMx channel 2 polarity.
  1369. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1370. * peripheral.
  1371. * @param TIM_OCPolarity: specifies the OC2 Polarity
  1372. * This parameter can be one of the following values:
  1373. * @arg TIM_OCPolarity_High: Output Compare active high
  1374. * @arg TIM_OCPolarity_Low: Output Compare active low
  1375. * @retval None
  1376. */
  1377. void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1378. {
  1379. uint16_t tmpccer = 0;
  1380. /* Check the parameters */
  1381. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1382. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1383. tmpccer = TIMx->CCER;
  1384. /* Set or Reset the CC2P Bit */
  1385. tmpccer &= (uint16_t)(~TIM_CCER_CC2P);
  1386. tmpccer |= (uint16_t)(TIM_OCPolarity << 4);
  1387. /* Write to TIMx CCER register */
  1388. TIMx->CCER = tmpccer;
  1389. }
  1390. /**
  1391. * @brief Configures the TIMx Channel 2N polarity.
  1392. * @param TIMx: where x can be 1 or 8 to select the TIM peripheral.
  1393. * @param TIM_OCNPolarity: specifies the OC2N Polarity
  1394. * This parameter can be one of the following values:
  1395. * @arg TIM_OCNPolarity_High: Output Compare active high
  1396. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1397. * @retval None
  1398. */
  1399. void TIM_OC2NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1400. {
  1401. uint16_t tmpccer = 0;
  1402. /* Check the parameters */
  1403. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1404. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1405. tmpccer = TIMx->CCER;
  1406. /* Set or Reset the CC2NP Bit */
  1407. tmpccer &= (uint16_t)~TIM_CCER_CC2NP;
  1408. tmpccer |= (uint16_t)(TIM_OCNPolarity << 4);
  1409. /* Write to TIMx CCER register */
  1410. TIMx->CCER = tmpccer;
  1411. }
  1412. /**
  1413. * @brief Configures the TIMx channel 3 polarity.
  1414. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1415. * @param TIM_OCPolarity: specifies the OC3 Polarity
  1416. * This parameter can be one of the following values:
  1417. * @arg TIM_OCPolarity_High: Output Compare active high
  1418. * @arg TIM_OCPolarity_Low: Output Compare active low
  1419. * @retval None
  1420. */
  1421. void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1422. {
  1423. uint16_t tmpccer = 0;
  1424. /* Check the parameters */
  1425. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1426. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1427. tmpccer = TIMx->CCER;
  1428. /* Set or Reset the CC3P Bit */
  1429. tmpccer &= (uint16_t)~TIM_CCER_CC3P;
  1430. tmpccer |= (uint16_t)(TIM_OCPolarity << 8);
  1431. /* Write to TIMx CCER register */
  1432. TIMx->CCER = tmpccer;
  1433. }
  1434. /**
  1435. * @brief Configures the TIMx Channel 3N polarity.
  1436. * @param TIMx: where x can be 1 or 8 to select the TIM peripheral.
  1437. * @param TIM_OCNPolarity: specifies the OC3N Polarity
  1438. * This parameter can be one of the following values:
  1439. * @arg TIM_OCNPolarity_High: Output Compare active high
  1440. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1441. * @retval None
  1442. */
  1443. void TIM_OC3NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1444. {
  1445. uint16_t tmpccer = 0;
  1446. /* Check the parameters */
  1447. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1448. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1449. tmpccer = TIMx->CCER;
  1450. /* Set or Reset the CC3NP Bit */
  1451. tmpccer &= (uint16_t)~TIM_CCER_CC3NP;
  1452. tmpccer |= (uint16_t)(TIM_OCNPolarity << 8);
  1453. /* Write to TIMx CCER register */
  1454. TIMx->CCER = tmpccer;
  1455. }
  1456. /**
  1457. * @brief Configures the TIMx channel 4 polarity.
  1458. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1459. * @param TIM_OCPolarity: specifies the OC4 Polarity
  1460. * This parameter can be one of the following values:
  1461. * @arg TIM_OCPolarity_High: Output Compare active high
  1462. * @arg TIM_OCPolarity_Low: Output Compare active low
  1463. * @retval None
  1464. */
  1465. void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1466. {
  1467. uint16_t tmpccer = 0;
  1468. /* Check the parameters */
  1469. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1470. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1471. tmpccer = TIMx->CCER;
  1472. /* Set or Reset the CC4P Bit */
  1473. tmpccer &= (uint16_t)~TIM_CCER_CC4P;
  1474. tmpccer |= (uint16_t)(TIM_OCPolarity << 12);
  1475. /* Write to TIMx CCER register */
  1476. TIMx->CCER = tmpccer;
  1477. }
  1478. /**
  1479. * @brief Enables or disables the TIM Capture Compare Channel x.
  1480. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1481. * @param TIM_Channel: specifies the TIM Channel
  1482. * This parameter can be one of the following values:
  1483. * @arg TIM_Channel_1: TIM Channel 1
  1484. * @arg TIM_Channel_2: TIM Channel 2
  1485. * @arg TIM_Channel_3: TIM Channel 3
  1486. * @arg TIM_Channel_4: TIM Channel 4
  1487. * @param TIM_CCx: specifies the TIM Channel CCxE bit new state.
  1488. * This parameter can be: TIM_CCx_Enable or TIM_CCx_Disable.
  1489. * @retval None
  1490. */
  1491. void TIM_CCxCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx)
  1492. {
  1493. uint16_t tmp = 0;
  1494. /* Check the parameters */
  1495. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1496. assert_param(IS_TIM_CHANNEL(TIM_Channel));
  1497. assert_param(IS_TIM_CCX(TIM_CCx));
  1498. tmp = CCER_CCE_SET << TIM_Channel;
  1499. /* Reset the CCxE Bit */
  1500. TIMx->CCER &= (uint16_t)~ tmp;
  1501. /* Set or reset the CCxE Bit */
  1502. TIMx->CCER |= (uint16_t)(TIM_CCx << TIM_Channel);
  1503. }
  1504. /**
  1505. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1506. * @param TIMx: where x can be 1 or 8 to select the TIM peripheral.
  1507. * @param TIM_Channel: specifies the TIM Channel
  1508. * This parameter can be one of the following values:
  1509. * @arg TIM_Channel_1: TIM Channel 1
  1510. * @arg TIM_Channel_2: TIM Channel 2
  1511. * @arg TIM_Channel_3: TIM Channel 3
  1512. * @param TIM_CCxN: specifies the TIM Channel CCxNE bit new state.
  1513. * This parameter can be: TIM_CCxN_Enable or TIM_CCxN_Disable.
  1514. * @retval None
  1515. */
  1516. void TIM_CCxNCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN)
  1517. {
  1518. uint16_t tmp = 0;
  1519. /* Check the parameters */
  1520. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1521. assert_param(IS_TIM_COMPLEMENTARY_CHANNEL(TIM_Channel));
  1522. assert_param(IS_TIM_CCXN(TIM_CCxN));
  1523. tmp = CCER_CCNE_SET << TIM_Channel;
  1524. /* Reset the CCxNE Bit */
  1525. TIMx->CCER &= (uint16_t) ~tmp;
  1526. /* Set or reset the CCxNE Bit */
  1527. TIMx->CCER |= (uint16_t)(TIM_CCxN << TIM_Channel);
  1528. }
  1529. /**
  1530. * @}
  1531. */
  1532. /** @defgroup TIM_Group3 Input Capture management functions
  1533. * @brief Input Capture management functions
  1534. *
  1535. @verbatim
  1536. ===============================================================================
  1537. ##### Input Capture management functions #####
  1538. ===============================================================================
  1539. ##### TIM Driver: how to use it in Input Capture Mode #####
  1540. ===============================================================================
  1541. [..]
  1542. To use the Timer in Input Capture mode, the following steps are mandatory:
  1543. (#) Enable TIM clock using RCC_APBxPeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE)
  1544. function
  1545. (#) Configure the TIM pins by configuring the corresponding GPIO pins
  1546. (#) Configure the Time base unit as described in the first part of this driver,
  1547. if needed, else the Timer will run with the default configuration:
  1548. (++) Autoreload value = 0xFFFF
  1549. (++) Prescaler value = 0x0000
  1550. (++) Counter mode = Up counting
  1551. (++) Clock Division = TIM_CKD_DIV1
  1552. (#) Fill the TIM_ICInitStruct with the desired parameters including:
  1553. (++) TIM Channel: TIM_Channel
  1554. (++) TIM Input Capture polarity: TIM_ICPolarity
  1555. (++) TIM Input Capture selection: TIM_ICSelection
  1556. (++) TIM Input Capture Prescaler: TIM_ICPrescaler
  1557. (++) TIM Input Capture filter value: TIM_ICFilter
  1558. (#) Call TIM_ICInit(TIMx, &TIM_ICInitStruct) to configure the desired channel
  1559. with the corresponding configuration and to measure only frequency
  1560. or duty cycle of the input signal, or, Call TIM_PWMIConfig(TIMx, &TIM_ICInitStruct)
  1561. to configure the desired channels with the corresponding configuration
  1562. and to measure the frequency and the duty cycle of the input signal
  1563. (#) Enable the NVIC or the DMA to read the measured frequency.
  1564. (#) Enable the corresponding interrupt (or DMA request) to read the Captured
  1565. value, using the function TIM_ITConfig(TIMx, TIM_IT_CCx)
  1566. (or TIM_DMA_Cmd(TIMx, TIM_DMA_CCx))
  1567. (#) Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
  1568. (#) Use TIM_GetCapturex(TIMx); to read the captured value.
  1569. -@- All other functions can be used separately to modify, if needed,
  1570. a specific feature of the Timer.
  1571. @endverbatim
  1572. * @{
  1573. */
  1574. /**
  1575. * @brief Initializes the TIM peripheral according to the specified parameters
  1576. * in the TIM_ICInitStruct.
  1577. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1578. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure that contains
  1579. * the configuration information for the specified TIM peripheral.
  1580. * @retval None
  1581. */
  1582. void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
  1583. {
  1584. /* Check the parameters */
  1585. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1586. assert_param(IS_TIM_IC_POLARITY(TIM_ICInitStruct->TIM_ICPolarity));
  1587. assert_param(IS_TIM_IC_SELECTION(TIM_ICInitStruct->TIM_ICSelection));
  1588. assert_param(IS_TIM_IC_PRESCALER(TIM_ICInitStruct->TIM_ICPrescaler));
  1589. assert_param(IS_TIM_IC_FILTER(TIM_ICInitStruct->TIM_ICFilter));
  1590. if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  1591. {
  1592. /* TI1 Configuration */
  1593. TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1594. TIM_ICInitStruct->TIM_ICSelection,
  1595. TIM_ICInitStruct->TIM_ICFilter);
  1596. /* Set the Input Capture Prescaler value */
  1597. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1598. }
  1599. else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_2)
  1600. {
  1601. /* TI2 Configuration */
  1602. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1603. TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1604. TIM_ICInitStruct->TIM_ICSelection,
  1605. TIM_ICInitStruct->TIM_ICFilter);
  1606. /* Set the Input Capture Prescaler value */
  1607. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1608. }
  1609. else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_3)
  1610. {
  1611. /* TI3 Configuration */
  1612. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1613. TI3_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1614. TIM_ICInitStruct->TIM_ICSelection,
  1615. TIM_ICInitStruct->TIM_ICFilter);
  1616. /* Set the Input Capture Prescaler value */
  1617. TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1618. }
  1619. else
  1620. {
  1621. /* TI4 Configuration */
  1622. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1623. TI4_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1624. TIM_ICInitStruct->TIM_ICSelection,
  1625. TIM_ICInitStruct->TIM_ICFilter);
  1626. /* Set the Input Capture Prescaler value */
  1627. TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1628. }
  1629. }
  1630. /**
  1631. * @brief Fills each TIM_ICInitStruct member with its default value.
  1632. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure which will
  1633. * be initialized.
  1634. * @retval None
  1635. */
  1636. void TIM_ICStructInit(TIM_ICInitTypeDef* TIM_ICInitStruct)
  1637. {
  1638. /* Set the default configuration */
  1639. TIM_ICInitStruct->TIM_Channel = TIM_Channel_1;
  1640. TIM_ICInitStruct->TIM_ICPolarity = TIM_ICPolarity_Rising;
  1641. TIM_ICInitStruct->TIM_ICSelection = TIM_ICSelection_DirectTI;
  1642. TIM_ICInitStruct->TIM_ICPrescaler = TIM_ICPSC_DIV1;
  1643. TIM_ICInitStruct->TIM_ICFilter = 0x00;
  1644. }
  1645. /**
  1646. * @brief Configures the TIM peripheral according to the specified parameters
  1647. * in the TIM_ICInitStruct to measure an external PWM signal.
  1648. * @param TIMx: where x can be 1, 2, 3, 4, 5,8, 9 or 12 to select the TIM
  1649. * peripheral.
  1650. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure that contains
  1651. * the configuration information for the specified TIM peripheral.
  1652. * @retval None
  1653. */
  1654. void TIM_PWMIConfig(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
  1655. {
  1656. uint16_t icoppositepolarity = TIM_ICPolarity_Rising;
  1657. uint16_t icoppositeselection = TIM_ICSelection_DirectTI;
  1658. /* Check the parameters */
  1659. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1660. /* Select the Opposite Input Polarity */
  1661. if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)
  1662. {
  1663. icoppositepolarity = TIM_ICPolarity_Falling;
  1664. }
  1665. else
  1666. {
  1667. icoppositepolarity = TIM_ICPolarity_Rising;
  1668. }
  1669. /* Select the Opposite Input */
  1670. if (TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)
  1671. {
  1672. icoppositeselection = TIM_ICSelection_IndirectTI;
  1673. }
  1674. else
  1675. {
  1676. icoppositeselection = TIM_ICSelection_DirectTI;
  1677. }
  1678. if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  1679. {
  1680. /* TI1 Configuration */
  1681. TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
  1682. TIM_ICInitStruct->TIM_ICFilter);
  1683. /* Set the Input Capture Prescaler value */
  1684. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1685. /* TI2 Configuration */
  1686. TI2_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
  1687. /* Set the Input Capture Prescaler value */
  1688. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1689. }
  1690. else
  1691. {
  1692. /* TI2 Configuration */
  1693. TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
  1694. TIM_ICInitStruct->TIM_ICFilter);
  1695. /* Set the Input Capture Prescaler value */
  1696. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1697. /* TI1 Configuration */
  1698. TI1_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
  1699. /* Set the Input Capture Prescaler value */
  1700. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1701. }
  1702. }
  1703. /**
  1704. * @brief Gets the TIMx Input Capture 1 value.
  1705. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1706. * @retval Capture Compare 1 Register value.
  1707. */
  1708. uint32_t TIM_GetCapture1(TIM_TypeDef* TIMx)
  1709. {
  1710. /* Check the parameters */
  1711. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1712. /* Get the Capture 1 Register value */
  1713. return TIMx->CCR1;
  1714. }
  1715. /**
  1716. * @brief Gets the TIMx Input Capture 2 value.
  1717. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1718. * peripheral.
  1719. * @retval Capture Compare 2 Register value.
  1720. */
  1721. uint32_t TIM_GetCapture2(TIM_TypeDef* TIMx)
  1722. {
  1723. /* Check the parameters */
  1724. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1725. /* Get the Capture 2 Register value */
  1726. return TIMx->CCR2;
  1727. }
  1728. /**
  1729. * @brief Gets the TIMx Input Capture 3 value.
  1730. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1731. * @retval Capture Compare 3 Register value.
  1732. */
  1733. uint32_t TIM_GetCapture3(TIM_TypeDef* TIMx)
  1734. {
  1735. /* Check the parameters */
  1736. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1737. /* Get the Capture 3 Register value */
  1738. return TIMx->CCR3;
  1739. }
  1740. /**
  1741. * @brief Gets the TIMx Input Capture 4 value.
  1742. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1743. * @retval Capture Compare 4 Register value.
  1744. */
  1745. uint32_t TIM_GetCapture4(TIM_TypeDef* TIMx)
  1746. {
  1747. /* Check the parameters */
  1748. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1749. /* Get the Capture 4 Register value */
  1750. return TIMx->CCR4;
  1751. }
  1752. /**
  1753. * @brief Sets the TIMx Input Capture 1 prescaler.
  1754. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1755. * @param TIM_ICPSC: specifies the Input Capture1 prescaler new value.
  1756. * This parameter can be one of the following values:
  1757. * @arg TIM_ICPSC_DIV1: no prescaler
  1758. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1759. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1760. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1761. * @retval None
  1762. */
  1763. void TIM_SetIC1Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1764. {
  1765. /* Check the parameters */
  1766. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1767. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1768. /* Reset the IC1PSC Bits */
  1769. TIMx->CCMR1 &= (uint16_t)~TIM_CCMR1_IC1PSC;
  1770. /* Set the IC1PSC value */
  1771. TIMx->CCMR1 |= TIM_ICPSC;
  1772. }
  1773. /**
  1774. * @brief Sets the TIMx Input Capture 2 prescaler.
  1775. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1776. * peripheral.
  1777. * @param TIM_ICPSC: specifies the Input Capture2 prescaler new value.
  1778. * This parameter can be one of the following values:
  1779. * @arg TIM_ICPSC_DIV1: no prescaler
  1780. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1781. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1782. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1783. * @retval None
  1784. */
  1785. void TIM_SetIC2Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1786. {
  1787. /* Check the parameters */
  1788. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1789. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1790. /* Reset the IC2PSC Bits */
  1791. TIMx->CCMR1 &= (uint16_t)~TIM_CCMR1_IC2PSC;
  1792. /* Set the IC2PSC value */
  1793. TIMx->CCMR1 |= (uint16_t)(TIM_ICPSC << 8);
  1794. }
  1795. /**
  1796. * @brief Sets the TIMx Input Capture 3 prescaler.
  1797. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1798. * @param TIM_ICPSC: specifies the Input Capture3 prescaler new value.
  1799. * This parameter can be one of the following values:
  1800. * @arg TIM_ICPSC_DIV1: no prescaler
  1801. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1802. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1803. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1804. * @retval None
  1805. */
  1806. void TIM_SetIC3Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1807. {
  1808. /* Check the parameters */
  1809. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1810. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1811. /* Reset the IC3PSC Bits */
  1812. TIMx->CCMR2 &= (uint16_t)~TIM_CCMR2_IC3PSC;
  1813. /* Set the IC3PSC value */
  1814. TIMx->CCMR2 |= TIM_ICPSC;
  1815. }
  1816. /**
  1817. * @brief Sets the TIMx Input Capture 4 prescaler.
  1818. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1819. * @param TIM_ICPSC: specifies the Input Capture4 prescaler new value.
  1820. * This parameter can be one of the following values:
  1821. * @arg TIM_ICPSC_DIV1: no prescaler
  1822. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1823. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1824. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1825. * @retval None
  1826. */
  1827. void TIM_SetIC4Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1828. {
  1829. /* Check the parameters */
  1830. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1831. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1832. /* Reset the IC4PSC Bits */
  1833. TIMx->CCMR2 &= (uint16_t)~TIM_CCMR2_IC4PSC;
  1834. /* Set the IC4PSC value */
  1835. TIMx->CCMR2 |= (uint16_t)(TIM_ICPSC << 8);
  1836. }
  1837. /**
  1838. * @}
  1839. */
  1840. /** @defgroup TIM_Group4 Advanced-control timers (TIM1 and TIM8) specific features
  1841. * @brief Advanced-control timers (TIM1 and TIM8) specific features
  1842. *
  1843. @verbatim
  1844. ===============================================================================
  1845. ##### Advanced-control timers (TIM1 and TIM8) specific features #####
  1846. ===============================================================================
  1847. ##### TIM Driver: how to use the Break feature #####
  1848. ===============================================================================
  1849. [..]
  1850. After configuring the Timer channel(s) in the appropriate Output Compare mode:
  1851. (#) Fill the TIM_BDTRInitStruct with the desired parameters for the Timer
  1852. Break Polarity, dead time, Lock level, the OSSI/OSSR State and the
  1853. AOE(automatic output enable).
  1854. (#) Call TIM_BDTRConfig(TIMx, &TIM_BDTRInitStruct) to configure the Timer
  1855. (#) Enable the Main Output using TIM_CtrlPWMOutputs(TIM1, ENABLE)
  1856. (#) Once the break even occurs, the Timer's output signals are put in reset
  1857. state or in a known state (according to the configuration made in
  1858. TIM_BDTRConfig() function).
  1859. @endverbatim
  1860. * @{
  1861. */
  1862. /**
  1863. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1864. * and the AOE(automatic output enable).
  1865. * @param TIMx: where x can be 1 or 8 to select the TIM
  1866. * @param TIM_BDTRInitStruct: pointer to a TIM_BDTRInitTypeDef structure that
  1867. * contains the BDTR Register configuration information for the TIM peripheral.
  1868. * @retval None
  1869. */
  1870. void TIM_BDTRConfig(TIM_TypeDef* TIMx, TIM_BDTRInitTypeDef *TIM_BDTRInitStruct)
  1871. {
  1872. /* Check the parameters */
  1873. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1874. assert_param(IS_TIM_OSSR_STATE(TIM_BDTRInitStruct->TIM_OSSRState));
  1875. assert_param(IS_TIM_OSSI_STATE(TIM_BDTRInitStruct->TIM_OSSIState));
  1876. assert_param(IS_TIM_LOCK_LEVEL(TIM_BDTRInitStruct->TIM_LOCKLevel));
  1877. assert_param(IS_TIM_BREAK_STATE(TIM_BDTRInitStruct->TIM_Break));
  1878. assert_param(IS_TIM_BREAK_POLARITY(TIM_BDTRInitStruct->TIM_BreakPolarity));
  1879. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(TIM_BDTRInitStruct->TIM_AutomaticOutput));
  1880. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1881. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1882. TIMx->BDTR = (uint32_t)TIM_BDTRInitStruct->TIM_OSSRState | TIM_BDTRInitStruct->TIM_OSSIState |
  1883. TIM_BDTRInitStruct->TIM_LOCKLevel | TIM_BDTRInitStruct->TIM_DeadTime |
  1884. TIM_BDTRInitStruct->TIM_Break | TIM_BDTRInitStruct->TIM_BreakPolarity |
  1885. TIM_BDTRInitStruct->TIM_AutomaticOutput;
  1886. }
  1887. /**
  1888. * @brief Fills each TIM_BDTRInitStruct member with its default value.
  1889. * @param TIM_BDTRInitStruct: pointer to a TIM_BDTRInitTypeDef structure which
  1890. * will be initialized.
  1891. * @retval None
  1892. */
  1893. void TIM_BDTRStructInit(TIM_BDTRInitTypeDef* TIM_BDTRInitStruct)
  1894. {
  1895. /* Set the default configuration */
  1896. TIM_BDTRInitStruct->TIM_OSSRState = TIM_OSSRState_Disable;
  1897. TIM_BDTRInitStruct->TIM_OSSIState = TIM_OSSIState_Disable;
  1898. TIM_BDTRInitStruct->TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  1899. TIM_BDTRInitStruct->TIM_DeadTime = 0x00;
  1900. TIM_BDTRInitStruct->TIM_Break = TIM_Break_Disable;
  1901. TIM_BDTRInitStruct->TIM_BreakPolarity = TIM_BreakPolarity_Low;
  1902. TIM_BDTRInitStruct->TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
  1903. }
  1904. /**
  1905. * @brief Enables or disables the TIM peripheral Main Outputs.
  1906. * @param TIMx: where x can be 1 or 8 to select the TIMx peripheral.
  1907. * @param NewState: new state of the TIM peripheral Main Outputs.
  1908. * This parameter can be: ENABLE or DISABLE.
  1909. * @retval None
  1910. */
  1911. void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState)
  1912. {
  1913. /* Check the parameters */
  1914. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1915. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1916. if (NewState != DISABLE)
  1917. {
  1918. /* Enable the TIM Main Output */
  1919. TIMx->BDTR |= TIM_BDTR_MOE;
  1920. }
  1921. else
  1922. {
  1923. /* Disable the TIM Main Output */
  1924. TIMx->BDTR &= (uint16_t)~TIM_BDTR_MOE;
  1925. }
  1926. }
  1927. /**
  1928. * @brief Selects the TIM peripheral Commutation event.
  1929. * @param TIMx: where x can be 1 or 8 to select the TIMx peripheral
  1930. * @param NewState: new state of the Commutation event.
  1931. * This parameter can be: ENABLE or DISABLE.
  1932. * @retval None
  1933. */
  1934. void TIM_SelectCOM(TIM_TypeDef* TIMx, FunctionalState NewState)
  1935. {
  1936. /* Check the parameters */
  1937. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1938. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1939. if (NewState != DISABLE)
  1940. {
  1941. /* Set the COM Bit */
  1942. TIMx->CR2 |= TIM_CR2_CCUS;
  1943. }
  1944. else
  1945. {
  1946. /* Reset the COM Bit */
  1947. TIMx->CR2 &= (uint16_t)~TIM_CR2_CCUS;
  1948. }
  1949. }
  1950. /**
  1951. * @brief Sets or Resets the TIM peripheral Capture Compare Preload Control bit.
  1952. * @param TIMx: where x can be 1 or 8 to select the TIMx peripheral
  1953. * @param NewState: new state of the Capture Compare Preload Control bit
  1954. * This parameter can be: ENABLE or DISABLE.
  1955. * @retval None
  1956. */
  1957. void TIM_CCPreloadControl(TIM_TypeDef* TIMx, FunctionalState NewState)
  1958. {
  1959. /* Check the parameters */
  1960. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1961. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1962. if (NewState != DISABLE)
  1963. {
  1964. /* Set the CCPC Bit */
  1965. TIMx->CR2 |= TIM_CR2_CCPC;
  1966. }
  1967. else
  1968. {
  1969. /* Reset the CCPC Bit */
  1970. TIMx->CR2 &= (uint16_t)~TIM_CR2_CCPC;
  1971. }
  1972. }
  1973. /**
  1974. * @}
  1975. */
  1976. /** @defgroup TIM_Group5 Interrupts DMA and flags management functions
  1977. * @brief Interrupts, DMA and flags management functions
  1978. *
  1979. @verbatim
  1980. ===============================================================================
  1981. ##### Interrupts, DMA and flags management functions #####
  1982. ===============================================================================
  1983. @endverbatim
  1984. * @{
  1985. */
  1986. /**
  1987. * @brief Enables or disables the specified TIM interrupts.
  1988. * @param TIMx: where x can be 1 to 14 to select the TIMx peripheral.
  1989. * @param TIM_IT: specifies the TIM interrupts sources to be enabled or disabled.
  1990. * This parameter can be any combination of the following values:
  1991. * @arg TIM_IT_Update: TIM update Interrupt source
  1992. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  1993. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  1994. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  1995. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  1996. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  1997. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  1998. * @arg TIM_IT_Break: TIM Break Interrupt source
  1999. *
  2000. * @note For TIM6 and TIM7 only the parameter TIM_IT_Update can be used
  2001. * @note For TIM9 and TIM12 only one of the following parameters can be used: TIM_IT_Update,
  2002. * TIM_IT_CC1, TIM_IT_CC2 or TIM_IT_Trigger.
  2003. * @note For TIM10, TIM11, TIM13 and TIM14 only one of the following parameters can
  2004. * be used: TIM_IT_Update or TIM_IT_CC1
  2005. * @note TIM_IT_COM and TIM_IT_Break can be used only with TIM1 and TIM8
  2006. *
  2007. * @param NewState: new state of the TIM interrupts.
  2008. * This parameter can be: ENABLE or DISABLE.
  2009. * @retval None
  2010. */
  2011. void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState)
  2012. {
  2013. /* Check the parameters */
  2014. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2015. assert_param(IS_TIM_IT(TIM_IT));
  2016. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2017. if (NewState != DISABLE)
  2018. {
  2019. /* Enable the Interrupt sources */
  2020. TIMx->DIER |= TIM_IT;
  2021. }
  2022. else
  2023. {
  2024. /* Disable the Interrupt sources */
  2025. TIMx->DIER &= (uint16_t)~TIM_IT;
  2026. }
  2027. }
  2028. /**
  2029. * @brief Configures the TIMx event to be generate by software.
  2030. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2031. * @param TIM_EventSource: specifies the event source.
  2032. * This parameter can be one or more of the following values:
  2033. * @arg TIM_EventSource_Update: Timer update Event source
  2034. * @arg TIM_EventSource_CC1: Timer Capture Compare 1 Event source
  2035. * @arg TIM_EventSource_CC2: Timer Capture Compare 2 Event source
  2036. * @arg TIM_EventSource_CC3: Timer Capture Compare 3 Event source
  2037. * @arg TIM_EventSource_CC4: Timer Capture Compare 4 Event source
  2038. * @arg TIM_EventSource_COM: Timer COM event source
  2039. * @arg TIM_EventSource_Trigger: Timer Trigger Event source
  2040. * @arg TIM_EventSource_Break: Timer Break event source
  2041. *
  2042. * @note TIM6 and TIM7 can only generate an update event.
  2043. * @note TIM_EventSource_COM and TIM_EventSource_Break are used only with TIM1 and TIM8.
  2044. *
  2045. * @retval None
  2046. */
  2047. void TIM_GenerateEvent(TIM_TypeDef* TIMx, uint16_t TIM_EventSource)
  2048. {
  2049. /* Check the parameters */
  2050. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2051. assert_param(IS_TIM_EVENT_SOURCE(TIM_EventSource));
  2052. /* Set the event sources */
  2053. TIMx->EGR = TIM_EventSource;
  2054. }
  2055. /**
  2056. * @brief Checks whether the specified TIM flag is set or not.
  2057. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2058. * @param TIM_FLAG: specifies the flag to check.
  2059. * This parameter can be one of the following values:
  2060. * @arg TIM_FLAG_Update: TIM update Flag
  2061. * @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  2062. * @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  2063. * @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  2064. * @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  2065. * @arg TIM_FLAG_COM: TIM Commutation Flag
  2066. * @arg TIM_FLAG_Trigger: TIM Trigger Flag
  2067. * @arg TIM_FLAG_Break: TIM Break Flag
  2068. * @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 over capture Flag
  2069. * @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 over capture Flag
  2070. * @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 over capture Flag
  2071. * @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 over capture Flag
  2072. *
  2073. * @note TIM6 and TIM7 can have only one update flag.
  2074. * @note TIM_FLAG_COM and TIM_FLAG_Break are used only with TIM1 and TIM8.
  2075. *
  2076. * @retval The new state of TIM_FLAG (SET or RESET).
  2077. */
  2078. FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, uint16_t TIM_FLAG)
  2079. {
  2080. ITStatus bitstatus = RESET;
  2081. /* Check the parameters */
  2082. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2083. assert_param(IS_TIM_GET_FLAG(TIM_FLAG));
  2084. if ((TIMx->SR & TIM_FLAG) != (uint16_t)RESET)
  2085. {
  2086. bitstatus = SET;
  2087. }
  2088. else
  2089. {
  2090. bitstatus = RESET;
  2091. }
  2092. return bitstatus;
  2093. }
  2094. /**
  2095. * @brief Clears the TIMx's pending flags.
  2096. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2097. * @param TIM_FLAG: specifies the flag bit to clear.
  2098. * This parameter can be any combination of the following values:
  2099. * @arg TIM_FLAG_Update: TIM update Flag
  2100. * @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  2101. * @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  2102. * @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  2103. * @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  2104. * @arg TIM_FLAG_COM: TIM Commutation Flag
  2105. * @arg TIM_FLAG_Trigger: TIM Trigger Flag
  2106. * @arg TIM_FLAG_Break: TIM Break Flag
  2107. * @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 over capture Flag
  2108. * @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 over capture Flag
  2109. * @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 over capture Flag
  2110. * @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 over capture Flag
  2111. *
  2112. * @note TIM6 and TIM7 can have only one update flag.
  2113. * @note TIM_FLAG_COM and TIM_FLAG_Break are used only with TIM1 and TIM8.
  2114. *
  2115. * @retval None
  2116. */
  2117. void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG)
  2118. {
  2119. /* Check the parameters */
  2120. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2121. /* Clear the flags */
  2122. TIMx->SR = (uint16_t)~TIM_FLAG;
  2123. }
  2124. /**
  2125. * @brief Checks whether the TIM interrupt has occurred or not.
  2126. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2127. * @param TIM_IT: specifies the TIM interrupt source to check.
  2128. * This parameter can be one of the following values:
  2129. * @arg TIM_IT_Update: TIM update Interrupt source
  2130. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  2131. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  2132. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  2133. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  2134. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  2135. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  2136. * @arg TIM_IT_Break: TIM Break Interrupt source
  2137. *
  2138. * @note TIM6 and TIM7 can generate only an update interrupt.
  2139. * @note TIM_IT_COM and TIM_IT_Break are used only with TIM1 and TIM8.
  2140. *
  2141. * @retval The new state of the TIM_IT(SET or RESET).
  2142. */
  2143. ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT)
  2144. {
  2145. ITStatus bitstatus = RESET;
  2146. uint16_t itstatus = 0x0, itenable = 0x0;
  2147. /* Check the parameters */
  2148. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2149. assert_param(IS_TIM_GET_IT(TIM_IT));
  2150. itstatus = TIMx->SR & TIM_IT;
  2151. itenable = TIMx->DIER & TIM_IT;
  2152. if ((itstatus != (uint16_t)RESET) && (itenable != (uint16_t)RESET))
  2153. {
  2154. bitstatus = SET;
  2155. }
  2156. else
  2157. {
  2158. bitstatus = RESET;
  2159. }
  2160. return bitstatus;
  2161. }
  2162. /**
  2163. * @brief Clears the TIMx's interrupt pending bits.
  2164. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2165. * @param TIM_IT: specifies the pending bit to clear.
  2166. * This parameter can be any combination of the following values:
  2167. * @arg TIM_IT_Update: TIM1 update Interrupt source
  2168. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  2169. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  2170. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  2171. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  2172. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  2173. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  2174. * @arg TIM_IT_Break: TIM Break Interrupt source
  2175. *
  2176. * @note TIM6 and TIM7 can generate only an update interrupt.
  2177. * @note TIM_IT_COM and TIM_IT_Break are used only with TIM1 and TIM8.
  2178. *
  2179. * @retval None
  2180. */
  2181. void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT)
  2182. {
  2183. /* Check the parameters */
  2184. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2185. /* Clear the IT pending Bit */
  2186. TIMx->SR = (uint16_t)~TIM_IT;
  2187. }
  2188. /**
  2189. * @brief Configures the TIMx's DMA interface.
  2190. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2191. * @param TIM_DMABase: DMA Base address.
  2192. * This parameter can be one of the following values:
  2193. * @arg TIM_DMABase_CR1
  2194. * @arg TIM_DMABase_CR2
  2195. * @arg TIM_DMABase_SMCR
  2196. * @arg TIM_DMABase_DIER
  2197. * @arg TIM1_DMABase_SR
  2198. * @arg TIM_DMABase_EGR
  2199. * @arg TIM_DMABase_CCMR1
  2200. * @arg TIM_DMABase_CCMR2
  2201. * @arg TIM_DMABase_CCER
  2202. * @arg TIM_DMABase_CNT
  2203. * @arg TIM_DMABase_PSC
  2204. * @arg TIM_DMABase_ARR
  2205. * @arg TIM_DMABase_RCR
  2206. * @arg TIM_DMABase_CCR1
  2207. * @arg TIM_DMABase_CCR2
  2208. * @arg TIM_DMABase_CCR3
  2209. * @arg TIM_DMABase_CCR4
  2210. * @arg TIM_DMABase_BDTR
  2211. * @arg TIM_DMABase_DCR
  2212. * @param TIM_DMABurstLength: DMA Burst length. This parameter can be one value
  2213. * between: TIM_DMABurstLength_1Transfer and TIM_DMABurstLength_18Transfers.
  2214. * @retval None
  2215. */
  2216. void TIM_DMAConfig(TIM_TypeDef* TIMx, uint16_t TIM_DMABase, uint16_t TIM_DMABurstLength)
  2217. {
  2218. /* Check the parameters */
  2219. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2220. assert_param(IS_TIM_DMA_BASE(TIM_DMABase));
  2221. assert_param(IS_TIM_DMA_LENGTH(TIM_DMABurstLength));
  2222. /* Set the DMA Base and the DMA Burst Length */
  2223. TIMx->DCR = TIM_DMABase | TIM_DMABurstLength;
  2224. }
  2225. /**
  2226. * @brief Enables or disables the TIMx's DMA Requests.
  2227. * @param TIMx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the TIM peripheral.
  2228. * @param TIM_DMASource: specifies the DMA Request sources.
  2229. * This parameter can be any combination of the following values:
  2230. * @arg TIM_DMA_Update: TIM update Interrupt source
  2231. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  2232. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  2233. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  2234. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  2235. * @arg TIM_DMA_COM: TIM Commutation DMA source
  2236. * @arg TIM_DMA_Trigger: TIM Trigger DMA source
  2237. * @param NewState: new state of the DMA Request sources.
  2238. * This parameter can be: ENABLE or DISABLE.
  2239. * @retval None
  2240. */
  2241. void TIM_DMACmd(TIM_TypeDef* TIMx, uint16_t TIM_DMASource, FunctionalState NewState)
  2242. {
  2243. /* Check the parameters */
  2244. assert_param(IS_TIM_LIST5_PERIPH(TIMx));
  2245. assert_param(IS_TIM_DMA_SOURCE(TIM_DMASource));
  2246. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2247. if (NewState != DISABLE)
  2248. {
  2249. /* Enable the DMA sources */
  2250. TIMx->DIER |= TIM_DMASource;
  2251. }
  2252. else
  2253. {
  2254. /* Disable the DMA sources */
  2255. TIMx->DIER &= (uint16_t)~TIM_DMASource;
  2256. }
  2257. }
  2258. /**
  2259. * @brief Selects the TIMx peripheral Capture Compare DMA source.
  2260. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2261. * @param NewState: new state of the Capture Compare DMA source
  2262. * This parameter can be: ENABLE or DISABLE.
  2263. * @retval None
  2264. */
  2265. void TIM_SelectCCDMA(TIM_TypeDef* TIMx, FunctionalState NewState)
  2266. {
  2267. /* Check the parameters */
  2268. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2269. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2270. if (NewState != DISABLE)
  2271. {
  2272. /* Set the CCDS Bit */
  2273. TIMx->CR2 |= TIM_CR2_CCDS;
  2274. }
  2275. else
  2276. {
  2277. /* Reset the CCDS Bit */
  2278. TIMx->CR2 &= (uint16_t)~TIM_CR2_CCDS;
  2279. }
  2280. }
  2281. /**
  2282. * @}
  2283. */
  2284. /** @defgroup TIM_Group6 Clocks management functions
  2285. * @brief Clocks management functions
  2286. *
  2287. @verbatim
  2288. ===============================================================================
  2289. ##### Clocks management functions #####
  2290. ===============================================================================
  2291. @endverbatim
  2292. * @{
  2293. */
  2294. /**
  2295. * @brief Configures the TIMx internal Clock
  2296. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2297. * peripheral.
  2298. * @retval None
  2299. */
  2300. void TIM_InternalClockConfig(TIM_TypeDef* TIMx)
  2301. {
  2302. /* Check the parameters */
  2303. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2304. /* Disable slave mode to clock the prescaler directly with the internal clock */
  2305. TIMx->SMCR &= (uint16_t)~TIM_SMCR_SMS;
  2306. }
  2307. /**
  2308. * @brief Configures the TIMx Internal Trigger as External Clock
  2309. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2310. * peripheral.
  2311. * @param TIM_InputTriggerSource: Trigger source.
  2312. * This parameter can be one of the following values:
  2313. * @arg TIM_TS_ITR0: Internal Trigger 0
  2314. * @arg TIM_TS_ITR1: Internal Trigger 1
  2315. * @arg TIM_TS_ITR2: Internal Trigger 2
  2316. * @arg TIM_TS_ITR3: Internal Trigger 3
  2317. * @retval None
  2318. */
  2319. void TIM_ITRxExternalClockConfig(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)
  2320. {
  2321. /* Check the parameters */
  2322. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2323. assert_param(IS_TIM_INTERNAL_TRIGGER_SELECTION(TIM_InputTriggerSource));
  2324. /* Select the Internal Trigger */
  2325. TIM_SelectInputTrigger(TIMx, TIM_InputTriggerSource);
  2326. /* Select the External clock mode1 */
  2327. TIMx->SMCR |= TIM_SlaveMode_External1;
  2328. }
  2329. /**
  2330. * @brief Configures the TIMx Trigger as External Clock
  2331. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13 or 14
  2332. * to select the TIM peripheral.
  2333. * @param TIM_TIxExternalCLKSource: Trigger source.
  2334. * This parameter can be one of the following values:
  2335. * @arg TIM_TIxExternalCLK1Source_TI1ED: TI1 Edge Detector
  2336. * @arg TIM_TIxExternalCLK1Source_TI1: Filtered Timer Input 1
  2337. * @arg TIM_TIxExternalCLK1Source_TI2: Filtered Timer Input 2
  2338. * @param TIM_ICPolarity: specifies the TIx Polarity.
  2339. * This parameter can be one of the following values:
  2340. * @arg TIM_ICPolarity_Rising
  2341. * @arg TIM_ICPolarity_Falling
  2342. * @param ICFilter: specifies the filter value.
  2343. * This parameter must be a value between 0x0 and 0xF.
  2344. * @retval None
  2345. */
  2346. void TIM_TIxExternalClockConfig(TIM_TypeDef* TIMx, uint16_t TIM_TIxExternalCLKSource,
  2347. uint16_t TIM_ICPolarity, uint16_t ICFilter)
  2348. {
  2349. /* Check the parameters */
  2350. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  2351. assert_param(IS_TIM_IC_POLARITY(TIM_ICPolarity));
  2352. assert_param(IS_TIM_IC_FILTER(ICFilter));
  2353. /* Configure the Timer Input Clock Source */
  2354. if (TIM_TIxExternalCLKSource == TIM_TIxExternalCLK1Source_TI2)
  2355. {
  2356. TI2_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
  2357. }
  2358. else
  2359. {
  2360. TI1_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
  2361. }
  2362. /* Select the Trigger source */
  2363. TIM_SelectInputTrigger(TIMx, TIM_TIxExternalCLKSource);
  2364. /* Select the External clock mode1 */
  2365. TIMx->SMCR |= TIM_SlaveMode_External1;
  2366. }
  2367. /**
  2368. * @brief Configures the External clock Mode1
  2369. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2370. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2371. * This parameter can be one of the following values:
  2372. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2373. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2374. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2375. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2376. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2377. * This parameter can be one of the following values:
  2378. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2379. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2380. * @param ExtTRGFilter: External Trigger Filter.
  2381. * This parameter must be a value between 0x00 and 0x0F
  2382. * @retval None
  2383. */
  2384. void TIM_ETRClockMode1Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler,
  2385. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
  2386. {
  2387. uint16_t tmpsmcr = 0;
  2388. /* Check the parameters */
  2389. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2390. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2391. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2392. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2393. /* Configure the ETR Clock source */
  2394. TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  2395. /* Get the TIMx SMCR register value */
  2396. tmpsmcr = TIMx->SMCR;
  2397. /* Reset the SMS Bits */
  2398. tmpsmcr &= (uint16_t)~TIM_SMCR_SMS;
  2399. /* Select the External clock mode1 */
  2400. tmpsmcr |= TIM_SlaveMode_External1;
  2401. /* Select the Trigger selection : ETRF */
  2402. tmpsmcr &= (uint16_t)~TIM_SMCR_TS;
  2403. tmpsmcr |= TIM_TS_ETRF;
  2404. /* Write to TIMx SMCR */
  2405. TIMx->SMCR = tmpsmcr;
  2406. }
  2407. /**
  2408. * @brief Configures the External clock Mode2
  2409. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2410. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2411. * This parameter can be one of the following values:
  2412. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2413. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2414. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2415. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2416. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2417. * This parameter can be one of the following values:
  2418. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2419. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2420. * @param ExtTRGFilter: External Trigger Filter.
  2421. * This parameter must be a value between 0x00 and 0x0F
  2422. * @retval None
  2423. */
  2424. void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler,
  2425. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
  2426. {
  2427. /* Check the parameters */
  2428. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2429. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2430. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2431. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2432. /* Configure the ETR Clock source */
  2433. TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  2434. /* Enable the External clock mode2 */
  2435. TIMx->SMCR |= TIM_SMCR_ECE;
  2436. }
  2437. /**
  2438. * @}
  2439. */
  2440. /** @defgroup TIM_Group7 Synchronization management functions
  2441. * @brief Synchronization management functions
  2442. *
  2443. @verbatim
  2444. ===============================================================================
  2445. ##### Synchronization management functions #####
  2446. ===============================================================================
  2447. ##### TIM Driver: how to use it in synchronization Mode #####
  2448. ===============================================================================
  2449. [..]
  2450. *** Case of two/several Timers ***
  2451. ==================================
  2452. [..]
  2453. (#) Configure the Master Timers using the following functions:
  2454. (++) void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_TRGOSource);
  2455. (++) void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_MasterSlaveMode);
  2456. (#) Configure the Slave Timers using the following functions:
  2457. (++) void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource);
  2458. (++) void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode);
  2459. *** Case of Timers and external trigger(ETR pin) ***
  2460. ====================================================
  2461. [..]
  2462. (#) Configure the External trigger using this function:
  2463. (++) void TIM_ETRConfig(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
  2464. uint16_t ExtTRGFilter);
  2465. (#) Configure the Slave Timers using the following functions:
  2466. (++) void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource);
  2467. (++) void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode);
  2468. @endverbatim
  2469. * @{
  2470. */
  2471. /**
  2472. * @brief Selects the Input Trigger source
  2473. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13 or 14
  2474. * to select the TIM peripheral.
  2475. * @param TIM_InputTriggerSource: The Input Trigger source.
  2476. * This parameter can be one of the following values:
  2477. * @arg TIM_TS_ITR0: Internal Trigger 0
  2478. * @arg TIM_TS_ITR1: Internal Trigger 1
  2479. * @arg TIM_TS_ITR2: Internal Trigger 2
  2480. * @arg TIM_TS_ITR3: Internal Trigger 3
  2481. * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  2482. * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  2483. * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  2484. * @arg TIM_TS_ETRF: External Trigger input
  2485. * @retval None
  2486. */
  2487. void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)
  2488. {
  2489. uint16_t tmpsmcr = 0;
  2490. /* Check the parameters */
  2491. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  2492. assert_param(IS_TIM_TRIGGER_SELECTION(TIM_InputTriggerSource));
  2493. /* Get the TIMx SMCR register value */
  2494. tmpsmcr = TIMx->SMCR;
  2495. /* Reset the TS Bits */
  2496. tmpsmcr &= (uint16_t)~TIM_SMCR_TS;
  2497. /* Set the Input Trigger source */
  2498. tmpsmcr |= TIM_InputTriggerSource;
  2499. /* Write to TIMx SMCR */
  2500. TIMx->SMCR = tmpsmcr;
  2501. }
  2502. /**
  2503. * @brief Selects the TIMx Trigger Output Mode.
  2504. * @param TIMx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the TIM peripheral.
  2505. *
  2506. * @param TIM_TRGOSource: specifies the Trigger Output source.
  2507. * This parameter can be one of the following values:
  2508. *
  2509. * - For all TIMx
  2510. * @arg TIM_TRGOSource_Reset: The UG bit in the TIM_EGR register is used as the trigger output(TRGO)
  2511. * @arg TIM_TRGOSource_Enable: The Counter Enable CEN is used as the trigger output(TRGO)
  2512. * @arg TIM_TRGOSource_Update: The update event is selected as the trigger output(TRGO)
  2513. *
  2514. * - For all TIMx except TIM6 and TIM7
  2515. * @arg TIM_TRGOSource_OC1: The trigger output sends a positive pulse when the CC1IF flag
  2516. * is to be set, as soon as a capture or compare match occurs(TRGO)
  2517. * @arg TIM_TRGOSource_OC1Ref: OC1REF signal is used as the trigger output(TRGO)
  2518. * @arg TIM_TRGOSource_OC2Ref: OC2REF signal is used as the trigger output(TRGO)
  2519. * @arg TIM_TRGOSource_OC3Ref: OC3REF signal is used as the trigger output(TRGO)
  2520. * @arg TIM_TRGOSource_OC4Ref: OC4REF signal is used as the trigger output(TRGO)
  2521. *
  2522. * @retval None
  2523. */
  2524. void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_TRGOSource)
  2525. {
  2526. /* Check the parameters */
  2527. assert_param(IS_TIM_LIST5_PERIPH(TIMx));
  2528. assert_param(IS_TIM_TRGO_SOURCE(TIM_TRGOSource));
  2529. /* Reset the MMS Bits */
  2530. TIMx->CR2 &= (uint16_t)~TIM_CR2_MMS;
  2531. /* Select the TRGO source */
  2532. TIMx->CR2 |= TIM_TRGOSource;
  2533. }
  2534. /**
  2535. * @brief Selects the TIMx Slave Mode.
  2536. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM peripheral.
  2537. * @param TIM_SlaveMode: specifies the Timer Slave Mode.
  2538. * This parameter can be one of the following values:
  2539. * @arg TIM_SlaveMode_Reset: Rising edge of the selected trigger signal(TRGI) reinitialize
  2540. * the counter and triggers an update of the registers
  2541. * @arg TIM_SlaveMode_Gated: The counter clock is enabled when the trigger signal (TRGI) is high
  2542. * @arg TIM_SlaveMode_Trigger: The counter starts at a rising edge of the trigger TRGI
  2543. * @arg TIM_SlaveMode_External1: Rising edges of the selected trigger (TRGI) clock the counter
  2544. * @retval None
  2545. */
  2546. void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode)
  2547. {
  2548. /* Check the parameters */
  2549. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2550. assert_param(IS_TIM_SLAVE_MODE(TIM_SlaveMode));
  2551. /* Reset the SMS Bits */
  2552. TIMx->SMCR &= (uint16_t)~TIM_SMCR_SMS;
  2553. /* Select the Slave Mode */
  2554. TIMx->SMCR |= TIM_SlaveMode;
  2555. }
  2556. /**
  2557. * @brief Sets or Resets the TIMx Master/Slave Mode.
  2558. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM peripheral.
  2559. * @param TIM_MasterSlaveMode: specifies the Timer Master Slave Mode.
  2560. * This parameter can be one of the following values:
  2561. * @arg TIM_MasterSlaveMode_Enable: synchronization between the current timer
  2562. * and its slaves (through TRGO)
  2563. * @arg TIM_MasterSlaveMode_Disable: No action
  2564. * @retval None
  2565. */
  2566. void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_MasterSlaveMode)
  2567. {
  2568. /* Check the parameters */
  2569. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2570. assert_param(IS_TIM_MSM_STATE(TIM_MasterSlaveMode));
  2571. /* Reset the MSM Bit */
  2572. TIMx->SMCR &= (uint16_t)~TIM_SMCR_MSM;
  2573. /* Set or Reset the MSM Bit */
  2574. TIMx->SMCR |= TIM_MasterSlaveMode;
  2575. }
  2576. /**
  2577. * @brief Configures the TIMx External Trigger (ETR).
  2578. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2579. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2580. * This parameter can be one of the following values:
  2581. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2582. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2583. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2584. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2585. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2586. * This parameter can be one of the following values:
  2587. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2588. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2589. * @param ExtTRGFilter: External Trigger Filter.
  2590. * This parameter must be a value between 0x00 and 0x0F
  2591. * @retval None
  2592. */
  2593. void TIM_ETRConfig(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler,
  2594. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
  2595. {
  2596. uint16_t tmpsmcr = 0;
  2597. /* Check the parameters */
  2598. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2599. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2600. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2601. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2602. tmpsmcr = TIMx->SMCR;
  2603. /* Reset the ETR Bits */
  2604. tmpsmcr &= SMCR_ETR_MASK;
  2605. /* Set the Prescaler, the Filter value and the Polarity */
  2606. tmpsmcr |= (uint16_t)(TIM_ExtTRGPrescaler | (uint16_t)(TIM_ExtTRGPolarity | (uint16_t)(ExtTRGFilter << (uint16_t)8)));
  2607. /* Write to TIMx SMCR */
  2608. TIMx->SMCR = tmpsmcr;
  2609. }
  2610. /**
  2611. * @}
  2612. */
  2613. /** @defgroup TIM_Group8 Specific interface management functions
  2614. * @brief Specific interface management functions
  2615. *
  2616. @verbatim
  2617. ===============================================================================
  2618. ##### Specific interface management functions #####
  2619. ===============================================================================
  2620. @endverbatim
  2621. * @{
  2622. */
  2623. /**
  2624. * @brief Configures the TIMx Encoder Interface.
  2625. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2626. * peripheral.
  2627. * @param TIM_EncoderMode: specifies the TIMx Encoder Mode.
  2628. * This parameter can be one of the following values:
  2629. * @arg TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
  2630. * @arg TIM_EncoderMode_TI2: Counter counts on TI2FP2 edge depending on TI1FP1 level.
  2631. * @arg TIM_EncoderMode_TI12: Counter counts on both TI1FP1 and TI2FP2 edges depending
  2632. * on the level of the other input.
  2633. * @param TIM_IC1Polarity: specifies the IC1 Polarity
  2634. * This parameter can be one of the following values:
  2635. * @arg TIM_ICPolarity_Falling: IC Falling edge.
  2636. * @arg TIM_ICPolarity_Rising: IC Rising edge.
  2637. * @param TIM_IC2Polarity: specifies the IC2 Polarity
  2638. * This parameter can be one of the following values:
  2639. * @arg TIM_ICPolarity_Falling: IC Falling edge.
  2640. * @arg TIM_ICPolarity_Rising: IC Rising edge.
  2641. * @retval None
  2642. */
  2643. void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,
  2644. uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity)
  2645. {
  2646. uint16_t tmpsmcr = 0;
  2647. uint16_t tmpccmr1 = 0;
  2648. uint16_t tmpccer = 0;
  2649. /* Check the parameters */
  2650. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2651. assert_param(IS_TIM_ENCODER_MODE(TIM_EncoderMode));
  2652. assert_param(IS_TIM_IC_POLARITY(TIM_IC1Polarity));
  2653. assert_param(IS_TIM_IC_POLARITY(TIM_IC2Polarity));
  2654. /* Get the TIMx SMCR register value */
  2655. tmpsmcr = TIMx->SMCR;
  2656. /* Get the TIMx CCMR1 register value */
  2657. tmpccmr1 = TIMx->CCMR1;
  2658. /* Get the TIMx CCER register value */
  2659. tmpccer = TIMx->CCER;
  2660. /* Set the encoder Mode */
  2661. tmpsmcr &= (uint16_t)~TIM_SMCR_SMS;
  2662. tmpsmcr |= TIM_EncoderMode;
  2663. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  2664. tmpccmr1 &= ((uint16_t)~TIM_CCMR1_CC1S) & ((uint16_t)~TIM_CCMR1_CC2S);
  2665. tmpccmr1 |= TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
  2666. /* Set the TI1 and the TI2 Polarities */
  2667. tmpccer &= ((uint16_t)~TIM_CCER_CC1P) & ((uint16_t)~TIM_CCER_CC2P);
  2668. tmpccer |= (uint16_t)(TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << (uint16_t)4));
  2669. /* Write to TIMx SMCR */
  2670. TIMx->SMCR = tmpsmcr;
  2671. /* Write to TIMx CCMR1 */
  2672. TIMx->CCMR1 = tmpccmr1;
  2673. /* Write to TIMx CCER */
  2674. TIMx->CCER = tmpccer;
  2675. }
  2676. /**
  2677. * @brief Enables or disables the TIMx's Hall sensor interface.
  2678. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2679. * peripheral.
  2680. * @param NewState: new state of the TIMx Hall sensor interface.
  2681. * This parameter can be: ENABLE or DISABLE.
  2682. * @retval None
  2683. */
  2684. void TIM_SelectHallSensor(TIM_TypeDef* TIMx, FunctionalState NewState)
  2685. {
  2686. /* Check the parameters */
  2687. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2688. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2689. if (NewState != DISABLE)
  2690. {
  2691. /* Set the TI1S Bit */
  2692. TIMx->CR2 |= TIM_CR2_TI1S;
  2693. }
  2694. else
  2695. {
  2696. /* Reset the TI1S Bit */
  2697. TIMx->CR2 &= (uint16_t)~TIM_CR2_TI1S;
  2698. }
  2699. }
  2700. /**
  2701. * @}
  2702. */
  2703. /** @defgroup TIM_Group9 Specific remapping management function
  2704. * @brief Specific remapping management function
  2705. *
  2706. @verbatim
  2707. ===============================================================================
  2708. ##### Specific remapping management function #####
  2709. ===============================================================================
  2710. @endverbatim
  2711. * @{
  2712. */
  2713. /**
  2714. * @brief Configures the TIM2, TIM5 and TIM11 Remapping input capabilities.
  2715. * @param TIMx: where x can be 2, 5 or 11 to select the TIM peripheral.
  2716. * @param TIM_Remap: specifies the TIM input remapping source.
  2717. * This parameter can be one of the following values:
  2718. * @arg TIM2_TIM8_TRGO: TIM2 ITR1 input is connected to TIM8 Trigger output(default)
  2719. * @arg TIM2_ETH_PTP: TIM2 ITR1 input is connected to ETH PTP trigger output.
  2720. * @arg TIM2_USBFS_SOF: TIM2 ITR1 input is connected to USB FS SOF.
  2721. * @arg TIM2_USBHS_SOF: TIM2 ITR1 input is connected to USB HS SOF.
  2722. * @arg TIM5_GPIO: TIM5 CH4 input is connected to dedicated Timer pin(default)
  2723. * @arg TIM5_LSI: TIM5 CH4 input is connected to LSI clock.
  2724. * @arg TIM5_LSE: TIM5 CH4 input is connected to LSE clock.
  2725. * @arg TIM5_RTC: TIM5 CH4 input is connected to RTC Output event.
  2726. * @arg TIM11_GPIO: TIM11 CH4 input is connected to dedicated Timer pin(default)
  2727. * @arg TIM11_HSE: TIM11 CH4 input is connected to HSE_RTC clock
  2728. * (HSE divided by a programmable prescaler)
  2729. * @retval None
  2730. */
  2731. void TIM_RemapConfig(TIM_TypeDef* TIMx, uint16_t TIM_Remap)
  2732. {
  2733. /* Check the parameters */
  2734. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2735. assert_param(IS_TIM_REMAP(TIM_Remap));
  2736. /* Set the Timer remapping configuration */
  2737. TIMx->OR = TIM_Remap;
  2738. }
  2739. /**
  2740. * @}
  2741. */
  2742. /**
  2743. * @brief Configure the TI1 as Input.
  2744. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13 or 14
  2745. * to select the TIM peripheral.
  2746. * @param TIM_ICPolarity : The Input Polarity.
  2747. * This parameter can be one of the following values:
  2748. * @arg TIM_ICPolarity_Rising
  2749. * @arg TIM_ICPolarity_Falling
  2750. * @arg TIM_ICPolarity_BothEdge
  2751. * @param TIM_ICSelection: specifies the input to be used.
  2752. * This parameter can be one of the following values:
  2753. * @arg TIM_ICSelection_DirectTI: TIM Input 1 is selected to be connected to IC1.
  2754. * @arg TIM_ICSelection_IndirectTI: TIM Input 1 is selected to be connected to IC2.
  2755. * @arg TIM_ICSelection_TRC: TIM Input 1 is selected to be connected to TRC.
  2756. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2757. * This parameter must be a value between 0x00 and 0x0F.
  2758. * @retval None
  2759. */
  2760. static void TI1_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2761. uint16_t TIM_ICFilter)
  2762. {
  2763. uint16_t tmpccmr1 = 0, tmpccer = 0;
  2764. /* Disable the Channel 1: Reset the CC1E Bit */
  2765. TIMx->CCER &= (uint16_t)~TIM_CCER_CC1E;
  2766. tmpccmr1 = TIMx->CCMR1;
  2767. tmpccer = TIMx->CCER;
  2768. /* Select the Input and set the filter */
  2769. tmpccmr1 &= ((uint16_t)~TIM_CCMR1_CC1S) & ((uint16_t)~TIM_CCMR1_IC1F);
  2770. tmpccmr1 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));
  2771. /* Select the Polarity and set the CC1E Bit */
  2772. tmpccer &= (uint16_t)~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  2773. tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CCER_CC1E);
  2774. /* Write to TIMx CCMR1 and CCER registers */
  2775. TIMx->CCMR1 = tmpccmr1;
  2776. TIMx->CCER = tmpccer;
  2777. }
  2778. /**
  2779. * @brief Configure the TI2 as Input.
  2780. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2781. * peripheral.
  2782. * @param TIM_ICPolarity : The Input Polarity.
  2783. * This parameter can be one of the following values:
  2784. * @arg TIM_ICPolarity_Rising
  2785. * @arg TIM_ICPolarity_Falling
  2786. * @arg TIM_ICPolarity_BothEdge
  2787. * @param TIM_ICSelection: specifies the input to be used.
  2788. * This parameter can be one of the following values:
  2789. * @arg TIM_ICSelection_DirectTI: TIM Input 2 is selected to be connected to IC2.
  2790. * @arg TIM_ICSelection_IndirectTI: TIM Input 2 is selected to be connected to IC1.
  2791. * @arg TIM_ICSelection_TRC: TIM Input 2 is selected to be connected to TRC.
  2792. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2793. * This parameter must be a value between 0x00 and 0x0F.
  2794. * @retval None
  2795. */
  2796. static void TI2_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2797. uint16_t TIM_ICFilter)
  2798. {
  2799. uint16_t tmpccmr1 = 0, tmpccer = 0, tmp = 0;
  2800. /* Disable the Channel 2: Reset the CC2E Bit */
  2801. TIMx->CCER &= (uint16_t)~TIM_CCER_CC2E;
  2802. tmpccmr1 = TIMx->CCMR1;
  2803. tmpccer = TIMx->CCER;
  2804. tmp = (uint16_t)(TIM_ICPolarity << 4);
  2805. /* Select the Input and set the filter */
  2806. tmpccmr1 &= ((uint16_t)~TIM_CCMR1_CC2S) & ((uint16_t)~TIM_CCMR1_IC2F);
  2807. tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12);
  2808. tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8);
  2809. /* Select the Polarity and set the CC2E Bit */
  2810. tmpccer &= (uint16_t)~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  2811. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC2E);
  2812. /* Write to TIMx CCMR1 and CCER registers */
  2813. TIMx->CCMR1 = tmpccmr1 ;
  2814. TIMx->CCER = tmpccer;
  2815. }
  2816. /**
  2817. * @brief Configure the TI3 as Input.
  2818. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2819. * @param TIM_ICPolarity : The Input Polarity.
  2820. * This parameter can be one of the following values:
  2821. * @arg TIM_ICPolarity_Rising
  2822. * @arg TIM_ICPolarity_Falling
  2823. * @arg TIM_ICPolarity_BothEdge
  2824. * @param TIM_ICSelection: specifies the input to be used.
  2825. * This parameter can be one of the following values:
  2826. * @arg TIM_ICSelection_DirectTI: TIM Input 3 is selected to be connected to IC3.
  2827. * @arg TIM_ICSelection_IndirectTI: TIM Input 3 is selected to be connected to IC4.
  2828. * @arg TIM_ICSelection_TRC: TIM Input 3 is selected to be connected to TRC.
  2829. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2830. * This parameter must be a value between 0x00 and 0x0F.
  2831. * @retval None
  2832. */
  2833. static void TI3_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2834. uint16_t TIM_ICFilter)
  2835. {
  2836. uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2837. /* Disable the Channel 3: Reset the CC3E Bit */
  2838. TIMx->CCER &= (uint16_t)~TIM_CCER_CC3E;
  2839. tmpccmr2 = TIMx->CCMR2;
  2840. tmpccer = TIMx->CCER;
  2841. tmp = (uint16_t)(TIM_ICPolarity << 8);
  2842. /* Select the Input and set the filter */
  2843. tmpccmr2 &= ((uint16_t)~TIM_CCMR1_CC1S) & ((uint16_t)~TIM_CCMR2_IC3F);
  2844. tmpccmr2 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));
  2845. /* Select the Polarity and set the CC3E Bit */
  2846. tmpccer &= (uint16_t)~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
  2847. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC3E);
  2848. /* Write to TIMx CCMR2 and CCER registers */
  2849. TIMx->CCMR2 = tmpccmr2;
  2850. TIMx->CCER = tmpccer;
  2851. }
  2852. /**
  2853. * @brief Configure the TI4 as Input.
  2854. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2855. * @param TIM_ICPolarity : The Input Polarity.
  2856. * This parameter can be one of the following values:
  2857. * @arg TIM_ICPolarity_Rising
  2858. * @arg TIM_ICPolarity_Falling
  2859. * @arg TIM_ICPolarity_BothEdge
  2860. * @param TIM_ICSelection: specifies the input to be used.
  2861. * This parameter can be one of the following values:
  2862. * @arg TIM_ICSelection_DirectTI: TIM Input 4 is selected to be connected to IC4.
  2863. * @arg TIM_ICSelection_IndirectTI: TIM Input 4 is selected to be connected to IC3.
  2864. * @arg TIM_ICSelection_TRC: TIM Input 4 is selected to be connected to TRC.
  2865. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2866. * This parameter must be a value between 0x00 and 0x0F.
  2867. * @retval None
  2868. */
  2869. static void TI4_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2870. uint16_t TIM_ICFilter)
  2871. {
  2872. uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2873. /* Disable the Channel 4: Reset the CC4E Bit */
  2874. TIMx->CCER &= (uint16_t)~TIM_CCER_CC4E;
  2875. tmpccmr2 = TIMx->CCMR2;
  2876. tmpccer = TIMx->CCER;
  2877. tmp = (uint16_t)(TIM_ICPolarity << 12);
  2878. /* Select the Input and set the filter */
  2879. tmpccmr2 &= ((uint16_t)~TIM_CCMR1_CC2S) & ((uint16_t)~TIM_CCMR1_IC2F);
  2880. tmpccmr2 |= (uint16_t)(TIM_ICSelection << 8);
  2881. tmpccmr2 |= (uint16_t)(TIM_ICFilter << 12);
  2882. /* Select the Polarity and set the CC4E Bit */
  2883. tmpccer &= (uint16_t)~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
  2884. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC4E);
  2885. /* Write to TIMx CCMR2 and CCER registers */
  2886. TIMx->CCMR2 = tmpccmr2;
  2887. TIMx->CCER = tmpccer ;
  2888. }
  2889. /**
  2890. * @}
  2891. */
  2892. /**
  2893. * @}
  2894. */
  2895. /**
  2896. * @}
  2897. */