]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_tim_ex.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_tim_ex.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_tim_ex.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 extension peripheral:\r
10   *           + Time Hall Sensor Interface Initialization\r
11   *           + Time Hall Sensor Interface Start\r
12   *           + Time Complementary signal bread and dead time configuration  \r
13   *           + Time Master and Slave synchronization configuration\r
14   *           + Time Output Compare/PWM Channel Configuration (for channels 5 and 6)\r
15   *           + Time OCRef clear configuration\r
16   *           + Timer remapping capabilities configuration  \r
17   @verbatim \r
18   ==============================================================================\r
19                       ##### TIMER Extended features #####\r
20   ==============================================================================\r
21   [..] \r
22     The Timer Extension features include: \r
23     (#) Complementary outputs with programmable dead-time for :\r
24         (++) Input Capture\r
25         (++) Output Compare\r
26         (++) PWM generation (Edge and Center-aligned Mode)\r
27         (++) One-pulse mode output\r
28     (#) Synchronization circuit to control the timer with external signals and to \r
29         interconnect several timers together.\r
30     (#) Break input to put the timer output signals in reset state or in a known state.\r
31     (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for \r
32         positioning purposes                \r
33    \r
34                         ##### How to use this driver #####\r
35   ==============================================================================\r
36   [..]\r
37      (#) Initialize the TIM low level resources by implementing the following functions \r
38          depending from feature used :\r
39            (++) Complementary Output Compare : HAL_TIM_OC_MspInit()\r
40            (++) Complementary PWM generation : HAL_TIM_PWM_MspInit()\r
41            (++) Complementary One-pulse mode output : HAL_TIM_OnePulse_MspInit()\r
42            (++) Hall Sensor output : HAL_TIM_HallSensor_MspInit()\r
43            \r
44      (#) Initialize the TIM low level resources :\r
45         (##) Enable the TIM interface clock using __TIMx_CLK_ENABLE(); \r
46         (##) TIM pins configuration\r
47             (+++) Enable the clock for the TIM GPIOs using the following function:\r
48                  __GPIOx_CLK_ENABLE();   \r
49             (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();  \r
50 \r
51      (#) The external Clock can be configured, if needed (the default clock is the \r
52          internal clock from the APBx), using the following function:\r
53          HAL_TIM_ConfigClockSource, the clock configuration should be done before \r
54          any start function.\r
55   \r
56     (#) Configure the TIM in the desired functioning mode using one of the \r
57         initialization function of this driver:\r
58         (++) HAL_TIMEx_HallSensor_Init and HAL_TIMEx_ConfigCommutationEvent: to use the \r
59              Timer Hall Sensor Interface and the commutation event with the corresponding \r
60              Interrupt and DMA request if needed (Note that One Timer is used to interface \r
61              with the Hall sensor Interface and another Timer should be used to use \r
62              the commutation event).\r
63 \r
64     (#) Activate the TIM peripheral using one of the start functions: \r
65            (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(), HAL_TIMEx_OC_Start_IT()\r
66            (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(), HAL_TIMEx_PWMN_Start_IT()\r
67            (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()\r
68            (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(), HAL_TIMEx_HallSensor_Start_IT().\r
69 \r
70   \r
71   @endverbatim\r
72   ******************************************************************************\r
73   * @attention\r
74   *\r
75   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
76   *\r
77   * Redistribution and use in source and binary forms, with or without modification,\r
78   * are permitted provided that the following conditions are met:\r
79   *   1. Redistributions of source code must retain the above copyright notice,\r
80   *      this list of conditions and the following disclaimer.\r
81   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
82   *      this list of conditions and the following disclaimer in the documentation\r
83   *      and/or other materials provided with the distribution.\r
84   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
85   *      may be used to endorse or promote products derived from this software\r
86   *      without specific prior written permission.\r
87   *\r
88   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
89   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
90   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
91   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
92   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
93   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
94   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
95   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
96   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
97   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
98   *\r
99   ******************************************************************************\r
100   */ \r
101 \r
102 /* Includes ------------------------------------------------------------------*/\r
103 #include "stm32f7xx_hal.h"\r
104 \r
105 /** @addtogroup STM32F7xx_HAL_Driver\r
106   * @{\r
107   */\r
108 \r
109 /** @defgroup TIMEx TIM Extended HAL module driver\r
110   * @brief TIM Extended HAL module driver\r
111   * @{\r
112   */\r
113 \r
114 #ifdef HAL_TIM_MODULE_ENABLED\r
115 \r
116 /* Private typedef -----------------------------------------------------------*/\r
117 /* Private define ------------------------------------------------------------*/\r
118 #define BDTR_BKF_SHIFT  (16)\r
119 #define BDTR_BK2F_SHIFT (20)\r
120 /* Private macro -------------------------------------------------------------*/\r
121 /* Private variables ---------------------------------------------------------*/\r
122 /* Private function prototypes -----------------------------------------------*/\r
123 /** @addtogroup TIMEx_Private_Functions\r
124   * @{\r
125   */\r
126 static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState);  \r
127 static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);\r
128 static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);\r
129 /**\r
130   * @}\r
131   */\r
132 /* Private functions ---------------------------------------------------------*/\r
133 \r
134 /** @defgroup TIMEx_Private_Functions\r
135   * @{\r
136   */\r
137 \r
138 /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions\r
139  *  @brief    Timer Hall Sensor functions \r
140  *\r
141 @verbatim    \r
142   ==============================================================================\r
143                       ##### Timer Hall Sensor functions #####\r
144   ==============================================================================\r
145   [..]  \r
146     This section provides functions allowing to:\r
147     (+) Initialize and configure TIM HAL Sensor. \r
148     (+) De-initialize TIM HAL Sensor.\r
149     (+) Start the Hall Sensor Interface.\r
150     (+) Stop the Hall Sensor Interface.\r
151     (+) Start the Hall Sensor Interface and enable interrupts.\r
152     (+) Stop the Hall Sensor Interface and disable interrupts.\r
153     (+) Start the Hall Sensor Interface and enable DMA transfers.\r
154     (+) Stop the Hall Sensor Interface and disable DMA transfers.\r
155  \r
156 @endverbatim\r
157   * @{\r
158   */\r
159 /**\r
160   * @brief  Initializes the TIM Hall Sensor Interface and create the associated handle.\r
161   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
162   *                the configuration information for TIM module.\r
163   * @param  sConfig: TIM Hall Sensor configuration structure\r
164   * @retval HAL status\r
165   */\r
166 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef* sConfig)\r
167 {\r
168   TIM_OC_InitTypeDef OC_Config;\r
169     \r
170   /* Check the TIM handle allocation */\r
171   if(htim == NULL)\r
172   {\r
173     return HAL_ERROR;\r
174   }\r
175   \r
176   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));\r
177   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));\r
178   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));\r
179   assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));\r
180   assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));\r
181   assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));\r
182 \r
183   /* Set the TIM state */\r
184   htim->State= HAL_TIM_STATE_BUSY;\r
185   \r
186   /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */\r
187   HAL_TIMEx_HallSensor_MspInit(htim);\r
188   \r
189   /* Configure the Time base in the Encoder Mode */\r
190   TIM_Base_SetConfig(htim->Instance, &htim->Init);\r
191   \r
192   /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the  Hall sensor */\r
193   TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);\r
194   \r
195   /* Reset the IC1PSC Bits */\r
196   htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;\r
197   /* Set the IC1PSC value */\r
198   htim->Instance->CCMR1 |= sConfig->IC1Prescaler;\r
199   \r
200   /* Enable the Hall sensor interface (XOR function of the three inputs) */\r
201   htim->Instance->CR2 |= TIM_CR2_TI1S;\r
202   \r
203   /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */\r
204   htim->Instance->SMCR &= ~TIM_SMCR_TS;\r
205   htim->Instance->SMCR |= TIM_TS_TI1F_ED;\r
206   \r
207   /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */  \r
208   htim->Instance->SMCR &= ~TIM_SMCR_SMS;\r
209   htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;\r
210   \r
211   /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/\r
212   OC_Config.OCFastMode = TIM_OCFAST_DISABLE;\r
213   OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;\r
214   OC_Config.OCMode = TIM_OCMODE_PWM2;\r
215   OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;\r
216   OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;\r
217   OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;\r
218   OC_Config.Pulse = sConfig->Commutation_Delay; \r
219     \r
220   TIM_OC2_SetConfig(htim->Instance, &OC_Config);\r
221   \r
222   /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2\r
223     register to 101 */\r
224   htim->Instance->CR2 &= ~TIM_CR2_MMS;\r
225   htim->Instance->CR2 |= TIM_TRGO_OC2REF; \r
226   \r
227   /* Initialize the TIM state*/\r
228   htim->State= HAL_TIM_STATE_READY;\r
229 \r
230   return HAL_OK;\r
231 }\r
232 \r
233 /**\r
234   * @brief  DeInitializes the TIM Hall Sensor interface  \r
235   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
236   *                the configuration information for TIM module.\r
237   * @retval HAL status\r
238   */\r
239 HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)\r
240 {\r
241   /* Check the parameters */\r
242   assert_param(IS_TIM_INSTANCE(htim->Instance));\r
243 \r
244   htim->State = HAL_TIM_STATE_BUSY;\r
245   \r
246   /* Disable the TIM Peripheral Clock */\r
247   __HAL_TIM_DISABLE(htim);\r
248     \r
249   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */\r
250   HAL_TIMEx_HallSensor_MspDeInit(htim);\r
251     \r
252   /* Change TIM state */  \r
253   htim->State = HAL_TIM_STATE_RESET; \r
254 \r
255   /* Release Lock */\r
256   __HAL_UNLOCK(htim);\r
257 \r
258   return HAL_OK;\r
259 }\r
260 \r
261 /**\r
262   * @brief  Initializes the TIM Hall Sensor MSP.\r
263   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
264   *                the configuration information for TIM module.\r
265   * @retval None\r
266   */\r
267 __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)\r
268 {\r
269   /* NOTE : This function Should not be modified, when the callback is needed,\r
270             the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file\r
271    */\r
272 }\r
273 \r
274 /**\r
275   * @brief  DeInitializes TIM Hall Sensor MSP.\r
276   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
277   *                the configuration information for TIM module.\r
278   * @retval None\r
279   */\r
280 __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)\r
281 {\r
282   /* NOTE : This function Should not be modified, when the callback is needed,\r
283             the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file\r
284    */\r
285 }\r
286 \r
287 /**\r
288   * @brief  Starts the TIM Hall Sensor Interface.\r
289   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
290   *                the configuration information for TIM module.\r
291   * @retval HAL status\r
292   */\r
293 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)\r
294 {\r
295   /* Check the parameters */\r
296   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));\r
297   \r
298   /* Enable the Input Capture channels 1\r
299     (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */  \r
300   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE); \r
301   \r
302   /* Enable the Peripheral */\r
303   __HAL_TIM_ENABLE(htim);\r
304   \r
305   /* Return function status */\r
306   return HAL_OK;\r
307 }\r
308 \r
309 /**\r
310   * @brief  Stops the TIM Hall sensor Interface.\r
311   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
312   *                the configuration information for TIM module.\r
313   * @retval HAL status\r
314   */\r
315 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)\r
316 {\r
317   /* Check the parameters */\r
318   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));\r
319   \r
320   /* Disable the Input Capture channels 1, 2 and 3\r
321     (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */  \r
322   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
323 \r
324   /* Disable the Peripheral */\r
325   __HAL_TIM_DISABLE(htim);\r
326   \r
327   /* Return function status */\r
328   return HAL_OK;\r
329 }\r
330 \r
331 /**\r
332   * @brief  Starts the TIM Hall Sensor Interface in interrupt mode.\r
333   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
334   *                the configuration information for TIM module.\r
335   * @retval HAL status\r
336   */\r
337 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)\r
338\r
339   /* Check the parameters */\r
340   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));\r
341   \r
342   /* Enable the capture compare Interrupts 1 event */\r
343   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
344   \r
345   /* Enable the Input Capture channels 1\r
346     (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */  \r
347   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);  \r
348   \r
349   /* Enable the Peripheral */\r
350   __HAL_TIM_ENABLE(htim);\r
351   \r
352   /* Return function status */\r
353   return HAL_OK;\r
354 }\r
355 \r
356 /**\r
357   * @brief  Stops the TIM Hall Sensor Interface in interrupt mode.\r
358   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
359   *                the configuration information for TIM module.\r
360   * @retval HAL status\r
361   */\r
362 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)\r
363 {\r
364   /* Check the parameters */\r
365   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));\r
366   \r
367   /* Disable the Input Capture channels 1\r
368     (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */  \r
369   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
370   \r
371   /* Disable the capture compare Interrupts event */\r
372   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
373   \r
374   /* Disable the Peripheral */\r
375   __HAL_TIM_DISABLE(htim);\r
376   \r
377   /* Return function status */\r
378   return HAL_OK;\r
379 }\r
380 \r
381 /**\r
382   * @brief  Starts the TIM Hall Sensor Interface in DMA mode.\r
383   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
384   *                the configuration information for TIM module.\r
385   * @param  pData: The destination Buffer address.\r
386   * @param  Length: The length of data to be transferred from TIM peripheral to memory.\r
387   * @retval HAL status\r
388   */\r
389 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)\r
390 {\r
391   /* Check the parameters */\r
392   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));\r
393   \r
394    if((htim->State == HAL_TIM_STATE_BUSY))\r
395   {\r
396      return HAL_BUSY;\r
397   }\r
398   else if((htim->State == HAL_TIM_STATE_READY))\r
399   {\r
400     if(((uint32_t)pData == 0 ) && (Length > 0)) \r
401     {\r
402       return HAL_ERROR;                                    \r
403     }\r
404     else\r
405     {\r
406       htim->State = HAL_TIM_STATE_BUSY;\r
407     }\r
408   }\r
409   /* Enable the Input Capture channels 1\r
410     (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */  \r
411   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE); \r
412   \r
413   /* Set the DMA Input Capture 1 Callback */\r
414   htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;     \r
415   /* Set the DMA error callback */\r
416   htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
417   \r
418   /* Enable the DMA Stream for Capture 1*/\r
419   HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);    \r
420   \r
421   /* Enable the capture compare 1 Interrupt */\r
422   __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);\r
423  \r
424   /* Enable the Peripheral */\r
425   __HAL_TIM_ENABLE(htim);\r
426   \r
427   /* Return function status */\r
428   return HAL_OK;\r
429 }\r
430 \r
431 /**\r
432   * @brief  Stops the TIM Hall Sensor Interface in DMA mode.\r
433   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
434   *                the configuration information for TIM module.\r
435   * @retval HAL status\r
436   */\r
437 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)\r
438 {\r
439   /* Check the parameters */\r
440   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));\r
441   \r
442   /* Disable the Input Capture channels 1\r
443     (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */  \r
444   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE); \r
445  \r
446   \r
447   /* Disable the capture compare Interrupts 1 event */\r
448   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);\r
449  \r
450   /* Disable the Peripheral */\r
451   __HAL_TIM_DISABLE(htim);\r
452   \r
453   /* Return function status */\r
454   return HAL_OK;\r
455 }\r
456 \r
457 /**\r
458   * @}\r
459   */\r
460   \r
461 /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions\r
462  *  @brief    Timer Complementary Output Compare functions \r
463  *\r
464 @verbatim   \r
465   ==============================================================================\r
466               ##### Timer Complementary Output Compare functions #####\r
467   ==============================================================================  \r
468   [..]  \r
469     This section provides functions allowing to:\r
470     (+) Start the Complementary Output Compare/PWM.\r
471     (+) Stop the Complementary Output Compare/PWM.\r
472     (+) Start the Complementary Output Compare/PWM and enable interrupts.\r
473     (+) Stop the Complementary Output Compare/PWM and disable interrupts.\r
474     (+) Start the Complementary Output Compare/PWM and enable DMA transfers.\r
475     (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.\r
476                \r
477 @endverbatim\r
478   * @{\r
479   */\r
480   \r
481 /**\r
482   * @brief  Starts the TIM Output Compare signal generation on the complementary\r
483   *         output.\r
484   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
485   *                the configuration information for TIM module.  \r
486   * @param  Channel: TIM Channel to be enabled.\r
487   *          This parameter can be one of the following values:\r
488   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
489   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
490   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
491   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
492   * @retval HAL status\r
493   */\r
494 HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)\r
495 {\r
496   /* Check the parameters */\r
497   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
498   \r
499      /* Enable the Capture compare channel N */\r
500      TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);\r
501     \r
502   /* Enable the Main Output */\r
503     __HAL_TIM_MOE_ENABLE(htim);\r
504 \r
505   /* Enable the Peripheral */\r
506   __HAL_TIM_ENABLE(htim);\r
507   \r
508   /* Return function status */\r
509   return HAL_OK;\r
510\r
511 \r
512 /**\r
513   * @brief  Stops the TIM Output Compare signal generation on the complementary\r
514   *         output.\r
515   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
516   *                the configuration information for TIM module.\r
517   * @param  Channel: TIM Channel to be disabled.\r
518   *          This parameter can be one of the following values:\r
519   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
520   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
521   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
522   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
523   * @retval HAL status\r
524   */\r
525 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)\r
526\r
527   /* Check the parameters */\r
528   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
529   \r
530     /* Disable the Capture compare channel N */\r
531   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);\r
532     \r
533   /* Disable the Main Output */\r
534     __HAL_TIM_MOE_DISABLE(htim);\r
535 \r
536   /* Disable the Peripheral */\r
537   __HAL_TIM_DISABLE(htim);\r
538   \r
539   /* Return function status */\r
540   return HAL_OK;\r
541\r
542 \r
543 /**\r
544   * @brief  Starts the TIM Output Compare signal generation in interrupt mode \r
545   *         on the complementary output.\r
546   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
547   *                the configuration information for TIM module.\r
548   * @param  Channel: TIM Channel to be enabled.\r
549   *          This parameter can be one of the following values:\r
550   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
551   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
552   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
553   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
554   * @retval HAL status\r
555   */\r
556 HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
557 {\r
558   /* Check the parameters */\r
559   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
560   \r
561   switch (Channel)\r
562   {\r
563     case TIM_CHANNEL_1:\r
564     {       \r
565       /* Enable the TIM Output Compare interrupt */\r
566       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
567     }\r
568     break;\r
569     \r
570     case TIM_CHANNEL_2:\r
571     {\r
572       /* Enable the TIM Output Compare interrupt */\r
573       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);\r
574     }\r
575     break;\r
576     \r
577     case TIM_CHANNEL_3:\r
578     {\r
579       /* Enable the TIM Output Compare interrupt */\r
580       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);\r
581     }\r
582     break;\r
583     \r
584     case TIM_CHANNEL_4:\r
585     {\r
586       /* Enable the TIM Output Compare interrupt */\r
587       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);\r
588     }\r
589     break;\r
590     \r
591     default:\r
592     break;\r
593   } \r
594   \r
595   /* Enable the TIM Break interrupt */\r
596   __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);\r
597   \r
598   /* Enable the Capture compare channel N */\r
599   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);\r
600 \r
601   /* Enable the Main Output */\r
602  __HAL_TIM_MOE_ENABLE(htim);\r
603 \r
604   /* Enable the Peripheral */\r
605   __HAL_TIM_ENABLE(htim);\r
606   \r
607   /* Return function status */\r
608   return HAL_OK;\r
609\r
610 \r
611 /**\r
612   * @brief  Stops the TIM Output Compare signal generation in interrupt mode \r
613   *         on the complementary output.\r
614   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
615   *                the configuration information for TIM module.\r
616   * @param  Channel: TIM Channel to be disabled.\r
617   *          This parameter can be one of the following values:\r
618   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
619   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
620   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
621   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
622   * @retval HAL status\r
623   */\r
624 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
625 {\r
626   uint32_t tmpccer = 0; \r
627 \r
628   /* Check the parameters */\r
629   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
630   \r
631   switch (Channel)\r
632   {\r
633     case TIM_CHANNEL_1:\r
634     {       \r
635       /* Disable the TIM Output Compare interrupt */\r
636       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
637     }\r
638     break;\r
639     \r
640     case TIM_CHANNEL_2:\r
641     {\r
642       /* Disable the TIM Output Compare interrupt */\r
643       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
644     }\r
645     break;\r
646     \r
647     case TIM_CHANNEL_3:\r
648     {\r
649       /* Disable the TIM Output Compare interrupt */\r
650       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);\r
651     }\r
652     break;\r
653     \r
654     case TIM_CHANNEL_4:\r
655     {\r
656       /* Disable the TIM Output Compare interrupt */\r
657       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);\r
658     }\r
659     break;\r
660     \r
661     default:\r
662     break; \r
663   }\r
664 \r
665   /* Disable the Capture compare channel N */\r
666   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);\r
667 \r
668   /* Disable the TIM Break interrupt (only if no more channel is active) */\r
669   tmpccer = htim->Instance->CCER;\r
670   if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)\r
671   {\r
672     __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);\r
673   }\r
674 \r
675   /* Disable the Main Output */\r
676   __HAL_TIM_MOE_DISABLE(htim);\r
677 \r
678   /* Disable the Peripheral */\r
679   __HAL_TIM_DISABLE(htim);\r
680   \r
681   /* Return function status */\r
682   return HAL_OK;\r
683\r
684 \r
685 /**\r
686   * @brief  Starts the TIM Output Compare signal generation in DMA mode \r
687   *         on the complementary output.\r
688   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
689   *                the configuration information for TIM module.\r
690   * @param  Channel: TIM Channel to be enabled.\r
691   *          This parameter can be one of the following values:\r
692   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
693   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
694   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
695   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
696   * @param  pData: The source Buffer address.\r
697   * @param  Length: The length of data to be transferred from memory to TIM peripheral\r
698   * @retval HAL status\r
699   */\r
700 HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)\r
701 {\r
702   /* Check the parameters */\r
703   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
704   \r
705   if((htim->State == HAL_TIM_STATE_BUSY))\r
706   {\r
707      return HAL_BUSY;\r
708   }\r
709   else if((htim->State == HAL_TIM_STATE_READY))\r
710   {\r
711     if(((uint32_t)pData == 0 ) && (Length > 0)) \r
712     {\r
713       return HAL_ERROR;                                    \r
714     }\r
715     else\r
716     {\r
717       htim->State = HAL_TIM_STATE_BUSY;\r
718     }\r
719   }    \r
720   switch (Channel)\r
721   {\r
722     case TIM_CHANNEL_1:\r
723     {      \r
724       /* Set the DMA Period elapsed callback */\r
725       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
726      \r
727       /* Set the DMA error callback */\r
728       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
729       \r
730       /* Enable the DMA Stream */\r
731       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);\r
732       \r
733       /* Enable the TIM Output Compare DMA request */\r
734       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);\r
735     }\r
736     break;\r
737     \r
738     case TIM_CHANNEL_2:\r
739     {\r
740       /* Set the DMA Period elapsed callback */\r
741       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
742      \r
743       /* Set the DMA error callback */\r
744       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;\r
745       \r
746       /* Enable the DMA Stream */\r
747       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);\r
748       \r
749       /* Enable the TIM Output Compare DMA request */\r
750       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);\r
751     }\r
752     break;\r
753     \r
754     case TIM_CHANNEL_3:\r
755 {\r
756       /* Set the DMA Period elapsed callback */\r
757       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
758      \r
759       /* Set the DMA error callback */\r
760       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;\r
761       \r
762       /* Enable the DMA Stream */\r
763       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);\r
764       \r
765       /* Enable the TIM Output Compare DMA request */\r
766       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);\r
767     }\r
768     break;\r
769     \r
770     case TIM_CHANNEL_4:\r
771     {\r
772      /* Set the DMA Period elapsed callback */\r
773       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
774      \r
775       /* Set the DMA error callback */\r
776       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;\r
777       \r
778       /* Enable the DMA Stream */\r
779       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);\r
780       \r
781       /* Enable the TIM Output Compare DMA request */\r
782       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);\r
783     }\r
784     break;\r
785     \r
786     default:\r
787     break;\r
788   }\r
789 \r
790   /* Enable the Capture compare channel N */\r
791   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);\r
792   \r
793   /* Enable the Main Output */\r
794   __HAL_TIM_MOE_ENABLE(htim);\r
795   \r
796   /* Enable the Peripheral */\r
797   __HAL_TIM_ENABLE(htim); \r
798   \r
799   /* Return function status */\r
800   return HAL_OK;\r
801 }\r
802 \r
803 /**\r
804   * @brief  Stops the TIM Output Compare signal generation in DMA mode \r
805   *         on the complementary output.\r
806   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
807   *                the configuration information for TIM module.\r
808   * @param  Channel: TIM Channel to be disabled.\r
809   *          This parameter can be one of the following values:\r
810   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
811   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
812   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
813   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
814   * @retval HAL status\r
815   */\r
816 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)\r
817 {\r
818   /* Check the parameters */\r
819   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
820   \r
821   switch (Channel)\r
822   {\r
823     case TIM_CHANNEL_1:\r
824     {       \r
825       /* Disable the TIM Output Compare DMA request */\r
826       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);\r
827     }\r
828     break;\r
829     \r
830     case TIM_CHANNEL_2:\r
831     {\r
832       /* Disable the TIM Output Compare DMA request */\r
833       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);\r
834     }\r
835     break;\r
836     \r
837     case TIM_CHANNEL_3:\r
838     {\r
839       /* Disable the TIM Output Compare DMA request */\r
840       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);\r
841     }\r
842     break;\r
843     \r
844     case TIM_CHANNEL_4:\r
845     {\r
846       /* Disable the TIM Output Compare interrupt */\r
847       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);\r
848     }\r
849     break;\r
850     \r
851     default:\r
852     break;\r
853   } \r
854   \r
855   /* Disable the Capture compare channel N */\r
856   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);\r
857   \r
858   /* Disable the Main Output */\r
859   __HAL_TIM_MOE_DISABLE(htim);\r
860   \r
861   /* Disable the Peripheral */\r
862   __HAL_TIM_DISABLE(htim);\r
863   \r
864   /* Change the htim state */\r
865   htim->State = HAL_TIM_STATE_READY;\r
866   \r
867   /* Return function status */\r
868   return HAL_OK;\r
869 }\r
870 \r
871 /**\r
872   * @}\r
873   */\r
874   \r
875 /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions\r
876  *  @brief    Timer Complementary PWM functions \r
877  *\r
878 @verbatim   \r
879   ==============================================================================\r
880                  ##### Timer Complementary PWM functions #####\r
881   ==============================================================================  \r
882   [..]  \r
883     This section provides functions allowing to:\r
884     (+) Start the Complementary PWM.\r
885     (+) Stop the Complementary PWM.\r
886     (+) Start the Complementary PWM and enable interrupts.\r
887     (+) Stop the Complementary PWM and disable interrupts.\r
888     (+) Start the Complementary PWM and enable DMA transfers.\r
889     (+) Stop the Complementary PWM and disable DMA transfers.\r
890     (+) Start the Complementary Input Capture measurement.\r
891     (+) Stop the Complementary Input Capture.\r
892     (+) Start the Complementary Input Capture and enable interrupts.\r
893     (+) Stop the Complementary Input Capture and disable interrupts.\r
894     (+) Start the Complementary Input Capture and enable DMA transfers.\r
895     (+) Stop the Complementary Input Capture and disable DMA transfers.\r
896     (+) Start the Complementary One Pulse generation.\r
897     (+) Stop the Complementary One Pulse.\r
898     (+) Start the Complementary One Pulse and enable interrupts.\r
899     (+) Stop the Complementary One Pulse and disable interrupts.\r
900                \r
901 @endverbatim\r
902   * @{\r
903   */\r
904 \r
905 /**\r
906   * @brief  Starts the PWM signal generation on the complementary output.\r
907   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
908   *                the configuration information for TIM module.\r
909   * @param  Channel: TIM Channel to be enabled.\r
910   *          This parameter can be one of the following values:\r
911   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
912   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
913   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
914   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
915   * @retval HAL status\r
916   */\r
917 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)\r
918 {\r
919   /* Check the parameters */\r
920   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
921   \r
922   /* Enable the complementary PWM output  */\r
923   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);\r
924   \r
925   /* Enable the Main Output */\r
926   __HAL_TIM_MOE_ENABLE(htim);\r
927   \r
928   /* Enable the Peripheral */\r
929   __HAL_TIM_ENABLE(htim);\r
930   \r
931   /* Return function status */\r
932   return HAL_OK;\r
933\r
934 \r
935 /**\r
936   * @brief  Stops the PWM signal generation on the complementary output.\r
937   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
938   *                the configuration information for TIM module.\r
939   * @param  Channel: TIM Channel to be disabled.\r
940   *          This parameter can be one of the following values:\r
941   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
942   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
943   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
944   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
945   * @retval HAL status\r
946   */\r
947 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)\r
948\r
949   /* Check the parameters */\r
950   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
951   \r
952   /* Disable the complementary PWM output  */\r
953   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);  \r
954   \r
955   /* Disable the Main Output */\r
956   __HAL_TIM_MOE_DISABLE(htim);\r
957   \r
958   /* Disable the Peripheral */\r
959   __HAL_TIM_DISABLE(htim);\r
960   \r
961   /* Return function status */\r
962   return HAL_OK;\r
963\r
964 \r
965 /**\r
966   * @brief  Starts the PWM signal generation in interrupt mode on the \r
967   *         complementary output.\r
968   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
969   *                the configuration information for TIM module.\r
970   * @param  Channel: TIM Channel to be disabled.\r
971   *          This parameter can be one of the following values:\r
972   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
973   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
974   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
975   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
976   * @retval HAL status\r
977   */\r
978 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)\r
979 {\r
980   /* Check the parameters */\r
981   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
982   \r
983   switch (Channel)\r
984   {\r
985     case TIM_CHANNEL_1:\r
986     {       \r
987       /* Enable the TIM Capture/Compare 1 interrupt */\r
988       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
989     }\r
990     break;\r
991     \r
992     case TIM_CHANNEL_2:\r
993     {\r
994       /* Enable the TIM Capture/Compare 2 interrupt */\r
995       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);\r
996     }\r
997     break;\r
998     \r
999     case TIM_CHANNEL_3:\r
1000     {\r
1001       /* Enable the TIM Capture/Compare 3 interrupt */\r
1002       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);\r
1003     }\r
1004     break;\r
1005     \r
1006     case TIM_CHANNEL_4:\r
1007     {\r
1008       /* Enable the TIM Capture/Compare 4 interrupt */\r
1009       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);\r
1010     }\r
1011     break;\r
1012     \r
1013     default:\r
1014     break;\r
1015   } \r
1016   \r
1017   /* Enable the TIM Break interrupt */\r
1018   __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);\r
1019   \r
1020   /* Enable the complementary PWM output  */\r
1021   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);\r
1022   \r
1023   /* Enable the Main Output */\r
1024   __HAL_TIM_MOE_ENABLE(htim);\r
1025   \r
1026   /* Enable the Peripheral */\r
1027   __HAL_TIM_ENABLE(htim);\r
1028   \r
1029   /* Return function status */\r
1030   return HAL_OK;\r
1031\r
1032 \r
1033 /**\r
1034   * @brief  Stops the PWM signal generation in interrupt mode on the \r
1035   *         complementary output.\r
1036   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1037   *                the configuration information for TIM module.\r
1038   * @param  Channel: TIM Channel to be disabled.\r
1039   *          This parameter can be one of the following values:\r
1040   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1041   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1042   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1043   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1044   * @retval HAL status\r
1045   */\r
1046 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)\r
1047 {\r
1048   uint32_t tmpccer = 0;\r
1049   \r
1050   /* Check the parameters */\r
1051   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
1052 \r
1053   switch (Channel)\r
1054   {\r
1055     case TIM_CHANNEL_1:\r
1056     {       \r
1057       /* Disable the TIM Capture/Compare 1 interrupt */\r
1058       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
1059     }\r
1060     break;\r
1061     \r
1062     case TIM_CHANNEL_2:\r
1063     {\r
1064       /* Disable the TIM Capture/Compare 2 interrupt */\r
1065       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
1066     }\r
1067     break;\r
1068     \r
1069     case TIM_CHANNEL_3:\r
1070     {\r
1071       /* Disable the TIM Capture/Compare 3 interrupt */\r
1072       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);\r
1073     }\r
1074     break;\r
1075     \r
1076     case TIM_CHANNEL_4:\r
1077     {\r
1078       /* Disable the TIM Capture/Compare 3 interrupt */\r
1079       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);\r
1080     }\r
1081     break;\r
1082     \r
1083     default:\r
1084     break; \r
1085   }\r
1086   \r
1087   /* Disable the complementary PWM output  */\r
1088   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);\r
1089   \r
1090   /* Disable the TIM Break interrupt (only if no more channel is active) */\r
1091   tmpccer = htim->Instance->CCER;\r
1092   if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)\r
1093   {\r
1094     __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);\r
1095   }\r
1096   \r
1097   /* Disable the Main Output */\r
1098   __HAL_TIM_MOE_DISABLE(htim);\r
1099   \r
1100   /* Disable the Peripheral */\r
1101   __HAL_TIM_DISABLE(htim);\r
1102   \r
1103   /* Return function status */\r
1104   return HAL_OK;\r
1105\r
1106 \r
1107 /**\r
1108   * @brief  Starts the TIM PWM signal generation in DMA mode on the \r
1109   *         complementary output\r
1110   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1111   *                the configuration information for TIM module.\r
1112   * @param  Channel: TIM Channel to be enabled.\r
1113   *          This parameter can be one of the following values:\r
1114   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1115   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1116   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1117   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1118   * @param  pData: The source Buffer address.\r
1119   * @param  Length: The length of data to be transferred from memory to TIM peripheral\r
1120   * @retval HAL status\r
1121   */\r
1122 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)\r
1123 {\r
1124   /* Check the parameters */\r
1125   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
1126   \r
1127   if((htim->State == HAL_TIM_STATE_BUSY))\r
1128   {\r
1129      return HAL_BUSY;\r
1130   }\r
1131   else if((htim->State == HAL_TIM_STATE_READY))\r
1132   {\r
1133     if(((uint32_t)pData == 0 ) && (Length > 0)) \r
1134     {\r
1135       return HAL_ERROR;                                    \r
1136     }\r
1137     else\r
1138     {\r
1139       htim->State = HAL_TIM_STATE_BUSY;\r
1140     }\r
1141   }    \r
1142   switch (Channel)\r
1143   {\r
1144     case TIM_CHANNEL_1:\r
1145     {      \r
1146       /* Set the DMA Period elapsed callback */\r
1147       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
1148      \r
1149       /* Set the DMA error callback */\r
1150       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;\r
1151       \r
1152       /* Enable the DMA Stream */\r
1153       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);\r
1154       \r
1155       /* Enable the TIM Capture/Compare 1 DMA request */\r
1156       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);\r
1157     }\r
1158     break;\r
1159     \r
1160     case TIM_CHANNEL_2:\r
1161     {\r
1162       /* Set the DMA Period elapsed callback */\r
1163       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
1164      \r
1165       /* Set the DMA error callback */\r
1166       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;\r
1167       \r
1168       /* Enable the DMA Stream */\r
1169       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);\r
1170       \r
1171       /* Enable the TIM Capture/Compare 2 DMA request */\r
1172       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);\r
1173     }\r
1174     break;\r
1175     \r
1176     case TIM_CHANNEL_3:\r
1177     {\r
1178       /* Set the DMA Period elapsed callback */\r
1179       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
1180      \r
1181       /* Set the DMA error callback */\r
1182       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;\r
1183       \r
1184       /* Enable the DMA Stream */\r
1185       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);\r
1186       \r
1187       /* Enable the TIM Capture/Compare 3 DMA request */\r
1188       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);\r
1189     }\r
1190     break;\r
1191     \r
1192     case TIM_CHANNEL_4:\r
1193     {\r
1194      /* Set the DMA Period elapsed callback */\r
1195       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;\r
1196      \r
1197       /* Set the DMA error callback */\r
1198       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;\r
1199       \r
1200       /* Enable the DMA Stream */\r
1201       HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);\r
1202       \r
1203       /* Enable the TIM Capture/Compare 4 DMA request */\r
1204       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);\r
1205     }\r
1206     break;\r
1207     \r
1208     default:\r
1209     break;\r
1210   }\r
1211 \r
1212   /* Enable the complementary PWM output  */\r
1213      TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);\r
1214     \r
1215   /* Enable the Main Output */\r
1216     __HAL_TIM_MOE_ENABLE(htim);\r
1217   \r
1218   /* Enable the Peripheral */\r
1219   __HAL_TIM_ENABLE(htim); \r
1220   \r
1221   /* Return function status */\r
1222   return HAL_OK;\r
1223 }\r
1224 \r
1225 /**\r
1226   * @brief  Stops the TIM PWM signal generation in DMA mode on the complementary\r
1227   *         output\r
1228   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1229   *                the configuration information for TIM module.\r
1230   * @param  Channel: TIM Channel to be disabled.\r
1231   *          This parameter can be one of the following values:\r
1232   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1233   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1234   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1235   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1236   * @retval HAL status\r
1237   */\r
1238 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)\r
1239 {\r
1240   /* Check the parameters */\r
1241   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel)); \r
1242   \r
1243   switch (Channel)\r
1244   {\r
1245     case TIM_CHANNEL_1:\r
1246     {       \r
1247       /* Disable the TIM Capture/Compare 1 DMA request */\r
1248       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);\r
1249     }\r
1250     break;\r
1251     \r
1252     case TIM_CHANNEL_2:\r
1253     {\r
1254       /* Disable the TIM Capture/Compare 2 DMA request */\r
1255       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);\r
1256     }\r
1257     break;\r
1258     \r
1259     case TIM_CHANNEL_3:\r
1260     {\r
1261       /* Disable the TIM Capture/Compare 3 DMA request */\r
1262       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);\r
1263     }\r
1264     break;\r
1265     \r
1266     case TIM_CHANNEL_4:\r
1267     {\r
1268       /* Disable the TIM Capture/Compare 4 DMA request */\r
1269       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);\r
1270     }\r
1271     break;\r
1272     \r
1273     default:\r
1274     break;\r
1275   } \r
1276   \r
1277   /* Disable the complementary PWM output */\r
1278     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);\r
1279      \r
1280   /* Disable the Main Output */\r
1281     __HAL_TIM_MOE_DISABLE(htim);\r
1282 \r
1283   /* Disable the Peripheral */\r
1284   __HAL_TIM_DISABLE(htim);\r
1285   \r
1286   /* Change the htim state */\r
1287   htim->State = HAL_TIM_STATE_READY;\r
1288   \r
1289   /* Return function status */\r
1290   return HAL_OK;\r
1291 }\r
1292 \r
1293 /**\r
1294   * @}\r
1295   */\r
1296   \r
1297 /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions\r
1298  *  @brief    Timer Complementary One Pulse functions \r
1299  *\r
1300 @verbatim   \r
1301   ==============================================================================\r
1302                 ##### Timer Complementary One Pulse functions #####\r
1303   ==============================================================================  \r
1304   [..]  \r
1305     This section provides functions allowing to:\r
1306     (+) Start the Complementary One Pulse generation.\r
1307     (+) Stop the Complementary One Pulse.\r
1308     (+) Start the Complementary One Pulse and enable interrupts.\r
1309     (+) Stop the Complementary One Pulse and disable interrupts.\r
1310                \r
1311 @endverbatim\r
1312   * @{\r
1313   */\r
1314 \r
1315 /**\r
1316   * @brief  Starts the TIM One Pulse signal generation on the complemetary \r
1317   *         output.\r
1318   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1319   *                the configuration information for TIM module.\r
1320   * @param  OutputChannel: TIM Channel to be enabled.\r
1321   *          This parameter can be one of the following values:\r
1322   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1323   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1324   * @retval HAL status\r
1325   */\r
1326 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)\r
1327   {\r
1328   /* Check the parameters */\r
1329   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel)); \r
1330   \r
1331   /* Enable the complementary One Pulse output */\r
1332   TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE); \r
1333   \r
1334   /* Enable the Main Output */\r
1335   __HAL_TIM_MOE_ENABLE(htim);\r
1336   \r
1337   /* Return function status */\r
1338   return HAL_OK;\r
1339 }\r
1340 \r
1341 /**\r
1342   * @brief  Stops the TIM One Pulse signal generation on the complementary \r
1343   *         output.\r
1344   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1345   *                the configuration information for TIM module.\r
1346   * @param  OutputChannel: TIM Channel to be disabled.\r
1347   *          This parameter can be one of the following values:\r
1348   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1349   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1350   * @retval HAL status\r
1351   */\r
1352 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)\r
1353 {\r
1354 \r
1355   /* Check the parameters */\r
1356   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel)); \r
1357 \r
1358   /* Disable the complementary One Pulse output */\r
1359     TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);\r
1360   \r
1361   /* Disable the Main Output */\r
1362     __HAL_TIM_MOE_DISABLE(htim);\r
1363   \r
1364   /* Disable the Peripheral */\r
1365   __HAL_TIM_DISABLE(htim); \r
1366    \r
1367   /* Return function status */\r
1368   return HAL_OK;\r
1369 }\r
1370 \r
1371 /**\r
1372   * @brief  Starts the TIM One Pulse signal generation in interrupt mode on the\r
1373   *         complementary channel.\r
1374   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1375   *                the configuration information for TIM module.\r
1376   * @param  OutputChannel: TIM Channel to be enabled.\r
1377   *          This parameter can be one of the following values:\r
1378   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1379   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1380   * @retval HAL status\r
1381   */\r
1382 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)\r
1383 {\r
1384   /* Check the parameters */\r
1385   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel)); \r
1386 \r
1387   /* Enable the TIM Capture/Compare 1 interrupt */\r
1388   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);\r
1389   \r
1390   /* Enable the TIM Capture/Compare 2 interrupt */\r
1391   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);\r
1392   \r
1393   /* Enable the complementary One Pulse output */\r
1394   TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE); \r
1395   \r
1396   /* Enable the Main Output */\r
1397   __HAL_TIM_MOE_ENABLE(htim);\r
1398   \r
1399   /* Return function status */\r
1400   return HAL_OK;\r
1401   } \r
1402   \r
1403 /**\r
1404   * @brief  Stops the TIM One Pulse signal generation in interrupt mode on the\r
1405   *         complementary channel.\r
1406   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1407   *                the configuration information for TIM module.\r
1408   * @param  OutputChannel: TIM Channel to be disabled.\r
1409   *          This parameter can be one of the following values:\r
1410   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1411   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1412   * @retval HAL status\r
1413   */\r
1414 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)\r
1415 {\r
1416   /* Check the parameters */\r
1417   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel)); \r
1418 \r
1419   /* Disable the TIM Capture/Compare 1 interrupt */\r
1420   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);\r
1421   \r
1422   /* Disable the TIM Capture/Compare 2 interrupt */\r
1423   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);\r
1424   \r
1425   /* Disable the complementary One Pulse output */\r
1426   TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);\r
1427   \r
1428   /* Disable the Main Output */\r
1429   __HAL_TIM_MOE_DISABLE(htim);\r
1430   \r
1431   /* Disable the Peripheral */\r
1432    __HAL_TIM_DISABLE(htim);  \r
1433   \r
1434   /* Return function status */\r
1435   return HAL_OK;\r
1436 }\r
1437 \r
1438 /**\r
1439   * @}\r
1440   */\r
1441   \r
1442 /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions\r
1443  *  @brief      Peripheral Control functions \r
1444  *\r
1445 @verbatim   \r
1446   ==============================================================================\r
1447                     ##### Peripheral Control functions #####\r
1448   ==============================================================================  \r
1449   [..]  \r
1450     This section provides functions allowing to:\r
1451     (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode. \r
1452     (+) Configure External Clock source.\r
1453     (+) Configure Complementary channels, break features and dead time.\r
1454     (+) Configure Master and the Slave synchronization.\r
1455     (+) Configure the commutation event in case of use of the Hall sensor interface.\r
1456     (+) Configure the DMA Burst Mode.\r
1457       \r
1458 @endverbatim\r
1459   * @{\r
1460   */\r
1461 /**\r
1462   * @brief  Configure the TIM commutation event sequence.\r
1463   * @note  This function is mandatory to use the commutation event in order to \r
1464   *        update the configuration at each commutation detection on the TRGI input of the Timer,\r
1465   *        the typical use of this feature is with the use of another Timer(interface Timer) \r
1466   *        configured in Hall sensor interface, this interface Timer will generate the \r
1467   *        commutation at its TRGO output (connected to Timer used in this function) each time \r
1468   *        the TI1 of the Interface Timer detect a commutation at its input TI1.\r
1469   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1470   *                the configuration information for TIM module.\r
1471   * @param  InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.\r
1472   *          This parameter can be one of the following values:\r
1473   *            @arg TIM_TS_ITR0: Internal trigger 0 selected\r
1474   *            @arg TIM_TS_ITR1: Internal trigger 1 selected\r
1475   *            @arg TIM_TS_ITR2: Internal trigger 2 selected\r
1476   *            @arg TIM_TS_ITR3: Internal trigger 3 selected\r
1477   *            @arg TIM_TS_NONE: No trigger is needed \r
1478   * @param  CommutationSource: the Commutation Event source.\r
1479   *          This parameter can be one of the following values:\r
1480   *            @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer\r
1481   *            @arg TIM_COMMUTATION_SOFTWARE:  Commutation source is set by software using the COMG bit\r
1482   * @retval HAL status\r
1483   */\r
1484 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent(TIM_HandleTypeDef *htim, uint32_t  InputTrigger, uint32_t  CommutationSource)\r
1485 {\r
1486   /* Check the parameters */\r
1487   assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));\r
1488   assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));\r
1489   \r
1490   __HAL_LOCK(htim);\r
1491   \r
1492   if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||\r
1493       (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))\r
1494   {    \r
1495     /* Select the Input trigger */\r
1496     htim->Instance->SMCR &= ~TIM_SMCR_TS;\r
1497     htim->Instance->SMCR |= InputTrigger;\r
1498   }\r
1499     \r
1500   /* Select the Capture Compare preload feature */\r
1501   htim->Instance->CR2 |= TIM_CR2_CCPC;\r
1502   /* Select the Commutation event source */\r
1503   htim->Instance->CR2 &= ~TIM_CR2_CCUS;\r
1504   htim->Instance->CR2 |= CommutationSource;\r
1505     \r
1506   __HAL_UNLOCK(htim);\r
1507   \r
1508   return HAL_OK;\r
1509 }\r
1510 \r
1511 /**\r
1512   * @brief  Configure the TIM commutation event sequence with interrupt.\r
1513   * @note  This function is mandatory to use the commutation event in order to \r
1514   *        update the configuration at each commutation detection on the TRGI input of the Timer,\r
1515   *        the typical use of this feature is with the use of another Timer(interface Timer) \r
1516   *        configured in Hall sensor interface, this interface Timer will generate the \r
1517   *        commutation at its TRGO output (connected to Timer used in this function) each time \r
1518   *        the TI1 of the Interface Timer detect a commutation at its input TI1.\r
1519   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1520   *                the configuration information for TIM module.\r
1521   * @param  InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.\r
1522   *          This parameter can be one of the following values:\r
1523   *            @arg TIM_TS_ITR0: Internal trigger 0 selected\r
1524   *            @arg TIM_TS_ITR1: Internal trigger 1 selected\r
1525   *            @arg TIM_TS_ITR2: Internal trigger 2 selected\r
1526   *            @arg TIM_TS_ITR3: Internal trigger 3 selected\r
1527   *            @arg TIM_TS_NONE: No trigger is needed\r
1528   * @param  CommutationSource: the Commutation Event source.\r
1529   *          This parameter can be one of the following values:\r
1530   *            @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer\r
1531   *            @arg TIM_COMMUTATION_SOFTWARE:  Commutation source is set by software using the COMG bit\r
1532   * @retval HAL status\r
1533   */\r
1534 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_IT(TIM_HandleTypeDef *htim, uint32_t  InputTrigger, uint32_t  CommutationSource)\r
1535 {\r
1536   /* Check the parameters */\r
1537   assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));\r
1538   assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));\r
1539   \r
1540   __HAL_LOCK(htim);\r
1541   \r
1542   if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||\r
1543       (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))\r
1544   {    \r
1545     /* Select the Input trigger */\r
1546     htim->Instance->SMCR &= ~TIM_SMCR_TS;\r
1547     htim->Instance->SMCR |= InputTrigger;\r
1548   }\r
1549   \r
1550   /* Select the Capture Compare preload feature */\r
1551   htim->Instance->CR2 |= TIM_CR2_CCPC;\r
1552   /* Select the Commutation event source */\r
1553   htim->Instance->CR2 &= ~TIM_CR2_CCUS;\r
1554   htim->Instance->CR2 |= CommutationSource;\r
1555     \r
1556   /* Enable the Commutation Interrupt Request */\r
1557   __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);\r
1558 \r
1559   __HAL_UNLOCK(htim);\r
1560   \r
1561   return HAL_OK;\r
1562 }\r
1563 \r
1564 /**\r
1565   * @brief  Configure the TIM commutation event sequence with DMA.\r
1566   * @note  This function is mandatory to use the commutation event in order to \r
1567   *        update the configuration at each commutation detection on the TRGI input of the Timer,\r
1568   *        the typical use of this feature is with the use of another Timer(interface Timer) \r
1569   *        configured in Hall sensor interface, this interface Timer will generate the \r
1570   *        commutation at its TRGO output (connected to Timer used in this function) each time \r
1571   *        the TI1 of the Interface Timer detect a commutation at its input TI1.\r
1572   * @note: The user should configure the DMA in his own software, in This function only the COMDE bit is set\r
1573   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
1574   *                the configuration information for TIM module.\r
1575   * @param  InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.\r
1576   *          This parameter can be one of the following values:\r
1577   *            @arg TIM_TS_ITR0: Internal trigger 0 selected\r
1578   *            @arg TIM_TS_ITR1: Internal trigger 1 selected\r
1579   *            @arg TIM_TS_ITR2: Internal trigger 2 selected\r
1580   *            @arg TIM_TS_ITR3: Internal trigger 3 selected\r
1581   *            @arg TIM_TS_NONE: No trigger is needed\r
1582   * @param  CommutationSource: the Commutation Event source.\r
1583   *          This parameter can be one of the following values:\r
1584   *            @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer\r
1585   *            @arg TIM_COMMUTATION_SOFTWARE:  Commutation source is set by software using the COMG bit\r
1586   * @retval HAL status\r
1587   */\r
1588 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_DMA(TIM_HandleTypeDef *htim, uint32_t  InputTrigger, uint32_t  CommutationSource)\r
1589 {\r
1590   /* Check the parameters */\r
1591   assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));\r
1592   assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));\r
1593   \r
1594   __HAL_LOCK(htim);\r
1595   \r
1596   if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||\r
1597       (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))\r
1598   {    \r
1599     /* Select the Input trigger */\r
1600     htim->Instance->SMCR &= ~TIM_SMCR_TS;\r
1601     htim->Instance->SMCR |= InputTrigger;\r
1602   }\r
1603   \r
1604   /* Select the Capture Compare preload feature */\r
1605   htim->Instance->CR2 |= TIM_CR2_CCPC;\r
1606   /* Select the Commutation event source */\r
1607   htim->Instance->CR2 &= ~TIM_CR2_CCUS;\r
1608   htim->Instance->CR2 |= CommutationSource;\r
1609   \r
1610   /* Enable the Commutation DMA Request */\r
1611   /* Set the DMA Commutation Callback */\r
1612   htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = HAL_TIMEx_DMACommutationCplt;     \r
1613   /* Set the DMA error callback */\r
1614   htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = HAL_TIM_DMAError;\r
1615   \r
1616   /* Enable the Commutation DMA Request */\r
1617   __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);\r
1618 \r
1619   __HAL_UNLOCK(htim);\r
1620   \r
1621   return HAL_OK;\r
1622 }\r
1623 \r
1624 /**\r
1625   * @brief  Initializes the TIM Output Compare Channels according to the specified\r
1626   *         parameters in the TIM_OC_InitTypeDef.\r
1627   * @param  htim: TIM Output Compare handle\r
1628   * @param  sConfig: TIM Output Compare configuration structure\r
1629   * @param  Channel : TIM Channels to configure\r
1630   *          This parameter can be one of the following values:\r
1631   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1632   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1633   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1634   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected \r
1635   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected \r
1636   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected \r
1637   *            @arg TIM_CHANNEL_ALL: all output channels supported by the timer instance selected\r
1638   * @retval HAL status\r
1639   */\r
1640 HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel)\r
1641 {  \r
1642   /* Check the parameters */\r
1643   assert_param(IS_TIM_CHANNELS(Channel)); \r
1644   assert_param(IS_TIM_OC_MODE(sConfig->OCMode));\r
1645   assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));\r
1646   assert_param(IS_TIM_OCN_POLARITY(sConfig->OCNPolarity));\r
1647   assert_param(IS_TIM_OCNIDLE_STATE(sConfig->OCNIdleState));\r
1648   assert_param(IS_TIM_OCIDLE_STATE(sConfig->OCIdleState));\r
1649   \r
1650   /* Check input state */\r
1651   __HAL_LOCK(htim); \r
1652   \r
1653   htim->State = HAL_TIM_STATE_BUSY;\r
1654   \r
1655   switch (Channel)\r
1656   {\r
1657     case TIM_CHANNEL_1:\r
1658     {\r
1659       /* Check the parameters */\r
1660       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance)); \r
1661       \r
1662      /* Configure the TIM Channel 1 in Output Compare */\r
1663       TIM_OC1_SetConfig(htim->Instance, sConfig);\r
1664     }\r
1665     break;\r
1666     \r
1667     case TIM_CHANNEL_2:\r
1668     {\r
1669       /* Check the parameters */\r
1670       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance)); \r
1671       \r
1672       /* Configure the TIM Channel 2 in Output Compare */\r
1673       TIM_OC2_SetConfig(htim->Instance, sConfig);\r
1674     }\r
1675     break;\r
1676     \r
1677     case TIM_CHANNEL_3:\r
1678     {\r
1679       /* Check the parameters */\r
1680       assert_param(IS_TIM_CC3_INSTANCE(htim->Instance)); \r
1681       \r
1682       /* Configure the TIM Channel 3 in Output Compare */\r
1683       TIM_OC3_SetConfig(htim->Instance, sConfig);\r
1684     }\r
1685     break;\r
1686     \r
1687     case TIM_CHANNEL_4:\r
1688     {\r
1689       /* Check the parameters */\r
1690       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance)); \r
1691       \r
1692        /* Configure the TIM Channel 4 in Output Compare */\r
1693        TIM_OC4_SetConfig(htim->Instance, sConfig);\r
1694     }\r
1695     break;\r
1696     \r
1697     case TIM_CHANNEL_5:\r
1698     {\r
1699       /* Check the parameters */\r
1700       assert_param(IS_TIM_CC5_INSTANCE(htim->Instance)); \r
1701       \r
1702        /* Configure the TIM Channel 5 in Output Compare */\r
1703        TIM_OC5_SetConfig(htim->Instance, sConfig);\r
1704     }\r
1705     break;\r
1706     \r
1707     case TIM_CHANNEL_6:\r
1708     {\r
1709       /* Check the parameters */\r
1710       assert_param(IS_TIM_CC6_INSTANCE(htim->Instance)); \r
1711       \r
1712        /* Configure the TIM Channel 6 in Output Compare */\r
1713        TIM_OC6_SetConfig(htim->Instance, sConfig);\r
1714     }\r
1715     break;\r
1716         \r
1717     default:\r
1718     break;    \r
1719   }\r
1720   \r
1721   htim->State = HAL_TIM_STATE_READY;\r
1722   \r
1723   __HAL_UNLOCK(htim); \r
1724   \r
1725   return HAL_OK;\r
1726 }\r
1727 \r
1728 /**\r
1729   * @brief  Initializes the TIM PWM  channels according to the specified\r
1730   *         parameters in the TIM_OC_InitTypeDef.\r
1731   * @param  htim: TIM PWM handle\r
1732   * @param  sConfig: TIM PWM configuration structure\r
1733   * @param  Channel : TIM Channels to be configured\r
1734   *          This parameter can be one of the following values:\r
1735   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected\r
1736   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected\r
1737   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected\r
1738   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected\r
1739   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected \r
1740   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected \r
1741   *            @arg TIM_CHANNEL_ALL: all PWM channels supported by the timer instance selected\r
1742   * @retval HAL status\r
1743   */\r
1744 HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim, \r
1745                                             TIM_OC_InitTypeDef* sConfig, \r
1746                                             uint32_t Channel)\r
1747 {\r
1748   /* Check the parameters */\r
1749   assert_param(IS_TIM_CHANNELS(Channel)); \r
1750   assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));\r
1751   assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));\r
1752   assert_param(IS_TIM_OCN_POLARITY(sConfig->OCNPolarity));\r
1753   assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));\r
1754   assert_param(IS_TIM_OCNIDLE_STATE(sConfig->OCNIdleState));\r
1755   assert_param(IS_TIM_OCIDLE_STATE(sConfig->OCIdleState));\r
1756   \r
1757   /* Check input state */\r
1758   __HAL_LOCK(htim);\r
1759   \r
1760   htim->State = HAL_TIM_STATE_BUSY;\r
1761     \r
1762   switch (Channel)\r
1763   {\r
1764     case TIM_CHANNEL_1:\r
1765     {\r
1766       /* Check the parameters */\r
1767       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance)); \r
1768       \r
1769       /* Configure the Channel 1 in PWM mode */\r
1770       TIM_OC1_SetConfig(htim->Instance, sConfig);\r
1771       \r
1772       /* Set the Preload enable bit for channel1 */\r
1773       htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;\r
1774       \r
1775       /* Configure the Output Fast mode */\r
1776       htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;\r
1777       htim->Instance->CCMR1 |= sConfig->OCFastMode;\r
1778     }\r
1779     break;\r
1780     \r
1781     case TIM_CHANNEL_2:\r
1782     {\r
1783       /* Check the parameters */\r
1784       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance)); \r
1785       \r
1786       /* Configure the Channel 2 in PWM mode */\r
1787       TIM_OC2_SetConfig(htim->Instance, sConfig);\r
1788       \r
1789       /* Set the Preload enable bit for channel2 */\r
1790       htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;\r
1791       \r
1792       /* Configure the Output Fast mode */\r
1793       htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;\r
1794       htim->Instance->CCMR1 |= sConfig->OCFastMode << 8;\r
1795     }\r
1796     break;\r
1797     \r
1798     case TIM_CHANNEL_3:\r
1799     {\r
1800       /* Check the parameters */\r
1801       assert_param(IS_TIM_CC3_INSTANCE(htim->Instance)); \r
1802       \r
1803       /* Configure the Channel 3 in PWM mode */\r
1804       TIM_OC3_SetConfig(htim->Instance, sConfig);\r
1805       \r
1806       /* Set the Preload enable bit for channel3 */\r
1807       htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;\r
1808       \r
1809      /* Configure the Output Fast mode */\r
1810       htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;\r
1811       htim->Instance->CCMR2 |= sConfig->OCFastMode;  \r
1812     }\r
1813     break;\r
1814     \r
1815     case TIM_CHANNEL_4:\r
1816     {\r
1817       /* Check the parameters */\r
1818       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance)); \r
1819       \r
1820       /* Configure the Channel 4 in PWM mode */\r
1821       TIM_OC4_SetConfig(htim->Instance, sConfig);\r
1822       \r
1823       /* Set the Preload enable bit for channel4 */\r
1824       htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;\r
1825       \r
1826      /* Configure the Output Fast mode */\r
1827       htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;\r
1828       htim->Instance->CCMR2 |= sConfig->OCFastMode << 8;  \r
1829     }\r
1830     break;\r
1831     \r
1832     case TIM_CHANNEL_5:\r
1833     {\r
1834        /* Check the parameters */\r
1835       assert_param(IS_TIM_CC5_INSTANCE(htim->Instance)); \r
1836       \r
1837      /* Configure the Channel 5 in PWM mode */\r
1838       TIM_OC5_SetConfig(htim->Instance, sConfig);\r
1839       \r
1840       /* Set the Preload enable bit for channel5*/\r
1841       htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;\r
1842       \r
1843      /* Configure the Output Fast mode */\r
1844       htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;\r
1845       htim->Instance->CCMR3 |= sConfig->OCFastMode;  \r
1846     }\r
1847     break;\r
1848     \r
1849     case TIM_CHANNEL_6:\r
1850     {\r
1851        /* Check the parameters */\r
1852       assert_param(IS_TIM_CC6_INSTANCE(htim->Instance)); \r
1853       \r
1854      /* Configure the Channel 5 in PWM mode */\r
1855       TIM_OC6_SetConfig(htim->Instance, sConfig);\r
1856       \r
1857       /* Set the Preload enable bit for channel6 */\r
1858       htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;\r
1859       \r
1860      /* Configure the Output Fast mode */\r
1861       htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;\r
1862       htim->Instance->CCMR3 |= sConfig->OCFastMode << 8;  \r
1863     }\r
1864     break;\r
1865     \r
1866     default:\r
1867     break;    \r
1868   }\r
1869   \r
1870   htim->State = HAL_TIM_STATE_READY;\r
1871     \r
1872   __HAL_UNLOCK(htim);\r
1873   \r
1874   return HAL_OK;\r
1875 }\r
1876 \r
1877 /**\r
1878   * @brief  Configures the OCRef clear feature\r
1879   * @param  htim: TIM handle\r
1880   * @param  sClearInputConfig: pointer to a TIM_ClearInputConfigTypeDef structure that\r
1881   *         contains the OCREF clear feature and parameters for the TIM peripheral. \r
1882   * @param  Channel: specifies the TIM Channel\r
1883   *          This parameter can be one of the following values:\r
1884   *            @arg TIM_Channel_1: TIM Channel 1\r
1885   *            @arg TIM_Channel_2: TIM Channel 2\r
1886   *            @arg TIM_Channel_3: TIM Channel 3\r
1887   *            @arg TIM_Channel_4: TIM Channel 4\r
1888   *            @arg TIM_Channel_5: TIM Channel 5\r
1889   *            @arg TIM_Channel_6: TIM Channel 6\r
1890   * @retval None\r
1891   */ \r
1892 HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,\r
1893                                            TIM_ClearInputConfigTypeDef *sClearInputConfig,\r
1894                                            uint32_t Channel)\r
1895\r
1896   uint32_t tmpsmcr = 0;\r
1897 \r
1898   /* Check the parameters */ \r
1899   assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));\r
1900   assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));\r
1901                                         \r
1902   /* Check input state */\r
1903   __HAL_LOCK(htim);\r
1904   \r
1905   switch (sClearInputConfig->ClearInputSource)\r
1906   {\r
1907     case TIM_CLEARINPUTSOURCE_NONE:\r
1908     {\r
1909       /* Clear the OCREF clear selection bit */\r
1910       tmpsmcr &= ~TIM_SMCR_OCCS;\r
1911       \r
1912       /* Clear the ETR Bits */\r
1913       tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);\r
1914       \r
1915       /* Set TIMx_SMCR */\r
1916       htim->Instance->SMCR = tmpsmcr;\r
1917    }\r
1918     break;\r
1919     \r
1920     case TIM_CLEARINPUTSOURCE_OCREFCLR:\r
1921     {\r
1922       /* Clear the OCREF clear selection bit */\r
1923       htim->Instance->SMCR &= ~TIM_SMCR_OCCS;\r
1924     }\r
1925     break;\r
1926     \r
1927     case TIM_CLEARINPUTSOURCE_ETR:\r
1928     {\r
1929       /* Check the parameters */ \r
1930       assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));\r
1931       assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));\r
1932       assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));\r
1933       \r
1934       TIM_ETR_SetConfig(htim->Instance,\r
1935                         sClearInputConfig->ClearInputPrescaler,\r
1936                         sClearInputConfig->ClearInputPolarity,\r
1937                         sClearInputConfig->ClearInputFilter);\r
1938       \r
1939       /* Set the OCREF clear selection bit */\r
1940       htim->Instance->SMCR |= TIM_SMCR_OCCS;\r
1941     }\r
1942     break;\r
1943     default:  \r
1944     break;\r
1945   }\r
1946   \r
1947   switch (Channel)\r
1948   { \r
1949     case TIM_CHANNEL_1:\r
1950       {\r
1951         if(sClearInputConfig->ClearInputState != RESET)\r
1952         {\r
1953           /* Enable the Ocref clear feature for Channel 1 */\r
1954           htim->Instance->CCMR1 |= TIM_CCMR1_OC1CE;\r
1955         }\r
1956         else\r
1957         {\r
1958           /* Disable the Ocref clear feature for Channel 1 */\r
1959           htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1CE;      \r
1960         }\r
1961       }    \r
1962       break;\r
1963     case TIM_CHANNEL_2:    \r
1964       {\r
1965         if(sClearInputConfig->ClearInputState != RESET)\r
1966         {\r
1967           /* Enable the Ocref clear feature for Channel 2 */\r
1968           htim->Instance->CCMR1 |= TIM_CCMR1_OC2CE;\r
1969         }\r
1970         else\r
1971         {\r
1972           /* Disable the Ocref clear feature for Channel 2 */\r
1973           htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2CE;      \r
1974         }\r
1975       }    \r
1976     break;\r
1977     case TIM_CHANNEL_3:    \r
1978       {\r
1979         if(sClearInputConfig->ClearInputState != RESET)\r
1980         {\r
1981           /* Enable the Ocref clear feature for Channel 3 */\r
1982           htim->Instance->CCMR2 |= TIM_CCMR2_OC3CE;\r
1983         }\r
1984         else\r
1985         {\r
1986           /* Disable the Ocref clear feature for Channel 3 */\r
1987           htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3CE;      \r
1988         }\r
1989       }    \r
1990     break;\r
1991     case TIM_CHANNEL_4:    \r
1992       {\r
1993         if(sClearInputConfig->ClearInputState != RESET)\r
1994         {\r
1995           /* Enable the Ocref clear feature for Channel 4 */\r
1996           htim->Instance->CCMR2 |= TIM_CCMR2_OC4CE;\r
1997         }\r
1998         else\r
1999         {\r
2000           /* Disable the Ocref clear feature for Channel 4 */\r
2001           htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4CE;      \r
2002         }\r
2003       }    \r
2004     break;\r
2005     case TIM_CHANNEL_5:    \r
2006       {\r
2007         if(sClearInputConfig->ClearInputState != RESET)\r
2008         {\r
2009           /* Enable the Ocref clear feature for Channel 1 */\r
2010           htim->Instance->CCMR3 |= TIM_CCMR3_OC5CE;\r
2011         }\r
2012         else\r
2013         {\r
2014           /* Disable the Ocref clear feature for Channel 1 */\r
2015           htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5CE;      \r
2016         }\r
2017       }    \r
2018     break;\r
2019     case TIM_CHANNEL_6:    \r
2020       {\r
2021         if(sClearInputConfig->ClearInputState != RESET)\r
2022         {\r
2023           /* Enable the Ocref clear feature for Channel 1 */\r
2024           htim->Instance->CCMR3 |= TIM_CCMR3_OC6CE;\r
2025         }\r
2026         else\r
2027         {\r
2028           /* Disable the Ocref clear feature for Channel 1 */\r
2029           htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6CE;      \r
2030         }\r
2031       }    \r
2032     break;\r
2033     default:  \r
2034     break;\r
2035   } \r
2036   \r
2037   __HAL_UNLOCK(htim);\r
2038 \r
2039   return HAL_OK;  \r
2040 }  \r
2041 \r
2042 /**\r
2043   * @brief  Configures the TIM in master mode.\r
2044   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2045   *                the configuration information for TIM module.   \r
2046   * @param  sMasterConfig: pointer to a TIM_MasterConfigTypeDef structure that\r
2047   *         contains the selected trigger output (TRGO) and the Master/Slave \r
2048   *         mode. \r
2049   * @retval HAL status\r
2050   */\r
2051 HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, TIM_MasterConfigTypeDef * sMasterConfig)\r
2052 {\r
2053   uint32_t tmpcr2;  \r
2054   uint32_t tmpsmcr;  \r
2055 \r
2056   /* Check the parameters */\r
2057   assert_param(IS_TIM_SYNCHRO_INSTANCE(htim->Instance));\r
2058   assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));\r
2059   assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));\r
2060   \r
2061   /* Check input state */\r
2062   __HAL_LOCK(htim);\r
2063 \r
2064  /* Get the TIMx CR2 register value */\r
2065   tmpcr2 = htim->Instance->CR2;\r
2066 \r
2067   /* Get the TIMx SMCR register value */\r
2068   tmpsmcr = htim->Instance->SMCR;\r
2069 \r
2070   /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */\r
2071   if (IS_TIM_TRGO2_INSTANCE(htim->Instance))\r
2072   {\r
2073     /* Check the parameters */\r
2074     assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));\r
2075     \r
2076     /* Clear the MMS2 bits */\r
2077     tmpcr2 &= ~TIM_CR2_MMS2;\r
2078     /* Select the TRGO2 source*/\r
2079     tmpcr2 |= sMasterConfig->MasterOutputTrigger2;\r
2080   }\r
2081   \r
2082   /* Reset the MMS Bits */\r
2083   tmpcr2 &= ~TIM_CR2_MMS;\r
2084   /* Select the TRGO source */\r
2085   tmpcr2 |=  sMasterConfig->MasterOutputTrigger;\r
2086 \r
2087   /* Reset the MSM Bit */\r
2088   tmpsmcr &= ~TIM_SMCR_MSM;\r
2089   /* Set master mode */\r
2090   tmpsmcr |= sMasterConfig->MasterSlaveMode;\r
2091   \r
2092   /* Update TIMx CR2 */\r
2093   htim->Instance->CR2 = tmpcr2;\r
2094   \r
2095   /* Update TIMx SMCR */\r
2096   htim->Instance->SMCR = tmpsmcr;\r
2097 \r
2098   __HAL_UNLOCK(htim);\r
2099   \r
2100   return HAL_OK;\r
2101\r
2102                                                      \r
2103 /**\r
2104   * @brief   Configures the Break feature, dead time, Lock level, OSSI/OSSR State\r
2105   *         and the AOE(automatic output enable).\r
2106   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2107   *                the configuration information for TIM module.\r
2108   * @param  sBreakDeadTimeConfig: pointer to a TIM_ConfigBreakDeadConfig_TypeDef structure that\r
2109   *         contains the BDTR Register configuration  information for the TIM peripheral. \r
2110   * @retval HAL status\r
2111   */    \r
2112 HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim, \r
2113                                               TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)\r
2114 {\r
2115   uint32_t tmpbdtr = 0;\r
2116   \r
2117   /* Check the parameters */\r
2118   assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));\r
2119   assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));\r
2120   assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));\r
2121   assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));\r
2122   assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));\r
2123   assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));\r
2124   assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));\r
2125   assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));\r
2126   assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));\r
2127   assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));\r
2128   assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));\r
2129   assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));\r
2130   \r
2131   /* Check input state */\r
2132   __HAL_LOCK(htim);\r
2133   \r
2134   htim->State = HAL_TIM_STATE_BUSY;\r
2135 \r
2136   /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,\r
2137      the OSSI State, the dead time value and the Automatic Output Enable Bit */\r
2138     \r
2139   /* Clear the BDTR bits */\r
2140   tmpbdtr &= ~(TIM_BDTR_DTG | TIM_BDTR_LOCK |  TIM_BDTR_OSSI | \r
2141                TIM_BDTR_OSSR | TIM_BDTR_BKE | TIM_BDTR_BKP | \r
2142                TIM_BDTR_AOE | TIM_BDTR_MOE | TIM_BDTR_BKF |\r
2143                TIM_BDTR_BK2F | TIM_BDTR_BK2E | TIM_BDTR_BK2P);\r
2144 \r
2145   /* Set the BDTR bits */\r
2146   tmpbdtr |= sBreakDeadTimeConfig->DeadTime;\r
2147   tmpbdtr |= sBreakDeadTimeConfig->LockLevel;\r
2148   tmpbdtr |= sBreakDeadTimeConfig->OffStateIDLEMode;\r
2149   tmpbdtr |= sBreakDeadTimeConfig->OffStateRunMode;\r
2150   tmpbdtr |= sBreakDeadTimeConfig->BreakState;\r
2151   tmpbdtr |= sBreakDeadTimeConfig->BreakPolarity;\r
2152   tmpbdtr |= sBreakDeadTimeConfig->AutomaticOutput;\r
2153   tmpbdtr |= (sBreakDeadTimeConfig->BreakFilter << BDTR_BKF_SHIFT);\r
2154   tmpbdtr |= (sBreakDeadTimeConfig->Break2Filter << BDTR_BK2F_SHIFT);\r
2155   tmpbdtr |= sBreakDeadTimeConfig->Break2State;\r
2156   tmpbdtr |= sBreakDeadTimeConfig->Break2Polarity;\r
2157   \r
2158   /* Set TIMx_BDTR */\r
2159   htim->Instance->BDTR = tmpbdtr;\r
2160   \r
2161   __HAL_UNLOCK(htim);\r
2162   \r
2163   return HAL_OK;\r
2164 }\r
2165 \r
2166 /**\r
2167   * @brief  Configures the TIM2, TIM5 and TIM11 Remapping input capabilities.\r
2168   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2169   *                the configuration information for TIM module.\r
2170   * @param  TIM_Remap: specifies the TIM input remapping source.\r
2171   *          This parameter can be one of the following values:\r
2172   *            @arg TIM_TIM2_TIM8_TRGO: TIM2 ITR1 input is connected to TIM8 Trigger output(default)\r
2173   *            @arg TIM_TIM2_ETH_PTP:   TIM2 ITR1 input is connected to ETH PTP trigger output.\r
2174   *            @arg TIM_TIM2_USBFS_SOF: TIM2 ITR1 input is connected to USB FS SOF. \r
2175   *            @arg TIM_TIM2_USBHS_SOF: TIM2 ITR1 input is connected to USB HS SOF. \r
2176   *            @arg TIM_TIM5_GPIO:      TIM5 CH4 input is connected to dedicated Timer pin(default)\r
2177   *            @arg TIM_TIM5_LSI:       TIM5 CH4 input is connected to LSI clock.\r
2178   *            @arg TIM_TIM5_LSE:       TIM5 CH4 input is connected to LSE clock.\r
2179   *            @arg TIM_TIM5_RTC:       TIM5 CH4 input is connected to RTC Output event.\r
2180   *            @arg TIM_TIM11_GPIO:     TIM11 CH4 input is connected to dedicated Timer pin(default) \r
2181   *            @arg TIM_TIM11_SPDIF:    SPDIF Frame synchronous   \r
2182   *            @arg TIM_TIM11_HSE:      TIM11 CH4 input is connected to HSE_RTC clock\r
2183   *                                     (HSE divided by a programmable prescaler) \r
2184   *            @arg TIM_TIM11_MCO1:     TIM11 CH1 input is connected to MCO1    \r
2185   * @retval HAL status\r
2186   */\r
2187 HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)\r
2188 {\r
2189   __HAL_LOCK(htim);\r
2190     \r
2191   /* Check parameters */\r
2192   assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));\r
2193   assert_param(IS_TIM_REMAP(Remap));\r
2194   \r
2195   /* Set the Timer remapping configuration */\r
2196   htim->Instance->OR = Remap;\r
2197   \r
2198   htim->State = HAL_TIM_STATE_READY;\r
2199   \r
2200   __HAL_UNLOCK(htim);  \r
2201   \r
2202   return HAL_OK;\r
2203 }\r
2204 \r
2205 /**\r
2206   * @brief  Group channel 5 and channel 1, 2 or 3\r
2207   * @param  htim: TIM handle.\r
2208   * @param  OCRef: specifies the reference signal(s) the OC5REF is combined with.\r
2209   *         This parameter can be any combination of the following values:\r
2210   *         TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC\r
2211   *         TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF\r
2212   *         TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF\r
2213   *         TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF\r
2214   * @retval HAL status\r
2215   */\r
2216 HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t OCRef)\r
2217 {\r
2218   /* Check parameters */\r
2219   assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));\r
2220   assert_param(IS_TIM_GROUPCH5(OCRef));\r
2221 \r
2222   /* Process Locked */\r
2223   __HAL_LOCK(htim);\r
2224   \r
2225   htim->State = HAL_TIM_STATE_BUSY;\r
2226   \r
2227   /* Clear GC5Cx bit fields */\r
2228   htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3|TIM_CCR5_GC5C2|TIM_CCR5_GC5C1);\r
2229   \r
2230   /* Set GC5Cx bit fields */\r
2231   htim->Instance->CCR5 |= OCRef;\r
2232                                    \r
2233   htim->State = HAL_TIM_STATE_READY;                                 \r
2234   \r
2235   __HAL_UNLOCK(htim);\r
2236   \r
2237   return HAL_OK;\r
2238 }\r
2239 \r
2240 /**\r
2241   * @}\r
2242   */\r
2243 \r
2244 /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions \r
2245   * @brief    Extended Callbacks functions\r
2246  *\r
2247 @verbatim   \r
2248   ==============================================================================\r
2249                     ##### Extension Callbacks functions #####\r
2250   ==============================================================================  \r
2251   [..]  \r
2252     This section provides Extension TIM callback functions:\r
2253     (+) Timer Commutation callback\r
2254     (+) Timer Break callback\r
2255 \r
2256 @endverbatim\r
2257   * @{\r
2258   */\r
2259 \r
2260 /**\r
2261   * @brief  Hall commutation changed callback in non blocking mode \r
2262   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2263   *                the configuration information for TIM module.\r
2264   * @retval None\r
2265   */\r
2266 __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)\r
2267 {\r
2268   /* NOTE : This function Should not be modified, when the callback is needed,\r
2269             the HAL_TIMEx_CommutationCallback could be implemented in the user file\r
2270    */\r
2271 }\r
2272 \r
2273 /**\r
2274   * @brief  Hall Break detection callback in non blocking mode \r
2275   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2276   *                the configuration information for TIM module.\r
2277   * @retval None\r
2278   */\r
2279 __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)\r
2280 {\r
2281   /* NOTE : This function Should not be modified, when the callback is needed,\r
2282             the HAL_TIMEx_BreakCallback could be implemented in the user file\r
2283    */\r
2284 }\r
2285 \r
2286 /**\r
2287   * @}\r
2288   */\r
2289 \r
2290 /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions \r
2291  *  @brief    Extended Peripheral State functions\r
2292  *\r
2293 @verbatim   \r
2294   ==============================================================================\r
2295                 ##### Extension Peripheral State functions #####\r
2296   ==============================================================================  \r
2297   [..]\r
2298     This subsection permits to get in run-time the status of the peripheral \r
2299     and the data flow.\r
2300 \r
2301 @endverbatim\r
2302   * @{\r
2303   */\r
2304 \r
2305 /**\r
2306   * @brief  Return the TIM Hall Sensor interface state\r
2307   * @param  htim: pointer to a TIM_HandleTypeDef structure that contains\r
2308   *                the configuration information for TIM module.\r
2309   * @retval HAL state\r
2310   */\r
2311 HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)\r
2312 {\r
2313   return htim->State;\r
2314 }\r
2315 \r
2316 /**\r
2317   * @}\r
2318   */\r
2319 \r
2320 /**\r
2321   * @brief  TIM DMA Commutation callback. \r
2322   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
2323   *                the configuration information for the specified DMA module.\r
2324   * @retval None\r
2325   */\r
2326 void HAL_TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)\r
2327 {\r
2328   TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
2329   \r
2330   htim->State= HAL_TIM_STATE_READY;\r
2331     \r
2332   HAL_TIMEx_CommutationCallback(htim); \r
2333 }\r
2334 \r
2335 /**\r
2336   * @brief  Enables or disables the TIM Capture Compare Channel xN.\r
2337   * @param  TIMx to select the TIM peripheral\r
2338   * @param  Channel: specifies the TIM Channel\r
2339   *          This parameter can be one of the following values:\r
2340   *            @arg TIM_Channel_1: TIM Channel 1\r
2341   *            @arg TIM_Channel_2: TIM Channel 2\r
2342   *            @arg TIM_Channel_3: TIM Channel 3\r
2343   * @param  ChannelNState: specifies the TIM Channel CCxNE bit new state.\r
2344   *          This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable. \r
2345   * @retval None\r
2346   */\r
2347 static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState)\r
2348 {\r
2349   uint32_t tmp = 0;\r
2350 \r
2351   /* Check the parameters */\r
2352   assert_param(IS_TIM_ADVANCED_INSTANCE(TIMx));\r
2353   assert_param(IS_TIM_COMPLEMENTARY_CHANNELS(Channel));\r
2354 \r
2355   tmp = TIM_CCER_CC1NE << Channel;\r
2356 \r
2357   /* Reset the CCxNE Bit */\r
2358   TIMx->CCER &= ~tmp;\r
2359 \r
2360   /* Set or reset the CCxNE Bit */ \r
2361   TIMx->CCER |= (uint32_t)(ChannelNState << Channel);\r
2362 }\r
2363 \r
2364 /**\r
2365   * @brief  Timer Output Compare 5 configuration\r
2366   * @param  TIMx to select the TIM peripheral\r
2367   * @param  OC_Config: The output configuration structure\r
2368   * @retval None\r
2369   */\r
2370 static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)\r
2371 {\r
2372   uint32_t tmpccmrx = 0;\r
2373   uint32_t tmpccer = 0;\r
2374   uint32_t tmpcr2 = 0; \r
2375 \r
2376   /* Disable the output: Reset the CCxE Bit */\r
2377   TIMx->CCER &= ~TIM_CCER_CC5E;\r
2378   \r
2379   /* Get the TIMx CCER register value */\r
2380   tmpccer = TIMx->CCER;\r
2381   /* Get the TIMx CR2 register value */\r
2382   tmpcr2 =  TIMx->CR2; \r
2383   /* Get the TIMx CCMR1 register value */\r
2384   tmpccmrx = TIMx->CCMR3;\r
2385 \r
2386   /* Reset the Output Compare Mode Bits */\r
2387   tmpccmrx &= ~(TIM_CCMR3_OC5M);\r
2388   /* Select the Output Compare Mode */\r
2389   tmpccmrx |= OC_Config->OCMode;\r
2390   \r
2391   /* Reset the Output Polarity level */\r
2392   tmpccer &= ~TIM_CCER_CC5P;\r
2393   /* Set the Output Compare Polarity */\r
2394   tmpccer |= (OC_Config->OCPolarity << 16);\r
2395 \r
2396   if(IS_TIM_BREAK_INSTANCE(TIMx))\r
2397   {   \r
2398     /* Reset the Output Compare IDLE State */\r
2399     tmpcr2 &= ~TIM_CR2_OIS5;\r
2400     /* Set the Output Idle state */\r
2401     tmpcr2 |= (OC_Config->OCIdleState << 8);\r
2402   }\r
2403   /* Write to TIMx CR2 */\r
2404   TIMx->CR2 = tmpcr2;\r
2405   \r
2406   /* Write to TIMx CCMR3 */\r
2407   TIMx->CCMR3 = tmpccmrx;\r
2408   \r
2409   /* Set the Capture Compare Register value */\r
2410   TIMx->CCR5 = OC_Config->Pulse;\r
2411   \r
2412   /* Write to TIMx CCER */\r
2413   TIMx->CCER = tmpccer;  \r
2414 }\r
2415 \r
2416 /**\r
2417   * @brief  Timer Output Compare 6 configuration\r
2418   * @param  TIMx to select the TIM peripheral\r
2419   * @param  OC_Config: The output configuration structure\r
2420   * @retval None\r
2421   */\r
2422 static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)\r
2423 {\r
2424   uint32_t tmpccmrx = 0;\r
2425   uint32_t tmpccer = 0;\r
2426   uint32_t tmpcr2 = 0; \r
2427 \r
2428   /* Disable the output: Reset the CCxE Bit */\r
2429   TIMx->CCER &= ~TIM_CCER_CC6E;\r
2430   \r
2431   /* Get the TIMx CCER register value */\r
2432   tmpccer = TIMx->CCER;\r
2433   /* Get the TIMx CR2 register value */\r
2434   tmpcr2 =  TIMx->CR2; \r
2435   /* Get the TIMx CCMR1 register value */\r
2436   tmpccmrx = TIMx->CCMR3;\r
2437     \r
2438   /* Reset the Output Compare Mode Bits */\r
2439   tmpccmrx &= ~(TIM_CCMR3_OC6M);\r
2440   /* Select the Output Compare Mode */\r
2441   tmpccmrx |= (OC_Config->OCMode << 8);\r
2442   \r
2443   /* Reset the Output Polarity level */\r
2444   tmpccer &= (uint32_t)~TIM_CCER_CC6P;\r
2445   /* Set the Output Compare Polarity */\r
2446   tmpccer |= (OC_Config->OCPolarity << 20);\r
2447 \r
2448   if(IS_TIM_BREAK_INSTANCE(TIMx))\r
2449   {   \r
2450     /* Reset the Output Compare IDLE State */\r
2451     tmpcr2 &= ~TIM_CR2_OIS6;\r
2452     /* Set the Output Idle state */\r
2453     tmpcr2 |= (OC_Config->OCIdleState << 10);\r
2454   }\r
2455   \r
2456   /* Write to TIMx CR2 */\r
2457   TIMx->CR2 = tmpcr2;\r
2458   \r
2459   /* Write to TIMx CCMR3 */\r
2460   TIMx->CCMR3 = tmpccmrx;\r
2461   \r
2462   /* Set the Capture Compare Register value */\r
2463   TIMx->CCR6 = OC_Config->Pulse;\r
2464   \r
2465   /* Write to TIMx CCER */\r
2466   TIMx->CCER = tmpccer;  \r
2467\r
2468 \r
2469 /**\r
2470   * @}\r
2471   */\r
2472 \r
2473 #endif /* HAL_TIM_MODULE_ENABLED */\r
2474 /**\r
2475   * @}\r
2476   */ \r
2477 \r
2478 /**\r
2479   * @}\r
2480   */ \r
2481 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r