Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_tim.c
blob4e45001b5a4bc83ef32591df0fc58b075e384675
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_hal_tim.c
4 * @author MCD Application Team
5 * @brief TIM HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Timer (TIM) peripheral:
8 * + TIM Time Base Initialization
9 * + TIM Time Base Start
10 * + TIM Time Base Start Interruption
11 * + TIM Time Base Start DMA
12 * + TIM Output Compare/PWM Initialization
13 * + TIM Output Compare/PWM Channel Configuration
14 * + TIM Output Compare/PWM Start
15 * + TIM Output Compare/PWM Start Interruption
16 * + TIM Output Compare/PWM Start DMA
17 * + TIM Input Capture Initialization
18 * + TIM Input Capture Channel Configuration
19 * + TIM Input Capture Start
20 * + TIM Input Capture Start Interruption
21 * + TIM Input Capture Start DMA
22 * + TIM One Pulse Initialization
23 * + TIM One Pulse Channel Configuration
24 * + TIM One Pulse Start
25 * + TIM Encoder Interface Initialization
26 * + TIM Encoder Interface Start
27 * + TIM Encoder Interface Start Interruption
28 * + TIM Encoder Interface Start DMA
29 * + Commutation Event configuration with Interruption and DMA
30 * + TIM OCRef clear configuration
31 * + TIM External Clock configuration
32 @verbatim
33 ==============================================================================
34 ##### TIMER Generic features #####
35 ==============================================================================
36 [..] The Timer features include:
37 (#) 16-bit up, down, up/down auto-reload counter.
38 (#) 16-bit programmable prescaler allowing dividing (also on the fly) the
39 counter clock frequency either by any factor between 1 and 65536.
40 (#) Up to 4 independent channels for:
41 (++) Input Capture
42 (++) Output Compare
43 (++) PWM generation (Edge and Center-aligned Mode)
44 (++) One-pulse mode output
45 (#) Synchronization circuit to control the timer with external signals and to interconnect
46 several timers together.
47 (#) Supports incremental encoder for positioning purposes
49 ##### How to use this driver #####
50 ==============================================================================
51 [..]
52 (#) Initialize the TIM low level resources by implementing the following functions
53 depending on the selected feature:
54 (++) Time Base : HAL_TIM_Base_MspInit()
55 (++) Input Capture : HAL_TIM_IC_MspInit()
56 (++) Output Compare : HAL_TIM_OC_MspInit()
57 (++) PWM generation : HAL_TIM_PWM_MspInit()
58 (++) One-pulse mode output : HAL_TIM_OnePulse_MspInit()
59 (++) Encoder mode output : HAL_TIM_Encoder_MspInit()
61 (#) Initialize the TIM low level resources :
62 (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
63 (##) TIM pins configuration
64 (+++) Enable the clock for the TIM GPIOs using the following function:
65 __HAL_RCC_GPIOx_CLK_ENABLE();
66 (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
68 (#) The external Clock can be configured, if needed (the default clock is the
69 internal clock from the APBx), using the following function:
70 HAL_TIM_ConfigClockSource, the clock configuration should be done before
71 any start function.
73 (#) Configure the TIM in the desired functioning mode using one of the
74 Initialization function of this driver:
75 (++) HAL_TIM_Base_Init: to use the Timer to generate a simple time base
76 (++) HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an
77 Output Compare signal.
78 (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a
79 PWM signal.
80 (++) HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an
81 external signal.
82 (++) HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer
83 in One Pulse Mode.
84 (++) HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
86 (#) Activate the TIM peripheral using one of the start functions depending from the feature used:
87 (++) Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
88 (++) Input Capture : HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
89 (++) Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
90 (++) PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
91 (++) One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
92 (++) Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(), HAL_TIM_Encoder_Start_IT().
94 (#) The DMA Burst is managed with the two following functions:
95 HAL_TIM_DMABurst_WriteStart()
96 HAL_TIM_DMABurst_ReadStart()
98 *** Callback registration ***
99 =============================================
101 [..]
102 The compilation define USE_HAL_TIM_REGISTER_CALLBACKS when set to 1
103 allows the user to configure dynamically the driver callbacks.
105 [..]
106 Use Function @ref HAL_TIM_RegisterCallback() to register a callback.
107 @ref HAL_TIM_RegisterCallback() takes as parameters the HAL peripheral handle,
108 the Callback ID and a pointer to the user callback function.
110 [..]
111 Use function @ref HAL_TIM_UnRegisterCallback() to reset a callback to the default
112 weak function.
113 @ref HAL_TIM_UnRegisterCallback takes as parameters the HAL peripheral handle,
114 and the Callback ID.
116 [..]
117 These functions allow to register/unregister following callbacks:
118 (+) Base_MspInitCallback : TIM Base Msp Init Callback.
119 (+) Base_MspDeInitCallback : TIM Base Msp DeInit Callback.
120 (+) IC_MspInitCallback : TIM IC Msp Init Callback.
121 (+) IC_MspDeInitCallback : TIM IC Msp DeInit Callback.
122 (+) OC_MspInitCallback : TIM OC Msp Init Callback.
123 (+) OC_MspDeInitCallback : TIM OC Msp DeInit Callback.
124 (+) PWM_MspInitCallback : TIM PWM Msp Init Callback.
125 (+) PWM_MspDeInitCallback : TIM PWM Msp DeInit Callback.
126 (+) OnePulse_MspInitCallback : TIM One Pulse Msp Init Callback.
127 (+) OnePulse_MspDeInitCallback : TIM One Pulse Msp DeInit Callback.
128 (+) Encoder_MspInitCallback : TIM Encoder Msp Init Callback.
129 (+) Encoder_MspDeInitCallback : TIM Encoder Msp DeInit Callback.
130 (+) HallSensor_MspInitCallback : TIM Hall Sensor Msp Init Callback.
131 (+) HallSensor_MspDeInitCallback : TIM Hall Sensor Msp DeInit Callback.
132 (+) PeriodElapsedCallback : TIM Period Elapsed Callback.
133 (+) PeriodElapsedHalfCpltCallback : TIM Period Elapsed half complete Callback.
134 (+) TriggerCallback : TIM Trigger Callback.
135 (+) TriggerHalfCpltCallback : TIM Trigger half complete Callback.
136 (+) IC_CaptureCallback : TIM Input Capture Callback.
137 (+) IC_CaptureHalfCpltCallback : TIM Input Capture half complete Callback.
138 (+) OC_DelayElapsedCallback : TIM Output Compare Delay Elapsed Callback.
139 (+) PWM_PulseFinishedCallback : TIM PWM Pulse Finished Callback.
140 (+) PWM_PulseFinishedHalfCpltCallback : TIM PWM Pulse Finished half complete Callback.
141 (+) ErrorCallback : TIM Error Callback.
142 (+) CommutationCallback : TIM Commutation Callback.
143 (+) CommutationHalfCpltCallback : TIM Commutation half complete Callback.
144 (+) BreakCallback : TIM Break Callback.
145 (+) Break2Callback : TIM Break2 Callback.
147 [..]
148 By default, after the Init and when the state is HAL_TIM_STATE_RESET
149 all interrupt callbacks are set to the corresponding weak functions:
150 examples @ref HAL_TIM_TriggerCallback(), @ref HAL_TIM_ErrorCallback().
152 [..]
153 Exception done for MspInit and MspDeInit functions that are reset to the legacy weak
154 functionalities in the Init / DeInit only when these callbacks are null
155 (not registered beforehand). If not, MspInit or MspDeInit are not null, the Init / DeInit
156 keep and use the user MspInit / MspDeInit callbacks(registered beforehand)
158 [..]
159 Callbacks can be registered / unregistered in HAL_TIM_STATE_READY state only.
160 Exception done MspInit / MspDeInit that can be registered / unregistered
161 in HAL_TIM_STATE_READY or HAL_TIM_STATE_RESET state,
162 thus registered(user) MspInit / DeInit callbacks can be used during the Init / DeInit.
163 In that case first register the MspInit/MspDeInit user callbacks
164 using @ref HAL_TIM_RegisterCallback() before calling DeInit or Init function.
166 [..]
167 When The compilation define USE_HAL_TIM_REGISTER_CALLBACKS is set to 0 or
168 not defined, the callback registration feature is not available and all callbacks
169 are set to the corresponding weak functions.
171 @endverbatim
172 ******************************************************************************
173 * @attention
175 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
176 * All rights reserved.</center></h2>
178 * This software component is licensed by ST under BSD 3-Clause license,
179 * the "License"; You may not use this file except in compliance with the
180 * License. You may obtain a copy of the License at:
181 * opensource.org/licenses/BSD-3-Clause
183 ******************************************************************************
186 /* Includes ------------------------------------------------------------------*/
187 #include "stm32h7xx_hal.h"
189 /** @addtogroup STM32H7xx_HAL_Driver
190 * @{
193 /** @defgroup TIM TIM
194 * @brief TIM HAL module driver
195 * @{
198 #ifdef HAL_TIM_MODULE_ENABLED
200 /* Private typedef -----------------------------------------------------------*/
201 /* Private define ------------------------------------------------------------*/
202 /* Private macro -------------------------------------------------------------*/
203 /* Private variables ---------------------------------------------------------*/
204 /* Private function prototypes -----------------------------------------------*/
205 /** @addtogroup TIM_Private_Functions
206 * @{
208 static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
209 static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
210 static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
211 static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
212 static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
213 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
214 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
215 uint32_t TIM_ICFilter);
216 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
217 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
218 uint32_t TIM_ICFilter);
219 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
220 uint32_t TIM_ICFilter);
221 static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource);
222 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma);
223 static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma);
224 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);
225 static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma);
226 static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
227 TIM_SlaveConfigTypeDef *sSlaveConfig);
229 * @}
231 /* Exported functions --------------------------------------------------------*/
233 /** @defgroup TIM_Exported_Functions TIM Exported Functions
234 * @{
237 /** @defgroup TIM_Exported_Functions_Group1 TIM Time Base functions
238 * @brief Time Base functions
240 @verbatim
241 ==============================================================================
242 ##### Time Base functions #####
243 ==============================================================================
244 [..]
245 This section provides functions allowing to:
246 (+) Initialize and configure the TIM base.
247 (+) De-initialize the TIM base.
248 (+) Start the Time Base.
249 (+) Stop the Time Base.
250 (+) Start the Time Base and enable interrupt.
251 (+) Stop the Time Base and disable interrupt.
252 (+) Start the Time Base and enable DMA transfer.
253 (+) Stop the Time Base and disable DMA transfer.
255 @endverbatim
256 * @{
259 * @brief Initializes the TIM Time base Unit according to the specified
260 * parameters in the TIM_HandleTypeDef and initialize the associated handle.
261 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
262 * requires a timer reset to avoid unexpected direction
263 * due to DIR bit readonly in center aligned mode.
264 * Ex: call @ref HAL_TIM_Base_DeInit() before HAL_TIM_Base_Init()
265 * @param htim TIM Base handle
266 * @retval HAL status
268 HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
270 /* Check the TIM handle allocation */
271 if (htim == NULL)
273 return HAL_ERROR;
276 /* Check the parameters */
277 assert_param(IS_TIM_INSTANCE(htim->Instance));
278 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
279 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
280 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
282 if (htim->State == HAL_TIM_STATE_RESET)
284 /* Allocate lock resource and initialize it */
285 htim->Lock = HAL_UNLOCKED;
287 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
288 /* Reset interrupt callbacks to legacy weak callbacks */
289 TIM_ResetCallback(htim);
291 if (htim->Base_MspInitCallback == NULL)
293 htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
295 /* Init the low level hardware : GPIO, CLOCK, NVIC */
296 htim->Base_MspInitCallback(htim);
297 #else
298 /* Init the low level hardware : GPIO, CLOCK, NVIC */
299 HAL_TIM_Base_MspInit(htim);
300 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
303 /* Set the TIM state */
304 htim->State = HAL_TIM_STATE_BUSY;
306 /* Set the Time Base configuration */
307 TIM_Base_SetConfig(htim->Instance, &htim->Init);
309 /* Initialize the TIM state*/
310 htim->State = HAL_TIM_STATE_READY;
312 return HAL_OK;
316 * @brief DeInitializes the TIM Base peripheral
317 * @param htim TIM Base handle
318 * @retval HAL status
320 HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)
322 /* Check the parameters */
323 assert_param(IS_TIM_INSTANCE(htim->Instance));
325 htim->State = HAL_TIM_STATE_BUSY;
327 /* Disable the TIM Peripheral Clock */
328 __HAL_TIM_DISABLE(htim);
330 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
331 if (htim->Base_MspDeInitCallback == NULL)
333 htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
335 /* DeInit the low level hardware */
336 htim->Base_MspDeInitCallback(htim);
337 #else
338 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
339 HAL_TIM_Base_MspDeInit(htim);
340 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
342 /* Change TIM state */
343 htim->State = HAL_TIM_STATE_RESET;
345 /* Release Lock */
346 __HAL_UNLOCK(htim);
348 return HAL_OK;
352 * @brief Initializes the TIM Base MSP.
353 * @param htim TIM Base handle
354 * @retval None
356 __weak void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
358 /* Prevent unused argument(s) compilation warning */
359 UNUSED(htim);
361 /* NOTE : This function should not be modified, when the callback is needed,
362 the HAL_TIM_Base_MspInit could be implemented in the user file
367 * @brief DeInitializes TIM Base MSP.
368 * @param htim TIM Base handle
369 * @retval None
371 __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
373 /* Prevent unused argument(s) compilation warning */
374 UNUSED(htim);
376 /* NOTE : This function should not be modified, when the callback is needed,
377 the HAL_TIM_Base_MspDeInit could be implemented in the user file
383 * @brief Starts the TIM Base generation.
384 * @param htim TIM Base handle
385 * @retval HAL status
387 HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)
389 uint32_t tmpsmcr;
391 /* Check the parameters */
392 assert_param(IS_TIM_INSTANCE(htim->Instance));
394 /* Set the TIM state */
395 htim->State = HAL_TIM_STATE_BUSY;
397 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
398 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
399 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
401 __HAL_TIM_ENABLE(htim);
404 /* Change the TIM state*/
405 htim->State = HAL_TIM_STATE_READY;
407 /* Return function status */
408 return HAL_OK;
412 * @brief Stops the TIM Base generation.
413 * @param htim TIM Base handle
414 * @retval HAL status
416 HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)
418 /* Check the parameters */
419 assert_param(IS_TIM_INSTANCE(htim->Instance));
421 /* Set the TIM state */
422 htim->State = HAL_TIM_STATE_BUSY;
424 /* Disable the Peripheral */
425 __HAL_TIM_DISABLE(htim);
427 /* Change the TIM state*/
428 htim->State = HAL_TIM_STATE_READY;
430 /* Return function status */
431 return HAL_OK;
435 * @brief Starts the TIM Base generation in interrupt mode.
436 * @param htim TIM Base handle
437 * @retval HAL status
439 HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
441 uint32_t tmpsmcr;
443 /* Check the parameters */
444 assert_param(IS_TIM_INSTANCE(htim->Instance));
446 /* Enable the TIM Update interrupt */
447 __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
449 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
450 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
451 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
453 __HAL_TIM_ENABLE(htim);
456 /* Return function status */
457 return HAL_OK;
461 * @brief Stops the TIM Base generation in interrupt mode.
462 * @param htim TIM Base handle
463 * @retval HAL status
465 HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)
467 /* Check the parameters */
468 assert_param(IS_TIM_INSTANCE(htim->Instance));
469 /* Disable the TIM Update interrupt */
470 __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);
472 /* Disable the Peripheral */
473 __HAL_TIM_DISABLE(htim);
475 /* Return function status */
476 return HAL_OK;
480 * @brief Starts the TIM Base generation in DMA mode.
481 * @param htim TIM Base handle
482 * @param pData The source Buffer address.
483 * @param Length The length of data to be transferred from memory to peripheral.
484 * @retval HAL status
486 HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
488 uint32_t tmpsmcr;
490 /* Check the parameters */
491 assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
493 if (htim->State == HAL_TIM_STATE_BUSY)
495 return HAL_BUSY;
497 else if (htim->State == HAL_TIM_STATE_READY)
499 if ((pData == NULL) && (Length > 0U))
501 return HAL_ERROR;
503 else
505 htim->State = HAL_TIM_STATE_BUSY;
508 else
510 /* nothing to do */
513 /* Set the DMA Period elapsed callbacks */
514 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
515 htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
517 /* Set the DMA error callback */
518 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
520 /* Enable the DMA stream */
521 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR, Length) != HAL_OK)
523 return HAL_ERROR;
526 /* Enable the TIM Update DMA request */
527 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);
529 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
530 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
531 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
533 __HAL_TIM_ENABLE(htim);
536 /* Return function status */
537 return HAL_OK;
541 * @brief Stops the TIM Base generation in DMA mode.
542 * @param htim TIM Base handle
543 * @retval HAL status
545 HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)
547 /* Check the parameters */
548 assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
550 /* Disable the TIM Update DMA request */
551 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);
553 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
555 /* Disable the Peripheral */
556 __HAL_TIM_DISABLE(htim);
558 /* Change the htim state */
559 htim->State = HAL_TIM_STATE_READY;
561 /* Return function status */
562 return HAL_OK;
566 * @}
569 /** @defgroup TIM_Exported_Functions_Group2 TIM Output Compare functions
570 * @brief TIM Output Compare functions
572 @verbatim
573 ==============================================================================
574 ##### TIM Output Compare functions #####
575 ==============================================================================
576 [..]
577 This section provides functions allowing to:
578 (+) Initialize and configure the TIM Output Compare.
579 (+) De-initialize the TIM Output Compare.
580 (+) Start the TIM Output Compare.
581 (+) Stop the TIM Output Compare.
582 (+) Start the TIM Output Compare and enable interrupt.
583 (+) Stop the TIM Output Compare and disable interrupt.
584 (+) Start the TIM Output Compare and enable DMA transfer.
585 (+) Stop the TIM Output Compare and disable DMA transfer.
587 @endverbatim
588 * @{
591 * @brief Initializes the TIM Output Compare according to the specified
592 * parameters in the TIM_HandleTypeDef and initializes the associated handle.
593 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
594 * requires a timer reset to avoid unexpected direction
595 * due to DIR bit readonly in center aligned mode.
596 * Ex: call @ref HAL_TIM_OC_DeInit() before HAL_TIM_OC_Init()
597 * @param htim TIM Output Compare handle
598 * @retval HAL status
600 HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim)
602 /* Check the TIM handle allocation */
603 if (htim == NULL)
605 return HAL_ERROR;
608 /* Check the parameters */
609 assert_param(IS_TIM_INSTANCE(htim->Instance));
610 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
611 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
612 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
614 if (htim->State == HAL_TIM_STATE_RESET)
616 /* Allocate lock resource and initialize it */
617 htim->Lock = HAL_UNLOCKED;
619 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
620 /* Reset interrupt callbacks to legacy weak callbacks */
621 TIM_ResetCallback(htim);
623 if (htim->OC_MspInitCallback == NULL)
625 htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
627 /* Init the low level hardware : GPIO, CLOCK, NVIC */
628 htim->OC_MspInitCallback(htim);
629 #else
630 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
631 HAL_TIM_OC_MspInit(htim);
632 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
635 /* Set the TIM state */
636 htim->State = HAL_TIM_STATE_BUSY;
638 /* Init the base time for the Output Compare */
639 TIM_Base_SetConfig(htim->Instance, &htim->Init);
641 /* Initialize the TIM state*/
642 htim->State = HAL_TIM_STATE_READY;
644 return HAL_OK;
648 * @brief DeInitializes the TIM peripheral
649 * @param htim TIM Output Compare handle
650 * @retval HAL status
652 HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)
654 /* Check the parameters */
655 assert_param(IS_TIM_INSTANCE(htim->Instance));
657 htim->State = HAL_TIM_STATE_BUSY;
659 /* Disable the TIM Peripheral Clock */
660 __HAL_TIM_DISABLE(htim);
662 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
663 if (htim->OC_MspDeInitCallback == NULL)
665 htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
667 /* DeInit the low level hardware */
668 htim->OC_MspDeInitCallback(htim);
669 #else
670 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
671 HAL_TIM_OC_MspDeInit(htim);
672 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
674 /* Change TIM state */
675 htim->State = HAL_TIM_STATE_RESET;
677 /* Release Lock */
678 __HAL_UNLOCK(htim);
680 return HAL_OK;
684 * @brief Initializes the TIM Output Compare MSP.
685 * @param htim TIM Output Compare handle
686 * @retval None
688 __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
690 /* Prevent unused argument(s) compilation warning */
691 UNUSED(htim);
693 /* NOTE : This function should not be modified, when the callback is needed,
694 the HAL_TIM_OC_MspInit could be implemented in the user file
699 * @brief DeInitializes TIM Output Compare MSP.
700 * @param htim TIM Output Compare handle
701 * @retval None
703 __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
705 /* Prevent unused argument(s) compilation warning */
706 UNUSED(htim);
708 /* NOTE : This function should not be modified, when the callback is needed,
709 the HAL_TIM_OC_MspDeInit could be implemented in the user file
714 * @brief Starts the TIM Output Compare signal generation.
715 * @param htim TIM Output Compare handle
716 * @param Channel TIM Channel to be enabled
717 * This parameter can be one of the following values:
718 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
719 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
720 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
721 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
722 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
723 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
724 * @retval HAL status
726 HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
728 uint32_t tmpsmcr;
730 /* Check the parameters */
731 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
733 /* Enable the Output compare channel */
734 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
736 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
738 /* Enable the main output */
739 __HAL_TIM_MOE_ENABLE(htim);
742 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
743 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
744 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
746 __HAL_TIM_ENABLE(htim);
749 /* Return function status */
750 return HAL_OK;
754 * @brief Stops the TIM Output Compare signal generation.
755 * @param htim TIM Output Compare handle
756 * @param Channel TIM Channel to be disabled
757 * This parameter can be one of the following values:
758 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
759 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
760 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
761 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
762 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
763 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
764 * @retval HAL status
766 HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
768 /* Check the parameters */
769 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
771 /* Disable the Output compare channel */
772 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
774 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
776 /* Disable the Main Output */
777 __HAL_TIM_MOE_DISABLE(htim);
780 /* Disable the Peripheral */
781 __HAL_TIM_DISABLE(htim);
783 /* Return function status */
784 return HAL_OK;
788 * @brief Starts the TIM Output Compare signal generation in interrupt mode.
789 * @param htim TIM Output Compare handle
790 * @param Channel TIM Channel to be enabled
791 * This parameter can be one of the following values:
792 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
793 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
794 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
795 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
796 * @retval HAL status
798 HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
800 uint32_t tmpsmcr;
802 /* Check the parameters */
803 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
805 switch (Channel)
807 case TIM_CHANNEL_1:
809 /* Enable the TIM Capture/Compare 1 interrupt */
810 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
811 break;
814 case TIM_CHANNEL_2:
816 /* Enable the TIM Capture/Compare 2 interrupt */
817 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
818 break;
821 case TIM_CHANNEL_3:
823 /* Enable the TIM Capture/Compare 3 interrupt */
824 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
825 break;
828 case TIM_CHANNEL_4:
830 /* Enable the TIM Capture/Compare 4 interrupt */
831 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
832 break;
835 default:
836 break;
839 /* Enable the Output compare channel */
840 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
842 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
844 /* Enable the main output */
845 __HAL_TIM_MOE_ENABLE(htim);
848 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
849 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
850 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
852 __HAL_TIM_ENABLE(htim);
855 /* Return function status */
856 return HAL_OK;
860 * @brief Stops the TIM Output Compare signal generation in interrupt mode.
861 * @param htim TIM Output Compare handle
862 * @param Channel TIM Channel to be disabled
863 * This parameter can be one of the following values:
864 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
865 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
866 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
867 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
868 * @retval HAL status
870 HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
872 /* Check the parameters */
873 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
875 switch (Channel)
877 case TIM_CHANNEL_1:
879 /* Disable the TIM Capture/Compare 1 interrupt */
880 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
881 break;
884 case TIM_CHANNEL_2:
886 /* Disable the TIM Capture/Compare 2 interrupt */
887 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
888 break;
891 case TIM_CHANNEL_3:
893 /* Disable the TIM Capture/Compare 3 interrupt */
894 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
895 break;
898 case TIM_CHANNEL_4:
900 /* Disable the TIM Capture/Compare 4 interrupt */
901 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
902 break;
905 default:
906 break;
909 /* Disable the Output compare channel */
910 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
912 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
914 /* Disable the Main Output */
915 __HAL_TIM_MOE_DISABLE(htim);
918 /* Disable the Peripheral */
919 __HAL_TIM_DISABLE(htim);
921 /* Return function status */
922 return HAL_OK;
926 * @brief Starts the TIM Output Compare signal generation in DMA mode.
927 * @param htim TIM Output Compare handle
928 * @param Channel TIM Channel to be enabled
929 * This parameter can be one of the following values:
930 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
931 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
932 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
933 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
934 * @param pData The source Buffer address.
935 * @param Length The length of data to be transferred from memory to TIM peripheral
936 * @retval HAL status
938 HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
940 uint32_t tmpsmcr;
942 /* Check the parameters */
943 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
945 if (htim->State == HAL_TIM_STATE_BUSY)
947 return HAL_BUSY;
949 else if (htim->State == HAL_TIM_STATE_READY)
951 if ((pData == NULL) && (Length > 0U))
953 return HAL_ERROR;
955 else
957 htim->State = HAL_TIM_STATE_BUSY;
960 else
962 /* nothing to do */
965 switch (Channel)
967 case TIM_CHANNEL_1:
969 /* Set the DMA compare callbacks */
970 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
971 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
973 /* Set the DMA error callback */
974 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
976 /* Enable the DMA stream */
977 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
979 return HAL_ERROR;
982 /* Enable the TIM Capture/Compare 1 DMA request */
983 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
984 break;
987 case TIM_CHANNEL_2:
989 /* Set the DMA compare callbacks */
990 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
991 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
993 /* Set the DMA error callback */
994 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
996 /* Enable the DMA stream */
997 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
999 return HAL_ERROR;
1002 /* Enable the TIM Capture/Compare 2 DMA request */
1003 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1004 break;
1007 case TIM_CHANNEL_3:
1009 /* Set the DMA compare callbacks */
1010 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
1011 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1013 /* Set the DMA error callback */
1014 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
1016 /* Enable the DMA stream */
1017 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
1019 return HAL_ERROR;
1021 /* Enable the TIM Capture/Compare 3 DMA request */
1022 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1023 break;
1026 case TIM_CHANNEL_4:
1028 /* Set the DMA compare callbacks */
1029 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
1030 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1032 /* Set the DMA error callback */
1033 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
1035 /* Enable the DMA stream */
1036 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length) != HAL_OK)
1038 return HAL_ERROR;
1040 /* Enable the TIM Capture/Compare 4 DMA request */
1041 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1042 break;
1045 default:
1046 break;
1049 /* Enable the Output compare channel */
1050 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1052 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1054 /* Enable the main output */
1055 __HAL_TIM_MOE_ENABLE(htim);
1058 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1059 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1060 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1062 __HAL_TIM_ENABLE(htim);
1065 /* Return function status */
1066 return HAL_OK;
1070 * @brief Stops the TIM Output Compare signal generation in DMA mode.
1071 * @param htim TIM Output Compare handle
1072 * @param Channel TIM Channel to be disabled
1073 * This parameter can be one of the following values:
1074 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1075 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1076 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1077 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1078 * @retval HAL status
1080 HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1082 /* Check the parameters */
1083 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1085 switch (Channel)
1087 case TIM_CHANNEL_1:
1089 /* Disable the TIM Capture/Compare 1 DMA request */
1090 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1091 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1092 break;
1095 case TIM_CHANNEL_2:
1097 /* Disable the TIM Capture/Compare 2 DMA request */
1098 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1099 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1100 break;
1103 case TIM_CHANNEL_3:
1105 /* Disable the TIM Capture/Compare 3 DMA request */
1106 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1107 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1108 break;
1111 case TIM_CHANNEL_4:
1113 /* Disable the TIM Capture/Compare 4 interrupt */
1114 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1115 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
1116 break;
1119 default:
1120 break;
1123 /* Disable the Output compare channel */
1124 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1126 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1128 /* Disable the Main Output */
1129 __HAL_TIM_MOE_DISABLE(htim);
1132 /* Disable the Peripheral */
1133 __HAL_TIM_DISABLE(htim);
1135 /* Change the htim state */
1136 htim->State = HAL_TIM_STATE_READY;
1138 /* Return function status */
1139 return HAL_OK;
1143 * @}
1146 /** @defgroup TIM_Exported_Functions_Group3 TIM PWM functions
1147 * @brief TIM PWM functions
1149 @verbatim
1150 ==============================================================================
1151 ##### TIM PWM functions #####
1152 ==============================================================================
1153 [..]
1154 This section provides functions allowing to:
1155 (+) Initialize and configure the TIM PWM.
1156 (+) De-initialize the TIM PWM.
1157 (+) Start the TIM PWM.
1158 (+) Stop the TIM PWM.
1159 (+) Start the TIM PWM and enable interrupt.
1160 (+) Stop the TIM PWM and disable interrupt.
1161 (+) Start the TIM PWM and enable DMA transfer.
1162 (+) Stop the TIM PWM and disable DMA transfer.
1164 @endverbatim
1165 * @{
1168 * @brief Initializes the TIM PWM Time Base according to the specified
1169 * parameters in the TIM_HandleTypeDef and initializes the associated handle.
1170 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
1171 * requires a timer reset to avoid unexpected direction
1172 * due to DIR bit readonly in center aligned mode.
1173 * Ex: call @ref HAL_TIM_PWM_DeInit() before HAL_TIM_PWM_Init()
1174 * @param htim TIM PWM handle
1175 * @retval HAL status
1177 HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)
1179 /* Check the TIM handle allocation */
1180 if (htim == NULL)
1182 return HAL_ERROR;
1185 /* Check the parameters */
1186 assert_param(IS_TIM_INSTANCE(htim->Instance));
1187 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
1188 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
1189 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
1191 if (htim->State == HAL_TIM_STATE_RESET)
1193 /* Allocate lock resource and initialize it */
1194 htim->Lock = HAL_UNLOCKED;
1196 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1197 /* Reset interrupt callbacks to legacy weak callbacks */
1198 TIM_ResetCallback(htim);
1200 if (htim->PWM_MspInitCallback == NULL)
1202 htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
1204 /* Init the low level hardware : GPIO, CLOCK, NVIC */
1205 htim->PWM_MspInitCallback(htim);
1206 #else
1207 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1208 HAL_TIM_PWM_MspInit(htim);
1209 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1212 /* Set the TIM state */
1213 htim->State = HAL_TIM_STATE_BUSY;
1215 /* Init the base time for the PWM */
1216 TIM_Base_SetConfig(htim->Instance, &htim->Init);
1218 /* Initialize the TIM state*/
1219 htim->State = HAL_TIM_STATE_READY;
1221 return HAL_OK;
1225 * @brief DeInitializes the TIM peripheral
1226 * @param htim TIM PWM handle
1227 * @retval HAL status
1229 HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)
1231 /* Check the parameters */
1232 assert_param(IS_TIM_INSTANCE(htim->Instance));
1234 htim->State = HAL_TIM_STATE_BUSY;
1236 /* Disable the TIM Peripheral Clock */
1237 __HAL_TIM_DISABLE(htim);
1239 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1240 if (htim->PWM_MspDeInitCallback == NULL)
1242 htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
1244 /* DeInit the low level hardware */
1245 htim->PWM_MspDeInitCallback(htim);
1246 #else
1247 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1248 HAL_TIM_PWM_MspDeInit(htim);
1249 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1251 /* Change TIM state */
1252 htim->State = HAL_TIM_STATE_RESET;
1254 /* Release Lock */
1255 __HAL_UNLOCK(htim);
1257 return HAL_OK;
1261 * @brief Initializes the TIM PWM MSP.
1262 * @param htim TIM PWM handle
1263 * @retval None
1265 __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
1267 /* Prevent unused argument(s) compilation warning */
1268 UNUSED(htim);
1270 /* NOTE : This function should not be modified, when the callback is needed,
1271 the HAL_TIM_PWM_MspInit could be implemented in the user file
1276 * @brief DeInitializes TIM PWM MSP.
1277 * @param htim TIM PWM handle
1278 * @retval None
1280 __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)
1282 /* Prevent unused argument(s) compilation warning */
1283 UNUSED(htim);
1285 /* NOTE : This function should not be modified, when the callback is needed,
1286 the HAL_TIM_PWM_MspDeInit could be implemented in the user file
1291 * @brief Starts the PWM signal generation.
1292 * @param htim TIM handle
1293 * @param Channel TIM Channels to be enabled
1294 * This parameter can be one of the following values:
1295 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1296 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1297 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1298 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1299 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
1300 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
1301 * @retval HAL status
1303 HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
1305 uint32_t tmpsmcr;
1307 /* Check the parameters */
1308 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1310 /* Enable the Capture compare channel */
1311 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1313 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1315 /* Enable the main output */
1316 __HAL_TIM_MOE_ENABLE(htim);
1319 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1320 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1321 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1323 __HAL_TIM_ENABLE(htim);
1326 /* Return function status */
1327 return HAL_OK;
1331 * @brief Stops the PWM signal generation.
1332 * @param htim TIM PWM handle
1333 * @param Channel TIM Channels to be disabled
1334 * This parameter can be one of the following values:
1335 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1336 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1337 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1338 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1339 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
1340 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
1341 * @retval HAL status
1343 HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
1345 /* Check the parameters */
1346 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1348 /* Disable the Capture compare channel */
1349 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1351 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1353 /* Disable the Main Output */
1354 __HAL_TIM_MOE_DISABLE(htim);
1357 /* Disable the Peripheral */
1358 __HAL_TIM_DISABLE(htim);
1360 /* Change the htim state */
1361 htim->State = HAL_TIM_STATE_READY;
1363 /* Return function status */
1364 return HAL_OK;
1368 * @brief Starts the PWM signal generation in interrupt mode.
1369 * @param htim TIM PWM handle
1370 * @param Channel TIM Channel to be enabled
1371 * This parameter can be one of the following values:
1372 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1373 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1374 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1375 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1376 * @retval HAL status
1378 HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1380 uint32_t tmpsmcr;
1381 /* Check the parameters */
1382 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1384 switch (Channel)
1386 case TIM_CHANNEL_1:
1388 /* Enable the TIM Capture/Compare 1 interrupt */
1389 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1390 break;
1393 case TIM_CHANNEL_2:
1395 /* Enable the TIM Capture/Compare 2 interrupt */
1396 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1397 break;
1400 case TIM_CHANNEL_3:
1402 /* Enable the TIM Capture/Compare 3 interrupt */
1403 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1404 break;
1407 case TIM_CHANNEL_4:
1409 /* Enable the TIM Capture/Compare 4 interrupt */
1410 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
1411 break;
1414 default:
1415 break;
1418 /* Enable the Capture compare channel */
1419 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1421 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1423 /* Enable the main output */
1424 __HAL_TIM_MOE_ENABLE(htim);
1427 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1428 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1429 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1431 __HAL_TIM_ENABLE(htim);
1434 /* Return function status */
1435 return HAL_OK;
1439 * @brief Stops the PWM signal generation in interrupt mode.
1440 * @param htim TIM PWM handle
1441 * @param Channel TIM Channels to be disabled
1442 * This parameter can be one of the following values:
1443 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1444 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1445 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1446 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1447 * @retval HAL status
1449 HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1451 /* Check the parameters */
1452 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1454 switch (Channel)
1456 case TIM_CHANNEL_1:
1458 /* Disable the TIM Capture/Compare 1 interrupt */
1459 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1460 break;
1463 case TIM_CHANNEL_2:
1465 /* Disable the TIM Capture/Compare 2 interrupt */
1466 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1467 break;
1470 case TIM_CHANNEL_3:
1472 /* Disable the TIM Capture/Compare 3 interrupt */
1473 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1474 break;
1477 case TIM_CHANNEL_4:
1479 /* Disable the TIM Capture/Compare 4 interrupt */
1480 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
1481 break;
1484 default:
1485 break;
1488 /* Disable the Capture compare channel */
1489 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1491 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1493 /* Disable the Main Output */
1494 __HAL_TIM_MOE_DISABLE(htim);
1497 /* Disable the Peripheral */
1498 __HAL_TIM_DISABLE(htim);
1500 /* Return function status */
1501 return HAL_OK;
1505 * @brief Starts the TIM PWM signal generation in DMA mode.
1506 * @param htim TIM PWM handle
1507 * @param Channel TIM Channels to be enabled
1508 * This parameter can be one of the following values:
1509 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1510 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1511 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1512 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1513 * @param pData The source Buffer address.
1514 * @param Length The length of data to be transferred from memory to TIM peripheral
1515 * @retval HAL status
1517 HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
1519 uint32_t tmpsmcr;
1521 /* Check the parameters */
1522 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1524 if (htim->State == HAL_TIM_STATE_BUSY)
1526 return HAL_BUSY;
1528 else if (htim->State == HAL_TIM_STATE_READY)
1530 if ((pData == NULL) && (Length > 0U))
1532 return HAL_ERROR;
1534 else
1536 htim->State = HAL_TIM_STATE_BUSY;
1539 else
1541 /* nothing to do */
1544 switch (Channel)
1546 case TIM_CHANNEL_1:
1548 /* Set the DMA compare callbacks */
1549 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
1550 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1552 /* Set the DMA error callback */
1553 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
1555 /* Enable the DMA stream */
1556 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
1558 return HAL_ERROR;
1561 /* Enable the TIM Capture/Compare 1 DMA request */
1562 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1563 break;
1566 case TIM_CHANNEL_2:
1568 /* Set the DMA compare callbacks */
1569 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
1570 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1572 /* Set the DMA error callback */
1573 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
1575 /* Enable the DMA stream */
1576 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
1578 return HAL_ERROR;
1580 /* Enable the TIM Capture/Compare 2 DMA request */
1581 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1582 break;
1585 case TIM_CHANNEL_3:
1587 /* Set the DMA compare callbacks */
1588 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
1589 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1591 /* Set the DMA error callback */
1592 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
1594 /* Enable the DMA stream */
1595 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
1597 return HAL_ERROR;
1599 /* Enable the TIM Output Capture/Compare 3 request */
1600 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1601 break;
1604 case TIM_CHANNEL_4:
1606 /* Set the DMA compare callbacks */
1607 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
1608 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1610 /* Set the DMA error callback */
1611 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
1613 /* Enable the DMA stream */
1614 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length) != HAL_OK)
1616 return HAL_ERROR;
1618 /* Enable the TIM Capture/Compare 4 DMA request */
1619 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1620 break;
1623 default:
1624 break;
1627 /* Enable the Capture compare channel */
1628 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1630 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1632 /* Enable the main output */
1633 __HAL_TIM_MOE_ENABLE(htim);
1636 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1637 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1638 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1640 __HAL_TIM_ENABLE(htim);
1643 /* Return function status */
1644 return HAL_OK;
1648 * @brief Stops the TIM PWM signal generation in DMA mode.
1649 * @param htim TIM PWM handle
1650 * @param Channel TIM Channels to be disabled
1651 * This parameter can be one of the following values:
1652 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1653 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1654 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1655 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1656 * @retval HAL status
1658 HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1660 /* Check the parameters */
1661 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1663 switch (Channel)
1665 case TIM_CHANNEL_1:
1667 /* Disable the TIM Capture/Compare 1 DMA request */
1668 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1669 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1670 break;
1673 case TIM_CHANNEL_2:
1675 /* Disable the TIM Capture/Compare 2 DMA request */
1676 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1677 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1678 break;
1681 case TIM_CHANNEL_3:
1683 /* Disable the TIM Capture/Compare 3 DMA request */
1684 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1685 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1686 break;
1689 case TIM_CHANNEL_4:
1691 /* Disable the TIM Capture/Compare 4 interrupt */
1692 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1693 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
1694 break;
1697 default:
1698 break;
1701 /* Disable the Capture compare channel */
1702 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1704 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1706 /* Disable the Main Output */
1707 __HAL_TIM_MOE_DISABLE(htim);
1710 /* Disable the Peripheral */
1711 __HAL_TIM_DISABLE(htim);
1713 /* Change the htim state */
1714 htim->State = HAL_TIM_STATE_READY;
1716 /* Return function status */
1717 return HAL_OK;
1721 * @}
1724 /** @defgroup TIM_Exported_Functions_Group4 TIM Input Capture functions
1725 * @brief TIM Input Capture functions
1727 @verbatim
1728 ==============================================================================
1729 ##### TIM Input Capture functions #####
1730 ==============================================================================
1731 [..]
1732 This section provides functions allowing to:
1733 (+) Initialize and configure the TIM Input Capture.
1734 (+) De-initialize the TIM Input Capture.
1735 (+) Start the TIM Input Capture.
1736 (+) Stop the TIM Input Capture.
1737 (+) Start the TIM Input Capture and enable interrupt.
1738 (+) Stop the TIM Input Capture and disable interrupt.
1739 (+) Start the TIM Input Capture and enable DMA transfer.
1740 (+) Stop the TIM Input Capture and disable DMA transfer.
1742 @endverbatim
1743 * @{
1746 * @brief Initializes the TIM Input Capture Time base according to the specified
1747 * parameters in the TIM_HandleTypeDef and initializes the associated handle.
1748 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
1749 * requires a timer reset to avoid unexpected direction
1750 * due to DIR bit readonly in center aligned mode.
1751 * Ex: call @ref HAL_TIM_IC_DeInit() before HAL_TIM_IC_Init()
1752 * @param htim TIM Input Capture handle
1753 * @retval HAL status
1755 HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)
1757 /* Check the TIM handle allocation */
1758 if (htim == NULL)
1760 return HAL_ERROR;
1763 /* Check the parameters */
1764 assert_param(IS_TIM_INSTANCE(htim->Instance));
1765 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
1766 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
1767 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
1769 if (htim->State == HAL_TIM_STATE_RESET)
1771 /* Allocate lock resource and initialize it */
1772 htim->Lock = HAL_UNLOCKED;
1774 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1775 /* Reset interrupt callbacks to legacy weak callbacks */
1776 TIM_ResetCallback(htim);
1778 if (htim->IC_MspInitCallback == NULL)
1780 htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
1782 /* Init the low level hardware : GPIO, CLOCK, NVIC */
1783 htim->IC_MspInitCallback(htim);
1784 #else
1785 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1786 HAL_TIM_IC_MspInit(htim);
1787 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1790 /* Set the TIM state */
1791 htim->State = HAL_TIM_STATE_BUSY;
1793 /* Init the base time for the input capture */
1794 TIM_Base_SetConfig(htim->Instance, &htim->Init);
1796 /* Initialize the TIM state*/
1797 htim->State = HAL_TIM_STATE_READY;
1799 return HAL_OK;
1803 * @brief DeInitializes the TIM peripheral
1804 * @param htim TIM Input Capture handle
1805 * @retval HAL status
1807 HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)
1809 /* Check the parameters */
1810 assert_param(IS_TIM_INSTANCE(htim->Instance));
1812 htim->State = HAL_TIM_STATE_BUSY;
1814 /* Disable the TIM Peripheral Clock */
1815 __HAL_TIM_DISABLE(htim);
1817 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1818 if (htim->IC_MspDeInitCallback == NULL)
1820 htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
1822 /* DeInit the low level hardware */
1823 htim->IC_MspDeInitCallback(htim);
1824 #else
1825 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1826 HAL_TIM_IC_MspDeInit(htim);
1827 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1829 /* Change TIM state */
1830 htim->State = HAL_TIM_STATE_RESET;
1832 /* Release Lock */
1833 __HAL_UNLOCK(htim);
1835 return HAL_OK;
1839 * @brief Initializes the TIM Input Capture MSP.
1840 * @param htim TIM Input Capture handle
1841 * @retval None
1843 __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
1845 /* Prevent unused argument(s) compilation warning */
1846 UNUSED(htim);
1848 /* NOTE : This function should not be modified, when the callback is needed,
1849 the HAL_TIM_IC_MspInit could be implemented in the user file
1854 * @brief DeInitializes TIM Input Capture MSP.
1855 * @param htim TIM handle
1856 * @retval None
1858 __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)
1860 /* Prevent unused argument(s) compilation warning */
1861 UNUSED(htim);
1863 /* NOTE : This function should not be modified, when the callback is needed,
1864 the HAL_TIM_IC_MspDeInit could be implemented in the user file
1869 * @brief Starts the TIM Input Capture measurement.
1870 * @param htim TIM Input Capture handle
1871 * @param Channel TIM Channels to be enabled
1872 * This parameter can be one of the following values:
1873 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1874 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1875 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1876 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1877 * @retval HAL status
1879 HAL_StatusTypeDef HAL_TIM_IC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
1881 uint32_t tmpsmcr;
1883 /* Check the parameters */
1884 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1886 /* Enable the Input Capture channel */
1887 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1889 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1890 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1891 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1893 __HAL_TIM_ENABLE(htim);
1896 /* Return function status */
1897 return HAL_OK;
1901 * @brief Stops the TIM Input Capture measurement.
1902 * @param htim TIM Input Capture handle
1903 * @param Channel TIM Channels to be disabled
1904 * This parameter can be one of the following values:
1905 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1906 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1907 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1908 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1909 * @retval HAL status
1911 HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
1913 /* Check the parameters */
1914 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1916 /* Disable the Input Capture channel */
1917 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1919 /* Disable the Peripheral */
1920 __HAL_TIM_DISABLE(htim);
1922 /* Return function status */
1923 return HAL_OK;
1927 * @brief Starts the TIM Input Capture measurement in interrupt mode.
1928 * @param htim TIM Input Capture handle
1929 * @param Channel TIM Channels to be enabled
1930 * This parameter can be one of the following values:
1931 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1932 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1933 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1934 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1935 * @retval HAL status
1937 HAL_StatusTypeDef HAL_TIM_IC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1939 uint32_t tmpsmcr;
1941 /* Check the parameters */
1942 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1944 switch (Channel)
1946 case TIM_CHANNEL_1:
1948 /* Enable the TIM Capture/Compare 1 interrupt */
1949 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1950 break;
1953 case TIM_CHANNEL_2:
1955 /* Enable the TIM Capture/Compare 2 interrupt */
1956 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1957 break;
1960 case TIM_CHANNEL_3:
1962 /* Enable the TIM Capture/Compare 3 interrupt */
1963 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1964 break;
1967 case TIM_CHANNEL_4:
1969 /* Enable the TIM Capture/Compare 4 interrupt */
1970 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
1971 break;
1974 default:
1975 break;
1977 /* Enable the Input Capture channel */
1978 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1980 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1981 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1982 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1984 __HAL_TIM_ENABLE(htim);
1987 /* Return function status */
1988 return HAL_OK;
1992 * @brief Stops the TIM Input Capture measurement in interrupt mode.
1993 * @param htim TIM Input Capture handle
1994 * @param Channel TIM Channels to be disabled
1995 * This parameter can be one of the following values:
1996 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1997 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1998 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1999 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2000 * @retval HAL status
2002 HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2004 /* Check the parameters */
2005 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2007 switch (Channel)
2009 case TIM_CHANNEL_1:
2011 /* Disable the TIM Capture/Compare 1 interrupt */
2012 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2013 break;
2016 case TIM_CHANNEL_2:
2018 /* Disable the TIM Capture/Compare 2 interrupt */
2019 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2020 break;
2023 case TIM_CHANNEL_3:
2025 /* Disable the TIM Capture/Compare 3 interrupt */
2026 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
2027 break;
2030 case TIM_CHANNEL_4:
2032 /* Disable the TIM Capture/Compare 4 interrupt */
2033 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
2034 break;
2037 default:
2038 break;
2041 /* Disable the Input Capture channel */
2042 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
2044 /* Disable the Peripheral */
2045 __HAL_TIM_DISABLE(htim);
2047 /* Return function status */
2048 return HAL_OK;
2052 * @brief Starts the TIM Input Capture measurement in DMA mode.
2053 * @param htim TIM Input Capture handle
2054 * @param Channel TIM Channels to be enabled
2055 * This parameter can be one of the following values:
2056 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2057 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2058 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2059 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2060 * @param pData The destination Buffer address.
2061 * @param Length The length of data to be transferred from TIM peripheral to memory.
2062 * @retval HAL status
2064 HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
2066 uint32_t tmpsmcr;
2068 /* Check the parameters */
2069 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2070 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2072 if (htim->State == HAL_TIM_STATE_BUSY)
2074 return HAL_BUSY;
2076 else if (htim->State == HAL_TIM_STATE_READY)
2078 if ((pData == NULL) && (Length > 0U))
2080 return HAL_ERROR;
2082 else
2084 htim->State = HAL_TIM_STATE_BUSY;
2087 else
2089 /* nothing to do */
2092 switch (Channel)
2094 case TIM_CHANNEL_1:
2096 /* Set the DMA capture callbacks */
2097 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
2098 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2100 /* Set the DMA error callback */
2101 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
2103 /* Enable the DMA stream */
2104 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
2106 return HAL_ERROR;
2108 /* Enable the TIM Capture/Compare 1 DMA request */
2109 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
2110 break;
2113 case TIM_CHANNEL_2:
2115 /* Set the DMA capture callbacks */
2116 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
2117 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2119 /* Set the DMA error callback */
2120 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
2122 /* Enable the DMA stream */
2123 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData, Length) != HAL_OK)
2125 return HAL_ERROR;
2127 /* Enable the TIM Capture/Compare 2 DMA request */
2128 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
2129 break;
2132 case TIM_CHANNEL_3:
2134 /* Set the DMA capture callbacks */
2135 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
2136 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2138 /* Set the DMA error callback */
2139 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
2141 /* Enable the DMA stream */
2142 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData, Length) != HAL_OK)
2144 return HAL_ERROR;
2146 /* Enable the TIM Capture/Compare 3 DMA request */
2147 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
2148 break;
2151 case TIM_CHANNEL_4:
2153 /* Set the DMA capture callbacks */
2154 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
2155 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2157 /* Set the DMA error callback */
2158 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
2160 /* Enable the DMA stream */
2161 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData, Length) != HAL_OK)
2163 return HAL_ERROR;
2165 /* Enable the TIM Capture/Compare 4 DMA request */
2166 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
2167 break;
2170 default:
2171 break;
2174 /* Enable the Input Capture channel */
2175 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
2177 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
2178 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
2179 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
2181 __HAL_TIM_ENABLE(htim);
2184 /* Return function status */
2185 return HAL_OK;
2189 * @brief Stops the TIM Input Capture measurement in DMA mode.
2190 * @param htim TIM Input Capture handle
2191 * @param Channel TIM Channels to be disabled
2192 * This parameter can be one of the following values:
2193 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2194 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2195 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2196 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2197 * @retval HAL status
2199 HAL_StatusTypeDef HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
2201 /* Check the parameters */
2202 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2203 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2205 switch (Channel)
2207 case TIM_CHANNEL_1:
2209 /* Disable the TIM Capture/Compare 1 DMA request */
2210 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
2211 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
2212 break;
2215 case TIM_CHANNEL_2:
2217 /* Disable the TIM Capture/Compare 2 DMA request */
2218 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
2219 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
2220 break;
2223 case TIM_CHANNEL_3:
2225 /* Disable the TIM Capture/Compare 3 DMA request */
2226 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
2227 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
2228 break;
2231 case TIM_CHANNEL_4:
2233 /* Disable the TIM Capture/Compare 4 DMA request */
2234 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
2235 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
2236 break;
2239 default:
2240 break;
2243 /* Disable the Input Capture channel */
2244 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
2246 /* Disable the Peripheral */
2247 __HAL_TIM_DISABLE(htim);
2249 /* Change the htim state */
2250 htim->State = HAL_TIM_STATE_READY;
2252 /* Return function status */
2253 return HAL_OK;
2256 * @}
2259 /** @defgroup TIM_Exported_Functions_Group5 TIM One Pulse functions
2260 * @brief TIM One Pulse functions
2262 @verbatim
2263 ==============================================================================
2264 ##### TIM One Pulse functions #####
2265 ==============================================================================
2266 [..]
2267 This section provides functions allowing to:
2268 (+) Initialize and configure the TIM One Pulse.
2269 (+) De-initialize the TIM One Pulse.
2270 (+) Start the TIM One Pulse.
2271 (+) Stop the TIM One Pulse.
2272 (+) Start the TIM One Pulse and enable interrupt.
2273 (+) Stop the TIM One Pulse and disable interrupt.
2274 (+) Start the TIM One Pulse and enable DMA transfer.
2275 (+) Stop the TIM One Pulse and disable DMA transfer.
2277 @endverbatim
2278 * @{
2281 * @brief Initializes the TIM One Pulse Time Base according to the specified
2282 * parameters in the TIM_HandleTypeDef and initializes the associated handle.
2283 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
2284 * requires a timer reset to avoid unexpected direction
2285 * due to DIR bit readonly in center aligned mode.
2286 * Ex: call @ref HAL_TIM_OnePulse_DeInit() before HAL_TIM_OnePulse_Init()
2287 * @param htim TIM One Pulse handle
2288 * @param OnePulseMode Select the One pulse mode.
2289 * This parameter can be one of the following values:
2290 * @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
2291 * @arg TIM_OPMODE_REPETITIVE: Repetitive pulses will be generated.
2292 * @retval HAL status
2294 HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)
2296 /* Check the TIM handle allocation */
2297 if (htim == NULL)
2299 return HAL_ERROR;
2302 /* Check the parameters */
2303 assert_param(IS_TIM_INSTANCE(htim->Instance));
2304 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
2305 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
2306 assert_param(IS_TIM_OPM_MODE(OnePulseMode));
2307 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
2309 if (htim->State == HAL_TIM_STATE_RESET)
2311 /* Allocate lock resource and initialize it */
2312 htim->Lock = HAL_UNLOCKED;
2314 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2315 /* Reset interrupt callbacks to legacy weak callbacks */
2316 TIM_ResetCallback(htim);
2318 if (htim->OnePulse_MspInitCallback == NULL)
2320 htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
2322 /* Init the low level hardware : GPIO, CLOCK, NVIC */
2323 htim->OnePulse_MspInitCallback(htim);
2324 #else
2325 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
2326 HAL_TIM_OnePulse_MspInit(htim);
2327 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2330 /* Set the TIM state */
2331 htim->State = HAL_TIM_STATE_BUSY;
2333 /* Configure the Time base in the One Pulse Mode */
2334 TIM_Base_SetConfig(htim->Instance, &htim->Init);
2336 /* Reset the OPM Bit */
2337 htim->Instance->CR1 &= ~TIM_CR1_OPM;
2339 /* Configure the OPM Mode */
2340 htim->Instance->CR1 |= OnePulseMode;
2342 /* Initialize the TIM state*/
2343 htim->State = HAL_TIM_STATE_READY;
2345 return HAL_OK;
2349 * @brief DeInitializes the TIM One Pulse
2350 * @param htim TIM One Pulse handle
2351 * @retval HAL status
2353 HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)
2355 /* Check the parameters */
2356 assert_param(IS_TIM_INSTANCE(htim->Instance));
2358 htim->State = HAL_TIM_STATE_BUSY;
2360 /* Disable the TIM Peripheral Clock */
2361 __HAL_TIM_DISABLE(htim);
2363 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2364 if (htim->OnePulse_MspDeInitCallback == NULL)
2366 htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
2368 /* DeInit the low level hardware */
2369 htim->OnePulse_MspDeInitCallback(htim);
2370 #else
2371 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
2372 HAL_TIM_OnePulse_MspDeInit(htim);
2373 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2375 /* Change TIM state */
2376 htim->State = HAL_TIM_STATE_RESET;
2378 /* Release Lock */
2379 __HAL_UNLOCK(htim);
2381 return HAL_OK;
2385 * @brief Initializes the TIM One Pulse MSP.
2386 * @param htim TIM One Pulse handle
2387 * @retval None
2389 __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
2391 /* Prevent unused argument(s) compilation warning */
2392 UNUSED(htim);
2394 /* NOTE : This function should not be modified, when the callback is needed,
2395 the HAL_TIM_OnePulse_MspInit could be implemented in the user file
2400 * @brief DeInitializes TIM One Pulse MSP.
2401 * @param htim TIM One Pulse handle
2402 * @retval None
2404 __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)
2406 /* Prevent unused argument(s) compilation warning */
2407 UNUSED(htim);
2409 /* NOTE : This function should not be modified, when the callback is needed,
2410 the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
2415 * @brief Starts the TIM One Pulse signal generation.
2416 * @param htim TIM One Pulse handle
2417 * @param OutputChannel TIM Channels to be enabled
2418 * This parameter can be one of the following values:
2419 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2420 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2421 * @retval HAL status
2423 HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2425 /* Prevent unused argument(s) compilation warning */
2426 UNUSED(OutputChannel);
2428 /* Enable the Capture compare and the Input Capture channels
2429 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2430 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2431 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2432 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2434 No need to enable the counter, it's enabled automatically by hardware
2435 (the counter starts in response to a stimulus and generate a pulse */
2437 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2438 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2440 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2442 /* Enable the main output */
2443 __HAL_TIM_MOE_ENABLE(htim);
2446 /* Return function status */
2447 return HAL_OK;
2451 * @brief Stops the TIM One Pulse signal generation.
2452 * @param htim TIM One Pulse handle
2453 * @param OutputChannel TIM Channels to be disable
2454 * This parameter can be one of the following values:
2455 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2456 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2457 * @retval HAL status
2459 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2461 /* Prevent unused argument(s) compilation warning */
2462 UNUSED(OutputChannel);
2464 /* Disable the Capture compare and the Input Capture channels
2465 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2466 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2467 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2468 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2470 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2471 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2473 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2475 /* Disable the Main Output */
2476 __HAL_TIM_MOE_DISABLE(htim);
2479 /* Disable the Peripheral */
2480 __HAL_TIM_DISABLE(htim);
2482 /* Return function status */
2483 return HAL_OK;
2487 * @brief Starts the TIM One Pulse signal generation in interrupt mode.
2488 * @param htim TIM One Pulse handle
2489 * @param OutputChannel TIM Channels to be enabled
2490 * This parameter can be one of the following values:
2491 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2492 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2493 * @retval HAL status
2495 HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2497 /* Prevent unused argument(s) compilation warning */
2498 UNUSED(OutputChannel);
2500 /* Enable the Capture compare and the Input Capture channels
2501 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2502 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2503 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2504 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2506 No need to enable the counter, it's enabled automatically by hardware
2507 (the counter starts in response to a stimulus and generate a pulse */
2509 /* Enable the TIM Capture/Compare 1 interrupt */
2510 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2512 /* Enable the TIM Capture/Compare 2 interrupt */
2513 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2515 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2516 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2518 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2520 /* Enable the main output */
2521 __HAL_TIM_MOE_ENABLE(htim);
2524 /* Return function status */
2525 return HAL_OK;
2529 * @brief Stops the TIM One Pulse signal generation in interrupt mode.
2530 * @param htim TIM One Pulse handle
2531 * @param OutputChannel TIM Channels to be enabled
2532 * This parameter can be one of the following values:
2533 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2534 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2535 * @retval HAL status
2537 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2539 /* Prevent unused argument(s) compilation warning */
2540 UNUSED(OutputChannel);
2542 /* Disable the TIM Capture/Compare 1 interrupt */
2543 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2545 /* Disable the TIM Capture/Compare 2 interrupt */
2546 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2548 /* Disable the Capture compare and the Input Capture channels
2549 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2550 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2551 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2552 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2553 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2554 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2556 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2558 /* Disable the Main Output */
2559 __HAL_TIM_MOE_DISABLE(htim);
2562 /* Disable the Peripheral */
2563 __HAL_TIM_DISABLE(htim);
2565 /* Return function status */
2566 return HAL_OK;
2570 * @}
2573 /** @defgroup TIM_Exported_Functions_Group6 TIM Encoder functions
2574 * @brief TIM Encoder functions
2576 @verbatim
2577 ==============================================================================
2578 ##### TIM Encoder functions #####
2579 ==============================================================================
2580 [..]
2581 This section provides functions allowing to:
2582 (+) Initialize and configure the TIM Encoder.
2583 (+) De-initialize the TIM Encoder.
2584 (+) Start the TIM Encoder.
2585 (+) Stop the TIM Encoder.
2586 (+) Start the TIM Encoder and enable interrupt.
2587 (+) Stop the TIM Encoder and disable interrupt.
2588 (+) Start the TIM Encoder and enable DMA transfer.
2589 (+) Stop the TIM Encoder and disable DMA transfer.
2591 @endverbatim
2592 * @{
2595 * @brief Initializes the TIM Encoder Interface and initialize the associated handle.
2596 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
2597 * requires a timer reset to avoid unexpected direction
2598 * due to DIR bit readonly in center aligned mode.
2599 * Ex: call @ref HAL_TIM_Encoder_DeInit() before HAL_TIM_Encoder_Init()
2600 * @note Encoder mode and External clock mode 2 are not compatible and must not be selected together
2601 * Ex: A call for @ref HAL_TIM_Encoder_Init will erase the settings of @ref HAL_TIM_ConfigClockSource
2602 * using TIM_CLOCKSOURCE_ETRMODE2 and vice versa
2603 * @param htim TIM Encoder Interface handle
2604 * @param sConfig TIM Encoder Interface configuration structure
2605 * @retval HAL status
2607 HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim, TIM_Encoder_InitTypeDef *sConfig)
2609 uint32_t tmpsmcr;
2610 uint32_t tmpccmr1;
2611 uint32_t tmpccer;
2613 /* Check the TIM handle allocation */
2614 if (htim == NULL)
2616 return HAL_ERROR;
2619 /* Check the parameters */
2620 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
2621 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
2622 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
2623 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2624 assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));
2625 assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));
2626 assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));
2627 assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC1Polarity));
2628 assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC2Polarity));
2629 assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
2630 assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));
2631 assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
2632 assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));
2634 if (htim->State == HAL_TIM_STATE_RESET)
2636 /* Allocate lock resource and initialize it */
2637 htim->Lock = HAL_UNLOCKED;
2639 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2640 /* Reset interrupt callbacks to legacy weak callbacks */
2641 TIM_ResetCallback(htim);
2643 if (htim->Encoder_MspInitCallback == NULL)
2645 htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
2647 /* Init the low level hardware : GPIO, CLOCK, NVIC */
2648 htim->Encoder_MspInitCallback(htim);
2649 #else
2650 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
2651 HAL_TIM_Encoder_MspInit(htim);
2652 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2655 /* Set the TIM state */
2656 htim->State = HAL_TIM_STATE_BUSY;
2658 /* Reset the SMS and ECE bits */
2659 htim->Instance->SMCR &= ~(TIM_SMCR_SMS | TIM_SMCR_ECE);
2661 /* Configure the Time base in the Encoder Mode */
2662 TIM_Base_SetConfig(htim->Instance, &htim->Init);
2664 /* Get the TIMx SMCR register value */
2665 tmpsmcr = htim->Instance->SMCR;
2667 /* Get the TIMx CCMR1 register value */
2668 tmpccmr1 = htim->Instance->CCMR1;
2670 /* Get the TIMx CCER register value */
2671 tmpccer = htim->Instance->CCER;
2673 /* Set the encoder Mode */
2674 tmpsmcr |= sConfig->EncoderMode;
2676 /* Select the Capture Compare 1 and the Capture Compare 2 as input */
2677 tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);
2678 tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8U));
2680 /* Set the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
2681 tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);
2682 tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);
2683 tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8U);
2684 tmpccmr1 |= (sConfig->IC1Filter << 4U) | (sConfig->IC2Filter << 12U);
2686 /* Set the TI1 and the TI2 Polarities */
2687 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
2688 tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);
2689 tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4U);
2691 /* Write to TIMx SMCR */
2692 htim->Instance->SMCR = tmpsmcr;
2694 /* Write to TIMx CCMR1 */
2695 htim->Instance->CCMR1 = tmpccmr1;
2697 /* Write to TIMx CCER */
2698 htim->Instance->CCER = tmpccer;
2700 /* Initialize the TIM state*/
2701 htim->State = HAL_TIM_STATE_READY;
2703 return HAL_OK;
2708 * @brief DeInitializes the TIM Encoder interface
2709 * @param htim TIM Encoder Interface handle
2710 * @retval HAL status
2712 HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)
2714 /* Check the parameters */
2715 assert_param(IS_TIM_INSTANCE(htim->Instance));
2717 htim->State = HAL_TIM_STATE_BUSY;
2719 /* Disable the TIM Peripheral Clock */
2720 __HAL_TIM_DISABLE(htim);
2722 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2723 if (htim->Encoder_MspDeInitCallback == NULL)
2725 htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
2727 /* DeInit the low level hardware */
2728 htim->Encoder_MspDeInitCallback(htim);
2729 #else
2730 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
2731 HAL_TIM_Encoder_MspDeInit(htim);
2732 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2734 /* Change TIM state */
2735 htim->State = HAL_TIM_STATE_RESET;
2737 /* Release Lock */
2738 __HAL_UNLOCK(htim);
2740 return HAL_OK;
2744 * @brief Initializes the TIM Encoder Interface MSP.
2745 * @param htim TIM Encoder Interface handle
2746 * @retval None
2748 __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
2750 /* Prevent unused argument(s) compilation warning */
2751 UNUSED(htim);
2753 /* NOTE : This function should not be modified, when the callback is needed,
2754 the HAL_TIM_Encoder_MspInit could be implemented in the user file
2759 * @brief DeInitializes TIM Encoder Interface MSP.
2760 * @param htim TIM Encoder Interface handle
2761 * @retval None
2763 __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)
2765 /* Prevent unused argument(s) compilation warning */
2766 UNUSED(htim);
2768 /* NOTE : This function should not be modified, when the callback is needed,
2769 the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
2774 * @brief Starts the TIM Encoder Interface.
2775 * @param htim TIM Encoder Interface handle
2776 * @param Channel TIM Channels to be enabled
2777 * This parameter can be one of the following values:
2778 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2779 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2780 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2781 * @retval HAL status
2783 HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
2785 /* Check the parameters */
2786 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2788 /* Enable the encoder interface channels */
2789 switch (Channel)
2791 case TIM_CHANNEL_1:
2793 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2794 break;
2797 case TIM_CHANNEL_2:
2799 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2800 break;
2803 default :
2805 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2806 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2807 break;
2810 /* Enable the Peripheral */
2811 __HAL_TIM_ENABLE(htim);
2813 /* Return function status */
2814 return HAL_OK;
2818 * @brief Stops the TIM Encoder Interface.
2819 * @param htim TIM Encoder Interface handle
2820 * @param Channel TIM Channels to be disabled
2821 * This parameter can be one of the following values:
2822 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2823 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2824 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2825 * @retval HAL status
2827 HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
2829 /* Check the parameters */
2830 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2832 /* Disable the Input Capture channels 1 and 2
2833 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
2834 switch (Channel)
2836 case TIM_CHANNEL_1:
2838 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2839 break;
2842 case TIM_CHANNEL_2:
2844 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2845 break;
2848 default :
2850 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2851 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2852 break;
2856 /* Disable the Peripheral */
2857 __HAL_TIM_DISABLE(htim);
2859 /* Return function status */
2860 return HAL_OK;
2864 * @brief Starts the TIM Encoder Interface in interrupt mode.
2865 * @param htim TIM Encoder Interface handle
2866 * @param Channel TIM Channels to be enabled
2867 * This parameter can be one of the following values:
2868 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2869 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2870 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2871 * @retval HAL status
2873 HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2875 /* Check the parameters */
2876 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2878 /* Enable the encoder interface channels */
2879 /* Enable the capture compare Interrupts 1 and/or 2 */
2880 switch (Channel)
2882 case TIM_CHANNEL_1:
2884 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2885 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2886 break;
2889 case TIM_CHANNEL_2:
2891 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2892 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2893 break;
2896 default :
2898 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2899 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2900 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2901 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2902 break;
2906 /* Enable the Peripheral */
2907 __HAL_TIM_ENABLE(htim);
2909 /* Return function status */
2910 return HAL_OK;
2914 * @brief Stops the TIM Encoder Interface in interrupt mode.
2915 * @param htim TIM Encoder Interface handle
2916 * @param Channel TIM Channels to be disabled
2917 * This parameter can be one of the following values:
2918 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2919 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2920 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2921 * @retval HAL status
2923 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2925 /* Check the parameters */
2926 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2928 /* Disable the Input Capture channels 1 and 2
2929 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
2930 if (Channel == TIM_CHANNEL_1)
2932 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2934 /* Disable the capture compare Interrupts 1 */
2935 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2937 else if (Channel == TIM_CHANNEL_2)
2939 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2941 /* Disable the capture compare Interrupts 2 */
2942 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2944 else
2946 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2947 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2949 /* Disable the capture compare Interrupts 1 and 2 */
2950 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2951 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2954 /* Disable the Peripheral */
2955 __HAL_TIM_DISABLE(htim);
2957 /* Change the htim state */
2958 htim->State = HAL_TIM_STATE_READY;
2960 /* Return function status */
2961 return HAL_OK;
2965 * @brief Starts the TIM Encoder Interface in DMA mode.
2966 * @param htim TIM Encoder Interface handle
2967 * @param Channel TIM Channels to be enabled
2968 * This parameter can be one of the following values:
2969 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2970 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2971 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2972 * @param pData1 The destination Buffer address for IC1.
2973 * @param pData2 The destination Buffer address for IC2.
2974 * @param Length The length of data to be transferred from TIM peripheral to memory.
2975 * @retval HAL status
2977 HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1,
2978 uint32_t *pData2, uint16_t Length)
2980 /* Check the parameters */
2981 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2983 if (htim->State == HAL_TIM_STATE_BUSY)
2985 return HAL_BUSY;
2987 else if (htim->State == HAL_TIM_STATE_READY)
2989 if ((((pData1 == NULL) || (pData2 == NULL))) && (Length > 0U))
2991 return HAL_ERROR;
2993 else
2995 htim->State = HAL_TIM_STATE_BUSY;
2998 else
3000 /* nothing to do */
3003 switch (Channel)
3005 case TIM_CHANNEL_1:
3007 /* Set the DMA capture callbacks */
3008 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
3009 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3011 /* Set the DMA error callback */
3012 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
3014 /* Enable the DMA stream */
3015 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length) != HAL_OK)
3017 return HAL_ERROR;
3019 /* Enable the TIM Input Capture DMA request */
3020 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
3022 /* Enable the Peripheral */
3023 __HAL_TIM_ENABLE(htim);
3025 /* Enable the Capture compare channel */
3026 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3027 break;
3030 case TIM_CHANNEL_2:
3032 /* Set the DMA capture callbacks */
3033 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
3034 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3036 /* Set the DMA error callback */
3037 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError;
3038 /* Enable the DMA stream */
3039 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length) != HAL_OK)
3041 return HAL_ERROR;
3043 /* Enable the TIM Input Capture DMA request */
3044 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
3046 /* Enable the Peripheral */
3047 __HAL_TIM_ENABLE(htim);
3049 /* Enable the Capture compare channel */
3050 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3051 break;
3054 case TIM_CHANNEL_ALL:
3056 /* Set the DMA capture callbacks */
3057 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
3058 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3060 /* Set the DMA error callback */
3061 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
3063 /* Enable the DMA stream */
3064 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length) != HAL_OK)
3066 return HAL_ERROR;
3069 /* Set the DMA capture callbacks */
3070 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
3071 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3073 /* Set the DMA error callback */
3074 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
3076 /* Enable the DMA stream */
3077 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length) != HAL_OK)
3079 return HAL_ERROR;
3081 /* Enable the Peripheral */
3082 __HAL_TIM_ENABLE(htim);
3084 /* Enable the Capture compare channel */
3085 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3086 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3088 /* Enable the TIM Input Capture DMA request */
3089 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
3090 /* Enable the TIM Input Capture DMA request */
3091 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
3092 break;
3095 default:
3096 break;
3098 /* Return function status */
3099 return HAL_OK;
3103 * @brief Stops the TIM Encoder Interface in DMA mode.
3104 * @param htim TIM Encoder Interface handle
3105 * @param Channel TIM Channels to be enabled
3106 * This parameter can be one of the following values:
3107 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3108 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3109 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3110 * @retval HAL status
3112 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
3114 /* Check the parameters */
3115 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
3117 /* Disable the Input Capture channels 1 and 2
3118 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
3119 if (Channel == TIM_CHANNEL_1)
3121 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3123 /* Disable the capture compare DMA Request 1 */
3124 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
3125 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
3127 else if (Channel == TIM_CHANNEL_2)
3129 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3131 /* Disable the capture compare DMA Request 2 */
3132 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
3133 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
3135 else
3137 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3138 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3140 /* Disable the capture compare DMA Request 1 and 2 */
3141 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
3142 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
3143 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
3144 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
3147 /* Disable the Peripheral */
3148 __HAL_TIM_DISABLE(htim);
3150 /* Change the htim state */
3151 htim->State = HAL_TIM_STATE_READY;
3153 /* Return function status */
3154 return HAL_OK;
3158 * @}
3160 /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
3161 * @brief TIM IRQ handler management
3163 @verbatim
3164 ==============================================================================
3165 ##### IRQ handler management #####
3166 ==============================================================================
3167 [..]
3168 This section provides Timer IRQ handler function.
3170 @endverbatim
3171 * @{
3174 * @brief This function handles TIM interrupts requests.
3175 * @param htim TIM handle
3176 * @retval None
3178 void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
3180 /* Capture compare 1 event */
3181 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)
3183 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) != RESET)
3186 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
3187 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
3189 /* Input capture event */
3190 if ((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U)
3192 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3193 htim->IC_CaptureCallback(htim);
3194 #else
3195 HAL_TIM_IC_CaptureCallback(htim);
3196 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3198 /* Output compare event */
3199 else
3201 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3202 htim->OC_DelayElapsedCallback(htim);
3203 htim->PWM_PulseFinishedCallback(htim);
3204 #else
3205 HAL_TIM_OC_DelayElapsedCallback(htim);
3206 HAL_TIM_PWM_PulseFinishedCallback(htim);
3207 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3209 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3213 /* Capture compare 2 event */
3214 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET)
3216 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) != RESET)
3218 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);
3219 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
3220 /* Input capture event */
3221 if ((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U)
3223 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3224 htim->IC_CaptureCallback(htim);
3225 #else
3226 HAL_TIM_IC_CaptureCallback(htim);
3227 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3229 /* Output compare event */
3230 else
3232 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3233 htim->OC_DelayElapsedCallback(htim);
3234 htim->PWM_PulseFinishedCallback(htim);
3235 #else
3236 HAL_TIM_OC_DelayElapsedCallback(htim);
3237 HAL_TIM_PWM_PulseFinishedCallback(htim);
3238 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3240 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3243 /* Capture compare 3 event */
3244 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET)
3246 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) != RESET)
3248 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);
3249 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
3250 /* Input capture event */
3251 if ((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U)
3253 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3254 htim->IC_CaptureCallback(htim);
3255 #else
3256 HAL_TIM_IC_CaptureCallback(htim);
3257 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3259 /* Output compare event */
3260 else
3262 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3263 htim->OC_DelayElapsedCallback(htim);
3264 htim->PWM_PulseFinishedCallback(htim);
3265 #else
3266 HAL_TIM_OC_DelayElapsedCallback(htim);
3267 HAL_TIM_PWM_PulseFinishedCallback(htim);
3268 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3270 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3273 /* Capture compare 4 event */
3274 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET)
3276 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) != RESET)
3278 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
3279 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
3280 /* Input capture event */
3281 if ((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U)
3283 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3284 htim->IC_CaptureCallback(htim);
3285 #else
3286 HAL_TIM_IC_CaptureCallback(htim);
3287 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3289 /* Output compare event */
3290 else
3292 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3293 htim->OC_DelayElapsedCallback(htim);
3294 htim->PWM_PulseFinishedCallback(htim);
3295 #else
3296 HAL_TIM_OC_DelayElapsedCallback(htim);
3297 HAL_TIM_PWM_PulseFinishedCallback(htim);
3298 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3300 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3303 /* TIM Update event */
3304 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)
3306 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_UPDATE) != RESET)
3308 __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
3309 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3310 htim->PeriodElapsedCallback(htim);
3311 #else
3312 HAL_TIM_PeriodElapsedCallback(htim);
3313 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3316 /* TIM Break input event */
3317 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK) != RESET)
3319 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) != RESET)
3321 __HAL_TIM_CLEAR_IT(htim, TIM_IT_BREAK);
3322 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3323 htim->BreakCallback(htim);
3324 #else
3325 HAL_TIMEx_BreakCallback(htim);
3326 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3329 /* TIM Break2 input event */
3330 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK2) != RESET)
3332 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) != RESET)
3334 __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_BREAK2);
3335 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3336 htim->Break2Callback(htim);
3337 #else
3338 HAL_TIMEx_Break2Callback(htim);
3339 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3342 /* TIM Trigger detection event */
3343 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_TRIGGER) != RESET)
3345 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_TRIGGER) != RESET)
3347 __HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);
3348 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3349 htim->TriggerCallback(htim);
3350 #else
3351 HAL_TIM_TriggerCallback(htim);
3352 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3355 /* TIM commutation event */
3356 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_COM) != RESET)
3358 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_COM) != RESET)
3360 __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_COM);
3361 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3362 htim->CommutationCallback(htim);
3363 #else
3364 HAL_TIMEx_CommutCallback(htim);
3365 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3371 * @}
3374 /** @defgroup TIM_Exported_Functions_Group8 TIM Peripheral Control functions
3375 * @brief TIM Peripheral Control functions
3377 @verbatim
3378 ==============================================================================
3379 ##### Peripheral Control functions #####
3380 ==============================================================================
3381 [..]
3382 This section provides functions allowing to:
3383 (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
3384 (+) Configure External Clock source.
3385 (+) Configure Complementary channels, break features and dead time.
3386 (+) Configure Master and the Slave synchronization.
3387 (+) Configure the DMA Burst Mode.
3389 @endverbatim
3390 * @{
3394 * @brief Initializes the TIM Output Compare Channels according to the specified
3395 * parameters in the TIM_OC_InitTypeDef.
3396 * @param htim TIM Output Compare handle
3397 * @param sConfig TIM Output Compare configuration structure
3398 * @param Channel TIM Channels to configure
3399 * This parameter can be one of the following values:
3400 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3401 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3402 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
3403 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
3404 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
3405 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
3406 * @retval HAL status
3408 HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim,
3409 TIM_OC_InitTypeDef *sConfig,
3410 uint32_t Channel)
3412 /* Check the parameters */
3413 assert_param(IS_TIM_CHANNELS(Channel));
3414 assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
3415 assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
3417 /* Process Locked */
3418 __HAL_LOCK(htim);
3420 htim->State = HAL_TIM_STATE_BUSY;
3422 switch (Channel)
3424 case TIM_CHANNEL_1:
3426 /* Check the parameters */
3427 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3429 /* Configure the TIM Channel 1 in Output Compare */
3430 TIM_OC1_SetConfig(htim->Instance, sConfig);
3431 break;
3434 case TIM_CHANNEL_2:
3436 /* Check the parameters */
3437 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3439 /* Configure the TIM Channel 2 in Output Compare */
3440 TIM_OC2_SetConfig(htim->Instance, sConfig);
3441 break;
3444 case TIM_CHANNEL_3:
3446 /* Check the parameters */
3447 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
3449 /* Configure the TIM Channel 3 in Output Compare */
3450 TIM_OC3_SetConfig(htim->Instance, sConfig);
3451 break;
3454 case TIM_CHANNEL_4:
3456 /* Check the parameters */
3457 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
3459 /* Configure the TIM Channel 4 in Output Compare */
3460 TIM_OC4_SetConfig(htim->Instance, sConfig);
3461 break;
3464 case TIM_CHANNEL_5:
3466 /* Check the parameters */
3467 assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
3469 /* Configure the TIM Channel 5 in Output Compare */
3470 TIM_OC5_SetConfig(htim->Instance, sConfig);
3471 break;
3474 case TIM_CHANNEL_6:
3476 /* Check the parameters */
3477 assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
3479 /* Configure the TIM Channel 6 in Output Compare */
3480 TIM_OC6_SetConfig(htim->Instance, sConfig);
3481 break;
3484 default:
3485 break;
3488 htim->State = HAL_TIM_STATE_READY;
3490 __HAL_UNLOCK(htim);
3492 return HAL_OK;
3496 * @brief Initializes the TIM Input Capture Channels according to the specified
3497 * parameters in the TIM_IC_InitTypeDef.
3498 * @param htim TIM IC handle
3499 * @param sConfig TIM Input Capture configuration structure
3500 * @param Channel TIM Channel to configure
3501 * This parameter can be one of the following values:
3502 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3503 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3504 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
3505 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
3506 * @retval HAL status
3508 HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_IC_InitTypeDef *sConfig, uint32_t Channel)
3510 /* Check the parameters */
3511 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3512 assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
3513 assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
3514 assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
3515 assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
3517 /* Process Locked */
3518 __HAL_LOCK(htim);
3520 htim->State = HAL_TIM_STATE_BUSY;
3522 if (Channel == TIM_CHANNEL_1)
3524 /* TI1 Configuration */
3525 TIM_TI1_SetConfig(htim->Instance,
3526 sConfig->ICPolarity,
3527 sConfig->ICSelection,
3528 sConfig->ICFilter);
3530 /* Reset the IC1PSC Bits */
3531 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
3533 /* Set the IC1PSC value */
3534 htim->Instance->CCMR1 |= sConfig->ICPrescaler;
3536 else if (Channel == TIM_CHANNEL_2)
3538 /* TI2 Configuration */
3539 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3541 TIM_TI2_SetConfig(htim->Instance,
3542 sConfig->ICPolarity,
3543 sConfig->ICSelection,
3544 sConfig->ICFilter);
3546 /* Reset the IC2PSC Bits */
3547 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
3549 /* Set the IC2PSC value */
3550 htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8U);
3552 else if (Channel == TIM_CHANNEL_3)
3554 /* TI3 Configuration */
3555 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
3557 TIM_TI3_SetConfig(htim->Instance,
3558 sConfig->ICPolarity,
3559 sConfig->ICSelection,
3560 sConfig->ICFilter);
3562 /* Reset the IC3PSC Bits */
3563 htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
3565 /* Set the IC3PSC value */
3566 htim->Instance->CCMR2 |= sConfig->ICPrescaler;
3568 else
3570 /* TI4 Configuration */
3571 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
3573 TIM_TI4_SetConfig(htim->Instance,
3574 sConfig->ICPolarity,
3575 sConfig->ICSelection,
3576 sConfig->ICFilter);
3578 /* Reset the IC4PSC Bits */
3579 htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
3581 /* Set the IC4PSC value */
3582 htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8U);
3585 htim->State = HAL_TIM_STATE_READY;
3587 __HAL_UNLOCK(htim);
3589 return HAL_OK;
3593 * @brief Initializes the TIM PWM channels according to the specified
3594 * parameters in the TIM_OC_InitTypeDef.
3595 * @param htim TIM PWM handle
3596 * @param sConfig TIM PWM configuration structure
3597 * @param Channel TIM Channels to be configured
3598 * This parameter can be one of the following values:
3599 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3600 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3601 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
3602 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
3603 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
3604 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
3605 * @retval HAL status
3607 HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
3608 TIM_OC_InitTypeDef *sConfig,
3609 uint32_t Channel)
3611 /* Check the parameters */
3612 assert_param(IS_TIM_CHANNELS(Channel));
3613 assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
3614 assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
3615 assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
3617 /* Process Locked */
3618 __HAL_LOCK(htim);
3620 htim->State = HAL_TIM_STATE_BUSY;
3622 switch (Channel)
3624 case TIM_CHANNEL_1:
3626 /* Check the parameters */
3627 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3629 /* Configure the Channel 1 in PWM mode */
3630 TIM_OC1_SetConfig(htim->Instance, sConfig);
3632 /* Set the Preload enable bit for channel1 */
3633 htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
3635 /* Configure the Output Fast mode */
3636 htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
3637 htim->Instance->CCMR1 |= sConfig->OCFastMode;
3638 break;
3641 case TIM_CHANNEL_2:
3643 /* Check the parameters */
3644 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3646 /* Configure the Channel 2 in PWM mode */
3647 TIM_OC2_SetConfig(htim->Instance, sConfig);
3649 /* Set the Preload enable bit for channel2 */
3650 htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
3652 /* Configure the Output Fast mode */
3653 htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
3654 htim->Instance->CCMR1 |= sConfig->OCFastMode << 8U;
3655 break;
3658 case TIM_CHANNEL_3:
3660 /* Check the parameters */
3661 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
3663 /* Configure the Channel 3 in PWM mode */
3664 TIM_OC3_SetConfig(htim->Instance, sConfig);
3666 /* Set the Preload enable bit for channel3 */
3667 htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
3669 /* Configure the Output Fast mode */
3670 htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
3671 htim->Instance->CCMR2 |= sConfig->OCFastMode;
3672 break;
3675 case TIM_CHANNEL_4:
3677 /* Check the parameters */
3678 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
3680 /* Configure the Channel 4 in PWM mode */
3681 TIM_OC4_SetConfig(htim->Instance, sConfig);
3683 /* Set the Preload enable bit for channel4 */
3684 htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
3686 /* Configure the Output Fast mode */
3687 htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
3688 htim->Instance->CCMR2 |= sConfig->OCFastMode << 8U;
3689 break;
3692 case TIM_CHANNEL_5:
3694 /* Check the parameters */
3695 assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
3697 /* Configure the Channel 5 in PWM mode */
3698 TIM_OC5_SetConfig(htim->Instance, sConfig);
3700 /* Set the Preload enable bit for channel5*/
3701 htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
3703 /* Configure the Output Fast mode */
3704 htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;
3705 htim->Instance->CCMR3 |= sConfig->OCFastMode;
3706 break;
3709 case TIM_CHANNEL_6:
3711 /* Check the parameters */
3712 assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
3714 /* Configure the Channel 6 in PWM mode */
3715 TIM_OC6_SetConfig(htim->Instance, sConfig);
3717 /* Set the Preload enable bit for channel6 */
3718 htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
3720 /* Configure the Output Fast mode */
3721 htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;
3722 htim->Instance->CCMR3 |= sConfig->OCFastMode << 8U;
3723 break;
3726 default:
3727 break;
3730 htim->State = HAL_TIM_STATE_READY;
3732 __HAL_UNLOCK(htim);
3734 return HAL_OK;
3738 * @brief Initializes the TIM One Pulse Channels according to the specified
3739 * parameters in the TIM_OnePulse_InitTypeDef.
3740 * @param htim TIM One Pulse handle
3741 * @param sConfig TIM One Pulse configuration structure
3742 * @param OutputChannel TIM output channel to configure
3743 * This parameter can be one of the following values:
3744 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3745 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3746 * @param InputChannel TIM input Channel to configure
3747 * This parameter can be one of the following values:
3748 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3749 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3750 * @note To output a waveform with a minimum delay user can enable the fast
3751 * mode by calling the @ref __HAL_TIM_ENABLE_OCxFAST macro. Then CCx
3752 * output is forced in response to the edge detection on TIx input,
3753 * without taking in account the comparison.
3754 * @retval HAL status
3756 HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef *sConfig,
3757 uint32_t OutputChannel, uint32_t InputChannel)
3759 TIM_OC_InitTypeDef temp1;
3761 /* Check the parameters */
3762 assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
3763 assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
3765 if (OutputChannel != InputChannel)
3767 /* Process Locked */
3768 __HAL_LOCK(htim);
3770 htim->State = HAL_TIM_STATE_BUSY;
3772 /* Extract the Output compare configuration from sConfig structure */
3773 temp1.OCMode = sConfig->OCMode;
3774 temp1.Pulse = sConfig->Pulse;
3775 temp1.OCPolarity = sConfig->OCPolarity;
3776 temp1.OCNPolarity = sConfig->OCNPolarity;
3777 temp1.OCIdleState = sConfig->OCIdleState;
3778 temp1.OCNIdleState = sConfig->OCNIdleState;
3780 switch (OutputChannel)
3782 case TIM_CHANNEL_1:
3784 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3786 TIM_OC1_SetConfig(htim->Instance, &temp1);
3787 break;
3789 case TIM_CHANNEL_2:
3791 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3793 TIM_OC2_SetConfig(htim->Instance, &temp1);
3794 break;
3796 default:
3797 break;
3800 switch (InputChannel)
3802 case TIM_CHANNEL_1:
3804 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3806 TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
3807 sConfig->ICSelection, sConfig->ICFilter);
3809 /* Reset the IC1PSC Bits */
3810 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
3812 /* Select the Trigger source */
3813 htim->Instance->SMCR &= ~TIM_SMCR_TS;
3814 htim->Instance->SMCR |= TIM_TS_TI1FP1;
3816 /* Select the Slave Mode */
3817 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
3818 htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
3819 break;
3821 case TIM_CHANNEL_2:
3823 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3825 TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
3826 sConfig->ICSelection, sConfig->ICFilter);
3828 /* Reset the IC2PSC Bits */
3829 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
3831 /* Select the Trigger source */
3832 htim->Instance->SMCR &= ~TIM_SMCR_TS;
3833 htim->Instance->SMCR |= TIM_TS_TI2FP2;
3835 /* Select the Slave Mode */
3836 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
3837 htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
3838 break;
3841 default:
3842 break;
3845 htim->State = HAL_TIM_STATE_READY;
3847 __HAL_UNLOCK(htim);
3849 return HAL_OK;
3851 else
3853 return HAL_ERROR;
3858 * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
3859 * @param htim TIM handle
3860 * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
3861 * This parameter can be one of the following values:
3862 * @arg TIM_DMABASE_CR1
3863 * @arg TIM_DMABASE_CR2
3864 * @arg TIM_DMABASE_SMCR
3865 * @arg TIM_DMABASE_DIER
3866 * @arg TIM_DMABASE_SR
3867 * @arg TIM_DMABASE_EGR
3868 * @arg TIM_DMABASE_CCMR1
3869 * @arg TIM_DMABASE_CCMR2
3870 * @arg TIM_DMABASE_CCER
3871 * @arg TIM_DMABASE_CNT
3872 * @arg TIM_DMABASE_PSC
3873 * @arg TIM_DMABASE_ARR
3874 * @arg TIM_DMABASE_RCR
3875 * @arg TIM_DMABASE_CCR1
3876 * @arg TIM_DMABASE_CCR2
3877 * @arg TIM_DMABASE_CCR3
3878 * @arg TIM_DMABASE_CCR4
3879 * @arg TIM_DMABASE_BDTR
3880 * @arg TIM_DMABASE_CCMR3
3881 * @arg TIM_DMABASE_CCR5
3882 * @arg TIM_DMABASE_CCR6
3883 * @arg TIM_DMABASE_AF1
3884 * @arg TIM_DMABASE_AF2
3885 * @arg TIM_DMABASE_TISEL
3887 * @param BurstRequestSrc TIM DMA Request sources
3888 * This parameter can be one of the following values:
3889 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
3890 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
3891 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
3892 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
3893 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
3894 * @arg TIM_DMA_COM: TIM Commutation DMA source
3895 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
3896 * @param BurstBuffer The Buffer address.
3897 * @param BurstLength DMA Burst length. This parameter can be one value
3898 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
3899 * @note This function should be used only when BurstLength is equal to DMA data transfer length.
3900 * @retval HAL status
3902 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
3903 uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
3905 return HAL_TIM_DMABurst_MultiWriteStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
3906 ((BurstLength) >> 8U) + 1U);
3910 * @brief Configure the DMA Burst to transfer multiple Data from the memory to the TIM peripheral
3911 * @param htim TIM handle
3912 * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
3913 * This parameter can be one of the following values:
3914 * @arg TIM_DMABASE_CR1
3915 * @arg TIM_DMABASE_CR2
3916 * @arg TIM_DMABASE_SMCR
3917 * @arg TIM_DMABASE_DIER
3918 * @arg TIM_DMABASE_SR
3919 * @arg TIM_DMABASE_EGR
3920 * @arg TIM_DMABASE_CCMR1
3921 * @arg TIM_DMABASE_CCMR2
3922 * @arg TIM_DMABASE_CCER
3923 * @arg TIM_DMABASE_CNT
3924 * @arg TIM_DMABASE_PSC
3925 * @arg TIM_DMABASE_ARR
3926 * @arg TIM_DMABASE_RCR
3927 * @arg TIM_DMABASE_CCR1
3928 * @arg TIM_DMABASE_CCR2
3929 * @arg TIM_DMABASE_CCR3
3930 * @arg TIM_DMABASE_CCR4
3931 * @arg TIM_DMABASE_BDTR
3932 * @arg TIM_DMABASE_CCMR3
3933 * @arg TIM_DMABASE_CCR5
3934 * @arg TIM_DMABASE_CCR6
3935 * @arg TIM_DMABASE_AF1
3936 * @arg TIM_DMABASE_AF2
3937 * @arg TIM_DMABASE_TISEL
3939 * @param BurstRequestSrc TIM DMA Request sources
3940 * This parameter can be one of the following values:
3941 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
3942 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
3943 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
3944 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
3945 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
3946 * @arg TIM_DMA_COM: TIM Commutation DMA source
3947 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
3948 * @param BurstBuffer The Buffer address.
3949 * @param BurstLength DMA Burst length. This parameter can be one value
3950 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
3951 * @param DataLength Data length. This parameter can be one value
3952 * between 1 and 0xFFFF.
3953 * @retval HAL status
3955 HAL_StatusTypeDef HAL_TIM_DMABurst_MultiWriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
3956 uint32_t BurstRequestSrc, uint32_t *BurstBuffer,
3957 uint32_t BurstLength, uint32_t DataLength)
3959 /* Check the parameters */
3960 assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
3961 assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
3962 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
3963 assert_param(IS_TIM_DMA_LENGTH(BurstLength));
3964 assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
3966 if (htim->State == HAL_TIM_STATE_BUSY)
3968 return HAL_BUSY;
3970 else if (htim->State == HAL_TIM_STATE_READY)
3972 if ((BurstBuffer == NULL) && (BurstLength > 0U))
3974 return HAL_ERROR;
3976 else
3978 htim->State = HAL_TIM_STATE_BUSY;
3981 else
3983 /* nothing to do */
3985 switch (BurstRequestSrc)
3987 case TIM_DMA_UPDATE:
3989 /* Set the DMA Period elapsed callbacks */
3990 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
3991 htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
3993 /* Set the DMA error callback */
3994 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
3996 /* Enable the DMA stream */
3997 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer,
3998 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4000 return HAL_ERROR;
4002 break;
4004 case TIM_DMA_CC1:
4006 /* Set the DMA compare callbacks */
4007 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
4008 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4010 /* Set the DMA error callback */
4011 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
4013 /* Enable the DMA stream */
4014 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer,
4015 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4017 return HAL_ERROR;
4019 break;
4021 case TIM_DMA_CC2:
4023 /* Set the DMA compare callbacks */
4024 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
4025 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4027 /* Set the DMA error callback */
4028 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
4030 /* Enable the DMA stream */
4031 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer,
4032 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4034 return HAL_ERROR;
4036 break;
4038 case TIM_DMA_CC3:
4040 /* Set the DMA compare callbacks */
4041 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
4042 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4044 /* Set the DMA error callback */
4045 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
4047 /* Enable the DMA stream */
4048 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer,
4049 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4051 return HAL_ERROR;
4053 break;
4055 case TIM_DMA_CC4:
4057 /* Set the DMA compare callbacks */
4058 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
4059 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4061 /* Set the DMA error callback */
4062 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
4064 /* Enable the DMA stream */
4065 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer,
4066 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4068 return HAL_ERROR;
4070 break;
4072 case TIM_DMA_COM:
4074 /* Set the DMA commutation callbacks */
4075 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
4076 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
4078 /* Set the DMA error callback */
4079 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
4081 /* Enable the DMA stream */
4082 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)BurstBuffer,
4083 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4085 return HAL_ERROR;
4087 break;
4089 case TIM_DMA_TRIGGER:
4091 /* Set the DMA trigger callbacks */
4092 htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
4093 htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
4095 /* Set the DMA error callback */
4096 htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
4098 /* Enable the DMA stream */
4099 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer,
4100 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4102 return HAL_ERROR;
4104 break;
4106 default:
4107 break;
4110 /* Configure the DMA Burst Mode */
4111 htim->Instance->DCR = (BurstBaseAddress | BurstLength);
4112 /* Enable the TIM DMA Request */
4113 __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
4115 htim->State = HAL_TIM_STATE_READY;
4117 /* Return function status */
4118 return HAL_OK;
4122 * @brief Stops the TIM DMA Burst mode
4123 * @param htim TIM handle
4124 * @param BurstRequestSrc TIM DMA Request sources to disable
4125 * @retval HAL status
4127 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
4129 HAL_StatusTypeDef status = HAL_OK;
4130 /* Check the parameters */
4131 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4133 /* Abort the DMA transfer (at least disable the DMA stream) */
4134 switch (BurstRequestSrc)
4136 case TIM_DMA_UPDATE:
4138 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
4139 break;
4141 case TIM_DMA_CC1:
4143 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
4144 break;
4146 case TIM_DMA_CC2:
4148 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
4149 break;
4151 case TIM_DMA_CC3:
4153 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
4154 break;
4156 case TIM_DMA_CC4:
4158 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
4159 break;
4161 case TIM_DMA_COM:
4163 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
4164 break;
4166 case TIM_DMA_TRIGGER:
4168 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
4169 break;
4171 default:
4172 break;
4175 if (HAL_OK == status)
4177 /* Disable the TIM Update DMA request */
4178 __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
4181 /* Return function status */
4182 return status;
4186 * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
4187 * @param htim TIM handle
4188 * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
4189 * This parameter can be one of the following values:
4190 * @arg TIM_DMABASE_CR1
4191 * @arg TIM_DMABASE_CR2
4192 * @arg TIM_DMABASE_SMCR
4193 * @arg TIM_DMABASE_DIER
4194 * @arg TIM_DMABASE_SR
4195 * @arg TIM_DMABASE_EGR
4196 * @arg TIM_DMABASE_CCMR1
4197 * @arg TIM_DMABASE_CCMR2
4198 * @arg TIM_DMABASE_CCER
4199 * @arg TIM_DMABASE_CNT
4200 * @arg TIM_DMABASE_PSC
4201 * @arg TIM_DMABASE_ARR
4202 * @arg TIM_DMABASE_RCR
4203 * @arg TIM_DMABASE_CCR1
4204 * @arg TIM_DMABASE_CCR2
4205 * @arg TIM_DMABASE_CCR3
4206 * @arg TIM_DMABASE_CCR4
4207 * @arg TIM_DMABASE_BDTR
4208 * @arg TIM_DMABASE_CCMR3
4209 * @arg TIM_DMABASE_CCR5
4210 * @arg TIM_DMABASE_CCR6
4211 * @arg TIM_DMABASE_AF1
4212 * @arg TIM_DMABASE_AF2
4213 * @arg TIM_DMABASE_TISEL
4215 * @param BurstRequestSrc TIM DMA Request sources
4216 * This parameter can be one of the following values:
4217 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
4218 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4219 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4220 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4221 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4222 * @arg TIM_DMA_COM: TIM Commutation DMA source
4223 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4224 * @param BurstBuffer The Buffer address.
4225 * @param BurstLength DMA Burst length. This parameter can be one value
4226 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4227 * @note This function should be used only when BurstLength is equal to DMA data transfer length.
4228 * @retval HAL status
4230 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4231 uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
4233 return HAL_TIM_DMABurst_MultiReadStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
4234 ((BurstLength) >> 8U) + 1U);
4238 * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
4239 * @param htim TIM handle
4240 * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
4241 * This parameter can be one of the following values:
4242 * @arg TIM_DMABASE_CR1
4243 * @arg TIM_DMABASE_CR2
4244 * @arg TIM_DMABASE_SMCR
4245 * @arg TIM_DMABASE_DIER
4246 * @arg TIM_DMABASE_SR
4247 * @arg TIM_DMABASE_EGR
4248 * @arg TIM_DMABASE_CCMR1
4249 * @arg TIM_DMABASE_CCMR2
4250 * @arg TIM_DMABASE_CCER
4251 * @arg TIM_DMABASE_CNT
4252 * @arg TIM_DMABASE_PSC
4253 * @arg TIM_DMABASE_ARR
4254 * @arg TIM_DMABASE_RCR
4255 * @arg TIM_DMABASE_CCR1
4256 * @arg TIM_DMABASE_CCR2
4257 * @arg TIM_DMABASE_CCR3
4258 * @arg TIM_DMABASE_CCR4
4259 * @arg TIM_DMABASE_BDTR
4260 * @arg TIM_DMABASE_CCMR3
4261 * @arg TIM_DMABASE_CCR5
4262 * @arg TIM_DMABASE_CCR6
4263 * @arg TIM_DMABASE_AF1
4264 * @arg TIM_DMABASE_AF2
4265 * @arg TIM_DMABASE_TISEL
4267 * @param BurstRequestSrc TIM DMA Request sources
4268 * This parameter can be one of the following values:
4269 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
4270 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4271 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4272 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4273 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4274 * @arg TIM_DMA_COM: TIM Commutation DMA source
4275 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4276 * @param BurstBuffer The Buffer address.
4277 * @param BurstLength DMA Burst length. This parameter can be one value
4278 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4279 * @param DataLength Data length. This parameter can be one value
4280 * between 1 and 0xFFFF.
4281 * @retval HAL status
4283 HAL_StatusTypeDef HAL_TIM_DMABurst_MultiReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4284 uint32_t BurstRequestSrc, uint32_t *BurstBuffer,
4285 uint32_t BurstLength, uint32_t DataLength)
4287 /* Check the parameters */
4288 assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
4289 assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
4290 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4291 assert_param(IS_TIM_DMA_LENGTH(BurstLength));
4292 assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
4294 if (htim->State == HAL_TIM_STATE_BUSY)
4296 return HAL_BUSY;
4298 else if (htim->State == HAL_TIM_STATE_READY)
4300 if ((BurstBuffer == NULL) && (BurstLength > 0U))
4302 return HAL_ERROR;
4304 else
4306 htim->State = HAL_TIM_STATE_BUSY;
4309 else
4311 /* nothing to do */
4313 switch (BurstRequestSrc)
4315 case TIM_DMA_UPDATE:
4317 /* Set the DMA Period elapsed callbacks */
4318 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
4319 htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
4321 /* Set the DMA error callback */
4322 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
4324 /* Enable the DMA stream */
4325 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4326 DataLength) != HAL_OK)
4328 return HAL_ERROR;
4330 break;
4332 case TIM_DMA_CC1:
4334 /* Set the DMA capture callbacks */
4335 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
4336 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
4338 /* Set the DMA error callback */
4339 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
4341 /* Enable the DMA stream */
4342 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4343 DataLength) != HAL_OK)
4345 return HAL_ERROR;
4347 break;
4349 case TIM_DMA_CC2:
4351 /* Set the DMA capture callbacks */
4352 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
4353 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
4355 /* Set the DMA error callback */
4356 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
4358 /* Enable the DMA stream */
4359 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4360 DataLength) != HAL_OK)
4362 return HAL_ERROR;
4364 break;
4366 case TIM_DMA_CC3:
4368 /* Set the DMA capture callbacks */
4369 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
4370 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
4372 /* Set the DMA error callback */
4373 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
4375 /* Enable the DMA stream */
4376 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4377 DataLength) != HAL_OK)
4379 return HAL_ERROR;
4381 break;
4383 case TIM_DMA_CC4:
4385 /* Set the DMA capture callbacks */
4386 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
4387 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
4389 /* Set the DMA error callback */
4390 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
4392 /* Enable the DMA stream */
4393 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4394 DataLength) != HAL_OK)
4396 return HAL_ERROR;
4398 break;
4400 case TIM_DMA_COM:
4402 /* Set the DMA commutation callbacks */
4403 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
4404 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
4406 /* Set the DMA error callback */
4407 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
4409 /* Enable the DMA stream */
4410 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4411 DataLength) != HAL_OK)
4413 return HAL_ERROR;
4415 break;
4417 case TIM_DMA_TRIGGER:
4419 /* Set the DMA trigger callbacks */
4420 htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
4421 htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
4423 /* Set the DMA error callback */
4424 htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
4426 /* Enable the DMA stream */
4427 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4428 DataLength) != HAL_OK)
4430 return HAL_ERROR;
4432 break;
4434 default:
4435 break;
4438 /* Configure the DMA Burst Mode */
4439 htim->Instance->DCR = (BurstBaseAddress | BurstLength);
4441 /* Enable the TIM DMA Request */
4442 __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
4444 htim->State = HAL_TIM_STATE_READY;
4446 /* Return function status */
4447 return HAL_OK;
4451 * @brief Stop the DMA burst reading
4452 * @param htim TIM handle
4453 * @param BurstRequestSrc TIM DMA Request sources to disable.
4454 * @retval HAL status
4456 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
4458 HAL_StatusTypeDef status = HAL_OK;
4459 /* Check the parameters */
4460 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4462 /* Abort the DMA transfer (at least disable the DMA stream) */
4463 switch (BurstRequestSrc)
4465 case TIM_DMA_UPDATE:
4467 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
4468 break;
4470 case TIM_DMA_CC1:
4472 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
4473 break;
4475 case TIM_DMA_CC2:
4477 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
4478 break;
4480 case TIM_DMA_CC3:
4482 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
4483 break;
4485 case TIM_DMA_CC4:
4487 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
4488 break;
4490 case TIM_DMA_COM:
4492 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
4493 break;
4495 case TIM_DMA_TRIGGER:
4497 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
4498 break;
4500 default:
4501 break;
4504 if (HAL_OK == status)
4506 /* Disable the TIM Update DMA request */
4507 __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
4510 /* Return function status */
4511 return status;
4515 * @brief Generate a software event
4516 * @param htim TIM handle
4517 * @param EventSource specifies the event source.
4518 * This parameter can be one of the following values:
4519 * @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
4520 * @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
4521 * @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
4522 * @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
4523 * @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
4524 * @arg TIM_EVENTSOURCE_COM: Timer COM event source
4525 * @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
4526 * @arg TIM_EVENTSOURCE_BREAK: Timer Break event source
4527 * @arg TIM_EVENTSOURCE_BREAK2: Timer Break2 event source
4528 * @note Basic timers can only generate an update event.
4529 * @note TIM_EVENTSOURCE_COM is relevant only with advanced timer instances.
4530 * @note TIM_EVENTSOURCE_BREAK and TIM_EVENTSOURCE_BREAK2 are relevant
4531 * only for timer instances supporting break input(s).
4532 * @retval HAL status
4535 HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
4537 /* Check the parameters */
4538 assert_param(IS_TIM_INSTANCE(htim->Instance));
4539 assert_param(IS_TIM_EVENT_SOURCE(EventSource));
4541 /* Process Locked */
4542 __HAL_LOCK(htim);
4544 /* Change the TIM state */
4545 htim->State = HAL_TIM_STATE_BUSY;
4547 /* Set the event sources */
4548 htim->Instance->EGR = EventSource;
4550 /* Change the TIM state */
4551 htim->State = HAL_TIM_STATE_READY;
4553 __HAL_UNLOCK(htim);
4555 /* Return function status */
4556 return HAL_OK;
4560 * @brief Configures the OCRef clear feature
4561 * @param htim TIM handle
4562 * @param sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
4563 * contains the OCREF clear feature and parameters for the TIM peripheral.
4564 * @param Channel specifies the TIM Channel
4565 * This parameter can be one of the following values:
4566 * @arg TIM_CHANNEL_1: TIM Channel 1
4567 * @arg TIM_CHANNEL_2: TIM Channel 2
4568 * @arg TIM_CHANNEL_3: TIM Channel 3
4569 * @arg TIM_CHANNEL_4: TIM Channel 4
4570 * @arg TIM_CHANNEL_5: TIM Channel 5
4571 * @arg TIM_CHANNEL_6: TIM Channel 6
4572 * @retval HAL status
4574 HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
4575 TIM_ClearInputConfigTypeDef *sClearInputConfig,
4576 uint32_t Channel)
4578 /* Check the parameters */
4579 assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
4580 assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
4582 /* Process Locked */
4583 __HAL_LOCK(htim);
4585 htim->State = HAL_TIM_STATE_BUSY;
4587 switch (sClearInputConfig->ClearInputSource)
4589 case TIM_CLEARINPUTSOURCE_NONE:
4591 /* Clear the OCREF clear selection bit and the the ETR Bits */
4592 CLEAR_BIT(htim->Instance->SMCR, (TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP));
4593 break;
4596 case TIM_CLEARINPUTSOURCE_ETR:
4598 /* Check the parameters */
4599 assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
4600 assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
4601 assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
4603 /* When OCRef clear feature is used with ETR source, ETR prescaler must be off */
4604 if (sClearInputConfig->ClearInputPrescaler != TIM_CLEARINPUTPRESCALER_DIV1)
4606 htim->State = HAL_TIM_STATE_READY;
4607 __HAL_UNLOCK(htim);
4608 return HAL_ERROR;
4611 TIM_ETR_SetConfig(htim->Instance,
4612 sClearInputConfig->ClearInputPrescaler,
4613 sClearInputConfig->ClearInputPolarity,
4614 sClearInputConfig->ClearInputFilter);
4615 break;
4618 default:
4619 break;
4622 switch (Channel)
4624 case TIM_CHANNEL_1:
4626 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
4628 /* Enable the OCREF clear feature for Channel 1 */
4629 SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
4631 else
4633 /* Disable the OCREF clear feature for Channel 1 */
4634 CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
4636 break;
4638 case TIM_CHANNEL_2:
4640 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
4642 /* Enable the OCREF clear feature for Channel 2 */
4643 SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
4645 else
4647 /* Disable the OCREF clear feature for Channel 2 */
4648 CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
4650 break;
4652 case TIM_CHANNEL_3:
4654 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
4656 /* Enable the OCREF clear feature for Channel 3 */
4657 SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
4659 else
4661 /* Disable the OCREF clear feature for Channel 3 */
4662 CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
4664 break;
4666 case TIM_CHANNEL_4:
4668 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
4670 /* Enable the OCREF clear feature for Channel 4 */
4671 SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
4673 else
4675 /* Disable the OCREF clear feature for Channel 4 */
4676 CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
4678 break;
4680 case TIM_CHANNEL_5:
4682 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
4684 /* Enable the OCREF clear feature for Channel 5 */
4685 SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
4687 else
4689 /* Disable the OCREF clear feature for Channel 5 */
4690 CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
4692 break;
4694 case TIM_CHANNEL_6:
4696 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
4698 /* Enable the OCREF clear feature for Channel 6 */
4699 SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
4701 else
4703 /* Disable the OCREF clear feature for Channel 6 */
4704 CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
4706 break;
4708 default:
4709 break;
4712 htim->State = HAL_TIM_STATE_READY;
4714 __HAL_UNLOCK(htim);
4716 return HAL_OK;
4720 * @brief Configures the clock source to be used
4721 * @param htim TIM handle
4722 * @param sClockSourceConfig pointer to a TIM_ClockConfigTypeDef structure that
4723 * contains the clock source information for the TIM peripheral.
4724 * @retval HAL status
4726 HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, TIM_ClockConfigTypeDef *sClockSourceConfig)
4728 uint32_t tmpsmcr;
4730 /* Process Locked */
4731 __HAL_LOCK(htim);
4733 htim->State = HAL_TIM_STATE_BUSY;
4735 /* Check the parameters */
4736 assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
4738 /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
4739 tmpsmcr = htim->Instance->SMCR;
4740 tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
4741 tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
4742 htim->Instance->SMCR = tmpsmcr;
4744 switch (sClockSourceConfig->ClockSource)
4746 case TIM_CLOCKSOURCE_INTERNAL:
4748 assert_param(IS_TIM_INSTANCE(htim->Instance));
4749 break;
4752 case TIM_CLOCKSOURCE_ETRMODE1:
4754 /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
4755 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
4757 /* Check ETR input conditioning related parameters */
4758 assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
4759 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
4760 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
4762 /* Configure the ETR Clock source */
4763 TIM_ETR_SetConfig(htim->Instance,
4764 sClockSourceConfig->ClockPrescaler,
4765 sClockSourceConfig->ClockPolarity,
4766 sClockSourceConfig->ClockFilter);
4768 /* Select the External clock mode1 and the ETRF trigger */
4769 tmpsmcr = htim->Instance->SMCR;
4770 tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
4771 /* Write to TIMx SMCR */
4772 htim->Instance->SMCR = tmpsmcr;
4773 break;
4776 case TIM_CLOCKSOURCE_ETRMODE2:
4778 /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
4779 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
4781 /* Check ETR input conditioning related parameters */
4782 assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
4783 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
4784 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
4786 /* Configure the ETR Clock source */
4787 TIM_ETR_SetConfig(htim->Instance,
4788 sClockSourceConfig->ClockPrescaler,
4789 sClockSourceConfig->ClockPolarity,
4790 sClockSourceConfig->ClockFilter);
4791 /* Enable the External clock mode2 */
4792 htim->Instance->SMCR |= TIM_SMCR_ECE;
4793 break;
4796 case TIM_CLOCKSOURCE_TI1:
4798 /* Check whether or not the timer instance supports external clock mode 1 */
4799 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
4801 /* Check TI1 input conditioning related parameters */
4802 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
4803 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
4805 TIM_TI1_ConfigInputStage(htim->Instance,
4806 sClockSourceConfig->ClockPolarity,
4807 sClockSourceConfig->ClockFilter);
4808 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
4809 break;
4812 case TIM_CLOCKSOURCE_TI2:
4814 /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
4815 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
4817 /* Check TI2 input conditioning related parameters */
4818 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
4819 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
4821 TIM_TI2_ConfigInputStage(htim->Instance,
4822 sClockSourceConfig->ClockPolarity,
4823 sClockSourceConfig->ClockFilter);
4824 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
4825 break;
4828 case TIM_CLOCKSOURCE_TI1ED:
4830 /* Check whether or not the timer instance supports external clock mode 1 */
4831 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
4833 /* Check TI1 input conditioning related parameters */
4834 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
4835 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
4837 TIM_TI1_ConfigInputStage(htim->Instance,
4838 sClockSourceConfig->ClockPolarity,
4839 sClockSourceConfig->ClockFilter);
4840 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
4841 break;
4844 case TIM_CLOCKSOURCE_ITR0:
4845 case TIM_CLOCKSOURCE_ITR1:
4846 case TIM_CLOCKSOURCE_ITR2:
4847 case TIM_CLOCKSOURCE_ITR3:
4848 case TIM_CLOCKSOURCE_ITR4:
4849 case TIM_CLOCKSOURCE_ITR5:
4850 case TIM_CLOCKSOURCE_ITR6:
4851 case TIM_CLOCKSOURCE_ITR7:
4852 case TIM_CLOCKSOURCE_ITR8:
4854 /* Check whether or not the timer instance supports internal trigger input */
4855 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
4857 TIM_ITRx_SetConfig(htim->Instance, sClockSourceConfig->ClockSource);
4858 break;
4861 default:
4862 break;
4864 htim->State = HAL_TIM_STATE_READY;
4866 __HAL_UNLOCK(htim);
4868 return HAL_OK;
4872 * @brief Selects the signal connected to the TI1 input: direct from CH1_input
4873 * or a XOR combination between CH1_input, CH2_input & CH3_input
4874 * @param htim TIM handle.
4875 * @param TI1_Selection Indicate whether or not channel 1 is connected to the
4876 * output of a XOR gate.
4877 * This parameter can be one of the following values:
4878 * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
4879 * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
4880 * pins are connected to the TI1 input (XOR combination)
4881 * @retval HAL status
4883 HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
4885 uint32_t tmpcr2;
4887 /* Check the parameters */
4888 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
4889 assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
4891 /* Get the TIMx CR2 register value */
4892 tmpcr2 = htim->Instance->CR2;
4894 /* Reset the TI1 selection */
4895 tmpcr2 &= ~TIM_CR2_TI1S;
4897 /* Set the TI1 selection */
4898 tmpcr2 |= TI1_Selection;
4900 /* Write to TIMxCR2 */
4901 htim->Instance->CR2 = tmpcr2;
4903 return HAL_OK;
4907 * @brief Configures the TIM in Slave mode
4908 * @param htim TIM handle.
4909 * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
4910 * contains the selected trigger (internal trigger input, filtered
4911 * timer input or external trigger input) and the Slave mode
4912 * (Disable, Reset, Gated, Trigger, External clock mode 1).
4913 * @retval HAL status
4915 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro(TIM_HandleTypeDef *htim, TIM_SlaveConfigTypeDef *sSlaveConfig)
4917 /* Check the parameters */
4918 assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
4919 assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
4920 assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
4922 __HAL_LOCK(htim);
4924 htim->State = HAL_TIM_STATE_BUSY;
4926 if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
4928 htim->State = HAL_TIM_STATE_READY;
4929 __HAL_UNLOCK(htim);
4930 return HAL_ERROR;
4933 /* Disable Trigger Interrupt */
4934 __HAL_TIM_DISABLE_IT(htim, TIM_IT_TRIGGER);
4936 /* Disable Trigger DMA request */
4937 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
4939 htim->State = HAL_TIM_STATE_READY;
4941 __HAL_UNLOCK(htim);
4943 return HAL_OK;
4947 * @brief Configures the TIM in Slave mode in interrupt mode
4948 * @param htim TIM handle.
4949 * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
4950 * contains the selected trigger (internal trigger input, filtered
4951 * timer input or external trigger input) and the Slave mode
4952 * (Disable, Reset, Gated, Trigger, External clock mode 1).
4953 * @retval HAL status
4955 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro_IT(TIM_HandleTypeDef *htim,
4956 TIM_SlaveConfigTypeDef *sSlaveConfig)
4958 /* Check the parameters */
4959 assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
4960 assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
4961 assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
4963 __HAL_LOCK(htim);
4965 htim->State = HAL_TIM_STATE_BUSY;
4967 if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
4969 htim->State = HAL_TIM_STATE_READY;
4970 __HAL_UNLOCK(htim);
4971 return HAL_ERROR;
4974 /* Enable Trigger Interrupt */
4975 __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);
4977 /* Disable Trigger DMA request */
4978 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
4980 htim->State = HAL_TIM_STATE_READY;
4982 __HAL_UNLOCK(htim);
4984 return HAL_OK;
4988 * @brief Read the captured value from Capture Compare unit
4989 * @param htim TIM handle.
4990 * @param Channel TIM Channels to be enabled
4991 * This parameter can be one of the following values:
4992 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
4993 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
4994 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
4995 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
4996 * @retval Captured value
4998 uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)
5000 uint32_t tmpreg = 0U;
5002 switch (Channel)
5004 case TIM_CHANNEL_1:
5006 /* Check the parameters */
5007 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
5009 /* Return the capture 1 value */
5010 tmpreg = htim->Instance->CCR1;
5012 break;
5014 case TIM_CHANNEL_2:
5016 /* Check the parameters */
5017 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
5019 /* Return the capture 2 value */
5020 tmpreg = htim->Instance->CCR2;
5022 break;
5025 case TIM_CHANNEL_3:
5027 /* Check the parameters */
5028 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
5030 /* Return the capture 3 value */
5031 tmpreg = htim->Instance->CCR3;
5033 break;
5036 case TIM_CHANNEL_4:
5038 /* Check the parameters */
5039 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
5041 /* Return the capture 4 value */
5042 tmpreg = htim->Instance->CCR4;
5044 break;
5047 default:
5048 break;
5051 return tmpreg;
5055 * @}
5058 /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
5059 * @brief TIM Callbacks functions
5061 @verbatim
5062 ==============================================================================
5063 ##### TIM Callbacks functions #####
5064 ==============================================================================
5065 [..]
5066 This section provides TIM callback functions:
5067 (+) TIM Period elapsed callback
5068 (+) TIM Output Compare callback
5069 (+) TIM Input capture callback
5070 (+) TIM Trigger callback
5071 (+) TIM Error callback
5073 @endverbatim
5074 * @{
5078 * @brief Period elapsed callback in non-blocking mode
5079 * @param htim TIM handle
5080 * @retval None
5082 __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
5084 /* Prevent unused argument(s) compilation warning */
5085 UNUSED(htim);
5087 /* NOTE : This function should not be modified, when the callback is needed,
5088 the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
5093 * @brief Period elapsed half complete callback in non-blocking mode
5094 * @param htim TIM handle
5095 * @retval None
5097 __weak void HAL_TIM_PeriodElapsedHalfCpltCallback(TIM_HandleTypeDef *htim)
5099 /* Prevent unused argument(s) compilation warning */
5100 UNUSED(htim);
5102 /* NOTE : This function should not be modified, when the callback is needed,
5103 the HAL_TIM_PeriodElapsedHalfCpltCallback could be implemented in the user file
5108 * @brief Output Compare callback in non-blocking mode
5109 * @param htim TIM OC handle
5110 * @retval None
5112 __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
5114 /* Prevent unused argument(s) compilation warning */
5115 UNUSED(htim);
5117 /* NOTE : This function should not be modified, when the callback is needed,
5118 the HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
5123 * @brief Input Capture callback in non-blocking mode
5124 * @param htim TIM IC handle
5125 * @retval None
5127 __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
5129 /* Prevent unused argument(s) compilation warning */
5130 UNUSED(htim);
5132 /* NOTE : This function should not be modified, when the callback is needed,
5133 the HAL_TIM_IC_CaptureCallback could be implemented in the user file
5138 * @brief Input Capture half complete callback in non-blocking mode
5139 * @param htim TIM IC handle
5140 * @retval None
5142 __weak void HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef *htim)
5144 /* Prevent unused argument(s) compilation warning */
5145 UNUSED(htim);
5147 /* NOTE : This function should not be modified, when the callback is needed,
5148 the HAL_TIM_IC_CaptureHalfCpltCallback could be implemented in the user file
5153 * @brief PWM Pulse finished callback in non-blocking mode
5154 * @param htim TIM handle
5155 * @retval None
5157 __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
5159 /* Prevent unused argument(s) compilation warning */
5160 UNUSED(htim);
5162 /* NOTE : This function should not be modified, when the callback is needed,
5163 the HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
5168 * @brief PWM Pulse finished half complete callback in non-blocking mode
5169 * @param htim TIM handle
5170 * @retval None
5172 __weak void HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef *htim)
5174 /* Prevent unused argument(s) compilation warning */
5175 UNUSED(htim);
5177 /* NOTE : This function should not be modified, when the callback is needed,
5178 the HAL_TIM_PWM_PulseFinishedHalfCpltCallback could be implemented in the user file
5183 * @brief Hall Trigger detection callback in non-blocking mode
5184 * @param htim TIM handle
5185 * @retval None
5187 __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
5189 /* Prevent unused argument(s) compilation warning */
5190 UNUSED(htim);
5192 /* NOTE : This function should not be modified, when the callback is needed,
5193 the HAL_TIM_TriggerCallback could be implemented in the user file
5198 * @brief Hall Trigger detection half complete callback in non-blocking mode
5199 * @param htim TIM handle
5200 * @retval None
5202 __weak void HAL_TIM_TriggerHalfCpltCallback(TIM_HandleTypeDef *htim)
5204 /* Prevent unused argument(s) compilation warning */
5205 UNUSED(htim);
5207 /* NOTE : This function should not be modified, when the callback is needed,
5208 the HAL_TIM_TriggerHalfCpltCallback could be implemented in the user file
5213 * @brief Timer error callback in non-blocking mode
5214 * @param htim TIM handle
5215 * @retval None
5217 __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
5219 /* Prevent unused argument(s) compilation warning */
5220 UNUSED(htim);
5222 /* NOTE : This function should not be modified, when the callback is needed,
5223 the HAL_TIM_ErrorCallback could be implemented in the user file
5227 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
5229 * @brief Register a User TIM callback to be used instead of the weak predefined callback
5230 * @param htim tim handle
5231 * @param CallbackID ID of the callback to be registered
5232 * This parameter can be one of the following values:
5233 * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
5234 * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
5235 * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
5236 * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
5237 * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
5238 * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
5239 * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
5240 * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
5241 * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
5242 * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
5243 * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
5244 * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
5245 * @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
5246 * @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
5247 * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
5248 * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
5249 * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
5250 * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
5251 * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
5252 * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
5253 * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
5254 * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
5255 * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
5256 * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
5257 * @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
5258 * @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
5259 * @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
5260 * @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
5261 * @param pCallback pointer to the callback function
5262 * @retval status
5264 HAL_StatusTypeDef HAL_TIM_RegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID,
5265 pTIM_CallbackTypeDef pCallback)
5267 HAL_StatusTypeDef status = HAL_OK;
5269 if (pCallback == NULL)
5271 return HAL_ERROR;
5273 /* Process locked */
5274 __HAL_LOCK(htim);
5276 if (htim->State == HAL_TIM_STATE_READY)
5278 switch (CallbackID)
5280 case HAL_TIM_BASE_MSPINIT_CB_ID :
5281 htim->Base_MspInitCallback = pCallback;
5282 break;
5284 case HAL_TIM_BASE_MSPDEINIT_CB_ID :
5285 htim->Base_MspDeInitCallback = pCallback;
5286 break;
5288 case HAL_TIM_IC_MSPINIT_CB_ID :
5289 htim->IC_MspInitCallback = pCallback;
5290 break;
5292 case HAL_TIM_IC_MSPDEINIT_CB_ID :
5293 htim->IC_MspDeInitCallback = pCallback;
5294 break;
5296 case HAL_TIM_OC_MSPINIT_CB_ID :
5297 htim->OC_MspInitCallback = pCallback;
5298 break;
5300 case HAL_TIM_OC_MSPDEINIT_CB_ID :
5301 htim->OC_MspDeInitCallback = pCallback;
5302 break;
5304 case HAL_TIM_PWM_MSPINIT_CB_ID :
5305 htim->PWM_MspInitCallback = pCallback;
5306 break;
5308 case HAL_TIM_PWM_MSPDEINIT_CB_ID :
5309 htim->PWM_MspDeInitCallback = pCallback;
5310 break;
5312 case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
5313 htim->OnePulse_MspInitCallback = pCallback;
5314 break;
5316 case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
5317 htim->OnePulse_MspDeInitCallback = pCallback;
5318 break;
5320 case HAL_TIM_ENCODER_MSPINIT_CB_ID :
5321 htim->Encoder_MspInitCallback = pCallback;
5322 break;
5324 case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
5325 htim->Encoder_MspDeInitCallback = pCallback;
5326 break;
5328 case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
5329 htim->HallSensor_MspInitCallback = pCallback;
5330 break;
5332 case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
5333 htim->HallSensor_MspDeInitCallback = pCallback;
5334 break;
5336 case HAL_TIM_PERIOD_ELAPSED_CB_ID :
5337 htim->PeriodElapsedCallback = pCallback;
5338 break;
5340 case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
5341 htim->PeriodElapsedHalfCpltCallback = pCallback;
5342 break;
5344 case HAL_TIM_TRIGGER_CB_ID :
5345 htim->TriggerCallback = pCallback;
5346 break;
5348 case HAL_TIM_TRIGGER_HALF_CB_ID :
5349 htim->TriggerHalfCpltCallback = pCallback;
5350 break;
5352 case HAL_TIM_IC_CAPTURE_CB_ID :
5353 htim->IC_CaptureCallback = pCallback;
5354 break;
5356 case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
5357 htim->IC_CaptureHalfCpltCallback = pCallback;
5358 break;
5360 case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
5361 htim->OC_DelayElapsedCallback = pCallback;
5362 break;
5364 case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
5365 htim->PWM_PulseFinishedCallback = pCallback;
5366 break;
5368 case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
5369 htim->PWM_PulseFinishedHalfCpltCallback = pCallback;
5370 break;
5372 case HAL_TIM_ERROR_CB_ID :
5373 htim->ErrorCallback = pCallback;
5374 break;
5376 case HAL_TIM_COMMUTATION_CB_ID :
5377 htim->CommutationCallback = pCallback;
5378 break;
5380 case HAL_TIM_COMMUTATION_HALF_CB_ID :
5381 htim->CommutationHalfCpltCallback = pCallback;
5382 break;
5384 case HAL_TIM_BREAK_CB_ID :
5385 htim->BreakCallback = pCallback;
5386 break;
5388 case HAL_TIM_BREAK2_CB_ID :
5389 htim->Break2Callback = pCallback;
5390 break;
5392 default :
5393 /* Return error status */
5394 status = HAL_ERROR;
5395 break;
5398 else if (htim->State == HAL_TIM_STATE_RESET)
5400 switch (CallbackID)
5402 case HAL_TIM_BASE_MSPINIT_CB_ID :
5403 htim->Base_MspInitCallback = pCallback;
5404 break;
5406 case HAL_TIM_BASE_MSPDEINIT_CB_ID :
5407 htim->Base_MspDeInitCallback = pCallback;
5408 break;
5410 case HAL_TIM_IC_MSPINIT_CB_ID :
5411 htim->IC_MspInitCallback = pCallback;
5412 break;
5414 case HAL_TIM_IC_MSPDEINIT_CB_ID :
5415 htim->IC_MspDeInitCallback = pCallback;
5416 break;
5418 case HAL_TIM_OC_MSPINIT_CB_ID :
5419 htim->OC_MspInitCallback = pCallback;
5420 break;
5422 case HAL_TIM_OC_MSPDEINIT_CB_ID :
5423 htim->OC_MspDeInitCallback = pCallback;
5424 break;
5426 case HAL_TIM_PWM_MSPINIT_CB_ID :
5427 htim->PWM_MspInitCallback = pCallback;
5428 break;
5430 case HAL_TIM_PWM_MSPDEINIT_CB_ID :
5431 htim->PWM_MspDeInitCallback = pCallback;
5432 break;
5434 case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
5435 htim->OnePulse_MspInitCallback = pCallback;
5436 break;
5438 case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
5439 htim->OnePulse_MspDeInitCallback = pCallback;
5440 break;
5442 case HAL_TIM_ENCODER_MSPINIT_CB_ID :
5443 htim->Encoder_MspInitCallback = pCallback;
5444 break;
5446 case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
5447 htim->Encoder_MspDeInitCallback = pCallback;
5448 break;
5450 case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
5451 htim->HallSensor_MspInitCallback = pCallback;
5452 break;
5454 case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
5455 htim->HallSensor_MspDeInitCallback = pCallback;
5456 break;
5458 default :
5459 /* Return error status */
5460 status = HAL_ERROR;
5461 break;
5464 else
5466 /* Return error status */
5467 status = HAL_ERROR;
5470 /* Release Lock */
5471 __HAL_UNLOCK(htim);
5473 return status;
5477 * @brief Unregister a TIM callback
5478 * TIM callback is redirected to the weak predefined callback
5479 * @param htim tim handle
5480 * @param CallbackID ID of the callback to be unregistered
5481 * This parameter can be one of the following values:
5482 * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
5483 * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
5484 * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
5485 * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
5486 * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
5487 * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
5488 * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
5489 * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
5490 * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
5491 * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
5492 * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
5493 * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
5494 * @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
5495 * @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
5496 * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
5497 * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
5498 * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
5499 * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
5500 * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
5501 * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
5502 * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
5503 * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
5504 * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
5505 * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
5506 * @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
5507 * @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
5508 * @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
5509 * @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
5510 * @retval status
5512 HAL_StatusTypeDef HAL_TIM_UnRegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID)
5514 HAL_StatusTypeDef status = HAL_OK;
5516 /* Process locked */
5517 __HAL_LOCK(htim);
5519 if (htim->State == HAL_TIM_STATE_READY)
5521 switch (CallbackID)
5523 case HAL_TIM_BASE_MSPINIT_CB_ID :
5524 htim->Base_MspInitCallback = HAL_TIM_Base_MspInit; /* Legacy weak Base MspInit Callback */
5525 break;
5527 case HAL_TIM_BASE_MSPDEINIT_CB_ID :
5528 htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit; /* Legacy weak Base Msp DeInit Callback */
5529 break;
5531 case HAL_TIM_IC_MSPINIT_CB_ID :
5532 htim->IC_MspInitCallback = HAL_TIM_IC_MspInit; /* Legacy weak IC Msp Init Callback */
5533 break;
5535 case HAL_TIM_IC_MSPDEINIT_CB_ID :
5536 htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit; /* Legacy weak IC Msp DeInit Callback */
5537 break;
5539 case HAL_TIM_OC_MSPINIT_CB_ID :
5540 htim->OC_MspInitCallback = HAL_TIM_OC_MspInit; /* Legacy weak OC Msp Init Callback */
5541 break;
5543 case HAL_TIM_OC_MSPDEINIT_CB_ID :
5544 htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit; /* Legacy weak OC Msp DeInit Callback */
5545 break;
5547 case HAL_TIM_PWM_MSPINIT_CB_ID :
5548 htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit; /* Legacy weak PWM Msp Init Callback */
5549 break;
5551 case HAL_TIM_PWM_MSPDEINIT_CB_ID :
5552 htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit; /* Legacy weak PWM Msp DeInit Callback */
5553 break;
5555 case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
5556 htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit; /* Legacy weak One Pulse Msp Init Callback */
5557 break;
5559 case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
5560 htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit; /* Legacy weak One Pulse Msp DeInit Callback */
5561 break;
5563 case HAL_TIM_ENCODER_MSPINIT_CB_ID :
5564 htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit; /* Legacy weak Encoder Msp Init Callback */
5565 break;
5567 case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
5568 htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit; /* Legacy weak Encoder Msp DeInit Callback */
5569 break;
5571 case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
5572 htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit; /* Legacy weak Hall Sensor Msp Init Callback */
5573 break;
5575 case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
5576 htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit; /* Legacy weak Hall Sensor Msp DeInit Callback */
5577 break;
5579 case HAL_TIM_PERIOD_ELAPSED_CB_ID :
5580 htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback; /* Legacy weak Period Elapsed Callback */
5581 break;
5583 case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
5584 htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback; /* Legacy weak Period Elapsed half complete Callback */
5585 break;
5587 case HAL_TIM_TRIGGER_CB_ID :
5588 htim->TriggerCallback = HAL_TIM_TriggerCallback; /* Legacy weak Trigger Callback */
5589 break;
5591 case HAL_TIM_TRIGGER_HALF_CB_ID :
5592 htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback; /* Legacy weak Trigger half complete Callback */
5593 break;
5595 case HAL_TIM_IC_CAPTURE_CB_ID :
5596 htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback; /* Legacy weak IC Capture Callback */
5597 break;
5599 case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
5600 htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback; /* Legacy weak IC Capture half complete Callback */
5601 break;
5603 case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
5604 htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback; /* Legacy weak OC Delay Elapsed Callback */
5605 break;
5607 case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
5608 htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback; /* Legacy weak PWM Pulse Finished Callback */
5609 break;
5611 case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
5612 htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback; /* Legacy weak PWM Pulse Finished half complete Callback */
5613 break;
5615 case HAL_TIM_ERROR_CB_ID :
5616 htim->ErrorCallback = HAL_TIM_ErrorCallback; /* Legacy weak Error Callback */
5617 break;
5619 case HAL_TIM_COMMUTATION_CB_ID :
5620 htim->CommutationCallback = HAL_TIMEx_CommutCallback; /* Legacy weak Commutation Callback */
5621 break;
5623 case HAL_TIM_COMMUTATION_HALF_CB_ID :
5624 htim->CommutationHalfCpltCallback = HAL_TIMEx_CommutHalfCpltCallback; /* Legacy weak Commutation half complete Callback */
5625 break;
5627 case HAL_TIM_BREAK_CB_ID :
5628 htim->BreakCallback = HAL_TIMEx_BreakCallback; /* Legacy weak Break Callback */
5629 break;
5631 case HAL_TIM_BREAK2_CB_ID :
5632 htim->Break2Callback = HAL_TIMEx_Break2Callback; /* Legacy weak Break2 Callback */
5633 break;
5635 default :
5636 /* Return error status */
5637 status = HAL_ERROR;
5638 break;
5641 else if (htim->State == HAL_TIM_STATE_RESET)
5643 switch (CallbackID)
5645 case HAL_TIM_BASE_MSPINIT_CB_ID :
5646 htim->Base_MspInitCallback = HAL_TIM_Base_MspInit; /* Legacy weak Base MspInit Callback */
5647 break;
5649 case HAL_TIM_BASE_MSPDEINIT_CB_ID :
5650 htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit; /* Legacy weak Base Msp DeInit Callback */
5651 break;
5653 case HAL_TIM_IC_MSPINIT_CB_ID :
5654 htim->IC_MspInitCallback = HAL_TIM_IC_MspInit; /* Legacy weak IC Msp Init Callback */
5655 break;
5657 case HAL_TIM_IC_MSPDEINIT_CB_ID :
5658 htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit; /* Legacy weak IC Msp DeInit Callback */
5659 break;
5661 case HAL_TIM_OC_MSPINIT_CB_ID :
5662 htim->OC_MspInitCallback = HAL_TIM_OC_MspInit; /* Legacy weak OC Msp Init Callback */
5663 break;
5665 case HAL_TIM_OC_MSPDEINIT_CB_ID :
5666 htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit; /* Legacy weak OC Msp DeInit Callback */
5667 break;
5669 case HAL_TIM_PWM_MSPINIT_CB_ID :
5670 htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit; /* Legacy weak PWM Msp Init Callback */
5671 break;
5673 case HAL_TIM_PWM_MSPDEINIT_CB_ID :
5674 htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit; /* Legacy weak PWM Msp DeInit Callback */
5675 break;
5677 case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
5678 htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit; /* Legacy weak One Pulse Msp Init Callback */
5679 break;
5681 case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
5682 htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit; /* Legacy weak One Pulse Msp DeInit Callback */
5683 break;
5685 case HAL_TIM_ENCODER_MSPINIT_CB_ID :
5686 htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit; /* Legacy weak Encoder Msp Init Callback */
5687 break;
5689 case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
5690 htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit; /* Legacy weak Encoder Msp DeInit Callback */
5691 break;
5693 case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
5694 htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit; /* Legacy weak Hall Sensor Msp Init Callback */
5695 break;
5697 case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
5698 htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit; /* Legacy weak Hall Sensor Msp DeInit Callback */
5699 break;
5701 default :
5702 /* Return error status */
5703 status = HAL_ERROR;
5704 break;
5707 else
5709 /* Return error status */
5710 status = HAL_ERROR;
5713 /* Release Lock */
5714 __HAL_UNLOCK(htim);
5716 return status;
5718 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
5721 * @}
5724 /** @defgroup TIM_Exported_Functions_Group10 TIM Peripheral State functions
5725 * @brief TIM Peripheral State functions
5727 @verbatim
5728 ==============================================================================
5729 ##### Peripheral State functions #####
5730 ==============================================================================
5731 [..]
5732 This subsection permits to get in run-time the status of the peripheral
5733 and the data flow.
5735 @endverbatim
5736 * @{
5740 * @brief Return the TIM Base handle state.
5741 * @param htim TIM Base handle
5742 * @retval HAL state
5744 HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(TIM_HandleTypeDef *htim)
5746 return htim->State;
5750 * @brief Return the TIM OC handle state.
5751 * @param htim TIM Output Compare handle
5752 * @retval HAL state
5754 HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(TIM_HandleTypeDef *htim)
5756 return htim->State;
5760 * @brief Return the TIM PWM handle state.
5761 * @param htim TIM handle
5762 * @retval HAL state
5764 HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(TIM_HandleTypeDef *htim)
5766 return htim->State;
5770 * @brief Return the TIM Input Capture handle state.
5771 * @param htim TIM IC handle
5772 * @retval HAL state
5774 HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(TIM_HandleTypeDef *htim)
5776 return htim->State;
5780 * @brief Return the TIM One Pulse Mode handle state.
5781 * @param htim TIM OPM handle
5782 * @retval HAL state
5784 HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(TIM_HandleTypeDef *htim)
5786 return htim->State;
5790 * @brief Return the TIM Encoder Mode handle state.
5791 * @param htim TIM Encoder Interface handle
5792 * @retval HAL state
5794 HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(TIM_HandleTypeDef *htim)
5796 return htim->State;
5800 * @}
5804 * @}
5807 /** @defgroup TIM_Private_Functions TIM Private Functions
5808 * @{
5812 * @brief TIM DMA error callback
5813 * @param hdma pointer to DMA handle.
5814 * @retval None
5816 void TIM_DMAError(DMA_HandleTypeDef *hdma)
5818 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
5820 htim->State = HAL_TIM_STATE_READY;
5822 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
5823 htim->ErrorCallback(htim);
5824 #else
5825 HAL_TIM_ErrorCallback(htim);
5826 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
5830 * @brief TIM DMA Delay Pulse complete callback.
5831 * @param hdma pointer to DMA handle.
5832 * @retval None
5834 void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
5836 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
5838 htim->State = HAL_TIM_STATE_READY;
5840 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
5842 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
5844 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
5846 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
5848 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
5850 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
5852 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
5854 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
5856 else
5858 /* nothing to do */
5861 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
5862 htim->PWM_PulseFinishedCallback(htim);
5863 #else
5864 HAL_TIM_PWM_PulseFinishedCallback(htim);
5865 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
5867 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
5871 * @brief TIM DMA Delay Pulse half complete callback.
5872 * @param hdma pointer to DMA handle.
5873 * @retval None
5875 void TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef *hdma)
5877 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
5879 htim->State = HAL_TIM_STATE_READY;
5881 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
5883 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
5885 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
5887 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
5889 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
5891 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
5893 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
5895 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
5897 else
5899 /* nothing to do */
5902 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
5903 htim->PWM_PulseFinishedHalfCpltCallback(htim);
5904 #else
5905 HAL_TIM_PWM_PulseFinishedHalfCpltCallback(htim);
5906 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
5908 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
5912 * @brief TIM DMA Capture complete callback.
5913 * @param hdma pointer to DMA handle.
5914 * @retval None
5916 void TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
5918 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
5920 htim->State = HAL_TIM_STATE_READY;
5922 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
5924 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
5926 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
5928 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
5930 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
5932 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
5934 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
5936 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
5938 else
5940 /* nothing to do */
5943 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
5944 htim->IC_CaptureCallback(htim);
5945 #else
5946 HAL_TIM_IC_CaptureCallback(htim);
5947 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
5949 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
5953 * @brief TIM DMA Capture half complete callback.
5954 * @param hdma pointer to DMA handle.
5955 * @retval None
5957 void TIM_DMACaptureHalfCplt(DMA_HandleTypeDef *hdma)
5959 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
5961 htim->State = HAL_TIM_STATE_READY;
5963 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
5965 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
5967 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
5969 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
5971 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
5973 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
5975 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
5977 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
5979 else
5981 /* nothing to do */
5984 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
5985 htim->IC_CaptureHalfCpltCallback(htim);
5986 #else
5987 HAL_TIM_IC_CaptureHalfCpltCallback(htim);
5988 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
5990 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
5994 * @brief TIM DMA Period Elapse complete callback.
5995 * @param hdma pointer to DMA handle.
5996 * @retval None
5998 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
6000 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6002 htim->State = HAL_TIM_STATE_READY;
6004 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6005 htim->PeriodElapsedCallback(htim);
6006 #else
6007 HAL_TIM_PeriodElapsedCallback(htim);
6008 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6012 * @brief TIM DMA Period Elapse half complete callback.
6013 * @param hdma pointer to DMA handle.
6014 * @retval None
6016 static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma)
6018 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6020 htim->State = HAL_TIM_STATE_READY;
6022 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6023 htim->PeriodElapsedHalfCpltCallback(htim);
6024 #else
6025 HAL_TIM_PeriodElapsedHalfCpltCallback(htim);
6026 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6030 * @brief TIM DMA Trigger callback.
6031 * @param hdma pointer to DMA handle.
6032 * @retval None
6034 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
6036 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6038 htim->State = HAL_TIM_STATE_READY;
6040 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6041 htim->TriggerCallback(htim);
6042 #else
6043 HAL_TIM_TriggerCallback(htim);
6044 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6048 * @brief TIM DMA Trigger half complete callback.
6049 * @param hdma pointer to DMA handle.
6050 * @retval None
6052 static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma)
6054 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6056 htim->State = HAL_TIM_STATE_READY;
6058 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6059 htim->TriggerHalfCpltCallback(htim);
6060 #else
6061 HAL_TIM_TriggerHalfCpltCallback(htim);
6062 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6066 * @brief Time Base configuration
6067 * @param TIMx TIM peripheral
6068 * @param Structure TIM Base configuration structure
6069 * @retval None
6071 void TIM_Base_SetConfig(TIM_TypeDef *TIMx, TIM_Base_InitTypeDef *Structure)
6073 uint32_t tmpcr1;
6074 tmpcr1 = TIMx->CR1;
6076 /* Set TIM Time Base Unit parameters ---------------------------------------*/
6077 if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
6079 /* Select the Counter Mode */
6080 tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
6081 tmpcr1 |= Structure->CounterMode;
6084 if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
6086 /* Set the clock division */
6087 tmpcr1 &= ~TIM_CR1_CKD;
6088 tmpcr1 |= (uint32_t)Structure->ClockDivision;
6091 /* Set the auto-reload preload */
6092 MODIFY_REG(tmpcr1, TIM_CR1_ARPE, Structure->AutoReloadPreload);
6094 TIMx->CR1 = tmpcr1;
6096 /* Set the Autoreload value */
6097 TIMx->ARR = (uint32_t)Structure->Period ;
6099 /* Set the Prescaler value */
6100 TIMx->PSC = Structure->Prescaler;
6102 if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
6104 /* Set the Repetition Counter value */
6105 TIMx->RCR = Structure->RepetitionCounter;
6108 /* Generate an update event to reload the Prescaler
6109 and the repetition counter (only for advanced timer) value immediately */
6110 TIMx->EGR = TIM_EGR_UG;
6114 * @brief Timer Output Compare 1 configuration
6115 * @param TIMx to select the TIM peripheral
6116 * @param OC_Config The ouput configuration structure
6117 * @retval None
6119 static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
6121 uint32_t tmpccmrx;
6122 uint32_t tmpccer;
6123 uint32_t tmpcr2;
6125 /* Disable the Channel 1: Reset the CC1E Bit */
6126 TIMx->CCER &= ~TIM_CCER_CC1E;
6128 /* Get the TIMx CCER register value */
6129 tmpccer = TIMx->CCER;
6130 /* Get the TIMx CR2 register value */
6131 tmpcr2 = TIMx->CR2;
6133 /* Get the TIMx CCMR1 register value */
6134 tmpccmrx = TIMx->CCMR1;
6136 /* Reset the Output Compare Mode Bits */
6137 tmpccmrx &= ~TIM_CCMR1_OC1M;
6138 tmpccmrx &= ~TIM_CCMR1_CC1S;
6139 /* Select the Output Compare Mode */
6140 tmpccmrx |= OC_Config->OCMode;
6142 /* Reset the Output Polarity level */
6143 tmpccer &= ~TIM_CCER_CC1P;
6144 /* Set the Output Compare Polarity */
6145 tmpccer |= OC_Config->OCPolarity;
6147 if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_1))
6149 /* Check parameters */
6150 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
6152 /* Reset the Output N Polarity level */
6153 tmpccer &= ~TIM_CCER_CC1NP;
6154 /* Set the Output N Polarity */
6155 tmpccer |= OC_Config->OCNPolarity;
6156 /* Reset the Output N State */
6157 tmpccer &= ~TIM_CCER_CC1NE;
6160 if (IS_TIM_BREAK_INSTANCE(TIMx))
6162 /* Check parameters */
6163 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
6164 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
6166 /* Reset the Output Compare and Output Compare N IDLE State */
6167 tmpcr2 &= ~TIM_CR2_OIS1;
6168 tmpcr2 &= ~TIM_CR2_OIS1N;
6169 /* Set the Output Idle state */
6170 tmpcr2 |= OC_Config->OCIdleState;
6171 /* Set the Output N Idle state */
6172 tmpcr2 |= OC_Config->OCNIdleState;
6175 /* Write to TIMx CR2 */
6176 TIMx->CR2 = tmpcr2;
6178 /* Write to TIMx CCMR1 */
6179 TIMx->CCMR1 = tmpccmrx;
6181 /* Set the Capture Compare Register value */
6182 TIMx->CCR1 = OC_Config->Pulse;
6184 /* Write to TIMx CCER */
6185 TIMx->CCER = tmpccer;
6189 * @brief Timer Output Compare 2 configuration
6190 * @param TIMx to select the TIM peripheral
6191 * @param OC_Config The ouput configuration structure
6192 * @retval None
6194 void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
6196 uint32_t tmpccmrx;
6197 uint32_t tmpccer;
6198 uint32_t tmpcr2;
6200 /* Disable the Channel 2: Reset the CC2E Bit */
6201 TIMx->CCER &= ~TIM_CCER_CC2E;
6203 /* Get the TIMx CCER register value */
6204 tmpccer = TIMx->CCER;
6205 /* Get the TIMx CR2 register value */
6206 tmpcr2 = TIMx->CR2;
6208 /* Get the TIMx CCMR1 register value */
6209 tmpccmrx = TIMx->CCMR1;
6211 /* Reset the Output Compare mode and Capture/Compare selection Bits */
6212 tmpccmrx &= ~TIM_CCMR1_OC2M;
6213 tmpccmrx &= ~TIM_CCMR1_CC2S;
6215 /* Select the Output Compare Mode */
6216 tmpccmrx |= (OC_Config->OCMode << 8U);
6218 /* Reset the Output Polarity level */
6219 tmpccer &= ~TIM_CCER_CC2P;
6220 /* Set the Output Compare Polarity */
6221 tmpccer |= (OC_Config->OCPolarity << 4U);
6223 if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_2))
6225 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
6227 /* Reset the Output N Polarity level */
6228 tmpccer &= ~TIM_CCER_CC2NP;
6229 /* Set the Output N Polarity */
6230 tmpccer |= (OC_Config->OCNPolarity << 4U);
6231 /* Reset the Output N State */
6232 tmpccer &= ~TIM_CCER_CC2NE;
6236 if (IS_TIM_BREAK_INSTANCE(TIMx))
6238 /* Check parameters */
6239 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
6240 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
6242 /* Reset the Output Compare and Output Compare N IDLE State */
6243 tmpcr2 &= ~TIM_CR2_OIS2;
6244 tmpcr2 &= ~TIM_CR2_OIS2N;
6245 /* Set the Output Idle state */
6246 tmpcr2 |= (OC_Config->OCIdleState << 2U);
6247 /* Set the Output N Idle state */
6248 tmpcr2 |= (OC_Config->OCNIdleState << 2U);
6251 /* Write to TIMx CR2 */
6252 TIMx->CR2 = tmpcr2;
6254 /* Write to TIMx CCMR1 */
6255 TIMx->CCMR1 = tmpccmrx;
6257 /* Set the Capture Compare Register value */
6258 TIMx->CCR2 = OC_Config->Pulse;
6260 /* Write to TIMx CCER */
6261 TIMx->CCER = tmpccer;
6265 * @brief Timer Output Compare 3 configuration
6266 * @param TIMx to select the TIM peripheral
6267 * @param OC_Config The ouput configuration structure
6268 * @retval None
6270 static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
6272 uint32_t tmpccmrx;
6273 uint32_t tmpccer;
6274 uint32_t tmpcr2;
6276 /* Disable the Channel 3: Reset the CC2E Bit */
6277 TIMx->CCER &= ~TIM_CCER_CC3E;
6279 /* Get the TIMx CCER register value */
6280 tmpccer = TIMx->CCER;
6281 /* Get the TIMx CR2 register value */
6282 tmpcr2 = TIMx->CR2;
6284 /* Get the TIMx CCMR2 register value */
6285 tmpccmrx = TIMx->CCMR2;
6287 /* Reset the Output Compare mode and Capture/Compare selection Bits */
6288 tmpccmrx &= ~TIM_CCMR2_OC3M;
6289 tmpccmrx &= ~TIM_CCMR2_CC3S;
6290 /* Select the Output Compare Mode */
6291 tmpccmrx |= OC_Config->OCMode;
6293 /* Reset the Output Polarity level */
6294 tmpccer &= ~TIM_CCER_CC3P;
6295 /* Set the Output Compare Polarity */
6296 tmpccer |= (OC_Config->OCPolarity << 8U);
6298 if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_3))
6300 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
6302 /* Reset the Output N Polarity level */
6303 tmpccer &= ~TIM_CCER_CC3NP;
6304 /* Set the Output N Polarity */
6305 tmpccer |= (OC_Config->OCNPolarity << 8U);
6306 /* Reset the Output N State */
6307 tmpccer &= ~TIM_CCER_CC3NE;
6310 if (IS_TIM_BREAK_INSTANCE(TIMx))
6312 /* Check parameters */
6313 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
6314 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
6316 /* Reset the Output Compare and Output Compare N IDLE State */
6317 tmpcr2 &= ~TIM_CR2_OIS3;
6318 tmpcr2 &= ~TIM_CR2_OIS3N;
6319 /* Set the Output Idle state */
6320 tmpcr2 |= (OC_Config->OCIdleState << 4U);
6321 /* Set the Output N Idle state */
6322 tmpcr2 |= (OC_Config->OCNIdleState << 4U);
6325 /* Write to TIMx CR2 */
6326 TIMx->CR2 = tmpcr2;
6328 /* Write to TIMx CCMR2 */
6329 TIMx->CCMR2 = tmpccmrx;
6331 /* Set the Capture Compare Register value */
6332 TIMx->CCR3 = OC_Config->Pulse;
6334 /* Write to TIMx CCER */
6335 TIMx->CCER = tmpccer;
6339 * @brief Timer Output Compare 4 configuration
6340 * @param TIMx to select the TIM peripheral
6341 * @param OC_Config The ouput configuration structure
6342 * @retval None
6344 static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
6346 uint32_t tmpccmrx;
6347 uint32_t tmpccer;
6348 uint32_t tmpcr2;
6350 /* Disable the Channel 4: Reset the CC4E Bit */
6351 TIMx->CCER &= ~TIM_CCER_CC4E;
6353 /* Get the TIMx CCER register value */
6354 tmpccer = TIMx->CCER;
6355 /* Get the TIMx CR2 register value */
6356 tmpcr2 = TIMx->CR2;
6358 /* Get the TIMx CCMR2 register value */
6359 tmpccmrx = TIMx->CCMR2;
6361 /* Reset the Output Compare mode and Capture/Compare selection Bits */
6362 tmpccmrx &= ~TIM_CCMR2_OC4M;
6363 tmpccmrx &= ~TIM_CCMR2_CC4S;
6365 /* Select the Output Compare Mode */
6366 tmpccmrx |= (OC_Config->OCMode << 8U);
6368 /* Reset the Output Polarity level */
6369 tmpccer &= ~TIM_CCER_CC4P;
6370 /* Set the Output Compare Polarity */
6371 tmpccer |= (OC_Config->OCPolarity << 12U);
6373 if (IS_TIM_BREAK_INSTANCE(TIMx))
6375 /* Check parameters */
6376 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
6378 /* Reset the Output Compare IDLE State */
6379 tmpcr2 &= ~TIM_CR2_OIS4;
6381 /* Set the Output Idle state */
6382 tmpcr2 |= (OC_Config->OCIdleState << 6U);
6385 /* Write to TIMx CR2 */
6386 TIMx->CR2 = tmpcr2;
6388 /* Write to TIMx CCMR2 */
6389 TIMx->CCMR2 = tmpccmrx;
6391 /* Set the Capture Compare Register value */
6392 TIMx->CCR4 = OC_Config->Pulse;
6394 /* Write to TIMx CCER */
6395 TIMx->CCER = tmpccer;
6399 * @brief Timer Output Compare 5 configuration
6400 * @param TIMx to select the TIM peripheral
6401 * @param OC_Config The ouput configuration structure
6402 * @retval None
6404 static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx,
6405 TIM_OC_InitTypeDef *OC_Config)
6407 uint32_t tmpccmrx;
6408 uint32_t tmpccer;
6409 uint32_t tmpcr2;
6411 /* Disable the output: Reset the CCxE Bit */
6412 TIMx->CCER &= ~TIM_CCER_CC5E;
6414 /* Get the TIMx CCER register value */
6415 tmpccer = TIMx->CCER;
6416 /* Get the TIMx CR2 register value */
6417 tmpcr2 = TIMx->CR2;
6418 /* Get the TIMx CCMR1 register value */
6419 tmpccmrx = TIMx->CCMR3;
6421 /* Reset the Output Compare Mode Bits */
6422 tmpccmrx &= ~(TIM_CCMR3_OC5M);
6423 /* Select the Output Compare Mode */
6424 tmpccmrx |= OC_Config->OCMode;
6426 /* Reset the Output Polarity level */
6427 tmpccer &= ~TIM_CCER_CC5P;
6428 /* Set the Output Compare Polarity */
6429 tmpccer |= (OC_Config->OCPolarity << 16U);
6431 if (IS_TIM_BREAK_INSTANCE(TIMx))
6433 /* Reset the Output Compare IDLE State */
6434 tmpcr2 &= ~TIM_CR2_OIS5;
6435 /* Set the Output Idle state */
6436 tmpcr2 |= (OC_Config->OCIdleState << 8U);
6438 /* Write to TIMx CR2 */
6439 TIMx->CR2 = tmpcr2;
6441 /* Write to TIMx CCMR3 */
6442 TIMx->CCMR3 = tmpccmrx;
6444 /* Set the Capture Compare Register value */
6445 TIMx->CCR5 = OC_Config->Pulse;
6447 /* Write to TIMx CCER */
6448 TIMx->CCER = tmpccer;
6452 * @brief Timer Output Compare 6 configuration
6453 * @param TIMx to select the TIM peripheral
6454 * @param OC_Config The ouput configuration structure
6455 * @retval None
6457 static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx,
6458 TIM_OC_InitTypeDef *OC_Config)
6460 uint32_t tmpccmrx;
6461 uint32_t tmpccer;
6462 uint32_t tmpcr2;
6464 /* Disable the output: Reset the CCxE Bit */
6465 TIMx->CCER &= ~TIM_CCER_CC6E;
6467 /* Get the TIMx CCER register value */
6468 tmpccer = TIMx->CCER;
6469 /* Get the TIMx CR2 register value */
6470 tmpcr2 = TIMx->CR2;
6471 /* Get the TIMx CCMR1 register value */
6472 tmpccmrx = TIMx->CCMR3;
6474 /* Reset the Output Compare Mode Bits */
6475 tmpccmrx &= ~(TIM_CCMR3_OC6M);
6476 /* Select the Output Compare Mode */
6477 tmpccmrx |= (OC_Config->OCMode << 8U);
6479 /* Reset the Output Polarity level */
6480 tmpccer &= (uint32_t)~TIM_CCER_CC6P;
6481 /* Set the Output Compare Polarity */
6482 tmpccer |= (OC_Config->OCPolarity << 20U);
6484 if (IS_TIM_BREAK_INSTANCE(TIMx))
6486 /* Reset the Output Compare IDLE State */
6487 tmpcr2 &= ~TIM_CR2_OIS6;
6488 /* Set the Output Idle state */
6489 tmpcr2 |= (OC_Config->OCIdleState << 10U);
6492 /* Write to TIMx CR2 */
6493 TIMx->CR2 = tmpcr2;
6495 /* Write to TIMx CCMR3 */
6496 TIMx->CCMR3 = tmpccmrx;
6498 /* Set the Capture Compare Register value */
6499 TIMx->CCR6 = OC_Config->Pulse;
6501 /* Write to TIMx CCER */
6502 TIMx->CCER = tmpccer;
6506 * @brief Slave Timer configuration function
6507 * @param htim TIM handle
6508 * @param sSlaveConfig Slave timer configuration
6509 * @retval None
6511 static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
6512 TIM_SlaveConfigTypeDef *sSlaveConfig)
6514 uint32_t tmpsmcr;
6515 uint32_t tmpccmr1;
6516 uint32_t tmpccer;
6518 /* Get the TIMx SMCR register value */
6519 tmpsmcr = htim->Instance->SMCR;
6521 /* Reset the Trigger Selection Bits */
6522 tmpsmcr &= ~TIM_SMCR_TS;
6523 /* Set the Input Trigger source */
6524 tmpsmcr |= sSlaveConfig->InputTrigger;
6526 /* Reset the slave mode Bits */
6527 tmpsmcr &= ~TIM_SMCR_SMS;
6528 /* Set the slave mode */
6529 tmpsmcr |= sSlaveConfig->SlaveMode;
6531 /* Write to TIMx SMCR */
6532 htim->Instance->SMCR = tmpsmcr;
6534 /* Configure the trigger prescaler, filter, and polarity */
6535 switch (sSlaveConfig->InputTrigger)
6537 case TIM_TS_ETRF:
6539 /* Check the parameters */
6540 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
6541 assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
6542 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
6543 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
6544 /* Configure the ETR Trigger source */
6545 TIM_ETR_SetConfig(htim->Instance,
6546 sSlaveConfig->TriggerPrescaler,
6547 sSlaveConfig->TriggerPolarity,
6548 sSlaveConfig->TriggerFilter);
6549 break;
6552 case TIM_TS_TI1F_ED:
6554 /* Check the parameters */
6555 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
6556 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
6558 if(sSlaveConfig->SlaveMode == TIM_SLAVEMODE_GATED)
6560 return HAL_ERROR;
6563 /* Disable the Channel 1: Reset the CC1E Bit */
6564 tmpccer = htim->Instance->CCER;
6565 htim->Instance->CCER &= ~TIM_CCER_CC1E;
6566 tmpccmr1 = htim->Instance->CCMR1;
6568 /* Set the filter */
6569 tmpccmr1 &= ~TIM_CCMR1_IC1F;
6570 tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4U);
6572 /* Write to TIMx CCMR1 and CCER registers */
6573 htim->Instance->CCMR1 = tmpccmr1;
6574 htim->Instance->CCER = tmpccer;
6575 break;
6578 case TIM_TS_TI1FP1:
6580 /* Check the parameters */
6581 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
6582 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
6583 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
6585 /* Configure TI1 Filter and Polarity */
6586 TIM_TI1_ConfigInputStage(htim->Instance,
6587 sSlaveConfig->TriggerPolarity,
6588 sSlaveConfig->TriggerFilter);
6589 break;
6592 case TIM_TS_TI2FP2:
6594 /* Check the parameters */
6595 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
6596 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
6597 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
6599 /* Configure TI2 Filter and Polarity */
6600 TIM_TI2_ConfigInputStage(htim->Instance,
6601 sSlaveConfig->TriggerPolarity,
6602 sSlaveConfig->TriggerFilter);
6603 break;
6606 case TIM_TS_ITR0:
6607 case TIM_TS_ITR1:
6608 case TIM_TS_ITR2:
6609 case TIM_TS_ITR3:
6610 case TIM_TS_ITR4:
6611 case TIM_TS_ITR5:
6612 case TIM_TS_ITR6:
6613 case TIM_TS_ITR7:
6614 case TIM_TS_ITR8:
6615 case TIM_TS_ITR9:
6616 case TIM_TS_ITR10:
6617 case TIM_TS_ITR11:
6618 case TIM_TS_ITR12:
6619 case TIM_TS_ITR13:
6621 /* Check the parameter */
6622 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
6623 break;
6626 default:
6627 break;
6629 return HAL_OK;
6633 * @brief Configure the TI1 as Input.
6634 * @param TIMx to select the TIM peripheral.
6635 * @param TIM_ICPolarity The Input Polarity.
6636 * This parameter can be one of the following values:
6637 * @arg TIM_ICPOLARITY_RISING
6638 * @arg TIM_ICPOLARITY_FALLING
6639 * @arg TIM_ICPOLARITY_BOTHEDGE
6640 * @param TIM_ICSelection specifies the input to be used.
6641 * This parameter can be one of the following values:
6642 * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 1 is selected to be connected to IC1.
6643 * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 1 is selected to be connected to IC2.
6644 * @arg TIM_ICSELECTION_TRC: TIM Input 1 is selected to be connected to TRC.
6645 * @param TIM_ICFilter Specifies the Input Capture Filter.
6646 * This parameter must be a value between 0x00 and 0x0F.
6647 * @retval None
6648 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
6649 * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
6650 * protected against un-initialized filter and polarity values.
6652 void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
6653 uint32_t TIM_ICFilter)
6655 uint32_t tmpccmr1;
6656 uint32_t tmpccer;
6658 /* Disable the Channel 1: Reset the CC1E Bit */
6659 TIMx->CCER &= ~TIM_CCER_CC1E;
6660 tmpccmr1 = TIMx->CCMR1;
6661 tmpccer = TIMx->CCER;
6663 /* Select the Input */
6664 if (IS_TIM_CC2_INSTANCE(TIMx) != RESET)
6666 tmpccmr1 &= ~TIM_CCMR1_CC1S;
6667 tmpccmr1 |= TIM_ICSelection;
6669 else
6671 tmpccmr1 |= TIM_CCMR1_CC1S_0;
6674 /* Set the filter */
6675 tmpccmr1 &= ~TIM_CCMR1_IC1F;
6676 tmpccmr1 |= ((TIM_ICFilter << 4U) & TIM_CCMR1_IC1F);
6678 /* Select the Polarity and set the CC1E Bit */
6679 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
6680 tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
6682 /* Write to TIMx CCMR1 and CCER registers */
6683 TIMx->CCMR1 = tmpccmr1;
6684 TIMx->CCER = tmpccer;
6688 * @brief Configure the Polarity and Filter for TI1.
6689 * @param TIMx to select the TIM peripheral.
6690 * @param TIM_ICPolarity The Input Polarity.
6691 * This parameter can be one of the following values:
6692 * @arg TIM_ICPOLARITY_RISING
6693 * @arg TIM_ICPOLARITY_FALLING
6694 * @arg TIM_ICPOLARITY_BOTHEDGE
6695 * @param TIM_ICFilter Specifies the Input Capture Filter.
6696 * This parameter must be a value between 0x00 and 0x0F.
6697 * @retval None
6699 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
6701 uint32_t tmpccmr1;
6702 uint32_t tmpccer;
6704 /* Disable the Channel 1: Reset the CC1E Bit */
6705 tmpccer = TIMx->CCER;
6706 TIMx->CCER &= ~TIM_CCER_CC1E;
6707 tmpccmr1 = TIMx->CCMR1;
6709 /* Set the filter */
6710 tmpccmr1 &= ~TIM_CCMR1_IC1F;
6711 tmpccmr1 |= (TIM_ICFilter << 4U);
6713 /* Select the Polarity and set the CC1E Bit */
6714 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
6715 tmpccer |= TIM_ICPolarity;
6717 /* Write to TIMx CCMR1 and CCER registers */
6718 TIMx->CCMR1 = tmpccmr1;
6719 TIMx->CCER = tmpccer;
6723 * @brief Configure the TI2 as Input.
6724 * @param TIMx to select the TIM peripheral
6725 * @param TIM_ICPolarity The Input Polarity.
6726 * This parameter can be one of the following values:
6727 * @arg TIM_ICPOLARITY_RISING
6728 * @arg TIM_ICPOLARITY_FALLING
6729 * @arg TIM_ICPOLARITY_BOTHEDGE
6730 * @param TIM_ICSelection specifies the input to be used.
6731 * This parameter can be one of the following values:
6732 * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 2 is selected to be connected to IC2.
6733 * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 2 is selected to be connected to IC1.
6734 * @arg TIM_ICSELECTION_TRC: TIM Input 2 is selected to be connected to TRC.
6735 * @param TIM_ICFilter Specifies the Input Capture Filter.
6736 * This parameter must be a value between 0x00 and 0x0F.
6737 * @retval None
6738 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
6739 * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
6740 * protected against un-initialized filter and polarity values.
6742 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
6743 uint32_t TIM_ICFilter)
6745 uint32_t tmpccmr1;
6746 uint32_t tmpccer;
6748 /* Disable the Channel 2: Reset the CC2E Bit */
6749 TIMx->CCER &= ~TIM_CCER_CC2E;
6750 tmpccmr1 = TIMx->CCMR1;
6751 tmpccer = TIMx->CCER;
6753 /* Select the Input */
6754 tmpccmr1 &= ~TIM_CCMR1_CC2S;
6755 tmpccmr1 |= (TIM_ICSelection << 8U);
6757 /* Set the filter */
6758 tmpccmr1 &= ~TIM_CCMR1_IC2F;
6759 tmpccmr1 |= ((TIM_ICFilter << 12U) & TIM_CCMR1_IC2F);
6761 /* Select the Polarity and set the CC2E Bit */
6762 tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
6763 tmpccer |= ((TIM_ICPolarity << 4U) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
6765 /* Write to TIMx CCMR1 and CCER registers */
6766 TIMx->CCMR1 = tmpccmr1 ;
6767 TIMx->CCER = tmpccer;
6771 * @brief Configure the Polarity and Filter for TI2.
6772 * @param TIMx to select the TIM peripheral.
6773 * @param TIM_ICPolarity The Input Polarity.
6774 * This parameter can be one of the following values:
6775 * @arg TIM_ICPOLARITY_RISING
6776 * @arg TIM_ICPOLARITY_FALLING
6777 * @arg TIM_ICPOLARITY_BOTHEDGE
6778 * @param TIM_ICFilter Specifies the Input Capture Filter.
6779 * This parameter must be a value between 0x00 and 0x0F.
6780 * @retval None
6782 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
6784 uint32_t tmpccmr1;
6785 uint32_t tmpccer;
6787 /* Disable the Channel 2: Reset the CC2E Bit */
6788 TIMx->CCER &= ~TIM_CCER_CC2E;
6789 tmpccmr1 = TIMx->CCMR1;
6790 tmpccer = TIMx->CCER;
6792 /* Set the filter */
6793 tmpccmr1 &= ~TIM_CCMR1_IC2F;
6794 tmpccmr1 |= (TIM_ICFilter << 12U);
6796 /* Select the Polarity and set the CC2E Bit */
6797 tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
6798 tmpccer |= (TIM_ICPolarity << 4U);
6800 /* Write to TIMx CCMR1 and CCER registers */
6801 TIMx->CCMR1 = tmpccmr1 ;
6802 TIMx->CCER = tmpccer;
6806 * @brief Configure the TI3 as Input.
6807 * @param TIMx to select the TIM peripheral
6808 * @param TIM_ICPolarity The Input Polarity.
6809 * This parameter can be one of the following values:
6810 * @arg TIM_ICPOLARITY_RISING
6811 * @arg TIM_ICPOLARITY_FALLING
6812 * @arg TIM_ICPOLARITY_BOTHEDGE
6813 * @param TIM_ICSelection specifies the input to be used.
6814 * This parameter can be one of the following values:
6815 * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 3 is selected to be connected to IC3.
6816 * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 3 is selected to be connected to IC4.
6817 * @arg TIM_ICSELECTION_TRC: TIM Input 3 is selected to be connected to TRC.
6818 * @param TIM_ICFilter Specifies the Input Capture Filter.
6819 * This parameter must be a value between 0x00 and 0x0F.
6820 * @retval None
6821 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
6822 * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
6823 * protected against un-initialized filter and polarity values.
6825 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
6826 uint32_t TIM_ICFilter)
6828 uint32_t tmpccmr2;
6829 uint32_t tmpccer;
6831 /* Disable the Channel 3: Reset the CC3E Bit */
6832 TIMx->CCER &= ~TIM_CCER_CC3E;
6833 tmpccmr2 = TIMx->CCMR2;
6834 tmpccer = TIMx->CCER;
6836 /* Select the Input */
6837 tmpccmr2 &= ~TIM_CCMR2_CC3S;
6838 tmpccmr2 |= TIM_ICSelection;
6840 /* Set the filter */
6841 tmpccmr2 &= ~TIM_CCMR2_IC3F;
6842 tmpccmr2 |= ((TIM_ICFilter << 4U) & TIM_CCMR2_IC3F);
6844 /* Select the Polarity and set the CC3E Bit */
6845 tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
6846 tmpccer |= ((TIM_ICPolarity << 8U) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
6848 /* Write to TIMx CCMR2 and CCER registers */
6849 TIMx->CCMR2 = tmpccmr2;
6850 TIMx->CCER = tmpccer;
6854 * @brief Configure the TI4 as Input.
6855 * @param TIMx to select the TIM peripheral
6856 * @param TIM_ICPolarity The Input Polarity.
6857 * This parameter can be one of the following values:
6858 * @arg TIM_ICPOLARITY_RISING
6859 * @arg TIM_ICPOLARITY_FALLING
6860 * @arg TIM_ICPOLARITY_BOTHEDGE
6861 * @param TIM_ICSelection specifies the input to be used.
6862 * This parameter can be one of the following values:
6863 * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 4 is selected to be connected to IC4.
6864 * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 4 is selected to be connected to IC3.
6865 * @arg TIM_ICSELECTION_TRC: TIM Input 4 is selected to be connected to TRC.
6866 * @param TIM_ICFilter Specifies the Input Capture Filter.
6867 * This parameter must be a value between 0x00 and 0x0F.
6868 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
6869 * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
6870 * protected against un-initialized filter and polarity values.
6871 * @retval None
6873 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
6874 uint32_t TIM_ICFilter)
6876 uint32_t tmpccmr2;
6877 uint32_t tmpccer;
6879 /* Disable the Channel 4: Reset the CC4E Bit */
6880 TIMx->CCER &= ~TIM_CCER_CC4E;
6881 tmpccmr2 = TIMx->CCMR2;
6882 tmpccer = TIMx->CCER;
6884 /* Select the Input */
6885 tmpccmr2 &= ~TIM_CCMR2_CC4S;
6886 tmpccmr2 |= (TIM_ICSelection << 8U);
6888 /* Set the filter */
6889 tmpccmr2 &= ~TIM_CCMR2_IC4F;
6890 tmpccmr2 |= ((TIM_ICFilter << 12U) & TIM_CCMR2_IC4F);
6892 /* Select the Polarity and set the CC4E Bit */
6893 tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
6894 tmpccer |= ((TIM_ICPolarity << 12U) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
6896 /* Write to TIMx CCMR2 and CCER registers */
6897 TIMx->CCMR2 = tmpccmr2;
6898 TIMx->CCER = tmpccer ;
6902 * @brief Selects the Input Trigger source
6903 * @param TIMx to select the TIM peripheral
6904 * @param InputTriggerSource The Input Trigger source.
6905 * This parameter can be one of the following values:
6906 * @arg TIM_TS_ITR0: Internal Trigger 0
6907 * @arg TIM_TS_ITR1: Internal Trigger 1
6908 * @arg TIM_TS_ITR2: Internal Trigger 2
6909 * @arg TIM_TS_ITR3: Internal Trigger 3
6910 * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
6911 * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
6912 * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
6913 * @arg TIM_TS_ETRF: External Trigger input
6914 * @arg TIM_TS_ITR4: Internal Trigger 4 (*)
6915 * @arg TIM_TS_ITR5: Internal Trigger 5
6916 * @arg TIM_TS_ITR6: Internal Trigger 6
6917 * @arg TIM_TS_ITR7: Internal Trigger 7
6918 * @arg TIM_TS_ITR8: Internal Trigger 8 (*)
6919 * @arg TIM_TS_ITR9: Internal Trigger 9 (*)
6920 * @arg TIM_TS_ITR10: Internal Trigger 10 (*)
6921 * @arg TIM_TS_ITR11: Internal Trigger 11 (*)
6922 * @arg TIM_TS_ITR12: Internal Trigger 12 (*)
6923 * @arg TIM_TS_ITR13: Internal Trigger 13 (*)
6925 * (*) Value not defined in all devices.
6927 * @retval None
6929 static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource)
6931 uint32_t tmpsmcr;
6933 /* Get the TIMx SMCR register value */
6934 tmpsmcr = TIMx->SMCR;
6935 /* Reset the TS Bits */
6936 tmpsmcr &= ~TIM_SMCR_TS;
6937 /* Set the Input Trigger source and the slave mode*/
6938 tmpsmcr |= (InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1);
6939 /* Write to TIMx SMCR */
6940 TIMx->SMCR = tmpsmcr;
6943 * @brief Configures the TIMx External Trigger (ETR).
6944 * @param TIMx to select the TIM peripheral
6945 * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
6946 * This parameter can be one of the following values:
6947 * @arg TIM_ETRPRESCALER_DIV1: ETRP Prescaler OFF.
6948 * @arg TIM_ETRPRESCALER_DIV2: ETRP frequency divided by 2.
6949 * @arg TIM_ETRPRESCALER_DIV4: ETRP frequency divided by 4.
6950 * @arg TIM_ETRPRESCALER_DIV8: ETRP frequency divided by 8.
6951 * @param TIM_ExtTRGPolarity The external Trigger Polarity.
6952 * This parameter can be one of the following values:
6953 * @arg TIM_ETRPOLARITY_INVERTED: active low or falling edge active.
6954 * @arg TIM_ETRPOLARITY_NONINVERTED: active high or rising edge active.
6955 * @param ExtTRGFilter External Trigger Filter.
6956 * This parameter must be a value between 0x00 and 0x0F
6957 * @retval None
6959 void TIM_ETR_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler,
6960 uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
6962 uint32_t tmpsmcr;
6964 tmpsmcr = TIMx->SMCR;
6966 /* Reset the ETR Bits */
6967 tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
6969 /* Set the Prescaler, the Filter value and the Polarity */
6970 tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8U)));
6972 /* Write to TIMx SMCR */
6973 TIMx->SMCR = tmpsmcr;
6977 * @brief Enables or disables the TIM Capture Compare Channel x.
6978 * @param TIMx to select the TIM peripheral
6979 * @param Channel specifies the TIM Channel
6980 * This parameter can be one of the following values:
6981 * @arg TIM_CHANNEL_1: TIM Channel 1
6982 * @arg TIM_CHANNEL_2: TIM Channel 2
6983 * @arg TIM_CHANNEL_3: TIM Channel 3
6984 * @arg TIM_CHANNEL_4: TIM Channel 4
6985 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
6986 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
6987 * @param ChannelState specifies the TIM Channel CCxE bit new state.
6988 * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
6989 * @retval None
6991 void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
6993 uint32_t tmp;
6995 /* Check the parameters */
6996 assert_param(IS_TIM_CC1_INSTANCE(TIMx));
6997 assert_param(IS_TIM_CHANNELS(Channel));
6999 tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
7001 /* Reset the CCxE Bit */
7002 TIMx->CCER &= ~tmp;
7004 /* Set or reset the CCxE Bit */
7005 TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
7008 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
7010 * @brief Reset interrupt callbacks to the legacy weak callbacks.
7011 * @param htim pointer to a TIM_HandleTypeDef structure that contains
7012 * the configuration information for TIM module.
7013 * @retval None
7015 void TIM_ResetCallback(TIM_HandleTypeDef *htim)
7017 /* Reset the TIM callback to the legacy weak callbacks */
7018 htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback; /* Legacy weak PeriodElapsedCallback */
7019 htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback; /* Legacy weak PeriodElapsedHalfCpltCallback */
7020 htim->TriggerCallback = HAL_TIM_TriggerCallback; /* Legacy weak TriggerCallback */
7021 htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback; /* Legacy weak TriggerHalfCpltCallback */
7022 htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback; /* Legacy weak IC_CaptureCallback */
7023 htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback; /* Legacy weak IC_CaptureHalfCpltCallback */
7024 htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback; /* Legacy weak OC_DelayElapsedCallback */
7025 htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback; /* Legacy weak PWM_PulseFinishedCallback */
7026 htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback; /* Legacy weak PWM_PulseFinishedHalfCpltCallback */
7027 htim->ErrorCallback = HAL_TIM_ErrorCallback; /* Legacy weak ErrorCallback */
7028 htim->CommutationCallback = HAL_TIMEx_CommutCallback; /* Legacy weak CommutationCallback */
7029 htim->CommutationHalfCpltCallback = HAL_TIMEx_CommutHalfCpltCallback; /* Legacy weak CommutationHalfCpltCallback */
7030 htim->BreakCallback = HAL_TIMEx_BreakCallback; /* Legacy weak BreakCallback */
7031 htim->Break2Callback = HAL_TIMEx_Break2Callback; /* Legacy weak Break2Callback */
7033 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
7036 * @}
7039 #endif /* HAL_TIM_MODULE_ENABLED */
7041 * @}
7045 * @}
7047 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/