1 /******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
\r
2 * File Name : 75x_tim.c
\r
3 * Author : MCD Application Team
\r
4 * Date First Issued : 03/10/2006
\r
5 * Description : This file provides all the TIM software functions.
\r
6 ********************************************************************************
\r
10 ********************************************************************************
\r
11 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
\r
13 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
\r
14 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
\r
15 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
\r
16 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
17 *******************************************************************************/
\r
19 /* Includes ------------------------------------------------------------------*/
\r
20 #include "75x_tim.h"
\r
21 #include "75x_mrcc.h"
\r
23 /* Private typedef -----------------------------------------------------------*/
\r
24 /* Private define ------------------------------------------------------------*/
\r
25 /* Private macro -------------------------------------------------------------*/
\r
26 /* Private variables ---------------------------------------------------------*/
\r
27 /* TIM interrupt masks */
\r
28 #define TIM_IT_Clear_Mask 0x7FFF
\r
29 #define TIM_IT_Enable_Mask 0x7FFF
\r
31 /* TIM Input Capture Selection Set/Reset */
\r
32 #define TIM_IC1S_Set 0x0001
\r
33 #define TIM_IC1S_Reset 0x003E
\r
35 /* TIM Input Capture Selection Set/Reset */
\r
36 #define TIM_IC2S_Set 0x0002
\r
37 #define TIM_IC2S_Reset 0x003D
\r
39 /* TIM_SCR Masks bit */
\r
40 #define TIM_Encoder_Mask 0x731C
\r
41 #define TIM_SlaveModeSelection_Mask 0x7307
\r
42 #define TIM_TriggerSelection_Mask 0x701F
\r
43 #define TIM_InternalTriggerSelection_Mask 0x031F
\r
45 /* TIM Encoder mode Set value */
\r
46 #define TIM_Encoder1_Set 0x0001
\r
47 #define TIM_Encoder2_Set 0x0002
\r
48 #define TIM_Encoder3_Set 0x0003
\r
50 /* TIM Slave Mode Enable Set/Reset value */
\r
51 #define TIM_SME_Reset 0x731B
\r
52 #define TIM_SME_Set 0x0004
\r
54 /* TIM Internal Trigger Selection value */
\r
55 #define TIM_ITS_TIM0 0x1000
\r
56 #define TIM_ITS_TIM1 0x2000
\r
57 #define TIM_ITS_TIM2 0x3000
\r
58 #define TIM_ITS_PWM 0x4000
\r
60 /* TIM Trigger Selection value */
\r
61 #define TIM_TS_IC1_Set 0x0200
\r
62 #define TIM_TS_IC2_Set 0x0300
\r
64 /* TIM Slave Mode selction external clock Set value */
\r
65 #define TIM_SMS_EXTCLK_Set 0x0008
\r
66 #define TIM_SMS_RESETCLK_Set 0x0000
\r
68 /* TIM_CR Masks bit */
\r
69 #define TIM_DBASE_Mask 0x077F
\r
70 #define TIM_MasterModeSelection_Mask 0xFC7F
\r
71 #define TIM_CounterMode_Mask 0xFF8F
\r
73 /* TIM Update flag selection Set/Reset value */
\r
74 #define TIM_UFS_Reset 0xFFFE
\r
75 #define TIM_UFS_Set 0x0001
\r
77 /* TIM Counter value */
\r
78 #define TIM_COUNTER_Reset 0x0002
\r
79 #define TIM_COUNTER_Start 0x0004
\r
80 #define TIM_COUNTER_Stop 0xFFFB
\r
82 /* TIM One pulse Mode set value */
\r
83 #define TIM_OPM_Set 0x0008
\r
84 #define TIM_OPM_Reset 0xFFF7
\r
86 /* TIM Debug Mode Set/Reset value */
\r
87 #define TIM_DBGC_Set 0x0400
\r
88 #define TIM_DBGC_Reset 0xFB7F
\r
90 /* TIM Input Capture Enable/Disable value */
\r
91 #define TIM_IC1_Enable 0x0004
\r
92 #define TIM_IC2_Enable 0x0010
\r
94 /* TIM Input Capture Polarity Set/Reset value */
\r
95 #define TIM_IC1P_Set 0x0008
\r
96 #define TIM_IC2P_Set 0x0020
\r
97 #define TIM_IC1P_Reset 0x0037
\r
98 #define TIM_IC2P_Reset 0x001F
\r
100 /* TIM Output Compare Polarity Set/Reset value */
\r
101 #define TIM_OC1P_Set 0x0020
\r
102 #define TIM_OC2P_Set 0x2000
\r
103 #define TIM_OC1P_Reset 0x3F1F
\r
104 #define TIM_OC2P_Reset 0x1F3F
\r
106 /* TIM Output Compare control mode constant */
\r
107 #define TIM_OCControl_PWM 0x000C
\r
108 #define TIM_OCControl_OCToggle 0x0006
\r
109 #define TIM_OCControl_OCInactive 0x0004
\r
110 #define TIM_OCControl_OCActive 0x0002
\r
111 #define TIM_OCControl_OCTiming 0x0000
\r
113 /* TIM Output Compare mode Enable value */
\r
114 #define TIM_OC1_Enable 0x0010
\r
115 #define TIM_OC2_Enable 0x1000
\r
117 /* TIM Output Compare mode Mask value */
\r
118 #define TIM_OC1C_Mask 0x3F31
\r
119 #define TIM_OC2C_Mask 0x313F
\r
121 /* TIM Preload bit Set/Reset value */
\r
122 #define TIM_PLD1_Set 0x0001
\r
123 #define TIM_PLD1_Reset 0xFFFE
\r
125 #define TIM_PLD2_Set 0x0100
\r
126 #define TIM_PLD2_Reset 0xFEFF
\r
128 /* TIM OCRM Set/Reset value */
\r
129 #define TIM_OCRM_Set 0x0080
\r
130 #define TIM_OCRM_Reset 0x030D
\r
132 /* Reset Register Masks */
\r
133 #define TIM_Pulse2_Reset_Mask 0x0000
\r
134 #define TIM_Prescaler_Reset_Mask 0x0000
\r
135 #define TIM_Pulse1_Reset_Mask 0x0000
\r
136 #define TIM_Period_Reset_Mask 0xFFFF
\r
137 #define TIM_Counter_Reset 0x0002
\r
139 /* Private function prototypes -----------------------------------------------*/
\r
140 static void ICAP_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct);
\r
141 static void Encoder_ModeConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct);
\r
142 static void OCM_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct);
\r
144 /* Private functions ---------------------------------------------------------*/
\r
146 /******************************************************************************
\r
147 * Function Name : TIM_DeInit
\r
148 * Description : Deinitializes TIM peripheral registers to their default reset
\r
150 * Input : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
153 *******************************************************************************/
\r
154 void TIM_DeInit(TIM_TypeDef *TIMx)
\r
158 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM0,ENABLE);
\r
159 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM0,DISABLE);
\r
161 else if(TIMx == TIM1)
\r
163 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM1,ENABLE);
\r
164 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM1,DISABLE);
\r
166 else if(TIMx == TIM2)
\r
168 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM2,ENABLE);
\r
169 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM2,DISABLE);
\r
173 /*******************************************************************************
\r
174 * Function Name : TIM_Init
\r
175 * Description : Initializes the TIMx peripheral according to the specified
\r
176 * parameters in the TIM_InitStruct .
\r
177 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
178 * - TIM_InitStruct: pointer to a TIM_InitTypeDef structure that
\r
179 * contains the configuration information for the specified TIM
\r
183 *******************************************************************************/
\r
184 void TIM_Init(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)
\r
186 /* Set the prescaler value */
\r
187 TIMx->PSC = TIM_InitStruct->TIM_Prescaler;
\r
189 /* Select the clock source */
\r
190 TIM_ClockSourceConfig(TIMx, TIM_InitStruct->TIM_ClockSource,
\r
191 TIM_InitStruct->TIM_ExtCLKEdge);
\r
193 /* Select the counter mode */
\r
194 TIMx->CR &= TIM_CounterMode_Mask;
\r
195 TIMx->CR |= TIM_InitStruct->TIM_CounterMode;
\r
197 /* Set the period value */
\r
198 TIMx->ARR = TIM_InitStruct->TIM_Period;
\r
200 switch(TIM_InitStruct->TIM_Mode)
\r
202 case TIM_Mode_OCTiming: case TIM_Mode_OCActive: case TIM_Mode_OCInactive:
\r
203 case TIM_Mode_OCToggle: case TIM_Mode_PWM:
\r
204 OCM_ModuleConfig(TIMx, TIM_InitStruct);
\r
207 case TIM_Mode_PWMI: case TIM_Mode_IC:
\r
208 ICAP_ModuleConfig(TIMx, TIM_InitStruct);
\r
211 case TIM_Mode_Encoder1: case TIM_Mode_Encoder2: case TIM_Mode_Encoder3:
\r
212 Encoder_ModeConfig(TIMx, TIM_InitStruct);
\r
215 case TIM_Mode_OPM_PWM: case TIM_Mode_OPM_Toggle: case TIM_Mode_OPM_Active:
\r
217 /* Output module configuration */
\r
218 OCM_ModuleConfig(TIMx, TIM_InitStruct);
\r
220 /* Input module configuration */
\r
221 ICAP_ModuleConfig(TIMx, TIM_InitStruct);
\r
223 /* Set the slave mode to trigger Mode */
\r
224 TIMx->SCR |= TIM_SynchroMode_Trigger;
\r
226 /* Repetitive pulse state selection */
\r
227 if(TIM_InitStruct->TIM_RepetitivePulse == TIM_RepetitivePulse_Disable)
\r
229 TIMx->CR |= TIM_OPM_Set;
\r
233 TIMx->CR &= TIM_OPM_Reset;
\r
242 /*******************************************************************************
\r
243 * Function Name : TIM_StructInit
\r
244 * Description : Fills each TIM_InitStruct member with its default value.
\r
245 * Input : TIM_InitStruct : pointer to a TIM_InitTypeDef structure
\r
246 * which will be initialized.
\r
249 *******************************************************************************/
\r
250 void TIM_StructInit(TIM_InitTypeDef *TIM_InitStruct)
\r
252 /* Set the default configuration */
\r
253 TIM_InitStruct->TIM_Mode = TIM_Mode_OCTiming;
\r
254 TIM_InitStruct->TIM_Prescaler = TIM_Prescaler_Reset_Mask;
\r
255 TIM_InitStruct->TIM_ClockSource = TIM_ClockSource_Internal;
\r
256 TIM_InitStruct->TIM_ExtCLKEdge = TIM_ExtCLKEdge_Rising;
\r
257 TIM_InitStruct->TIM_CounterMode = TIM_CounterMode_Up;
\r
258 TIM_InitStruct->TIM_Period = TIM_Period_Reset_Mask;
\r
259 TIM_InitStruct->TIM_Channel = TIM_Channel_ALL;
\r
260 TIM_InitStruct->TIM_Pulse1 = TIM_Pulse1_Reset_Mask;
\r
261 TIM_InitStruct->TIM_Pulse2 = TIM_Pulse2_Reset_Mask;
\r
262 TIM_InitStruct->TIM_RepetitivePulse = TIM_RepetitivePulse_Disable;
\r
263 TIM_InitStruct->TIM_Polarity1 = TIM_Polarity1_Low;
\r
264 TIM_InitStruct->TIM_Polarity2 = TIM_Polarity2_Low;
\r
265 TIM_InitStruct->TIM_IC1Selection = TIM_IC1Selection_TI1;
\r
266 TIM_InitStruct->TIM_IC2Selection = TIM_IC2Selection_TI1;
\r
267 TIM_InitStruct->TIM_IC1Polarity = TIM_IC1Polarity_Rising;
\r
268 TIM_InitStruct->TIM_IC2Polarity = TIM_IC2Polarity_Rising;
\r
269 TIM_InitStruct->TIM_PWMI_ICSelection = TIM_PWMI_ICSelection_TI1;
\r
270 TIM_InitStruct->TIM_PWMI_ICPolarity = TIM_PWMI_ICPolarity_Rising;
\r
273 /*******************************************************************************
\r
274 * Function Name : TIM_Cmd
\r
275 * Description : Enables or disables the specified TIM peripheral.
\r
276 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
277 * - Newstate: new state of the TIMx peripheral.
\r
278 * This parameter can be: ENABLE or DISABLE.
\r
281 *******************************************************************************/
\r
282 void TIM_Cmd(TIM_TypeDef *TIMx, FunctionalState Newstate)
\r
284 if(Newstate == ENABLE)
\r
286 TIMx->CR |= TIM_COUNTER_Start;
\r
290 TIMx->CR &= TIM_COUNTER_Stop;
\r
294 /*******************************************************************************
\r
295 * Function Name : TIM_ITConfig
\r
296 * Description : Enables or disables the TIM interrupts.
\r
297 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
298 * - TIM_IT: specifies the TIM interrupts sources to be enabled
\r
300 * This parameter can be any combination of the following values:
\r
301 * - TIM_IT_IC1: Input Capture 1 Interrupt
\r
302 * - TIM_IT_OC1: Output Compare 1 Interrupt
\r
303 * - TIM_IT_Update: Timer update Interrupt
\r
304 * - TIM_IT_GlobalUpdate: Timer global update Interrupt
\r
305 * - TIM_IT_IC2: Input Capture 2 Interrupt
\r
306 * - TIM_IT_OC2: Output Compare 2 Interrupt
\r
307 * - Newstate: new state of the specified TIMx interrupts.
\r
308 * This parameter can be: ENABLE or DISABLE.
\r
311 *******************************************************************************/
\r
312 void TIM_ITConfig(TIM_TypeDef *TIMx, u16 TIM_IT, FunctionalState Newstate)
\r
314 u16 TIM_IT_Enable = 0;
\r
316 TIM_IT_Enable = TIM_IT & TIM_IT_Enable_Mask;
\r
318 if(Newstate == ENABLE)
\r
320 /* Update interrupt global source: overflow/undeflow, counter reset operation
\r
321 or slave mode controller in reset mode */
\r
322 if((TIM_IT & TIM_IT_GlobalUpdate) == TIM_IT_GlobalUpdate)
\r
324 TIMx->CR &= TIM_UFS_Reset;
\r
326 /* Update interrupt source: counter overflow/underflow */
\r
327 else if((TIM_IT & TIM_IT_Update) == TIM_IT_Update)
\r
329 TIMx->CR |= TIM_UFS_Set;
\r
331 /* Select and enable the interrupts requests */
\r
332 TIMx->RSR |= TIM_IT_Enable;
\r
333 TIMx->RER |= TIM_IT_Enable;
\r
335 /* Disable the interrupts requests */
\r
338 TIMx->RSR &= ~TIM_IT_Enable;
\r
339 TIMx->RER &= ~TIM_IT_Enable;
\r
343 /*******************************************************************************
\r
344 * Function Name : TIM_PreloadConfig
\r
345 * Description : Enables or disables TIM peripheral Preload register on OCRx.
\r
346 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
347 * - TIM_Channel: specifies the TIM channel to be used.
\r
348 * This parameter can be one of the following values:
\r
349 * - TIM_Channel_1: TIM Channel 1 is used
\r
350 * - TIM_Channel_2: TIM Channel 2 is used
\r
351 * - TIM_Channel_ALL: TIM Channel 1and 2 are used
\r
352 * - Newstate: new state of the TIMx peripheral Preload register
\r
353 * This parameter can be: ENABLE or DISABLE.
\r
356 *******************************************************************************/
\r
357 void TIM_PreloadConfig(TIM_TypeDef *TIMx, u16 TIM_Channel, FunctionalState Newstate)
\r
359 if(Newstate == ENABLE)
\r
361 switch (TIM_Channel)
\r
363 case TIM_Channel_1:
\r
364 TIMx->OMR1 |= TIM_PLD1_Set;
\r
367 case TIM_Channel_2:
\r
368 TIMx->OMR1 |= TIM_PLD2_Set;
\r
371 case TIM_Channel_ALL:
\r
372 TIMx->OMR1 |= TIM_PLD1_Set | TIM_PLD2_Set;
\r
381 switch (TIM_Channel)
\r
383 case TIM_Channel_1:
\r
384 TIMx->OMR1 &= TIM_PLD1_Reset;
\r
387 case TIM_Channel_2:
\r
388 TIMx->OMR1 &= TIM_PLD2_Reset;
\r
391 case TIM_Channel_ALL:
\r
392 TIMx->OMR1 &= TIM_PLD1_Reset & TIM_PLD2_Reset;
\r
401 /*******************************************************************************
\r
402 * Function Name : TIM_DMAConfig
\r
403 * Description : Configures the TIM0
\92s DMA interface.
\r
404 * Input : - TIM_DMASources: specifies the DMA Request sources.
\r
405 * This parameter can be any combination of the following values:
\r
406 * - TIM_DMASource_OC1: Output Compare 1 DMA source
\r
407 * - TIM_DMASource_OC2: Output Compare 2 DMA source
\r
408 * - TIM_DMASource_IC1: Input Capture 1 DMA source
\r
409 * - TIM_DMASource_IC2: Input Capture 2 DMA source
\r
410 * - TIM_DMASource_Update: Timer Update DMA source
\r
411 * - TIM_OCRMState: the state of output compare request mode.
\r
412 * This parameter can be one of the following values:
\r
413 * - TIM_OCRMState_Enable
\r
414 * - TIM_OCRMState_Disable
\r
415 * - TIM_DMABase:DMA Base address.
\r
416 * This parameter can be one of the following values:
\r
417 * TIM_DMABase_CR, TIM_DMABase_SCR, TIM_DMABase_IMCR,
\r
418 * TIM_DMABase_OMR1, TIM_DMABase_RSR,
\r
419 * TIM_DMABase_RER, TIM_DMABase_ISR, TIM_DMABase_CNT,
\r
420 * TIM_DMABase_PSC, TIM_DMABase_ARR, TIM_DMABase_OCR1,
\r
421 * TIM_DMABase_OCR2, TIM_DMABase_ICR1, TIM_DMABase_ICR2
\r
424 *******************************************************************************/
\r
425 void TIM_DMAConfig(u16 TIM_DMASources, u16 TIM_OCRMState, u16 TIM_DMABase)
\r
427 /* Select the DMA requests */
\r
428 TIM0->RSR &= TIM_DMASources;
\r
430 /* Set the OCRM state */
\r
431 if(TIM_OCRMState == TIM_OCRMState_Enable)
\r
433 TIM0->RSR |= TIM_OCRM_Set;
\r
437 TIM0->RSR &= TIM_OCRM_Reset;
\r
440 /* Set the DMA Base address */
\r
441 TIM0->CR &= TIM_DBASE_Mask;
\r
442 TIM0->CR |= TIM_DMABase;
\r
445 /*******************************************************************************
\r
446 * Function Name : TIM_DMACmd
\r
447 * Description : Enables or disables the TIM0
\92s DMA interface.
\r
448 * Input : - TIM_DMASources: specifies the DMA Request sources.
\r
449 * This parameter can be any combination of the following values:
\r
450 * - TIM_DMASource_OC1: Output Compare 1 DMA source
\r
451 * - TIM_DMASource_OC2: Output Compare 2 DMA source
\r
452 * - TIM_DMASource_IC1: Input Capture 1 DMA source
\r
453 * - TIM_DMASource_IC2: Input Capture 2 DMA source
\r
454 * - TIM_DMASource_Update: Timer Update DMA source
\r
455 * - Newstate: new state of the DMA Request sources.
\r
456 * This parameter can be: ENABLE or DISABLE.
\r
459 *******************************************************************************/
\r
460 void TIM_DMACmd(u16 TIM_DMASources, FunctionalState Newstate)
\r
462 if(Newstate == ENABLE)
\r
464 TIM0->RER |= TIM_DMASources;
\r
468 TIM0->RER &= ~TIM_DMASources;
\r
472 /*******************************************************************************
\r
473 * Function Name : TIM_ClockSourceConfig
\r
474 * Description : Configures the TIM clock source.
\r
475 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
476 * - TIM_ClockSource: specifies the TIM clock source to be
\r
478 * This parameter can be one of the following values:
\r
479 * - TIM_ClockSource_Internal: CK_TIM internal clock
\r
480 * - TIM_ClockSource_TI11: External input pin TI1
\r
481 * connected to IC1 channel.
\r
482 * - TIM_ClockSource_TI12: External input pin TI1
\r
483 * connected to IC2 channel.
\r
484 * - TIM_ClockSource_TI22: External input pin TI2
\r
485 * connected to IC2 channel.
\r
486 * - TIM_ClockSource_TI21: External input pin TI2
\r
487 * connected to IC1 channel.
\r
488 * - TIM_ExtCLKEdge: specifies the External input signal edge.
\r
489 * This parameter can be one of the following values:
\r
490 * - TIM_ExtCLKEdge_Falling : Falling edge selected.
\r
491 * - TIM_ExtCLKEdge_Rising : Rising edge selected.
\r
494 *******************************************************************************/
\r
495 void TIM_ClockSourceConfig(TIM_TypeDef *TIMx, u16 TIM_ClockSource,
\r
496 u16 TIM_ExtCLKEdge)
\r
498 if(TIM_ClockSource == TIM_ClockSource_Internal)
\r
500 /* CK_TIM is used as clock source */
\r
501 TIMx->SCR &= TIM_SME_Reset & TIM_SlaveModeSelection_Mask & TIM_TriggerSelection_Mask;
\r
504 /* Input Captures are used as TIM external clock */
\r
506 TIMx->SCR &= TIM_SME_Reset & TIM_SlaveModeSelection_Mask & TIM_TriggerSelection_Mask;
\r
507 TIMx->SCR |= TIM_SMS_EXTCLK_Set | TIM_SME_Set;
\r
509 if((TIM_ClockSource == TIM_ClockSource_TI11) ||
\r
510 (TIM_ClockSource == TIM_ClockSource_TI21))
\r
511 /* Input Capture 1 is selected */
\r
513 /* Input capture Enable */
\r
514 TIMx->IMCR |= TIM_IC1_Enable;
\r
515 TIMx->SCR |= TIM_TS_IC1_Set;
\r
517 if(TIM_ExtCLKEdge == TIM_ExtCLKEdge_Falling)
\r
518 /* Set the corresponding polarity */
\r
520 TIMx->IMCR |= TIM_IC1P_Set;
\r
524 TIMx->IMCR &= TIM_IC1P_Reset;
\r
526 if(TIM_ClockSource == TIM_ClockSource_TI11)
\r
528 /* External signal TI1 connected to IC1 channel */
\r
529 TIMx->IMCR &= TIM_IC1S_Reset;
\r
533 /* External signal TI2 connected to IC1 channel */
\r
534 TIMx->IMCR |= TIM_IC1S_Set;
\r
538 /* Input Capture 2 is selected */
\r
540 /* Input capture Enable */
\r
541 TIMx->IMCR |= TIM_IC2_Enable;
\r
542 TIMx->SCR |= TIM_TS_IC2_Set;
\r
544 if(TIM_ExtCLKEdge == TIM_ExtCLKEdge_Falling)
\r
545 /* Set the corresponding polarity */
\r
547 TIMx->IMCR |= TIM_IC2P_Set;
\r
551 TIMx->IMCR &= TIM_IC2P_Reset;
\r
553 if(TIM_ClockSource == TIM_ClockSource_TI22)
\r
555 /* External signal TI2 connected to IC2 channel */
\r
556 TIMx->IMCR &= TIM_IC2S_Reset;
\r
560 /* External signal TI1 connected to IC2 channel */
\r
561 TIMx->IMCR |= TIM_IC2S_Set;
\r
567 /*******************************************************************************
\r
568 * Function Name : TIM_SetPrescaler
\r
569 * Description : Sets the TIM prescaler value.
\r
570 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
571 * - Prescaler: TIM prescaler new value.
\r
574 *******************************************************************************/
\r
575 void TIM_SetPrescaler(TIM_TypeDef* TIMx, u16 Prescaler)
\r
577 TIMx->PSC = Prescaler;
\r
580 /*******************************************************************************
\r
581 * Function Name : TIM_SetPeriod
\r
582 * Description : Sets the TIM period value.
\r
583 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
584 * - Period: TIM period new value.
\r
587 *******************************************************************************/
\r
588 void TIM_SetPeriod(TIM_TypeDef* TIMx, u16 Period)
\r
590 TIMx->ARR = Period;
\r
593 /*******************************************************************************
\r
594 * Function Name : TIM_SetPulse
\r
595 * Description : Sets the TIM pulse value.
\r
596 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
597 * - TIM_Channel: specifies the TIM channel to be used.
\r
598 * This parameter can be one of the following values:
\r
599 * - TIM_Channel_1: TIM Channel 1 is used
\r
600 * - TIM_Channel_2: TIM Channel 2 is used
\r
601 * - TIM_Channel_ALL: TIM Channel 1and 2 are used
\r
602 * - Pulse: TIM pulse new value.
\r
605 *******************************************************************************/
\r
606 void TIM_SetPulse(TIM_TypeDef* TIMx, u16 TIM_Channel, u16 Pulse)
\r
608 /* Set Channel 1 pulse value */
\r
609 if(TIM_Channel == TIM_Channel_1)
\r
611 TIMx->OCR1 = Pulse;
\r
613 /* Set Channel 2 pulse value */
\r
614 else if(TIM_Channel == TIM_Channel_2)
\r
616 TIMx->OCR2 = Pulse;
\r
618 /* Set Channel 1 and Channel 2 pulse values */
\r
619 else if(TIM_Channel == TIM_Channel_ALL)
\r
621 TIMx->OCR1 = Pulse;
\r
622 TIMx->OCR2 = Pulse;
\r
626 /*******************************************************************************
\r
627 * Function Name : TIM_GetICAP1
\r
628 * Description : Gets the Input Capture 1 value.
\r
629 * Input : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
631 * Return : Input Capture 1 Register value.
\r
632 *******************************************************************************/
\r
633 u16 TIM_GetICAP1(TIM_TypeDef *TIMx)
\r
638 /*******************************************************************************
\r
639 * Function Name : TIM_GetICAP2
\r
640 * Description : Gets the Input Capture 2 value.
\r
641 * Input : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
643 * Return : Input Capture 2 Register value
\r
644 *******************************************************************************/
\r
645 u16 TIM_GetICAP2(TIM_TypeDef *TIMx)
\r
650 /*******************************************************************************
\r
651 * Function Name : TIM_GetPWMIPulse
\r
652 * Description : Gets the PWM Input pulse value.
\r
653 * Input : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
655 * Return : Input Capture 2 Register value
\r
656 *******************************************************************************/
\r
657 u16 TIM_GetPWMIPulse(TIM_TypeDef *TIMx)
\r
662 /*******************************************************************************
\r
663 * Function Name : TIM_GetPWMIPeriod
\r
664 * Description : Gets the PWM Input period value.
\r
665 * Input : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
667 * Return : Input Capture 1 Register value
\r
668 *******************************************************************************/
\r
669 u16 TIM_GetPWMIPeriod(TIM_TypeDef *TIMx)
\r
674 /*******************************************************************************
\r
675 * Function Name : TIM_DebugCmd
\r
676 * Description : Enables or disables the specified TIM peripheral Debug control.
\r
677 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
678 * - Newstate: new state of the TIMx Debug control.
\r
679 This parameter can be: ENABLE or DISABLE.
\r
682 *******************************************************************************/
\r
683 void TIM_DebugCmd(TIM_TypeDef *TIMx, FunctionalState Newstate)
\r
685 if(Newstate == ENABLE)
\r
687 TIMx->CR |= TIM_DBGC_Set;
\r
691 TIMx->CR &= TIM_DBGC_Reset;
\r
695 /*******************************************************************************
\r
696 * Function Name : TIM_CounterModeConfig
\r
697 * Description : Specifies the Counter Mode to be used.
\r
698 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
699 * - TIM_CounterMode: specifies the Counter Mode to be used
\r
700 * This parameter can be one of the following values:
\r
701 * - TIM_CounterMode_Up: TIM Up Counting Mode
\r
702 * - TIM_CounterMode_Down: TIM Down Counting Mode
\r
703 * - TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1
\r
704 * - TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2
\r
705 * - TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3
\r
708 *******************************************************************************/
\r
709 void TIM_CounterModeConfig(TIM_TypeDef* TIMx, u16 TIM_CounterMode)
\r
711 /* Counter mode configuration */
\r
712 TIMx->CR &= TIM_CounterMode_Mask;
\r
713 TIMx->CR |= TIM_CounterMode;
\r
716 /*******************************************************************************
\r
717 * Function Name : TIM_ForcedOCConfig
\r
718 * Description : Forces the TIM output waveform to active or inactive level.
\r
719 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
720 * - TIM_Channel: specifies the TIM channel to be used.
\r
721 * This parameter can be one of the following values:
\r
722 * - TIM_Channel_1: Timer Channel 1 is used
\r
723 * - TIM_Channel_2: Timer Channel 2 is used
\r
724 * - TIM_Channel_ALL: Timer Channel 1 and 2 are used
\r
725 * - TIM_ForcedAction: specifies the forced Action to be set to
\r
726 * the output waveform.
\r
727 * This parameter can be one of the following values:
\r
728 * - TIM_ForcedAction_Active: Force active level on OCxREF
\r
729 * - TIM_ForcedAction_InActive: Force inactive level on
\r
733 *******************************************************************************/
\r
734 void TIM_ForcedOCConfig(TIM_TypeDef* TIMx, u16 TIM_Channel,u16 TIM_ForcedAction)
\r
736 /* Channel 1 Forced Output Compare mode configuration */
\r
737 if(TIM_Channel == TIM_Channel_1)
\r
739 TIMx->OMR1 &= TIM_OC1C_Mask;
\r
740 TIMx->OMR1 |= TIM_ForcedAction;
\r
742 /* Channel 2 Forced Output Compare mode configuration */
\r
745 if(TIM_Channel == TIM_Channel_2)
\r
747 TIMx->OMR1 &= TIM_OC2C_Mask;
\r
748 TIMx->OMR1 |= (TIM_ForcedAction<<8);
\r
750 /* Channel 1 and Channel 2 Forced Output Compare mode configuration */
\r
753 TIMx->OMR1 &= TIM_OC1C_Mask & TIM_OC2C_Mask;
\r
754 TIMx->OMR1 |= TIM_ForcedAction |(TIM_ForcedAction<<8);
\r
759 /*******************************************************************************
\r
760 * Function Name : TIM_ResetCounter
\r
761 * Description : Re-intializes the TIM counter and generates an update of the
\r
763 * Input : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
766 *******************************************************************************/
\r
767 void TIM_ResetCounter(TIM_TypeDef* TIMx)
\r
769 /* Re-intialize the TIM counter */
\r
770 TIMx->CR |= TIM_COUNTER_Reset;
\r
773 /*******************************************************************************
\r
774 * Function Name : TIM_SynchroConfig
\r
775 * Description : Synchronizes two Timers in a specified mode.
\r
776 * Input : - Master: specifies the peripheral master.
\r
777 * This parameter can be one of the following values:
\r
778 * PWM_Master, TIM0_Master, TIM1_Master or TIM2_Master.
\r
779 * - Slave: specifies the peripheral slave.
\r
780 * This parameter can be one of the following values:
\r
781 * PWM_Slave, TIM0_Slave, TIM1_Slave or TIM2_Slave.
\r
782 * - TIM_SynchroAction: specifies the synchronization Action to
\r
784 * This parameter can be one of the following values:
\r
785 * - TIM_SynchroAction_Enable: The CNT_EN bit is used as TRGO
\r
786 * - TIM_SynchroAction_Update: The Update event is used as TRGO
\r
787 * - TIM_SynchroAction_Reset: The CNT_RST bit is used as TRGO
\r
788 * - TIM_SynchroAction_OC: The OC1 signal is used as TRGO
\r
789 * - TIM_SynchroMode: specifies the synchronization Mode to be used.
\r
790 * This parameter can be one of the following values:
\r
791 * - TIM_SynchroMode_Gated: Both start and stop of the
\r
792 * counter is controlled.
\r
793 * - TIM_SynchroMode_Trigger: Only the start of the
\r
794 * counter is controlled.
\r
795 * - TIM_SynchroMode_External: The rising edge of selected trigger
\r
796 * clocks the counter.
\r
797 * - TIM_SynchroMode_Reset: The rising edge of the selected trigger
\r
798 * signal resets the counter and generates an update of the registers.
\r
801 *******************************************************************************/
\r
802 void TIM_SynchroConfig(Master_TypeDef Master, Slave_TypeDef Slave,
\r
803 u16 TIM_SynchroAction, u16 TIM_SynchroMode)
\r
809 PWM->SCR &= TIM_SME_Reset & TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask &
\r
810 TIM_InternalTriggerSelection_Mask;
\r
811 PWM->SCR |= TIM_SynchroMode | TIM_SME_Set;
\r
813 if(Master == TIM1_Master)
\r
815 /* Set the internal trigger */
\r
816 PWM->SCR |= TIM_ITS_TIM1;
\r
818 /* Set the synchronization action */
\r
819 TIM1->CR &= TIM_MasterModeSelection_Mask;
\r
820 TIM1->CR |= TIM_SynchroAction;
\r
823 else if(Master == TIM0_Master)
\r
825 /* Set the internal trigger */
\r
826 PWM->SCR |= TIM_ITS_TIM0;
\r
828 /* Set the synchronization action */
\r
829 TIM0->CR &= TIM_MasterModeSelection_Mask;
\r
830 TIM0->CR |= TIM_SynchroAction;
\r
833 else if(Master == TIM2_Master)
\r
835 /* Set the internal trigger */
\r
836 PWM->SCR |= TIM_ITS_TIM2;
\r
838 /* Set the synchronization action */
\r
839 TIM2->CR &= TIM_MasterModeSelection_Mask;
\r
840 TIM2->CR |= TIM_SynchroAction;
\r
847 TIM0->SCR &= TIM_SME_Reset & TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask &
\r
848 TIM_InternalTriggerSelection_Mask;
\r
849 TIM0->SCR |= TIM_SynchroMode | TIM_SME_Set;
\r
851 if(Master == PWM_Master)
\r
853 /* Set the internal trigger */
\r
854 TIM0->SCR |= TIM_ITS_PWM;
\r
856 /* Set the synchronization action */
\r
857 PWM->CR &= TIM_MasterModeSelection_Mask;
\r
858 PWM->CR |= TIM_SynchroAction;
\r
861 else if(Master == TIM1_Master)
\r
863 /* Set the internal trigger */
\r
864 TIM0->SCR |= TIM_ITS_TIM1;
\r
866 /* Set the synchronization action */
\r
867 TIM1->CR &= TIM_MasterModeSelection_Mask;
\r
868 TIM1->CR |= TIM_SynchroAction;
\r
871 else if(Master == TIM2_Master)
\r
873 /* Set the internal trigger */
\r
874 TIM0->SCR |= TIM_ITS_TIM2;
\r
876 /* Set the synchronization action */
\r
877 TIM2->CR &= TIM_MasterModeSelection_Mask;
\r
878 TIM2->CR |= TIM_SynchroAction;
\r
886 TIM1->SCR &= TIM_SME_Reset & TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask &
\r
887 TIM_InternalTriggerSelection_Mask;
\r
888 TIM1->SCR |= TIM_SynchroMode | TIM_SME_Set;
\r
890 if(Master == PWM_Master)
\r
892 /* Set the internal trigger */
\r
893 TIM1->SCR |= TIM_ITS_PWM;
\r
895 /* Set the synchronization action */
\r
896 PWM->CR &= TIM_MasterModeSelection_Mask;
\r
897 PWM->CR |= TIM_SynchroAction;
\r
899 else if(Master == TIM0_Master)
\r
901 /* Set the internal trigger */
\r
902 TIM1->SCR |= TIM_ITS_TIM0;
\r
904 /* Set the synchronization action */
\r
905 TIM0->CR &= TIM_MasterModeSelection_Mask;
\r
906 TIM0->CR |= TIM_SynchroAction;
\r
909 else if(Master == TIM2_Master)
\r
911 /* Set the internal trigger */
\r
912 TIM1->SCR |= TIM_ITS_TIM2;
\r
914 /* Set the synchronization action */
\r
915 TIM2->CR &= TIM_MasterModeSelection_Mask;
\r
916 TIM2->CR |= TIM_SynchroAction;
\r
924 TIM2->SCR &= TIM_SME_Reset & TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask &
\r
925 TIM_InternalTriggerSelection_Mask;
\r
926 TIM2->SCR |= TIM_SynchroMode | TIM_SME_Set;
\r
928 if(Master == PWM_Master)
\r
930 /* Internal trigger selection */
\r
931 TIM2->SCR |= TIM_ITS_PWM;
\r
933 /* Set the synchronization action */
\r
934 PWM->CR &= TIM_MasterModeSelection_Mask;
\r
935 PWM->CR |= TIM_SynchroAction;
\r
938 else if(Master == TIM1_Master)
\r
940 /* Internal trigger selection */
\r
941 TIM2->SCR |= TIM_ITS_TIM1;
\r
943 /* Set the synchronization action */
\r
944 TIM1->CR &= TIM_MasterModeSelection_Mask;
\r
945 TIM1->CR |= TIM_SynchroAction;
\r
948 else if(Master == TIM0_Master)
\r
950 /* Internal trigger selection */
\r
951 TIM2->SCR |= TIM_ITS_TIM0;
\r
953 /* Set the synchronization action */
\r
954 TIM0->CR &= TIM_MasterModeSelection_Mask;
\r
955 TIM0->CR |= TIM_SynchroAction;
\r
965 /*******************************************************************************
\r
966 * Function Name : TIM_GetFlagStatus
\r
967 * Description : Checks whether the specified TIM flag is set or not.
\r
968 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
969 * - TIM_FLAG: specifies the flag to check.
\r
970 * This parameter can be one of the following values:
\r
971 * - TIM_FLAG_IC1: Input Capture 1 Flag
\r
972 * - TIM_FLAG_OC1: Output Compare 1 Flag
\r
973 * - TIM_FLAG_Update: Timer update Flag
\r
974 * - TIM_FLAG_IC2: Input Capture 2 Flag
\r
975 * - TIM_FLAG_OC2: Output Compare 2 Flag
\r
977 * Return : The new state of TIM_FLAG (SET or RESET).
\r
978 *******************************************************************************/
\r
979 FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, u16 TIM_FLAG)
\r
981 if((TIMx->ISR & TIM_FLAG) != RESET )
\r
991 /*******************************************************************************
\r
992 * Function Name : TIM_ClearFlag
\r
993 * Description : Clears the TIMx's pending flags.
\r
994 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
995 * - TIM_FLAG: specifies the flag bit to clear.
\r
996 * This parameter can be any combination of the following values:
\r
997 * - TIM_FLAG_IC1: Timer Input Capture 1 flag
\r
998 * - TIM_FLAG_OC1: Timer Output Compare 1 flag
\r
999 * - TIM_FLAG_Update: Timer update flag
\r
1000 * - TIM_FLAG_IC2: Timer Input Capture 2 flag
\r
1001 * - TIM_FLAG_OC2: Timer Output Compare 2 flag
\r
1004 *******************************************************************************/
\r
1005 void TIM_ClearFlag(TIM_TypeDef* TIMx, u16 TIM_FLAG)
\r
1007 /* Clear the flags */
\r
1008 TIMx->ISR &= ~TIM_FLAG;
\r
1011 /*******************************************************************************
\r
1012 * Function Name : TIM_GetITStatus
\r
1013 * Description : Checks whether the specified TIM interrupt has occurred or not.
\r
1014 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
1015 * - TIM_IT: specifies the TIM interrupt source to check.
\r
1016 * This parameter can be one of the following values:
\r
1017 * - TIM_IT_IC1: Input Capture 1 interrupt
\r
1018 * - TIM_IT_OC1: Output Compare 1 interrupt
\r
1019 * - TIM_IT_Update: Timer update interrupt
\r
1020 * - TIM_IT_GlobalUpdate: Timer global update interrupt
\r
1021 * - TIM_IT_IC2: Input Capture 2 interrupt
\r
1022 * - TIM_IT_OC2: Output Compare 2 interrupt
\r
1024 * Return : The new state of TIM_IT(SET or RESET).
\r
1025 *******************************************************************************/
\r
1026 ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, u16 TIM_IT)
\r
1028 u16 TIM_IT_Check = 0;
\r
1030 /* Calculates the pending bits to be checked */
\r
1031 TIM_IT_Check = TIM_IT & TIM_IT_Clear_Mask;
\r
1033 if((TIMx->ISR & TIM_IT_Check) != RESET )
\r
1043 /*******************************************************************************
\r
1044 * Function Name : TIM_ClearITPendingBit
\r
1045 * Description : Clears the TIM's interrupt pending bits.
\r
1046 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
\r
1047 * - TIM_IT: specifies the interrupt pending bit to clear.
\r
1048 * This parameter can be one of the following values:
\r
1049 * - TIM_IT_IC1: Input Capture 1 Interrupt
\r
1050 * - TIM_IT_OC1: Output Compare 1 Interrupt
\r
1051 * - TIM_IT_Update: Timer update Interrupt
\r
1052 * - TIM_IT_GlobalUpdate: Timer global update Interrupt
\r
1053 * - TIM_IT_IC2: Input Capture 2 Interrupt
\r
1054 * - TIM_IT_OC2: Output Compare 2 Interrupt
\r
1057 *******************************************************************************/
\r
1058 void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, u16 TIM_IT)
\r
1060 u16 TIM_IT_Clear = 0;
\r
1062 /* Calculate the pending bits to be cleared */
\r
1063 TIM_IT_Clear = TIM_IT & TIM_IT_Clear_Mask;
\r
1065 /* Clear the pending bits */
\r
1066 TIMx->ISR &= ~TIM_IT_Clear;
\r
1069 /*******************************************************************************
\r
1070 * Function Name : OCM_ModuleConfig
\r
1071 * Description : Output Compare Module configuration
\r
1072 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
1073 * - TIM_InitStruct: pointer to a TIM_InitTypeDef structure that
\r
1074 * contains the configuration information for the specified TIM
\r
1078 *******************************************************************************/
\r
1079 static void OCM_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)
\r
1081 u16 TIM_OCControl = 0x0000;
\r
1083 if(TIM_InitStruct->TIM_Mode == TIM_Mode_OCTiming)
\r
1085 TIM_OCControl = TIM_OCControl_OCTiming;
\r
1089 if((TIM_InitStruct->TIM_Mode == TIM_Mode_OCActive) ||
\r
1090 (TIM_InitStruct->TIM_Mode == TIM_Mode_OPM_Active))
\r
1092 TIM_OCControl = TIM_OCControl_OCActive;
\r
1096 if(TIM_InitStruct->TIM_Mode == TIM_Mode_OCInactive)
\r
1098 TIM_OCControl = TIM_OCControl_OCInactive;
\r
1102 if((TIM_InitStruct->TIM_Mode == TIM_Mode_OCToggle) ||
\r
1103 (TIM_InitStruct->TIM_Mode == TIM_Mode_OPM_Toggle))
\r
1105 TIM_OCControl = TIM_OCControl_OCToggle;
\r
1109 TIM_OCControl = TIM_OCControl_PWM;
\r
1116 if(TIM_InitStruct->TIM_Channel == TIM_Channel_1)
\r
1118 /* Configure Channel 1 on Output Compare mode */
\r
1119 TIMx->OMR1 &= TIM_OC1C_Mask;
\r
1120 TIMx->OMR1 |= TIM_OCControl|TIM_OC1_Enable;
\r
1121 TIMx->OMR1 |= TIM_PLD1_Set;
\r
1122 TIMx->OCR1 = TIM_InitStruct->TIM_Pulse1;
\r
1124 /* Set the OC1 wave polarity */
\r
1125 if(TIM_InitStruct->TIM_Polarity1 == TIM_Polarity1_Low)
\r
1127 TIMx->OMR1 |= TIM_OC1P_Set;
\r
1131 TIMx->OMR1 &= TIM_OC1P_Reset;
\r
1136 if(TIM_InitStruct->TIM_Channel == TIM_Channel_2)
\r
1138 /* Configure Channel 2 on Output Compare mode */
\r
1139 TIMx->OMR1 &= TIM_OC2C_Mask;
\r
1140 TIMx->OMR1 |= TIM_OCControl<<8|TIM_OC2_Enable;
\r
1141 TIMx->OMR1 |= TIM_PLD2_Set;
\r
1142 TIMx->OCR2 = TIM_InitStruct->TIM_Pulse2;
\r
1144 /* Set the OCB wave polarity */
\r
1145 if(TIM_InitStruct->TIM_Polarity2 == TIM_Polarity2_Low)
\r
1147 TIMx->OMR1 |= TIM_OC2P_Set;
\r
1151 TIMx->OMR1 &= TIM_OC2P_Reset;
\r
1154 /* Configure Channel 1 and Channel 2 on Output Compare mode */
\r
1157 TIMx->OMR1 &= TIM_OC1C_Mask & TIM_OC2C_Mask;
\r
1158 TIMx->OMR1 |= TIM_OCControl|(TIM_OCControl<<8)|TIM_OC1_Enable|TIM_OC2_Enable|
\r
1159 TIM_PLD1_Set|TIM_PLD2_Set;
\r
1161 TIMx->OCR1 = TIM_InitStruct->TIM_Pulse1;
\r
1162 TIMx->OCR2 = TIM_InitStruct->TIM_Pulse2;
\r
1164 /* Set the OC1 wave polarity */
\r
1165 if(TIM_InitStruct->TIM_Polarity1 == TIM_Polarity1_Low)
\r
1167 TIMx->OMR1 |= TIM_OC1P_Set;
\r
1171 TIMx->OMR1 &= TIM_OC1P_Reset;
\r
1174 /* Set the OC2 wave polarity */
\r
1175 if(TIM_InitStruct->TIM_Polarity2 == TIM_Polarity2_Low)
\r
1177 TIMx->OMR1 |= TIM_OC2P_Set;
\r
1181 TIMx->OMR1 &= TIM_OC2P_Reset;
\r
1187 /*******************************************************************************
\r
1188 * Function Name : ICAP_ModuleConfig
\r
1189 * Description : Input Capture Module configuration
\r
1190 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
1191 * - TIM_InitStruct: pointer to a TIM_InitTypeDef structure that
\r
1192 * contains the configuration information for the specified TIM
\r
1196 *******************************************************************************/
\r
1197 static void ICAP_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)
\r
1199 if(TIM_InitStruct->TIM_Mode == TIM_Mode_PWMI)
\r
1200 { /* PWM input mode configuration */
\r
1201 TIMx->SCR |= TIM_TS_IC1_Set|TIM_SMS_RESETCLK_Set|TIM_SME_Set;
\r
1203 /* Channel 1 and channel 2 input selection */
\r
1204 if(TIM_InitStruct->TIM_PWMI_ICSelection == TIM_PWMI_ICSelection_TI1)
\r
1206 TIMx->IMCR &= TIM_IC1S_Reset;
\r
1207 TIMx->IMCR |= TIM_IC2S_Set;
\r
1211 TIMx->IMCR |= TIM_IC1S_Set;
\r
1212 TIMx->IMCR &= TIM_IC2S_Reset;
\r
1215 /* Channel polarity */
\r
1216 if(TIM_InitStruct->TIM_PWMI_ICPolarity == TIM_PWMI_ICPolarity_Rising)
\r
1218 TIMx->IMCR &= TIM_IC1P_Reset;
\r
1219 TIMx->IMCR |= TIM_IC2P_Set;
\r
1223 TIMx->IMCR |= TIM_IC1P_Set;
\r
1224 TIMx->IMCR &= TIM_IC2P_Reset;
\r
1227 /* Input capture Enable */
\r
1228 TIMx->IMCR |= TIM_IC1_Enable |TIM_IC2_Enable;
\r
1232 if(TIM_InitStruct->TIM_Channel == TIM_Channel_1)
\r
1234 /* Input Capture 1 mode configuration */
\r
1235 TIMx->SCR &= TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask;
\r
1236 TIMx->SCR |= TIM_TS_IC1_Set|TIM_SMS_RESETCLK_Set|TIM_SME_Set;
\r
1238 /* Channel 1 input selection */
\r
1239 if(TIM_InitStruct->TIM_IC1Selection == TIM_IC1Selection_TI1)
\r
1241 TIMx->IMCR &= TIM_IC1S_Reset;
\r
1245 TIMx->IMCR |= TIM_IC1S_Set;
\r
1247 /* Channel 1 polarity */
\r
1248 if(TIM_InitStruct->TIM_IC1Polarity == TIM_IC1Polarity_Rising)
\r
1250 TIMx->IMCR &= TIM_IC1P_Reset;
\r
1254 TIMx->IMCR |= TIM_IC1P_Set;
\r
1257 /* Input capture Enable */
\r
1258 TIMx->IMCR |= TIM_IC1_Enable;
\r
1262 /* Input Capture 2 mode configuration */
\r
1263 TIMx->SCR &= (TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask);
\r
1264 TIMx->SCR |= TIM_TS_IC2_Set|TIM_SMS_RESETCLK_Set|TIM_SME_Set;
\r
1266 /* Channel 2 input selection */
\r
1267 if(TIM_InitStruct->TIM_IC2Selection == TIM_IC2Selection_TI2)
\r
1269 TIMx->IMCR &= TIM_IC2S_Reset;
\r
1273 TIMx->IMCR |= TIM_IC2S_Set;
\r
1276 /* Channel 2 polarity */
\r
1277 if(TIM_InitStruct->TIM_IC2Polarity == TIM_IC2Polarity_Rising)
\r
1279 TIMx->IMCR &= TIM_IC2P_Reset;
\r
1283 TIMx->IMCR |= TIM_IC2P_Set;
\r
1286 /* Input capture Enable */
\r
1287 TIMx->IMCR |= TIM_IC2_Enable;
\r
1292 /*******************************************************************************
\r
1293 * Function Name : Encoder_ModeConfig
\r
1294 * Description : Encoder Mode configuration
\r
1295 * Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
\r
1296 * - TIM_InitStruct: pointer to a TIM_InitTypeDef structure that
\r
1297 * contains the configuration information for the specified TIM
\r
1301 *******************************************************************************/
\r
1302 static void Encoder_ModeConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)
\r
1304 /* Set Encoder mode */
\r
1305 TIMx->SCR &= TIM_Encoder_Mask;
\r
1307 if(TIM_InitStruct->TIM_Mode == TIM_Mode_Encoder1)
\r
1309 TIMx->SCR |= TIM_Encoder1_Set;
\r
1311 else if (TIM_InitStruct->TIM_Mode == TIM_Mode_Encoder2)
\r
1313 TIMx->SCR |= TIM_Encoder2_Set;
\r
1317 TIMx->SCR |= TIM_Encoder3_Set;
\r
1320 /* Channel 1 input selection */
\r
1321 if(TIM_InitStruct->TIM_IC1Selection == TIM_IC1Selection_TI2)
\r
1323 TIMx->IMCR |= TIM_IC1S_Set;
\r
1327 TIMx->IMCR &= TIM_IC1S_Reset;
\r
1330 /* Channel 2 input selection */
\r
1331 if(TIM_InitStruct->TIM_IC2Selection == TIM_IC2Selection_TI1)
\r
1333 TIMx->IMCR |= TIM_IC2S_Set;
\r
1337 TIMx->IMCR &= TIM_IC2S_Reset;
\r
1340 /* Channel 1 polarity */
\r
1341 if(TIM_InitStruct->TIM_IC1Polarity == TIM_IC1Polarity_Falling)
\r
1343 TIMx->IMCR |= TIM_IC1P_Set;
\r
1347 TIMx->IMCR &= TIM_IC1P_Reset;
\r
1350 /* Channel 2 polarity */
\r
1351 if(TIM_InitStruct->TIM_IC2Polarity == TIM_IC2Polarity_Falling)
\r
1353 TIMx->IMCR |= TIM_IC2P_Set;
\r
1357 TIMx->IMCR &= TIM_IC2P_Reset;
\r
1360 /******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
\r