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