1 /**
2   ******************************************************************************
3   * @file    stm32h7xx_hal_tim.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 (TIM) peripheral:
8   *           + TIM Time Base Initialization
9   *           + TIM Time Base Start
10   *           + TIM Time Base Start Interruption
11   *           + TIM Time Base Start DMA
12   *           + TIM Output Compare/PWM Initialization
13   *           + TIM Output Compare/PWM Channel Configuration
14   *           + TIM Output Compare/PWM  Start
15   *           + TIM Output Compare/PWM  Start Interruption
16   *           + TIM Output Compare/PWM Start DMA
17   *           + TIM Input Capture Initialization
18   *           + TIM Input Capture Channel Configuration
19   *           + TIM Input Capture Start
20   *           + TIM Input Capture Start Interruption
21   *           + TIM Input Capture Start DMA
22   *           + TIM One Pulse Initialization
23   *           + TIM One Pulse Channel Configuration
24   *           + TIM One Pulse Start
25   *           + TIM Encoder Interface Initialization
26   *           + TIM Encoder Interface Start
27   *           + TIM Encoder Interface Start Interruption
28   *           + TIM Encoder Interface Start DMA
29   *           + Commutation Event configuration with Interruption and DMA
30   *           + TIM OCRef clear configuration
31   *           + TIM External Clock configuration
32   ******************************************************************************
33   * @attention
34   *
35   * Copyright (c) 2017 STMicroelectronics.
36   * All rights reserved.
37   *
38   * This software is licensed under terms that can be found in the LICENSE file
39   * in the root directory of this software component.
40   * If no LICENSE file comes with this software, it is provided AS-IS.
41   *
42   ******************************************************************************
43   @verbatim
44   ==============================================================================
45                       ##### TIMER Generic features #####
46   ==============================================================================
47   [..] The Timer features include:
48        (#) 16-bit up, down, up/down auto-reload counter.
49        (#) 16-bit programmable prescaler allowing dividing (also on the fly) the
50            counter clock frequency either by any factor between 1 and 65536.
51        (#) Up to 4 independent channels for:
52            (++) Input Capture
53            (++) Output Compare
54            (++) PWM generation (Edge and Center-aligned Mode)
55            (++) One-pulse mode output
56        (#) Synchronization circuit to control the timer with external signals and to interconnect
57             several timers together.
58        (#) Supports incremental encoder for positioning purposes
59 
60             ##### How to use this driver #####
61   ==============================================================================
62     [..]
63      (#) Initialize the TIM low level resources by implementing the following functions
64          depending on the selected feature:
65            (++) Time Base : HAL_TIM_Base_MspInit()
66            (++) Input Capture : HAL_TIM_IC_MspInit()
67            (++) Output Compare : HAL_TIM_OC_MspInit()
68            (++) PWM generation : HAL_TIM_PWM_MspInit()
69            (++) One-pulse mode output : HAL_TIM_OnePulse_MspInit()
70            (++) Encoder mode output : HAL_TIM_Encoder_MspInit()
71 
72      (#) Initialize the TIM low level resources :
73         (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
74         (##) TIM pins configuration
75             (+++) Enable the clock for the TIM GPIOs using the following function:
76              __HAL_RCC_GPIOx_CLK_ENABLE();
77             (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
78 
79      (#) The external Clock can be configured, if needed (the default clock is the
80          internal clock from the APBx), using the following function:
81          HAL_TIM_ConfigClockSource, the clock configuration should be done before
82          any start function.
83 
84      (#) Configure the TIM in the desired functioning mode using one of the
85        Initialization function of this driver:
86        (++) HAL_TIM_Base_Init: to use the Timer to generate a simple time base
87        (++) HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an
88             Output Compare signal.
89        (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a
90             PWM signal.
91        (++) HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an
92             external signal.
93        (++) HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer
94             in One Pulse Mode.
95        (++) HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
96 
97      (#) Activate the TIM peripheral using one of the start functions depending from the feature used:
98            (++) Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
99            (++) Input Capture :  HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
100            (++) Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
101            (++) PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
102            (++) One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
103            (++) Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(), HAL_TIM_Encoder_Start_IT().
104 
105      (#) The DMA Burst is managed with the two following functions:
106          HAL_TIM_DMABurst_WriteStart()
107          HAL_TIM_DMABurst_ReadStart()
108 
109     *** Callback registration ***
110   =============================================
111 
112   [..]
113   The compilation define  USE_HAL_TIM_REGISTER_CALLBACKS when set to 1
114   allows the user to configure dynamically the driver callbacks.
115 
116   [..]
117   Use Function HAL_TIM_RegisterCallback() to register a callback.
118   HAL_TIM_RegisterCallback() takes as parameters the HAL peripheral handle,
119   the Callback ID and a pointer to the user callback function.
120 
121   [..]
122   Use function HAL_TIM_UnRegisterCallback() to reset a callback to the default
123   weak function.
124   HAL_TIM_UnRegisterCallback takes as parameters the HAL peripheral handle,
125   and the Callback ID.
126 
127   [..]
128   These functions allow to register/unregister following callbacks:
129     (+) Base_MspInitCallback              : TIM Base Msp Init Callback.
130     (+) Base_MspDeInitCallback            : TIM Base Msp DeInit Callback.
131     (+) IC_MspInitCallback                : TIM IC Msp Init Callback.
132     (+) IC_MspDeInitCallback              : TIM IC Msp DeInit Callback.
133     (+) OC_MspInitCallback                : TIM OC Msp Init Callback.
134     (+) OC_MspDeInitCallback              : TIM OC Msp DeInit Callback.
135     (+) PWM_MspInitCallback               : TIM PWM Msp Init Callback.
136     (+) PWM_MspDeInitCallback             : TIM PWM Msp DeInit Callback.
137     (+) OnePulse_MspInitCallback          : TIM One Pulse Msp Init Callback.
138     (+) OnePulse_MspDeInitCallback        : TIM One Pulse Msp DeInit Callback.
139     (+) Encoder_MspInitCallback           : TIM Encoder Msp Init Callback.
140     (+) Encoder_MspDeInitCallback         : TIM Encoder Msp DeInit Callback.
141     (+) HallSensor_MspInitCallback        : TIM Hall Sensor Msp Init Callback.
142     (+) HallSensor_MspDeInitCallback      : TIM Hall Sensor Msp DeInit Callback.
143     (+) PeriodElapsedCallback             : TIM Period Elapsed Callback.
144     (+) PeriodElapsedHalfCpltCallback     : TIM Period Elapsed half complete Callback.
145     (+) TriggerCallback                   : TIM Trigger Callback.
146     (+) TriggerHalfCpltCallback           : TIM Trigger half complete Callback.
147     (+) IC_CaptureCallback                : TIM Input Capture Callback.
148     (+) IC_CaptureHalfCpltCallback        : TIM Input Capture half complete Callback.
149     (+) OC_DelayElapsedCallback           : TIM Output Compare Delay Elapsed Callback.
150     (+) PWM_PulseFinishedCallback         : TIM PWM Pulse Finished Callback.
151     (+) PWM_PulseFinishedHalfCpltCallback : TIM PWM Pulse Finished half complete Callback.
152     (+) ErrorCallback                     : TIM Error Callback.
153     (+) CommutationCallback               : TIM Commutation Callback.
154     (+) CommutationHalfCpltCallback       : TIM Commutation half complete Callback.
155     (+) BreakCallback                     : TIM Break Callback.
156     (+) Break2Callback                    : TIM Break2 Callback.
157 
158   [..]
159 By default, after the Init and when the state is HAL_TIM_STATE_RESET
160 all interrupt callbacks are set to the corresponding weak functions:
161   examples HAL_TIM_TriggerCallback(), HAL_TIM_ErrorCallback().
162 
163   [..]
164   Exception done for MspInit and MspDeInit functions that are reset to the legacy weak
165   functionalities in the Init / DeInit only when these callbacks are null
166   (not registered beforehand). If not, MspInit or MspDeInit are not null, the Init / DeInit
167     keep and use the user MspInit / MspDeInit callbacks(registered beforehand)
168 
169   [..]
170     Callbacks can be registered / unregistered in HAL_TIM_STATE_READY state only.
171     Exception done MspInit / MspDeInit that can be registered / unregistered
172     in HAL_TIM_STATE_READY or HAL_TIM_STATE_RESET state,
173     thus registered(user) MspInit / DeInit callbacks can be used during the Init / DeInit.
174   In that case first register the MspInit/MspDeInit user callbacks
175       using HAL_TIM_RegisterCallback() before calling DeInit or Init function.
176 
177   [..]
178       When The compilation define USE_HAL_TIM_REGISTER_CALLBACKS is set to 0 or
179       not defined, the callback registration feature is not available and all callbacks
180       are set to the corresponding weak functions.
181 
182   @endverbatim
183   ******************************************************************************
184   */
185 
186 /* Includes ------------------------------------------------------------------*/
187 #include "stm32h7xx_hal.h"
188 
189 /** @addtogroup STM32H7xx_HAL_Driver
190   * @{
191   */
192 
193 /** @defgroup TIM TIM
194   * @brief TIM HAL module driver
195   * @{
196   */
197 
198 #ifdef HAL_TIM_MODULE_ENABLED
199 
200 /* Private typedef -----------------------------------------------------------*/
201 /* Private define ------------------------------------------------------------*/
202 /* Private macros ------------------------------------------------------------*/
203 /* Private variables ---------------------------------------------------------*/
204 /* Private function prototypes -----------------------------------------------*/
205 /** @addtogroup TIM_Private_Functions
206   * @{
207   */
208 static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
209 static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
210 static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
211 static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
212 static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
213 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
214 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
215                               uint32_t TIM_ICFilter);
216 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
217 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
218                               uint32_t TIM_ICFilter);
219 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
220                               uint32_t TIM_ICFilter);
221 static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource);
222 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma);
223 static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma);
224 static void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma);
225 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);
226 static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma);
227 static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
228                                                   const TIM_SlaveConfigTypeDef *sSlaveConfig);
229 /**
230   * @}
231   */
232 /* Exported functions --------------------------------------------------------*/
233 
234 /** @defgroup TIM_Exported_Functions TIM Exported Functions
235   * @{
236   */
237 
238 /** @defgroup TIM_Exported_Functions_Group1 TIM Time Base functions
239   *  @brief    Time Base functions
240   *
241 @verbatim
242   ==============================================================================
243               ##### Time Base functions #####
244   ==============================================================================
245   [..]
246     This section provides functions allowing to:
247     (+) Initialize and configure the TIM base.
248     (+) De-initialize the TIM base.
249     (+) Start the Time Base.
250     (+) Stop the Time Base.
251     (+) Start the Time Base and enable interrupt.
252     (+) Stop the Time Base and disable interrupt.
253     (+) Start the Time Base and enable DMA transfer.
254     (+) Stop the Time Base and disable DMA transfer.
255 
256 @endverbatim
257   * @{
258   */
259 /**
260   * @brief  Initializes the TIM Time base Unit according to the specified
261   *         parameters in the TIM_HandleTypeDef and initialize the associated handle.
262   * @note   Switching from Center Aligned counter mode to Edge counter mode (or reverse)
263   *         requires a timer reset to avoid unexpected direction
264   *         due to DIR bit readonly in center aligned mode.
265   *         Ex: call @ref HAL_TIM_Base_DeInit() before HAL_TIM_Base_Init()
266   * @param  htim TIM Base handle
267   * @retval HAL status
268   */
HAL_TIM_Base_Init(TIM_HandleTypeDef * htim)269 HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
270 {
271   /* Check the TIM handle allocation */
272   if (htim == NULL)
273   {
274     return HAL_ERROR;
275   }
276 
277   /* Check the parameters */
278   assert_param(IS_TIM_INSTANCE(htim->Instance));
279   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
280   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
281   assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
282   assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
283 
284   if (htim->State == HAL_TIM_STATE_RESET)
285   {
286     /* Allocate lock resource and initialize it */
287     htim->Lock = HAL_UNLOCKED;
288 
289 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
290     /* Reset interrupt callbacks to legacy weak callbacks */
291     TIM_ResetCallback(htim);
292 
293     if (htim->Base_MspInitCallback == NULL)
294     {
295       htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
296     }
297     /* Init the low level hardware : GPIO, CLOCK, NVIC */
298     htim->Base_MspInitCallback(htim);
299 #else
300     /* Init the low level hardware : GPIO, CLOCK, NVIC */
301     HAL_TIM_Base_MspInit(htim);
302 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
303   }
304 
305   /* Set the TIM state */
306   htim->State = HAL_TIM_STATE_BUSY;
307 
308   /* Set the Time Base configuration */
309   TIM_Base_SetConfig(htim->Instance, &htim->Init);
310 
311   /* Initialize the DMA burst operation state */
312   htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
313 
314   /* Initialize the TIM channels state */
315   TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
316   TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
317 
318   /* Initialize the TIM state*/
319   htim->State = HAL_TIM_STATE_READY;
320 
321   return HAL_OK;
322 }
323 
324 /**
325   * @brief  DeInitializes the TIM Base peripheral
326   * @param  htim TIM Base handle
327   * @retval HAL status
328   */
HAL_TIM_Base_DeInit(TIM_HandleTypeDef * htim)329 HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)
330 {
331   /* Check the parameters */
332   assert_param(IS_TIM_INSTANCE(htim->Instance));
333 
334   htim->State = HAL_TIM_STATE_BUSY;
335 
336   /* Disable the TIM Peripheral Clock */
337   __HAL_TIM_DISABLE(htim);
338 
339 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
340   if (htim->Base_MspDeInitCallback == NULL)
341   {
342     htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
343   }
344   /* DeInit the low level hardware */
345   htim->Base_MspDeInitCallback(htim);
346 #else
347   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
348   HAL_TIM_Base_MspDeInit(htim);
349 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
350 
351   /* Change the DMA burst operation state */
352   htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
353 
354   /* Change the TIM channels state */
355   TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
356   TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
357 
358   /* Change TIM state */
359   htim->State = HAL_TIM_STATE_RESET;
360 
361   /* Release Lock */
362   __HAL_UNLOCK(htim);
363 
364   return HAL_OK;
365 }
366 
367 /**
368   * @brief  Initializes the TIM Base MSP.
369   * @param  htim TIM Base handle
370   * @retval None
371   */
HAL_TIM_Base_MspInit(TIM_HandleTypeDef * htim)372 __weak void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
373 {
374   /* Prevent unused argument(s) compilation warning */
375   UNUSED(htim);
376 
377   /* NOTE : This function should not be modified, when the callback is needed,
378             the HAL_TIM_Base_MspInit could be implemented in the user file
379    */
380 }
381 
382 /**
383   * @brief  DeInitializes TIM Base MSP.
384   * @param  htim TIM Base handle
385   * @retval None
386   */
HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef * htim)387 __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
388 {
389   /* Prevent unused argument(s) compilation warning */
390   UNUSED(htim);
391 
392   /* NOTE : This function should not be modified, when the callback is needed,
393             the HAL_TIM_Base_MspDeInit could be implemented in the user file
394    */
395 }
396 
397 
398 /**
399   * @brief  Starts the TIM Base generation.
400   * @param  htim TIM Base handle
401   * @retval HAL status
402   */
HAL_TIM_Base_Start(TIM_HandleTypeDef * htim)403 HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)
404 {
405   uint32_t tmpsmcr;
406 
407   /* Check the parameters */
408   assert_param(IS_TIM_INSTANCE(htim->Instance));
409 
410   /* Check the TIM state */
411   if (htim->State != HAL_TIM_STATE_READY)
412   {
413     return HAL_ERROR;
414   }
415 
416   /* Set the TIM state */
417   htim->State = HAL_TIM_STATE_BUSY;
418 
419   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
420   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
421   {
422     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
423     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
424     {
425       __HAL_TIM_ENABLE(htim);
426     }
427   }
428   else
429   {
430     __HAL_TIM_ENABLE(htim);
431   }
432 
433   /* Return function status */
434   return HAL_OK;
435 }
436 
437 /**
438   * @brief  Stops the TIM Base generation.
439   * @param  htim TIM Base handle
440   * @retval HAL status
441   */
HAL_TIM_Base_Stop(TIM_HandleTypeDef * htim)442 HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)
443 {
444   /* Check the parameters */
445   assert_param(IS_TIM_INSTANCE(htim->Instance));
446 
447   /* Disable the Peripheral */
448   __HAL_TIM_DISABLE(htim);
449 
450   /* Set the TIM state */
451   htim->State = HAL_TIM_STATE_READY;
452 
453   /* Return function status */
454   return HAL_OK;
455 }
456 
457 /**
458   * @brief  Starts the TIM Base generation in interrupt mode.
459   * @param  htim TIM Base handle
460   * @retval HAL status
461   */
HAL_TIM_Base_Start_IT(TIM_HandleTypeDef * htim)462 HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
463 {
464   uint32_t tmpsmcr;
465 
466   /* Check the parameters */
467   assert_param(IS_TIM_INSTANCE(htim->Instance));
468 
469   /* Check the TIM state */
470   if (htim->State != HAL_TIM_STATE_READY)
471   {
472     return HAL_ERROR;
473   }
474 
475   /* Set the TIM state */
476   htim->State = HAL_TIM_STATE_BUSY;
477 
478   /* Enable the TIM Update interrupt */
479   __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
480 
481   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
482   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
483   {
484     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
485     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
486     {
487       __HAL_TIM_ENABLE(htim);
488     }
489   }
490   else
491   {
492     __HAL_TIM_ENABLE(htim);
493   }
494 
495   /* Return function status */
496   return HAL_OK;
497 }
498 
499 /**
500   * @brief  Stops the TIM Base generation in interrupt mode.
501   * @param  htim TIM Base handle
502   * @retval HAL status
503   */
HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef * htim)504 HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)
505 {
506   /* Check the parameters */
507   assert_param(IS_TIM_INSTANCE(htim->Instance));
508 
509   /* Disable the TIM Update interrupt */
510   __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);
511 
512   /* Disable the Peripheral */
513   __HAL_TIM_DISABLE(htim);
514 
515   /* Set the TIM state */
516   htim->State = HAL_TIM_STATE_READY;
517 
518   /* Return function status */
519   return HAL_OK;
520 }
521 
522 /**
523   * @brief  Starts the TIM Base generation in DMA mode.
524   * @param  htim TIM Base handle
525   * @param  pData The source Buffer address.
526   * @param  Length The length of data to be transferred from memory to peripheral.
527   * @retval HAL status
528   */
HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef * htim,const uint32_t * pData,uint16_t Length)529 HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, const uint32_t *pData, uint16_t Length)
530 {
531   uint32_t tmpsmcr;
532 
533   /* Check the parameters */
534   assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
535 
536   /* Set the TIM state */
537   if (htim->State == HAL_TIM_STATE_BUSY)
538   {
539     return HAL_BUSY;
540   }
541   else if (htim->State == HAL_TIM_STATE_READY)
542   {
543     if ((pData == NULL) || (Length == 0U))
544     {
545       return HAL_ERROR;
546     }
547     else
548     {
549       htim->State = HAL_TIM_STATE_BUSY;
550     }
551   }
552   else
553   {
554     return HAL_ERROR;
555   }
556 
557   /* Set the DMA Period elapsed callbacks */
558   htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
559   htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
560 
561   /* Set the DMA error callback */
562   htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
563 
564   /* Enable the DMA stream */
565   if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR,
566                        Length) != HAL_OK)
567   {
568     /* Return error status */
569     return HAL_ERROR;
570   }
571 
572   /* Enable the TIM Update DMA request */
573   __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);
574 
575   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
576   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
577   {
578     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
579     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
580     {
581       __HAL_TIM_ENABLE(htim);
582     }
583   }
584   else
585   {
586     __HAL_TIM_ENABLE(htim);
587   }
588 
589   /* Return function status */
590   return HAL_OK;
591 }
592 
593 /**
594   * @brief  Stops the TIM Base generation in DMA mode.
595   * @param  htim TIM Base handle
596   * @retval HAL status
597   */
HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef * htim)598 HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)
599 {
600   /* Check the parameters */
601   assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
602 
603   /* Disable the TIM Update DMA request */
604   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);
605 
606   (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
607 
608   /* Disable the Peripheral */
609   __HAL_TIM_DISABLE(htim);
610 
611   /* Set the TIM state */
612   htim->State = HAL_TIM_STATE_READY;
613 
614   /* Return function status */
615   return HAL_OK;
616 }
617 
618 /**
619   * @}
620   */
621 
622 /** @defgroup TIM_Exported_Functions_Group2 TIM Output Compare functions
623   *  @brief    TIM Output Compare functions
624   *
625 @verbatim
626   ==============================================================================
627                   ##### TIM Output Compare functions #####
628   ==============================================================================
629   [..]
630     This section provides functions allowing to:
631     (+) Initialize and configure the TIM Output Compare.
632     (+) De-initialize the TIM Output Compare.
633     (+) Start the TIM Output Compare.
634     (+) Stop the TIM Output Compare.
635     (+) Start the TIM Output Compare and enable interrupt.
636     (+) Stop the TIM Output Compare and disable interrupt.
637     (+) Start the TIM Output Compare and enable DMA transfer.
638     (+) Stop the TIM Output Compare and disable DMA transfer.
639 
640 @endverbatim
641   * @{
642   */
643 /**
644   * @brief  Initializes the TIM Output Compare according to the specified
645   *         parameters in the TIM_HandleTypeDef and initializes the associated handle.
646   * @note   Switching from Center Aligned counter mode to Edge counter mode (or reverse)
647   *         requires a timer reset to avoid unexpected direction
648   *         due to DIR bit readonly in center aligned mode.
649   *         Ex: call @ref HAL_TIM_OC_DeInit() before HAL_TIM_OC_Init()
650   * @param  htim TIM Output Compare handle
651   * @retval HAL status
652   */
HAL_TIM_OC_Init(TIM_HandleTypeDef * htim)653 HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim)
654 {
655   /* Check the TIM handle allocation */
656   if (htim == NULL)
657   {
658     return HAL_ERROR;
659   }
660 
661   /* Check the parameters */
662   assert_param(IS_TIM_INSTANCE(htim->Instance));
663   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
664   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
665   assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
666   assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
667 
668   if (htim->State == HAL_TIM_STATE_RESET)
669   {
670     /* Allocate lock resource and initialize it */
671     htim->Lock = HAL_UNLOCKED;
672 
673 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
674     /* Reset interrupt callbacks to legacy weak callbacks */
675     TIM_ResetCallback(htim);
676 
677     if (htim->OC_MspInitCallback == NULL)
678     {
679       htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
680     }
681     /* Init the low level hardware : GPIO, CLOCK, NVIC */
682     htim->OC_MspInitCallback(htim);
683 #else
684     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
685     HAL_TIM_OC_MspInit(htim);
686 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
687   }
688 
689   /* Set the TIM state */
690   htim->State = HAL_TIM_STATE_BUSY;
691 
692   /* Init the base time for the Output Compare */
693   TIM_Base_SetConfig(htim->Instance,  &htim->Init);
694 
695   /* Initialize the DMA burst operation state */
696   htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
697 
698   /* Initialize the TIM channels state */
699   TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
700   TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
701 
702   /* Initialize the TIM state*/
703   htim->State = HAL_TIM_STATE_READY;
704 
705   return HAL_OK;
706 }
707 
708 /**
709   * @brief  DeInitializes the TIM peripheral
710   * @param  htim TIM Output Compare handle
711   * @retval HAL status
712   */
HAL_TIM_OC_DeInit(TIM_HandleTypeDef * htim)713 HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)
714 {
715   /* Check the parameters */
716   assert_param(IS_TIM_INSTANCE(htim->Instance));
717 
718   htim->State = HAL_TIM_STATE_BUSY;
719 
720   /* Disable the TIM Peripheral Clock */
721   __HAL_TIM_DISABLE(htim);
722 
723 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
724   if (htim->OC_MspDeInitCallback == NULL)
725   {
726     htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
727   }
728   /* DeInit the low level hardware */
729   htim->OC_MspDeInitCallback(htim);
730 #else
731   /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
732   HAL_TIM_OC_MspDeInit(htim);
733 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
734 
735   /* Change the DMA burst operation state */
736   htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
737 
738   /* Change the TIM channels state */
739   TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
740   TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
741 
742   /* Change TIM state */
743   htim->State = HAL_TIM_STATE_RESET;
744 
745   /* Release Lock */
746   __HAL_UNLOCK(htim);
747 
748   return HAL_OK;
749 }
750 
751 /**
752   * @brief  Initializes the TIM Output Compare MSP.
753   * @param  htim TIM Output Compare handle
754   * @retval None
755   */
HAL_TIM_OC_MspInit(TIM_HandleTypeDef * htim)756 __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
757 {
758   /* Prevent unused argument(s) compilation warning */
759   UNUSED(htim);
760 
761   /* NOTE : This function should not be modified, when the callback is needed,
762             the HAL_TIM_OC_MspInit could be implemented in the user file
763    */
764 }
765 
766 /**
767   * @brief  DeInitializes TIM Output Compare MSP.
768   * @param  htim TIM Output Compare handle
769   * @retval None
770   */
HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef * htim)771 __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
772 {
773   /* Prevent unused argument(s) compilation warning */
774   UNUSED(htim);
775 
776   /* NOTE : This function should not be modified, when the callback is needed,
777             the HAL_TIM_OC_MspDeInit could be implemented in the user file
778    */
779 }
780 
781 /**
782   * @brief  Starts the TIM Output Compare signal generation.
783   * @param  htim TIM Output Compare handle
784   * @param  Channel TIM Channel to be enabled
785   *          This parameter can be one of the following values:
786   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
787   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
788   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
789   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
790   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected
791   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected
792   * @retval HAL status
793   */
HAL_TIM_OC_Start(TIM_HandleTypeDef * htim,uint32_t Channel)794 HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
795 {
796   uint32_t tmpsmcr;
797 
798   /* Check the parameters */
799   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
800 
801   /* Check the TIM channel state */
802   if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
803   {
804     return HAL_ERROR;
805   }
806 
807   /* Set the TIM channel state */
808   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
809 
810   /* Enable the Output compare channel */
811   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
812 
813   if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
814   {
815     /* Enable the main output */
816     __HAL_TIM_MOE_ENABLE(htim);
817   }
818 
819   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
820   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
821   {
822     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
823     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
824     {
825       __HAL_TIM_ENABLE(htim);
826     }
827   }
828   else
829   {
830     __HAL_TIM_ENABLE(htim);
831   }
832 
833   /* Return function status */
834   return HAL_OK;
835 }
836 
837 /**
838   * @brief  Stops the TIM Output Compare signal generation.
839   * @param  htim TIM Output Compare handle
840   * @param  Channel TIM Channel to be disabled
841   *          This parameter can be one of the following values:
842   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
843   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
844   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
845   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
846   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected
847   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected
848   * @retval HAL status
849   */
HAL_TIM_OC_Stop(TIM_HandleTypeDef * htim,uint32_t Channel)850 HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
851 {
852   /* Check the parameters */
853   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
854 
855   /* Disable the Output compare channel */
856   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
857 
858   if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
859   {
860     /* Disable the Main Output */
861     __HAL_TIM_MOE_DISABLE(htim);
862   }
863 
864   /* Disable the Peripheral */
865   __HAL_TIM_DISABLE(htim);
866 
867   /* Set the TIM channel state */
868   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
869 
870   /* Return function status */
871   return HAL_OK;
872 }
873 
874 /**
875   * @brief  Starts the TIM Output Compare signal generation in interrupt mode.
876   * @param  htim TIM Output Compare handle
877   * @param  Channel TIM Channel to be enabled
878   *          This parameter can be one of the following values:
879   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
880   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
881   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
882   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
883   * @retval HAL status
884   */
HAL_TIM_OC_Start_IT(TIM_HandleTypeDef * htim,uint32_t Channel)885 HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
886 {
887   HAL_StatusTypeDef status = HAL_OK;
888   uint32_t tmpsmcr;
889 
890   /* Check the parameters */
891   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
892 
893   /* Check the TIM channel state */
894   if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
895   {
896     return HAL_ERROR;
897   }
898 
899   /* Set the TIM channel state */
900   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
901 
902   switch (Channel)
903   {
904     case TIM_CHANNEL_1:
905     {
906       /* Enable the TIM Capture/Compare 1 interrupt */
907       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
908       break;
909     }
910 
911     case TIM_CHANNEL_2:
912     {
913       /* Enable the TIM Capture/Compare 2 interrupt */
914       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
915       break;
916     }
917 
918     case TIM_CHANNEL_3:
919     {
920       /* Enable the TIM Capture/Compare 3 interrupt */
921       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
922       break;
923     }
924 
925     case TIM_CHANNEL_4:
926     {
927       /* Enable the TIM Capture/Compare 4 interrupt */
928       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
929       break;
930     }
931 
932     default:
933       status = HAL_ERROR;
934       break;
935   }
936 
937   if (status == HAL_OK)
938   {
939     /* Enable the Output compare channel */
940     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
941 
942     if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
943     {
944       /* Enable the main output */
945       __HAL_TIM_MOE_ENABLE(htim);
946     }
947 
948     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
949     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
950     {
951       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
952       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
953       {
954         __HAL_TIM_ENABLE(htim);
955       }
956     }
957     else
958     {
959       __HAL_TIM_ENABLE(htim);
960     }
961   }
962 
963   /* Return function status */
964   return status;
965 }
966 
967 /**
968   * @brief  Stops the TIM Output Compare signal generation in interrupt mode.
969   * @param  htim TIM Output Compare handle
970   * @param  Channel TIM Channel to be disabled
971   *          This parameter can be one of the following values:
972   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
973   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
974   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
975   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
976   * @retval HAL status
977   */
HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef * htim,uint32_t Channel)978 HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
979 {
980   HAL_StatusTypeDef status = HAL_OK;
981 
982   /* Check the parameters */
983   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
984 
985   switch (Channel)
986   {
987     case TIM_CHANNEL_1:
988     {
989       /* Disable the TIM Capture/Compare 1 interrupt */
990       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
991       break;
992     }
993 
994     case TIM_CHANNEL_2:
995     {
996       /* Disable the TIM Capture/Compare 2 interrupt */
997       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
998       break;
999     }
1000 
1001     case TIM_CHANNEL_3:
1002     {
1003       /* Disable the TIM Capture/Compare 3 interrupt */
1004       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1005       break;
1006     }
1007 
1008     case TIM_CHANNEL_4:
1009     {
1010       /* Disable the TIM Capture/Compare 4 interrupt */
1011       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
1012       break;
1013     }
1014 
1015     default:
1016       status = HAL_ERROR;
1017       break;
1018   }
1019 
1020   if (status == HAL_OK)
1021   {
1022     /* Disable the Output compare channel */
1023     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1024 
1025     if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1026     {
1027       /* Disable the Main Output */
1028       __HAL_TIM_MOE_DISABLE(htim);
1029     }
1030 
1031     /* Disable the Peripheral */
1032     __HAL_TIM_DISABLE(htim);
1033 
1034     /* Set the TIM channel state */
1035     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1036   }
1037 
1038   /* Return function status */
1039   return status;
1040 }
1041 
1042 /**
1043   * @brief  Starts the TIM Output Compare signal generation in DMA mode.
1044   * @param  htim TIM Output Compare handle
1045   * @param  Channel TIM Channel to be enabled
1046   *          This parameter can be one of the following values:
1047   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1048   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1049   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1050   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
1051   * @param  pData The source Buffer address.
1052   * @param  Length The length of data to be transferred from memory to TIM peripheral
1053   * @retval HAL status
1054   */
HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef * htim,uint32_t Channel,const uint32_t * pData,uint16_t Length)1055 HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
1056                                        uint16_t Length)
1057 {
1058   HAL_StatusTypeDef status = HAL_OK;
1059   uint32_t tmpsmcr;
1060 
1061   /* Check the parameters */
1062   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
1063 
1064   /* Set the TIM channel state */
1065   if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
1066   {
1067     return HAL_BUSY;
1068   }
1069   else if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
1070   {
1071     if ((pData == NULL) || (Length == 0U))
1072     {
1073       return HAL_ERROR;
1074     }
1075     else
1076     {
1077       TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1078     }
1079   }
1080   else
1081   {
1082     return HAL_ERROR;
1083   }
1084 
1085   switch (Channel)
1086   {
1087     case TIM_CHANNEL_1:
1088     {
1089       /* Set the DMA compare callbacks */
1090       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
1091       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1092 
1093       /* Set the DMA error callback */
1094       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
1095 
1096       /* Enable the DMA stream */
1097       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
1098                            Length) != HAL_OK)
1099       {
1100         /* Return error status */
1101         return HAL_ERROR;
1102       }
1103 
1104       /* Enable the TIM Capture/Compare 1 DMA request */
1105       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1106       break;
1107     }
1108 
1109     case TIM_CHANNEL_2:
1110     {
1111       /* Set the DMA compare callbacks */
1112       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
1113       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1114 
1115       /* Set the DMA error callback */
1116       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
1117 
1118       /* Enable the DMA stream */
1119       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
1120                            Length) != HAL_OK)
1121       {
1122         /* Return error status */
1123         return HAL_ERROR;
1124       }
1125 
1126       /* Enable the TIM Capture/Compare 2 DMA request */
1127       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1128       break;
1129     }
1130 
1131     case TIM_CHANNEL_3:
1132     {
1133       /* Set the DMA compare callbacks */
1134       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
1135       htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1136 
1137       /* Set the DMA error callback */
1138       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
1139 
1140       /* Enable the DMA stream */
1141       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
1142                            Length) != HAL_OK)
1143       {
1144         /* Return error status */
1145         return HAL_ERROR;
1146       }
1147       /* Enable the TIM Capture/Compare 3 DMA request */
1148       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1149       break;
1150     }
1151 
1152     case TIM_CHANNEL_4:
1153     {
1154       /* Set the DMA compare callbacks */
1155       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
1156       htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1157 
1158       /* Set the DMA error callback */
1159       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
1160 
1161       /* Enable the DMA stream */
1162       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4,
1163                            Length) != HAL_OK)
1164       {
1165         /* Return error status */
1166         return HAL_ERROR;
1167       }
1168       /* Enable the TIM Capture/Compare 4 DMA request */
1169       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1170       break;
1171     }
1172 
1173     default:
1174       status = HAL_ERROR;
1175       break;
1176   }
1177 
1178   if (status == HAL_OK)
1179   {
1180     /* Enable the Output compare channel */
1181     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1182 
1183     if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1184     {
1185       /* Enable the main output */
1186       __HAL_TIM_MOE_ENABLE(htim);
1187     }
1188 
1189     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1190     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1191     {
1192       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1193       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1194       {
1195         __HAL_TIM_ENABLE(htim);
1196       }
1197     }
1198     else
1199     {
1200       __HAL_TIM_ENABLE(htim);
1201     }
1202   }
1203 
1204   /* Return function status */
1205   return status;
1206 }
1207 
1208 /**
1209   * @brief  Stops the TIM Output Compare signal generation in DMA mode.
1210   * @param  htim TIM Output Compare handle
1211   * @param  Channel TIM Channel to be disabled
1212   *          This parameter can be one of the following values:
1213   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1214   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1215   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1216   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
1217   * @retval HAL status
1218   */
HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef * htim,uint32_t Channel)1219 HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1220 {
1221   HAL_StatusTypeDef status = HAL_OK;
1222 
1223   /* Check the parameters */
1224   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
1225 
1226   switch (Channel)
1227   {
1228     case TIM_CHANNEL_1:
1229     {
1230       /* Disable the TIM Capture/Compare 1 DMA request */
1231       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1232       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1233       break;
1234     }
1235 
1236     case TIM_CHANNEL_2:
1237     {
1238       /* Disable the TIM Capture/Compare 2 DMA request */
1239       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1240       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1241       break;
1242     }
1243 
1244     case TIM_CHANNEL_3:
1245     {
1246       /* Disable the TIM Capture/Compare 3 DMA request */
1247       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1248       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1249       break;
1250     }
1251 
1252     case TIM_CHANNEL_4:
1253     {
1254       /* Disable the TIM Capture/Compare 4 interrupt */
1255       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1256       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
1257       break;
1258     }
1259 
1260     default:
1261       status = HAL_ERROR;
1262       break;
1263   }
1264 
1265   if (status == HAL_OK)
1266   {
1267     /* Disable the Output compare channel */
1268     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1269 
1270     if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1271     {
1272       /* Disable the Main Output */
1273       __HAL_TIM_MOE_DISABLE(htim);
1274     }
1275 
1276     /* Disable the Peripheral */
1277     __HAL_TIM_DISABLE(htim);
1278 
1279     /* Set the TIM channel state */
1280     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1281   }
1282 
1283   /* Return function status */
1284   return status;
1285 }
1286 
1287 /**
1288   * @}
1289   */
1290 
1291 /** @defgroup TIM_Exported_Functions_Group3 TIM PWM functions
1292   *  @brief    TIM PWM functions
1293   *
1294 @verbatim
1295   ==============================================================================
1296                           ##### TIM PWM functions #####
1297   ==============================================================================
1298   [..]
1299     This section provides functions allowing to:
1300     (+) Initialize and configure the TIM PWM.
1301     (+) De-initialize the TIM PWM.
1302     (+) Start the TIM PWM.
1303     (+) Stop the TIM PWM.
1304     (+) Start the TIM PWM and enable interrupt.
1305     (+) Stop the TIM PWM and disable interrupt.
1306     (+) Start the TIM PWM and enable DMA transfer.
1307     (+) Stop the TIM PWM and disable DMA transfer.
1308 
1309 @endverbatim
1310   * @{
1311   */
1312 /**
1313   * @brief  Initializes the TIM PWM Time Base according to the specified
1314   *         parameters in the TIM_HandleTypeDef and initializes the associated handle.
1315   * @note   Switching from Center Aligned counter mode to Edge counter mode (or reverse)
1316   *         requires a timer reset to avoid unexpected direction
1317   *         due to DIR bit readonly in center aligned mode.
1318   *         Ex: call @ref HAL_TIM_PWM_DeInit() before HAL_TIM_PWM_Init()
1319   * @param  htim TIM PWM handle
1320   * @retval HAL status
1321   */
HAL_TIM_PWM_Init(TIM_HandleTypeDef * htim)1322 HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)
1323 {
1324   /* Check the TIM handle allocation */
1325   if (htim == NULL)
1326   {
1327     return HAL_ERROR;
1328   }
1329 
1330   /* Check the parameters */
1331   assert_param(IS_TIM_INSTANCE(htim->Instance));
1332   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
1333   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
1334   assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
1335   assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
1336 
1337   if (htim->State == HAL_TIM_STATE_RESET)
1338   {
1339     /* Allocate lock resource and initialize it */
1340     htim->Lock = HAL_UNLOCKED;
1341 
1342 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1343     /* Reset interrupt callbacks to legacy weak callbacks */
1344     TIM_ResetCallback(htim);
1345 
1346     if (htim->PWM_MspInitCallback == NULL)
1347     {
1348       htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
1349     }
1350     /* Init the low level hardware : GPIO, CLOCK, NVIC */
1351     htim->PWM_MspInitCallback(htim);
1352 #else
1353     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1354     HAL_TIM_PWM_MspInit(htim);
1355 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1356   }
1357 
1358   /* Set the TIM state */
1359   htim->State = HAL_TIM_STATE_BUSY;
1360 
1361   /* Init the base time for the PWM */
1362   TIM_Base_SetConfig(htim->Instance, &htim->Init);
1363 
1364   /* Initialize the DMA burst operation state */
1365   htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
1366 
1367   /* Initialize the TIM channels state */
1368   TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
1369   TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
1370 
1371   /* Initialize the TIM state*/
1372   htim->State = HAL_TIM_STATE_READY;
1373 
1374   return HAL_OK;
1375 }
1376 
1377 /**
1378   * @brief  DeInitializes the TIM peripheral
1379   * @param  htim TIM PWM handle
1380   * @retval HAL status
1381   */
HAL_TIM_PWM_DeInit(TIM_HandleTypeDef * htim)1382 HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)
1383 {
1384   /* Check the parameters */
1385   assert_param(IS_TIM_INSTANCE(htim->Instance));
1386 
1387   htim->State = HAL_TIM_STATE_BUSY;
1388 
1389   /* Disable the TIM Peripheral Clock */
1390   __HAL_TIM_DISABLE(htim);
1391 
1392 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1393   if (htim->PWM_MspDeInitCallback == NULL)
1394   {
1395     htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
1396   }
1397   /* DeInit the low level hardware */
1398   htim->PWM_MspDeInitCallback(htim);
1399 #else
1400   /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1401   HAL_TIM_PWM_MspDeInit(htim);
1402 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1403 
1404   /* Change the DMA burst operation state */
1405   htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
1406 
1407   /* Change the TIM channels state */
1408   TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
1409   TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
1410 
1411   /* Change TIM state */
1412   htim->State = HAL_TIM_STATE_RESET;
1413 
1414   /* Release Lock */
1415   __HAL_UNLOCK(htim);
1416 
1417   return HAL_OK;
1418 }
1419 
1420 /**
1421   * @brief  Initializes the TIM PWM MSP.
1422   * @param  htim TIM PWM handle
1423   * @retval None
1424   */
HAL_TIM_PWM_MspInit(TIM_HandleTypeDef * htim)1425 __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
1426 {
1427   /* Prevent unused argument(s) compilation warning */
1428   UNUSED(htim);
1429 
1430   /* NOTE : This function should not be modified, when the callback is needed,
1431             the HAL_TIM_PWM_MspInit could be implemented in the user file
1432    */
1433 }
1434 
1435 /**
1436   * @brief  DeInitializes TIM PWM MSP.
1437   * @param  htim TIM PWM handle
1438   * @retval None
1439   */
HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef * htim)1440 __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)
1441 {
1442   /* Prevent unused argument(s) compilation warning */
1443   UNUSED(htim);
1444 
1445   /* NOTE : This function should not be modified, when the callback is needed,
1446             the HAL_TIM_PWM_MspDeInit could be implemented in the user file
1447    */
1448 }
1449 
1450 /**
1451   * @brief  Starts the PWM signal generation.
1452   * @param  htim TIM handle
1453   * @param  Channel TIM Channels to be enabled
1454   *          This parameter can be one of the following values:
1455   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1456   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1457   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1458   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
1459   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected
1460   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected
1461   * @retval HAL status
1462   */
HAL_TIM_PWM_Start(TIM_HandleTypeDef * htim,uint32_t Channel)1463 HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
1464 {
1465   uint32_t tmpsmcr;
1466 
1467   /* Check the parameters */
1468   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1469 
1470   /* Check the TIM channel state */
1471   if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
1472   {
1473     return HAL_ERROR;
1474   }
1475 
1476   /* Set the TIM channel state */
1477   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1478 
1479   /* Enable the Capture compare channel */
1480   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1481 
1482   if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1483   {
1484     /* Enable the main output */
1485     __HAL_TIM_MOE_ENABLE(htim);
1486   }
1487 
1488   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1489   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1490   {
1491     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1492     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1493     {
1494       __HAL_TIM_ENABLE(htim);
1495     }
1496   }
1497   else
1498   {
1499     __HAL_TIM_ENABLE(htim);
1500   }
1501 
1502   /* Return function status */
1503   return HAL_OK;
1504 }
1505 
1506 /**
1507   * @brief  Stops the PWM signal generation.
1508   * @param  htim TIM PWM handle
1509   * @param  Channel TIM Channels to be disabled
1510   *          This parameter can be one of the following values:
1511   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1512   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1513   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1514   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
1515   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected
1516   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected
1517   * @retval HAL status
1518   */
HAL_TIM_PWM_Stop(TIM_HandleTypeDef * htim,uint32_t Channel)1519 HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
1520 {
1521   /* Check the parameters */
1522   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1523 
1524   /* Disable the Capture compare channel */
1525   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1526 
1527   if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1528   {
1529     /* Disable the Main Output */
1530     __HAL_TIM_MOE_DISABLE(htim);
1531   }
1532 
1533   /* Disable the Peripheral */
1534   __HAL_TIM_DISABLE(htim);
1535 
1536   /* Set the TIM channel state */
1537   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1538 
1539   /* Return function status */
1540   return HAL_OK;
1541 }
1542 
1543 /**
1544   * @brief  Starts the PWM signal generation in interrupt mode.
1545   * @param  htim TIM PWM handle
1546   * @param  Channel TIM Channel to be enabled
1547   *          This parameter can be one of the following values:
1548   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1549   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1550   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1551   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
1552   * @retval HAL status
1553   */
HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef * htim,uint32_t Channel)1554 HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1555 {
1556   HAL_StatusTypeDef status = HAL_OK;
1557   uint32_t tmpsmcr;
1558 
1559   /* Check the parameters */
1560   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
1561 
1562   /* Check the TIM channel state */
1563   if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
1564   {
1565     return HAL_ERROR;
1566   }
1567 
1568   /* Set the TIM channel state */
1569   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1570 
1571   switch (Channel)
1572   {
1573     case TIM_CHANNEL_1:
1574     {
1575       /* Enable the TIM Capture/Compare 1 interrupt */
1576       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1577       break;
1578     }
1579 
1580     case TIM_CHANNEL_2:
1581     {
1582       /* Enable the TIM Capture/Compare 2 interrupt */
1583       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1584       break;
1585     }
1586 
1587     case TIM_CHANNEL_3:
1588     {
1589       /* Enable the TIM Capture/Compare 3 interrupt */
1590       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1591       break;
1592     }
1593 
1594     case TIM_CHANNEL_4:
1595     {
1596       /* Enable the TIM Capture/Compare 4 interrupt */
1597       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
1598       break;
1599     }
1600 
1601     default:
1602       status = HAL_ERROR;
1603       break;
1604   }
1605 
1606   if (status == HAL_OK)
1607   {
1608     /* Enable the Capture compare channel */
1609     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1610 
1611     if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1612     {
1613       /* Enable the main output */
1614       __HAL_TIM_MOE_ENABLE(htim);
1615     }
1616 
1617     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1618     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1619     {
1620       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1621       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1622       {
1623         __HAL_TIM_ENABLE(htim);
1624       }
1625     }
1626     else
1627     {
1628       __HAL_TIM_ENABLE(htim);
1629     }
1630   }
1631 
1632   /* Return function status */
1633   return status;
1634 }
1635 
1636 /**
1637   * @brief  Stops the PWM signal generation in interrupt mode.
1638   * @param  htim TIM PWM handle
1639   * @param  Channel TIM Channels to be disabled
1640   *          This parameter can be one of the following values:
1641   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1642   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1643   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1644   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
1645   * @retval HAL status
1646   */
HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef * htim,uint32_t Channel)1647 HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1648 {
1649   HAL_StatusTypeDef status = HAL_OK;
1650 
1651   /* Check the parameters */
1652   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
1653 
1654   switch (Channel)
1655   {
1656     case TIM_CHANNEL_1:
1657     {
1658       /* Disable the TIM Capture/Compare 1 interrupt */
1659       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1660       break;
1661     }
1662 
1663     case TIM_CHANNEL_2:
1664     {
1665       /* Disable the TIM Capture/Compare 2 interrupt */
1666       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1667       break;
1668     }
1669 
1670     case TIM_CHANNEL_3:
1671     {
1672       /* Disable the TIM Capture/Compare 3 interrupt */
1673       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1674       break;
1675     }
1676 
1677     case TIM_CHANNEL_4:
1678     {
1679       /* Disable the TIM Capture/Compare 4 interrupt */
1680       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
1681       break;
1682     }
1683 
1684     default:
1685       status = HAL_ERROR;
1686       break;
1687   }
1688 
1689   if (status == HAL_OK)
1690   {
1691     /* Disable the Capture compare channel */
1692     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1693 
1694     if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1695     {
1696       /* Disable the Main Output */
1697       __HAL_TIM_MOE_DISABLE(htim);
1698     }
1699 
1700     /* Disable the Peripheral */
1701     __HAL_TIM_DISABLE(htim);
1702 
1703     /* Set the TIM channel state */
1704     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1705   }
1706 
1707   /* Return function status */
1708   return status;
1709 }
1710 
1711 /**
1712   * @brief  Starts the TIM PWM signal generation in DMA mode.
1713   * @param  htim TIM PWM handle
1714   * @param  Channel TIM Channels to be enabled
1715   *          This parameter can be one of the following values:
1716   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1717   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1718   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1719   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
1720   * @param  pData The source Buffer address.
1721   * @param  Length The length of data to be transferred from memory to TIM peripheral
1722   * @retval HAL status
1723   */
HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef * htim,uint32_t Channel,const uint32_t * pData,uint16_t Length)1724 HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
1725                                         uint16_t Length)
1726 {
1727   HAL_StatusTypeDef status = HAL_OK;
1728   uint32_t tmpsmcr;
1729 
1730   /* Check the parameters */
1731   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
1732 
1733   /* Set the TIM channel state */
1734   if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
1735   {
1736     return HAL_BUSY;
1737   }
1738   else if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
1739   {
1740     if ((pData == NULL) || (Length == 0U))
1741     {
1742       return HAL_ERROR;
1743     }
1744     else
1745     {
1746       TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1747     }
1748   }
1749   else
1750   {
1751     return HAL_ERROR;
1752   }
1753 
1754   switch (Channel)
1755   {
1756     case TIM_CHANNEL_1:
1757     {
1758       /* Set the DMA compare callbacks */
1759       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
1760       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1761 
1762       /* Set the DMA error callback */
1763       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
1764 
1765       /* Enable the DMA stream */
1766       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
1767                            Length) != HAL_OK)
1768       {
1769         /* Return error status */
1770         return HAL_ERROR;
1771       }
1772 
1773       /* Enable the TIM Capture/Compare 1 DMA request */
1774       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1775       break;
1776     }
1777 
1778     case TIM_CHANNEL_2:
1779     {
1780       /* Set the DMA compare callbacks */
1781       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
1782       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1783 
1784       /* Set the DMA error callback */
1785       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
1786 
1787       /* Enable the DMA stream */
1788       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
1789                            Length) != HAL_OK)
1790       {
1791         /* Return error status */
1792         return HAL_ERROR;
1793       }
1794       /* Enable the TIM Capture/Compare 2 DMA request */
1795       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1796       break;
1797     }
1798 
1799     case TIM_CHANNEL_3:
1800     {
1801       /* Set the DMA compare callbacks */
1802       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
1803       htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1804 
1805       /* Set the DMA error callback */
1806       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
1807 
1808       /* Enable the DMA stream */
1809       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
1810                            Length) != HAL_OK)
1811       {
1812         /* Return error status */
1813         return HAL_ERROR;
1814       }
1815       /* Enable the TIM Output Capture/Compare 3 request */
1816       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1817       break;
1818     }
1819 
1820     case TIM_CHANNEL_4:
1821     {
1822       /* Set the DMA compare callbacks */
1823       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
1824       htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1825 
1826       /* Set the DMA error callback */
1827       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
1828 
1829       /* Enable the DMA stream */
1830       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4,
1831                            Length) != HAL_OK)
1832       {
1833         /* Return error status */
1834         return HAL_ERROR;
1835       }
1836       /* Enable the TIM Capture/Compare 4 DMA request */
1837       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1838       break;
1839     }
1840 
1841     default:
1842       status = HAL_ERROR;
1843       break;
1844   }
1845 
1846   if (status == HAL_OK)
1847   {
1848     /* Enable the Capture compare channel */
1849     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1850 
1851     if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1852     {
1853       /* Enable the main output */
1854       __HAL_TIM_MOE_ENABLE(htim);
1855     }
1856 
1857     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1858     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1859     {
1860       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1861       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1862       {
1863         __HAL_TIM_ENABLE(htim);
1864       }
1865     }
1866     else
1867     {
1868       __HAL_TIM_ENABLE(htim);
1869     }
1870   }
1871 
1872   /* Return function status */
1873   return status;
1874 }
1875 
1876 /**
1877   * @brief  Stops the TIM PWM signal generation in DMA mode.
1878   * @param  htim TIM PWM handle
1879   * @param  Channel TIM Channels to be disabled
1880   *          This parameter can be one of the following values:
1881   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1882   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1883   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1884   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
1885   * @retval HAL status
1886   */
HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef * htim,uint32_t Channel)1887 HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1888 {
1889   HAL_StatusTypeDef status = HAL_OK;
1890 
1891   /* Check the parameters */
1892   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
1893 
1894   switch (Channel)
1895   {
1896     case TIM_CHANNEL_1:
1897     {
1898       /* Disable the TIM Capture/Compare 1 DMA request */
1899       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1900       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1901       break;
1902     }
1903 
1904     case TIM_CHANNEL_2:
1905     {
1906       /* Disable the TIM Capture/Compare 2 DMA request */
1907       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1908       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1909       break;
1910     }
1911 
1912     case TIM_CHANNEL_3:
1913     {
1914       /* Disable the TIM Capture/Compare 3 DMA request */
1915       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1916       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1917       break;
1918     }
1919 
1920     case TIM_CHANNEL_4:
1921     {
1922       /* Disable the TIM Capture/Compare 4 interrupt */
1923       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1924       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
1925       break;
1926     }
1927 
1928     default:
1929       status = HAL_ERROR;
1930       break;
1931   }
1932 
1933   if (status == HAL_OK)
1934   {
1935     /* Disable the Capture compare channel */
1936     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1937 
1938     if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1939     {
1940       /* Disable the Main Output */
1941       __HAL_TIM_MOE_DISABLE(htim);
1942     }
1943 
1944     /* Disable the Peripheral */
1945     __HAL_TIM_DISABLE(htim);
1946 
1947     /* Set the TIM channel state */
1948     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1949   }
1950 
1951   /* Return function status */
1952   return status;
1953 }
1954 
1955 /**
1956   * @}
1957   */
1958 
1959 /** @defgroup TIM_Exported_Functions_Group4 TIM Input Capture functions
1960   *  @brief    TIM Input Capture functions
1961   *
1962 @verbatim
1963   ==============================================================================
1964               ##### TIM Input Capture functions #####
1965   ==============================================================================
1966  [..]
1967    This section provides functions allowing to:
1968    (+) Initialize and configure the TIM Input Capture.
1969    (+) De-initialize the TIM Input Capture.
1970    (+) Start the TIM Input Capture.
1971    (+) Stop the TIM Input Capture.
1972    (+) Start the TIM Input Capture and enable interrupt.
1973    (+) Stop the TIM Input Capture and disable interrupt.
1974    (+) Start the TIM Input Capture and enable DMA transfer.
1975    (+) Stop the TIM Input Capture and disable DMA transfer.
1976 
1977 @endverbatim
1978   * @{
1979   */
1980 /**
1981   * @brief  Initializes the TIM Input Capture Time base according to the specified
1982   *         parameters in the TIM_HandleTypeDef and initializes the associated handle.
1983   * @note   Switching from Center Aligned counter mode to Edge counter mode (or reverse)
1984   *         requires a timer reset to avoid unexpected direction
1985   *         due to DIR bit readonly in center aligned mode.
1986   *         Ex: call @ref HAL_TIM_IC_DeInit() before HAL_TIM_IC_Init()
1987   * @param  htim TIM Input Capture handle
1988   * @retval HAL status
1989   */
HAL_TIM_IC_Init(TIM_HandleTypeDef * htim)1990 HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)
1991 {
1992   /* Check the TIM handle allocation */
1993   if (htim == NULL)
1994   {
1995     return HAL_ERROR;
1996   }
1997 
1998   /* Check the parameters */
1999   assert_param(IS_TIM_INSTANCE(htim->Instance));
2000   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
2001   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
2002   assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
2003   assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
2004 
2005   if (htim->State == HAL_TIM_STATE_RESET)
2006   {
2007     /* Allocate lock resource and initialize it */
2008     htim->Lock = HAL_UNLOCKED;
2009 
2010 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2011     /* Reset interrupt callbacks to legacy weak callbacks */
2012     TIM_ResetCallback(htim);
2013 
2014     if (htim->IC_MspInitCallback == NULL)
2015     {
2016       htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
2017     }
2018     /* Init the low level hardware : GPIO, CLOCK, NVIC */
2019     htim->IC_MspInitCallback(htim);
2020 #else
2021     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
2022     HAL_TIM_IC_MspInit(htim);
2023 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2024   }
2025 
2026   /* Set the TIM state */
2027   htim->State = HAL_TIM_STATE_BUSY;
2028 
2029   /* Init the base time for the input capture */
2030   TIM_Base_SetConfig(htim->Instance, &htim->Init);
2031 
2032   /* Initialize the DMA burst operation state */
2033   htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
2034 
2035   /* Initialize the TIM channels state */
2036   TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
2037   TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
2038 
2039   /* Initialize the TIM state*/
2040   htim->State = HAL_TIM_STATE_READY;
2041 
2042   return HAL_OK;
2043 }
2044 
2045 /**
2046   * @brief  DeInitializes the TIM peripheral
2047   * @param  htim TIM Input Capture handle
2048   * @retval HAL status
2049   */
HAL_TIM_IC_DeInit(TIM_HandleTypeDef * htim)2050 HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)
2051 {
2052   /* Check the parameters */
2053   assert_param(IS_TIM_INSTANCE(htim->Instance));
2054 
2055   htim->State = HAL_TIM_STATE_BUSY;
2056 
2057   /* Disable the TIM Peripheral Clock */
2058   __HAL_TIM_DISABLE(htim);
2059 
2060 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2061   if (htim->IC_MspDeInitCallback == NULL)
2062   {
2063     htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
2064   }
2065   /* DeInit the low level hardware */
2066   htim->IC_MspDeInitCallback(htim);
2067 #else
2068   /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
2069   HAL_TIM_IC_MspDeInit(htim);
2070 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2071 
2072   /* Change the DMA burst operation state */
2073   htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
2074 
2075   /* Change the TIM channels state */
2076   TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
2077   TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
2078 
2079   /* Change TIM state */
2080   htim->State = HAL_TIM_STATE_RESET;
2081 
2082   /* Release Lock */
2083   __HAL_UNLOCK(htim);
2084 
2085   return HAL_OK;
2086 }
2087 
2088 /**
2089   * @brief  Initializes the TIM Input Capture MSP.
2090   * @param  htim TIM Input Capture handle
2091   * @retval None
2092   */
HAL_TIM_IC_MspInit(TIM_HandleTypeDef * htim)2093 __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
2094 {
2095   /* Prevent unused argument(s) compilation warning */
2096   UNUSED(htim);
2097 
2098   /* NOTE : This function should not be modified, when the callback is needed,
2099             the HAL_TIM_IC_MspInit could be implemented in the user file
2100    */
2101 }
2102 
2103 /**
2104   * @brief  DeInitializes TIM Input Capture MSP.
2105   * @param  htim TIM handle
2106   * @retval None
2107   */
HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef * htim)2108 __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)
2109 {
2110   /* Prevent unused argument(s) compilation warning */
2111   UNUSED(htim);
2112 
2113   /* NOTE : This function should not be modified, when the callback is needed,
2114             the HAL_TIM_IC_MspDeInit could be implemented in the user file
2115    */
2116 }
2117 
2118 /**
2119   * @brief  Starts the TIM Input Capture measurement.
2120   * @param  htim TIM Input Capture handle
2121   * @param  Channel TIM Channels to be enabled
2122   *          This parameter can be one of the following values:
2123   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
2124   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
2125   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
2126   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
2127   * @retval HAL status
2128   */
HAL_TIM_IC_Start(TIM_HandleTypeDef * htim,uint32_t Channel)2129 HAL_StatusTypeDef HAL_TIM_IC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
2130 {
2131   uint32_t tmpsmcr;
2132   HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
2133   HAL_TIM_ChannelStateTypeDef complementary_channel_state = TIM_CHANNEL_N_STATE_GET(htim, Channel);
2134 
2135   /* Check the parameters */
2136   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
2137 
2138   /* Check the TIM channel state */
2139   if ((channel_state != HAL_TIM_CHANNEL_STATE_READY)
2140       || (complementary_channel_state != HAL_TIM_CHANNEL_STATE_READY))
2141   {
2142     return HAL_ERROR;
2143   }
2144 
2145   /* Set the TIM channel state */
2146   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2147   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2148 
2149   /* Enable the Input Capture channel */
2150   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
2151 
2152   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
2153   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
2154   {
2155     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
2156     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
2157     {
2158       __HAL_TIM_ENABLE(htim);
2159     }
2160   }
2161   else
2162   {
2163     __HAL_TIM_ENABLE(htim);
2164   }
2165 
2166   /* Return function status */
2167   return HAL_OK;
2168 }
2169 
2170 /**
2171   * @brief  Stops the TIM Input Capture measurement.
2172   * @param  htim TIM Input Capture handle
2173   * @param  Channel TIM Channels to be disabled
2174   *          This parameter can be one of the following values:
2175   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
2176   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
2177   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
2178   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
2179   * @retval HAL status
2180   */
HAL_TIM_IC_Stop(TIM_HandleTypeDef * htim,uint32_t Channel)2181 HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
2182 {
2183   /* Check the parameters */
2184   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
2185 
2186   /* Disable the Input Capture channel */
2187   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
2188 
2189   /* Disable the Peripheral */
2190   __HAL_TIM_DISABLE(htim);
2191 
2192   /* Set the TIM channel state */
2193   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2194   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2195 
2196   /* Return function status */
2197   return HAL_OK;
2198 }
2199 
2200 /**
2201   * @brief  Starts the TIM Input Capture measurement in interrupt mode.
2202   * @param  htim TIM Input Capture handle
2203   * @param  Channel TIM Channels to be enabled
2204   *          This parameter can be one of the following values:
2205   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
2206   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
2207   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
2208   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
2209   * @retval HAL status
2210   */
HAL_TIM_IC_Start_IT(TIM_HandleTypeDef * htim,uint32_t Channel)2211 HAL_StatusTypeDef HAL_TIM_IC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2212 {
2213   HAL_StatusTypeDef status = HAL_OK;
2214   uint32_t tmpsmcr;
2215 
2216   HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
2217   HAL_TIM_ChannelStateTypeDef complementary_channel_state = TIM_CHANNEL_N_STATE_GET(htim, Channel);
2218 
2219   /* Check the parameters */
2220   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
2221 
2222   /* Check the TIM channel state */
2223   if ((channel_state != HAL_TIM_CHANNEL_STATE_READY)
2224       || (complementary_channel_state != HAL_TIM_CHANNEL_STATE_READY))
2225   {
2226     return HAL_ERROR;
2227   }
2228 
2229   /* Set the TIM channel state */
2230   TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2231   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2232 
2233   switch (Channel)
2234   {
2235     case TIM_CHANNEL_1:
2236     {
2237       /* Enable the TIM Capture/Compare 1 interrupt */
2238       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2239       break;
2240     }
2241 
2242     case TIM_CHANNEL_2:
2243     {
2244       /* Enable the TIM Capture/Compare 2 interrupt */
2245       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2246       break;
2247     }
2248 
2249     case TIM_CHANNEL_3:
2250     {
2251       /* Enable the TIM Capture/Compare 3 interrupt */
2252       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
2253       break;
2254     }
2255 
2256     case TIM_CHANNEL_4:
2257     {
2258       /* Enable the TIM Capture/Compare 4 interrupt */
2259       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
2260       break;
2261     }
2262 
2263     default:
2264       status = HAL_ERROR;
2265       break;
2266   }
2267 
2268   if (status == HAL_OK)
2269   {
2270     /* Enable the Input Capture channel */
2271     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
2272 
2273     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
2274     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
2275     {
2276       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
2277       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
2278       {
2279         __HAL_TIM_ENABLE(htim);
2280       }
2281     }
2282     else
2283     {
2284       __HAL_TIM_ENABLE(htim);
2285     }
2286   }
2287 
2288   /* Return function status */
2289   return status;
2290 }
2291 
2292 /**
2293   * @brief  Stops the TIM Input Capture measurement in interrupt mode.
2294   * @param  htim TIM Input Capture handle
2295   * @param  Channel TIM Channels to be disabled
2296   *          This parameter can be one of the following values:
2297   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
2298   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
2299   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
2300   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
2301   * @retval HAL status
2302   */
HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef * htim,uint32_t Channel)2303 HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2304 {
2305   HAL_StatusTypeDef status = HAL_OK;
2306 
2307   /* Check the parameters */
2308   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
2309 
2310   switch (Channel)
2311   {
2312     case TIM_CHANNEL_1:
2313     {
2314       /* Disable the TIM Capture/Compare 1 interrupt */
2315       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2316       break;
2317     }
2318 
2319     case TIM_CHANNEL_2:
2320     {
2321       /* Disable the TIM Capture/Compare 2 interrupt */
2322       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2323       break;
2324     }
2325 
2326     case TIM_CHANNEL_3:
2327     {
2328       /* Disable the TIM Capture/Compare 3 interrupt */
2329       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
2330       break;
2331     }
2332 
2333     case TIM_CHANNEL_4:
2334     {
2335       /* Disable the TIM Capture/Compare 4 interrupt */
2336       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
2337       break;
2338     }
2339 
2340     default:
2341       status = HAL_ERROR;
2342       break;
2343   }
2344 
2345   if (status == HAL_OK)
2346   {
2347     /* Disable the Input Capture channel */
2348     TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
2349 
2350     /* Disable the Peripheral */
2351     __HAL_TIM_DISABLE(htim);
2352 
2353     /* Set the TIM channel state */
2354     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2355     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2356   }
2357 
2358   /* Return function status */
2359   return status;
2360 }
2361 
2362 /**
2363   * @brief  Starts the TIM Input Capture measurement in DMA mode.
2364   * @param  htim TIM Input Capture handle
2365   * @param  Channel TIM Channels to be enabled
2366   *          This parameter can be one of the following values:
2367   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
2368   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
2369   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
2370   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
2371   * @param  pData The destination Buffer address.
2372   * @param  Length The length of data to be transferred from TIM peripheral to memory.
2373   * @retval HAL status
2374   */
HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef * htim,uint32_t Channel,uint32_t * pData,uint16_t Length)2375 HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
2376 {
2377   HAL_StatusTypeDef status = HAL_OK;
2378   uint32_t tmpsmcr;
2379 
2380   HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
2381   HAL_TIM_ChannelStateTypeDef complementary_channel_state = TIM_CHANNEL_N_STATE_GET(htim, Channel);
2382 
2383   /* Check the parameters */
2384   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
2385   assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2386 
2387   /* Set the TIM channel state */
2388   if ((channel_state == HAL_TIM_CHANNEL_STATE_BUSY)
2389       || (complementary_channel_state == HAL_TIM_CHANNEL_STATE_BUSY))
2390   {
2391     return HAL_BUSY;
2392   }
2393   else if ((channel_state == HAL_TIM_CHANNEL_STATE_READY)
2394            && (complementary_channel_state == HAL_TIM_CHANNEL_STATE_READY))
2395   {
2396     if ((pData == NULL) || (Length == 0U))
2397     {
2398       return HAL_ERROR;
2399     }
2400     else
2401     {
2402       TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2403       TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2404     }
2405   }
2406   else
2407   {
2408     return HAL_ERROR;
2409   }
2410 
2411   /* Enable the Input Capture channel */
2412   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
2413 
2414   switch (Channel)
2415   {
2416     case TIM_CHANNEL_1:
2417     {
2418       /* Set the DMA capture callbacks */
2419       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
2420       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2421 
2422       /* Set the DMA error callback */
2423       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
2424 
2425       /* Enable the DMA stream */
2426       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData,
2427                            Length) != HAL_OK)
2428       {
2429         /* Return error status */
2430         return HAL_ERROR;
2431       }
2432       /* Enable the TIM Capture/Compare 1 DMA request */
2433       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
2434       break;
2435     }
2436 
2437     case TIM_CHANNEL_2:
2438     {
2439       /* Set the DMA capture callbacks */
2440       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
2441       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2442 
2443       /* Set the DMA error callback */
2444       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
2445 
2446       /* Enable the DMA stream */
2447       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData,
2448                            Length) != HAL_OK)
2449       {
2450         /* Return error status */
2451         return HAL_ERROR;
2452       }
2453       /* Enable the TIM Capture/Compare 2  DMA request */
2454       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
2455       break;
2456     }
2457 
2458     case TIM_CHANNEL_3:
2459     {
2460       /* Set the DMA capture callbacks */
2461       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
2462       htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2463 
2464       /* Set the DMA error callback */
2465       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
2466 
2467       /* Enable the DMA stream */
2468       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData,
2469                            Length) != HAL_OK)
2470       {
2471         /* Return error status */
2472         return HAL_ERROR;
2473       }
2474       /* Enable the TIM Capture/Compare 3  DMA request */
2475       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
2476       break;
2477     }
2478 
2479     case TIM_CHANNEL_4:
2480     {
2481       /* Set the DMA capture callbacks */
2482       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
2483       htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2484 
2485       /* Set the DMA error callback */
2486       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
2487 
2488       /* Enable the DMA stream */
2489       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData,
2490                            Length) != HAL_OK)
2491       {
2492         /* Return error status */
2493         return HAL_ERROR;
2494       }
2495       /* Enable the TIM Capture/Compare 4  DMA request */
2496       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
2497       break;
2498     }
2499 
2500     default:
2501       status = HAL_ERROR;
2502       break;
2503   }
2504 
2505   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
2506   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
2507   {
2508     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
2509     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
2510     {
2511       __HAL_TIM_ENABLE(htim);
2512     }
2513   }
2514   else
2515   {
2516     __HAL_TIM_ENABLE(htim);
2517   }
2518 
2519   /* Return function status */
2520   return status;
2521 }
2522 
2523 /**
2524   * @brief  Stops the TIM Input Capture measurement in DMA mode.
2525   * @param  htim TIM Input Capture handle
2526   * @param  Channel TIM Channels to be disabled
2527   *          This parameter can be one of the following values:
2528   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
2529   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
2530   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
2531   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
2532   * @retval HAL status
2533   */
HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef * htim,uint32_t Channel)2534 HAL_StatusTypeDef HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
2535 {
2536   HAL_StatusTypeDef status = HAL_OK;
2537 
2538   /* Check the parameters */
2539   assert_param(IS_TIM_CCX_CHANNEL(htim->Instance, Channel));
2540   assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2541 
2542   /* Disable the Input Capture channel */
2543   TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
2544 
2545   switch (Channel)
2546   {
2547     case TIM_CHANNEL_1:
2548     {
2549       /* Disable the TIM Capture/Compare 1 DMA request */
2550       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
2551       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
2552       break;
2553     }
2554 
2555     case TIM_CHANNEL_2:
2556     {
2557       /* Disable the TIM Capture/Compare 2 DMA request */
2558       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
2559       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
2560       break;
2561     }
2562 
2563     case TIM_CHANNEL_3:
2564     {
2565       /* Disable the TIM Capture/Compare 3  DMA request */
2566       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
2567       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
2568       break;
2569     }
2570 
2571     case TIM_CHANNEL_4:
2572     {
2573       /* Disable the TIM Capture/Compare 4  DMA request */
2574       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
2575       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
2576       break;
2577     }
2578 
2579     default:
2580       status = HAL_ERROR;
2581       break;
2582   }
2583 
2584   if (status == HAL_OK)
2585   {
2586     /* Disable the Peripheral */
2587     __HAL_TIM_DISABLE(htim);
2588 
2589     /* Set the TIM channel state */
2590     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2591     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2592   }
2593 
2594   /* Return function status */
2595   return status;
2596 }
2597 /**
2598   * @}
2599   */
2600 
2601 /** @defgroup TIM_Exported_Functions_Group5 TIM One Pulse functions
2602   *  @brief    TIM One Pulse functions
2603   *
2604 @verbatim
2605   ==============================================================================
2606                         ##### TIM One Pulse functions #####
2607   ==============================================================================
2608   [..]
2609     This section provides functions allowing to:
2610     (+) Initialize and configure the TIM One Pulse.
2611     (+) De-initialize the TIM One Pulse.
2612     (+) Start the TIM One Pulse.
2613     (+) Stop the TIM One Pulse.
2614     (+) Start the TIM One Pulse and enable interrupt.
2615     (+) Stop the TIM One Pulse and disable interrupt.
2616     (+) Start the TIM One Pulse and enable DMA transfer.
2617     (+) Stop the TIM One Pulse and disable DMA transfer.
2618 
2619 @endverbatim
2620   * @{
2621   */
2622 /**
2623   * @brief  Initializes the TIM One Pulse Time Base according to the specified
2624   *         parameters in the TIM_HandleTypeDef and initializes the associated handle.
2625   * @note   Switching from Center Aligned counter mode to Edge counter mode (or reverse)
2626   *         requires a timer reset to avoid unexpected direction
2627   *         due to DIR bit readonly in center aligned mode.
2628   *         Ex: call @ref HAL_TIM_OnePulse_DeInit() before HAL_TIM_OnePulse_Init()
2629   * @note   When the timer instance is initialized in One Pulse mode, timer
2630   *         channels 1 and channel 2 are reserved and cannot be used for other
2631   *         purpose.
2632   * @param  htim TIM One Pulse handle
2633   * @param  OnePulseMode Select the One pulse mode.
2634   *         This parameter can be one of the following values:
2635   *            @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
2636   *            @arg TIM_OPMODE_REPETITIVE: Repetitive pulses will be generated.
2637   * @retval HAL status
2638   */
HAL_TIM_OnePulse_Init(TIM_HandleTypeDef * htim,uint32_t OnePulseMode)2639 HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)
2640 {
2641   /* Check the TIM handle allocation */
2642   if (htim == NULL)
2643   {
2644     return HAL_ERROR;
2645   }
2646 
2647   /* Check the parameters */
2648   assert_param(IS_TIM_INSTANCE(htim->Instance));
2649   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
2650   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
2651   assert_param(IS_TIM_OPM_MODE(OnePulseMode));
2652   assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
2653   assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
2654 
2655   if (htim->State == HAL_TIM_STATE_RESET)
2656   {
2657     /* Allocate lock resource and initialize it */
2658     htim->Lock = HAL_UNLOCKED;
2659 
2660 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2661     /* Reset interrupt callbacks to legacy weak callbacks */
2662     TIM_ResetCallback(htim);
2663 
2664     if (htim->OnePulse_MspInitCallback == NULL)
2665     {
2666       htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
2667     }
2668     /* Init the low level hardware : GPIO, CLOCK, NVIC */
2669     htim->OnePulse_MspInitCallback(htim);
2670 #else
2671     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
2672     HAL_TIM_OnePulse_MspInit(htim);
2673 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2674   }
2675 
2676   /* Set the TIM state */
2677   htim->State = HAL_TIM_STATE_BUSY;
2678 
2679   /* Configure the Time base in the One Pulse Mode */
2680   TIM_Base_SetConfig(htim->Instance, &htim->Init);
2681 
2682   /* Reset the OPM Bit */
2683   htim->Instance->CR1 &= ~TIM_CR1_OPM;
2684 
2685   /* Configure the OPM Mode */
2686   htim->Instance->CR1 |= OnePulseMode;
2687 
2688   /* Initialize the DMA burst operation state */
2689   htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
2690 
2691   /* Initialize the TIM channels state */
2692   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2693   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2694   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2695   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2696 
2697   /* Initialize the TIM state*/
2698   htim->State = HAL_TIM_STATE_READY;
2699 
2700   return HAL_OK;
2701 }
2702 
2703 /**
2704   * @brief  DeInitializes the TIM One Pulse
2705   * @param  htim TIM One Pulse handle
2706   * @retval HAL status
2707   */
HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef * htim)2708 HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)
2709 {
2710   /* Check the parameters */
2711   assert_param(IS_TIM_INSTANCE(htim->Instance));
2712 
2713   htim->State = HAL_TIM_STATE_BUSY;
2714 
2715   /* Disable the TIM Peripheral Clock */
2716   __HAL_TIM_DISABLE(htim);
2717 
2718 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2719   if (htim->OnePulse_MspDeInitCallback == NULL)
2720   {
2721     htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
2722   }
2723   /* DeInit the low level hardware */
2724   htim->OnePulse_MspDeInitCallback(htim);
2725 #else
2726   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
2727   HAL_TIM_OnePulse_MspDeInit(htim);
2728 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2729 
2730   /* Change the DMA burst operation state */
2731   htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
2732 
2733   /* Set the TIM channel state */
2734   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
2735   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
2736   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
2737   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
2738 
2739   /* Change TIM state */
2740   htim->State = HAL_TIM_STATE_RESET;
2741 
2742   /* Release Lock */
2743   __HAL_UNLOCK(htim);
2744 
2745   return HAL_OK;
2746 }
2747 
2748 /**
2749   * @brief  Initializes the TIM One Pulse MSP.
2750   * @param  htim TIM One Pulse handle
2751   * @retval None
2752   */
HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef * htim)2753 __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
2754 {
2755   /* Prevent unused argument(s) compilation warning */
2756   UNUSED(htim);
2757 
2758   /* NOTE : This function should not be modified, when the callback is needed,
2759             the HAL_TIM_OnePulse_MspInit could be implemented in the user file
2760    */
2761 }
2762 
2763 /**
2764   * @brief  DeInitializes TIM One Pulse MSP.
2765   * @param  htim TIM One Pulse handle
2766   * @retval None
2767   */
HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef * htim)2768 __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)
2769 {
2770   /* Prevent unused argument(s) compilation warning */
2771   UNUSED(htim);
2772 
2773   /* NOTE : This function should not be modified, when the callback is needed,
2774             the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
2775    */
2776 }
2777 
2778 /**
2779   * @brief  Starts the TIM One Pulse signal generation.
2780   * @note Though OutputChannel parameter is deprecated and ignored by the function
2781   *        it has been kept to avoid HAL_TIM API compatibility break.
2782   * @note The pulse output channel is determined when calling
2783   *       @ref HAL_TIM_OnePulse_ConfigChannel().
2784   * @param  htim TIM One Pulse handle
2785   * @param  OutputChannel See note above
2786   * @retval HAL status
2787   */
HAL_TIM_OnePulse_Start(TIM_HandleTypeDef * htim,uint32_t OutputChannel)2788 HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2789 {
2790   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
2791   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
2792   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
2793   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
2794 
2795   /* Prevent unused argument(s) compilation warning */
2796   UNUSED(OutputChannel);
2797 
2798   /* Check the TIM channels state */
2799   if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
2800       || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
2801       || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
2802       || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
2803   {
2804     return HAL_ERROR;
2805   }
2806 
2807   /* Set the TIM channels state */
2808   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
2809   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
2810   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
2811   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
2812 
2813   /* Enable the Capture compare and the Input Capture channels
2814     (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2815     if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2816     if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2817     whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2818 
2819     No need to enable the counter, it's enabled automatically by hardware
2820     (the counter starts in response to a stimulus and generate a pulse */
2821 
2822   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2823   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2824 
2825   if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2826   {
2827     /* Enable the main output */
2828     __HAL_TIM_MOE_ENABLE(htim);
2829   }
2830 
2831   /* Return function status */
2832   return HAL_OK;
2833 }
2834 
2835 /**
2836   * @brief  Stops the TIM One Pulse signal generation.
2837   * @note Though OutputChannel parameter is deprecated and ignored by the function
2838   *        it has been kept to avoid HAL_TIM API compatibility break.
2839   * @note The pulse output channel is determined when calling
2840   *       @ref HAL_TIM_OnePulse_ConfigChannel().
2841   * @param  htim TIM One Pulse handle
2842   * @param  OutputChannel See note above
2843   * @retval HAL status
2844   */
HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef * htim,uint32_t OutputChannel)2845 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2846 {
2847   /* Prevent unused argument(s) compilation warning */
2848   UNUSED(OutputChannel);
2849 
2850   /* Disable the Capture compare and the Input Capture channels
2851   (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2852   if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2853   if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2854   whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2855 
2856   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2857   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2858 
2859   if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2860   {
2861     /* Disable the Main Output */
2862     __HAL_TIM_MOE_DISABLE(htim);
2863   }
2864 
2865   /* Disable the Peripheral */
2866   __HAL_TIM_DISABLE(htim);
2867 
2868   /* Set the TIM channels state */
2869   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2870   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2871   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2872   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2873 
2874   /* Return function status */
2875   return HAL_OK;
2876 }
2877 
2878 /**
2879   * @brief  Starts the TIM One Pulse signal generation in interrupt mode.
2880   * @note Though OutputChannel parameter is deprecated and ignored by the function
2881   *        it has been kept to avoid HAL_TIM API compatibility break.
2882   * @note The pulse output channel is determined when calling
2883   *       @ref HAL_TIM_OnePulse_ConfigChannel().
2884   * @param  htim TIM One Pulse handle
2885   * @param  OutputChannel See note above
2886   * @retval HAL status
2887   */
HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef * htim,uint32_t OutputChannel)2888 HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2889 {
2890   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
2891   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
2892   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
2893   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
2894 
2895   /* Prevent unused argument(s) compilation warning */
2896   UNUSED(OutputChannel);
2897 
2898   /* Check the TIM channels state */
2899   if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
2900       || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
2901       || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
2902       || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
2903   {
2904     return HAL_ERROR;
2905   }
2906 
2907   /* Set the TIM channels state */
2908   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
2909   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
2910   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
2911   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
2912 
2913   /* Enable the Capture compare and the Input Capture channels
2914     (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2915     if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2916     if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2917     whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2918 
2919     No need to enable the counter, it's enabled automatically by hardware
2920     (the counter starts in response to a stimulus and generate a pulse */
2921 
2922   /* Enable the TIM Capture/Compare 1 interrupt */
2923   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2924 
2925   /* Enable the TIM Capture/Compare 2 interrupt */
2926   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2927 
2928   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2929   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2930 
2931   if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2932   {
2933     /* Enable the main output */
2934     __HAL_TIM_MOE_ENABLE(htim);
2935   }
2936 
2937   /* Return function status */
2938   return HAL_OK;
2939 }
2940 
2941 /**
2942   * @brief  Stops the TIM One Pulse signal generation in interrupt mode.
2943   * @note Though OutputChannel parameter is deprecated and ignored by the function
2944   *        it has been kept to avoid HAL_TIM API compatibility break.
2945   * @note The pulse output channel is determined when calling
2946   *       @ref HAL_TIM_OnePulse_ConfigChannel().
2947   * @param  htim TIM One Pulse handle
2948   * @param  OutputChannel See note above
2949   * @retval HAL status
2950   */
HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef * htim,uint32_t OutputChannel)2951 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2952 {
2953   /* Prevent unused argument(s) compilation warning */
2954   UNUSED(OutputChannel);
2955 
2956   /* Disable the TIM Capture/Compare 1 interrupt */
2957   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2958 
2959   /* Disable the TIM Capture/Compare 2 interrupt */
2960   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2961 
2962   /* Disable the Capture compare and the Input Capture channels
2963   (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2964   if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2965   if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2966   whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2967   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2968   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2969 
2970   if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2971   {
2972     /* Disable the Main Output */
2973     __HAL_TIM_MOE_DISABLE(htim);
2974   }
2975 
2976   /* Disable the Peripheral */
2977   __HAL_TIM_DISABLE(htim);
2978 
2979   /* Set the TIM channels state */
2980   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2981   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2982   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2983   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2984 
2985   /* Return function status */
2986   return HAL_OK;
2987 }
2988 
2989 /**
2990   * @}
2991   */
2992 
2993 /** @defgroup TIM_Exported_Functions_Group6 TIM Encoder functions
2994   *  @brief    TIM Encoder functions
2995   *
2996 @verbatim
2997   ==============================================================================
2998                           ##### TIM Encoder functions #####
2999   ==============================================================================
3000   [..]
3001     This section provides functions allowing to:
3002     (+) Initialize and configure the TIM Encoder.
3003     (+) De-initialize the TIM Encoder.
3004     (+) Start the TIM Encoder.
3005     (+) Stop the TIM Encoder.
3006     (+) Start the TIM Encoder and enable interrupt.
3007     (+) Stop the TIM Encoder and disable interrupt.
3008     (+) Start the TIM Encoder and enable DMA transfer.
3009     (+) Stop the TIM Encoder and disable DMA transfer.
3010 
3011 @endverbatim
3012   * @{
3013   */
3014 /**
3015   * @brief  Initializes the TIM Encoder Interface and initialize the associated handle.
3016   * @note   Switching from Center Aligned counter mode to Edge counter mode (or reverse)
3017   *         requires a timer reset to avoid unexpected direction
3018   *         due to DIR bit readonly in center aligned mode.
3019   *         Ex: call @ref HAL_TIM_Encoder_DeInit() before HAL_TIM_Encoder_Init()
3020   * @note   Encoder mode and External clock mode 2 are not compatible and must not be selected together
3021   *         Ex: A call for @ref HAL_TIM_Encoder_Init will erase the settings of @ref HAL_TIM_ConfigClockSource
3022   *         using TIM_CLOCKSOURCE_ETRMODE2 and vice versa
3023   * @note   When the timer instance is initialized in Encoder mode, timer
3024   *         channels 1 and channel 2 are reserved and cannot be used for other
3025   *         purpose.
3026   * @param  htim TIM Encoder Interface handle
3027   * @param  sConfig TIM Encoder Interface configuration structure
3028   * @retval HAL status
3029   */
HAL_TIM_Encoder_Init(TIM_HandleTypeDef * htim,const TIM_Encoder_InitTypeDef * sConfig)3030 HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim, const TIM_Encoder_InitTypeDef *sConfig)
3031 {
3032   uint32_t tmpsmcr;
3033   uint32_t tmpccmr1;
3034   uint32_t tmpccer;
3035 
3036   /* Check the TIM handle allocation */
3037   if (htim == NULL)
3038   {
3039     return HAL_ERROR;
3040   }
3041 
3042   /* Check the parameters */
3043   assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3044   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
3045   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
3046   assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
3047   assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));
3048   assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));
3049   assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));
3050   assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC1Polarity));
3051   assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC2Polarity));
3052   assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
3053   assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));
3054   assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
3055   assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));
3056   assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
3057 
3058   if (htim->State == HAL_TIM_STATE_RESET)
3059   {
3060     /* Allocate lock resource and initialize it */
3061     htim->Lock = HAL_UNLOCKED;
3062 
3063 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3064     /* Reset interrupt callbacks to legacy weak callbacks */
3065     TIM_ResetCallback(htim);
3066 
3067     if (htim->Encoder_MspInitCallback == NULL)
3068     {
3069       htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
3070     }
3071     /* Init the low level hardware : GPIO, CLOCK, NVIC */
3072     htim->Encoder_MspInitCallback(htim);
3073 #else
3074     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
3075     HAL_TIM_Encoder_MspInit(htim);
3076 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3077   }
3078 
3079   /* Set the TIM state */
3080   htim->State = HAL_TIM_STATE_BUSY;
3081 
3082   /* Reset the SMS and ECE bits */
3083   htim->Instance->SMCR &= ~(TIM_SMCR_SMS | TIM_SMCR_ECE);
3084 
3085   /* Configure the Time base in the Encoder Mode */
3086   TIM_Base_SetConfig(htim->Instance, &htim->Init);
3087 
3088   /* Get the TIMx SMCR register value */
3089   tmpsmcr = htim->Instance->SMCR;
3090 
3091   /* Get the TIMx CCMR1 register value */
3092   tmpccmr1 = htim->Instance->CCMR1;
3093 
3094   /* Get the TIMx CCER register value */
3095   tmpccer = htim->Instance->CCER;
3096 
3097   /* Set the encoder Mode */
3098   tmpsmcr |= sConfig->EncoderMode;
3099 
3100   /* Select the Capture Compare 1 and the Capture Compare 2 as input */
3101   tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);
3102   tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8U));
3103 
3104   /* Set the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
3105   tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);
3106   tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);
3107   tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8U);
3108   tmpccmr1 |= (sConfig->IC1Filter << 4U) | (sConfig->IC2Filter << 12U);
3109 
3110   /* Set the TI1 and the TI2 Polarities */
3111   tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
3112   tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);
3113   tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4U);
3114 
3115   /* Write to TIMx SMCR */
3116   htim->Instance->SMCR = tmpsmcr;
3117 
3118   /* Write to TIMx CCMR1 */
3119   htim->Instance->CCMR1 = tmpccmr1;
3120 
3121   /* Write to TIMx CCER */
3122   htim->Instance->CCER = tmpccer;
3123 
3124   /* Initialize the DMA burst operation state */
3125   htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
3126 
3127   /* Set the TIM channels state */
3128   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3129   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3130   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3131   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3132 
3133   /* Initialize the TIM state*/
3134   htim->State = HAL_TIM_STATE_READY;
3135 
3136   return HAL_OK;
3137 }
3138 
3139 
3140 /**
3141   * @brief  DeInitializes the TIM Encoder interface
3142   * @param  htim TIM Encoder Interface handle
3143   * @retval HAL status
3144   */
HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef * htim)3145 HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)
3146 {
3147   /* Check the parameters */
3148   assert_param(IS_TIM_INSTANCE(htim->Instance));
3149 
3150   htim->State = HAL_TIM_STATE_BUSY;
3151 
3152   /* Disable the TIM Peripheral Clock */
3153   __HAL_TIM_DISABLE(htim);
3154 
3155 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3156   if (htim->Encoder_MspDeInitCallback == NULL)
3157   {
3158     htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
3159   }
3160   /* DeInit the low level hardware */
3161   htim->Encoder_MspDeInitCallback(htim);
3162 #else
3163   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
3164   HAL_TIM_Encoder_MspDeInit(htim);
3165 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3166 
3167   /* Change the DMA burst operation state */
3168   htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
3169 
3170   /* Set the TIM channels state */
3171   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
3172   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
3173   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
3174   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
3175 
3176   /* Change TIM state */
3177   htim->State = HAL_TIM_STATE_RESET;
3178 
3179   /* Release Lock */
3180   __HAL_UNLOCK(htim);
3181 
3182   return HAL_OK;
3183 }
3184 
3185 /**
3186   * @brief  Initializes the TIM Encoder Interface MSP.
3187   * @param  htim TIM Encoder Interface handle
3188   * @retval None
3189   */
HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef * htim)3190 __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
3191 {
3192   /* Prevent unused argument(s) compilation warning */
3193   UNUSED(htim);
3194 
3195   /* NOTE : This function should not be modified, when the callback is needed,
3196             the HAL_TIM_Encoder_MspInit could be implemented in the user file
3197    */
3198 }
3199 
3200 /**
3201   * @brief  DeInitializes TIM Encoder Interface MSP.
3202   * @param  htim TIM Encoder Interface handle
3203   * @retval None
3204   */
HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef * htim)3205 __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)
3206 {
3207   /* Prevent unused argument(s) compilation warning */
3208   UNUSED(htim);
3209 
3210   /* NOTE : This function should not be modified, when the callback is needed,
3211             the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
3212    */
3213 }
3214 
3215 /**
3216   * @brief  Starts the TIM Encoder Interface.
3217   * @param  htim TIM Encoder Interface handle
3218   * @param  Channel TIM Channels to be enabled
3219   *          This parameter can be one of the following values:
3220   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
3221   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
3222   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3223   * @retval HAL status
3224   */
HAL_TIM_Encoder_Start(TIM_HandleTypeDef * htim,uint32_t Channel)3225 HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
3226 {
3227   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
3228   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
3229   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
3230   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
3231 
3232   /* Check the parameters */
3233   assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3234 
3235   /* Set the TIM channel(s) state */
3236   if (Channel == TIM_CHANNEL_1)
3237   {
3238     if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3239         || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY))
3240     {
3241       return HAL_ERROR;
3242     }
3243     else
3244     {
3245       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3246       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3247     }
3248   }
3249   else if (Channel == TIM_CHANNEL_2)
3250   {
3251     if ((channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
3252         || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
3253     {
3254       return HAL_ERROR;
3255     }
3256     else
3257     {
3258       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3259       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3260     }
3261   }
3262   else
3263   {
3264     if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3265         || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
3266         || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3267         || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
3268     {
3269       return HAL_ERROR;
3270     }
3271     else
3272     {
3273       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3274       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3275       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3276       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3277     }
3278   }
3279 
3280   /* Enable the encoder interface channels */
3281   switch (Channel)
3282   {
3283     case TIM_CHANNEL_1:
3284     {
3285       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3286       break;
3287     }
3288 
3289     case TIM_CHANNEL_2:
3290     {
3291       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3292       break;
3293     }
3294 
3295     default :
3296     {
3297       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3298       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3299       break;
3300     }
3301   }
3302   /* Enable the Peripheral */
3303   __HAL_TIM_ENABLE(htim);
3304 
3305   /* Return function status */
3306   return HAL_OK;
3307 }
3308 
3309 /**
3310   * @brief  Stops the TIM Encoder Interface.
3311   * @param  htim TIM Encoder Interface handle
3312   * @param  Channel TIM Channels to be disabled
3313   *          This parameter can be one of the following values:
3314   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
3315   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
3316   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3317   * @retval HAL status
3318   */
HAL_TIM_Encoder_Stop(TIM_HandleTypeDef * htim,uint32_t Channel)3319 HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
3320 {
3321   /* Check the parameters */
3322   assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3323 
3324   /* Disable the Input Capture channels 1 and 2
3325     (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
3326   switch (Channel)
3327   {
3328     case TIM_CHANNEL_1:
3329     {
3330       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3331       break;
3332     }
3333 
3334     case TIM_CHANNEL_2:
3335     {
3336       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3337       break;
3338     }
3339 
3340     default :
3341     {
3342       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3343       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3344       break;
3345     }
3346   }
3347 
3348   /* Disable the Peripheral */
3349   __HAL_TIM_DISABLE(htim);
3350 
3351   /* Set the TIM channel(s) state */
3352   if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
3353   {
3354     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3355     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3356   }
3357   else
3358   {
3359     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3360     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3361     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3362     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3363   }
3364 
3365   /* Return function status */
3366   return HAL_OK;
3367 }
3368 
3369 /**
3370   * @brief  Starts the TIM Encoder Interface in interrupt mode.
3371   * @param  htim TIM Encoder Interface handle
3372   * @param  Channel TIM Channels to be enabled
3373   *          This parameter can be one of the following values:
3374   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
3375   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
3376   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3377   * @retval HAL status
3378   */
HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef * htim,uint32_t Channel)3379 HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
3380 {
3381   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
3382   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
3383   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
3384   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
3385 
3386   /* Check the parameters */
3387   assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3388 
3389   /* Set the TIM channel(s) state */
3390   if (Channel == TIM_CHANNEL_1)
3391   {
3392     if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3393         || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY))
3394     {
3395       return HAL_ERROR;
3396     }
3397     else
3398     {
3399       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3400       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3401     }
3402   }
3403   else if (Channel == TIM_CHANNEL_2)
3404   {
3405     if ((channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
3406         || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
3407     {
3408       return HAL_ERROR;
3409     }
3410     else
3411     {
3412       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3413       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3414     }
3415   }
3416   else
3417   {
3418     if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3419         || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
3420         || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3421         || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
3422     {
3423       return HAL_ERROR;
3424     }
3425     else
3426     {
3427       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3428       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3429       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3430       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3431     }
3432   }
3433 
3434   /* Enable the encoder interface channels */
3435   /* Enable the capture compare Interrupts 1 and/or 2 */
3436   switch (Channel)
3437   {
3438     case TIM_CHANNEL_1:
3439     {
3440       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3441       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
3442       break;
3443     }
3444 
3445     case TIM_CHANNEL_2:
3446     {
3447       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3448       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
3449       break;
3450     }
3451 
3452     default :
3453     {
3454       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3455       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3456       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
3457       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
3458       break;
3459     }
3460   }
3461 
3462   /* Enable the Peripheral */
3463   __HAL_TIM_ENABLE(htim);
3464 
3465   /* Return function status */
3466   return HAL_OK;
3467 }
3468 
3469 /**
3470   * @brief  Stops the TIM Encoder Interface in interrupt mode.
3471   * @param  htim TIM Encoder Interface handle
3472   * @param  Channel TIM Channels to be disabled
3473   *          This parameter can be one of the following values:
3474   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
3475   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
3476   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3477   * @retval HAL status
3478   */
HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef * htim,uint32_t Channel)3479 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
3480 {
3481   /* Check the parameters */
3482   assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3483 
3484   /* Disable the Input Capture channels 1 and 2
3485     (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
3486   if (Channel == TIM_CHANNEL_1)
3487   {
3488     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3489 
3490     /* Disable the capture compare Interrupts 1 */
3491     __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
3492   }
3493   else if (Channel == TIM_CHANNEL_2)
3494   {
3495     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3496 
3497     /* Disable the capture compare Interrupts 2 */
3498     __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
3499   }
3500   else
3501   {
3502     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3503     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3504 
3505     /* Disable the capture compare Interrupts 1 and 2 */
3506     __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
3507     __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
3508   }
3509 
3510   /* Disable the Peripheral */
3511   __HAL_TIM_DISABLE(htim);
3512 
3513   /* Set the TIM channel(s) state */
3514   if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
3515   {
3516     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3517     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3518   }
3519   else
3520   {
3521     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3522     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3523     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3524     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3525   }
3526 
3527   /* Return function status */
3528   return HAL_OK;
3529 }
3530 
3531 /**
3532   * @brief  Starts the TIM Encoder Interface in DMA mode.
3533   * @param  htim TIM Encoder Interface handle
3534   * @param  Channel TIM Channels to be enabled
3535   *          This parameter can be one of the following values:
3536   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
3537   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
3538   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3539   * @param  pData1 The destination Buffer address for IC1.
3540   * @param  pData2 The destination Buffer address for IC2.
3541   * @param  Length The length of data to be transferred from TIM peripheral to memory.
3542   * @retval HAL status
3543   */
HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef * htim,uint32_t Channel,uint32_t * pData1,uint32_t * pData2,uint16_t Length)3544 HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1,
3545                                             uint32_t *pData2, uint16_t Length)
3546 {
3547   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
3548   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
3549   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
3550   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
3551 
3552   /* Check the parameters */
3553   assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3554 
3555   /* Set the TIM channel(s) state */
3556   if (Channel == TIM_CHANNEL_1)
3557   {
3558     if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
3559         || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY))
3560     {
3561       return HAL_BUSY;
3562     }
3563     else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
3564              && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY))
3565     {
3566       if ((pData1 == NULL) || (Length == 0U))
3567       {
3568         return HAL_ERROR;
3569       }
3570       else
3571       {
3572         TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3573         TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3574       }
3575     }
3576     else
3577     {
3578       return HAL_ERROR;
3579     }
3580   }
3581   else if (Channel == TIM_CHANNEL_2)
3582   {
3583     if ((channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY)
3584         || (complementary_channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY))
3585     {
3586       return HAL_BUSY;
3587     }
3588     else if ((channel_2_state == HAL_TIM_CHANNEL_STATE_READY)
3589              && (complementary_channel_2_state == HAL_TIM_CHANNEL_STATE_READY))
3590     {
3591       if ((pData2 == NULL) || (Length == 0U))
3592       {
3593         return HAL_ERROR;
3594       }
3595       else
3596       {
3597         TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3598         TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3599       }
3600     }
3601     else
3602     {
3603       return HAL_ERROR;
3604     }
3605   }
3606   else
3607   {
3608     if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
3609         || (channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY)
3610         || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
3611         || (complementary_channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY))
3612     {
3613       return HAL_BUSY;
3614     }
3615     else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
3616              && (channel_2_state == HAL_TIM_CHANNEL_STATE_READY)
3617              && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
3618              && (complementary_channel_2_state == HAL_TIM_CHANNEL_STATE_READY))
3619     {
3620       if ((((pData1 == NULL) || (pData2 == NULL))) || (Length == 0U))
3621       {
3622         return HAL_ERROR;
3623       }
3624       else
3625       {
3626         TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3627         TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3628         TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3629         TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3630       }
3631     }
3632     else
3633     {
3634       return HAL_ERROR;
3635     }
3636   }
3637 
3638   switch (Channel)
3639   {
3640     case TIM_CHANNEL_1:
3641     {
3642       /* Set the DMA capture callbacks */
3643       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
3644       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3645 
3646       /* Set the DMA error callback */
3647       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
3648 
3649       /* Enable the DMA stream */
3650       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1,
3651                            Length) != HAL_OK)
3652       {
3653         /* Return error status */
3654         return HAL_ERROR;
3655       }
3656       /* Enable the TIM Input Capture DMA request */
3657       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
3658 
3659       /* Enable the Capture compare channel */
3660       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3661 
3662       /* Enable the Peripheral */
3663       __HAL_TIM_ENABLE(htim);
3664 
3665       break;
3666     }
3667 
3668     case TIM_CHANNEL_2:
3669     {
3670       /* Set the DMA capture callbacks */
3671       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
3672       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3673 
3674       /* Set the DMA error callback */
3675       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError;
3676       /* Enable the DMA stream */
3677       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2,
3678                            Length) != HAL_OK)
3679       {
3680         /* Return error status */
3681         return HAL_ERROR;
3682       }
3683       /* Enable the TIM Input Capture  DMA request */
3684       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
3685 
3686       /* Enable the Capture compare channel */
3687       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3688 
3689       /* Enable the Peripheral */
3690       __HAL_TIM_ENABLE(htim);
3691 
3692       break;
3693     }
3694 
3695     default:
3696     {
3697       /* Set the DMA capture callbacks */
3698       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
3699       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3700 
3701       /* Set the DMA error callback */
3702       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
3703 
3704       /* Enable the DMA stream */
3705       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1,
3706                            Length) != HAL_OK)
3707       {
3708         /* Return error status */
3709         return HAL_ERROR;
3710       }
3711 
3712       /* Set the DMA capture callbacks */
3713       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
3714       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3715 
3716       /* Set the DMA error callback */
3717       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
3718 
3719       /* Enable the DMA stream */
3720       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2,
3721                            Length) != HAL_OK)
3722       {
3723         /* Return error status */
3724         return HAL_ERROR;
3725       }
3726 
3727       /* Enable the TIM Input Capture  DMA request */
3728       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
3729       /* Enable the TIM Input Capture  DMA request */
3730       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
3731 
3732       /* Enable the Capture compare channel */
3733       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3734       TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3735 
3736       /* Enable the Peripheral */
3737       __HAL_TIM_ENABLE(htim);
3738 
3739       break;
3740     }
3741   }
3742 
3743   /* Return function status */
3744   return HAL_OK;
3745 }
3746 
3747 /**
3748   * @brief  Stops the TIM Encoder Interface in DMA mode.
3749   * @param  htim TIM Encoder Interface handle
3750   * @param  Channel TIM Channels to be enabled
3751   *          This parameter can be one of the following values:
3752   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
3753   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
3754   *            @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3755   * @retval HAL status
3756   */
HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef * htim,uint32_t Channel)3757 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
3758 {
3759   /* Check the parameters */
3760   assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3761 
3762   /* Disable the Input Capture channels 1 and 2
3763     (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
3764   if (Channel == TIM_CHANNEL_1)
3765   {
3766     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3767 
3768     /* Disable the capture compare DMA Request 1 */
3769     __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
3770     (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
3771   }
3772   else if (Channel == TIM_CHANNEL_2)
3773   {
3774     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3775 
3776     /* Disable the capture compare DMA Request 2 */
3777     __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
3778     (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
3779   }
3780   else
3781   {
3782     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3783     TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3784 
3785     /* Disable the capture compare DMA Request 1 and 2 */
3786     __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
3787     __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
3788     (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
3789     (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
3790   }
3791 
3792   /* Disable the Peripheral */
3793   __HAL_TIM_DISABLE(htim);
3794 
3795   /* Set the TIM channel(s) state */
3796   if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
3797   {
3798     TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3799     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3800   }
3801   else
3802   {
3803     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3804     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3805     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3806     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3807   }
3808 
3809   /* Return function status */
3810   return HAL_OK;
3811 }
3812 
3813 /**
3814   * @}
3815   */
3816 /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
3817   *  @brief    TIM IRQ handler management
3818   *
3819 @verbatim
3820   ==============================================================================
3821                         ##### IRQ handler management #####
3822   ==============================================================================
3823   [..]
3824     This section provides Timer IRQ handler function.
3825 
3826 @endverbatim
3827   * @{
3828   */
3829 /**
3830   * @brief  This function handles TIM interrupts requests.
3831   * @param  htim TIM  handle
3832   * @retval None
3833   */
HAL_TIM_IRQHandler(TIM_HandleTypeDef * htim)3834 void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
3835 {
3836   uint32_t itsource = htim->Instance->DIER;
3837   uint32_t itflag   = htim->Instance->SR;
3838 
3839   /* Capture compare 1 event */
3840   if ((itflag & (TIM_FLAG_CC1)) == (TIM_FLAG_CC1))
3841   {
3842     if ((itsource & (TIM_IT_CC1)) == (TIM_IT_CC1))
3843     {
3844       {
3845         __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_CC1);
3846         htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
3847 
3848         /* Input capture event */
3849         if ((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U)
3850         {
3851 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3852           htim->IC_CaptureCallback(htim);
3853 #else
3854           HAL_TIM_IC_CaptureCallback(htim);
3855 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3856         }
3857         /* Output compare event */
3858         else
3859         {
3860 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3861           htim->OC_DelayElapsedCallback(htim);
3862           htim->PWM_PulseFinishedCallback(htim);
3863 #else
3864           HAL_TIM_OC_DelayElapsedCallback(htim);
3865           HAL_TIM_PWM_PulseFinishedCallback(htim);
3866 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3867         }
3868         htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3869       }
3870     }
3871   }
3872   /* Capture compare 2 event */
3873   if ((itflag & (TIM_FLAG_CC2)) == (TIM_FLAG_CC2))
3874   {
3875     if ((itsource & (TIM_IT_CC2)) == (TIM_IT_CC2))
3876     {
3877       __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_CC2);
3878       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
3879       /* Input capture event */
3880       if ((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U)
3881       {
3882 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3883         htim->IC_CaptureCallback(htim);
3884 #else
3885         HAL_TIM_IC_CaptureCallback(htim);
3886 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3887       }
3888       /* Output compare event */
3889       else
3890       {
3891 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3892         htim->OC_DelayElapsedCallback(htim);
3893         htim->PWM_PulseFinishedCallback(htim);
3894 #else
3895         HAL_TIM_OC_DelayElapsedCallback(htim);
3896         HAL_TIM_PWM_PulseFinishedCallback(htim);
3897 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3898       }
3899       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3900     }
3901   }
3902   /* Capture compare 3 event */
3903   if ((itflag & (TIM_FLAG_CC3)) == (TIM_FLAG_CC3))
3904   {
3905     if ((itsource & (TIM_IT_CC3)) == (TIM_IT_CC3))
3906     {
3907       __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_CC3);
3908       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
3909       /* Input capture event */
3910       if ((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U)
3911       {
3912 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3913         htim->IC_CaptureCallback(htim);
3914 #else
3915         HAL_TIM_IC_CaptureCallback(htim);
3916 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3917       }
3918       /* Output compare event */
3919       else
3920       {
3921 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3922         htim->OC_DelayElapsedCallback(htim);
3923         htim->PWM_PulseFinishedCallback(htim);
3924 #else
3925         HAL_TIM_OC_DelayElapsedCallback(htim);
3926         HAL_TIM_PWM_PulseFinishedCallback(htim);
3927 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3928       }
3929       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3930     }
3931   }
3932   /* Capture compare 4 event */
3933   if ((itflag & (TIM_FLAG_CC4)) == (TIM_FLAG_CC4))
3934   {
3935     if ((itsource & (TIM_IT_CC4)) == (TIM_IT_CC4))
3936     {
3937       __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_CC4);
3938       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
3939       /* Input capture event */
3940       if ((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U)
3941       {
3942 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3943         htim->IC_CaptureCallback(htim);
3944 #else
3945         HAL_TIM_IC_CaptureCallback(htim);
3946 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3947       }
3948       /* Output compare event */
3949       else
3950       {
3951 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3952         htim->OC_DelayElapsedCallback(htim);
3953         htim->PWM_PulseFinishedCallback(htim);
3954 #else
3955         HAL_TIM_OC_DelayElapsedCallback(htim);
3956         HAL_TIM_PWM_PulseFinishedCallback(htim);
3957 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3958       }
3959       htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3960     }
3961   }
3962   /* TIM Update event */
3963   if ((itflag & (TIM_FLAG_UPDATE)) == (TIM_FLAG_UPDATE))
3964   {
3965     if ((itsource & (TIM_IT_UPDATE)) == (TIM_IT_UPDATE))
3966     {
3967       __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_UPDATE);
3968 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3969       htim->PeriodElapsedCallback(htim);
3970 #else
3971       HAL_TIM_PeriodElapsedCallback(htim);
3972 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3973     }
3974   }
3975   /* TIM Break input event */
3976   if (((itflag & (TIM_FLAG_BREAK)) == (TIM_FLAG_BREAK)) || \
3977       ((itflag & (TIM_FLAG_SYSTEM_BREAK)) == (TIM_FLAG_SYSTEM_BREAK)))
3978   {
3979     if ((itsource & (TIM_IT_BREAK)) == (TIM_IT_BREAK))
3980     {
3981       __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_BREAK | TIM_FLAG_SYSTEM_BREAK);
3982 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3983       htim->BreakCallback(htim);
3984 #else
3985       HAL_TIMEx_BreakCallback(htim);
3986 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3987     }
3988   }
3989   /* TIM Break2 input event */
3990   if ((itflag & (TIM_FLAG_BREAK2)) == (TIM_FLAG_BREAK2))
3991   {
3992     if ((itsource & (TIM_IT_BREAK)) == (TIM_IT_BREAK))
3993     {
3994       __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_BREAK2);
3995 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3996       htim->Break2Callback(htim);
3997 #else
3998       HAL_TIMEx_Break2Callback(htim);
3999 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
4000     }
4001   }
4002   /* TIM Trigger detection event */
4003   if ((itflag & (TIM_FLAG_TRIGGER)) == (TIM_FLAG_TRIGGER))
4004   {
4005     if ((itsource & (TIM_IT_TRIGGER)) == (TIM_IT_TRIGGER))
4006     {
4007       __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_TRIGGER);
4008 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
4009       htim->TriggerCallback(htim);
4010 #else
4011       HAL_TIM_TriggerCallback(htim);
4012 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
4013     }
4014   }
4015   /* TIM commutation event */
4016   if ((itflag & (TIM_FLAG_COM)) == (TIM_FLAG_COM))
4017   {
4018     if ((itsource & (TIM_IT_COM)) == (TIM_IT_COM))
4019     {
4020       __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_COM);
4021 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
4022       htim->CommutationCallback(htim);
4023 #else
4024       HAL_TIMEx_CommutCallback(htim);
4025 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
4026     }
4027   }
4028 }
4029 
4030 /**
4031   * @}
4032   */
4033 
4034 /** @defgroup TIM_Exported_Functions_Group8 TIM Peripheral Control functions
4035   *  @brief    TIM Peripheral Control functions
4036   *
4037 @verbatim
4038   ==============================================================================
4039                    ##### Peripheral Control functions #####
4040   ==============================================================================
4041  [..]
4042    This section provides functions allowing to:
4043       (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
4044       (+) Configure External Clock source.
4045       (+) Configure Complementary channels, break features and dead time.
4046       (+) Configure Master and the Slave synchronization.
4047       (+) Configure the DMA Burst Mode.
4048 
4049 @endverbatim
4050   * @{
4051   */
4052 
4053 /**
4054   * @brief  Initializes the TIM Output Compare Channels according to the specified
4055   *         parameters in the TIM_OC_InitTypeDef.
4056   * @param  htim TIM Output Compare handle
4057   * @param  sConfig TIM Output Compare configuration structure
4058   * @param  Channel TIM Channels to configure
4059   *          This parameter can be one of the following values:
4060   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
4061   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
4062   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
4063   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
4064   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected
4065   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected
4066   * @retval HAL status
4067   */
HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef * htim,const TIM_OC_InitTypeDef * sConfig,uint32_t Channel)4068 HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim,
4069                                            const TIM_OC_InitTypeDef *sConfig,
4070                                            uint32_t Channel)
4071 {
4072   HAL_StatusTypeDef status = HAL_OK;
4073 
4074   /* Check the parameters */
4075   assert_param(IS_TIM_CHANNELS(Channel));
4076   assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
4077   assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
4078 
4079   /* Process Locked */
4080   __HAL_LOCK(htim);
4081 
4082   switch (Channel)
4083   {
4084     case TIM_CHANNEL_1:
4085     {
4086       /* Check the parameters */
4087       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4088 
4089       /* Configure the TIM Channel 1 in Output Compare */
4090       TIM_OC1_SetConfig(htim->Instance, sConfig);
4091       break;
4092     }
4093 
4094     case TIM_CHANNEL_2:
4095     {
4096       /* Check the parameters */
4097       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4098 
4099       /* Configure the TIM Channel 2 in Output Compare */
4100       TIM_OC2_SetConfig(htim->Instance, sConfig);
4101       break;
4102     }
4103 
4104     case TIM_CHANNEL_3:
4105     {
4106       /* Check the parameters */
4107       assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
4108 
4109       /* Configure the TIM Channel 3 in Output Compare */
4110       TIM_OC3_SetConfig(htim->Instance, sConfig);
4111       break;
4112     }
4113 
4114     case TIM_CHANNEL_4:
4115     {
4116       /* Check the parameters */
4117       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
4118 
4119       /* Configure the TIM Channel 4 in Output Compare */
4120       TIM_OC4_SetConfig(htim->Instance, sConfig);
4121       break;
4122     }
4123 
4124     case TIM_CHANNEL_5:
4125     {
4126       /* Check the parameters */
4127       assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
4128 
4129       /* Configure the TIM Channel 5 in Output Compare */
4130       TIM_OC5_SetConfig(htim->Instance, sConfig);
4131       break;
4132     }
4133 
4134     case TIM_CHANNEL_6:
4135     {
4136       /* Check the parameters */
4137       assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
4138 
4139       /* Configure the TIM Channel 6 in Output Compare */
4140       TIM_OC6_SetConfig(htim->Instance, sConfig);
4141       break;
4142     }
4143 
4144     default:
4145       status = HAL_ERROR;
4146       break;
4147   }
4148 
4149   __HAL_UNLOCK(htim);
4150 
4151   return status;
4152 }
4153 
4154 /**
4155   * @brief  Initializes the TIM Input Capture Channels according to the specified
4156   *         parameters in the TIM_IC_InitTypeDef.
4157   * @param  htim TIM IC handle
4158   * @param  sConfig TIM Input Capture configuration structure
4159   * @param  Channel TIM Channel to configure
4160   *          This parameter can be one of the following values:
4161   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
4162   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
4163   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
4164   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
4165   * @retval HAL status
4166   */
HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef * htim,const TIM_IC_InitTypeDef * sConfig,uint32_t Channel)4167 HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, const TIM_IC_InitTypeDef *sConfig, uint32_t Channel)
4168 {
4169   HAL_StatusTypeDef status = HAL_OK;
4170 
4171   /* Check the parameters */
4172   assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4173   assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
4174   assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
4175   assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
4176   assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
4177 
4178   /* Process Locked */
4179   __HAL_LOCK(htim);
4180 
4181   if (Channel == TIM_CHANNEL_1)
4182   {
4183     /* TI1 Configuration */
4184     TIM_TI1_SetConfig(htim->Instance,
4185                       sConfig->ICPolarity,
4186                       sConfig->ICSelection,
4187                       sConfig->ICFilter);
4188 
4189     /* Reset the IC1PSC Bits */
4190     htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
4191 
4192     /* Set the IC1PSC value */
4193     htim->Instance->CCMR1 |= sConfig->ICPrescaler;
4194   }
4195   else if (Channel == TIM_CHANNEL_2)
4196   {
4197     /* TI2 Configuration */
4198     assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4199 
4200     TIM_TI2_SetConfig(htim->Instance,
4201                       sConfig->ICPolarity,
4202                       sConfig->ICSelection,
4203                       sConfig->ICFilter);
4204 
4205     /* Reset the IC2PSC Bits */
4206     htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
4207 
4208     /* Set the IC2PSC value */
4209     htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8U);
4210   }
4211   else if (Channel == TIM_CHANNEL_3)
4212   {
4213     /* TI3 Configuration */
4214     assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
4215 
4216     TIM_TI3_SetConfig(htim->Instance,
4217                       sConfig->ICPolarity,
4218                       sConfig->ICSelection,
4219                       sConfig->ICFilter);
4220 
4221     /* Reset the IC3PSC Bits */
4222     htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
4223 
4224     /* Set the IC3PSC value */
4225     htim->Instance->CCMR2 |= sConfig->ICPrescaler;
4226   }
4227   else if (Channel == TIM_CHANNEL_4)
4228   {
4229     /* TI4 Configuration */
4230     assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
4231 
4232     TIM_TI4_SetConfig(htim->Instance,
4233                       sConfig->ICPolarity,
4234                       sConfig->ICSelection,
4235                       sConfig->ICFilter);
4236 
4237     /* Reset the IC4PSC Bits */
4238     htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
4239 
4240     /* Set the IC4PSC value */
4241     htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8U);
4242   }
4243   else
4244   {
4245     status = HAL_ERROR;
4246   }
4247 
4248   __HAL_UNLOCK(htim);
4249 
4250   return status;
4251 }
4252 
4253 /**
4254   * @brief  Initializes the TIM PWM  channels according to the specified
4255   *         parameters in the TIM_OC_InitTypeDef.
4256   * @param  htim TIM PWM handle
4257   * @param  sConfig TIM PWM configuration structure
4258   * @param  Channel TIM Channels to be configured
4259   *          This parameter can be one of the following values:
4260   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
4261   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
4262   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
4263   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
4264   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected
4265   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected
4266   * @retval HAL status
4267   */
HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef * htim,const TIM_OC_InitTypeDef * sConfig,uint32_t Channel)4268 HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
4269                                             const TIM_OC_InitTypeDef *sConfig,
4270                                             uint32_t Channel)
4271 {
4272   HAL_StatusTypeDef status = HAL_OK;
4273 
4274   /* Check the parameters */
4275   assert_param(IS_TIM_CHANNELS(Channel));
4276   assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
4277   assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
4278   assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
4279 
4280   /* Process Locked */
4281   __HAL_LOCK(htim);
4282 
4283   switch (Channel)
4284   {
4285     case TIM_CHANNEL_1:
4286     {
4287       /* Check the parameters */
4288       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4289 
4290       /* Configure the Channel 1 in PWM mode */
4291       TIM_OC1_SetConfig(htim->Instance, sConfig);
4292 
4293       /* Set the Preload enable bit for channel1 */
4294       htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
4295 
4296       /* Configure the Output Fast mode */
4297       htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
4298       htim->Instance->CCMR1 |= sConfig->OCFastMode;
4299       break;
4300     }
4301 
4302     case TIM_CHANNEL_2:
4303     {
4304       /* Check the parameters */
4305       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4306 
4307       /* Configure the Channel 2 in PWM mode */
4308       TIM_OC2_SetConfig(htim->Instance, sConfig);
4309 
4310       /* Set the Preload enable bit for channel2 */
4311       htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
4312 
4313       /* Configure the Output Fast mode */
4314       htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
4315       htim->Instance->CCMR1 |= sConfig->OCFastMode << 8U;
4316       break;
4317     }
4318 
4319     case TIM_CHANNEL_3:
4320     {
4321       /* Check the parameters */
4322       assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
4323 
4324       /* Configure the Channel 3 in PWM mode */
4325       TIM_OC3_SetConfig(htim->Instance, sConfig);
4326 
4327       /* Set the Preload enable bit for channel3 */
4328       htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
4329 
4330       /* Configure the Output Fast mode */
4331       htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
4332       htim->Instance->CCMR2 |= sConfig->OCFastMode;
4333       break;
4334     }
4335 
4336     case TIM_CHANNEL_4:
4337     {
4338       /* Check the parameters */
4339       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
4340 
4341       /* Configure the Channel 4 in PWM mode */
4342       TIM_OC4_SetConfig(htim->Instance, sConfig);
4343 
4344       /* Set the Preload enable bit for channel4 */
4345       htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
4346 
4347       /* Configure the Output Fast mode */
4348       htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
4349       htim->Instance->CCMR2 |= sConfig->OCFastMode << 8U;
4350       break;
4351     }
4352 
4353     case TIM_CHANNEL_5:
4354     {
4355       /* Check the parameters */
4356       assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
4357 
4358       /* Configure the Channel 5 in PWM mode */
4359       TIM_OC5_SetConfig(htim->Instance, sConfig);
4360 
4361       /* Set the Preload enable bit for channel5*/
4362       htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
4363 
4364       /* Configure the Output Fast mode */
4365       htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;
4366       htim->Instance->CCMR3 |= sConfig->OCFastMode;
4367       break;
4368     }
4369 
4370     case TIM_CHANNEL_6:
4371     {
4372       /* Check the parameters */
4373       assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
4374 
4375       /* Configure the Channel 6 in PWM mode */
4376       TIM_OC6_SetConfig(htim->Instance, sConfig);
4377 
4378       /* Set the Preload enable bit for channel6 */
4379       htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
4380 
4381       /* Configure the Output Fast mode */
4382       htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;
4383       htim->Instance->CCMR3 |= sConfig->OCFastMode << 8U;
4384       break;
4385     }
4386 
4387     default:
4388       status = HAL_ERROR;
4389       break;
4390   }
4391 
4392   __HAL_UNLOCK(htim);
4393 
4394   return status;
4395 }
4396 
4397 /**
4398   * @brief  Initializes the TIM One Pulse Channels according to the specified
4399   *         parameters in the TIM_OnePulse_InitTypeDef.
4400   * @param  htim TIM One Pulse handle
4401   * @param  sConfig TIM One Pulse configuration structure
4402   * @param  OutputChannel TIM output channel to configure
4403   *          This parameter can be one of the following values:
4404   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
4405   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
4406   * @param  InputChannel TIM input Channel to configure
4407   *          This parameter can be one of the following values:
4408   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
4409   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
4410   * @note  To output a waveform with a minimum delay user can enable the fast
4411   *        mode by calling the @ref __HAL_TIM_ENABLE_OCxFAST macro. Then CCx
4412   *        output is forced in response to the edge detection on TIx input,
4413   *        without taking in account the comparison.
4414   * @retval HAL status
4415   */
HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef * htim,TIM_OnePulse_InitTypeDef * sConfig,uint32_t OutputChannel,uint32_t InputChannel)4416 HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim,  TIM_OnePulse_InitTypeDef *sConfig,
4417                                                  uint32_t OutputChannel,  uint32_t InputChannel)
4418 {
4419   HAL_StatusTypeDef status = HAL_OK;
4420   TIM_OC_InitTypeDef temp1;
4421 
4422   /* Check the parameters */
4423   assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
4424   assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
4425 
4426   if (OutputChannel != InputChannel)
4427   {
4428     /* Process Locked */
4429     __HAL_LOCK(htim);
4430 
4431     htim->State = HAL_TIM_STATE_BUSY;
4432 
4433     /* Extract the Output compare configuration from sConfig structure */
4434     temp1.OCMode = sConfig->OCMode;
4435     temp1.Pulse = sConfig->Pulse;
4436     temp1.OCPolarity = sConfig->OCPolarity;
4437     temp1.OCNPolarity = sConfig->OCNPolarity;
4438     temp1.OCIdleState = sConfig->OCIdleState;
4439     temp1.OCNIdleState = sConfig->OCNIdleState;
4440 
4441     switch (OutputChannel)
4442     {
4443       case TIM_CHANNEL_1:
4444       {
4445         assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4446 
4447         TIM_OC1_SetConfig(htim->Instance, &temp1);
4448         break;
4449       }
4450 
4451       case TIM_CHANNEL_2:
4452       {
4453         assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4454 
4455         TIM_OC2_SetConfig(htim->Instance, &temp1);
4456         break;
4457       }
4458 
4459       default:
4460         status = HAL_ERROR;
4461         break;
4462     }
4463 
4464     if (status == HAL_OK)
4465     {
4466       switch (InputChannel)
4467       {
4468         case TIM_CHANNEL_1:
4469         {
4470           assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4471 
4472           TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
4473                             sConfig->ICSelection, sConfig->ICFilter);
4474 
4475           /* Reset the IC1PSC Bits */
4476           htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
4477 
4478           /* Select the Trigger source */
4479           htim->Instance->SMCR &= ~TIM_SMCR_TS;
4480           htim->Instance->SMCR |= TIM_TS_TI1FP1;
4481 
4482           /* Select the Slave Mode */
4483           htim->Instance->SMCR &= ~TIM_SMCR_SMS;
4484           htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
4485           break;
4486         }
4487 
4488         case TIM_CHANNEL_2:
4489         {
4490           assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4491 
4492           TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
4493                             sConfig->ICSelection, sConfig->ICFilter);
4494 
4495           /* Reset the IC2PSC Bits */
4496           htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
4497 
4498           /* Select the Trigger source */
4499           htim->Instance->SMCR &= ~TIM_SMCR_TS;
4500           htim->Instance->SMCR |= TIM_TS_TI2FP2;
4501 
4502           /* Select the Slave Mode */
4503           htim->Instance->SMCR &= ~TIM_SMCR_SMS;
4504           htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
4505           break;
4506         }
4507 
4508         default:
4509           status = HAL_ERROR;
4510           break;
4511       }
4512     }
4513 
4514     htim->State = HAL_TIM_STATE_READY;
4515 
4516     __HAL_UNLOCK(htim);
4517 
4518     return status;
4519   }
4520   else
4521   {
4522     return HAL_ERROR;
4523   }
4524 }
4525 
4526 /**
4527   * @brief  Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
4528   * @param  htim TIM handle
4529   * @param  BurstBaseAddress TIM Base address from where the DMA  will start the Data write
4530   *         This parameter can be one of the following values:
4531   *            @arg TIM_DMABASE_CR1
4532   *            @arg TIM_DMABASE_CR2
4533   *            @arg TIM_DMABASE_SMCR
4534   *            @arg TIM_DMABASE_DIER
4535   *            @arg TIM_DMABASE_SR
4536   *            @arg TIM_DMABASE_EGR
4537   *            @arg TIM_DMABASE_CCMR1
4538   *            @arg TIM_DMABASE_CCMR2
4539   *            @arg TIM_DMABASE_CCER
4540   *            @arg TIM_DMABASE_CNT
4541   *            @arg TIM_DMABASE_PSC
4542   *            @arg TIM_DMABASE_ARR
4543   *            @arg TIM_DMABASE_RCR
4544   *            @arg TIM_DMABASE_CCR1
4545   *            @arg TIM_DMABASE_CCR2
4546   *            @arg TIM_DMABASE_CCR3
4547   *            @arg TIM_DMABASE_CCR4
4548   *            @arg TIM_DMABASE_BDTR
4549   *            @arg TIM_DMABASE_CCMR3
4550   *            @arg TIM_DMABASE_CCR5
4551   *            @arg TIM_DMABASE_CCR6
4552   *            @arg TIM_DMABASE_AF1
4553   *            @arg TIM_DMABASE_AF2
4554   *            @arg TIM_DMABASE_TISEL
4555   *
4556   * @param  BurstRequestSrc TIM DMA Request sources
4557   *         This parameter can be one of the following values:
4558   *            @arg TIM_DMA_UPDATE: TIM update Interrupt source
4559   *            @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4560   *            @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4561   *            @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4562   *            @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4563   *            @arg TIM_DMA_COM: TIM Commutation DMA source
4564   *            @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4565   * @param  BurstBuffer The Buffer address.
4566   * @param  BurstLength DMA Burst length. This parameter can be one value
4567   *         between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4568   * @note   This function should be used only when BurstLength is equal to DMA data transfer length.
4569   * @retval HAL status
4570   */
HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef * htim,uint32_t BurstBaseAddress,uint32_t BurstRequestSrc,const uint32_t * BurstBuffer,uint32_t BurstLength)4571 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4572                                               uint32_t BurstRequestSrc, const uint32_t *BurstBuffer,
4573                                               uint32_t  BurstLength)
4574 {
4575   HAL_StatusTypeDef status;
4576 
4577   status = HAL_TIM_DMABurst_MultiWriteStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
4578                                             ((BurstLength) >> 8U) + 1U);
4579 
4580 
4581 
4582   return status;
4583 }
4584 
4585 /**
4586   * @brief  Configure the DMA Burst to transfer multiple Data from the memory to the TIM peripheral
4587   * @param  htim TIM handle
4588   * @param  BurstBaseAddress TIM Base address from where the DMA will start the Data write
4589   *         This parameter can be one of the following values:
4590   *            @arg TIM_DMABASE_CR1
4591   *            @arg TIM_DMABASE_CR2
4592   *            @arg TIM_DMABASE_SMCR
4593   *            @arg TIM_DMABASE_DIER
4594   *            @arg TIM_DMABASE_SR
4595   *            @arg TIM_DMABASE_EGR
4596   *            @arg TIM_DMABASE_CCMR1
4597   *            @arg TIM_DMABASE_CCMR2
4598   *            @arg TIM_DMABASE_CCER
4599   *            @arg TIM_DMABASE_CNT
4600   *            @arg TIM_DMABASE_PSC
4601   *            @arg TIM_DMABASE_ARR
4602   *            @arg TIM_DMABASE_RCR
4603   *            @arg TIM_DMABASE_CCR1
4604   *            @arg TIM_DMABASE_CCR2
4605   *            @arg TIM_DMABASE_CCR3
4606   *            @arg TIM_DMABASE_CCR4
4607   *            @arg TIM_DMABASE_BDTR
4608   *            @arg TIM_DMABASE_CCMR3
4609   *            @arg TIM_DMABASE_CCR5
4610   *            @arg TIM_DMABASE_CCR6
4611   *            @arg TIM_DMABASE_AF1
4612   *            @arg TIM_DMABASE_AF2
4613   *            @arg TIM_DMABASE_TISEL
4614   *
4615   * @param  BurstRequestSrc TIM DMA Request sources
4616   *         This parameter can be one of the following values:
4617   *            @arg TIM_DMA_UPDATE: TIM update Interrupt source
4618   *            @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4619   *            @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4620   *            @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4621   *            @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4622   *            @arg TIM_DMA_COM: TIM Commutation DMA source
4623   *            @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4624   * @param  BurstBuffer The Buffer address.
4625   * @param  BurstLength DMA Burst length. This parameter can be one value
4626   *         between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4627   * @param  DataLength Data length. This parameter can be one value
4628   *         between 1 and 0xFFFF.
4629   * @retval HAL status
4630   */
HAL_TIM_DMABurst_MultiWriteStart(TIM_HandleTypeDef * htim,uint32_t BurstBaseAddress,uint32_t BurstRequestSrc,const uint32_t * BurstBuffer,uint32_t BurstLength,uint32_t DataLength)4631 HAL_StatusTypeDef HAL_TIM_DMABurst_MultiWriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4632                                                    uint32_t BurstRequestSrc, const uint32_t *BurstBuffer,
4633                                                    uint32_t  BurstLength,  uint32_t  DataLength)
4634 {
4635   HAL_StatusTypeDef status = HAL_OK;
4636 
4637   /* Check the parameters */
4638   assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
4639   assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
4640   assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4641   assert_param(IS_TIM_DMA_LENGTH(BurstLength));
4642   assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
4643 
4644   if (htim->DMABurstState == HAL_DMA_BURST_STATE_BUSY)
4645   {
4646     return HAL_BUSY;
4647   }
4648   else if (htim->DMABurstState == HAL_DMA_BURST_STATE_READY)
4649   {
4650     if ((BurstBuffer == NULL) && (BurstLength > 0U))
4651     {
4652       return HAL_ERROR;
4653     }
4654     else
4655     {
4656       htim->DMABurstState = HAL_DMA_BURST_STATE_BUSY;
4657     }
4658   }
4659   else
4660   {
4661     /* nothing to do */
4662   }
4663 
4664   switch (BurstRequestSrc)
4665   {
4666     case TIM_DMA_UPDATE:
4667     {
4668       /* Set the DMA Period elapsed callbacks */
4669       htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
4670       htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
4671 
4672       /* Set the DMA error callback */
4673       htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
4674 
4675       /* Enable the DMA stream */
4676       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer,
4677                            (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4678       {
4679         /* Return error status */
4680         return HAL_ERROR;
4681       }
4682       break;
4683     }
4684     case TIM_DMA_CC1:
4685     {
4686       /* Set the DMA compare callbacks */
4687       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
4688       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4689 
4690       /* Set the DMA error callback */
4691       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
4692 
4693       /* Enable the DMA stream */
4694       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer,
4695                            (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4696       {
4697         /* Return error status */
4698         return HAL_ERROR;
4699       }
4700       break;
4701     }
4702     case TIM_DMA_CC2:
4703     {
4704       /* Set the DMA compare callbacks */
4705       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
4706       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4707 
4708       /* Set the DMA error callback */
4709       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
4710 
4711       /* Enable the DMA stream */
4712       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer,
4713                            (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4714       {
4715         /* Return error status */
4716         return HAL_ERROR;
4717       }
4718       break;
4719     }
4720     case TIM_DMA_CC3:
4721     {
4722       /* Set the DMA compare callbacks */
4723       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
4724       htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4725 
4726       /* Set the DMA error callback */
4727       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
4728 
4729       /* Enable the DMA stream */
4730       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer,
4731                            (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4732       {
4733         /* Return error status */
4734         return HAL_ERROR;
4735       }
4736       break;
4737     }
4738     case TIM_DMA_CC4:
4739     {
4740       /* Set the DMA compare callbacks */
4741       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
4742       htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4743 
4744       /* Set the DMA error callback */
4745       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
4746 
4747       /* Enable the DMA stream */
4748       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer,
4749                            (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4750       {
4751         /* Return error status */
4752         return HAL_ERROR;
4753       }
4754       break;
4755     }
4756     case TIM_DMA_COM:
4757     {
4758       /* Set the DMA commutation callbacks */
4759       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback =  TIMEx_DMACommutationCplt;
4760       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback =  TIMEx_DMACommutationHalfCplt;
4761 
4762       /* Set the DMA error callback */
4763       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
4764 
4765       /* Enable the DMA stream */
4766       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)BurstBuffer,
4767                            (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4768       {
4769         /* Return error status */
4770         return HAL_ERROR;
4771       }
4772       break;
4773     }
4774     case TIM_DMA_TRIGGER:
4775     {
4776       /* Set the DMA trigger callbacks */
4777       htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
4778       htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
4779 
4780       /* Set the DMA error callback */
4781       htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
4782 
4783       /* Enable the DMA stream */
4784       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer,
4785                            (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4786       {
4787         /* Return error status */
4788         return HAL_ERROR;
4789       }
4790       break;
4791     }
4792     default:
4793       status = HAL_ERROR;
4794       break;
4795   }
4796 
4797   if (status == HAL_OK)
4798   {
4799     /* Configure the DMA Burst Mode */
4800     htim->Instance->DCR = (BurstBaseAddress | BurstLength);
4801     /* Enable the TIM DMA Request */
4802     __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
4803   }
4804 
4805   /* Return function status */
4806   return status;
4807 }
4808 
4809 /**
4810   * @brief  Stops the TIM DMA Burst mode
4811   * @param  htim TIM handle
4812   * @param  BurstRequestSrc TIM DMA Request sources to disable
4813   * @retval HAL status
4814   */
HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef * htim,uint32_t BurstRequestSrc)4815 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
4816 {
4817   HAL_StatusTypeDef status = HAL_OK;
4818 
4819   /* Check the parameters */
4820   assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4821 
4822   /* Abort the DMA transfer (at least disable the DMA stream) */
4823   switch (BurstRequestSrc)
4824   {
4825     case TIM_DMA_UPDATE:
4826     {
4827       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
4828       break;
4829     }
4830     case TIM_DMA_CC1:
4831     {
4832       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
4833       break;
4834     }
4835     case TIM_DMA_CC2:
4836     {
4837       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
4838       break;
4839     }
4840     case TIM_DMA_CC3:
4841     {
4842       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
4843       break;
4844     }
4845     case TIM_DMA_CC4:
4846     {
4847       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
4848       break;
4849     }
4850     case TIM_DMA_COM:
4851     {
4852       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
4853       break;
4854     }
4855     case TIM_DMA_TRIGGER:
4856     {
4857       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
4858       break;
4859     }
4860     default:
4861       status = HAL_ERROR;
4862       break;
4863   }
4864 
4865   if (status == HAL_OK)
4866   {
4867     /* Disable the TIM Update DMA request */
4868     __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
4869 
4870     /* Change the DMA burst operation state */
4871     htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
4872   }
4873 
4874   /* Return function status */
4875   return status;
4876 }
4877 
4878 /**
4879   * @brief  Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
4880   * @param  htim TIM handle
4881   * @param  BurstBaseAddress TIM Base address from where the DMA  will start the Data read
4882   *         This parameter can be one of the following values:
4883   *            @arg TIM_DMABASE_CR1
4884   *            @arg TIM_DMABASE_CR2
4885   *            @arg TIM_DMABASE_SMCR
4886   *            @arg TIM_DMABASE_DIER
4887   *            @arg TIM_DMABASE_SR
4888   *            @arg TIM_DMABASE_EGR
4889   *            @arg TIM_DMABASE_CCMR1
4890   *            @arg TIM_DMABASE_CCMR2
4891   *            @arg TIM_DMABASE_CCER
4892   *            @arg TIM_DMABASE_CNT
4893   *            @arg TIM_DMABASE_PSC
4894   *            @arg TIM_DMABASE_ARR
4895   *            @arg TIM_DMABASE_RCR
4896   *            @arg TIM_DMABASE_CCR1
4897   *            @arg TIM_DMABASE_CCR2
4898   *            @arg TIM_DMABASE_CCR3
4899   *            @arg TIM_DMABASE_CCR4
4900   *            @arg TIM_DMABASE_BDTR
4901   *            @arg TIM_DMABASE_CCMR3
4902   *            @arg TIM_DMABASE_CCR5
4903   *            @arg TIM_DMABASE_CCR6
4904   *            @arg TIM_DMABASE_AF1
4905   *            @arg TIM_DMABASE_AF2
4906   *            @arg TIM_DMABASE_TISEL
4907   *
4908   * @param  BurstRequestSrc TIM DMA Request sources
4909   *         This parameter can be one of the following values:
4910   *            @arg TIM_DMA_UPDATE: TIM update Interrupt source
4911   *            @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4912   *            @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4913   *            @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4914   *            @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4915   *            @arg TIM_DMA_COM: TIM Commutation DMA source
4916   *            @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4917   * @param  BurstBuffer The Buffer address.
4918   * @param  BurstLength DMA Burst length. This parameter can be one value
4919   *         between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4920   * @note   This function should be used only when BurstLength is equal to DMA data transfer length.
4921   * @retval HAL status
4922   */
HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef * htim,uint32_t BurstBaseAddress,uint32_t BurstRequestSrc,uint32_t * BurstBuffer,uint32_t BurstLength)4923 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4924                                              uint32_t BurstRequestSrc, uint32_t  *BurstBuffer, uint32_t  BurstLength)
4925 {
4926   HAL_StatusTypeDef status;
4927 
4928   status = HAL_TIM_DMABurst_MultiReadStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
4929                                            ((BurstLength) >> 8U) + 1U);
4930 
4931 
4932   return status;
4933 }
4934 
4935 /**
4936   * @brief  Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
4937   * @param  htim TIM handle
4938   * @param  BurstBaseAddress TIM Base address from where the DMA  will start the Data read
4939   *         This parameter can be one of the following values:
4940   *            @arg TIM_DMABASE_CR1
4941   *            @arg TIM_DMABASE_CR2
4942   *            @arg TIM_DMABASE_SMCR
4943   *            @arg TIM_DMABASE_DIER
4944   *            @arg TIM_DMABASE_SR
4945   *            @arg TIM_DMABASE_EGR
4946   *            @arg TIM_DMABASE_CCMR1
4947   *            @arg TIM_DMABASE_CCMR2
4948   *            @arg TIM_DMABASE_CCER
4949   *            @arg TIM_DMABASE_CNT
4950   *            @arg TIM_DMABASE_PSC
4951   *            @arg TIM_DMABASE_ARR
4952   *            @arg TIM_DMABASE_RCR
4953   *            @arg TIM_DMABASE_CCR1
4954   *            @arg TIM_DMABASE_CCR2
4955   *            @arg TIM_DMABASE_CCR3
4956   *            @arg TIM_DMABASE_CCR4
4957   *            @arg TIM_DMABASE_BDTR
4958   *            @arg TIM_DMABASE_CCMR3
4959   *            @arg TIM_DMABASE_CCR5
4960   *            @arg TIM_DMABASE_CCR6
4961   *            @arg TIM_DMABASE_AF1
4962   *            @arg TIM_DMABASE_AF2
4963   *            @arg TIM_DMABASE_TISEL
4964   *
4965   * @param  BurstRequestSrc TIM DMA Request sources
4966   *         This parameter can be one of the following values:
4967   *            @arg TIM_DMA_UPDATE: TIM update Interrupt source
4968   *            @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4969   *            @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4970   *            @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4971   *            @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4972   *            @arg TIM_DMA_COM: TIM Commutation DMA source
4973   *            @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4974   * @param  BurstBuffer The Buffer address.
4975   * @param  BurstLength DMA Burst length. This parameter can be one value
4976   *         between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4977   * @param  DataLength Data length. This parameter can be one value
4978   *         between 1 and 0xFFFF.
4979   * @retval HAL status
4980   */
HAL_TIM_DMABurst_MultiReadStart(TIM_HandleTypeDef * htim,uint32_t BurstBaseAddress,uint32_t BurstRequestSrc,uint32_t * BurstBuffer,uint32_t BurstLength,uint32_t DataLength)4981 HAL_StatusTypeDef HAL_TIM_DMABurst_MultiReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4982                                                   uint32_t BurstRequestSrc, uint32_t  *BurstBuffer,
4983                                                   uint32_t  BurstLength, uint32_t  DataLength)
4984 {
4985   HAL_StatusTypeDef status = HAL_OK;
4986 
4987   /* Check the parameters */
4988   assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
4989   assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
4990   assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4991   assert_param(IS_TIM_DMA_LENGTH(BurstLength));
4992   assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
4993 
4994   if (htim->DMABurstState == HAL_DMA_BURST_STATE_BUSY)
4995   {
4996     return HAL_BUSY;
4997   }
4998   else if (htim->DMABurstState == HAL_DMA_BURST_STATE_READY)
4999   {
5000     if ((BurstBuffer == NULL) && (BurstLength > 0U))
5001     {
5002       return HAL_ERROR;
5003     }
5004     else
5005     {
5006       htim->DMABurstState = HAL_DMA_BURST_STATE_BUSY;
5007     }
5008   }
5009   else
5010   {
5011     /* nothing to do */
5012   }
5013   switch (BurstRequestSrc)
5014   {
5015     case TIM_DMA_UPDATE:
5016     {
5017       /* Set the DMA Period elapsed callbacks */
5018       htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
5019       htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
5020 
5021       /* Set the DMA error callback */
5022       htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
5023 
5024       /* Enable the DMA stream */
5025       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
5026                            DataLength) != HAL_OK)
5027       {
5028         /* Return error status */
5029         return HAL_ERROR;
5030       }
5031       break;
5032     }
5033     case TIM_DMA_CC1:
5034     {
5035       /* Set the DMA capture callbacks */
5036       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
5037       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
5038 
5039       /* Set the DMA error callback */
5040       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
5041 
5042       /* Enable the DMA stream */
5043       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
5044                            DataLength) != HAL_OK)
5045       {
5046         /* Return error status */
5047         return HAL_ERROR;
5048       }
5049       break;
5050     }
5051     case TIM_DMA_CC2:
5052     {
5053       /* Set the DMA capture callbacks */
5054       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
5055       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
5056 
5057       /* Set the DMA error callback */
5058       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
5059 
5060       /* Enable the DMA stream */
5061       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
5062                            DataLength) != HAL_OK)
5063       {
5064         /* Return error status */
5065         return HAL_ERROR;
5066       }
5067       break;
5068     }
5069     case TIM_DMA_CC3:
5070     {
5071       /* Set the DMA capture callbacks */
5072       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
5073       htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
5074 
5075       /* Set the DMA error callback */
5076       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
5077 
5078       /* Enable the DMA stream */
5079       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
5080                            DataLength) != HAL_OK)
5081       {
5082         /* Return error status */
5083         return HAL_ERROR;
5084       }
5085       break;
5086     }
5087     case TIM_DMA_CC4:
5088     {
5089       /* Set the DMA capture callbacks */
5090       htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
5091       htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
5092 
5093       /* Set the DMA error callback */
5094       htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
5095 
5096       /* Enable the DMA stream */
5097       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
5098                            DataLength) != HAL_OK)
5099       {
5100         /* Return error status */
5101         return HAL_ERROR;
5102       }
5103       break;
5104     }
5105     case TIM_DMA_COM:
5106     {
5107       /* Set the DMA commutation callbacks */
5108       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback =  TIMEx_DMACommutationCplt;
5109       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback =  TIMEx_DMACommutationHalfCplt;
5110 
5111       /* Set the DMA error callback */
5112       htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
5113 
5114       /* Enable the DMA stream */
5115       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
5116                            DataLength) != HAL_OK)
5117       {
5118         /* Return error status */
5119         return HAL_ERROR;
5120       }
5121       break;
5122     }
5123     case TIM_DMA_TRIGGER:
5124     {
5125       /* Set the DMA trigger callbacks */
5126       htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
5127       htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
5128 
5129       /* Set the DMA error callback */
5130       htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
5131 
5132       /* Enable the DMA stream */
5133       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
5134                            DataLength) != HAL_OK)
5135       {
5136         /* Return error status */
5137         return HAL_ERROR;
5138       }
5139       break;
5140     }
5141     default:
5142       status = HAL_ERROR;
5143       break;
5144   }
5145 
5146   if (status == HAL_OK)
5147   {
5148     /* Configure the DMA Burst Mode */
5149     htim->Instance->DCR = (BurstBaseAddress | BurstLength);
5150 
5151     /* Enable the TIM DMA Request */
5152     __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
5153   }
5154 
5155   /* Return function status */
5156   return status;
5157 }
5158 
5159 /**
5160   * @brief  Stop the DMA burst reading
5161   * @param  htim TIM handle
5162   * @param  BurstRequestSrc TIM DMA Request sources to disable.
5163   * @retval HAL status
5164   */
HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef * htim,uint32_t BurstRequestSrc)5165 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
5166 {
5167   HAL_StatusTypeDef status = HAL_OK;
5168 
5169   /* Check the parameters */
5170   assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
5171 
5172   /* Abort the DMA transfer (at least disable the DMA stream) */
5173   switch (BurstRequestSrc)
5174   {
5175     case TIM_DMA_UPDATE:
5176     {
5177       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
5178       break;
5179     }
5180     case TIM_DMA_CC1:
5181     {
5182       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
5183       break;
5184     }
5185     case TIM_DMA_CC2:
5186     {
5187       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
5188       break;
5189     }
5190     case TIM_DMA_CC3:
5191     {
5192       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
5193       break;
5194     }
5195     case TIM_DMA_CC4:
5196     {
5197       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
5198       break;
5199     }
5200     case TIM_DMA_COM:
5201     {
5202       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
5203       break;
5204     }
5205     case TIM_DMA_TRIGGER:
5206     {
5207       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
5208       break;
5209     }
5210     default:
5211       status = HAL_ERROR;
5212       break;
5213   }
5214 
5215   if (status == HAL_OK)
5216   {
5217     /* Disable the TIM Update DMA request */
5218     __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
5219 
5220     /* Change the DMA burst operation state */
5221     htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
5222   }
5223 
5224   /* Return function status */
5225   return status;
5226 }
5227 
5228 /**
5229   * @brief  Generate a software event
5230   * @param  htim TIM handle
5231   * @param  EventSource specifies the event source.
5232   *          This parameter can be one of the following values:
5233   *            @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
5234   *            @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
5235   *            @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
5236   *            @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
5237   *            @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
5238   *            @arg TIM_EVENTSOURCE_COM: Timer COM event source
5239   *            @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
5240   *            @arg TIM_EVENTSOURCE_BREAK: Timer Break event source
5241   *            @arg TIM_EVENTSOURCE_BREAK2: Timer Break2 event source
5242   * @note   Basic timers can only generate an update event.
5243   * @note   TIM_EVENTSOURCE_COM is relevant only with advanced timer instances.
5244   * @note   TIM_EVENTSOURCE_BREAK and TIM_EVENTSOURCE_BREAK2 are relevant
5245   *         only for timer instances supporting break input(s).
5246   * @retval HAL status
5247   */
5248 
HAL_TIM_GenerateEvent(TIM_HandleTypeDef * htim,uint32_t EventSource)5249 HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
5250 {
5251   /* Check the parameters */
5252   assert_param(IS_TIM_INSTANCE(htim->Instance));
5253   assert_param(IS_TIM_EVENT_SOURCE(EventSource));
5254 
5255   /* Process Locked */
5256   __HAL_LOCK(htim);
5257 
5258   /* Change the TIM state */
5259   htim->State = HAL_TIM_STATE_BUSY;
5260 
5261   /* Set the event sources */
5262   htim->Instance->EGR = EventSource;
5263 
5264   /* Change the TIM state */
5265   htim->State = HAL_TIM_STATE_READY;
5266 
5267   __HAL_UNLOCK(htim);
5268 
5269   /* Return function status */
5270   return HAL_OK;
5271 }
5272 
5273 /**
5274   * @brief  Configures the OCRef clear feature
5275   * @param  htim TIM handle
5276   * @param  sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
5277   *         contains the OCREF clear feature and parameters for the TIM peripheral.
5278   * @param  Channel specifies the TIM Channel
5279   *          This parameter can be one of the following values:
5280   *            @arg TIM_CHANNEL_1: TIM Channel 1
5281   *            @arg TIM_CHANNEL_2: TIM Channel 2
5282   *            @arg TIM_CHANNEL_3: TIM Channel 3
5283   *            @arg TIM_CHANNEL_4: TIM Channel 4
5284   *            @arg TIM_CHANNEL_5: TIM Channel 5
5285   *            @arg TIM_CHANNEL_6: TIM Channel 6
5286   * @retval HAL status
5287   */
HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef * htim,const TIM_ClearInputConfigTypeDef * sClearInputConfig,uint32_t Channel)5288 HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
5289                                            const TIM_ClearInputConfigTypeDef *sClearInputConfig,
5290                                            uint32_t Channel)
5291 {
5292   HAL_StatusTypeDef status = HAL_OK;
5293 
5294   /* Check the parameters */
5295   assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
5296   assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
5297 
5298   /* Process Locked */
5299   __HAL_LOCK(htim);
5300 
5301   htim->State = HAL_TIM_STATE_BUSY;
5302 
5303   switch (sClearInputConfig->ClearInputSource)
5304   {
5305     case TIM_CLEARINPUTSOURCE_NONE:
5306     {
5307       /* Clear the OCREF clear selection bit and the the ETR Bits */
5308       CLEAR_BIT(htim->Instance->SMCR, (TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP));
5309       break;
5310     }
5311 
5312     case TIM_CLEARINPUTSOURCE_ETR:
5313     {
5314       /* Check the parameters */
5315       assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
5316       assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
5317       assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
5318 
5319       /* When OCRef clear feature is used with ETR source, ETR prescaler must be off */
5320       if (sClearInputConfig->ClearInputPrescaler != TIM_CLEARINPUTPRESCALER_DIV1)
5321       {
5322         htim->State = HAL_TIM_STATE_READY;
5323         __HAL_UNLOCK(htim);
5324         return HAL_ERROR;
5325       }
5326 
5327       TIM_ETR_SetConfig(htim->Instance,
5328                         sClearInputConfig->ClearInputPrescaler,
5329                         sClearInputConfig->ClearInputPolarity,
5330                         sClearInputConfig->ClearInputFilter);
5331       break;
5332     }
5333 
5334     default:
5335       status = HAL_ERROR;
5336       break;
5337   }
5338 
5339   if (status == HAL_OK)
5340   {
5341     switch (Channel)
5342     {
5343       case TIM_CHANNEL_1:
5344       {
5345         if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5346         {
5347           /* Enable the OCREF clear feature for Channel 1 */
5348           SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
5349         }
5350         else
5351         {
5352           /* Disable the OCREF clear feature for Channel 1 */
5353           CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
5354         }
5355         break;
5356       }
5357       case TIM_CHANNEL_2:
5358       {
5359         if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5360         {
5361           /* Enable the OCREF clear feature for Channel 2 */
5362           SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
5363         }
5364         else
5365         {
5366           /* Disable the OCREF clear feature for Channel 2 */
5367           CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
5368         }
5369         break;
5370       }
5371       case TIM_CHANNEL_3:
5372       {
5373         if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5374         {
5375           /* Enable the OCREF clear feature for Channel 3 */
5376           SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
5377         }
5378         else
5379         {
5380           /* Disable the OCREF clear feature for Channel 3 */
5381           CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
5382         }
5383         break;
5384       }
5385       case TIM_CHANNEL_4:
5386       {
5387         if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5388         {
5389           /* Enable the OCREF clear feature for Channel 4 */
5390           SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
5391         }
5392         else
5393         {
5394           /* Disable the OCREF clear feature for Channel 4 */
5395           CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
5396         }
5397         break;
5398       }
5399       case TIM_CHANNEL_5:
5400       {
5401         if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5402         {
5403           /* Enable the OCREF clear feature for Channel 5 */
5404           SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
5405         }
5406         else
5407         {
5408           /* Disable the OCREF clear feature for Channel 5 */
5409           CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
5410         }
5411         break;
5412       }
5413       case TIM_CHANNEL_6:
5414       {
5415         if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5416         {
5417           /* Enable the OCREF clear feature for Channel 6 */
5418           SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
5419         }
5420         else
5421         {
5422           /* Disable the OCREF clear feature for Channel 6 */
5423           CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
5424         }
5425         break;
5426       }
5427       default:
5428         break;
5429     }
5430   }
5431 
5432   htim->State = HAL_TIM_STATE_READY;
5433 
5434   __HAL_UNLOCK(htim);
5435 
5436   return status;
5437 }
5438 
5439 /**
5440   * @brief   Configures the clock source to be used
5441   * @param  htim TIM handle
5442   * @param  sClockSourceConfig pointer to a TIM_ClockConfigTypeDef structure that
5443   *         contains the clock source information for the TIM peripheral.
5444   * @retval HAL status
5445   */
HAL_TIM_ConfigClockSource(TIM_HandleTypeDef * htim,const TIM_ClockConfigTypeDef * sClockSourceConfig)5446 HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, const TIM_ClockConfigTypeDef *sClockSourceConfig)
5447 {
5448   HAL_StatusTypeDef status = HAL_OK;
5449   uint32_t tmpsmcr;
5450 
5451   /* Process Locked */
5452   __HAL_LOCK(htim);
5453 
5454   htim->State = HAL_TIM_STATE_BUSY;
5455 
5456   /* Check the parameters */
5457   assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
5458 
5459   /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
5460   tmpsmcr = htim->Instance->SMCR;
5461   tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
5462   tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
5463   htim->Instance->SMCR = tmpsmcr;
5464 
5465   switch (sClockSourceConfig->ClockSource)
5466   {
5467     case TIM_CLOCKSOURCE_INTERNAL:
5468     {
5469       assert_param(IS_TIM_INSTANCE(htim->Instance));
5470       break;
5471     }
5472 
5473     case TIM_CLOCKSOURCE_ETRMODE1:
5474     {
5475       /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
5476       assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
5477 
5478       /* Check ETR input conditioning related parameters */
5479       assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
5480       assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5481       assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5482 
5483       /* Configure the ETR Clock source */
5484       TIM_ETR_SetConfig(htim->Instance,
5485                         sClockSourceConfig->ClockPrescaler,
5486                         sClockSourceConfig->ClockPolarity,
5487                         sClockSourceConfig->ClockFilter);
5488 
5489       /* Select the External clock mode1 and the ETRF trigger */
5490       tmpsmcr = htim->Instance->SMCR;
5491       tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
5492       /* Write to TIMx SMCR */
5493       htim->Instance->SMCR = tmpsmcr;
5494       break;
5495     }
5496 
5497     case TIM_CLOCKSOURCE_ETRMODE2:
5498     {
5499       /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
5500       assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
5501 
5502       /* Check ETR input conditioning related parameters */
5503       assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
5504       assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5505       assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5506 
5507       /* Configure the ETR Clock source */
5508       TIM_ETR_SetConfig(htim->Instance,
5509                         sClockSourceConfig->ClockPrescaler,
5510                         sClockSourceConfig->ClockPolarity,
5511                         sClockSourceConfig->ClockFilter);
5512       /* Enable the External clock mode2 */
5513       htim->Instance->SMCR |= TIM_SMCR_ECE;
5514       break;
5515     }
5516 
5517     case TIM_CLOCKSOURCE_TI1:
5518     {
5519       /* Check whether or not the timer instance supports external clock mode 1 */
5520       assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
5521 
5522       /* Check TI1 input conditioning related parameters */
5523       assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5524       assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5525 
5526       TIM_TI1_ConfigInputStage(htim->Instance,
5527                                sClockSourceConfig->ClockPolarity,
5528                                sClockSourceConfig->ClockFilter);
5529       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
5530       break;
5531     }
5532 
5533     case TIM_CLOCKSOURCE_TI2:
5534     {
5535       /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
5536       assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
5537 
5538       /* Check TI2 input conditioning related parameters */
5539       assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5540       assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5541 
5542       TIM_TI2_ConfigInputStage(htim->Instance,
5543                                sClockSourceConfig->ClockPolarity,
5544                                sClockSourceConfig->ClockFilter);
5545       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
5546       break;
5547     }
5548 
5549     case TIM_CLOCKSOURCE_TI1ED:
5550     {
5551       /* Check whether or not the timer instance supports external clock mode 1 */
5552       assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
5553 
5554       /* Check TI1 input conditioning related parameters */
5555       assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5556       assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5557 
5558       TIM_TI1_ConfigInputStage(htim->Instance,
5559                                sClockSourceConfig->ClockPolarity,
5560                                sClockSourceConfig->ClockFilter);
5561       TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
5562       break;
5563     }
5564 
5565     case TIM_CLOCKSOURCE_ITR0:
5566     case TIM_CLOCKSOURCE_ITR1:
5567     case TIM_CLOCKSOURCE_ITR2:
5568     case TIM_CLOCKSOURCE_ITR3:
5569     case TIM_CLOCKSOURCE_ITR4:
5570     case TIM_CLOCKSOURCE_ITR5:
5571     case TIM_CLOCKSOURCE_ITR6:
5572     case TIM_CLOCKSOURCE_ITR7:
5573     case TIM_CLOCKSOURCE_ITR8:
5574     {
5575       /* Check whether or not the timer instance supports internal trigger input */
5576       assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
5577 
5578       TIM_ITRx_SetConfig(htim->Instance, sClockSourceConfig->ClockSource);
5579       break;
5580     }
5581 
5582     default:
5583       status = HAL_ERROR;
5584       break;
5585   }
5586   htim->State = HAL_TIM_STATE_READY;
5587 
5588   __HAL_UNLOCK(htim);
5589 
5590   return status;
5591 }
5592 
5593 /**
5594   * @brief  Selects the signal connected to the TI1 input: direct from CH1_input
5595   *         or a XOR combination between CH1_input, CH2_input & CH3_input
5596   * @param  htim TIM handle.
5597   * @param  TI1_Selection Indicate whether or not channel 1 is connected to the
5598   *         output of a XOR gate.
5599   *          This parameter can be one of the following values:
5600   *            @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
5601   *            @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
5602   *            pins are connected to the TI1 input (XOR combination)
5603   * @retval HAL status
5604   */
HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef * htim,uint32_t TI1_Selection)5605 HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
5606 {
5607   uint32_t tmpcr2;
5608 
5609   /* Check the parameters */
5610   assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
5611   assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
5612 
5613   /* Get the TIMx CR2 register value */
5614   tmpcr2 = htim->Instance->CR2;
5615 
5616   /* Reset the TI1 selection */
5617   tmpcr2 &= ~TIM_CR2_TI1S;
5618 
5619   /* Set the TI1 selection */
5620   tmpcr2 |= TI1_Selection;
5621 
5622   /* Write to TIMxCR2 */
5623   htim->Instance->CR2 = tmpcr2;
5624 
5625   return HAL_OK;
5626 }
5627 
5628 /**
5629   * @brief  Configures the TIM in Slave mode
5630   * @param  htim TIM handle.
5631   * @param  sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
5632   *         contains the selected trigger (internal trigger input, filtered
5633   *         timer input or external trigger input) and the Slave mode
5634   *         (Disable, Reset, Gated, Trigger, External clock mode 1).
5635   * @retval HAL status
5636   */
HAL_TIM_SlaveConfigSynchro(TIM_HandleTypeDef * htim,const TIM_SlaveConfigTypeDef * sSlaveConfig)5637 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro(TIM_HandleTypeDef *htim, const TIM_SlaveConfigTypeDef *sSlaveConfig)
5638 {
5639   /* Check the parameters */
5640   assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
5641   assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
5642   assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
5643 
5644   __HAL_LOCK(htim);
5645 
5646   htim->State = HAL_TIM_STATE_BUSY;
5647 
5648   if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
5649   {
5650     htim->State = HAL_TIM_STATE_READY;
5651     __HAL_UNLOCK(htim);
5652     return HAL_ERROR;
5653   }
5654 
5655   /* Disable Trigger Interrupt */
5656   __HAL_TIM_DISABLE_IT(htim, TIM_IT_TRIGGER);
5657 
5658   /* Disable Trigger DMA request */
5659   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
5660 
5661   htim->State = HAL_TIM_STATE_READY;
5662 
5663   __HAL_UNLOCK(htim);
5664 
5665   return HAL_OK;
5666 }
5667 
5668 /**
5669   * @brief  Configures the TIM in Slave mode in interrupt mode
5670   * @param  htim TIM handle.
5671   * @param  sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
5672   *         contains the selected trigger (internal trigger input, filtered
5673   *         timer input or external trigger input) and the Slave mode
5674   *         (Disable, Reset, Gated, Trigger, External clock mode 1).
5675   * @retval HAL status
5676   */
HAL_TIM_SlaveConfigSynchro_IT(TIM_HandleTypeDef * htim,const TIM_SlaveConfigTypeDef * sSlaveConfig)5677 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro_IT(TIM_HandleTypeDef *htim,
5678                                                 const TIM_SlaveConfigTypeDef *sSlaveConfig)
5679 {
5680   /* Check the parameters */
5681   assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
5682   assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
5683   assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
5684 
5685   __HAL_LOCK(htim);
5686 
5687   htim->State = HAL_TIM_STATE_BUSY;
5688 
5689   if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
5690   {
5691     htim->State = HAL_TIM_STATE_READY;
5692     __HAL_UNLOCK(htim);
5693     return HAL_ERROR;
5694   }
5695 
5696   /* Enable Trigger Interrupt */
5697   __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);
5698 
5699   /* Disable Trigger DMA request */
5700   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
5701 
5702   htim->State = HAL_TIM_STATE_READY;
5703 
5704   __HAL_UNLOCK(htim);
5705 
5706   return HAL_OK;
5707 }
5708 
5709 /**
5710   * @brief  Read the captured value from Capture Compare unit
5711   * @param  htim TIM handle.
5712   * @param  Channel TIM Channels to be enabled
5713   *          This parameter can be one of the following values:
5714   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
5715   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
5716   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
5717   *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
5718   * @retval Captured value
5719   */
HAL_TIM_ReadCapturedValue(const TIM_HandleTypeDef * htim,uint32_t Channel)5720 uint32_t HAL_TIM_ReadCapturedValue(const TIM_HandleTypeDef *htim, uint32_t Channel)
5721 {
5722   uint32_t tmpreg = 0U;
5723 
5724   switch (Channel)
5725   {
5726     case TIM_CHANNEL_1:
5727     {
5728       /* Check the parameters */
5729       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
5730 
5731       /* Return the capture 1 value */
5732       tmpreg =  htim->Instance->CCR1;
5733 
5734       break;
5735     }
5736     case TIM_CHANNEL_2:
5737     {
5738       /* Check the parameters */
5739       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
5740 
5741       /* Return the capture 2 value */
5742       tmpreg =   htim->Instance->CCR2;
5743 
5744       break;
5745     }
5746 
5747     case TIM_CHANNEL_3:
5748     {
5749       /* Check the parameters */
5750       assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
5751 
5752       /* Return the capture 3 value */
5753       tmpreg =   htim->Instance->CCR3;
5754 
5755       break;
5756     }
5757 
5758     case TIM_CHANNEL_4:
5759     {
5760       /* Check the parameters */
5761       assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
5762 
5763       /* Return the capture 4 value */
5764       tmpreg =   htim->Instance->CCR4;
5765 
5766       break;
5767     }
5768 
5769     default:
5770       break;
5771   }
5772 
5773   return tmpreg;
5774 }
5775 
5776 /**
5777   * @}
5778   */
5779 
5780 /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
5781   *  @brief    TIM Callbacks functions
5782   *
5783 @verbatim
5784   ==============================================================================
5785                         ##### TIM Callbacks functions #####
5786   ==============================================================================
5787  [..]
5788    This section provides TIM callback functions:
5789    (+) TIM Period elapsed callback
5790    (+) TIM Output Compare callback
5791    (+) TIM Input capture callback
5792    (+) TIM Trigger callback
5793    (+) TIM Error callback
5794 
5795 @endverbatim
5796   * @{
5797   */
5798 
5799 /**
5800   * @brief  Period elapsed callback in non-blocking mode
5801   * @param  htim TIM handle
5802   * @retval None
5803   */
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)5804 __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
5805 {
5806   /* Prevent unused argument(s) compilation warning */
5807   UNUSED(htim);
5808 
5809   /* NOTE : This function should not be modified, when the callback is needed,
5810             the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
5811    */
5812 }
5813 
5814 /**
5815   * @brief  Period elapsed half complete callback in non-blocking mode
5816   * @param  htim TIM handle
5817   * @retval None
5818   */
HAL_TIM_PeriodElapsedHalfCpltCallback(TIM_HandleTypeDef * htim)5819 __weak void HAL_TIM_PeriodElapsedHalfCpltCallback(TIM_HandleTypeDef *htim)
5820 {
5821   /* Prevent unused argument(s) compilation warning */
5822   UNUSED(htim);
5823 
5824   /* NOTE : This function should not be modified, when the callback is needed,
5825             the HAL_TIM_PeriodElapsedHalfCpltCallback could be implemented in the user file
5826    */
5827 }
5828 
5829 /**
5830   * @brief  Output Compare callback in non-blocking mode
5831   * @param  htim TIM OC handle
5832   * @retval None
5833   */
HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef * htim)5834 __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
5835 {
5836   /* Prevent unused argument(s) compilation warning */
5837   UNUSED(htim);
5838 
5839   /* NOTE : This function should not be modified, when the callback is needed,
5840             the HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
5841    */
5842 }
5843 
5844 /**
5845   * @brief  Input Capture callback in non-blocking mode
5846   * @param  htim TIM IC handle
5847   * @retval None
5848   */
HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef * htim)5849 __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
5850 {
5851   /* Prevent unused argument(s) compilation warning */
5852   UNUSED(htim);
5853 
5854   /* NOTE : This function should not be modified, when the callback is needed,
5855             the HAL_TIM_IC_CaptureCallback could be implemented in the user file
5856    */
5857 }
5858 
5859 /**
5860   * @brief  Input Capture half complete callback in non-blocking mode
5861   * @param  htim TIM IC handle
5862   * @retval None
5863   */
HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef * htim)5864 __weak void HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef *htim)
5865 {
5866   /* Prevent unused argument(s) compilation warning */
5867   UNUSED(htim);
5868 
5869   /* NOTE : This function should not be modified, when the callback is needed,
5870             the HAL_TIM_IC_CaptureHalfCpltCallback could be implemented in the user file
5871    */
5872 }
5873 
5874 /**
5875   * @brief  PWM Pulse finished callback in non-blocking mode
5876   * @param  htim TIM handle
5877   * @retval None
5878   */
HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef * htim)5879 __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
5880 {
5881   /* Prevent unused argument(s) compilation warning */
5882   UNUSED(htim);
5883 
5884   /* NOTE : This function should not be modified, when the callback is needed,
5885             the HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
5886    */
5887 }
5888 
5889 /**
5890   * @brief  PWM Pulse finished half complete callback in non-blocking mode
5891   * @param  htim TIM handle
5892   * @retval None
5893   */
HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef * htim)5894 __weak void HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef *htim)
5895 {
5896   /* Prevent unused argument(s) compilation warning */
5897   UNUSED(htim);
5898 
5899   /* NOTE : This function should not be modified, when the callback is needed,
5900             the HAL_TIM_PWM_PulseFinishedHalfCpltCallback could be implemented in the user file
5901    */
5902 }
5903 
5904 /**
5905   * @brief  Hall Trigger detection callback in non-blocking mode
5906   * @param  htim TIM handle
5907   * @retval None
5908   */
HAL_TIM_TriggerCallback(TIM_HandleTypeDef * htim)5909 __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
5910 {
5911   /* Prevent unused argument(s) compilation warning */
5912   UNUSED(htim);
5913 
5914   /* NOTE : This function should not be modified, when the callback is needed,
5915             the HAL_TIM_TriggerCallback could be implemented in the user file
5916    */
5917 }
5918 
5919 /**
5920   * @brief  Hall Trigger detection half complete callback in non-blocking mode
5921   * @param  htim TIM handle
5922   * @retval None
5923   */
HAL_TIM_TriggerHalfCpltCallback(TIM_HandleTypeDef * htim)5924 __weak void HAL_TIM_TriggerHalfCpltCallback(TIM_HandleTypeDef *htim)
5925 {
5926   /* Prevent unused argument(s) compilation warning */
5927   UNUSED(htim);
5928 
5929   /* NOTE : This function should not be modified, when the callback is needed,
5930             the HAL_TIM_TriggerHalfCpltCallback could be implemented in the user file
5931    */
5932 }
5933 
5934 /**
5935   * @brief  Timer error callback in non-blocking mode
5936   * @param  htim TIM handle
5937   * @retval None
5938   */
HAL_TIM_ErrorCallback(TIM_HandleTypeDef * htim)5939 __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
5940 {
5941   /* Prevent unused argument(s) compilation warning */
5942   UNUSED(htim);
5943 
5944   /* NOTE : This function should not be modified, when the callback is needed,
5945             the HAL_TIM_ErrorCallback could be implemented in the user file
5946    */
5947 }
5948 
5949 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
5950 /**
5951   * @brief  Register a User TIM callback to be used instead of the weak predefined callback
5952   * @param htim tim handle
5953   * @param CallbackID ID of the callback to be registered
5954   *        This parameter can be one of the following values:
5955   *          @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
5956   *          @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
5957   *          @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
5958   *          @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
5959   *          @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
5960   *          @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
5961   *          @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
5962   *          @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
5963   *          @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
5964   *          @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
5965   *          @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
5966   *          @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
5967   *          @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
5968   *          @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
5969   *          @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
5970   *          @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
5971   *          @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
5972   *          @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
5973   *          @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
5974   *          @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
5975   *          @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
5976   *          @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
5977   *          @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
5978   *          @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
5979   *          @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
5980   *          @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
5981   *          @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
5982   *          @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
5983   *          @param pCallback pointer to the callback function
5984   *          @retval status
5985   */
HAL_TIM_RegisterCallback(TIM_HandleTypeDef * htim,HAL_TIM_CallbackIDTypeDef CallbackID,pTIM_CallbackTypeDef pCallback)5986 HAL_StatusTypeDef HAL_TIM_RegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID,
5987                                            pTIM_CallbackTypeDef pCallback)
5988 {
5989   HAL_StatusTypeDef status = HAL_OK;
5990 
5991   if (pCallback == NULL)
5992   {
5993     return HAL_ERROR;
5994   }
5995 
5996   if (htim->State == HAL_TIM_STATE_READY)
5997   {
5998     switch (CallbackID)
5999     {
6000       case HAL_TIM_BASE_MSPINIT_CB_ID :
6001         htim->Base_MspInitCallback                 = pCallback;
6002         break;
6003 
6004       case HAL_TIM_BASE_MSPDEINIT_CB_ID :
6005         htim->Base_MspDeInitCallback               = pCallback;
6006         break;
6007 
6008       case HAL_TIM_IC_MSPINIT_CB_ID :
6009         htim->IC_MspInitCallback                   = pCallback;
6010         break;
6011 
6012       case HAL_TIM_IC_MSPDEINIT_CB_ID :
6013         htim->IC_MspDeInitCallback                 = pCallback;
6014         break;
6015 
6016       case HAL_TIM_OC_MSPINIT_CB_ID :
6017         htim->OC_MspInitCallback                   = pCallback;
6018         break;
6019 
6020       case HAL_TIM_OC_MSPDEINIT_CB_ID :
6021         htim->OC_MspDeInitCallback                 = pCallback;
6022         break;
6023 
6024       case HAL_TIM_PWM_MSPINIT_CB_ID :
6025         htim->PWM_MspInitCallback                  = pCallback;
6026         break;
6027 
6028       case HAL_TIM_PWM_MSPDEINIT_CB_ID :
6029         htim->PWM_MspDeInitCallback                = pCallback;
6030         break;
6031 
6032       case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
6033         htim->OnePulse_MspInitCallback             = pCallback;
6034         break;
6035 
6036       case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
6037         htim->OnePulse_MspDeInitCallback           = pCallback;
6038         break;
6039 
6040       case HAL_TIM_ENCODER_MSPINIT_CB_ID :
6041         htim->Encoder_MspInitCallback              = pCallback;
6042         break;
6043 
6044       case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
6045         htim->Encoder_MspDeInitCallback            = pCallback;
6046         break;
6047 
6048       case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
6049         htim->HallSensor_MspInitCallback           = pCallback;
6050         break;
6051 
6052       case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
6053         htim->HallSensor_MspDeInitCallback         = pCallback;
6054         break;
6055 
6056       case HAL_TIM_PERIOD_ELAPSED_CB_ID :
6057         htim->PeriodElapsedCallback                = pCallback;
6058         break;
6059 
6060       case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
6061         htim->PeriodElapsedHalfCpltCallback        = pCallback;
6062         break;
6063 
6064       case HAL_TIM_TRIGGER_CB_ID :
6065         htim->TriggerCallback                      = pCallback;
6066         break;
6067 
6068       case HAL_TIM_TRIGGER_HALF_CB_ID :
6069         htim->TriggerHalfCpltCallback              = pCallback;
6070         break;
6071 
6072       case HAL_TIM_IC_CAPTURE_CB_ID :
6073         htim->IC_CaptureCallback                   = pCallback;
6074         break;
6075 
6076       case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
6077         htim->IC_CaptureHalfCpltCallback           = pCallback;
6078         break;
6079 
6080       case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
6081         htim->OC_DelayElapsedCallback              = pCallback;
6082         break;
6083 
6084       case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
6085         htim->PWM_PulseFinishedCallback            = pCallback;
6086         break;
6087 
6088       case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
6089         htim->PWM_PulseFinishedHalfCpltCallback    = pCallback;
6090         break;
6091 
6092       case HAL_TIM_ERROR_CB_ID :
6093         htim->ErrorCallback                        = pCallback;
6094         break;
6095 
6096       case HAL_TIM_COMMUTATION_CB_ID :
6097         htim->CommutationCallback                  = pCallback;
6098         break;
6099 
6100       case HAL_TIM_COMMUTATION_HALF_CB_ID :
6101         htim->CommutationHalfCpltCallback          = pCallback;
6102         break;
6103 
6104       case HAL_TIM_BREAK_CB_ID :
6105         htim->BreakCallback                        = pCallback;
6106         break;
6107 
6108       case HAL_TIM_BREAK2_CB_ID :
6109         htim->Break2Callback                       = pCallback;
6110         break;
6111 
6112       default :
6113         /* Return error status */
6114         status = HAL_ERROR;
6115         break;
6116     }
6117   }
6118   else if (htim->State == HAL_TIM_STATE_RESET)
6119   {
6120     switch (CallbackID)
6121     {
6122       case HAL_TIM_BASE_MSPINIT_CB_ID :
6123         htim->Base_MspInitCallback         = pCallback;
6124         break;
6125 
6126       case HAL_TIM_BASE_MSPDEINIT_CB_ID :
6127         htim->Base_MspDeInitCallback       = pCallback;
6128         break;
6129 
6130       case HAL_TIM_IC_MSPINIT_CB_ID :
6131         htim->IC_MspInitCallback           = pCallback;
6132         break;
6133 
6134       case HAL_TIM_IC_MSPDEINIT_CB_ID :
6135         htim->IC_MspDeInitCallback         = pCallback;
6136         break;
6137 
6138       case HAL_TIM_OC_MSPINIT_CB_ID :
6139         htim->OC_MspInitCallback           = pCallback;
6140         break;
6141 
6142       case HAL_TIM_OC_MSPDEINIT_CB_ID :
6143         htim->OC_MspDeInitCallback         = pCallback;
6144         break;
6145 
6146       case HAL_TIM_PWM_MSPINIT_CB_ID :
6147         htim->PWM_MspInitCallback          = pCallback;
6148         break;
6149 
6150       case HAL_TIM_PWM_MSPDEINIT_CB_ID :
6151         htim->PWM_MspDeInitCallback        = pCallback;
6152         break;
6153 
6154       case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
6155         htim->OnePulse_MspInitCallback     = pCallback;
6156         break;
6157 
6158       case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
6159         htim->OnePulse_MspDeInitCallback   = pCallback;
6160         break;
6161 
6162       case HAL_TIM_ENCODER_MSPINIT_CB_ID :
6163         htim->Encoder_MspInitCallback      = pCallback;
6164         break;
6165 
6166       case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
6167         htim->Encoder_MspDeInitCallback    = pCallback;
6168         break;
6169 
6170       case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
6171         htim->HallSensor_MspInitCallback   = pCallback;
6172         break;
6173 
6174       case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
6175         htim->HallSensor_MspDeInitCallback = pCallback;
6176         break;
6177 
6178       default :
6179         /* Return error status */
6180         status = HAL_ERROR;
6181         break;
6182     }
6183   }
6184   else
6185   {
6186     /* Return error status */
6187     status = HAL_ERROR;
6188   }
6189 
6190   return status;
6191 }
6192 
6193 /**
6194   * @brief  Unregister a TIM callback
6195   *         TIM callback is redirected to the weak predefined callback
6196   * @param htim tim handle
6197   * @param CallbackID ID of the callback to be unregistered
6198   *        This parameter can be one of the following values:
6199   *          @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
6200   *          @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
6201   *          @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
6202   *          @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
6203   *          @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
6204   *          @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
6205   *          @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
6206   *          @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
6207   *          @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
6208   *          @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
6209   *          @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
6210   *          @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
6211   *          @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
6212   *          @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
6213   *          @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
6214   *          @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
6215   *          @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
6216   *          @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
6217   *          @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
6218   *          @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
6219   *          @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
6220   *          @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
6221   *          @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
6222   *          @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
6223   *          @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
6224   *          @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
6225   *          @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
6226   *          @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
6227   *          @retval status
6228   */
HAL_TIM_UnRegisterCallback(TIM_HandleTypeDef * htim,HAL_TIM_CallbackIDTypeDef CallbackID)6229 HAL_StatusTypeDef HAL_TIM_UnRegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID)
6230 {
6231   HAL_StatusTypeDef status = HAL_OK;
6232 
6233   if (htim->State == HAL_TIM_STATE_READY)
6234   {
6235     switch (CallbackID)
6236     {
6237       case HAL_TIM_BASE_MSPINIT_CB_ID :
6238         /* Legacy weak Base MspInit Callback */
6239         htim->Base_MspInitCallback              = HAL_TIM_Base_MspInit;
6240         break;
6241 
6242       case HAL_TIM_BASE_MSPDEINIT_CB_ID :
6243         /* Legacy weak Base Msp DeInit Callback */
6244         htim->Base_MspDeInitCallback            = HAL_TIM_Base_MspDeInit;
6245         break;
6246 
6247       case HAL_TIM_IC_MSPINIT_CB_ID :
6248         /* Legacy weak IC Msp Init Callback */
6249         htim->IC_MspInitCallback                = HAL_TIM_IC_MspInit;
6250         break;
6251 
6252       case HAL_TIM_IC_MSPDEINIT_CB_ID :
6253         /* Legacy weak IC Msp DeInit Callback */
6254         htim->IC_MspDeInitCallback              = HAL_TIM_IC_MspDeInit;
6255         break;
6256 
6257       case HAL_TIM_OC_MSPINIT_CB_ID :
6258         /* Legacy weak OC Msp Init Callback */
6259         htim->OC_MspInitCallback                = HAL_TIM_OC_MspInit;
6260         break;
6261 
6262       case HAL_TIM_OC_MSPDEINIT_CB_ID :
6263         /* Legacy weak OC Msp DeInit Callback */
6264         htim->OC_MspDeInitCallback              = HAL_TIM_OC_MspDeInit;
6265         break;
6266 
6267       case HAL_TIM_PWM_MSPINIT_CB_ID :
6268         /* Legacy weak PWM Msp Init Callback */
6269         htim->PWM_MspInitCallback               = HAL_TIM_PWM_MspInit;
6270         break;
6271 
6272       case HAL_TIM_PWM_MSPDEINIT_CB_ID :
6273         /* Legacy weak PWM Msp DeInit Callback */
6274         htim->PWM_MspDeInitCallback             = HAL_TIM_PWM_MspDeInit;
6275         break;
6276 
6277       case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
6278         /* Legacy weak One Pulse Msp Init Callback */
6279         htim->OnePulse_MspInitCallback          = HAL_TIM_OnePulse_MspInit;
6280         break;
6281 
6282       case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
6283         /* Legacy weak One Pulse Msp DeInit Callback */
6284         htim->OnePulse_MspDeInitCallback        = HAL_TIM_OnePulse_MspDeInit;
6285         break;
6286 
6287       case HAL_TIM_ENCODER_MSPINIT_CB_ID :
6288         /* Legacy weak Encoder Msp Init Callback */
6289         htim->Encoder_MspInitCallback           = HAL_TIM_Encoder_MspInit;
6290         break;
6291 
6292       case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
6293         /* Legacy weak Encoder Msp DeInit Callback */
6294         htim->Encoder_MspDeInitCallback         = HAL_TIM_Encoder_MspDeInit;
6295         break;
6296 
6297       case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
6298         /* Legacy weak Hall Sensor Msp Init Callback */
6299         htim->HallSensor_MspInitCallback        = HAL_TIMEx_HallSensor_MspInit;
6300         break;
6301 
6302       case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
6303         /* Legacy weak Hall Sensor Msp DeInit Callback */
6304         htim->HallSensor_MspDeInitCallback      = HAL_TIMEx_HallSensor_MspDeInit;
6305         break;
6306 
6307       case HAL_TIM_PERIOD_ELAPSED_CB_ID :
6308         /* Legacy weak Period Elapsed Callback */
6309         htim->PeriodElapsedCallback             = HAL_TIM_PeriodElapsedCallback;
6310         break;
6311 
6312       case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
6313         /* Legacy weak Period Elapsed half complete Callback */
6314         htim->PeriodElapsedHalfCpltCallback     = HAL_TIM_PeriodElapsedHalfCpltCallback;
6315         break;
6316 
6317       case HAL_TIM_TRIGGER_CB_ID :
6318         /* Legacy weak Trigger Callback */
6319         htim->TriggerCallback                   = HAL_TIM_TriggerCallback;
6320         break;
6321 
6322       case HAL_TIM_TRIGGER_HALF_CB_ID :
6323         /* Legacy weak Trigger half complete Callback */
6324         htim->TriggerHalfCpltCallback           = HAL_TIM_TriggerHalfCpltCallback;
6325         break;
6326 
6327       case HAL_TIM_IC_CAPTURE_CB_ID :
6328         /* Legacy weak IC Capture Callback */
6329         htim->IC_CaptureCallback                = HAL_TIM_IC_CaptureCallback;
6330         break;
6331 
6332       case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
6333         /* Legacy weak IC Capture half complete Callback */
6334         htim->IC_CaptureHalfCpltCallback        = HAL_TIM_IC_CaptureHalfCpltCallback;
6335         break;
6336 
6337       case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
6338         /* Legacy weak OC Delay Elapsed Callback */
6339         htim->OC_DelayElapsedCallback           = HAL_TIM_OC_DelayElapsedCallback;
6340         break;
6341 
6342       case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
6343         /* Legacy weak PWM Pulse Finished Callback */
6344         htim->PWM_PulseFinishedCallback         = HAL_TIM_PWM_PulseFinishedCallback;
6345         break;
6346 
6347       case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
6348         /* Legacy weak PWM Pulse Finished half complete Callback */
6349         htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback;
6350         break;
6351 
6352       case HAL_TIM_ERROR_CB_ID :
6353         /* Legacy weak Error Callback */
6354         htim->ErrorCallback                     = HAL_TIM_ErrorCallback;
6355         break;
6356 
6357       case HAL_TIM_COMMUTATION_CB_ID :
6358         /* Legacy weak Commutation Callback */
6359         htim->CommutationCallback               = HAL_TIMEx_CommutCallback;
6360         break;
6361 
6362       case HAL_TIM_COMMUTATION_HALF_CB_ID :
6363         /* Legacy weak Commutation half complete Callback */
6364         htim->CommutationHalfCpltCallback       = HAL_TIMEx_CommutHalfCpltCallback;
6365         break;
6366 
6367       case HAL_TIM_BREAK_CB_ID :
6368         /* Legacy weak Break Callback */
6369         htim->BreakCallback                     = HAL_TIMEx_BreakCallback;
6370         break;
6371 
6372       case HAL_TIM_BREAK2_CB_ID :
6373         /* Legacy weak Break2 Callback */
6374         htim->Break2Callback                    = HAL_TIMEx_Break2Callback;
6375         break;
6376 
6377       default :
6378         /* Return error status */
6379         status = HAL_ERROR;
6380         break;
6381     }
6382   }
6383   else if (htim->State == HAL_TIM_STATE_RESET)
6384   {
6385     switch (CallbackID)
6386     {
6387       case HAL_TIM_BASE_MSPINIT_CB_ID :
6388         /* Legacy weak Base MspInit Callback */
6389         htim->Base_MspInitCallback         = HAL_TIM_Base_MspInit;
6390         break;
6391 
6392       case HAL_TIM_BASE_MSPDEINIT_CB_ID :
6393         /* Legacy weak Base Msp DeInit Callback */
6394         htim->Base_MspDeInitCallback       = HAL_TIM_Base_MspDeInit;
6395         break;
6396 
6397       case HAL_TIM_IC_MSPINIT_CB_ID :
6398         /* Legacy weak IC Msp Init Callback */
6399         htim->IC_MspInitCallback           = HAL_TIM_IC_MspInit;
6400         break;
6401 
6402       case HAL_TIM_IC_MSPDEINIT_CB_ID :
6403         /* Legacy weak IC Msp DeInit Callback */
6404         htim->IC_MspDeInitCallback         = HAL_TIM_IC_MspDeInit;
6405         break;
6406 
6407       case HAL_TIM_OC_MSPINIT_CB_ID :
6408         /* Legacy weak OC Msp Init Callback */
6409         htim->OC_MspInitCallback           = HAL_TIM_OC_MspInit;
6410         break;
6411 
6412       case HAL_TIM_OC_MSPDEINIT_CB_ID :
6413         /* Legacy weak OC Msp DeInit Callback */
6414         htim->OC_MspDeInitCallback         = HAL_TIM_OC_MspDeInit;
6415         break;
6416 
6417       case HAL_TIM_PWM_MSPINIT_CB_ID :
6418         /* Legacy weak PWM Msp Init Callback */
6419         htim->PWM_MspInitCallback          = HAL_TIM_PWM_MspInit;
6420         break;
6421 
6422       case HAL_TIM_PWM_MSPDEINIT_CB_ID :
6423         /* Legacy weak PWM Msp DeInit Callback */
6424         htim->PWM_MspDeInitCallback        = HAL_TIM_PWM_MspDeInit;
6425         break;
6426 
6427       case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
6428         /* Legacy weak One Pulse Msp Init Callback */
6429         htim->OnePulse_MspInitCallback     = HAL_TIM_OnePulse_MspInit;
6430         break;
6431 
6432       case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
6433         /* Legacy weak One Pulse Msp DeInit Callback */
6434         htim->OnePulse_MspDeInitCallback   = HAL_TIM_OnePulse_MspDeInit;
6435         break;
6436 
6437       case HAL_TIM_ENCODER_MSPINIT_CB_ID :
6438         /* Legacy weak Encoder Msp Init Callback */
6439         htim->Encoder_MspInitCallback      = HAL_TIM_Encoder_MspInit;
6440         break;
6441 
6442       case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
6443         /* Legacy weak Encoder Msp DeInit Callback */
6444         htim->Encoder_MspDeInitCallback    = HAL_TIM_Encoder_MspDeInit;
6445         break;
6446 
6447       case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
6448         /* Legacy weak Hall Sensor Msp Init Callback */
6449         htim->HallSensor_MspInitCallback   = HAL_TIMEx_HallSensor_MspInit;
6450         break;
6451 
6452       case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
6453         /* Legacy weak Hall Sensor Msp DeInit Callback */
6454         htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit;
6455         break;
6456 
6457       default :
6458         /* Return error status */
6459         status = HAL_ERROR;
6460         break;
6461     }
6462   }
6463   else
6464   {
6465     /* Return error status */
6466     status = HAL_ERROR;
6467   }
6468 
6469   return status;
6470 }
6471 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6472 
6473 /**
6474   * @}
6475   */
6476 
6477 /** @defgroup TIM_Exported_Functions_Group10 TIM Peripheral State functions
6478   *  @brief   TIM Peripheral State functions
6479   *
6480 @verbatim
6481   ==============================================================================
6482                         ##### Peripheral State functions #####
6483   ==============================================================================
6484     [..]
6485     This subsection permits to get in run-time the status of the peripheral
6486     and the data flow.
6487 
6488 @endverbatim
6489   * @{
6490   */
6491 
6492 /**
6493   * @brief  Return the TIM Base handle state.
6494   * @param  htim TIM Base handle
6495   * @retval HAL state
6496   */
HAL_TIM_Base_GetState(const TIM_HandleTypeDef * htim)6497 HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(const TIM_HandleTypeDef *htim)
6498 {
6499   return htim->State;
6500 }
6501 
6502 /**
6503   * @brief  Return the TIM OC handle state.
6504   * @param  htim TIM Output Compare handle
6505   * @retval HAL state
6506   */
HAL_TIM_OC_GetState(const TIM_HandleTypeDef * htim)6507 HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(const TIM_HandleTypeDef *htim)
6508 {
6509   return htim->State;
6510 }
6511 
6512 /**
6513   * @brief  Return the TIM PWM handle state.
6514   * @param  htim TIM handle
6515   * @retval HAL state
6516   */
HAL_TIM_PWM_GetState(const TIM_HandleTypeDef * htim)6517 HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(const TIM_HandleTypeDef *htim)
6518 {
6519   return htim->State;
6520 }
6521 
6522 /**
6523   * @brief  Return the TIM Input Capture handle state.
6524   * @param  htim TIM IC handle
6525   * @retval HAL state
6526   */
HAL_TIM_IC_GetState(const TIM_HandleTypeDef * htim)6527 HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(const TIM_HandleTypeDef *htim)
6528 {
6529   return htim->State;
6530 }
6531 
6532 /**
6533   * @brief  Return the TIM One Pulse Mode handle state.
6534   * @param  htim TIM OPM handle
6535   * @retval HAL state
6536   */
HAL_TIM_OnePulse_GetState(const TIM_HandleTypeDef * htim)6537 HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(const TIM_HandleTypeDef *htim)
6538 {
6539   return htim->State;
6540 }
6541 
6542 /**
6543   * @brief  Return the TIM Encoder Mode handle state.
6544   * @param  htim TIM Encoder Interface handle
6545   * @retval HAL state
6546   */
HAL_TIM_Encoder_GetState(const TIM_HandleTypeDef * htim)6547 HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(const TIM_HandleTypeDef *htim)
6548 {
6549   return htim->State;
6550 }
6551 
6552 /**
6553   * @brief  Return the TIM Encoder Mode handle state.
6554   * @param  htim TIM handle
6555   * @retval Active channel
6556   */
HAL_TIM_GetActiveChannel(const TIM_HandleTypeDef * htim)6557 HAL_TIM_ActiveChannel HAL_TIM_GetActiveChannel(const TIM_HandleTypeDef *htim)
6558 {
6559   return htim->Channel;
6560 }
6561 
6562 /**
6563   * @brief  Return actual state of the TIM channel.
6564   * @param  htim TIM handle
6565   * @param  Channel TIM Channel
6566   *          This parameter can be one of the following values:
6567   *            @arg TIM_CHANNEL_1: TIM Channel 1
6568   *            @arg TIM_CHANNEL_2: TIM Channel 2
6569   *            @arg TIM_CHANNEL_3: TIM Channel 3
6570   *            @arg TIM_CHANNEL_4: TIM Channel 4
6571   *            @arg TIM_CHANNEL_5: TIM Channel 5
6572   *            @arg TIM_CHANNEL_6: TIM Channel 6
6573   * @retval TIM Channel state
6574   */
HAL_TIM_GetChannelState(const TIM_HandleTypeDef * htim,uint32_t Channel)6575 HAL_TIM_ChannelStateTypeDef HAL_TIM_GetChannelState(const TIM_HandleTypeDef *htim,  uint32_t Channel)
6576 {
6577   HAL_TIM_ChannelStateTypeDef channel_state;
6578 
6579   /* Check the parameters */
6580   assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
6581 
6582   channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
6583 
6584   return channel_state;
6585 }
6586 
6587 /**
6588   * @brief  Return actual state of a DMA burst operation.
6589   * @param  htim TIM handle
6590   * @retval DMA burst state
6591   */
HAL_TIM_DMABurstState(const TIM_HandleTypeDef * htim)6592 HAL_TIM_DMABurstStateTypeDef HAL_TIM_DMABurstState(const TIM_HandleTypeDef *htim)
6593 {
6594   /* Check the parameters */
6595   assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
6596 
6597   return htim->DMABurstState;
6598 }
6599 
6600 /**
6601   * @}
6602   */
6603 
6604 /**
6605   * @}
6606   */
6607 
6608 /** @defgroup TIM_Private_Functions TIM Private Functions
6609   * @{
6610   */
6611 
6612 /**
6613   * @brief  TIM DMA error callback
6614   * @param  hdma pointer to DMA handle.
6615   * @retval None
6616   */
TIM_DMAError(DMA_HandleTypeDef * hdma)6617 void TIM_DMAError(DMA_HandleTypeDef *hdma)
6618 {
6619   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6620 
6621   if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6622   {
6623     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6624     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
6625   }
6626   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6627   {
6628     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6629     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
6630   }
6631   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6632   {
6633     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6634     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
6635   }
6636   else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6637   {
6638     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6639     TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
6640   }
6641   else
6642   {
6643     htim->State = HAL_TIM_STATE_READY;
6644   }
6645 
6646 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6647   htim->ErrorCallback(htim);
6648 #else
6649   HAL_TIM_ErrorCallback(htim);
6650 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6651 
6652   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6653 }
6654 
6655 /**
6656   * @brief  TIM DMA Delay Pulse complete callback.
6657   * @param  hdma pointer to DMA handle.
6658   * @retval None
6659   */
TIM_DMADelayPulseCplt(DMA_HandleTypeDef * hdma)6660 static void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
6661 {
6662   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6663 
6664   if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6665   {
6666     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6667 
6668     if (hdma->Init.Mode == DMA_NORMAL)
6669     {
6670       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
6671     }
6672   }
6673   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6674   {
6675     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6676 
6677     if (hdma->Init.Mode == DMA_NORMAL)
6678     {
6679       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
6680     }
6681   }
6682   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6683   {
6684     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6685 
6686     if (hdma->Init.Mode == DMA_NORMAL)
6687     {
6688       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
6689     }
6690   }
6691   else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6692   {
6693     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6694 
6695     if (hdma->Init.Mode == DMA_NORMAL)
6696     {
6697       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
6698     }
6699   }
6700   else
6701   {
6702     /* nothing to do */
6703   }
6704 
6705 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6706   htim->PWM_PulseFinishedCallback(htim);
6707 #else
6708   HAL_TIM_PWM_PulseFinishedCallback(htim);
6709 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6710 
6711   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6712 }
6713 
6714 /**
6715   * @brief  TIM DMA Delay Pulse half complete callback.
6716   * @param  hdma pointer to DMA handle.
6717   * @retval None
6718   */
TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef * hdma)6719 void TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef *hdma)
6720 {
6721   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6722 
6723   if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6724   {
6725     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6726   }
6727   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6728   {
6729     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6730   }
6731   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6732   {
6733     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6734   }
6735   else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6736   {
6737     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6738   }
6739   else
6740   {
6741     /* nothing to do */
6742   }
6743 
6744 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6745   htim->PWM_PulseFinishedHalfCpltCallback(htim);
6746 #else
6747   HAL_TIM_PWM_PulseFinishedHalfCpltCallback(htim);
6748 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6749 
6750   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6751 }
6752 
6753 /**
6754   * @brief  TIM DMA Capture complete callback.
6755   * @param  hdma pointer to DMA handle.
6756   * @retval None
6757   */
TIM_DMACaptureCplt(DMA_HandleTypeDef * hdma)6758 void TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
6759 {
6760   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6761 
6762   if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6763   {
6764     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6765 
6766     if (hdma->Init.Mode == DMA_NORMAL)
6767     {
6768       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
6769       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
6770     }
6771   }
6772   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6773   {
6774     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6775 
6776     if (hdma->Init.Mode == DMA_NORMAL)
6777     {
6778       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
6779       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
6780     }
6781   }
6782   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6783   {
6784     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6785 
6786     if (hdma->Init.Mode == DMA_NORMAL)
6787     {
6788       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
6789       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
6790     }
6791   }
6792   else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6793   {
6794     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6795 
6796     if (hdma->Init.Mode == DMA_NORMAL)
6797     {
6798       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
6799       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
6800     }
6801   }
6802   else
6803   {
6804     /* nothing to do */
6805   }
6806 
6807 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6808   htim->IC_CaptureCallback(htim);
6809 #else
6810   HAL_TIM_IC_CaptureCallback(htim);
6811 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6812 
6813   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6814 }
6815 
6816 /**
6817   * @brief  TIM DMA Capture half complete callback.
6818   * @param  hdma pointer to DMA handle.
6819   * @retval None
6820   */
TIM_DMACaptureHalfCplt(DMA_HandleTypeDef * hdma)6821 void TIM_DMACaptureHalfCplt(DMA_HandleTypeDef *hdma)
6822 {
6823   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6824 
6825   if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6826   {
6827     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6828   }
6829   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6830   {
6831     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6832   }
6833   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6834   {
6835     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6836   }
6837   else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6838   {
6839     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6840   }
6841   else
6842   {
6843     /* nothing to do */
6844   }
6845 
6846 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6847   htim->IC_CaptureHalfCpltCallback(htim);
6848 #else
6849   HAL_TIM_IC_CaptureHalfCpltCallback(htim);
6850 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6851 
6852   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6853 }
6854 
6855 /**
6856   * @brief  TIM DMA Period Elapse complete callback.
6857   * @param  hdma pointer to DMA handle.
6858   * @retval None
6859   */
TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef * hdma)6860 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
6861 {
6862   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6863 
6864   if (htim->hdma[TIM_DMA_ID_UPDATE]->Init.Mode == DMA_NORMAL)
6865   {
6866     htim->State = HAL_TIM_STATE_READY;
6867   }
6868 
6869 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6870   htim->PeriodElapsedCallback(htim);
6871 #else
6872   HAL_TIM_PeriodElapsedCallback(htim);
6873 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6874 }
6875 
6876 /**
6877   * @brief  TIM DMA Period Elapse half complete callback.
6878   * @param  hdma pointer to DMA handle.
6879   * @retval None
6880   */
TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef * hdma)6881 static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma)
6882 {
6883   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6884 
6885 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6886   htim->PeriodElapsedHalfCpltCallback(htim);
6887 #else
6888   HAL_TIM_PeriodElapsedHalfCpltCallback(htim);
6889 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6890 }
6891 
6892 /**
6893   * @brief  TIM DMA Trigger callback.
6894   * @param  hdma pointer to DMA handle.
6895   * @retval None
6896   */
TIM_DMATriggerCplt(DMA_HandleTypeDef * hdma)6897 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
6898 {
6899   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6900 
6901   if (htim->hdma[TIM_DMA_ID_TRIGGER]->Init.Mode == DMA_NORMAL)
6902   {
6903     htim->State = HAL_TIM_STATE_READY;
6904   }
6905 
6906 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6907   htim->TriggerCallback(htim);
6908 #else
6909   HAL_TIM_TriggerCallback(htim);
6910 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6911 }
6912 
6913 /**
6914   * @brief  TIM DMA Trigger half complete callback.
6915   * @param  hdma pointer to DMA handle.
6916   * @retval None
6917   */
TIM_DMATriggerHalfCplt(DMA_HandleTypeDef * hdma)6918 static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma)
6919 {
6920   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6921 
6922 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6923   htim->TriggerHalfCpltCallback(htim);
6924 #else
6925   HAL_TIM_TriggerHalfCpltCallback(htim);
6926 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6927 }
6928 
6929 /**
6930   * @brief  Time Base configuration
6931   * @param  TIMx TIM peripheral
6932   * @param  Structure TIM Base configuration structure
6933   * @retval None
6934   */
TIM_Base_SetConfig(TIM_TypeDef * TIMx,const TIM_Base_InitTypeDef * Structure)6935 void TIM_Base_SetConfig(TIM_TypeDef *TIMx, const TIM_Base_InitTypeDef *Structure)
6936 {
6937   uint32_t tmpcr1;
6938   tmpcr1 = TIMx->CR1;
6939 
6940   /* Set TIM Time Base Unit parameters ---------------------------------------*/
6941   if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
6942   {
6943     /* Select the Counter Mode */
6944     tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
6945     tmpcr1 |= Structure->CounterMode;
6946   }
6947 
6948   if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
6949   {
6950     /* Set the clock division */
6951     tmpcr1 &= ~TIM_CR1_CKD;
6952     tmpcr1 |= (uint32_t)Structure->ClockDivision;
6953   }
6954 
6955   /* Set the auto-reload preload */
6956   MODIFY_REG(tmpcr1, TIM_CR1_ARPE, Structure->AutoReloadPreload);
6957 
6958   /* Set the Autoreload value */
6959   TIMx->ARR = (uint32_t)Structure->Period ;
6960 
6961   /* Set the Prescaler value */
6962   TIMx->PSC = Structure->Prescaler;
6963 
6964   if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
6965   {
6966     /* Set the Repetition Counter value */
6967     TIMx->RCR = Structure->RepetitionCounter;
6968   }
6969 
6970   /* Disable Update Event (UEV) with Update Generation (UG)
6971      by changing Update Request Source (URS) to avoid Update flag (UIF) */
6972   SET_BIT(TIMx->CR1, TIM_CR1_URS);
6973 
6974   /* Generate an update event to reload the Prescaler
6975      and the repetition counter (only for advanced timer) value immediately */
6976   TIMx->EGR = TIM_EGR_UG;
6977 
6978   TIMx->CR1 = tmpcr1;
6979 }
6980 
6981 /**
6982   * @brief  Timer Output Compare 1 configuration
6983   * @param  TIMx to select the TIM peripheral
6984   * @param  OC_Config The output configuration structure
6985   * @retval None
6986   */
TIM_OC1_SetConfig(TIM_TypeDef * TIMx,const TIM_OC_InitTypeDef * OC_Config)6987 static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
6988 {
6989   uint32_t tmpccmrx;
6990   uint32_t tmpccer;
6991   uint32_t tmpcr2;
6992 
6993   /* Get the TIMx CCER register value */
6994   tmpccer = TIMx->CCER;
6995 
6996   /* Disable the Channel 1: Reset the CC1E Bit */
6997   TIMx->CCER &= ~TIM_CCER_CC1E;
6998 
6999   /* Get the TIMx CR2 register value */
7000   tmpcr2 =  TIMx->CR2;
7001 
7002   /* Get the TIMx CCMR1 register value */
7003   tmpccmrx = TIMx->CCMR1;
7004 
7005   /* Reset the Output Compare Mode Bits */
7006   tmpccmrx &= ~TIM_CCMR1_OC1M;
7007   tmpccmrx &= ~TIM_CCMR1_CC1S;
7008   /* Select the Output Compare Mode */
7009   tmpccmrx |= OC_Config->OCMode;
7010 
7011   /* Reset the Output Polarity level */
7012   tmpccer &= ~TIM_CCER_CC1P;
7013   /* Set the Output Compare Polarity */
7014   tmpccer |= OC_Config->OCPolarity;
7015 
7016   if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_1))
7017   {
7018     /* Check parameters */
7019     assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
7020 
7021     /* Reset the Output N Polarity level */
7022     tmpccer &= ~TIM_CCER_CC1NP;
7023     /* Set the Output N Polarity */
7024     tmpccer |= OC_Config->OCNPolarity;
7025     /* Reset the Output N State */
7026     tmpccer &= ~TIM_CCER_CC1NE;
7027   }
7028 
7029   if (IS_TIM_BREAK_INSTANCE(TIMx))
7030   {
7031     /* Check parameters */
7032     assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
7033     assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
7034 
7035     /* Reset the Output Compare and Output Compare N IDLE State */
7036     tmpcr2 &= ~TIM_CR2_OIS1;
7037     tmpcr2 &= ~TIM_CR2_OIS1N;
7038     /* Set the Output Idle state */
7039     tmpcr2 |= OC_Config->OCIdleState;
7040     /* Set the Output N Idle state */
7041     tmpcr2 |= OC_Config->OCNIdleState;
7042   }
7043 
7044   /* Write to TIMx CR2 */
7045   TIMx->CR2 = tmpcr2;
7046 
7047   /* Write to TIMx CCMR1 */
7048   TIMx->CCMR1 = tmpccmrx;
7049 
7050   /* Set the Capture Compare Register value */
7051   TIMx->CCR1 = OC_Config->Pulse;
7052 
7053   /* Write to TIMx CCER */
7054   TIMx->CCER = tmpccer;
7055 }
7056 
7057 /**
7058   * @brief  Timer Output Compare 2 configuration
7059   * @param  TIMx to select the TIM peripheral
7060   * @param  OC_Config The output configuration structure
7061   * @retval None
7062   */
TIM_OC2_SetConfig(TIM_TypeDef * TIMx,const TIM_OC_InitTypeDef * OC_Config)7063 void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
7064 {
7065   uint32_t tmpccmrx;
7066   uint32_t tmpccer;
7067   uint32_t tmpcr2;
7068 
7069   /* Get the TIMx CCER register value */
7070   tmpccer = TIMx->CCER;
7071 
7072   /* Disable the Channel 2: Reset the CC2E Bit */
7073   TIMx->CCER &= ~TIM_CCER_CC2E;
7074 
7075   /* Get the TIMx CR2 register value */
7076   tmpcr2 =  TIMx->CR2;
7077 
7078   /* Get the TIMx CCMR1 register value */
7079   tmpccmrx = TIMx->CCMR1;
7080 
7081   /* Reset the Output Compare mode and Capture/Compare selection Bits */
7082   tmpccmrx &= ~TIM_CCMR1_OC2M;
7083   tmpccmrx &= ~TIM_CCMR1_CC2S;
7084 
7085   /* Select the Output Compare Mode */
7086   tmpccmrx |= (OC_Config->OCMode << 8U);
7087 
7088   /* Reset the Output Polarity level */
7089   tmpccer &= ~TIM_CCER_CC2P;
7090   /* Set the Output Compare Polarity */
7091   tmpccer |= (OC_Config->OCPolarity << 4U);
7092 
7093   if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_2))
7094   {
7095     assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
7096 
7097     /* Reset the Output N Polarity level */
7098     tmpccer &= ~TIM_CCER_CC2NP;
7099     /* Set the Output N Polarity */
7100     tmpccer |= (OC_Config->OCNPolarity << 4U);
7101     /* Reset the Output N State */
7102     tmpccer &= ~TIM_CCER_CC2NE;
7103   }
7104 
7105   if (IS_TIM_BREAK_INSTANCE(TIMx))
7106   {
7107     /* Check parameters */
7108     assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
7109     assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
7110 
7111     /* Reset the Output Compare and Output Compare N IDLE State */
7112     tmpcr2 &= ~TIM_CR2_OIS2;
7113     tmpcr2 &= ~TIM_CR2_OIS2N;
7114     /* Set the Output Idle state */
7115     tmpcr2 |= (OC_Config->OCIdleState << 2U);
7116     /* Set the Output N Idle state */
7117     tmpcr2 |= (OC_Config->OCNIdleState << 2U);
7118   }
7119 
7120   /* Write to TIMx CR2 */
7121   TIMx->CR2 = tmpcr2;
7122 
7123   /* Write to TIMx CCMR1 */
7124   TIMx->CCMR1 = tmpccmrx;
7125 
7126   /* Set the Capture Compare Register value */
7127   TIMx->CCR2 = OC_Config->Pulse;
7128 
7129   /* Write to TIMx CCER */
7130   TIMx->CCER = tmpccer;
7131 }
7132 
7133 /**
7134   * @brief  Timer Output Compare 3 configuration
7135   * @param  TIMx to select the TIM peripheral
7136   * @param  OC_Config The output configuration structure
7137   * @retval None
7138   */
TIM_OC3_SetConfig(TIM_TypeDef * TIMx,const TIM_OC_InitTypeDef * OC_Config)7139 static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
7140 {
7141   uint32_t tmpccmrx;
7142   uint32_t tmpccer;
7143   uint32_t tmpcr2;
7144 
7145   /* Get the TIMx CCER register value */
7146   tmpccer = TIMx->CCER;
7147 
7148   /* Disable the Channel 3: Reset the CC2E Bit */
7149   TIMx->CCER &= ~TIM_CCER_CC3E;
7150 
7151   /* Get the TIMx CR2 register value */
7152   tmpcr2 =  TIMx->CR2;
7153 
7154   /* Get the TIMx CCMR2 register value */
7155   tmpccmrx = TIMx->CCMR2;
7156 
7157   /* Reset the Output Compare mode and Capture/Compare selection Bits */
7158   tmpccmrx &= ~TIM_CCMR2_OC3M;
7159   tmpccmrx &= ~TIM_CCMR2_CC3S;
7160   /* Select the Output Compare Mode */
7161   tmpccmrx |= OC_Config->OCMode;
7162 
7163   /* Reset the Output Polarity level */
7164   tmpccer &= ~TIM_CCER_CC3P;
7165   /* Set the Output Compare Polarity */
7166   tmpccer |= (OC_Config->OCPolarity << 8U);
7167 
7168   if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_3))
7169   {
7170     assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
7171 
7172     /* Reset the Output N Polarity level */
7173     tmpccer &= ~TIM_CCER_CC3NP;
7174     /* Set the Output N Polarity */
7175     tmpccer |= (OC_Config->OCNPolarity << 8U);
7176     /* Reset the Output N State */
7177     tmpccer &= ~TIM_CCER_CC3NE;
7178   }
7179 
7180   if (IS_TIM_BREAK_INSTANCE(TIMx))
7181   {
7182     /* Check parameters */
7183     assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
7184     assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
7185 
7186     /* Reset the Output Compare and Output Compare N IDLE State */
7187     tmpcr2 &= ~TIM_CR2_OIS3;
7188     tmpcr2 &= ~TIM_CR2_OIS3N;
7189     /* Set the Output Idle state */
7190     tmpcr2 |= (OC_Config->OCIdleState << 4U);
7191     /* Set the Output N Idle state */
7192     tmpcr2 |= (OC_Config->OCNIdleState << 4U);
7193   }
7194 
7195   /* Write to TIMx CR2 */
7196   TIMx->CR2 = tmpcr2;
7197 
7198   /* Write to TIMx CCMR2 */
7199   TIMx->CCMR2 = tmpccmrx;
7200 
7201   /* Set the Capture Compare Register value */
7202   TIMx->CCR3 = OC_Config->Pulse;
7203 
7204   /* Write to TIMx CCER */
7205   TIMx->CCER = tmpccer;
7206 }
7207 
7208 /**
7209   * @brief  Timer Output Compare 4 configuration
7210   * @param  TIMx to select the TIM peripheral
7211   * @param  OC_Config The output configuration structure
7212   * @retval None
7213   */
TIM_OC4_SetConfig(TIM_TypeDef * TIMx,const TIM_OC_InitTypeDef * OC_Config)7214 static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
7215 {
7216   uint32_t tmpccmrx;
7217   uint32_t tmpccer;
7218   uint32_t tmpcr2;
7219 
7220   /* Get the TIMx CCER register value */
7221   tmpccer = TIMx->CCER;
7222 
7223   /* Disable the Channel 4: Reset the CC4E Bit */
7224   TIMx->CCER &= ~TIM_CCER_CC4E;
7225 
7226   /* Get the TIMx CR2 register value */
7227   tmpcr2 =  TIMx->CR2;
7228 
7229   /* Get the TIMx CCMR2 register value */
7230   tmpccmrx = TIMx->CCMR2;
7231 
7232   /* Reset the Output Compare mode and Capture/Compare selection Bits */
7233   tmpccmrx &= ~TIM_CCMR2_OC4M;
7234   tmpccmrx &= ~TIM_CCMR2_CC4S;
7235 
7236   /* Select the Output Compare Mode */
7237   tmpccmrx |= (OC_Config->OCMode << 8U);
7238 
7239   /* Reset the Output Polarity level */
7240   tmpccer &= ~TIM_CCER_CC4P;
7241   /* Set the Output Compare Polarity */
7242   tmpccer |= (OC_Config->OCPolarity << 12U);
7243 
7244   if (IS_TIM_BREAK_INSTANCE(TIMx))
7245   {
7246     /* Check parameters */
7247     assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
7248 
7249     /* Reset the Output Compare IDLE State */
7250     tmpcr2 &= ~TIM_CR2_OIS4;
7251 
7252     /* Set the Output Idle state */
7253     tmpcr2 |= (OC_Config->OCIdleState << 6U);
7254   }
7255 
7256   /* Write to TIMx CR2 */
7257   TIMx->CR2 = tmpcr2;
7258 
7259   /* Write to TIMx CCMR2 */
7260   TIMx->CCMR2 = tmpccmrx;
7261 
7262   /* Set the Capture Compare Register value */
7263   TIMx->CCR4 = OC_Config->Pulse;
7264 
7265   /* Write to TIMx CCER */
7266   TIMx->CCER = tmpccer;
7267 }
7268 
7269 /**
7270   * @brief  Timer Output Compare 5 configuration
7271   * @param  TIMx to select the TIM peripheral
7272   * @param  OC_Config The output configuration structure
7273   * @retval None
7274   */
TIM_OC5_SetConfig(TIM_TypeDef * TIMx,const TIM_OC_InitTypeDef * OC_Config)7275 static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx,
7276                               const TIM_OC_InitTypeDef *OC_Config)
7277 {
7278   uint32_t tmpccmrx;
7279   uint32_t tmpccer;
7280   uint32_t tmpcr2;
7281 
7282   /* Get the TIMx CCER register value */
7283   tmpccer = TIMx->CCER;
7284 
7285   /* Disable the output: Reset the CCxE Bit */
7286   TIMx->CCER &= ~TIM_CCER_CC5E;
7287 
7288   /* Get the TIMx CR2 register value */
7289   tmpcr2 =  TIMx->CR2;
7290   /* Get the TIMx CCMR1 register value */
7291   tmpccmrx = TIMx->CCMR3;
7292 
7293   /* Reset the Output Compare Mode Bits */
7294   tmpccmrx &= ~(TIM_CCMR3_OC5M);
7295   /* Select the Output Compare Mode */
7296   tmpccmrx |= OC_Config->OCMode;
7297 
7298   /* Reset the Output Polarity level */
7299   tmpccer &= ~TIM_CCER_CC5P;
7300   /* Set the Output Compare Polarity */
7301   tmpccer |= (OC_Config->OCPolarity << 16U);
7302 
7303   if (IS_TIM_BREAK_INSTANCE(TIMx))
7304   {
7305     /* Reset the Output Compare IDLE State */
7306     tmpcr2 &= ~TIM_CR2_OIS5;
7307     /* Set the Output Idle state */
7308     tmpcr2 |= (OC_Config->OCIdleState << 8U);
7309   }
7310   /* Write to TIMx CR2 */
7311   TIMx->CR2 = tmpcr2;
7312 
7313   /* Write to TIMx CCMR3 */
7314   TIMx->CCMR3 = tmpccmrx;
7315 
7316   /* Set the Capture Compare Register value */
7317   TIMx->CCR5 = OC_Config->Pulse;
7318 
7319   /* Write to TIMx CCER */
7320   TIMx->CCER = tmpccer;
7321 }
7322 
7323 /**
7324   * @brief  Timer Output Compare 6 configuration
7325   * @param  TIMx to select the TIM peripheral
7326   * @param  OC_Config The output configuration structure
7327   * @retval None
7328   */
TIM_OC6_SetConfig(TIM_TypeDef * TIMx,const TIM_OC_InitTypeDef * OC_Config)7329 static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx,
7330                               const TIM_OC_InitTypeDef *OC_Config)
7331 {
7332   uint32_t tmpccmrx;
7333   uint32_t tmpccer;
7334   uint32_t tmpcr2;
7335 
7336   /* Get the TIMx CCER register value */
7337   tmpccer = TIMx->CCER;
7338 
7339   /* Disable the output: Reset the CCxE Bit */
7340   TIMx->CCER &= ~TIM_CCER_CC6E;
7341 
7342   /* Get the TIMx CR2 register value */
7343   tmpcr2 =  TIMx->CR2;
7344   /* Get the TIMx CCMR1 register value */
7345   tmpccmrx = TIMx->CCMR3;
7346 
7347   /* Reset the Output Compare Mode Bits */
7348   tmpccmrx &= ~(TIM_CCMR3_OC6M);
7349   /* Select the Output Compare Mode */
7350   tmpccmrx |= (OC_Config->OCMode << 8U);
7351 
7352   /* Reset the Output Polarity level */
7353   tmpccer &= (uint32_t)~TIM_CCER_CC6P;
7354   /* Set the Output Compare Polarity */
7355   tmpccer |= (OC_Config->OCPolarity << 20U);
7356 
7357   if (IS_TIM_BREAK_INSTANCE(TIMx))
7358   {
7359     /* Reset the Output Compare IDLE State */
7360     tmpcr2 &= ~TIM_CR2_OIS6;
7361     /* Set the Output Idle state */
7362     tmpcr2 |= (OC_Config->OCIdleState << 10U);
7363   }
7364 
7365   /* Write to TIMx CR2 */
7366   TIMx->CR2 = tmpcr2;
7367 
7368   /* Write to TIMx CCMR3 */
7369   TIMx->CCMR3 = tmpccmrx;
7370 
7371   /* Set the Capture Compare Register value */
7372   TIMx->CCR6 = OC_Config->Pulse;
7373 
7374   /* Write to TIMx CCER */
7375   TIMx->CCER = tmpccer;
7376 }
7377 
7378 /**
7379   * @brief  Slave Timer configuration function
7380   * @param  htim TIM handle
7381   * @param  sSlaveConfig Slave timer configuration
7382   * @retval None
7383   */
TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef * htim,const TIM_SlaveConfigTypeDef * sSlaveConfig)7384 static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
7385                                                   const TIM_SlaveConfigTypeDef *sSlaveConfig)
7386 {
7387   HAL_StatusTypeDef status = HAL_OK;
7388   uint32_t tmpsmcr;
7389   uint32_t tmpccmr1;
7390   uint32_t tmpccer;
7391 
7392   /* Get the TIMx SMCR register value */
7393   tmpsmcr = htim->Instance->SMCR;
7394 
7395   /* Reset the Trigger Selection Bits */
7396   tmpsmcr &= ~TIM_SMCR_TS;
7397   /* Set the Input Trigger source */
7398   tmpsmcr |= sSlaveConfig->InputTrigger;
7399 
7400   /* Reset the slave mode Bits */
7401   tmpsmcr &= ~TIM_SMCR_SMS;
7402   /* Set the slave mode */
7403   tmpsmcr |= sSlaveConfig->SlaveMode;
7404 
7405   /* Write to TIMx SMCR */
7406   htim->Instance->SMCR = tmpsmcr;
7407 
7408   /* Configure the trigger prescaler, filter, and polarity */
7409   switch (sSlaveConfig->InputTrigger)
7410   {
7411     case TIM_TS_ETRF:
7412     {
7413       /* Check the parameters */
7414       assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
7415       assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
7416       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
7417       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
7418       /* Configure the ETR Trigger source */
7419       TIM_ETR_SetConfig(htim->Instance,
7420                         sSlaveConfig->TriggerPrescaler,
7421                         sSlaveConfig->TriggerPolarity,
7422                         sSlaveConfig->TriggerFilter);
7423       break;
7424     }
7425 
7426     case TIM_TS_TI1F_ED:
7427     {
7428       /* Check the parameters */
7429       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
7430       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
7431 
7432       if (sSlaveConfig->SlaveMode == TIM_SLAVEMODE_GATED)
7433       {
7434         return HAL_ERROR;
7435       }
7436 
7437       /* Disable the Channel 1: Reset the CC1E Bit */
7438       tmpccer = htim->Instance->CCER;
7439       htim->Instance->CCER &= ~TIM_CCER_CC1E;
7440       tmpccmr1 = htim->Instance->CCMR1;
7441 
7442       /* Set the filter */
7443       tmpccmr1 &= ~TIM_CCMR1_IC1F;
7444       tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4U);
7445 
7446       /* Write to TIMx CCMR1 and CCER registers */
7447       htim->Instance->CCMR1 = tmpccmr1;
7448       htim->Instance->CCER = tmpccer;
7449       break;
7450     }
7451 
7452     case TIM_TS_TI1FP1:
7453     {
7454       /* Check the parameters */
7455       assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
7456       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
7457       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
7458 
7459       /* Configure TI1 Filter and Polarity */
7460       TIM_TI1_ConfigInputStage(htim->Instance,
7461                                sSlaveConfig->TriggerPolarity,
7462                                sSlaveConfig->TriggerFilter);
7463       break;
7464     }
7465 
7466     case TIM_TS_TI2FP2:
7467     {
7468       /* Check the parameters */
7469       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
7470       assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
7471       assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
7472 
7473       /* Configure TI2 Filter and Polarity */
7474       TIM_TI2_ConfigInputStage(htim->Instance,
7475                                sSlaveConfig->TriggerPolarity,
7476                                sSlaveConfig->TriggerFilter);
7477       break;
7478     }
7479 
7480     case TIM_TS_ITR0:
7481     case TIM_TS_ITR1:
7482     case TIM_TS_ITR2:
7483     case TIM_TS_ITR3:
7484     case TIM_TS_ITR4:
7485     case TIM_TS_ITR5:
7486     case TIM_TS_ITR6:
7487     case TIM_TS_ITR7:
7488     case TIM_TS_ITR8:
7489     case TIM_TS_ITR9:
7490     case TIM_TS_ITR10:
7491     case TIM_TS_ITR11:
7492     case TIM_TS_ITR12:
7493     case TIM_TS_ITR13:
7494     {
7495       /* Check the parameter */
7496       assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
7497       break;
7498     }
7499 
7500     default:
7501       status = HAL_ERROR;
7502       break;
7503   }
7504 
7505   return status;
7506 }
7507 
7508 /**
7509   * @brief  Configure the TI1 as Input.
7510   * @param  TIMx to select the TIM peripheral.
7511   * @param  TIM_ICPolarity The Input Polarity.
7512   *          This parameter can be one of the following values:
7513   *            @arg TIM_ICPOLARITY_RISING
7514   *            @arg TIM_ICPOLARITY_FALLING
7515   *            @arg TIM_ICPOLARITY_BOTHEDGE
7516   * @param  TIM_ICSelection specifies the input to be used.
7517   *          This parameter can be one of the following values:
7518   *            @arg TIM_ICSELECTION_DIRECTTI: TIM Input 1 is selected to be connected to IC1.
7519   *            @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 1 is selected to be connected to IC2.
7520   *            @arg TIM_ICSELECTION_TRC: TIM Input 1 is selected to be connected to TRC.
7521   * @param  TIM_ICFilter Specifies the Input Capture Filter.
7522   *          This parameter must be a value between 0x00 and 0x0F.
7523   * @retval None
7524   * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
7525   *       (on channel2 path) is used as the input signal. Therefore CCMR1 must be
7526   *        protected against un-initialized filter and polarity values.
7527   */
TIM_TI1_SetConfig(TIM_TypeDef * TIMx,uint32_t TIM_ICPolarity,uint32_t TIM_ICSelection,uint32_t TIM_ICFilter)7528 void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
7529                        uint32_t TIM_ICFilter)
7530 {
7531   uint32_t tmpccmr1;
7532   uint32_t tmpccer;
7533 
7534   /* Disable the Channel 1: Reset the CC1E Bit */
7535   tmpccer = TIMx->CCER;
7536   TIMx->CCER &= ~TIM_CCER_CC1E;
7537   tmpccmr1 = TIMx->CCMR1;
7538 
7539   /* Select the Input */
7540   if (IS_TIM_CC2_INSTANCE(TIMx) != RESET)
7541   {
7542     tmpccmr1 &= ~TIM_CCMR1_CC1S;
7543     tmpccmr1 |= TIM_ICSelection;
7544   }
7545   else
7546   {
7547     tmpccmr1 |= TIM_CCMR1_CC1S_0;
7548   }
7549 
7550   /* Set the filter */
7551   tmpccmr1 &= ~TIM_CCMR1_IC1F;
7552   tmpccmr1 |= ((TIM_ICFilter << 4U) & TIM_CCMR1_IC1F);
7553 
7554   /* Select the Polarity and set the CC1E Bit */
7555   tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
7556   tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
7557 
7558   /* Write to TIMx CCMR1 and CCER registers */
7559   TIMx->CCMR1 = tmpccmr1;
7560   TIMx->CCER = tmpccer;
7561 }
7562 
7563 /**
7564   * @brief  Configure the Polarity and Filter for TI1.
7565   * @param  TIMx to select the TIM peripheral.
7566   * @param  TIM_ICPolarity The Input Polarity.
7567   *          This parameter can be one of the following values:
7568   *            @arg TIM_ICPOLARITY_RISING
7569   *            @arg TIM_ICPOLARITY_FALLING
7570   *            @arg TIM_ICPOLARITY_BOTHEDGE
7571   * @param  TIM_ICFilter Specifies the Input Capture Filter.
7572   *          This parameter must be a value between 0x00 and 0x0F.
7573   * @retval None
7574   */
TIM_TI1_ConfigInputStage(TIM_TypeDef * TIMx,uint32_t TIM_ICPolarity,uint32_t TIM_ICFilter)7575 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
7576 {
7577   uint32_t tmpccmr1;
7578   uint32_t tmpccer;
7579 
7580   /* Disable the Channel 1: Reset the CC1E Bit */
7581   tmpccer = TIMx->CCER;
7582   TIMx->CCER &= ~TIM_CCER_CC1E;
7583   tmpccmr1 = TIMx->CCMR1;
7584 
7585   /* Set the filter */
7586   tmpccmr1 &= ~TIM_CCMR1_IC1F;
7587   tmpccmr1 |= (TIM_ICFilter << 4U);
7588 
7589   /* Select the Polarity and set the CC1E Bit */
7590   tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
7591   tmpccer |= TIM_ICPolarity;
7592 
7593   /* Write to TIMx CCMR1 and CCER registers */
7594   TIMx->CCMR1 = tmpccmr1;
7595   TIMx->CCER = tmpccer;
7596 }
7597 
7598 /**
7599   * @brief  Configure the TI2 as Input.
7600   * @param  TIMx to select the TIM peripheral
7601   * @param  TIM_ICPolarity The Input Polarity.
7602   *          This parameter can be one of the following values:
7603   *            @arg TIM_ICPOLARITY_RISING
7604   *            @arg TIM_ICPOLARITY_FALLING
7605   *            @arg TIM_ICPOLARITY_BOTHEDGE
7606   * @param  TIM_ICSelection specifies the input to be used.
7607   *          This parameter can be one of the following values:
7608   *            @arg TIM_ICSELECTION_DIRECTTI: TIM Input 2 is selected to be connected to IC2.
7609   *            @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 2 is selected to be connected to IC1.
7610   *            @arg TIM_ICSELECTION_TRC: TIM Input 2 is selected to be connected to TRC.
7611   * @param  TIM_ICFilter Specifies the Input Capture Filter.
7612   *          This parameter must be a value between 0x00 and 0x0F.
7613   * @retval None
7614   * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
7615   *       (on channel1 path) is used as the input signal. Therefore CCMR1 must be
7616   *        protected against un-initialized filter and polarity values.
7617   */
TIM_TI2_SetConfig(TIM_TypeDef * TIMx,uint32_t TIM_ICPolarity,uint32_t TIM_ICSelection,uint32_t TIM_ICFilter)7618 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
7619                               uint32_t TIM_ICFilter)
7620 {
7621   uint32_t tmpccmr1;
7622   uint32_t tmpccer;
7623 
7624   /* Disable the Channel 2: Reset the CC2E Bit */
7625   tmpccer = TIMx->CCER;
7626   TIMx->CCER &= ~TIM_CCER_CC2E;
7627   tmpccmr1 = TIMx->CCMR1;
7628 
7629   /* Select the Input */
7630   tmpccmr1 &= ~TIM_CCMR1_CC2S;
7631   tmpccmr1 |= (TIM_ICSelection << 8U);
7632 
7633   /* Set the filter */
7634   tmpccmr1 &= ~TIM_CCMR1_IC2F;
7635   tmpccmr1 |= ((TIM_ICFilter << 12U) & TIM_CCMR1_IC2F);
7636 
7637   /* Select the Polarity and set the CC2E Bit */
7638   tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
7639   tmpccer |= ((TIM_ICPolarity << 4U) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
7640 
7641   /* Write to TIMx CCMR1 and CCER registers */
7642   TIMx->CCMR1 = tmpccmr1 ;
7643   TIMx->CCER = tmpccer;
7644 }
7645 
7646 /**
7647   * @brief  Configure the Polarity and Filter for TI2.
7648   * @param  TIMx to select the TIM peripheral.
7649   * @param  TIM_ICPolarity The Input Polarity.
7650   *          This parameter can be one of the following values:
7651   *            @arg TIM_ICPOLARITY_RISING
7652   *            @arg TIM_ICPOLARITY_FALLING
7653   *            @arg TIM_ICPOLARITY_BOTHEDGE
7654   * @param  TIM_ICFilter Specifies the Input Capture Filter.
7655   *          This parameter must be a value between 0x00 and 0x0F.
7656   * @retval None
7657   */
TIM_TI2_ConfigInputStage(TIM_TypeDef * TIMx,uint32_t TIM_ICPolarity,uint32_t TIM_ICFilter)7658 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
7659 {
7660   uint32_t tmpccmr1;
7661   uint32_t tmpccer;
7662 
7663   /* Disable the Channel 2: Reset the CC2E Bit */
7664   tmpccer = TIMx->CCER;
7665   TIMx->CCER &= ~TIM_CCER_CC2E;
7666   tmpccmr1 = TIMx->CCMR1;
7667 
7668   /* Set the filter */
7669   tmpccmr1 &= ~TIM_CCMR1_IC2F;
7670   tmpccmr1 |= (TIM_ICFilter << 12U);
7671 
7672   /* Select the Polarity and set the CC2E Bit */
7673   tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
7674   tmpccer |= (TIM_ICPolarity << 4U);
7675 
7676   /* Write to TIMx CCMR1 and CCER registers */
7677   TIMx->CCMR1 = tmpccmr1 ;
7678   TIMx->CCER = tmpccer;
7679 }
7680 
7681 /**
7682   * @brief  Configure the TI3 as Input.
7683   * @param  TIMx to select the TIM peripheral
7684   * @param  TIM_ICPolarity The Input Polarity.
7685   *          This parameter can be one of the following values:
7686   *            @arg TIM_ICPOLARITY_RISING
7687   *            @arg TIM_ICPOLARITY_FALLING
7688   *            @arg TIM_ICPOLARITY_BOTHEDGE
7689   * @param  TIM_ICSelection specifies the input to be used.
7690   *          This parameter can be one of the following values:
7691   *            @arg TIM_ICSELECTION_DIRECTTI: TIM Input 3 is selected to be connected to IC3.
7692   *            @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 3 is selected to be connected to IC4.
7693   *            @arg TIM_ICSELECTION_TRC: TIM Input 3 is selected to be connected to TRC.
7694   * @param  TIM_ICFilter Specifies the Input Capture Filter.
7695   *          This parameter must be a value between 0x00 and 0x0F.
7696   * @retval None
7697   * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
7698   *       (on channel1 path) is used as the input signal. Therefore CCMR2 must be
7699   *        protected against un-initialized filter and polarity values.
7700   */
TIM_TI3_SetConfig(TIM_TypeDef * TIMx,uint32_t TIM_ICPolarity,uint32_t TIM_ICSelection,uint32_t TIM_ICFilter)7701 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
7702                               uint32_t TIM_ICFilter)
7703 {
7704   uint32_t tmpccmr2;
7705   uint32_t tmpccer;
7706 
7707   /* Disable the Channel 3: Reset the CC3E Bit */
7708   tmpccer = TIMx->CCER;
7709   TIMx->CCER &= ~TIM_CCER_CC3E;
7710   tmpccmr2 = TIMx->CCMR2;
7711 
7712   /* Select the Input */
7713   tmpccmr2 &= ~TIM_CCMR2_CC3S;
7714   tmpccmr2 |= TIM_ICSelection;
7715 
7716   /* Set the filter */
7717   tmpccmr2 &= ~TIM_CCMR2_IC3F;
7718   tmpccmr2 |= ((TIM_ICFilter << 4U) & TIM_CCMR2_IC3F);
7719 
7720   /* Select the Polarity and set the CC3E Bit */
7721   tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
7722   tmpccer |= ((TIM_ICPolarity << 8U) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
7723 
7724   /* Write to TIMx CCMR2 and CCER registers */
7725   TIMx->CCMR2 = tmpccmr2;
7726   TIMx->CCER = tmpccer;
7727 }
7728 
7729 /**
7730   * @brief  Configure the TI4 as Input.
7731   * @param  TIMx to select the TIM peripheral
7732   * @param  TIM_ICPolarity The Input Polarity.
7733   *          This parameter can be one of the following values:
7734   *            @arg TIM_ICPOLARITY_RISING
7735   *            @arg TIM_ICPOLARITY_FALLING
7736   *            @arg TIM_ICPOLARITY_BOTHEDGE
7737   * @param  TIM_ICSelection specifies the input to be used.
7738   *          This parameter can be one of the following values:
7739   *            @arg TIM_ICSELECTION_DIRECTTI: TIM Input 4 is selected to be connected to IC4.
7740   *            @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 4 is selected to be connected to IC3.
7741   *            @arg TIM_ICSELECTION_TRC: TIM Input 4 is selected to be connected to TRC.
7742   * @param  TIM_ICFilter Specifies the Input Capture Filter.
7743   *          This parameter must be a value between 0x00 and 0x0F.
7744   * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
7745   *       (on channel1 path) is used as the input signal. Therefore CCMR2 must be
7746   *        protected against un-initialized filter and polarity values.
7747   * @retval None
7748   */
TIM_TI4_SetConfig(TIM_TypeDef * TIMx,uint32_t TIM_ICPolarity,uint32_t TIM_ICSelection,uint32_t TIM_ICFilter)7749 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
7750                               uint32_t TIM_ICFilter)
7751 {
7752   uint32_t tmpccmr2;
7753   uint32_t tmpccer;
7754 
7755   /* Disable the Channel 4: Reset the CC4E Bit */
7756   tmpccer = TIMx->CCER;
7757   TIMx->CCER &= ~TIM_CCER_CC4E;
7758   tmpccmr2 = TIMx->CCMR2;
7759 
7760   /* Select the Input */
7761   tmpccmr2 &= ~TIM_CCMR2_CC4S;
7762   tmpccmr2 |= (TIM_ICSelection << 8U);
7763 
7764   /* Set the filter */
7765   tmpccmr2 &= ~TIM_CCMR2_IC4F;
7766   tmpccmr2 |= ((TIM_ICFilter << 12U) & TIM_CCMR2_IC4F);
7767 
7768   /* Select the Polarity and set the CC4E Bit */
7769   tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
7770   tmpccer |= ((TIM_ICPolarity << 12U) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
7771 
7772   /* Write to TIMx CCMR2 and CCER registers */
7773   TIMx->CCMR2 = tmpccmr2;
7774   TIMx->CCER = tmpccer ;
7775 }
7776 
7777 /**
7778   * @brief  Selects the Input Trigger source
7779   * @param  TIMx to select the TIM peripheral
7780   * @param  InputTriggerSource The Input Trigger source.
7781   *          This parameter can be one of the following values:
7782   *            @arg TIM_TS_ITR0: Internal Trigger 0
7783   *            @arg TIM_TS_ITR1: Internal Trigger 1
7784   *            @arg TIM_TS_ITR2: Internal Trigger 2
7785   *            @arg TIM_TS_ITR3: Internal Trigger 3
7786   *            @arg TIM_TS_ITR4: Internal Trigger 4  (*)
7787   *            @arg TIM_TS_ITR5: Internal Trigger 5
7788   *            @arg TIM_TS_ITR6: Internal Trigger 6
7789   *            @arg TIM_TS_ITR7: Internal Trigger 7
7790   *            @arg TIM_TS_ITR8: Internal Trigger 8  (*)
7791   *            @arg TIM_TS_ITR9: Internal Trigger 9  (*)
7792   *            @arg TIM_TS_ITR10: Internal Trigger 10 (*)
7793   *            @arg TIM_TS_ITR11: Internal Trigger 11 (*)
7794   *            @arg TIM_TS_ITR12: Internal Trigger 12 (*)
7795   *            @arg TIM_TS_ITR13: Internal Trigger 13 (*)
7796   *            @arg TIM_TS_TI1F_ED: TI1 Edge Detector
7797   *            @arg TIM_TS_TI1FP1: Filtered Timer Input 1
7798   *            @arg TIM_TS_TI2FP2: Filtered Timer Input 2
7799   *            @arg TIM_TS_ETRF: External Trigger input
7800   *
7801   *       (*)  Value not defined in all devices.
7802   *
7803   * @retval None
7804   */
TIM_ITRx_SetConfig(TIM_TypeDef * TIMx,uint32_t InputTriggerSource)7805 static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource)
7806 {
7807   uint32_t tmpsmcr;
7808 
7809   /* Get the TIMx SMCR register value */
7810   tmpsmcr = TIMx->SMCR;
7811   /* Reset the TS Bits */
7812   tmpsmcr &= ~TIM_SMCR_TS;
7813   /* Set the Input Trigger source and the slave mode*/
7814   tmpsmcr |= (InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1);
7815   /* Write to TIMx SMCR */
7816   TIMx->SMCR = tmpsmcr;
7817 }
7818 /**
7819   * @brief  Configures the TIMx External Trigger (ETR).
7820   * @param  TIMx to select the TIM peripheral
7821   * @param  TIM_ExtTRGPrescaler The external Trigger Prescaler.
7822   *          This parameter can be one of the following values:
7823   *            @arg TIM_ETRPRESCALER_DIV1: ETRP Prescaler OFF.
7824   *            @arg TIM_ETRPRESCALER_DIV2: ETRP frequency divided by 2.
7825   *            @arg TIM_ETRPRESCALER_DIV4: ETRP frequency divided by 4.
7826   *            @arg TIM_ETRPRESCALER_DIV8: ETRP frequency divided by 8.
7827   * @param  TIM_ExtTRGPolarity The external Trigger Polarity.
7828   *          This parameter can be one of the following values:
7829   *            @arg TIM_ETRPOLARITY_INVERTED: active low or falling edge active.
7830   *            @arg TIM_ETRPOLARITY_NONINVERTED: active high or rising edge active.
7831   * @param  ExtTRGFilter External Trigger Filter.
7832   *          This parameter must be a value between 0x00 and 0x0F
7833   * @retval None
7834   */
TIM_ETR_SetConfig(TIM_TypeDef * TIMx,uint32_t TIM_ExtTRGPrescaler,uint32_t TIM_ExtTRGPolarity,uint32_t ExtTRGFilter)7835 void TIM_ETR_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler,
7836                        uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
7837 {
7838   uint32_t tmpsmcr;
7839 
7840   tmpsmcr = TIMx->SMCR;
7841 
7842   /* Reset the ETR Bits */
7843   tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
7844 
7845   /* Set the Prescaler, the Filter value and the Polarity */
7846   tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8U)));
7847 
7848   /* Write to TIMx SMCR */
7849   TIMx->SMCR = tmpsmcr;
7850 }
7851 
7852 /**
7853   * @brief  Enables or disables the TIM Capture Compare Channel x.
7854   * @param  TIMx to select the TIM peripheral
7855   * @param  Channel specifies the TIM Channel
7856   *          This parameter can be one of the following values:
7857   *            @arg TIM_CHANNEL_1: TIM Channel 1
7858   *            @arg TIM_CHANNEL_2: TIM Channel 2
7859   *            @arg TIM_CHANNEL_3: TIM Channel 3
7860   *            @arg TIM_CHANNEL_4: TIM Channel 4
7861   *            @arg TIM_CHANNEL_5: TIM Channel 5 selected
7862   *            @arg TIM_CHANNEL_6: TIM Channel 6 selected
7863   * @param  ChannelState specifies the TIM Channel CCxE bit new state.
7864   *          This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
7865   * @retval None
7866   */
TIM_CCxChannelCmd(TIM_TypeDef * TIMx,uint32_t Channel,uint32_t ChannelState)7867 void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
7868 {
7869   uint32_t tmp;
7870 
7871   /* Check the parameters */
7872   assert_param(IS_TIM_CC1_INSTANCE(TIMx));
7873   assert_param(IS_TIM_CHANNELS(Channel));
7874 
7875   tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
7876 
7877   /* Reset the CCxE Bit */
7878   TIMx->CCER &= ~tmp;
7879 
7880   /* Set or reset the CCxE Bit */
7881   TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
7882 }
7883 
7884 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
7885 /**
7886   * @brief  Reset interrupt callbacks to the legacy weak callbacks.
7887   * @param  htim pointer to a TIM_HandleTypeDef structure that contains
7888   *                the configuration information for TIM module.
7889   * @retval None
7890   */
TIM_ResetCallback(TIM_HandleTypeDef * htim)7891 void TIM_ResetCallback(TIM_HandleTypeDef *htim)
7892 {
7893   /* Reset the TIM callback to the legacy weak callbacks */
7894   htim->PeriodElapsedCallback             = HAL_TIM_PeriodElapsedCallback;
7895   htim->PeriodElapsedHalfCpltCallback     = HAL_TIM_PeriodElapsedHalfCpltCallback;
7896   htim->TriggerCallback                   = HAL_TIM_TriggerCallback;
7897   htim->TriggerHalfCpltCallback           = HAL_TIM_TriggerHalfCpltCallback;
7898   htim->IC_CaptureCallback                = HAL_TIM_IC_CaptureCallback;
7899   htim->IC_CaptureHalfCpltCallback        = HAL_TIM_IC_CaptureHalfCpltCallback;
7900   htim->OC_DelayElapsedCallback           = HAL_TIM_OC_DelayElapsedCallback;
7901   htim->PWM_PulseFinishedCallback         = HAL_TIM_PWM_PulseFinishedCallback;
7902   htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback;
7903   htim->ErrorCallback                     = HAL_TIM_ErrorCallback;
7904   htim->CommutationCallback               = HAL_TIMEx_CommutCallback;
7905   htim->CommutationHalfCpltCallback       = HAL_TIMEx_CommutHalfCpltCallback;
7906   htim->BreakCallback                     = HAL_TIMEx_BreakCallback;
7907   htim->Break2Callback                    = HAL_TIMEx_Break2Callback;
7908 }
7909 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
7910 
7911 /**
7912   * @}
7913   */
7914 
7915 #endif /* HAL_TIM_MODULE_ENABLED */
7916 /**
7917   * @}
7918   */
7919 
7920 /**
7921   * @}
7922   */
7923