stm32f10x_tim.c 105 KB

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