]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F103_IAR/STM32F10xFWLib/src/stm32f10x_tim.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTEX_STM32F103_IAR / STM32F10xFWLib / src / stm32f10x_tim.c
1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************\r
2 * File Name          : stm32f10x_tim.c\r
3 * Author             : MCD Application Team\r
4 * Date First Issued  : 09/29/2006\r
5 * Description        : This file provides all the TIM firmware functions.\r
6 ********************************************************************************\r
7 * History:\r
8 * 04/02/2007: V0.2\r
9 * 02/05/2007: V0.1\r
10 * 09/29/2006: V0.01\r
11 ********************************************************************************\r
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.\r
14 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,\r
15 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE\r
16 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING\r
17 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
18 *******************************************************************************/\r
19 \r
20 /* Includes ------------------------------------------------------------------*/\r
21 #include "stm32f10x_tim.h"\r
22 #include "stm32f10x_rcc.h"\r
23 \r
24 /* Private typedef -----------------------------------------------------------*/\r
25 /* Private define ------------------------------------------------------------*/\r
26 /* ---------------------- TIM registers bit mask ------------------------ */\r
27 #define CR1_CEN_Set                 ((u16)0x0001)\r
28 #define CR1_CEN_Reset               ((u16)0x03FE)\r
29 #define CR1_UDIS_Set                ((u16)0x0002)\r
30 #define CR1_UDIS_Reset              ((u16)0x03FD)\r
31 #define CR1_URS_Set                 ((u16)0x0004)\r
32 #define CR1_URS_Reset               ((u16)0x03FB)\r
33 #define CR1_OPM_Mask                ((u16)0x03F7)\r
34 #define CR1_CounterMode_Mask        ((u16)0x039F)\r
35 #define CR1_ARPE_Set                ((u16)0x0080)\r
36 #define CR1_ARPE_Reset              ((u16)0x037F)\r
37 #define CR1_CKD_Mask                ((u16)0x00FF)\r
38 \r
39 #define CR2_CCDS_Set                ((u16)0x0008)\r
40 #define CR2_CCDS_Reset              ((u16)0x0007)\r
41 #define CR2_MMS_Mask                ((u16)0x0080)\r
42 #define CR2_TI1S_Set                ((u16)0x0080)\r
43 #define CR2_TI1S_Reset              ((u16)0xFF70)\r
44 \r
45 #define SMCR_SMS_Mask               ((u16)0xFFF0)\r
46 #define SMCR_ETR_Mask               ((u16)0x00F7)\r
47 #define SMCR_TS_Mask                ((u16)0xFF87)\r
48 #define SMCR_MSM_Mask               ((u16)0xFF77)\r
49 #define SMCR_ECE_Set                ((u16)0x4000)\r
50 \r
51 #define CCMR_CC13S_Mask             ((u16)0x7F7C)\r
52 #define CCMR_CC24S_Mask             ((u16)0x7C7F)\r
53 #define CCMR_TI13Direct_Set         ((u16)0x0001)\r
54 #define CCMR_TI24Direct_Set         ((u16)0x0100)\r
55 #define CCMR_OC13FE_Mask            ((u16)0x7F7B)\r
56 #define CCMR_OC24FE_Mask            ((u16)0x7B7F)\r
57 #define CCMR_OC13PE_Mask            ((u16)0x7F77)\r
58 #define CCMR_OC24PE_Mask            ((u16)0x777F)\r
59 #define CCMR_OCM13_Mask             ((u16)0x7F0F)\r
60 #define CCMR_OCM24_Mask             ((u16)0x0F7F)\r
61 #define CCMR_IC13PSC_Mask           ((u16)0xFFF3)\r
62 #define CCMR_IC24PSC_Mask           ((u16)0xF3FF)\r
63 #define CCMR_IC13F_Mask             ((u16)0xFF0F)\r
64 #define CCMR_IC24F_Mask             ((u16)0x0FFF)\r
65 #define CCER_CC1P_Mask              ((u16)0xFFFD)\r
66 \r
67 #define CCER_CC2P_Mask              ((u16)0xFFDF)\r
68 #define CCER_CC3P_Mask              ((u16)0xFDFF)\r
69 #define CCER_CC4P_Mask              ((u16)0xDFFF)\r
70 \r
71 #define CCRE_CC1E_Set               ((u16)0x0001)\r
72 #define CCRE_CC1E_Reset             ((u16)0xFFFE)\r
73 #define CCRE_CC1E_Mask              ((u16)0xFFFE)\r
74 \r
75 #define CCRE_CC2E_Set               ((u16)0x0010)\r
76 #define CCRE_CC2E_Reset             ((u16)0xFFEF)\r
77 #define CCRE_CC2E_Mask              ((u16)0xFFEF)\r
78 \r
79 #define CCRE_CC3E_Set               ((u16)0x0100)\r
80 #define CCRE_CC3E_Reset             ((u16)0xFEFF)\r
81 \r
82 #define CCRE_CC4E_Set               ((u16)0x1000)\r
83 #define CCRE_CC4E_Reset             ((u16)0xEFFF)\r
84 #define CCRE_CC4E_Mask              ((u16)0xEFFF)\r
85 \r
86 #define DCR_DMA_Mask                ((u16)0x0000)\r
87 \r
88 /* TIM private Masks */\r
89 #define TIM_Period_Reset_Mask       ((u16)0x0000)\r
90 #define TIM_Prescaler_Reset_Mask    ((u16)0x0000)\r
91 #define TIM_Pulse_Reset_Mask        ((u16)0x0000)\r
92 #define TIM_ICFilter_Mask           ((u8)0x00)\r
93 \r
94 /* Private macro -------------------------------------------------------------*/\r
95 /* Private variables ---------------------------------------------------------*/\r
96 static uc16 Tab_OCModeMask[4] = {0xFF00, 0x00FF, 0xFF00, 0x00FF};\r
97 static uc16 Tab_PolarityMask[4] = {CCER_CC1P_Mask, CCER_CC2P_Mask, CCER_CC3P_Mask, CCER_CC4P_Mask};\r
98 \r
99 /* Private function prototypes -----------------------------------------------*/\r
100 static void PWMI_Config(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct);\r
101 static void TI1_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,\r
102                        u8 TIM_ICFilter);\r
103 static void TI2_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,\r
104                        u8 TIM_ICFilter);\r
105 static void TI3_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,\r
106                        u8 TIM_ICFilter);\r
107 static void TI4_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,\r
108                        u8 TIM_ICFilter);\r
109 static void ETR_Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, \r
110                        u16 TIM_ExtTRGPolarity, u8 ExtTRGFilter);\r
111 /* Private functions ---------------------------------------------------------*/\r
112 \r
113 /*******************************************************************************\r
114 * Function Name  : TIM_DeInit\r
115 * Description    : Deinitializes the TIMx peripheral registers to their default\r
116 *                  reset values.\r
117 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
118 * Output         : None\r
119 * Return         : None\r
120 *******************************************************************************/\r
121 void TIM_DeInit(TIM_TypeDef* TIMx)\r
122 {  \r
123   switch (*(u32*)&TIMx)\r
124   {\r
125     case TIM2_BASE:\r
126       RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);\r
127       RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, DISABLE);\r
128       break;\r
129  \r
130     case TIM3_BASE:\r
131       RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);\r
132       RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, DISABLE);\r
133       break;\r
134  \r
135     case TIM4_BASE:\r
136       RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);\r
137       RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, DISABLE);\r
138       break;\r
139  \r
140     default:\r
141       break;\r
142   }\r
143 }\r
144 \r
145 /*******************************************************************************\r
146 * Function Name  : TIM_TimeBaseInit\r
147 * Description    : Initializes the TIMx Time Base Unit peripheral according to \r
148 *                  the specified parameters in the TIM_TimeBaseInitStruct.\r
149 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
150 *                  - TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef\r
151 *                   structure that contains the configuration information for\r
152 *                   the specified TIM peripheral.\r
153 * Output         : None\r
154 * Return         : None\r
155 *******************************************************************************/\r
156 void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)\r
157 {\r
158   /* Check the parameters */\r
159   assert(IS_TIM_COUNTER_MODE(TIM_TimeBaseInitStruct->TIM_CounterMode));\r
160   assert(IS_TIM_CKD_DIV(TIM_TimeBaseInitStruct->TIM_ClockDivision));\r
161   \r
162   /* Set the Autoreload value */\r
163   TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ;\r
164 \r
165   /* Set the Prescaler value */\r
166   TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;\r
167 \r
168   /* Select the Counter Mode and set the clock division */\r
169   TIMx->CR1 &= CR1_CKD_Mask & CR1_CounterMode_Mask;\r
170   TIMx->CR1 |= (u32)TIM_TimeBaseInitStruct->TIM_ClockDivision |\r
171                TIM_TimeBaseInitStruct->TIM_CounterMode;\r
172 }\r
173 /*******************************************************************************\r
174 * Function Name  : TIM_OCInit\r
175 * Description    : Initializes the TIMx peripheral according to the specified\r
176 *                  parameters in the TIM_OCInitStruct.\r
177 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
178 *                  - TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure\r
179 *                    that contains the configuration information for the specified\r
180 *                    TIM peripheral.\r
181 * Output         : None\r
182 * Return         : None\r
183 *******************************************************************************/\r
184 void TIM_OCInit(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)\r
185 {\r
186   u32 tmpccmrx = 0, tmpccer = 0;\r
187   \r
188   /* Check the parameters */\r
189   assert(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));\r
190   assert(IS_TIM_CHANNEL(TIM_OCInitStruct->TIM_Channel));\r
191   assert(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));\r
192 \r
193   tmpccer = TIMx->CCER;\r
194 \r
195   if ((TIM_OCInitStruct->TIM_Channel == (u16)TIM_Channel_1) ||\r
196       (TIM_OCInitStruct->TIM_Channel == (u16)TIM_Channel_2))\r
197   {\r
198     tmpccmrx = TIMx->CCMR1;\r
199     \r
200     /* Reset the Output Compare Bits */\r
201     tmpccmrx &= Tab_OCModeMask[TIM_OCInitStruct->TIM_Channel];\r
202 \r
203     /* Set the Output Polarity level */\r
204     tmpccer &= Tab_PolarityMask[TIM_OCInitStruct->TIM_Channel];\r
205 \r
206     if (TIM_OCInitStruct->TIM_Channel == TIM_Channel_1)\r
207     {\r
208       /* Disable the Channel 1: Reset the CCE Bit */\r
209       TIMx->CCER &= CCRE_CC1E_Reset;\r
210 \r
211       /* Select the Output Compare Mode */\r
212       tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;\r
213 \r
214       /* Set the Capture Compare Register value */\r
215       TIMx->CCR1 = TIM_OCInitStruct->TIM_Pulse;\r
216 \r
217       /* Set the Capture Compare Enable Bit */\r
218       tmpccer |= CCRE_CC1E_Set;\r
219 \r
220       /* Set the Capture Compare Polarity */\r
221       tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;\r
222     }\r
223     else /* TIM_Channel_2 */\r
224     {\r
225       /* Disable the Channel 2: Reset the CCE Bit */\r
226       TIMx->CCER &= CCRE_CC2E_Reset;\r
227 \r
228       /* Select the Output Compare Mode */\r
229       tmpccmrx |= (u32)TIM_OCInitStruct->TIM_OCMode << 8;\r
230 \r
231       /* Set the Capture Compare Register value */\r
232       TIMx->CCR2 = TIM_OCInitStruct->TIM_Pulse;\r
233 \r
234       /* Set the Capture Compare Enable Bit */\r
235       tmpccer |= CCRE_CC2E_Set;\r
236 \r
237       /* Set the Capture Compare Polarity */\r
238       tmpccer |= (u32)TIM_OCInitStruct->TIM_OCPolarity << 4;\r
239     }\r
240 \r
241     TIMx->CCMR1 = (u16)tmpccmrx;\r
242   }\r
243   else \r
244   {\r
245     if ((TIM_OCInitStruct->TIM_Channel == TIM_Channel_3) ||\r
246         (TIM_OCInitStruct->TIM_Channel == TIM_Channel_4))\r
247     { \r
248       tmpccmrx = TIMx->CCMR2;\r
249 \r
250       /* Reset the Output Compare Bits */\r
251       tmpccmrx &= Tab_OCModeMask[TIM_OCInitStruct->TIM_Channel];\r
252 \r
253       /* Set the Output Polarity level */\r
254       tmpccer &= Tab_PolarityMask[TIM_OCInitStruct->TIM_Channel];\r
255 \r
256       if (TIM_OCInitStruct->TIM_Channel == TIM_Channel_3)\r
257       {\r
258         /* Disable the Channel 3: Reset the CCE Bit */\r
259         TIMx->CCER &= CCRE_CC3E_Reset;\r
260 \r
261         /* Select the Output Compare Mode */\r
262         tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;\r
263 \r
264         /* Set the Capture Compare Register value */\r
265         TIMx->CCR3 = TIM_OCInitStruct->TIM_Pulse;\r
266 \r
267         /* Set the Capture Compare Enable Bit */\r
268         tmpccer |= CCRE_CC3E_Set;\r
269 \r
270         /* Set the Capture Compare Polarity */\r
271         tmpccer |= (u32)TIM_OCInitStruct->TIM_OCPolarity << 8;\r
272       }\r
273       else  /* TIM_Channel_4 */\r
274       {\r
275         /* Disable the Channel 4: Reset the CCE Bit */\r
276         TIMx->CCER &= CCRE_CC4E_Reset;\r
277 \r
278        /* Select the Output Compare Mode */\r
279         tmpccmrx |= (u32)TIM_OCInitStruct->TIM_OCMode << 8;\r
280 \r
281         /* Set the Capture Compare Register value */\r
282         TIMx->CCR4 = TIM_OCInitStruct->TIM_Pulse;\r
283 \r
284         /* Set the Capture Compare Enable Bit */\r
285         tmpccer |= CCRE_CC4E_Set;\r
286 \r
287         /* Set the Capture Compare Polarity */\r
288         tmpccer |= (u32)TIM_OCInitStruct->TIM_OCPolarity << 12;\r
289       }\r
290 \r
291       TIMx->CCMR2 = (u16)tmpccmrx;\r
292     }\r
293   }\r
294   \r
295   TIMx->CCER = (u16)tmpccer;\r
296 }\r
297 \r
298 /*******************************************************************************\r
299 * Function Name  : TIM_ICInit\r
300 * Description    : Initializes the TIMx peripheral according to the specified\r
301 *                  parameters in the TIM_ICInitStruct.\r
302 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
303 *                  - TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure\r
304 *                    that contains the configuration information for the specified\r
305 *                    TIM peripheral.\r
306 * Output         : None\r
307 * Return         : None\r
308 *******************************************************************************/\r
309 void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)\r
310 {\r
311   /* Check the parameters */\r
312   assert(IS_TIM_IC_MODE(TIM_ICInitStruct->TIM_ICMode));\r
313   assert(IS_TIM_CHANNEL(TIM_ICInitStruct->TIM_Channel));\r
314   assert(IS_TIM_IC_POLARITY(TIM_ICInitStruct->TIM_ICPolarity));\r
315   assert(IS_TIM_IC_SELECTION(TIM_ICInitStruct->TIM_ICSelection));\r
316   assert(IS_TIM_IC_PRESCALER(TIM_ICInitStruct->TIM_ICPrescaler));\r
317   assert(IS_TIM_IC_FILTER(TIM_ICInitStruct->TIM_ICFilter));\r
318   \r
319   if (TIM_ICInitStruct->TIM_ICMode == TIM_ICMode_ICAP)\r
320   {\r
321     if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)\r
322     {\r
323       /* TI1 Configuration */\r
324       TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,\r
325                  TIM_ICInitStruct->TIM_ICSelection,\r
326                  TIM_ICInitStruct->TIM_ICFilter);\r
327 \r
328       /* Set the Input Capture Prescaler value */\r
329       TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);\r
330     }\r
331     else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_2)\r
332     {\r
333       /* TI2 Configuration */\r
334       TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,\r
335                  TIM_ICInitStruct->TIM_ICSelection,\r
336                  TIM_ICInitStruct->TIM_ICFilter);\r
337 \r
338       /* Set the Input Capture Prescaler value */\r
339       TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);\r
340     }\r
341     else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_3)\r
342     {\r
343       /* TI3 Configuration */\r
344       TI3_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,\r
345                  TIM_ICInitStruct->TIM_ICSelection,\r
346                  TIM_ICInitStruct->TIM_ICFilter);\r
347 \r
348       /* Set the Input Capture Prescaler value */\r
349       TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);\r
350     }\r
351     else /* TIM_Channel_4 */\r
352     {\r
353       /* TI4 Configuration */\r
354       TI4_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,\r
355                  TIM_ICInitStruct->TIM_ICSelection,\r
356                  TIM_ICInitStruct->TIM_ICFilter);\r
357 \r
358       /* Set the Input Capture Prescaler value */\r
359       TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);\r
360     }\r
361   }\r
362   else\r
363   {\r
364     PWMI_Config(TIMx, TIM_ICInitStruct);\r
365   }\r
366 }\r
367 \r
368 /*******************************************************************************\r
369 * Function Name  : TIM_TimeBaseStructInit\r
370 * Description    : Fills each TIM_TimeBaseInitStruct member with its default value.\r
371 * Input          : - TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef\r
372 *                    structure which will be initialized.\r
373 * Output         : None\r
374 * Return         : None\r
375 *******************************************************************************/\r
376 void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)\r
377 {\r
378   /* Set the default configuration */\r
379   TIM_TimeBaseInitStruct->TIM_Period = TIM_Period_Reset_Mask;\r
380   TIM_TimeBaseInitStruct->TIM_Prescaler = TIM_Prescaler_Reset_Mask;\r
381   TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;\r
382   TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;\r
383 }\r
384 \r
385 /*******************************************************************************\r
386 * Function Name  : TIM_OCStructInit\r
387 * Description    : Fills each TIM_OCInitStruct member with its default value.\r
388 * Input          : - TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure\r
389 *                    which will be initialized.\r
390 * Output         : None\r
391 * Return         : None\r
392 *******************************************************************************/\r
393 void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct)\r
394 {\r
395   /* Set the default configuration */\r
396   TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;\r
397   TIM_OCInitStruct->TIM_Channel = TIM_Channel_1;\r
398   TIM_OCInitStruct->TIM_Pulse = TIM_Pulse_Reset_Mask;\r
399   TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;\r
400 }\r
401 \r
402 /*******************************************************************************\r
403 * Function Name  : TIM_ICStructInit\r
404 * Description    : Fills each TIM_InitStruct member with its default value.\r
405 * Input          : - TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure\r
406 *                    which will be initialized.\r
407 * Output         : None\r
408 * Return         : None\r
409 *******************************************************************************/\r
410 void TIM_ICStructInit(TIM_ICInitTypeDef* TIM_ICInitStruct)\r
411 {\r
412   /* Set the default configuration */\r
413   TIM_ICInitStruct->TIM_ICMode = TIM_ICMode_ICAP;\r
414   TIM_ICInitStruct->TIM_Channel = TIM_Channel_1;\r
415   TIM_ICInitStruct->TIM_ICPolarity = TIM_ICPolarity_Rising;\r
416   TIM_ICInitStruct->TIM_ICSelection = TIM_ICSelection_DirectTI;\r
417   TIM_ICInitStruct->TIM_ICPrescaler = TIM_ICPSC_DIV1;\r
418   TIM_ICInitStruct->TIM_ICFilter = TIM_ICFilter_Mask;\r
419 }\r
420 \r
421 /*******************************************************************************\r
422 * Function Name  : TIM_Cmd\r
423 * Description    : Enables or disables the specified TIM peripheral.\r
424 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIMx peripheral.\r
425 *                  - Newstate: new state of the TIMx peripheral.\r
426 *                    This parameter can be: ENABLE or DISABLE.\r
427 * Output         : None\r
428 * Return         : None\r
429 *******************************************************************************/\r
430 void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)\r
431 {\r
432   /* Check the parameters */\r
433   assert(IS_FUNCTIONAL_STATE(NewState));\r
434   \r
435   if (NewState != DISABLE)\r
436   {\r
437     /* Enable the TIM Counter */\r
438     TIMx->CR1 |= CR1_CEN_Set;\r
439   }\r
440   else\r
441   {\r
442     /* Disable the TIM Counter */\r
443     TIMx->CR1 &= CR1_CEN_Reset;\r
444   }\r
445 }\r
446 \r
447 /*******************************************************************************\r
448 * Function Name  : TIM_ITConfig\r
449 * Description    : Enables or disables the TIMx interrupts.\r
450 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
451 *                  - TIM_IT: specifies the TIM interrupts sources to be enabled\r
452 *                    or disabled.\r
453 *                    This parameter can be any combination of the following values:\r
454 *                       - TIM_IT_Update: Timer update Interrupt\r
455 *                       - TIM_IT_CC1: Capture Compare 1 Interrupt\r
456 *                       - TIM_IT_CC2: Capture Compare 2 Interrupt\r
457 *                       - TIM_IT_CC3: Capture Compare 3 Interrupt\r
458 *                       - TIM_IT_CC4: Capture Compare 4 Interrupt\r
459 *                       - TIM_IT_Trigger: Trigger Interrupt\r
460 *                  - Newstate: new state of the specified TIMx interrupts.\r
461 *                    This parameter can be: ENABLE or DISABLE.\r
462 * Output         : None\r
463 * Return         : None\r
464 *******************************************************************************/\r
465 void TIM_ITConfig(TIM_TypeDef* TIMx, u16 TIM_IT, FunctionalState NewState)\r
466 {\r
467   /* Check the parameters */\r
468   assert(IS_TIM_IT(TIM_IT));\r
469   assert(IS_FUNCTIONAL_STATE(NewState));\r
470   \r
471   if (NewState != DISABLE)\r
472   {\r
473     /* Enable the Interrupt sources */\r
474     TIMx->DIER |= TIM_IT;\r
475   }\r
476   else\r
477   {\r
478     /* Disable the Interrupt sources */\r
479     TIMx->DIER &= (u16)(~TIM_IT);\r
480   }\r
481 }\r
482 \r
483 /*******************************************************************************\r
484 * Function Name  : TIM_DMAConfig\r
485 * Description    : Configures the TIMx\92s DMA interface.\r
486 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
487 *                  - TIM_DMABase: DMA Base address.\r
488 *                    This parameter can be one of the following values:\r
489 *                       - TIM_DMABase_CR1, TIM_DMABase_CR2, TIM_DMABase_SMCR,\r
490 *                         TIM_DMABase_DIER, TIM_DMABase_SR, TIM_DMABase_EGR,\r
491 *                         TIM_DMABase_CCMR1, TIM_DMABase_CCMR2, TIM_DMABase_CCER,\r
492 *                         TIM_DMABase_CNT, TIM_DMABase_PSC, TIM_DMABase_ARR,\r
493 *                         TIM_DMABase_CCR1, TIM_DMABase_CCR2, TIM_DMABase_CCR3,\r
494 *                         TIM_DMABase_CCR4, TIM_DMABase_DCR.\r
495 *                  - TIM_DMABurstLength: DMA Burst length.\r
496 *                    This parameter can be one value between:\r
497 *                    TIM_DMABurstLength_1Byte and TIM_DMABurstLength_18Bytes.\r
498 * Output         : None\r
499 * Return         : None\r
500 *******************************************************************************/\r
501 void TIM_DMAConfig(TIM_TypeDef* TIMx, u16 TIM_DMABase, u16 TIM_DMABurstLength)\r
502 {\r
503   u32 tmpdcr = 0;\r
504 \r
505   /* Check the parameters */\r
506   assert(IS_TIM_DMA_BASE(TIM_DMABase));\r
507   assert(IS_TIM_DMA_LENGTH(TIM_DMABurstLength));\r
508   \r
509   tmpdcr = TIMx->DCR;\r
510 \r
511   /* Reset the DBA and the DBL Bits */\r
512   tmpdcr &= DCR_DMA_Mask;\r
513 \r
514   /* Set the DMA Base and the DMA Burst Length */\r
515   tmpdcr |= TIM_DMABase | TIM_DMABurstLength;\r
516 \r
517   TIMx->DCR = (u16)tmpdcr;\r
518 }\r
519 \r
520 /*******************************************************************************\r
521 * Function Name  : TIM_DMACmd\r
522 * Description    : Enables or disables the TIMx\92s DMA Requests.\r
523 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
524 *                  - TIM_DMASources: specifies the DMA Request sources.\r
525 *                    This parameter can be any combination of the following values:\r
526 *                       - TIM_DMA_CC1: Capture Compare 1 DMA source\r
527 *                       - TIM_DMA_CC2: Capture Compare 2 DMA source\r
528 *                       - TIM_DMA_CC3: Capture Compare 3 DMA source\r
529 *                       - TIM_DMA_CC4: Capture Compare 4 DMA source\r
530 *                       - TIM_DMA_Trigger: Trigger DMA source\r
531 *                  - Newstate: new state of the DMA Request sources.\r
532 *                    This parameter can be: ENABLE or DISABLE.\r
533 * Output         : None\r
534 * Return         : None\r
535 *******************************************************************************/\r
536 void TIM_DMACmd(TIM_TypeDef* TIMx, u16 TIM_DMASource, FunctionalState Newstate)\r
537 {\r
538   u32 tmpdier = 0;\r
539   \r
540   /* Check the parameters */\r
541   assert(IS_TIM_DMA_SOURCE(TIM_DMASource));\r
542   assert(IS_FUNCTIONAL_STATE(Newstate));\r
543 \r
544   tmpdier = TIMx->DIER;\r
545 \r
546   if (Newstate != DISABLE)\r
547   {\r
548     /* Enable the DMA sources */\r
549     tmpdier |= TIM_DMASource;\r
550   }\r
551   else\r
552   {\r
553     /* Disable the DMA sources */\r
554     tmpdier &= (u16)(~TIM_DMASource);\r
555   }\r
556   TIMx->DIER = (u16)tmpdier;\r
557 }\r
558 \r
559 /*******************************************************************************\r
560 * Function Name  : TIM_InternalClockConfig\r
561 * Description    : Configures the TIMx interrnal Clock\r
562 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
563 * Output         : None\r
564 * Return         : None\r
565 *******************************************************************************/\r
566 void TIM_InternalClockConfig(TIM_TypeDef* TIMx)\r
567 {\r
568   /* Disable slave mode to clock the prescaler directly with the internal clock */\r
569   TIMx->SMCR &=  SMCR_SMS_Mask;\r
570 }\r
571 /*******************************************************************************\r
572 * Function Name  : TIM_ITRxExternalClockConfig\r
573 * Description    : Configures the TIMx Internal Trigger as External Clock\r
574 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
575 *                  - TIM_ITRSource: Trigger source.\r
576 *                    This parameter can be one of the following values:\r
577 *                       - TIM_TS_ITR0: Internal Trigger 0\r
578 *                       - TIM_TS_ITR1: Internal Trigger 1\r
579 *                       - TIM_TS_ITR2: Internal Trigger 2\r
580 *                       - TIM_TS_ITR3: Internal Trigger 3\r
581 * Output         : None\r
582 * Return         : None\r
583 *******************************************************************************/\r
584 void TIM_ITRxExternalClockConfig(TIM_TypeDef* TIMx, u16 TIM_InputTriggerSource)\r
585 {\r
586   /* Check the parameters */\r
587   assert(IS_TIM_INTERNAL_TRIGGER_SELECTION(TIM_InputTriggerSource));\r
588 \r
589   /* Select the Internal Trigger */\r
590   TIM_SelectInputTrigger(TIMx, TIM_InputTriggerSource);\r
591 \r
592   /* Select the External clock mode1 */\r
593   TIMx->SMCR |= TIM_SlaveMode_External1;\r
594 }\r
595 /*******************************************************************************\r
596 * Function Name  : TIM_TIxExternalClockConfig\r
597 * Description    : Configures the TIMx Trigger as External Clock\r
598 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
599 *                  - TIM_TIxExternalCLKSource: Trigger source.\r
600 *                    This parameter can be one of the following values:\r
601 *                       - TIM_TS_TI1F_ED: TI1 Edge Detector\r
602 *                       - TIM_TS_TI1FP1: Filtered Timer Input 1\r
603 *                       - TIM_TS_TI2FP2: Filtered Timer Input 2\r
604 *                  - TIM_ICPolarity: specifies the TIx Polarity.\r
605 *                    This parameter can be:\r
606 *                       - TIM_ICPolarity_Rising\r
607 *                       - TIM_ICPolarity_Falling\r
608 *                   - ICFilter : specifies the filter value.\r
609 *                     This parameter must be a value between 0x0 and 0xF.\r
610 * Output         : None\r
611 * Return         : None\r
612 *******************************************************************************/\r
613 void TIM_TIxExternalClockConfig(TIM_TypeDef* TIMx, u16 TIM_TIxExternalCLKSource,\r
614                                 u16 TIM_ICPolarity, u8 ICFilter)\r
615 {\r
616   /* Check the parameters */\r
617   assert(IS_TIM_TIX_TRIGGER_SELECTION(TIM_TIxExternalCLKSource));\r
618   assert(IS_TIM_IC_POLARITY(TIM_ICPolarity));\r
619   assert(IS_TIM_IC_FILTER(ICFilter));\r
620 \r
621   /* Configure the Timer Input Clock Source */\r
622   if (TIM_TIxExternalCLKSource == TIM_TIxExternalCLK1Source_TI2)\r
623   {\r
624     TI2_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);\r
625   }\r
626   else\r
627   {\r
628     TI1_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);\r
629   }\r
630 \r
631   /* Select the Trigger source */\r
632   TIM_SelectInputTrigger(TIMx, TIM_TIxExternalCLKSource);\r
633 \r
634   /* Select the External clock mode1 */\r
635   TIMx->SMCR |= TIM_SlaveMode_External1;\r
636 }\r
637 \r
638 /*******************************************************************************\r
639 * Function Name  : TIM_ETRClockMode1Config\r
640 * Description    : Configures the External clock Mode1\r
641 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
642 *                  - TIM_ExtTRGPrescaler: The external Trigger Prescaler.\r
643 *                    It can be one of the following values:\r
644 *                       - TIM_ExtTRGPSC_OFF\r
645 *                       - TIM_ExtTRGPSC_DIV2\r
646 *                       - TIM_ExtTRGPSC_DIV4\r
647 *                       - TIM_ExtTRGPSC_DIV8.\r
648 *                  - TIM_ExtTRGPolarity: The external Trigger Polarity.\r
649 *                    It can be one of the following values:\r
650 *                       - TIM_ExtTRGPolarity_Inverted\r
651 *                       - TIM_ExtTRGPolarity_NonInverted\r
652 *                  - ExtTRGFilter: External Trigger Filter.\r
653 *                    This parameter must be a value between 0x00 and 0x0F\r
654 * Output         : None\r
655 * Return         : None\r
656 *******************************************************************************/\r
657 void TIM_ETRClockMode1Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, u16 TIM_ExtTRGPolarity,\r
658                              u8 ExtTRGFilter)\r
659 {\r
660   /* Check the parameters */\r
661   assert(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));\r
662   assert(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));\r
663 \r
664   /* Configure the ETR Clock source */\r
665   ETR_Config(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);\r
666 \r
667   /* Select the External clock mode1 */\r
668   TIMx->SMCR &= SMCR_SMS_Mask;\r
669   TIMx->SMCR |= TIM_SlaveMode_External1;\r
670 \r
671   /* Select the Trigger selection : ETRF */\r
672   TIMx->SMCR &= SMCR_TS_Mask;\r
673   TIMx->SMCR |= TIM_TS_ETRF;\r
674 }\r
675 \r
676 /*******************************************************************************\r
677 * Function Name  : TIM_ETRClockMode2Config\r
678 * Description    : Configures the External clock Mode2\r
679 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
680 *                  - TIM_ExtTRGPrescaler: The external Trigger Prescaler.\r
681 *                    It can be one of the following values:\r
682 *                       - TIM_ExtTRGPSC_OFF\r
683 *                       - TIM_ExtTRGPSC_DIV2\r
684 *                       - TIM_ExtTRGPSC_DIV4\r
685 *                       - TIM_ExtTRGPSC_DIV8\r
686 *                  - TIM_ExtTRGPolarity: The external Trigger Polarity.\r
687 *                    It can be one of the following values:\r
688 *                       - TIM_ExtTRGPolarity_Inverted\r
689 *                       - TIM_ExtTRGPolarity_NonInverted\r
690 *                  - ExtTRGFilter: External Trigger Filter.\r
691 *                    This parameter must be a value between 0x00 and 0x0F\r
692 * Output         : None\r
693 * Return         : None\r
694 *******************************************************************************/\r
695 void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, \r
696                              u16 TIM_ExtTRGPolarity, u8 ExtTRGFilter)\r
697 {\r
698   /* Check the parameters */\r
699   assert(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));\r
700   assert(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));\r
701 \r
702   /* Configure the ETR Clock source */\r
703   ETR_Config(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);\r
704 \r
705   /* Enable the External clock mode2 */\r
706   TIMx->SMCR |= SMCR_ECE_Set;\r
707 }\r
708 /*******************************************************************************\r
709 * Function Name  : TIM_SelectInputTrigger\r
710 * Description    : Selects the Input Trigger source\r
711 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
712 *                  - TIM_InputTriggerSource: The Input Trigger source.\r
713 *                    This parameter can be one of the following values:\r
714 *                       - TIM_TS_ITR0: Internal Trigger 0\r
715 *                       - TIM_TS_ITR1: Internal Trigger 1\r
716 *                       - TIM_TS_ITR2: Internal Trigger 2\r
717 *                       - TIM_TS_ITR3: Internal Trigger 3\r
718 *                       - TIM_TS_TI1F_ED: TI1 Edge Detector\r
719 *                       - TIM_TS_TI1FP1: Filtered Timer Input 1\r
720 *                       - TIM_TS_TI2FP2: Filtered Timer Input 2\r
721 *                       - TIM_TS_ETRF: External Trigger input\r
722 * Output         : None\r
723 * Return         : None\r
724 *******************************************************************************/\r
725 void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, u16 TIM_InputTriggerSource)\r
726 {\r
727   u32 tmpsmcr = 0;\r
728 \r
729   /* Check the parameters */\r
730   assert(IS_TIM_TRIGGER_SELECTION(TIM_InputTriggerSource));\r
731 \r
732   tmpsmcr = TIMx->SMCR;\r
733 \r
734   /* Select the Tgigger Source */\r
735   tmpsmcr &= SMCR_TS_Mask;\r
736   tmpsmcr |= TIM_InputTriggerSource;\r
737 \r
738   TIMx->SMCR = (u16)tmpsmcr;\r
739 }\r
740 \r
741 /*******************************************************************************\r
742 * Function Name  : TIM_PrescalerConfig\r
743 * Description    : Configures the TIMx Prescaler.\r
744 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
745 *                  - Prescaler: specifies the Prescaler Register value\r
746 *                  - TIM_PSCReloadMode: specifies the TIM Prescaler Reload mode\r
747 *                    This parameter can be one of the following values:\r
748 *                       - TIM_PSCReloadMode_Update: The Prescaler is loaded at\r
749 *                         the update event.\r
750 *                       - TIM_PSCReloadMode_Immediate: The Prescaler is loaded\r
751 *                         immediatly.\r
752 * Output         : None\r
753 * Return         : None\r
754 *******************************************************************************/\r
755 void TIM_PrescalerConfig(TIM_TypeDef* TIMx, u16 Prescaler, u16 TIM_PSCReloadMode)\r
756 {\r
757   /* Check the parameters */\r
758   assert(IS_TIM_PRESCALER_RELOAD(TIM_PSCReloadMode));\r
759 \r
760   /* Set the Prescaler value */\r
761   TIMx->PSC = Prescaler;\r
762 \r
763   /* Set or reset the UG Bit */\r
764   if (TIM_PSCReloadMode == TIM_PSCReloadMode_Immediate)\r
765   {\r
766     TIMx->EGR |= TIM_EventSource_Update;\r
767   }\r
768   else\r
769   {\r
770     TIMx->EGR &= TIM_EventSource_Update;\r
771   }\r
772 }\r
773 \r
774 /*******************************************************************************\r
775 * Function Name  : TIM_CounterModeConfig\r
776 * Description    : Specifies the TIMx Counter Mode to be used.\r
777 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
778 *                  - TIM_CounterMode: specifies the Counter Mode to be used\r
779 *                    This parameter can be one of the following values:\r
780 *                       - TIM_CounterMode_Up: TIM Up Counting Mode\r
781 *                       - TIM_CounterMode_Down: TIM Down Counting Mode\r
782 *                       - TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1\r
783 *                       - TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2\r
784 *                       - TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3\r
785 * Output         : None\r
786 * Return         : None\r
787 *******************************************************************************/\r
788 void TIM_CounterModeConfig(TIM_TypeDef* TIMx, u16 TIM_CounterMode)\r
789 {\r
790   u32 tmpcr1 = 0;\r
791 \r
792   /* Check the parameters */\r
793   assert(IS_TIM_COUNTER_MODE(TIM_CounterMode));\r
794 \r
795   tmpcr1 = TIMx->CR1;\r
796 \r
797   /* Reset the CMS and DIR Bits */\r
798   tmpcr1 &= CR1_CounterMode_Mask;\r
799 \r
800   /* Set the Counter Mode */\r
801   tmpcr1 |= TIM_CounterMode;\r
802 \r
803   TIMx->CR1 = (u16)tmpcr1;\r
804 }\r
805 \r
806 /*******************************************************************************\r
807 * Function Name  : TIM_ForcedOC1Config\r
808 * Description    : Forces the TIMx output 1 waveform to active or inactive level.\r
809 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
810 *                  - TIM_ForcedAction: specifies the forced Action to be set to\r
811 *                    the output waveform.\r
812 *                    This parameter can be one of the following values:\r
813 *                       - TIM_ForcedAction_Active: Force active level on OC1REF\r
814 *                       - TIM_ForcedAction_InActive: Force inactive level on\r
815 *                         OC1REF.\r
816 * Output         : None\r
817 * Return         : None\r
818 *******************************************************************************/\r
819 void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction)\r
820 {\r
821   u32 tmpccmr1 = 0;\r
822 \r
823   /* Check the parameters */\r
824   assert(IS_TIM_FORCED_ACTION(TIM_ForcedAction));\r
825 \r
826   tmpccmr1 = TIMx->CCMR1;\r
827 \r
828   /* Reset the OCM Bits */\r
829   tmpccmr1 &= CCMR_OCM13_Mask;\r
830 \r
831   /* Configure The Forced output Mode */\r
832   tmpccmr1 |= TIM_ForcedAction;\r
833 \r
834   TIMx->CCMR1 = (u16)tmpccmr1;\r
835 }\r
836 \r
837 /*******************************************************************************\r
838 * Function Name  : TIM_ForcedOC2Config\r
839 * Description    : Forces the TIMx output 2 waveform to active or inactive level.\r
840 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
841 *                  - TIM_ForcedAction: specifies the forced Action to be set to\r
842 *                    the output waveform.\r
843 *                    This parameter can be one of the following values:\r
844 *                       - TIM_ForcedAction_Active: Force active level on OC2REF\r
845 *                       - TIM_ForcedAction_InActive: Force inactive level on\r
846 *                         OC2REF.\r
847 * Output         : None\r
848 * Return         : None\r
849 *******************************************************************************/\r
850 void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction)\r
851 {\r
852   u32 tmpccmr1 = 0;\r
853 \r
854   /* Check the parameters */\r
855   assert(IS_TIM_FORCED_ACTION(TIM_ForcedAction));\r
856 \r
857   tmpccmr1 = TIMx->CCMR1;\r
858 \r
859   /* Reset the OCM Bits */\r
860   tmpccmr1 &= CCMR_OCM24_Mask;\r
861 \r
862   /* Configure The Forced output Mode */\r
863   tmpccmr1 |= (u16)(TIM_ForcedAction << 8);\r
864 \r
865   TIMx->CCMR1 = (u16)tmpccmr1;\r
866 }\r
867 \r
868 /*******************************************************************************\r
869 * Function Name  : TIM_ForcedOC3Config\r
870 * Description    : Forces the TIMx output 3 waveform to active or inactive level.\r
871 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
872 *                  - TIM_ForcedAction: specifies the forced Action to be set to\r
873 *                    the output waveform.\r
874 *                    This parameter can be one of the following values:\r
875 *                       - TIM_ForcedAction_Active: Force active level on OC3REF\r
876 *                       - TIM_ForcedAction_InActive: Force inactive level on\r
877 *                         OC3REF.\r
878 * Output         : None\r
879 * Return         : None\r
880 *******************************************************************************/\r
881 void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction)\r
882 {\r
883   u32 tmpccmr2 = 0;\r
884 \r
885   /* Check the parameters */\r
886   assert(IS_TIM_FORCED_ACTION(TIM_ForcedAction));\r
887 \r
888   tmpccmr2 = TIMx->CCMR2;\r
889 \r
890   /* Reset the OCM Bits */\r
891   tmpccmr2 &= CCMR_OCM13_Mask;\r
892 \r
893   /* Configure The Forced output Mode */\r
894   tmpccmr2 |= TIM_ForcedAction;\r
895 \r
896   TIMx->CCMR2 = (u16)tmpccmr2;\r
897 }\r
898 \r
899 /*******************************************************************************\r
900 * Function Name  : TIM_ForcedOC4Config\r
901 * Description    : Forces the TIMx output 4 waveform to active or inactive level.\r
902 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
903 *                  - TIM_ForcedAction: specifies the forced Action to be set to\r
904 *                    the output waveform.\r
905 *                    This parameter can be one of the following values:\r
906 *                       - TIM_ForcedAction_Active: Force active level on OC4REF\r
907 *                       - TIM_ForcedAction_InActive: Force inactive level on\r
908 *                         OC4REF.\r
909 * Output         : None\r
910 * Return         : None\r
911 *******************************************************************************/\r
912 void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction)\r
913 {\r
914   u32 tmpccmr2 = 0;\r
915 \r
916   /* Check the parameters */\r
917   assert(IS_TIM_FORCED_ACTION(TIM_ForcedAction));\r
918 \r
919   tmpccmr2 = TIMx->CCMR2;\r
920 \r
921   /* Reset the OCM Bits */\r
922   tmpccmr2 &= CCMR_OCM24_Mask;\r
923 \r
924   /* Configure The Forced output Mode */\r
925   tmpccmr2 |= (u16)(TIM_ForcedAction << 8);\r
926 \r
927   TIMx->CCMR2 = (u16)tmpccmr2;\r
928 }\r
929 \r
930 /*******************************************************************************\r
931 * Function Name  : TIM_ARRPreloadConfig\r
932 * Description    : Enables or disables TIMx peripheral Preload register on ARR.\r
933 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
934 *                  - Newstate: new state of the TIMx peripheral Preload register\r
935 *                    This parameter can be: ENABLE or DISABLE.\r
936 * Output         : None\r
937 * Return         : None\r
938 *******************************************************************************/\r
939 void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState Newstate)\r
940 {\r
941   u32 tmpcr1 = 0;\r
942   \r
943   /* Check the parameters */\r
944   assert(IS_FUNCTIONAL_STATE(Newstate));\r
945 \r
946   tmpcr1 = TIMx->CR1;\r
947 \r
948   if (Newstate != DISABLE)\r
949   {\r
950     /* Set the ARR Preload Bit */\r
951     tmpcr1 |= CR1_ARPE_Set;\r
952   }\r
953   else\r
954   {\r
955     /* Reset the ARR Preload Bit */\r
956     tmpcr1 &= CR1_ARPE_Reset;\r
957   }\r
958 \r
959   TIMx->CR1 = (u16)tmpcr1;\r
960 }\r
961 \r
962 /*******************************************************************************\r
963 * Function Name  : TIM_SelectCCDMA\r
964 * Description    : Selects the TIMx peripheral Capture Compare DMA source.\r
965 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
966 *                  - Newstate: new state of the Capture Compare DMA source\r
967 *                    This parameter can be: ENABLE or DISABLE.\r
968 * Output         : None\r
969 * Return         : None\r
970 *******************************************************************************/\r
971 void TIM_SelectCCDMA(TIM_TypeDef* TIMx, FunctionalState Newstate)\r
972 {\r
973   u32 tmpcr2 = 0;\r
974 \r
975   /* Check the parameters */\r
976   assert(IS_FUNCTIONAL_STATE(Newstate));\r
977 \r
978   tmpcr2 = TIMx->CR2;\r
979 \r
980   if (Newstate != DISABLE)\r
981   {\r
982     /* Set the CCDS Bit */\r
983     tmpcr2 |= CR2_CCDS_Set;\r
984   }\r
985   else\r
986   {\r
987     /* Reset the CCDS Bit */\r
988     tmpcr2 &= CR2_CCDS_Reset;\r
989   }\r
990 \r
991   TIMx->CR2 = (u16)tmpcr2;\r
992 }\r
993 \r
994 /*******************************************************************************\r
995 * Function Name  : TIM_OC1PreloadConfig\r
996 * Description    : Enables or disables the TIMx peripheral Preload register on CCR1.\r
997 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
998 *                  - TIM_OCPreload: new state of the TIMx peripheral Preload\r
999 *                    register\r
1000 *                    This parameter can be one of the following values:\r
1001 *                       - TIM_OCPreload_Enable\r
1002 *                       - TIM_OCPreload_Disable\r
1003 * Output         : None\r
1004 * Return         : None\r
1005 *******************************************************************************/\r
1006 void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, u16 TIM_OCPreload)\r
1007 {\r
1008   u32 tmpccmr1 = 0;\r
1009 \r
1010   /* Check the parameters */\r
1011   assert(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));\r
1012 \r
1013   tmpccmr1 = TIMx->CCMR1;\r
1014 \r
1015   /* Reset the OCPE Bit */\r
1016   tmpccmr1 &= CCMR_OC13PE_Mask;\r
1017 \r
1018   /* Enable or Disable the Output Compare Preload feature */\r
1019   tmpccmr1 |= TIM_OCPreload;\r
1020 \r
1021   TIMx->CCMR1 = (u16)tmpccmr1;\r
1022 }\r
1023 \r
1024 /*******************************************************************************\r
1025 * Function Name  : TIM_OC2PreloadConfig\r
1026 * Description    : Enables or disables the TIMx peripheral Preload register on CCR2.\r
1027 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1028 *                  - TIM_OCPreload: new state of the TIMx peripheral Preload\r
1029 *                    register\r
1030 *                    This parameter can be one of the following values:\r
1031 *                       - TIM_OCPreload_Enable\r
1032 *                       - TIM_OCPreload_Disable\r
1033 * Output         : None\r
1034 * Return         : None\r
1035 *******************************************************************************/\r
1036 void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, u16 TIM_OCPreload)\r
1037 {\r
1038   u32 tmpccmr1 = 0;\r
1039 \r
1040   /* Check the parameters */\r
1041   assert(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));\r
1042 \r
1043   tmpccmr1 = TIMx->CCMR1;\r
1044 \r
1045   /* Reset the OCPE Bit */\r
1046   tmpccmr1 &= CCMR_OC24PE_Mask;\r
1047 \r
1048   /* Enable or Disable the Output Compare Preload feature */\r
1049   tmpccmr1 |= (u16)(TIM_OCPreload << 8);\r
1050 \r
1051   TIMx->CCMR1 = (u16)tmpccmr1;\r
1052 }\r
1053 \r
1054 /*******************************************************************************\r
1055 * Function Name  : TIM_OC3PreloadConfig\r
1056 * Description    : Enables or disables the TIMx peripheral Preload register on CCR3.\r
1057 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1058 *                  - TIM_OCPreload: new state of the TIMx peripheral Preload\r
1059 *                    register\r
1060 *                    This parameter can be one of the following values:\r
1061 *                       - TIM_OCPreload_Enable\r
1062 *                       - TIM_OCPreload_Disable\r
1063 * Output         : None\r
1064 * Return         : None\r
1065 *******************************************************************************/\r
1066 void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, u16 TIM_OCPreload)\r
1067 {\r
1068   u32 tmpccmr2 = 0;\r
1069 \r
1070   /* Check the parameters */\r
1071   assert(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));\r
1072 \r
1073   tmpccmr2 = TIMx->CCMR2;\r
1074 \r
1075   /* Reset the OCPE Bit */\r
1076   tmpccmr2 &= CCMR_OC13PE_Mask;\r
1077 \r
1078   /* Enable or Disable the Output Compare Preload feature */\r
1079   tmpccmr2 |= TIM_OCPreload;\r
1080 \r
1081   TIMx->CCMR2 = (u16)tmpccmr2;\r
1082 }\r
1083 \r
1084 /*******************************************************************************\r
1085 * Function Name  : TIM_OC4PreloadConfig\r
1086 * Description    : Enables or disables the TIMx peripheral Preload register on CCR4.\r
1087 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1088 *                  - TIM_OCPreload: new state of the TIMx peripheral Preload\r
1089 *                    register\r
1090 *                    This parameter can be one of the following values:\r
1091 *                       - TIM_OCPreload_Enable\r
1092 *                       - TIM_OCPreload_Disable\r
1093 * Output         : None\r
1094 * Return         : None\r
1095 *******************************************************************************/\r
1096 void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, u16 TIM_OCPreload)\r
1097 {\r
1098   u32 tmpccmr2 = 0;\r
1099 \r
1100   /* Check the parameters */\r
1101   assert(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));\r
1102 \r
1103   tmpccmr2 = TIMx->CCMR2;\r
1104 \r
1105   /* Reset the OCPE Bit */\r
1106   tmpccmr2 &= CCMR_OC24PE_Mask;\r
1107 \r
1108   /* Enable or Disable the Output Compare Preload feature */\r
1109   tmpccmr2 |= (u16)(TIM_OCPreload << 8);\r
1110 \r
1111   TIMx->CCMR2 = (u16)tmpccmr2;\r
1112 }\r
1113 \r
1114 /*******************************************************************************\r
1115 * Function Name  : TIM_OC1FastConfig\r
1116 * Description    : Configures the TIMx Output Compare 1 Fast feature.\r
1117 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1118 *                  - TIM_OCFast: new state of the Output Compare Fast Enable Bit.\r
1119 *                    This parameter can be one of the following values:\r
1120 *                       - TIM_OCFast_Enable\r
1121 *                       - TIM_OCFast_Disable\r
1122 * Output         : None\r
1123 * Return         : None\r
1124 *******************************************************************************/\r
1125 void TIM_OC1FastConfig(TIM_TypeDef* TIMx, u16 TIM_OCFast)\r
1126 {\r
1127   u32 tmpccmr1 = 0;\r
1128 \r
1129   /* Check the parameters */\r
1130   assert(IS_TIM_OCFAST_STATE(TIM_OCFast));\r
1131 \r
1132   tmpccmr1 = TIMx->CCMR1;\r
1133 \r
1134   /* Reset the OCFE Bit */\r
1135   tmpccmr1 &= CCMR_OC13FE_Mask;\r
1136 \r
1137   /* Enable or Disable the Output Compare Fast Bit */\r
1138   tmpccmr1 |= TIM_OCFast;\r
1139 \r
1140   TIMx->CCMR1 = (u16)tmpccmr1;\r
1141 }\r
1142 \r
1143 /*******************************************************************************\r
1144 * Function Name  : TIM_OC2FastConfig\r
1145 * Description    : Configures the TIMx Output Compare 2 Fast feature.\r
1146 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1147 *                  - TIM_OCFast: new state of the Output Compare Fast Enable Bit.\r
1148 *                    This parameter can be one of the following values:\r
1149 *                       - TIM_OCFast_Enable\r
1150 *                       - TIM_OCFast_Disable\r
1151 * Output         : None\r
1152 * Return         : None\r
1153 *******************************************************************************/\r
1154 void TIM_OC2FastConfig(TIM_TypeDef* TIMx, u16 TIM_OCFast)\r
1155 {\r
1156   u32 tmpccmr1 = 0;\r
1157 \r
1158   /* Check the parameters */\r
1159   assert(IS_TIM_OCFAST_STATE(TIM_OCFast));\r
1160 \r
1161   tmpccmr1 = TIMx->CCMR1;\r
1162 \r
1163   /* Reset the OCFE Bit */\r
1164   tmpccmr1 &= CCMR_OC24FE_Mask;\r
1165 \r
1166   /* Enable or Disable the Output Compare Fast Bit */\r
1167   tmpccmr1 |= (u16)(TIM_OCFast << 8);\r
1168 \r
1169   TIMx->CCMR1 = (u16)tmpccmr1;\r
1170 }\r
1171 \r
1172 /*******************************************************************************\r
1173 * Function Name  : TIM_OC3FastConfig\r
1174 * Description    : Configures the TIMx Output Compare 3 Fast feature.\r
1175 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1176 *                  - TIM_OCFast: new state of the Output Compare Fast Enable Bit.\r
1177 *                    This parameter can be one of the following values:\r
1178 *                       - TIM_OCFast_Enable\r
1179 *                       - TIM_OCFast_Disable\r
1180 * Output         : None\r
1181 * Return         : None\r
1182 *******************************************************************************/\r
1183 void TIM_OC3FastConfig(TIM_TypeDef* TIMx, u16 TIM_OCFast)\r
1184 {\r
1185   u32 tmpccmr2 = 0;\r
1186 \r
1187   /* Check the parameters */\r
1188   assert(IS_TIM_OCFAST_STATE(TIM_OCFast));\r
1189 \r
1190   tmpccmr2 = TIMx->CCMR2;\r
1191 \r
1192   /* Reset the OCFE Bit */\r
1193   tmpccmr2 &= CCMR_OC13FE_Mask;\r
1194 \r
1195   /* Enable or Disable the Output Compare Fast Bit */\r
1196   tmpccmr2 |= TIM_OCFast;\r
1197 \r
1198   TIMx->CCMR2 = (u16)tmpccmr2;\r
1199 }\r
1200 \r
1201 /*******************************************************************************\r
1202 * Function Name  : TIM_OC4FastConfig\r
1203 * Description    : Configures the TIMx Output Compare 4 Fast feature.\r
1204 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1205 *                  - TIM_OCFast: new state of the Output Compare Fast Enable Bit.\r
1206 *                    This parameter can be one of the following values:\r
1207 *                       - TIM_OCFast_Enable\r
1208 *                       - TIM_OCFast_Disable\r
1209 * Output         : None\r
1210 * Return         : None\r
1211 *******************************************************************************/\r
1212 void TIM_OC4FastConfig(TIM_TypeDef* TIMx, u16 TIM_OCFast)\r
1213 {\r
1214   u32 tmpccmr2 = 0;\r
1215 \r
1216   /* Check the parameters */\r
1217   assert(IS_TIM_OCFAST_STATE(TIM_OCFast));\r
1218 \r
1219   tmpccmr2 = TIMx->CCMR2;\r
1220 \r
1221   /* Reset the OCFE Bit */\r
1222   tmpccmr2 &= CCMR_OC24FE_Mask;\r
1223 \r
1224   /* Enable or Disable the Output Compare Fast Bit */\r
1225   tmpccmr2 |= (u16)(TIM_OCFast << 8);\r
1226 \r
1227   TIMx->CCMR2 = (u16)tmpccmr2;\r
1228 }\r
1229 \r
1230 /*******************************************************************************\r
1231 * Function Name  : TIM_UpdateDisableConfig\r
1232 * Description    : Enables or Disables the TIMx Update event.\r
1233 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1234 *                  - Newstate: new state of the TIMx peripheral Preload register\r
1235 *                    This parameter can be: ENABLE or DISABLE.\r
1236 * Output         : None\r
1237 * Return         : None\r
1238 *******************************************************************************/\r
1239 void TIM_UpdateDisableConfig(TIM_TypeDef* TIMx, FunctionalState Newstate)\r
1240 {\r
1241   u32 tmpcr1 = 0;\r
1242 \r
1243   /* Check the parameters */\r
1244   assert(IS_FUNCTIONAL_STATE(Newstate));\r
1245 \r
1246   tmpcr1 = TIMx->CR1;\r
1247 \r
1248   if (Newstate != DISABLE)\r
1249   {\r
1250     /* Set the Update Disable Bit */\r
1251     tmpcr1 |= CR1_UDIS_Set;\r
1252   }\r
1253   else\r
1254   {\r
1255     /* Reset the Update Disable Bit */\r
1256     tmpcr1 &= CR1_UDIS_Reset;\r
1257   }\r
1258 \r
1259   TIMx->CR1 = (u16)tmpcr1;\r
1260 }\r
1261 \r
1262 /*******************************************************************************\r
1263 * Function Name  : TIM_EncoderInterfaceConfig\r
1264 * Description    : Configures the TIMx Encoder Interface.\r
1265 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1266 *                  - TIM_EncoderMode: specifies the TIMx Encoder Mode.\r
1267 *                    This parameter can be one of the following values:\r
1268 *                       - TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge\r
1269 *                         depending on TI2FP2 level.\r
1270 *                       - TIM_EncoderMode_TI2: Counter counts on TI2FP2 edge\r
1271 *                         depending on TI1FP1 level.\r
1272 *                       - TIM_EncoderMode_TI12: Counter counts on both TI1FP1 and\r
1273 *                         TI2FP2 edges depending on the level of the other input.\r
1274 *                  - TIM_IC1Polarity: specifies the IC1 Polarity\r
1275 *                    This parmeter can be one of the following values:\r
1276 *                        - TIM_ICPolarity_Falling\r
1277 *                        - TIM_ICPolarity_Rising\r
1278 *                  - TIM_IC2Polarity: specifies the IC2 Polarity\r
1279 *                    This parmeter can be one of the following values:\r
1280 *                       - TIM_ICPolarity_Falling\r
1281 *                       - TIM_ICPolarity_Rising\r
1282 * Output         : None\r
1283 * Return         : None\r
1284 *******************************************************************************/\r
1285 void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, u16 TIM_EncoderMode,\r
1286                                 u16 TIM_IC1Polarity, u16 TIM_IC2Polarity)\r
1287 {\r
1288   u32 tmpsmcr = 0;\r
1289   u32 tmpccmr1 = 0;\r
1290   u32 tmpccer = 0;\r
1291     \r
1292   /* Check the parameters */\r
1293   assert(IS_TIM_ENCODER_MODE(TIM_EncoderMode));\r
1294   assert(IS_TIM_IC_POLARITY(TIM_IC1Polarity));\r
1295   assert(IS_TIM_IC_POLARITY(TIM_IC2Polarity));\r
1296 \r
1297   tmpsmcr = TIMx->SMCR;\r
1298   tmpccmr1 = TIMx->CCMR1;\r
1299   tmpccer = TIMx->CCER;\r
1300 \r
1301   /* Set the encoder Mode */\r
1302   tmpsmcr &= SMCR_SMS_Mask;\r
1303   tmpsmcr |= TIM_EncoderMode;\r
1304 \r
1305   /* Select the Capture Compare 1 and the Capture Compare 2 as input */\r
1306   tmpccmr1 &= CCMR_CC13S_Mask & CCMR_CC24S_Mask;\r
1307   tmpccmr1 |= CCMR_TI13Direct_Set | CCMR_TI24Direct_Set;\r
1308 \r
1309   /* Set the TI1 and the TI2 Polarities */\r
1310   tmpccer &= CCER_CC1P_Mask & CCER_CC2P_Mask;\r
1311   tmpccer |= (TIM_IC1Polarity | (u16)((u16)TIM_IC2Polarity << 4));\r
1312 \r
1313   TIMx->SMCR = (u16)tmpsmcr;\r
1314 \r
1315   TIMx->CCMR1 = (u16)tmpccmr1;\r
1316 \r
1317   TIMx->CCER = (u16)tmpccer;\r
1318 }\r
1319 \r
1320 /*******************************************************************************\r
1321 * Function Name  : TIM_GenerateEvent\r
1322 * Description    : Configures the TIMx event to be generate by software.\r
1323 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1324 *                  - TIM_EventSource: specifies the event source.\r
1325 *                    This parameter can be one or more of the following values:\r
1326 *                       - TIM_EventSource_Update: Timer update Event source\r
1327 *                       - TIM_EventSource_CC1: Timer Capture Compare 1 Event source\r
1328 *                       - TIM_EventSource_CC2: Timer Capture Compare 2 Event source\r
1329 *                       - TIM_EventSource_CC3: Timer Capture Compare 3 Event source\r
1330 *                       - TIM_EventSource_CC4: Timer Capture Compare 4 Event source\r
1331 *                       - TIM_EventSource_Trigger: Timer Trigger Event source\r
1332 * Output         : None\r
1333 * Return         : None\r
1334 *******************************************************************************/\r
1335 void TIM_GenerateEvent(TIM_TypeDef* TIMx, u16 TIM_EventSource)\r
1336 {\r
1337   /* Check the parameters */\r
1338   assert(IS_TIM_EVENT_SOURCE(TIM_EventSource));\r
1339 \r
1340   /* Set the event sources */\r
1341   TIMx->EGR |= TIM_EventSource;\r
1342 }\r
1343 \r
1344 /*******************************************************************************\r
1345 * Function Name  : TIM_OC1PolarityConfig\r
1346 * Description    : Configures the TIMx channel 1 polarity.\r
1347 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1348 *                  - TIM_OCPolarity: specifies the OC1 Polarity\r
1349 *                    This parmeter can be one of the following values:\r
1350 *                       - TIM_OCPolarity_High: Output Compare active high\r
1351 *                       - TIM_OCPolarity_Low: Output Compare active low\r
1352 * Output         : None\r
1353 * Return         : None\r
1354 *******************************************************************************/\r
1355 void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, u16 TIM_OCPolarity)\r
1356 {\r
1357   u32 tmpccer = 0;\r
1358 \r
1359   /* Check the parameters */\r
1360   assert(IS_TIM_OC_POLARITY(TIM_OCPolarity));\r
1361 \r
1362   tmpccer = TIMx->CCER;\r
1363 \r
1364   /* Set or Reset the CC1P Bit */\r
1365   tmpccer &= CCER_CC1P_Mask;\r
1366   tmpccer |= TIM_OCPolarity;\r
1367 \r
1368   TIMx->CCER = (u16)tmpccer;\r
1369 }\r
1370 \r
1371 /*******************************************************************************\r
1372 * Function Name  : TIM_OC2PolarityConfig\r
1373 * Description    : Configures the TIMx channel 2 polarity.\r
1374 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1375 *                  - TIM_OCPolarity: specifies the OC2 Polarity\r
1376 *                    This parmeter can be one of the following values:\r
1377 *                       - TIM_OCPolarity_High: Output Compare active high\r
1378 *                       - TIM_OCPolarity_Low: Output Compare active low\r
1379 * Output         : None\r
1380 * Return         : None\r
1381 *******************************************************************************/\r
1382 void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, u16 TIM_OCPolarity)\r
1383 {\r
1384   u32 tmpccer = 0;\r
1385 \r
1386   /* Check the parameters */\r
1387   assert(IS_TIM_OC_POLARITY(TIM_OCPolarity));\r
1388 \r
1389   tmpccer = TIMx->CCER;\r
1390 \r
1391   /* Set or Reset the CC2P Bit */\r
1392   tmpccer &= CCER_CC2P_Mask;\r
1393   tmpccer |= (u16)((u16)TIM_OCPolarity << 4);\r
1394 \r
1395   TIMx->CCER = (u16)tmpccer;\r
1396 }\r
1397 \r
1398 /*******************************************************************************\r
1399 * Function Name  : TIM_OC3PolarityConfig\r
1400 * Description    : Configures the TIMx channel 3 polarity.\r
1401 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1402 *                  - TIM_OCPolarity: specifies the OC3 Polarity\r
1403 *                    This parmeter can be one of the following values:\r
1404 *                       - TIM_OCPolarity_High: Output Compare active high\r
1405 *                       - TIM_OCPolarity_Low: Output Compare active low\r
1406 * Output         : None\r
1407 * Return         : None\r
1408 *******************************************************************************/\r
1409 void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, u16 TIM_OCPolarity)\r
1410 {\r
1411   u32 tmpccer = 0;\r
1412 \r
1413   /* Check the parameters */\r
1414   assert(IS_TIM_OC_POLARITY(TIM_OCPolarity));\r
1415 \r
1416   tmpccer = TIMx->CCER;\r
1417 \r
1418   /* Set or Reset the CC3P Bit */\r
1419   tmpccer &= CCER_CC3P_Mask;\r
1420   tmpccer |= (u16)((u16)TIM_OCPolarity << 8);\r
1421 \r
1422   TIMx->CCER = (u16)tmpccer;\r
1423 }\r
1424 \r
1425 /*******************************************************************************\r
1426 * Function Name  : TIM_OC4PolarityConfig\r
1427 * Description    : Configures the TIMx channel 4 polarity.\r
1428 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1429 *                  - TIM_OCPolarity: specifies the OC4 Polarity\r
1430 *                    This parmeter can be one of the following values:\r
1431 *                       - TIM_OCPolarity_High: Output Compare active high\r
1432 *                       - TIM_OCPolarity_Low: Output Compare active low\r
1433 * Output         : None\r
1434 * Return         : None\r
1435 *******************************************************************************/\r
1436 void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, u16 TIM_OCPolarity)\r
1437 {\r
1438   u32 tmpccer = 0;\r
1439 \r
1440   /* Check the parameters */\r
1441   assert(IS_TIM_OC_POLARITY(TIM_OCPolarity));\r
1442 \r
1443   tmpccer = TIMx->CCER;\r
1444 \r
1445   /* Set or Reset the CC4P Bit */\r
1446   tmpccer &= CCER_CC4P_Mask;\r
1447   tmpccer |= (u16)((u16)TIM_OCPolarity << 12);\r
1448 \r
1449   TIMx->CCER = (u16)tmpccer;\r
1450 }\r
1451 \r
1452 /*******************************************************************************\r
1453 * Function Name  : TIM_UpdateRequestConfig\r
1454 * Description    : Configures the TIMx Update Request Interrupt source.\r
1455 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1456 *                  - TIM_UpdateSource: specifies the Update source.\r
1457 *                    This parameter can be one of the following values:\r
1458 *                       - TIM_UpdateSource_Regular\r
1459 *                       - TIM_UpdateSource_Global\r
1460 * Output         : None\r
1461 * Return         : None\r
1462 *******************************************************************************/\r
1463 void TIM_UpdateRequestConfig(TIM_TypeDef* TIMx, u16 TIM_UpdateSource)\r
1464 {\r
1465   u32 tmpcr1 = 0;\r
1466 \r
1467   /* Check the parameters */\r
1468   assert(IS_TIM_UPDATE_SOURCE(TIM_UpdateSource));\r
1469 \r
1470   tmpcr1 = TIMx->CR1;\r
1471 \r
1472   if (TIM_UpdateSource == TIM_UpdateSource_Regular)\r
1473   {\r
1474     /* Set the URS Bit */\r
1475     tmpcr1 |= CR1_URS_Set;\r
1476   }\r
1477   else\r
1478   {\r
1479     /* Reset the URS Bit */\r
1480     tmpcr1 &= CR1_URS_Reset;\r
1481   }\r
1482   TIMx->CR1 = (u16)tmpcr1;\r
1483 }\r
1484 \r
1485 /*******************************************************************************\r
1486 * Function Name  : TIM_SelectHallSensor\r
1487 * Description    : Enables or disables the TIMx\92s Hall sensor interface.\r
1488 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1489 *                  - Newstate: new state of the TIMx Hall sensor interface.\r
1490 *                    This parameter can be: ENABLE or DISABLE.\r
1491 * Output         : None\r
1492 * Return         : None\r
1493 *******************************************************************************/\r
1494 void TIM_SelectHallSensor(TIM_TypeDef* TIMx, FunctionalState Newstate)\r
1495 {\r
1496   /* Check the parameters */\r
1497   assert(IS_FUNCTIONAL_STATE(Newstate));\r
1498 \r
1499   if (Newstate != DISABLE)\r
1500   {\r
1501     /* Set the TI1S Bit */\r
1502     TIMx->CR2 |= CR2_TI1S_Set;\r
1503   }\r
1504   else\r
1505   {\r
1506     /* Reset the TI1S Bit */\r
1507     TIMx->CR2 &= CR2_TI1S_Reset;\r
1508   }\r
1509 }\r
1510 \r
1511 /*******************************************************************************\r
1512 * Function Name  : TIM_SelectOnePulseMode\r
1513 * Description    : Selects the TIMx\92s One Pulse Mode.\r
1514 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1515 *                  - TIM_OPMode: specifies the OPM Mode to be used.\r
1516 *                    This parameter can be one of the following values:\r
1517 *                       - TIM_OPMode_Single\r
1518 *                       - TIM_OPMode_Repetitive\r
1519 * Output         : None\r
1520 * Return         : None\r
1521 *******************************************************************************/\r
1522 void TIM_SelectOnePulseMode(TIM_TypeDef* TIMx, u16 TIM_OPMode)\r
1523 {\r
1524   u32 tmpcr1 = 0;\r
1525 \r
1526   /* Check the parameters */\r
1527   assert(IS_TIM_OPM_MODE(TIM_OPMode));\r
1528 \r
1529   tmpcr1 = TIMx->CR1;\r
1530 \r
1531   /* Reset the OPM Bit */\r
1532   tmpcr1 &= CR1_OPM_Mask;\r
1533 \r
1534   /* Configure the OPM Mode */\r
1535   tmpcr1 |= TIM_OPMode;\r
1536 \r
1537   TIMx->CR1 = (u16)tmpcr1;\r
1538 }\r
1539 \r
1540 /*******************************************************************************\r
1541 * Function Name  : TIM_SelectOutputTrigger\r
1542 * Description    : Selects the TIMx Trigger Output Mode.\r
1543 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1544 *                  - TIM_TRGOSource: specifies the Trigger Output source.\r
1545 *                    This paramter can be one of the following values:\r
1546 *                       - TIM_TRGOSource_Reset\r
1547 *                       - TIM_TRGOSource_Enable\r
1548 *                       - TIM_TRGOSource_Update\r
1549 *                       - TIM_TRGOSource_OC1\r
1550 *                       - TIM_TRGOSource_OC1Ref\r
1551 *                       - TIM_TRGOSource_OC2Ref\r
1552 *                       - TIM_TRGOSource_OC3Ref\r
1553 *                       - TIM_TRGOSource_OC4Ref\r
1554 * Output         : None\r
1555 * Return         : None\r
1556 *******************************************************************************/\r
1557 void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, u16 TIM_TRGOSource)\r
1558 {\r
1559   u32 tmpcr2 = 0;\r
1560 \r
1561   /* Check the parameters */\r
1562   assert(IS_TIM_TRGO_SOURCE(TIM_TRGOSource));\r
1563 \r
1564   tmpcr2 = TIMx->CR2;\r
1565   /* Reset the MMS Bits */\r
1566   tmpcr2 &= CR2_MMS_Mask;\r
1567 \r
1568   /* Select the TRGO source */\r
1569   tmpcr2 |=  TIM_TRGOSource;\r
1570 \r
1571   TIMx->CR2 = (u16)tmpcr2;\r
1572 }\r
1573 \r
1574 /*******************************************************************************\r
1575 * Function Name  : TIM_SelectSlaveMode\r
1576 * Description    : Selects the TIMx Slave Mode.\r
1577 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1578 *                  - TIM_SlaveMode: specifies the Timer Slave Mode.\r
1579 *                    This paramter can be one of the following values:\r
1580 *                       - TIM_SlaveMode_Reset\r
1581 *                       - TIM_SlaveMode_Gated\r
1582 *                       - TIM_SlaveMode_Trigger\r
1583 *                       - TIM_SlaveMode_External1\r
1584 * Output         : None\r
1585 * Return         : None\r
1586 *******************************************************************************/\r
1587 void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, u16 TIM_SlaveMode)\r
1588 {\r
1589   u32 tmpsmcr = 0;\r
1590 \r
1591   /* Check the parameters */\r
1592   assert(IS_TIM_SLAVE_MODE(TIM_SlaveMode));\r
1593 \r
1594   tmpsmcr = TIMx->SMCR;\r
1595 \r
1596   /* Reset the SMS Bits */\r
1597   tmpsmcr &= SMCR_SMS_Mask;\r
1598 \r
1599   /* Select the Slave Mode */\r
1600   tmpsmcr |= TIM_SlaveMode;\r
1601 \r
1602   TIMx->SMCR = (u16)tmpsmcr;\r
1603 }\r
1604 \r
1605 /*******************************************************************************\r
1606 * Function Name  : TIM_SelectMasterSlaveMode\r
1607 * Description    : Sets or Resets the TIMx Master/Slave Mode.\r
1608 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1609 *                  - TIM_MasterSlaveMode: specifies the Timer Master Slave Mode.\r
1610 *                    This paramter can be one of the following values:\r
1611 *                       - TIM_MasterSlaveMode_Enable: synchronization between the\r
1612 *                         current timer and its slaves (through TRGO).\r
1613 *                       - TIM_MasterSlaveMode_Disable: No action\r
1614 * Output         : None\r
1615 * Return         : None\r
1616 *******************************************************************************/\r
1617 void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, u16 TIM_MasterSlaveMode)\r
1618 {\r
1619   u32 tmpsmcr = 0;\r
1620 \r
1621   /* Check the parameters */\r
1622   assert(IS_TIM_MSM_STATE(TIM_MasterSlaveMode));\r
1623 \r
1624   tmpsmcr = TIMx->SMCR;\r
1625 \r
1626   /* Set or Reset the MSM Bit */\r
1627   tmpsmcr &= SMCR_MSM_Mask;\r
1628   tmpsmcr |= TIM_MasterSlaveMode;\r
1629 \r
1630   TIMx->SMCR = (u16)tmpsmcr;\r
1631 }\r
1632 \r
1633 /*******************************************************************************\r
1634 * Function Name  : TIM_SetAutoreload\r
1635 * Description    : Sets the TIMx Autoreload Register value\r
1636 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1637 *                  - Autoreload: specifies the Autoreload register new value.\r
1638 * Output         : None\r
1639 * Return         : None\r
1640 *******************************************************************************/\r
1641 void TIM_SetAutoreload(TIM_TypeDef* TIMx, u16 Autoreload)\r
1642 {\r
1643   /* Set the Autoreload Register value */\r
1644   TIMx->ARR = Autoreload;\r
1645 }\r
1646 \r
1647 /*******************************************************************************\r
1648 * Function Name  : TIM_SetCompare1\r
1649 * Description    : Sets the TIMx Capture Compare1 Register value\r
1650 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1651 *                  - Compare1: specifies the Capture Compare1 register new value.\r
1652 * Output         : None\r
1653 * Return         : None\r
1654 *******************************************************************************/\r
1655 void TIM_SetCompare1(TIM_TypeDef* TIMx, u16 Compare1)\r
1656 {\r
1657   /* Set the Capture Compare1 Register value */\r
1658   TIMx->CCR1 = Compare1;\r
1659 }\r
1660 \r
1661 /*******************************************************************************\r
1662 * Function Name  : TIM_SetCompare2\r
1663 * Description    : Sets the TIMx Capture Compare2 Register value\r
1664 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1665 *                  - Compare2: specifies the Capture Compare2 register new value.\r
1666 * Output         : None\r
1667 * Return         : None\r
1668 *******************************************************************************/\r
1669 void TIM_SetCompare2(TIM_TypeDef* TIMx, u16 Compare2)\r
1670 {\r
1671   /* Set the Capture Compare2 Register value */\r
1672   TIMx->CCR2 = Compare2;\r
1673 }\r
1674 \r
1675 /*******************************************************************************\r
1676 * Function Name  : TIM_SetCompare3\r
1677 * Description    : Sets the TIMx Capture Compare3 Register value\r
1678 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1679 *                  - Compare3: specifies the Capture Compare3 register new value.\r
1680 * Output         : None\r
1681 * Return         : None\r
1682 *******************************************************************************/\r
1683 void TIM_SetCompare3(TIM_TypeDef* TIMx, u16 Compare3)\r
1684 {\r
1685   /* Set the Capture Compare3 Register value */\r
1686   TIMx->CCR3 = Compare3;\r
1687 }\r
1688 \r
1689 /*******************************************************************************\r
1690 * Function Name  : TIM_SetCompare4\r
1691 * Description    : Sets the TIMx Capture Compare4 Register value\r
1692 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1693 *                  - Compare4: specifies the Capture Compare4 register new value.\r
1694 * Output         : None\r
1695 * Return         : None\r
1696 *******************************************************************************/\r
1697 void TIM_SetCompare4(TIM_TypeDef* TIMx, u16 Compare4)\r
1698 {\r
1699   /* Set the Capture Compare4 Register value */\r
1700   TIMx->CCR4 = Compare4;\r
1701 }\r
1702 \r
1703 /*******************************************************************************\r
1704 * Function Name  : TIM_SetIC1Prescaler\r
1705 * Description    : Sets the TIMx Input Capture 1 prescaler.\r
1706 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1707 *                  - TIM_IC1Prescaler: specifies the Input Capture1 prescaler\r
1708 *                    new value.\r
1709 *                    This parameter can be one of the following values:\r
1710 *                       - TIM_ICPSC_DIV1: no prescaler\r
1711 *                       - TIM_ICPSC_DIV2: capture is done once every 2 events\r
1712 *                       - TIM_ICPSC_DIV4: capture is done once every 4 events\r
1713 *                       - TIM_ICPSC_DIV8: capture is done once every 8 events\r
1714 * Output         : None\r
1715 * Return         : None\r
1716 *******************************************************************************/\r
1717 void TIM_SetIC1Prescaler(TIM_TypeDef* TIMx, u16 TIM_IC1Prescaler)\r
1718 {\r
1719   u32 tmpccmr1 = 0;\r
1720 \r
1721   /* Check the parameters */\r
1722   assert(IS_TIM_IC_PRESCALER(TIM_IC1Prescaler));\r
1723 \r
1724   tmpccmr1 = TIMx->CCMR1;\r
1725 \r
1726   /* Reset the IC1PSC Bits */\r
1727   tmpccmr1 &= CCMR_IC13PSC_Mask;\r
1728 \r
1729   /* Set the IC1PSC value */\r
1730   tmpccmr1 |= TIM_IC1Prescaler;\r
1731 \r
1732   TIMx->CCMR1 = (u16)tmpccmr1;\r
1733 }\r
1734 \r
1735 /*******************************************************************************\r
1736 * Function Name  : TIM_SetIC2Prescaler\r
1737 * Description    : Sets the TIMx Input Capture 2 prescaler.\r
1738 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1739 *                  - TIM_IC2Prescaler: specifies the Input Capture2 prescaler\r
1740 *                    new value.\r
1741 *                    This parameter can be one of the following values:\r
1742 *                       - TIM_ICPSC_DIV1: no prescaler\r
1743 *                       - TIM_ICPSC_DIV2: capture is done once every 2 events\r
1744 *                       - TIM_ICPSC_DIV4: capture is done once every 4 events\r
1745 *                       - TIM_ICPSC_DIV8: capture is done once every 8 events\r
1746 * Output         : None\r
1747 * Return         : None\r
1748 *******************************************************************************/\r
1749 void TIM_SetIC2Prescaler(TIM_TypeDef* TIMx, u16 TIM_IC2Prescaler)\r
1750 {\r
1751   u32 tmpccmr1 = 0;\r
1752 \r
1753   /* Check the parameters */\r
1754   assert(IS_TIM_IC_PRESCALER(TIM_IC2Prescaler));\r
1755 \r
1756   tmpccmr1 = TIMx->CCMR1;\r
1757 \r
1758   /* Reset the IC2PSC Bits */\r
1759   tmpccmr1 &= CCMR_IC24PSC_Mask;\r
1760 \r
1761   /* Set the IC2PSC value */\r
1762   tmpccmr1 |= (u16)((u16)TIM_IC2Prescaler << 8);\r
1763 \r
1764   TIMx->CCMR1 = (u16)tmpccmr1;\r
1765 }\r
1766 \r
1767 /*******************************************************************************\r
1768 * Function Name  : TIM_SetIC3Prescaler\r
1769 * Description    : Sets the TIMx Input Capture 3 prescaler.\r
1770 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1771 *                  - TIM_IC3Prescaler: specifies the Input Capture3 prescaler\r
1772 *                    new value.\r
1773 *                    This parameter can be one of the following values:\r
1774 *                       - TIM_ICPSC_DIV1: no prescaler\r
1775 *                       - TIM_ICPSC_DIV2: capture is done once every 2 events\r
1776 *                       - TIM_ICPSC_DIV4: capture is done once every 4 events\r
1777 *                       - TIM_ICPSC_DIV8: capture is done once every 8 events\r
1778 * Output         : None\r
1779 * Return         : None\r
1780 *******************************************************************************/\r
1781 void TIM_SetIC3Prescaler(TIM_TypeDef* TIMx, u16 TIM_IC3Prescaler)\r
1782 {\r
1783   u32 tmpccmr2 = 0;\r
1784 \r
1785   /* Check the parameters */\r
1786   assert(IS_TIM_IC_PRESCALER(TIM_IC3Prescaler));\r
1787 \r
1788   tmpccmr2 = TIMx->CCMR2;\r
1789 \r
1790   /* Reset the IC3PSC Bits */\r
1791   tmpccmr2 &= CCMR_IC13PSC_Mask;\r
1792 \r
1793   /* Set the IC3PSC value */\r
1794   tmpccmr2 |= TIM_IC3Prescaler;\r
1795 \r
1796   TIMx->CCMR2 = (u16)tmpccmr2;\r
1797 }\r
1798 \r
1799 /*******************************************************************************\r
1800 * Function Name  : TIM_SetIC4Prescaler\r
1801 * Description    : Sets the TIMx Input Capture 4 prescaler.\r
1802 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1803 *                  - TIM_IC4Prescaler: specifies the Input Capture4 prescaler\r
1804 *                    new value.\r
1805 *                    This parameter can be one of the following values:\r
1806 *                      - TIM_ICPSC_DIV1: no prescaler\r
1807 *                      - TIM_ICPSC_DIV2: capture is done once every 2 events\r
1808 *                      - TIM_ICPSC_DIV4: capture is done once every 4 events\r
1809 *                      - TIM_ICPSC_DIV8: capture is done once every 8 events\r
1810 * Output         : None\r
1811 * Return         : None\r
1812 *******************************************************************************/\r
1813 void TIM_SetIC4Prescaler(TIM_TypeDef* TIMx, u16 TIM_IC4Prescaler)\r
1814 {\r
1815   u32 tmpccmr2 = 0;\r
1816    \r
1817   /* Check the parameters */\r
1818   assert(IS_TIM_IC_PRESCALER(TIM_IC4Prescaler));\r
1819 \r
1820   tmpccmr2 = TIMx->CCMR2;\r
1821 \r
1822   /* Reset the IC4PSC Bits */\r
1823   tmpccmr2 &= CCMR_IC24PSC_Mask;\r
1824 \r
1825   /* Set the IC4PSC value */\r
1826   tmpccmr2 |= (u16)((u16)TIM_IC4Prescaler << 8);\r
1827 \r
1828   TIMx->CCMR2 = (u16)tmpccmr2;\r
1829 }\r
1830 \r
1831 /*******************************************************************************\r
1832 * Function Name  : TIM_SetClockDivision\r
1833 * Description    : Sets the TIMx Clock Division value.\r
1834 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1835 *                  - TIM_CKD: specifies the clock division value.\r
1836 *                    This parameter can be one of the following value:\r
1837 *                       - TIM_CKD_DIV1: TDTS = Tck_tim\r
1838 *                       - TIM_CKD_DIV2: TDTS = 2*Tck_tim\r
1839 *                       - TIM_CKD_DIV4: TDTS = 4*Tck_tim\r
1840 * Output         : None\r
1841 * Return         : None\r
1842 *******************************************************************************/\r
1843 void TIM_SetClockDivision(TIM_TypeDef* TIMx, u16 TIM_CKD)\r
1844 {\r
1845   u32 tmpcr1 = 0;\r
1846 \r
1847   /* Check the parameters */\r
1848   assert(IS_TIM_CKD_DIV(TIM_CKD));\r
1849 \r
1850   tmpcr1 = TIMx->CR1;\r
1851 \r
1852   /* Reset the CKD Bits */\r
1853   tmpcr1 &= CR1_CKD_Mask;\r
1854 \r
1855   /* Set the CKD value */\r
1856   tmpcr1 |= TIM_CKD;\r
1857 \r
1858   TIMx->CR1 = (u16)tmpcr1;\r
1859 }\r
1860 \r
1861 /*******************************************************************************\r
1862 * Function Name  : TIM_GetCapture1\r
1863 * Description    : Gets the TIMx Input Capture 1 value.\r
1864 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1865 * Output         : None\r
1866 * Return         : Capture Compare 1 Register value.\r
1867 *******************************************************************************/\r
1868 u16 TIM_GetCapture1(TIM_TypeDef* TIMx)\r
1869 {\r
1870   /* Get the Capture 1 Register value */\r
1871   return TIMx->CCR1;\r
1872 }\r
1873 \r
1874 /*******************************************************************************\r
1875 * Function Name  : TIM_GetCapture2\r
1876 * Description    : Gets the TIMx Input Capture 2 value.\r
1877 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1878 * Output         : None\r
1879 * Return         : Capture Compare 2 Register value.\r
1880 *******************************************************************************/\r
1881 u16 TIM_GetCapture2(TIM_TypeDef* TIMx)\r
1882 {\r
1883   /* Get the Capture 2 Register value */\r
1884   return TIMx->CCR2;\r
1885 }\r
1886 \r
1887 /*******************************************************************************\r
1888 * Function Name  : TIM_GetCapture3\r
1889 * Description    : Gets the TIMx Input Capture 3 value.\r
1890 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1891 * Output         : None\r
1892 * Return         : Capture Compare 3 Register value.\r
1893 *******************************************************************************/\r
1894 u16 TIM_GetCapture3(TIM_TypeDef* TIMx)\r
1895 {\r
1896   /* Get the Capture 3 Register value */\r
1897   return TIMx->CCR3;\r
1898 }\r
1899 \r
1900 /*******************************************************************************\r
1901 * Function Name  : TIM_GetCapture4\r
1902 * Description    : Gets the TIMx Input Capture 4 value.\r
1903 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1904 * Output         : None\r
1905 * Return         : Capture Compare 4 Register value.\r
1906 *******************************************************************************/\r
1907 u16 TIM_GetCapture4(TIM_TypeDef* TIMx)\r
1908 {\r
1909   /* Get the Capture 4 Register value */\r
1910   return TIMx->CCR4;\r
1911 }\r
1912 \r
1913 /*******************************************************************************\r
1914 * Function Name  : TIM_GetCounter\r
1915 * Description    : Gets the TIMx Counter value.\r
1916 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1917 * Output         : None\r
1918 * Return         : Counter Register value.\r
1919 *******************************************************************************/\r
1920 u16 TIM_GetCounter(TIM_TypeDef* TIMx)\r
1921 {\r
1922   /* Get the Counter Register value */\r
1923   return TIMx->CNT;\r
1924 }\r
1925 \r
1926 /*******************************************************************************\r
1927 * Function Name  : TIM_GetPrescaler\r
1928 * Description    : Gets the TIMx Prescaler value.\r
1929 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1930 * Output         : None\r
1931 * Return         : Prescaler Register value.\r
1932 *******************************************************************************/\r
1933 u16 TIM_GetPrescaler(TIM_TypeDef* TIMx)\r
1934 {\r
1935   /* Get the Prescaler Register value */\r
1936   return TIMx->PSC;\r
1937 }\r
1938 \r
1939 /*******************************************************************************\r
1940 * Function Name  : TIM_GetFlagStatus\r
1941 * Description    : Checks whether the specified TIMx flag is set or not.\r
1942 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1943 *                  - TIM_FLAG: specifies the flag to check.\r
1944 *                    This parameter can be one of the following values:\r
1945 *                       - TIM_FLAG_Update: Timer update Flag\r
1946 *                       - TIM_FLAG_CC1: Timer Capture Compare 1 Flag\r
1947 *                       - TIM_FLAG_CC2: Timer Capture Compare 2 Flag\r
1948 *                       - TIM_FLAG_CC3: Timer Capture Compare 3 Flag\r
1949 *                       - TIM_FLAG_CC4: Timer Capture Compare 4 Flag\r
1950 *                       - TIM_FLAG_Trigger: Timer Trigger Flag\r
1951 *                       - TIM_FLAG_CC1OF: Timer Capture Compare 1 overcapture Flag\r
1952 *                       - TIM_FLAG_CC2OF: Timer Capture Compare 2 overcapture Flag\r
1953 *                       - TIM_FLAG_CC3OF: Timer Capture Compare 3 overcapture Flag\r
1954 *                       - TIM_FLAG_CC4OF: Timer Capture Compare 4 overcapture Flag\r
1955 * Output         : None\r
1956 * Return         : The new state of TIM_FLAG (SET or RESET).\r
1957 *******************************************************************************/\r
1958 FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, u16 TIM_FLAG)\r
1959 {\r
1960   FlagStatus bitstatus = RESET;\r
1961 \r
1962   /* Check the parameters */\r
1963   assert(IS_TIM_GET_FLAG(TIM_FLAG));\r
1964 \r
1965   if ((TIMx->SR & TIM_FLAG) != (u16)RESET )\r
1966   {\r
1967     bitstatus = SET;\r
1968   }\r
1969   else\r
1970   {\r
1971     bitstatus = RESET;\r
1972   }\r
1973   return bitstatus;\r
1974 }\r
1975 \r
1976 /*******************************************************************************\r
1977 * Function Name  : TIM_ClearFlag\r
1978 * Description    : Clears the TIMx's pending flags.\r
1979 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
1980 *                  - TIM_FLAG: specifies the flag bit to clear.\r
1981 *                    This parameter can be any combination of the following values:\r
1982 *                       - TIM_FLAG_Update: Timer update Flag\r
1983 *                       - TIM_FLAG_CC1: Timer Capture Compare 1 Flag\r
1984 *                       - TIM_FLAG_CC2: Timer Capture Compare 2 Flag\r
1985 *                       - TIM_FLAG_CC3: Timer Capture Compare 3 Flag\r
1986 *                       - TIM_FLAG_CC4: Timer Capture Compare 4 Flag\r
1987 *                       - TIM_FLAG_Trigger: Timer Trigger Flag\r
1988 *                       - TIM_FLAG_CC1OF: Timer Capture Compare 1 overcapture Flag\r
1989 *                       - TIM_FLAG_CC2OF: Timer Capture Compare 2 overcapture Flag\r
1990 *                       - TIM_FLAG_CC3OF: Timer Capture Compare 3 overcapture Flag\r
1991 *                       - TIM_FLAG_CC4OF: Timer Capture Compare 4 overcapture Flag\r
1992 * Output         : None\r
1993 * Return         : None\r
1994 *******************************************************************************/\r
1995 void TIM_ClearFlag(TIM_TypeDef* TIMx, u16 TIM_FLAG)\r
1996 {\r
1997   /* Check the parameters */\r
1998   assert(IS_TIM_CLEAR_FLAG(TIM_FLAG));\r
1999 \r
2000   /* Clear the flags */\r
2001   TIMx->SR &= (u16)~TIM_FLAG;\r
2002 }\r
2003 \r
2004 /*******************************************************************************\r
2005 * Function Name  : TIM_GetITStatus\r
2006 * Description    : Checks whether the TIMx interrupt has occurred or not.\r
2007 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
2008 *                  - TIM_IT: specifies the TIM interrupt source to check.\r
2009 *                    This parameter can be one of the following values:\r
2010 *                         - TIM_IT_Update: Timer update Interrupt source\r
2011 *                         - TIM_IT_CC1: Timer Capture Compare 1 Interrupt source\r
2012 *                         - TIM_IT_CC2: Timer Capture Compare 2 Interrupt source\r
2013 *                         - TIM_IT_CC3: Timer Capture Compare 3 Interrupt source\r
2014 *                         - TIM_IT_CC4: Timer Capture Compare 4 Interrupt source\r
2015 *                         - TIM_IT_Trigger: Timer Trigger Interrupt source\r
2016 * Output         : None\r
2017 * Return         : The new state of the TIM_IT(SET or RESET).\r
2018 *******************************************************************************/\r
2019 ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, u16 TIM_IT)\r
2020 {\r
2021   ITStatus bitstatus = RESET;\r
2022   \r
2023   u16 itstatus = 0x0, itenable = 0x0;\r
2024 \r
2025   /* Check the parameters */\r
2026   assert(IS_TIM_GET_IT(TIM_IT));\r
2027   \r
2028   itstatus = TIMx->SR & TIM_IT;\r
2029   \r
2030   itenable = TIMx->DIER & TIM_IT;\r
2031 \r
2032   if ((itstatus != (u16)RESET)  && (itenable != (u16)RESET))\r
2033   {\r
2034     bitstatus = SET;\r
2035   }\r
2036   else\r
2037   {\r
2038     bitstatus = RESET;\r
2039   }\r
2040   return bitstatus;\r
2041 }\r
2042 \r
2043 /*******************************************************************************\r
2044 * Function Name  : TIM_ClearITPendingBit\r
2045 * Description    : Clears the TIMx's interrupt pending bits.\r
2046 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
2047 *                  - TIM_IT: specifies the pending bit to clear.\r
2048 *                    This parameter can be any combination of the following values:\r
2049 *                       - TIM_IT_Update: Timer update Interrupt source\r
2050 *                       - TIM_IT_CC1: Timer Capture Compare 1 Interrupt source\r
2051 *                       - TIM_IT_CC2: Timer Capture Compare 2 Interrupt source\r
2052 *                       - TIM_IT_CC3: Timer Capture Compare 3 Interrupt source\r
2053 *                       - TIM_IT_CC4: Timer Capture Compare 4 Interrupt source\r
2054 *                       - TIM_IT_Trigger: Timer Trigger Interrupt source\r
2055 * Output         : None\r
2056 * Return         : None\r
2057 *******************************************************************************/\r
2058 void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, u16 TIM_IT)\r
2059 {\r
2060   /* Check the parameters */\r
2061   assert(IS_TIM_IT(TIM_IT));\r
2062   \r
2063   /* Clear the IT pending Bit */\r
2064   TIMx->SR &= (u16)~TIM_IT;\r
2065 }\r
2066 \r
2067 /*******************************************************************************\r
2068 * Function Name  : PWMInput_Config\r
2069 * Description    : Configures the TIM peripheral according to the specified\r
2070 *                  parameters in the TIM_ICInitStruct to measure an external PWM\r
2071 *                  signal.\r
2072 * Input          : - TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure\r
2073 *                    that contains the configuration information for the specified\r
2074 *                    TIM peripheral.\r
2075 * Output         : None\r
2076 * Return         : None\r
2077 *******************************************************************************/\r
2078 static void PWMI_Config(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)\r
2079 {\r
2080   u8 ICPolarity = TIM_ICPolarity_Rising;\r
2081   u8 ICSelection = TIM_ICSelection_DirectTI;\r
2082 \r
2083   /* Select the Opposite Input Polarity */\r
2084   if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)\r
2085   {\r
2086     ICPolarity = TIM_ICPolarity_Falling;\r
2087   }\r
2088   else\r
2089   {\r
2090     ICPolarity = TIM_ICPolarity_Rising;\r
2091   }\r
2092 \r
2093   /* Select the Opposite Input */\r
2094   if (TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)\r
2095   {\r
2096     ICSelection = TIM_ICSelection_IndirectTI;\r
2097   }\r
2098   else\r
2099   {\r
2100     ICSelection = TIM_ICSelection_DirectTI;\r
2101   }\r
2102 \r
2103   if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)\r
2104   {\r
2105     /* TI1 Configuration */\r
2106     TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,\r
2107                TIM_ICInitStruct->TIM_ICFilter);\r
2108 \r
2109     /* Set the Input Capture Prescaler value */\r
2110     TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);\r
2111 \r
2112     /* TI2 Configuration */\r
2113     TI2_Config(TIMx, ICPolarity, ICSelection, TIM_ICInitStruct->TIM_ICFilter);\r
2114 \r
2115     /* Set the Input Capture Prescaler value */\r
2116     TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);\r
2117   }\r
2118   else\r
2119   {      \r
2120     /* TI1 Configuration */\r
2121     TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,\r
2122                TIM_ICInitStruct->TIM_ICFilter);\r
2123 \r
2124     /* Set the Input Capture Prescaler value */\r
2125     TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);\r
2126 \r
2127     /* TI2 Configuration */\r
2128     TI1_Config(TIMx, ICPolarity, ICSelection, TIM_ICInitStruct->TIM_ICFilter);\r
2129 \r
2130     /* Set the Input Capture Prescaler value */\r
2131     TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);\r
2132   }\r
2133 }\r
2134 \r
2135 /*******************************************************************************\r
2136 * Function Name  : TI1_Config\r
2137 * Description    : Configure the TI1 as Input.\r
2138 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
2139 *                  - TIM_ICPolarity : The Input Polarity.\r
2140 *                    This parameter can be one of the following values:\r
2141 *                       - TIM_ICPolarity_Rising\r
2142 *                       - TIM_ICPolarity_Falling\r
2143 *                  - TIM_ICSelection: specifies the input to be used.\r
2144 *                    This parameter can be one of the following values:\r
2145 *                       - TIM_ICSelection_DirectTI: TIM Input 1 is selected to\r
2146 *                         be connected to IC1.\r
2147 *                       - TIM_ICSelection_IndirectTI: TIM Input 1 is selected to\r
2148 *                         be connected to IC2.\r
2149 *                       - TIM_ICSelection_TRGI: TIM Input 1 is selected to be\r
2150 *                         connected to TRGI.\r
2151 *                  - TIM_ICFilter: Specifies the Input Capture Filter.\r
2152 *                    This parameter must be a value between 0x00 and 0x0F.\r
2153 * Output         : None\r
2154 * Return         : None\r
2155 *******************************************************************************/\r
2156 static void TI1_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,\r
2157                        u8 TIM_ICFilter)\r
2158 {\r
2159   u32 tmpccmr1 = 0, tmpccer = 0;\r
2160 \r
2161   tmpccmr1 = TIMx->CCMR1;\r
2162   tmpccer = TIMx->CCER;\r
2163 \r
2164   /* Disable the Channel 1: Reset the CCE Bit */\r
2165   TIMx->CCER &= CCRE_CC1E_Reset;\r
2166 \r
2167   /* Select the Input and set the filter */\r
2168   tmpccmr1 &= CCMR_CC13S_Mask & CCMR_IC13F_Mask;\r
2169   tmpccmr1 |= TIM_ICSelection | (u16)((u16)TIM_ICFilter << 4);\r
2170 \r
2171   /* Select the Polarity  and set the CCE Bit */\r
2172   tmpccer &= CCER_CC1P_Mask & CCRE_CC1E_Mask;\r
2173   tmpccer |= TIM_ICPolarity | CCRE_CC1E_Set;\r
2174 \r
2175   TIMx->CCMR1 = 0x0000;\r
2176   TIMx->CCMR1 = (u16)tmpccmr1;\r
2177   TIMx->CCER = (u16)tmpccer;\r
2178 }\r
2179 \r
2180 /*******************************************************************************\r
2181 * Function Name  : TI2_Config\r
2182 * Description    : Configure the TI2 as Input.\r
2183 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
2184 *                  - TIM_ICPolarity : The Input Polarity.\r
2185 *                    This parameter can be one of the following values:\r
2186 *                       - TIM_ICPolarity_Rising\r
2187 *                       - TIM_ICPolarity_Falling\r
2188 *                  - TIM_ICSelection: specifies the input to be used.\r
2189 *                    This parameter can be one of the following values:\r
2190 *                       - TIM_ICSelection_DirectTI: TIM Input 2 is selected to\r
2191 *                         be connected to IC2.\r
2192 *                       - TIM_ICSelection_IndirectTI: TIM Input 2 is selected to\r
2193 *                         be connected to IC1.\r
2194 *                       - TIM_ICSelection_TRGI: TIM Input 2 is selected to be\r
2195 *                         connected to TRGI.\r
2196 *                  - TIM_ICFilter: Specifies the Input Capture Filter.\r
2197 *                    This parameter must be a value between 0x00 and 0x0F.\r
2198 * Output         : None\r
2199 * Return         : None\r
2200 *******************************************************************************/\r
2201 static void TI2_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,\r
2202                        u8 TIM_ICFilter)\r
2203 {\r
2204   u32 tmpccmr1 = 0, tmpccer = 0, tmp = 0;\r
2205 \r
2206   tmpccmr1 = TIMx->CCMR1;\r
2207   tmpccer = TIMx->CCER;\r
2208   tmp = (u16)((u16)TIM_ICPolarity << 4);\r
2209 \r
2210   /* Disable the Channel 2: Reset the CCE Bit */\r
2211   TIMx->CCER &= CCRE_CC2E_Reset;\r
2212 \r
2213   /* Select the Input and set the filter */\r
2214   tmpccmr1 &= CCMR_CC24S_Mask & CCMR_IC24F_Mask;\r
2215   tmpccmr1 |= (u16)((u16)TIM_ICFilter << 12);\r
2216   tmpccmr1 |= (u16)((u16)TIM_ICSelection << 8);\r
2217 \r
2218   /* Select the Polarity  and set the CCE Bit */\r
2219   tmpccer &= CCER_CC2P_Mask & CCRE_CC2E_Mask;\r
2220   tmpccer |=  tmp | CCRE_CC2E_Set;\r
2221 \r
2222   TIMx->CCMR1 = (u16)tmpccmr1 ;\r
2223   TIMx->CCER = (u16)tmpccer;\r
2224 }\r
2225 \r
2226 /*******************************************************************************\r
2227 * Function Name  : TI3_Config\r
2228 * Description    : Configure the TI3 as Input.\r
2229 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
2230 *                  - TIM_ICPolarity : The Input Polarity.\r
2231 *                    This parameter can be one of the following values:\r
2232 *                       - TIM_ICPolarity_Rising\r
2233 *                       - TIM_ICPolarity_Falling\r
2234 *                  - TIM_ICSelection: specifies the input to be used.\r
2235 *                    This parameter can be one of the following values:\r
2236 *                       - TIM_ICSelection_DirectTI: TIM Input 3 is selected to\r
2237 *                         be connected to IC3.\r
2238 *                       - TIM_ICSelection_IndirectTI: TIM Input 3 is selected to\r
2239 *                         be connected to IC4.\r
2240 *                       - TIM_ICSelection_TRGI: TIM Input 3 is selected to be\r
2241 *                         connected to TRGI.\r
2242 *                  - TIM_ICFilter: Specifies the Input Capture Filter.\r
2243 *                    This parameter must be a value between 0x00 and 0x0F.\r
2244 * Output         : None\r
2245 * Return         : None\r
2246 *******************************************************************************/\r
2247 static void TI3_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,\r
2248                        u8 TIM_ICFilter)\r
2249 {\r
2250   u32 tmpccmr2 = 0, tmpccer = 0, tmp = 0;\r
2251 \r
2252   tmpccmr2 = TIMx->CCMR2;\r
2253   tmpccer = TIMx->CCER;\r
2254   tmp = (u16)((u16)TIM_ICPolarity << 8);\r
2255 \r
2256   /* Disable the Channel 3: Reset the CCE Bit */\r
2257   TIMx->CCER &= CCRE_CC3E_Reset;\r
2258 \r
2259   /* Select the Input and set the filter */\r
2260   tmpccmr2 &= CCMR_CC13S_Mask & CCMR_IC13F_Mask;\r
2261   tmpccmr2 |= TIM_ICSelection | (u16)((u16)TIM_ICFilter << 4);\r
2262 \r
2263   /* Select the Polarity  and set the CCE Bit */\r
2264   tmpccer &= CCER_CC1P_Mask & CCRE_CC1E_Mask;\r
2265   tmpccer |= tmp | CCRE_CC3E_Set;\r
2266 \r
2267   TIMx->CCMR2 = (u16)tmpccmr2;\r
2268   TIMx->CCER = (u16)tmpccer;\r
2269 }\r
2270 \r
2271 /*******************************************************************************\r
2272 * Function Name  : TI4_Config\r
2273 * Description    : Configure the TI1 as Input.\r
2274 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
2275 *                  - TIM_ICPolarity : The Input Polarity.\r
2276 *                    This parameter can be one of the following values:\r
2277 *                       - TIM_ICPolarity_Rising\r
2278 *                       - TIM_ICPolarity_Falling\r
2279 *                  - TIM_ICSelection: specifies the input to be used.\r
2280 *                    This parameter can be one of the following values:\r
2281 *                       - TIM_ICSelection_DirectTI: TIM Input 4 is selected to\r
2282 *                         be connected to IC4.\r
2283 *                       - TIM_ICSelection_IndirectTI: TIM Input 4 is selected to\r
2284 *                         be connected to IC3.\r
2285 *                       - TIM_ICSelection_TRGI: TIM Input 4 is selected to be\r
2286 *                         connected to TRGI.\r
2287 *                  - TIM_ICFilter: Specifies the Input Capture Filter.\r
2288 *                    This parameter must be a value between 0x00 and 0x0F.\r
2289 * Output         : None\r
2290 * Return         : None\r
2291 *******************************************************************************/\r
2292 static void TI4_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection,\r
2293                        u8 TIM_ICFilter)\r
2294 {\r
2295   u32 tmpccmr2 = 0, tmpccer = 0, tmp = 0;\r
2296 \r
2297   tmpccmr2 = TIMx->CCMR2;\r
2298   tmpccer = TIMx->CCER;\r
2299   tmp = (u16)((u16)TIM_ICPolarity << 12);\r
2300 \r
2301   /* Disable the Channel 4: Reset the CCE Bit */\r
2302   TIMx->CCER &= CCRE_CC4E_Reset;\r
2303 \r
2304   /* Select the Input and set the filter */\r
2305   tmpccmr2 &= CCMR_CC24S_Mask & CCMR_IC24F_Mask;\r
2306   tmpccmr2 |= (u16)((u16)TIM_ICSelection << 8) | (u16)((u16)TIM_ICFilter << 12);\r
2307 \r
2308   /* Select the Polarity  and set the CCE Bit */\r
2309   tmpccer &= CCER_CC4P_Mask & CCRE_CC4E_Mask;\r
2310   tmpccer |= tmp | CCRE_CC4E_Set;\r
2311 \r
2312   TIMx->CCMR2 = (u16)tmpccmr2;\r
2313   TIMx->CCER = (u16)tmpccer ;\r
2314 }\r
2315 \r
2316 /*******************************************************************************\r
2317 * Function Name  : ETR_Config\r
2318 * Description    : Configure the External Trigger\r
2319 * Input          : - TIMx: where x can be 2, 3 or 4 to select the TIM peripheral.\r
2320 *                  - TIM_ExtTRGPrescaler: The external Trigger Prescaler.\r
2321 *                    This parameter can be one of the following values:\r
2322 *                       - TIM_ExtTRGPSC_OFF\r
2323 *                       - TIM_ExtTRGPSC_DIV2\r
2324 *                       - TIM_ExtTRGPSC_DIV4\r
2325 *                       - TIM_ExtTRGPSC_DIV8\r
2326 *                  - TIM_ExtTRGPolarity: The external Trigger Polarity.\r
2327 *                    This parameter can be one of the following values:\r
2328 *                       - TIM_ExtTRGPolarity_Inverted\r
2329 *                       - TIM_ExtTRGPolarity_NonInverted\r
2330 *                  - ExtTRGFilter: External Trigger Filter.\r
2331 *                    This parameter must be a value between 0x00 and 0x0F.\r
2332 * Output         : None\r
2333 * Return         : None\r
2334 *******************************************************************************/\r
2335 static void ETR_Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, u16 TIM_ExtTRGPolarity,\r
2336                        u8 ExtTRGFilter)\r
2337 {\r
2338   u32 tmpsmcr = 0;\r
2339 \r
2340   tmpsmcr = TIMx->SMCR;\r
2341 \r
2342   /* Set the Prescaler, the Filter value and the Polarity */\r
2343   tmpsmcr &= SMCR_ETR_Mask;\r
2344   tmpsmcr |= TIM_ExtTRGPrescaler | TIM_ExtTRGPolarity | (u16)((u16)ExtTRGFilter << 8);\r
2345 \r
2346   TIMx->SMCR = (u16)tmpsmcr;\r
2347 }\r
2348 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/\r