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