]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_tim.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_tim.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_tim.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   TIM HAL module driver.\r
8   *          This file provides firmware functions to manage the following \r
9   *          functionalities of the Timer (TIM) peripheral:\r
10   *           + Time Base Initialization\r
11   *           + Time Base Start\r
12   *           + Time Base Start Interruption\r
13   *           + Time Base Start DMA\r
14   *           + Time Output Compare/PWM Initialization\r
15   *           + Time Output Compare/PWM Channel Configuration\r
16   *           + Time Output Compare/PWM  Start\r
17   *           + Time Output Compare/PWM  Start Interruption\r
18   *           + Time Output Compare/PWM Start DMA\r
19   *           + Time Input Capture Initialization\r
20   *           + Time Input Capture Channel Configuration\r
21   *           + Time Input Capture Start\r
22   *           + Time Input Capture Start Interruption \r
23   *           + Time Input Capture Start DMA\r
24   *           + Time One Pulse Initialization\r
25   *           + Time One Pulse Channel Configuration\r
26   *           + Time One Pulse Start \r
27   *           + Time Encoder Interface Initialization\r
28   *           + Time Encoder Interface Start\r
29   *           + Time Encoder Interface Start Interruption\r
30   *           + Time Encoder Interface Start DMA\r
31   *           + Commutation Event configuration with Interruption and DMA\r
32   *           + Time OCRef clear configuration\r
33   *           + Time External Clock configuration\r
34   @verbatim \r
35   ==============================================================================\r
36                       ##### TIMER Generic features #####\r
37   ==============================================================================\r
38   [..] The Timer features include: \r
39        (#) 16-bit up, down, up/down auto-reload counter.\r
40        (#) 16-bit programmable prescaler allowing dividing (also on the fly) the \r
41            counter clock frequency either by any factor between 1 and 65536.\r
42        (#) Up to 4 independent channels for:\r
43            (++) Input Capture\r
44            (++) Output Compare\r
45            (++) PWM generation (Edge and Center-aligned Mode)\r
46            (++) One-pulse mode output               \r
47    \r
48                         ##### How to use this driver #####\r
49   ==============================================================================\r
50     [..]\r
51      (#) Initialize the TIM low level resources by implementing the following functions \r
52          depending from feature used :\r
53            (++) Time Base : HAL_TIM_Base_MspInit() \r
54            (++) Input Capture : HAL_TIM_IC_MspInit()\r
55            (++) Output Compare : HAL_TIM_OC_MspInit()\r
56            (++) PWM generation : HAL_TIM_PWM_MspInit()\r
57            (++) One-pulse mode output : HAL_TIM_OnePulse_MspInit()\r
58            (++) Encoder mode output : HAL_TIM_Encoder_MspInit()\r
59            \r
60      (#) Initialize the TIM low level resources :\r
61         (##) Enable the TIM interface clock using __TIMx_CLK_ENABLE(); \r
62         (##) TIM pins configuration\r
63             (+++) Enable the clock for the TIM GPIOs using the following function:\r
64                  __GPIOx_CLK_ENABLE();   \r
65             (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();  \r
66 \r
67      (#) The external Clock can be configured, if needed (the default clock is the \r
68          internal clock from the APBx), using the following function:\r
69          HAL_TIM_ConfigClockSource, the clock configuration should be done before \r
70          any start function.\r
71   \r
72      (#) Configure the TIM in the desired functioning mode using one of the \r
73          initialization function of this driver:\r
74          (++) HAL_TIM_Base_Init: to use the Timer to generate a simple time base\r
75          (++) HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an \r
76               Output Compare signal.\r
77          (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a \r
78               PWM signal.\r
79          (++) HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an \r
80               external signal.\r
81          (++) HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer \r
82               in One Pulse Mode.\r
83          (++) HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.\r
84          \r
85      (#) Activate the TIM peripheral using one of the start functions depending from the feature used: \r
86            (++) Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()\r
87            (++) Input Capture :  HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()\r
88            (++) Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()\r
89            (++) PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()\r
90            (++) One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()\r
91            (++) Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(), HAL_TIM_Encoder_Start_IT().\r
92 \r
93      (#) The DMA Burst is managed with the two following functions:\r
94          HAL_TIM_DMABurst_WriteStart()\r
95          HAL_TIM_DMABurst_ReadStart()\r
96   \r
97   @endverbatim\r
98   ******************************************************************************\r
99   * @attention\r
100   *\r
101   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
102   *\r
103   * Redistribution and use in source and binary forms, with or without modification,\r
104   * are permitted provided that the following conditions are met:\r
105   *   1. Redistributions of source code must retain the above copyright notice,\r
106   *      this list of conditions and the following disclaimer.\r
107   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
108   *      this list of conditions and the following disclaimer in the documentation\r
109   *      and/or other materials provided with the distribution.\r
110   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
111   *      may be used to endorse or promote products derived from this software\r
112   *      without specific prior written permission.\r
113   *\r
114   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
115   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
116   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
117   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
118   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
119   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
120   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
121   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
122   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
123   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
124   *\r
125   ******************************************************************************\r
126   */ \r
127 \r
128 /* Includes ------------------------------------------------------------------*/\r
129 #include "stm32f7xx_hal.h"\r
130 \r
131 /** @addtogroup STM32F7xx_HAL_Driver\r
132   * @{\r
133   */\r
134 \r
135 /** @defgroup TIM TIM\r
136   * @brief TIM HAL module driver\r
137   * @{\r
138   */\r
139 \r
140 #ifdef HAL_TIM_MODULE_ENABLED\r
141 \r
142 /* Private typedef -----------------------------------------------------------*/\r
143 /* Private define ------------------------------------------------------------*/\r
144 /* Private macro -------------------------------------------------------------*/\r
145 /* Private variables ---------------------------------------------------------*/\r
146 /** @addtogroup TIM_Private_Functions\r
147   * @{\r
148   */\r
149 /* Private function prototypes -----------------------------------------------*/\r
150 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);\r
151 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,\r
152                        uint32_t TIM_ICFilter);\r
153 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);\r
154 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,\r
155                        uint32_t TIM_ICFilter);\r
156 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,\r
157                        uint32_t TIM_ICFilter);\r
158 \r
159 static void TIM_ITRx_SetConfig(TIM_TypeDef* TIMx, uint16_t TIM_ITRx);\r
160 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma);\r
161 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);\r
162 static void TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,\r
163                                      TIM_SlaveConfigTypeDef * sSlaveConfig);\r
164 /**\r
165   * @}\r
166   */\r
167   \r
168 /* Exported functions --------------------------------------------------------*/\r
169 /** @defgroup TIM_Exported_Functions TIM Exported Functions\r
170   * @{\r
171   */\r
172 \r
173 /** @defgroup TIM_Exported_Functions_Group1 Time Base functions \r
174  *  @brief    Time Base functions \r
175  *\r
176 @verbatim    \r
177   ==============================================================================\r
178               ##### Time Base functions #####\r
179   ==============================================================================\r
180   [..]  \r
181     This section provides functions allowing to:\r
182     (+) Initialize and configure the TIM base. \r
183     (+) De-initialize the TIM base.\r
184     (+) Start the Time Base.\r
185     (+) Stop the Time Base.\r
186     (+) Start the Time Base and enable interrupt.\r
187     (+) Stop the Time Base and disable interrupt.\r
188     (+) Start the Time Base and enable DMA transfer.\r
189     (+) Stop the Time Base and disable DMA transfer.\r
190  \r
191 @endverbatim\r
192   * @{\r
193   */\r
194 /**\r
195   * @brief  Initializes the TIM Time base Unit according to the specified\r
196   *         parameters in the TIM_HandleTypeDef and create the associated handle.\r
197   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
198   *                the configuration information for TIM module.\r
199   * @retval HAL status\r
200   */\r
201 HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)\r
202\r
203   /* Check the TIM handle allocation */\r
204   if(htim == NULL)\r
205   {\r
206     return HAL_ERROR;\r
207   }\r
208   \r
209   /* Check the parameters */\r
210   assert_param(IS_TIM_INSTANCE(htim->Instance)); \r
211   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));\r
212   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));\r
213   \r
214   if(htim->State == HAL_TIM_STATE_RESET)\r
215   {  \r
216     /* Init the low level hardware : GPIO, CLOCK, NVIC */\r
217     HAL_TIM_Base_MspInit(htim);\r
218   }\r
219   \r
220   /* Set the TIM state */\r
221   htim->State= HAL_TIM_STATE_BUSY;\r
222   \r
223   /* Set the Time Base configuration */\r
224   TIM_Base_SetConfig(htim->Instance, &htim->Init); \r
225   \r
226   /* Initialize the TIM state*/\r
227   htim->State= HAL_TIM_STATE_READY;\r
228   \r
229   return HAL_OK;\r
230 }\r
231 \r
232 /**\r
233   * @brief  DeInitializes the TIM Base peripheral \r
234   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
235   *                the configuration information for TIM module.\r
236   * @retval HAL status\r
237   */\r
238 HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)\r
239 {  \r
240   /* Check the parameters */\r
241   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
242 \r
243   htim->State = HAL_TIM_STATE_BUSY;\r
244    \r
245   /* Disable the TIM Peripheral Clock */\r
246   __HAL_TIM_DISABLE(htim);\r
247     \r
248   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */\r
249   HAL_TIM_Base_MspDeInit(htim);\r
250   \r
251   /* Change TIM state */  \r
252   htim->State = HAL_TIM_STATE_RESET; \r
253   \r
254   /* Release Lock */\r
255   __HAL_UNLOCK(htim);\r
256 \r
257   return HAL_OK;\r
258 }\r
259 \r
260 /**\r
261   * @brief  Initializes the TIM Base MSP.\r
262   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
263   *                the configuration information for TIM module.\r
264   * @retval None\r
265   */\r
266 __weak void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)\r
267 {\r
268   /* NOTE : This function Should not be modified, when the callback is needed,\r
269             the HAL_TIM_Base_MspInit could be implemented in the user file\r
270    */\r
271 }\r
272 \r
273 /**\r
274   * @brief  DeInitializes TIM Base MSP.\r
275   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
276   *                the configuration information for TIM module.\r
277   * @retval None\r
278   */\r
279 __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)\r
280 {\r
281   /* NOTE : This function Should not be modified, when the callback is needed,\r
282             the HAL_TIM_Base_MspDeInit could be implemented in the user file\r
283    */\r
284 }\r
285 \r
286 /**\r
287   * @brief  Starts the TIM Base generation.\r
288   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
289   *                the configuration information for TIM module.\r
290   * @retval HAL status\r
291   */\r
292 HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)\r
293 {\r
294   /* Check the parameters */\r
295   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
296   \r
297   /* Set the TIM state */\r
298   htim->State= HAL_TIM_STATE_BUSY;\r
299   \r
300   /* Enable the Peripheral */\r
301   __HAL_TIM_ENABLE(htim);\r
302   \r
303   /* Change the TIM state*/\r
304   htim->State= HAL_TIM_STATE_READY;\r
305   \r
306   /* Return function status */\r
307   return HAL_OK;\r
308 }\r
309 \r
310 /**\r
311   * @brief  Stops the TIM Base generation.\r
312   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
313   *                the configuration information for TIM module.\r
314   * @retval HAL status\r
315   */\r
316 HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)\r
317 {\r
318   /* Check the parameters */\r
319   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
320   \r
321   /* Set the TIM state */\r
322   htim->State= HAL_TIM_STATE_BUSY;\r
323   \r
324   /* Disable the Peripheral */\r
325   __HAL_TIM_DISABLE(htim);\r
326   \r
327   /* Change the TIM state*/\r
328   htim->State= HAL_TIM_STATE_READY;\r
329   \r
330   /* Return function status */\r
331   return HAL_OK;\r
332 }\r
333 \r
334 /**\r
335   * @brief  Starts the TIM Base generation in interrupt mode.\r
336   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
337   *                the configuration information for TIM module.\r
338   * @retval HAL status\r
339   */\r
340 HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)\r
341 {\r
342   /* Check the parameters */\r
343   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
344   \r
345   /* Enable the TIM Update interrupt */\r
346   __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);\r
347       \r
348   /* Enable the Peripheral */\r
349   __HAL_TIM_ENABLE(htim);\r
350       \r
351   /* Return function status */\r
352   return HAL_OK;\r
353 }\r
354 \r
355 /**\r
356   * @brief  Stops the TIM Base generation in interrupt mode.\r
357   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
358   *                the configuration information for TIM module.\r
359   * @retval HAL status\r
360   */\r
361 HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)\r
362 {\r
363   /* Check the parameters */\r
364   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
365   /* Disable the TIM Update interrupt */\r
366   __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);\r
367       \r
368   /* Disable the Peripheral */\r
369   __HAL_TIM_DISABLE(htim);\r
370     \r
371   /* Return function status */\r
372   return HAL_OK;\r
373 }\r
374 \r
375 /**\r
376   * @brief  Starts the TIM Base generation in DMA mode.\r
377   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
378   *                the configuration information for TIM module.\r
379   * @param  pData: The source Buffer address.\r
380   * @param  Length: The length of data to be transferred from memory to peripheral.\r
381   * @retval HAL status\r
382   */\r
383 HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)\r
384 {\r
385   /* Check the parameters */\r
386   assert_param(IS_TIM_DMA_INSTANCE(htim->Instance)); \r
387   \r
388   if((htim->State == HAL_TIM_STATE_BUSY))\r
389   {\r
390      return HAL_BUSY;\r
391   }\r
392   else if((htim->State == HAL_TIM_STATE_READY))\r
393   {\r
394     if((pData == 0 ) && (Length > 0)) \r
395     {\r
396       return HAL_ERROR;                                    \r
397     }\r
398     else\r
399     {\r
400       htim->State = HAL_TIM_STATE_BUSY;\r
401     }\r
402   }  \r
403   /* Set the DMA Period elapsed callback */\r
404   htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;\r
405      \r
406   /* Set the DMA error callback */\r
407   htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = HAL_TIM_DMAError ;\r
408   \r
409   /* Enable the DMA Stream */\r
410   HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR, Length);\r
411   \r
412   /* Enable the TIM Update DMA request */\r
413   __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);\r
414 \r
415   /* Enable the Peripheral */\r
416   __HAL_TIM_ENABLE(htim);  \r
417   \r
418   /* Return function status */\r
419   return HAL_OK;\r
420 }\r
421 \r
422 /**\r
423   * @brief  Stops the TIM Base generation in DMA mode.\r
424   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
425   *                the configuration information for TIM module.\r
426   * @retval HAL status\r
427   */\r
428 HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)\r
429 {\r
430   /* Check the parameters */\r
431   assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));\r
432   \r
433   /* Disable the TIM Update DMA request */\r
434   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);\r
435       \r
436   /* Disable the Peripheral */\r
437   __HAL_TIM_DISABLE(htim);\r
438     \r
439   /* Change the htim state */\r
440   htim->State = HAL_TIM_STATE_READY;\r
441       \r
442   /* Return function status */\r
443   return HAL_OK;\r
444 }\r
445 \r
446 /**\r
447   * @}\r
448   */\r
449   \r
450 /** @defgroup TIM_Exported_Functions_Group2 Time Output Compare functions \r
451  *  @brief    Time Output Compare functions \r
452  *\r
453 @verbatim    \r
454   ==============================================================================\r
455                   ##### Time Output Compare functions #####\r
456   ==============================================================================\r
457   [..]\r
458     This section provides functions allowing to:\r
459     (+) Initialize and configure the TIM Output Compare. \r
460     (+) De-initialize the TIM Output Compare.\r
461     (+) Start the Time Output Compare.\r
462     (+) Stop the Time Output Compare.\r
463     (+) Start the Time Output Compare and enable interrupt.\r
464     (+) Stop the Time Output Compare and disable interrupt.\r
465     (+) Start the Time Output Compare and enable DMA transfer.\r
466     (+) Stop the Time Output Compare and disable DMA transfer.\r
467  \r
468 @endverbatim\r
469   * @{\r
470   */\r
471 /**\r
472   * @brief  Initializes the TIM Output Compare according to the specified\r
473   *         parameters in the TIM_HandleTypeDef and create the associated handle.\r
474   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
475   *                the configuration information for TIM module.\r
476   * @retval HAL status\r
477   */\r
478 HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef* htim)\r
479 {\r
480   /* Check the TIM handle allocation */\r
481   if(htim == NULL)\r
482   {\r
483     return HAL_ERROR;\r
484   }\r
485 \r
486   /* Check the parameters */\r
487   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
488   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));\r
489   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));\r
490  \r
491   if(htim->State == HAL_TIM_STATE_RESET)\r
492   {   \r
493     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */\r
494     HAL_TIM_OC_MspInit(htim);\r
495   }\r
496   \r
497   /* Set the TIM state */\r
498   htim->State= HAL_TIM_STATE_BUSY;\r
499   \r
500   /* Init the base time for the Output Compare */  \r
501   TIM_Base_SetConfig(htim->Instance,  &htim->Init); \r
502   \r
503   /* Initialize the TIM state*/\r
504   htim->State= HAL_TIM_STATE_READY;\r
505   \r
506   return HAL_OK;\r
507 }\r
508 \r
509 /**\r
510   * @brief  DeInitializes the TIM peripheral \r
511   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
512   *                the configuration information for TIM module.\r
513   * @retval HAL status\r
514   */\r
515 HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)\r
516 {\r
517   /* Check the parameters */\r
518   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
519   \r
520    htim->State = HAL_TIM_STATE_BUSY;\r
521    \r
522   /* Disable the TIM Peripheral Clock */\r
523   __HAL_TIM_DISABLE(htim);\r
524   \r
525   /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */\r
526   HAL_TIM_OC_MspDeInit(htim);\r
527     \r
528   /* Change TIM state */  \r
529   htim->State = HAL_TIM_STATE_RESET; \r
530 \r
531   /* Release Lock */\r
532   __HAL_UNLOCK(htim);\r
533 \r
534   return HAL_OK;\r
535 }\r
536 \r
537 /**\r
538   * @brief  Initializes the TIM Output Compare MSP.\r
539   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
540   *                the configuration information for TIM module.\r
541   * @retval None\r
542   */\r
543 __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)\r
544 {\r
545   /* NOTE : This function Should not be modified, when the callback is needed,\r
546             the HAL_TIM_OC_MspInit could be implemented in the user file\r
547    */\r
548 }\r
549 \r
550 /**\r
551   * @brief  DeInitializes TIM Output Compare MSP.\r
552   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
553   *                the configuration information for TIM module.\r
554   * @retval None\r
555   */\r
556 __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)\r
557 {\r
558   /* NOTE : This function Should not be modified, when the callback is needed,\r
559             the HAL_TIM_OC_MspDeInit could be implemented in the user file\r
560    */\r
561 }\r
562 \r
563 /**\r
564   * @brief  Starts the TIM Output Compare signal generation.\r
565   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
566   *                the configuration information for TIM module.  \r
567   * @param  Channel: TIM Channel to be enabled.\r
568   *          This parameter can be one of the following values:\r
569   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
570   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
571   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
572   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected   \r
573   * @retval HAL status\r
574   */\r
575 HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)\r
576 {\r
577   /* Check the parameters */\r
578   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
579   \r
580   /* Enable the Output compare channel */\r
581   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
582   \r
583   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
584   {\r
585     /* Enable the main output */\r
586     __HAL_TIM_MOE_ENABLE(htim);\r
587   }\r
588   \r
589   /* Enable the Peripheral */\r
590   __HAL_TIM_ENABLE(htim); \r
591   \r
592   /* Return function status */\r
593   return HAL_OK;\r
594 }\r
595 \r
596 /**\r
597   * @brief  Stops the TIM Output Compare signal generation.\r
598   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
599   *                the configuration information for TIM module.\r
600   * @param  Channel: TIM Channel to be disabled.\r
601   *          This parameter can be one of the following values:\r
602   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
603   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
604   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
605   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
606   * @retval HAL status\r
607   */\r
608 HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)\r
609 {\r
610   /* Check the parameters */\r
611   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
612   \r
613   /* Disable the Output compare channel */\r
614   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);\r
615   \r
616   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
617   {\r
618     /* Disable the Main Output */\r
619     __HAL_TIM_MOE_DISABLE(htim);\r
620   }  \r
621   \r
622   /* Disable the Peripheral */\r
623   __HAL_TIM_DISABLE(htim);  \r
624   \r
625   /* Return function status */\r
626   return HAL_OK;\r
627 }  \r
628 \r
629 /**\r
630   * @brief  Starts the TIM Output Compare signal generation in interrupt mode.\r
631   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
632   *                the configuration information for TIM module.\r
633   * @param  Channel: TIM Channel to be enabled.\r
634   *          This parameter can be one of the following values:\r
635   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
636   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
637   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
638   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
639   * @retval HAL status\r
640   */\r
641 HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
642 {\r
643   /* Check the parameters */\r
644   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
645   \r
646   switch (Channel)\r
647   {\r
648     case TIM_CHANNEL_1:\r
649     {       \r
650       /* Enable the TIM Capture/Compare 1 interrupt */\r
651       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
652     }\r
653     break;\r
654     \r
655     case TIM_CHANNEL_2:\r
656     {\r
657       /* Enable the TIM Capture/Compare 2 interrupt */\r
658       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);\r
659     }\r
660     break;\r
661     \r
662     case TIM_CHANNEL_3:\r
663     {\r
664       /* Enable the TIM Capture/Compare 3 interrupt */\r
665       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);\r
666     }\r
667     break;\r
668     \r
669     case TIM_CHANNEL_4:\r
670     {\r
671       /* Enable the TIM Capture/Compare 4 interrupt */\r
672       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);\r
673     }\r
674     break;\r
675     \r
676     default:\r
677     break;\r
678   } \r
679 \r
680   /* Enable the Output compare channel */\r
681   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
682   \r
683   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
684   {\r
685     /* Enable the main output */\r
686     __HAL_TIM_MOE_ENABLE(htim);\r
687   }\r
688 \r
689   /* Enable the Peripheral */\r
690   __HAL_TIM_ENABLE(htim);\r
691   \r
692   /* Return function status */\r
693   return HAL_OK;\r
694 }\r
695 \r
696 /**\r
697   * @brief  Stops the TIM Output Compare signal generation in interrupt mode.\r
698   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
699   *                the configuration information for TIM module.\r
700   * @param  Channel: TIM Channel to be disabled.\r
701   *          This parameter can be one of the following values:\r
702   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
703   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
704   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
705   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
706   * @retval HAL status\r
707   */\r
708 HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
709 {\r
710   /* Check the parameters */\r
711   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
712   \r
713   switch (Channel)\r
714   {\r
715     case TIM_CHANNEL_1:\r
716     {       \r
717       /* Disable the TIM Capture/Compare 1 interrupt */\r
718       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
719     }\r
720     break;\r
721     \r
722     case TIM_CHANNEL_2:\r
723     {\r
724       /* Disable the TIM Capture/Compare 2 interrupt */\r
725       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
726     }\r
727     break;\r
728     \r
729     case TIM_CHANNEL_3:\r
730     {\r
731       /* Disable the TIM Capture/Compare 3 interrupt */\r
732       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);\r
733     }\r
734     break;\r
735     \r
736     case TIM_CHANNEL_4:\r
737     {\r
738       /* Disable the TIM Capture/Compare 4 interrupt */\r
739       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);\r
740     }\r
741     break;\r
742     \r
743     default:\r
744     break; \r
745   } \r
746   \r
747   /* Disable the Output compare channel */\r
748   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE); \r
749   \r
750   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
751   {\r
752     /* Disable the Main Output */\r
753     __HAL_TIM_MOE_DISABLE(htim);\r
754   }\r
755   \r
756   /* Disable the Peripheral */\r
757   __HAL_TIM_DISABLE(htim);  \r
758   \r
759   /* Return function status */\r
760   return HAL_OK;\r
761 }\r
762 \r
763 /**\r
764   * @brief  Starts the TIM Output Compare signal generation in DMA mode.\r
765   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
766   *                the configuration information for TIM module.\r
767   * @param  Channel: TIM Channel to be enabled.\r
768   *          This parameter can be one of the following values:\r
769   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
770   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
771   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
772   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
773   * @param  pData: The source Buffer address.\r
774   * @param  Length: The length of data to be transferred from memory to TIM peripheral\r
775   * @retval HAL status\r
776   */\r
777 HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)\r
778 {\r
779   /* Check the parameters */\r
780   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
781   \r
782   if((htim->State == HAL_TIM_STATE_BUSY))\r
783   {\r
784      return HAL_BUSY;\r
785   }\r
786   else if((htim->State == HAL_TIM_STATE_READY))\r
787   {\r
788     if(((uint32_t)pData == 0 ) && (Length > 0)) \r
789     {\r
790       return HAL_ERROR;                                    \r
791     }\r
792     else\r
793     {\r
794       htim->State = HAL_TIM_STATE_BUSY;\r
795     }\r
796   }    \r
797   switch (Channel)\r
798   {\r
799     case TIM_CHANNEL_1:\r
800     {      \r
801       /* Set the DMA Period elapsed callback */\r
802       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
803      \r
804       /* Set the DMA error callback */\r
805       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
806       \r
807       /* Enable the DMA Stream */\r
808       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);\r
809       \r
810       /* Enable the TIM Capture/Compare 1 DMA request */\r
811       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);\r
812     }\r
813     break;\r
814     \r
815     case TIM_CHANNEL_2:\r
816     {\r
817       /* Set the DMA Period elapsed callback */\r
818       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
819      \r
820       /* Set the DMA error callback */\r
821       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;\r
822       \r
823       /* Enable the DMA Stream */\r
824       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);\r
825       \r
826       /* Enable the TIM Capture/Compare 2 DMA request */\r
827       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);\r
828     }\r
829     break;\r
830     \r
831     case TIM_CHANNEL_3:\r
832     {\r
833       /* Set the DMA Period elapsed callback */\r
834       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
835      \r
836       /* Set the DMA error callback */\r
837       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;\r
838       \r
839       /* Enable the DMA Stream */\r
840       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);\r
841       \r
842       /* Enable the TIM Capture/Compare 3 DMA request */\r
843       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);\r
844     }\r
845     break;\r
846     \r
847     case TIM_CHANNEL_4:\r
848     {\r
849      /* Set the DMA Period elapsed callback */\r
850       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
851      \r
852       /* Set the DMA error callback */\r
853       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;\r
854       \r
855       /* Enable the DMA Stream */\r
856       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);\r
857       \r
858       /* Enable the TIM Capture/Compare 4 DMA request */\r
859       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);\r
860     }\r
861     break;\r
862     \r
863     default:\r
864     break;\r
865   }\r
866 \r
867   /* Enable the Output compare channel */\r
868   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
869   \r
870   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
871   {\r
872     /* Enable the main output */\r
873     __HAL_TIM_MOE_ENABLE(htim);\r
874   }  \r
875   \r
876   /* Enable the Peripheral */\r
877   __HAL_TIM_ENABLE(htim); \r
878   \r
879   /* Return function status */\r
880   return HAL_OK;\r
881 }\r
882 \r
883 /**\r
884   * @brief  Stops the TIM Output Compare signal generation in DMA mode.\r
885   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
886   *                the configuration information for TIM module.\r
887   * @param  Channel: TIM Channel to be disabled.\r
888   *          This parameter can be one of the following values:\r
889   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
890   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
891   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
892   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
893   * @retval HAL status\r
894   */\r
895 HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)\r
896 {\r
897   /* Check the parameters */\r
898   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
899   \r
900   switch (Channel)\r
901   {\r
902     case TIM_CHANNEL_1:\r
903     {       \r
904       /* Disable the TIM Capture/Compare 1 DMA request */\r
905       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);\r
906     }\r
907     break;\r
908     \r
909     case TIM_CHANNEL_2:\r
910     {\r
911       /* Disable the TIM Capture/Compare 2 DMA request */\r
912       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);\r
913     }\r
914     break;\r
915     \r
916     case TIM_CHANNEL_3:\r
917     {\r
918       /* Disable the TIM Capture/Compare 3 DMA request */\r
919       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);\r
920     }\r
921     break;\r
922     \r
923     case TIM_CHANNEL_4:\r
924     {\r
925       /* Disable the TIM Capture/Compare 4 interrupt */\r
926       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);\r
927     }\r
928     break;\r
929     \r
930     default:\r
931     break;\r
932   } \r
933   \r
934   /* Disable the Output compare channel */\r
935   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);\r
936   \r
937   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
938   {\r
939     /* Disable the Main Output */\r
940     __HAL_TIM_MOE_DISABLE(htim);\r
941   }\r
942   \r
943   /* Disable the Peripheral */\r
944   __HAL_TIM_DISABLE(htim);\r
945   \r
946   /* Change the htim state */\r
947   htim->State = HAL_TIM_STATE_READY;\r
948   \r
949   /* Return function status */\r
950   return HAL_OK;\r
951 }\r
952 \r
953 /**\r
954   * @}\r
955   */\r
956 \r
957 /** @defgroup TIM_Exported_Functions_Group3 Time PWM functions \r
958  *  @brief    Time PWM functions \r
959  *\r
960 @verbatim    \r
961   ==============================================================================\r
962                           ##### Time PWM functions #####\r
963   ==============================================================================\r
964   [..]  \r
965     This section provides functions allowing to:\r
966     (+) Initialize and configure the TIM OPWM. \r
967     (+) De-initialize the TIM PWM.\r
968     (+) Start the Time PWM.\r
969     (+) Stop the Time PWM.\r
970     (+) Start the Time PWM and enable interrupt.\r
971     (+) Stop the Time PWM and disable interrupt.\r
972     (+) Start the Time PWM and enable DMA transfer.\r
973     (+) Stop the Time PWM and disable DMA transfer.\r
974  \r
975 @endverbatim\r
976   * @{\r
977   */\r
978 /**\r
979   * @brief  Initializes the TIM PWM Time Base according to the specified\r
980   *         parameters in the TIM_HandleTypeDef and create the associated handle.\r
981   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
982   *                the configuration information for TIM module.\r
983   * @retval HAL status\r
984   */\r
985 HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)\r
986 {\r
987   /* Check the TIM handle allocation */\r
988   if(htim == NULL)\r
989   {\r
990     return HAL_ERROR;\r
991   }\r
992 \r
993   /* Check the parameters */\r
994   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
995   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));\r
996   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));\r
997 \r
998   if(htim->State == HAL_TIM_STATE_RESET)\r
999   {\r
1000     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */\r
1001     HAL_TIM_PWM_MspInit(htim);\r
1002   }\r
1003 \r
1004   /* Set the TIM state */\r
1005   htim->State= HAL_TIM_STATE_BUSY;  \r
1006   \r
1007   /* Init the base time for the PWM */  \r
1008   TIM_Base_SetConfig(htim->Instance, &htim->Init); \r
1009    \r
1010   /* Initialize the TIM state*/\r
1011   htim->State= HAL_TIM_STATE_READY;\r
1012   \r
1013   return HAL_OK;\r
1014 }  \r
1015 \r
1016 /**\r
1017   * @brief  DeInitializes the TIM peripheral \r
1018   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1019   *                the configuration information for TIM module.\r
1020   * @retval HAL status\r
1021   */\r
1022 HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)\r
1023 {\r
1024   /* Check the parameters */\r
1025   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
1026   \r
1027   htim->State = HAL_TIM_STATE_BUSY;\r
1028   \r
1029   /* Disable the TIM Peripheral Clock */\r
1030   __HAL_TIM_DISABLE(htim);\r
1031     \r
1032   /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */\r
1033   HAL_TIM_PWM_MspDeInit(htim);\r
1034     \r
1035   /* Change TIM state */  \r
1036   htim->State = HAL_TIM_STATE_RESET; \r
1037 \r
1038   /* Release Lock */\r
1039   __HAL_UNLOCK(htim);\r
1040 \r
1041   return HAL_OK;\r
1042 }\r
1043 \r
1044 /**\r
1045   * @brief  Initializes the TIM PWM MSP.\r
1046   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1047   *                the configuration information for TIM module.\r
1048   * @retval None\r
1049   */\r
1050 __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)\r
1051 {\r
1052   /* NOTE : This function Should not be modified, when the callback is needed,\r
1053             the HAL_TIM_PWM_MspInit could be implemented in the user file\r
1054    */\r
1055 }\r
1056 \r
1057 /**\r
1058   * @brief  DeInitializes TIM PWM MSP.\r
1059   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1060   *                the configuration information for TIM module.\r
1061   * @retval None\r
1062   */\r
1063 __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)\r
1064 {\r
1065   /* NOTE : This function Should not be modified, when the callback is needed,\r
1066             the HAL_TIM_PWM_MspDeInit could be implemented in the user file\r
1067    */\r
1068 }\r
1069 \r
1070 /**\r
1071   * @brief  Starts the PWM signal generation.\r
1072   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1073   *                the configuration information for TIM module.\r
1074   * @param  Channel: TIM Channels to be enabled.\r
1075   *          This parameter can be one of the following values:\r
1076   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1077   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1078   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1079   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1080   * @retval HAL status\r
1081   */\r
1082 HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)\r
1083 {\r
1084   /* Check the parameters */\r
1085   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1086 \r
1087   /* Enable the Capture compare channel */\r
1088   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
1089   \r
1090   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
1091   {\r
1092     /* Enable the main output */\r
1093     __HAL_TIM_MOE_ENABLE(htim);\r
1094   }\r
1095     \r
1096   /* Enable the Peripheral */\r
1097   __HAL_TIM_ENABLE(htim);\r
1098   \r
1099   /* Return function status */\r
1100   return HAL_OK;\r
1101\r
1102 \r
1103 /**\r
1104   * @brief  Stops the PWM signal generation.\r
1105   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1106   *                the configuration information for TIM module.\r
1107   * @param  Channel: TIM Channels to be disabled.\r
1108   *          This parameter can be one of the following values:\r
1109   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1110   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1111   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1112   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1113   * @retval HAL status\r
1114   */\r
1115 HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)\r
1116\r
1117   /* Check the parameters */\r
1118   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1119     \r
1120   /* Disable the Capture compare channel */\r
1121   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);\r
1122   \r
1123   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
1124   {\r
1125     /* Disable the Main Output */\r
1126     __HAL_TIM_MOE_DISABLE(htim);\r
1127   }\r
1128   \r
1129   /* Disable the Peripheral */\r
1130   __HAL_TIM_DISABLE(htim);\r
1131   \r
1132   /* Change the htim state */\r
1133   htim->State = HAL_TIM_STATE_READY;\r
1134   \r
1135   /* Return function status */\r
1136   return HAL_OK;\r
1137\r
1138 \r
1139 /**\r
1140   * @brief  Starts the PWM signal generation in interrupt mode.\r
1141   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1142   *                the configuration information for TIM module.\r
1143   * @param  Channel: TIM Channel to be disabled.\r
1144   *          This parameter can be one of the following values:\r
1145   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1146   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1147   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1148   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1149   * @retval HAL status\r
1150   */\r
1151 HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
1152 {\r
1153   /* Check the parameters */\r
1154   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1155   \r
1156   switch (Channel)\r
1157   {\r
1158     case TIM_CHANNEL_1:\r
1159     {       \r
1160       /* Enable the TIM Capture/Compare 1 interrupt */\r
1161       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
1162     }\r
1163     break;\r
1164     \r
1165     case TIM_CHANNEL_2:\r
1166     {\r
1167       /* Enable the TIM Capture/Compare 2 interrupt */\r
1168       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);\r
1169     }\r
1170     break;\r
1171     \r
1172     case TIM_CHANNEL_3:\r
1173     {\r
1174       /* Enable the TIM Capture/Compare 3 interrupt */\r
1175       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);\r
1176     }\r
1177     break;\r
1178     \r
1179     case TIM_CHANNEL_4:\r
1180     {\r
1181       /* Enable the TIM Capture/Compare 4 interrupt */\r
1182       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);\r
1183     }\r
1184     break;\r
1185     \r
1186     default:\r
1187     break;\r
1188   } \r
1189   \r
1190   /* Enable the Capture compare channel */\r
1191   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
1192   \r
1193   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
1194   {\r
1195     /* Enable the main output */\r
1196     __HAL_TIM_MOE_ENABLE(htim);\r
1197   }\r
1198 \r
1199   /* Enable the Peripheral */\r
1200   __HAL_TIM_ENABLE(htim);\r
1201   \r
1202   /* Return function status */\r
1203   return HAL_OK;\r
1204\r
1205 \r
1206 /**\r
1207   * @brief  Stops the PWM signal generation in interrupt mode.\r
1208   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1209   *                the configuration information for TIM module.\r
1210   * @param  Channel: TIM Channels to be disabled.\r
1211   *          This parameter can be one of the following values:\r
1212   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1213   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1214   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1215   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1216   * @retval HAL status\r
1217   */\r
1218 HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)\r
1219 {\r
1220   /* Check the parameters */\r
1221   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1222   \r
1223   switch (Channel)\r
1224   {\r
1225     case TIM_CHANNEL_1:\r
1226     {       \r
1227       /* Disable the TIM Capture/Compare 1 interrupt */\r
1228       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
1229     }\r
1230     break;\r
1231     \r
1232     case TIM_CHANNEL_2:\r
1233     {\r
1234       /* Disable the TIM Capture/Compare 2 interrupt */\r
1235       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
1236     }\r
1237     break;\r
1238     \r
1239     case TIM_CHANNEL_3:\r
1240     {\r
1241       /* Disable the TIM Capture/Compare 3 interrupt */\r
1242       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);\r
1243     }\r
1244     break;\r
1245     \r
1246     case TIM_CHANNEL_4:\r
1247     {\r
1248       /* Disable the TIM Capture/Compare 4 interrupt */\r
1249       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);\r
1250     }\r
1251     break;\r
1252     \r
1253     default:\r
1254     break; \r
1255   }\r
1256   \r
1257   /* Disable the Capture compare channel */\r
1258   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);\r
1259   \r
1260   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
1261   {\r
1262     /* Disable the Main Output */\r
1263     __HAL_TIM_MOE_DISABLE(htim);\r
1264   }\r
1265   \r
1266   /* Disable the Peripheral */\r
1267   __HAL_TIM_DISABLE(htim);\r
1268   \r
1269   /* Return function status */\r
1270   return HAL_OK;\r
1271\r
1272 \r
1273 /**\r
1274   * @brief  Starts the TIM PWM signal generation in DMA mode.\r
1275   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1276   *                the configuration information for TIM module.\r
1277   * @param  Channel: TIM Channels to be enabled.\r
1278   *          This parameter can be one of the following values:\r
1279   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1280   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1281   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1282   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1283   * @param  pData: The source Buffer address.\r
1284   * @param  Length: The length of data to be transferred from memory to TIM peripheral\r
1285   * @retval HAL status\r
1286   */\r
1287 HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)\r
1288 {\r
1289   /* Check the parameters */\r
1290   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1291   \r
1292   if((htim->State == HAL_TIM_STATE_BUSY))\r
1293   {\r
1294      return HAL_BUSY;\r
1295   }\r
1296   else if((htim->State == HAL_TIM_STATE_READY))\r
1297   {\r
1298     if(((uint32_t)pData == 0 ) && (Length > 0)) \r
1299     {\r
1300       return HAL_ERROR;                                    \r
1301     }\r
1302     else\r
1303     {\r
1304       htim->State = HAL_TIM_STATE_BUSY;\r
1305     }\r
1306   }    \r
1307   switch (Channel)\r
1308   {\r
1309     case TIM_CHANNEL_1:\r
1310     {      \r
1311       /* Set the DMA Period elapsed callback */\r
1312       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
1313      \r
1314       /* Set the DMA error callback */\r
1315       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
1316       \r
1317       /* Enable the DMA Stream */\r
1318       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);\r
1319       \r
1320       /* Enable the TIM Capture/Compare 1 DMA request */\r
1321       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);\r
1322     }\r
1323     break;\r
1324     \r
1325     case TIM_CHANNEL_2:\r
1326     {\r
1327       /* Set the DMA Period elapsed callback */\r
1328       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
1329      \r
1330       /* Set the DMA error callback */\r
1331       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;\r
1332       \r
1333       /* Enable the DMA Stream */\r
1334       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);\r
1335       \r
1336       /* Enable the TIM Capture/Compare 2 DMA request */\r
1337       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);\r
1338     }\r
1339     break;\r
1340     \r
1341     case TIM_CHANNEL_3:\r
1342     {\r
1343       /* Set the DMA Period elapsed callback */\r
1344       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
1345      \r
1346       /* Set the DMA error callback */\r
1347       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;\r
1348       \r
1349       /* Enable the DMA Stream */\r
1350       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);\r
1351       \r
1352       /* Enable the TIM Output Capture/Compare 3 request */\r
1353       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);\r
1354     }\r
1355     break;\r
1356     \r
1357     case TIM_CHANNEL_4:\r
1358     {\r
1359      /* Set the DMA Period elapsed callback */\r
1360       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
1361      \r
1362       /* Set the DMA error callback */\r
1363       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;\r
1364       \r
1365       /* Enable the DMA Stream */\r
1366       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);\r
1367       \r
1368       /* Enable the TIM Capture/Compare 4 DMA request */\r
1369       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);\r
1370     }\r
1371     break;\r
1372     \r
1373     default:\r
1374     break;\r
1375   }\r
1376 \r
1377   /* Enable the Capture compare channel */\r
1378   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
1379     \r
1380   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
1381   {\r
1382     /* Enable the main output */\r
1383     __HAL_TIM_MOE_ENABLE(htim);\r
1384   }\r
1385   \r
1386   /* Enable the Peripheral */\r
1387   __HAL_TIM_ENABLE(htim); \r
1388   \r
1389   /* Return function status */\r
1390   return HAL_OK;\r
1391 }\r
1392 \r
1393 /**\r
1394   * @brief  Stops the TIM PWM signal generation in DMA mode.\r
1395   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1396   *                the configuration information for TIM module.\r
1397   * @param  Channel: TIM Channels to be disabled.\r
1398   *          This parameter can be one of the following values:\r
1399   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1400   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1401   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1402   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1403   * @retval HAL status\r
1404   */\r
1405 HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)\r
1406 {\r
1407   /* Check the parameters */\r
1408   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1409   \r
1410   switch (Channel)\r
1411   {\r
1412     case TIM_CHANNEL_1:\r
1413     {       \r
1414       /* Disable the TIM Capture/Compare 1 DMA request */\r
1415       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);\r
1416     }\r
1417     break;\r
1418     \r
1419     case TIM_CHANNEL_2:\r
1420     {\r
1421       /* Disable the TIM Capture/Compare 2 DMA request */\r
1422       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);\r
1423     }\r
1424     break;\r
1425     \r
1426     case TIM_CHANNEL_3:\r
1427     {\r
1428       /* Disable the TIM Capture/Compare 3 DMA request */\r
1429       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);\r
1430     }\r
1431     break;\r
1432     \r
1433     case TIM_CHANNEL_4:\r
1434     {\r
1435       /* Disable the TIM Capture/Compare 4 interrupt */\r
1436       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);\r
1437     }\r
1438     break;\r
1439     \r
1440     default:\r
1441     break;\r
1442   } \r
1443   \r
1444   /* Disable the Capture compare channel */\r
1445   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);\r
1446   \r
1447   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
1448   {\r
1449     /* Disable the Main Output */\r
1450     __HAL_TIM_MOE_DISABLE(htim);\r
1451   }\r
1452   \r
1453   /* Disable the Peripheral */\r
1454   __HAL_TIM_DISABLE(htim);\r
1455   \r
1456   /* Change the htim state */\r
1457   htim->State = HAL_TIM_STATE_READY;\r
1458   \r
1459   /* Return function status */\r
1460   return HAL_OK;\r
1461 }\r
1462 \r
1463 /**\r
1464   * @}\r
1465   */\r
1466 \r
1467 /** @defgroup TIM_Exported_Functions_Group4 Time Input Capture functions \r
1468  *  @brief    Time Input Capture functions \r
1469  *\r
1470 @verbatim    \r
1471   ==============================================================================\r
1472               ##### Time Input Capture functions #####\r
1473   ==============================================================================\r
1474  [..]  \r
1475    This section provides functions allowing to:\r
1476    (+) Initialize and configure the TIM Input Capture. \r
1477    (+) De-initialize the TIM Input Capture.\r
1478    (+) Start the Time Input Capture.\r
1479    (+) Stop the Time Input Capture.\r
1480    (+) Start the Time Input Capture and enable interrupt.\r
1481    (+) Stop the Time Input Capture and disable interrupt.\r
1482    (+) Start the Time Input Capture and enable DMA transfer.\r
1483    (+) Stop the Time Input Capture and disable DMA transfer.\r
1484  \r
1485 @endverbatim\r
1486   * @{\r
1487   */\r
1488 /**\r
1489   * @brief  Initializes the TIM Input Capture Time base according to the specified\r
1490   *         parameters in the TIM_HandleTypeDef and create the associated handle.\r
1491   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1492   *                the configuration information for TIM module.\r
1493   * @retval HAL status\r
1494   */\r
1495 HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)\r
1496 {\r
1497   /* Check the TIM handle allocation */\r
1498   if(htim == NULL)\r
1499   {\r
1500     return HAL_ERROR;\r
1501   }\r
1502 \r
1503   /* Check the parameters */\r
1504   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
1505   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));\r
1506   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision)); \r
1507 \r
1508   if(htim->State == HAL_TIM_STATE_RESET)\r
1509   {  \r
1510     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */\r
1511     HAL_TIM_IC_MspInit(htim);\r
1512   }\r
1513   \r
1514   /* Set the TIM state */\r
1515   htim->State= HAL_TIM_STATE_BUSY;   \r
1516   \r
1517   /* Init the base time for the input capture */  \r
1518   TIM_Base_SetConfig(htim->Instance, &htim->Init); \r
1519    \r
1520   /* Initialize the TIM state*/\r
1521   htim->State= HAL_TIM_STATE_READY;\r
1522   \r
1523   return HAL_OK;\r
1524 }\r
1525 \r
1526 /**\r
1527   * @brief  DeInitializes the TIM peripheral \r
1528   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1529   *                the configuration information for TIM module.\r
1530   * @retval HAL status\r
1531   */\r
1532 HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)\r
1533 {\r
1534   /* Check the parameters */\r
1535   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
1536 \r
1537   htim->State = HAL_TIM_STATE_BUSY;\r
1538   \r
1539   /* Disable the TIM Peripheral Clock */\r
1540   __HAL_TIM_DISABLE(htim);\r
1541     \r
1542   /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */\r
1543   HAL_TIM_IC_MspDeInit(htim);\r
1544     \r
1545   /* Change TIM state */  \r
1546   htim->State = HAL_TIM_STATE_RESET;\r
1547 \r
1548   /* Release Lock */\r
1549   __HAL_UNLOCK(htim);\r
1550 \r
1551   return HAL_OK;\r
1552 }\r
1553 \r
1554 /**\r
1555   * @brief  Initializes the TIM INput Capture MSP.\r
1556   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1557   *                the configuration information for TIM module.\r
1558   * @retval None\r
1559   */\r
1560 __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)\r
1561 {\r
1562   /* NOTE : This function Should not be modified, when the callback is needed,\r
1563             the HAL_TIM_IC_MspInit could be implemented in the user file\r
1564    */\r
1565 }\r
1566 \r
1567 /**\r
1568   * @brief  DeInitializes TIM Input Capture MSP.\r
1569   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1570   *                the configuration information for TIM module.\r
1571   * @retval None\r
1572   */\r
1573 __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)\r
1574 {\r
1575   /* NOTE : This function Should not be modified, when the callback is needed,\r
1576             the HAL_TIM_IC_MspDeInit could be implemented in the user file\r
1577    */\r
1578 }\r
1579 \r
1580 /**\r
1581   * @brief  Starts the TIM Input Capture measurement.\r
1582   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1583   *                the configuration information for TIM module.\r
1584   * @param  Channel: TIM Channels to be enabled.\r
1585   *          This parameter can be one of the following values:\r
1586   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1587   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1588   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1589   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1590   * @retval HAL status\r
1591   */\r
1592 HAL_StatusTypeDef HAL_TIM_IC_Start (TIM_HandleTypeDef *htim, uint32_t Channel)\r
1593 {\r
1594   /* Check the parameters */\r
1595   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1596   \r
1597   /* Enable the Input Capture channel */\r
1598   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
1599     \r
1600   /* Enable the Peripheral */\r
1601   __HAL_TIM_ENABLE(htim);  \r
1602 \r
1603   /* Return function status */\r
1604   return HAL_OK;  \r
1605\r
1606 \r
1607 /**\r
1608   * @brief  Stops the TIM Input Capture measurement.\r
1609   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1610   *                the configuration information for TIM module.\r
1611   * @param  Channel: TIM Channels to be disabled.\r
1612   *          This parameter can be one of the following values:\r
1613   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1614   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1615   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1616   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1617   * @retval HAL status\r
1618   */\r
1619 HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)\r
1620\r
1621   /* Check the parameters */\r
1622   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1623   \r
1624   /* Disable the Input Capture channel */\r
1625   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);\r
1626   \r
1627   /* Disable the Peripheral */\r
1628   __HAL_TIM_DISABLE(htim); \r
1629   \r
1630   /* Return function status */\r
1631   return HAL_OK;\r
1632 }\r
1633 \r
1634 /**\r
1635   * @brief  Starts the TIM Input Capture measurement in interrupt mode.\r
1636   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1637   *                the configuration information for TIM module.\r
1638   * @param  Channel: TIM Channels to be enabled.\r
1639   *          This parameter can be one of the following values:\r
1640   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1641   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1642   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1643   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1644   * @retval HAL status\r
1645   */\r
1646 HAL_StatusTypeDef HAL_TIM_IC_Start_IT (TIM_HandleTypeDef *htim, uint32_t Channel)\r
1647 {\r
1648   /* Check the parameters */\r
1649   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1650   \r
1651   switch (Channel)\r
1652   {\r
1653     case TIM_CHANNEL_1:\r
1654     {       \r
1655       /* Enable the TIM Capture/Compare 1 interrupt */\r
1656       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
1657     }\r
1658     break;\r
1659     \r
1660     case TIM_CHANNEL_2:\r
1661     {\r
1662       /* Enable the TIM Capture/Compare 2 interrupt */\r
1663       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);\r
1664     }\r
1665     break;\r
1666     \r
1667     case TIM_CHANNEL_3:\r
1668     {\r
1669       /* Enable the TIM Capture/Compare 3 interrupt */\r
1670       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);\r
1671     }\r
1672     break;\r
1673     \r
1674     case TIM_CHANNEL_4:\r
1675     {\r
1676       /* Enable the TIM Capture/Compare 4 interrupt */\r
1677       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);\r
1678     }\r
1679     break;\r
1680     \r
1681     default:\r
1682     break;\r
1683   }  \r
1684   /* Enable the Input Capture channel */\r
1685   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
1686     \r
1687   /* Enable the Peripheral */\r
1688   __HAL_TIM_ENABLE(htim);  \r
1689 \r
1690   /* Return function status */\r
1691   return HAL_OK;  \r
1692\r
1693 \r
1694 /**\r
1695   * @brief  Stops the TIM Input Capture measurement in interrupt mode.\r
1696   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1697   *                the configuration information for TIM module.\r
1698   * @param  Channel: TIM Channels to be disabled.\r
1699   *          This parameter can be one of the following values:\r
1700   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1701   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1702   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1703   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1704   * @retval HAL status\r
1705   */\r
1706 HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
1707 {\r
1708   /* Check the parameters */\r
1709   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1710   \r
1711   switch (Channel)\r
1712   {\r
1713     case TIM_CHANNEL_1:\r
1714     {       \r
1715       /* Disable the TIM Capture/Compare 1 interrupt */\r
1716       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
1717     }\r
1718     break;\r
1719     \r
1720     case TIM_CHANNEL_2:\r
1721     {\r
1722       /* Disable the TIM Capture/Compare 2 interrupt */\r
1723       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
1724     }\r
1725     break;\r
1726     \r
1727     case TIM_CHANNEL_3:\r
1728     {\r
1729       /* Disable the TIM Capture/Compare 3 interrupt */\r
1730       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);\r
1731     }\r
1732     break;\r
1733     \r
1734     case TIM_CHANNEL_4:\r
1735     {\r
1736       /* Disable the TIM Capture/Compare 4 interrupt */\r
1737       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);\r
1738     }\r
1739     break;\r
1740     \r
1741     default:\r
1742     break; \r
1743   } \r
1744   \r
1745   /* Disable the Input Capture channel */\r
1746   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE); \r
1747   \r
1748   /* Disable the Peripheral */\r
1749   __HAL_TIM_DISABLE(htim); \r
1750   \r
1751   /* Return function status */\r
1752   return HAL_OK;\r
1753 }\r
1754 \r
1755 /**\r
1756   * @brief  Starts the TIM Input Capture measurement on in DMA mode.\r
1757   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1758   *                the configuration information for TIM module.\r
1759   * @param  Channel: TIM Channels to be enabled.\r
1760   *          This parameter can be one of the following values:\r
1761   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1762   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1763   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1764   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1765   * @param  pData: The destination Buffer address.\r
1766   * @param  Length: The length of data to be transferred from TIM peripheral to memory.\r
1767   * @retval HAL status\r
1768   */\r
1769 HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)\r
1770 {\r
1771   /* Check the parameters */\r
1772   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1773   assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));\r
1774   \r
1775   if((htim->State == HAL_TIM_STATE_BUSY))\r
1776   {\r
1777      return HAL_BUSY;\r
1778   }\r
1779   else if((htim->State == HAL_TIM_STATE_READY))\r
1780   {\r
1781     if((pData == 0 ) && (Length > 0)) \r
1782     {\r
1783       return HAL_ERROR;                                    \r
1784     }\r
1785     else\r
1786     {\r
1787       htim->State = HAL_TIM_STATE_BUSY;\r
1788     }\r
1789   }  \r
1790    \r
1791   switch (Channel)\r
1792   {\r
1793     case TIM_CHANNEL_1:\r
1794     {\r
1795       /* Set the DMA Period elapsed callback */\r
1796       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
1797      \r
1798       /* Set the DMA error callback */\r
1799       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
1800       \r
1801       /* Enable the DMA Stream */\r
1802       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length); \r
1803       \r
1804       /* Enable the TIM Capture/Compare 1 DMA request */      \r
1805       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);\r
1806     }\r
1807     break;\r
1808     \r
1809     case TIM_CHANNEL_2:\r
1810     {\r
1811       /* Set the DMA Period elapsed callback */\r
1812       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
1813      \r
1814       /* Set the DMA error callback */\r
1815       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;\r
1816       \r
1817       /* Enable the DMA Stream */\r
1818       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData, Length);\r
1819       \r
1820       /* Enable the TIM Capture/Compare 2  DMA request */\r
1821       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);\r
1822     }\r
1823     break;\r
1824     \r
1825     case TIM_CHANNEL_3:\r
1826     {\r
1827       /* Set the DMA Period elapsed callback */\r
1828       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
1829      \r
1830       /* Set the DMA error callback */\r
1831       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;\r
1832       \r
1833       /* Enable the DMA Stream */\r
1834       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData, Length);\r
1835       \r
1836       /* Enable the TIM Capture/Compare 3  DMA request */\r
1837       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);\r
1838     }\r
1839     break;\r
1840     \r
1841     case TIM_CHANNEL_4:\r
1842     {\r
1843       /* Set the DMA Period elapsed callback */\r
1844       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
1845      \r
1846       /* Set the DMA error callback */\r
1847       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;\r
1848       \r
1849       /* Enable the DMA Stream */\r
1850       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData, Length);\r
1851       \r
1852       /* Enable the TIM Capture/Compare 4  DMA request */\r
1853       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);\r
1854     }\r
1855     break;\r
1856     \r
1857     default:\r
1858     break;\r
1859   }\r
1860 \r
1861   /* Enable the Input Capture channel */\r
1862   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);\r
1863    \r
1864   /* Enable the Peripheral */\r
1865   __HAL_TIM_ENABLE(htim); \r
1866   \r
1867   /* Return function status */\r
1868   return HAL_OK;\r
1869 }\r
1870 \r
1871 /**\r
1872   * @brief  Stops the TIM Input Capture measurement on in DMA mode.\r
1873   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1874   *                the configuration information for TIM module.\r
1875   * @param  Channel: TIM Channels to be disabled.\r
1876   *          This parameter can be one of the following values:\r
1877   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1878   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1879   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1880   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1881   * @retval HAL status\r
1882   */\r
1883 HAL_StatusTypeDef HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)\r
1884 {\r
1885   /* Check the parameters */\r
1886   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));\r
1887   assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));\r
1888   \r
1889   switch (Channel)\r
1890   {\r
1891     case TIM_CHANNEL_1:\r
1892     {       \r
1893       /* Disable the TIM Capture/Compare 1 DMA request */\r
1894       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);\r
1895     }\r
1896     break;\r
1897     \r
1898     case TIM_CHANNEL_2:\r
1899     {\r
1900       /* Disable the TIM Capture/Compare 2 DMA request */\r
1901       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);\r
1902     }\r
1903     break;\r
1904     \r
1905     case TIM_CHANNEL_3:\r
1906     {\r
1907       /* Disable the TIM Capture/Compare 3  DMA request */\r
1908       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);\r
1909     }\r
1910     break;\r
1911     \r
1912     case TIM_CHANNEL_4:\r
1913     {\r
1914       /* Disable the TIM Capture/Compare 4  DMA request */\r
1915       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);\r
1916     }\r
1917     break;\r
1918     \r
1919     default:\r
1920     break;\r
1921   }\r
1922 \r
1923   /* Disable the Input Capture channel */\r
1924   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);\r
1925   \r
1926   /* Disable the Peripheral */\r
1927   __HAL_TIM_DISABLE(htim); \r
1928   \r
1929   /* Change the htim state */\r
1930   htim->State = HAL_TIM_STATE_READY;\r
1931   \r
1932   /* Return function status */\r
1933   return HAL_OK;\r
1934 }  \r
1935 /**\r
1936   * @}\r
1937   */\r
1938   \r
1939 /** @defgroup TIM_Exported_Functions_Group5 Time One Pulse functions \r
1940  *  @brief    Time One Pulse functions \r
1941  *\r
1942 @verbatim    \r
1943   ==============================================================================\r
1944                         ##### Time One Pulse functions #####\r
1945   ==============================================================================\r
1946   [..]  \r
1947     This section provides functions allowing to:\r
1948     (+) Initialize and configure the TIM One Pulse. \r
1949     (+) De-initialize the TIM One Pulse.\r
1950     (+) Start the Time One Pulse.\r
1951     (+) Stop the Time One Pulse.\r
1952     (+) Start the Time One Pulse and enable interrupt.\r
1953     (+) Stop the Time One Pulse and disable interrupt.\r
1954     (+) Start the Time One Pulse and enable DMA transfer.\r
1955     (+) Stop the Time One Pulse and disable DMA transfer.\r
1956  \r
1957 @endverbatim\r
1958   * @{\r
1959   */\r
1960 /**\r
1961   * @brief  Initializes the TIM One Pulse Time Base according to the specified\r
1962   *         parameters in the TIM_HandleTypeDef and create the associated handle.\r
1963   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1964   *                the configuration information for TIM module.\r
1965   * @param  OnePulseMode: Select the One pulse mode.\r
1966   *         This parameter can be one of the following values:\r
1967   *            @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.\r
1968   *            @arg TIM_OPMODE_REPETITIVE: Repetitive pulses will be generated.\r
1969   * @retval HAL status\r
1970   */\r
1971 HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)\r
1972 {\r
1973   /* Check the TIM handle allocation */\r
1974   if(htim == NULL)\r
1975   {\r
1976     return HAL_ERROR;\r
1977   }\r
1978 \r
1979   /* Check the parameters */\r
1980   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
1981   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));\r
1982   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));\r
1983   assert_param(IS_TIM_OPM_MODE(OnePulseMode));\r
1984   \r
1985   if(htim->State == HAL_TIM_STATE_RESET)\r
1986   {   \r
1987     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */\r
1988     HAL_TIM_OnePulse_MspInit(htim);\r
1989   }\r
1990   \r
1991   /* Set the TIM state */\r
1992   htim->State= HAL_TIM_STATE_BUSY;  \r
1993   \r
1994   /* Configure the Time base in the One Pulse Mode */\r
1995   TIM_Base_SetConfig(htim->Instance, &htim->Init);\r
1996   \r
1997   /* Reset the OPM Bit */\r
1998   htim->Instance->CR1 &= ~TIM_CR1_OPM;\r
1999 \r
2000   /* Configure the OPM Mode */\r
2001   htim->Instance->CR1 |= OnePulseMode;\r
2002    \r
2003   /* Initialize the TIM state*/\r
2004   htim->State= HAL_TIM_STATE_READY;\r
2005   \r
2006   return HAL_OK;\r
2007 }\r
2008 \r
2009 /**\r
2010   * @brief  DeInitializes the TIM One Pulse  \r
2011   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2012   *                the configuration information for TIM module.\r
2013   * @retval HAL status\r
2014   */\r
2015 HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)\r
2016 {\r
2017   /* Check the parameters */\r
2018   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
2019   \r
2020   htim->State = HAL_TIM_STATE_BUSY;\r
2021   \r
2022   /* Disable the TIM Peripheral Clock */\r
2023   __HAL_TIM_DISABLE(htim);\r
2024   \r
2025   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */\r
2026   HAL_TIM_OnePulse_MspDeInit(htim);\r
2027     \r
2028   /* Change TIM state */  \r
2029   htim->State = HAL_TIM_STATE_RESET;\r
2030 \r
2031   /* Release Lock */\r
2032   __HAL_UNLOCK(htim);\r
2033 \r
2034   return HAL_OK;\r
2035 }\r
2036 \r
2037 /**\r
2038   * @brief  Initializes the TIM One Pulse MSP.\r
2039   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2040   *                the configuration information for TIM module.\r
2041   * @retval None\r
2042   */\r
2043 __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)\r
2044 {\r
2045   /* NOTE : This function Should not be modified, when the callback is needed,\r
2046             the HAL_TIM_OnePulse_MspInit could be implemented in the user file\r
2047    */\r
2048 }\r
2049 \r
2050 /**\r
2051   * @brief  DeInitializes TIM One Pulse MSP.\r
2052   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2053   *                the configuration information for TIM module.\r
2054   * @retval None\r
2055   */\r
2056 __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)\r
2057 {\r
2058   /* NOTE : This function Should not be modified, when the callback is needed,\r
2059             the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file\r
2060    */\r
2061 }\r
2062 \r
2063 /**\r
2064   * @brief  Starts the TIM One Pulse signal generation.\r
2065   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2066   *                the configuration information for TIM module.\r
2067   * @param  OutputChannel : TIM Channels to be enabled.\r
2068   *          This parameter can be one of the following values:\r
2069   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2070   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2071   * @retval HAL status\r
2072   */\r
2073 HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)\r
2074 {\r
2075   /* Enable the Capture compare and the Input Capture channels \r
2076     (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)\r
2077     if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and\r
2078     if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output \r
2079     in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together \r
2080     \r
2081     No need to enable the counter, it's enabled automatically by hardware \r
2082     (the counter starts in response to a stimulus and generate a pulse */\r
2083   \r
2084   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE); \r
2085   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE); \r
2086   \r
2087   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
2088   {\r
2089     /* Enable the main output */\r
2090     __HAL_TIM_MOE_ENABLE(htim);\r
2091   }\r
2092   \r
2093   /* Return function status */\r
2094   return HAL_OK;\r
2095 }\r
2096 \r
2097 /**\r
2098   * @brief  Stops the TIM One Pulse signal generation.\r
2099   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2100   *                the configuration information for TIM module.\r
2101   * @param  OutputChannel : TIM Channels to be disable.\r
2102   *          This parameter can be one of the following values:\r
2103   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2104   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2105   * @retval HAL status\r
2106   */\r
2107 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)\r
2108 {\r
2109   /* Disable the Capture compare and the Input Capture channels \r
2110   (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)\r
2111   if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and\r
2112   if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output \r
2113   in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */\r
2114   \r
2115   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
2116   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE); \r
2117     \r
2118   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
2119   {\r
2120     /* Disable the Main Output */\r
2121     __HAL_TIM_MOE_DISABLE(htim);\r
2122   }\r
2123     \r
2124   /* Disable the Peripheral */\r
2125   __HAL_TIM_DISABLE(htim); \r
2126   \r
2127   /* Return function status */\r
2128   return HAL_OK;\r
2129 }\r
2130 \r
2131 /**\r
2132   * @brief  Starts the TIM One Pulse signal generation in interrupt mode.\r
2133   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2134   *                the configuration information for TIM module.\r
2135   * @param  OutputChannel : TIM Channels to be enabled.\r
2136   *          This parameter can be one of the following values:\r
2137   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2138   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2139   * @retval HAL status\r
2140   */\r
2141 HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)\r
2142 {\r
2143   /* Enable the Capture compare and the Input Capture channels \r
2144     (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)\r
2145     if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and\r
2146     if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output \r
2147     in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together \r
2148     \r
2149     No need to enable the counter, it's enabled automatically by hardware \r
2150     (the counter starts in response to a stimulus and generate a pulse */\r
2151  \r
2152   /* Enable the TIM Capture/Compare 1 interrupt */\r
2153   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
2154   \r
2155   /* Enable the TIM Capture/Compare 2 interrupt */\r
2156   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);\r
2157   \r
2158   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE); \r
2159   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE); \r
2160   \r
2161   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
2162   {\r
2163     /* Enable the main output */\r
2164     __HAL_TIM_MOE_ENABLE(htim);\r
2165   }\r
2166   \r
2167   /* Return function status */\r
2168   return HAL_OK;\r
2169 }\r
2170 \r
2171 /**\r
2172   * @brief  Stops the TIM One Pulse signal generation in interrupt mode.\r
2173   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2174   *                the configuration information for TIM module.\r
2175   * @param  OutputChannel : TIM Channels to be enabled.\r
2176   *          This parameter can be one of the following values:\r
2177   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2178   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2179   * @retval HAL status\r
2180   */\r
2181 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)\r
2182 {\r
2183   /* Disable the TIM Capture/Compare 1 interrupt */\r
2184   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);  \r
2185   \r
2186   /* Disable the TIM Capture/Compare 2 interrupt */\r
2187   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
2188   \r
2189   /* Disable the Capture compare and the Input Capture channels \r
2190   (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)\r
2191   if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and\r
2192   if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output \r
2193   in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */  \r
2194   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
2195   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE); \r
2196     \r
2197   if(IS_TIM_ADVANCED_INSTANCE(htim->Instance) != RESET)  \r
2198   {\r
2199     /* Disable the Main Output */\r
2200     __HAL_TIM_MOE_DISABLE(htim);\r
2201   }\r
2202     \r
2203   /* Disable the Peripheral */\r
2204    __HAL_TIM_DISABLE(htim);  \r
2205   \r
2206   /* Return function status */\r
2207   return HAL_OK;\r
2208 }\r
2209 \r
2210 /**\r
2211   * @}\r
2212   */\r
2213 \r
2214 /** @defgroup TIM_Exported_Functions_Group6 Time Encoder functions \r
2215  *  @brief    Time Encoder functions \r
2216  *\r
2217 @verbatim    \r
2218   ==============================================================================\r
2219                           ##### Time Encoder functions #####\r
2220   ==============================================================================\r
2221   [..]\r
2222     This section provides functions allowing to:\r
2223     (+) Initialize and configure the TIM Encoder. \r
2224     (+) De-initialize the TIM Encoder.\r
2225     (+) Start the Time Encoder.\r
2226     (+) Stop the Time Encoder.\r
2227     (+) Start the Time Encoder and enable interrupt.\r
2228     (+) Stop the Time Encoder and disable interrupt.\r
2229     (+) Start the Time Encoder and enable DMA transfer.\r
2230     (+) Stop the Time Encoder and disable DMA transfer.\r
2231  \r
2232 @endverbatim\r
2233   * @{\r
2234   */\r
2235 /**\r
2236   * @brief  Initializes the TIM Encoder Interface and create the associated handle.\r
2237   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2238   *                the configuration information for TIM module.\r
2239   * @param  sConfig: TIM Encoder Interface configuration structure\r
2240   * @retval HAL status\r
2241   */\r
2242 HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim,  TIM_Encoder_InitTypeDef* sConfig)\r
2243 {\r
2244   uint32_t tmpsmcr = 0;\r
2245   uint32_t tmpccmr1 = 0;\r
2246   uint32_t tmpccer = 0;\r
2247   \r
2248   /* Check the TIM handle allocation */\r
2249   if(htim == NULL)\r
2250   {\r
2251     return HAL_ERROR;\r
2252   }\r
2253    \r
2254   /* Check the parameters */\r
2255   assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
2256   assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));\r
2257   assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));\r
2258   assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));\r
2259   assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));\r
2260   assert_param(IS_TIM_IC_POLARITY(sConfig->IC2Polarity));\r
2261   assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));\r
2262   assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));\r
2263   assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));\r
2264   assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));\r
2265 \r
2266   if(htim->State == HAL_TIM_STATE_RESET)\r
2267   { \r
2268     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */\r
2269     HAL_TIM_Encoder_MspInit(htim);\r
2270   }\r
2271   \r
2272   /* Set the TIM state */\r
2273   htim->State= HAL_TIM_STATE_BUSY;   \r
2274     \r
2275   /* Reset the SMS bits */\r
2276   htim->Instance->SMCR &= ~TIM_SMCR_SMS;\r
2277   \r
2278   /* Configure the Time base in the Encoder Mode */\r
2279   TIM_Base_SetConfig(htim->Instance, &htim->Init);  \r
2280   \r
2281   /* Get the TIMx SMCR register value */\r
2282   tmpsmcr = htim->Instance->SMCR;\r
2283 \r
2284   /* Get the TIMx CCMR1 register value */\r
2285   tmpccmr1 = htim->Instance->CCMR1;\r
2286 \r
2287   /* Get the TIMx CCER register value */\r
2288   tmpccer = htim->Instance->CCER;\r
2289 \r
2290   /* Set the encoder Mode */\r
2291   tmpsmcr |= sConfig->EncoderMode;\r
2292 \r
2293   /* Select the Capture Compare 1 and the Capture Compare 2 as input */\r
2294   tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);\r
2295   tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8));\r
2296   \r
2297   /* Set the Capture Compare 1 and the Capture Compare 2 prescalers and filters */\r
2298   tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);\r
2299   tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);\r
2300   tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8);\r
2301   tmpccmr1 |= (sConfig->IC1Filter << 4) | (sConfig->IC2Filter << 12);\r
2302 \r
2303   /* Set the TI1 and the TI2 Polarities */\r
2304   tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);\r
2305   tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);\r
2306   tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4);\r
2307   \r
2308   /* Write to TIMx SMCR */\r
2309   htim->Instance->SMCR = tmpsmcr;\r
2310 \r
2311   /* Write to TIMx CCMR1 */\r
2312   htim->Instance->CCMR1 = tmpccmr1;\r
2313 \r
2314   /* Write to TIMx CCER */\r
2315   htim->Instance->CCER = tmpccer;\r
2316   \r
2317   /* Initialize the TIM state*/\r
2318   htim->State= HAL_TIM_STATE_READY;\r
2319   \r
2320   return HAL_OK;\r
2321 }\r
2322 \r
2323 /**\r
2324   * @brief  DeInitializes the TIM Encoder interface  \r
2325   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2326   *                the configuration information for TIM module.\r
2327   * @retval HAL status\r
2328   */\r
2329 HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)\r
2330 {\r
2331   /* Check the parameters */\r
2332   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
2333   \r
2334   htim->State = HAL_TIM_STATE_BUSY;\r
2335   \r
2336   /* Disable the TIM Peripheral Clock */\r
2337   __HAL_TIM_DISABLE(htim);\r
2338   \r
2339   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */\r
2340   HAL_TIM_Encoder_MspDeInit(htim);\r
2341     \r
2342   /* Change TIM state */  \r
2343   htim->State = HAL_TIM_STATE_RESET;\r
2344  \r
2345   /* Release Lock */\r
2346   __HAL_UNLOCK(htim);\r
2347 \r
2348   return HAL_OK;\r
2349 }\r
2350 \r
2351 /**\r
2352   * @brief  Initializes the TIM Encoder Interface MSP.\r
2353   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2354   *                the configuration information for TIM module.\r
2355   * @retval None\r
2356   */\r
2357 __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)\r
2358 {\r
2359   /* NOTE : This function Should not be modified, when the callback is needed,\r
2360             the HAL_TIM_Encoder_MspInit could be implemented in the user file\r
2361    */\r
2362 }\r
2363 \r
2364 /**\r
2365   * @brief  DeInitializes TIM Encoder Interface MSP.\r
2366   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2367   *                the configuration information for TIM module.\r
2368   * @retval None\r
2369   */\r
2370 __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)\r
2371 {\r
2372   /* NOTE : This function Should not be modified, when the callback is needed,\r
2373             the HAL_TIM_Encoder_MspDeInit could be implemented in the user file\r
2374    */\r
2375 }\r
2376 \r
2377 /**\r
2378   * @brief  Starts the TIM Encoder Interface.\r
2379   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2380   *                the configuration information for TIM module.\r
2381   * @param  Channel: TIM Channels to be enabled.\r
2382   *          This parameter can be one of the following values:\r
2383   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2384   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2385   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected\r
2386   * @retval HAL status\r
2387   */\r
2388 HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)\r
2389 {\r
2390   /* Check the parameters */\r
2391   assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
2392   \r
2393   /* Enable the encoder interface channels */\r
2394   switch (Channel)\r
2395   {\r
2396     case TIM_CHANNEL_1:\r
2397     {\r
2398       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);\r
2399       break; \r
2400     }\r
2401     case TIM_CHANNEL_2:\r
2402     { \r
2403       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE); \r
2404       break;\r
2405     }  \r
2406     default :\r
2407     {\r
2408      TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);\r
2409      TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);\r
2410      break; \r
2411     }\r
2412   }  \r
2413   /* Enable the Peripheral */\r
2414   __HAL_TIM_ENABLE(htim);\r
2415   \r
2416   /* Return function status */\r
2417   return HAL_OK;\r
2418 }\r
2419 \r
2420 /**\r
2421   * @brief  Stops the TIM Encoder Interface.\r
2422   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2423   *                the configuration information for TIM module.\r
2424   * @param  Channel: TIM Channels to be disabled.\r
2425   *          This parameter can be one of the following values:\r
2426   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2427   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2428   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected\r
2429   * @retval HAL status\r
2430   */\r
2431 HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)\r
2432 {\r
2433   /* Check the parameters */\r
2434     assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
2435     \r
2436    /* Disable the Input Capture channels 1 and 2\r
2437     (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */ \r
2438   switch (Channel)\r
2439   {\r
2440     case TIM_CHANNEL_1:\r
2441     {\r
2442       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);\r
2443       break; \r
2444     }\r
2445     case TIM_CHANNEL_2:\r
2446     { \r
2447       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE); \r
2448       break;\r
2449     }  \r
2450     default :\r
2451     {\r
2452      TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);\r
2453      TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);\r
2454      break; \r
2455     }\r
2456   }  \r
2457   /* Disable the Peripheral */\r
2458   __HAL_TIM_DISABLE(htim);\r
2459   \r
2460   /* Return function status */\r
2461   return HAL_OK;\r
2462 }\r
2463 \r
2464 /**\r
2465   * @brief  Starts the TIM Encoder Interface in interrupt mode.\r
2466   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2467   *                the configuration information for TIM module.\r
2468   * @param  Channel: TIM Channels to be enabled.\r
2469   *          This parameter can be one of the following values:\r
2470   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2471   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2472   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected\r
2473   * @retval HAL status\r
2474   */\r
2475 HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
2476 {\r
2477   /* Check the parameters */\r
2478   assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
2479   \r
2480   /* Enable the encoder interface channels */\r
2481   /* Enable the capture compare Interrupts 1 and/or 2 */\r
2482   switch (Channel)\r
2483   {\r
2484     case TIM_CHANNEL_1:\r
2485     {\r
2486       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);\r
2487       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
2488       break; \r
2489     }\r
2490     case TIM_CHANNEL_2:\r
2491     { \r
2492       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);\r
2493       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2); \r
2494       break;\r
2495     }  \r
2496     default :\r
2497     {\r
2498      TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);\r
2499      TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);\r
2500      __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
2501      __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);\r
2502      break; \r
2503     }\r
2504   }\r
2505   \r
2506   /* Enable the Peripheral */\r
2507   __HAL_TIM_ENABLE(htim);\r
2508   \r
2509   /* Return function status */\r
2510   return HAL_OK;\r
2511 }\r
2512 \r
2513 /**\r
2514   * @brief  Stops the TIM Encoder Interface in interrupt mode.\r
2515   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2516   *                the configuration information for TIM module.\r
2517   * @param  Channel: TIM Channels to be disabled.\r
2518   *          This parameter can be one of the following values:\r
2519   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2520   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2521   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected\r
2522   * @retval HAL status\r
2523   */\r
2524 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
2525 {\r
2526   /* Check the parameters */\r
2527   assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
2528     \r
2529   /* Disable the Input Capture channels 1 and 2\r
2530     (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */ \r
2531   if(Channel == TIM_CHANNEL_1)\r
2532   {\r
2533     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
2534     \r
2535     /* Disable the capture compare Interrupts 1 */\r
2536   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
2537   }  \r
2538   else if(Channel == TIM_CHANNEL_2)\r
2539   {  \r
2540     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE); \r
2541     \r
2542     /* Disable the capture compare Interrupts 2 */\r
2543   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
2544   }  \r
2545   else\r
2546   {\r
2547     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
2548     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE); \r
2549     \r
2550     /* Disable the capture compare Interrupts 1 and 2 */\r
2551     __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
2552     __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
2553   }\r
2554     \r
2555   /* Disable the Peripheral */\r
2556   __HAL_TIM_DISABLE(htim);\r
2557   \r
2558   /* Change the htim state */\r
2559   htim->State = HAL_TIM_STATE_READY;\r
2560   \r
2561   /* Return function status */\r
2562   return HAL_OK;\r
2563 }\r
2564 \r
2565 /**\r
2566   * @brief  Starts the TIM Encoder Interface in DMA mode.\r
2567   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2568   *                the configuration information for TIM module.\r
2569   * @param  Channel: TIM Channels to be enabled.\r
2570   *          This parameter can be one of the following values:\r
2571   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2572   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2573   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected\r
2574   * @param  pData1: The destination Buffer address for IC1.\r
2575   * @param  pData2: The destination Buffer address for IC2.\r
2576   * @param  Length: The length of data to be transferred from TIM peripheral to memory.\r
2577   * @retval HAL status\r
2578   */\r
2579 HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1, uint32_t *pData2, uint16_t Length)\r
2580 {\r
2581   /* Check the parameters */\r
2582   assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));\r
2583   \r
2584   if((htim->State == HAL_TIM_STATE_BUSY))\r
2585   {\r
2586      return HAL_BUSY;\r
2587   }\r
2588   else if((htim->State == HAL_TIM_STATE_READY))\r
2589   {\r
2590     if((((pData1 == 0) || (pData2 == 0) )) && (Length > 0)) \r
2591     {\r
2592       return HAL_ERROR;                                    \r
2593     }\r
2594     else\r
2595     {\r
2596       htim->State = HAL_TIM_STATE_BUSY;\r
2597     }\r
2598   }  \r
2599    \r
2600   switch (Channel)\r
2601   {\r
2602     case TIM_CHANNEL_1:\r
2603     {\r
2604       /* Set the DMA Period elapsed callback */\r
2605       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
2606      \r
2607       /* Set the DMA error callback */\r
2608       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
2609       \r
2610       /* Enable the DMA Stream */\r
2611       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t )pData1, Length); \r
2612       \r
2613       /* Enable the TIM Input Capture DMA request */      \r
2614       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);\r
2615             \r
2616       /* Enable the Peripheral */\r
2617       __HAL_TIM_ENABLE(htim);\r
2618       \r
2619       /* Enable the Capture compare channel */\r
2620       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);\r
2621     }\r
2622     break;\r
2623     \r
2624     case TIM_CHANNEL_2:\r
2625     {\r
2626       /* Set the DMA Period elapsed callback */\r
2627       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
2628      \r
2629       /* Set the DMA error callback */\r
2630       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError;\r
2631       /* Enable the DMA Stream */\r
2632       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length);\r
2633       \r
2634       /* Enable the TIM Input Capture  DMA request */\r
2635       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);\r
2636      \r
2637       /* Enable the Peripheral */\r
2638       __HAL_TIM_ENABLE(htim);\r
2639       \r
2640       /* Enable the Capture compare channel */\r
2641       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);\r
2642     }\r
2643     break;\r
2644     \r
2645     case TIM_CHANNEL_ALL:\r
2646     {\r
2647       /* Set the DMA Period elapsed callback */\r
2648       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
2649      \r
2650       /* Set the DMA error callback */\r
2651       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
2652       \r
2653       /* Enable the DMA Stream */\r
2654       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length);\r
2655       \r
2656       /* Set the DMA Period elapsed callback */\r
2657       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
2658      \r
2659       /* Set the DMA error callback */\r
2660       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;\r
2661       \r
2662       /* Enable the DMA Stream */\r
2663       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length);\r
2664           \r
2665      /* Enable the Peripheral */\r
2666       __HAL_TIM_ENABLE(htim);\r
2667       \r
2668       /* Enable the Capture compare channel */\r
2669       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);\r
2670       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);\r
2671       \r
2672       /* Enable the TIM Input Capture  DMA request */\r
2673       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);\r
2674       /* Enable the TIM Input Capture  DMA request */\r
2675       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);\r
2676     }\r
2677     break;\r
2678     \r
2679     default:\r
2680     break;\r
2681   }  \r
2682   /* Return function status */\r
2683   return HAL_OK;\r
2684 }\r
2685 \r
2686 /**\r
2687   * @brief  Stops the TIM Encoder Interface in DMA mode.\r
2688   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2689   *                the configuration information for TIM module.\r
2690   * @param  Channel: TIM Channels to be enabled.\r
2691   *          This parameter can be one of the following values:\r
2692   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2693   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2694   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected\r
2695   * @retval HAL status\r
2696   */\r
2697 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)\r
2698 {\r
2699   /* Check the parameters */\r
2700   assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));\r
2701   \r
2702   /* Disable the Input Capture channels 1 and 2\r
2703     (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */ \r
2704   if(Channel == TIM_CHANNEL_1)\r
2705   {\r
2706     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
2707     \r
2708     /* Disable the capture compare DMA Request 1 */\r
2709     __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);\r
2710   }  \r
2711   else if(Channel == TIM_CHANNEL_2)\r
2712   {  \r
2713     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE); \r
2714     \r
2715     /* Disable the capture compare DMA Request 2 */\r
2716     __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);\r
2717   }  \r
2718   else\r
2719   {\r
2720     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
2721     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE); \r
2722     \r
2723     /* Disable the capture compare DMA Request 1 and 2 */\r
2724     __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);\r
2725     __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);\r
2726   }\r
2727   \r
2728   /* Disable the Peripheral */\r
2729   __HAL_TIM_DISABLE(htim);\r
2730   \r
2731   /* Change the htim state */\r
2732   htim->State = HAL_TIM_STATE_READY;\r
2733   \r
2734   /* Return function status */\r
2735   return HAL_OK;\r
2736 }\r
2737 \r
2738 /**\r
2739   * @}\r
2740   */\r
2741 /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management \r
2742  *  @brief    IRQ handler management \r
2743  *\r
2744 @verbatim   \r
2745   ==============================================================================\r
2746                         ##### IRQ handler management #####\r
2747   ==============================================================================  \r
2748   [..]  \r
2749     This section provides Timer IRQ handler function.\r
2750                \r
2751 @endverbatim\r
2752   * @{\r
2753   */\r
2754 /**\r
2755   * @brief  This function handles TIM interrupts requests.\r
2756   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2757   *                the configuration information for TIM module.\r
2758   * @retval None\r
2759   */\r
2760 void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)\r
2761 {\r
2762   /* Capture compare 1 event */\r
2763   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)\r
2764   {\r
2765     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) !=RESET)\r
2766     {\r
2767       {\r
2768         __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);\r
2769         htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;\r
2770         \r
2771         /* Input capture event */\r
2772         if((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00)\r
2773         {\r
2774           HAL_TIM_IC_CaptureCallback(htim);\r
2775         }\r
2776         /* Output compare event */\r
2777         else\r
2778         {\r
2779           HAL_TIM_OC_DelayElapsedCallback(htim);\r
2780           HAL_TIM_PWM_PulseFinishedCallback(htim);\r
2781         }\r
2782         htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;\r
2783       }\r
2784     }\r
2785   }\r
2786   /* Capture compare 2 event */\r
2787   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET)\r
2788   {\r
2789     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) !=RESET)\r
2790     {\r
2791       __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);\r
2792       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;\r
2793       /* Input capture event */\r
2794       if((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00)\r
2795       {          \r
2796         HAL_TIM_IC_CaptureCallback(htim);\r
2797       }\r
2798       /* Output compare event */\r
2799       else\r
2800       {\r
2801         HAL_TIM_OC_DelayElapsedCallback(htim);\r
2802         HAL_TIM_PWM_PulseFinishedCallback(htim);\r
2803       }\r
2804       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;\r
2805     }\r
2806   }\r
2807   /* Capture compare 3 event */\r
2808   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET)\r
2809   {\r
2810     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) !=RESET)\r
2811     {\r
2812       __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);\r
2813       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;\r
2814       /* Input capture event */\r
2815       if((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00)\r
2816       {          \r
2817         HAL_TIM_IC_CaptureCallback(htim);\r
2818       }\r
2819       /* Output compare event */\r
2820       else\r
2821       {\r
2822         HAL_TIM_OC_DelayElapsedCallback(htim);\r
2823         HAL_TIM_PWM_PulseFinishedCallback(htim); \r
2824       }\r
2825       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;\r
2826     }\r
2827   }\r
2828   /* Capture compare 4 event */\r
2829   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET)\r
2830   {\r
2831     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) !=RESET)\r
2832     {\r
2833       __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);\r
2834       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;\r
2835       /* Input capture event */\r
2836       if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00)\r
2837       {          \r
2838         HAL_TIM_IC_CaptureCallback(htim);\r
2839       }\r
2840       /* Output compare event */\r
2841       else\r
2842       {\r
2843         HAL_TIM_OC_DelayElapsedCallback(htim);\r
2844         HAL_TIM_PWM_PulseFinishedCallback(htim);\r
2845       }\r
2846       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;\r
2847     }\r
2848   }\r
2849   /* TIM Update event */\r
2850   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)\r
2851   {\r
2852     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_UPDATE) !=RESET)\r
2853     {\r
2854       __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);\r
2855       HAL_TIM_PeriodElapsedCallback(htim);\r
2856     }\r
2857   }\r
2858   /* TIM Break input event */\r
2859   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK) != RESET)\r
2860   {\r
2861     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) !=RESET)\r
2862     {\r
2863       __HAL_TIM_CLEAR_IT(htim, TIM_IT_BREAK);\r
2864       HAL_TIMEx_BreakCallback(htim);\r
2865     }\r
2866   }\r
2867   \r
2868     /* TIM Break input event */\r
2869   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK2) != RESET)\r
2870   {\r
2871     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) !=RESET)\r
2872     {\r
2873       __HAL_TIM_CLEAR_IT(htim, TIM_IT_BREAK);\r
2874       HAL_TIMEx_BreakCallback(htim);\r
2875     }\r
2876   }\r
2877 \r
2878   /* TIM Trigger detection event */\r
2879   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_TRIGGER) != RESET)\r
2880   {\r
2881     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_TRIGGER) !=RESET)\r
2882     {\r
2883       __HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);\r
2884       HAL_TIM_TriggerCallback(htim);\r
2885     }\r
2886   }\r
2887   /* TIM commutation event */\r
2888   if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_COM) != RESET)\r
2889   {\r
2890     if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_COM) !=RESET)\r
2891     {\r
2892       __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_COM);\r
2893       HAL_TIMEx_CommutationCallback(htim);\r
2894     }\r
2895   }\r
2896 }\r
2897 \r
2898 /**\r
2899   * @}\r
2900   */\r
2901   \r
2902 /** @defgroup TIM_Exported_Functions_Group8 Peripheral Control functions\r
2903  *  @brief      Peripheral Control functions \r
2904  *\r
2905 @verbatim   \r
2906   ==============================================================================\r
2907                    ##### Peripheral Control functions #####\r
2908   ==============================================================================  \r
2909  [..] \r
2910    This section provides functions allowing to:\r
2911    (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode. \r
2912    (+) Configure External Clock source.\r
2913    (+) Configure Complementary channels, break features and dead time.\r
2914    (+) Configure Master and the Slave synchronization.\r
2915    (+) Configure the DMA Burst Mode.\r
2916       \r
2917 @endverbatim\r
2918   * @{\r
2919   */\r
2920   \r
2921 /**\r
2922   * @brief  Initializes the TIM Output Compare Channels according to the specified\r
2923   *         parameters in the TIM_OC_InitTypeDef.\r
2924   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2925   *                the configuration information for TIM module.\r
2926   * @param  sConfig: TIM Output Compare configuration structure\r
2927   * @param  Channel: TIM Channels to be enabled.\r
2928   *          This parameter can be one of the following values:\r
2929   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
2930   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
2931   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
2932   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected \r
2933   * @retval HAL status\r
2934   */\r
2935 __weak HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel)\r
2936 {\r
2937   /* Check the parameters */ \r
2938   assert_param(IS_TIM_CHANNELS(Channel)); \r
2939   assert_param(IS_TIM_OC_MODE(sConfig->OCMode));\r
2940   assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));\r
2941   assert_param(IS_TIM_OCN_POLARITY(sConfig->OCNPolarity));\r
2942   assert_param(IS_TIM_OCNIDLE_STATE(sConfig->OCNIdleState));\r
2943   assert_param(IS_TIM_OCIDLE_STATE(sConfig->OCIdleState));\r
2944   \r
2945   /* Check input state */\r
2946   __HAL_LOCK(htim); \r
2947   \r
2948   htim->State = HAL_TIM_STATE_BUSY;\r
2949   \r
2950   switch (Channel)\r
2951   {\r
2952     case TIM_CHANNEL_1:\r
2953     {\r
2954       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
2955       /* Configure the TIM Channel 1 in Output Compare */\r
2956       TIM_OC1_SetConfig(htim->Instance, sConfig);\r
2957     }\r
2958     break;\r
2959     \r
2960     case TIM_CHANNEL_2:\r
2961     {\r
2962       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
2963       /* Configure the TIM Channel 2 in Output Compare */\r
2964       TIM_OC2_SetConfig(htim->Instance, sConfig);\r
2965     }\r
2966     break;\r
2967     \r
2968     case TIM_CHANNEL_3:\r
2969     {\r
2970        assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));\r
2971       /* Configure the TIM Channel 3 in Output Compare */\r
2972       TIM_OC3_SetConfig(htim->Instance, sConfig);\r
2973     }\r
2974     break;\r
2975     \r
2976     case TIM_CHANNEL_4:\r
2977     {\r
2978       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));\r
2979       /* Configure the TIM Channel 4 in Output Compare */\r
2980       TIM_OC4_SetConfig(htim->Instance, sConfig);\r
2981     }\r
2982     break;\r
2983     \r
2984     default:\r
2985     break;    \r
2986   }\r
2987   htim->State = HAL_TIM_STATE_READY;\r
2988   \r
2989   __HAL_UNLOCK(htim); \r
2990   \r
2991   return HAL_OK;\r
2992 }\r
2993 \r
2994 /**\r
2995   * @brief  Initializes the TIM Input Capture Channels according to the specified\r
2996   *         parameters in the TIM_IC_InitTypeDef.\r
2997   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2998   *                the configuration information for TIM module.\r
2999   * @param  sConfig: TIM Input Capture configuration structure\r
3000   * @param  Channel: TIM Channels to be enabled.\r
3001   *          This parameter can be one of the following values:\r
3002   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
3003   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
3004   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
3005   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected \r
3006   * @retval HAL status\r
3007   */\r
3008 HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_IC_InitTypeDef* sConfig, uint32_t Channel)\r
3009 {\r
3010   /* Check the parameters */\r
3011   assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
3012   assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));\r
3013   assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));\r
3014   assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));\r
3015   assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));\r
3016   \r
3017   __HAL_LOCK(htim);\r
3018   \r
3019   htim->State = HAL_TIM_STATE_BUSY;\r
3020   \r
3021   if (Channel == TIM_CHANNEL_1)\r
3022   {\r
3023     /* TI1 Configuration */\r
3024     TIM_TI1_SetConfig(htim->Instance,\r
3025                sConfig->ICPolarity,\r
3026                sConfig->ICSelection,\r
3027                sConfig->ICFilter);\r
3028                \r
3029     /* Reset the IC1PSC Bits */\r
3030     htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;\r
3031 \r
3032     /* Set the IC1PSC value */\r
3033     htim->Instance->CCMR1 |= sConfig->ICPrescaler;\r
3034   }\r
3035   else if (Channel == TIM_CHANNEL_2)\r
3036   {\r
3037     /* TI2 Configuration */\r
3038     assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
3039     \r
3040     TIM_TI2_SetConfig(htim->Instance, \r
3041                       sConfig->ICPolarity,\r
3042                       sConfig->ICSelection,\r
3043                       sConfig->ICFilter);\r
3044                \r
3045     /* Reset the IC2PSC Bits */\r
3046     htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;\r
3047 \r
3048     /* Set the IC2PSC value */\r
3049     htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8);\r
3050   }\r
3051   else if (Channel == TIM_CHANNEL_3)\r
3052   {\r
3053     /* TI3 Configuration */\r
3054     assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));\r
3055     \r
3056     TIM_TI3_SetConfig(htim->Instance,  \r
3057                sConfig->ICPolarity,\r
3058                sConfig->ICSelection,\r
3059                sConfig->ICFilter);\r
3060                \r
3061     /* Reset the IC3PSC Bits */\r
3062     htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;\r
3063 \r
3064     /* Set the IC3PSC value */\r
3065     htim->Instance->CCMR2 |= sConfig->ICPrescaler;\r
3066   }\r
3067   else\r
3068   {\r
3069     /* TI4 Configuration */\r
3070     assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));\r
3071     \r
3072     TIM_TI4_SetConfig(htim->Instance, \r
3073                sConfig->ICPolarity,\r
3074                sConfig->ICSelection,\r
3075                sConfig->ICFilter);\r
3076                \r
3077     /* Reset the IC4PSC Bits */\r
3078     htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;\r
3079 \r
3080     /* Set the IC4PSC value */\r
3081     htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8);\r
3082   }\r
3083   \r
3084   htim->State = HAL_TIM_STATE_READY;\r
3085     \r
3086   __HAL_UNLOCK(htim);\r
3087   \r
3088   return HAL_OK; \r
3089 }\r
3090 \r
3091 /**\r
3092   * @brief  Initializes the TIM PWM  channels according to the specified\r
3093   *         parameters in the TIM_OC_InitTypeDef.\r
3094   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3095   *                the configuration information for TIM module.\r
3096   * @param  sConfig: TIM PWM configuration structure\r
3097   * @param  Channel: TIM Channels to be enabled.\r
3098   *          This parameter can be one of the following values:\r
3099   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
3100   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
3101   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
3102   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
3103   * @retval HAL status\r
3104   */\r
3105 __weak HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel)\r
3106 {\r
3107   __HAL_LOCK(htim);\r
3108   \r
3109   /* Check the parameters */ \r
3110   assert_param(IS_TIM_CHANNELS(Channel)); \r
3111   assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));\r
3112   assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));\r
3113   assert_param(IS_TIM_OCN_POLARITY(sConfig->OCNPolarity));\r
3114   assert_param(IS_TIM_OCNIDLE_STATE(sConfig->OCNIdleState));\r
3115   assert_param(IS_TIM_OCIDLE_STATE(sConfig->OCIdleState));\r
3116   assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode)); \r
3117   \r
3118   htim->State = HAL_TIM_STATE_BUSY;\r
3119     \r
3120   switch (Channel)\r
3121   {\r
3122     case TIM_CHANNEL_1:\r
3123     {\r
3124       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
3125       /* Configure the Channel 1 in PWM mode */\r
3126       TIM_OC1_SetConfig(htim->Instance, sConfig);\r
3127       \r
3128       /* Set the Preload enable bit for channel1 */\r
3129       htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;\r
3130       \r
3131       /* Configure the Output Fast mode */\r
3132       htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;\r
3133       htim->Instance->CCMR1 |= sConfig->OCFastMode;\r
3134     }\r
3135     break;\r
3136     \r
3137     case TIM_CHANNEL_2:\r
3138     {\r
3139       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
3140       /* Configure the Channel 2 in PWM mode */\r
3141       TIM_OC2_SetConfig(htim->Instance, sConfig);\r
3142       \r
3143       /* Set the Preload enable bit for channel2 */\r
3144       htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;\r
3145       \r
3146       /* Configure the Output Fast mode */\r
3147       htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;\r
3148       htim->Instance->CCMR1 |= sConfig->OCFastMode << 8;\r
3149     }\r
3150     break;\r
3151     \r
3152     case TIM_CHANNEL_3:\r
3153     {\r
3154       assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));\r
3155       /* Configure the Channel 3 in PWM mode */\r
3156       TIM_OC3_SetConfig(htim->Instance, sConfig);\r
3157       \r
3158       /* Set the Preload enable bit for channel3 */\r
3159       htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;\r
3160       \r
3161      /* Configure the Output Fast mode */\r
3162       htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;\r
3163       htim->Instance->CCMR2 |= sConfig->OCFastMode;  \r
3164     }\r
3165     break;\r
3166     \r
3167     case TIM_CHANNEL_4:\r
3168     {\r
3169       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));\r
3170       /* Configure the Channel 4 in PWM mode */\r
3171       TIM_OC4_SetConfig(htim->Instance, sConfig);\r
3172       \r
3173       /* Set the Preload enable bit for channel4 */\r
3174       htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;\r
3175       \r
3176      /* Configure the Output Fast mode */\r
3177       htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;\r
3178       htim->Instance->CCMR2 |= sConfig->OCFastMode << 8;  \r
3179     }\r
3180     break;\r
3181     \r
3182     default:\r
3183     break;    \r
3184   }\r
3185   \r
3186   htim->State = HAL_TIM_STATE_READY;\r
3187     \r
3188   __HAL_UNLOCK(htim);\r
3189   \r
3190   return HAL_OK;\r
3191 }\r
3192 \r
3193 /**\r
3194   * @brief  Initializes the TIM One Pulse Channels according to the specified\r
3195   *         parameters in the TIM_OnePulse_InitTypeDef.\r
3196   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3197   *                the configuration information for TIM module.\r
3198   * @param  sConfig: TIM One Pulse configuration structure\r
3199   * @param  OutputChannel: TIM Channels to be enabled.\r
3200   *          This parameter can be one of the following values:\r
3201   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
3202   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
3203   * @param  InputChannel: TIM Channels to be enabled.\r
3204   *          This parameter can be one of the following values:\r
3205   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
3206   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
3207   * @retval HAL status\r
3208   */\r
3209 HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim,  TIM_OnePulse_InitTypeDef* sConfig, uint32_t OutputChannel,  uint32_t InputChannel)\r
3210 {\r
3211   TIM_OC_InitTypeDef temp1;\r
3212   \r
3213   /* Check the parameters */\r
3214   assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));\r
3215   assert_param(IS_TIM_OPM_CHANNELS(InputChannel));\r
3216 \r
3217   if(OutputChannel != InputChannel)  \r
3218   {\r
3219     __HAL_LOCK(htim);\r
3220   \r
3221     htim->State = HAL_TIM_STATE_BUSY;\r
3222 \r
3223     /* Extract the Output compare configuration from sConfig structure */  \r
3224     temp1.OCMode = sConfig->OCMode;\r
3225     temp1.Pulse = sConfig->Pulse;\r
3226     temp1.OCPolarity = sConfig->OCPolarity;\r
3227     temp1.OCNPolarity = sConfig->OCNPolarity;\r
3228     temp1.OCIdleState = sConfig->OCIdleState;\r
3229     temp1.OCNIdleState = sConfig->OCNIdleState; \r
3230     \r
3231     switch (OutputChannel)\r
3232     {\r
3233       case TIM_CHANNEL_1:\r
3234       {\r
3235         assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
3236       \r
3237         TIM_OC1_SetConfig(htim->Instance, &temp1); \r
3238       }\r
3239       break;\r
3240       case TIM_CHANNEL_2:\r
3241       {\r
3242         assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
3243       \r
3244         TIM_OC2_SetConfig(htim->Instance, &temp1);\r
3245       }\r
3246       break;\r
3247       default:\r
3248       break;  \r
3249     } \r
3250     switch (InputChannel)\r
3251     {\r
3252       case TIM_CHANNEL_1:\r
3253       {\r
3254         assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
3255       \r
3256         TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,\r
3257                         sConfig->ICSelection, sConfig->ICFilter);\r
3258                \r
3259         /* Reset the IC1PSC Bits */\r
3260         htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;\r
3261 \r
3262         /* Select the Trigger source */\r
3263         htim->Instance->SMCR &= ~TIM_SMCR_TS;\r
3264         htim->Instance->SMCR |= TIM_TS_TI1FP1;\r
3265       \r
3266         /* Select the Slave Mode */      \r
3267         htim->Instance->SMCR &= ~TIM_SMCR_SMS;\r
3268         htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;\r
3269       }\r
3270       break;\r
3271       case TIM_CHANNEL_2:\r
3272       {\r
3273         assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
3274       \r
3275         TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,\r
3276                  sConfig->ICSelection, sConfig->ICFilter);\r
3277                \r
3278         /* Reset the IC2PSC Bits */\r
3279         htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;\r
3280 \r
3281         /* Select the Trigger source */\r
3282         htim->Instance->SMCR &= ~TIM_SMCR_TS;\r
3283         htim->Instance->SMCR |= TIM_TS_TI2FP2;\r
3284       \r
3285         /* Select the Slave Mode */      \r
3286         htim->Instance->SMCR &= ~TIM_SMCR_SMS;\r
3287         htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;\r
3288       }\r
3289       break;\r
3290     \r
3291       default:\r
3292       break;  \r
3293     }\r
3294   \r
3295     htim->State = HAL_TIM_STATE_READY;\r
3296     \r
3297     __HAL_UNLOCK(htim);\r
3298   \r
3299     return HAL_OK;\r
3300   }\r
3301   else\r
3302   {\r
3303     return HAL_ERROR;\r
3304   }\r
3305\r
3306 \r
3307 /**\r
3308   * @brief  Configure the DMA Burst to transfer Data from the memory to the TIM peripheral  \r
3309   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3310   *                the configuration information for TIM module.\r
3311   * @param  BurstBaseAddress: TIM Base address from when the DMA will starts the Data write.\r
3312   *         This parameters can be on of the following values:\r
3313   *            @arg TIM_DMABASE_CR1  \r
3314   *            @arg TIM_DMABASE_CR2\r
3315   *            @arg TIM_DMABASE_SMCR\r
3316   *            @arg TIM_DMABASE_DIER\r
3317   *            @arg TIM_DMABASE_SR\r
3318   *            @arg TIM_DMABASE_EGR\r
3319   *            @arg TIM_DMABASE_CCMR1\r
3320   *            @arg TIM_DMABASE_CCMR2\r
3321   *            @arg TIM_DMABASE_CCER\r
3322   *            @arg TIM_DMABASE_CNT   \r
3323   *            @arg TIM_DMABASE_PSC   \r
3324   *            @arg TIM_DMABASE_ARR\r
3325   *            @arg TIM_DMABASE_RCR\r
3326   *            @arg TIM_DMABASE_CCR1\r
3327   *            @arg TIM_DMABASE_CCR2\r
3328   *            @arg TIM_DMABASE_CCR3  \r
3329   *            @arg TIM_DMABASE_CCR4\r
3330   *            @arg TIM_DMABASE_BDTR\r
3331   *            @arg TIM_DMABASE_DCR\r
3332   * @param  BurstRequestSrc: TIM DMA Request sources.\r
3333   *         This parameters can be on of the following values:\r
3334   *            @arg TIM_DMA_UPDATE: TIM update Interrupt source\r
3335   *            @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source\r
3336   *            @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source\r
3337   *            @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source\r
3338   *            @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source\r
3339   *            @arg TIM_DMA_COM: TIM Commutation DMA source\r
3340   *            @arg TIM_DMA_TRIGGER: TIM Trigger DMA source\r
3341   * @param  BurstBuffer: The Buffer address.\r
3342   * @param  BurstLength: DMA Burst length. This parameter can be one value\r
3343   *         between TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.\r
3344   * @retval HAL status\r
3345   */\r
3346 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress, uint32_t BurstRequestSrc,\r
3347                                               uint32_t* BurstBuffer, uint32_t  BurstLength)\r
3348 {\r
3349   /* Check the parameters */\r
3350   assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));\r
3351   assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));\r
3352   assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));\r
3353   assert_param(IS_TIM_DMA_LENGTH(BurstLength));\r
3354   \r
3355   if((htim->State == HAL_TIM_STATE_BUSY))\r
3356   {\r
3357      return HAL_BUSY;\r
3358   }\r
3359   else if((htim->State == HAL_TIM_STATE_READY))\r
3360   {\r
3361     if((BurstBuffer == 0 ) && (BurstLength > 0)) \r
3362     {\r
3363       return HAL_ERROR;                                    \r
3364     }\r
3365     else\r
3366     {\r
3367       htim->State = HAL_TIM_STATE_BUSY;\r
3368     }\r
3369   }\r
3370   switch(BurstRequestSrc)\r
3371   {\r
3372     case TIM_DMA_UPDATE:\r
3373     {  \r
3374       /* Set the DMA Period elapsed callback */\r
3375       htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;\r
3376      \r
3377       /* Set the DMA error callback */\r
3378       htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = HAL_TIM_DMAError ;\r
3379   \r
3380       /* Enable the DMA Stream */\r
3381       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1); \r
3382     }\r
3383     break;\r
3384     case TIM_DMA_CC1:\r
3385     {  \r
3386       /* Set the DMA Period elapsed callback */\r
3387       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
3388      \r
3389       /* Set the DMA error callback */\r
3390       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
3391   \r
3392       /* Enable the DMA Stream */\r
3393       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);     \r
3394     }\r
3395     break;\r
3396     case TIM_DMA_CC2:\r
3397     {  \r
3398       /* Set the DMA Period elapsed callback */\r
3399       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
3400      \r
3401       /* Set the DMA error callback */\r
3402       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;\r
3403   \r
3404       /* Enable the DMA Stream */\r
3405       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);     \r
3406     }\r
3407     break;\r
3408     case TIM_DMA_CC3:\r
3409     {  \r
3410       /* Set the DMA Period elapsed callback */\r
3411       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
3412      \r
3413       /* Set the DMA error callback */\r
3414       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;\r
3415   \r
3416       /* Enable the DMA Stream */\r
3417       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);     \r
3418     }\r
3419     break;\r
3420     case TIM_DMA_CC4:\r
3421     {  \r
3422       /* Set the DMA Period elapsed callback */\r
3423       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
3424      \r
3425       /* Set the DMA error callback */\r
3426       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;\r
3427   \r
3428       /* Enable the DMA Stream */\r
3429       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);     \r
3430     }\r
3431     break;\r
3432     case TIM_DMA_COM:\r
3433     {  \r
3434       /* Set the DMA Period elapsed callback */\r
3435       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = HAL_TIMEx_DMACommutationCplt;\r
3436      \r
3437       /* Set the DMA error callback */\r
3438       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = HAL_TIM_DMAError ;\r
3439   \r
3440       /* Enable the DMA Stream */\r
3441       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);     \r
3442     }\r
3443     break;\r
3444     case TIM_DMA_TRIGGER:\r
3445     {  \r
3446       /* Set the DMA Period elapsed callback */\r
3447       htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;\r
3448      \r
3449       /* Set the DMA error callback */\r
3450       htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = HAL_TIM_DMAError ;\r
3451   \r
3452       /* Enable the DMA Stream */\r
3453       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);     \r
3454     }\r
3455     break;\r
3456     default:\r
3457     break;  \r
3458   }\r
3459    /* configure the DMA Burst Mode */\r
3460    htim->Instance->DCR = BurstBaseAddress | BurstLength;  \r
3461    \r
3462    /* Enable the TIM DMA Request */\r
3463    __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);  \r
3464    \r
3465    htim->State = HAL_TIM_STATE_READY;\r
3466   \r
3467   /* Return function status */\r
3468   return HAL_OK;\r
3469 }\r
3470 \r
3471 /**\r
3472   * @brief  Stops the TIM DMA Burst mode \r
3473   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3474   *                the configuration information for TIM module.\r
3475   * @param  BurstRequestSrc: TIM DMA Request sources to disable\r
3476   * @retval HAL status\r
3477   */\r
3478 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)\r
3479 {\r
3480   /* Check the parameters */\r
3481   assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));\r
3482   \r
3483   /* Abort the DMA transfer (at least disable the DMA channel) */\r
3484   switch(BurstRequestSrc)\r
3485   {\r
3486     case TIM_DMA_UPDATE:\r
3487     {  \r
3488       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_UPDATE]);\r
3489     }\r
3490     break;\r
3491     case TIM_DMA_CC1:\r
3492     {  \r
3493       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC1]);\r
3494     }\r
3495     break;\r
3496     case TIM_DMA_CC2:\r
3497     {  \r
3498       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC2]);\r
3499     }\r
3500     break;\r
3501     case TIM_DMA_CC3:\r
3502     {  \r
3503       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC3]);\r
3504     }\r
3505     break;\r
3506     case TIM_DMA_CC4:\r
3507     {  \r
3508       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC4]);\r
3509     }\r
3510     break;\r
3511     case TIM_DMA_COM:\r
3512     {  \r
3513       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_COMMUTATION]);\r
3514     }\r
3515     break;\r
3516     case TIM_DMA_TRIGGER:\r
3517     {  \r
3518       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_TRIGGER]);\r
3519     }\r
3520     break;\r
3521     default:\r
3522     break;\r
3523   }\r
3524 \r
3525   /* Disable the TIM Update DMA request */\r
3526   __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);\r
3527       \r
3528   /* Return function status */\r
3529   return HAL_OK;  \r
3530 }\r
3531 \r
3532 /**\r
3533   * @brief  Configure the DMA Burst to transfer Data from the TIM peripheral to the memory \r
3534   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3535   *                the configuration information for TIM module.\r
3536   * @param  BurstBaseAddress: TIM Base address from when the DMA will starts the Data read.\r
3537   *         This parameters can be on of the following values:\r
3538   *            @arg TIM_DMABASE_CR1  \r
3539   *            @arg TIM_DMABASE_CR2\r
3540   *            @arg TIM_DMABASE_SMCR\r
3541   *            @arg TIM_DMABASE_DIER\r
3542   *            @arg TIM_DMABASE_SR\r
3543   *            @arg TIM_DMABASE_EGR\r
3544   *            @arg TIM_DMABASE_CCMR1\r
3545   *            @arg TIM_DMABASE_CCMR2\r
3546   *            @arg TIM_DMABASE_CCER\r
3547   *            @arg TIM_DMABASE_CNT   \r
3548   *            @arg TIM_DMABASE_PSC   \r
3549   *            @arg TIM_DMABASE_ARR\r
3550   *            @arg TIM_DMABASE_RCR\r
3551   *            @arg TIM_DMABASE_CCR1\r
3552   *            @arg TIM_DMABASE_CCR2\r
3553   *            @arg TIM_DMABASE_CCR3  \r
3554   *            @arg TIM_DMABASE_CCR4\r
3555   *            @arg TIM_DMABASE_BDTR\r
3556   *            @arg TIM_DMABASE_DCR\r
3557   * @param  BurstRequestSrc: TIM DMA Request sources.\r
3558   *         This parameters can be on of the following values:\r
3559   *            @arg TIM_DMA_UPDATE: TIM update Interrupt source\r
3560   *            @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source\r
3561   *            @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source\r
3562   *            @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source\r
3563   *            @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source\r
3564   *            @arg TIM_DMA_COM: TIM Commutation DMA source\r
3565   *            @arg TIM_DMA_TRIGGER: TIM Trigger DMA source\r
3566   * @param  BurstBuffer: The Buffer address.\r
3567   * @param  BurstLength: DMA Burst length. This parameter can be one value\r
3568   *         between TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.\r
3569   * @retval HAL status\r
3570   */\r
3571 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress, uint32_t BurstRequestSrc,\r
3572                                              uint32_t  *BurstBuffer, uint32_t  BurstLength)\r
3573 {\r
3574   /* Check the parameters */\r
3575   assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));\r
3576   assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));\r
3577   assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));\r
3578   assert_param(IS_TIM_DMA_LENGTH(BurstLength));\r
3579   \r
3580   if((htim->State == HAL_TIM_STATE_BUSY))\r
3581   {\r
3582      return HAL_BUSY;\r
3583   }\r
3584   else if((htim->State == HAL_TIM_STATE_READY))\r
3585   {\r
3586     if((BurstBuffer == 0 ) && (BurstLength > 0)) \r
3587     {\r
3588       return HAL_ERROR;                                    \r
3589     }\r
3590     else\r
3591     {\r
3592       htim->State = HAL_TIM_STATE_BUSY;\r
3593     }\r
3594   }  \r
3595   switch(BurstRequestSrc)\r
3596   {\r
3597     case TIM_DMA_UPDATE:\r
3598     {  \r
3599       /* Set the DMA Period elapsed callback */\r
3600       htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;\r
3601      \r
3602       /* Set the DMA error callback */\r
3603       htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = HAL_TIM_DMAError ;\r
3604   \r
3605       /* Enable the DMA Stream */\r
3606        HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);     \r
3607     }\r
3608     break;\r
3609     case TIM_DMA_CC1:\r
3610     {  \r
3611       /* Set the DMA Period elapsed callback */\r
3612       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
3613      \r
3614       /* Set the DMA error callback */\r
3615       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
3616   \r
3617       /* Enable the DMA Stream */\r
3618       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);      \r
3619     }\r
3620     break;\r
3621     case TIM_DMA_CC2:\r
3622     {  \r
3623       /* Set the DMA Period elapsed callback */\r
3624       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
3625      \r
3626       /* Set the DMA error callback */\r
3627       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;\r
3628   \r
3629       /* Enable the DMA Stream */\r
3630       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);     \r
3631     }\r
3632     break;\r
3633     case TIM_DMA_CC3:\r
3634     {  \r
3635       /* Set the DMA Period elapsed callback */\r
3636       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
3637      \r
3638       /* Set the DMA error callback */\r
3639       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;\r
3640   \r
3641       /* Enable the DMA Stream */\r
3642       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);      \r
3643     }\r
3644     break;\r
3645     case TIM_DMA_CC4:\r
3646     {  \r
3647       /* Set the DMA Period elapsed callback */\r
3648       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMACaptureCplt;\r
3649      \r
3650       /* Set the DMA error callback */\r
3651       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;\r
3652   \r
3653       /* Enable the DMA Stream */\r
3654       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);      \r
3655     }\r
3656     break;\r
3657     case TIM_DMA_COM:\r
3658     {  \r
3659       /* Set the DMA Period elapsed callback */\r
3660       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = HAL_TIMEx_DMACommutationCplt;\r
3661      \r
3662       /* Set the DMA error callback */\r
3663       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = HAL_TIM_DMAError ;\r
3664   \r
3665       /* Enable the DMA Stream */\r
3666       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);      \r
3667     }\r
3668     break;\r
3669     case TIM_DMA_TRIGGER:\r
3670     {  \r
3671       /* Set the DMA Period elapsed callback */\r
3672       htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;\r
3673      \r
3674       /* Set the DMA error callback */\r
3675       htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = HAL_TIM_DMAError ;\r
3676   \r
3677       /* Enable the DMA Stream */\r
3678       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);      \r
3679     }\r
3680     break;\r
3681     default:\r
3682     break;  \r
3683   }\r
3684 \r
3685   /* configure the DMA Burst Mode */\r
3686   htim->Instance->DCR = BurstBaseAddress | BurstLength;  \r
3687   \r
3688   /* Enable the TIM DMA Request */\r
3689   __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);\r
3690   \r
3691   htim->State = HAL_TIM_STATE_READY;\r
3692   \r
3693   /* Return function status */\r
3694   return HAL_OK;\r
3695 }\r
3696 \r
3697 /**\r
3698   * @brief  Stop the DMA burst reading \r
3699   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3700   *                the configuration information for TIM module.\r
3701   * @param  BurstRequestSrc: TIM DMA Request sources to disable.\r
3702   * @retval HAL status\r
3703   */\r
3704 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)\r
3705 {\r
3706   /* Check the parameters */\r
3707   assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));\r
3708   \r
3709   /* Abort the DMA transfer (at least disable the DMA channel) */\r
3710   switch(BurstRequestSrc)\r
3711   {\r
3712     case TIM_DMA_UPDATE:\r
3713     {  \r
3714       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_UPDATE]);\r
3715     }\r
3716     break;\r
3717     case TIM_DMA_CC1:\r
3718     {  \r
3719       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC1]);\r
3720     }\r
3721     break;\r
3722     case TIM_DMA_CC2:\r
3723     {  \r
3724       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC2]);\r
3725     }\r
3726     break;\r
3727     case TIM_DMA_CC3:\r
3728     {  \r
3729       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC3]);\r
3730     }\r
3731     break;\r
3732     case TIM_DMA_CC4:\r
3733     {  \r
3734       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC4]);\r
3735     }\r
3736     break;\r
3737     case TIM_DMA_COM:\r
3738     {  \r
3739       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_COMMUTATION]);\r
3740     }\r
3741     break;\r
3742     case TIM_DMA_TRIGGER:\r
3743     {  \r
3744       HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_TRIGGER]);\r
3745     }\r
3746     break;\r
3747     default:\r
3748     break;  \r
3749   }\r
3750   \r
3751   /* Disable the TIM Update DMA request */\r
3752   __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);\r
3753       \r
3754   /* Return function status */\r
3755   return HAL_OK;  \r
3756 }\r
3757 \r
3758 /**\r
3759   * @brief  Generate a software event\r
3760   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3761   *                the configuration information for TIM module.\r
3762   * @param  EventSource: specifies the event source.\r
3763   *          This parameter can be one of the following values:\r
3764   *            @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source\r
3765   *            @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source\r
3766   *            @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source\r
3767   *            @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source\r
3768   *            @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source\r
3769   *            @arg TIM_EVENTSOURCE_COM: Timer COM event source  \r
3770   *            @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source\r
3771   *            @arg TIM_EVENTSOURCE_BREAK: Timer Break event source\r
3772   *            @arg TIM_EVENTSOURCE_BREAK2: Timer Break2 event source  \r
3773   * @note   TIM6 and TIM7 can only generate an update event. \r
3774   * @note   TIM_EVENTSOURCE_COM, TIM_EVENTSOURCE_BREAK and TIM_EVENTSOURCE_BREAK2 are used only with TIM1 and TIM8.\r
3775   * @retval HAL status\r
3776   */ \r
3777 \r
3778 HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)\r
3779 {\r
3780   /* Check the parameters */\r
3781   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
3782   assert_param(IS_TIM_EVENT_SOURCE(EventSource));\r
3783   \r
3784   /* Process Locked */\r
3785   __HAL_LOCK(htim);\r
3786   \r
3787   /* Change the TIM state */\r
3788   htim->State = HAL_TIM_STATE_BUSY;\r
3789   \r
3790   /* Set the event sources */\r
3791   htim->Instance->EGR = EventSource;\r
3792   \r
3793   /* Change the TIM state */\r
3794   htim->State = HAL_TIM_STATE_READY;\r
3795   \r
3796   __HAL_UNLOCK(htim);\r
3797   \r
3798   /* Return function status */\r
3799   return HAL_OK;  \r
3800 }\r
3801 \r
3802 /**\r
3803   * @brief  Configures the OCRef clear feature\r
3804   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3805   *                the configuration information for TIM module.\r
3806   * @param  sClearInputConfig: pointer to a TIM_ClearInputConfigTypeDef structure that\r
3807   *         contains the OCREF clear feature and parameters for the TIM peripheral. \r
3808   * @param  Channel: specifies the TIM Channel.\r
3809   *          This parameter can be one of the following values:\r
3810   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
3811   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
3812   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
3813   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
3814   * @retval HAL status\r
3815   */ \r
3816 __weak HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim, TIM_ClearInputConfigTypeDef * sClearInputConfig, uint32_t Channel)\r
3817\r
3818   /* Check the parameters */\r
3819   assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
3820   assert_param(IS_TIM_CHANNELS(Channel));\r
3821   assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));\r
3822    \r
3823   /* Process Locked */\r
3824   __HAL_LOCK(htim);\r
3825   \r
3826   htim->State = HAL_TIM_STATE_BUSY;\r
3827   \r
3828   if(sClearInputConfig->ClearInputSource == TIM_CLEARINPUTSOURCE_ETR)\r
3829   {\r
3830     assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));\r
3831     assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));\r
3832     assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));\r
3833   \r
3834     TIM_ETR_SetConfig(htim->Instance, \r
3835                       sClearInputConfig->ClearInputPrescaler,\r
3836                       sClearInputConfig->ClearInputPolarity,\r
3837                       sClearInputConfig->ClearInputFilter);\r
3838   }\r
3839   \r
3840   switch (Channel)\r
3841   {\r
3842     case TIM_CHANNEL_1:\r
3843     {        \r
3844       if(sClearInputConfig->ClearInputState != RESET)  \r
3845       {\r
3846         /* Enable the Ocref clear feature for Channel 1 */\r
3847         htim->Instance->CCMR1 |= TIM_CCMR1_OC1CE;\r
3848       }\r
3849       else\r
3850       {\r
3851         /* Disable the Ocref clear feature for Channel 1 */\r
3852         htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1CE;      \r
3853       }\r
3854     }    \r
3855     break;\r
3856     case TIM_CHANNEL_2:    \r
3857     { \r
3858       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance)); \r
3859       if(sClearInputConfig->ClearInputState != RESET)  \r
3860       {\r
3861         /* Enable the Ocref clear feature for Channel 2 */\r
3862         htim->Instance->CCMR1 |= TIM_CCMR1_OC2CE;\r
3863       }\r
3864       else\r
3865       {\r
3866         /* Disable the Ocref clear feature for Channel 2 */\r
3867         htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2CE;      \r
3868       }\r
3869     } \r
3870     break;\r
3871     case TIM_CHANNEL_3:   \r
3872     {  \r
3873       assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));\r
3874       if(sClearInputConfig->ClearInputState != RESET)  \r
3875       {\r
3876         /* Enable the Ocref clear feature for Channel 3 */\r
3877         htim->Instance->CCMR2 |= TIM_CCMR2_OC3CE;\r
3878       }\r
3879       else\r
3880       {\r
3881         /* Disable the Ocref clear feature for Channel 3 */\r
3882         htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3CE;      \r
3883       }\r
3884     } \r
3885     break;\r
3886     case TIM_CHANNEL_4:    \r
3887     {  \r
3888       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));\r
3889       if(sClearInputConfig->ClearInputState != RESET)  \r
3890       {\r
3891         /* Enable the Ocref clear feature for Channel 4 */\r
3892         htim->Instance->CCMR2 |= TIM_CCMR2_OC4CE;\r
3893       }\r
3894       else\r
3895       {\r
3896         /* Disable the Ocref clear feature for Channel 4 */\r
3897         htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4CE;      \r
3898       }\r
3899     } \r
3900     break;\r
3901     default:  \r
3902     break;\r
3903   } \r
3904 \r
3905   htim->State = HAL_TIM_STATE_READY;\r
3906   \r
3907   __HAL_UNLOCK(htim);\r
3908   \r
3909   return HAL_OK;  \r
3910 }  \r
3911 \r
3912 /**\r
3913   * @brief   Configures the clock source to be used\r
3914   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
3915   *                the configuration information for TIM module.\r
3916   * @param  sClockSourceConfig: pointer to a TIM_ClockConfigTypeDef structure that\r
3917   *         contains the clock source information for the TIM peripheral. \r
3918   * @retval HAL status\r
3919   */ \r
3920 HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, TIM_ClockConfigTypeDef * sClockSourceConfig)    \r
3921 {\r
3922   uint32_t tmpsmcr = 0;\r
3923     \r
3924   /* Process Locked */\r
3925   __HAL_LOCK(htim);\r
3926   \r
3927   htim->State = HAL_TIM_STATE_BUSY;\r
3928   \r
3929   /* Check the parameters */\r
3930   assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));\r
3931   assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));\r
3932   assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));\r
3933   assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));\r
3934   \r
3935   /* Reset the SMS, TS, ECE, ETPS and ETRF bits */\r
3936   tmpsmcr = htim->Instance->SMCR;\r
3937   tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);\r
3938   tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);\r
3939   htim->Instance->SMCR = tmpsmcr;\r
3940   \r
3941   switch (sClockSourceConfig->ClockSource)\r
3942   {\r
3943     case TIM_CLOCKSOURCE_INTERNAL:\r
3944     { \r
3945       assert_param(IS_TIM_INSTANCE(htim->Instance));      \r
3946       /* Disable slave mode to clock the prescaler directly with the internal clock */\r
3947       htim->Instance->SMCR &= ~TIM_SMCR_SMS;\r
3948     }\r
3949     break;\r
3950     \r
3951     case TIM_CLOCKSOURCE_ETRMODE1:\r
3952     {\r
3953       assert_param(IS_TIM_ETR_INSTANCE(htim->Instance));\r
3954       /* Configure the ETR Clock source */\r
3955       TIM_ETR_SetConfig(htim->Instance, \r
3956                         sClockSourceConfig->ClockPrescaler, \r
3957                         sClockSourceConfig->ClockPolarity, \r
3958                         sClockSourceConfig->ClockFilter);\r
3959       /* Get the TIMx SMCR register value */\r
3960       tmpsmcr = htim->Instance->SMCR;\r
3961       /* Reset the SMS and TS Bits */\r
3962       tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);\r
3963       /* Select the External clock mode1 and the ETRF trigger */\r
3964       tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);\r
3965       /* Write to TIMx SMCR */\r
3966       htim->Instance->SMCR = tmpsmcr;\r
3967     }\r
3968     break;\r
3969     \r
3970     case TIM_CLOCKSOURCE_ETRMODE2:\r
3971     {\r
3972       assert_param(IS_TIM_ETR_INSTANCE(htim->Instance));\r
3973       /* Configure the ETR Clock source */\r
3974       TIM_ETR_SetConfig(htim->Instance, \r
3975                         sClockSourceConfig->ClockPrescaler, \r
3976                         sClockSourceConfig->ClockPolarity,\r
3977                         sClockSourceConfig->ClockFilter);\r
3978       /* Enable the External clock mode2 */\r
3979       htim->Instance->SMCR |= TIM_SMCR_ECE;\r
3980     }\r
3981     break;\r
3982     \r
3983     case TIM_CLOCKSOURCE_TI1:\r
3984     {\r
3985       assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));\r
3986       TIM_TI1_ConfigInputStage(htim->Instance, \r
3987                         sClockSourceConfig->ClockPolarity, \r
3988                         sClockSourceConfig->ClockFilter);\r
3989       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);\r
3990     }\r
3991     break;\r
3992     case TIM_CLOCKSOURCE_TI2:\r
3993     {\r
3994       assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));\r
3995       TIM_TI2_ConfigInputStage(htim->Instance, \r
3996                         sClockSourceConfig->ClockPolarity, \r
3997                         sClockSourceConfig->ClockFilter);\r
3998       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);\r
3999     }\r
4000     break;\r
4001     case TIM_CLOCKSOURCE_TI1ED:\r
4002     {\r
4003       assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));\r
4004       TIM_TI1_ConfigInputStage(htim->Instance, \r
4005                         sClockSourceConfig->ClockPolarity,\r
4006                         sClockSourceConfig->ClockFilter);\r
4007       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);\r
4008     }\r
4009     break;\r
4010     case TIM_CLOCKSOURCE_ITR0:\r
4011     {\r
4012       assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));\r
4013       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR0);\r
4014     }\r
4015     break;\r
4016     case TIM_CLOCKSOURCE_ITR1:\r
4017     {\r
4018       assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));\r
4019       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR1);\r
4020     }\r
4021     break;\r
4022     case TIM_CLOCKSOURCE_ITR2:\r
4023     {\r
4024       assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));\r
4025       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR2);\r
4026     }\r
4027     break;\r
4028     case TIM_CLOCKSOURCE_ITR3:\r
4029     {\r
4030       assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));\r
4031       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR3);\r
4032     }\r
4033     break;\r
4034     \r
4035     default:\r
4036     break;    \r
4037   }\r
4038   htim->State = HAL_TIM_STATE_READY;\r
4039   \r
4040   __HAL_UNLOCK(htim);\r
4041   \r
4042   return HAL_OK;\r
4043 }\r
4044 \r
4045 /**\r
4046   * @brief  Selects the signal connected to the TI1 input: direct from CH1_input\r
4047   *         or a XOR combination between CH1_input, CH2_input & CH3_input\r
4048   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4049   *                the configuration information for TIM module.\r
4050   * @param  TI1_Selection: Indicate whether or not channel 1 is connected to the\r
4051   *         output of a XOR gate.\r
4052   *         This parameter can be one of the following values:\r
4053   *            @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input\r
4054   *            @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3\r
4055   *            pins are connected to the TI1 input (XOR combination)\r
4056   * @retval HAL status\r
4057   */\r
4058 HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)\r
4059 {\r
4060   uint32_t tmpcr2 = 0;\r
4061   \r
4062   /* Check the parameters */\r
4063   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance)); \r
4064   assert_param(IS_TIM_TI1SELECTION(TI1_Selection));\r
4065 \r
4066   /* Get the TIMx CR2 register value */\r
4067   tmpcr2 = htim->Instance->CR2;\r
4068 \r
4069   /* Reset the TI1 selection */\r
4070   tmpcr2 &= ~TIM_CR2_TI1S;\r
4071 \r
4072   /* Set the TI1 selection */\r
4073   tmpcr2 |= TI1_Selection;\r
4074   \r
4075   /* Write to TIMxCR2 */\r
4076   htim->Instance->CR2 = tmpcr2;\r
4077 \r
4078   return HAL_OK;\r
4079 }\r
4080 \r
4081 /**\r
4082   * @brief  Configures the TIM in Slave mode\r
4083   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4084   *                the configuration information for TIM module.\r
4085   * @param  sSlaveConfig: pointer to a TIM_SlaveConfigTypeDef structure that\r
4086   *         contains the selected trigger (internal trigger input, filtered\r
4087   *         timer input or external trigger input) and the ) and the Slave \r
4088   *         mode (Disable, Reset, Gated, Trigger, External clock mode 1). \r
4089   * @retval HAL status\r
4090   */\r
4091 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchronization(TIM_HandleTypeDef *htim, TIM_SlaveConfigTypeDef * sSlaveConfig)\r
4092 {\r
4093   uint32_t tmpsmcr  = 0;\r
4094   uint32_t tmpccmr1 = 0;\r
4095   uint32_t tmpccer = 0;\r
4096 \r
4097   /* Check the parameters */\r
4098   assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));\r
4099   assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));\r
4100   assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));\r
4101    \r
4102   __HAL_LOCK(htim);\r
4103   \r
4104   htim->State = HAL_TIM_STATE_BUSY;\r
4105 \r
4106   /* Get the TIMx SMCR register value */\r
4107   tmpsmcr = htim->Instance->SMCR;\r
4108 \r
4109   /* Reset the Trigger Selection Bits */\r
4110   tmpsmcr &= ~TIM_SMCR_TS;\r
4111   /* Set the Input Trigger source */\r
4112   tmpsmcr |= sSlaveConfig->InputTrigger;\r
4113 \r
4114   /* Reset the slave mode Bits */\r
4115   tmpsmcr &= ~TIM_SMCR_SMS;\r
4116   /* Set the slave mode */\r
4117   tmpsmcr |= sSlaveConfig->SlaveMode;\r
4118 \r
4119   /* Write to TIMx SMCR */\r
4120   htim->Instance->SMCR = tmpsmcr;\r
4121   \r
4122   /* Configure the trigger prescaler, filter, and polarity */\r
4123   switch (sSlaveConfig->InputTrigger)\r
4124   {\r
4125   case TIM_TS_ETRF:\r
4126     {\r
4127       /* Check the parameters */\r
4128       assert_param(IS_TIM_ETR_INSTANCE(htim->Instance));\r
4129       assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));\r
4130       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));\r
4131       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));\r
4132       /* Configure the ETR Trigger source */\r
4133       TIM_ETR_SetConfig(htim->Instance, \r
4134                         sSlaveConfig->TriggerPrescaler, \r
4135                         sSlaveConfig->TriggerPolarity, \r
4136                         sSlaveConfig->TriggerFilter);\r
4137     }\r
4138     break;\r
4139     \r
4140   case TIM_TS_TI1F_ED:\r
4141     {\r
4142       /* Check the parameters */\r
4143       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
4144       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));\r
4145       \r
4146       /* Disable the Channel 1: Reset the CC1E Bit */\r
4147       tmpccer = htim->Instance->CCER;\r
4148       htim->Instance->CCER &= ~TIM_CCER_CC1E;\r
4149       tmpccmr1 = htim->Instance->CCMR1;    \r
4150       \r
4151       /* Set the filter */\r
4152       tmpccmr1 &= ~TIM_CCMR1_IC1F;\r
4153       tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4);\r
4154       \r
4155       /* Write to TIMx CCMR1 and CCER registers */\r
4156       htim->Instance->CCMR1 = tmpccmr1;\r
4157       htim->Instance->CCER = tmpccer;                               \r
4158                                \r
4159     }\r
4160     break;\r
4161     \r
4162   case TIM_TS_TI1FP1:\r
4163     {\r
4164       /* Check the parameters */\r
4165       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
4166       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));\r
4167       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));\r
4168 \r
4169       /* Configure TI1 Filter and Polarity */\r
4170       TIM_TI1_ConfigInputStage(htim->Instance,\r
4171                                sSlaveConfig->TriggerPolarity,\r
4172                                sSlaveConfig->TriggerFilter);\r
4173     }\r
4174     break;\r
4175     \r
4176   case TIM_TS_TI2FP2:\r
4177     {\r
4178       /* Check the parameters */\r
4179       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
4180       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));\r
4181       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));\r
4182       \r
4183       /* Configure TI2 Filter and Polarity */\r
4184       TIM_TI2_ConfigInputStage(htim->Instance,\r
4185                                 sSlaveConfig->TriggerPolarity,\r
4186                                 sSlaveConfig->TriggerFilter);\r
4187     }\r
4188     break;\r
4189     \r
4190   case TIM_TS_ITR0:\r
4191     {\r
4192       /* Check the parameter */\r
4193       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
4194     }\r
4195     break;\r
4196     \r
4197   case TIM_TS_ITR1:\r
4198     {\r
4199       /* Check the parameter */\r
4200       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
4201     }\r
4202     break;\r
4203     \r
4204   case TIM_TS_ITR2:\r
4205     {\r
4206       /* Check the parameter */\r
4207       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
4208     }\r
4209     break;\r
4210     \r
4211   case TIM_TS_ITR3:\r
4212     {\r
4213       /* Check the parameter */\r
4214       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
4215     }\r
4216     break;\r
4217        \r
4218   default:\r
4219     break;\r
4220   }\r
4221   \r
4222   htim->State = HAL_TIM_STATE_READY;\r
4223      \r
4224   __HAL_UNLOCK(htim);  \r
4225   \r
4226   return HAL_OK;\r
4227\r
4228 \r
4229 /**\r
4230   * @brief  Configures the TIM in Slave mode in interrupt mode\r
4231   * @param  htim: TIM handle.\r
4232   * @param  sSlaveConfig: pointer to a TIM_SlaveConfigTypeDef structure that\r
4233   *         contains the selected trigger (internal trigger input, filtered\r
4234   *         timer input or external trigger input) and the ) and the Slave \r
4235   *         mode (Disable, Reset, Gated, Trigger, External clock mode 1). \r
4236   * @retval HAL status\r
4237   */\r
4238 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchronization_IT(TIM_HandleTypeDef *htim, \r
4239                                                         TIM_SlaveConfigTypeDef * sSlaveConfig)\r
4240 {\r
4241   /* Check the parameters */\r
4242   assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));\r
4243   assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));\r
4244   assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));\r
4245   \r
4246   __HAL_LOCK(htim);\r
4247 \r
4248   htim->State = HAL_TIM_STATE_BUSY;\r
4249   \r
4250   TIM_SlaveTimer_SetConfig(htim, sSlaveConfig);\r
4251   \r
4252   /* Enable Trigger Interrupt */\r
4253   __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);\r
4254   \r
4255   /* Disable Trigger DMA request */\r
4256   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);\r
4257   \r
4258   htim->State = HAL_TIM_STATE_READY;\r
4259      \r
4260   __HAL_UNLOCK(htim);  \r
4261   \r
4262   return HAL_OK;\r
4263 }\r
4264 \r
4265 /**\r
4266   * @brief  Read the captured value from Capture Compare unit\r
4267   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4268   *                the configuration information for TIM module.\r
4269   * @param  Channel: TIM Channels to be enabled.\r
4270   *          This parameter can be one of the following values:\r
4271   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
4272   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
4273   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
4274   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
4275   * @retval Captured value\r
4276   */\r
4277 uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)\r
4278 {\r
4279   uint32_t tmpreg = 0;\r
4280   \r
4281   __HAL_LOCK(htim);\r
4282   \r
4283   switch (Channel)\r
4284   {\r
4285     case TIM_CHANNEL_1:\r
4286     {\r
4287       /* Check the parameters */\r
4288       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
4289       \r
4290       /* Return the capture 1 value */\r
4291       tmpreg = htim->Instance->CCR1;\r
4292       \r
4293       break;\r
4294     }\r
4295     case TIM_CHANNEL_2:\r
4296     {\r
4297       /* Check the parameters */\r
4298       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
4299       \r
4300       /* Return the capture 2 value */\r
4301       tmpreg = htim->Instance->CCR2;\r
4302       \r
4303       break;\r
4304     }\r
4305     \r
4306     case TIM_CHANNEL_3:\r
4307     {\r
4308       /* Check the parameters */\r
4309       assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));\r
4310       \r
4311       /* Return the capture 3 value */\r
4312       tmpreg = htim->Instance->CCR3;\r
4313       \r
4314       break;\r
4315     }\r
4316     \r
4317     case TIM_CHANNEL_4:\r
4318     {\r
4319       /* Check the parameters */\r
4320       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));\r
4321       \r
4322       /* Return the capture 4 value */\r
4323       tmpreg = htim->Instance->CCR4;\r
4324       \r
4325       break;\r
4326     }\r
4327     \r
4328     default:\r
4329     break;  \r
4330   }\r
4331      \r
4332   __HAL_UNLOCK(htim);  \r
4333   return tmpreg;\r
4334 }\r
4335 \r
4336 /**\r
4337   * @}\r
4338   */\r
4339   \r
4340 /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions\r
4341  *  @brief    TIM Callbacks functions \r
4342  *\r
4343 @verbatim   \r
4344   ==============================================================================\r
4345                         ##### TIM Callbacks functions #####\r
4346   ==============================================================================  \r
4347  [..]  \r
4348    This section provides TIM callback functions:\r
4349    (+) Timer Period elapsed callback\r
4350    (+) Timer Output Compare callback\r
4351    (+) Timer Input capture callback\r
4352    (+) Timer Trigger callback\r
4353    (+) Timer Error callback\r
4354 \r
4355 @endverbatim\r
4356   * @{\r
4357   */\r
4358 \r
4359 /**\r
4360   * @brief  Period elapsed callback in non blocking mode \r
4361   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4362   *                the configuration information for TIM module.\r
4363   * @retval None\r
4364   */\r
4365 __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)\r
4366 {\r
4367   /* NOTE : This function Should not be modified, when the callback is needed,\r
4368             the __HAL_TIM_PeriodElapsedCallback could be implemented in the user file\r
4369    */\r
4370   \r
4371 }\r
4372 /**\r
4373   * @brief  Output Compare callback in non blocking mode \r
4374   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4375   *                the configuration information for TIM module.\r
4376   * @retval None\r
4377   */\r
4378 __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)\r
4379 {\r
4380   /* NOTE : This function Should not be modified, when the callback is needed,\r
4381             the __HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file\r
4382    */\r
4383 }\r
4384 /**\r
4385   * @brief  Input Capture callback in non blocking mode \r
4386   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4387   *                the configuration information for TIM module.\r
4388   * @retval None\r
4389   */\r
4390 __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)\r
4391 {\r
4392   /* NOTE : This function Should not be modified, when the callback is needed,\r
4393             the __HAL_TIM_IC_CaptureCallback could be implemented in the user file\r
4394    */\r
4395 }\r
4396 \r
4397 /**\r
4398   * @brief  PWM Pulse finished callback in non blocking mode \r
4399   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4400   *                the configuration information for TIM module.\r
4401   * @retval None\r
4402   */\r
4403 __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)\r
4404 {\r
4405   /* NOTE : This function Should not be modified, when the callback is needed,\r
4406             the __HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file\r
4407    */\r
4408 }\r
4409 \r
4410 /**\r
4411   * @brief  Hall Trigger detection callback in non blocking mode \r
4412   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4413   *                the configuration information for TIM module.\r
4414   * @retval None\r
4415   */\r
4416 __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)\r
4417 {\r
4418   /* NOTE : This function Should not be modified, when the callback is needed,\r
4419             the HAL_TIM_TriggerCallback could be implemented in the user file\r
4420    */\r
4421 }\r
4422 \r
4423 /**\r
4424   * @brief  Timer error callback in non blocking mode \r
4425   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4426   *                the configuration information for TIM module.\r
4427   * @retval None\r
4428   */\r
4429 __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)\r
4430 {\r
4431   /* NOTE : This function Should not be modified, when the callback is needed,\r
4432             the HAL_TIM_ErrorCallback could be implemented in the user file\r
4433    */\r
4434 }\r
4435 \r
4436 /**\r
4437   * @}\r
4438   */\r
4439 \r
4440 /** @defgroup TIM_Exported_Functions_Group10 Peripheral State functions \r
4441  *  @brief   Peripheral State functions \r
4442  *\r
4443 @verbatim   \r
4444   ==============================================================================\r
4445                         ##### Peripheral State functions #####\r
4446   ==============================================================================  \r
4447   [..]\r
4448     This subsection permits to get in run-time the status of the peripheral \r
4449     and the data flow.\r
4450 \r
4451 @endverbatim\r
4452   * @{\r
4453   */\r
4454 \r
4455 /**\r
4456   * @brief  Return the TIM Base state\r
4457   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4458   *                the configuration information for TIM module.\r
4459   * @retval HAL state\r
4460   */\r
4461 HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(TIM_HandleTypeDef *htim)\r
4462 {\r
4463   return htim->State;\r
4464 }\r
4465 \r
4466 /**\r
4467   * @brief  Return the TIM OC state\r
4468   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4469   *                the configuration information for TIM module.\r
4470   * @retval HAL state\r
4471   */\r
4472 HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(TIM_HandleTypeDef *htim)\r
4473 {\r
4474   return htim->State;\r
4475 }\r
4476 \r
4477 /**\r
4478   * @brief  Return the TIM PWM state\r
4479   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4480   *                the configuration information for TIM module.\r
4481   * @retval HAL state\r
4482   */\r
4483 HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(TIM_HandleTypeDef *htim)\r
4484 {\r
4485   return htim->State;\r
4486 }\r
4487 \r
4488 /**\r
4489   * @brief  Return the TIM Input Capture state\r
4490   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4491   *                the configuration information for TIM module.\r
4492   * @retval HAL state\r
4493   */\r
4494 HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(TIM_HandleTypeDef *htim)\r
4495 {\r
4496   return htim->State;\r
4497 }\r
4498 \r
4499 /**\r
4500   * @brief  Return the TIM One Pulse Mode state\r
4501   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4502   *                the configuration information for TIM module.\r
4503   * @retval HAL state\r
4504   */\r
4505 HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(TIM_HandleTypeDef *htim)\r
4506 {\r
4507   return htim->State;\r
4508 }\r
4509 \r
4510 /**\r
4511   * @brief  Return the TIM Encoder Mode state\r
4512   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4513   *                the configuration information for TIM module.\r
4514   * @retval HAL state\r
4515   */\r
4516 HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(TIM_HandleTypeDef *htim)\r
4517 {\r
4518   return htim->State;\r
4519 }\r
4520 \r
4521 /**\r
4522   * @}\r
4523   */\r
4524 \r
4525 /**\r
4526   * @brief  TIM DMA error callback \r
4527   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
4528   *                the configuration information for the specified DMA module.\r
4529   * @retval None\r
4530   */\r
4531 void HAL_TIM_DMAError(DMA_HandleTypeDef *hdma)\r
4532 {\r
4533   TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
4534   \r
4535   htim->State= HAL_TIM_STATE_READY;\r
4536    \r
4537   HAL_TIM_ErrorCallback(htim);\r
4538 }\r
4539 \r
4540 /**\r
4541   * @brief  TIM DMA Delay Pulse complete callback. \r
4542   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
4543   *                the configuration information for the specified DMA module.\r
4544   * @retval None\r
4545   */\r
4546 void HAL_TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)\r
4547 {\r
4548   TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
4549   \r
4550   htim->State= HAL_TIM_STATE_READY; \r
4551   \r
4552   if (hdma == htim->hdma[TIM_DMA_ID_CC1])\r
4553   {\r
4554     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;\r
4555   }\r
4556   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])\r
4557   {\r
4558     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;\r
4559   }\r
4560   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])\r
4561   {\r
4562     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;\r
4563   }\r
4564   else if (hdma == htim->hdma[TIM_DMA_ID_CC4])\r
4565   {\r
4566     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;\r
4567   }\r
4568 \r
4569   HAL_TIM_PWM_PulseFinishedCallback(htim);\r
4570 \r
4571   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;\r
4572 }\r
4573 /**\r
4574   * @brief  TIM DMA Capture complete callback. \r
4575   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
4576   *                the configuration information for the specified DMA module.\r
4577   * @retval None\r
4578   */\r
4579 void HAL_TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)\r
4580 {\r
4581   TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
4582     \r
4583    htim->State= HAL_TIM_STATE_READY; \r
4584     \r
4585   if (hdma == htim->hdma[TIM_DMA_ID_CC1])\r
4586   {\r
4587     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;\r
4588   }\r
4589   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])\r
4590   {\r
4591     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;\r
4592   }\r
4593   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])\r
4594   {\r
4595     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;\r
4596   }\r
4597   else if (hdma == htim->hdma[TIM_DMA_ID_CC4])\r
4598   {\r
4599     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;\r
4600   }\r
4601   \r
4602   HAL_TIM_IC_CaptureCallback(htim); \r
4603   \r
4604   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;\r
4605 \r
4606 }\r
4607 \r
4608 /**\r
4609   * @brief  TIM DMA Period Elapse complete callback. \r
4610   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
4611   *                the configuration information for the specified DMA module.\r
4612   * @retval None\r
4613   */\r
4614 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)\r
4615 {\r
4616   TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
4617   \r
4618   htim->State= HAL_TIM_STATE_READY;\r
4619   \r
4620   HAL_TIM_PeriodElapsedCallback(htim);\r
4621 }\r
4622 \r
4623 /**\r
4624   * @brief  TIM DMA Trigger callback. \r
4625   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
4626   *                the configuration information for the specified DMA module.\r
4627   * @retval None\r
4628   */\r
4629 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)\r
4630 {\r
4631   TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;  \r
4632   \r
4633   htim->State= HAL_TIM_STATE_READY; \r
4634   \r
4635   HAL_TIM_TriggerCallback(htim);\r
4636 }\r
4637 \r
4638 /**\r
4639   * @brief  Time Base configuration\r
4640   * @param  TIMx: TIM peripheral\r
4641   * @param  Structure: pointer on TIM Time Base required parameters  \r
4642   * @retval None\r
4643   */\r
4644 void TIM_Base_SetConfig(TIM_TypeDef *TIMx, TIM_Base_InitTypeDef *Structure)\r
4645 {\r
4646   uint32_t tmpcr1 = 0;\r
4647   tmpcr1 = TIMx->CR1;\r
4648   \r
4649   /* Set TIM Time Base Unit parameters ---------------------------------------*/\r
4650   if(IS_TIM_CC3_INSTANCE(TIMx) != RESET)   \r
4651   {\r
4652     /* Select the Counter Mode */\r
4653     tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);\r
4654     tmpcr1 |= Structure->CounterMode;\r
4655   }\r
4656  \r
4657   if(IS_TIM_CC1_INSTANCE(TIMx) != RESET)  \r
4658   {\r
4659     /* Set the clock division */\r
4660     tmpcr1 &= ~TIM_CR1_CKD;\r
4661     tmpcr1 |= (uint32_t)Structure->ClockDivision;\r
4662   }\r
4663 \r
4664   TIMx->CR1 = tmpcr1;\r
4665 \r
4666   /* Set the Auto-reload value */\r
4667   TIMx->ARR = (uint32_t)Structure->Period ;\r
4668  \r
4669   /* Set the Prescaler value */\r
4670   TIMx->PSC = (uint32_t)Structure->Prescaler;\r
4671     \r
4672   if(IS_TIM_ADVANCED_INSTANCE(TIMx) != RESET)  \r
4673   {\r
4674     /* Set the Repetition Counter value */\r
4675     TIMx->RCR = Structure->RepetitionCounter;\r
4676   }\r
4677 \r
4678   /* Generate an update event to reload the Prescaler \r
4679      and the repetition counter(only for TIM1 and TIM8) value immediately */\r
4680   TIMx->EGR = TIM_EGR_UG;\r
4681 }\r
4682 \r
4683 /**\r
4684   * @brief  Time Output Compare 1 configuration\r
4685   * @param  TIMx to select the TIM peripheral\r
4686   * @param  OC_Config: The output configuration structure\r
4687   * @retval None\r
4688   */\r
4689 void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)\r
4690 {\r
4691   uint32_t tmpccmrx = 0;\r
4692   uint32_t tmpccer = 0;\r
4693   uint32_t tmpcr2 = 0;  \r
4694 \r
4695   /* Disable the Channel 1: Reset the CC1E Bit */\r
4696   TIMx->CCER &= ~TIM_CCER_CC1E;\r
4697   \r
4698   /* Get the TIMx CCER register value */\r
4699   tmpccer = TIMx->CCER;\r
4700   /* Get the TIMx CR2 register value */\r
4701   tmpcr2 = TIMx->CR2;\r
4702   \r
4703   /* Get the TIMx CCMR1 register value */\r
4704   tmpccmrx = TIMx->CCMR1;\r
4705     \r
4706   /* Reset the Output Compare Mode Bits */\r
4707   tmpccmrx &= ~TIM_CCMR1_OC1M;\r
4708   tmpccmrx &= ~TIM_CCMR1_CC1S;\r
4709   /* Select the Output Compare Mode */\r
4710   tmpccmrx |= OC_Config->OCMode;\r
4711   \r
4712   /* Reset the Output Polarity level */\r
4713   tmpccer &= ~TIM_CCER_CC1P;\r
4714   /* Set the Output Compare Polarity */\r
4715   tmpccer |= OC_Config->OCPolarity;\r
4716 \r
4717     \r
4718   if(IS_TIM_ADVANCED_INSTANCE(TIMx) != RESET)\r
4719   {   \r
4720     /* Reset the Output N Polarity level */\r
4721     tmpccer &= ~TIM_CCER_CC1NP;\r
4722     /* Set the Output N Polarity */\r
4723     tmpccer |= OC_Config->OCNPolarity;\r
4724     /* Reset the Output N State */\r
4725     tmpccer &= ~TIM_CCER_CC1NE;\r
4726     \r
4727     /* Reset the Output Compare and Output Compare N IDLE State */\r
4728     tmpcr2 &= ~TIM_CR2_OIS1;\r
4729     tmpcr2 &= ~TIM_CR2_OIS1N;\r
4730     /* Set the Output Idle state */\r
4731     tmpcr2 |= OC_Config->OCIdleState;\r
4732     /* Set the Output N Idle state */\r
4733     tmpcr2 |= OC_Config->OCNIdleState;\r
4734   }\r
4735   /* Write to TIMx CR2 */\r
4736   TIMx->CR2 = tmpcr2;\r
4737   \r
4738   /* Write to TIMx CCMR1 */\r
4739   TIMx->CCMR1 = tmpccmrx;\r
4740   \r
4741   /* Set the Capture Compare Register value */\r
4742   TIMx->CCR1 = OC_Config->Pulse;\r
4743   \r
4744   /* Write to TIMx CCER */\r
4745   TIMx->CCER = tmpccer;  \r
4746\r
4747 \r
4748 /**\r
4749   * @brief  Time Output Compare 2 configuration\r
4750   * @param  TIMx to select the TIM peripheral\r
4751   * @param  OC_Config: The output configuration structure\r
4752   * @retval None\r
4753   */\r
4754 void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)\r
4755 {\r
4756   uint32_t tmpccmrx = 0;\r
4757   uint32_t tmpccer = 0;\r
4758   uint32_t tmpcr2 = 0;\r
4759    \r
4760   /* Disable the Channel 2: Reset the CC2E Bit */\r
4761   TIMx->CCER &= ~TIM_CCER_CC2E;\r
4762   \r
4763   /* Get the TIMx CCER register value */  \r
4764   tmpccer = TIMx->CCER;\r
4765   /* Get the TIMx CR2 register value */\r
4766   tmpcr2 = TIMx->CR2;\r
4767   \r
4768   /* Get the TIMx CCMR1 register value */\r
4769   tmpccmrx = TIMx->CCMR1;\r
4770     \r
4771   /* Reset the Output Compare mode and Capture/Compare selection Bits */\r
4772   tmpccmrx &= ~TIM_CCMR1_OC2M;\r
4773   tmpccmrx &= ~TIM_CCMR1_CC2S;\r
4774   \r
4775   /* Select the Output Compare Mode */\r
4776   tmpccmrx |= (OC_Config->OCMode << 8);\r
4777   \r
4778   /* Reset the Output Polarity level */\r
4779   tmpccer &= ~TIM_CCER_CC2P;\r
4780   /* Set the Output Compare Polarity */\r
4781   tmpccer |= (OC_Config->OCPolarity << 4);\r
4782     \r
4783   if(IS_TIM_ADVANCED_INSTANCE(TIMx) != RESET)\r
4784   {\r
4785     assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));\r
4786     assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));\r
4787     assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));\r
4788     \r
4789     /* Reset the Output N Polarity level */\r
4790     tmpccer &= ~TIM_CCER_CC2NP;\r
4791     /* Set the Output N Polarity */\r
4792     tmpccer |= (OC_Config->OCNPolarity << 4);\r
4793     /* Reset the Output N State */\r
4794     tmpccer &= ~TIM_CCER_CC2NE;\r
4795     \r
4796     /* Reset the Output Compare and Output Compare N IDLE State */\r
4797     tmpcr2 &= ~TIM_CR2_OIS2;\r
4798     tmpcr2 &= ~TIM_CR2_OIS2N;\r
4799     /* Set the Output Idle state */\r
4800     tmpcr2 |= (OC_Config->OCIdleState << 2);\r
4801     /* Set the Output N Idle state */\r
4802     tmpcr2 |= (OC_Config->OCNIdleState << 2);\r
4803   }\r
4804   /* Write to TIMx CR2 */\r
4805   TIMx->CR2 = tmpcr2;\r
4806   \r
4807   /* Write to TIMx CCMR1 */\r
4808   TIMx->CCMR1 = tmpccmrx;\r
4809   \r
4810   /* Set the Capture Compare Register value */\r
4811   TIMx->CCR2 = OC_Config->Pulse;\r
4812   \r
4813   /* Write to TIMx CCER */\r
4814   TIMx->CCER = tmpccer;\r
4815 }\r
4816 \r
4817 /**\r
4818   * @brief  Time Output Compare 3 configuration\r
4819   * @param  TIMx to select the TIM peripheral\r
4820   * @param  OC_Config: The output configuration structure\r
4821   * @retval None\r
4822   */\r
4823 void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)\r
4824 {\r
4825   uint32_t tmpccmrx = 0;\r
4826   uint32_t tmpccer = 0;\r
4827   uint32_t tmpcr2 = 0;   \r
4828 \r
4829   /* Disable the Channel 3: Reset the CC2E Bit */\r
4830   TIMx->CCER &= ~TIM_CCER_CC3E;\r
4831   \r
4832   /* Get the TIMx CCER register value */\r
4833   tmpccer = TIMx->CCER;\r
4834   /* Get the TIMx CR2 register value */\r
4835   tmpcr2 = TIMx->CR2;\r
4836   \r
4837   /* Get the TIMx CCMR2 register value */\r
4838   tmpccmrx = TIMx->CCMR2;\r
4839     \r
4840   /* Reset the Output Compare mode and Capture/Compare selection Bits */\r
4841   tmpccmrx &= ~TIM_CCMR2_OC3M;\r
4842   tmpccmrx &= ~TIM_CCMR2_CC3S;  \r
4843   /* Select the Output Compare Mode */\r
4844   tmpccmrx |= OC_Config->OCMode;\r
4845   \r
4846   /* Reset the Output Polarity level */\r
4847   tmpccer &= ~TIM_CCER_CC3P;\r
4848   /* Set the Output Compare Polarity */\r
4849   tmpccer |= (OC_Config->OCPolarity << 8);\r
4850     \r
4851   if(IS_TIM_ADVANCED_INSTANCE(TIMx) != RESET)\r
4852   {\r
4853     assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));\r
4854     assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));\r
4855     assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));\r
4856     \r
4857     /* Reset the Output N Polarity level */\r
4858     tmpccer &= ~TIM_CCER_CC3NP;\r
4859     /* Set the Output N Polarity */\r
4860     tmpccer |= (OC_Config->OCNPolarity << 8);\r
4861     /* Reset the Output N State */\r
4862     tmpccer &= ~TIM_CCER_CC3NE;\r
4863     \r
4864     /* Reset the Output Compare and Output Compare N IDLE State */\r
4865     tmpcr2 &= ~TIM_CR2_OIS3;\r
4866     tmpcr2 &= ~TIM_CR2_OIS3N;\r
4867     /* Set the Output Idle state */\r
4868     tmpcr2 |= (OC_Config->OCIdleState << 4);\r
4869     /* Set the Output N Idle state */\r
4870     tmpcr2 |= (OC_Config->OCNIdleState << 4);\r
4871   }\r
4872   /* Write to TIMx CR2 */\r
4873   TIMx->CR2 = tmpcr2;\r
4874   \r
4875   /* Write to TIMx CCMR2 */\r
4876   TIMx->CCMR2 = tmpccmrx;\r
4877   \r
4878   /* Set the Capture Compare Register value */\r
4879   TIMx->CCR3 = OC_Config->Pulse;\r
4880   \r
4881   /* Write to TIMx CCER */\r
4882   TIMx->CCER = tmpccer;\r
4883 }\r
4884 \r
4885 /**\r
4886   * @brief  Time Output Compare 4 configuration\r
4887   * @param  TIMx to select the TIM peripheral\r
4888   * @param  OC_Config: The output configuration structure\r
4889   * @retval None\r
4890   */\r
4891 void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)\r
4892 {\r
4893   uint32_t tmpccmrx = 0;\r
4894   uint32_t tmpccer = 0;\r
4895   uint32_t tmpcr2 = 0;\r
4896 \r
4897   /* Disable the Channel 4: Reset the CC4E Bit */\r
4898   TIMx->CCER &= ~TIM_CCER_CC4E;\r
4899   \r
4900   /* Get the TIMx CCER register value */\r
4901   tmpccer = TIMx->CCER;\r
4902   /* Get the TIMx CR2 register value */\r
4903   tmpcr2 = TIMx->CR2;\r
4904   \r
4905   /* Get the TIMx CCMR2 register value */\r
4906   tmpccmrx = TIMx->CCMR2;\r
4907     \r
4908   /* Reset the Output Compare mode and Capture/Compare selection Bits */\r
4909   tmpccmrx &= ~TIM_CCMR2_OC4M;\r
4910   tmpccmrx &= ~TIM_CCMR2_CC4S;\r
4911   \r
4912   /* Select the Output Compare Mode */\r
4913   tmpccmrx |= (OC_Config->OCMode << 8);\r
4914   \r
4915   /* Reset the Output Polarity level */\r
4916   tmpccer &= ~TIM_CCER_CC4P;\r
4917   /* Set the Output Compare Polarity */\r
4918   tmpccer |= (OC_Config->OCPolarity << 12);\r
4919    \r
4920   /*if((TIMx == TIM1) || (TIMx == TIM8))*/\r
4921   if(IS_TIM_ADVANCED_INSTANCE(TIMx) != RESET)\r
4922   {\r
4923     assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));\r
4924     /* Reset the Output Compare IDLE State */\r
4925     tmpcr2 &= ~TIM_CR2_OIS4;\r
4926     /* Set the Output Idle state */\r
4927     tmpcr2 |= (OC_Config->OCIdleState << 6);\r
4928   }\r
4929   /* Write to TIMx CR2 */\r
4930   TIMx->CR2 = tmpcr2;\r
4931   \r
4932   /* Write to TIMx CCMR2 */  \r
4933   TIMx->CCMR2 = tmpccmrx;\r
4934     \r
4935   /* Set the Capture Compare Register value */\r
4936   TIMx->CCR4 = OC_Config->Pulse;\r
4937   \r
4938   /* Write to TIMx CCER */\r
4939   TIMx->CCER = tmpccer;\r
4940 }\r
4941 \r
4942 /**\r
4943   * @brief  Time Output Compare 4 configuration\r
4944   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
4945   *                the configuration information for TIM module.\r
4946   * @param  sSlaveConfig: The slave configuration structure\r
4947   * @retval None\r
4948   */\r
4949 static void TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,\r
4950                               TIM_SlaveConfigTypeDef * sSlaveConfig)\r
4951 {\r
4952   uint32_t tmpsmcr = 0;\r
4953   uint32_t tmpccmr1 = 0;\r
4954   uint32_t tmpccer = 0;\r
4955 \r
4956  /* Get the TIMx SMCR register value */\r
4957   tmpsmcr = htim->Instance->SMCR;\r
4958 \r
4959   /* Reset the Trigger Selection Bits */\r
4960   tmpsmcr &= ~TIM_SMCR_TS;\r
4961   /* Set the Input Trigger source */\r
4962   tmpsmcr |= sSlaveConfig->InputTrigger;\r
4963 \r
4964   /* Reset the slave mode Bits */\r
4965   tmpsmcr &= ~TIM_SMCR_SMS;\r
4966   /* Set the slave mode */\r
4967   tmpsmcr |= sSlaveConfig->SlaveMode;\r
4968 \r
4969   /* Write to TIMx SMCR */\r
4970   htim->Instance->SMCR = tmpsmcr;\r
4971  \r
4972   /* Configure the trigger prescaler, filter, and polarity */\r
4973   switch (sSlaveConfig->InputTrigger)\r
4974   {\r
4975   case TIM_TS_ETRF:\r
4976     {\r
4977       /* Check the parameters */\r
4978       assert_param(IS_TIM_ETR_INSTANCE(htim->Instance));\r
4979       assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));\r
4980       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));\r
4981       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));\r
4982       /* Configure the ETR Trigger source */\r
4983       TIM_ETR_SetConfig(htim->Instance, \r
4984                         sSlaveConfig->TriggerPrescaler, \r
4985                         sSlaveConfig->TriggerPolarity, \r
4986                         sSlaveConfig->TriggerFilter);\r
4987     }\r
4988     break;\r
4989     \r
4990   case TIM_TS_TI1F_ED:\r
4991     {\r
4992       /* Check the parameters */\r
4993       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
4994       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));\r
4995       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));\r
4996   \r
4997       /* Disable the Channel 1: Reset the CC1E Bit */\r
4998       tmpccer = htim->Instance->CCER;\r
4999       htim->Instance->CCER &= ~TIM_CCER_CC1E;\r
5000       tmpccmr1 = htim->Instance->CCMR1;    \r
5001       \r
5002       /* Set the filter */\r
5003       tmpccmr1 &= ~TIM_CCMR1_IC1F;\r
5004       tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4);\r
5005       \r
5006       /* Write to TIMx CCMR1 and CCER registers */\r
5007       htim->Instance->CCMR1 = tmpccmr1;\r
5008       htim->Instance->CCER = tmpccer;                               \r
5009                                \r
5010     }\r
5011     break;\r
5012     \r
5013   case TIM_TS_TI1FP1:\r
5014     {\r
5015       /* Check the parameters */\r
5016       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));\r
5017       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));\r
5018       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));\r
5019 \r
5020       /* Configure TI1 Filter and Polarity */\r
5021       TIM_TI1_ConfigInputStage(htim->Instance,\r
5022                                sSlaveConfig->TriggerPolarity,\r
5023                                sSlaveConfig->TriggerFilter);\r
5024     }\r
5025     break;\r
5026     \r
5027   case TIM_TS_TI2FP2:\r
5028     {\r
5029       /* Check the parameters */\r
5030       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
5031       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));\r
5032       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));\r
5033   \r
5034       /* Configure TI2 Filter and Polarity */\r
5035       TIM_TI2_ConfigInputStage(htim->Instance,\r
5036                                 sSlaveConfig->TriggerPolarity,\r
5037                                 sSlaveConfig->TriggerFilter);\r
5038     }\r
5039     break;\r
5040     \r
5041   case TIM_TS_ITR0:\r
5042     {\r
5043       /* Check the parameter */\r
5044       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
5045     }\r
5046     break;\r
5047     \r
5048   case TIM_TS_ITR1:\r
5049     {\r
5050       /* Check the parameter */\r
5051       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
5052     }\r
5053     break;\r
5054     \r
5055   case TIM_TS_ITR2:\r
5056     {\r
5057       /* Check the parameter */\r
5058       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
5059     }\r
5060     break;\r
5061     \r
5062   case TIM_TS_ITR3:\r
5063     {\r
5064       /* Check the parameter */\r
5065       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));\r
5066     }\r
5067     break;\r
5068        \r
5069   default:\r
5070     break;\r
5071   }\r
5072 }\r
5073 \r
5074 /**\r
5075   * @brief  Configure the TI1 as Input.\r
5076   * @param  TIMx to select the TIM peripheral.\r
5077   * @param  TIM_ICPolarity : The Input Polarity.\r
5078   *          This parameter can be one of the following values:\r
5079   *            @arg TIM_ICPolarity_Rising\r
5080   *            @arg TIM_ICPolarity_Falling\r
5081   *            @arg TIM_ICPolarity_BothEdge  \r
5082   * @param  TIM_ICSelection: specifies the input to be used.\r
5083   *          This parameter can be one of the following values:\r
5084   *            @arg TIM_ICSelection_DirectTI: TIM Input 1 is selected to be connected to IC1.\r
5085   *            @arg TIM_ICSelection_IndirectTI: TIM Input 1 is selected to be connected to IC2.\r
5086   *            @arg TIM_ICSelection_TRC: TIM Input 1 is selected to be connected to TRC.\r
5087   * @param  TIM_ICFilter: Specifies the Input Capture Filter.\r
5088   *          This parameter must be a value between 0x00 and 0x0F.\r
5089   * @retval None  \r
5090   * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1 \r
5091   *       (on channel2 path) is used as the input signal. Therefore CCMR1 must be \r
5092   *        protected against un-initialized filter and polarity values.  \r
5093   */\r
5094 void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,\r
5095                        uint32_t TIM_ICFilter)\r
5096 {\r
5097   uint32_t tmpccmr1 = 0;\r
5098   uint32_t tmpccer = 0;\r
5099 \r
5100   /* Disable the Channel 1: Reset the CC1E Bit */\r
5101   TIMx->CCER &= ~TIM_CCER_CC1E;\r
5102   tmpccmr1 = TIMx->CCMR1;\r
5103   tmpccer = TIMx->CCER;\r
5104 \r
5105   /* Select the Input */\r
5106   if(IS_TIM_CC2_INSTANCE(TIMx) != RESET)\r
5107   {\r
5108     tmpccmr1 &= ~TIM_CCMR1_CC1S;\r
5109     tmpccmr1 |= TIM_ICSelection;\r
5110   } \r
5111   else\r
5112   {\r
5113     tmpccmr1 |= TIM_CCMR1_CC1S_0;\r
5114   }\r
5115   \r
5116   /* Set the filter */\r
5117   tmpccmr1 &= ~TIM_CCMR1_IC1F;\r
5118   tmpccmr1 |= ((TIM_ICFilter << 4) & TIM_CCMR1_IC1F);\r
5119 \r
5120   /* Select the Polarity and set the CC1E Bit */\r
5121   tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);\r
5122   tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));\r
5123 \r
5124   /* Write to TIMx CCMR1 and CCER registers */\r
5125   TIMx->CCMR1 = tmpccmr1;\r
5126   TIMx->CCER = tmpccer;\r
5127 }\r
5128 \r
5129 /**\r
5130   * @brief  Configure the Polarity and Filter for TI1.\r
5131   * @param  TIMx to select the TIM peripheral.\r
5132   * @param  TIM_ICPolarity : The Input Polarity.\r
5133   *          This parameter can be one of the following values:\r
5134   *            @arg TIM_ICPolarity_Rising\r
5135   *            @arg TIM_ICPolarity_Falling\r
5136   *            @arg TIM_ICPolarity_BothEdge\r
5137   * @param  TIM_ICFilter: Specifies the Input Capture Filter.\r
5138   *          This parameter must be a value between 0x00 and 0x0F.\r
5139   * @retval None\r
5140   */\r
5141 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)\r
5142 {\r
5143   uint32_t tmpccmr1 = 0;\r
5144   uint32_t tmpccer = 0;\r
5145   \r
5146   /* Disable the Channel 1: Reset the CC1E Bit */\r
5147   tmpccer = TIMx->CCER;\r
5148   TIMx->CCER &= ~TIM_CCER_CC1E;\r
5149   tmpccmr1 = TIMx->CCMR1;    \r
5150   \r
5151   /* Set the filter */\r
5152   tmpccmr1 &= ~TIM_CCMR1_IC1F;\r
5153   tmpccmr1 |= (TIM_ICFilter << 4);\r
5154   \r
5155   /* Select the Polarity and set the CC1E Bit */\r
5156   tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);\r
5157   tmpccer |= TIM_ICPolarity;\r
5158   \r
5159   /* Write to TIMx CCMR1 and CCER registers */\r
5160   TIMx->CCMR1 = tmpccmr1;\r
5161   TIMx->CCER = tmpccer;\r
5162 }\r
5163 \r
5164 /**\r
5165   * @brief  Configure the TI2 as Input.\r
5166   * @param  TIMx to select the TIM peripheral\r
5167   * @param  TIM_ICPolarity : The Input Polarity.\r
5168   *          This parameter can be one of the following values:\r
5169   *            @arg TIM_ICPolarity_Rising\r
5170   *            @arg TIM_ICPolarity_Falling\r
5171   *            @arg TIM_ICPolarity_BothEdge   \r
5172   * @param  TIM_ICSelection: specifies the input to be used.\r
5173   *          This parameter can be one of the following values:\r
5174   *            @arg TIM_ICSelection_DirectTI: TIM Input 2 is selected to be connected to IC2.\r
5175   *            @arg TIM_ICSelection_IndirectTI: TIM Input 2 is selected to be connected to IC1.\r
5176   *            @arg TIM_ICSelection_TRC: TIM Input 2 is selected to be connected to TRC.\r
5177   * @param  TIM_ICFilter: Specifies the Input Capture Filter.\r
5178   *          This parameter must be a value between 0x00 and 0x0F.\r
5179   * @retval None\r
5180   * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2 \r
5181   *       (on channel1 path) is used as the input signal. Therefore CCMR1 must be \r
5182   *        protected against un-initialized filter and polarity values.  \r
5183   */\r
5184 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,\r
5185                        uint32_t TIM_ICFilter)\r
5186 {\r
5187   uint32_t tmpccmr1 = 0;\r
5188   uint32_t tmpccer = 0;\r
5189 \r
5190   /* Disable the Channel 2: Reset the CC2E Bit */\r
5191   TIMx->CCER &= ~TIM_CCER_CC2E;\r
5192   tmpccmr1 = TIMx->CCMR1;\r
5193   tmpccer = TIMx->CCER;\r
5194 \r
5195   /* Select the Input */\r
5196   tmpccmr1 &= ~TIM_CCMR1_CC2S;\r
5197   tmpccmr1 |= (TIM_ICSelection << 8);\r
5198 \r
5199   /* Set the filter */\r
5200   tmpccmr1 &= ~TIM_CCMR1_IC2F;\r
5201   tmpccmr1 |= ((TIM_ICFilter << 12) & TIM_CCMR1_IC2F);\r
5202 \r
5203   /* Select the Polarity and set the CC2E Bit */\r
5204   tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);\r
5205   tmpccer |= ((TIM_ICPolarity << 4) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));\r
5206 \r
5207   /* Write to TIMx CCMR1 and CCER registers */\r
5208   TIMx->CCMR1 = tmpccmr1 ;\r
5209   TIMx->CCER = tmpccer;\r
5210 }\r
5211 \r
5212 /**\r
5213   * @brief  Configure the Polarity and Filter for TI2.\r
5214   * @param  TIMx to select the TIM peripheral.\r
5215   * @param  TIM_ICPolarity : The Input Polarity.\r
5216   *          This parameter can be one of the following values:\r
5217   *            @arg TIM_ICPolarity_Rising\r
5218   *            @arg TIM_ICPolarity_Falling\r
5219   *            @arg TIM_ICPolarity_BothEdge\r
5220   * @param  TIM_ICFilter: Specifies the Input Capture Filter.\r
5221   *          This parameter must be a value between 0x00 and 0x0F.\r
5222   * @retval None\r
5223   */\r
5224 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)\r
5225 {\r
5226 uint32_t tmpccmr1 = 0;\r
5227   uint32_t tmpccer = 0;\r
5228   \r
5229   /* Disable the Channel 2: Reset the CC2E Bit */\r
5230   TIMx->CCER &= ~TIM_CCER_CC2E;\r
5231   tmpccmr1 = TIMx->CCMR1;\r
5232   tmpccer = TIMx->CCER;\r
5233   \r
5234   /* Set the filter */\r
5235   tmpccmr1 &= ~TIM_CCMR1_IC2F;\r
5236   tmpccmr1 |= (TIM_ICFilter << 12);\r
5237 \r
5238   /* Select the Polarity and set the CC2E Bit */\r
5239   tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);\r
5240   tmpccer |= (TIM_ICPolarity << 4);\r
5241 \r
5242   /* Write to TIMx CCMR1 and CCER registers */\r
5243   TIMx->CCMR1 = tmpccmr1 ;\r
5244   TIMx->CCER = tmpccer;\r
5245 }\r
5246 \r
5247 /**\r
5248   * @brief  Configure the TI3 as Input.\r
5249   * @param  TIMx to select the TIM peripheral\r
5250   * @param  TIM_ICPolarity : The Input Polarity.\r
5251   *          This parameter can be one of the following values:\r
5252   *            @arg TIM_ICPolarity_Rising\r
5253   *            @arg TIM_ICPolarity_Falling\r
5254   *            @arg TIM_ICPolarity_BothEdge         \r
5255   * @param  TIM_ICSelection: specifies the input to be used.\r
5256   *          This parameter can be one of the following values:\r
5257   *            @arg TIM_ICSelection_DirectTI: TIM Input 3 is selected to be connected to IC3.\r
5258   *            @arg TIM_ICSelection_IndirectTI: TIM Input 3 is selected to be connected to IC4.\r
5259   *            @arg TIM_ICSelection_TRC: TIM Input 3 is selected to be connected to TRC.\r
5260   * @param  TIM_ICFilter: Specifies the Input Capture Filter.\r
5261   *          This parameter must be a value between 0x00 and 0x0F.\r
5262   * @retval None\r
5263   * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4 \r
5264   *       (on channel1 path) is used as the input signal. Therefore CCMR2 must be \r
5265   *        protected against un-initialized filter and polarity values.  \r
5266   */\r
5267 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,\r
5268                        uint32_t TIM_ICFilter)\r
5269 {\r
5270   uint32_t tmpccmr2 = 0;\r
5271   uint32_t tmpccer = 0;\r
5272 \r
5273   /* Disable the Channel 3: Reset the CC3E Bit */\r
5274   TIMx->CCER &= ~TIM_CCER_CC3E;\r
5275   tmpccmr2 = TIMx->CCMR2;\r
5276   tmpccer = TIMx->CCER;\r
5277 \r
5278   /* Select the Input */\r
5279   tmpccmr2 &= ~TIM_CCMR2_CC3S;\r
5280   tmpccmr2 |= TIM_ICSelection;\r
5281 \r
5282   /* Set the filter */\r
5283   tmpccmr2 &= ~TIM_CCMR2_IC3F;\r
5284   tmpccmr2 |= ((TIM_ICFilter << 4) & TIM_CCMR2_IC3F);\r
5285 \r
5286   /* Select the Polarity and set the CC3E Bit */\r
5287   tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);\r
5288   tmpccer |= ((TIM_ICPolarity << 8) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));\r
5289 \r
5290   /* Write to TIMx CCMR2 and CCER registers */\r
5291   TIMx->CCMR2 = tmpccmr2;\r
5292   TIMx->CCER = tmpccer;\r
5293 }\r
5294 \r
5295 /**\r
5296   * @brief  Configure the TI4 as Input.\r
5297   * @param  TIMx to select the TIM peripheral\r
5298   * @param  TIM_ICPolarity : The Input Polarity.\r
5299   *          This parameter can be one of the following values:\r
5300   *            @arg TIM_ICPolarity_Rising\r
5301   *            @arg TIM_ICPolarity_Falling\r
5302   *            @arg TIM_ICPolarity_BothEdge     \r
5303   * @param  TIM_ICSelection: specifies the input to be used.\r
5304   *          This parameter can be one of the following values:\r
5305   *            @arg TIM_ICSelection_DirectTI: TIM Input 4 is selected to be connected to IC4.\r
5306   *            @arg TIM_ICSelection_IndirectTI: TIM Input 4 is selected to be connected to IC3.\r
5307   *            @arg TIM_ICSelection_TRC: TIM Input 4 is selected to be connected to TRC.\r
5308   * @param  TIM_ICFilter: Specifies the Input Capture Filter.\r
5309   *          This parameter must be a value between 0x00 and 0x0F.\r
5310   * @retval None\r
5311   * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3 \r
5312   *       (on channel1 path) is used as the input signal. Therefore CCMR2 must be \r
5313   *        protected against un-initialized filter and polarity values.  \r
5314   */\r
5315 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,\r
5316                        uint32_t TIM_ICFilter)\r
5317 {\r
5318   uint32_t tmpccmr2 = 0;\r
5319   uint32_t tmpccer = 0;\r
5320 \r
5321   /* Disable the Channel 4: Reset the CC4E Bit */\r
5322   TIMx->CCER &= ~TIM_CCER_CC4E;\r
5323   tmpccmr2 = TIMx->CCMR2;\r
5324   tmpccer = TIMx->CCER;\r
5325 \r
5326   /* Select the Input */\r
5327   tmpccmr2 &= ~TIM_CCMR2_CC4S;\r
5328   tmpccmr2 |= (TIM_ICSelection << 8);\r
5329 \r
5330   /* Set the filter */\r
5331   tmpccmr2 &= ~TIM_CCMR2_IC4F;\r
5332   tmpccmr2 |= ((TIM_ICFilter << 12) & TIM_CCMR2_IC4F);\r
5333 \r
5334   /* Select the Polarity and set the CC4E Bit */\r
5335   tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);\r
5336   tmpccer |= ((TIM_ICPolarity << 12) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));\r
5337 \r
5338   /* Write to TIMx CCMR2 and CCER registers */\r
5339   TIMx->CCMR2 = tmpccmr2;\r
5340   TIMx->CCER = tmpccer ;\r
5341 }\r
5342 \r
5343 /**\r
5344   * @brief  Selects the Input Trigger source\r
5345   * @param  TIMx to select the TIM peripheral\r
5346   * @param  TIM_ITRx: The Input Trigger source.\r
5347   *          This parameter can be one of the following values:\r
5348   *            @arg TIM_TS_ITR0: Internal Trigger 0\r
5349   *            @arg TIM_TS_ITR1: Internal Trigger 1\r
5350   *            @arg TIM_TS_ITR2: Internal Trigger 2\r
5351   *            @arg TIM_TS_ITR3: Internal Trigger 3\r
5352   *            @arg TIM_TS_TI1F_ED: TI1 Edge Detector\r
5353   *            @arg TIM_TS_TI1FP1: Filtered Timer Input 1\r
5354   *            @arg TIM_TS_TI2FP2: Filtered Timer Input 2\r
5355   *            @arg TIM_TS_ETRF: External Trigger input\r
5356   * @retval None\r
5357   */\r
5358 static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint16_t TIM_ITRx)\r
5359 {\r
5360   uint32_t tmpsmcr = 0;\r
5361   \r
5362    /* Get the TIMx SMCR register value */\r
5363    tmpsmcr = TIMx->SMCR;\r
5364    /* Reset the TS Bits */\r
5365    tmpsmcr &= ~TIM_SMCR_TS;\r
5366    /* Set the Input Trigger source and the slave mode*/\r
5367    tmpsmcr |= TIM_ITRx | TIM_SLAVEMODE_EXTERNAL1;\r
5368    /* Write to TIMx SMCR */\r
5369    TIMx->SMCR = tmpsmcr;\r
5370 }\r
5371 \r
5372 /**\r
5373   * @brief  Configures the TIMx External Trigger (ETR).\r
5374   * @param  TIMx to select the TIM peripheral\r
5375   * @param  TIM_ExtTRGPrescaler: The external Trigger Prescaler.\r
5376   *          This parameter can be one of the following values:\r
5377   *            @arg TIM_ExtTRGPSC_DIV1: ETRP Prescaler OFF.\r
5378   *            @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.\r
5379   *            @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.\r
5380   *            @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.\r
5381   * @param  TIM_ExtTRGPolarity: The external Trigger Polarity.\r
5382   *          This parameter can be one of the following values:\r
5383   *            @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.\r
5384   *            @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.\r
5385   * @param  ExtTRGFilter: External Trigger Filter.\r
5386   *          This parameter must be a value between 0x00 and 0x0F\r
5387   * @retval None\r
5388   */\r
5389 void TIM_ETR_SetConfig(TIM_TypeDef* TIMx, uint32_t TIM_ExtTRGPrescaler,\r
5390                        uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)\r
5391 {\r
5392   uint32_t tmpsmcr = 0;\r
5393 \r
5394   tmpsmcr = TIMx->SMCR;\r
5395 \r
5396   /* Reset the ETR Bits */\r
5397   tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);\r
5398 \r
5399   /* Set the Prescaler, the Filter value and the Polarity */\r
5400   tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8)));\r
5401 \r
5402   /* Write to TIMx SMCR */\r
5403   TIMx->SMCR = tmpsmcr;\r
5404\r
5405 \r
5406 /**\r
5407   * @brief  Enables or disables the TIM Capture Compare Channel x.\r
5408   * @param  TIMx to select the TIM peripheral\r
5409   * @param  Channel: specifies the TIM Channel\r
5410   *          This parameter can be one of the following values:\r
5411   *            @arg TIM_Channel_1: TIM Channel 1\r
5412   *            @arg TIM_Channel_2: TIM Channel 2\r
5413   *            @arg TIM_Channel_3: TIM Channel 3\r
5414   *            @arg TIM_Channel_4: TIM Channel 4\r
5415   * @param  ChannelState: specifies the TIM Channel CCxE bit new state.\r
5416   *          This parameter can be: TIM_CCx_ENABLE or TIM_CCx_Disable. \r
5417   * @retval None\r
5418   */\r
5419 void TIM_CCxChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelState)\r
5420 {\r
5421   uint32_t tmp = 0;\r
5422 \r
5423   /* Check the parameters */\r
5424   assert_param(IS_TIM_CC1_INSTANCE(TIMx)); \r
5425   assert_param(IS_TIM_CHANNELS(Channel));\r
5426 \r
5427   tmp = TIM_CCER_CC1E << Channel;\r
5428 \r
5429   /* Reset the CCxE Bit */\r
5430   TIMx->CCER &= ~tmp;\r
5431 \r
5432   /* Set or reset the CCxE Bit */ \r
5433   TIMx->CCER |= (uint32_t)(ChannelState << Channel);\r
5434 }\r
5435 \r
5436 \r
5437 /**\r
5438   * @}\r
5439   */\r
5440 \r
5441 #endif /* HAL_TIM_MODULE_ENABLED */\r
5442 /**\r
5443   * @}\r
5444   */ \r
5445 \r
5446 /**\r
5447   * @}\r
5448   */ \r
5449 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r