HAL_tim.c 96 KB

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