Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_tim.c
blobea6cedbe4a5586b7217ebfce12ef0c8ecd58f164
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 macros ------------------------------------------------------------*/
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 void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma);
225 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);
226 static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma);
227 static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
228 TIM_SlaveConfigTypeDef *sSlaveConfig);
230 * @}
232 /* Exported functions --------------------------------------------------------*/
234 /** @defgroup TIM_Exported_Functions TIM Exported Functions
235 * @{
238 /** @defgroup TIM_Exported_Functions_Group1 TIM Time Base functions
239 * @brief Time Base functions
241 @verbatim
242 ==============================================================================
243 ##### Time Base functions #####
244 ==============================================================================
245 [..]
246 This section provides functions allowing to:
247 (+) Initialize and configure the TIM base.
248 (+) De-initialize the TIM base.
249 (+) Start the Time Base.
250 (+) Stop the Time Base.
251 (+) Start the Time Base and enable interrupt.
252 (+) Stop the Time Base and disable interrupt.
253 (+) Start the Time Base and enable DMA transfer.
254 (+) Stop the Time Base and disable DMA transfer.
256 @endverbatim
257 * @{
260 * @brief Initializes the TIM Time base Unit according to the specified
261 * parameters in the TIM_HandleTypeDef and initialize the associated handle.
262 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
263 * requires a timer reset to avoid unexpected direction
264 * due to DIR bit readonly in center aligned mode.
265 * Ex: call @ref HAL_TIM_Base_DeInit() before HAL_TIM_Base_Init()
266 * @param htim TIM Base handle
267 * @retval HAL status
269 HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
271 /* Check the TIM handle allocation */
272 if (htim == NULL)
274 return HAL_ERROR;
277 /* Check the parameters */
278 assert_param(IS_TIM_INSTANCE(htim->Instance));
279 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
280 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
281 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
283 if (htim->State == HAL_TIM_STATE_RESET)
285 /* Allocate lock resource and initialize it */
286 htim->Lock = HAL_UNLOCKED;
288 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
289 /* Reset interrupt callbacks to legacy weak callbacks */
290 TIM_ResetCallback(htim);
292 if (htim->Base_MspInitCallback == NULL)
294 htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
296 /* Init the low level hardware : GPIO, CLOCK, NVIC */
297 htim->Base_MspInitCallback(htim);
298 #else
299 /* Init the low level hardware : GPIO, CLOCK, NVIC */
300 HAL_TIM_Base_MspInit(htim);
301 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
304 /* Set the TIM state */
305 htim->State = HAL_TIM_STATE_BUSY;
307 /* Set the Time Base configuration */
308 TIM_Base_SetConfig(htim->Instance, &htim->Init);
310 /* Initialize the DMA burst operation state */
311 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
313 /* Initialize the TIM channels state */
314 TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
315 TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
317 /* Initialize the TIM state*/
318 htim->State = HAL_TIM_STATE_READY;
320 return HAL_OK;
324 * @brief DeInitializes the TIM Base peripheral
325 * @param htim TIM Base handle
326 * @retval HAL status
328 HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)
330 /* Check the parameters */
331 assert_param(IS_TIM_INSTANCE(htim->Instance));
333 htim->State = HAL_TIM_STATE_BUSY;
335 /* Disable the TIM Peripheral Clock */
336 __HAL_TIM_DISABLE(htim);
338 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
339 if (htim->Base_MspDeInitCallback == NULL)
341 htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
343 /* DeInit the low level hardware */
344 htim->Base_MspDeInitCallback(htim);
345 #else
346 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
347 HAL_TIM_Base_MspDeInit(htim);
348 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
350 /* Change the DMA burst operation state */
351 htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
353 /* Change the TIM channels state */
354 TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
355 TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
357 /* Change TIM state */
358 htim->State = HAL_TIM_STATE_RESET;
360 /* Release Lock */
361 __HAL_UNLOCK(htim);
363 return HAL_OK;
367 * @brief Initializes the TIM Base MSP.
368 * @param htim TIM Base handle
369 * @retval None
371 __weak void HAL_TIM_Base_MspInit(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_MspInit could be implemented in the user file
382 * @brief DeInitializes TIM Base MSP.
383 * @param htim TIM Base handle
384 * @retval None
386 __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
388 /* Prevent unused argument(s) compilation warning */
389 UNUSED(htim);
391 /* NOTE : This function should not be modified, when the callback is needed,
392 the HAL_TIM_Base_MspDeInit could be implemented in the user file
398 * @brief Starts the TIM Base generation.
399 * @param htim TIM Base handle
400 * @retval HAL status
402 HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)
404 uint32_t tmpsmcr;
406 /* Check the parameters */
407 assert_param(IS_TIM_INSTANCE(htim->Instance));
409 /* Check the TIM state */
410 if (htim->State != HAL_TIM_STATE_READY)
412 return HAL_ERROR;
415 /* Set the TIM state */
416 htim->State = HAL_TIM_STATE_BUSY;
418 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
419 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
420 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
422 __HAL_TIM_ENABLE(htim);
425 /* Return function status */
426 return HAL_OK;
430 * @brief Stops the TIM Base generation.
431 * @param htim TIM Base handle
432 * @retval HAL status
434 HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)
436 /* Check the parameters */
437 assert_param(IS_TIM_INSTANCE(htim->Instance));
439 /* Disable the Peripheral */
440 __HAL_TIM_DISABLE(htim);
442 /* Set the TIM state */
443 htim->State = HAL_TIM_STATE_READY;
445 /* Return function status */
446 return HAL_OK;
450 * @brief Starts the TIM Base generation in interrupt mode.
451 * @param htim TIM Base handle
452 * @retval HAL status
454 HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
456 uint32_t tmpsmcr;
458 /* Check the parameters */
459 assert_param(IS_TIM_INSTANCE(htim->Instance));
461 /* Check the TIM state */
462 if (htim->State != HAL_TIM_STATE_READY)
464 return HAL_ERROR;
467 /* Set the TIM state */
468 htim->State = HAL_TIM_STATE_BUSY;
470 /* Enable the TIM Update interrupt */
471 __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
473 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
474 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
475 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
477 __HAL_TIM_ENABLE(htim);
480 /* Return function status */
481 return HAL_OK;
485 * @brief Stops the TIM Base generation in interrupt mode.
486 * @param htim TIM Base handle
487 * @retval HAL status
489 HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)
491 /* Check the parameters */
492 assert_param(IS_TIM_INSTANCE(htim->Instance));
494 /* Disable the TIM Update interrupt */
495 __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);
497 /* Disable the Peripheral */
498 __HAL_TIM_DISABLE(htim);
500 /* Set the TIM state */
501 htim->State = HAL_TIM_STATE_READY;
503 /* Return function status */
504 return HAL_OK;
508 * @brief Starts the TIM Base generation in DMA mode.
509 * @param htim TIM Base handle
510 * @param pData The source Buffer address.
511 * @param Length The length of data to be transferred from memory to peripheral.
512 * @retval HAL status
514 HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
516 uint32_t tmpsmcr;
518 /* Check the parameters */
519 assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
521 /* Set the TIM state */
522 if (htim->State == HAL_TIM_STATE_BUSY)
524 return HAL_BUSY;
526 else if (htim->State == HAL_TIM_STATE_READY)
528 if ((pData == NULL) && (Length > 0U))
530 return HAL_ERROR;
532 else
534 htim->State = HAL_TIM_STATE_BUSY;
537 else
539 return HAL_ERROR;
542 /* Set the DMA Period elapsed callbacks */
543 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
544 htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
546 /* Set the DMA error callback */
547 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
549 /* Enable the DMA stream */
550 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR, Length) != HAL_OK)
552 return HAL_ERROR;
555 /* Enable the TIM Update DMA request */
556 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);
558 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
559 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
560 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
562 __HAL_TIM_ENABLE(htim);
565 /* Return function status */
566 return HAL_OK;
570 * @brief Stops the TIM Base generation in DMA mode.
571 * @param htim TIM Base handle
572 * @retval HAL status
574 HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)
576 /* Check the parameters */
577 assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
579 /* Disable the TIM Update DMA request */
580 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);
582 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
584 /* Disable the Peripheral */
585 __HAL_TIM_DISABLE(htim);
587 /* Set the TIM state */
588 htim->State = HAL_TIM_STATE_READY;
590 /* Return function status */
591 return HAL_OK;
595 * @}
598 /** @defgroup TIM_Exported_Functions_Group2 TIM Output Compare functions
599 * @brief TIM Output Compare functions
601 @verbatim
602 ==============================================================================
603 ##### TIM Output Compare functions #####
604 ==============================================================================
605 [..]
606 This section provides functions allowing to:
607 (+) Initialize and configure the TIM Output Compare.
608 (+) De-initialize the TIM Output Compare.
609 (+) Start the TIM Output Compare.
610 (+) Stop the TIM Output Compare.
611 (+) Start the TIM Output Compare and enable interrupt.
612 (+) Stop the TIM Output Compare and disable interrupt.
613 (+) Start the TIM Output Compare and enable DMA transfer.
614 (+) Stop the TIM Output Compare and disable DMA transfer.
616 @endverbatim
617 * @{
620 * @brief Initializes the TIM Output Compare according to the specified
621 * parameters in the TIM_HandleTypeDef and initializes the associated handle.
622 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
623 * requires a timer reset to avoid unexpected direction
624 * due to DIR bit readonly in center aligned mode.
625 * Ex: call @ref HAL_TIM_OC_DeInit() before HAL_TIM_OC_Init()
626 * @param htim TIM Output Compare handle
627 * @retval HAL status
629 HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim)
631 /* Check the TIM handle allocation */
632 if (htim == NULL)
634 return HAL_ERROR;
637 /* Check the parameters */
638 assert_param(IS_TIM_INSTANCE(htim->Instance));
639 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
640 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
641 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
643 if (htim->State == HAL_TIM_STATE_RESET)
645 /* Allocate lock resource and initialize it */
646 htim->Lock = HAL_UNLOCKED;
648 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
649 /* Reset interrupt callbacks to legacy weak callbacks */
650 TIM_ResetCallback(htim);
652 if (htim->OC_MspInitCallback == NULL)
654 htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
656 /* Init the low level hardware : GPIO, CLOCK, NVIC */
657 htim->OC_MspInitCallback(htim);
658 #else
659 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
660 HAL_TIM_OC_MspInit(htim);
661 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
664 /* Set the TIM state */
665 htim->State = HAL_TIM_STATE_BUSY;
667 /* Init the base time for the Output Compare */
668 TIM_Base_SetConfig(htim->Instance, &htim->Init);
670 /* Initialize the DMA burst operation state */
671 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
673 /* Initialize the TIM channels state */
674 TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
675 TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
677 /* Initialize the TIM state*/
678 htim->State = HAL_TIM_STATE_READY;
680 return HAL_OK;
684 * @brief DeInitializes the TIM peripheral
685 * @param htim TIM Output Compare handle
686 * @retval HAL status
688 HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)
690 /* Check the parameters */
691 assert_param(IS_TIM_INSTANCE(htim->Instance));
693 htim->State = HAL_TIM_STATE_BUSY;
695 /* Disable the TIM Peripheral Clock */
696 __HAL_TIM_DISABLE(htim);
698 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
699 if (htim->OC_MspDeInitCallback == NULL)
701 htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
703 /* DeInit the low level hardware */
704 htim->OC_MspDeInitCallback(htim);
705 #else
706 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
707 HAL_TIM_OC_MspDeInit(htim);
708 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
710 /* Change the DMA burst operation state */
711 htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
713 /* Change the TIM channels state */
714 TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
715 TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
717 /* Change TIM state */
718 htim->State = HAL_TIM_STATE_RESET;
720 /* Release Lock */
721 __HAL_UNLOCK(htim);
723 return HAL_OK;
727 * @brief Initializes the TIM Output Compare MSP.
728 * @param htim TIM Output Compare handle
729 * @retval None
731 __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
733 /* Prevent unused argument(s) compilation warning */
734 UNUSED(htim);
736 /* NOTE : This function should not be modified, when the callback is needed,
737 the HAL_TIM_OC_MspInit could be implemented in the user file
742 * @brief DeInitializes TIM Output Compare MSP.
743 * @param htim TIM Output Compare handle
744 * @retval None
746 __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
748 /* Prevent unused argument(s) compilation warning */
749 UNUSED(htim);
751 /* NOTE : This function should not be modified, when the callback is needed,
752 the HAL_TIM_OC_MspDeInit could be implemented in the user file
757 * @brief Starts the TIM Output Compare signal generation.
758 * @param htim TIM Output Compare handle
759 * @param Channel TIM Channel to be enabled
760 * This parameter can be one of the following values:
761 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
762 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
763 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
764 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
765 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
766 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
767 * @retval HAL status
769 HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
771 uint32_t tmpsmcr;
773 /* Check the parameters */
774 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
776 /* Check the TIM channel state */
777 if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
779 return HAL_ERROR;
782 /* Set the TIM channel state */
783 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
785 /* Enable the Output compare channel */
786 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
788 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
790 /* Enable the main output */
791 __HAL_TIM_MOE_ENABLE(htim);
794 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
795 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
796 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
798 __HAL_TIM_ENABLE(htim);
801 /* Return function status */
802 return HAL_OK;
806 * @brief Stops the TIM Output Compare signal generation.
807 * @param htim TIM Output Compare handle
808 * @param Channel TIM Channel to be disabled
809 * This parameter can be one of the following values:
810 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
811 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
812 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
813 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
814 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
815 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
816 * @retval HAL status
818 HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
820 /* Check the parameters */
821 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
823 /* Disable the Output compare channel */
824 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
826 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
828 /* Disable the Main Output */
829 __HAL_TIM_MOE_DISABLE(htim);
832 /* Disable the Peripheral */
833 __HAL_TIM_DISABLE(htim);
835 /* Set the TIM channel state */
836 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
838 /* Return function status */
839 return HAL_OK;
843 * @brief Starts the TIM Output Compare signal generation in interrupt mode.
844 * @param htim TIM Output Compare handle
845 * @param Channel TIM Channel to be enabled
846 * This parameter can be one of the following values:
847 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
848 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
849 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
850 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
851 * @retval HAL status
853 HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
855 uint32_t tmpsmcr;
857 /* Check the parameters */
858 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
860 /* Check the TIM channel state */
861 if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
863 return HAL_ERROR;
866 /* Set the TIM channel state */
867 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
869 switch (Channel)
871 case TIM_CHANNEL_1:
873 /* Enable the TIM Capture/Compare 1 interrupt */
874 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
875 break;
878 case TIM_CHANNEL_2:
880 /* Enable the TIM Capture/Compare 2 interrupt */
881 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
882 break;
885 case TIM_CHANNEL_3:
887 /* Enable the TIM Capture/Compare 3 interrupt */
888 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
889 break;
892 case TIM_CHANNEL_4:
894 /* Enable the TIM Capture/Compare 4 interrupt */
895 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
896 break;
899 default:
900 break;
903 /* Enable the Output compare channel */
904 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
906 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
908 /* Enable the main output */
909 __HAL_TIM_MOE_ENABLE(htim);
912 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
913 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
914 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
916 __HAL_TIM_ENABLE(htim);
919 /* Return function status */
920 return HAL_OK;
924 * @brief Stops the TIM Output Compare signal generation in interrupt mode.
925 * @param htim TIM Output Compare handle
926 * @param Channel TIM Channel to be disabled
927 * This parameter can be one of the following values:
928 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
929 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
930 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
931 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
932 * @retval HAL status
934 HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
936 /* Check the parameters */
937 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
939 switch (Channel)
941 case TIM_CHANNEL_1:
943 /* Disable the TIM Capture/Compare 1 interrupt */
944 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
945 break;
948 case TIM_CHANNEL_2:
950 /* Disable the TIM Capture/Compare 2 interrupt */
951 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
952 break;
955 case TIM_CHANNEL_3:
957 /* Disable the TIM Capture/Compare 3 interrupt */
958 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
959 break;
962 case TIM_CHANNEL_4:
964 /* Disable the TIM Capture/Compare 4 interrupt */
965 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
966 break;
969 default:
970 break;
973 /* Disable the Output compare channel */
974 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
976 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
978 /* Disable the Main Output */
979 __HAL_TIM_MOE_DISABLE(htim);
982 /* Disable the Peripheral */
983 __HAL_TIM_DISABLE(htim);
985 /* Set the TIM channel state */
986 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
988 /* Return function status */
989 return HAL_OK;
993 * @brief Starts the TIM Output Compare signal generation in DMA mode.
994 * @param htim TIM Output Compare handle
995 * @param Channel TIM Channel to be enabled
996 * This parameter can be one of the following values:
997 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
998 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
999 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1000 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1001 * @param pData The source Buffer address.
1002 * @param Length The length of data to be transferred from memory to TIM peripheral
1003 * @retval HAL status
1005 HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
1007 uint32_t tmpsmcr;
1009 /* Check the parameters */
1010 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1012 /* Set the TIM channel state */
1013 if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
1015 return HAL_BUSY;
1017 else if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
1019 if ((pData == NULL) && (Length > 0U))
1021 return HAL_ERROR;
1023 else
1025 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1028 else
1030 return HAL_ERROR;
1033 switch (Channel)
1035 case TIM_CHANNEL_1:
1037 /* Set the DMA compare callbacks */
1038 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
1039 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1041 /* Set the DMA error callback */
1042 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
1044 /* Enable the DMA stream */
1045 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
1047 return HAL_ERROR;
1050 /* Enable the TIM Capture/Compare 1 DMA request */
1051 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1052 break;
1055 case TIM_CHANNEL_2:
1057 /* Set the DMA compare callbacks */
1058 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
1059 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1061 /* Set the DMA error callback */
1062 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
1064 /* Enable the DMA stream */
1065 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
1067 return HAL_ERROR;
1070 /* Enable the TIM Capture/Compare 2 DMA request */
1071 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1072 break;
1075 case TIM_CHANNEL_3:
1077 /* Set the DMA compare callbacks */
1078 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
1079 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1081 /* Set the DMA error callback */
1082 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
1084 /* Enable the DMA stream */
1085 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
1087 return HAL_ERROR;
1089 /* Enable the TIM Capture/Compare 3 DMA request */
1090 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1091 break;
1094 case TIM_CHANNEL_4:
1096 /* Set the DMA compare callbacks */
1097 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
1098 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1100 /* Set the DMA error callback */
1101 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
1103 /* Enable the DMA stream */
1104 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length) != HAL_OK)
1106 return HAL_ERROR;
1108 /* Enable the TIM Capture/Compare 4 DMA request */
1109 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1110 break;
1113 default:
1114 break;
1117 /* Enable the Output compare channel */
1118 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1120 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1122 /* Enable the main output */
1123 __HAL_TIM_MOE_ENABLE(htim);
1126 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1127 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1128 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1130 __HAL_TIM_ENABLE(htim);
1133 /* Return function status */
1134 return HAL_OK;
1138 * @brief Stops the TIM Output Compare signal generation in DMA mode.
1139 * @param htim TIM Output Compare handle
1140 * @param Channel TIM Channel to be disabled
1141 * This parameter can be one of the following values:
1142 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1143 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1144 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1145 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1146 * @retval HAL status
1148 HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1150 /* Check the parameters */
1151 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1153 switch (Channel)
1155 case TIM_CHANNEL_1:
1157 /* Disable the TIM Capture/Compare 1 DMA request */
1158 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1159 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1160 break;
1163 case TIM_CHANNEL_2:
1165 /* Disable the TIM Capture/Compare 2 DMA request */
1166 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1167 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1168 break;
1171 case TIM_CHANNEL_3:
1173 /* Disable the TIM Capture/Compare 3 DMA request */
1174 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1175 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1176 break;
1179 case TIM_CHANNEL_4:
1181 /* Disable the TIM Capture/Compare 4 interrupt */
1182 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1183 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
1184 break;
1187 default:
1188 break;
1191 /* Disable the Output compare channel */
1192 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1194 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1196 /* Disable the Main Output */
1197 __HAL_TIM_MOE_DISABLE(htim);
1200 /* Disable the Peripheral */
1201 __HAL_TIM_DISABLE(htim);
1203 /* Set the TIM channel state */
1204 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1206 /* Return function status */
1207 return HAL_OK;
1211 * @}
1214 /** @defgroup TIM_Exported_Functions_Group3 TIM PWM functions
1215 * @brief TIM PWM functions
1217 @verbatim
1218 ==============================================================================
1219 ##### TIM PWM functions #####
1220 ==============================================================================
1221 [..]
1222 This section provides functions allowing to:
1223 (+) Initialize and configure the TIM PWM.
1224 (+) De-initialize the TIM PWM.
1225 (+) Start the TIM PWM.
1226 (+) Stop the TIM PWM.
1227 (+) Start the TIM PWM and enable interrupt.
1228 (+) Stop the TIM PWM and disable interrupt.
1229 (+) Start the TIM PWM and enable DMA transfer.
1230 (+) Stop the TIM PWM and disable DMA transfer.
1232 @endverbatim
1233 * @{
1236 * @brief Initializes the TIM PWM Time Base according to the specified
1237 * parameters in the TIM_HandleTypeDef and initializes the associated handle.
1238 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
1239 * requires a timer reset to avoid unexpected direction
1240 * due to DIR bit readonly in center aligned mode.
1241 * Ex: call @ref HAL_TIM_PWM_DeInit() before HAL_TIM_PWM_Init()
1242 * @param htim TIM PWM handle
1243 * @retval HAL status
1245 HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)
1247 /* Check the TIM handle allocation */
1248 if (htim == NULL)
1250 return HAL_ERROR;
1253 /* Check the parameters */
1254 assert_param(IS_TIM_INSTANCE(htim->Instance));
1255 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
1256 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
1257 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
1259 if (htim->State == HAL_TIM_STATE_RESET)
1261 /* Allocate lock resource and initialize it */
1262 htim->Lock = HAL_UNLOCKED;
1264 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1265 /* Reset interrupt callbacks to legacy weak callbacks */
1266 TIM_ResetCallback(htim);
1268 if (htim->PWM_MspInitCallback == NULL)
1270 htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
1272 /* Init the low level hardware : GPIO, CLOCK, NVIC */
1273 htim->PWM_MspInitCallback(htim);
1274 #else
1275 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1276 HAL_TIM_PWM_MspInit(htim);
1277 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1280 /* Set the TIM state */
1281 htim->State = HAL_TIM_STATE_BUSY;
1283 /* Init the base time for the PWM */
1284 TIM_Base_SetConfig(htim->Instance, &htim->Init);
1286 /* Initialize the DMA burst operation state */
1287 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
1289 /* Initialize the TIM channels state */
1290 TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
1291 TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
1293 /* Initialize the TIM state*/
1294 htim->State = HAL_TIM_STATE_READY;
1296 return HAL_OK;
1300 * @brief DeInitializes the TIM peripheral
1301 * @param htim TIM PWM handle
1302 * @retval HAL status
1304 HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)
1306 /* Check the parameters */
1307 assert_param(IS_TIM_INSTANCE(htim->Instance));
1309 htim->State = HAL_TIM_STATE_BUSY;
1311 /* Disable the TIM Peripheral Clock */
1312 __HAL_TIM_DISABLE(htim);
1314 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1315 if (htim->PWM_MspDeInitCallback == NULL)
1317 htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
1319 /* DeInit the low level hardware */
1320 htim->PWM_MspDeInitCallback(htim);
1321 #else
1322 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1323 HAL_TIM_PWM_MspDeInit(htim);
1324 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1326 /* Change the DMA burst operation state */
1327 htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
1329 /* Change the TIM channels state */
1330 TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
1331 TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
1333 /* Change TIM state */
1334 htim->State = HAL_TIM_STATE_RESET;
1336 /* Release Lock */
1337 __HAL_UNLOCK(htim);
1339 return HAL_OK;
1343 * @brief Initializes the TIM PWM MSP.
1344 * @param htim TIM PWM handle
1345 * @retval None
1347 __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
1349 /* Prevent unused argument(s) compilation warning */
1350 UNUSED(htim);
1352 /* NOTE : This function should not be modified, when the callback is needed,
1353 the HAL_TIM_PWM_MspInit could be implemented in the user file
1358 * @brief DeInitializes TIM PWM MSP.
1359 * @param htim TIM PWM handle
1360 * @retval None
1362 __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)
1364 /* Prevent unused argument(s) compilation warning */
1365 UNUSED(htim);
1367 /* NOTE : This function should not be modified, when the callback is needed,
1368 the HAL_TIM_PWM_MspDeInit could be implemented in the user file
1373 * @brief Starts the PWM signal generation.
1374 * @param htim TIM handle
1375 * @param Channel TIM Channels to be enabled
1376 * This parameter can be one of the following values:
1377 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1378 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1379 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1380 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1381 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
1382 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
1383 * @retval HAL status
1385 HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
1387 uint32_t tmpsmcr;
1389 /* Check the parameters */
1390 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1392 /* Check the TIM channel state */
1393 if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
1395 return HAL_ERROR;
1398 /* Set the TIM channel state */
1399 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1401 /* Enable the Capture compare channel */
1402 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1404 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1406 /* Enable the main output */
1407 __HAL_TIM_MOE_ENABLE(htim);
1410 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1411 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1412 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1414 __HAL_TIM_ENABLE(htim);
1417 /* Return function status */
1418 return HAL_OK;
1422 * @brief Stops the PWM signal generation.
1423 * @param htim TIM PWM handle
1424 * @param Channel TIM Channels to be disabled
1425 * This parameter can be one of the following values:
1426 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1427 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1428 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1429 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1430 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
1431 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
1432 * @retval HAL status
1434 HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
1436 /* Check the parameters */
1437 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1439 /* Disable the Capture compare channel */
1440 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1442 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1444 /* Disable the Main Output */
1445 __HAL_TIM_MOE_DISABLE(htim);
1448 /* Disable the Peripheral */
1449 __HAL_TIM_DISABLE(htim);
1451 /* Set the TIM channel state */
1452 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1454 /* Return function status */
1455 return HAL_OK;
1459 * @brief Starts the PWM signal generation in interrupt mode.
1460 * @param htim TIM PWM handle
1461 * @param Channel TIM Channel to be enabled
1462 * This parameter can be one of the following values:
1463 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1464 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1465 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1466 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1467 * @retval HAL status
1469 HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1471 uint32_t tmpsmcr;
1472 /* Check the parameters */
1473 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1475 /* Check the TIM channel state */
1476 if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
1478 return HAL_ERROR;
1481 /* Set the TIM channel state */
1482 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1484 switch (Channel)
1486 case TIM_CHANNEL_1:
1488 /* Enable the TIM Capture/Compare 1 interrupt */
1489 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1490 break;
1493 case TIM_CHANNEL_2:
1495 /* Enable the TIM Capture/Compare 2 interrupt */
1496 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1497 break;
1500 case TIM_CHANNEL_3:
1502 /* Enable the TIM Capture/Compare 3 interrupt */
1503 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1504 break;
1507 case TIM_CHANNEL_4:
1509 /* Enable the TIM Capture/Compare 4 interrupt */
1510 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
1511 break;
1514 default:
1515 break;
1518 /* Enable the Capture compare channel */
1519 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1521 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1523 /* Enable the main output */
1524 __HAL_TIM_MOE_ENABLE(htim);
1527 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1528 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1529 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1531 __HAL_TIM_ENABLE(htim);
1534 /* Return function status */
1535 return HAL_OK;
1539 * @brief Stops the PWM signal generation in interrupt mode.
1540 * @param htim TIM PWM handle
1541 * @param Channel TIM Channels to be disabled
1542 * This parameter can be one of the following values:
1543 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1544 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1545 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1546 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1547 * @retval HAL status
1549 HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1551 /* Check the parameters */
1552 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1554 switch (Channel)
1556 case TIM_CHANNEL_1:
1558 /* Disable the TIM Capture/Compare 1 interrupt */
1559 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1560 break;
1563 case TIM_CHANNEL_2:
1565 /* Disable the TIM Capture/Compare 2 interrupt */
1566 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1567 break;
1570 case TIM_CHANNEL_3:
1572 /* Disable the TIM Capture/Compare 3 interrupt */
1573 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1574 break;
1577 case TIM_CHANNEL_4:
1579 /* Disable the TIM Capture/Compare 4 interrupt */
1580 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
1581 break;
1584 default:
1585 break;
1588 /* Disable the Capture compare channel */
1589 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1591 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1593 /* Disable the Main Output */
1594 __HAL_TIM_MOE_DISABLE(htim);
1597 /* Disable the Peripheral */
1598 __HAL_TIM_DISABLE(htim);
1600 /* Set the TIM channel state */
1601 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1603 /* Return function status */
1604 return HAL_OK;
1608 * @brief Starts the TIM PWM signal generation in DMA mode.
1609 * @param htim TIM PWM handle
1610 * @param Channel TIM Channels to be enabled
1611 * This parameter can be one of the following values:
1612 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1613 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1614 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1615 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1616 * @param pData The source Buffer address.
1617 * @param Length The length of data to be transferred from memory to TIM peripheral
1618 * @retval HAL status
1620 HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
1622 uint32_t tmpsmcr;
1624 /* Check the parameters */
1625 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1627 /* Set the TIM channel state */
1628 if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
1630 return HAL_BUSY;
1632 else if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
1634 if ((pData == NULL) && (Length > 0U))
1636 return HAL_ERROR;
1638 else
1640 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1643 else
1645 return HAL_ERROR;
1648 switch (Channel)
1650 case TIM_CHANNEL_1:
1652 /* Set the DMA compare callbacks */
1653 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
1654 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1656 /* Set the DMA error callback */
1657 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
1659 /* Enable the DMA stream */
1660 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
1662 return HAL_ERROR;
1665 /* Enable the TIM Capture/Compare 1 DMA request */
1666 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1667 break;
1670 case TIM_CHANNEL_2:
1672 /* Set the DMA compare callbacks */
1673 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
1674 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1676 /* Set the DMA error callback */
1677 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
1679 /* Enable the DMA stream */
1680 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
1682 return HAL_ERROR;
1684 /* Enable the TIM Capture/Compare 2 DMA request */
1685 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1686 break;
1689 case TIM_CHANNEL_3:
1691 /* Set the DMA compare callbacks */
1692 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
1693 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1695 /* Set the DMA error callback */
1696 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
1698 /* Enable the DMA stream */
1699 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
1701 return HAL_ERROR;
1703 /* Enable the TIM Output Capture/Compare 3 request */
1704 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1705 break;
1708 case TIM_CHANNEL_4:
1710 /* Set the DMA compare callbacks */
1711 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
1712 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1714 /* Set the DMA error callback */
1715 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
1717 /* Enable the DMA stream */
1718 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length) != HAL_OK)
1720 return HAL_ERROR;
1722 /* Enable the TIM Capture/Compare 4 DMA request */
1723 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1724 break;
1727 default:
1728 break;
1731 /* Enable the Capture compare channel */
1732 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1734 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1736 /* Enable the main output */
1737 __HAL_TIM_MOE_ENABLE(htim);
1740 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1741 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1742 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1744 __HAL_TIM_ENABLE(htim);
1747 /* Return function status */
1748 return HAL_OK;
1752 * @brief Stops the TIM PWM signal generation in DMA mode.
1753 * @param htim TIM PWM handle
1754 * @param Channel TIM Channels to be disabled
1755 * This parameter can be one of the following values:
1756 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1757 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1758 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1759 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1760 * @retval HAL status
1762 HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1764 /* Check the parameters */
1765 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1767 switch (Channel)
1769 case TIM_CHANNEL_1:
1771 /* Disable the TIM Capture/Compare 1 DMA request */
1772 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1773 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1774 break;
1777 case TIM_CHANNEL_2:
1779 /* Disable the TIM Capture/Compare 2 DMA request */
1780 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1781 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1782 break;
1785 case TIM_CHANNEL_3:
1787 /* Disable the TIM Capture/Compare 3 DMA request */
1788 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1789 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1790 break;
1793 case TIM_CHANNEL_4:
1795 /* Disable the TIM Capture/Compare 4 interrupt */
1796 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1797 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
1798 break;
1801 default:
1802 break;
1805 /* Disable the Capture compare channel */
1806 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1808 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1810 /* Disable the Main Output */
1811 __HAL_TIM_MOE_DISABLE(htim);
1814 /* Disable the Peripheral */
1815 __HAL_TIM_DISABLE(htim);
1817 /* Set the TIM channel state */
1818 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1820 /* Return function status */
1821 return HAL_OK;
1825 * @}
1828 /** @defgroup TIM_Exported_Functions_Group4 TIM Input Capture functions
1829 * @brief TIM Input Capture functions
1831 @verbatim
1832 ==============================================================================
1833 ##### TIM Input Capture functions #####
1834 ==============================================================================
1835 [..]
1836 This section provides functions allowing to:
1837 (+) Initialize and configure the TIM Input Capture.
1838 (+) De-initialize the TIM Input Capture.
1839 (+) Start the TIM Input Capture.
1840 (+) Stop the TIM Input Capture.
1841 (+) Start the TIM Input Capture and enable interrupt.
1842 (+) Stop the TIM Input Capture and disable interrupt.
1843 (+) Start the TIM Input Capture and enable DMA transfer.
1844 (+) Stop the TIM Input Capture and disable DMA transfer.
1846 @endverbatim
1847 * @{
1850 * @brief Initializes the TIM Input Capture Time base according to the specified
1851 * parameters in the TIM_HandleTypeDef and initializes the associated handle.
1852 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
1853 * requires a timer reset to avoid unexpected direction
1854 * due to DIR bit readonly in center aligned mode.
1855 * Ex: call @ref HAL_TIM_IC_DeInit() before HAL_TIM_IC_Init()
1856 * @param htim TIM Input Capture handle
1857 * @retval HAL status
1859 HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)
1861 /* Check the TIM handle allocation */
1862 if (htim == NULL)
1864 return HAL_ERROR;
1867 /* Check the parameters */
1868 assert_param(IS_TIM_INSTANCE(htim->Instance));
1869 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
1870 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
1871 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
1873 if (htim->State == HAL_TIM_STATE_RESET)
1875 /* Allocate lock resource and initialize it */
1876 htim->Lock = HAL_UNLOCKED;
1878 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1879 /* Reset interrupt callbacks to legacy weak callbacks */
1880 TIM_ResetCallback(htim);
1882 if (htim->IC_MspInitCallback == NULL)
1884 htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
1886 /* Init the low level hardware : GPIO, CLOCK, NVIC */
1887 htim->IC_MspInitCallback(htim);
1888 #else
1889 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1890 HAL_TIM_IC_MspInit(htim);
1891 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1894 /* Set the TIM state */
1895 htim->State = HAL_TIM_STATE_BUSY;
1897 /* Init the base time for the input capture */
1898 TIM_Base_SetConfig(htim->Instance, &htim->Init);
1900 /* Initialize the DMA burst operation state */
1901 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
1903 /* Initialize the TIM channels state */
1904 TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
1905 TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
1907 /* Initialize the TIM state*/
1908 htim->State = HAL_TIM_STATE_READY;
1910 return HAL_OK;
1914 * @brief DeInitializes the TIM peripheral
1915 * @param htim TIM Input Capture handle
1916 * @retval HAL status
1918 HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)
1920 /* Check the parameters */
1921 assert_param(IS_TIM_INSTANCE(htim->Instance));
1923 htim->State = HAL_TIM_STATE_BUSY;
1925 /* Disable the TIM Peripheral Clock */
1926 __HAL_TIM_DISABLE(htim);
1928 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
1929 if (htim->IC_MspDeInitCallback == NULL)
1931 htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
1933 /* DeInit the low level hardware */
1934 htim->IC_MspDeInitCallback(htim);
1935 #else
1936 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1937 HAL_TIM_IC_MspDeInit(htim);
1938 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
1940 /* Change the DMA burst operation state */
1941 htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
1943 /* Change the TIM channels state */
1944 TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
1945 TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
1947 /* Change TIM state */
1948 htim->State = HAL_TIM_STATE_RESET;
1950 /* Release Lock */
1951 __HAL_UNLOCK(htim);
1953 return HAL_OK;
1957 * @brief Initializes the TIM Input Capture MSP.
1958 * @param htim TIM Input Capture handle
1959 * @retval None
1961 __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
1963 /* Prevent unused argument(s) compilation warning */
1964 UNUSED(htim);
1966 /* NOTE : This function should not be modified, when the callback is needed,
1967 the HAL_TIM_IC_MspInit could be implemented in the user file
1972 * @brief DeInitializes TIM Input Capture MSP.
1973 * @param htim TIM handle
1974 * @retval None
1976 __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)
1978 /* Prevent unused argument(s) compilation warning */
1979 UNUSED(htim);
1981 /* NOTE : This function should not be modified, when the callback is needed,
1982 the HAL_TIM_IC_MspDeInit could be implemented in the user file
1987 * @brief Starts the TIM Input Capture measurement.
1988 * @param htim TIM Input Capture handle
1989 * @param Channel TIM Channels to be enabled
1990 * This parameter can be one of the following values:
1991 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1992 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1993 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1994 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1995 * @retval HAL status
1997 HAL_StatusTypeDef HAL_TIM_IC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
1999 uint32_t tmpsmcr;
2000 HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
2001 HAL_TIM_ChannelStateTypeDef complementary_channel_state = TIM_CHANNEL_N_STATE_GET(htim, Channel);
2003 /* Check the parameters */
2004 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2006 /* Check the TIM channel state */
2007 if ((channel_state != HAL_TIM_CHANNEL_STATE_READY)
2008 || (complementary_channel_state != HAL_TIM_CHANNEL_STATE_READY))
2010 return HAL_ERROR;
2013 /* Set the TIM channel state */
2014 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2015 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2017 /* Enable the Input Capture channel */
2018 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
2020 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
2021 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
2022 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
2024 __HAL_TIM_ENABLE(htim);
2027 /* Return function status */
2028 return HAL_OK;
2032 * @brief Stops the TIM Input Capture measurement.
2033 * @param htim TIM Input Capture handle
2034 * @param Channel TIM Channels to be disabled
2035 * This parameter can be one of the following values:
2036 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2037 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2038 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2039 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2040 * @retval HAL status
2042 HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
2044 /* Check the parameters */
2045 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2047 /* Disable the Input Capture channel */
2048 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
2050 /* Disable the Peripheral */
2051 __HAL_TIM_DISABLE(htim);
2053 /* Set the TIM channel state */
2054 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2055 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2057 /* Return function status */
2058 return HAL_OK;
2062 * @brief Starts the TIM Input Capture measurement in interrupt mode.
2063 * @param htim TIM Input Capture handle
2064 * @param Channel TIM Channels to be enabled
2065 * This parameter can be one of the following values:
2066 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2067 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2068 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2069 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2070 * @retval HAL status
2072 HAL_StatusTypeDef HAL_TIM_IC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2074 uint32_t tmpsmcr;
2075 HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
2076 HAL_TIM_ChannelStateTypeDef complementary_channel_state = TIM_CHANNEL_N_STATE_GET(htim, Channel);
2078 /* Check the parameters */
2079 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2081 /* Check the TIM channel state */
2082 if ((channel_state != HAL_TIM_CHANNEL_STATE_READY)
2083 || (complementary_channel_state != HAL_TIM_CHANNEL_STATE_READY))
2085 return HAL_ERROR;
2088 /* Set the TIM channel state */
2089 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2090 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2092 switch (Channel)
2094 case TIM_CHANNEL_1:
2096 /* Enable the TIM Capture/Compare 1 interrupt */
2097 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2098 break;
2101 case TIM_CHANNEL_2:
2103 /* Enable the TIM Capture/Compare 2 interrupt */
2104 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2105 break;
2108 case TIM_CHANNEL_3:
2110 /* Enable the TIM Capture/Compare 3 interrupt */
2111 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
2112 break;
2115 case TIM_CHANNEL_4:
2117 /* Enable the TIM Capture/Compare 4 interrupt */
2118 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
2119 break;
2122 default:
2123 break;
2125 /* Enable the Input Capture channel */
2126 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
2128 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
2129 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
2130 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
2132 __HAL_TIM_ENABLE(htim);
2135 /* Return function status */
2136 return HAL_OK;
2140 * @brief Stops the TIM Input Capture measurement in interrupt mode.
2141 * @param htim TIM Input Capture handle
2142 * @param Channel TIM Channels to be disabled
2143 * This parameter can be one of the following values:
2144 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2145 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2146 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2147 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2148 * @retval HAL status
2150 HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2152 /* Check the parameters */
2153 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2155 switch (Channel)
2157 case TIM_CHANNEL_1:
2159 /* Disable the TIM Capture/Compare 1 interrupt */
2160 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2161 break;
2164 case TIM_CHANNEL_2:
2166 /* Disable the TIM Capture/Compare 2 interrupt */
2167 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2168 break;
2171 case TIM_CHANNEL_3:
2173 /* Disable the TIM Capture/Compare 3 interrupt */
2174 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
2175 break;
2178 case TIM_CHANNEL_4:
2180 /* Disable the TIM Capture/Compare 4 interrupt */
2181 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
2182 break;
2185 default:
2186 break;
2189 /* Disable the Input Capture channel */
2190 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
2192 /* Disable the Peripheral */
2193 __HAL_TIM_DISABLE(htim);
2195 /* Set the TIM channel state */
2196 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2197 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2199 /* Return function status */
2200 return HAL_OK;
2204 * @brief Starts the TIM Input Capture measurement in DMA mode.
2205 * @param htim TIM Input Capture handle
2206 * @param Channel TIM Channels to be enabled
2207 * This parameter can be one of the following values:
2208 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2209 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2210 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2211 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2212 * @param pData The destination Buffer address.
2213 * @param Length The length of data to be transferred from TIM peripheral to memory.
2214 * @retval HAL status
2216 HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
2218 uint32_t tmpsmcr;
2219 HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
2220 HAL_TIM_ChannelStateTypeDef complementary_channel_state = TIM_CHANNEL_N_STATE_GET(htim, Channel);
2222 /* Check the parameters */
2223 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2224 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2226 /* Set the TIM channel state */
2227 if ((channel_state == HAL_TIM_CHANNEL_STATE_BUSY)
2228 || (complementary_channel_state == HAL_TIM_CHANNEL_STATE_BUSY))
2230 return HAL_BUSY;
2232 else if ((channel_state == HAL_TIM_CHANNEL_STATE_READY)
2233 && (complementary_channel_state == HAL_TIM_CHANNEL_STATE_READY))
2235 if ((pData == NULL) && (Length > 0U))
2237 return HAL_ERROR;
2239 else
2241 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2242 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
2245 else
2247 return HAL_ERROR;
2250 switch (Channel)
2252 case TIM_CHANNEL_1:
2254 /* Set the DMA capture callbacks */
2255 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
2256 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2258 /* Set the DMA error callback */
2259 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
2261 /* Enable the DMA stream */
2262 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
2264 return HAL_ERROR;
2266 /* Enable the TIM Capture/Compare 1 DMA request */
2267 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
2268 break;
2271 case TIM_CHANNEL_2:
2273 /* Set the DMA capture callbacks */
2274 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
2275 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2277 /* Set the DMA error callback */
2278 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
2280 /* Enable the DMA stream */
2281 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData, Length) != HAL_OK)
2283 return HAL_ERROR;
2285 /* Enable the TIM Capture/Compare 2 DMA request */
2286 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
2287 break;
2290 case TIM_CHANNEL_3:
2292 /* Set the DMA capture callbacks */
2293 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
2294 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2296 /* Set the DMA error callback */
2297 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
2299 /* Enable the DMA stream */
2300 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData, Length) != HAL_OK)
2302 return HAL_ERROR;
2304 /* Enable the TIM Capture/Compare 3 DMA request */
2305 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
2306 break;
2309 case TIM_CHANNEL_4:
2311 /* Set the DMA capture callbacks */
2312 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
2313 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
2315 /* Set the DMA error callback */
2316 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
2318 /* Enable the DMA stream */
2319 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData, Length) != HAL_OK)
2321 return HAL_ERROR;
2323 /* Enable the TIM Capture/Compare 4 DMA request */
2324 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
2325 break;
2328 default:
2329 break;
2332 /* Enable the Input Capture channel */
2333 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
2335 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
2336 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
2337 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
2339 __HAL_TIM_ENABLE(htim);
2342 /* Return function status */
2343 return HAL_OK;
2347 * @brief Stops the TIM Input Capture measurement in DMA mode.
2348 * @param htim TIM Input Capture handle
2349 * @param Channel TIM Channels to be disabled
2350 * This parameter can be one of the following values:
2351 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2352 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2353 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2354 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2355 * @retval HAL status
2357 HAL_StatusTypeDef HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
2359 /* Check the parameters */
2360 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
2361 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2363 /* Disable the Input Capture channel */
2364 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
2366 switch (Channel)
2368 case TIM_CHANNEL_1:
2370 /* Disable the TIM Capture/Compare 1 DMA request */
2371 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
2372 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
2373 break;
2376 case TIM_CHANNEL_2:
2378 /* Disable the TIM Capture/Compare 2 DMA request */
2379 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
2380 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
2381 break;
2384 case TIM_CHANNEL_3:
2386 /* Disable the TIM Capture/Compare 3 DMA request */
2387 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
2388 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
2389 break;
2392 case TIM_CHANNEL_4:
2394 /* Disable the TIM Capture/Compare 4 DMA request */
2395 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
2396 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
2397 break;
2400 default:
2401 break;
2404 /* Disable the Peripheral */
2405 __HAL_TIM_DISABLE(htim);
2407 /* Set the TIM channel state */
2408 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2409 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
2411 /* Return function status */
2412 return HAL_OK;
2415 * @}
2418 /** @defgroup TIM_Exported_Functions_Group5 TIM One Pulse functions
2419 * @brief TIM One Pulse functions
2421 @verbatim
2422 ==============================================================================
2423 ##### TIM One Pulse functions #####
2424 ==============================================================================
2425 [..]
2426 This section provides functions allowing to:
2427 (+) Initialize and configure the TIM One Pulse.
2428 (+) De-initialize the TIM One Pulse.
2429 (+) Start the TIM One Pulse.
2430 (+) Stop the TIM One Pulse.
2431 (+) Start the TIM One Pulse and enable interrupt.
2432 (+) Stop the TIM One Pulse and disable interrupt.
2433 (+) Start the TIM One Pulse and enable DMA transfer.
2434 (+) Stop the TIM One Pulse and disable DMA transfer.
2436 @endverbatim
2437 * @{
2440 * @brief Initializes the TIM One Pulse Time Base according to the specified
2441 * parameters in the TIM_HandleTypeDef and initializes the associated handle.
2442 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
2443 * requires a timer reset to avoid unexpected direction
2444 * due to DIR bit readonly in center aligned mode.
2445 * Ex: call @ref HAL_TIM_OnePulse_DeInit() before HAL_TIM_OnePulse_Init()
2446 * @note When the timer instance is initialized in One Pulse mode, timer
2447 * channels 1 and channel 2 are reserved and cannot be used for other
2448 * purpose.
2449 * @param htim TIM One Pulse handle
2450 * @param OnePulseMode Select the One pulse mode.
2451 * This parameter can be one of the following values:
2452 * @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
2453 * @arg TIM_OPMODE_REPETITIVE: Repetitive pulses will be generated.
2454 * @retval HAL status
2456 HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)
2458 /* Check the TIM handle allocation */
2459 if (htim == NULL)
2461 return HAL_ERROR;
2464 /* Check the parameters */
2465 assert_param(IS_TIM_INSTANCE(htim->Instance));
2466 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
2467 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
2468 assert_param(IS_TIM_OPM_MODE(OnePulseMode));
2469 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
2471 if (htim->State == HAL_TIM_STATE_RESET)
2473 /* Allocate lock resource and initialize it */
2474 htim->Lock = HAL_UNLOCKED;
2476 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2477 /* Reset interrupt callbacks to legacy weak callbacks */
2478 TIM_ResetCallback(htim);
2480 if (htim->OnePulse_MspInitCallback == NULL)
2482 htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
2484 /* Init the low level hardware : GPIO, CLOCK, NVIC */
2485 htim->OnePulse_MspInitCallback(htim);
2486 #else
2487 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
2488 HAL_TIM_OnePulse_MspInit(htim);
2489 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2492 /* Set the TIM state */
2493 htim->State = HAL_TIM_STATE_BUSY;
2495 /* Configure the Time base in the One Pulse Mode */
2496 TIM_Base_SetConfig(htim->Instance, &htim->Init);
2498 /* Reset the OPM Bit */
2499 htim->Instance->CR1 &= ~TIM_CR1_OPM;
2501 /* Configure the OPM Mode */
2502 htim->Instance->CR1 |= OnePulseMode;
2504 /* Initialize the DMA burst operation state */
2505 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
2507 /* Initialize the TIM channels state */
2508 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2509 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2510 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2511 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2513 /* Initialize the TIM state*/
2514 htim->State = HAL_TIM_STATE_READY;
2516 return HAL_OK;
2520 * @brief DeInitializes the TIM One Pulse
2521 * @param htim TIM One Pulse handle
2522 * @retval HAL status
2524 HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)
2526 /* Check the parameters */
2527 assert_param(IS_TIM_INSTANCE(htim->Instance));
2529 htim->State = HAL_TIM_STATE_BUSY;
2531 /* Disable the TIM Peripheral Clock */
2532 __HAL_TIM_DISABLE(htim);
2534 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2535 if (htim->OnePulse_MspDeInitCallback == NULL)
2537 htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
2539 /* DeInit the low level hardware */
2540 htim->OnePulse_MspDeInitCallback(htim);
2541 #else
2542 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
2543 HAL_TIM_OnePulse_MspDeInit(htim);
2544 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2546 /* Change the DMA burst operation state */
2547 htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
2549 /* Set the TIM channel state */
2550 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
2551 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
2552 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
2553 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
2555 /* Change TIM state */
2556 htim->State = HAL_TIM_STATE_RESET;
2558 /* Release Lock */
2559 __HAL_UNLOCK(htim);
2561 return HAL_OK;
2565 * @brief Initializes the TIM One Pulse MSP.
2566 * @param htim TIM One Pulse handle
2567 * @retval None
2569 __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
2571 /* Prevent unused argument(s) compilation warning */
2572 UNUSED(htim);
2574 /* NOTE : This function should not be modified, when the callback is needed,
2575 the HAL_TIM_OnePulse_MspInit could be implemented in the user file
2580 * @brief DeInitializes TIM One Pulse MSP.
2581 * @param htim TIM One Pulse handle
2582 * @retval None
2584 __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)
2586 /* Prevent unused argument(s) compilation warning */
2587 UNUSED(htim);
2589 /* NOTE : This function should not be modified, when the callback is needed,
2590 the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
2595 * @brief Starts the TIM One Pulse signal generation.
2596 * @param htim TIM One Pulse handle
2597 * @param OutputChannel TIM Channels to be enabled
2598 * This parameter can be one of the following values:
2599 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2600 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2601 * @retval HAL status
2603 HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2605 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
2606 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
2607 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
2608 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
2610 /* Prevent unused argument(s) compilation warning */
2611 UNUSED(OutputChannel);
2613 /* Check the TIM channels state */
2614 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
2615 || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
2616 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
2617 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
2619 return HAL_ERROR;
2622 /* Set the TIM channels state */
2623 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
2624 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
2625 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
2626 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
2628 /* Enable the Capture compare and the Input Capture channels
2629 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2630 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2631 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2632 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2634 No need to enable the counter, it's enabled automatically by hardware
2635 (the counter starts in response to a stimulus and generate a pulse */
2637 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2638 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2640 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2642 /* Enable the main output */
2643 __HAL_TIM_MOE_ENABLE(htim);
2646 /* Return function status */
2647 return HAL_OK;
2651 * @brief Stops the TIM One Pulse signal generation.
2652 * @param htim TIM One Pulse handle
2653 * @param OutputChannel TIM Channels to be disable
2654 * This parameter can be one of the following values:
2655 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2656 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2657 * @retval HAL status
2659 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2661 /* Prevent unused argument(s) compilation warning */
2662 UNUSED(OutputChannel);
2664 /* Disable the Capture compare and the Input Capture channels
2665 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2666 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2667 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2668 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2670 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2671 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2673 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2675 /* Disable the Main Output */
2676 __HAL_TIM_MOE_DISABLE(htim);
2679 /* Disable the Peripheral */
2680 __HAL_TIM_DISABLE(htim);
2682 /* Set the TIM channels state */
2683 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2684 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2685 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2686 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2688 /* Return function status */
2689 return HAL_OK;
2693 * @brief Starts the TIM One Pulse signal generation in interrupt mode.
2694 * @param htim TIM One Pulse handle
2695 * @param OutputChannel TIM Channels to be enabled
2696 * This parameter can be one of the following values:
2697 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2698 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2699 * @retval HAL status
2701 HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2703 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
2704 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
2705 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
2706 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
2708 /* Prevent unused argument(s) compilation warning */
2709 UNUSED(OutputChannel);
2711 /* Check the TIM channels state */
2712 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
2713 || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
2714 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
2715 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
2717 return HAL_ERROR;
2720 /* Set the TIM channels state */
2721 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
2722 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
2723 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
2724 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
2726 /* Enable the Capture compare and the Input Capture channels
2727 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2728 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2729 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2730 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2732 No need to enable the counter, it's enabled automatically by hardware
2733 (the counter starts in response to a stimulus and generate a pulse */
2735 /* Enable the TIM Capture/Compare 1 interrupt */
2736 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2738 /* Enable the TIM Capture/Compare 2 interrupt */
2739 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2741 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2742 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2744 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2746 /* Enable the main output */
2747 __HAL_TIM_MOE_ENABLE(htim);
2750 /* Return function status */
2751 return HAL_OK;
2755 * @brief Stops the TIM One Pulse signal generation in interrupt mode.
2756 * @param htim TIM One Pulse handle
2757 * @param OutputChannel TIM Channels to be enabled
2758 * This parameter can be one of the following values:
2759 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2760 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2761 * @retval HAL status
2763 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2765 /* Prevent unused argument(s) compilation warning */
2766 UNUSED(OutputChannel);
2768 /* Disable the TIM Capture/Compare 1 interrupt */
2769 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2771 /* Disable the TIM Capture/Compare 2 interrupt */
2772 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2774 /* Disable the Capture compare and the Input Capture channels
2775 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2776 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2777 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2778 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2779 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2780 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2782 if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2784 /* Disable the Main Output */
2785 __HAL_TIM_MOE_DISABLE(htim);
2788 /* Disable the Peripheral */
2789 __HAL_TIM_DISABLE(htim);
2791 /* Set the TIM channels state */
2792 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2793 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2794 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2795 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2797 /* Return function status */
2798 return HAL_OK;
2802 * @}
2805 /** @defgroup TIM_Exported_Functions_Group6 TIM Encoder functions
2806 * @brief TIM Encoder functions
2808 @verbatim
2809 ==============================================================================
2810 ##### TIM Encoder functions #####
2811 ==============================================================================
2812 [..]
2813 This section provides functions allowing to:
2814 (+) Initialize and configure the TIM Encoder.
2815 (+) De-initialize the TIM Encoder.
2816 (+) Start the TIM Encoder.
2817 (+) Stop the TIM Encoder.
2818 (+) Start the TIM Encoder and enable interrupt.
2819 (+) Stop the TIM Encoder and disable interrupt.
2820 (+) Start the TIM Encoder and enable DMA transfer.
2821 (+) Stop the TIM Encoder and disable DMA transfer.
2823 @endverbatim
2824 * @{
2827 * @brief Initializes the TIM Encoder Interface and initialize the associated handle.
2828 * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
2829 * requires a timer reset to avoid unexpected direction
2830 * due to DIR bit readonly in center aligned mode.
2831 * Ex: call @ref HAL_TIM_Encoder_DeInit() before HAL_TIM_Encoder_Init()
2832 * @note Encoder mode and External clock mode 2 are not compatible and must not be selected together
2833 * Ex: A call for @ref HAL_TIM_Encoder_Init will erase the settings of @ref HAL_TIM_ConfigClockSource
2834 * using TIM_CLOCKSOURCE_ETRMODE2 and vice versa
2835 * @note When the timer instance is initialized in Encoder mode, timer
2836 * channels 1 and channel 2 are reserved and cannot be used for other
2837 * purpose.
2838 * @param htim TIM Encoder Interface handle
2839 * @param sConfig TIM Encoder Interface configuration structure
2840 * @retval HAL status
2842 HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim, TIM_Encoder_InitTypeDef *sConfig)
2844 uint32_t tmpsmcr;
2845 uint32_t tmpccmr1;
2846 uint32_t tmpccer;
2848 /* Check the TIM handle allocation */
2849 if (htim == NULL)
2851 return HAL_ERROR;
2854 /* Check the parameters */
2855 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
2856 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
2857 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
2858 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
2859 assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));
2860 assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));
2861 assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));
2862 assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC1Polarity));
2863 assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC2Polarity));
2864 assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
2865 assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));
2866 assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
2867 assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));
2869 if (htim->State == HAL_TIM_STATE_RESET)
2871 /* Allocate lock resource and initialize it */
2872 htim->Lock = HAL_UNLOCKED;
2874 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2875 /* Reset interrupt callbacks to legacy weak callbacks */
2876 TIM_ResetCallback(htim);
2878 if (htim->Encoder_MspInitCallback == NULL)
2880 htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
2882 /* Init the low level hardware : GPIO, CLOCK, NVIC */
2883 htim->Encoder_MspInitCallback(htim);
2884 #else
2885 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
2886 HAL_TIM_Encoder_MspInit(htim);
2887 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2890 /* Set the TIM state */
2891 htim->State = HAL_TIM_STATE_BUSY;
2893 /* Reset the SMS and ECE bits */
2894 htim->Instance->SMCR &= ~(TIM_SMCR_SMS | TIM_SMCR_ECE);
2896 /* Configure the Time base in the Encoder Mode */
2897 TIM_Base_SetConfig(htim->Instance, &htim->Init);
2899 /* Get the TIMx SMCR register value */
2900 tmpsmcr = htim->Instance->SMCR;
2902 /* Get the TIMx CCMR1 register value */
2903 tmpccmr1 = htim->Instance->CCMR1;
2905 /* Get the TIMx CCER register value */
2906 tmpccer = htim->Instance->CCER;
2908 /* Set the encoder Mode */
2909 tmpsmcr |= sConfig->EncoderMode;
2911 /* Select the Capture Compare 1 and the Capture Compare 2 as input */
2912 tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);
2913 tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8U));
2915 /* Set the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
2916 tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);
2917 tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);
2918 tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8U);
2919 tmpccmr1 |= (sConfig->IC1Filter << 4U) | (sConfig->IC2Filter << 12U);
2921 /* Set the TI1 and the TI2 Polarities */
2922 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
2923 tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);
2924 tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4U);
2926 /* Write to TIMx SMCR */
2927 htim->Instance->SMCR = tmpsmcr;
2929 /* Write to TIMx CCMR1 */
2930 htim->Instance->CCMR1 = tmpccmr1;
2932 /* Write to TIMx CCER */
2933 htim->Instance->CCER = tmpccer;
2935 /* Initialize the DMA burst operation state */
2936 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
2938 /* Set the TIM channels state */
2939 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2940 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2941 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2942 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2944 /* Initialize the TIM state*/
2945 htim->State = HAL_TIM_STATE_READY;
2947 return HAL_OK;
2952 * @brief DeInitializes the TIM Encoder interface
2953 * @param htim TIM Encoder Interface handle
2954 * @retval HAL status
2956 HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)
2958 /* Check the parameters */
2959 assert_param(IS_TIM_INSTANCE(htim->Instance));
2961 htim->State = HAL_TIM_STATE_BUSY;
2963 /* Disable the TIM Peripheral Clock */
2964 __HAL_TIM_DISABLE(htim);
2966 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2967 if (htim->Encoder_MspDeInitCallback == NULL)
2969 htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
2971 /* DeInit the low level hardware */
2972 htim->Encoder_MspDeInitCallback(htim);
2973 #else
2974 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
2975 HAL_TIM_Encoder_MspDeInit(htim);
2976 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2978 /* Change the DMA burst operation state */
2979 htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
2981 /* Set the TIM channels state */
2982 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
2983 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
2984 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
2985 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
2987 /* Change TIM state */
2988 htim->State = HAL_TIM_STATE_RESET;
2990 /* Release Lock */
2991 __HAL_UNLOCK(htim);
2993 return HAL_OK;
2997 * @brief Initializes the TIM Encoder Interface MSP.
2998 * @param htim TIM Encoder Interface handle
2999 * @retval None
3001 __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
3003 /* Prevent unused argument(s) compilation warning */
3004 UNUSED(htim);
3006 /* NOTE : This function should not be modified, when the callback is needed,
3007 the HAL_TIM_Encoder_MspInit could be implemented in the user file
3012 * @brief DeInitializes TIM Encoder Interface MSP.
3013 * @param htim TIM Encoder Interface handle
3014 * @retval None
3016 __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)
3018 /* Prevent unused argument(s) compilation warning */
3019 UNUSED(htim);
3021 /* NOTE : This function should not be modified, when the callback is needed,
3022 the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
3027 * @brief Starts the TIM Encoder Interface.
3028 * @param htim TIM Encoder Interface handle
3029 * @param Channel TIM Channels to be enabled
3030 * This parameter can be one of the following values:
3031 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3032 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3033 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3034 * @retval HAL status
3036 HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
3038 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
3039 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
3040 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
3041 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
3043 /* Check the parameters */
3044 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3046 /* Set the TIM channel(s) state */
3047 if (Channel == TIM_CHANNEL_1)
3049 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3050 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY))
3052 return HAL_ERROR;
3054 else
3056 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3057 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3060 else if (Channel == TIM_CHANNEL_2)
3062 if ((channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
3063 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
3065 return HAL_ERROR;
3067 else
3069 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3070 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3073 else
3075 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3076 || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
3077 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3078 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
3080 return HAL_ERROR;
3082 else
3084 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3085 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3086 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3087 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3091 /* Enable the encoder interface channels */
3092 switch (Channel)
3094 case TIM_CHANNEL_1:
3096 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3097 break;
3100 case TIM_CHANNEL_2:
3102 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3103 break;
3106 default :
3108 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3109 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3110 break;
3113 /* Enable the Peripheral */
3114 __HAL_TIM_ENABLE(htim);
3116 /* Return function status */
3117 return HAL_OK;
3121 * @brief Stops the TIM Encoder Interface.
3122 * @param htim TIM Encoder Interface handle
3123 * @param Channel TIM Channels to be disabled
3124 * This parameter can be one of the following values:
3125 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3126 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3127 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3128 * @retval HAL status
3130 HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
3132 /* Check the parameters */
3133 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3135 /* Disable the Input Capture channels 1 and 2
3136 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
3137 switch (Channel)
3139 case TIM_CHANNEL_1:
3141 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3142 break;
3145 case TIM_CHANNEL_2:
3147 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3148 break;
3151 default :
3153 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3154 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3155 break;
3159 /* Disable the Peripheral */
3160 __HAL_TIM_DISABLE(htim);
3162 /* Set the TIM channel(s) state */
3163 if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
3165 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3166 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3168 else
3170 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3171 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3172 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3173 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3176 /* Return function status */
3177 return HAL_OK;
3181 * @brief Starts the TIM Encoder Interface in interrupt mode.
3182 * @param htim TIM Encoder Interface handle
3183 * @param Channel TIM Channels to be enabled
3184 * This parameter can be one of the following values:
3185 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3186 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3187 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3188 * @retval HAL status
3190 HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
3192 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
3193 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
3194 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
3195 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
3197 /* Check the parameters */
3198 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3200 /* Set the TIM channel(s) state */
3201 if (Channel == TIM_CHANNEL_1)
3203 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3204 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY))
3206 return HAL_ERROR;
3208 else
3210 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3211 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3214 else if (Channel == TIM_CHANNEL_2)
3216 if ((channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
3217 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
3219 return HAL_ERROR;
3221 else
3223 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3224 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3227 else
3229 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3230 || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
3231 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
3232 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
3234 return HAL_ERROR;
3236 else
3238 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3239 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3240 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3241 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3245 /* Enable the encoder interface channels */
3246 /* Enable the capture compare Interrupts 1 and/or 2 */
3247 switch (Channel)
3249 case TIM_CHANNEL_1:
3251 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3252 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
3253 break;
3256 case TIM_CHANNEL_2:
3258 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3259 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
3260 break;
3263 default :
3265 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3266 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3267 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
3268 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
3269 break;
3273 /* Enable the Peripheral */
3274 __HAL_TIM_ENABLE(htim);
3276 /* Return function status */
3277 return HAL_OK;
3281 * @brief Stops the TIM Encoder Interface in interrupt mode.
3282 * @param htim TIM Encoder Interface handle
3283 * @param Channel TIM Channels to be disabled
3284 * This parameter can be one of the following values:
3285 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3286 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3287 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3288 * @retval HAL status
3290 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
3292 /* Check the parameters */
3293 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3295 /* Disable the Input Capture channels 1 and 2
3296 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
3297 if (Channel == TIM_CHANNEL_1)
3299 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3301 /* Disable the capture compare Interrupts 1 */
3302 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
3304 else if (Channel == TIM_CHANNEL_2)
3306 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3308 /* Disable the capture compare Interrupts 2 */
3309 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
3311 else
3313 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3314 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3316 /* Disable the capture compare Interrupts 1 and 2 */
3317 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
3318 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
3321 /* Disable the Peripheral */
3322 __HAL_TIM_DISABLE(htim);
3324 /* Set the TIM channel(s) state */
3325 if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
3327 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3328 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3330 else
3332 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3333 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3334 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3335 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3338 /* Return function status */
3339 return HAL_OK;
3343 * @brief Starts the TIM Encoder Interface in DMA mode.
3344 * @param htim TIM Encoder Interface handle
3345 * @param Channel TIM Channels to be enabled
3346 * This parameter can be one of the following values:
3347 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3348 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3349 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3350 * @param pData1 The destination Buffer address for IC1.
3351 * @param pData2 The destination Buffer address for IC2.
3352 * @param Length The length of data to be transferred from TIM peripheral to memory.
3353 * @retval HAL status
3355 HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1,
3356 uint32_t *pData2, uint16_t Length)
3358 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
3359 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
3360 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
3361 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
3363 /* Check the parameters */
3364 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3366 /* Set the TIM channel(s) state */
3367 if (Channel == TIM_CHANNEL_1)
3369 if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
3370 || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY))
3372 return HAL_BUSY;
3374 else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
3375 && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY))
3377 if ((pData1 == NULL) && (Length > 0U))
3379 return HAL_ERROR;
3381 else
3383 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3384 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3387 else
3389 return HAL_ERROR;
3392 else if (Channel == TIM_CHANNEL_2)
3394 if ((channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY)
3395 || (complementary_channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY))
3397 return HAL_BUSY;
3399 else if ((channel_2_state == HAL_TIM_CHANNEL_STATE_READY)
3400 && (complementary_channel_2_state == HAL_TIM_CHANNEL_STATE_READY))
3402 if ((pData2 == NULL) && (Length > 0U))
3404 return HAL_ERROR;
3406 else
3408 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3409 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3412 else
3414 return HAL_ERROR;
3417 else
3419 if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
3420 || (channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY)
3421 || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
3422 || (complementary_channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY))
3424 return HAL_BUSY;
3426 else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
3427 && (channel_2_state == HAL_TIM_CHANNEL_STATE_READY)
3428 && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
3429 && (complementary_channel_2_state == HAL_TIM_CHANNEL_STATE_READY))
3431 if ((((pData1 == NULL) || (pData2 == NULL))) && (Length > 0U))
3433 return HAL_ERROR;
3435 else
3437 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3438 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3439 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
3440 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
3443 else
3445 return HAL_ERROR;
3449 switch (Channel)
3451 case TIM_CHANNEL_1:
3453 /* Set the DMA capture callbacks */
3454 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
3455 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3457 /* Set the DMA error callback */
3458 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
3460 /* Enable the DMA stream */
3461 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length) != HAL_OK)
3463 return HAL_ERROR;
3465 /* Enable the TIM Input Capture DMA request */
3466 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
3468 /* Enable the Peripheral */
3469 __HAL_TIM_ENABLE(htim);
3471 /* Enable the Capture compare channel */
3472 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3473 break;
3476 case TIM_CHANNEL_2:
3478 /* Set the DMA capture callbacks */
3479 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
3480 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3482 /* Set the DMA error callback */
3483 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError;
3484 /* Enable the DMA stream */
3485 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length) != HAL_OK)
3487 return HAL_ERROR;
3489 /* Enable the TIM Input Capture DMA request */
3490 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
3492 /* Enable the Peripheral */
3493 __HAL_TIM_ENABLE(htim);
3495 /* Enable the Capture compare channel */
3496 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3497 break;
3500 case TIM_CHANNEL_ALL:
3502 /* Set the DMA capture callbacks */
3503 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
3504 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3506 /* Set the DMA error callback */
3507 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
3509 /* Enable the DMA stream */
3510 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length) != HAL_OK)
3512 return HAL_ERROR;
3515 /* Set the DMA capture callbacks */
3516 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
3517 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
3519 /* Set the DMA error callback */
3520 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
3522 /* Enable the DMA stream */
3523 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length) != HAL_OK)
3525 return HAL_ERROR;
3527 /* Enable the Peripheral */
3528 __HAL_TIM_ENABLE(htim);
3530 /* Enable the Capture compare channel */
3531 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
3532 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
3534 /* Enable the TIM Input Capture DMA request */
3535 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
3536 /* Enable the TIM Input Capture DMA request */
3537 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
3538 break;
3541 default:
3542 break;
3545 /* Return function status */
3546 return HAL_OK;
3550 * @brief Stops the TIM Encoder Interface in DMA mode.
3551 * @param htim TIM Encoder Interface handle
3552 * @param Channel TIM Channels to be enabled
3553 * This parameter can be one of the following values:
3554 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3555 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3556 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
3557 * @retval HAL status
3559 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
3561 /* Check the parameters */
3562 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3564 /* Disable the Input Capture channels 1 and 2
3565 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
3566 if (Channel == TIM_CHANNEL_1)
3568 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3570 /* Disable the capture compare DMA Request 1 */
3571 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
3572 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
3574 else if (Channel == TIM_CHANNEL_2)
3576 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3578 /* Disable the capture compare DMA Request 2 */
3579 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
3580 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
3582 else
3584 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
3585 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
3587 /* Disable the capture compare DMA Request 1 and 2 */
3588 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
3589 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
3590 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
3591 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
3594 /* Disable the Peripheral */
3595 __HAL_TIM_DISABLE(htim);
3597 /* Set the TIM channel(s) state */
3598 if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
3600 TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3601 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
3603 else
3605 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3606 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3607 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3608 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3611 /* Return function status */
3612 return HAL_OK;
3616 * @}
3618 /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
3619 * @brief TIM IRQ handler management
3621 @verbatim
3622 ==============================================================================
3623 ##### IRQ handler management #####
3624 ==============================================================================
3625 [..]
3626 This section provides Timer IRQ handler function.
3628 @endverbatim
3629 * @{
3632 * @brief This function handles TIM interrupts requests.
3633 * @param htim TIM handle
3634 * @retval None
3636 void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
3638 /* Capture compare 1 event */
3639 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)
3641 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) != RESET)
3644 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
3645 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
3647 /* Input capture event */
3648 if ((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U)
3650 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3651 htim->IC_CaptureCallback(htim);
3652 #else
3653 HAL_TIM_IC_CaptureCallback(htim);
3654 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3656 /* Output compare event */
3657 else
3659 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3660 htim->OC_DelayElapsedCallback(htim);
3661 htim->PWM_PulseFinishedCallback(htim);
3662 #else
3663 HAL_TIM_OC_DelayElapsedCallback(htim);
3664 HAL_TIM_PWM_PulseFinishedCallback(htim);
3665 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3667 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3671 /* Capture compare 2 event */
3672 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET)
3674 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) != RESET)
3676 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);
3677 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
3678 /* Input capture event */
3679 if ((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U)
3681 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3682 htim->IC_CaptureCallback(htim);
3683 #else
3684 HAL_TIM_IC_CaptureCallback(htim);
3685 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3687 /* Output compare event */
3688 else
3690 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3691 htim->OC_DelayElapsedCallback(htim);
3692 htim->PWM_PulseFinishedCallback(htim);
3693 #else
3694 HAL_TIM_OC_DelayElapsedCallback(htim);
3695 HAL_TIM_PWM_PulseFinishedCallback(htim);
3696 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3698 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3701 /* Capture compare 3 event */
3702 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET)
3704 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) != RESET)
3706 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);
3707 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
3708 /* Input capture event */
3709 if ((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U)
3711 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3712 htim->IC_CaptureCallback(htim);
3713 #else
3714 HAL_TIM_IC_CaptureCallback(htim);
3715 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3717 /* Output compare event */
3718 else
3720 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3721 htim->OC_DelayElapsedCallback(htim);
3722 htim->PWM_PulseFinishedCallback(htim);
3723 #else
3724 HAL_TIM_OC_DelayElapsedCallback(htim);
3725 HAL_TIM_PWM_PulseFinishedCallback(htim);
3726 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3728 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3731 /* Capture compare 4 event */
3732 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET)
3734 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) != RESET)
3736 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
3737 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
3738 /* Input capture event */
3739 if ((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U)
3741 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3742 htim->IC_CaptureCallback(htim);
3743 #else
3744 HAL_TIM_IC_CaptureCallback(htim);
3745 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3747 /* Output compare event */
3748 else
3750 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3751 htim->OC_DelayElapsedCallback(htim);
3752 htim->PWM_PulseFinishedCallback(htim);
3753 #else
3754 HAL_TIM_OC_DelayElapsedCallback(htim);
3755 HAL_TIM_PWM_PulseFinishedCallback(htim);
3756 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3758 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3761 /* TIM Update event */
3762 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)
3764 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_UPDATE) != RESET)
3766 __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
3767 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3768 htim->PeriodElapsedCallback(htim);
3769 #else
3770 HAL_TIM_PeriodElapsedCallback(htim);
3771 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3774 /* TIM Break input event */
3775 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK) != RESET)
3777 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) != RESET)
3779 __HAL_TIM_CLEAR_IT(htim, TIM_IT_BREAK);
3780 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3781 htim->BreakCallback(htim);
3782 #else
3783 HAL_TIMEx_BreakCallback(htim);
3784 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3787 /* TIM Break2 input event */
3788 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK2) != RESET)
3790 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) != RESET)
3792 __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_BREAK2);
3793 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3794 htim->Break2Callback(htim);
3795 #else
3796 HAL_TIMEx_Break2Callback(htim);
3797 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3800 /* TIM Trigger detection event */
3801 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_TRIGGER) != RESET)
3803 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_TRIGGER) != RESET)
3805 __HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);
3806 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3807 htim->TriggerCallback(htim);
3808 #else
3809 HAL_TIM_TriggerCallback(htim);
3810 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3813 /* TIM commutation event */
3814 if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_COM) != RESET)
3816 if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_COM) != RESET)
3818 __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_COM);
3819 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3820 htim->CommutationCallback(htim);
3821 #else
3822 HAL_TIMEx_CommutCallback(htim);
3823 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3829 * @}
3832 /** @defgroup TIM_Exported_Functions_Group8 TIM Peripheral Control functions
3833 * @brief TIM Peripheral Control functions
3835 @verbatim
3836 ==============================================================================
3837 ##### Peripheral Control functions #####
3838 ==============================================================================
3839 [..]
3840 This section provides functions allowing to:
3841 (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
3842 (+) Configure External Clock source.
3843 (+) Configure Complementary channels, break features and dead time.
3844 (+) Configure Master and the Slave synchronization.
3845 (+) Configure the DMA Burst Mode.
3847 @endverbatim
3848 * @{
3852 * @brief Initializes the TIM Output Compare Channels according to the specified
3853 * parameters in the TIM_OC_InitTypeDef.
3854 * @param htim TIM Output Compare handle
3855 * @param sConfig TIM Output Compare configuration structure
3856 * @param Channel TIM Channels to configure
3857 * This parameter can be one of the following values:
3858 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3859 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3860 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
3861 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
3862 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
3863 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
3864 * @retval HAL status
3866 HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim,
3867 TIM_OC_InitTypeDef *sConfig,
3868 uint32_t Channel)
3870 /* Check the parameters */
3871 assert_param(IS_TIM_CHANNELS(Channel));
3872 assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
3873 assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
3875 /* Process Locked */
3876 __HAL_LOCK(htim);
3878 switch (Channel)
3880 case TIM_CHANNEL_1:
3882 /* Check the parameters */
3883 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3885 /* Configure the TIM Channel 1 in Output Compare */
3886 TIM_OC1_SetConfig(htim->Instance, sConfig);
3887 break;
3890 case TIM_CHANNEL_2:
3892 /* Check the parameters */
3893 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3895 /* Configure the TIM Channel 2 in Output Compare */
3896 TIM_OC2_SetConfig(htim->Instance, sConfig);
3897 break;
3900 case TIM_CHANNEL_3:
3902 /* Check the parameters */
3903 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
3905 /* Configure the TIM Channel 3 in Output Compare */
3906 TIM_OC3_SetConfig(htim->Instance, sConfig);
3907 break;
3910 case TIM_CHANNEL_4:
3912 /* Check the parameters */
3913 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
3915 /* Configure the TIM Channel 4 in Output Compare */
3916 TIM_OC4_SetConfig(htim->Instance, sConfig);
3917 break;
3920 case TIM_CHANNEL_5:
3922 /* Check the parameters */
3923 assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
3925 /* Configure the TIM Channel 5 in Output Compare */
3926 TIM_OC5_SetConfig(htim->Instance, sConfig);
3927 break;
3930 case TIM_CHANNEL_6:
3932 /* Check the parameters */
3933 assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
3935 /* Configure the TIM Channel 6 in Output Compare */
3936 TIM_OC6_SetConfig(htim->Instance, sConfig);
3937 break;
3940 default:
3941 break;
3944 __HAL_UNLOCK(htim);
3946 return HAL_OK;
3950 * @brief Initializes the TIM Input Capture Channels according to the specified
3951 * parameters in the TIM_IC_InitTypeDef.
3952 * @param htim TIM IC handle
3953 * @param sConfig TIM Input Capture configuration structure
3954 * @param Channel TIM Channel to configure
3955 * This parameter can be one of the following values:
3956 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3957 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3958 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
3959 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
3960 * @retval HAL status
3962 HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_IC_InitTypeDef *sConfig, uint32_t Channel)
3964 /* Check the parameters */
3965 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3966 assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
3967 assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
3968 assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
3969 assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
3971 /* Process Locked */
3972 __HAL_LOCK(htim);
3974 if (Channel == TIM_CHANNEL_1)
3976 /* TI1 Configuration */
3977 TIM_TI1_SetConfig(htim->Instance,
3978 sConfig->ICPolarity,
3979 sConfig->ICSelection,
3980 sConfig->ICFilter);
3982 /* Reset the IC1PSC Bits */
3983 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
3985 /* Set the IC1PSC value */
3986 htim->Instance->CCMR1 |= sConfig->ICPrescaler;
3988 else if (Channel == TIM_CHANNEL_2)
3990 /* TI2 Configuration */
3991 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3993 TIM_TI2_SetConfig(htim->Instance,
3994 sConfig->ICPolarity,
3995 sConfig->ICSelection,
3996 sConfig->ICFilter);
3998 /* Reset the IC2PSC Bits */
3999 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
4001 /* Set the IC2PSC value */
4002 htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8U);
4004 else if (Channel == TIM_CHANNEL_3)
4006 /* TI3 Configuration */
4007 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
4009 TIM_TI3_SetConfig(htim->Instance,
4010 sConfig->ICPolarity,
4011 sConfig->ICSelection,
4012 sConfig->ICFilter);
4014 /* Reset the IC3PSC Bits */
4015 htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
4017 /* Set the IC3PSC value */
4018 htim->Instance->CCMR2 |= sConfig->ICPrescaler;
4020 else
4022 /* TI4 Configuration */
4023 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
4025 TIM_TI4_SetConfig(htim->Instance,
4026 sConfig->ICPolarity,
4027 sConfig->ICSelection,
4028 sConfig->ICFilter);
4030 /* Reset the IC4PSC Bits */
4031 htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
4033 /* Set the IC4PSC value */
4034 htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8U);
4037 __HAL_UNLOCK(htim);
4039 return HAL_OK;
4043 * @brief Initializes the TIM PWM channels according to the specified
4044 * parameters in the TIM_OC_InitTypeDef.
4045 * @param htim TIM PWM handle
4046 * @param sConfig TIM PWM configuration structure
4047 * @param Channel TIM Channels to be configured
4048 * This parameter can be one of the following values:
4049 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
4050 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
4051 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
4052 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
4053 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
4054 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
4055 * @retval HAL status
4057 HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
4058 TIM_OC_InitTypeDef *sConfig,
4059 uint32_t Channel)
4061 /* Check the parameters */
4062 assert_param(IS_TIM_CHANNELS(Channel));
4063 assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
4064 assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
4065 assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
4067 /* Process Locked */
4068 __HAL_LOCK(htim);
4070 switch (Channel)
4072 case TIM_CHANNEL_1:
4074 /* Check the parameters */
4075 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4077 /* Configure the Channel 1 in PWM mode */
4078 TIM_OC1_SetConfig(htim->Instance, sConfig);
4080 /* Set the Preload enable bit for channel1 */
4081 htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
4083 /* Configure the Output Fast mode */
4084 htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
4085 htim->Instance->CCMR1 |= sConfig->OCFastMode;
4086 break;
4089 case TIM_CHANNEL_2:
4091 /* Check the parameters */
4092 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4094 /* Configure the Channel 2 in PWM mode */
4095 TIM_OC2_SetConfig(htim->Instance, sConfig);
4097 /* Set the Preload enable bit for channel2 */
4098 htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
4100 /* Configure the Output Fast mode */
4101 htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
4102 htim->Instance->CCMR1 |= sConfig->OCFastMode << 8U;
4103 break;
4106 case TIM_CHANNEL_3:
4108 /* Check the parameters */
4109 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
4111 /* Configure the Channel 3 in PWM mode */
4112 TIM_OC3_SetConfig(htim->Instance, sConfig);
4114 /* Set the Preload enable bit for channel3 */
4115 htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
4117 /* Configure the Output Fast mode */
4118 htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
4119 htim->Instance->CCMR2 |= sConfig->OCFastMode;
4120 break;
4123 case TIM_CHANNEL_4:
4125 /* Check the parameters */
4126 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
4128 /* Configure the Channel 4 in PWM mode */
4129 TIM_OC4_SetConfig(htim->Instance, sConfig);
4131 /* Set the Preload enable bit for channel4 */
4132 htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
4134 /* Configure the Output Fast mode */
4135 htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
4136 htim->Instance->CCMR2 |= sConfig->OCFastMode << 8U;
4137 break;
4140 case TIM_CHANNEL_5:
4142 /* Check the parameters */
4143 assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
4145 /* Configure the Channel 5 in PWM mode */
4146 TIM_OC5_SetConfig(htim->Instance, sConfig);
4148 /* Set the Preload enable bit for channel5*/
4149 htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
4151 /* Configure the Output Fast mode */
4152 htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;
4153 htim->Instance->CCMR3 |= sConfig->OCFastMode;
4154 break;
4157 case TIM_CHANNEL_6:
4159 /* Check the parameters */
4160 assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
4162 /* Configure the Channel 6 in PWM mode */
4163 TIM_OC6_SetConfig(htim->Instance, sConfig);
4165 /* Set the Preload enable bit for channel6 */
4166 htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
4168 /* Configure the Output Fast mode */
4169 htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;
4170 htim->Instance->CCMR3 |= sConfig->OCFastMode << 8U;
4171 break;
4174 default:
4175 break;
4178 __HAL_UNLOCK(htim);
4180 return HAL_OK;
4184 * @brief Initializes the TIM One Pulse Channels according to the specified
4185 * parameters in the TIM_OnePulse_InitTypeDef.
4186 * @param htim TIM One Pulse handle
4187 * @param sConfig TIM One Pulse configuration structure
4188 * @param OutputChannel TIM output channel to configure
4189 * This parameter can be one of the following values:
4190 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
4191 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
4192 * @param InputChannel TIM input Channel to configure
4193 * This parameter can be one of the following values:
4194 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
4195 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
4196 * @note To output a waveform with a minimum delay user can enable the fast
4197 * mode by calling the @ref __HAL_TIM_ENABLE_OCxFAST macro. Then CCx
4198 * output is forced in response to the edge detection on TIx input,
4199 * without taking in account the comparison.
4200 * @retval HAL status
4202 HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef *sConfig,
4203 uint32_t OutputChannel, uint32_t InputChannel)
4205 TIM_OC_InitTypeDef temp1;
4207 /* Check the parameters */
4208 assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
4209 assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
4211 if (OutputChannel != InputChannel)
4213 /* Process Locked */
4214 __HAL_LOCK(htim);
4216 htim->State = HAL_TIM_STATE_BUSY;
4218 /* Extract the Output compare configuration from sConfig structure */
4219 temp1.OCMode = sConfig->OCMode;
4220 temp1.Pulse = sConfig->Pulse;
4221 temp1.OCPolarity = sConfig->OCPolarity;
4222 temp1.OCNPolarity = sConfig->OCNPolarity;
4223 temp1.OCIdleState = sConfig->OCIdleState;
4224 temp1.OCNIdleState = sConfig->OCNIdleState;
4226 switch (OutputChannel)
4228 case TIM_CHANNEL_1:
4230 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4232 TIM_OC1_SetConfig(htim->Instance, &temp1);
4233 break;
4235 case TIM_CHANNEL_2:
4237 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4239 TIM_OC2_SetConfig(htim->Instance, &temp1);
4240 break;
4242 default:
4243 break;
4246 switch (InputChannel)
4248 case TIM_CHANNEL_1:
4250 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4252 TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
4253 sConfig->ICSelection, sConfig->ICFilter);
4255 /* Reset the IC1PSC Bits */
4256 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
4258 /* Select the Trigger source */
4259 htim->Instance->SMCR &= ~TIM_SMCR_TS;
4260 htim->Instance->SMCR |= TIM_TS_TI1FP1;
4262 /* Select the Slave Mode */
4263 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
4264 htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
4265 break;
4267 case TIM_CHANNEL_2:
4269 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4271 TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
4272 sConfig->ICSelection, sConfig->ICFilter);
4274 /* Reset the IC2PSC Bits */
4275 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
4277 /* Select the Trigger source */
4278 htim->Instance->SMCR &= ~TIM_SMCR_TS;
4279 htim->Instance->SMCR |= TIM_TS_TI2FP2;
4281 /* Select the Slave Mode */
4282 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
4283 htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
4284 break;
4287 default:
4288 break;
4291 htim->State = HAL_TIM_STATE_READY;
4293 __HAL_UNLOCK(htim);
4295 return HAL_OK;
4297 else
4299 return HAL_ERROR;
4304 * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
4305 * @param htim TIM handle
4306 * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
4307 * This parameter can be one of the following values:
4308 * @arg TIM_DMABASE_CR1
4309 * @arg TIM_DMABASE_CR2
4310 * @arg TIM_DMABASE_SMCR
4311 * @arg TIM_DMABASE_DIER
4312 * @arg TIM_DMABASE_SR
4313 * @arg TIM_DMABASE_EGR
4314 * @arg TIM_DMABASE_CCMR1
4315 * @arg TIM_DMABASE_CCMR2
4316 * @arg TIM_DMABASE_CCER
4317 * @arg TIM_DMABASE_CNT
4318 * @arg TIM_DMABASE_PSC
4319 * @arg TIM_DMABASE_ARR
4320 * @arg TIM_DMABASE_RCR
4321 * @arg TIM_DMABASE_CCR1
4322 * @arg TIM_DMABASE_CCR2
4323 * @arg TIM_DMABASE_CCR3
4324 * @arg TIM_DMABASE_CCR4
4325 * @arg TIM_DMABASE_BDTR
4326 * @arg TIM_DMABASE_CCMR3
4327 * @arg TIM_DMABASE_CCR5
4328 * @arg TIM_DMABASE_CCR6
4329 * @arg TIM_DMABASE_AF1
4330 * @arg TIM_DMABASE_AF2
4331 * @arg TIM_DMABASE_TISEL
4333 * @param BurstRequestSrc TIM DMA Request sources
4334 * This parameter can be one of the following values:
4335 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
4336 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4337 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4338 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4339 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4340 * @arg TIM_DMA_COM: TIM Commutation DMA source
4341 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4342 * @param BurstBuffer The Buffer address.
4343 * @param BurstLength DMA Burst length. This parameter can be one value
4344 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4345 * @note This function should be used only when BurstLength is equal to DMA data transfer length.
4346 * @retval HAL status
4348 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4349 uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
4351 return HAL_TIM_DMABurst_MultiWriteStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
4352 ((BurstLength) >> 8U) + 1U);
4356 * @brief Configure the DMA Burst to transfer multiple Data from the memory to the TIM peripheral
4357 * @param htim TIM handle
4358 * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
4359 * This parameter can be one of the following values:
4360 * @arg TIM_DMABASE_CR1
4361 * @arg TIM_DMABASE_CR2
4362 * @arg TIM_DMABASE_SMCR
4363 * @arg TIM_DMABASE_DIER
4364 * @arg TIM_DMABASE_SR
4365 * @arg TIM_DMABASE_EGR
4366 * @arg TIM_DMABASE_CCMR1
4367 * @arg TIM_DMABASE_CCMR2
4368 * @arg TIM_DMABASE_CCER
4369 * @arg TIM_DMABASE_CNT
4370 * @arg TIM_DMABASE_PSC
4371 * @arg TIM_DMABASE_ARR
4372 * @arg TIM_DMABASE_RCR
4373 * @arg TIM_DMABASE_CCR1
4374 * @arg TIM_DMABASE_CCR2
4375 * @arg TIM_DMABASE_CCR3
4376 * @arg TIM_DMABASE_CCR4
4377 * @arg TIM_DMABASE_BDTR
4378 * @arg TIM_DMABASE_CCMR3
4379 * @arg TIM_DMABASE_CCR5
4380 * @arg TIM_DMABASE_CCR6
4381 * @arg TIM_DMABASE_AF1
4382 * @arg TIM_DMABASE_AF2
4383 * @arg TIM_DMABASE_TISEL
4385 * @param BurstRequestSrc TIM DMA Request sources
4386 * This parameter can be one of the following values:
4387 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
4388 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4389 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4390 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4391 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4392 * @arg TIM_DMA_COM: TIM Commutation DMA source
4393 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4394 * @param BurstBuffer The Buffer address.
4395 * @param BurstLength DMA Burst length. This parameter can be one value
4396 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4397 * @param DataLength Data length. This parameter can be one value
4398 * between 1 and 0xFFFF.
4399 * @retval HAL status
4401 HAL_StatusTypeDef HAL_TIM_DMABurst_MultiWriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4402 uint32_t BurstRequestSrc, uint32_t *BurstBuffer,
4403 uint32_t BurstLength, uint32_t DataLength)
4405 /* Check the parameters */
4406 assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
4407 assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
4408 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4409 assert_param(IS_TIM_DMA_LENGTH(BurstLength));
4410 assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
4412 if (htim->DMABurstState == HAL_DMA_BURST_STATE_BUSY)
4414 return HAL_BUSY;
4416 else if (htim->DMABurstState == HAL_DMA_BURST_STATE_READY)
4418 if ((BurstBuffer == NULL) && (BurstLength > 0U))
4420 return HAL_ERROR;
4422 else
4424 htim->DMABurstState = HAL_DMA_BURST_STATE_BUSY;
4427 else
4429 /* nothing to do */
4431 switch (BurstRequestSrc)
4433 case TIM_DMA_UPDATE:
4435 /* Set the DMA Period elapsed callbacks */
4436 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
4437 htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
4439 /* Set the DMA error callback */
4440 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
4442 /* Enable the DMA stream */
4443 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer,
4444 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4446 return HAL_ERROR;
4448 break;
4450 case TIM_DMA_CC1:
4452 /* Set the DMA compare callbacks */
4453 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
4454 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4456 /* Set the DMA error callback */
4457 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
4459 /* Enable the DMA stream */
4460 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer,
4461 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4463 return HAL_ERROR;
4465 break;
4467 case TIM_DMA_CC2:
4469 /* Set the DMA compare callbacks */
4470 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
4471 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4473 /* Set the DMA error callback */
4474 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
4476 /* Enable the DMA stream */
4477 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer,
4478 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4480 return HAL_ERROR;
4482 break;
4484 case TIM_DMA_CC3:
4486 /* Set the DMA compare callbacks */
4487 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
4488 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4490 /* Set the DMA error callback */
4491 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
4493 /* Enable the DMA stream */
4494 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer,
4495 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4497 return HAL_ERROR;
4499 break;
4501 case TIM_DMA_CC4:
4503 /* Set the DMA compare callbacks */
4504 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
4505 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
4507 /* Set the DMA error callback */
4508 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
4510 /* Enable the DMA stream */
4511 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer,
4512 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4514 return HAL_ERROR;
4516 break;
4518 case TIM_DMA_COM:
4520 /* Set the DMA commutation callbacks */
4521 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
4522 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
4524 /* Set the DMA error callback */
4525 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
4527 /* Enable the DMA stream */
4528 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)BurstBuffer,
4529 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4531 return HAL_ERROR;
4533 break;
4535 case TIM_DMA_TRIGGER:
4537 /* Set the DMA trigger callbacks */
4538 htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
4539 htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
4541 /* Set the DMA error callback */
4542 htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
4544 /* Enable the DMA stream */
4545 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer,
4546 (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
4548 return HAL_ERROR;
4550 break;
4552 default:
4553 break;
4556 /* Configure the DMA Burst Mode */
4557 htim->Instance->DCR = (BurstBaseAddress | BurstLength);
4558 /* Enable the TIM DMA Request */
4559 __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
4561 /* Return function status */
4562 return HAL_OK;
4566 * @brief Stops the TIM DMA Burst mode
4567 * @param htim TIM handle
4568 * @param BurstRequestSrc TIM DMA Request sources to disable
4569 * @retval HAL status
4571 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
4573 HAL_StatusTypeDef status = HAL_OK;
4574 /* Check the parameters */
4575 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4577 /* Abort the DMA transfer (at least disable the DMA stream) */
4578 switch (BurstRequestSrc)
4580 case TIM_DMA_UPDATE:
4582 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
4583 break;
4585 case TIM_DMA_CC1:
4587 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
4588 break;
4590 case TIM_DMA_CC2:
4592 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
4593 break;
4595 case TIM_DMA_CC3:
4597 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
4598 break;
4600 case TIM_DMA_CC4:
4602 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
4603 break;
4605 case TIM_DMA_COM:
4607 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
4608 break;
4610 case TIM_DMA_TRIGGER:
4612 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
4613 break;
4615 default:
4616 break;
4619 if (HAL_OK == status)
4621 /* Disable the TIM Update DMA request */
4622 __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
4625 /* Change the DMA burst operation state */
4626 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
4628 /* Return function status */
4629 return status;
4633 * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
4634 * @param htim TIM handle
4635 * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
4636 * This parameter can be one of the following values:
4637 * @arg TIM_DMABASE_CR1
4638 * @arg TIM_DMABASE_CR2
4639 * @arg TIM_DMABASE_SMCR
4640 * @arg TIM_DMABASE_DIER
4641 * @arg TIM_DMABASE_SR
4642 * @arg TIM_DMABASE_EGR
4643 * @arg TIM_DMABASE_CCMR1
4644 * @arg TIM_DMABASE_CCMR2
4645 * @arg TIM_DMABASE_CCER
4646 * @arg TIM_DMABASE_CNT
4647 * @arg TIM_DMABASE_PSC
4648 * @arg TIM_DMABASE_ARR
4649 * @arg TIM_DMABASE_RCR
4650 * @arg TIM_DMABASE_CCR1
4651 * @arg TIM_DMABASE_CCR2
4652 * @arg TIM_DMABASE_CCR3
4653 * @arg TIM_DMABASE_CCR4
4654 * @arg TIM_DMABASE_BDTR
4655 * @arg TIM_DMABASE_CCMR3
4656 * @arg TIM_DMABASE_CCR5
4657 * @arg TIM_DMABASE_CCR6
4658 * @arg TIM_DMABASE_AF1
4659 * @arg TIM_DMABASE_AF2
4660 * @arg TIM_DMABASE_TISEL
4662 * @param BurstRequestSrc TIM DMA Request sources
4663 * This parameter can be one of the following values:
4664 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
4665 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4666 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4667 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4668 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4669 * @arg TIM_DMA_COM: TIM Commutation DMA source
4670 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4671 * @param BurstBuffer The Buffer address.
4672 * @param BurstLength DMA Burst length. This parameter can be one value
4673 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4674 * @note This function should be used only when BurstLength is equal to DMA data transfer length.
4675 * @retval HAL status
4677 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4678 uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
4680 return HAL_TIM_DMABurst_MultiReadStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
4681 ((BurstLength) >> 8U) + 1U);
4685 * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
4686 * @param htim TIM handle
4687 * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
4688 * This parameter can be one of the following values:
4689 * @arg TIM_DMABASE_CR1
4690 * @arg TIM_DMABASE_CR2
4691 * @arg TIM_DMABASE_SMCR
4692 * @arg TIM_DMABASE_DIER
4693 * @arg TIM_DMABASE_SR
4694 * @arg TIM_DMABASE_EGR
4695 * @arg TIM_DMABASE_CCMR1
4696 * @arg TIM_DMABASE_CCMR2
4697 * @arg TIM_DMABASE_CCER
4698 * @arg TIM_DMABASE_CNT
4699 * @arg TIM_DMABASE_PSC
4700 * @arg TIM_DMABASE_ARR
4701 * @arg TIM_DMABASE_RCR
4702 * @arg TIM_DMABASE_CCR1
4703 * @arg TIM_DMABASE_CCR2
4704 * @arg TIM_DMABASE_CCR3
4705 * @arg TIM_DMABASE_CCR4
4706 * @arg TIM_DMABASE_BDTR
4707 * @arg TIM_DMABASE_CCMR3
4708 * @arg TIM_DMABASE_CCR5
4709 * @arg TIM_DMABASE_CCR6
4710 * @arg TIM_DMABASE_AF1
4711 * @arg TIM_DMABASE_AF2
4712 * @arg TIM_DMABASE_TISEL
4714 * @param BurstRequestSrc TIM DMA Request sources
4715 * This parameter can be one of the following values:
4716 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
4717 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
4718 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
4719 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
4720 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
4721 * @arg TIM_DMA_COM: TIM Commutation DMA source
4722 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
4723 * @param BurstBuffer The Buffer address.
4724 * @param BurstLength DMA Burst length. This parameter can be one value
4725 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
4726 * @param DataLength Data length. This parameter can be one value
4727 * between 1 and 0xFFFF.
4728 * @retval HAL status
4730 HAL_StatusTypeDef HAL_TIM_DMABurst_MultiReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
4731 uint32_t BurstRequestSrc, uint32_t *BurstBuffer,
4732 uint32_t BurstLength, uint32_t DataLength)
4734 /* Check the parameters */
4735 assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
4736 assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
4737 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4738 assert_param(IS_TIM_DMA_LENGTH(BurstLength));
4739 assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
4741 if (htim->DMABurstState == HAL_DMA_BURST_STATE_BUSY)
4743 return HAL_BUSY;
4745 else if (htim->DMABurstState == HAL_DMA_BURST_STATE_READY)
4747 if ((BurstBuffer == NULL) && (BurstLength > 0U))
4749 return HAL_ERROR;
4751 else
4753 htim->DMABurstState = HAL_DMA_BURST_STATE_BUSY;
4756 else
4758 /* nothing to do */
4760 switch (BurstRequestSrc)
4762 case TIM_DMA_UPDATE:
4764 /* Set the DMA Period elapsed callbacks */
4765 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
4766 htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
4768 /* Set the DMA error callback */
4769 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
4771 /* Enable the DMA stream */
4772 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4773 DataLength) != HAL_OK)
4775 return HAL_ERROR;
4777 break;
4779 case TIM_DMA_CC1:
4781 /* Set the DMA capture callbacks */
4782 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
4783 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
4785 /* Set the DMA error callback */
4786 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
4788 /* Enable the DMA stream */
4789 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4790 DataLength) != HAL_OK)
4792 return HAL_ERROR;
4794 break;
4796 case TIM_DMA_CC2:
4798 /* Set the DMA capture callbacks */
4799 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
4800 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
4802 /* Set the DMA error callback */
4803 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
4805 /* Enable the DMA stream */
4806 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4807 DataLength) != HAL_OK)
4809 return HAL_ERROR;
4811 break;
4813 case TIM_DMA_CC3:
4815 /* Set the DMA capture callbacks */
4816 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
4817 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
4819 /* Set the DMA error callback */
4820 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
4822 /* Enable the DMA stream */
4823 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4824 DataLength) != HAL_OK)
4826 return HAL_ERROR;
4828 break;
4830 case TIM_DMA_CC4:
4832 /* Set the DMA capture callbacks */
4833 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
4834 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
4836 /* Set the DMA error callback */
4837 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
4839 /* Enable the DMA stream */
4840 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4841 DataLength) != HAL_OK)
4843 return HAL_ERROR;
4845 break;
4847 case TIM_DMA_COM:
4849 /* Set the DMA commutation callbacks */
4850 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
4851 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
4853 /* Set the DMA error callback */
4854 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
4856 /* Enable the DMA stream */
4857 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4858 DataLength) != HAL_OK)
4860 return HAL_ERROR;
4862 break;
4864 case TIM_DMA_TRIGGER:
4866 /* Set the DMA trigger callbacks */
4867 htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
4868 htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
4870 /* Set the DMA error callback */
4871 htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
4873 /* Enable the DMA stream */
4874 if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
4875 DataLength) != HAL_OK)
4877 return HAL_ERROR;
4879 break;
4881 default:
4882 break;
4885 /* Configure the DMA Burst Mode */
4886 htim->Instance->DCR = (BurstBaseAddress | BurstLength);
4888 /* Enable the TIM DMA Request */
4889 __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
4891 /* Return function status */
4892 return HAL_OK;
4896 * @brief Stop the DMA burst reading
4897 * @param htim TIM handle
4898 * @param BurstRequestSrc TIM DMA Request sources to disable.
4899 * @retval HAL status
4901 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
4903 HAL_StatusTypeDef status = HAL_OK;
4904 /* Check the parameters */
4905 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
4907 /* Abort the DMA transfer (at least disable the DMA stream) */
4908 switch (BurstRequestSrc)
4910 case TIM_DMA_UPDATE:
4912 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
4913 break;
4915 case TIM_DMA_CC1:
4917 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
4918 break;
4920 case TIM_DMA_CC2:
4922 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
4923 break;
4925 case TIM_DMA_CC3:
4927 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
4928 break;
4930 case TIM_DMA_CC4:
4932 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
4933 break;
4935 case TIM_DMA_COM:
4937 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
4938 break;
4940 case TIM_DMA_TRIGGER:
4942 status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
4943 break;
4945 default:
4946 break;
4949 if (HAL_OK == status)
4951 /* Disable the TIM Update DMA request */
4952 __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
4955 /* Change the DMA burst operation state */
4956 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
4958 /* Return function status */
4959 return status;
4963 * @brief Generate a software event
4964 * @param htim TIM handle
4965 * @param EventSource specifies the event source.
4966 * This parameter can be one of the following values:
4967 * @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
4968 * @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
4969 * @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
4970 * @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
4971 * @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
4972 * @arg TIM_EVENTSOURCE_COM: Timer COM event source
4973 * @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
4974 * @arg TIM_EVENTSOURCE_BREAK: Timer Break event source
4975 * @arg TIM_EVENTSOURCE_BREAK2: Timer Break2 event source
4976 * @note Basic timers can only generate an update event.
4977 * @note TIM_EVENTSOURCE_COM is relevant only with advanced timer instances.
4978 * @note TIM_EVENTSOURCE_BREAK and TIM_EVENTSOURCE_BREAK2 are relevant
4979 * only for timer instances supporting break input(s).
4980 * @retval HAL status
4983 HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
4985 /* Check the parameters */
4986 assert_param(IS_TIM_INSTANCE(htim->Instance));
4987 assert_param(IS_TIM_EVENT_SOURCE(EventSource));
4989 /* Process Locked */
4990 __HAL_LOCK(htim);
4992 /* Change the TIM state */
4993 htim->State = HAL_TIM_STATE_BUSY;
4995 /* Set the event sources */
4996 htim->Instance->EGR = EventSource;
4998 /* Change the TIM state */
4999 htim->State = HAL_TIM_STATE_READY;
5001 __HAL_UNLOCK(htim);
5003 /* Return function status */
5004 return HAL_OK;
5008 * @brief Configures the OCRef clear feature
5009 * @param htim TIM handle
5010 * @param sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
5011 * contains the OCREF clear feature and parameters for the TIM peripheral.
5012 * @param Channel specifies the TIM Channel
5013 * This parameter can be one of the following values:
5014 * @arg TIM_CHANNEL_1: TIM Channel 1
5015 * @arg TIM_CHANNEL_2: TIM Channel 2
5016 * @arg TIM_CHANNEL_3: TIM Channel 3
5017 * @arg TIM_CHANNEL_4: TIM Channel 4
5018 * @arg TIM_CHANNEL_5: TIM Channel 5
5019 * @arg TIM_CHANNEL_6: TIM Channel 6
5020 * @retval HAL status
5022 HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
5023 TIM_ClearInputConfigTypeDef *sClearInputConfig,
5024 uint32_t Channel)
5026 /* Check the parameters */
5027 assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
5028 assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
5030 /* Process Locked */
5031 __HAL_LOCK(htim);
5033 htim->State = HAL_TIM_STATE_BUSY;
5035 switch (sClearInputConfig->ClearInputSource)
5037 case TIM_CLEARINPUTSOURCE_NONE:
5039 /* Clear the OCREF clear selection bit and the the ETR Bits */
5040 CLEAR_BIT(htim->Instance->SMCR, (TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP));
5041 break;
5044 case TIM_CLEARINPUTSOURCE_ETR:
5046 /* Check the parameters */
5047 assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
5048 assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
5049 assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
5051 /* When OCRef clear feature is used with ETR source, ETR prescaler must be off */
5052 if (sClearInputConfig->ClearInputPrescaler != TIM_CLEARINPUTPRESCALER_DIV1)
5054 htim->State = HAL_TIM_STATE_READY;
5055 __HAL_UNLOCK(htim);
5056 return HAL_ERROR;
5059 TIM_ETR_SetConfig(htim->Instance,
5060 sClearInputConfig->ClearInputPrescaler,
5061 sClearInputConfig->ClearInputPolarity,
5062 sClearInputConfig->ClearInputFilter);
5063 break;
5066 default:
5067 break;
5070 switch (Channel)
5072 case TIM_CHANNEL_1:
5074 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5076 /* Enable the OCREF clear feature for Channel 1 */
5077 SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
5079 else
5081 /* Disable the OCREF clear feature for Channel 1 */
5082 CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
5084 break;
5086 case TIM_CHANNEL_2:
5088 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5090 /* Enable the OCREF clear feature for Channel 2 */
5091 SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
5093 else
5095 /* Disable the OCREF clear feature for Channel 2 */
5096 CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
5098 break;
5100 case TIM_CHANNEL_3:
5102 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5104 /* Enable the OCREF clear feature for Channel 3 */
5105 SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
5107 else
5109 /* Disable the OCREF clear feature for Channel 3 */
5110 CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
5112 break;
5114 case TIM_CHANNEL_4:
5116 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5118 /* Enable the OCREF clear feature for Channel 4 */
5119 SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
5121 else
5123 /* Disable the OCREF clear feature for Channel 4 */
5124 CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
5126 break;
5128 case TIM_CHANNEL_5:
5130 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5132 /* Enable the OCREF clear feature for Channel 5 */
5133 SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
5135 else
5137 /* Disable the OCREF clear feature for Channel 5 */
5138 CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
5140 break;
5142 case TIM_CHANNEL_6:
5144 if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
5146 /* Enable the OCREF clear feature for Channel 6 */
5147 SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
5149 else
5151 /* Disable the OCREF clear feature for Channel 6 */
5152 CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
5154 break;
5156 default:
5157 break;
5160 htim->State = HAL_TIM_STATE_READY;
5162 __HAL_UNLOCK(htim);
5164 return HAL_OK;
5168 * @brief Configures the clock source to be used
5169 * @param htim TIM handle
5170 * @param sClockSourceConfig pointer to a TIM_ClockConfigTypeDef structure that
5171 * contains the clock source information for the TIM peripheral.
5172 * @retval HAL status
5174 HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, TIM_ClockConfigTypeDef *sClockSourceConfig)
5176 uint32_t tmpsmcr;
5178 /* Process Locked */
5179 __HAL_LOCK(htim);
5181 htim->State = HAL_TIM_STATE_BUSY;
5183 /* Check the parameters */
5184 assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
5186 /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
5187 tmpsmcr = htim->Instance->SMCR;
5188 tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
5189 tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
5190 htim->Instance->SMCR = tmpsmcr;
5192 switch (sClockSourceConfig->ClockSource)
5194 case TIM_CLOCKSOURCE_INTERNAL:
5196 assert_param(IS_TIM_INSTANCE(htim->Instance));
5197 break;
5200 case TIM_CLOCKSOURCE_ETRMODE1:
5202 /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
5203 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
5205 /* Check ETR input conditioning related parameters */
5206 assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
5207 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5208 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5210 /* Configure the ETR Clock source */
5211 TIM_ETR_SetConfig(htim->Instance,
5212 sClockSourceConfig->ClockPrescaler,
5213 sClockSourceConfig->ClockPolarity,
5214 sClockSourceConfig->ClockFilter);
5216 /* Select the External clock mode1 and the ETRF trigger */
5217 tmpsmcr = htim->Instance->SMCR;
5218 tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
5219 /* Write to TIMx SMCR */
5220 htim->Instance->SMCR = tmpsmcr;
5221 break;
5224 case TIM_CLOCKSOURCE_ETRMODE2:
5226 /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
5227 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
5229 /* Check ETR input conditioning related parameters */
5230 assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
5231 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5232 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5234 /* Configure the ETR Clock source */
5235 TIM_ETR_SetConfig(htim->Instance,
5236 sClockSourceConfig->ClockPrescaler,
5237 sClockSourceConfig->ClockPolarity,
5238 sClockSourceConfig->ClockFilter);
5239 /* Enable the External clock mode2 */
5240 htim->Instance->SMCR |= TIM_SMCR_ECE;
5241 break;
5244 case TIM_CLOCKSOURCE_TI1:
5246 /* Check whether or not the timer instance supports external clock mode 1 */
5247 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
5249 /* Check TI1 input conditioning related parameters */
5250 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5251 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5253 TIM_TI1_ConfigInputStage(htim->Instance,
5254 sClockSourceConfig->ClockPolarity,
5255 sClockSourceConfig->ClockFilter);
5256 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
5257 break;
5260 case TIM_CLOCKSOURCE_TI2:
5262 /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
5263 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
5265 /* Check TI2 input conditioning related parameters */
5266 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5267 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5269 TIM_TI2_ConfigInputStage(htim->Instance,
5270 sClockSourceConfig->ClockPolarity,
5271 sClockSourceConfig->ClockFilter);
5272 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
5273 break;
5276 case TIM_CLOCKSOURCE_TI1ED:
5278 /* Check whether or not the timer instance supports external clock mode 1 */
5279 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
5281 /* Check TI1 input conditioning related parameters */
5282 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
5283 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
5285 TIM_TI1_ConfigInputStage(htim->Instance,
5286 sClockSourceConfig->ClockPolarity,
5287 sClockSourceConfig->ClockFilter);
5288 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
5289 break;
5292 case TIM_CLOCKSOURCE_ITR0:
5293 case TIM_CLOCKSOURCE_ITR1:
5294 case TIM_CLOCKSOURCE_ITR2:
5295 case TIM_CLOCKSOURCE_ITR3:
5296 case TIM_CLOCKSOURCE_ITR4:
5297 case TIM_CLOCKSOURCE_ITR5:
5298 case TIM_CLOCKSOURCE_ITR6:
5299 case TIM_CLOCKSOURCE_ITR7:
5300 case TIM_CLOCKSOURCE_ITR8:
5302 /* Check whether or not the timer instance supports internal trigger input */
5303 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
5305 TIM_ITRx_SetConfig(htim->Instance, sClockSourceConfig->ClockSource);
5306 break;
5309 default:
5310 break;
5312 htim->State = HAL_TIM_STATE_READY;
5314 __HAL_UNLOCK(htim);
5316 return HAL_OK;
5320 * @brief Selects the signal connected to the TI1 input: direct from CH1_input
5321 * or a XOR combination between CH1_input, CH2_input & CH3_input
5322 * @param htim TIM handle.
5323 * @param TI1_Selection Indicate whether or not channel 1 is connected to the
5324 * output of a XOR gate.
5325 * This parameter can be one of the following values:
5326 * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
5327 * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
5328 * pins are connected to the TI1 input (XOR combination)
5329 * @retval HAL status
5331 HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
5333 uint32_t tmpcr2;
5335 /* Check the parameters */
5336 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
5337 assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
5339 /* Get the TIMx CR2 register value */
5340 tmpcr2 = htim->Instance->CR2;
5342 /* Reset the TI1 selection */
5343 tmpcr2 &= ~TIM_CR2_TI1S;
5345 /* Set the TI1 selection */
5346 tmpcr2 |= TI1_Selection;
5348 /* Write to TIMxCR2 */
5349 htim->Instance->CR2 = tmpcr2;
5351 return HAL_OK;
5355 * @brief Configures the TIM in Slave mode
5356 * @param htim TIM handle.
5357 * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
5358 * contains the selected trigger (internal trigger input, filtered
5359 * timer input or external trigger input) and the Slave mode
5360 * (Disable, Reset, Gated, Trigger, External clock mode 1).
5361 * @retval HAL status
5363 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro(TIM_HandleTypeDef *htim, TIM_SlaveConfigTypeDef *sSlaveConfig)
5365 /* Check the parameters */
5366 assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
5367 assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
5368 assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
5370 __HAL_LOCK(htim);
5372 htim->State = HAL_TIM_STATE_BUSY;
5374 if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
5376 htim->State = HAL_TIM_STATE_READY;
5377 __HAL_UNLOCK(htim);
5378 return HAL_ERROR;
5381 /* Disable Trigger Interrupt */
5382 __HAL_TIM_DISABLE_IT(htim, TIM_IT_TRIGGER);
5384 /* Disable Trigger DMA request */
5385 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
5387 htim->State = HAL_TIM_STATE_READY;
5389 __HAL_UNLOCK(htim);
5391 return HAL_OK;
5395 * @brief Configures the TIM in Slave mode in interrupt mode
5396 * @param htim TIM handle.
5397 * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
5398 * contains the selected trigger (internal trigger input, filtered
5399 * timer input or external trigger input) and the Slave mode
5400 * (Disable, Reset, Gated, Trigger, External clock mode 1).
5401 * @retval HAL status
5403 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro_IT(TIM_HandleTypeDef *htim,
5404 TIM_SlaveConfigTypeDef *sSlaveConfig)
5406 /* Check the parameters */
5407 assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
5408 assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
5409 assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
5411 __HAL_LOCK(htim);
5413 htim->State = HAL_TIM_STATE_BUSY;
5415 if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
5417 htim->State = HAL_TIM_STATE_READY;
5418 __HAL_UNLOCK(htim);
5419 return HAL_ERROR;
5422 /* Enable Trigger Interrupt */
5423 __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);
5425 /* Disable Trigger DMA request */
5426 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
5428 htim->State = HAL_TIM_STATE_READY;
5430 __HAL_UNLOCK(htim);
5432 return HAL_OK;
5436 * @brief Read the captured value from Capture Compare unit
5437 * @param htim TIM handle.
5438 * @param Channel TIM Channels to be enabled
5439 * This parameter can be one of the following values:
5440 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
5441 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
5442 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
5443 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
5444 * @retval Captured value
5446 uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)
5448 uint32_t tmpreg = 0U;
5450 switch (Channel)
5452 case TIM_CHANNEL_1:
5454 /* Check the parameters */
5455 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
5457 /* Return the capture 1 value */
5458 tmpreg = htim->Instance->CCR1;
5460 break;
5462 case TIM_CHANNEL_2:
5464 /* Check the parameters */
5465 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
5467 /* Return the capture 2 value */
5468 tmpreg = htim->Instance->CCR2;
5470 break;
5473 case TIM_CHANNEL_3:
5475 /* Check the parameters */
5476 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
5478 /* Return the capture 3 value */
5479 tmpreg = htim->Instance->CCR3;
5481 break;
5484 case TIM_CHANNEL_4:
5486 /* Check the parameters */
5487 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
5489 /* Return the capture 4 value */
5490 tmpreg = htim->Instance->CCR4;
5492 break;
5495 default:
5496 break;
5499 return tmpreg;
5503 * @}
5506 /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
5507 * @brief TIM Callbacks functions
5509 @verbatim
5510 ==============================================================================
5511 ##### TIM Callbacks functions #####
5512 ==============================================================================
5513 [..]
5514 This section provides TIM callback functions:
5515 (+) TIM Period elapsed callback
5516 (+) TIM Output Compare callback
5517 (+) TIM Input capture callback
5518 (+) TIM Trigger callback
5519 (+) TIM Error callback
5521 @endverbatim
5522 * @{
5526 * @brief Period elapsed callback in non-blocking mode
5527 * @param htim TIM handle
5528 * @retval None
5530 __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
5532 /* Prevent unused argument(s) compilation warning */
5533 UNUSED(htim);
5535 /* NOTE : This function should not be modified, when the callback is needed,
5536 the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
5541 * @brief Period elapsed half complete callback in non-blocking mode
5542 * @param htim TIM handle
5543 * @retval None
5545 __weak void HAL_TIM_PeriodElapsedHalfCpltCallback(TIM_HandleTypeDef *htim)
5547 /* Prevent unused argument(s) compilation warning */
5548 UNUSED(htim);
5550 /* NOTE : This function should not be modified, when the callback is needed,
5551 the HAL_TIM_PeriodElapsedHalfCpltCallback could be implemented in the user file
5556 * @brief Output Compare callback in non-blocking mode
5557 * @param htim TIM OC handle
5558 * @retval None
5560 __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
5562 /* Prevent unused argument(s) compilation warning */
5563 UNUSED(htim);
5565 /* NOTE : This function should not be modified, when the callback is needed,
5566 the HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
5571 * @brief Input Capture callback in non-blocking mode
5572 * @param htim TIM IC handle
5573 * @retval None
5575 __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
5577 /* Prevent unused argument(s) compilation warning */
5578 UNUSED(htim);
5580 /* NOTE : This function should not be modified, when the callback is needed,
5581 the HAL_TIM_IC_CaptureCallback could be implemented in the user file
5586 * @brief Input Capture half complete callback in non-blocking mode
5587 * @param htim TIM IC handle
5588 * @retval None
5590 __weak void HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef *htim)
5592 /* Prevent unused argument(s) compilation warning */
5593 UNUSED(htim);
5595 /* NOTE : This function should not be modified, when the callback is needed,
5596 the HAL_TIM_IC_CaptureHalfCpltCallback could be implemented in the user file
5601 * @brief PWM Pulse finished callback in non-blocking mode
5602 * @param htim TIM handle
5603 * @retval None
5605 __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
5607 /* Prevent unused argument(s) compilation warning */
5608 UNUSED(htim);
5610 /* NOTE : This function should not be modified, when the callback is needed,
5611 the HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
5616 * @brief PWM Pulse finished half complete callback in non-blocking mode
5617 * @param htim TIM handle
5618 * @retval None
5620 __weak void HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef *htim)
5622 /* Prevent unused argument(s) compilation warning */
5623 UNUSED(htim);
5625 /* NOTE : This function should not be modified, when the callback is needed,
5626 the HAL_TIM_PWM_PulseFinishedHalfCpltCallback could be implemented in the user file
5631 * @brief Hall Trigger detection callback in non-blocking mode
5632 * @param htim TIM handle
5633 * @retval None
5635 __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
5637 /* Prevent unused argument(s) compilation warning */
5638 UNUSED(htim);
5640 /* NOTE : This function should not be modified, when the callback is needed,
5641 the HAL_TIM_TriggerCallback could be implemented in the user file
5646 * @brief Hall Trigger detection half complete callback in non-blocking mode
5647 * @param htim TIM handle
5648 * @retval None
5650 __weak void HAL_TIM_TriggerHalfCpltCallback(TIM_HandleTypeDef *htim)
5652 /* Prevent unused argument(s) compilation warning */
5653 UNUSED(htim);
5655 /* NOTE : This function should not be modified, when the callback is needed,
5656 the HAL_TIM_TriggerHalfCpltCallback could be implemented in the user file
5661 * @brief Timer error callback in non-blocking mode
5662 * @param htim TIM handle
5663 * @retval None
5665 __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
5667 /* Prevent unused argument(s) compilation warning */
5668 UNUSED(htim);
5670 /* NOTE : This function should not be modified, when the callback is needed,
5671 the HAL_TIM_ErrorCallback could be implemented in the user file
5675 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
5677 * @brief Register a User TIM callback to be used instead of the weak predefined callback
5678 * @param htim tim handle
5679 * @param CallbackID ID of the callback to be registered
5680 * This parameter can be one of the following values:
5681 * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
5682 * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
5683 * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
5684 * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
5685 * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
5686 * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
5687 * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
5688 * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
5689 * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
5690 * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
5691 * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
5692 * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
5693 * @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
5694 * @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
5695 * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
5696 * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
5697 * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
5698 * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
5699 * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
5700 * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
5701 * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
5702 * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
5703 * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
5704 * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
5705 * @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
5706 * @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
5707 * @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
5708 * @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
5709 * @param pCallback pointer to the callback function
5710 * @retval status
5712 HAL_StatusTypeDef HAL_TIM_RegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID,
5713 pTIM_CallbackTypeDef pCallback)
5715 HAL_StatusTypeDef status = HAL_OK;
5717 if (pCallback == NULL)
5719 return HAL_ERROR;
5721 /* Process locked */
5722 __HAL_LOCK(htim);
5724 if (htim->State == HAL_TIM_STATE_READY)
5726 switch (CallbackID)
5728 case HAL_TIM_BASE_MSPINIT_CB_ID :
5729 htim->Base_MspInitCallback = pCallback;
5730 break;
5732 case HAL_TIM_BASE_MSPDEINIT_CB_ID :
5733 htim->Base_MspDeInitCallback = pCallback;
5734 break;
5736 case HAL_TIM_IC_MSPINIT_CB_ID :
5737 htim->IC_MspInitCallback = pCallback;
5738 break;
5740 case HAL_TIM_IC_MSPDEINIT_CB_ID :
5741 htim->IC_MspDeInitCallback = pCallback;
5742 break;
5744 case HAL_TIM_OC_MSPINIT_CB_ID :
5745 htim->OC_MspInitCallback = pCallback;
5746 break;
5748 case HAL_TIM_OC_MSPDEINIT_CB_ID :
5749 htim->OC_MspDeInitCallback = pCallback;
5750 break;
5752 case HAL_TIM_PWM_MSPINIT_CB_ID :
5753 htim->PWM_MspInitCallback = pCallback;
5754 break;
5756 case HAL_TIM_PWM_MSPDEINIT_CB_ID :
5757 htim->PWM_MspDeInitCallback = pCallback;
5758 break;
5760 case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
5761 htim->OnePulse_MspInitCallback = pCallback;
5762 break;
5764 case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
5765 htim->OnePulse_MspDeInitCallback = pCallback;
5766 break;
5768 case HAL_TIM_ENCODER_MSPINIT_CB_ID :
5769 htim->Encoder_MspInitCallback = pCallback;
5770 break;
5772 case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
5773 htim->Encoder_MspDeInitCallback = pCallback;
5774 break;
5776 case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
5777 htim->HallSensor_MspInitCallback = pCallback;
5778 break;
5780 case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
5781 htim->HallSensor_MspDeInitCallback = pCallback;
5782 break;
5784 case HAL_TIM_PERIOD_ELAPSED_CB_ID :
5785 htim->PeriodElapsedCallback = pCallback;
5786 break;
5788 case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
5789 htim->PeriodElapsedHalfCpltCallback = pCallback;
5790 break;
5792 case HAL_TIM_TRIGGER_CB_ID :
5793 htim->TriggerCallback = pCallback;
5794 break;
5796 case HAL_TIM_TRIGGER_HALF_CB_ID :
5797 htim->TriggerHalfCpltCallback = pCallback;
5798 break;
5800 case HAL_TIM_IC_CAPTURE_CB_ID :
5801 htim->IC_CaptureCallback = pCallback;
5802 break;
5804 case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
5805 htim->IC_CaptureHalfCpltCallback = pCallback;
5806 break;
5808 case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
5809 htim->OC_DelayElapsedCallback = pCallback;
5810 break;
5812 case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
5813 htim->PWM_PulseFinishedCallback = pCallback;
5814 break;
5816 case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
5817 htim->PWM_PulseFinishedHalfCpltCallback = pCallback;
5818 break;
5820 case HAL_TIM_ERROR_CB_ID :
5821 htim->ErrorCallback = pCallback;
5822 break;
5824 case HAL_TIM_COMMUTATION_CB_ID :
5825 htim->CommutationCallback = pCallback;
5826 break;
5828 case HAL_TIM_COMMUTATION_HALF_CB_ID :
5829 htim->CommutationHalfCpltCallback = pCallback;
5830 break;
5832 case HAL_TIM_BREAK_CB_ID :
5833 htim->BreakCallback = pCallback;
5834 break;
5836 case HAL_TIM_BREAK2_CB_ID :
5837 htim->Break2Callback = pCallback;
5838 break;
5840 default :
5841 /* Return error status */
5842 status = HAL_ERROR;
5843 break;
5846 else if (htim->State == HAL_TIM_STATE_RESET)
5848 switch (CallbackID)
5850 case HAL_TIM_BASE_MSPINIT_CB_ID :
5851 htim->Base_MspInitCallback = pCallback;
5852 break;
5854 case HAL_TIM_BASE_MSPDEINIT_CB_ID :
5855 htim->Base_MspDeInitCallback = pCallback;
5856 break;
5858 case HAL_TIM_IC_MSPINIT_CB_ID :
5859 htim->IC_MspInitCallback = pCallback;
5860 break;
5862 case HAL_TIM_IC_MSPDEINIT_CB_ID :
5863 htim->IC_MspDeInitCallback = pCallback;
5864 break;
5866 case HAL_TIM_OC_MSPINIT_CB_ID :
5867 htim->OC_MspInitCallback = pCallback;
5868 break;
5870 case HAL_TIM_OC_MSPDEINIT_CB_ID :
5871 htim->OC_MspDeInitCallback = pCallback;
5872 break;
5874 case HAL_TIM_PWM_MSPINIT_CB_ID :
5875 htim->PWM_MspInitCallback = pCallback;
5876 break;
5878 case HAL_TIM_PWM_MSPDEINIT_CB_ID :
5879 htim->PWM_MspDeInitCallback = pCallback;
5880 break;
5882 case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
5883 htim->OnePulse_MspInitCallback = pCallback;
5884 break;
5886 case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
5887 htim->OnePulse_MspDeInitCallback = pCallback;
5888 break;
5890 case HAL_TIM_ENCODER_MSPINIT_CB_ID :
5891 htim->Encoder_MspInitCallback = pCallback;
5892 break;
5894 case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
5895 htim->Encoder_MspDeInitCallback = pCallback;
5896 break;
5898 case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
5899 htim->HallSensor_MspInitCallback = pCallback;
5900 break;
5902 case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
5903 htim->HallSensor_MspDeInitCallback = pCallback;
5904 break;
5906 default :
5907 /* Return error status */
5908 status = HAL_ERROR;
5909 break;
5912 else
5914 /* Return error status */
5915 status = HAL_ERROR;
5918 /* Release Lock */
5919 __HAL_UNLOCK(htim);
5921 return status;
5925 * @brief Unregister a TIM callback
5926 * TIM callback is redirected to the weak predefined callback
5927 * @param htim tim handle
5928 * @param CallbackID ID of the callback to be unregistered
5929 * This parameter can be one of the following values:
5930 * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
5931 * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
5932 * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
5933 * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
5934 * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
5935 * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
5936 * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
5937 * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
5938 * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
5939 * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
5940 * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
5941 * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
5942 * @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
5943 * @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
5944 * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
5945 * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
5946 * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
5947 * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
5948 * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
5949 * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
5950 * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
5951 * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
5952 * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
5953 * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
5954 * @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
5955 * @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
5956 * @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
5957 * @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
5958 * @retval status
5960 HAL_StatusTypeDef HAL_TIM_UnRegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID)
5962 HAL_StatusTypeDef status = HAL_OK;
5964 /* Process locked */
5965 __HAL_LOCK(htim);
5967 if (htim->State == HAL_TIM_STATE_READY)
5969 switch (CallbackID)
5971 case HAL_TIM_BASE_MSPINIT_CB_ID :
5972 htim->Base_MspInitCallback = HAL_TIM_Base_MspInit; /* Legacy weak Base MspInit Callback */
5973 break;
5975 case HAL_TIM_BASE_MSPDEINIT_CB_ID :
5976 htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit; /* Legacy weak Base Msp DeInit Callback */
5977 break;
5979 case HAL_TIM_IC_MSPINIT_CB_ID :
5980 htim->IC_MspInitCallback = HAL_TIM_IC_MspInit; /* Legacy weak IC Msp Init Callback */
5981 break;
5983 case HAL_TIM_IC_MSPDEINIT_CB_ID :
5984 htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit; /* Legacy weak IC Msp DeInit Callback */
5985 break;
5987 case HAL_TIM_OC_MSPINIT_CB_ID :
5988 htim->OC_MspInitCallback = HAL_TIM_OC_MspInit; /* Legacy weak OC Msp Init Callback */
5989 break;
5991 case HAL_TIM_OC_MSPDEINIT_CB_ID :
5992 htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit; /* Legacy weak OC Msp DeInit Callback */
5993 break;
5995 case HAL_TIM_PWM_MSPINIT_CB_ID :
5996 htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit; /* Legacy weak PWM Msp Init Callback */
5997 break;
5999 case HAL_TIM_PWM_MSPDEINIT_CB_ID :
6000 htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit; /* Legacy weak PWM Msp DeInit Callback */
6001 break;
6003 case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
6004 htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit; /* Legacy weak One Pulse Msp Init Callback */
6005 break;
6007 case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
6008 htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit; /* Legacy weak One Pulse Msp DeInit Callback */
6009 break;
6011 case HAL_TIM_ENCODER_MSPINIT_CB_ID :
6012 htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit; /* Legacy weak Encoder Msp Init Callback */
6013 break;
6015 case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
6016 htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit; /* Legacy weak Encoder Msp DeInit Callback */
6017 break;
6019 case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
6020 htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit; /* Legacy weak Hall Sensor Msp Init Callback */
6021 break;
6023 case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
6024 htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit; /* Legacy weak Hall Sensor Msp DeInit Callback */
6025 break;
6027 case HAL_TIM_PERIOD_ELAPSED_CB_ID :
6028 htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback; /* Legacy weak Period Elapsed Callback */
6029 break;
6031 case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
6032 htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback; /* Legacy weak Period Elapsed half complete Callback */
6033 break;
6035 case HAL_TIM_TRIGGER_CB_ID :
6036 htim->TriggerCallback = HAL_TIM_TriggerCallback; /* Legacy weak Trigger Callback */
6037 break;
6039 case HAL_TIM_TRIGGER_HALF_CB_ID :
6040 htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback; /* Legacy weak Trigger half complete Callback */
6041 break;
6043 case HAL_TIM_IC_CAPTURE_CB_ID :
6044 htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback; /* Legacy weak IC Capture Callback */
6045 break;
6047 case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
6048 htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback; /* Legacy weak IC Capture half complete Callback */
6049 break;
6051 case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
6052 htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback; /* Legacy weak OC Delay Elapsed Callback */
6053 break;
6055 case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
6056 htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback; /* Legacy weak PWM Pulse Finished Callback */
6057 break;
6059 case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
6060 htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback; /* Legacy weak PWM Pulse Finished half complete Callback */
6061 break;
6063 case HAL_TIM_ERROR_CB_ID :
6064 htim->ErrorCallback = HAL_TIM_ErrorCallback; /* Legacy weak Error Callback */
6065 break;
6067 case HAL_TIM_COMMUTATION_CB_ID :
6068 htim->CommutationCallback = HAL_TIMEx_CommutCallback; /* Legacy weak Commutation Callback */
6069 break;
6071 case HAL_TIM_COMMUTATION_HALF_CB_ID :
6072 htim->CommutationHalfCpltCallback = HAL_TIMEx_CommutHalfCpltCallback; /* Legacy weak Commutation half complete Callback */
6073 break;
6075 case HAL_TIM_BREAK_CB_ID :
6076 htim->BreakCallback = HAL_TIMEx_BreakCallback; /* Legacy weak Break Callback */
6077 break;
6079 case HAL_TIM_BREAK2_CB_ID :
6080 htim->Break2Callback = HAL_TIMEx_Break2Callback; /* Legacy weak Break2 Callback */
6081 break;
6083 default :
6084 /* Return error status */
6085 status = HAL_ERROR;
6086 break;
6089 else if (htim->State == HAL_TIM_STATE_RESET)
6091 switch (CallbackID)
6093 case HAL_TIM_BASE_MSPINIT_CB_ID :
6094 htim->Base_MspInitCallback = HAL_TIM_Base_MspInit; /* Legacy weak Base MspInit Callback */
6095 break;
6097 case HAL_TIM_BASE_MSPDEINIT_CB_ID :
6098 htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit; /* Legacy weak Base Msp DeInit Callback */
6099 break;
6101 case HAL_TIM_IC_MSPINIT_CB_ID :
6102 htim->IC_MspInitCallback = HAL_TIM_IC_MspInit; /* Legacy weak IC Msp Init Callback */
6103 break;
6105 case HAL_TIM_IC_MSPDEINIT_CB_ID :
6106 htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit; /* Legacy weak IC Msp DeInit Callback */
6107 break;
6109 case HAL_TIM_OC_MSPINIT_CB_ID :
6110 htim->OC_MspInitCallback = HAL_TIM_OC_MspInit; /* Legacy weak OC Msp Init Callback */
6111 break;
6113 case HAL_TIM_OC_MSPDEINIT_CB_ID :
6114 htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit; /* Legacy weak OC Msp DeInit Callback */
6115 break;
6117 case HAL_TIM_PWM_MSPINIT_CB_ID :
6118 htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit; /* Legacy weak PWM Msp Init Callback */
6119 break;
6121 case HAL_TIM_PWM_MSPDEINIT_CB_ID :
6122 htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit; /* Legacy weak PWM Msp DeInit Callback */
6123 break;
6125 case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
6126 htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit; /* Legacy weak One Pulse Msp Init Callback */
6127 break;
6129 case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
6130 htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit; /* Legacy weak One Pulse Msp DeInit Callback */
6131 break;
6133 case HAL_TIM_ENCODER_MSPINIT_CB_ID :
6134 htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit; /* Legacy weak Encoder Msp Init Callback */
6135 break;
6137 case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
6138 htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit; /* Legacy weak Encoder Msp DeInit Callback */
6139 break;
6141 case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
6142 htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit; /* Legacy weak Hall Sensor Msp Init Callback */
6143 break;
6145 case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
6146 htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit; /* Legacy weak Hall Sensor Msp DeInit Callback */
6147 break;
6149 default :
6150 /* Return error status */
6151 status = HAL_ERROR;
6152 break;
6155 else
6157 /* Return error status */
6158 status = HAL_ERROR;
6161 /* Release Lock */
6162 __HAL_UNLOCK(htim);
6164 return status;
6166 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6169 * @}
6172 /** @defgroup TIM_Exported_Functions_Group10 TIM Peripheral State functions
6173 * @brief TIM Peripheral State functions
6175 @verbatim
6176 ==============================================================================
6177 ##### Peripheral State functions #####
6178 ==============================================================================
6179 [..]
6180 This subsection permits to get in run-time the status of the peripheral
6181 and the data flow.
6183 @endverbatim
6184 * @{
6188 * @brief Return the TIM Base handle state.
6189 * @param htim TIM Base handle
6190 * @retval HAL state
6192 HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(TIM_HandleTypeDef *htim)
6194 return htim->State;
6198 * @brief Return the TIM OC handle state.
6199 * @param htim TIM Output Compare handle
6200 * @retval HAL state
6202 HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(TIM_HandleTypeDef *htim)
6204 return htim->State;
6208 * @brief Return the TIM PWM handle state.
6209 * @param htim TIM handle
6210 * @retval HAL state
6212 HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(TIM_HandleTypeDef *htim)
6214 return htim->State;
6218 * @brief Return the TIM Input Capture handle state.
6219 * @param htim TIM IC handle
6220 * @retval HAL state
6222 HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(TIM_HandleTypeDef *htim)
6224 return htim->State;
6228 * @brief Return the TIM One Pulse Mode handle state.
6229 * @param htim TIM OPM handle
6230 * @retval HAL state
6232 HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(TIM_HandleTypeDef *htim)
6234 return htim->State;
6238 * @brief Return the TIM Encoder Mode handle state.
6239 * @param htim TIM Encoder Interface handle
6240 * @retval HAL state
6242 HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(TIM_HandleTypeDef *htim)
6244 return htim->State;
6248 * @brief Return the TIM Encoder Mode handle state.
6249 * @param htim TIM handle
6250 * @retval Active channel
6252 HAL_TIM_ActiveChannel HAL_TIM_GetActiveChannel(TIM_HandleTypeDef *htim)
6254 return htim->Channel;
6258 * @brief Return actual state of the TIM channel.
6259 * @param htim TIM handle
6260 * @param Channel TIM Channel
6261 * This parameter can be one of the following values:
6262 * @arg TIM_CHANNEL_1: TIM Channel 1
6263 * @arg TIM_CHANNEL_2: TIM Channel 2
6264 * @arg TIM_CHANNEL_3: TIM Channel 3
6265 * @arg TIM_CHANNEL_4: TIM Channel 4
6266 * @arg TIM_CHANNEL_5: TIM Channel 5
6267 * @arg TIM_CHANNEL_6: TIM Channel 6
6268 * @retval TIM Channel state
6270 HAL_TIM_ChannelStateTypeDef HAL_TIM_GetChannelState(TIM_HandleTypeDef *htim, uint32_t Channel)
6272 HAL_TIM_ChannelStateTypeDef channel_state;
6274 /* Check the parameters */
6275 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
6277 channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
6279 return channel_state;
6283 * @brief Return actual state of a DMA burst operation.
6284 * @param htim TIM handle
6285 * @retval DMA burst state
6287 HAL_TIM_DMABurstStateTypeDef HAL_TIM_DMABurstState(TIM_HandleTypeDef *htim)
6289 /* Check the parameters */
6290 assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
6292 return htim->DMABurstState;
6296 * @}
6300 * @}
6303 /** @defgroup TIM_Private_Functions TIM Private Functions
6304 * @{
6308 * @brief TIM DMA error callback
6309 * @param hdma pointer to DMA handle.
6310 * @retval None
6312 void TIM_DMAError(DMA_HandleTypeDef *hdma)
6314 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6316 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6318 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6319 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
6321 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6323 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6324 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
6326 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6328 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6329 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
6331 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6333 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6334 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
6336 else
6338 htim->State = HAL_TIM_STATE_READY;
6341 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6342 htim->ErrorCallback(htim);
6343 #else
6344 HAL_TIM_ErrorCallback(htim);
6345 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6347 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6351 * @brief TIM DMA Delay Pulse complete callback.
6352 * @param hdma pointer to DMA handle.
6353 * @retval None
6355 void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
6357 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6359 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6361 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6363 if (hdma->Init.Mode == DMA_NORMAL)
6365 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
6368 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6370 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6372 if (hdma->Init.Mode == DMA_NORMAL)
6374 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
6377 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6379 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6381 if (hdma->Init.Mode == DMA_NORMAL)
6383 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
6386 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6388 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6390 if (hdma->Init.Mode == DMA_NORMAL)
6392 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
6395 else
6397 /* nothing to do */
6400 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6401 htim->PWM_PulseFinishedCallback(htim);
6402 #else
6403 HAL_TIM_PWM_PulseFinishedCallback(htim);
6404 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6406 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6410 * @brief TIM DMA Delay Pulse half complete callback.
6411 * @param hdma pointer to DMA handle.
6412 * @retval None
6414 void TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef *hdma)
6416 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6418 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6420 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6422 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6424 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6426 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6428 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6430 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6432 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6434 else
6436 /* nothing to do */
6439 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6440 htim->PWM_PulseFinishedHalfCpltCallback(htim);
6441 #else
6442 HAL_TIM_PWM_PulseFinishedHalfCpltCallback(htim);
6443 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6445 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6449 * @brief TIM DMA Capture complete callback.
6450 * @param hdma pointer to DMA handle.
6451 * @retval None
6453 void TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
6455 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6457 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6459 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6461 if (hdma->Init.Mode == DMA_NORMAL)
6463 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
6464 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
6467 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6469 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6471 if (hdma->Init.Mode == DMA_NORMAL)
6473 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
6474 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
6477 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6479 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6481 if (hdma->Init.Mode == DMA_NORMAL)
6483 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
6484 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
6487 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6489 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6491 if (hdma->Init.Mode == DMA_NORMAL)
6493 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
6494 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
6497 else
6499 /* nothing to do */
6502 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6503 htim->IC_CaptureCallback(htim);
6504 #else
6505 HAL_TIM_IC_CaptureCallback(htim);
6506 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6508 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6512 * @brief TIM DMA Capture half complete callback.
6513 * @param hdma pointer to DMA handle.
6514 * @retval None
6516 void TIM_DMACaptureHalfCplt(DMA_HandleTypeDef *hdma)
6518 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6520 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
6522 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
6524 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
6526 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
6528 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
6530 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
6532 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
6534 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
6536 else
6538 /* nothing to do */
6541 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6542 htim->IC_CaptureHalfCpltCallback(htim);
6543 #else
6544 HAL_TIM_IC_CaptureHalfCpltCallback(htim);
6545 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6547 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
6551 * @brief TIM DMA Period Elapse complete callback.
6552 * @param hdma pointer to DMA handle.
6553 * @retval None
6555 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
6557 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6559 if (htim->hdma[TIM_DMA_ID_UPDATE]->Init.Mode == DMA_NORMAL)
6561 htim->State = HAL_TIM_STATE_READY;
6564 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6565 htim->PeriodElapsedCallback(htim);
6566 #else
6567 HAL_TIM_PeriodElapsedCallback(htim);
6568 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6572 * @brief TIM DMA Period Elapse half complete callback.
6573 * @param hdma pointer to DMA handle.
6574 * @retval None
6576 static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma)
6578 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6580 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6581 htim->PeriodElapsedHalfCpltCallback(htim);
6582 #else
6583 HAL_TIM_PeriodElapsedHalfCpltCallback(htim);
6584 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6588 * @brief TIM DMA Trigger callback.
6589 * @param hdma pointer to DMA handle.
6590 * @retval None
6592 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
6594 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6596 if (htim->hdma[TIM_DMA_ID_TRIGGER]->Init.Mode == DMA_NORMAL)
6598 htim->State = HAL_TIM_STATE_READY;
6601 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6602 htim->TriggerCallback(htim);
6603 #else
6604 HAL_TIM_TriggerCallback(htim);
6605 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6609 * @brief TIM DMA Trigger half complete callback.
6610 * @param hdma pointer to DMA handle.
6611 * @retval None
6613 static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma)
6615 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
6617 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
6618 htim->TriggerHalfCpltCallback(htim);
6619 #else
6620 HAL_TIM_TriggerHalfCpltCallback(htim);
6621 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
6625 * @brief Time Base configuration
6626 * @param TIMx TIM peripheral
6627 * @param Structure TIM Base configuration structure
6628 * @retval None
6630 void TIM_Base_SetConfig(TIM_TypeDef *TIMx, TIM_Base_InitTypeDef *Structure)
6632 uint32_t tmpcr1;
6633 tmpcr1 = TIMx->CR1;
6635 /* Set TIM Time Base Unit parameters ---------------------------------------*/
6636 if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
6638 /* Select the Counter Mode */
6639 tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
6640 tmpcr1 |= Structure->CounterMode;
6643 if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
6645 /* Set the clock division */
6646 tmpcr1 &= ~TIM_CR1_CKD;
6647 tmpcr1 |= (uint32_t)Structure->ClockDivision;
6650 /* Set the auto-reload preload */
6651 MODIFY_REG(tmpcr1, TIM_CR1_ARPE, Structure->AutoReloadPreload);
6653 TIMx->CR1 = tmpcr1;
6655 /* Set the Autoreload value */
6656 TIMx->ARR = (uint32_t)Structure->Period ;
6658 /* Set the Prescaler value */
6659 TIMx->PSC = Structure->Prescaler;
6661 if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
6663 /* Set the Repetition Counter value */
6664 TIMx->RCR = Structure->RepetitionCounter;
6667 /* Generate an update event to reload the Prescaler
6668 and the repetition counter (only for advanced timer) value immediately */
6669 TIMx->EGR = TIM_EGR_UG;
6673 * @brief Timer Output Compare 1 configuration
6674 * @param TIMx to select the TIM peripheral
6675 * @param OC_Config The ouput configuration structure
6676 * @retval None
6678 static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
6680 uint32_t tmpccmrx;
6681 uint32_t tmpccer;
6682 uint32_t tmpcr2;
6684 /* Disable the Channel 1: Reset the CC1E Bit */
6685 TIMx->CCER &= ~TIM_CCER_CC1E;
6687 /* Get the TIMx CCER register value */
6688 tmpccer = TIMx->CCER;
6689 /* Get the TIMx CR2 register value */
6690 tmpcr2 = TIMx->CR2;
6692 /* Get the TIMx CCMR1 register value */
6693 tmpccmrx = TIMx->CCMR1;
6695 /* Reset the Output Compare Mode Bits */
6696 tmpccmrx &= ~TIM_CCMR1_OC1M;
6697 tmpccmrx &= ~TIM_CCMR1_CC1S;
6698 /* Select the Output Compare Mode */
6699 tmpccmrx |= OC_Config->OCMode;
6701 /* Reset the Output Polarity level */
6702 tmpccer &= ~TIM_CCER_CC1P;
6703 /* Set the Output Compare Polarity */
6704 tmpccer |= OC_Config->OCPolarity;
6706 if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_1))
6708 /* Check parameters */
6709 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
6711 /* Reset the Output N Polarity level */
6712 tmpccer &= ~TIM_CCER_CC1NP;
6713 /* Set the Output N Polarity */
6714 tmpccer |= OC_Config->OCNPolarity;
6715 /* Reset the Output N State */
6716 tmpccer &= ~TIM_CCER_CC1NE;
6719 if (IS_TIM_BREAK_INSTANCE(TIMx))
6721 /* Check parameters */
6722 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
6723 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
6725 /* Reset the Output Compare and Output Compare N IDLE State */
6726 tmpcr2 &= ~TIM_CR2_OIS1;
6727 tmpcr2 &= ~TIM_CR2_OIS1N;
6728 /* Set the Output Idle state */
6729 tmpcr2 |= OC_Config->OCIdleState;
6730 /* Set the Output N Idle state */
6731 tmpcr2 |= OC_Config->OCNIdleState;
6734 /* Write to TIMx CR2 */
6735 TIMx->CR2 = tmpcr2;
6737 /* Write to TIMx CCMR1 */
6738 TIMx->CCMR1 = tmpccmrx;
6740 /* Set the Capture Compare Register value */
6741 TIMx->CCR1 = OC_Config->Pulse;
6743 /* Write to TIMx CCER */
6744 TIMx->CCER = tmpccer;
6748 * @brief Timer Output Compare 2 configuration
6749 * @param TIMx to select the TIM peripheral
6750 * @param OC_Config The ouput configuration structure
6751 * @retval None
6753 void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
6755 uint32_t tmpccmrx;
6756 uint32_t tmpccer;
6757 uint32_t tmpcr2;
6759 /* Disable the Channel 2: Reset the CC2E Bit */
6760 TIMx->CCER &= ~TIM_CCER_CC2E;
6762 /* Get the TIMx CCER register value */
6763 tmpccer = TIMx->CCER;
6764 /* Get the TIMx CR2 register value */
6765 tmpcr2 = TIMx->CR2;
6767 /* Get the TIMx CCMR1 register value */
6768 tmpccmrx = TIMx->CCMR1;
6770 /* Reset the Output Compare mode and Capture/Compare selection Bits */
6771 tmpccmrx &= ~TIM_CCMR1_OC2M;
6772 tmpccmrx &= ~TIM_CCMR1_CC2S;
6774 /* Select the Output Compare Mode */
6775 tmpccmrx |= (OC_Config->OCMode << 8U);
6777 /* Reset the Output Polarity level */
6778 tmpccer &= ~TIM_CCER_CC2P;
6779 /* Set the Output Compare Polarity */
6780 tmpccer |= (OC_Config->OCPolarity << 4U);
6782 if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_2))
6784 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
6786 /* Reset the Output N Polarity level */
6787 tmpccer &= ~TIM_CCER_CC2NP;
6788 /* Set the Output N Polarity */
6789 tmpccer |= (OC_Config->OCNPolarity << 4U);
6790 /* Reset the Output N State */
6791 tmpccer &= ~TIM_CCER_CC2NE;
6795 if (IS_TIM_BREAK_INSTANCE(TIMx))
6797 /* Check parameters */
6798 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
6799 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
6801 /* Reset the Output Compare and Output Compare N IDLE State */
6802 tmpcr2 &= ~TIM_CR2_OIS2;
6803 tmpcr2 &= ~TIM_CR2_OIS2N;
6804 /* Set the Output Idle state */
6805 tmpcr2 |= (OC_Config->OCIdleState << 2U);
6806 /* Set the Output N Idle state */
6807 tmpcr2 |= (OC_Config->OCNIdleState << 2U);
6810 /* Write to TIMx CR2 */
6811 TIMx->CR2 = tmpcr2;
6813 /* Write to TIMx CCMR1 */
6814 TIMx->CCMR1 = tmpccmrx;
6816 /* Set the Capture Compare Register value */
6817 TIMx->CCR2 = OC_Config->Pulse;
6819 /* Write to TIMx CCER */
6820 TIMx->CCER = tmpccer;
6824 * @brief Timer Output Compare 3 configuration
6825 * @param TIMx to select the TIM peripheral
6826 * @param OC_Config The ouput configuration structure
6827 * @retval None
6829 static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
6831 uint32_t tmpccmrx;
6832 uint32_t tmpccer;
6833 uint32_t tmpcr2;
6835 /* Disable the Channel 3: Reset the CC2E Bit */
6836 TIMx->CCER &= ~TIM_CCER_CC3E;
6838 /* Get the TIMx CCER register value */
6839 tmpccer = TIMx->CCER;
6840 /* Get the TIMx CR2 register value */
6841 tmpcr2 = TIMx->CR2;
6843 /* Get the TIMx CCMR2 register value */
6844 tmpccmrx = TIMx->CCMR2;
6846 /* Reset the Output Compare mode and Capture/Compare selection Bits */
6847 tmpccmrx &= ~TIM_CCMR2_OC3M;
6848 tmpccmrx &= ~TIM_CCMR2_CC3S;
6849 /* Select the Output Compare Mode */
6850 tmpccmrx |= OC_Config->OCMode;
6852 /* Reset the Output Polarity level */
6853 tmpccer &= ~TIM_CCER_CC3P;
6854 /* Set the Output Compare Polarity */
6855 tmpccer |= (OC_Config->OCPolarity << 8U);
6857 if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_3))
6859 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
6861 /* Reset the Output N Polarity level */
6862 tmpccer &= ~TIM_CCER_CC3NP;
6863 /* Set the Output N Polarity */
6864 tmpccer |= (OC_Config->OCNPolarity << 8U);
6865 /* Reset the Output N State */
6866 tmpccer &= ~TIM_CCER_CC3NE;
6869 if (IS_TIM_BREAK_INSTANCE(TIMx))
6871 /* Check parameters */
6872 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
6873 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
6875 /* Reset the Output Compare and Output Compare N IDLE State */
6876 tmpcr2 &= ~TIM_CR2_OIS3;
6877 tmpcr2 &= ~TIM_CR2_OIS3N;
6878 /* Set the Output Idle state */
6879 tmpcr2 |= (OC_Config->OCIdleState << 4U);
6880 /* Set the Output N Idle state */
6881 tmpcr2 |= (OC_Config->OCNIdleState << 4U);
6884 /* Write to TIMx CR2 */
6885 TIMx->CR2 = tmpcr2;
6887 /* Write to TIMx CCMR2 */
6888 TIMx->CCMR2 = tmpccmrx;
6890 /* Set the Capture Compare Register value */
6891 TIMx->CCR3 = OC_Config->Pulse;
6893 /* Write to TIMx CCER */
6894 TIMx->CCER = tmpccer;
6898 * @brief Timer Output Compare 4 configuration
6899 * @param TIMx to select the TIM peripheral
6900 * @param OC_Config The ouput configuration structure
6901 * @retval None
6903 static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
6905 uint32_t tmpccmrx;
6906 uint32_t tmpccer;
6907 uint32_t tmpcr2;
6909 /* Disable the Channel 4: Reset the CC4E Bit */
6910 TIMx->CCER &= ~TIM_CCER_CC4E;
6912 /* Get the TIMx CCER register value */
6913 tmpccer = TIMx->CCER;
6914 /* Get the TIMx CR2 register value */
6915 tmpcr2 = TIMx->CR2;
6917 /* Get the TIMx CCMR2 register value */
6918 tmpccmrx = TIMx->CCMR2;
6920 /* Reset the Output Compare mode and Capture/Compare selection Bits */
6921 tmpccmrx &= ~TIM_CCMR2_OC4M;
6922 tmpccmrx &= ~TIM_CCMR2_CC4S;
6924 /* Select the Output Compare Mode */
6925 tmpccmrx |= (OC_Config->OCMode << 8U);
6927 /* Reset the Output Polarity level */
6928 tmpccer &= ~TIM_CCER_CC4P;
6929 /* Set the Output Compare Polarity */
6930 tmpccer |= (OC_Config->OCPolarity << 12U);
6932 if (IS_TIM_BREAK_INSTANCE(TIMx))
6934 /* Check parameters */
6935 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
6937 /* Reset the Output Compare IDLE State */
6938 tmpcr2 &= ~TIM_CR2_OIS4;
6940 /* Set the Output Idle state */
6941 tmpcr2 |= (OC_Config->OCIdleState << 6U);
6944 /* Write to TIMx CR2 */
6945 TIMx->CR2 = tmpcr2;
6947 /* Write to TIMx CCMR2 */
6948 TIMx->CCMR2 = tmpccmrx;
6950 /* Set the Capture Compare Register value */
6951 TIMx->CCR4 = OC_Config->Pulse;
6953 /* Write to TIMx CCER */
6954 TIMx->CCER = tmpccer;
6958 * @brief Timer Output Compare 5 configuration
6959 * @param TIMx to select the TIM peripheral
6960 * @param OC_Config The ouput configuration structure
6961 * @retval None
6963 static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx,
6964 TIM_OC_InitTypeDef *OC_Config)
6966 uint32_t tmpccmrx;
6967 uint32_t tmpccer;
6968 uint32_t tmpcr2;
6970 /* Disable the output: Reset the CCxE Bit */
6971 TIMx->CCER &= ~TIM_CCER_CC5E;
6973 /* Get the TIMx CCER register value */
6974 tmpccer = TIMx->CCER;
6975 /* Get the TIMx CR2 register value */
6976 tmpcr2 = TIMx->CR2;
6977 /* Get the TIMx CCMR1 register value */
6978 tmpccmrx = TIMx->CCMR3;
6980 /* Reset the Output Compare Mode Bits */
6981 tmpccmrx &= ~(TIM_CCMR3_OC5M);
6982 /* Select the Output Compare Mode */
6983 tmpccmrx |= OC_Config->OCMode;
6985 /* Reset the Output Polarity level */
6986 tmpccer &= ~TIM_CCER_CC5P;
6987 /* Set the Output Compare Polarity */
6988 tmpccer |= (OC_Config->OCPolarity << 16U);
6990 if (IS_TIM_BREAK_INSTANCE(TIMx))
6992 /* Reset the Output Compare IDLE State */
6993 tmpcr2 &= ~TIM_CR2_OIS5;
6994 /* Set the Output Idle state */
6995 tmpcr2 |= (OC_Config->OCIdleState << 8U);
6997 /* Write to TIMx CR2 */
6998 TIMx->CR2 = tmpcr2;
7000 /* Write to TIMx CCMR3 */
7001 TIMx->CCMR3 = tmpccmrx;
7003 /* Set the Capture Compare Register value */
7004 TIMx->CCR5 = OC_Config->Pulse;
7006 /* Write to TIMx CCER */
7007 TIMx->CCER = tmpccer;
7011 * @brief Timer Output Compare 6 configuration
7012 * @param TIMx to select the TIM peripheral
7013 * @param OC_Config The ouput configuration structure
7014 * @retval None
7016 static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx,
7017 TIM_OC_InitTypeDef *OC_Config)
7019 uint32_t tmpccmrx;
7020 uint32_t tmpccer;
7021 uint32_t tmpcr2;
7023 /* Disable the output: Reset the CCxE Bit */
7024 TIMx->CCER &= ~TIM_CCER_CC6E;
7026 /* Get the TIMx CCER register value */
7027 tmpccer = TIMx->CCER;
7028 /* Get the TIMx CR2 register value */
7029 tmpcr2 = TIMx->CR2;
7030 /* Get the TIMx CCMR1 register value */
7031 tmpccmrx = TIMx->CCMR3;
7033 /* Reset the Output Compare Mode Bits */
7034 tmpccmrx &= ~(TIM_CCMR3_OC6M);
7035 /* Select the Output Compare Mode */
7036 tmpccmrx |= (OC_Config->OCMode << 8U);
7038 /* Reset the Output Polarity level */
7039 tmpccer &= (uint32_t)~TIM_CCER_CC6P;
7040 /* Set the Output Compare Polarity */
7041 tmpccer |= (OC_Config->OCPolarity << 20U);
7043 if (IS_TIM_BREAK_INSTANCE(TIMx))
7045 /* Reset the Output Compare IDLE State */
7046 tmpcr2 &= ~TIM_CR2_OIS6;
7047 /* Set the Output Idle state */
7048 tmpcr2 |= (OC_Config->OCIdleState << 10U);
7051 /* Write to TIMx CR2 */
7052 TIMx->CR2 = tmpcr2;
7054 /* Write to TIMx CCMR3 */
7055 TIMx->CCMR3 = tmpccmrx;
7057 /* Set the Capture Compare Register value */
7058 TIMx->CCR6 = OC_Config->Pulse;
7060 /* Write to TIMx CCER */
7061 TIMx->CCER = tmpccer;
7065 * @brief Slave Timer configuration function
7066 * @param htim TIM handle
7067 * @param sSlaveConfig Slave timer configuration
7068 * @retval None
7070 static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
7071 TIM_SlaveConfigTypeDef *sSlaveConfig)
7073 uint32_t tmpsmcr;
7074 uint32_t tmpccmr1;
7075 uint32_t tmpccer;
7077 /* Get the TIMx SMCR register value */
7078 tmpsmcr = htim->Instance->SMCR;
7080 /* Reset the Trigger Selection Bits */
7081 tmpsmcr &= ~TIM_SMCR_TS;
7082 /* Set the Input Trigger source */
7083 tmpsmcr |= sSlaveConfig->InputTrigger;
7085 /* Reset the slave mode Bits */
7086 tmpsmcr &= ~TIM_SMCR_SMS;
7087 /* Set the slave mode */
7088 tmpsmcr |= sSlaveConfig->SlaveMode;
7090 /* Write to TIMx SMCR */
7091 htim->Instance->SMCR = tmpsmcr;
7093 /* Configure the trigger prescaler, filter, and polarity */
7094 switch (sSlaveConfig->InputTrigger)
7096 case TIM_TS_ETRF:
7098 /* Check the parameters */
7099 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
7100 assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
7101 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
7102 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
7103 /* Configure the ETR Trigger source */
7104 TIM_ETR_SetConfig(htim->Instance,
7105 sSlaveConfig->TriggerPrescaler,
7106 sSlaveConfig->TriggerPolarity,
7107 sSlaveConfig->TriggerFilter);
7108 break;
7111 case TIM_TS_TI1F_ED:
7113 /* Check the parameters */
7114 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
7115 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
7117 if(sSlaveConfig->SlaveMode == TIM_SLAVEMODE_GATED)
7119 return HAL_ERROR;
7122 /* Disable the Channel 1: Reset the CC1E Bit */
7123 tmpccer = htim->Instance->CCER;
7124 htim->Instance->CCER &= ~TIM_CCER_CC1E;
7125 tmpccmr1 = htim->Instance->CCMR1;
7127 /* Set the filter */
7128 tmpccmr1 &= ~TIM_CCMR1_IC1F;
7129 tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4U);
7131 /* Write to TIMx CCMR1 and CCER registers */
7132 htim->Instance->CCMR1 = tmpccmr1;
7133 htim->Instance->CCER = tmpccer;
7134 break;
7137 case TIM_TS_TI1FP1:
7139 /* Check the parameters */
7140 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
7141 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
7142 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
7144 /* Configure TI1 Filter and Polarity */
7145 TIM_TI1_ConfigInputStage(htim->Instance,
7146 sSlaveConfig->TriggerPolarity,
7147 sSlaveConfig->TriggerFilter);
7148 break;
7151 case TIM_TS_TI2FP2:
7153 /* Check the parameters */
7154 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
7155 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
7156 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
7158 /* Configure TI2 Filter and Polarity */
7159 TIM_TI2_ConfigInputStage(htim->Instance,
7160 sSlaveConfig->TriggerPolarity,
7161 sSlaveConfig->TriggerFilter);
7162 break;
7165 case TIM_TS_ITR0:
7166 case TIM_TS_ITR1:
7167 case TIM_TS_ITR2:
7168 case TIM_TS_ITR3:
7169 case TIM_TS_ITR4:
7170 case TIM_TS_ITR5:
7171 case TIM_TS_ITR6:
7172 case TIM_TS_ITR7:
7173 case TIM_TS_ITR8:
7174 case TIM_TS_ITR9:
7175 case TIM_TS_ITR10:
7176 case TIM_TS_ITR11:
7177 case TIM_TS_ITR12:
7178 case TIM_TS_ITR13:
7180 /* Check the parameter */
7181 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
7182 break;
7185 default:
7186 break;
7188 return HAL_OK;
7192 * @brief Configure the TI1 as Input.
7193 * @param TIMx to select the TIM peripheral.
7194 * @param TIM_ICPolarity The Input Polarity.
7195 * This parameter can be one of the following values:
7196 * @arg TIM_ICPOLARITY_RISING
7197 * @arg TIM_ICPOLARITY_FALLING
7198 * @arg TIM_ICPOLARITY_BOTHEDGE
7199 * @param TIM_ICSelection specifies the input to be used.
7200 * This parameter can be one of the following values:
7201 * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 1 is selected to be connected to IC1.
7202 * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 1 is selected to be connected to IC2.
7203 * @arg TIM_ICSELECTION_TRC: TIM Input 1 is selected to be connected to TRC.
7204 * @param TIM_ICFilter Specifies the Input Capture Filter.
7205 * This parameter must be a value between 0x00 and 0x0F.
7206 * @retval None
7207 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
7208 * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
7209 * protected against un-initialized filter and polarity values.
7211 void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
7212 uint32_t TIM_ICFilter)
7214 uint32_t tmpccmr1;
7215 uint32_t tmpccer;
7217 /* Disable the Channel 1: Reset the CC1E Bit */
7218 TIMx->CCER &= ~TIM_CCER_CC1E;
7219 tmpccmr1 = TIMx->CCMR1;
7220 tmpccer = TIMx->CCER;
7222 /* Select the Input */
7223 if (IS_TIM_CC2_INSTANCE(TIMx) != RESET)
7225 tmpccmr1 &= ~TIM_CCMR1_CC1S;
7226 tmpccmr1 |= TIM_ICSelection;
7228 else
7230 tmpccmr1 |= TIM_CCMR1_CC1S_0;
7233 /* Set the filter */
7234 tmpccmr1 &= ~TIM_CCMR1_IC1F;
7235 tmpccmr1 |= ((TIM_ICFilter << 4U) & TIM_CCMR1_IC1F);
7237 /* Select the Polarity and set the CC1E Bit */
7238 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
7239 tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
7241 /* Write to TIMx CCMR1 and CCER registers */
7242 TIMx->CCMR1 = tmpccmr1;
7243 TIMx->CCER = tmpccer;
7247 * @brief Configure the Polarity and Filter for TI1.
7248 * @param TIMx to select the TIM peripheral.
7249 * @param TIM_ICPolarity The Input Polarity.
7250 * This parameter can be one of the following values:
7251 * @arg TIM_ICPOLARITY_RISING
7252 * @arg TIM_ICPOLARITY_FALLING
7253 * @arg TIM_ICPOLARITY_BOTHEDGE
7254 * @param TIM_ICFilter Specifies the Input Capture Filter.
7255 * This parameter must be a value between 0x00 and 0x0F.
7256 * @retval None
7258 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
7260 uint32_t tmpccmr1;
7261 uint32_t tmpccer;
7263 /* Disable the Channel 1: Reset the CC1E Bit */
7264 tmpccer = TIMx->CCER;
7265 TIMx->CCER &= ~TIM_CCER_CC1E;
7266 tmpccmr1 = TIMx->CCMR1;
7268 /* Set the filter */
7269 tmpccmr1 &= ~TIM_CCMR1_IC1F;
7270 tmpccmr1 |= (TIM_ICFilter << 4U);
7272 /* Select the Polarity and set the CC1E Bit */
7273 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
7274 tmpccer |= TIM_ICPolarity;
7276 /* Write to TIMx CCMR1 and CCER registers */
7277 TIMx->CCMR1 = tmpccmr1;
7278 TIMx->CCER = tmpccer;
7282 * @brief Configure the TI2 as Input.
7283 * @param TIMx to select the TIM peripheral
7284 * @param TIM_ICPolarity The Input Polarity.
7285 * This parameter can be one of the following values:
7286 * @arg TIM_ICPOLARITY_RISING
7287 * @arg TIM_ICPOLARITY_FALLING
7288 * @arg TIM_ICPOLARITY_BOTHEDGE
7289 * @param TIM_ICSelection specifies the input to be used.
7290 * This parameter can be one of the following values:
7291 * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 2 is selected to be connected to IC2.
7292 * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 2 is selected to be connected to IC1.
7293 * @arg TIM_ICSELECTION_TRC: TIM Input 2 is selected to be connected to TRC.
7294 * @param TIM_ICFilter Specifies the Input Capture Filter.
7295 * This parameter must be a value between 0x00 and 0x0F.
7296 * @retval None
7297 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
7298 * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
7299 * protected against un-initialized filter and polarity values.
7301 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
7302 uint32_t TIM_ICFilter)
7304 uint32_t tmpccmr1;
7305 uint32_t tmpccer;
7307 /* Disable the Channel 2: Reset the CC2E Bit */
7308 TIMx->CCER &= ~TIM_CCER_CC2E;
7309 tmpccmr1 = TIMx->CCMR1;
7310 tmpccer = TIMx->CCER;
7312 /* Select the Input */
7313 tmpccmr1 &= ~TIM_CCMR1_CC2S;
7314 tmpccmr1 |= (TIM_ICSelection << 8U);
7316 /* Set the filter */
7317 tmpccmr1 &= ~TIM_CCMR1_IC2F;
7318 tmpccmr1 |= ((TIM_ICFilter << 12U) & TIM_CCMR1_IC2F);
7320 /* Select the Polarity and set the CC2E Bit */
7321 tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
7322 tmpccer |= ((TIM_ICPolarity << 4U) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
7324 /* Write to TIMx CCMR1 and CCER registers */
7325 TIMx->CCMR1 = tmpccmr1 ;
7326 TIMx->CCER = tmpccer;
7330 * @brief Configure the Polarity and Filter for TI2.
7331 * @param TIMx to select the TIM peripheral.
7332 * @param TIM_ICPolarity The Input Polarity.
7333 * This parameter can be one of the following values:
7334 * @arg TIM_ICPOLARITY_RISING
7335 * @arg TIM_ICPOLARITY_FALLING
7336 * @arg TIM_ICPOLARITY_BOTHEDGE
7337 * @param TIM_ICFilter Specifies the Input Capture Filter.
7338 * This parameter must be a value between 0x00 and 0x0F.
7339 * @retval None
7341 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
7343 uint32_t tmpccmr1;
7344 uint32_t tmpccer;
7346 /* Disable the Channel 2: Reset the CC2E Bit */
7347 TIMx->CCER &= ~TIM_CCER_CC2E;
7348 tmpccmr1 = TIMx->CCMR1;
7349 tmpccer = TIMx->CCER;
7351 /* Set the filter */
7352 tmpccmr1 &= ~TIM_CCMR1_IC2F;
7353 tmpccmr1 |= (TIM_ICFilter << 12U);
7355 /* Select the Polarity and set the CC2E Bit */
7356 tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
7357 tmpccer |= (TIM_ICPolarity << 4U);
7359 /* Write to TIMx CCMR1 and CCER registers */
7360 TIMx->CCMR1 = tmpccmr1 ;
7361 TIMx->CCER = tmpccer;
7365 * @brief Configure the TI3 as Input.
7366 * @param TIMx to select the TIM peripheral
7367 * @param TIM_ICPolarity The Input Polarity.
7368 * This parameter can be one of the following values:
7369 * @arg TIM_ICPOLARITY_RISING
7370 * @arg TIM_ICPOLARITY_FALLING
7371 * @arg TIM_ICPOLARITY_BOTHEDGE
7372 * @param TIM_ICSelection specifies the input to be used.
7373 * This parameter can be one of the following values:
7374 * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 3 is selected to be connected to IC3.
7375 * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 3 is selected to be connected to IC4.
7376 * @arg TIM_ICSELECTION_TRC: TIM Input 3 is selected to be connected to TRC.
7377 * @param TIM_ICFilter Specifies the Input Capture Filter.
7378 * This parameter must be a value between 0x00 and 0x0F.
7379 * @retval None
7380 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
7381 * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
7382 * protected against un-initialized filter and polarity values.
7384 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
7385 uint32_t TIM_ICFilter)
7387 uint32_t tmpccmr2;
7388 uint32_t tmpccer;
7390 /* Disable the Channel 3: Reset the CC3E Bit */
7391 TIMx->CCER &= ~TIM_CCER_CC3E;
7392 tmpccmr2 = TIMx->CCMR2;
7393 tmpccer = TIMx->CCER;
7395 /* Select the Input */
7396 tmpccmr2 &= ~TIM_CCMR2_CC3S;
7397 tmpccmr2 |= TIM_ICSelection;
7399 /* Set the filter */
7400 tmpccmr2 &= ~TIM_CCMR2_IC3F;
7401 tmpccmr2 |= ((TIM_ICFilter << 4U) & TIM_CCMR2_IC3F);
7403 /* Select the Polarity and set the CC3E Bit */
7404 tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
7405 tmpccer |= ((TIM_ICPolarity << 8U) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
7407 /* Write to TIMx CCMR2 and CCER registers */
7408 TIMx->CCMR2 = tmpccmr2;
7409 TIMx->CCER = tmpccer;
7413 * @brief Configure the TI4 as Input.
7414 * @param TIMx to select the TIM peripheral
7415 * @param TIM_ICPolarity The Input Polarity.
7416 * This parameter can be one of the following values:
7417 * @arg TIM_ICPOLARITY_RISING
7418 * @arg TIM_ICPOLARITY_FALLING
7419 * @arg TIM_ICPOLARITY_BOTHEDGE
7420 * @param TIM_ICSelection specifies the input to be used.
7421 * This parameter can be one of the following values:
7422 * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 4 is selected to be connected to IC4.
7423 * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 4 is selected to be connected to IC3.
7424 * @arg TIM_ICSELECTION_TRC: TIM Input 4 is selected to be connected to TRC.
7425 * @param TIM_ICFilter Specifies the Input Capture Filter.
7426 * This parameter must be a value between 0x00 and 0x0F.
7427 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
7428 * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
7429 * protected against un-initialized filter and polarity values.
7430 * @retval None
7432 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
7433 uint32_t TIM_ICFilter)
7435 uint32_t tmpccmr2;
7436 uint32_t tmpccer;
7438 /* Disable the Channel 4: Reset the CC4E Bit */
7439 TIMx->CCER &= ~TIM_CCER_CC4E;
7440 tmpccmr2 = TIMx->CCMR2;
7441 tmpccer = TIMx->CCER;
7443 /* Select the Input */
7444 tmpccmr2 &= ~TIM_CCMR2_CC4S;
7445 tmpccmr2 |= (TIM_ICSelection << 8U);
7447 /* Set the filter */
7448 tmpccmr2 &= ~TIM_CCMR2_IC4F;
7449 tmpccmr2 |= ((TIM_ICFilter << 12U) & TIM_CCMR2_IC4F);
7451 /* Select the Polarity and set the CC4E Bit */
7452 tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
7453 tmpccer |= ((TIM_ICPolarity << 12U) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
7455 /* Write to TIMx CCMR2 and CCER registers */
7456 TIMx->CCMR2 = tmpccmr2;
7457 TIMx->CCER = tmpccer ;
7461 * @brief Selects the Input Trigger source
7462 * @param TIMx to select the TIM peripheral
7463 * @param InputTriggerSource The Input Trigger source.
7464 * This parameter can be one of the following values:
7465 * @arg TIM_TS_ITR0: Internal Trigger 0
7466 * @arg TIM_TS_ITR1: Internal Trigger 1
7467 * @arg TIM_TS_ITR2: Internal Trigger 2
7468 * @arg TIM_TS_ITR3: Internal Trigger 3
7469 * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
7470 * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
7471 * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
7472 * @arg TIM_TS_ETRF: External Trigger input
7473 * @arg TIM_TS_ITR4: Internal Trigger 4 (*)
7474 * @arg TIM_TS_ITR5: Internal Trigger 5
7475 * @arg TIM_TS_ITR6: Internal Trigger 6
7476 * @arg TIM_TS_ITR7: Internal Trigger 7
7477 * @arg TIM_TS_ITR8: Internal Trigger 8 (*)
7478 * @arg TIM_TS_ITR9: Internal Trigger 9 (*)
7479 * @arg TIM_TS_ITR10: Internal Trigger 10 (*)
7480 * @arg TIM_TS_ITR11: Internal Trigger 11 (*)
7481 * @arg TIM_TS_ITR12: Internal Trigger 12 (*)
7482 * @arg TIM_TS_ITR13: Internal Trigger 13 (*)
7484 * (*) Value not defined in all devices.
7486 * @retval None
7488 static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource)
7490 uint32_t tmpsmcr;
7492 /* Get the TIMx SMCR register value */
7493 tmpsmcr = TIMx->SMCR;
7494 /* Reset the TS Bits */
7495 tmpsmcr &= ~TIM_SMCR_TS;
7496 /* Set the Input Trigger source and the slave mode*/
7497 tmpsmcr |= (InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1);
7498 /* Write to TIMx SMCR */
7499 TIMx->SMCR = tmpsmcr;
7502 * @brief Configures the TIMx External Trigger (ETR).
7503 * @param TIMx to select the TIM peripheral
7504 * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
7505 * This parameter can be one of the following values:
7506 * @arg TIM_ETRPRESCALER_DIV1: ETRP Prescaler OFF.
7507 * @arg TIM_ETRPRESCALER_DIV2: ETRP frequency divided by 2.
7508 * @arg TIM_ETRPRESCALER_DIV4: ETRP frequency divided by 4.
7509 * @arg TIM_ETRPRESCALER_DIV8: ETRP frequency divided by 8.
7510 * @param TIM_ExtTRGPolarity The external Trigger Polarity.
7511 * This parameter can be one of the following values:
7512 * @arg TIM_ETRPOLARITY_INVERTED: active low or falling edge active.
7513 * @arg TIM_ETRPOLARITY_NONINVERTED: active high or rising edge active.
7514 * @param ExtTRGFilter External Trigger Filter.
7515 * This parameter must be a value between 0x00 and 0x0F
7516 * @retval None
7518 void TIM_ETR_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler,
7519 uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
7521 uint32_t tmpsmcr;
7523 tmpsmcr = TIMx->SMCR;
7525 /* Reset the ETR Bits */
7526 tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
7528 /* Set the Prescaler, the Filter value and the Polarity */
7529 tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8U)));
7531 /* Write to TIMx SMCR */
7532 TIMx->SMCR = tmpsmcr;
7536 * @brief Enables or disables the TIM Capture Compare Channel x.
7537 * @param TIMx to select the TIM peripheral
7538 * @param Channel specifies the TIM Channel
7539 * This parameter can be one of the following values:
7540 * @arg TIM_CHANNEL_1: TIM Channel 1
7541 * @arg TIM_CHANNEL_2: TIM Channel 2
7542 * @arg TIM_CHANNEL_3: TIM Channel 3
7543 * @arg TIM_CHANNEL_4: TIM Channel 4
7544 * @arg TIM_CHANNEL_5: TIM Channel 5 selected
7545 * @arg TIM_CHANNEL_6: TIM Channel 6 selected
7546 * @param ChannelState specifies the TIM Channel CCxE bit new state.
7547 * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
7548 * @retval None
7550 void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
7552 uint32_t tmp;
7554 /* Check the parameters */
7555 assert_param(IS_TIM_CC1_INSTANCE(TIMx));
7556 assert_param(IS_TIM_CHANNELS(Channel));
7558 tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
7560 /* Reset the CCxE Bit */
7561 TIMx->CCER &= ~tmp;
7563 /* Set or reset the CCxE Bit */
7564 TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
7567 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
7569 * @brief Reset interrupt callbacks to the legacy weak callbacks.
7570 * @param htim pointer to a TIM_HandleTypeDef structure that contains
7571 * the configuration information for TIM module.
7572 * @retval None
7574 void TIM_ResetCallback(TIM_HandleTypeDef *htim)
7576 /* Reset the TIM callback to the legacy weak callbacks */
7577 htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback; /* Legacy weak PeriodElapsedCallback */
7578 htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback; /* Legacy weak PeriodElapsedHalfCpltCallback */
7579 htim->TriggerCallback = HAL_TIM_TriggerCallback; /* Legacy weak TriggerCallback */
7580 htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback; /* Legacy weak TriggerHalfCpltCallback */
7581 htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback; /* Legacy weak IC_CaptureCallback */
7582 htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback; /* Legacy weak IC_CaptureHalfCpltCallback */
7583 htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback; /* Legacy weak OC_DelayElapsedCallback */
7584 htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback; /* Legacy weak PWM_PulseFinishedCallback */
7585 htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback; /* Legacy weak PWM_PulseFinishedHalfCpltCallback */
7586 htim->ErrorCallback = HAL_TIM_ErrorCallback; /* Legacy weak ErrorCallback */
7587 htim->CommutationCallback = HAL_TIMEx_CommutCallback; /* Legacy weak CommutationCallback */
7588 htim->CommutationHalfCpltCallback = HAL_TIMEx_CommutHalfCpltCallback; /* Legacy weak CommutationHalfCpltCallback */
7589 htim->BreakCallback = HAL_TIMEx_BreakCallback; /* Legacy weak BreakCallback */
7590 htim->Break2Callback = HAL_TIMEx_Break2Callback; /* Legacy weak Break2Callback */
7592 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
7595 * @}
7598 #endif /* HAL_TIM_MODULE_ENABLED */
7600 * @}
7604 * @}
7606 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/