stm8s_tim2.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /**
  2. ******************************************************************************
  3. * @file stm8s_tim2.c
  4. * @author MCD Application Team
  5. * @version V2.3.0
  6. * @date 16-June-2017
  7. * @brief This file contains all the functions for the TIM2 peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm8s_tim2.h"
  29. /** @addtogroup STM8S_StdPeriph_Driver
  30. * @{
  31. */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private function prototypes -----------------------------------------------*/
  37. static void TI1_Config(uint8_t TIM2_ICPolarity, uint8_t TIM2_ICSelection, uint8_t TIM2_ICFilter);
  38. static void TI2_Config(uint8_t TIM2_ICPolarity, uint8_t TIM2_ICSelection, uint8_t TIM2_ICFilter);
  39. static void TI3_Config(uint8_t TIM2_ICPolarity, uint8_t TIM2_ICSelection, uint8_t TIM2_ICFilter);
  40. /**
  41. * @addtogroup TIM2_Public_Functions
  42. * @{
  43. */
  44. /**
  45. * @brief Deinitializes the TIM2 peripheral registers to their default reset values.
  46. * @param None
  47. * @retval None
  48. */
  49. void TIM2_DeInit(void)
  50. {
  51. TIM2->CR1 = (uint8_t)TIM2_CR1_RESET_VALUE;
  52. TIM2->IER = (uint8_t)TIM2_IER_RESET_VALUE;
  53. TIM2->SR2 = (uint8_t)TIM2_SR2_RESET_VALUE;
  54. /* Disable channels */
  55. TIM2->CCER1 = (uint8_t)TIM2_CCER1_RESET_VALUE;
  56. TIM2->CCER2 = (uint8_t)TIM2_CCER2_RESET_VALUE;
  57. /* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
  58. TIM2->CCER1 = (uint8_t)TIM2_CCER1_RESET_VALUE;
  59. TIM2->CCER2 = (uint8_t)TIM2_CCER2_RESET_VALUE;
  60. TIM2->CCMR1 = (uint8_t)TIM2_CCMR1_RESET_VALUE;
  61. TIM2->CCMR2 = (uint8_t)TIM2_CCMR2_RESET_VALUE;
  62. TIM2->CCMR3 = (uint8_t)TIM2_CCMR3_RESET_VALUE;
  63. TIM2->CNTRH = (uint8_t)TIM2_CNTRH_RESET_VALUE;
  64. TIM2->CNTRL = (uint8_t)TIM2_CNTRL_RESET_VALUE;
  65. TIM2->PSCR = (uint8_t)TIM2_PSCR_RESET_VALUE;
  66. TIM2->ARRH = (uint8_t)TIM2_ARRH_RESET_VALUE;
  67. TIM2->ARRL = (uint8_t)TIM2_ARRL_RESET_VALUE;
  68. TIM2->CCR1H = (uint8_t)TIM2_CCR1H_RESET_VALUE;
  69. TIM2->CCR1L = (uint8_t)TIM2_CCR1L_RESET_VALUE;
  70. TIM2->CCR2H = (uint8_t)TIM2_CCR2H_RESET_VALUE;
  71. TIM2->CCR2L = (uint8_t)TIM2_CCR2L_RESET_VALUE;
  72. TIM2->CCR3H = (uint8_t)TIM2_CCR3H_RESET_VALUE;
  73. TIM2->CCR3L = (uint8_t)TIM2_CCR3L_RESET_VALUE;
  74. TIM2->SR1 = (uint8_t)TIM2_SR1_RESET_VALUE;
  75. }
  76. /**
  77. * @brief Initializes the TIM2 Time Base Unit according to the specified parameters.
  78. * @param TIM2_Prescaler specifies the Prescaler from TIM2_Prescaler_TypeDef.
  79. * @param TIM2_Period specifies the Period value.
  80. * @retval None
  81. */
  82. void TIM2_TimeBaseInit( TIM2_Prescaler_TypeDef TIM2_Prescaler,
  83. uint16_t TIM2_Period)
  84. {
  85. /* Set the Prescaler value */
  86. TIM2->PSCR = (uint8_t)(TIM2_Prescaler);
  87. /* Set the Autoreload value */
  88. TIM2->ARRH = (uint8_t)(TIM2_Period >> 8);
  89. TIM2->ARRL = (uint8_t)(TIM2_Period);
  90. }
  91. /**
  92. * @brief Initializes the TIM2 Channel1 according to the specified parameters.
  93. * @param TIM2_OCMode specifies the Output Compare mode from @ref TIM2_OCMode_TypeDef.
  94. * @param TIM2_OutputState specifies the Output State from @ref TIM2_OutputState_TypeDef.
  95. * @param TIM2_Pulse specifies the Pulse width value.
  96. * @param TIM2_OCPolarity specifies the Output Compare Polarity from @ref TIM2_OCPolarity_TypeDef.
  97. * @retval None
  98. */
  99. void TIM2_OC1Init(TIM2_OCMode_TypeDef TIM2_OCMode,
  100. TIM2_OutputState_TypeDef TIM2_OutputState,
  101. uint16_t TIM2_Pulse,
  102. TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
  103. {
  104. /* Check the parameters */
  105. assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
  106. assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
  107. assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));
  108. /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
  109. TIM2->CCER1 &= (uint8_t)(~( TIM2_CCER1_CC1E | TIM2_CCER1_CC1P));
  110. /* Set the Output State & Set the Output Polarity */
  111. TIM2->CCER1 |= (uint8_t)((uint8_t)(TIM2_OutputState & TIM2_CCER1_CC1E ) |
  112. (uint8_t)(TIM2_OCPolarity & TIM2_CCER1_CC1P));
  113. /* Reset the Output Compare Bits & Set the Ouput Compare Mode */
  114. TIM2->CCMR1 = (uint8_t)((uint8_t)(TIM2->CCMR1 & (uint8_t)(~TIM2_CCMR_OCM)) |
  115. (uint8_t)TIM2_OCMode);
  116. /* Set the Pulse value */
  117. TIM2->CCR1H = (uint8_t)(TIM2_Pulse >> 8);
  118. TIM2->CCR1L = (uint8_t)(TIM2_Pulse);
  119. }
  120. /**
  121. * @brief Initializes the TIM2 Channel2 according to the specified parameters.
  122. * @param TIM2_OCMode specifies the Output Compare mode from @ref TIM2_OCMode_TypeDef.
  123. * @param TIM2_OutputState specifies the Output State from @ref TIM2_OutputState_TypeDef.
  124. * @param TIM2_Pulse specifies the Pulse width value.
  125. * @param TIM2_OCPolarity specifies the Output Compare Polarity from @ref TIM2_OCPolarity_TypeDef.
  126. * @retval None
  127. */
  128. void TIM2_OC2Init(TIM2_OCMode_TypeDef TIM2_OCMode,
  129. TIM2_OutputState_TypeDef TIM2_OutputState,
  130. uint16_t TIM2_Pulse,
  131. TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
  132. {
  133. /* Check the parameters */
  134. assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
  135. assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
  136. assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));
  137. /* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
  138. TIM2->CCER1 &= (uint8_t)(~( TIM2_CCER1_CC2E | TIM2_CCER1_CC2P ));
  139. /* Set the Output State & Set the Output Polarity */
  140. TIM2->CCER1 |= (uint8_t)((uint8_t)(TIM2_OutputState & TIM2_CCER1_CC2E ) |
  141. (uint8_t)(TIM2_OCPolarity & TIM2_CCER1_CC2P));
  142. /* Reset the Output Compare Bits & Set the Output Compare Mode */
  143. TIM2->CCMR2 = (uint8_t)((uint8_t)(TIM2->CCMR2 & (uint8_t)(~TIM2_CCMR_OCM)) |
  144. (uint8_t)TIM2_OCMode);
  145. /* Set the Pulse value */
  146. TIM2->CCR2H = (uint8_t)(TIM2_Pulse >> 8);
  147. TIM2->CCR2L = (uint8_t)(TIM2_Pulse);
  148. }
  149. /**
  150. * @brief Initializes the TIM2 Channel3 according to the specified parameters.
  151. * @param TIM2_OCMode specifies the Output Compare mode from @ref TIM2_OCMode_TypeDef.
  152. * @param TIM2_OutputState specifies the Output State from @ref TIM2_OutputState_TypeDef.
  153. * @param TIM2_Pulse specifies the Pulse width value.
  154. * @param TIM2_OCPolarity specifies the Output Compare Polarity from @ref TIM2_OCPolarity_TypeDef.
  155. * @retval None
  156. */
  157. void TIM2_OC3Init(TIM2_OCMode_TypeDef TIM2_OCMode,
  158. TIM2_OutputState_TypeDef TIM2_OutputState,
  159. uint16_t TIM2_Pulse,
  160. TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
  161. {
  162. /* Check the parameters */
  163. assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
  164. assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
  165. assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));
  166. /* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
  167. TIM2->CCER2 &= (uint8_t)(~( TIM2_CCER2_CC3E | TIM2_CCER2_CC3P));
  168. /* Set the Output State & Set the Output Polarity */
  169. TIM2->CCER2 |= (uint8_t)((uint8_t)(TIM2_OutputState & TIM2_CCER2_CC3E) |
  170. (uint8_t)(TIM2_OCPolarity & TIM2_CCER2_CC3P));
  171. /* Reset the Output Compare Bits & Set the Output Compare Mode */
  172. TIM2->CCMR3 = (uint8_t)((uint8_t)(TIM2->CCMR3 & (uint8_t)(~TIM2_CCMR_OCM)) |
  173. (uint8_t)TIM2_OCMode);
  174. /* Set the Pulse value */
  175. TIM2->CCR3H = (uint8_t)(TIM2_Pulse >> 8);
  176. TIM2->CCR3L = (uint8_t)(TIM2_Pulse);
  177. }
  178. /**
  179. * @brief Initializes the TIM2 peripheral according to the specified parameters.
  180. * @param TIM2_Channel specifies the Input Capture Channel from @ref TIM2_Channel_TypeDef.
  181. * @param TIM2_ICPolarity specifies the Input Capture Polarity from @ref TIM2_ICPolarity_TypeDef.
  182. * @param TIM2_ICSelection specifies the Input Capture Selection from @ref TIM2_ICSelection_TypeDef.
  183. * @param TIM2_ICPrescaler specifies the Input Capture Prescaler from @ref TIM2_ICPSC_TypeDef.
  184. * @param TIM2_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  185. * @retval None
  186. */
  187. void TIM2_ICInit(TIM2_Channel_TypeDef TIM2_Channel,
  188. TIM2_ICPolarity_TypeDef TIM2_ICPolarity,
  189. TIM2_ICSelection_TypeDef TIM2_ICSelection,
  190. TIM2_ICPSC_TypeDef TIM2_ICPrescaler,
  191. uint8_t TIM2_ICFilter)
  192. {
  193. /* Check the parameters */
  194. assert_param(IS_TIM2_CHANNEL_OK(TIM2_Channel));
  195. assert_param(IS_TIM2_IC_POLARITY_OK(TIM2_ICPolarity));
  196. assert_param(IS_TIM2_IC_SELECTION_OK(TIM2_ICSelection));
  197. assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_ICPrescaler));
  198. assert_param(IS_TIM2_IC_FILTER_OK(TIM2_ICFilter));
  199. if (TIM2_Channel == TIM2_CHANNEL_1)
  200. {
  201. /* TI1 Configuration */
  202. TI1_Config((uint8_t)TIM2_ICPolarity,
  203. (uint8_t)TIM2_ICSelection,
  204. (uint8_t)TIM2_ICFilter);
  205. /* Set the Input Capture Prescaler value */
  206. TIM2_SetIC1Prescaler(TIM2_ICPrescaler);
  207. }
  208. else if (TIM2_Channel == TIM2_CHANNEL_2)
  209. {
  210. /* TI2 Configuration */
  211. TI2_Config((uint8_t)TIM2_ICPolarity,
  212. (uint8_t)TIM2_ICSelection,
  213. (uint8_t)TIM2_ICFilter);
  214. /* Set the Input Capture Prescaler value */
  215. TIM2_SetIC2Prescaler(TIM2_ICPrescaler);
  216. }
  217. else
  218. {
  219. /* TI3 Configuration */
  220. TI3_Config((uint8_t)TIM2_ICPolarity,
  221. (uint8_t)TIM2_ICSelection,
  222. (uint8_t)TIM2_ICFilter);
  223. /* Set the Input Capture Prescaler value */
  224. TIM2_SetIC3Prescaler(TIM2_ICPrescaler);
  225. }
  226. }
  227. /**
  228. * @brief Configures the TIM2 peripheral in PWM Input Mode according to the specified parameters.
  229. * @param TIM2_Channel specifies the Input Capture Channel from @ref TIM2_Channel_TypeDef.
  230. * @param TIM2_ICPolarity specifies the Input Capture Polarity from @ref TIM2_ICPolarity_TypeDef.
  231. * @param TIM2_ICSelection specifies the Input Capture Selection from @ref TIM2_ICSelection_TypeDef.
  232. * @param TIM2_ICPrescaler specifies the Input Capture Prescaler from @ref TIM2_ICPSC_TypeDef.
  233. * @param TIM2_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  234. * @retval None
  235. */
  236. void TIM2_PWMIConfig(TIM2_Channel_TypeDef TIM2_Channel,
  237. TIM2_ICPolarity_TypeDef TIM2_ICPolarity,
  238. TIM2_ICSelection_TypeDef TIM2_ICSelection,
  239. TIM2_ICPSC_TypeDef TIM2_ICPrescaler,
  240. uint8_t TIM2_ICFilter)
  241. {
  242. uint8_t icpolarity = (uint8_t)TIM2_ICPOLARITY_RISING;
  243. uint8_t icselection = (uint8_t)TIM2_ICSELECTION_DIRECTTI;
  244. /* Check the parameters */
  245. assert_param(IS_TIM2_PWMI_CHANNEL_OK(TIM2_Channel));
  246. assert_param(IS_TIM2_IC_POLARITY_OK(TIM2_ICPolarity));
  247. assert_param(IS_TIM2_IC_SELECTION_OK(TIM2_ICSelection));
  248. assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_ICPrescaler));
  249. /* Select the Opposite Input Polarity */
  250. if (TIM2_ICPolarity != TIM2_ICPOLARITY_FALLING)
  251. {
  252. icpolarity = (uint8_t)TIM2_ICPOLARITY_FALLING;
  253. }
  254. else
  255. {
  256. icpolarity = (uint8_t)TIM2_ICPOLARITY_RISING;
  257. }
  258. /* Select the Opposite Input */
  259. if (TIM2_ICSelection == TIM2_ICSELECTION_DIRECTTI)
  260. {
  261. icselection = (uint8_t)TIM2_ICSELECTION_INDIRECTTI;
  262. }
  263. else
  264. {
  265. icselection = (uint8_t)TIM2_ICSELECTION_DIRECTTI;
  266. }
  267. if (TIM2_Channel == TIM2_CHANNEL_1)
  268. {
  269. /* TI1 Configuration */
  270. TI1_Config((uint8_t)TIM2_ICPolarity, (uint8_t)TIM2_ICSelection,
  271. (uint8_t)TIM2_ICFilter);
  272. /* Set the Input Capture Prescaler value */
  273. TIM2_SetIC1Prescaler(TIM2_ICPrescaler);
  274. /* TI2 Configuration */
  275. TI2_Config(icpolarity, icselection, TIM2_ICFilter);
  276. /* Set the Input Capture Prescaler value */
  277. TIM2_SetIC2Prescaler(TIM2_ICPrescaler);
  278. }
  279. else
  280. {
  281. /* TI2 Configuration */
  282. TI2_Config((uint8_t)TIM2_ICPolarity, (uint8_t)TIM2_ICSelection,
  283. (uint8_t)TIM2_ICFilter);
  284. /* Set the Input Capture Prescaler value */
  285. TIM2_SetIC2Prescaler(TIM2_ICPrescaler);
  286. /* TI1 Configuration */
  287. TI1_Config((uint8_t)icpolarity, icselection, (uint8_t)TIM2_ICFilter);
  288. /* Set the Input Capture Prescaler value */
  289. TIM2_SetIC1Prescaler(TIM2_ICPrescaler);
  290. }
  291. }
  292. /**
  293. * @brief Enables or disables the TIM2 peripheral.
  294. * @param NewState new state of the TIM2 peripheral. This parameter can
  295. * be ENABLE or DISABLE.
  296. * @retval None
  297. */
  298. void TIM2_Cmd(FunctionalState NewState)
  299. {
  300. /* Check the parameters */
  301. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  302. /* set or Reset the CEN Bit */
  303. if (NewState != DISABLE)
  304. {
  305. TIM2->CR1 |= (uint8_t)TIM2_CR1_CEN;
  306. }
  307. else
  308. {
  309. TIM2->CR1 &= (uint8_t)(~TIM2_CR1_CEN);
  310. }
  311. }
  312. /**
  313. * @brief Enables or disables the specified TIM2 interrupts.
  314. * @param NewState new state of the TIM2 peripheral.
  315. * This parameter can be: ENABLE or DISABLE.
  316. * @param TIM2_IT specifies the TIM2 interrupts sources to be enabled or disabled.
  317. * This parameter can be any combination of the following values:
  318. * - TIM2_IT_UPDATE: TIM2 update Interrupt source
  319. * - TIM2_IT_CC1: TIM2 Capture Compare 1 Interrupt source
  320. * - TIM2_IT_CC2: TIM2 Capture Compare 2 Interrupt source
  321. * - TIM2_IT_CC3: TIM2 Capture Compare 3 Interrupt source
  322. * @param NewState new state of the TIM2 peripheral.
  323. * @retval None
  324. */
  325. void TIM2_ITConfig(TIM2_IT_TypeDef TIM2_IT, FunctionalState NewState)
  326. {
  327. /* Check the parameters */
  328. assert_param(IS_TIM2_IT_OK(TIM2_IT));
  329. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  330. if (NewState != DISABLE)
  331. {
  332. /* Enable the Interrupt sources */
  333. TIM2->IER |= (uint8_t)TIM2_IT;
  334. }
  335. else
  336. {
  337. /* Disable the Interrupt sources */
  338. TIM2->IER &= (uint8_t)(~TIM2_IT);
  339. }
  340. }
  341. /**
  342. * @brief Enables or Disables the TIM2 Update event.
  343. * @param NewState new state of the TIM2 peripheral Preload register. This parameter can
  344. * be ENABLE or DISABLE.
  345. * @retval None
  346. */
  347. void TIM2_UpdateDisableConfig(FunctionalState NewState)
  348. {
  349. /* Check the parameters */
  350. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  351. /* Set or Reset the UDIS Bit */
  352. if (NewState != DISABLE)
  353. {
  354. TIM2->CR1 |= (uint8_t)TIM2_CR1_UDIS;
  355. }
  356. else
  357. {
  358. TIM2->CR1 &= (uint8_t)(~TIM2_CR1_UDIS);
  359. }
  360. }
  361. /**
  362. * @brief Selects the TIM2 Update Request Interrupt source.
  363. * @param TIM2_UpdateSource specifies the Update source.
  364. * This parameter can be one of the following values
  365. * - TIM2_UPDATESOURCE_REGULAR
  366. * - TIM2_UPDATESOURCE_GLOBAL
  367. * @retval None
  368. */
  369. void TIM2_UpdateRequestConfig(TIM2_UpdateSource_TypeDef TIM2_UpdateSource)
  370. {
  371. /* Check the parameters */
  372. assert_param(IS_TIM2_UPDATE_SOURCE_OK(TIM2_UpdateSource));
  373. /* Set or Reset the URS Bit */
  374. if (TIM2_UpdateSource != TIM2_UPDATESOURCE_GLOBAL)
  375. {
  376. TIM2->CR1 |= (uint8_t)TIM2_CR1_URS;
  377. }
  378. else
  379. {
  380. TIM2->CR1 &= (uint8_t)(~TIM2_CR1_URS);
  381. }
  382. }
  383. /**
  384. * @brief Selects the TIM2’s One Pulse Mode.
  385. * @param TIM2_OPMode specifies the OPM Mode to be used.
  386. * This parameter can be one of the following values
  387. * - TIM2_OPMODE_SINGLE
  388. * - TIM2_OPMODE_REPETITIVE
  389. * @retval None
  390. */
  391. void TIM2_SelectOnePulseMode(TIM2_OPMode_TypeDef TIM2_OPMode)
  392. {
  393. /* Check the parameters */
  394. assert_param(IS_TIM2_OPM_MODE_OK(TIM2_OPMode));
  395. /* Set or Reset the OPM Bit */
  396. if (TIM2_OPMode != TIM2_OPMODE_REPETITIVE)
  397. {
  398. TIM2->CR1 |= (uint8_t)TIM2_CR1_OPM;
  399. }
  400. else
  401. {
  402. TIM2->CR1 &= (uint8_t)(~TIM2_CR1_OPM);
  403. }
  404. }
  405. /**
  406. * @brief Configures the TIM2 Prescaler.
  407. * @param Prescaler specifies the Prescaler Register value
  408. * This parameter can be one of the following values
  409. * - TIM2_PRESCALER_1
  410. * - TIM2_PRESCALER_2
  411. * - TIM2_PRESCALER_4
  412. * - TIM2_PRESCALER_8
  413. * - TIM2_PRESCALER_16
  414. * - TIM2_PRESCALER_32
  415. * - TIM2_PRESCALER_64
  416. * - TIM2_PRESCALER_128
  417. * - TIM2_PRESCALER_256
  418. * - TIM2_PRESCALER_512
  419. * - TIM2_PRESCALER_1024
  420. * - TIM2_PRESCALER_2048
  421. * - TIM2_PRESCALER_4096
  422. * - TIM2_PRESCALER_8192
  423. * - TIM2_PRESCALER_16384
  424. * - TIM2_PRESCALER_32768
  425. * @param TIM2_PSCReloadMode specifies the TIM2 Prescaler Reload mode.
  426. * This parameter can be one of the following values
  427. * - TIM2_PSCRELOADMODE_IMMEDIATE: The Prescaler is loaded
  428. * immediately.
  429. * - TIM2_PSCRELOADMODE_UPDATE: The Prescaler is loaded at
  430. * the update event.
  431. * @retval None
  432. */
  433. void TIM2_PrescalerConfig(TIM2_Prescaler_TypeDef Prescaler,
  434. TIM2_PSCReloadMode_TypeDef TIM2_PSCReloadMode)
  435. {
  436. /* Check the parameters */
  437. assert_param(IS_TIM2_PRESCALER_RELOAD_OK(TIM2_PSCReloadMode));
  438. assert_param(IS_TIM2_PRESCALER_OK(Prescaler));
  439. /* Set the Prescaler value */
  440. TIM2->PSCR = (uint8_t)Prescaler;
  441. /* Set or reset the UG Bit */
  442. TIM2->EGR = (uint8_t)TIM2_PSCReloadMode;
  443. }
  444. /**
  445. * @brief Forces the TIM2 Channel1 output waveform to active or inactive level.
  446. * @param TIM2_ForcedAction specifies the forced Action to be set to the output waveform.
  447. * This parameter can be one of the following values:
  448. * - TIM2_FORCEDACTION_ACTIVE: Force active level on OC1REF
  449. * - TIM2_FORCEDACTION_INACTIVE: Force inactive level on
  450. * OC1REF.
  451. * @retval None
  452. */
  453. void TIM2_ForcedOC1Config(TIM2_ForcedAction_TypeDef TIM2_ForcedAction)
  454. {
  455. /* Check the parameters */
  456. assert_param(IS_TIM2_FORCED_ACTION_OK(TIM2_ForcedAction));
  457. /* Reset the OCM Bits & Configure the Forced output Mode */
  458. TIM2->CCMR1 = (uint8_t)((uint8_t)(TIM2->CCMR1 & (uint8_t)(~TIM2_CCMR_OCM))
  459. | (uint8_t)TIM2_ForcedAction);
  460. }
  461. /**
  462. * @brief Forces the TIM2 Channel2 output waveform to active or inactive level.
  463. * @param TIM2_ForcedAction specifies the forced Action to be set to the output waveform.
  464. * This parameter can be one of the following values:
  465. * - TIM2_FORCEDACTION_ACTIVE: Force active level on OC2REF
  466. * - TIM2_FORCEDACTION_INACTIVE: Force inactive level on
  467. * OC2REF.
  468. * @retval None
  469. */
  470. void TIM2_ForcedOC2Config(TIM2_ForcedAction_TypeDef TIM2_ForcedAction)
  471. {
  472. /* Check the parameters */
  473. assert_param(IS_TIM2_FORCED_ACTION_OK(TIM2_ForcedAction));
  474. /* Reset the OCM Bits & Configure the Forced output Mode */
  475. TIM2->CCMR2 = (uint8_t)((uint8_t)(TIM2->CCMR2 & (uint8_t)(~TIM2_CCMR_OCM))
  476. | (uint8_t)TIM2_ForcedAction);
  477. }
  478. /**
  479. * @brief Forces the TIM2 Channel3 output waveform to active or inactive level.
  480. * @param TIM2_ForcedAction specifies the forced Action to be set to the output waveform.
  481. * This parameter can be one of the following values:
  482. * - TIM2_FORCEDACTION_ACTIVE: Force active level on OC3REF
  483. * - TIM2_FORCEDACTION_INACTIVE: Force inactive level on
  484. * OC3REF.
  485. * @retval None
  486. */
  487. void TIM2_ForcedOC3Config(TIM2_ForcedAction_TypeDef TIM2_ForcedAction)
  488. {
  489. /* Check the parameters */
  490. assert_param(IS_TIM2_FORCED_ACTION_OK(TIM2_ForcedAction));
  491. /* Reset the OCM Bits & Configure the Forced output Mode */
  492. TIM2->CCMR3 = (uint8_t)((uint8_t)(TIM2->CCMR3 & (uint8_t)(~TIM2_CCMR_OCM))
  493. | (uint8_t)TIM2_ForcedAction);
  494. }
  495. /**
  496. * @brief Enables or disables TIM2 peripheral Preload register on ARR.
  497. * @param NewState new state of the TIM2 peripheral Preload register.
  498. * This parameter can be ENABLE or DISABLE.
  499. * @retval None
  500. */
  501. void TIM2_ARRPreloadConfig(FunctionalState NewState)
  502. {
  503. /* Check the parameters */
  504. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  505. /* Set or Reset the ARPE Bit */
  506. if (NewState != DISABLE)
  507. {
  508. TIM2->CR1 |= (uint8_t)TIM2_CR1_ARPE;
  509. }
  510. else
  511. {
  512. TIM2->CR1 &= (uint8_t)(~TIM2_CR1_ARPE);
  513. }
  514. }
  515. /**
  516. * @brief Enables or disables the TIM2 peripheral Preload Register on CCR1.
  517. * @param NewState new state of the Capture Compare Preload register.
  518. * This parameter can be ENABLE or DISABLE.
  519. * @retval None
  520. */
  521. void TIM2_OC1PreloadConfig(FunctionalState NewState)
  522. {
  523. /* Check the parameters */
  524. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  525. /* Set or Reset the OC1PE Bit */
  526. if (NewState != DISABLE)
  527. {
  528. TIM2->CCMR1 |= (uint8_t)TIM2_CCMR_OCxPE;
  529. }
  530. else
  531. {
  532. TIM2->CCMR1 &= (uint8_t)(~TIM2_CCMR_OCxPE);
  533. }
  534. }
  535. /**
  536. * @brief Enables or disables the TIM2 peripheral Preload Register on CCR2.
  537. * @param NewState new state of the Capture Compare Preload register.
  538. * This parameter can be ENABLE or DISABLE.
  539. * @retval None
  540. */
  541. void TIM2_OC2PreloadConfig(FunctionalState NewState)
  542. {
  543. /* Check the parameters */
  544. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  545. /* Set or Reset the OC2PE Bit */
  546. if (NewState != DISABLE)
  547. {
  548. TIM2->CCMR2 |= (uint8_t)TIM2_CCMR_OCxPE;
  549. }
  550. else
  551. {
  552. TIM2->CCMR2 &= (uint8_t)(~TIM2_CCMR_OCxPE);
  553. }
  554. }
  555. /**
  556. * @brief Enables or disables the TIM2 peripheral Preload Register on CCR3.
  557. * @param NewState new state of the Capture Compare Preload register.
  558. * This parameter can be ENABLE or DISABLE.
  559. * @retval None
  560. */
  561. void TIM2_OC3PreloadConfig(FunctionalState NewState)
  562. {
  563. /* Check the parameters */
  564. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  565. /* Set or Reset the OC3PE Bit */
  566. if (NewState != DISABLE)
  567. {
  568. TIM2->CCMR3 |= (uint8_t)TIM2_CCMR_OCxPE;
  569. }
  570. else
  571. {
  572. TIM2->CCMR3 &= (uint8_t)(~TIM2_CCMR_OCxPE);
  573. }
  574. }
  575. /**
  576. * @brief Configures the TIM2 event to be generated by software.
  577. * @param TIM2_EventSource specifies the event source.
  578. * This parameter can be one of the following values:
  579. * - TIM2_EVENTSOURCE_UPDATE: TIM2 update Event source
  580. * - TIM2_EVENTSOURCE_CC1: TIM2 Capture Compare 1 Event source
  581. * - TIM2_EVENTSOURCE_CC2: TIM2 Capture Compare 2 Event source
  582. * - TIM2_EVENTSOURCE_CC3: TIM2 Capture Compare 3 Event source
  583. * @retval None
  584. */
  585. void TIM2_GenerateEvent(TIM2_EventSource_TypeDef TIM2_EventSource)
  586. {
  587. /* Check the parameters */
  588. assert_param(IS_TIM2_EVENT_SOURCE_OK(TIM2_EventSource));
  589. /* Set the event sources */
  590. TIM2->EGR = (uint8_t)TIM2_EventSource;
  591. }
  592. /**
  593. * @brief Configures the TIM2 Channel 1 polarity.
  594. * @param TIM2_OCPolarity specifies the OC1 Polarity.
  595. * This parameter can be one of the following values:
  596. * - TIM2_OCPOLARITY_LOW: Output Compare active low
  597. * - TIM2_OCPOLARITY_HIGH: Output Compare active high
  598. * @retval None
  599. */
  600. void TIM2_OC1PolarityConfig(TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
  601. {
  602. /* Check the parameters */
  603. assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));
  604. /* Set or Reset the CC1P Bit */
  605. if (TIM2_OCPolarity != TIM2_OCPOLARITY_HIGH)
  606. {
  607. TIM2->CCER1 |= (uint8_t)TIM2_CCER1_CC1P;
  608. }
  609. else
  610. {
  611. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC1P);
  612. }
  613. }
  614. /**
  615. * @brief Configures the TIM2 Channel 2 polarity.
  616. * @param TIM2_OCPolarity specifies the OC2 Polarity.
  617. * This parameter can be one of the following values:
  618. * - TIM2_OCPOLARITY_LOW: Output Compare active low
  619. * - TIM2_OCPOLARITY_HIGH: Output Compare active high
  620. * @retval None
  621. */
  622. void TIM2_OC2PolarityConfig(TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
  623. {
  624. /* Check the parameters */
  625. assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));
  626. /* Set or Reset the CC2P Bit */
  627. if (TIM2_OCPolarity != TIM2_OCPOLARITY_HIGH)
  628. {
  629. TIM2->CCER1 |= TIM2_CCER1_CC2P;
  630. }
  631. else
  632. {
  633. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC2P);
  634. }
  635. }
  636. /**
  637. * @brief Configures the TIM2 Channel 3 polarity.
  638. * @param TIM2_OCPolarity specifies the OC3 Polarity.
  639. * This parameter can be one of the following values:
  640. * - TIM2_OCPOLARITY_LOW: Output Compare active low
  641. * - TIM2_OCPOLARITY_HIGH: Output Compare active high
  642. * @retval None
  643. */
  644. void TIM2_OC3PolarityConfig(TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
  645. {
  646. /* Check the parameters */
  647. assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));
  648. /* Set or Reset the CC3P Bit */
  649. if (TIM2_OCPolarity != TIM2_OCPOLARITY_HIGH)
  650. {
  651. TIM2->CCER2 |= (uint8_t)TIM2_CCER2_CC3P;
  652. }
  653. else
  654. {
  655. TIM2->CCER2 &= (uint8_t)(~TIM2_CCER2_CC3P);
  656. }
  657. }
  658. /**
  659. * @brief Enables or disables the TIM2 Capture Compare Channel x.
  660. * @param TIM2_Channel specifies the TIM2 Channel.
  661. * This parameter can be one of the following values:
  662. * - TIM2_CHANNEL_1: TIM2 Channel1
  663. * - TIM2_CHANNEL_2: TIM2 Channel2
  664. * - TIM2_CHANNEL_3: TIM2 Channel3
  665. * @param NewState specifies the TIM2 Channel CCxE bit new state.
  666. * This parameter can be: ENABLE or DISABLE.
  667. * @retval None
  668. */
  669. void TIM2_CCxCmd(TIM2_Channel_TypeDef TIM2_Channel, FunctionalState NewState)
  670. {
  671. /* Check the parameters */
  672. assert_param(IS_TIM2_CHANNEL_OK(TIM2_Channel));
  673. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  674. if (TIM2_Channel == TIM2_CHANNEL_1)
  675. {
  676. /* Set or Reset the CC1E Bit */
  677. if (NewState != DISABLE)
  678. {
  679. TIM2->CCER1 |= (uint8_t)TIM2_CCER1_CC1E;
  680. }
  681. else
  682. {
  683. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC1E);
  684. }
  685. }
  686. else if (TIM2_Channel == TIM2_CHANNEL_2)
  687. {
  688. /* Set or Reset the CC2E Bit */
  689. if (NewState != DISABLE)
  690. {
  691. TIM2->CCER1 |= (uint8_t)TIM2_CCER1_CC2E;
  692. }
  693. else
  694. {
  695. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC2E);
  696. }
  697. }
  698. else
  699. {
  700. /* Set or Reset the CC3E Bit */
  701. if (NewState != DISABLE)
  702. {
  703. TIM2->CCER2 |= (uint8_t)TIM2_CCER2_CC3E;
  704. }
  705. else
  706. {
  707. TIM2->CCER2 &= (uint8_t)(~TIM2_CCER2_CC3E);
  708. }
  709. }
  710. }
  711. /**
  712. * @brief Selects the TIM2 Output Compare Mode. This function disables the
  713. * selected channel before changing the Output Compare Mode. User has to
  714. * enable this channel using TIM2_CCxCmd and TIM2_CCxNCmd functions.
  715. * @param TIM2_Channel specifies the TIM2 Channel.
  716. * This parameter can be one of the following values:
  717. * - TIM2_CHANNEL_1: TIM2 Channel1
  718. * - TIM2_CHANNEL_2: TIM2 Channel2
  719. * - TIM2_CHANNEL_3: TIM2 Channel3
  720. * @param TIM2_OCMode specifies the TIM2 Output Compare Mode.
  721. * This parameter can be one of the following values:
  722. * - TIM2_OCMODE_TIMING
  723. * - TIM2_OCMODE_ACTIVE
  724. * - TIM2_OCMODE_TOGGLE
  725. * - TIM2_OCMODE_PWM1
  726. * - TIM2_OCMODE_PWM2
  727. * - TIM2_FORCEDACTION_ACTIVE
  728. * - TIM2_FORCEDACTION_INACTIVE
  729. * @retval None
  730. */
  731. void TIM2_SelectOCxM(TIM2_Channel_TypeDef TIM2_Channel, TIM2_OCMode_TypeDef TIM2_OCMode)
  732. {
  733. /* Check the parameters */
  734. assert_param(IS_TIM2_CHANNEL_OK(TIM2_Channel));
  735. assert_param(IS_TIM2_OCM_OK(TIM2_OCMode));
  736. if (TIM2_Channel == TIM2_CHANNEL_1)
  737. {
  738. /* Disable the Channel 1: Reset the CCE Bit */
  739. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC1E);
  740. /* Reset the Output Compare Bits & Set the Output Compare Mode */
  741. TIM2->CCMR1 = (uint8_t)((uint8_t)(TIM2->CCMR1 & (uint8_t)(~TIM2_CCMR_OCM))
  742. | (uint8_t)TIM2_OCMode);
  743. }
  744. else if (TIM2_Channel == TIM2_CHANNEL_2)
  745. {
  746. /* Disable the Channel 2: Reset the CCE Bit */
  747. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC2E);
  748. /* Reset the Output Compare Bits & Set the Output Compare Mode */
  749. TIM2->CCMR2 = (uint8_t)((uint8_t)(TIM2->CCMR2 & (uint8_t)(~TIM2_CCMR_OCM))
  750. | (uint8_t)TIM2_OCMode);
  751. }
  752. else
  753. {
  754. /* Disable the Channel 3: Reset the CCE Bit */
  755. TIM2->CCER2 &= (uint8_t)(~TIM2_CCER2_CC3E);
  756. /* Reset the Output Compare Bits & Set the Output Compare Mode */
  757. TIM2->CCMR3 = (uint8_t)((uint8_t)(TIM2->CCMR3 & (uint8_t)(~TIM2_CCMR_OCM))
  758. | (uint8_t)TIM2_OCMode);
  759. }
  760. }
  761. /**
  762. * @brief Sets the TIM2 Counter Register value.
  763. * @param Counter specifies the Counter register new value.
  764. * This parameter is between 0x0000 and 0xFFFF.
  765. * @retval None
  766. */
  767. void TIM2_SetCounter(uint16_t Counter)
  768. {
  769. /* Set the Counter Register value */
  770. TIM2->CNTRH = (uint8_t)(Counter >> 8);
  771. TIM2->CNTRL = (uint8_t)(Counter);
  772. }
  773. /**
  774. * @brief Sets the TIM2 Autoreload Register value.
  775. * @param Autoreload specifies the Autoreload register new value.
  776. * This parameter is between 0x0000 and 0xFFFF.
  777. * @retval None
  778. */
  779. void TIM2_SetAutoreload(uint16_t Autoreload)
  780. {
  781. /* Set the Autoreload Register value */
  782. TIM2->ARRH = (uint8_t)(Autoreload >> 8);
  783. TIM2->ARRL = (uint8_t)(Autoreload);
  784. }
  785. /**
  786. * @brief Sets the TIM2 Capture Compare1 Register value.
  787. * @param Compare1 specifies the Capture Compare1 register new value.
  788. * This parameter is between 0x0000 and 0xFFFF.
  789. * @retval None
  790. */
  791. void TIM2_SetCompare1(uint16_t Compare1)
  792. {
  793. /* Set the Capture Compare1 Register value */
  794. TIM2->CCR1H = (uint8_t)(Compare1 >> 8);
  795. TIM2->CCR1L = (uint8_t)(Compare1);
  796. }
  797. /**
  798. * @brief Sets the TIM2 Capture Compare2 Register value.
  799. * @param Compare2 specifies the Capture Compare2 register new value.
  800. * This parameter is between 0x0000 and 0xFFFF.
  801. * @retval None
  802. */
  803. void TIM2_SetCompare2(uint16_t Compare2)
  804. {
  805. /* Set the Capture Compare2 Register value */
  806. TIM2->CCR2H = (uint8_t)(Compare2 >> 8);
  807. TIM2->CCR2L = (uint8_t)(Compare2);
  808. }
  809. /**
  810. * @brief Sets the TIM2 Capture Compare3 Register value.
  811. * @param Compare3 specifies the Capture Compare3 register new value.
  812. * This parameter is between 0x0000 and 0xFFFF.
  813. * @retval None
  814. */
  815. void TIM2_SetCompare3(uint16_t Compare3)
  816. {
  817. /* Set the Capture Compare3 Register value */
  818. TIM2->CCR3H = (uint8_t)(Compare3 >> 8);
  819. TIM2->CCR3L = (uint8_t)(Compare3);
  820. }
  821. /**
  822. * @brief Sets the TIM2 Input Capture 1 Prescaler.
  823. * @param TIM2_IC1Prescaler specifies the Input Capture prescaler new value
  824. * This parameter can be one of the following values:
  825. * - TIM2_ICPSC_DIV1: no prescaler
  826. * - TIM2_ICPSC_DIV2: capture is done once every 2 events
  827. * - TIM2_ICPSC_DIV4: capture is done once every 4 events
  828. * - TIM2_ICPSC_DIV8: capture is done once every 8 events
  829. * @retval None
  830. */
  831. void TIM2_SetIC1Prescaler(TIM2_ICPSC_TypeDef TIM2_IC1Prescaler)
  832. {
  833. /* Check the parameters */
  834. assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_IC1Prescaler));
  835. /* Reset the IC1PSC Bits &Set the IC1PSC value */
  836. TIM2->CCMR1 = (uint8_t)((uint8_t)(TIM2->CCMR1 & (uint8_t)(~TIM2_CCMR_ICxPSC))
  837. | (uint8_t)TIM2_IC1Prescaler);
  838. }
  839. /**
  840. * @brief Sets the TIM2 Input Capture 2 prescaler.
  841. * @param TIM2_IC2Prescaler specifies the Input Capture prescaler new value
  842. * This parameter can be one of the following values:
  843. * - TIM2_ICPSC_DIV1: no prescaler
  844. * - TIM2_ICPSC_DIV2: capture is done once every 2 events
  845. * - TIM2_ICPSC_DIV4: capture is done once every 4 events
  846. * - TIM2_ICPSC_DIV8: capture is done once every 8 events
  847. * @retval None
  848. */
  849. void TIM2_SetIC2Prescaler(TIM2_ICPSC_TypeDef TIM2_IC2Prescaler)
  850. {
  851. /* Check the parameters */
  852. assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_IC2Prescaler));
  853. /* Reset the IC1PSC Bits &Set the IC1PSC value */
  854. TIM2->CCMR2 = (uint8_t)((uint8_t)(TIM2->CCMR2 & (uint8_t)(~TIM2_CCMR_ICxPSC))
  855. | (uint8_t)TIM2_IC2Prescaler);
  856. }
  857. /**
  858. * @brief Sets the TIM2 Input Capture 3 prescaler.
  859. * @param TIM2_IC3Prescaler specifies the Input Capture prescaler new value
  860. * This parameter can be one of the following values:
  861. * - TIM2_ICPSC_DIV1: no prescaler
  862. * - TIM2_ICPSC_DIV2: capture is done once every 2 events
  863. * - TIM2_ICPSC_DIV4: capture is done once every 4 events
  864. * - TIM2_ICPSC_DIV8: capture is done once every 8 events
  865. * @retval None
  866. */
  867. void TIM2_SetIC3Prescaler(TIM2_ICPSC_TypeDef TIM2_IC3Prescaler)
  868. {
  869. /* Check the parameters */
  870. assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_IC3Prescaler));
  871. /* Reset the IC1PSC Bits &Set the IC1PSC value */
  872. TIM2->CCMR3 = (uint8_t)((uint8_t)(TIM2->CCMR3 & (uint8_t)(~TIM2_CCMR_ICxPSC))
  873. | (uint8_t)TIM2_IC3Prescaler);
  874. }
  875. /**
  876. * @brief Gets the TIM2 Input Capture 1 value.
  877. * @param None
  878. * @retval Capture Compare 1 Register value.
  879. */
  880. uint16_t TIM2_GetCapture1(void)
  881. {
  882. /* Get the Capture 1 Register value */
  883. uint16_t tmpccr1 = 0;
  884. uint8_t tmpccr1l=0, tmpccr1h=0;
  885. tmpccr1h = TIM2->CCR1H;
  886. tmpccr1l = TIM2->CCR1L;
  887. tmpccr1 = (uint16_t)(tmpccr1l);
  888. tmpccr1 |= (uint16_t)((uint16_t)tmpccr1h << 8);
  889. /* Get the Capture 1 Register value */
  890. return (uint16_t)tmpccr1;
  891. }
  892. /**
  893. * @brief Gets the TIM2 Input Capture 2 value.
  894. * @param None
  895. * @retval Capture Compare 2 Register value.
  896. */
  897. uint16_t TIM2_GetCapture2(void)
  898. {
  899. /* Get the Capture 2 Register value */
  900. uint16_t tmpccr2 = 0;
  901. uint8_t tmpccr2l=0, tmpccr2h=0;
  902. tmpccr2h = TIM2->CCR2H;
  903. tmpccr2l = TIM2->CCR2L;
  904. tmpccr2 = (uint16_t)(tmpccr2l);
  905. tmpccr2 |= (uint16_t)((uint16_t)tmpccr2h << 8);
  906. /* Get the Capture 2 Register value */
  907. return (uint16_t)tmpccr2;
  908. }
  909. /**
  910. * @brief Gets the TIM2 Input Capture 3 value.
  911. * @param None
  912. * @retval Capture Compare 3 Register value.
  913. */
  914. uint16_t TIM2_GetCapture3(void)
  915. {
  916. /* Get the Capture 3 Register value */
  917. uint16_t tmpccr3 = 0;
  918. uint8_t tmpccr3l=0, tmpccr3h=0;
  919. tmpccr3h = TIM2->CCR3H;
  920. tmpccr3l = TIM2->CCR3L;
  921. tmpccr3 = (uint16_t)(tmpccr3l);
  922. tmpccr3 |= (uint16_t)((uint16_t)tmpccr3h << 8);
  923. /* Get the Capture 3 Register value */
  924. return (uint16_t)tmpccr3;
  925. }
  926. /**
  927. * @brief Gets the TIM2 Counter value.
  928. * @param None
  929. * @retval Counter Register value.
  930. */
  931. uint16_t TIM2_GetCounter(void)
  932. {
  933. uint16_t tmpcntr = 0;
  934. tmpcntr = ((uint16_t)TIM2->CNTRH << 8);
  935. /* Get the Counter Register value */
  936. return (uint16_t)( tmpcntr| (uint16_t)(TIM2->CNTRL));
  937. }
  938. /**
  939. * @brief Gets the TIM2 Prescaler value.
  940. * @param None
  941. * @retval Prescaler Register configuration value @ref TIM2_Prescaler_TypeDef.
  942. */
  943. TIM2_Prescaler_TypeDef TIM2_GetPrescaler(void)
  944. {
  945. /* Get the Prescaler Register value */
  946. return (TIM2_Prescaler_TypeDef)(TIM2->PSCR);
  947. }
  948. /**
  949. * @brief Checks whether the specified TIM2 flag is set or not.
  950. * @param TIM2_FLAG specifies the flag to check.
  951. * This parameter can be one of the following values:
  952. * - TIM2_FLAG_UPDATE: TIM2 update Flag
  953. * - TIM2_FLAG_CC1: TIM2 Capture Compare 1 Flag
  954. * - TIM2_FLAG_CC2: TIM2 Capture Compare 2 Flag
  955. * - TIM2_FLAG_CC3: TIM2 Capture Compare 3 Flag
  956. * - TIM2_FLAG_CC1OF: TIM2 Capture Compare 1 over capture Flag
  957. * - TIM2_FLAG_CC2OF: TIM2 Capture Compare 2 over capture Flag
  958. * - TIM2_FLAG_CC3OF: TIM2 Capture Compare 3 over capture Flag
  959. * @retval FlagStatus The new state of TIM2_FLAG (SET or RESET).
  960. */
  961. FlagStatus TIM2_GetFlagStatus(TIM2_FLAG_TypeDef TIM2_FLAG)
  962. {
  963. FlagStatus bitstatus = RESET;
  964. uint8_t tim2_flag_l = 0, tim2_flag_h = 0;
  965. /* Check the parameters */
  966. assert_param(IS_TIM2_GET_FLAG_OK(TIM2_FLAG));
  967. tim2_flag_l = (uint8_t)(TIM2->SR1 & (uint8_t)TIM2_FLAG);
  968. tim2_flag_h = (uint8_t)((uint16_t)TIM2_FLAG >> 8);
  969. if ((tim2_flag_l | (uint8_t)(TIM2->SR2 & tim2_flag_h)) != (uint8_t)RESET )
  970. {
  971. bitstatus = SET;
  972. }
  973. else
  974. {
  975. bitstatus = RESET;
  976. }
  977. return (FlagStatus)bitstatus;
  978. }
  979. /**
  980. * @brief Clears the TIM2’s pending flags.
  981. * @param TIM2_FLAG specifies the flag to clear.
  982. * This parameter can be one of the following values:
  983. * - TIM2_FLAG_UPDATE: TIM2 update Flag
  984. * - TIM2_FLAG_CC1: TIM2 Capture Compare 1 Flag
  985. * - TIM2_FLAG_CC2: TIM2 Capture Compare 2 Flag
  986. * - TIM2_FLAG_CC3: TIM2 Capture Compare 3 Flag
  987. * - TIM2_FLAG_CC1OF: TIM2 Capture Compare 1 over capture Flag
  988. * - TIM2_FLAG_CC2OF: TIM2 Capture Compare 2 over capture Flag
  989. * - TIM2_FLAG_CC3OF: TIM2 Capture Compare 3 over capture Flag
  990. * @retval None.
  991. */
  992. void TIM2_ClearFlag(TIM2_FLAG_TypeDef TIM2_FLAG)
  993. {
  994. /* Check the parameters */
  995. assert_param(IS_TIM2_CLEAR_FLAG_OK(TIM2_FLAG));
  996. /* Clear the flags (rc_w0) clear this bit by writing 0. Writing ‘1’ has no effect*/
  997. TIM2->SR1 = (uint8_t)(~((uint8_t)(TIM2_FLAG)));
  998. TIM2->SR2 = (uint8_t)(~((uint8_t)((uint8_t)TIM2_FLAG >> 8)));
  999. }
  1000. /**
  1001. * @brief Checks whether the TIM2 interrupt has occurred or not.
  1002. * @param TIM2_IT specifies the TIM2 interrupt source to check.
  1003. * This parameter can be one of the following values:
  1004. * - TIM2_IT_UPDATE: TIM2 update Interrupt source
  1005. * - TIM2_IT_CC1: TIM2 Capture Compare 1 Interrupt source
  1006. * - TIM2_IT_CC2: TIM2 Capture Compare 2 Interrupt source
  1007. * - TIM2_IT_CC3: TIM2 Capture Compare 3 Interrupt source
  1008. * @retval ITStatus The new state of the TIM2_IT(SET or RESET).
  1009. */
  1010. ITStatus TIM2_GetITStatus(TIM2_IT_TypeDef TIM2_IT)
  1011. {
  1012. ITStatus bitstatus = RESET;
  1013. uint8_t TIM2_itStatus = 0, TIM2_itEnable = 0;
  1014. /* Check the parameters */
  1015. assert_param(IS_TIM2_GET_IT_OK(TIM2_IT));
  1016. TIM2_itStatus = (uint8_t)(TIM2->SR1 & TIM2_IT);
  1017. TIM2_itEnable = (uint8_t)(TIM2->IER & TIM2_IT);
  1018. if ((TIM2_itStatus != (uint8_t)RESET ) && (TIM2_itEnable != (uint8_t)RESET ))
  1019. {
  1020. bitstatus = SET;
  1021. }
  1022. else
  1023. {
  1024. bitstatus = RESET;
  1025. }
  1026. return (ITStatus)(bitstatus);
  1027. }
  1028. /**
  1029. * @brief Clears the TIM2's interrupt pending bits.
  1030. * @param TIM2_IT specifies the pending bit to clear.
  1031. * This parameter can be one of the following values:
  1032. * - TIM2_IT_UPDATE: TIM2 update Interrupt source
  1033. * - TIM2_IT_CC1: TIM2 Capture Compare 1 Interrupt source
  1034. * - TIM2_IT_CC2: TIM2 Capture Compare 2 Interrupt source
  1035. * - TIM2_IT_CC3: TIM2 Capture Compare 3 Interrupt source
  1036. * @retval None.
  1037. */
  1038. void TIM2_ClearITPendingBit(TIM2_IT_TypeDef TIM2_IT)
  1039. {
  1040. /* Check the parameters */
  1041. assert_param(IS_TIM2_IT_OK(TIM2_IT));
  1042. /* Clear the IT pending Bit */
  1043. TIM2->SR1 = (uint8_t)(~TIM2_IT);
  1044. }
  1045. /**
  1046. * @brief Configure the TI1 as Input.
  1047. * @param TIM2_ICPolarity The Input Polarity.
  1048. * This parameter can be one of the following values:
  1049. * - TIM2_ICPOLARITY_FALLING
  1050. * - TIM2_ICPOLARITY_RISING
  1051. * @param TIM2_ICSelection specifies the input to be used.
  1052. * This parameter can be one of the following values:
  1053. * - TIM2_ICSELECTION_DIRECTTI: TIM2 Input 1 is selected to
  1054. * be connected to IC1.
  1055. * - TIM2_ICSELECTION_INDIRECTTI: TIM2 Input 1 is selected to
  1056. * be connected to IC2.
  1057. * @param TIM2_ICFilter Specifies the Input Capture Filter.
  1058. * This parameter must be a value between 0x00 and 0x0F.
  1059. * @retval None
  1060. */
  1061. static void TI1_Config(uint8_t TIM2_ICPolarity,
  1062. uint8_t TIM2_ICSelection,
  1063. uint8_t TIM2_ICFilter)
  1064. {
  1065. /* Disable the Channel 1: Reset the CCE Bit */
  1066. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC1E);
  1067. /* Select the Input and set the filter */
  1068. TIM2->CCMR1 = (uint8_t)((uint8_t)(TIM2->CCMR1 & (uint8_t)(~(uint8_t)( TIM2_CCMR_CCxS | TIM2_CCMR_ICxF )))
  1069. | (uint8_t)(((TIM2_ICSelection)) | ((uint8_t)( TIM2_ICFilter << 4))));
  1070. /* Select the Polarity */
  1071. if (TIM2_ICPolarity != TIM2_ICPOLARITY_RISING)
  1072. {
  1073. TIM2->CCER1 |= TIM2_CCER1_CC1P;
  1074. }
  1075. else
  1076. {
  1077. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC1P);
  1078. }
  1079. /* Set the CCE Bit */
  1080. TIM2->CCER1 |= TIM2_CCER1_CC1E;
  1081. }
  1082. /**
  1083. * @brief Configure the TI2 as Input.
  1084. * @param TIM2_ICPolarity The Input Polarity.
  1085. * This parameter can be one of the following values:
  1086. * - TIM2_ICPOLARITY_FALLING
  1087. * - TIM2_ICPOLARITY_RISING
  1088. * @param TIM2_ICSelection specifies the input to be used.
  1089. * This parameter can be one of the following values:
  1090. * - TIM2_ICSELECTION_DIRECTTI: TIM2 Input 2 is selected to
  1091. * be connected to IC2.
  1092. * - TIM2_ICSELECTION_INDIRECTTI: TIM2 Input 2 is selected to
  1093. * be connected to IC1.
  1094. * @param TIM2_ICFilter Specifies the Input Capture Filter.
  1095. * This parameter must be a value between 0x00 and 0x0F.
  1096. * @retval None
  1097. */
  1098. static void TI2_Config(uint8_t TIM2_ICPolarity,
  1099. uint8_t TIM2_ICSelection,
  1100. uint8_t TIM2_ICFilter)
  1101. {
  1102. /* Disable the Channel 2: Reset the CCE Bit */
  1103. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC2E);
  1104. /* Select the Input and set the filter */
  1105. TIM2->CCMR2 = (uint8_t)((uint8_t)(TIM2->CCMR2 & (uint8_t)(~(uint8_t)( TIM2_CCMR_CCxS | TIM2_CCMR_ICxF )))
  1106. | (uint8_t)(( (TIM2_ICSelection)) | ((uint8_t)( TIM2_ICFilter << 4))));
  1107. /* Select the Polarity */
  1108. if (TIM2_ICPolarity != TIM2_ICPOLARITY_RISING)
  1109. {
  1110. TIM2->CCER1 |= TIM2_CCER1_CC2P;
  1111. }
  1112. else
  1113. {
  1114. TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC2P);
  1115. }
  1116. /* Set the CCE Bit */
  1117. TIM2->CCER1 |= TIM2_CCER1_CC2E;
  1118. }
  1119. /**
  1120. * @brief Configure the TI3 as Input.
  1121. * @param TIM2_ICPolarity The Input Polarity.
  1122. * This parameter can be one of the following values:
  1123. * - TIM2_ICPOLARITY_FALLING
  1124. * - TIM2_ICPOLARITY_RISING
  1125. * @param TIM2_ICSelection specifies the input to be used.
  1126. * This parameter can be one of the following values:
  1127. * - TIM2_ICSELECTION_DIRECTTI: TIM2 Input 3 is selected to
  1128. * be connected to IC3.
  1129. * @param TIM2_ICFilter Specifies the Input Capture Filter.
  1130. * This parameter must be a value between 0x00 and 0x0F.
  1131. * @retval None
  1132. */
  1133. static void TI3_Config(uint8_t TIM2_ICPolarity, uint8_t TIM2_ICSelection,
  1134. uint8_t TIM2_ICFilter)
  1135. {
  1136. /* Disable the Channel 3: Reset the CCE Bit */
  1137. TIM2->CCER2 &= (uint8_t)(~TIM2_CCER2_CC3E);
  1138. /* Select the Input and set the filter */
  1139. TIM2->CCMR3 = (uint8_t)((uint8_t)(TIM2->CCMR3 & (uint8_t)(~( TIM2_CCMR_CCxS | TIM2_CCMR_ICxF)))
  1140. | (uint8_t)(( (TIM2_ICSelection)) | ((uint8_t)( TIM2_ICFilter << 4))));
  1141. /* Select the Polarity */
  1142. if (TIM2_ICPolarity != TIM2_ICPOLARITY_RISING)
  1143. {
  1144. TIM2->CCER2 |= TIM2_CCER2_CC3P;
  1145. }
  1146. else
  1147. {
  1148. TIM2->CCER2 &= (uint8_t)(~TIM2_CCER2_CC3P);
  1149. }
  1150. /* Set the CCE Bit */
  1151. TIM2->CCER2 |= TIM2_CCER2_CC3E;
  1152. }
  1153. /**
  1154. * @}
  1155. */
  1156. /**
  1157. * @}
  1158. */
  1159. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/