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