1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
2 * File Name : stm32f10x_tim.c
3 * Author : MCD Application Team
4 * Date First Issued : 09/29/2006
5 * Description : This file provides all the TIM firmware functions.
6 ********************************************************************************
11 ********************************************************************************
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
14 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
15 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
16 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
17 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 *******************************************************************************/
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32f10x_tim.h"
22 #include "stm32f10x_rcc.h"
24 /* Private typedef -----------------------------------------------------------*/
25 /* Private define ------------------------------------------------------------*/
26 /* ---------------------- TIM registers bit mask ------------------------ */
27 #define CR1_CEN_Set ((u16)0x0001)
28 #define CR1_CEN_Reset ((u16)0x03FE)
29 #define CR1_UDIS_Set ((u16)0x0002)
30 #define CR1_UDIS_Reset ((u16)0x03FD)
31 #define CR1_URS_Set ((u16)0x0004)
32 #define CR1_URS_Reset ((u16)0x03FB)
33 #define CR1_OPM_Mask ((u16)0x03F7)
34 #define CR1_CounterMode_Mask ((u16)0x039F)
35 #define CR1_ARPE_Set ((u16)0x0080)
36 #define CR1_ARPE_Reset ((u16)0x037F)
37 #define CR1_CKD_Mask ((u16)0x00FF)
39 #define CR2_CCDS_Set ((u16)0x0008)
40 #define CR2_CCDS_Reset ((u16)0x0007)
41 #define CR2_MMS_Mask ((u16)0x0080)
42 #define CR2_TI1S_Set ((u16)0x0080)
43 #define CR2_TI1S_Reset ((u16)0xFF70)
45 #define SMCR_SMS_Mask ((u16)0xFFF0)
46 #define SMCR_ETR_Mask ((u16)0x00F7)
47 #define SMCR_TS_Mask ((u16)0xFF87)
48 #define SMCR_MSM_Mask ((u16)0xFF77)
49 #define SMCR_ECE_Set ((u16)0x4000)
51 #define CCMR_CC13S_Mask ((u16)0x7F7C)
52 #define CCMR_CC24S_Mask ((u16)0x7C7F)
53 #define CCMR_TI13Direct_Set ((u16)0x0001)
54 #define CCMR_TI24Direct_Set ((u16)0x0100)
55 #define CCMR_OC13FE_Mask ((u16)0x7F7B)
56 #define CCMR_OC24FE_Mask ((u16)0x7B7F)
57 #define CCMR_OC13PE_Mask ((u16)0x7F77)
58 #define CCMR_OC24PE_Mask ((u16)0x777F)
59 #define CCMR_OCM13_Mask ((u16)0x7F0F)
60 #define CCMR_OCM24_Mask ((u16)0x0F7F)
61 #define CCMR_IC13PSC_Mask ((u16)0xFFF3)
62 #define CCMR_IC24PSC_Mask ((u16)0xF3FF)
63 #define CCMR_IC13F_Mask ((u16)0xFF0F)
64 #define CCMR_IC24F_Mask ((u16)0x0FFF)
65 #define CCER_CC1P_Mask ((u16)0xFFFD)
67 #define CCER_CC2P_Mask ((u16)0xFFDF)
68 #define CCER_CC3P_Mask ((u16)0xFDFF)
69 #define CCER_CC4P_Mask ((u16)0xDFFF)
71 #define CCRE_CC1E_Set ((u16)0x0001)
72 #define CCRE_CC1E_Reset ((u16)0xFFFE)
73 #define CCRE_CC1E_Mask ((u16)0xFFFE)
75 #define CCRE_CC2E_Set ((u16)0x0010)
76 #define CCRE_CC2E_Reset ((u16)0xFFEF)
77 #define CCRE_CC2E_Mask ((u16)0xFFEF)
79 #define CCRE_CC3E_Set ((u16)0x0100)
80 #define CCRE_CC3E_Reset ((u16)0xFEFF)
82 #define CCRE_CC4E_Set ((u16)0x1000)
83 #define CCRE_CC4E_Reset ((u16)0xEFFF)
84 #define CCRE_CC4E_Mask ((u16)0xEFFF)
86 #define DCR_DMA_Mask ((u16)0x0000)
88 /* TIM private Masks */
89 #define TIM_Period_Reset_Mask ((u16)0x0000)
90 #define TIM_Prescaler_Reset_Mask ((u16)0x0000)
91 #define TIM_Pulse_Reset_Mask ((u16)0x0000)
92 #define TIM_ICFilter_Mask ((u8)0x00)
94 /* Private macro -------------------------------------------------------------*/
95 /* Private variables ---------------------------------------------------------*/
96 static uc16 Tab_OCModeMask[4] = {0xFF00, 0x00FF, 0xFF00, 0x00FF};
97 static uc16 Tab_PolarityMask[4] = {CCER_CC1P_Mask, CCER_CC2P_Mask, CCER_CC3P_Mask, CCER_CC4P_Mask};
99 /* Private function prototypes -----------------------------------------------*/
100 static void PWMI_Config(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct);
101 static void TI1_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,
103 static void TI2_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,
105 static void TI3_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,
107 static void TI4_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,
109 static void ETR_Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler,
110 u16 TIM_ExtTRGPolarity, u8 ExtTRGFilter);
111 /* Private functions ---------------------------------------------------------*/
113 /*******************************************************************************
114 * Function Name : TIM_DeInit
115 * Description : Deinitializes the TIMx peripheral registers to their default
117 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
120 *******************************************************************************/
121 void TIM_DeInit(TIM_TypeDef* TIMx)
123 switch (*(u32*)&TIMx)
126 RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
127 RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, DISABLE);
131 RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);
132 RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, DISABLE);
136 RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);
137 RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, DISABLE);
145 /*******************************************************************************
146 * Function Name : TIM_TimeBaseInit
147 * Description : Initializes the TIMx Time Base Unit peripheral according to
148 * the specified parameters in the TIM_TimeBaseInitStruct.
149 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
150 * - TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef
151 * structure that contains the configuration information for
152 * the specified TIM peripheral.
155 *******************************************************************************/
156 void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
158 /* Check the parameters */
159 assert(IS_TIM_COUNTER_MODE(TIM_TimeBaseInitStruct->TIM_CounterMode));
160 assert(IS_TIM_CKD_DIV(TIM_TimeBaseInitStruct->TIM_ClockDivision));
162 /* Set the Autoreload value */
163 TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ;
165 /* Set the Prescaler value */
166 TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;
168 /* Select the Counter Mode and set the clock division */
169 TIMx->CR1 &= CR1_CKD_Mask & CR1_CounterMode_Mask;
170 TIMx->CR1 |= (u32)TIM_TimeBaseInitStruct->TIM_ClockDivision |
171 TIM_TimeBaseInitStruct->TIM_CounterMode;
173 /*******************************************************************************
174 * Function Name : TIM_OCInit
175 * Description : Initializes the TIMx peripheral according to the specified
176 * parameters in the TIM_OCInitStruct.
177 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
178 * - TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure
179 * that contains the configuration information for the specified
183 *******************************************************************************/
184 void TIM_OCInit(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
186 u32 tmpccmrx = 0, tmpccer = 0;
188 /* Check the parameters */
189 assert(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
190 assert(IS_TIM_CHANNEL(TIM_OCInitStruct->TIM_Channel));
191 assert(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
193 tmpccer = TIMx->CCER;
195 if ((TIM_OCInitStruct->TIM_Channel == (u16)TIM_Channel_1) ||
196 (TIM_OCInitStruct->TIM_Channel == (u16)TIM_Channel_2))
198 tmpccmrx = TIMx->CCMR1;
200 /* Reset the Output Compare Bits */
201 tmpccmrx &= Tab_OCModeMask[TIM_OCInitStruct->TIM_Channel];
203 /* Set the Output Polarity level */
204 tmpccer &= Tab_PolarityMask[TIM_OCInitStruct->TIM_Channel];
206 if (TIM_OCInitStruct->TIM_Channel == TIM_Channel_1)
208 /* Disable the Channel 1: Reset the CCE Bit */
209 TIMx->CCER &= CCRE_CC1E_Reset;
211 /* Select the Output Compare Mode */
212 tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
214 /* Set the Capture Compare Register value */
215 TIMx->CCR1 = TIM_OCInitStruct->TIM_Pulse;
217 /* Set the Capture Compare Enable Bit */
218 tmpccer |= CCRE_CC1E_Set;
220 /* Set the Capture Compare Polarity */
221 tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;
223 else /* TIM_Channel_2 */
225 /* Disable the Channel 2: Reset the CCE Bit */
226 TIMx->CCER &= CCRE_CC2E_Reset;
228 /* Select the Output Compare Mode */
229 tmpccmrx |= (u32)TIM_OCInitStruct->TIM_OCMode << 8;
231 /* Set the Capture Compare Register value */
232 TIMx->CCR2 = TIM_OCInitStruct->TIM_Pulse;
234 /* Set the Capture Compare Enable Bit */
235 tmpccer |= CCRE_CC2E_Set;
237 /* Set the Capture Compare Polarity */
238 tmpccer |= (u32)TIM_OCInitStruct->TIM_OCPolarity << 4;
241 TIMx->CCMR1 = (u16)tmpccmrx;
245 if ((TIM_OCInitStruct->TIM_Channel == TIM_Channel_3) ||
246 (TIM_OCInitStruct->TIM_Channel == TIM_Channel_4))
248 tmpccmrx = TIMx->CCMR2;
250 /* Reset the Output Compare Bits */
251 tmpccmrx &= Tab_OCModeMask[TIM_OCInitStruct->TIM_Channel];
253 /* Set the Output Polarity level */
254 tmpccer &= Tab_PolarityMask[TIM_OCInitStruct->TIM_Channel];
256 if (TIM_OCInitStruct->TIM_Channel == TIM_Channel_3)
258 /* Disable the Channel 3: Reset the CCE Bit */
259 TIMx->CCER &= CCRE_CC3E_Reset;
261 /* Select the Output Compare Mode */
262 tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
264 /* Set the Capture Compare Register value */
265 TIMx->CCR3 = TIM_OCInitStruct->TIM_Pulse;
267 /* Set the Capture Compare Enable Bit */
268 tmpccer |= CCRE_CC3E_Set;
270 /* Set the Capture Compare Polarity */
271 tmpccer |= (u32)TIM_OCInitStruct->TIM_OCPolarity << 8;
273 else /* TIM_Channel_4 */
275 /* Disable the Channel 4: Reset the CCE Bit */
276 TIMx->CCER &= CCRE_CC4E_Reset;
278 /* Select the Output Compare Mode */
279 tmpccmrx |= (u32)TIM_OCInitStruct->TIM_OCMode << 8;
281 /* Set the Capture Compare Register value */
282 TIMx->CCR4 = TIM_OCInitStruct->TIM_Pulse;
284 /* Set the Capture Compare Enable Bit */
285 tmpccer |= CCRE_CC4E_Set;
287 /* Set the Capture Compare Polarity */
288 tmpccer |= (u32)TIM_OCInitStruct->TIM_OCPolarity << 12;
291 TIMx->CCMR2 = (u16)tmpccmrx;
295 TIMx->CCER = (u16)tmpccer;
298 /*******************************************************************************
299 * Function Name : TIM_ICInit
300 * Description : Initializes the TIMx peripheral according to the specified
301 * parameters in the TIM_ICInitStruct.
302 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
303 * - TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure
304 * that contains the configuration information for the specified
308 *******************************************************************************/
309 void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
311 /* Check the parameters */
312 assert(IS_TIM_IC_MODE(TIM_ICInitStruct->TIM_ICMode));
313 assert(IS_TIM_CHANNEL(TIM_ICInitStruct->TIM_Channel));
314 assert(IS_TIM_IC_POLARITY(TIM_ICInitStruct->TIM_ICPolarity));
315 assert(IS_TIM_IC_SELECTION(TIM_ICInitStruct->TIM_ICSelection));
316 assert(IS_TIM_IC_PRESCALER(TIM_ICInitStruct->TIM_ICPrescaler));
317 assert(IS_TIM_IC_FILTER(TIM_ICInitStruct->TIM_ICFilter));
319 if (TIM_ICInitStruct->TIM_ICMode == TIM_ICMode_ICAP)
321 if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
323 /* TI1 Configuration */
324 TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
325 TIM_ICInitStruct->TIM_ICSelection,
326 TIM_ICInitStruct->TIM_ICFilter);
328 /* Set the Input Capture Prescaler value */
329 TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
331 else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_2)
333 /* TI2 Configuration */
334 TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
335 TIM_ICInitStruct->TIM_ICSelection,
336 TIM_ICInitStruct->TIM_ICFilter);
338 /* Set the Input Capture Prescaler value */
339 TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
341 else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_3)
343 /* TI3 Configuration */
344 TI3_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
345 TIM_ICInitStruct->TIM_ICSelection,
346 TIM_ICInitStruct->TIM_ICFilter);
348 /* Set the Input Capture Prescaler value */
349 TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
351 else /* TIM_Channel_4 */
353 /* TI4 Configuration */
354 TI4_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
355 TIM_ICInitStruct->TIM_ICSelection,
356 TIM_ICInitStruct->TIM_ICFilter);
358 /* Set the Input Capture Prescaler value */
359 TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
364 PWMI_Config(TIMx, TIM_ICInitStruct);
368 /*******************************************************************************
369 * Function Name : TIM_TimeBaseStructInit
370 * Description : Fills each TIM_TimeBaseInitStruct member with its default value.
371 * Input : - TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef
372 * structure which will be initialized.
375 *******************************************************************************/
376 void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
378 /* Set the default configuration */
379 TIM_TimeBaseInitStruct->TIM_Period = TIM_Period_Reset_Mask;
380 TIM_TimeBaseInitStruct->TIM_Prescaler = TIM_Prescaler_Reset_Mask;
381 TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;
382 TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;
385 /*******************************************************************************
386 * Function Name : TIM_OCStructInit
387 * Description : Fills each TIM_OCInitStruct member with its default value.
388 * Input : - TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure
389 * which will be initialized.
392 *******************************************************************************/
393 void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct)
395 /* Set the default configuration */
396 TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;
397 TIM_OCInitStruct->TIM_Channel = TIM_Channel_1;
398 TIM_OCInitStruct->TIM_Pulse = TIM_Pulse_Reset_Mask;
399 TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;
402 /*******************************************************************************
403 * Function Name : TIM_ICStructInit
404 * Description : Fills each TIM_InitStruct member with its default value.
405 * Input : - TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure
406 * which will be initialized.
409 *******************************************************************************/
410 void TIM_ICStructInit(TIM_ICInitTypeDef* TIM_ICInitStruct)
412 /* Set the default configuration */
413 TIM_ICInitStruct->TIM_ICMode = TIM_ICMode_ICAP;
414 TIM_ICInitStruct->TIM_Channel = TIM_Channel_1;
415 TIM_ICInitStruct->TIM_ICPolarity = TIM_ICPolarity_Rising;
416 TIM_ICInitStruct->TIM_ICSelection = TIM_ICSelection_DirectTI;
417 TIM_ICInitStruct->TIM_ICPrescaler = TIM_ICPSC_DIV1;
418 TIM_ICInitStruct->TIM_ICFilter = TIM_ICFilter_Mask;
421 /*******************************************************************************
422 * Function Name : TIM_Cmd
423 * Description : Enables or disables the specified TIM peripheral.
424 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIMx peripheral.
425 * - Newstate: new state of the TIMx peripheral.
426 * This parameter can be: ENABLE or DISABLE.
429 *******************************************************************************/
430 void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
432 /* Check the parameters */
433 assert(IS_FUNCTIONAL_STATE(NewState));
435 if (NewState != DISABLE)
437 /* Enable the TIM Counter */
438 TIMx->CR1 |= CR1_CEN_Set;
442 /* Disable the TIM Counter */
443 TIMx->CR1 &= CR1_CEN_Reset;
447 /*******************************************************************************
448 * Function Name : TIM_ITConfig
449 * Description : Enables or disables the TIMx interrupts.
450 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
451 * - TIM_IT: specifies the TIM interrupts sources to be enabled
453 * This parameter can be any combination of the following values:
454 * - TIM_IT_Update: Timer update Interrupt
455 * - TIM_IT_CC1: Capture Compare 1 Interrupt
456 * - TIM_IT_CC2: Capture Compare 2 Interrupt
457 * - TIM_IT_CC3: Capture Compare 3 Interrupt
458 * - TIM_IT_CC4: Capture Compare 4 Interrupt
459 * - TIM_IT_Trigger: Trigger Interrupt
460 * - Newstate: new state of the specified TIMx interrupts.
461 * This parameter can be: ENABLE or DISABLE.
464 *******************************************************************************/
465 void TIM_ITConfig(TIM_TypeDef* TIMx, u16 TIM_IT, FunctionalState NewState)
467 /* Check the parameters */
468 assert(IS_TIM_IT(TIM_IT));
469 assert(IS_FUNCTIONAL_STATE(NewState));
471 if (NewState != DISABLE)
473 /* Enable the Interrupt sources */
474 TIMx->DIER |= TIM_IT;
478 /* Disable the Interrupt sources */
479 TIMx->DIER &= (u16)(~TIM_IT);
483 /*******************************************************************************
484 * Function Name : TIM_DMAConfig
485 * Description : Configures the TIMx
\92s DMA interface.
486 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
487 * - TIM_DMABase: DMA Base address.
488 * This parameter can be one of the following values:
489 * - TIM_DMABase_CR1, TIM_DMABase_CR2, TIM_DMABase_SMCR,
490 * TIM_DMABase_DIER, TIM_DMABase_SR, TIM_DMABase_EGR,
491 * TIM_DMABase_CCMR1, TIM_DMABase_CCMR2, TIM_DMABase_CCER,
492 * TIM_DMABase_CNT, TIM_DMABase_PSC, TIM_DMABase_ARR,
493 * TIM_DMABase_CCR1, TIM_DMABase_CCR2, TIM_DMABase_CCR3,
494 * TIM_DMABase_CCR4, TIM_DMABase_DCR.
495 * - TIM_DMABurstLength: DMA Burst length.
496 * This parameter can be one value between:
497 * TIM_DMABurstLength_1Byte and TIM_DMABurstLength_18Bytes.
500 *******************************************************************************/
501 void TIM_DMAConfig(TIM_TypeDef* TIMx, u16 TIM_DMABase, u16 TIM_DMABurstLength)
505 /* Check the parameters */
506 assert(IS_TIM_DMA_BASE(TIM_DMABase));
507 assert(IS_TIM_DMA_LENGTH(TIM_DMABurstLength));
511 /* Reset the DBA and the DBL Bits */
512 tmpdcr &= DCR_DMA_Mask;
514 /* Set the DMA Base and the DMA Burst Length */
515 tmpdcr |= TIM_DMABase | TIM_DMABurstLength;
517 TIMx->DCR = (u16)tmpdcr;
520 /*******************************************************************************
521 * Function Name : TIM_DMACmd
522 * Description : Enables or disables the TIMx
\92s DMA Requests.
523 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
524 * - TIM_DMASources: specifies the DMA Request sources.
525 * This parameter can be any combination of the following values:
526 * - TIM_DMA_CC1: Capture Compare 1 DMA source
527 * - TIM_DMA_CC2: Capture Compare 2 DMA source
528 * - TIM_DMA_CC3: Capture Compare 3 DMA source
529 * - TIM_DMA_CC4: Capture Compare 4 DMA source
530 * - TIM_DMA_Trigger: Trigger DMA source
531 * - Newstate: new state of the DMA Request sources.
532 * This parameter can be: ENABLE or DISABLE.
535 *******************************************************************************/
536 void TIM_DMACmd(TIM_TypeDef* TIMx, u16 TIM_DMASource, FunctionalState Newstate)
540 /* Check the parameters */
541 assert(IS_TIM_DMA_SOURCE(TIM_DMASource));
542 assert(IS_FUNCTIONAL_STATE(Newstate));
544 tmpdier = TIMx->DIER;
546 if (Newstate != DISABLE)
548 /* Enable the DMA sources */
549 tmpdier |= TIM_DMASource;
553 /* Disable the DMA sources */
554 tmpdier &= (u16)(~TIM_DMASource);
556 TIMx->DIER = (u16)tmpdier;
559 /*******************************************************************************
560 * Function Name : TIM_InternalClockConfig
561 * Description : Configures the TIMx interrnal Clock
562 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
565 *******************************************************************************/
566 void TIM_InternalClockConfig(TIM_TypeDef* TIMx)
568 /* Disable slave mode to clock the prescaler directly with the internal clock */
569 TIMx->SMCR &= SMCR_SMS_Mask;
571 /*******************************************************************************
572 * Function Name : TIM_ITRxExternalClockConfig
573 * Description : Configures the TIMx Internal Trigger as External Clock
574 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
575 * - TIM_ITRSource: Trigger source.
576 * This parameter can be one of the following values:
577 * - TIM_TS_ITR0: Internal Trigger 0
578 * - TIM_TS_ITR1: Internal Trigger 1
579 * - TIM_TS_ITR2: Internal Trigger 2
580 * - TIM_TS_ITR3: Internal Trigger 3
583 *******************************************************************************/
584 void TIM_ITRxExternalClockConfig(TIM_TypeDef* TIMx, u16 TIM_InputTriggerSource)
586 /* Check the parameters */
587 assert(IS_TIM_INTERNAL_TRIGGER_SELECTION(TIM_InputTriggerSource));
589 /* Select the Internal Trigger */
590 TIM_SelectInputTrigger(TIMx, TIM_InputTriggerSource);
592 /* Select the External clock mode1 */
593 TIMx->SMCR |= TIM_SlaveMode_External1;
595 /*******************************************************************************
596 * Function Name : TIM_TIxExternalClockConfig
597 * Description : Configures the TIMx Trigger as External Clock
598 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
599 * - TIM_TIxExternalCLKSource: Trigger source.
600 * This parameter can be one of the following values:
601 * - TIM_TS_TI1F_ED: TI1 Edge Detector
602 * - TIM_TS_TI1FP1: Filtered Timer Input 1
603 * - TIM_TS_TI2FP2: Filtered Timer Input 2
604 * - TIM_ICPolarity: specifies the TIx Polarity.
605 * This parameter can be:
606 * - TIM_ICPolarity_Rising
607 * - TIM_ICPolarity_Falling
608 * - ICFilter : specifies the filter value.
609 * This parameter must be a value between 0x0 and 0xF.
612 *******************************************************************************/
613 void TIM_TIxExternalClockConfig(TIM_TypeDef* TIMx, u16 TIM_TIxExternalCLKSource,
614 u16 TIM_ICPolarity, u8 ICFilter)
616 /* Check the parameters */
617 assert(IS_TIM_TIX_TRIGGER_SELECTION(TIM_TIxExternalCLKSource));
618 assert(IS_TIM_IC_POLARITY(TIM_ICPolarity));
619 assert(IS_TIM_IC_FILTER(ICFilter));
621 /* Configure the Timer Input Clock Source */
622 if (TIM_TIxExternalCLKSource == TIM_TIxExternalCLK1Source_TI2)
624 TI2_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
628 TI1_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
631 /* Select the Trigger source */
632 TIM_SelectInputTrigger(TIMx, TIM_TIxExternalCLKSource);
634 /* Select the External clock mode1 */
635 TIMx->SMCR |= TIM_SlaveMode_External1;
638 /*******************************************************************************
639 * Function Name : TIM_ETRClockMode1Config
640 * Description : Configures the External clock Mode1
641 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
642 * - TIM_ExtTRGPrescaler: The external Trigger Prescaler.
643 * It can be one of the following values:
644 * - TIM_ExtTRGPSC_OFF
645 * - TIM_ExtTRGPSC_DIV2
646 * - TIM_ExtTRGPSC_DIV4
647 * - TIM_ExtTRGPSC_DIV8.
648 * - TIM_ExtTRGPolarity: The external Trigger Polarity.
649 * It can be one of the following values:
650 * - TIM_ExtTRGPolarity_Inverted
651 * - TIM_ExtTRGPolarity_NonInverted
652 * - ExtTRGFilter: External Trigger Filter.
653 * This parameter must be a value between 0x00 and 0x0F
656 *******************************************************************************/
657 void TIM_ETRClockMode1Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, u16 TIM_ExtTRGPolarity,
660 /* Check the parameters */
661 assert(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
662 assert(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
664 /* Configure the ETR Clock source */
665 ETR_Config(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
667 /* Select the External clock mode1 */
668 TIMx->SMCR &= SMCR_SMS_Mask;
669 TIMx->SMCR |= TIM_SlaveMode_External1;
671 /* Select the Trigger selection : ETRF */
672 TIMx->SMCR &= SMCR_TS_Mask;
673 TIMx->SMCR |= TIM_TS_ETRF;
676 /*******************************************************************************
677 * Function Name : TIM_ETRClockMode2Config
678 * Description : Configures the External clock Mode2
679 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
680 * - TIM_ExtTRGPrescaler: The external Trigger Prescaler.
681 * It can be one of the following values:
682 * - TIM_ExtTRGPSC_OFF
683 * - TIM_ExtTRGPSC_DIV2
684 * - TIM_ExtTRGPSC_DIV4
685 * - TIM_ExtTRGPSC_DIV8
686 * - TIM_ExtTRGPolarity: The external Trigger Polarity.
687 * It can be one of the following values:
688 * - TIM_ExtTRGPolarity_Inverted
689 * - TIM_ExtTRGPolarity_NonInverted
690 * - ExtTRGFilter: External Trigger Filter.
691 * This parameter must be a value between 0x00 and 0x0F
694 *******************************************************************************/
695 void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler,
696 u16 TIM_ExtTRGPolarity, u8 ExtTRGFilter)
698 /* Check the parameters */
699 assert(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
700 assert(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
702 /* Configure the ETR Clock source */
703 ETR_Config(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
705 /* Enable the External clock mode2 */
706 TIMx->SMCR |= SMCR_ECE_Set;
708 /*******************************************************************************
709 * Function Name : TIM_SelectInputTrigger
710 * Description : Selects the Input Trigger source
711 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
712 * - TIM_InputTriggerSource: The Input Trigger source.
713 * This parameter can be one of the following values:
714 * - TIM_TS_ITR0: Internal Trigger 0
715 * - TIM_TS_ITR1: Internal Trigger 1
716 * - TIM_TS_ITR2: Internal Trigger 2
717 * - TIM_TS_ITR3: Internal Trigger 3
718 * - TIM_TS_TI1F_ED: TI1 Edge Detector
719 * - TIM_TS_TI1FP1: Filtered Timer Input 1
720 * - TIM_TS_TI2FP2: Filtered Timer Input 2
721 * - TIM_TS_ETRF: External Trigger input
724 *******************************************************************************/
725 void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, u16 TIM_InputTriggerSource)
729 /* Check the parameters */
730 assert(IS_TIM_TRIGGER_SELECTION(TIM_InputTriggerSource));
732 tmpsmcr = TIMx->SMCR;
734 /* Select the Tgigger Source */
735 tmpsmcr &= SMCR_TS_Mask;
736 tmpsmcr |= TIM_InputTriggerSource;
738 TIMx->SMCR = (u16)tmpsmcr;
741 /*******************************************************************************
742 * Function Name : TIM_PrescalerConfig
743 * Description : Configures the TIMx Prescaler.
744 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
745 * - Prescaler: specifies the Prescaler Register value
746 * - TIM_PSCReloadMode: specifies the TIM Prescaler Reload mode
747 * This parameter can be one of the following values:
748 * - TIM_PSCReloadMode_Update: The Prescaler is loaded at
750 * - TIM_PSCReloadMode_Immediate: The Prescaler is loaded
754 *******************************************************************************/
755 void TIM_PrescalerConfig(TIM_TypeDef* TIMx, u16 Prescaler, u16 TIM_PSCReloadMode)
757 /* Check the parameters */
758 assert(IS_TIM_PRESCALER_RELOAD(TIM_PSCReloadMode));
760 /* Set the Prescaler value */
761 TIMx->PSC = Prescaler;
763 /* Set or reset the UG Bit */
764 if (TIM_PSCReloadMode == TIM_PSCReloadMode_Immediate)
766 TIMx->EGR |= TIM_EventSource_Update;
770 TIMx->EGR &= TIM_EventSource_Update;
774 /*******************************************************************************
775 * Function Name : TIM_CounterModeConfig
776 * Description : Specifies the TIMx Counter Mode to be used.
777 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
778 * - TIM_CounterMode: specifies the Counter Mode to be used
779 * This parameter can be one of the following values:
780 * - TIM_CounterMode_Up: TIM Up Counting Mode
781 * - TIM_CounterMode_Down: TIM Down Counting Mode
782 * - TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1
783 * - TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2
784 * - TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3
787 *******************************************************************************/
788 void TIM_CounterModeConfig(TIM_TypeDef* TIMx, u16 TIM_CounterMode)
792 /* Check the parameters */
793 assert(IS_TIM_COUNTER_MODE(TIM_CounterMode));
797 /* Reset the CMS and DIR Bits */
798 tmpcr1 &= CR1_CounterMode_Mask;
800 /* Set the Counter Mode */
801 tmpcr1 |= TIM_CounterMode;
803 TIMx->CR1 = (u16)tmpcr1;
806 /*******************************************************************************
807 * Function Name : TIM_ForcedOC1Config
808 * Description : Forces the TIMx output 1 waveform to active or inactive level.
809 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
810 * - TIM_ForcedAction: specifies the forced Action to be set to
811 * the output waveform.
812 * This parameter can be one of the following values:
813 * - TIM_ForcedAction_Active: Force active level on OC1REF
814 * - TIM_ForcedAction_InActive: Force inactive level on
818 *******************************************************************************/
819 void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction)
823 /* Check the parameters */
824 assert(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
826 tmpccmr1 = TIMx->CCMR1;
828 /* Reset the OCM Bits */
829 tmpccmr1 &= CCMR_OCM13_Mask;
831 /* Configure The Forced output Mode */
832 tmpccmr1 |= TIM_ForcedAction;
834 TIMx->CCMR1 = (u16)tmpccmr1;
837 /*******************************************************************************
838 * Function Name : TIM_ForcedOC2Config
839 * Description : Forces the TIMx output 2 waveform to active or inactive level.
840 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
841 * - TIM_ForcedAction: specifies the forced Action to be set to
842 * the output waveform.
843 * This parameter can be one of the following values:
844 * - TIM_ForcedAction_Active: Force active level on OC2REF
845 * - TIM_ForcedAction_InActive: Force inactive level on
849 *******************************************************************************/
850 void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction)
854 /* Check the parameters */
855 assert(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
857 tmpccmr1 = TIMx->CCMR1;
859 /* Reset the OCM Bits */
860 tmpccmr1 &= CCMR_OCM24_Mask;
862 /* Configure The Forced output Mode */
863 tmpccmr1 |= (u16)(TIM_ForcedAction << 8);
865 TIMx->CCMR1 = (u16)tmpccmr1;
868 /*******************************************************************************
869 * Function Name : TIM_ForcedOC3Config
870 * Description : Forces the TIMx output 3 waveform to active or inactive level.
871 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
872 * - TIM_ForcedAction: specifies the forced Action to be set to
873 * the output waveform.
874 * This parameter can be one of the following values:
875 * - TIM_ForcedAction_Active: Force active level on OC3REF
876 * - TIM_ForcedAction_InActive: Force inactive level on
880 *******************************************************************************/
881 void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction)
885 /* Check the parameters */
886 assert(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
888 tmpccmr2 = TIMx->CCMR2;
890 /* Reset the OCM Bits */
891 tmpccmr2 &= CCMR_OCM13_Mask;
893 /* Configure The Forced output Mode */
894 tmpccmr2 |= TIM_ForcedAction;
896 TIMx->CCMR2 = (u16)tmpccmr2;
899 /*******************************************************************************
900 * Function Name : TIM_ForcedOC4Config
901 * Description : Forces the TIMx output 4 waveform to active or inactive level.
902 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
903 * - TIM_ForcedAction: specifies the forced Action to be set to
904 * the output waveform.
905 * This parameter can be one of the following values:
906 * - TIM_ForcedAction_Active: Force active level on OC4REF
907 * - TIM_ForcedAction_InActive: Force inactive level on
911 *******************************************************************************/
912 void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction)
916 /* Check the parameters */
917 assert(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
919 tmpccmr2 = TIMx->CCMR2;
921 /* Reset the OCM Bits */
922 tmpccmr2 &= CCMR_OCM24_Mask;
924 /* Configure The Forced output Mode */
925 tmpccmr2 |= (u16)(TIM_ForcedAction << 8);
927 TIMx->CCMR2 = (u16)tmpccmr2;
930 /*******************************************************************************
931 * Function Name : TIM_ARRPreloadConfig
932 * Description : Enables or disables TIMx peripheral Preload register on ARR.
933 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
934 * - Newstate: new state of the TIMx peripheral Preload register
935 * This parameter can be: ENABLE or DISABLE.
938 *******************************************************************************/
939 void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState Newstate)
943 /* Check the parameters */
944 assert(IS_FUNCTIONAL_STATE(Newstate));
948 if (Newstate != DISABLE)
950 /* Set the ARR Preload Bit */
951 tmpcr1 |= CR1_ARPE_Set;
955 /* Reset the ARR Preload Bit */
956 tmpcr1 &= CR1_ARPE_Reset;
959 TIMx->CR1 = (u16)tmpcr1;
962 /*******************************************************************************
963 * Function Name : TIM_SelectCCDMA
964 * Description : Selects the TIMx peripheral Capture Compare DMA source.
965 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
966 * - Newstate: new state of the Capture Compare DMA source
967 * This parameter can be: ENABLE or DISABLE.
970 *******************************************************************************/
971 void TIM_SelectCCDMA(TIM_TypeDef* TIMx, FunctionalState Newstate)
975 /* Check the parameters */
976 assert(IS_FUNCTIONAL_STATE(Newstate));
980 if (Newstate != DISABLE)
982 /* Set the CCDS Bit */
983 tmpcr2 |= CR2_CCDS_Set;
987 /* Reset the CCDS Bit */
988 tmpcr2 &= CR2_CCDS_Reset;
991 TIMx->CR2 = (u16)tmpcr2;
994 /*******************************************************************************
995 * Function Name : TIM_OC1PreloadConfig
996 * Description : Enables or disables the TIMx peripheral Preload register on CCR1.
997 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
998 * - TIM_OCPreload: new state of the TIMx peripheral Preload
1000 * This parameter can be one of the following values:
1001 * - TIM_OCPreload_Enable
1002 * - TIM_OCPreload_Disable
1005 *******************************************************************************/
1006 void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, u16 TIM_OCPreload)
1010 /* Check the parameters */
1011 assert(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
1013 tmpccmr1 = TIMx->CCMR1;
1015 /* Reset the OCPE Bit */
1016 tmpccmr1 &= CCMR_OC13PE_Mask;
1018 /* Enable or Disable the Output Compare Preload feature */
1019 tmpccmr1 |= TIM_OCPreload;
1021 TIMx->CCMR1 = (u16)tmpccmr1;
1024 /*******************************************************************************
1025 * Function Name : TIM_OC2PreloadConfig
1026 * Description : Enables or disables the TIMx peripheral Preload register on CCR2.
1027 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1028 * - TIM_OCPreload: new state of the TIMx peripheral Preload
1030 * This parameter can be one of the following values:
1031 * - TIM_OCPreload_Enable
1032 * - TIM_OCPreload_Disable
1035 *******************************************************************************/
1036 void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, u16 TIM_OCPreload)
1040 /* Check the parameters */
1041 assert(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
1043 tmpccmr1 = TIMx->CCMR1;
1045 /* Reset the OCPE Bit */
1046 tmpccmr1 &= CCMR_OC24PE_Mask;
1048 /* Enable or Disable the Output Compare Preload feature */
1049 tmpccmr1 |= (u16)(TIM_OCPreload << 8);
1051 TIMx->CCMR1 = (u16)tmpccmr1;
1054 /*******************************************************************************
1055 * Function Name : TIM_OC3PreloadConfig
1056 * Description : Enables or disables the TIMx peripheral Preload register on CCR3.
1057 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1058 * - TIM_OCPreload: new state of the TIMx peripheral Preload
1060 * This parameter can be one of the following values:
1061 * - TIM_OCPreload_Enable
1062 * - TIM_OCPreload_Disable
1065 *******************************************************************************/
1066 void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, u16 TIM_OCPreload)
1070 /* Check the parameters */
1071 assert(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
1073 tmpccmr2 = TIMx->CCMR2;
1075 /* Reset the OCPE Bit */
1076 tmpccmr2 &= CCMR_OC13PE_Mask;
1078 /* Enable or Disable the Output Compare Preload feature */
1079 tmpccmr2 |= TIM_OCPreload;
1081 TIMx->CCMR2 = (u16)tmpccmr2;
1084 /*******************************************************************************
1085 * Function Name : TIM_OC4PreloadConfig
1086 * Description : Enables or disables the TIMx peripheral Preload register on CCR4.
1087 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1088 * - TIM_OCPreload: new state of the TIMx peripheral Preload
1090 * This parameter can be one of the following values:
1091 * - TIM_OCPreload_Enable
1092 * - TIM_OCPreload_Disable
1095 *******************************************************************************/
1096 void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, u16 TIM_OCPreload)
1100 /* Check the parameters */
1101 assert(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
1103 tmpccmr2 = TIMx->CCMR2;
1105 /* Reset the OCPE Bit */
1106 tmpccmr2 &= CCMR_OC24PE_Mask;
1108 /* Enable or Disable the Output Compare Preload feature */
1109 tmpccmr2 |= (u16)(TIM_OCPreload << 8);
1111 TIMx->CCMR2 = (u16)tmpccmr2;
1114 /*******************************************************************************
1115 * Function Name : TIM_OC1FastConfig
1116 * Description : Configures the TIMx Output Compare 1 Fast feature.
1117 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1118 * - TIM_OCFast: new state of the Output Compare Fast Enable Bit.
1119 * This parameter can be one of the following values:
1120 * - TIM_OCFast_Enable
1121 * - TIM_OCFast_Disable
1124 *******************************************************************************/
1125 void TIM_OC1FastConfig(TIM_TypeDef* TIMx, u16 TIM_OCFast)
1129 /* Check the parameters */
1130 assert(IS_TIM_OCFAST_STATE(TIM_OCFast));
1132 tmpccmr1 = TIMx->CCMR1;
1134 /* Reset the OCFE Bit */
1135 tmpccmr1 &= CCMR_OC13FE_Mask;
1137 /* Enable or Disable the Output Compare Fast Bit */
1138 tmpccmr1 |= TIM_OCFast;
1140 TIMx->CCMR1 = (u16)tmpccmr1;
1143 /*******************************************************************************
1144 * Function Name : TIM_OC2FastConfig
1145 * Description : Configures the TIMx Output Compare 2 Fast feature.
1146 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1147 * - TIM_OCFast: new state of the Output Compare Fast Enable Bit.
1148 * This parameter can be one of the following values:
1149 * - TIM_OCFast_Enable
1150 * - TIM_OCFast_Disable
1153 *******************************************************************************/
1154 void TIM_OC2FastConfig(TIM_TypeDef* TIMx, u16 TIM_OCFast)
1158 /* Check the parameters */
1159 assert(IS_TIM_OCFAST_STATE(TIM_OCFast));
1161 tmpccmr1 = TIMx->CCMR1;
1163 /* Reset the OCFE Bit */
1164 tmpccmr1 &= CCMR_OC24FE_Mask;
1166 /* Enable or Disable the Output Compare Fast Bit */
1167 tmpccmr1 |= (u16)(TIM_OCFast << 8);
1169 TIMx->CCMR1 = (u16)tmpccmr1;
1172 /*******************************************************************************
1173 * Function Name : TIM_OC3FastConfig
1174 * Description : Configures the TIMx Output Compare 3 Fast feature.
1175 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1176 * - TIM_OCFast: new state of the Output Compare Fast Enable Bit.
1177 * This parameter can be one of the following values:
1178 * - TIM_OCFast_Enable
1179 * - TIM_OCFast_Disable
1182 *******************************************************************************/
1183 void TIM_OC3FastConfig(TIM_TypeDef* TIMx, u16 TIM_OCFast)
1187 /* Check the parameters */
1188 assert(IS_TIM_OCFAST_STATE(TIM_OCFast));
1190 tmpccmr2 = TIMx->CCMR2;
1192 /* Reset the OCFE Bit */
1193 tmpccmr2 &= CCMR_OC13FE_Mask;
1195 /* Enable or Disable the Output Compare Fast Bit */
1196 tmpccmr2 |= TIM_OCFast;
1198 TIMx->CCMR2 = (u16)tmpccmr2;
1201 /*******************************************************************************
1202 * Function Name : TIM_OC4FastConfig
1203 * Description : Configures the TIMx Output Compare 4 Fast feature.
1204 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1205 * - TIM_OCFast: new state of the Output Compare Fast Enable Bit.
1206 * This parameter can be one of the following values:
1207 * - TIM_OCFast_Enable
1208 * - TIM_OCFast_Disable
1211 *******************************************************************************/
1212 void TIM_OC4FastConfig(TIM_TypeDef* TIMx, u16 TIM_OCFast)
1216 /* Check the parameters */
1217 assert(IS_TIM_OCFAST_STATE(TIM_OCFast));
1219 tmpccmr2 = TIMx->CCMR2;
1221 /* Reset the OCFE Bit */
1222 tmpccmr2 &= CCMR_OC24FE_Mask;
1224 /* Enable or Disable the Output Compare Fast Bit */
1225 tmpccmr2 |= (u16)(TIM_OCFast << 8);
1227 TIMx->CCMR2 = (u16)tmpccmr2;
1230 /*******************************************************************************
1231 * Function Name : TIM_UpdateDisableConfig
1232 * Description : Enables or Disables the TIMx Update event.
1233 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1234 * - Newstate: new state of the TIMx peripheral Preload register
1235 * This parameter can be: ENABLE or DISABLE.
1238 *******************************************************************************/
1239 void TIM_UpdateDisableConfig(TIM_TypeDef* TIMx, FunctionalState Newstate)
1243 /* Check the parameters */
1244 assert(IS_FUNCTIONAL_STATE(Newstate));
1248 if (Newstate != DISABLE)
1250 /* Set the Update Disable Bit */
1251 tmpcr1 |= CR1_UDIS_Set;
1255 /* Reset the Update Disable Bit */
1256 tmpcr1 &= CR1_UDIS_Reset;
1259 TIMx->CR1 = (u16)tmpcr1;
1262 /*******************************************************************************
1263 * Function Name : TIM_EncoderInterfaceConfig
1264 * Description : Configures the TIMx Encoder Interface.
1265 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1266 * - TIM_EncoderMode: specifies the TIMx Encoder Mode.
1267 * This parameter can be one of the following values:
1268 * - TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge
1269 * depending on TI2FP2 level.
1270 * - TIM_EncoderMode_TI2: Counter counts on TI2FP2 edge
1271 * depending on TI1FP1 level.
1272 * - TIM_EncoderMode_TI12: Counter counts on both TI1FP1 and
1273 * TI2FP2 edges depending on the level of the other input.
1274 * - TIM_IC1Polarity: specifies the IC1 Polarity
1275 * This parmeter can be one of the following values:
1276 * - TIM_ICPolarity_Falling
1277 * - TIM_ICPolarity_Rising
1278 * - TIM_IC2Polarity: specifies the IC2 Polarity
1279 * This parmeter can be one of the following values:
1280 * - TIM_ICPolarity_Falling
1281 * - TIM_ICPolarity_Rising
1284 *******************************************************************************/
1285 void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, u16 TIM_EncoderMode,
1286 u16 TIM_IC1Polarity, u16 TIM_IC2Polarity)
1292 /* Check the parameters */
1293 assert(IS_TIM_ENCODER_MODE(TIM_EncoderMode));
1294 assert(IS_TIM_IC_POLARITY(TIM_IC1Polarity));
1295 assert(IS_TIM_IC_POLARITY(TIM_IC2Polarity));
1297 tmpsmcr = TIMx->SMCR;
1298 tmpccmr1 = TIMx->CCMR1;
1299 tmpccer = TIMx->CCER;
1301 /* Set the encoder Mode */
1302 tmpsmcr &= SMCR_SMS_Mask;
1303 tmpsmcr |= TIM_EncoderMode;
1305 /* Select the Capture Compare 1 and the Capture Compare 2 as input */
1306 tmpccmr1 &= CCMR_CC13S_Mask & CCMR_CC24S_Mask;
1307 tmpccmr1 |= CCMR_TI13Direct_Set | CCMR_TI24Direct_Set;
1309 /* Set the TI1 and the TI2 Polarities */
1310 tmpccer &= CCER_CC1P_Mask & CCER_CC2P_Mask;
1311 tmpccer |= (TIM_IC1Polarity | (u16)((u16)TIM_IC2Polarity << 4));
1313 TIMx->SMCR = (u16)tmpsmcr;
1315 TIMx->CCMR1 = (u16)tmpccmr1;
1317 TIMx->CCER = (u16)tmpccer;
1320 /*******************************************************************************
1321 * Function Name : TIM_GenerateEvent
1322 * Description : Configures the TIMx event to be generate by software.
1323 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1324 * - TIM_EventSource: specifies the event source.
1325 * This parameter can be one or more of the following values:
1326 * - TIM_EventSource_Update: Timer update Event source
1327 * - TIM_EventSource_CC1: Timer Capture Compare 1 Event source
1328 * - TIM_EventSource_CC2: Timer Capture Compare 2 Event source
1329 * - TIM_EventSource_CC3: Timer Capture Compare 3 Event source
1330 * - TIM_EventSource_CC4: Timer Capture Compare 4 Event source
1331 * - TIM_EventSource_Trigger: Timer Trigger Event source
1334 *******************************************************************************/
1335 void TIM_GenerateEvent(TIM_TypeDef* TIMx, u16 TIM_EventSource)
1337 /* Check the parameters */
1338 assert(IS_TIM_EVENT_SOURCE(TIM_EventSource));
1340 /* Set the event sources */
1341 TIMx->EGR |= TIM_EventSource;
1344 /*******************************************************************************
1345 * Function Name : TIM_OC1PolarityConfig
1346 * Description : Configures the TIMx channel 1 polarity.
1347 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1348 * - TIM_OCPolarity: specifies the OC1 Polarity
1349 * This parmeter can be one of the following values:
1350 * - TIM_OCPolarity_High: Output Compare active high
1351 * - TIM_OCPolarity_Low: Output Compare active low
1354 *******************************************************************************/
1355 void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, u16 TIM_OCPolarity)
1359 /* Check the parameters */
1360 assert(IS_TIM_OC_POLARITY(TIM_OCPolarity));
1362 tmpccer = TIMx->CCER;
1364 /* Set or Reset the CC1P Bit */
1365 tmpccer &= CCER_CC1P_Mask;
1366 tmpccer |= TIM_OCPolarity;
1368 TIMx->CCER = (u16)tmpccer;
1371 /*******************************************************************************
1372 * Function Name : TIM_OC2PolarityConfig
1373 * Description : Configures the TIMx channel 2 polarity.
1374 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1375 * - TIM_OCPolarity: specifies the OC2 Polarity
1376 * This parmeter can be one of the following values:
1377 * - TIM_OCPolarity_High: Output Compare active high
1378 * - TIM_OCPolarity_Low: Output Compare active low
1381 *******************************************************************************/
1382 void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, u16 TIM_OCPolarity)
1386 /* Check the parameters */
1387 assert(IS_TIM_OC_POLARITY(TIM_OCPolarity));
1389 tmpccer = TIMx->CCER;
1391 /* Set or Reset the CC2P Bit */
1392 tmpccer &= CCER_CC2P_Mask;
1393 tmpccer |= (u16)((u16)TIM_OCPolarity << 4);
1395 TIMx->CCER = (u16)tmpccer;
1398 /*******************************************************************************
1399 * Function Name : TIM_OC3PolarityConfig
1400 * Description : Configures the TIMx channel 3 polarity.
1401 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1402 * - TIM_OCPolarity: specifies the OC3 Polarity
1403 * This parmeter can be one of the following values:
1404 * - TIM_OCPolarity_High: Output Compare active high
1405 * - TIM_OCPolarity_Low: Output Compare active low
1408 *******************************************************************************/
1409 void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, u16 TIM_OCPolarity)
1413 /* Check the parameters */
1414 assert(IS_TIM_OC_POLARITY(TIM_OCPolarity));
1416 tmpccer = TIMx->CCER;
1418 /* Set or Reset the CC3P Bit */
1419 tmpccer &= CCER_CC3P_Mask;
1420 tmpccer |= (u16)((u16)TIM_OCPolarity << 8);
1422 TIMx->CCER = (u16)tmpccer;
1425 /*******************************************************************************
1426 * Function Name : TIM_OC4PolarityConfig
1427 * Description : Configures the TIMx channel 4 polarity.
1428 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1429 * - TIM_OCPolarity: specifies the OC4 Polarity
1430 * This parmeter can be one of the following values:
1431 * - TIM_OCPolarity_High: Output Compare active high
1432 * - TIM_OCPolarity_Low: Output Compare active low
1435 *******************************************************************************/
1436 void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, u16 TIM_OCPolarity)
1440 /* Check the parameters */
1441 assert(IS_TIM_OC_POLARITY(TIM_OCPolarity));
1443 tmpccer = TIMx->CCER;
1445 /* Set or Reset the CC4P Bit */
1446 tmpccer &= CCER_CC4P_Mask;
1447 tmpccer |= (u16)((u16)TIM_OCPolarity << 12);
1449 TIMx->CCER = (u16)tmpccer;
1452 /*******************************************************************************
1453 * Function Name : TIM_UpdateRequestConfig
1454 * Description : Configures the TIMx Update Request Interrupt source.
1455 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1456 * - TIM_UpdateSource: specifies the Update source.
1457 * This parameter can be one of the following values:
1458 * - TIM_UpdateSource_Regular
1459 * - TIM_UpdateSource_Global
1462 *******************************************************************************/
1463 void TIM_UpdateRequestConfig(TIM_TypeDef* TIMx, u16 TIM_UpdateSource)
1467 /* Check the parameters */
1468 assert(IS_TIM_UPDATE_SOURCE(TIM_UpdateSource));
1472 if (TIM_UpdateSource == TIM_UpdateSource_Regular)
1474 /* Set the URS Bit */
1475 tmpcr1 |= CR1_URS_Set;
1479 /* Reset the URS Bit */
1480 tmpcr1 &= CR1_URS_Reset;
1482 TIMx->CR1 = (u16)tmpcr1;
1485 /*******************************************************************************
1486 * Function Name : TIM_SelectHallSensor
1487 * Description : Enables or disables the TIMx
\92s Hall sensor interface.
1488 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1489 * - Newstate: new state of the TIMx Hall sensor interface.
1490 * This parameter can be: ENABLE or DISABLE.
1493 *******************************************************************************/
1494 void TIM_SelectHallSensor(TIM_TypeDef* TIMx, FunctionalState Newstate)
1496 /* Check the parameters */
1497 assert(IS_FUNCTIONAL_STATE(Newstate));
1499 if (Newstate != DISABLE)
1501 /* Set the TI1S Bit */
1502 TIMx->CR2 |= CR2_TI1S_Set;
1506 /* Reset the TI1S Bit */
1507 TIMx->CR2 &= CR2_TI1S_Reset;
1511 /*******************************************************************************
1512 * Function Name : TIM_SelectOnePulseMode
1513 * Description : Selects the TIMx
\92s One Pulse Mode.
1514 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1515 * - TIM_OPMode: specifies the OPM Mode to be used.
1516 * This parameter can be one of the following values:
1517 * - TIM_OPMode_Single
1518 * - TIM_OPMode_Repetitive
1521 *******************************************************************************/
1522 void TIM_SelectOnePulseMode(TIM_TypeDef* TIMx, u16 TIM_OPMode)
1526 /* Check the parameters */
1527 assert(IS_TIM_OPM_MODE(TIM_OPMode));
1531 /* Reset the OPM Bit */
1532 tmpcr1 &= CR1_OPM_Mask;
1534 /* Configure the OPM Mode */
1535 tmpcr1 |= TIM_OPMode;
1537 TIMx->CR1 = (u16)tmpcr1;
1540 /*******************************************************************************
1541 * Function Name : TIM_SelectOutputTrigger
1542 * Description : Selects the TIMx Trigger Output Mode.
1543 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1544 * - TIM_TRGOSource: specifies the Trigger Output source.
1545 * This paramter can be one of the following values:
1546 * - TIM_TRGOSource_Reset
1547 * - TIM_TRGOSource_Enable
1548 * - TIM_TRGOSource_Update
1549 * - TIM_TRGOSource_OC1
1550 * - TIM_TRGOSource_OC1Ref
1551 * - TIM_TRGOSource_OC2Ref
1552 * - TIM_TRGOSource_OC3Ref
1553 * - TIM_TRGOSource_OC4Ref
1556 *******************************************************************************/
1557 void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, u16 TIM_TRGOSource)
1561 /* Check the parameters */
1562 assert(IS_TIM_TRGO_SOURCE(TIM_TRGOSource));
1565 /* Reset the MMS Bits */
1566 tmpcr2 &= CR2_MMS_Mask;
1568 /* Select the TRGO source */
1569 tmpcr2 |= TIM_TRGOSource;
1571 TIMx->CR2 = (u16)tmpcr2;
1574 /*******************************************************************************
1575 * Function Name : TIM_SelectSlaveMode
1576 * Description : Selects the TIMx Slave Mode.
1577 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1578 * - TIM_SlaveMode: specifies the Timer Slave Mode.
1579 * This paramter can be one of the following values:
1580 * - TIM_SlaveMode_Reset
1581 * - TIM_SlaveMode_Gated
1582 * - TIM_SlaveMode_Trigger
1583 * - TIM_SlaveMode_External1
1586 *******************************************************************************/
1587 void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, u16 TIM_SlaveMode)
1591 /* Check the parameters */
1592 assert(IS_TIM_SLAVE_MODE(TIM_SlaveMode));
1594 tmpsmcr = TIMx->SMCR;
1596 /* Reset the SMS Bits */
1597 tmpsmcr &= SMCR_SMS_Mask;
1599 /* Select the Slave Mode */
1600 tmpsmcr |= TIM_SlaveMode;
1602 TIMx->SMCR = (u16)tmpsmcr;
1605 /*******************************************************************************
1606 * Function Name : TIM_SelectMasterSlaveMode
1607 * Description : Sets or Resets the TIMx Master/Slave Mode.
1608 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1609 * - TIM_MasterSlaveMode: specifies the Timer Master Slave Mode.
1610 * This paramter can be one of the following values:
1611 * - TIM_MasterSlaveMode_Enable: synchronization between the
1612 * current timer and its slaves (through TRGO).
1613 * - TIM_MasterSlaveMode_Disable: No action
1616 *******************************************************************************/
1617 void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, u16 TIM_MasterSlaveMode)
1621 /* Check the parameters */
1622 assert(IS_TIM_MSM_STATE(TIM_MasterSlaveMode));
1624 tmpsmcr = TIMx->SMCR;
1626 /* Set or Reset the MSM Bit */
1627 tmpsmcr &= SMCR_MSM_Mask;
1628 tmpsmcr |= TIM_MasterSlaveMode;
1630 TIMx->SMCR = (u16)tmpsmcr;
1633 /*******************************************************************************
1634 * Function Name : TIM_SetAutoreload
1635 * Description : Sets the TIMx Autoreload Register value
1636 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1637 * - Autoreload: specifies the Autoreload register new value.
1640 *******************************************************************************/
1641 void TIM_SetAutoreload(TIM_TypeDef* TIMx, u16 Autoreload)
1643 /* Set the Autoreload Register value */
1644 TIMx->ARR = Autoreload;
1647 /*******************************************************************************
1648 * Function Name : TIM_SetCompare1
1649 * Description : Sets the TIMx Capture Compare1 Register value
1650 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1651 * - Compare1: specifies the Capture Compare1 register new value.
1654 *******************************************************************************/
1655 void TIM_SetCompare1(TIM_TypeDef* TIMx, u16 Compare1)
1657 /* Set the Capture Compare1 Register value */
1658 TIMx->CCR1 = Compare1;
1661 /*******************************************************************************
1662 * Function Name : TIM_SetCompare2
1663 * Description : Sets the TIMx Capture Compare2 Register value
1664 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1665 * - Compare2: specifies the Capture Compare2 register new value.
1668 *******************************************************************************/
1669 void TIM_SetCompare2(TIM_TypeDef* TIMx, u16 Compare2)
1671 /* Set the Capture Compare2 Register value */
1672 TIMx->CCR2 = Compare2;
1675 /*******************************************************************************
1676 * Function Name : TIM_SetCompare3
1677 * Description : Sets the TIMx Capture Compare3 Register value
1678 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1679 * - Compare3: specifies the Capture Compare3 register new value.
1682 *******************************************************************************/
1683 void TIM_SetCompare3(TIM_TypeDef* TIMx, u16 Compare3)
1685 /* Set the Capture Compare3 Register value */
1686 TIMx->CCR3 = Compare3;
1689 /*******************************************************************************
1690 * Function Name : TIM_SetCompare4
1691 * Description : Sets the TIMx Capture Compare4 Register value
1692 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1693 * - Compare4: specifies the Capture Compare4 register new value.
1696 *******************************************************************************/
1697 void TIM_SetCompare4(TIM_TypeDef* TIMx, u16 Compare4)
1699 /* Set the Capture Compare4 Register value */
1700 TIMx->CCR4 = Compare4;
1703 /*******************************************************************************
1704 * Function Name : TIM_SetIC1Prescaler
1705 * Description : Sets the TIMx Input Capture 1 prescaler.
1706 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1707 * - TIM_IC1Prescaler: specifies the Input Capture1 prescaler
1709 * This parameter can be one of the following values:
1710 * - TIM_ICPSC_DIV1: no prescaler
1711 * - TIM_ICPSC_DIV2: capture is done once every 2 events
1712 * - TIM_ICPSC_DIV4: capture is done once every 4 events
1713 * - TIM_ICPSC_DIV8: capture is done once every 8 events
1716 *******************************************************************************/
1717 void TIM_SetIC1Prescaler(TIM_TypeDef* TIMx, u16 TIM_IC1Prescaler)
1721 /* Check the parameters */
1722 assert(IS_TIM_IC_PRESCALER(TIM_IC1Prescaler));
1724 tmpccmr1 = TIMx->CCMR1;
1726 /* Reset the IC1PSC Bits */
1727 tmpccmr1 &= CCMR_IC13PSC_Mask;
1729 /* Set the IC1PSC value */
1730 tmpccmr1 |= TIM_IC1Prescaler;
1732 TIMx->CCMR1 = (u16)tmpccmr1;
1735 /*******************************************************************************
1736 * Function Name : TIM_SetIC2Prescaler
1737 * Description : Sets the TIMx Input Capture 2 prescaler.
1738 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1739 * - TIM_IC2Prescaler: specifies the Input Capture2 prescaler
1741 * This parameter can be one of the following values:
1742 * - TIM_ICPSC_DIV1: no prescaler
1743 * - TIM_ICPSC_DIV2: capture is done once every 2 events
1744 * - TIM_ICPSC_DIV4: capture is done once every 4 events
1745 * - TIM_ICPSC_DIV8: capture is done once every 8 events
1748 *******************************************************************************/
1749 void TIM_SetIC2Prescaler(TIM_TypeDef* TIMx, u16 TIM_IC2Prescaler)
1753 /* Check the parameters */
1754 assert(IS_TIM_IC_PRESCALER(TIM_IC2Prescaler));
1756 tmpccmr1 = TIMx->CCMR1;
1758 /* Reset the IC2PSC Bits */
1759 tmpccmr1 &= CCMR_IC24PSC_Mask;
1761 /* Set the IC2PSC value */
1762 tmpccmr1 |= (u16)((u16)TIM_IC2Prescaler << 8);
1764 TIMx->CCMR1 = (u16)tmpccmr1;
1767 /*******************************************************************************
1768 * Function Name : TIM_SetIC3Prescaler
1769 * Description : Sets the TIMx Input Capture 3 prescaler.
1770 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1771 * - TIM_IC3Prescaler: specifies the Input Capture3 prescaler
1773 * This parameter can be one of the following values:
1774 * - TIM_ICPSC_DIV1: no prescaler
1775 * - TIM_ICPSC_DIV2: capture is done once every 2 events
1776 * - TIM_ICPSC_DIV4: capture is done once every 4 events
1777 * - TIM_ICPSC_DIV8: capture is done once every 8 events
1780 *******************************************************************************/
1781 void TIM_SetIC3Prescaler(TIM_TypeDef* TIMx, u16 TIM_IC3Prescaler)
1785 /* Check the parameters */
1786 assert(IS_TIM_IC_PRESCALER(TIM_IC3Prescaler));
1788 tmpccmr2 = TIMx->CCMR2;
1790 /* Reset the IC3PSC Bits */
1791 tmpccmr2 &= CCMR_IC13PSC_Mask;
1793 /* Set the IC3PSC value */
1794 tmpccmr2 |= TIM_IC3Prescaler;
1796 TIMx->CCMR2 = (u16)tmpccmr2;
1799 /*******************************************************************************
1800 * Function Name : TIM_SetIC4Prescaler
1801 * Description : Sets the TIMx Input Capture 4 prescaler.
1802 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1803 * - TIM_IC4Prescaler: specifies the Input Capture4 prescaler
1805 * This parameter can be one of the following values:
1806 * - TIM_ICPSC_DIV1: no prescaler
1807 * - TIM_ICPSC_DIV2: capture is done once every 2 events
1808 * - TIM_ICPSC_DIV4: capture is done once every 4 events
1809 * - TIM_ICPSC_DIV8: capture is done once every 8 events
1812 *******************************************************************************/
1813 void TIM_SetIC4Prescaler(TIM_TypeDef* TIMx, u16 TIM_IC4Prescaler)
1817 /* Check the parameters */
1818 assert(IS_TIM_IC_PRESCALER(TIM_IC4Prescaler));
1820 tmpccmr2 = TIMx->CCMR2;
1822 /* Reset the IC4PSC Bits */
1823 tmpccmr2 &= CCMR_IC24PSC_Mask;
1825 /* Set the IC4PSC value */
1826 tmpccmr2 |= (u16)((u16)TIM_IC4Prescaler << 8);
1828 TIMx->CCMR2 = (u16)tmpccmr2;
1831 /*******************************************************************************
1832 * Function Name : TIM_SetClockDivision
1833 * Description : Sets the TIMx Clock Division value.
1834 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1835 * - TIM_CKD: specifies the clock division value.
1836 * This parameter can be one of the following value:
1837 * - TIM_CKD_DIV1: TDTS = Tck_tim
1838 * - TIM_CKD_DIV2: TDTS = 2*Tck_tim
1839 * - TIM_CKD_DIV4: TDTS = 4*Tck_tim
1842 *******************************************************************************/
1843 void TIM_SetClockDivision(TIM_TypeDef* TIMx, u16 TIM_CKD)
1847 /* Check the parameters */
1848 assert(IS_TIM_CKD_DIV(TIM_CKD));
1852 /* Reset the CKD Bits */
1853 tmpcr1 &= CR1_CKD_Mask;
1855 /* Set the CKD value */
1858 TIMx->CR1 = (u16)tmpcr1;
1861 /*******************************************************************************
1862 * Function Name : TIM_GetCapture1
1863 * Description : Gets the TIMx Input Capture 1 value.
1864 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1866 * Return : Capture Compare 1 Register value.
1867 *******************************************************************************/
1868 u16 TIM_GetCapture1(TIM_TypeDef* TIMx)
1870 /* Get the Capture 1 Register value */
1874 /*******************************************************************************
1875 * Function Name : TIM_GetCapture2
1876 * Description : Gets the TIMx Input Capture 2 value.
1877 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1879 * Return : Capture Compare 2 Register value.
1880 *******************************************************************************/
1881 u16 TIM_GetCapture2(TIM_TypeDef* TIMx)
1883 /* Get the Capture 2 Register value */
1887 /*******************************************************************************
1888 * Function Name : TIM_GetCapture3
1889 * Description : Gets the TIMx Input Capture 3 value.
1890 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1892 * Return : Capture Compare 3 Register value.
1893 *******************************************************************************/
1894 u16 TIM_GetCapture3(TIM_TypeDef* TIMx)
1896 /* Get the Capture 3 Register value */
1900 /*******************************************************************************
1901 * Function Name : TIM_GetCapture4
1902 * Description : Gets the TIMx Input Capture 4 value.
1903 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1905 * Return : Capture Compare 4 Register value.
1906 *******************************************************************************/
1907 u16 TIM_GetCapture4(TIM_TypeDef* TIMx)
1909 /* Get the Capture 4 Register value */
1913 /*******************************************************************************
1914 * Function Name : TIM_GetCounter
1915 * Description : Gets the TIMx Counter value.
1916 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1918 * Return : Counter Register value.
1919 *******************************************************************************/
1920 u16 TIM_GetCounter(TIM_TypeDef* TIMx)
1922 /* Get the Counter Register value */
1926 /*******************************************************************************
1927 * Function Name : TIM_GetPrescaler
1928 * Description : Gets the TIMx Prescaler value.
1929 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1931 * Return : Prescaler Register value.
1932 *******************************************************************************/
1933 u16 TIM_GetPrescaler(TIM_TypeDef* TIMx)
1935 /* Get the Prescaler Register value */
1939 /*******************************************************************************
1940 * Function Name : TIM_GetFlagStatus
1941 * Description : Checks whether the specified TIMx flag is set or not.
1942 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1943 * - TIM_FLAG: specifies the flag to check.
1944 * This parameter can be one of the following values:
1945 * - TIM_FLAG_Update: Timer update Flag
1946 * - TIM_FLAG_CC1: Timer Capture Compare 1 Flag
1947 * - TIM_FLAG_CC2: Timer Capture Compare 2 Flag
1948 * - TIM_FLAG_CC3: Timer Capture Compare 3 Flag
1949 * - TIM_FLAG_CC4: Timer Capture Compare 4 Flag
1950 * - TIM_FLAG_Trigger: Timer Trigger Flag
1951 * - TIM_FLAG_CC1OF: Timer Capture Compare 1 overcapture Flag
1952 * - TIM_FLAG_CC2OF: Timer Capture Compare 2 overcapture Flag
1953 * - TIM_FLAG_CC3OF: Timer Capture Compare 3 overcapture Flag
1954 * - TIM_FLAG_CC4OF: Timer Capture Compare 4 overcapture Flag
1956 * Return : The new state of TIM_FLAG (SET or RESET).
1957 *******************************************************************************/
1958 FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, u16 TIM_FLAG)
1960 FlagStatus bitstatus = RESET;
1962 /* Check the parameters */
1963 assert(IS_TIM_GET_FLAG(TIM_FLAG));
1965 if ((TIMx->SR & TIM_FLAG) != (u16)RESET )
1976 /*******************************************************************************
1977 * Function Name : TIM_ClearFlag
1978 * Description : Clears the TIMx's pending flags.
1979 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
1980 * - TIM_FLAG: specifies the flag bit to clear.
1981 * This parameter can be any combination of the following values:
1982 * - TIM_FLAG_Update: Timer update Flag
1983 * - TIM_FLAG_CC1: Timer Capture Compare 1 Flag
1984 * - TIM_FLAG_CC2: Timer Capture Compare 2 Flag
1985 * - TIM_FLAG_CC3: Timer Capture Compare 3 Flag
1986 * - TIM_FLAG_CC4: Timer Capture Compare 4 Flag
1987 * - TIM_FLAG_Trigger: Timer Trigger Flag
1988 * - TIM_FLAG_CC1OF: Timer Capture Compare 1 overcapture Flag
1989 * - TIM_FLAG_CC2OF: Timer Capture Compare 2 overcapture Flag
1990 * - TIM_FLAG_CC3OF: Timer Capture Compare 3 overcapture Flag
1991 * - TIM_FLAG_CC4OF: Timer Capture Compare 4 overcapture Flag
1994 *******************************************************************************/
1995 void TIM_ClearFlag(TIM_TypeDef* TIMx, u16 TIM_FLAG)
1997 /* Check the parameters */
1998 assert(IS_TIM_CLEAR_FLAG(TIM_FLAG));
2000 /* Clear the flags */
2001 TIMx->SR &= (u16)~TIM_FLAG;
2004 /*******************************************************************************
2005 * Function Name : TIM_GetITStatus
2006 * Description : Checks whether the TIMx interrupt has occurred or not.
2007 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
2008 * - TIM_IT: specifies the TIM interrupt source to check.
2009 * This parameter can be one of the following values:
2010 * - TIM_IT_Update: Timer update Interrupt source
2011 * - TIM_IT_CC1: Timer Capture Compare 1 Interrupt source
2012 * - TIM_IT_CC2: Timer Capture Compare 2 Interrupt source
2013 * - TIM_IT_CC3: Timer Capture Compare 3 Interrupt source
2014 * - TIM_IT_CC4: Timer Capture Compare 4 Interrupt source
2015 * - TIM_IT_Trigger: Timer Trigger Interrupt source
2017 * Return : The new state of the TIM_IT(SET or RESET).
2018 *******************************************************************************/
2019 ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, u16 TIM_IT)
2021 ITStatus bitstatus = RESET;
2023 u16 itstatus = 0x0, itenable = 0x0;
2025 /* Check the parameters */
2026 assert(IS_TIM_GET_IT(TIM_IT));
2028 itstatus = TIMx->SR & TIM_IT;
2030 itenable = TIMx->DIER & TIM_IT;
2032 if ((itstatus != (u16)RESET) && (itenable != (u16)RESET))
2043 /*******************************************************************************
2044 * Function Name : TIM_ClearITPendingBit
2045 * Description : Clears the TIMx's interrupt pending bits.
2046 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
2047 * - TIM_IT: specifies the pending bit to clear.
2048 * This parameter can be any combination of the following values:
2049 * - TIM_IT_Update: Timer update Interrupt source
2050 * - TIM_IT_CC1: Timer Capture Compare 1 Interrupt source
2051 * - TIM_IT_CC2: Timer Capture Compare 2 Interrupt source
2052 * - TIM_IT_CC3: Timer Capture Compare 3 Interrupt source
2053 * - TIM_IT_CC4: Timer Capture Compare 4 Interrupt source
2054 * - TIM_IT_Trigger: Timer Trigger Interrupt source
2057 *******************************************************************************/
2058 void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, u16 TIM_IT)
2060 /* Check the parameters */
2061 assert(IS_TIM_IT(TIM_IT));
2063 /* Clear the IT pending Bit */
2064 TIMx->SR &= (u16)~TIM_IT;
2067 /*******************************************************************************
2068 * Function Name : PWMInput_Config
2069 * Description : Configures the TIM peripheral according to the specified
2070 * parameters in the TIM_ICInitStruct to measure an external PWM
2072 * Input : - TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure
2073 * that contains the configuration information for the specified
2077 *******************************************************************************/
2078 static void PWMI_Config(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
2080 u8 ICPolarity = TIM_ICPolarity_Rising;
2081 u8 ICSelection = TIM_ICSelection_DirectTI;
2083 /* Select the Opposite Input Polarity */
2084 if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)
2086 ICPolarity = TIM_ICPolarity_Falling;
2090 ICPolarity = TIM_ICPolarity_Rising;
2093 /* Select the Opposite Input */
2094 if (TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)
2096 ICSelection = TIM_ICSelection_IndirectTI;
2100 ICSelection = TIM_ICSelection_DirectTI;
2103 if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
2105 /* TI1 Configuration */
2106 TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
2107 TIM_ICInitStruct->TIM_ICFilter);
2109 /* Set the Input Capture Prescaler value */
2110 TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
2112 /* TI2 Configuration */
2113 TI2_Config(TIMx, ICPolarity, ICSelection, TIM_ICInitStruct->TIM_ICFilter);
2115 /* Set the Input Capture Prescaler value */
2116 TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
2120 /* TI1 Configuration */
2121 TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
2122 TIM_ICInitStruct->TIM_ICFilter);
2124 /* Set the Input Capture Prescaler value */
2125 TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
2127 /* TI2 Configuration */
2128 TI1_Config(TIMx, ICPolarity, ICSelection, TIM_ICInitStruct->TIM_ICFilter);
2130 /* Set the Input Capture Prescaler value */
2131 TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
2135 /*******************************************************************************
2136 * Function Name : TI1_Config
2137 * Description : Configure the TI1 as Input.
2138 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
2139 * - TIM_ICPolarity : The Input Polarity.
2140 * This parameter can be one of the following values:
2141 * - TIM_ICPolarity_Rising
2142 * - TIM_ICPolarity_Falling
2143 * - TIM_ICSelection: specifies the input to be used.
2144 * This parameter can be one of the following values:
2145 * - TIM_ICSelection_DirectTI: TIM Input 1 is selected to
2146 * be connected to IC1.
2147 * - TIM_ICSelection_IndirectTI: TIM Input 1 is selected to
2148 * be connected to IC2.
2149 * - TIM_ICSelection_TRGI: TIM Input 1 is selected to be
2150 * connected to TRGI.
2151 * - TIM_ICFilter: Specifies the Input Capture Filter.
2152 * This parameter must be a value between 0x00 and 0x0F.
2155 *******************************************************************************/
2156 static void TI1_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,
2159 u32 tmpccmr1 = 0, tmpccer = 0;
2161 tmpccmr1 = TIMx->CCMR1;
2162 tmpccer = TIMx->CCER;
2164 /* Disable the Channel 1: Reset the CCE Bit */
2165 TIMx->CCER &= CCRE_CC1E_Reset;
2167 /* Select the Input and set the filter */
2168 tmpccmr1 &= CCMR_CC13S_Mask & CCMR_IC13F_Mask;
2169 tmpccmr1 |= TIM_ICSelection | (u16)((u16)TIM_ICFilter << 4);
2171 /* Select the Polarity and set the CCE Bit */
2172 tmpccer &= CCER_CC1P_Mask & CCRE_CC1E_Mask;
2173 tmpccer |= TIM_ICPolarity | CCRE_CC1E_Set;
2175 TIMx->CCMR1 = 0x0000;
2176 TIMx->CCMR1 = (u16)tmpccmr1;
2177 TIMx->CCER = (u16)tmpccer;
2180 /*******************************************************************************
2181 * Function Name : TI2_Config
2182 * Description : Configure the TI2 as Input.
2183 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
2184 * - TIM_ICPolarity : The Input Polarity.
2185 * This parameter can be one of the following values:
2186 * - TIM_ICPolarity_Rising
2187 * - TIM_ICPolarity_Falling
2188 * - TIM_ICSelection: specifies the input to be used.
2189 * This parameter can be one of the following values:
2190 * - TIM_ICSelection_DirectTI: TIM Input 2 is selected to
2191 * be connected to IC2.
2192 * - TIM_ICSelection_IndirectTI: TIM Input 2 is selected to
2193 * be connected to IC1.
2194 * - TIM_ICSelection_TRGI: TIM Input 2 is selected to be
2195 * connected to TRGI.
2196 * - TIM_ICFilter: Specifies the Input Capture Filter.
2197 * This parameter must be a value between 0x00 and 0x0F.
2200 *******************************************************************************/
2201 static void TI2_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,
2204 u32 tmpccmr1 = 0, tmpccer = 0, tmp = 0;
2206 tmpccmr1 = TIMx->CCMR1;
2207 tmpccer = TIMx->CCER;
2208 tmp = (u16)((u16)TIM_ICPolarity << 4);
2210 /* Disable the Channel 2: Reset the CCE Bit */
2211 TIMx->CCER &= CCRE_CC2E_Reset;
2213 /* Select the Input and set the filter */
2214 tmpccmr1 &= CCMR_CC24S_Mask & CCMR_IC24F_Mask;
2215 tmpccmr1 |= (u16)((u16)TIM_ICFilter << 12);
2216 tmpccmr1 |= (u16)((u16)TIM_ICSelection << 8);
2218 /* Select the Polarity and set the CCE Bit */
2219 tmpccer &= CCER_CC2P_Mask & CCRE_CC2E_Mask;
2220 tmpccer |= tmp | CCRE_CC2E_Set;
2222 TIMx->CCMR1 = (u16)tmpccmr1 ;
2223 TIMx->CCER = (u16)tmpccer;
2226 /*******************************************************************************
2227 * Function Name : TI3_Config
2228 * Description : Configure the TI3 as Input.
2229 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
2230 * - TIM_ICPolarity : The Input Polarity.
2231 * This parameter can be one of the following values:
2232 * - TIM_ICPolarity_Rising
2233 * - TIM_ICPolarity_Falling
2234 * - TIM_ICSelection: specifies the input to be used.
2235 * This parameter can be one of the following values:
2236 * - TIM_ICSelection_DirectTI: TIM Input 3 is selected to
2237 * be connected to IC3.
2238 * - TIM_ICSelection_IndirectTI: TIM Input 3 is selected to
2239 * be connected to IC4.
2240 * - TIM_ICSelection_TRGI: TIM Input 3 is selected to be
2241 * connected to TRGI.
2242 * - TIM_ICFilter: Specifies the Input Capture Filter.
2243 * This parameter must be a value between 0x00 and 0x0F.
2246 *******************************************************************************/
2247 static void TI3_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,
2250 u32 tmpccmr2 = 0, tmpccer = 0, tmp = 0;
2252 tmpccmr2 = TIMx->CCMR2;
2253 tmpccer = TIMx->CCER;
2254 tmp = (u16)((u16)TIM_ICPolarity << 8);
2256 /* Disable the Channel 3: Reset the CCE Bit */
2257 TIMx->CCER &= CCRE_CC3E_Reset;
2259 /* Select the Input and set the filter */
2260 tmpccmr2 &= CCMR_CC13S_Mask & CCMR_IC13F_Mask;
2261 tmpccmr2 |= TIM_ICSelection | (u16)((u16)TIM_ICFilter << 4);
2263 /* Select the Polarity and set the CCE Bit */
2264 tmpccer &= CCER_CC1P_Mask & CCRE_CC1E_Mask;
2265 tmpccer |= tmp | CCRE_CC3E_Set;
2267 TIMx->CCMR2 = (u16)tmpccmr2;
2268 TIMx->CCER = (u16)tmpccer;
2271 /*******************************************************************************
2272 * Function Name : TI4_Config
2273 * Description : Configure the TI1 as Input.
2274 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
2275 * - TIM_ICPolarity : The Input Polarity.
2276 * This parameter can be one of the following values:
2277 * - TIM_ICPolarity_Rising
2278 * - TIM_ICPolarity_Falling
2279 * - TIM_ICSelection: specifies the input to be used.
2280 * This parameter can be one of the following values:
2281 * - TIM_ICSelection_DirectTI: TIM Input 4 is selected to
2282 * be connected to IC4.
2283 * - TIM_ICSelection_IndirectTI: TIM Input 4 is selected to
2284 * be connected to IC3.
2285 * - TIM_ICSelection_TRGI: TIM Input 4 is selected to be
2286 * connected to TRGI.
2287 * - TIM_ICFilter: Specifies the Input Capture Filter.
2288 * This parameter must be a value between 0x00 and 0x0F.
2291 *******************************************************************************/
2292 static void TI4_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,
2295 u32 tmpccmr2 = 0, tmpccer = 0, tmp = 0;
2297 tmpccmr2 = TIMx->CCMR2;
2298 tmpccer = TIMx->CCER;
2299 tmp = (u16)((u16)TIM_ICPolarity << 12);
2301 /* Disable the Channel 4: Reset the CCE Bit */
2302 TIMx->CCER &= CCRE_CC4E_Reset;
2304 /* Select the Input and set the filter */
2305 tmpccmr2 &= CCMR_CC24S_Mask & CCMR_IC24F_Mask;
2306 tmpccmr2 |= (u16)((u16)TIM_ICSelection << 8) | (u16)((u16)TIM_ICFilter << 12);
2308 /* Select the Polarity and set the CCE Bit */
2309 tmpccer &= CCER_CC4P_Mask & CCRE_CC4E_Mask;
2310 tmpccer |= tmp | CCRE_CC4E_Set;
2312 TIMx->CCMR2 = (u16)tmpccmr2;
2313 TIMx->CCER = (u16)tmpccer ;
2316 /*******************************************************************************
2317 * Function Name : ETR_Config
2318 * Description : Configure the External Trigger
2319 * Input : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.
2320 * - TIM_ExtTRGPrescaler: The external Trigger Prescaler.
2321 * This parameter can be one of the following values:
2322 * - TIM_ExtTRGPSC_OFF
2323 * - TIM_ExtTRGPSC_DIV2
2324 * - TIM_ExtTRGPSC_DIV4
2325 * - TIM_ExtTRGPSC_DIV8
2326 * - TIM_ExtTRGPolarity: The external Trigger Polarity.
2327 * This parameter can be one of the following values:
2328 * - TIM_ExtTRGPolarity_Inverted
2329 * - TIM_ExtTRGPolarity_NonInverted
2330 * - ExtTRGFilter: External Trigger Filter.
2331 * This parameter must be a value between 0x00 and 0x0F.
2334 *******************************************************************************/
2335 static void ETR_Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, u16 TIM_ExtTRGPolarity,
2340 tmpsmcr = TIMx->SMCR;
2342 /* Set the Prescaler, the Filter value and the Polarity */
2343 tmpsmcr &= SMCR_ETR_Mask;
2344 tmpsmcr |= TIM_ExtTRGPrescaler | TIM_ExtTRGPolarity | (u16)((u16)ExtTRGFilter << 8);
2346 TIMx->SMCR = (u16)tmpsmcr;
2348 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/