]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_STR75x_IAR/STLibrary/src/75x_tim.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / ARM7_STR75x_IAR / STLibrary / src / 75x_tim.c
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
7 * History:\r
8 * 07/17/2006 : V1.0\r
9 * 03/10/2006 : V0.1\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
18 \r
19 /* Includes ------------------------------------------------------------------*/\r
20 #include "75x_tim.h" \r
21 #include "75x_mrcc.h"\r
22 \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
30 \r
31 /* TIM Input Capture Selection Set/Reset */\r
32 #define TIM_IC1S_Set    0x0001\r
33 #define TIM_IC1S_Reset  0x003E\r
34 \r
35 /* TIM Input Capture Selection Set/Reset */\r
36 #define TIM_IC2S_Set    0x0002\r
37 #define TIM_IC2S_Reset  0x003D\r
38 \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
44 \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
49 \r
50 /* TIM Slave Mode Enable Set/Reset value */\r
51 #define TIM_SME_Reset  0x731B\r
52 #define TIM_SME_Set    0x0004\r
53 \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
59 \r
60 /* TIM Trigger Selection value */\r
61 #define TIM_TS_IC1_Set  0x0200\r
62 #define TIM_TS_IC2_Set  0x0300\r
63 \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
67 \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
72 \r
73 /* TIM Update flag selection Set/Reset value */\r
74 #define TIM_UFS_Reset  0xFFFE\r
75 #define TIM_UFS_Set    0x0001\r
76 \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
81 \r
82 /* TIM One pulse Mode set value */\r
83 #define TIM_OPM_Set    0x0008\r
84 #define TIM_OPM_Reset  0xFFF7\r
85 \r
86 /* TIM Debug Mode Set/Reset value */\r
87 #define TIM_DBGC_Set    0x0400\r
88 #define TIM_DBGC_Reset  0xFB7F\r
89 \r
90 /* TIM Input Capture Enable/Disable value */\r
91 #define TIM_IC1_Enable  0x0004\r
92 #define TIM_IC2_Enable  0x0010\r
93 \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
99 \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
105 \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
112 \r
113 /* TIM Output Compare mode Enable value */\r
114 #define TIM_OC1_Enable  0x0010\r
115 #define TIM_OC2_Enable  0x1000\r
116 \r
117 /* TIM Output Compare mode Mask value */\r
118 #define TIM_OC1C_Mask  0x3F31\r
119 #define TIM_OC2C_Mask  0x313F\r
120 \r
121 /* TIM Preload bit Set/Reset value */\r
122 #define TIM_PLD1_Set    0x0001\r
123 #define TIM_PLD1_Reset  0xFFFE\r
124 \r
125 #define TIM_PLD2_Set    0x0100\r
126 #define TIM_PLD2_Reset  0xFEFF\r
127 \r
128 /* TIM OCRM Set/Reset value */\r
129 #define TIM_OCRM_Set    0x0080\r
130 #define TIM_OCRM_Reset  0x030D\r
131 \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
138 \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
143 \r
144 /* Private functions ---------------------------------------------------------*/\r
145 \r
146 /******************************************************************************\r
147 * Function Name  : TIM_DeInit\r
148 * Description    : Deinitializes TIM peripheral registers to their default reset\r
149 *                  values.\r
150 * Input          : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.\r
151 * Output         : None\r
152 * Return         : None\r
153 *******************************************************************************/\r
154 void TIM_DeInit(TIM_TypeDef *TIMx)\r
155\r
156   if(TIMx == TIM0)\r
157   {\r
158     MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM0,ENABLE);\r
159     MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM0,DISABLE);\r
160   }\r
161   else if(TIMx == TIM1)\r
162   {\r
163     MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM1,ENABLE);\r
164     MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM1,DISABLE);\r
165   }\r
166   else if(TIMx == TIM2)\r
167   {\r
168     MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM2,ENABLE);\r
169     MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM2,DISABLE);\r
170   }\r
171 }\r
172 \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
180 *                    peripheral.\r
181 * Output         : None\r
182 * Return         : None\r
183 *******************************************************************************/\r
184 void TIM_Init(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)\r
185 {\r
186   /* Set the prescaler value */\r
187   TIMx->PSC = TIM_InitStruct->TIM_Prescaler;\r
188 \r
189   /* Select the clock source */\r
190   TIM_ClockSourceConfig(TIMx, TIM_InitStruct->TIM_ClockSource,\r
191                            TIM_InitStruct->TIM_ExtCLKEdge);\r
192 \r
193   /* Select the counter mode */\r
194   TIMx->CR &= TIM_CounterMode_Mask;\r
195   TIMx->CR |= TIM_InitStruct->TIM_CounterMode;\r
196 \r
197   /* Set the period value */\r
198   TIMx->ARR = TIM_InitStruct->TIM_Period;\r
199 \r
200   switch(TIM_InitStruct->TIM_Mode)\r
201   {\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
205     break;\r
206 \r
207     case TIM_Mode_PWMI: case TIM_Mode_IC:\r
208       ICAP_ModuleConfig(TIMx, TIM_InitStruct);\r
209     break;\r
210 \r
211     case TIM_Mode_Encoder1: case TIM_Mode_Encoder2: case TIM_Mode_Encoder3:\r
212       Encoder_ModeConfig(TIMx, TIM_InitStruct);\r
213     break;\r
214 \r
215     case TIM_Mode_OPM_PWM: case TIM_Mode_OPM_Toggle: case TIM_Mode_OPM_Active:\r
216 \r
217       /* Output module configuration */\r
218       OCM_ModuleConfig(TIMx, TIM_InitStruct);\r
219 \r
220       /* Input module configuration */\r
221       ICAP_ModuleConfig(TIMx, TIM_InitStruct);\r
222       \r
223       /* Set the slave mode to trigger Mode */\r
224       TIMx->SCR |= TIM_SynchroMode_Trigger;\r
225 \r
226       /* Repetitive pulse state selection */\r
227       if(TIM_InitStruct->TIM_RepetitivePulse == TIM_RepetitivePulse_Disable)\r
228       {\r
229         TIMx->CR |= TIM_OPM_Set;\r
230       }\r
231       else\r
232       {\r
233         TIMx->CR &= TIM_OPM_Reset;\r
234       }\r
235     break;\r
236 \r
237     default:\r
238     break;\r
239   }\r
240 }\r
241 \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
247 * Output         : None                        \r
248 * Return         : None.\r
249 *******************************************************************************/\r
250 void TIM_StructInit(TIM_InitTypeDef *TIM_InitStruct)\r
251 {\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
271 }\r
272 \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
279 * Output         : None\r
280 * Return         : None\r
281 *******************************************************************************/\r
282 void TIM_Cmd(TIM_TypeDef *TIMx, FunctionalState Newstate)\r
283 {\r
284  if(Newstate == ENABLE)\r
285   { \r
286     TIMx->CR |= TIM_COUNTER_Start;\r
287   }\r
288   else\r
289   {\r
290     TIMx->CR &= TIM_COUNTER_Stop;\r
291   }\r
292 }\r
293 \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
299 *                    or disabled.\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
309 * Output         : None\r
310 * Return         : None\r
311 *******************************************************************************/\r
312 void TIM_ITConfig(TIM_TypeDef *TIMx, u16 TIM_IT, FunctionalState Newstate)\r
313\r
314   u16 TIM_IT_Enable = 0;\r
315 \r
316   TIM_IT_Enable = TIM_IT & TIM_IT_Enable_Mask;\r
317 \r
318   if(Newstate == ENABLE)\r
319   {\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
323     {\r
324       TIMx->CR &= TIM_UFS_Reset;\r
325     }\r
326     /* Update interrupt source: counter overflow/underflow */\r
327     else if((TIM_IT & TIM_IT_Update) == TIM_IT_Update)\r
328     {\r
329       TIMx->CR |= TIM_UFS_Set;\r
330     }\r
331     /* Select and enable the interrupts requests */\r
332     TIMx->RSR |= TIM_IT_Enable;\r
333     TIMx->RER |= TIM_IT_Enable;\r
334   }\r
335   /* Disable the interrupts requests */\r
336   else\r
337   {\r
338     TIMx->RSR &= ~TIM_IT_Enable;\r
339     TIMx->RER &= ~TIM_IT_Enable;\r
340   }\r
341 }\r
342 \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
354 * Output         : None\r
355 * Return         : None\r
356 *******************************************************************************/\r
357 void TIM_PreloadConfig(TIM_TypeDef *TIMx, u16 TIM_Channel, FunctionalState Newstate)\r
358 {\r
359   if(Newstate == ENABLE)\r
360   {\r
361     switch (TIM_Channel)\r
362     {\r
363       case TIM_Channel_1:\r
364       TIMx->OMR1 |= TIM_PLD1_Set;\r
365       break;\r
366    \r
367       case TIM_Channel_2:\r
368       TIMx->OMR1 |= TIM_PLD2_Set;\r
369       break;\r
370 \r
371       case TIM_Channel_ALL:\r
372       TIMx->OMR1 |= TIM_PLD1_Set | TIM_PLD2_Set;\r
373       break;\r
374 \r
375       default:\r
376       break;\r
377    }\r
378   }\r
379   else\r
380   {\r
381     switch (TIM_Channel)\r
382     {\r
383       case TIM_Channel_1:\r
384       TIMx->OMR1 &= TIM_PLD1_Reset;\r
385       break;\r
386    \r
387       case TIM_Channel_2:\r
388       TIMx->OMR1 &= TIM_PLD2_Reset;\r
389       break;\r
390 \r
391       case TIM_Channel_ALL:\r
392       TIMx->OMR1 &= TIM_PLD1_Reset & TIM_PLD2_Reset;\r
393       break;\r
394 \r
395       default:\r
396       break;\r
397     }\r
398   }  \r
399 }\r
400 \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
422 * Output         : None\r
423 * Return         : None\r
424 *******************************************************************************/\r
425 void TIM_DMAConfig(u16 TIM_DMASources, u16 TIM_OCRMState, u16 TIM_DMABase)\r
426 {\r
427   /* Select the DMA requests */\r
428   TIM0->RSR &= TIM_DMASources;\r
429 \r
430   /* Set the OCRM state */\r
431   if(TIM_OCRMState == TIM_OCRMState_Enable)\r
432   {\r
433     TIM0->RSR |= TIM_OCRM_Set;\r
434   }\r
435   else\r
436   {\r
437     TIM0->RSR &= TIM_OCRM_Reset;\r
438   }\r
439 \r
440   /* Set the DMA Base address */\r
441   TIM0->CR &= TIM_DBASE_Mask;\r
442   TIM0->CR |= TIM_DMABase;\r
443 }\r
444 \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
457 * Output         : None\r
458 * Return         : None\r
459 *******************************************************************************/\r
460 void TIM_DMACmd(u16 TIM_DMASources, FunctionalState Newstate)\r
461 {\r
462   if(Newstate == ENABLE)\r
463   {\r
464     TIM0->RER |= TIM_DMASources;\r
465   }\r
466   else\r
467   {\r
468     TIM0->RER &= ~TIM_DMASources;\r
469   }\r
470 }\r
471 \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
477 *                    selected.\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
492 * Output         : None\r
493 * Return         : None\r
494 *******************************************************************************/\r
495 void TIM_ClockSourceConfig(TIM_TypeDef *TIMx, u16 TIM_ClockSource,\r
496                            u16 TIM_ExtCLKEdge)\r
497 {\r
498   if(TIM_ClockSource == TIM_ClockSource_Internal)\r
499   {\r
500     /* CK_TIM is used as clock source */\r
501     TIMx->SCR &= TIM_SME_Reset & TIM_SlaveModeSelection_Mask & TIM_TriggerSelection_Mask;\r
502   }\r
503   else\r
504   /* Input Captures are used as TIM external clock */\r
505   {\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
508 \r
509     if((TIM_ClockSource == TIM_ClockSource_TI11) ||\r
510       (TIM_ClockSource == TIM_ClockSource_TI21))\r
511     /* Input Capture 1 is selected */\r
512     {\r
513      /* Input capture  Enable */\r
514       TIMx->IMCR |= TIM_IC1_Enable;\r
515       TIMx->SCR |= TIM_TS_IC1_Set;\r
516 \r
517       if(TIM_ExtCLKEdge == TIM_ExtCLKEdge_Falling)\r
518       /* Set the corresponding polarity */\r
519       {\r
520         TIMx->IMCR |= TIM_IC1P_Set;\r
521       }\r
522       else\r
523       {   \r
524         TIMx->IMCR &= TIM_IC1P_Reset;\r
525       }\r
526       if(TIM_ClockSource == TIM_ClockSource_TI11)\r
527       {\r
528         /* External signal TI1 connected to IC1 channel */\r
529         TIMx->IMCR &= TIM_IC1S_Reset;\r
530       }\r
531       else\r
532       {\r
533         /* External signal TI2 connected to IC1 channel */\r
534         TIMx->IMCR |= TIM_IC1S_Set;\r
535       }\r
536     }\r
537     else\r
538     /* Input Capture 2 is selected */\r
539     {\r
540       /* Input capture  Enable */\r
541       TIMx->IMCR |= TIM_IC2_Enable;\r
542       TIMx->SCR |= TIM_TS_IC2_Set;\r
543 \r
544       if(TIM_ExtCLKEdge == TIM_ExtCLKEdge_Falling)\r
545       /* Set the corresponding polarity */\r
546       {\r
547         TIMx->IMCR |= TIM_IC2P_Set;\r
548       }\r
549       else\r
550       {\r
551          TIMx->IMCR &= TIM_IC2P_Reset;\r
552       }\r
553       if(TIM_ClockSource == TIM_ClockSource_TI22)\r
554       {\r
555         /* External signal TI2 connected to IC2 channel */\r
556         TIMx->IMCR &= TIM_IC2S_Reset;\r
557       }\r
558       else\r
559       {\r
560         /* External signal TI1 connected to IC2 channel */\r
561         TIMx->IMCR |= TIM_IC2S_Set;\r
562       }\r
563     }\r
564   }\r
565 }\r
566 \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
572 * Output         : None\r
573 * Return         : None\r
574 *******************************************************************************/\r
575 void TIM_SetPrescaler(TIM_TypeDef* TIMx, u16 Prescaler)\r
576 {\r
577   TIMx->PSC = Prescaler;\r
578 }\r
579 \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
585 * Output         : None\r
586 * Return         : None\r
587 *******************************************************************************/\r
588 void TIM_SetPeriod(TIM_TypeDef* TIMx, u16 Period)\r
589 {\r
590   TIMx->ARR = Period;\r
591 }\r
592 \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
603 * Output         : None\r
604 * Return         : None\r
605 *******************************************************************************/\r
606 void TIM_SetPulse(TIM_TypeDef* TIMx, u16 TIM_Channel, u16 Pulse)\r
607 {\r
608   /* Set Channel 1 pulse value */\r
609   if(TIM_Channel == TIM_Channel_1)\r
610   {\r
611     TIMx->OCR1 = Pulse;\r
612   }\r
613   /* Set Channel 2 pulse value */\r
614   else if(TIM_Channel == TIM_Channel_2)\r
615   {\r
616    TIMx->OCR2 = Pulse;\r
617   }\r
618   /* Set Channel 1 and Channel 2 pulse values */\r
619   else if(TIM_Channel == TIM_Channel_ALL)\r
620   {\r
621     TIMx->OCR1 = Pulse;\r
622     TIMx->OCR2 = Pulse;\r
623   }\r
624 }\r
625 \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
630 * Output         : None\r
631 * Return         : Input Capture 1 Register value.\r
632 *******************************************************************************/\r
633 u16 TIM_GetICAP1(TIM_TypeDef *TIMx)\r
634 {\r
635   return TIMx->ICR1;\r
636 }\r
637 \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
642 * Output         : None\r
643 * Return         : Input Capture 2 Register value\r
644 *******************************************************************************/\r
645 u16 TIM_GetICAP2(TIM_TypeDef *TIMx)\r
646 {\r
647   return TIMx->ICR2;\r
648 }\r
649 \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
654 * Output         : None\r
655 * Return         : Input Capture 2 Register value\r
656 *******************************************************************************/\r
657 u16 TIM_GetPWMIPulse(TIM_TypeDef *TIMx)\r
658 {\r
659   return TIMx->ICR2;\r
660 }\r
661 \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
666 * Output         : None\r
667 * Return         : Input Capture 1 Register value\r
668 *******************************************************************************/\r
669 u16 TIM_GetPWMIPeriod(TIM_TypeDef *TIMx)\r
670 {\r
671   return TIMx->ICR1;\r
672 }\r
673 \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
680 * Output         : None\r
681 * Return         : None\r
682 *******************************************************************************/\r
683 void TIM_DebugCmd(TIM_TypeDef *TIMx, FunctionalState Newstate)\r
684 {\r
685   if(Newstate == ENABLE)\r
686   {\r
687     TIMx->CR |= TIM_DBGC_Set;\r
688   }\r
689   else\r
690   {\r
691     TIMx->CR &= TIM_DBGC_Reset;\r
692   }\r
693 }\r
694 \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
706 * Output         : None\r
707 * Return         : None\r
708 *******************************************************************************/\r
709 void TIM_CounterModeConfig(TIM_TypeDef* TIMx, u16 TIM_CounterMode)\r
710 {\r
711   /* Counter mode configuration */\r
712   TIMx->CR &= TIM_CounterMode_Mask;\r
713   TIMx->CR |= TIM_CounterMode;\r
714 }\r
715 \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
730 *                         OCxREF.\r
731 * Output         : None\r
732 * Return         : None\r
733 *******************************************************************************/\r
734 void TIM_ForcedOCConfig(TIM_TypeDef* TIMx, u16 TIM_Channel,u16 TIM_ForcedAction)\r
735 {\r
736   /* Channel 1 Forced Output Compare mode configuration */\r
737   if(TIM_Channel == TIM_Channel_1)\r
738   {\r
739     TIMx->OMR1 &= TIM_OC1C_Mask;\r
740     TIMx->OMR1 |= TIM_ForcedAction;\r
741   }\r
742   /* Channel 2 Forced Output Compare mode configuration */\r
743   else\r
744   {\r
745     if(TIM_Channel == TIM_Channel_2)\r
746     {\r
747       TIMx->OMR1 &= TIM_OC2C_Mask;\r
748       TIMx->OMR1 |= (TIM_ForcedAction<<8);\r
749     }\r
750     /* Channel 1 and Channel 2 Forced Output Compare mode configuration */\r
751     else\r
752     {\r
753       TIMx->OMR1 &= TIM_OC1C_Mask & TIM_OC2C_Mask;\r
754       TIMx->OMR1 |= TIM_ForcedAction |(TIM_ForcedAction<<8);\r
755     }\r
756   }\r
757 }\r
758 \r
759 /*******************************************************************************\r
760 * Function Name  : TIM_ResetCounter\r
761 * Description    : Re-intializes the TIM counter and generates an update of the\r
762 *                  registers.\r
763 * Input          : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral\r
764 * Output         : None\r
765 * Return         : None\r
766 *******************************************************************************/\r
767 void TIM_ResetCounter(TIM_TypeDef* TIMx)\r
768 {\r
769   /* Re-intialize the TIM counter */\r
770   TIMx->CR |= TIM_COUNTER_Reset;\r
771 }\r
772 \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
783 *                    be used.\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
799 * Output         : None\r
800 * Return         : None\r
801 *******************************************************************************/\r
802 void TIM_SynchroConfig(Master_TypeDef Master, Slave_TypeDef Slave,\r
803                        u16 TIM_SynchroAction, u16 TIM_SynchroMode)\r
804 {\r
805   switch (Slave)\r
806   {\r
807     case PWM_Slave:\r
808     {\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
812 \r
813       if(Master == TIM1_Master)\r
814       {\r
815         /* Set the internal trigger */\r
816         PWM->SCR |= TIM_ITS_TIM1;\r
817 \r
818         /* Set the synchronization action */\r
819         TIM1->CR &= TIM_MasterModeSelection_Mask;\r
820         TIM1->CR |= TIM_SynchroAction;\r
821       }\r
822 \r
823       else if(Master == TIM0_Master)\r
824       {\r
825         /* Set the internal trigger */\r
826         PWM->SCR |= TIM_ITS_TIM0;\r
827 \r
828         /* Set the synchronization action */\r
829         TIM0->CR &= TIM_MasterModeSelection_Mask;\r
830         TIM0->CR |= TIM_SynchroAction;\r
831       }\r
832 \r
833       else if(Master == TIM2_Master)\r
834       {\r
835         /* Set the internal trigger */\r
836         PWM->SCR |= TIM_ITS_TIM2;\r
837 \r
838         /* Set the synchronization action */\r
839         TIM2->CR &= TIM_MasterModeSelection_Mask;\r
840         TIM2->CR |= TIM_SynchroAction;\r
841       }\r
842     }\r
843     break;\r
844 \r
845     case TIM0_Slave:\r
846     {\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
850 \r
851       if(Master == PWM_Master)\r
852       {\r
853         /* Set the internal trigger */\r
854         TIM0->SCR |= TIM_ITS_PWM;\r
855 \r
856         /* Set the synchronization action */\r
857         PWM->CR &= TIM_MasterModeSelection_Mask;\r
858         PWM->CR |= TIM_SynchroAction;\r
859       }\r
860 \r
861       else if(Master == TIM1_Master)\r
862       {\r
863         /* Set the internal trigger */\r
864         TIM0->SCR |= TIM_ITS_TIM1;\r
865 \r
866         /* Set the synchronization action */\r
867         TIM1->CR &= TIM_MasterModeSelection_Mask;\r
868         TIM1->CR |= TIM_SynchroAction;\r
869       }\r
870 \r
871       else if(Master == TIM2_Master)\r
872       {\r
873         /* Set the internal trigger */\r
874         TIM0->SCR |= TIM_ITS_TIM2;\r
875 \r
876         /* Set the synchronization action */\r
877         TIM2->CR &= TIM_MasterModeSelection_Mask;\r
878         TIM2->CR |= TIM_SynchroAction;\r
879       }\r
880     }\r
881     break;\r
882 \r
883     case TIM1_Slave:\r
884     {\r
885 \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
889      \r
890       if(Master == PWM_Master)\r
891       {\r
892          /* Set the internal trigger */\r
893          TIM1->SCR |= TIM_ITS_PWM;\r
894 \r
895         /* Set the synchronization action */\r
896         PWM->CR &= TIM_MasterModeSelection_Mask;\r
897         PWM->CR |= TIM_SynchroAction;\r
898       }\r
899       else if(Master == TIM0_Master)\r
900       {\r
901         /* Set the internal trigger */\r
902         TIM1->SCR |= TIM_ITS_TIM0;\r
903 \r
904         /* Set the synchronization action */\r
905         TIM0->CR &= TIM_MasterModeSelection_Mask;\r
906         TIM0->CR |= TIM_SynchroAction;\r
907       }\r
908 \r
909       else if(Master == TIM2_Master)\r
910       {\r
911         /* Set the internal trigger */\r
912         TIM1->SCR |= TIM_ITS_TIM2;\r
913 \r
914         /* Set the synchronization action */\r
915         TIM2->CR &= TIM_MasterModeSelection_Mask;\r
916         TIM2->CR |= TIM_SynchroAction;\r
917       }\r
918     }\r
919     break;\r
920 \r
921     case TIM2_Slave:\r
922     {\r
923      \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
927 \r
928       if(Master == PWM_Master)\r
929       {\r
930         /* Internal trigger selection */\r
931         TIM2->SCR |= TIM_ITS_PWM;\r
932 \r
933         /* Set the synchronization action */\r
934         PWM->CR &= TIM_MasterModeSelection_Mask;\r
935         PWM->CR |= TIM_SynchroAction;\r
936       }\r
937 \r
938       else if(Master == TIM1_Master)\r
939       {\r
940         /* Internal trigger selection */\r
941         TIM2->SCR |= TIM_ITS_TIM1;\r
942 \r
943         /* Set the synchronization action */\r
944         TIM1->CR &= TIM_MasterModeSelection_Mask;\r
945         TIM1->CR |= TIM_SynchroAction;\r
946       }\r
947 \r
948       else if(Master == TIM0_Master)\r
949       {\r
950         /* Internal trigger selection */\r
951         TIM2->SCR |= TIM_ITS_TIM0;\r
952 \r
953         /* Set the synchronization action */\r
954         TIM0->CR &= TIM_MasterModeSelection_Mask;\r
955         TIM0->CR |= TIM_SynchroAction;\r
956       }\r
957     }\r
958     break;\r
959 \r
960     default:\r
961     break;\r
962   }\r
963 }\r
964 \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
976 * Output         : None\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
980 {\r
981   if((TIMx->ISR & TIM_FLAG) != RESET )\r
982   {\r
983     return SET;\r
984   }\r
985   else\r
986   {\r
987     return RESET;\r
988   }\r
989 }\r
990 \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
1002 * Output         : None\r
1003 * Return         : None\r
1004 *******************************************************************************/\r
1005 void TIM_ClearFlag(TIM_TypeDef* TIMx, u16 TIM_FLAG)\r
1006 {\r
1007   /* Clear the flags */\r
1008   TIMx->ISR &= ~TIM_FLAG;\r
1009 }\r
1010 \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
1023 * Output         : None\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
1027 {\r
1028   u16 TIM_IT_Check = 0;\r
1029 \r
1030   /* Calculates the pending bits to be checked */\r
1031   TIM_IT_Check = TIM_IT & TIM_IT_Clear_Mask;\r
1032   \r
1033   if((TIMx->ISR & TIM_IT_Check) != RESET )\r
1034   {\r
1035     return SET;\r
1036   }\r
1037   else\r
1038   {\r
1039     return RESET;\r
1040   }\r
1041 }\r
1042 \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
1055 * Output         : None\r
1056 * Return         : None\r
1057 *******************************************************************************/\r
1058 void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, u16 TIM_IT)\r
1059 {\r
1060   u16 TIM_IT_Clear = 0;\r
1061 \r
1062   /* Calculate the pending bits to be cleared */\r
1063   TIM_IT_Clear = TIM_IT & TIM_IT_Clear_Mask;\r
1064 \r
1065   /* Clear the pending bits */\r
1066   TIMx->ISR &= ~TIM_IT_Clear;\r
1067 }\r
1068 \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
1075 *                  peripheral.\r
1076 * Output         : None\r
1077 * Return         : None\r
1078 *******************************************************************************/\r
1079 static void OCM_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)\r
1080 {\r
1081   u16 TIM_OCControl = 0x0000;\r
1082 \r
1083   if(TIM_InitStruct->TIM_Mode == TIM_Mode_OCTiming)\r
1084   {\r
1085     TIM_OCControl = TIM_OCControl_OCTiming;\r
1086   }\r
1087   else\r
1088   {\r
1089     if((TIM_InitStruct->TIM_Mode == TIM_Mode_OCActive) || \r
1090        (TIM_InitStruct->TIM_Mode == TIM_Mode_OPM_Active))\r
1091     {\r
1092       TIM_OCControl = TIM_OCControl_OCActive;\r
1093     }\r
1094     else\r
1095     {\r
1096       if(TIM_InitStruct->TIM_Mode == TIM_Mode_OCInactive)\r
1097       {\r
1098         TIM_OCControl = TIM_OCControl_OCInactive;\r
1099       }\r
1100       else\r
1101       {\r
1102          if((TIM_InitStruct->TIM_Mode == TIM_Mode_OCToggle) ||\r
1103             (TIM_InitStruct->TIM_Mode == TIM_Mode_OPM_Toggle))\r
1104         {\r
1105           TIM_OCControl = TIM_OCControl_OCToggle;\r
1106         }\r
1107         else\r
1108         {\r
1109           TIM_OCControl = TIM_OCControl_PWM;\r
1110 \r
1111         }\r
1112       }\r
1113     }\r
1114   }\r
1115 \r
1116   if(TIM_InitStruct->TIM_Channel == TIM_Channel_1)\r
1117   {\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
1123 \r
1124     /* Set the OC1 wave polarity */\r
1125     if(TIM_InitStruct->TIM_Polarity1 == TIM_Polarity1_Low)\r
1126     {\r
1127       TIMx->OMR1 |= TIM_OC1P_Set;\r
1128     }\r
1129     else\r
1130     {\r
1131       TIMx->OMR1 &= TIM_OC1P_Reset;\r
1132     }\r
1133   }\r
1134   else\r
1135   {\r
1136     if(TIM_InitStruct->TIM_Channel == TIM_Channel_2)\r
1137     {\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
1143 \r
1144       /* Set the OCB wave polarity */\r
1145       if(TIM_InitStruct->TIM_Polarity2 == TIM_Polarity2_Low)\r
1146       {\r
1147         TIMx->OMR1 |= TIM_OC2P_Set;\r
1148       }\r
1149       else\r
1150       {\r
1151         TIMx->OMR1 &= TIM_OC2P_Reset;\r
1152       }\r
1153     }\r
1154      /* Configure Channel 1 and Channel 2 on Output Compare mode */\r
1155     else\r
1156     {\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
1160 \r
1161       TIMx->OCR1 = TIM_InitStruct->TIM_Pulse1;\r
1162       TIMx->OCR2 = TIM_InitStruct->TIM_Pulse2;\r
1163 \r
1164       /* Set the OC1 wave polarity */\r
1165       if(TIM_InitStruct->TIM_Polarity1 == TIM_Polarity1_Low)\r
1166       {\r
1167         TIMx->OMR1 |= TIM_OC1P_Set;\r
1168       }\r
1169       else\r
1170       {\r
1171         TIMx->OMR1 &= TIM_OC1P_Reset;\r
1172       }\r
1173 \r
1174       /* Set the OC2 wave polarity */\r
1175       if(TIM_InitStruct->TIM_Polarity2 == TIM_Polarity2_Low)\r
1176       {\r
1177         TIMx->OMR1 |= TIM_OC2P_Set;\r
1178       }\r
1179       else\r
1180       {\r
1181         TIMx->OMR1 &= TIM_OC2P_Reset;\r
1182       }\r
1183     }\r
1184   }\r
1185 }\r
1186 \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
1193 *                  peripheral.\r
1194 * Output         : None\r
1195 * Return         : None\r
1196 *******************************************************************************/\r
1197 static void ICAP_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)\r
1198 {\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
1202 \r
1203     /* Channel 1 and channel 2 input selection */\r
1204     if(TIM_InitStruct->TIM_PWMI_ICSelection == TIM_PWMI_ICSelection_TI1)\r
1205     {\r
1206       TIMx->IMCR &= TIM_IC1S_Reset;\r
1207       TIMx->IMCR |= TIM_IC2S_Set;\r
1208     }\r
1209     else\r
1210     {\r
1211       TIMx->IMCR |= TIM_IC1S_Set;\r
1212       TIMx->IMCR &= TIM_IC2S_Reset;\r
1213     }\r
1214 \r
1215     /* Channel polarity */\r
1216     if(TIM_InitStruct->TIM_PWMI_ICPolarity == TIM_PWMI_ICPolarity_Rising)\r
1217     {\r
1218       TIMx->IMCR &= TIM_IC1P_Reset;\r
1219       TIMx->IMCR |= TIM_IC2P_Set;\r
1220     }\r
1221     else\r
1222     {\r
1223       TIMx->IMCR |= TIM_IC1P_Set;\r
1224       TIMx->IMCR &= TIM_IC2P_Reset;\r
1225     }\r
1226 \r
1227     /* Input capture  Enable */\r
1228     TIMx->IMCR |= TIM_IC1_Enable |TIM_IC2_Enable;\r
1229   }\r
1230   else\r
1231   {\r
1232     if(TIM_InitStruct->TIM_Channel == TIM_Channel_1)\r
1233     {\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
1237       \r
1238       /* Channel 1 input selection */\r
1239       if(TIM_InitStruct->TIM_IC1Selection == TIM_IC1Selection_TI1)\r
1240       {\r
1241         TIMx->IMCR &= TIM_IC1S_Reset;\r
1242       }\r
1243       else\r
1244       {\r
1245         TIMx->IMCR |= TIM_IC1S_Set;\r
1246       }\r
1247       /* Channel 1 polarity */\r
1248       if(TIM_InitStruct->TIM_IC1Polarity == TIM_IC1Polarity_Rising)\r
1249       {\r
1250         TIMx->IMCR &= TIM_IC1P_Reset;\r
1251       }\r
1252       else\r
1253       {\r
1254         TIMx->IMCR |= TIM_IC1P_Set;\r
1255       }\r
1256 \r
1257       /* Input capture  Enable */\r
1258       TIMx->IMCR |= TIM_IC1_Enable;\r
1259     }\r
1260     else\r
1261     {\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
1265 \r
1266       /* Channel 2 input selection */\r
1267       if(TIM_InitStruct->TIM_IC2Selection == TIM_IC2Selection_TI2)\r
1268       {\r
1269         TIMx->IMCR &= TIM_IC2S_Reset;\r
1270       }\r
1271       else\r
1272       {\r
1273         TIMx->IMCR |= TIM_IC2S_Set;\r
1274       }\r
1275 \r
1276       /* Channel 2 polarity */\r
1277       if(TIM_InitStruct->TIM_IC2Polarity == TIM_IC2Polarity_Rising)\r
1278       {\r
1279         TIMx->IMCR &= TIM_IC2P_Reset;\r
1280       }\r
1281       else\r
1282       {\r
1283         TIMx->IMCR |= TIM_IC2P_Set;\r
1284       }\r
1285 \r
1286       /* Input capture  Enable */\r
1287       TIMx->IMCR |= TIM_IC2_Enable;\r
1288     }\r
1289   }\r
1290 }\r
1291 \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
1298 *                  peripheral.\r
1299 * Output         : None\r
1300 * Return         : None\r
1301 *******************************************************************************/\r
1302 static void Encoder_ModeConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)\r
1303 {\r
1304   /* Set Encoder mode */\r
1305   TIMx->SCR &= TIM_Encoder_Mask;\r
1306   \r
1307   if(TIM_InitStruct->TIM_Mode == TIM_Mode_Encoder1) \r
1308   {\r
1309     TIMx->SCR |= TIM_Encoder1_Set;\r
1310   }\r
1311   else if (TIM_InitStruct->TIM_Mode == TIM_Mode_Encoder2)\r
1312   {\r
1313     TIMx->SCR |= TIM_Encoder2_Set;\r
1314   }\r
1315   else \r
1316   {\r
1317     TIMx->SCR |= TIM_Encoder3_Set;\r
1318   }\r
1319 \r
1320   /* Channel 1 input selection */\r
1321   if(TIM_InitStruct->TIM_IC1Selection == TIM_IC1Selection_TI2)\r
1322   {\r
1323     TIMx->IMCR |= TIM_IC1S_Set;\r
1324   }\r
1325   else\r
1326   {\r
1327     TIMx->IMCR &= TIM_IC1S_Reset;\r
1328   }\r
1329 \r
1330    /* Channel 2 input selection */\r
1331    if(TIM_InitStruct->TIM_IC2Selection == TIM_IC2Selection_TI1)\r
1332    {\r
1333      TIMx->IMCR |= TIM_IC2S_Set;\r
1334    }\r
1335    else\r
1336    {\r
1337      TIMx->IMCR &= TIM_IC2S_Reset;\r
1338    }\r
1339 \r
1340    /* Channel 1 polarity */\r
1341    if(TIM_InitStruct->TIM_IC1Polarity == TIM_IC1Polarity_Falling)\r
1342    {\r
1343      TIMx->IMCR |= TIM_IC1P_Set;\r
1344    }\r
1345    else\r
1346    {\r
1347      TIMx->IMCR &= TIM_IC1P_Reset;\r
1348    }\r
1349 \r
1350    /* Channel 2 polarity */\r
1351    if(TIM_InitStruct->TIM_IC2Polarity == TIM_IC2Polarity_Falling)\r
1352    {\r
1353      TIMx->IMCR |= TIM_IC2P_Set;\r
1354    }\r
1355    else\r
1356    {\r
1357      TIMx->IMCR &= TIM_IC2P_Reset;\r
1358    }\r
1359 }\r
1360 /******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/\r