hal_tim.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_tim.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE TIM FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_TIM_C_
  19. // Files includes
  20. #include "hal_rcc.h"
  21. #include "hal_tim.h"
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /// @addtogroup MM32_Hardware_Abstract_Layer
  24. /// @{
  25. ////////////////////////////////////////////////////////////////////////////////
  26. /// @addtogroup TIM_HAL
  27. /// @{
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /// @addtogroup TIM_Exported_Functions
  30. /// @{
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /// @brief Deinitializes the tim peripheral registers to their default reset values.
  33. /// @param tim: select the TIM peripheral.
  34. /// @retval None.
  35. ////////////////////////////////////////////////////////////////////////////////
  36. void TIM_DeInit(TIM_TypeDef* tim)
  37. {
  38. switch (*(vu32*)&tim) {
  39. case (u32)TIM1:
  40. exRCC_APB2PeriphReset(RCC_APB2ENR_TIM1);
  41. break;
  42. case (u32)TIM2:
  43. exRCC_APB1PeriphReset(RCC_APB1ENR_TIM2);
  44. break;
  45. case (u32)TIM3:
  46. exRCC_APB1PeriphReset(RCC_APB1ENR_TIM3);
  47. break;
  48. case (u32)TIM4:
  49. exRCC_APB1PeriphReset(RCC_APB1ENR_TIM4);
  50. break;
  51. case (u32)TIM5:
  52. exRCC_APB1PeriphReset(RCC_APB1ENR_TIM5);
  53. break;
  54. case (u32)TIM6:
  55. exRCC_APB1PeriphReset(RCC_APB1ENR_TIM6);
  56. break;
  57. case (u32)TIM7:
  58. exRCC_APB1PeriphReset(RCC_APB1ENR_TIM7);
  59. break;
  60. case (u32)TIM8:
  61. exRCC_APB2PeriphReset(RCC_APB2ENR_TIM8);
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. ////////////////////////////////////////////////////////////////////////////////
  68. /// @brief Initializes the tim Time Base Unit peripheral according to
  69. /// the specified parameters in the init_struct.
  70. /// @param tim: select the TIM peripheral.
  71. /// @param init_struct: pointer to a TIM_TimeBaseInitTypeDef
  72. /// structure that contains the configuration information for the
  73. /// specified TIM peripheral.
  74. /// @retval None.
  75. ////////////////////////////////////////////////////////////////////////////////
  76. void TIM_TimeBaseInit(TIM_TypeDef* tim, TIM_TimeBaseInitTypeDef* init_struct)
  77. {
  78. MODIFY_REG(tim->CR1, TIM_CR1_CKD, init_struct->TIM_ClockDivision);
  79. if ((tim == TIM1) || (tim == TIM2) || (tim == TIM3) || (tim == TIM4) || (tim == TIM5) || (tim == TIM8))
  80. MODIFY_REG(tim->CR1, TIM_CR1_CMS | TIM_CR1_DIR, init_struct->TIM_CounterMode);
  81. if ((tim == TIM1) || (tim == TIM8) )
  82. MODIFY_REG(tim->RCR, TIM_RCR_REP, init_struct->TIM_RepetitionCounter);
  83. WRITE_REG(tim->ARR, init_struct->TIM_Period);
  84. WRITE_REG(tim->PSC, init_struct->TIM_Prescaler);
  85. WRITE_REG(tim->EGR, TIM_PSCReloadMode_Immediate);
  86. }
  87. ////////////////////////////////////////////////////////////////////////////////
  88. /// @brief Initializes the tim Channel1 according to the specified
  89. /// parameters in the init_struct.
  90. /// @param tim: select the TIM peripheral.
  91. /// @param init_struct: pointer to a TIM_OCInitTypeDef structure that
  92. /// contains the configuration information for the specified TIM peripheral.
  93. /// @retval None.
  94. ////////////////////////////////////////////////////////////////////////////////
  95. void TIM_OC1Init(TIM_TypeDef* tim, TIM_OCInitTypeDef* init_struct)
  96. {
  97. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC1M, init_struct->TIM_OCMode);
  98. MODIFY_REG(tim->CCER, TIM_CCER_CC1P | TIM_CCER_CC1EN, \
  99. ((u32)init_struct->TIM_OCPolarity) | ((u32)init_struct->TIM_OutputState));
  100. WRITE_REG(tim->CCR1, init_struct->TIM_Pulse);
  101. if ((tim == TIM1) || (tim == TIM8)) {
  102. MODIFY_REG(tim->CCER, TIM_CCER_CC1NP | TIM_CCER_CC1NEN, \
  103. ((u32)init_struct->TIM_OCNPolarity) | ((u32)init_struct->TIM_OutputNState));
  104. MODIFY_REG(tim->CR2, TIM_CR2_OIS1 | TIM_CR2_OIS1N, \
  105. ((u32)init_struct->TIM_OCIdleState) | ((u32)init_struct->TIM_OCNIdleState));
  106. }
  107. }
  108. ////////////////////////////////////////////////////////////////////////////////
  109. /// @brief Initializes the tim Channel2 according to the specified
  110. /// parameters in the init_struct.
  111. /// @param tim: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  112. /// @param init_struct: pointer to a TIM_OCInitTypeDef structure that
  113. /// contains the configuration information for the specified TIM peripheral.
  114. /// @retval None.
  115. ////////////////////////////////////////////////////////////////////////////////
  116. void TIM_OC2Init(TIM_TypeDef* tim, TIM_OCInitTypeDef* init_struct)
  117. {
  118. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC2M, init_struct->TIM_OCMode << 8);
  119. MODIFY_REG(tim->CCER, TIM_CCER_CC2EN | TIM_CCER_CC2P, \
  120. (init_struct->TIM_OCPolarity << 4) | (init_struct->TIM_OutputState << 4));
  121. WRITE_REG(tim->CCR2, init_struct->TIM_Pulse);
  122. if ((tim == TIM1) || (tim == TIM8)) {
  123. MODIFY_REG(tim->CCER, TIM_CCER_CC2NP | TIM_CCER_CC2NEN, \
  124. (init_struct->TIM_OCNPolarity << 4) | (init_struct->TIM_OutputNState << 4));
  125. MODIFY_REG(tim->CR2, TIM_CR2_OIS2 | TIM_CR2_OIS2N, \
  126. (init_struct->TIM_OCIdleState << 2) | (init_struct->TIM_OCNIdleState << 2));
  127. }
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////
  130. /// @brief Initializes the tim Channel3 according to the specified
  131. /// parameters in the init_struct.
  132. /// @param tim: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  133. /// @param init_struct: pointer to a TIM_OCInitTypeDef structure that
  134. /// contains the configuration information for the specified TIM peripheral.
  135. /// @retval None.
  136. ////////////////////////////////////////////////////////////////////////////////
  137. void TIM_OC3Init(TIM_TypeDef* tim, TIM_OCInitTypeDef* init_struct)
  138. {
  139. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC3M, init_struct->TIM_OCMode);
  140. MODIFY_REG(tim->CCER, TIM_CCER_CC3EN | TIM_CCER_CC3P, \
  141. (init_struct->TIM_OCPolarity << 8) | (init_struct->TIM_OutputState << 8));
  142. WRITE_REG(tim->CCR3, init_struct->TIM_Pulse);
  143. if ((tim == TIM1) || (tim == TIM8)) {
  144. MODIFY_REG(tim->CCER, TIM_CCER_CC3NP | TIM_CCER_CC3NEN, \
  145. (init_struct->TIM_OCNPolarity << 8) | (init_struct->TIM_OutputNState << 8));
  146. MODIFY_REG(tim->CR2, TIM_CR2_OIS3 | TIM_CR2_OIS3N, \
  147. (init_struct->TIM_OCIdleState << 4) | (init_struct->TIM_OCNIdleState << 4));
  148. }
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////
  151. /// @brief Initializes the tim Channel4 according to the specified
  152. /// parameters in the init_struct.
  153. /// @param tim:select the TIM peripheral.
  154. /// @param init_struct: pointer to a TIM_OCInitTypeDef structure that
  155. /// contains the configuration information for the specified TIM peripheral.
  156. /// @retval None.
  157. ////////////////////////////////////////////////////////////////////////////////
  158. void TIM_OC4Init(TIM_TypeDef* tim, TIM_OCInitTypeDef* init_struct)
  159. {
  160. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC4M, (init_struct->TIM_OCMode) << 8);
  161. MODIFY_REG(tim->CCER, TIM_CCER_CC4EN | TIM_CCER_CC4P, \
  162. (init_struct->TIM_OCPolarity << 12) | (init_struct->TIM_OutputState << 12));
  163. WRITE_REG(tim->CCR4, init_struct->TIM_Pulse);
  164. if ((tim == TIM1) || (tim == TIM8))
  165. MODIFY_REG(tim->CR2, TIM_CR2_OIS4, init_struct->TIM_OCIdleState << 6);
  166. }
  167. ////////////////////////////////////////////////////////////////////////////////
  168. /// @brief Initializes the TIM peripheral according to the specified
  169. /// parameters in the init_struct.
  170. /// @param tim: select the TIM peripheral.
  171. /// @param init_struct: pointer to a TIM_ICInitTypeDef structure that
  172. /// contains the configuration information for the specified TIM peripheral.
  173. /// @retval None.
  174. ////////////////////////////////////////////////////////////////////////////////
  175. void TIM_ICInit(TIM_TypeDef* tim, TIM_ICInitTypeDef* init_struct)
  176. {
  177. switch (init_struct->TIM_Channel) {
  178. case TIM_Channel_1:
  179. TI1_Configure(tim, init_struct->TIM_ICPolarity, init_struct->TIM_ICSelection, init_struct->TIM_ICFilter);
  180. TIM_SetIC1Prescaler(tim, init_struct->TIM_ICPrescaler);
  181. break;
  182. case TIM_Channel_2:
  183. TI2_Configure(tim, init_struct->TIM_ICPolarity, init_struct->TIM_ICSelection, init_struct->TIM_ICFilter);
  184. TIM_SetIC2Prescaler(tim, init_struct->TIM_ICPrescaler);
  185. break;
  186. case TIM_Channel_3:
  187. TI3_Configure(tim, init_struct->TIM_ICPolarity, init_struct->TIM_ICSelection, init_struct->TIM_ICFilter);
  188. TIM_SetIC3Prescaler(tim, init_struct->TIM_ICPrescaler);
  189. break;
  190. case TIM_Channel_4:
  191. TI4_Configure(tim, init_struct->TIM_ICPolarity, init_struct->TIM_ICSelection, init_struct->TIM_ICFilter);
  192. TIM_SetIC4Prescaler(tim, init_struct->TIM_ICPrescaler);
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. ////////////////////////////////////////////////////////////////////////////////
  199. /// @brief Configures the TIM peripheral according to the specified
  200. /// parameters in the init_struct to measure an external PWM signal.
  201. /// @param tim: select the TIM peripheral.
  202. /// @param init_struct: pointer to a TIM_ICInitTypeDef structure that
  203. /// contains the configuration information for the specified TIM peripheral.
  204. /// @retval None.
  205. ////////////////////////////////////////////////////////////////////////////////
  206. void TIM_PWMIConfig(TIM_TypeDef* tim, TIM_ICInitTypeDef* init_struct)
  207. {
  208. u16 icoppositepolarity = TIM_ICPolarity_Rising;
  209. u16 icoppositeselection = TIM_ICSelection_DirectTI;
  210. icoppositepolarity = (init_struct->TIM_ICPolarity == TIM_ICPolarity_Rising) ? TIM_ICPolarity_Falling : TIM_ICPolarity_Rising;
  211. icoppositeselection =
  212. (init_struct->TIM_ICSelection == TIM_ICSelection_DirectTI) ? TIM_ICSelection_IndirectTI : TIM_ICSelection_DirectTI;
  213. if (init_struct->TIM_Channel == TIM_Channel_1) {
  214. TI1_Configure(tim, init_struct->TIM_ICPolarity, init_struct->TIM_ICSelection, init_struct->TIM_ICFilter);
  215. TIM_SetIC1Prescaler(tim, init_struct->TIM_ICPrescaler);
  216. TI2_Configure(tim, icoppositepolarity, icoppositeselection, init_struct->TIM_ICFilter);
  217. TIM_SetIC2Prescaler(tim, init_struct->TIM_ICPrescaler);
  218. }
  219. else {
  220. TI2_Configure(tim, init_struct->TIM_ICPolarity, init_struct->TIM_ICSelection, init_struct->TIM_ICFilter);
  221. TIM_SetIC2Prescaler(tim, init_struct->TIM_ICPrescaler);
  222. TI1_Configure(tim, icoppositepolarity, icoppositeselection, init_struct->TIM_ICFilter);
  223. TIM_SetIC1Prescaler(tim, init_struct->TIM_ICPrescaler);
  224. }
  225. }
  226. ////////////////////////////////////////////////////////////////////////////////
  227. /// @brief Configures the: Break feature, dead time, Lock level, the OSSI,
  228. /// the OSSR State and the AOE(automatic output enable).
  229. /// @param tim: select the TIM
  230. /// @param init_struct: pointer to a TIM_BDTRInitTypeDef structure that
  231. /// contains the BDTR Register configuration information for the TIM peripheral.
  232. /// @retval None.
  233. ////////////////////////////////////////////////////////////////////////////////
  234. void TIM_BDTRConfig(TIM_TypeDef* tim, TIM_BDTRInitTypeDef* init_struct)
  235. {
  236. tim->BDTR = (u32)init_struct->TIM_OSSRState | init_struct->TIM_OSSIState | init_struct->TIM_LOCKLevel |
  237. init_struct->TIM_DeadTime | init_struct->TIM_Break | init_struct->TIM_BreakPolarity |
  238. init_struct->TIM_AutomaticOutput;
  239. }
  240. ////////////////////////////////////////////////////////////////////////////////
  241. /// @brief Fills each init_struct member with its default value.
  242. /// @param init_struct : pointer to a TIM_TimeBaseInitTypeDef
  243. /// structure which will be initialized.
  244. /// @retval None.
  245. ////////////////////////////////////////////////////////////////////////////////
  246. void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* init_struct)
  247. {
  248. init_struct->TIM_Period = 0xFFFFFFFF;
  249. init_struct->TIM_Prescaler = 0x0000;
  250. init_struct->TIM_ClockDivision = TIM_CKD_DIV1;
  251. init_struct->TIM_CounterMode = TIM_CounterMode_Up;
  252. init_struct->TIM_RepetitionCounter = 0x00;
  253. }
  254. ////////////////////////////////////////////////////////////////////////////////
  255. /// @brief Fills each init_struct member with its default value.
  256. /// @param init_struct : pointer to a TIM_OCInitTypeDef structure which will
  257. /// be initialized.
  258. /// @retval None.
  259. ////////////////////////////////////////////////////////////////////////////////
  260. void TIM_OCStructInit(TIM_OCInitTypeDef* init_struct)
  261. {
  262. init_struct->TIM_OCMode = TIM_OCMode_Timing;
  263. init_struct->TIM_OutputState = TIM_OutputState_Disable;
  264. init_struct->TIM_OutputNState = TIM_OutputNState_Disable;
  265. init_struct->TIM_Pulse = 0x00000000;
  266. init_struct->TIM_OCPolarity = TIM_OCPolarity_High;
  267. init_struct->TIM_OCNPolarity = TIM_OCNPolarity_High;
  268. init_struct->TIM_OCIdleState = TIM_OCIdleState_Reset;
  269. init_struct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  270. }
  271. ////////////////////////////////////////////////////////////////////////////////
  272. /// @brief Fills each init_struct member with its default value.
  273. /// @param init_struct: pointer to a TIM_ICInitTypeDef structure which will
  274. /// be initialized.
  275. /// @retval None.
  276. ////////////////////////////////////////////////////////////////////////////////
  277. void TIM_ICStructInit(TIM_ICInitTypeDef* init_struct)
  278. {
  279. init_struct->TIM_Channel = TIM_Channel_1;
  280. init_struct->TIM_ICPolarity = TIM_ICPolarity_Rising;
  281. init_struct->TIM_ICSelection = TIM_ICSelection_DirectTI;
  282. init_struct->TIM_ICPrescaler = TIM_ICPSC_DIV1;
  283. init_struct->TIM_ICFilter = 0x00;
  284. }
  285. ////////////////////////////////////////////////////////////////////////////////
  286. /// @brief Fills each init_struct member with its default value.
  287. /// @param init_struct: pointer to a TIM_BDTRInitTypeDef structure which
  288. /// will be initialized.
  289. /// @retval None.
  290. ////////////////////////////////////////////////////////////////////////////////
  291. void TIM_BDTRStructInit(TIM_BDTRInitTypeDef* init_struct)
  292. {
  293. init_struct->TIM_OSSRState = TIM_OSSRState_Disable;
  294. init_struct->TIM_OSSIState = TIM_OSSIState_Disable;
  295. init_struct->TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  296. init_struct->TIM_DeadTime = 0x00;
  297. init_struct->TIM_Break = TIM_Break_Disable;
  298. init_struct->TIM_BreakPolarity = TIM_BreakPolarity_Low;
  299. init_struct->TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
  300. }
  301. ////////////////////////////////////////////////////////////////////////////////
  302. /// @brief Enables or disables the specified TIM peripheral.
  303. /// @param tim: where x can be 1 to 17 to select the tim peripheral.
  304. /// @param state: new state of the tim peripheral.
  305. /// This parameter can be: ENABLE or DISABLE.
  306. /// @retval None.
  307. ////////////////////////////////////////////////////////////////////////////////
  308. void TIM_Cmd(TIM_TypeDef* tim, FunctionalState state)
  309. {
  310. (state) ? SET_BIT(tim->CR1, TIM_CR1_CEN) : CLEAR_BIT(tim->CR1, TIM_CR1_CEN);
  311. }
  312. ////////////////////////////////////////////////////////////////////////////////
  313. /// @brief Enables or disables the TIM peripheral Main Outputs.
  314. /// @param tim: where x can be 1, 8, 16 or 17 to select the tim peripheral.
  315. /// @param state: new state of the TIM peripheral Main Outputs.
  316. /// This parameter can be: ENABLE or DISABLE.
  317. /// @retval None.
  318. ////////////////////////////////////////////////////////////////////////////////
  319. void TIM_CtrlPWMOutputs(TIM_TypeDef* tim, FunctionalState state)
  320. {
  321. (state) ? SET_BIT(tim->BDTR, TIM_BDTR_MOEN) : CLEAR_BIT(tim->BDTR, TIM_BDTR_MOEN);
  322. }
  323. ////////////////////////////////////////////////////////////////////////////////
  324. /// @brief Enables or disables the specified TIM interrupts.
  325. /// @param tim: select the tim peripheral.
  326. /// @param it: specifies the TIM interrupts sources to be enabled or disabled.
  327. /// This parameter can be any combination of the following values:
  328. /// @arg TIM_IT_Update: TIM update Interrupt source
  329. /// @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  330. /// @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  331. /// @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  332. /// @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  333. /// @arg TIM_IT_COM: TIM Commutation Interrupt source
  334. /// @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  335. /// @arg TIM_IT_Break: TIM Break Interrupt source
  336. /// @note
  337. /// - Partial timer can have TIM_IT_Update or TIM_IT_CC1.
  338. /// - TIM_IT_Break is used only with partial timer.
  339. /// - TIM_IT_COM is used only with partial timer.
  340. /// @param state: new state of the TIM interrupts.
  341. /// This parameter can be: ENABLE or DISABLE.
  342. /// @retval None.
  343. ////////////////////////////////////////////////////////////////////////////////
  344. void TIM_ITConfig(TIM_TypeDef* tim, u32 it, FunctionalState state) //TIMIT_TypeDef
  345. {
  346. (state) ? SET_BIT(tim->DIER, it) : CLEAR_BIT(tim->DIER, it);
  347. }
  348. ////////////////////////////////////////////////////////////////////////////////
  349. /// @brief Configures the tim event to be generate by software.
  350. /// @param tim: select the TIM peripheral.
  351. /// @param source: specifies the event source.
  352. /// This parameter can be one or more of the following values:
  353. /// @arg TIM_EventSource_Update: Timer update Event source
  354. /// @arg TIM_EventSource_CC1: Timer Capture Compare 1 Event source
  355. /// @arg TIM_EventSource_CC2: Timer Capture Compare 2 Event source
  356. /// @arg TIM_EventSource_CC3: Timer Capture Compare 3 Event source
  357. /// @arg TIM_EventSource_CC4: Timer Capture Compare 4 Event source
  358. /// @arg TIM_EventSource_COM: Timer COM event source
  359. /// @arg TIM_EventSource_Trigger: Timer Trigger Event source
  360. /// @arg TIM_EventSource_Break: Timer Break event source
  361. /// @note
  362. /// - TIM_EventSource_COM and TIM_EventSource_Break are used only with partial timer.
  363. /// @retval None.
  364. ////////////////////////////////////////////////////////////////////////////////
  365. void TIM_GenerateEvent(TIM_TypeDef* tim, TIMEGR_Typedef source)
  366. {
  367. WRITE_REG(tim->EGR, source);
  368. }
  369. ////////////////////////////////////////////////////////////////////////////////
  370. /// @brief Configures the tim's DMA interface.
  371. /// @param tim: select the TIM peripheral.
  372. /// @param dma_base: DMA Base address.
  373. /// This parameter can be one of the following values:
  374. /// @arg TIM_DMABase_CR, TIM_DMABase_CR2, TIM_DMABase_SMCR,
  375. /// TIM_DMABase_DIER, TIM1_DMABase_SR, TIM_DMABase_EGR,
  376. /// TIM_DMABase_CCMR1, TIM_DMABase_CCMR2, TIM_DMABase_CCER,
  377. /// TIM_DMABase_CNT, TIM_DMABase_PSC, TIM_DMABase_ARR,
  378. /// TIM_DMABase_RCR, TIM_DMABase_CCR1, TIM_DMABase_CCR2,
  379. /// TIM_DMABase_CCR3, TIM_DMABase_CCR4, TIM_DMABase_BDTR,
  380. /// TIM_DMABase_DCR.
  381. /// @param length: DMA Burst length.
  382. /// This parameter can be one value between:
  383. /// TIM_DMABurstLength_1Transfer and TIM_DMABurstLength_18Transfers.
  384. /// @retval None.
  385. ////////////////////////////////////////////////////////////////////////////////
  386. void TIM_DMAConfig(TIM_TypeDef* tim, TIMDMABASE_Typedef dma_base, TIMDMABURSTLENGTH_Typedef length)
  387. {
  388. WRITE_REG(tim->DCR, ((u32)dma_base) | ((u32)length));
  389. }
  390. ////////////////////////////////////////////////////////////////////////////////
  391. /// @brief Enables or disables the tim's DMA Requests.
  392. /// @param tim: select the TIM peripheral.
  393. /// @param source: specifies the DMA Request sources.
  394. /// This parameter can be any combination of the following values:
  395. /// @arg TIM_DMA_Update: TIM update Interrupt source
  396. /// @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  397. /// @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  398. /// @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  399. /// @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  400. /// @arg TIM_DMA_COM: TIM Commutation DMA source
  401. /// @arg TIM_DMA_Trigger: TIM Trigger DMA source
  402. /// @param state: new state of the DMA Request sources.
  403. /// This parameter can be: ENABLE or DISABLE.
  404. /// @retval None.
  405. ////////////////////////////////////////////////////////////////////////////////
  406. void TIM_DMACmd(TIM_TypeDef* tim, TIMDMASRC_Typedef source, FunctionalState state)
  407. {
  408. (state) ? SET_BIT(tim->DIER, source) : CLEAR_BIT(tim->DIER, source);
  409. }
  410. ////////////////////////////////////////////////////////////////////////////////
  411. /// @brief Configures the tim internal Clock
  412. /// @param tim: select the TIM peripheral.
  413. /// @retval None.
  414. ////////////////////////////////////////////////////////////////////////////////
  415. void TIM_InternalClockConfig(TIM_TypeDef* tim)
  416. {
  417. CLEAR_BIT(tim->SMCR, TIM_SMCR_SMS);
  418. }
  419. ////////////////////////////////////////////////////////////////////////////////
  420. /// @brief Configures the tim Internal Trigger as External Clock
  421. /// @param tim: select the TIM peripheral.
  422. /// @param source: Trigger source.
  423. /// This parameter can be one of the following values:
  424. /// @arg TIM_TS_ITR0: Internal Trigger 0
  425. /// @arg TIM_TS_ITR1: Internal Trigger 1
  426. /// @arg TIM_TS_ITR2: Internal Trigger 2
  427. /// @arg TIM_TS_ITR3: Internal Trigger 3
  428. /// @retval None.
  429. ////////////////////////////////////////////////////////////////////////////////
  430. void TIM_ITRxExternalClockConfig(TIM_TypeDef* tim, TIMTS_TypeDef source)
  431. {
  432. TIM_SelectInputTrigger(tim, source);
  433. SET_BIT(tim->SMCR, TIM_SlaveMode_External1);
  434. }
  435. ////////////////////////////////////////////////////////////////////////////////
  436. /// @brief Configures the tim Trigger as External Clock
  437. /// @param tim: select the TIM peripheral.
  438. /// @param source: Trigger source.
  439. /// This parameter can be one of the following values:
  440. /// @arg TIM_TIxExternalCLK1Source_TI1ED: TI1 Edge Detector
  441. /// @arg TIM_TIxExternalCLK1Source_TI1: Filtered Timer Input 1
  442. /// @arg TIM_TIxExternalCLK1Source_TI2: Filtered Timer Input 2
  443. /// @param polarity: specifies the TIx Polarity.
  444. /// This parameter can be one of the following values:
  445. /// @arg TIM_ICPolarity_Rising
  446. /// @arg TIM_ICPolarity_Falling
  447. /// @param filter : specifies the filter value.
  448. /// This parameter must be a value between 0x0 and 0xF.
  449. /// @retval None.
  450. ////////////////////////////////////////////////////////////////////////////////
  451. void TIM_TIxExternalClockConfig(TIM_TypeDef* tim, TIM_TIEXTCLKSRC_Typedef source, TIMICP_Typedef polarity, u16 filter)
  452. {
  453. (source == TIM_TIxExternalCLK1Source_TI2) ? (TI2_Configure(tim, polarity, TIM_ICSelection_DirectTI, filter))
  454. : (TI1_Configure(tim, polarity, TIM_ICSelection_DirectTI, filter));
  455. TIM_SelectInputTrigger(tim, (TIMTS_TypeDef)source);
  456. SET_BIT(tim->SMCR, TIM_SlaveMode_External1);
  457. }
  458. ////////////////////////////////////////////////////////////////////////////////
  459. /// @brief Configures the tim External Trigger (ETR).
  460. /// @param tim: select the TIM peripheral.
  461. /// @param psc: The external Trigger Prescaler.
  462. /// This parameter can be one of the following values:
  463. /// @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  464. /// @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  465. /// @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  466. /// @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  467. /// @param polarity: The external Trigger Polarity.
  468. /// This parameter can be one of the following values:
  469. /// @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  470. /// @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  471. /// @param filter: External Trigger Filter.
  472. /// This parameter must be a value between 0x00 and 0x0F
  473. /// @retval None.
  474. ////////////////////////////////////////////////////////////////////////////////
  475. void TIM_ETRConfig(TIM_TypeDef* tim, TIMEXTTRGPSC_Typedef psc, TIMETP_Typedef polarity, u16 filter)
  476. {
  477. CLEAR_BIT(tim->SMCR, TIM_SMCR_ECEN);
  478. MODIFY_REG(tim->SMCR, TIM_SMCR_ETP, polarity);
  479. MODIFY_REG(tim->SMCR, TIM_SMCR_ETPS, psc);
  480. MODIFY_REG(tim->SMCR, TIM_SMCR_ETF, filter << 8);
  481. }
  482. ////////////////////////////////////////////////////////////////////////////////
  483. /// @brief Configures the External clock Mode1
  484. /// @param tim: select the TIM peripheral.
  485. /// @param psc: The external Trigger Prescaler.
  486. /// This parameter can be one of the following values:
  487. /// @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  488. /// @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  489. /// @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  490. /// @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  491. /// @param polarity: The external Trigger Polarity.
  492. /// This parameter can be one of the following values:
  493. /// @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  494. /// @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  495. /// @param filter: External Trigger Filter.
  496. /// This parameter must be a value between 0x00 and 0x0F
  497. /// @retval None.
  498. ////////////////////////////////////////////////////////////////////////////////
  499. void TIM_ETRClockMode1Config(TIM_TypeDef* tim, TIMEXTTRGPSC_Typedef psc, TIMETP_Typedef polarity, u16 filter)
  500. {
  501. TIM_ETRConfig(tim, psc, polarity, filter);
  502. MODIFY_REG(tim->SMCR, TIM_SMCR_TS | TIM_SMCR_SMS, ((u32)TIM_TS_ETRF) | ((u32)TIM_SlaveMode_External1));
  503. }
  504. ////////////////////////////////////////////////////////////////////////////////
  505. /// @brief Configures the External clock Mode2
  506. /// @param tim: select the TIM peripheral.
  507. /// @param psc: The external Trigger Prescaler.
  508. /// This parameter can be one of the following values:
  509. /// @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  510. /// @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  511. /// @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  512. /// @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  513. /// @param polarity: The external Trigger Polarity.
  514. /// This parameter can be one of the following values:
  515. /// @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  516. /// @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  517. /// @param filter: External Trigger Filter.
  518. /// This parameter must be a value between 0x00 and 0x0F
  519. /// @retval None.
  520. ////////////////////////////////////////////////////////////////////////////////
  521. void TIM_ETRClockMode2Config(TIM_TypeDef* tim, TIMEXTTRGPSC_Typedef psc, TIMETP_Typedef polarity, u16 filter)
  522. {
  523. TIM_ETRConfig(tim, psc, polarity, filter);
  524. SET_BIT(tim->SMCR, TIM_SMCR_ECEN);
  525. }
  526. ////////////////////////////////////////////////////////////////////////////////
  527. /// @brief Configures the tim Prescaler.
  528. /// @param tim: select the TIM peripheral.
  529. /// @param prescaler: specifies the Prescaler Register value
  530. /// @param reloadMode: specifies the TIM Prescaler Reload mode
  531. /// This parameter can be one of the following values:
  532. /// @arg TIM_PSCReloadMode_Update: The Prescaler is loaded at the update event.
  533. /// @arg TIM_PSCReloadMode_Immediate: The Prescaler is loaded immediately.
  534. /// @retval None.
  535. ////////////////////////////////////////////////////////////////////////////////
  536. void TIM_PrescalerConfig(TIM_TypeDef* tim, u16 prescaler, TIMUG_Typedef reloadMode)
  537. {
  538. WRITE_REG(tim->PSC, prescaler);
  539. WRITE_REG(tim->EGR, reloadMode);
  540. }
  541. ////////////////////////////////////////////////////////////////////////////////
  542. /// @brief Specifies the tim Counter Mode to be used.
  543. /// @param tim:select the TIM peripheral.
  544. /// @param counter_mode: specifies the Counter Mode to be used
  545. /// This parameter can be one of the following values:
  546. /// @arg TIM_CounterMode_Up: TIM Up Counting Mode
  547. /// @arg TIM_CounterMode_Down: TIM Down Counting Mode
  548. /// @arg TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1
  549. /// @arg TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2
  550. /// @arg TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3
  551. /// @retval None.
  552. ////////////////////////////////////////////////////////////////////////////////
  553. void TIM_CounterModeConfig(TIM_TypeDef* tim, TIMCOUNTMODE_Typedef counter_mode)
  554. {
  555. MODIFY_REG(tim->CR1, TIM_CR1_CMS | TIM_CR1_DIR, counter_mode);
  556. }
  557. ////////////////////////////////////////////////////////////////////////////////
  558. /// @brief Selects the Input Trigger source
  559. /// @param tim: select the TIM peripheral.
  560. /// @param source: The Input Trigger source.
  561. /// This parameter can be one of the following values:
  562. /// @arg TIM_TS_ITR0: Internal Trigger 0
  563. /// @arg TIM_TS_ITR1: Internal Trigger 1
  564. /// @arg TIM_TS_ITR2: Internal Trigger 2
  565. /// @arg TIM_TS_ITR3: Internal Trigger 3
  566. /// @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  567. /// @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  568. /// @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  569. /// @arg TIM_TS_ETRF: External Trigger input
  570. /// @retval None.
  571. ////////////////////////////////////////////////////////////////////////////////
  572. void TIM_SelectInputTrigger(TIM_TypeDef* tim, TIMTS_TypeDef source)
  573. {
  574. MODIFY_REG(tim->SMCR, TIM_SMCR_TS, source);
  575. }
  576. ////////////////////////////////////////////////////////////////////////////////
  577. /// @brief Configures the tim Encoder Interface.
  578. /// @param tim: select the TIM peripheral.
  579. /// @param encoder_mode: specifies the tim Encoder Mode.
  580. /// This parameter can be one of the following values:
  581. /// @arg TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
  582. /// @arg TIM_EncoderMode_TI2: Counter counts on TI2FP2 edge depending on TI1FP1 level.
  583. /// @arg TIM_EncoderMode_TI12: Counter counts on both TI1FP1 and TI2FP2 edges depending
  584. /// on the level of the other input.
  585. /// @param ic1_polarity: specifies the IC1 Polarity
  586. /// This parameter can be one of the following values:
  587. /// @arg TIM_ICPolarity_Falling: IC Falling edge.
  588. /// @arg TIM_ICPolarity_Rising: IC Rising edge.
  589. /// @param ic2_polarity: specifies the IC2 Polarity
  590. /// This parameter can be one of the following values:
  591. /// @arg TIM_ICPolarity_Falling: IC Falling edge.
  592. /// @arg TIM_ICPolarity_Rising: IC Rising edge.
  593. /// @retval None.
  594. ////////////////////////////////////////////////////////////////////////////////
  595. void TIM_EncoderInterfaceConfig(TIM_TypeDef* tim,
  596. TIMSMSENCODER_Typedef encoder_mode,
  597. TIMICP_Typedef ic1_polarity,
  598. TIMICP_Typedef ic2_polarity)
  599. {
  600. MODIFY_REG(tim->SMCR, TIM_SMCR_SMS, encoder_mode);
  601. MODIFY_REG(tim->CCMR1, TIM_CCMR1_CC1S | TIM_CCMR1_CC2S, TIM_CCMR1_CC1S_DIRECTTI | TIM_CCMR1_CC2S_DIRECTTI);
  602. MODIFY_REG(tim->CCER, TIM_CCER_CC1P | TIM_CCER_CC2P, ic1_polarity | (ic2_polarity << 4));
  603. }
  604. ////////////////////////////////////////////////////////////////////////////////
  605. /// @brief Forces the tim output 1 waveform to active or inactive level.
  606. /// @param tim: select the TIM peripheral.
  607. /// @param forced_action: specifies the forced Action to be set to the output waveform.
  608. /// This parameter can be one of the following values:
  609. /// @arg TIM_ForcedAction_Active: Force active level on OC1REF
  610. /// @arg TIM_ForcedAction_InActive: Force inactive level on OC1REF.
  611. /// @retval None.
  612. ////////////////////////////////////////////////////////////////////////////////
  613. void TIM_ForcedOC1Config(TIM_TypeDef* tim, TIMOCMODE_Typedef forced_action)
  614. {
  615. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC1M, forced_action);
  616. }
  617. ////////////////////////////////////////////////////////////////////////////////
  618. /// @brief Forces the tim output 2 waveform to active or inactive level.
  619. /// @param tim: select the TIM peripheral.
  620. /// @param forced_action: specifies the forced Action to be set to the output waveform.
  621. /// This parameter can be one of the following values:
  622. /// @arg TIM_ForcedAction_Active: Force active level on OC2REF
  623. /// @arg TIM_ForcedAction_InActive: Force inactive level on OC2REF.
  624. /// @retval None.
  625. ////////////////////////////////////////////////////////////////////////////////
  626. void TIM_ForcedOC2Config(TIM_TypeDef* tim, TIMOCMODE_Typedef forced_action)
  627. {
  628. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC2M, forced_action << 8);
  629. }
  630. ////////////////////////////////////////////////////////////////////////////////
  631. /// @brief Forces the tim output 3 waveform to active or inactive level.
  632. /// @param tim: select the TIM peripheral.
  633. /// @param forced_action: specifies the forced Action to be set to the output waveform.
  634. /// This parameter can be one of the following values:
  635. /// @arg TIM_ForcedAction_Active: Force active level on OC3REF
  636. /// @arg TIM_ForcedAction_InActive: Force inactive level on OC3REF.
  637. /// @retval None.
  638. ////////////////////////////////////////////////////////////////////////////////
  639. void TIM_ForcedOC3Config(TIM_TypeDef* tim, TIMOCMODE_Typedef forced_action)
  640. {
  641. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC3M, forced_action);
  642. }
  643. ////////////////////////////////////////////////////////////////////////////////
  644. /// @brief Forces the tim output 4 waveform to active or inactive level.
  645. /// @param tim: select the TIM peripheral.
  646. /// @param forced_action: specifies the forced Action to be set to the output waveform.
  647. /// This parameter can be one of the following values:
  648. /// @arg TIM_ForcedAction_Active: Force active level on OC4REF
  649. /// @arg TIM_ForcedAction_InActive: Force inactive level on OC4REF.
  650. /// @retval None.
  651. ////////////////////////////////////////////////////////////////////////////////
  652. void TIM_ForcedOC4Config(TIM_TypeDef* tim, TIMOCMODE_Typedef forced_action)
  653. {
  654. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC4M, forced_action << 8);
  655. }
  656. ////////////////////////////////////////////////////////////////////////////////
  657. /// @brief Enables or disables tim peripheral Preload register on ARR.
  658. /// @param tim: select the TIM peripheral.
  659. /// @param state: new state of the tim peripheral Preload register
  660. /// This parameter can be: ENABLE or DISABLE.
  661. /// @retval None.
  662. ////////////////////////////////////////////////////////////////////////////////
  663. void TIM_ARRPreloadConfig(TIM_TypeDef* tim, FunctionalState state)
  664. {
  665. (state) ? SET_BIT(tim->CR1, TIM_CR1_ARPEN) : CLEAR_BIT(tim->CR1, TIM_CR1_ARPEN);
  666. }
  667. ////////////////////////////////////////////////////////////////////////////////
  668. /// @brief Selects the TIM peripheral Commutation event.
  669. /// @param tim: select the tim peripheral.
  670. /// @param state: new state of the Commutation event.
  671. /// This parameter can be: ENABLE or DISABLE.
  672. /// @retval None.
  673. ////////////////////////////////////////////////////////////////////////////////
  674. void TIM_SelectCOM(TIM_TypeDef* tim, FunctionalState state)
  675. {
  676. (state) ? SET_BIT(tim->CR2, TIM_CR2_CCUS) : CLEAR_BIT(tim->CR2, TIM_CR2_CCUS);
  677. }
  678. ////////////////////////////////////////////////////////////////////////////////
  679. /// @brief Selects the tim peripheral Capture Compare DMA source.
  680. /// @param tim: select the TIM peripheral.
  681. /// @param state: new state of the Capture Compare DMA source
  682. /// This parameter can be: ENABLE or DISABLE.
  683. /// @retval None.
  684. ////////////////////////////////////////////////////////////////////////////////
  685. void TIM_SelectCCDMA(TIM_TypeDef* tim, FunctionalState state)
  686. {
  687. (state) ? SET_BIT(tim->CR2, TIM_CR2_CCDS) : CLEAR_BIT(tim->CR2, TIM_CR2_CCDS);
  688. }
  689. ////////////////////////////////////////////////////////////////////////////////
  690. /// @brief Sets or Resets the TIM peripheral Capture Compare Preload Control bit.
  691. /// @param tim: select the tim peripheral.
  692. /// @param state: new state of the Capture Compare Preload Control bit
  693. /// This parameter can be: ENABLE or DISABLE.
  694. /// @retval None.
  695. ////////////////////////////////////////////////////////////////////////////////
  696. void TIM_CCPreloadControl(TIM_TypeDef* tim, FunctionalState state)
  697. {
  698. (state) ? SET_BIT(tim->CR2, TIM_CR2_CCPC) : CLEAR_BIT(tim->CR2, TIM_CR2_CCPC);
  699. }
  700. ////////////////////////////////////////////////////////////////////////////////
  701. /// @brief Enables or disables the tim peripheral Preload register on CCR1.
  702. /// @param tim: select the TIM peripheral.
  703. /// @param preload: new state of the tim peripheral Preload register
  704. /// This parameter can be one of the following values:
  705. /// @arg TIM_OCPreload_Enable
  706. /// @arg TIM_OCPreload_Disable
  707. /// @retval None.
  708. ////////////////////////////////////////////////////////////////////////////////
  709. void TIM_OC1PreloadConfig(TIM_TypeDef* tim, TIMOCPE_Typedef preload)
  710. {
  711. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC1PEN, preload);
  712. }
  713. ////////////////////////////////////////////////////////////////////////////////
  714. /// @brief Enables or disables the tim peripheral Preload register on CCR2.
  715. /// @param tim: select the TIM peripheral.
  716. /// @param preload: new state of the tim peripheral Preload register
  717. /// This parameter can be one of the following values:
  718. /// @arg TIM_OCPreload_Enable
  719. /// @arg TIM_OCPreload_Disable
  720. /// @retval None.
  721. ////////////////////////////////////////////////////////////////////////////////
  722. void TIM_OC2PreloadConfig(TIM_TypeDef* tim, TIMOCPE_Typedef preload)
  723. {
  724. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC2PEN, preload << 8);
  725. }
  726. ////////////////////////////////////////////////////////////////////////////////
  727. /// @brief Enables or disables the tim peripheral Preload register on CCR3.
  728. /// @param tim: select the TIM peripheral.
  729. /// @param preload: new state of the tim peripheral Preload register
  730. /// This parameter can be one of the following values:
  731. /// @arg TIM_OCPreload_Enable
  732. /// @arg TIM_OCPreload_Disable
  733. /// @retval None.
  734. ////////////////////////////////////////////////////////////////////////////////
  735. void TIM_OC3PreloadConfig(TIM_TypeDef* tim, TIMOCPE_Typedef preload)
  736. {
  737. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC3PEN, preload);
  738. }
  739. ////////////////////////////////////////////////////////////////////////////////
  740. /// @brief Enables or disables the tim peripheral Preload register on CCR4.
  741. /// @param tim: select the TIM peripheral.
  742. /// @param preload: new state of the tim peripheral Preload register
  743. /// This parameter can be one of the following values:
  744. /// @arg TIM_OCPreload_Enable
  745. /// @arg TIM_OCPreload_Disable
  746. /// @retval None.
  747. ////////////////////////////////////////////////////////////////////////////////
  748. void TIM_OC4PreloadConfig(TIM_TypeDef* tim, TIMOCPE_Typedef preload)
  749. {
  750. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC4PEN, preload << 8);
  751. }
  752. ////////////////////////////////////////////////////////////////////////////////
  753. /// @brief Configures the tim Output Compare 1 Fast feature.
  754. /// @param tim: select the TIM peripheral.
  755. /// @param fast: new state of the Output Compare Fast Enable Bit.
  756. /// This parameter can be one of the following values:
  757. /// @arg TIM_OCFast_Enable: TIM output compare fast enable
  758. /// @arg TIM_OCFast_Disable: TIM output compare fast disable
  759. /// @retval None.
  760. ////////////////////////////////////////////////////////////////////////////////
  761. void TIM_OC1FastConfig(TIM_TypeDef* tim, TIMOCFE_Typedef fast)
  762. {
  763. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC1FEN, fast);
  764. }
  765. ////////////////////////////////////////////////////////////////////////////////
  766. /// @brief Configures the tim Output Compare 2 Fast feature.
  767. /// @param tim: select the TIM peripheral.
  768. /// @param fast: new state of the Output Compare Fast Enable Bit.
  769. /// This parameter can be one of the following values:
  770. /// @arg TIM_OCFast_Enable: TIM output compare fast enable
  771. /// @arg TIM_OCFast_Disable: TIM output compare fast disable
  772. /// @retval None.
  773. ////////////////////////////////////////////////////////////////////////////////
  774. void TIM_OC2FastConfig(TIM_TypeDef* tim, TIMOCFE_Typedef fast)
  775. {
  776. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC2FEN, fast << 8);
  777. }
  778. ////////////////////////////////////////////////////////////////////////////////
  779. /// @brief Configures the tim Output Compare 3 Fast feature.
  780. /// @param tim: select the TIM peripheral.
  781. /// @param fast: new state of the Output Compare Fast Enable Bit.
  782. /// This parameter can be one of the following values:
  783. /// @arg TIM_OCFast_Enable: TIM output compare fast enable
  784. /// @arg TIM_OCFast_Disable: TIM output compare fast disable
  785. /// @retval None.
  786. ////////////////////////////////////////////////////////////////////////////////
  787. void TIM_OC3FastConfig(TIM_TypeDef* tim, TIMOCFE_Typedef fast)
  788. {
  789. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC3FEN, fast);
  790. }
  791. ////////////////////////////////////////////////////////////////////////////////
  792. /// @brief Configures the tim Output Compare 4 Fast feature.
  793. /// @param tim: select the TIM peripheral.
  794. /// @param fast: new state of the Output Compare Fast Enable Bit.
  795. /// This parameter can be one of the following values:
  796. /// @arg TIM_OCFast_Enable: TIM output compare fast enable
  797. /// @arg TIM_OCFast_Disable: TIM output compare fast disable
  798. /// @retval None.
  799. ////////////////////////////////////////////////////////////////////////////////
  800. void TIM_OC4FastConfig(TIM_TypeDef* tim, TIMOCFE_Typedef fast)
  801. {
  802. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC4FEN, fast << 8);
  803. }
  804. ////////////////////////////////////////////////////////////////////////////////
  805. /// @brief Clears or safeguards the OCREF1 signal on an external event
  806. /// @param tim: select the TIM peripheral.
  807. /// @param clear: new state of the Output Compare Clear Enable Bit.
  808. /// This parameter can be one of the following values:
  809. /// @arg TIM_OCClear_Enable: TIM Output clear enable
  810. /// @arg TIM_OCClear_Disable: TIM Output clear disable
  811. /// @retval None.
  812. ////////////////////////////////////////////////////////////////////////////////
  813. void TIM_ClearOC1Ref(TIM_TypeDef* tim, TIMOCCE_Typedef clear)
  814. {
  815. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC1CEN, clear);
  816. }
  817. ////////////////////////////////////////////////////////////////////////////////
  818. /// @brief Clears or safeguards the OCREF2 signal on an external event
  819. /// @param tim: select the TIM peripheral.
  820. /// @param clear: new state of the Output Compare Clear Enable Bit.
  821. /// This parameter can be one of the following values:
  822. /// @arg TIM_OCClear_Enable: TIM Output clear enable
  823. /// @arg TIM_OCClear_Disable: TIM Output clear disable
  824. /// @retval None.
  825. ////////////////////////////////////////////////////////////////////////////////
  826. void TIM_ClearOC2Ref(TIM_TypeDef* tim, TIMOCCE_Typedef clear)
  827. {
  828. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC2CEN, clear << 8);
  829. }
  830. ////////////////////////////////////////////////////////////////////////////////
  831. /// @brief Clears or safeguards the OCREF3 signal on an external event
  832. /// @param tim: select the TIM peripheral.
  833. /// @param clear: new state of the Output Compare Clear Enable Bit.
  834. /// This parameter can be one of the following values:
  835. /// @arg TIM_OCClear_Enable: TIM Output clear enable
  836. /// @arg TIM_OCClear_Disable: TIM Output clear disable
  837. /// @retval None.
  838. ////////////////////////////////////////////////////////////////////////////////
  839. void TIM_ClearOC3Ref(TIM_TypeDef* tim, TIMOCCE_Typedef clear)
  840. {
  841. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC3CEN, clear);
  842. }
  843. ////////////////////////////////////////////////////////////////////////////////
  844. /// @brief Clears or safeguards the OCREF4 signal on an external event
  845. /// @param tim: select the TIM peripheral.
  846. /// @param clear: new state of the Output Compare Clear Enable Bit.
  847. /// This parameter can be one of the following values:
  848. /// @arg TIM_OCClear_Enable: TIM Output clear enable
  849. /// @arg TIM_OCClear_Disable: TIM Output clear disable
  850. /// @retval None.
  851. ////////////////////////////////////////////////////////////////////////////////
  852. void TIM_ClearOC4Ref(TIM_TypeDef* tim, TIMOCCE_Typedef clear)
  853. {
  854. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC4CEN, clear << 8);
  855. }
  856. ////////////////////////////////////////////////////////////////////////////////
  857. /// @brief Configures the tim channel 1 polarity.
  858. /// @param tim: select the TIM peripheral.
  859. /// @param polarity: specifies the OC1 Polarity
  860. /// This parameter can be one of the following values:
  861. /// @arg TIM_OCPolarity_High: Output Compare active high
  862. /// @arg TIM_OCPolarity_Low: Output Compare active low
  863. /// @retval None.
  864. ////////////////////////////////////////////////////////////////////////////////
  865. void TIM_OC1PolarityConfig(TIM_TypeDef* tim, TIMCCxP_Typedef polarity)
  866. {
  867. MODIFY_REG(tim->CCER, TIM_CCER_CC1P, polarity);
  868. }
  869. ////////////////////////////////////////////////////////////////////////////////
  870. /// @brief Configures the tim Channel 1N polarity.
  871. /// @param tim: select the TIM peripheral.
  872. /// @param polarity: specifies the OC1N Polarity
  873. /// This parameter can be one of the following values:
  874. /// @arg TIM_OCNPolarity_High: Output Compare active high
  875. /// @arg TIM_OCNPolarity_Low: Output Compare active low
  876. /// @retval None.
  877. ////////////////////////////////////////////////////////////////////////////////
  878. void TIM_OC1NPolarityConfig(TIM_TypeDef* tim, TIMCCxNP_Typedef polarity)
  879. {
  880. MODIFY_REG(tim->CCER, TIM_CCER_CC1NP, polarity);
  881. }
  882. ////////////////////////////////////////////////////////////////////////////////
  883. /// @brief Configures the tim channel 2 polarity.
  884. /// @param tim: select the TIM peripheral.
  885. /// @param polarity: specifies the OC2 Polarity
  886. /// This parameter can be one of the following values:
  887. /// @arg TIM_OCPolarity_High: Output Compare active high
  888. /// @arg TIM_OCPolarity_Low: Output Compare active low
  889. /// @retval None.
  890. ////////////////////////////////////////////////////////////////////////////////
  891. void TIM_OC2PolarityConfig(TIM_TypeDef* tim, TIMCCxP_Typedef polarity)
  892. {
  893. MODIFY_REG(tim->CCER, TIM_CCER_CC2P, polarity << 4);
  894. }
  895. ////////////////////////////////////////////////////////////////////////////////
  896. /// @brief Configures the tim Channel 2N polarity.
  897. /// @param tim: select the TIM peripheral.
  898. /// @param polarity: specifies the OC2N Polarity
  899. /// This parameter can be one of the following values:
  900. /// @arg TIM_OCNPolarity_High: Output Compare active high
  901. /// @arg TIM_OCNPolarity_Low: Output Compare active low
  902. /// @retval None.
  903. ////////////////////////////////////////////////////////////////////////////////
  904. void TIM_OC2NPolarityConfig(TIM_TypeDef* tim, TIMCCxNP_Typedef polarity)
  905. {
  906. MODIFY_REG(tim->CCER, TIM_CCER_CC2NP, polarity << 4);
  907. }
  908. ////////////////////////////////////////////////////////////////////////////////
  909. /// @brief Configures the tim channel 3 polarity.
  910. /// @param tim: select the TIM peripheral.
  911. /// @param polarity: specifies the OC3 Polarity
  912. /// This parameter can be one of the following values:
  913. /// @arg TIM_OCPolarity_High: Output Compare active high
  914. /// @arg TIM_OCPolarity_Low: Output Compare active low
  915. /// @retval None.
  916. ////////////////////////////////////////////////////////////////////////////////
  917. void TIM_OC3PolarityConfig(TIM_TypeDef* tim, TIMCCxP_Typedef polarity)
  918. {
  919. MODIFY_REG(tim->CCER, TIM_CCER_CC3P, polarity << 8);
  920. }
  921. ////////////////////////////////////////////////////////////////////////////////
  922. /// @brief Configures the tim Channel 3N polarity.
  923. /// @param tim: select the TIM peripheral.
  924. /// @param polarity: specifies the OC3N Polarity
  925. /// This parameter can be one of the following values:
  926. /// @arg TIM_OCNPolarity_High: Output Compare active high
  927. /// @arg TIM_OCNPolarity_Low: Output Compare active low
  928. /// @retval None.
  929. ////////////////////////////////////////////////////////////////////////////////
  930. void TIM_OC3NPolarityConfig(TIM_TypeDef* tim, TIMCCxNP_Typedef polarity)
  931. {
  932. MODIFY_REG(tim->CCER, TIM_CCER_CC3NP, polarity << 8);
  933. }
  934. ////////////////////////////////////////////////////////////////////////////////
  935. /// @brief Configures the tim channel 4 polarity.
  936. /// @param tim: select the TIM peripheral.
  937. /// @param polarity: specifies the OC4 Polarity
  938. /// This parameter can be one of the following values:
  939. /// @arg TIM_OCPolarity_High: Output Compare active high
  940. /// @arg TIM_OCPolarity_Low: Output Compare active low
  941. /// @retval None.
  942. ////////////////////////////////////////////////////////////////////////////////
  943. void TIM_OC4PolarityConfig(TIM_TypeDef* tim, TIMCCxP_Typedef polarity)
  944. {
  945. MODIFY_REG(tim->CCER, TIM_CCER_CC4P, polarity << 12);
  946. }
  947. ////////////////////////////////////////////////////////////////////////////////
  948. /// @brief Enables or disables the TIM Capture Compare Channel x.
  949. /// @param tim: select the TIM peripheral.
  950. /// @param channel: specifies the TIM Channel
  951. /// This parameter can be one of the following values:
  952. /// @arg TIM_Channel_1: TIM Channel 1
  953. /// @arg TIM_Channel_2: TIM Channel 2
  954. /// @arg TIM_Channel_3: TIM Channel 3
  955. /// @arg TIM_Channel_4: TIM Channel 4
  956. /// @arg TIM_Channel_5: TIM Channel 5(Only for some MM32 TIM1/8)
  957. /// @param TIM_CCx: specifies the TIM Channel CCxE bit new state.
  958. /// This parameter can be: TIM_CCx_Enable or TIM_CCx_Disable.
  959. /// @retval None.
  960. ////////////////////////////////////////////////////////////////////////////////
  961. void TIM_CCxCmd(TIM_TypeDef* tim, TIMCHx_Typedef channel, TIMCCxE_Typedef ccx_en)
  962. {
  963. MODIFY_REG(tim->CCER, TIM_CCER_CC1EN << channel, ccx_en << channel);
  964. }
  965. ////////////////////////////////////////////////////////////////////////////////
  966. /// @brief Enables or disables the TIM Capture Compare Channel xN.
  967. /// @param tim: select the TIM peripheral.
  968. /// @param channel: specifies the TIM Channel
  969. /// This parameter can be one of the following values:
  970. /// @arg TIM_Channel_1: TIM Channel 1
  971. /// @arg TIM_Channel_2: TIM Channel 2
  972. /// @arg TIM_Channel_3: TIM Channel 3
  973. /// @param TIM_CCxN: specifies the TIM Channel CCxNE bit new state.
  974. /// This parameter can be: TIM_CCxN_Enable or TIM_CCxN_Disable.
  975. /// @retval None.
  976. ////////////////////////////////////////////////////////////////////////////////
  977. void TIM_CCxNCmd(TIM_TypeDef* tim, TIMCHx_Typedef channel, TIMCCxNE_Typedef ccxn_en)
  978. {
  979. if (channel != TIM_Channel_4)
  980. MODIFY_REG(tim->CCER, TIM_CCER_CC1NEN << channel, ccxn_en << channel);
  981. }
  982. ////////////////////////////////////////////////////////////////////////////////
  983. /// @brief Selects the TIM Output Compare Mode.
  984. /// @note This function disables the selected channel before changing the Output
  985. /// Compare Mode.
  986. /// User has to enable this channel using TIM_CCxCmd and TIM_CCxNCmd functions.
  987. /// @param tim: select the TIM peripheral.
  988. /// @param channel: specifies the TIM Channel
  989. /// This parameter can be one of the following values:
  990. /// @arg TIM_Channel_1: TIM Channel 1
  991. /// @arg TIM_Channel_2: TIM Channel 2
  992. /// @arg TIM_Channel_3: TIM Channel 3
  993. /// @arg TIM_Channel_4: TIM Channel 4
  994. /// @param mode: specifies the TIM Output Compare Mode.
  995. /// This parameter can be one of the following values:
  996. /// @arg TIM_OCMode_Timing
  997. /// @arg TIM_OCMode_Active
  998. /// @arg TIM_OCMode_Toggle
  999. /// @arg TIM_OCMode_PWM1
  1000. /// @arg TIM_OCMode_PWM2
  1001. /// @arg TIM_ForcedAction_Active
  1002. /// @arg TIM_ForcedAction_InActive
  1003. /// @retval None.
  1004. ////////////////////////////////////////////////////////////////////////////////
  1005. void TIM_SelectOCxM(TIM_TypeDef* tim, TIMCHx_Typedef channel, TIMOCMODE_Typedef mode)
  1006. {
  1007. CLEAR_BIT(tim->CCER, TIM_CCER_CC1EN << channel);
  1008. switch (channel) {
  1009. case TIM_Channel_1:
  1010. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC1M, mode);
  1011. break;
  1012. case TIM_Channel_2:
  1013. MODIFY_REG(tim->CCMR1, TIM_CCMR1_OC2M, mode << 8);
  1014. break;
  1015. case TIM_Channel_3:
  1016. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC3M, mode);
  1017. break;
  1018. case TIM_Channel_4:
  1019. MODIFY_REG(tim->CCMR2, TIM_CCMR2_OC4M, mode << 8);
  1020. break;
  1021. default:
  1022. break;
  1023. }
  1024. }
  1025. ////////////////////////////////////////////////////////////////////////////////
  1026. /// @brief Enables or Disables the tim Update event.
  1027. /// @param tim: select the TIM peripheral.
  1028. /// @param state: new state of the tim UDIS bit
  1029. /// This parameter can be: ENABLE or DISABLE.
  1030. /// @retval None.
  1031. ////////////////////////////////////////////////////////////////////////////////
  1032. void TIM_UpdateDisableConfig(TIM_TypeDef* tim, FunctionalState state)
  1033. {
  1034. (state) ? SET_BIT(tim->CR1, TIM_CR1_UDIS) : CLEAR_BIT(tim->CR1, TIM_CR1_UDIS);
  1035. }
  1036. ////////////////////////////////////////////////////////////////////////////////
  1037. /// @brief Configures the tim Update Request Interrupt source.
  1038. /// @param tim: select the TIM peripheral.
  1039. /// @param source: specifies the Update source.
  1040. /// This parameter can be one of the following values:
  1041. /// @arg TIM_UpdateSource_Regular: Source of update is the counter overflow/underflow
  1042. /// or the setting of UG bit, or an update generation
  1043. /// through the slave mode controller.
  1044. /// @arg TIM_UpdateSource_Global: Source of update is counter overflow/underflow.
  1045. /// @retval None.
  1046. ////////////////////////////////////////////////////////////////////////////////
  1047. void TIM_UpdateRequestConfig(TIM_TypeDef* tim, TIMURS_Typedef source)
  1048. {
  1049. MODIFY_REG(tim->CR1, TIM_CR1_URS, source);
  1050. }
  1051. ////////////////////////////////////////////////////////////////////////////////
  1052. /// @brief Enables or disables the tim's Hall sensor interface.
  1053. /// @param tim: select the TIM peripheral.
  1054. /// @param state: new state of the tim Hall sensor interface.
  1055. /// This parameter can be: ENABLE or DISABLE.
  1056. /// @retval None.
  1057. ////////////////////////////////////////////////////////////////////////////////
  1058. void TIM_SelectHallSensor(TIM_TypeDef* tim, FunctionalState state)
  1059. {
  1060. (state) ? SET_BIT(tim->CR2, TIM_CR2_TI1S) : CLEAR_BIT(tim->CR2, TIM_CR2_TI1S);
  1061. }
  1062. ////////////////////////////////////////////////////////////////////////////////
  1063. /// @brief Selects the tim's One Pulse Mode.
  1064. /// @param tim: select the TIM peripheral.
  1065. /// @param mode: specifies the OPM Mode to be used.
  1066. /// This parameter can be one of the following values:
  1067. /// @arg TIM_OPMode_Single
  1068. /// @arg TIM_OPMode_Repetitive
  1069. /// @retval None.
  1070. ////////////////////////////////////////////////////////////////////////////////
  1071. void TIM_SelectOnePulseMode(TIM_TypeDef* tim, TIMOPMODE_Typedef mode)
  1072. {
  1073. MODIFY_REG(tim->CR1, TIM_CR1_OPM, mode);
  1074. }
  1075. ////////////////////////////////////////////////////////////////////////////////
  1076. /// @brief Selects the tim Trigger Output Mode.
  1077. /// @param tim:select the TIM peripheral.
  1078. /// @param source: specifies the Trigger Output source.
  1079. /// This paramter can be one of the following values:
  1080. /// - For all tim
  1081. /// @arg TIM_TRIGSource_Reset: The UG bit in the TIM_EGR register is used as the trigger output (TRIG).
  1082. /// @arg TIM_TRIGSource_Enable: The Counter Enable CEN is used as the trigger output (TRIG).
  1083. /// @arg TIM_TRIGSource_Update: The update event is selected as the trigger output (TRIG).
  1084. /// @arg TIM_TRIGSource_OC1: The trigger output sends a positive pulse when the CC1IF flag
  1085. /// is to be set, as soon as a capture or compare match occurs (TRIG).
  1086. /// @arg TIM_TRIGSource_OC1Ref: OC1REF signal is used as the trigger output (TRIG).
  1087. /// @arg TIM_TRIGSource_OC2Ref: OC2REF signal is used as the trigger output (TRIG).
  1088. /// @arg TIM_TRIGSource_OC3Ref: OC3REF signal is used as the trigger output (TRIG).
  1089. /// @arg TIM_TRIGSource_OC4Ref: OC4REF signal is used as the trigger output (TRIG).
  1090. ///
  1091. /// @retval None.
  1092. ////////////////////////////////////////////////////////////////////////////////
  1093. void TIM_SelectOutputTrigger(TIM_TypeDef* tim, TIMMMS_Typedef source)
  1094. {
  1095. MODIFY_REG(tim->CR2, TIM_CR2_MMS, source);
  1096. }
  1097. ////////////////////////////////////////////////////////////////////////////////
  1098. /// @brief Selects the tim Slave Mode.
  1099. /// @param tim: select the TIM peripheral.
  1100. /// @param mode: specifies the Timer Slave Mode.
  1101. /// This parameter can be one of the following values:
  1102. /// @arg TIM_SlaveMode_Reset: Rising edge of the selected trigger signal (TRGI) re-initializes
  1103. /// the counter and triggers an update of the registers.
  1104. /// @arg TIM_SlaveMode_Gated: The counter clock is enabled when the trigger signal (TRGI) is high.
  1105. /// @arg TIM_SlaveMode_Trigger: The counter starts at a rising edge of the trigger TRGI.
  1106. /// @arg TIM_SlaveMode_External1: Rising edges of the selected trigger (TRGI) clock the counter.
  1107. /// @retval None.
  1108. ////////////////////////////////////////////////////////////////////////////////
  1109. void TIM_SelectSlaveMode(TIM_TypeDef* tim, TIMSMSMODE_Typedef mode)
  1110. {
  1111. MODIFY_REG(tim->SMCR, TIM_SMCR_SMS, mode);
  1112. }
  1113. ////////////////////////////////////////////////////////////////////////////////
  1114. /// @brief Sets or Resets the tim Master/Slave Mode.
  1115. /// @param tim: select the TIM peripheral.
  1116. /// @param mode: specifies the Timer Master Slave Mode.
  1117. /// This parameter can be one of the following values:
  1118. /// @arg TIM_MasterSlaveMode_Enable: synchronization between the current timer
  1119. /// and its slaves (through TRIG).
  1120. /// @arg TIM_MasterSlaveMode_Disable: No action
  1121. /// @retval None.
  1122. ////////////////////////////////////////////////////////////////////////////////
  1123. void TIM_SelectMasterSlaveMode(TIM_TypeDef* tim, TIMMSM_Typedef mode)
  1124. {
  1125. MODIFY_REG(tim->SMCR, TIM_SMCR_MSM, mode);
  1126. }
  1127. ////////////////////////////////////////////////////////////////////////////////
  1128. /// @brief Sets the tim Counter Register value
  1129. /// @param tim: select the TIM peripheral.
  1130. /// @param auto_reload: specifies the Counter register new value.
  1131. /// @retval None.
  1132. ////////////////////////////////////////////////////////////////////////////////
  1133. void TIM_SetAutoreload(TIM_TypeDef* tim, u16 auto_reload)
  1134. {
  1135. WRITE_REG(tim->ARR, auto_reload);
  1136. }
  1137. ////////////////////////////////////////////////////////////////////////////////
  1138. /// @brief Sets the tim Input Capture 1 prescaler.
  1139. /// @param tim: select the TIM peripheral.
  1140. /// @param psc: specifies the Input Capture1 prescaler new value.
  1141. /// This parameter can be one of the following values:
  1142. /// @arg TIM_ICPSC_DIV1: no prescaler
  1143. /// @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1144. /// @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1145. /// @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1146. /// @retval None.
  1147. ////////////////////////////////////////////////////////////////////////////////
  1148. void TIM_SetIC1Prescaler(TIM_TypeDef* tim, TIMICPSC_Typedef psc)
  1149. {
  1150. MODIFY_REG(tim->CCMR1, TIM_CCMR1_IC1PSC, psc);
  1151. }
  1152. ////////////////////////////////////////////////////////////////////////////////
  1153. /// @brief Sets the tim Input Capture 2 prescaler.
  1154. /// @param tim: select the TIM peripheral.
  1155. /// @param psc: specifies the Input Capture2 prescaler new value.
  1156. /// This parameter can be one of the following values:
  1157. /// @arg TIM_ICPSC_DIV1: no prescaler
  1158. /// @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1159. /// @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1160. /// @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1161. /// @retval None.
  1162. ////////////////////////////////////////////////////////////////////////////////
  1163. void TIM_SetIC2Prescaler(TIM_TypeDef* tim, TIMICPSC_Typedef psc)
  1164. {
  1165. MODIFY_REG(tim->CCMR1, TIM_CCMR1_IC2PSC, psc << 8);
  1166. }
  1167. ////////////////////////////////////////////////////////////////////////////////
  1168. /// @brief Sets the tim Input Capture 3 prescaler.
  1169. /// @param tim: select the TIM peripheral.
  1170. /// @param psc: specifies the Input Capture3 prescaler new value.
  1171. /// This parameter can be one of the following values:
  1172. /// @arg TIM_ICPSC_DIV1: no prescaler
  1173. /// @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1174. /// @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1175. /// @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1176. /// @retval None.
  1177. ////////////////////////////////////////////////////////////////////////////////
  1178. void TIM_SetIC3Prescaler(TIM_TypeDef* tim, TIMICPSC_Typedef psc)
  1179. {
  1180. MODIFY_REG(tim->CCMR2, TIM_CCMR2_IC3PSC, psc);
  1181. }
  1182. ////////////////////////////////////////////////////////////////////////////////
  1183. /// @brief Sets the tim Input Capture 4 prescaler.
  1184. /// @param tim: select the TIM peripheral.
  1185. /// @param psc: specifies the Input Capture4 prescaler new value.
  1186. /// This parameter can be one of the following values:
  1187. /// @arg TIM_ICPSC_DIV1: no prescaler
  1188. /// @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1189. /// @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1190. /// @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1191. /// @retval None.
  1192. ////////////////////////////////////////////////////////////////////////////////
  1193. void TIM_SetIC4Prescaler(TIM_TypeDef* tim, TIMICPSC_Typedef psc)
  1194. {
  1195. MODIFY_REG(tim->CCMR2, TIM_CCMR2_IC4PSC, psc << 8);
  1196. }
  1197. ////////////////////////////////////////////////////////////////////////////////
  1198. /// @brief Sets the tim Clock Division value.
  1199. /// @param tim: select
  1200. /// the TIM peripheral.
  1201. /// @param clock_div: specifies the clock division value.
  1202. /// This parameter can be one of the following value:
  1203. /// @arg TIM_CKD_DIV1: TDTS = Tck_tim
  1204. /// @arg TIM_CKD_DIV2: TDTS = 2 * Tck_tim
  1205. /// @arg TIM_CKD_DIV4: TDTS = 4 * Tck_tim
  1206. /// @retval None.
  1207. ////////////////////////////////////////////////////////////////////////////////
  1208. void TIM_SetClockDivision(TIM_TypeDef* tim, TIMCKD_TypeDef clock_div)
  1209. {
  1210. MODIFY_REG(tim->CR1, TIM_CR1_CKD, clock_div);
  1211. }
  1212. ////////////////////////////////////////////////////////////////////////////////
  1213. /// @brief Sets the tim Counter Register value
  1214. /// @param tim: select the TIM peripheral.
  1215. /// @param counter: specifies the Counter register new value.
  1216. /// @retval None.
  1217. ////////////////////////////////////////////////////////////////////////////////
  1218. void TIM_SetCounter(TIM_TypeDef* tim, u32 counter)
  1219. {
  1220. if ((tim == TIM2) || (tim == TIM5))
  1221. WRITE_REG(tim->CNT, (u32)counter);
  1222. else
  1223. WRITE_REG(tim->CNT, (u16)counter);
  1224. }
  1225. ////////////////////////////////////////////////////////////////////////////////
  1226. /// @brief Sets the tim Capture Compare1 Register value
  1227. /// @param tim: select the TIM peripheral.
  1228. /// @param compare: specifies the Capture Compare1 register new value.
  1229. /// @retval None.
  1230. ////////////////////////////////////////////////////////////////////////////////
  1231. void TIM_SetCompare1(TIM_TypeDef* tim, u32 compare)
  1232. {
  1233. if ((tim == TIM2) || (tim == TIM5))
  1234. WRITE_REG(tim->CCR1, (u32)compare);
  1235. else
  1236. WRITE_REG(tim->CCR1, (u16)compare);
  1237. }
  1238. ////////////////////////////////////////////////////////////////////////////////
  1239. /// @brief Sets the tim Capture Compare2 Register value
  1240. /// @param tim: select the TIM peripheral.
  1241. /// @param compare: specifies the Capture Compare2 register new value.
  1242. /// @retval None.
  1243. ////////////////////////////////////////////////////////////////////////////////
  1244. void TIM_SetCompare2(TIM_TypeDef* tim, u32 compare)
  1245. {
  1246. if ((tim == TIM2) || (tim == TIM5))
  1247. WRITE_REG(tim->CCR2, (u32)compare);
  1248. else
  1249. WRITE_REG(tim->CCR2, (u16)compare);
  1250. }
  1251. ////////////////////////////////////////////////////////////////////////////////
  1252. /// @brief Sets the tim Capture Compare3 Register value
  1253. /// @param tim: select the TIM peripheral.
  1254. /// @param compare: specifies the Capture Compare3 register new value.
  1255. /// @retval None.
  1256. ////////////////////////////////////////////////////////////////////////////////
  1257. void TIM_SetCompare3(TIM_TypeDef* tim, u32 compare)
  1258. {
  1259. if ((tim == TIM2) || (tim == TIM5))
  1260. WRITE_REG(tim->CCR3, (u32)compare);
  1261. else
  1262. WRITE_REG(tim->CCR3, (u16)compare);
  1263. }
  1264. ////////////////////////////////////////////////////////////////////////////////
  1265. /// @brief Sets the tim Capture Compare4 Register value
  1266. /// @param tim: select the TIM peripheral.
  1267. /// @param compare: specifies the Capture Compare4 register new value.
  1268. /// @retval None.
  1269. ////////////////////////////////////////////////////////////////////////////////
  1270. void TIM_SetCompare4(TIM_TypeDef* tim, u32 compare)
  1271. {
  1272. if ((tim == TIM2) || (tim == TIM5))
  1273. WRITE_REG(tim->CCR4, (u32)compare);
  1274. else
  1275. WRITE_REG(tim->CCR4, (u16)compare);
  1276. }
  1277. ////////////////////////////////////////////////////////////////////////////////
  1278. /// @brief Gets the tim Input Capture 1 value.
  1279. /// @param tim: select the TIM peripheral.
  1280. /// @retval Value: Capture Compare 1 Register value.
  1281. ////////////////////////////////////////////////////////////////////////////////
  1282. u32 TIM_GetCapture1(TIM_TypeDef* tim)
  1283. {
  1284. return tim->CCR1;
  1285. }
  1286. ////////////////////////////////////////////////////////////////////////////////
  1287. /// @brief Gets the tim Input Capture 2 value.
  1288. /// @param tim: select the TIM peripheral.
  1289. /// @retval Value: Capture Compare 2 Register value.
  1290. ////////////////////////////////////////////////////////////////////////////////
  1291. u32 TIM_GetCapture2(TIM_TypeDef* tim)
  1292. {
  1293. return tim->CCR2;
  1294. }
  1295. ////////////////////////////////////////////////////////////////////////////////
  1296. /// @brief Gets the tim Input Capture 3 value.
  1297. /// @param tim: select the TIM peripheral.
  1298. /// @retval Value: Capture Compare 3 Register value.
  1299. ////////////////////////////////////////////////////////////////////////////////
  1300. u32 TIM_GetCapture3(TIM_TypeDef* tim)
  1301. {
  1302. return tim->CCR3;
  1303. }
  1304. ////////////////////////////////////////////////////////////////////////////////
  1305. /// @brief Gets the tim Input Capture 4 value.
  1306. /// @param tim: select the TIM peripheral.
  1307. /// @retval Value: Capture Compare 4 Register value.
  1308. ////////////////////////////////////////////////////////////////////////////////
  1309. u32 TIM_GetCapture4(TIM_TypeDef* tim)
  1310. {
  1311. return tim->CCR4;
  1312. }
  1313. ////////////////////////////////////////////////////////////////////////////////
  1314. /// @brief Gets the tim Counter value.
  1315. /// @param tim: select the TIM peripheral.
  1316. /// @retval Value: Counter Register value.
  1317. ////////////////////////////////////////////////////////////////////////////////
  1318. u32 TIM_GetCounter(TIM_TypeDef* tim)
  1319. {
  1320. return tim->CNT;
  1321. }
  1322. ////////////////////////////////////////////////////////////////////////////////
  1323. /// @brief Gets the tim Prescaler value.
  1324. /// @param tim: select the TIM peripheral.
  1325. /// @retval Value: Prescaler Register value.
  1326. ////////////////////////////////////////////////////////////////////////////////
  1327. u16 TIM_GetPrescaler(TIM_TypeDef* tim)
  1328. {
  1329. return tim->PSC;
  1330. }
  1331. ////////////////////////////////////////////////////////////////////////////////
  1332. /// @brief Checks whether the specified TIM flag is set or not.
  1333. /// @param tim: select the TIM peripheral.
  1334. /// @param flag: specifies the flag to check.
  1335. /// This parameter can be one of the following values:
  1336. /// @arg TIM_FLAG_Update: TIM update Flag
  1337. /// @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  1338. /// @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  1339. /// @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  1340. /// @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  1341. /// @arg TIM_FLAG_COM: TIM Commutation Flag
  1342. /// @arg TIM_FLAG_Trigger: TIM Trigger Flag
  1343. /// @arg TIM_FLAG_Break: TIM Break Flag
  1344. /// @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 overcapture Flag
  1345. /// @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 overcapture Flag
  1346. /// @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 overcapture Flag
  1347. /// @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 overcapture Flag
  1348. /// @note
  1349. /// - TIM14, TIM16 and TIM17 can have TIM_FLAG_Update or TIM_FLAG_CC1.
  1350. /// - TIM_FLAG_Break is used only with TIM1 and TIM8.
  1351. /// - TIM_FLAG_COM is used only with TIM1, TIM8, TIM16 and TIM17.
  1352. /// @retval State: The new state of TIM_FLAG (SET or RESET).
  1353. ////////////////////////////////////////////////////////////////////////////////
  1354. FlagStatus TIM_GetFlagStatus(TIM_TypeDef* tim, TIMFLAG_Typedef flag)
  1355. {
  1356. return ((tim->SR & flag) ? SET : RESET);
  1357. }
  1358. ////////////////////////////////////////////////////////////////////////////////
  1359. /// @brief Clears the tim's pending flags.
  1360. /// @param tim: select the TIM peripheral.
  1361. /// @param flag: specifies the flag bit to clear.
  1362. /// This parameter can be any combination of the following values:
  1363. /// @arg TIM_FLAG_Update: TIM update Flag
  1364. /// @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  1365. /// @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  1366. /// @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  1367. /// @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  1368. /// @arg TIM_FLAG_COM: TIM Commutation Flag
  1369. /// @arg TIM_FLAG_Trigger: TIM Trigger Flag
  1370. /// @arg TIM_FLAG_Break: TIM Break Flag
  1371. /// @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 overcapture Flag
  1372. /// @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 overcapture Flag
  1373. /// @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 overcapture Flag
  1374. /// @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 overcapture Flag
  1375. /// @note
  1376. /// - TIM14, TIM16 and TIM17 can have TIM_FLAG_Update or TIM_FLAG_CC1.
  1377. /// - TIM_FLAG_Break is used only with TIM1 and TIM8.
  1378. /// - TIM_FLAG_COM is used only with TIM1, TIM8, TIM16 and TIM17.
  1379. /// @retval None.
  1380. ////////////////////////////////////////////////////////////////////////////////
  1381. void TIM_ClearFlag(TIM_TypeDef* tim, TIMFLAG_Typedef flag)
  1382. {
  1383. CLEAR_BIT(tim->SR, flag);
  1384. }
  1385. ////////////////////////////////////////////////////////////////////////////////
  1386. /// @brief Checks whether the TIM interrupt has occurred or not.
  1387. /// @param tim: select the TIM peripheral.
  1388. /// @param it: specifies the TIM interrupt source to check.
  1389. /// This parameter can be one of the following values:
  1390. /// @arg TIM_IT_Update: TIM update Interrupt source
  1391. /// @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  1392. /// @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  1393. /// @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  1394. /// @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  1395. /// @arg TIM_IT_COM: TIM Commutation Interrupt source
  1396. /// @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  1397. /// @arg TIM_IT_Break: TIM Break Interrupt source
  1398. /// @note
  1399. /// - TIM14, TIM16 and TIM17 can have TIM_IT_Update or TIM_IT_CC1.
  1400. /// - TIM_IT_Break is used only with TIM1 and TIM8.
  1401. /// - TIM_IT_COM is used only with TIM1, TIM8, TIM16 and TIM17.
  1402. /// @retval State: The new state of the TIM_IT(SET or RESET).
  1403. ////////////////////////////////////////////////////////////////////////////////
  1404. ITStatus TIM_GetITStatus(TIM_TypeDef* tim, TIMIT_TypeDef it)
  1405. {
  1406. return (((tim->SR & it) && (tim->DIER & it)) ? SET : RESET);
  1407. }
  1408. ////////////////////////////////////////////////////////////////////////////////
  1409. /// @brief Clears the tim's interrupt pending bits.
  1410. /// @param tim: select the TIM peripheral.
  1411. /// @param it: specifies the pending bit to clear.
  1412. /// This parameter can be any combination of the following values:
  1413. /// @arg TIM_IT_Update: TIM1 update Interrupt source
  1414. /// @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  1415. /// @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  1416. /// @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  1417. /// @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  1418. /// @arg TIM_IT_COM: TIM Commutation Interrupt source
  1419. /// @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  1420. /// @arg TIM_IT_Break: TIM Break Interrupt source
  1421. /// @note
  1422. /// - TIM14, TIM16 and TIM17 can have TIM_IT_Update or TIM_IT_CC1.
  1423. /// - TIM_IT_Break is used only with TIM1 and TIM8.
  1424. /// - TIM_IT_COM is used only with TIM1, TIM8, TIM16 and TIM17.
  1425. /// @retval None.
  1426. ////////////////////////////////////////////////////////////////////////////////
  1427. void TIM_ClearITPendingBit(TIM_TypeDef* tim, u32 it) //TIMIT_TypeDef
  1428. {
  1429. CLEAR_BIT(tim->SR, it);
  1430. }
  1431. ////////////////////////////////////////////////////////////////////////////////
  1432. /// @brief Configures the tim channel 1 polarity.
  1433. /// @param tim: select the TIM peripheral.
  1434. /// @param polarity: specifies the IC1 Polarity
  1435. /// This parameter can be one of the following values:
  1436. /// @arg TIM_ICPolarity_Rising
  1437. /// @arg TIM_ICPolarity_Falling
  1438. /// @retval None.
  1439. ////////////////////////////////////////////////////////////////////////////////
  1440. void TIM_SetIC1Plority(TIM_TypeDef* tim, TIMICP_Typedef pol)
  1441. {
  1442. (pol) ? SET_BIT(tim->CCER, TIM_CCER_CC1P) : CLEAR_BIT(tim->CCER, TIM_CCER_CC1P);
  1443. }
  1444. ////////////////////////////////////////////////////////////////////////////////
  1445. /// @brief Configures the tim channel 2 polarity.
  1446. /// @param tim: select the TIM peripheral.
  1447. /// @param polarity: specifies the IC2 Polarity
  1448. /// This parameter can be one of the following values:
  1449. /// @arg TIM_ICPolarity_Rising
  1450. /// @arg TIM_ICPolarity_Falling
  1451. /// @retval None.
  1452. ////////////////////////////////////////////////////////////////////////////////
  1453. void TIM_SetIC2Plority(TIM_TypeDef* tim, TIMICP_Typedef pol)
  1454. {
  1455. (pol) ? SET_BIT(tim->CCER, TIM_CCER_CC2P) : CLEAR_BIT(tim->CCER, TIM_CCER_CC2P);
  1456. }
  1457. ////////////////////////////////////////////////////////////////////////////////
  1458. /// @brief Configures the tim channel 3 polarity.
  1459. /// @param tim: select the TIM peripheral.
  1460. /// @param polarity: specifies the IC3 Polarity
  1461. /// This parameter can be one of the following values:
  1462. /// @arg TIM_ICPolarity_Rising
  1463. /// @arg TIM_ICPolarity_Falling
  1464. /// @retval None.
  1465. ////////////////////////////////////////////////////////////////////////////////
  1466. void TIM_SetIC3Plority(TIM_TypeDef* tim, TIMICP_Typedef pol)
  1467. {
  1468. (pol) ? SET_BIT(tim->CCER, TIM_CCER_CC3P) : CLEAR_BIT(tim->CCER, TIM_CCER_CC3P);
  1469. }
  1470. ////////////////////////////////////////////////////////////////////////////////
  1471. /// @brief Configures the tim channel 4 polarity.
  1472. /// @param tim: select the TIM peripheral.
  1473. /// @param polarity: specifies the IC4 Polarity
  1474. /// This parameter can be one of the following values:
  1475. /// @arg TIM_ICPolarity_Rising
  1476. /// @arg TIM_ICPolarity_Falling
  1477. /// @retval None.
  1478. ////////////////////////////////////////////////////////////////////////////////
  1479. void TIM_SetIC4Plority(TIM_TypeDef* tim, TIMICP_Typedef pol)
  1480. {
  1481. (pol) ? SET_BIT(tim->CCER, TIM_CCER_CC4P) : CLEAR_BIT(tim->CCER, TIM_CCER_CC4P);
  1482. }
  1483. ////////////////////////////////////////////////////////////////////////////////
  1484. /// @brief Sets the tim Capture Compare 5 Register value
  1485. /// @param tim: select the TIM peripheral.
  1486. /// @param compare: specifies the Capture Compare5 register new value.
  1487. /// @retval None.
  1488. ////////////////////////////////////////////////////////////////////////////////
  1489. void TIM_SetCompare5(TIM_TypeDef* tim, u32 compare)
  1490. {
  1491. WRITE_REG(tim->CCR5, (u16)compare);
  1492. }
  1493. ////////////////////////////////////////////////////////////////////////////////
  1494. /// @brief Gets the tim Input Capture 5 value.
  1495. /// @param tim: select the TIM peripheral.
  1496. /// @retval Value: Capture Compare 5 Register value.
  1497. ////////////////////////////////////////////////////////////////////////////////
  1498. u32 TIM_GetCapture5(TIM_TypeDef* tim)
  1499. {
  1500. return tim->CCR5;
  1501. }
  1502. ////////////////////////////////////////////////////////////////////////////////
  1503. /// @brief Initializes the tim Channel5 according to the specified
  1504. /// parameters in the init_struct.
  1505. /// @param tim: select the TIM peripheral.
  1506. /// @param init_struct: pointer to a TIM_OCInitTypeDef structure that
  1507. /// contains the configuration information for the specified TIM peripheral.
  1508. /// @retval None.
  1509. ////////////////////////////////////////////////////////////////////////////////
  1510. void TIM_OC5Init(TIM_TypeDef* tim, TIM_OCInitTypeDef* init_struct)
  1511. {
  1512. MODIFY_REG(tim->CCMR3, TIM_CCMR3_OC5M, (init_struct->TIM_OCMode) << 4);
  1513. MODIFY_REG(tim->CCER, TIM_CCER_CC5EN | TIM_CCER_CC5P,
  1514. (init_struct->TIM_OCPolarity << 16) | (init_struct->TIM_OutputState << 16));
  1515. WRITE_REG(tim->CCR4, init_struct->TIM_Pulse);
  1516. if ((tim == TIM1) || (tim == TIM8))
  1517. MODIFY_REG(tim->CR2, TIM_CR2_OIS5, init_struct->TIM_OCIdleState << 8);
  1518. }
  1519. ////////////////////////////////////////////////////////////////////////////////
  1520. /// @brief Enables or disables the tim peripheral Preload register on CCR5.
  1521. /// @param tim: select the TIM peripheral.
  1522. /// @param preload: new state of the tim peripheral Preload register
  1523. /// This parameter can be one of the following values:
  1524. /// @arg TIM_OCPreload_Enable
  1525. /// @arg TIM_OCPreload_Disable
  1526. /// @retval None.
  1527. ////////////////////////////////////////////////////////////////////////////////
  1528. void TIM_OC5PreloadConfig(TIM_TypeDef* tim, TIMOCPE_Typedef preload)
  1529. {
  1530. MODIFY_REG(tim->CCMR3, TIM_CCMR3_OC5PEN, preload);
  1531. }
  1532. ////////////////////////////////////////////////////////////////////////////////
  1533. /// @brief Configures the tim channel 5 polarity.
  1534. /// @param tim: select the TIM peripheral.
  1535. /// @param polarity: specifies the OC5 Polarity
  1536. /// This parameter can be one of the following values:
  1537. /// @arg TIM_OCPolarity_High: Output Compare active high
  1538. /// @arg TIM_OCPolarity_Low: Output Compare active low
  1539. /// @retval None.
  1540. ////////////////////////////////////////////////////////////////////////////////
  1541. void TIM_OC5PolarityConfig(TIM_TypeDef* tim, TIMCCxP_Typedef polarity)
  1542. {
  1543. MODIFY_REG(tim->CCER, TIM_CCER_CC5P, polarity << 16);
  1544. }
  1545. ////////////////////////////////////////////////////////////////////////////////
  1546. /// @brief Configures the tim Output Compare 5 Fast feature.
  1547. /// @param tim: select the TIM peripheral.
  1548. /// @param fast: 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_OC5FastConfig(TIM_TypeDef* tim, TIMOCFE_Typedef fast)
  1555. {
  1556. MODIFY_REG(tim->CCMR3, TIM_CCMR3_OC5FEN, fast);
  1557. }
  1558. ////////////////////////////////////////////////////////////////////////////////
  1559. /// @brief Clears or safeguards the OCREF4 signal on an external event
  1560. /// @param tim: select the TIM peripheral.
  1561. /// @param clear: new state of the Output Compare Clear Enable Bit.
  1562. /// This parameter can be one of the following values:
  1563. /// @arg TIM_OCClear_Enable: TIM Output clear enable
  1564. /// @arg TIM_OCClear_Disable: TIM Output clear disable
  1565. /// @retval None.
  1566. ////////////////////////////////////////////////////////////////////////////////
  1567. void TIM_ClearOC5Ref(TIM_TypeDef* tim, TIMOCCE_Typedef clear)
  1568. {
  1569. MODIFY_REG(tim->CCMR3, TIM_CCMR3_OC5CEN, clear);
  1570. }
  1571. ////////////////////////////////////////////////////////////////////////////////
  1572. /// @brief Enables or disables the tim complementary PWM output Status after Break.
  1573. /// @param tim: select the TIM peripheral.
  1574. /// @param state: new state of the tim complementary PWM output.
  1575. /// This parameter can be: ENABLE or DISABLE.
  1576. /// @arg ENABLE: Direct output enable, no longer waiting for output after dead time.
  1577. /// @arg DISABLE: Direct output disable, output waiting for dead time.
  1578. /// @retval None.
  1579. ////////////////////////////////////////////////////////////////////////////////
  1580. void TIM_DirectOutput(TIM_TypeDef* tim, FunctionalState state)
  1581. {
  1582. (state) ? SET_BIT(tim->BDTR, TIM_BDTR_DOEN) : CLEAR_BIT(tim->BDTR, TIM_BDTR_DOEN);
  1583. }
  1584. /// @}
  1585. ////////////////////////////////////////////////////////////////////////////////
  1586. /// @defgroup TIM_Private_Functions
  1587. /// @{
  1588. ////////////////////////////////////////////////////////////////////////////////
  1589. /// @brief Configure the TI1 as Input.
  1590. /// @param tim: select the TIM peripheral.
  1591. /// @param polarity : The Input Polarity.
  1592. /// This parameter can be one of the following values:
  1593. /// @arg TIM_ICPolarity_Rising
  1594. /// @arg TIM_ICPolarity_Falling
  1595. /// @param selection: specifies the input to be used.
  1596. /// This parameter can be one of the following values:
  1597. /// @arg TIM_ICSelection_DirectTI: TIM Input 1 is selected to be connected to IC1.
  1598. /// @arg TIM_ICSelection_IndirectTI: TIM Input 1 is selected to be connected to IC2.
  1599. /// @arg TIM_ICSelection_TRC: TIM Input 1 is selected to be connected to TRC.
  1600. /// @param filter: Specifies the Input Capture Filter.
  1601. /// This parameter must be a value between 0x00 and 0x0F.
  1602. /// @retval None.
  1603. ////////////////////////////////////////////////////////////////////////////////
  1604. static void TI1_Configure(TIM_TypeDef* tim, u16 polarity, u16 selection, u16 filter)
  1605. {
  1606. MODIFY_REG(tim->CCMR1, TIM_CCMR1_CC1S | TIM_CCMR1_IC1F, (filter << 4) | selection);
  1607. MODIFY_REG(tim->CCER, TIM_CCER_CC1EN | TIM_CCER_CC1P, polarity | TIM_CCER_CC1EN);
  1608. }
  1609. ////////////////////////////////////////////////////////////////////////////////
  1610. /// @brief Configure the TI2 as Input.
  1611. /// @param tim: select the TIM peripheral.
  1612. /// @param polarity : The Input Polarity.
  1613. /// This parameter can be one of the following values:
  1614. /// @arg TIM_ICPolarity_Rising
  1615. /// @arg TIM_ICPolarity_Falling
  1616. /// @param selection: specifies the input to be used.
  1617. /// This parameter can be one of the following values:
  1618. /// @arg TIM_ICSelection_DirectTI: TIM Input 2 is selected to be connected to IC2.
  1619. /// @arg TIM_ICSelection_IndirectTI: TIM Input 2 is selected to be connected to IC1.
  1620. /// @arg TIM_ICSelection_TRC: TIM Input 2 is selected to be connected to TRC.
  1621. /// @param filter: Specifies the Input Capture Filter.
  1622. /// This parameter must be a value between 0x00 and 0x0F.
  1623. /// @retval None.
  1624. ////////////////////////////////////////////////////////////////////////////////
  1625. static void TI2_Configure(TIM_TypeDef* tim, u16 polarity, u16 selection, u16 filter)
  1626. {
  1627. MODIFY_REG(tim->CCMR1, TIM_CCMR1_CC2S | TIM_CCMR1_IC2F, (filter << 12) | (selection << 8));
  1628. MODIFY_REG(tim->CCER, TIM_CCER_CC2EN | TIM_CCER_CC2P, (polarity << 4) | TIM_CCER_CC2EN);
  1629. }
  1630. ////////////////////////////////////////////////////////////////////////////////
  1631. /// @brief Configure the TI3 as Input.
  1632. /// @param tim: select the TIM peripheral.
  1633. /// @param polarity : The Input Polarity.
  1634. /// This parameter can be one of the following values:
  1635. /// @arg TIM_ICPolarity_Rising
  1636. /// @arg TIM_ICPolarity_Falling
  1637. /// @param selection: specifies the input to be used.
  1638. /// This parameter can be one of the following values:
  1639. /// @arg TIM_ICSelection_DirectTI: TIM Input 3 is selected to be connected to IC3.
  1640. /// @arg TIM_ICSelection_IndirectTI: TIM Input 3 is selected to be connected to IC4.
  1641. /// @arg TIM_ICSelection_TRC: TIM Input 3 is selected to be connected to TRC.
  1642. /// @param filter: Specifies the Input Capture Filter.
  1643. /// This parameter must be a value between 0x00 and 0x0F.
  1644. /// @retval None.
  1645. ////////////////////////////////////////////////////////////////////////////////
  1646. static void TI3_Configure(TIM_TypeDef* tim, u16 polarity, u16 selection, u16 filter)
  1647. {
  1648. MODIFY_REG(tim->CCMR2, TIM_CCMR2_CC3S | TIM_CCMR2_IC3F, (filter << 4) | selection);
  1649. MODIFY_REG(tim->CCER, TIM_CCER_CC3EN | TIM_CCER_CC3P, (polarity << 8) | TIM_CCER_CC3EN);
  1650. }
  1651. ////////////////////////////////////////////////////////////////////////////////
  1652. /// @brief Configure the TI4 as Input.
  1653. /// @param tim: select the TIM peripheral.
  1654. /// @param polarity : The Input Polarity.
  1655. /// This parameter can be one of the following values:
  1656. /// @arg TIM_ICPolarity_Rising
  1657. /// @arg TIM_ICPolarity_Falling
  1658. /// @param selection: specifies the input to be used.
  1659. /// This parameter can be one of the following values:
  1660. /// @arg TIM_ICSelection_DirectTI: TIM Input 4 is selected to be connected to IC4.
  1661. /// @arg TIM_ICSelection_IndirectTI: TIM Input 4 is selected to be connected to IC3.
  1662. /// @arg TIM_ICSelection_TRC: TIM Input 4 is selected to be connected to TRC.
  1663. /// @param filter: Specifies the Input Capture Filter.
  1664. /// This parameter must be a value between 0x00 and 0x0F.
  1665. /// @retval None.
  1666. ////////////////////////////////////////////////////////////////////////////////
  1667. static void TI4_Configure(TIM_TypeDef* tim, u16 polarity, u16 selection, u16 filter)
  1668. {
  1669. MODIFY_REG(tim->CCMR2, TIM_CCMR2_CC4S | TIM_CCMR2_IC4F, (filter << 12) | (selection << 8));
  1670. MODIFY_REG(tim->CCER, TIM_CCER_CC4EN | TIM_CCER_CC4P, (polarity << 12) | TIM_CCER_CC4EN);
  1671. }
  1672. ////////////////////////////////////////////////////////////////////////////////
  1673. /// @brief Enables or disables the specified TIM PWM shift /DMA reqeat.
  1674. /// @param tim: select the tim peripheral.
  1675. /// @param it: Specifies the TIM PWM shift channel to enable or disable.
  1676. /// This parameter can be any combination of the following values:
  1677. /// @arg TIM_PDER_CCDREPE: TIM DMA reqeat enable bit
  1678. /// @arg TIM_PDER_CCR1SHIFTEN: TIM Channel 1 output PWM phase shift enable bit
  1679. /// @arg TIM_PDER_CCR2SHIFTEN: TIM Channel 2 output PWM phase shift enable bit
  1680. /// @arg TIM_PDER_CCR3SHIFTEN: TIM Channel 3 output PWM phase shift enable bit
  1681. /// @arg TIM_PDER_CCR4SHIFTEN: TIM Channel 4 output PWM phase shift enable bit
  1682. /// @arg TIM_PDER_CCR5SHIFTEN: TIM Channel 5 output PWM phase shift enable bit
  1683. /// @param state: new state of the TIM interrupts.
  1684. /// This parameter can be: ENABLE or DISABLE.
  1685. /// @retval None.
  1686. ////////////////////////////////////////////////////////////////////////////////
  1687. void TIM_PWMShiftConfig(TIM_TypeDef* tim, u32 it, FunctionalState state)//TIMIT_TypeDef
  1688. {
  1689. (state) ? SET_BIT(tim->PDER, it) : CLEAR_BIT(tim->PDER, it);
  1690. }
  1691. ////////////////////////////////////////////////////////////////////////////////
  1692. /// @brief Sets the tim CCR1 shift Register value
  1693. /// @param tim: select the TIM peripheral.
  1694. /// @param compare: specifies the Capture Compare1 register new value.
  1695. /// @retval None.
  1696. ////////////////////////////////////////////////////////////////////////////////
  1697. void TIM_SetCCR1FALL(TIM_TypeDef* tim, u32 shift)
  1698. {
  1699. if (tim == TIM1)
  1700. WRITE_REG(tim->CCR1FALL, (u32)shift);
  1701. }
  1702. ////////////////////////////////////////////////////////////////////////////////
  1703. /// @brief Sets the tim CCR2 shift Register value
  1704. /// @param tim: select the TIM peripheral.
  1705. /// @param compare: specifies the Capture Compare1 register new value.
  1706. /// @retval None.
  1707. ////////////////////////////////////////////////////////////////////////////////
  1708. void TIM_SetCCR2FALL(TIM_TypeDef* tim, u32 shift)
  1709. {
  1710. if (tim == TIM1)
  1711. WRITE_REG(tim->CCR2FALL, (u32)shift);
  1712. }
  1713. ////////////////////////////////////////////////////////////////////////////////
  1714. /// @brief Sets the tim CCR3 shift Register value
  1715. /// @param tim: select the TIM peripheral.
  1716. /// @param compare: specifies the Capture Compare1 register new value.
  1717. /// @retval None.
  1718. ////////////////////////////////////////////////////////////////////////////////
  1719. void TIM_SetCCR3FALL(TIM_TypeDef* tim, u32 shift)
  1720. {
  1721. if (tim == TIM1)
  1722. WRITE_REG(tim->CCR3FALL, (u32)shift);
  1723. }
  1724. ////////////////////////////////////////////////////////////////////////////////
  1725. /// @brief Sets the tim CCR4 shift Register value
  1726. /// @param tim: select the TIM peripheral.
  1727. /// @param compare: specifies the Capture Compare1 register new value.
  1728. /// @retval None.
  1729. ////////////////////////////////////////////////////////////////////////////////
  1730. void TIM_SetCCR4FALL(TIM_TypeDef* tim, u32 shift)
  1731. {
  1732. if (tim == TIM1)
  1733. WRITE_REG(tim->CCR4FALL, (u32)shift);
  1734. }
  1735. ////////////////////////////////////////////////////////////////////////////////
  1736. /// @brief Sets the tim CCR5 shift Register value
  1737. /// @param tim: select the TIM peripheral.
  1738. /// @param compare: specifies the Capture Compare1 register new value.
  1739. /// @retval None.
  1740. ////////////////////////////////////////////////////////////////////////////////
  1741. void TIM_SetCCR5FALL(TIM_TypeDef* tim, u32 shift)
  1742. {
  1743. if (tim == TIM1)
  1744. WRITE_REG(tim->CCR5FALL, (u32)shift);
  1745. }
  1746. /// @}
  1747. /// @}
  1748. /// @}