Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

3366 řádky
118 KiB

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