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