2 ******************************************************************************
3 * @file stm32f3xx_hal_tim.c
4 * @author MCD Application Team
7 * @brief TIM HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Timer (TIM) peripheral:
10 * + Time Base Initialization
12 * + Time Base Start Interruption
13 * + Time Base Start DMA
14 * + Time Output Compare/PWM Initialization
15 * + Time Output Compare/PWM Channel Configuration
16 * + Time Output Compare/PWM Start
17 * + Time Output Compare/PWM Start Interruption
18 * + Time Output Compare/PWM Start DMA
19 * + Time Input Capture Initialization
20 * + Time Input Capture Channel Configuration
21 * + Time Input Capture Start
22 * + Time Input Capture Start Interruption
23 * + Time Input Capture Start DMA
24 * + Time One Pulse Initialization
25 * + Time One Pulse Channel Configuration
26 * + Time One Pulse Start
27 * + Time Encoder Interface Initialization
28 * + Time Encoder Interface Start
29 * + Time Encoder Interface Start Interruption
30 * + Time Encoder Interface Start DMA
31 * + Commutation Event configuration with Interruption and DMA
32 * + Time OCRef clear configuration
33 * + Time External Clock configuration
35 ==============================================================================
36 ##### TIMER Generic features #####
37 ==============================================================================
38 [..] The Timer features include:
39 (#) 16-bit up, down, up/down auto-reload counter.
40 (#) 16-bit programmable prescaler allowing dividing (also on the fly) the
41 counter clock frequency either by any factor between 1 and 65536.
42 (#) Up to 4 independent channels for:
45 (++) PWM generation (Edge and Center-aligned Mode)
46 (++) One-pulse mode output
48 ##### How to use this driver #####
49 ==============================================================================
51 (#) Initialize the TIM low level resources by implementing the following functions
52 depending from feature used :
53 (++) Time Base : HAL_TIM_Base_MspInit()
54 (++) Input Capture : HAL_TIM_IC_MspInit()
55 (++) Output Compare : HAL_TIM_OC_MspInit()
56 (++) PWM generation : HAL_TIM_PWM_MspInit()
57 (++) One-pulse mode output : HAL_TIM_OnePulse_MspInit()
58 (++) Encoder mode output : HAL_TIM_Encoder_MspInit()
60 (#) Initialize the TIM low level resources :
61 (##) Enable the TIM interface clock using __TIMx_CLK_ENABLE();
62 (##) TIM pins configuration
63 (+++) Enable the clock for the TIM GPIOs using the following function:
65 (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
67 (#) The external Clock can be configured, if needed (the default clock is the
68 internal clock from the APBx), using the following function:
69 HAL_TIM_ConfigClockSource, the clock configuration should be done before
72 (#) Configure the TIM in the desired functioning mode using one of the
73 Initialization function of this driver:
74 (++) HAL_TIM_Base_Init: to use the Timer to generate a simple time base
75 (++) HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an
76 Output Compare signal.
77 (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a
79 (++) HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an
81 (++) HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer
83 (++) HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
85 (#) Activate the TIM peripheral using one of the start functions depending from the feature used:
86 (++) Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
87 (++) Input Capture : HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
88 (++) Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
89 (++) PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
90 (++) One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
91 (++) Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(), HAL_TIM_Encoder_Start_IT().
93 (#) The DMA Burst is managed with the two following functions:
94 HAL_TIM_DMABurst_WriteStart()
95 HAL_TIM_DMABurst_ReadStart()
98 ******************************************************************************
101 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
103 * Redistribution and use in source and binary forms, with or without modification,
104 * are permitted provided that the following conditions are met:
105 * 1. Redistributions of source code must retain the above copyright notice,
106 * this list of conditions and the following disclaimer.
107 * 2. Redistributions in binary form must reproduce the above copyright notice,
108 * this list of conditions and the following disclaimer in the documentation
109 * and/or other materials provided with the distribution.
110 * 3. Neither the name of STMicroelectronics nor the names of its contributors
111 * may be used to endorse or promote products derived from this software
112 * without specific prior written permission.
114 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
115 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
116 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
117 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
118 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
119 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
120 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
121 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
122 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
123 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125 ******************************************************************************
128 /* Includes ------------------------------------------------------------------*/
129 #include "stm32f3xx_hal.h"
131 /** @addtogroup STM32F3xx_HAL_Driver
135 /** @defgroup TIM TIM HAL module driver
136 * @brief TIM HAL module driver
140 #ifdef HAL_TIM_MODULE_ENABLED
142 /* Private typedef -----------------------------------------------------------*/
143 /* Private define ------------------------------------------------------------*/
144 /* Private macro -------------------------------------------------------------*/
145 /* Private variables ---------------------------------------------------------*/
146 /* Private function prototypes -----------------------------------------------*/
147 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
148 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
149 uint32_t TIM_ICFilter);
150 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
151 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
152 uint32_t TIM_ICFilter);
153 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
154 uint32_t TIM_ICFilter);
155 static void TIM_ITRx_SetConfig(TIM_TypeDef* TIMx, uint16_t InputTriggerSource);
156 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma);
157 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);
158 /* Private functions ---------------------------------------------------------*/
160 /** @defgroup TIM_Exported_Functions TIM Exported Functions
164 /** @defgroup TIM_Exported_Functions_Group1 Time Base functions
165 * @brief Time Base functions
168 ==============================================================================
169 ##### Time Base functions #####
170 ==============================================================================
172 This section provides functions allowing to:
173 (+) Initialize and configure the TIM base.
174 (+) De-initialize the TIM base.
175 (+) Start the Time Base.
176 (+) Stop the Time Base.
177 (+) Start the Time Base and enable interrupt.
178 (+) Stop the Time Base and disable interrupt.
179 (+) Start the Time Base and enable DMA transfer.
180 (+) Stop the Time Base and disable DMA transfer.
186 * @brief Initializes the TIM Time base Unit according to the specified
187 * parameters in the TIM_HandleTypeDef and create the associated handle.
188 * @param htim: TIM Base handle
191 HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
193 /* Check the TIM handle allocation */
199 /* Check the parameters */
200 assert_param(IS_TIM_INSTANCE(htim->Instance));
201 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
202 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
204 if(htim->State == HAL_TIM_STATE_RESET)
206 /* Init the low level hardware : GPIO, CLOCK, NVIC */
207 HAL_TIM_Base_MspInit(htim);
210 /* Set the TIM state */
211 htim->State= HAL_TIM_STATE_BUSY;
213 /* Set the Time Base configuration */
214 TIM_Base_SetConfig(htim->Instance, &htim->Init);
216 /* Initialize the TIM state*/
217 htim->State= HAL_TIM_STATE_READY;
223 * @brief DeInitializes the TIM Base peripheral
224 * @param htim: TIM Base handle
227 HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)
229 /* Check the parameters */
230 assert_param(IS_TIM_INSTANCE(htim->Instance));
232 htim->State = HAL_TIM_STATE_BUSY;
234 /* Disable the TIM Peripheral Clock */
235 __HAL_TIM_DISABLE(htim);
237 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
238 HAL_TIM_Base_MspDeInit(htim);
240 /* Change TIM state */
241 htim->State = HAL_TIM_STATE_RESET;
250 * @brief Initializes the TIM Base MSP.
251 * @param htim: TIM handle
254 __weak void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
256 /* NOTE : This function Should not be modified, when the callback is needed,
257 the HAL_TIM_Base_MspInit could be implemented in the user file
262 * @brief DeInitializes TIM Base MSP.
263 * @param htim: TIM handle
266 __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
268 /* NOTE : This function Should not be modified, when the callback is needed,
269 the HAL_TIM_Base_MspDeInit could be implemented in the user file
275 * @brief Starts the TIM Base generation.
276 * @param htim : TIM handle
279 HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)
281 /* Check the parameters */
282 assert_param(IS_TIM_INSTANCE(htim->Instance));
284 /* Set the TIM state */
285 htim->State= HAL_TIM_STATE_BUSY;
287 /* Enable the Peripheral */
288 __HAL_TIM_ENABLE(htim);
290 /* Change the TIM state*/
291 htim->State= HAL_TIM_STATE_READY;
293 /* Return function status */
298 * @brief Stops the TIM Base generation.
299 * @param htim : TIM handle
302 HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)
304 /* Check the parameters */
305 assert_param(IS_TIM_INSTANCE(htim->Instance));
307 /* Set the TIM state */
308 htim->State= HAL_TIM_STATE_BUSY;
310 /* Disable the Peripheral */
311 __HAL_TIM_DISABLE(htim);
313 /* Change the TIM state*/
314 htim->State= HAL_TIM_STATE_READY;
316 /* Return function status */
321 * @brief Starts the TIM Base generation in interrupt mode.
322 * @param htim : TIM handle
325 HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
327 /* Check the parameters */
328 assert_param(IS_TIM_INSTANCE(htim->Instance));
330 /* Enable the TIM Update interrupt */
331 __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
333 /* Enable the Peripheral */
334 __HAL_TIM_ENABLE(htim);
336 /* Return function status */
341 * @brief Stops the TIM Base generation in interrupt mode.
342 * @param htim : TIM handle
345 HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)
347 /* Check the parameters */
348 assert_param(IS_TIM_INSTANCE(htim->Instance));
349 /* Disable the TIM Update interrupt */
350 __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);
352 /* Disable the Peripheral */
353 __HAL_TIM_DISABLE(htim);
355 /* Return function status */
360 * @brief Starts the TIM Base generation in DMA mode.
361 * @param htim : TIM handle
362 * @param pData: The source Buffer address.
363 * @param Length: The length of data to be transferred from memory to peripheral.
366 HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
368 /* Check the parameters */
369 assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
371 if((htim->State == HAL_TIM_STATE_BUSY))
375 else if((htim->State == HAL_TIM_STATE_READY))
377 if((pData == 0 ) && (Length > 0))
383 htim->State = HAL_TIM_STATE_BUSY;
386 /* Set the DMA Period elapsed callback */
387 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
389 /* Set the DMA error callback */
390 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = HAL_TIM_DMAError ;
392 /* Enable the DMA channel */
393 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR, Length);
395 /* Enable the TIM Update DMA request */
396 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);
398 /* Enable the Peripheral */
399 __HAL_TIM_ENABLE(htim);
401 /* Return function status */
406 * @brief Stops the TIM Base generation in DMA mode.
407 * @param htim : TIM handle
410 HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)
412 /* Check the parameters */
413 assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
415 /* Disable the TIM Update DMA request */
416 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);
418 /* Disable the Peripheral */
419 __HAL_TIM_DISABLE(htim);
421 /* Change the htim state */
422 htim->State = HAL_TIM_STATE_READY;
424 /* Return function status */
432 /** @defgroup TIM_Exported_Functions_Group2 Time Output Compare functions
433 * @brief Time Output Compare functions
436 ==============================================================================
437 ##### Time Output Compare functions #####
438 ==============================================================================
440 This section provides functions allowing to:
441 (+) Initialize and configure the TIM Output Compare.
442 (+) De-initialize the TIM Output Compare.
443 (+) Start the Time Output Compare.
444 (+) Stop the Time Output Compare.
445 (+) Start the Time Output Compare and enable interrupt.
446 (+) Stop the Time Output Compare and disable interrupt.
447 (+) Start the Time Output Compare and enable DMA transfer.
448 (+) Stop the Time Output Compare and disable DMA transfer.
454 * @brief Initializes the TIM Output Compare according to the specified
455 * parameters in the TIM_HandleTypeDef and create the associated handle.
456 * @param htim: TIM Output Compare handle
459 HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef* htim)
461 /* Check the TIM handle allocation */
467 /* Check the parameters */
468 assert_param(IS_TIM_INSTANCE(htim->Instance));
469 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
470 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
472 if(htim->State == HAL_TIM_STATE_RESET)
474 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
475 HAL_TIM_OC_MspInit(htim);
478 /* Set the TIM state */
479 htim->State= HAL_TIM_STATE_BUSY;
481 /* Init the base time for the Output Compare */
482 TIM_Base_SetConfig(htim->Instance, &htim->Init);
484 /* Initialize the TIM state*/
485 htim->State= HAL_TIM_STATE_READY;
491 * @brief DeInitializes the TIM peripheral
492 * @param htim: TIM Output Compare handle
495 HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)
497 /* Check the parameters */
498 assert_param(IS_TIM_INSTANCE(htim->Instance));
500 htim->State = HAL_TIM_STATE_BUSY;
502 /* Disable the TIM Peripheral Clock */
503 __HAL_TIM_DISABLE(htim);
505 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
506 HAL_TIM_OC_MspDeInit(htim);
508 /* Change TIM state */
509 htim->State = HAL_TIM_STATE_RESET;
518 * @brief Initializes the TIM Output Compare MSP.
519 * @param htim: TIM handle
522 __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
524 /* NOTE : This function Should not be modified, when the callback is needed,
525 the HAL_TIM_OC_MspInit could be implemented in the user file
530 * @brief DeInitializes TIM Output Compare MSP.
531 * @param htim: TIM handle
534 __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
536 /* NOTE : This function Should not be modified, when the callback is needed,
537 the HAL_TIM_OC_MspDeInit could be implemented in the user file
542 * @brief Starts the TIM Output Compare signal generation.
543 * @param htim : TIM Output Compare handle
544 * @param Channel : TIM Channel to be enabled
545 * This parameter can be one of the following values:
546 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
547 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
548 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
549 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
552 HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
554 /* Check the parameters */
555 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
557 /* Enable the Output compare channel */
558 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
560 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
562 /* Enable the main output */
563 __HAL_TIM_MOE_ENABLE(htim);
566 /* Enable the Peripheral */
567 __HAL_TIM_ENABLE(htim);
569 /* Return function status */
574 * @brief Stops the TIM Output Compare signal generation.
575 * @param htim : TIM handle
576 * @param Channel : TIM Channel to be disabled
577 * This parameter can be one of the following values:
578 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
579 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
580 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
581 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
584 HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
586 /* Check the parameters */
587 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
589 /* Disable the Output compare channel */
590 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
592 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
594 /* Disable the Main Ouput */
595 __HAL_TIM_MOE_DISABLE(htim);
598 /* Disable the Peripheral */
599 __HAL_TIM_DISABLE(htim);
601 /* Return function status */
606 * @brief Starts the TIM Output Compare signal generation in interrupt mode.
607 * @param htim : TIM OC handle
608 * @param Channel : TIM Channel to be enabled
609 * This parameter can be one of the following values:
610 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
611 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
612 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
613 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
616 HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
618 /* Check the parameters */
619 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
625 /* Enable the TIM Capture/Compare 1 interrupt */
626 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
632 /* Enable the TIM Capture/Compare 2 interrupt */
633 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
639 /* Enable the TIM Capture/Compare 3 interrupt */
640 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
646 /* Enable the TIM Capture/Compare 4 interrupt */
647 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
655 /* Enable the Output compare channel */
656 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
658 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
660 /* Enable the main output */
661 __HAL_TIM_MOE_ENABLE(htim);
664 /* Enable the Peripheral */
665 __HAL_TIM_ENABLE(htim);
667 /* Return function status */
672 * @brief Stops the TIM Output Compare signal generation in interrupt mode.
673 * @param htim : TIM Output Compare handle
674 * @param Channel : TIM Channel to be disabled
675 * This parameter can be one of the following values:
676 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
677 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
678 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
679 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
682 HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
684 /* Check the parameters */
685 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
691 /* Disable the TIM Capture/Compare 1 interrupt */
692 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
698 /* Disable the TIM Capture/Compare 2 interrupt */
699 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
705 /* Disable the TIM Capture/Compare 3 interrupt */
706 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
712 /* Disable the TIM Capture/Compare 4 interrupt */
713 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
721 /* Disable the Output compare channel */
722 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
724 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
726 /* Disable the Main Ouput */
727 __HAL_TIM_MOE_DISABLE(htim);
730 /* Disable the Peripheral */
731 __HAL_TIM_DISABLE(htim);
733 /* Return function status */
738 * @brief Starts the TIM Output Compare signal generation in DMA mode.
739 * @param htim : TIM Output Compare handle
740 * @param Channel : TIM Channel to be enabled
741 * This parameter can be one of the following values:
742 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
743 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
744 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
745 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
746 * @param pData: The source Buffer address.
747 * @param Length: The length of data to be transferred from memory to TIM peripheral
750 HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
752 /* Check the parameters */
753 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
755 if((htim->State == HAL_TIM_STATE_BUSY))
759 else if((htim->State == HAL_TIM_STATE_READY))
761 if(((uint32_t)pData == 0 ) && (Length > 0))
767 htim->State = HAL_TIM_STATE_BUSY;
774 /* Set the DMA Period elapsed callback */
775 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
777 /* Set the DMA error callback */
778 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
780 /* Enable the DMA channel */
781 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
783 /* Enable the TIM Capture/Compare 1 DMA request */
784 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
790 /* Set the DMA Period elapsed callback */
791 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
793 /* Set the DMA error callback */
794 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
796 /* Enable the DMA channel */
797 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
799 /* Enable the TIM Capture/Compare 2 DMA request */
800 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
806 /* Set the DMA Period elapsed callback */
807 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
809 /* Set the DMA error callback */
810 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
812 /* Enable the DMA channel */
813 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
815 /* Enable the TIM Capture/Compare 3 DMA request */
816 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
822 /* Set the DMA Period elapsed callback */
823 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
825 /* Set the DMA error callback */
826 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
828 /* Enable the DMA channel */
829 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
831 /* Enable the TIM Capture/Compare 4 DMA request */
832 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
840 /* Enable the Output compare channel */
841 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
843 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
845 /* Enable the main output */
846 __HAL_TIM_MOE_ENABLE(htim);
849 /* Enable the Peripheral */
850 __HAL_TIM_ENABLE(htim);
852 /* Return function status */
857 * @brief Stops the TIM Output Compare signal generation in DMA mode.
858 * @param htim : TIM Output Compare handle
859 * @param Channel : TIM Channel to be disabled
860 * This parameter can be one of the following values:
861 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
862 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
863 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
864 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
867 HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
869 /* Check the parameters */
870 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
876 /* Disable the TIM Capture/Compare 1 DMA request */
877 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
883 /* Disable the TIM Capture/Compare 2 DMA request */
884 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
890 /* Disable the TIM Capture/Compare 3 DMA request */
891 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
897 /* Disable the TIM Capture/Compare 4 interrupt */
898 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
906 /* Disable the Output compare channel */
907 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
909 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
911 /* Disable the Main Ouput */
912 __HAL_TIM_MOE_DISABLE(htim);
915 /* Disable the Peripheral */
916 __HAL_TIM_DISABLE(htim);
918 /* Change the htim state */
919 htim->State = HAL_TIM_STATE_READY;
921 /* Return function status */
929 /** @defgroup TIM_Exported_Functions_Group3 Time PWM functions
930 * @brief Time PWM functions
933 ==============================================================================
934 ##### Time PWM functions #####
935 ==============================================================================
937 This section provides functions allowing to:
938 (+) Initialize and configure the TIM OPWM.
939 (+) De-initialize the TIM PWM.
940 (+) Start the Time PWM.
941 (+) Stop the Time PWM.
942 (+) Start the Time PWM and enable interrupt.
943 (+) Stop the Time PWM and disable interrupt.
944 (+) Start the Time PWM and enable DMA transfer.
945 (+) Stop the Time PWM and disable DMA transfer.
951 * @brief Initializes the TIM PWM Time Base according to the specified
952 * parameters in the TIM_HandleTypeDef and create the associated handle.
953 * @param htim: TIM handle
956 HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)
958 /* Check the TIM handle allocation */
964 /* Check the parameters */
965 assert_param(IS_TIM_INSTANCE(htim->Instance));
966 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
967 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
969 if(htim->State == HAL_TIM_STATE_RESET)
971 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
972 HAL_TIM_PWM_MspInit(htim);
975 /* Set the TIM state */
976 htim->State= HAL_TIM_STATE_BUSY;
978 /* Init the base time for the PWM */
979 TIM_Base_SetConfig(htim->Instance, &htim->Init);
981 /* Initialize the TIM state*/
982 htim->State= HAL_TIM_STATE_READY;
988 * @brief DeInitializes the TIM peripheral
989 * @param htim: TIM handle
992 HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)
994 /* Check the parameters */
995 assert_param(IS_TIM_INSTANCE(htim->Instance));
997 htim->State = HAL_TIM_STATE_BUSY;
999 /* Disable the TIM Peripheral Clock */
1000 __HAL_TIM_DISABLE(htim);
1002 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1003 HAL_TIM_PWM_MspDeInit(htim);
1005 /* Change TIM state */
1006 htim->State = HAL_TIM_STATE_RESET;
1015 * @brief Initializes the TIM PWM MSP.
1016 * @param htim: TIM handle
1019 __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
1021 /* NOTE : This function Should not be modified, when the callback is needed,
1022 the HAL_TIM_PWM_MspInit could be implemented in the user file
1027 * @brief DeInitializes TIM PWM MSP.
1028 * @param htim: TIM handle
1031 __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)
1033 /* NOTE : This function Should not be modified, when the callback is needed,
1034 the HAL_TIM_PWM_MspDeInit could be implemented in the user file
1039 * @brief Starts the PWM signal generation.
1040 * @param htim : TIM handle
1041 * @param Channel : TIM Channels to be enabled
1042 * This parameter can be one of the following values:
1043 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1044 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1045 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1046 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1047 * @retval HAL status
1049 HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
1051 /* Check the parameters */
1052 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1054 /* Enable the Capture compare channel */
1055 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1057 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1059 /* Enable the main output */
1060 __HAL_TIM_MOE_ENABLE(htim);
1063 /* Enable the Peripheral */
1064 __HAL_TIM_ENABLE(htim);
1066 /* Return function status */
1071 * @brief Stops the PWM signal generation.
1072 * @param htim : TIM handle
1073 * @param Channel : TIM Channels to be disabled
1074 * This parameter can be one of the following values:
1075 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1076 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1077 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1078 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1079 * @retval HAL status
1081 HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
1083 /* Check the parameters */
1084 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1086 /* Disable the Capture compare channel */
1087 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1089 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1091 /* Disable the Main Ouput */
1092 __HAL_TIM_MOE_DISABLE(htim);
1095 /* Disable the Peripheral */
1096 __HAL_TIM_DISABLE(htim);
1098 /* Change the htim state */
1099 htim->State = HAL_TIM_STATE_READY;
1101 /* Return function status */
1106 * @brief Starts the PWM signal generation in interrupt mode.
1107 * @param htim : TIM handle
1108 * @param Channel : TIM Channel to be disabled
1109 * This parameter can be one of the following values:
1110 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1111 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1112 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1113 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1114 * @retval HAL status
1116 HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1118 /* Check the parameters */
1119 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1125 /* Enable the TIM Capture/Compare 1 interrupt */
1126 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1132 /* Enable the TIM Capture/Compare 2 interrupt */
1133 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1139 /* Enable the TIM Capture/Compare 3 interrupt */
1140 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1146 /* Enable the TIM Capture/Compare 4 interrupt */
1147 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
1155 /* Enable the Capture compare channel */
1156 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1158 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1160 /* Enable the main output */
1161 __HAL_TIM_MOE_ENABLE(htim);
1164 /* Enable the Peripheral */
1165 __HAL_TIM_ENABLE(htim);
1167 /* Return function status */
1172 * @brief Stops the PWM signal generation in interrupt mode.
1173 * @param htim : TIM handle
1174 * @param Channel : TIM Channels to be disabled
1175 * This parameter can be one of the following values:
1176 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1177 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1178 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1179 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1180 * @retval HAL status
1182 HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
1184 /* Check the parameters */
1185 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1191 /* Disable the TIM Capture/Compare 1 interrupt */
1192 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1198 /* Disable the TIM Capture/Compare 2 interrupt */
1199 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1205 /* Disable the TIM Capture/Compare 3 interrupt */
1206 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1212 /* Disable the TIM Capture/Compare 4 interrupt */
1213 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
1221 /* Disable the Capture compare channel */
1222 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1224 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1226 /* Disable the Main Ouput */
1227 __HAL_TIM_MOE_DISABLE(htim);
1230 /* Disable the Peripheral */
1231 __HAL_TIM_DISABLE(htim);
1233 /* Return function status */
1238 * @brief Starts the TIM PWM signal generation in DMA mode.
1239 * @param htim : TIM handle
1240 * @param Channel : TIM Channels to be enabled
1241 * This parameter can be one of the following values:
1242 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1243 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1244 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1245 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1246 * @param pData: The source Buffer address.
1247 * @param Length: The length of data to be transferred from memory to TIM peripheral
1248 * @retval HAL status
1250 HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
1252 /* Check the parameters */
1253 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1255 if((htim->State == HAL_TIM_STATE_BUSY))
1259 else if((htim->State == HAL_TIM_STATE_READY))
1261 if(((uint32_t)pData == 0 ) && (Length > 0))
1267 htim->State = HAL_TIM_STATE_BUSY;
1274 /* Set the DMA Period elapsed callback */
1275 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
1277 /* Set the DMA error callback */
1278 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
1280 /* Enable the DMA channel */
1281 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
1283 /* Enable the TIM Capture/Compare 1 DMA request */
1284 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1290 /* Set the DMA Period elapsed callback */
1291 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
1293 /* Set the DMA error callback */
1294 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
1296 /* Enable the DMA channel */
1297 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
1299 /* Enable the TIM Capture/Compare 2 DMA request */
1300 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1306 /* Set the DMA Period elapsed callback */
1307 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
1309 /* Set the DMA error callback */
1310 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
1312 /* Enable the DMA channel */
1313 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
1315 /* Enable the TIM Output Capture/Compare 3 request */
1316 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1322 /* Set the DMA Period elapsed callback */
1323 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
1325 /* Set the DMA error callback */
1326 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
1328 /* Enable the DMA channel */
1329 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
1331 /* Enable the TIM Capture/Compare 4 DMA request */
1332 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1340 /* Enable the Capture compare channel */
1341 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1343 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1345 /* Enable the main output */
1346 __HAL_TIM_MOE_ENABLE(htim);
1349 /* Enable the Peripheral */
1350 __HAL_TIM_ENABLE(htim);
1352 /* Return function status */
1357 * @brief Stops the TIM PWM signal generation in DMA mode.
1358 * @param htim : TIM handle
1359 * @param Channel : TIM Channels to be disabled
1360 * This parameter can be one of the following values:
1361 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1362 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1363 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1364 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1365 * @retval HAL status
1367 HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1369 /* Check the parameters */
1370 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1376 /* Disable the TIM Capture/Compare 1 DMA request */
1377 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1383 /* Disable the TIM Capture/Compare 2 DMA request */
1384 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1390 /* Disable the TIM Capture/Compare 3 DMA request */
1391 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1397 /* Disable the TIM Capture/Compare 4 interrupt */
1398 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1406 /* Disable the Capture compare channel */
1407 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1409 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
1411 /* Disable the Main Ouput */
1412 __HAL_TIM_MOE_DISABLE(htim);
1415 /* Disable the Peripheral */
1416 __HAL_TIM_DISABLE(htim);
1418 /* Change the htim state */
1419 htim->State = HAL_TIM_STATE_READY;
1421 /* Return function status */
1429 /** @defgroup TIM_Exported_Functions_Group4 Time Input Capture functions
1430 * @brief Time Input Capture functions
1433 ==============================================================================
1434 ##### Time Input Capture functions #####
1435 ==============================================================================
1437 This section provides functions allowing to:
1438 (+) Initialize and configure the TIM Input Capture.
1439 (+) De-initialize the TIM Input Capture.
1440 (+) Start the Time Input Capture.
1441 (+) Stop the Time Input Capture.
1442 (+) Start the Time Input Capture and enable interrupt.
1443 (+) Stop the Time Input Capture and disable interrupt.
1444 (+) Start the Time Input Capture and enable DMA transfer.
1445 (+) Stop the Time Input Capture and disable DMA transfer.
1451 * @brief Initializes the TIM Input Capture Time base according to the specified
1452 * parameters in the TIM_HandleTypeDef and create the associated handle.
1453 * @param htim: TIM Input Capture handle
1454 * @retval HAL status
1456 HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)
1458 /* Check the TIM handle allocation */
1459 if(htim == HAL_NULL)
1464 /* Check the parameters */
1465 assert_param(IS_TIM_INSTANCE(htim->Instance));
1466 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
1467 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
1469 if(htim->State == HAL_TIM_STATE_RESET)
1471 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1472 HAL_TIM_IC_MspInit(htim);
1475 /* Set the TIM state */
1476 htim->State= HAL_TIM_STATE_BUSY;
1478 /* Init the base time for the input capture */
1479 TIM_Base_SetConfig(htim->Instance, &htim->Init);
1481 /* Initialize the TIM state*/
1482 htim->State= HAL_TIM_STATE_READY;
1488 * @brief DeInitializes the TIM peripheral
1489 * @param htim: TIM Input Capture handle
1490 * @retval HAL status
1492 HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)
1494 /* Check the parameters */
1495 assert_param(IS_TIM_INSTANCE(htim->Instance));
1497 htim->State = HAL_TIM_STATE_BUSY;
1499 /* Disable the TIM Peripheral Clock */
1500 __HAL_TIM_DISABLE(htim);
1502 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1503 HAL_TIM_IC_MspDeInit(htim);
1505 /* Change TIM state */
1506 htim->State = HAL_TIM_STATE_RESET;
1515 * @brief Initializes the TIM INput Capture MSP.
1516 * @param htim: TIM handle
1519 __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
1521 /* NOTE : This function Should not be modified, when the callback is needed,
1522 the HAL_TIM_IC_MspInit could be implemented in the user file
1527 * @brief DeInitializes TIM Input Capture MSP.
1528 * @param htim: TIM handle
1531 __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)
1533 /* NOTE : This function Should not be modified, when the callback is needed,
1534 the HAL_TIM_IC_MspDeInit could be implemented in the user file
1539 * @brief Starts the TIM Input Capture measurement.
1540 * @param htim : TIM Input Capture handle
1541 * @param Channel : TIM Channels to be enabled
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_IC_Start (TIM_HandleTypeDef *htim, uint32_t Channel)
1551 /* Check the parameters */
1552 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1554 /* Enable the Input Capture channel */
1555 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1557 /* Enable the Peripheral */
1558 __HAL_TIM_ENABLE(htim);
1560 /* Return function status */
1565 * @brief Stops the TIM Input Capture measurement.
1566 * @param htim : TIM handle
1567 * @param Channel : TIM Channels to be disabled
1568 * This parameter can be one of the following values:
1569 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1570 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1571 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1572 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1573 * @retval HAL status
1575 HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
1577 /* Check the parameters */
1578 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1580 /* Disable the Input Capture channel */
1581 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1583 /* Disable the Peripheral */
1584 __HAL_TIM_DISABLE(htim);
1586 /* Return function status */
1591 * @brief Starts the TIM Input Capture measurement in interrupt mode.
1592 * @param htim : TIM Input Capture handle
1593 * @param Channel : TIM Channels to be enabled
1594 * This parameter can be one of the following values:
1595 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1596 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1597 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1598 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1599 * @retval HAL status
1601 HAL_StatusTypeDef HAL_TIM_IC_Start_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
1603 /* Check the parameters */
1604 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1610 /* Enable the TIM Capture/Compare 1 interrupt */
1611 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1617 /* Enable the TIM Capture/Compare 2 interrupt */
1618 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1624 /* Enable the TIM Capture/Compare 3 interrupt */
1625 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1631 /* Enable the TIM Capture/Compare 4 interrupt */
1632 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
1639 /* Enable the Input Capture channel */
1640 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1642 /* Enable the Peripheral */
1643 __HAL_TIM_ENABLE(htim);
1645 /* Return function status */
1650 * @brief Stops the TIM Input Capture measurement in interrupt mode.
1651 * @param htim : TIM handle
1652 * @param Channel : TIM Channels to be disabled
1653 * This parameter can be one of the following values:
1654 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1655 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1656 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1657 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1658 * @retval HAL status
1660 HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1662 /* Check the parameters */
1663 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1669 /* Disable the TIM Capture/Compare 1 interrupt */
1670 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1676 /* Disable the TIM Capture/Compare 2 interrupt */
1677 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1683 /* Disable the TIM Capture/Compare 3 interrupt */
1684 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1690 /* Disable the TIM Capture/Compare 4 interrupt */
1691 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
1699 /* Disable the Input Capture channel */
1700 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1702 /* Disable the Peripheral */
1703 __HAL_TIM_DISABLE(htim);
1705 /* Return function status */
1710 * @brief Starts the TIM Input Capture measurement on in DMA mode.
1711 * @param htim : TIM Input Capture handle
1712 * @param Channel : TIM Channels to be enabled
1713 * This parameter can be one of the following values:
1714 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1715 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1716 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1717 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1718 * @param pData: The destination Buffer address.
1719 * @param Length: The length of data to be transferred from TIM peripheral to memory.
1720 * @retval HAL status
1722 HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
1724 /* Check the parameters */
1725 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1726 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
1728 if((htim->State == HAL_TIM_STATE_BUSY))
1732 else if((htim->State == HAL_TIM_STATE_READY))
1734 if((pData == 0 ) && (Length > 0))
1740 htim->State = HAL_TIM_STATE_BUSY;
1748 /* Set the DMA Period elapsed callback */
1749 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
1751 /* Set the DMA error callback */
1752 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
1754 /* Enable the DMA channel */
1755 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
1757 /* Enable the TIM Capture/Compare 1 DMA request */
1758 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1764 /* Set the DMA Period elapsed callback */
1765 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
1767 /* Set the DMA error callback */
1768 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
1770 /* Enable the DMA channel */
1771 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData, Length);
1773 /* Enable the TIM Capture/Compare 2 DMA request */
1774 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1780 /* Set the DMA Period elapsed callback */
1781 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
1783 /* Set the DMA error callback */
1784 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
1786 /* Enable the DMA channel */
1787 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData, Length);
1789 /* Enable the TIM Capture/Compare 3 DMA request */
1790 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1796 /* Set the DMA Period elapsed callback */
1797 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
1799 /* Set the DMA error callback */
1800 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
1802 /* Enable the DMA channel */
1803 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData, Length);
1805 /* Enable the TIM Capture/Compare 4 DMA request */
1806 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1814 /* Enable the Input Capture channel */
1815 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1817 /* Enable the Peripheral */
1818 __HAL_TIM_ENABLE(htim);
1820 /* Return function status */
1825 * @brief Stops the TIM Input Capture measurement on in DMA mode.
1826 * @param htim : TIM Input Capture handle
1827 * @param Channel : TIM Channels to be disabled
1828 * This parameter can be one of the following values:
1829 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1830 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1831 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1832 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1833 * @retval HAL status
1835 HAL_StatusTypeDef HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1837 /* Check the parameters */
1838 assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
1839 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
1845 /* Disable the TIM Capture/Compare 1 DMA request */
1846 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1852 /* Disable the TIM Capture/Compare 2 DMA request */
1853 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1859 /* Disable the TIM Capture/Compare 3 DMA request */
1860 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1866 /* Disable the TIM Capture/Compare 4 DMA request */
1867 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1875 /* Disable the Input Capture channel */
1876 TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
1878 /* Disable the Peripheral */
1879 __HAL_TIM_DISABLE(htim);
1881 /* Change the htim state */
1882 htim->State = HAL_TIM_STATE_READY;
1884 /* Return function status */
1891 /** @defgroup TIM_Exported_Functions_Group5 Time One Pulse functions
1892 * @brief Time One Pulse functions
1895 ==============================================================================
1896 ##### Time One Pulse functions #####
1897 ==============================================================================
1899 This section provides functions allowing to:
1900 (+) Initialize and configure the TIM One Pulse.
1901 (+) De-initialize the TIM One Pulse.
1902 (+) Start the Time One Pulse.
1903 (+) Stop the Time One Pulse.
1904 (+) Start the Time One Pulse and enable interrupt.
1905 (+) Stop the Time One Pulse and disable interrupt.
1906 (+) Start the Time One Pulse and enable DMA transfer.
1907 (+) Stop the Time One Pulse and disable DMA transfer.
1913 * @brief Initializes the TIM One Pulse Time Base according to the specified
1914 * parameters in the TIM_HandleTypeDef and create the associated handle.
1915 * @param htim: TIM OnePulse handle
1916 * @param OnePulseMode: Select the One pulse mode.
1917 * This parameter can be one of the following values:
1918 * @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
1919 * @arg TIM_OPMODE_REPETITIVE: Repetitive pulses wil be generated.
1920 * @retval HAL status
1922 HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)
1924 /* Check the TIM handle allocation */
1925 if(htim == HAL_NULL)
1930 /* Check the parameters */
1931 assert_param(IS_TIM_INSTANCE(htim->Instance));
1932 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
1933 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
1934 assert_param(IS_TIM_OPM_MODE(OnePulseMode));
1936 if(htim->State == HAL_TIM_STATE_RESET)
1938 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1939 HAL_TIM_OnePulse_MspInit(htim);
1942 /* Set the TIM state */
1943 htim->State= HAL_TIM_STATE_BUSY;
1945 /* Configure the Time base in the One Pulse Mode */
1946 TIM_Base_SetConfig(htim->Instance, &htim->Init);
1948 /* Reset the OPM Bit */
1949 htim->Instance->CR1 &= ~TIM_CR1_OPM;
1951 /* Configure the OPM Mode */
1952 htim->Instance->CR1 |= OnePulseMode;
1954 /* Initialize the TIM state*/
1955 htim->State= HAL_TIM_STATE_READY;
1961 * @brief DeInitializes the TIM One Pulse
1962 * @param htim: TIM One Pulse handle
1963 * @retval HAL status
1965 HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)
1967 /* Check the parameters */
1968 assert_param(IS_TIM_INSTANCE(htim->Instance));
1970 htim->State = HAL_TIM_STATE_BUSY;
1972 /* Disable the TIM Peripheral Clock */
1973 __HAL_TIM_DISABLE(htim);
1975 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
1976 HAL_TIM_OnePulse_MspDeInit(htim);
1978 /* Change TIM state */
1979 htim->State = HAL_TIM_STATE_RESET;
1988 * @brief Initializes the TIM One Pulse MSP.
1989 * @param htim: TIM handle
1992 __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
1994 /* NOTE : This function Should not be modified, when the callback is needed,
1995 the HAL_TIM_OnePulse_MspInit could be implemented in the user file
2000 * @brief DeInitializes TIM One Pulse MSP.
2001 * @param htim: TIM handle
2004 __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)
2006 /* NOTE : This function Should not be modified, when the callback is needed,
2007 the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
2012 * @brief Starts the TIM One Pulse signal generation.
2013 * @param htim : TIM One Pulse handle
2014 * @param OutputChannel : TIM Channels to be enabled
2015 * This parameter can be one of the following values:
2016 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2017 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2018 * @retval HAL status
2020 HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2022 /* Enable the Capture compare and the Input Capture channels
2023 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2024 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2025 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2026 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2028 No need to enable the counter, it's enabled automatically by hardware
2029 (the counter starts in response to a stimulus and generate a pulse */
2031 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2032 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2034 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2036 /* Enable the main output */
2037 __HAL_TIM_MOE_ENABLE(htim);
2040 /* Return function status */
2045 * @brief Stops the TIM One Pulse signal generation.
2046 * @param htim : TIM One Pulse handle
2047 * @param OutputChannel : TIM Channels to be disable
2048 * This parameter can be one of the following values:
2049 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2050 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2051 * @retval HAL status
2053 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2055 /* Disable the Capture compare and the Input Capture channels
2056 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2057 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2058 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2059 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2061 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2062 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2064 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2066 /* Disable the Main Ouput */
2067 __HAL_TIM_MOE_DISABLE(htim);
2070 /* Disable the Peripheral */
2071 __HAL_TIM_DISABLE(htim);
2073 /* Return function status */
2078 * @brief Starts the TIM One Pulse signal generation in interrupt mode.
2079 * @param htim : TIM One Pulse handle
2080 * @param OutputChannel : TIM Channels to be enabled
2081 * This parameter can be one of the following values:
2082 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2083 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2084 * @retval HAL status
2086 HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2088 /* Enable the Capture compare and the Input Capture channels
2089 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2090 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2091 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2092 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2094 No need to enable the counter, it's enabled automatically by hardware
2095 (the counter starts in response to a stimulus and generate a pulse */
2097 /* Enable the TIM Capture/Compare 1 interrupt */
2098 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2100 /* Enable the TIM Capture/Compare 2 interrupt */
2101 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2103 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2104 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2106 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2108 /* Enable the main output */
2109 __HAL_TIM_MOE_ENABLE(htim);
2112 /* Return function status */
2117 * @brief Stops the TIM One Pulse signal generation in interrupt mode.
2118 * @param htim : TIM One Pulse handle
2119 * @param OutputChannel : TIM Channels to be enabled
2120 * This parameter can be one of the following values:
2121 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2122 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2123 * @retval HAL status
2125 HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
2127 /* Disable the TIM Capture/Compare 1 interrupt */
2128 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2130 /* Disable the TIM Capture/Compare 2 interrupt */
2131 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2133 /* Disable the Capture compare and the Input Capture channels
2134 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2135 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2136 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2137 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2138 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2139 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2141 if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
2143 /* Disable the Main Ouput */
2144 __HAL_TIM_MOE_DISABLE(htim);
2147 /* Disable the Peripheral */
2148 __HAL_TIM_DISABLE(htim);
2150 /* Return function status */
2158 /** @defgroup TIM_Exported_Functions_Group6 Time Encoder functions
2159 * @brief Time Encoder functions
2162 ==============================================================================
2163 ##### Time Encoder functions #####
2164 ==============================================================================
2166 This section provides functions allowing to:
2167 (+) Initialize and configure the TIM Encoder.
2168 (+) De-initialize the TIM Encoder.
2169 (+) Start the Time Encoder.
2170 (+) Stop the Time Encoder.
2171 (+) Start the Time Encoder and enable interrupt.
2172 (+) Stop the Time Encoder and disable interrupt.
2173 (+) Start the Time Encoder and enable DMA transfer.
2174 (+) Stop the Time Encoder and disable DMA transfer.
2180 * @brief Initializes the TIM Encoder Interface and create the associated handle.
2181 * @param htim: TIM Encoder Interface handle
2182 * @param sConfig: TIM Encoder Interface configuration structure
2183 * @retval HAL status
2185 HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim, TIM_Encoder_InitTypeDef* sConfig)
2187 uint32_t tmpsmcr = 0;
2188 uint32_t tmpccmr1 = 0;
2189 uint32_t tmpccer = 0;
2191 /* Check the TIM handle allocation */
2192 if(htim == HAL_NULL)
2197 /* Check the parameters */
2198 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2199 assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));
2200 assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));
2201 assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));
2202 assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
2203 assert_param(IS_TIM_IC_POLARITY(sConfig->IC2Polarity));
2204 assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
2205 assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));
2206 assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
2207 assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));
2209 if(htim->State == HAL_TIM_STATE_RESET)
2211 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
2212 HAL_TIM_Encoder_MspInit(htim);
2215 /* Set the TIM state */
2216 htim->State= HAL_TIM_STATE_BUSY;
2218 /* Reset the SMS bits */
2219 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
2221 /* Configure the Time base in the Encoder Mode */
2222 TIM_Base_SetConfig(htim->Instance, &htim->Init);
2224 /* Get the TIMx SMCR register value */
2225 tmpsmcr = htim->Instance->SMCR;
2227 /* Get the TIMx CCMR1 register value */
2228 tmpccmr1 = htim->Instance->CCMR1;
2230 /* Get the TIMx CCER register value */
2231 tmpccer = htim->Instance->CCER;
2233 /* Set the encoder Mode */
2234 tmpsmcr |= sConfig->EncoderMode;
2236 /* Select the Capture Compare 1 and the Capture Compare 2 as input */
2237 tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);
2238 tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8));
2240 /* Set the the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
2241 tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);
2242 tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);
2243 tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8);
2244 tmpccmr1 |= (sConfig->IC1Filter << 4) | (sConfig->IC2Filter << 12);
2246 /* Set the TI1 and the TI2 Polarities */
2247 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
2248 tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);
2249 tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4);
2251 /* Write to TIMx SMCR */
2252 htim->Instance->SMCR = tmpsmcr;
2254 /* Write to TIMx CCMR1 */
2255 htim->Instance->CCMR1 = tmpccmr1;
2257 /* Write to TIMx CCER */
2258 htim->Instance->CCER = tmpccer;
2260 /* Initialize the TIM state*/
2261 htim->State= HAL_TIM_STATE_READY;
2268 * @brief DeInitializes the TIM Encoder interface
2269 * @param htim: TIM Encoder handle
2270 * @retval HAL status
2272 HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)
2274 /* Check the parameters */
2275 assert_param(IS_TIM_INSTANCE(htim->Instance));
2277 htim->State = HAL_TIM_STATE_BUSY;
2279 /* Disable the TIM Peripheral Clock */
2280 __HAL_TIM_DISABLE(htim);
2282 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
2283 HAL_TIM_Encoder_MspDeInit(htim);
2285 /* Change TIM state */
2286 htim->State = HAL_TIM_STATE_RESET;
2295 * @brief Initializes the TIM Encoder Interface MSP.
2296 * @param htim: TIM handle
2299 __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
2301 /* NOTE : This function Should not be modified, when the callback is needed,
2302 the HAL_TIM_Encoder_MspInit could be implemented in the user file
2307 * @brief DeInitializes TIM Encoder Interface MSP.
2308 * @param htim: TIM handle
2311 __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)
2313 /* NOTE : This function Should not be modified, when the callback is needed,
2314 the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
2319 * @brief Starts the TIM Encoder Interface.
2320 * @param htim : TIM Encoder Interface handle
2321 * @param Channel : TIM Channels to be enabled
2322 * This parameter can be one of the following values:
2323 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2324 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2325 * @retval HAL status
2327 HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
2329 /* Check the parameters */
2330 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2332 /* Enable the encoder interface channels */
2337 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2342 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2347 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2348 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2352 /* Enable the Peripheral */
2353 __HAL_TIM_ENABLE(htim);
2355 /* Return function status */
2360 * @brief Stops the TIM Encoder Interface.
2361 * @param htim : TIM Encoder Interface handle
2362 * @param Channel : TIM Channels to be disabled
2363 * This parameter can be one of the following values:
2364 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2365 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2366 * @retval HAL status
2368 HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
2370 /* Check the parameters */
2371 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2373 /* Disable the Input Capture channels 1 and 2
2374 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
2379 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2384 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2389 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2390 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2395 /* Disable the Peripheral */
2396 __HAL_TIM_DISABLE(htim);
2398 /* Return function status */
2403 * @brief Starts the TIM Encoder Interface in interrupt mode.
2404 * @param htim : TIM Encoder Interface handle
2405 * @param Channel : TIM Channels to be enabled
2406 * This parameter can be one of the following values:
2407 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2408 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2409 * @retval HAL status
2411 HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2413 /* Check the parameters */
2414 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2416 /* Enable the encoder interface channels */
2417 /* Enable the capture compare Interrupts 1 and/or 2 */
2422 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2423 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2428 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2429 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2434 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2435 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2436 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
2437 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
2442 /* Enable the Peripheral */
2443 __HAL_TIM_ENABLE(htim);
2445 /* Return function status */
2450 * @brief Stops the TIM Encoder Interface in interrupt mode.
2451 * @param htim : TIM Encoder Interface handle
2452 * @param Channel : TIM Channels to be disabled
2453 * This parameter can be one of the following values:
2454 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2455 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2456 * @retval HAL status
2458 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
2460 /* Check the parameters */
2461 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2463 /* Disable the Input Capture channels 1 and 2
2464 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
2465 if(Channel == TIM_CHANNEL_1)
2467 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2469 /* Disable the capture compare Interrupts 1 */
2470 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2472 else if(Channel == TIM_CHANNEL_2)
2474 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2476 /* Disable the capture compare Interrupts 2 */
2477 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2481 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2482 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2484 /* Disable the capture compare Interrupts 1 and 2 */
2485 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
2486 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
2489 /* Disable the Peripheral */
2490 __HAL_TIM_DISABLE(htim);
2492 /* Change the htim state */
2493 htim->State = HAL_TIM_STATE_READY;
2495 /* Return function status */
2500 * @brief Starts the TIM Encoder Interface in DMA mode.
2501 * @param htim : TIM Encoder Interface handle
2502 * @param Channel : TIM Channels to be enabled
2503 * This parameter can be one of the following values:
2504 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2505 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2506 * @param pData1: The destination Buffer address for IC1.
2507 * @param pData2: The destination Buffer address for IC2.
2508 * @param Length: The length of data to be transferred from TIM peripheral to memory.
2509 * @retval HAL status
2511 HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1, uint32_t *pData2, uint16_t Length)
2513 /* Check the parameters */
2514 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2516 if((htim->State == HAL_TIM_STATE_BUSY))
2520 else if((htim->State == HAL_TIM_STATE_READY))
2522 if((((pData1 == 0) || (pData2 == 0) )) && (Length > 0))
2528 htim->State = HAL_TIM_STATE_BUSY;
2536 /* Set the DMA Period elapsed callback */
2537 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
2539 /* Set the DMA error callback */
2540 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
2542 /* Enable the DMA channel */
2543 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t )pData1, Length);
2545 /* Enable the TIM Input Capture DMA request */
2546 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
2548 /* Enable the Peripheral */
2549 __HAL_TIM_ENABLE(htim);
2551 /* Enable the Capture compare channel */
2552 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2558 /* Set the DMA Period elapsed callback */
2559 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
2561 /* Set the DMA error callback */
2562 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError;
2563 /* Enable the DMA channel */
2564 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length);
2566 /* Enable the TIM Input Capture DMA request */
2567 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
2569 /* Enable the Peripheral */
2570 __HAL_TIM_ENABLE(htim);
2572 /* Enable the Capture compare channel */
2573 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2577 case TIM_CHANNEL_ALL:
2579 /* Set the DMA Period elapsed callback */
2580 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
2582 /* Set the DMA error callback */
2583 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
2585 /* Enable the DMA channel */
2586 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length);
2588 /* Set the DMA Period elapsed callback */
2589 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
2591 /* Set the DMA error callback */
2592 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
2594 /* Enable the DMA channel */
2595 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length);
2597 /* Enable the Peripheral */
2598 __HAL_TIM_ENABLE(htim);
2600 /* Enable the Capture compare channel */
2601 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
2602 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
2604 /* Enable the TIM Input Capture DMA request */
2605 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
2606 /* Enable the TIM Input Capture DMA request */
2607 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
2614 /* Return function status */
2619 * @brief Stops the TIM Encoder Interface in DMA mode.
2620 * @param htim : TIM Encoder Interface handle
2621 * @param Channel : TIM Channels to be enabled
2622 * This parameter can be one of the following values:
2623 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2624 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2625 * @retval HAL status
2627 HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
2629 /* Check the parameters */
2630 assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
2632 /* Disable the Input Capture channels 1 and 2
2633 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
2634 if(Channel == TIM_CHANNEL_1)
2636 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2638 /* Disable the capture compare DMA Request 1 */
2639 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
2641 else if(Channel == TIM_CHANNEL_2)
2643 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2645 /* Disable the capture compare DMA Request 2 */
2646 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
2650 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
2651 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
2653 /* Disable the capture compare DMA Request 1 and 2 */
2654 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
2655 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
2658 /* Disable the Peripheral */
2659 __HAL_TIM_DISABLE(htim);
2661 /* Change the htim state */
2662 htim->State = HAL_TIM_STATE_READY;
2664 /* Return function status */
2671 /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
2672 * @brief IRQ handler management
2675 ==============================================================================
2676 ##### IRQ handler management #####
2677 ==============================================================================
2679 This section provides Timer IRQ handler function.
2685 * @brief This function handles TIM interrupts requests.
2686 * @param htim: TIM handle
2689 void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
2691 /* Capture compare 1 event */
2692 if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)
2694 if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_CC1) !=RESET)
2697 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
2698 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
2700 /* Input capture event */
2701 if((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00)
2703 HAL_TIM_IC_CaptureCallback(htim);
2705 /* Output compare event */
2708 HAL_TIM_OC_DelayElapsedCallback(htim);
2709 HAL_TIM_PWM_PulseFinishedCallback(htim);
2711 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
2715 /* Capture compare 2 event */
2716 if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET)
2718 if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_CC2) !=RESET)
2720 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);
2721 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
2722 /* Input capture event */
2723 if((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00)
2725 HAL_TIM_IC_CaptureCallback(htim);
2727 /* Output compare event */
2730 HAL_TIM_OC_DelayElapsedCallback(htim);
2731 HAL_TIM_PWM_PulseFinishedCallback(htim);
2733 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
2736 /* Capture compare 3 event */
2737 if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET)
2739 if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_CC3) !=RESET)
2741 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);
2742 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
2743 /* Input capture event */
2744 if((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00)
2746 HAL_TIM_IC_CaptureCallback(htim);
2748 /* Output compare event */
2751 HAL_TIM_OC_DelayElapsedCallback(htim);
2752 HAL_TIM_PWM_PulseFinishedCallback(htim);
2754 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
2757 /* Capture compare 4 event */
2758 if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET)
2760 if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_CC4) !=RESET)
2762 __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
2763 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
2764 /* Input capture event */
2765 if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00)
2767 HAL_TIM_IC_CaptureCallback(htim);
2769 /* Output compare event */
2772 HAL_TIM_OC_DelayElapsedCallback(htim);
2773 HAL_TIM_PWM_PulseFinishedCallback(htim);
2775 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
2778 /* TIM Update event */
2779 if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)
2781 if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_UPDATE) !=RESET)
2783 __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
2784 HAL_TIM_PeriodElapsedCallback(htim);
2787 /* TIM Break input event */
2788 if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK) != RESET)
2790 if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_BREAK) !=RESET)
2792 __HAL_TIM_CLEAR_IT(htim, TIM_IT_BREAK);
2793 HAL_TIMEx_BreakCallback(htim);
2796 /* TIM Trigger detection event */
2797 if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_TRIGGER) != RESET)
2799 if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_TRIGGER) !=RESET)
2801 __HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);
2802 HAL_TIM_TriggerCallback(htim);
2805 /* TIM commutation event */
2806 if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_COM) != RESET)
2808 if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_COM) !=RESET)
2810 __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_COM);
2811 HAL_TIMEx_CommutationCallback(htim);
2820 /** @defgroup TIM_Exported_Functions_Group8 Peripheral Control functions
2821 * @brief Peripheral Control functions
2824 ==============================================================================
2825 ##### Peripheral Control functions #####
2826 ==============================================================================
2828 This section provides functions allowing to:
2829 (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
2830 (+) Configure External Clock source.
2831 (+) Configure Complementary channels, break features and dead time.
2832 (+) Configure Master and the Slave synchronization.
2833 (+) Configure the DMA Burst Mode.
2840 * @brief Initializes the TIM Output Compare Channels according to the specified
2841 * parameters in the TIM_OC_InitTypeDef.
2842 * @param htim: TIM Output Compare handle
2843 * @param sConfig: TIM Output Compare configuration structure
2844 * @param Channel : TIM Channels to be enabled
2845 * This parameter can be one of the following values:
2846 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2847 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2848 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2849 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2850 * @retval HAL status
2852 __weak HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel)
2854 /* Check the parameters */
2855 assert_param(IS_TIM_CHANNELS(Channel));
2856 assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
2857 assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
2858 assert_param(IS_TIM_OCN_POLARITY(sConfig->OCNPolarity));
2859 assert_param(IS_TIM_OCNIDLE_STATE(sConfig->OCNIdleState));
2860 assert_param(IS_TIM_OCIDLE_STATE(sConfig->OCIdleState));
2862 /* Check input state */
2865 htim->State = HAL_TIM_STATE_BUSY;
2871 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
2872 /* Configure the TIM Channel 1 in Output Compare */
2873 TIM_OC1_SetConfig(htim->Instance, sConfig);
2879 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2880 /* Configure the TIM Channel 2 in Output Compare */
2881 TIM_OC2_SetConfig(htim->Instance, sConfig);
2887 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
2888 /* Configure the TIM Channel 3 in Output Compare */
2889 TIM_OC3_SetConfig(htim->Instance, sConfig);
2895 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
2896 /* Configure the TIM Channel 4 in Output Compare */
2897 TIM_OC4_SetConfig(htim->Instance, sConfig);
2904 htim->State = HAL_TIM_STATE_READY;
2912 * @brief Initializes the TIM Input Capture Channels according to the specified
2913 * parameters in the TIM_IC_InitTypeDef.
2914 * @param htim: TIM IC handle
2915 * @param sConfig: TIM Input Capture configuration structure
2916 * @param Channel : TIM Channels to be enabled
2917 * This parameter can be one of the following values:
2918 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2919 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2920 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2921 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2922 * @retval HAL status
2924 HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_IC_InitTypeDef* sConfig, uint32_t Channel)
2926 /* Check the parameters */
2927 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
2928 assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
2929 assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
2930 assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
2931 assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
2935 htim->State = HAL_TIM_STATE_BUSY;
2937 if (Channel == TIM_CHANNEL_1)
2939 /* TI1 Configuration */
2940 TIM_TI1_SetConfig(htim->Instance,
2941 sConfig->ICPolarity,
2942 sConfig->ICSelection,
2945 /* Reset the IC1PSC Bits */
2946 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
2948 /* Set the IC1PSC value */
2949 htim->Instance->CCMR1 |= sConfig->ICPrescaler;
2951 else if (Channel == TIM_CHANNEL_2)
2953 /* TI2 Configuration */
2954 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
2956 TIM_TI2_SetConfig(htim->Instance,
2957 sConfig->ICPolarity,
2958 sConfig->ICSelection,
2961 /* Reset the IC2PSC Bits */
2962 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
2964 /* Set the IC2PSC value */
2965 htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8);
2967 else if (Channel == TIM_CHANNEL_3)
2969 /* TI3 Configuration */
2970 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
2972 TIM_TI3_SetConfig(htim->Instance,
2973 sConfig->ICPolarity,
2974 sConfig->ICSelection,
2977 /* Reset the IC3PSC Bits */
2978 htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
2980 /* Set the IC3PSC value */
2981 htim->Instance->CCMR2 |= sConfig->ICPrescaler;
2985 /* TI4 Configuration */
2986 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
2988 TIM_TI4_SetConfig(htim->Instance,
2989 sConfig->ICPolarity,
2990 sConfig->ICSelection,
2993 /* Reset the IC4PSC Bits */
2994 htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
2996 /* Set the IC4PSC value */
2997 htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8);
3000 htim->State = HAL_TIM_STATE_READY;
3008 * @brief Initializes the TIM PWM channels according to the specified
3009 * parameters in the TIM_OC_InitTypeDef.
3010 * @param htim: TIM handle
3011 * @param sConfig: TIM PWM configuration structure
3012 * @param Channel : TIM Channels to be enabled
3013 * This parameter can be one of the following values:
3014 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3015 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3016 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
3017 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
3018 * @retval HAL status
3020 __weak HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel)
3024 /* Check the parameters */
3025 assert_param(IS_TIM_CHANNELS(Channel));
3026 assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
3027 assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
3028 assert_param(IS_TIM_OCN_POLARITY(sConfig->OCNPolarity));
3029 assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
3030 assert_param(IS_TIM_OCNIDLE_STATE(sConfig->OCNIdleState));
3031 assert_param(IS_TIM_OCIDLE_STATE(sConfig->OCIdleState));
3033 htim->State = HAL_TIM_STATE_BUSY;
3039 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3040 /* Configure the Channel 1 in PWM mode */
3041 TIM_OC1_SetConfig(htim->Instance, sConfig);
3043 /* Set the Preload enable bit for channel1 */
3044 htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
3046 /* Configure the Output Fast mode */
3047 htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
3048 htim->Instance->CCMR1 |= sConfig->OCFastMode;
3054 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3055 /* Configure the Channel 2 in PWM mode */
3056 TIM_OC2_SetConfig(htim->Instance, sConfig);
3058 /* Set the Preload enable bit for channel2 */
3059 htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
3061 /* Configure the Output Fast mode */
3062 htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
3063 htim->Instance->CCMR1 |= sConfig->OCFastMode << 8;
3069 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
3070 /* Configure the Channel 3 in PWM mode */
3071 TIM_OC3_SetConfig(htim->Instance, sConfig);
3073 /* Set the Preload enable bit for channel3 */
3074 htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
3076 /* Configure the Output Fast mode */
3077 htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
3078 htim->Instance->CCMR2 |= sConfig->OCFastMode;
3084 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
3085 /* Configure the Channel 4 in PWM mode */
3086 TIM_OC4_SetConfig(htim->Instance, sConfig);
3088 /* Set the Preload enable bit for channel4 */
3089 htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
3091 /* Configure the Output Fast mode */
3092 htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
3093 htim->Instance->CCMR2 |= sConfig->OCFastMode << 8;
3101 htim->State = HAL_TIM_STATE_READY;
3109 * @brief Initializes the TIM One Pulse Channels according to the specified
3110 * parameters in the TIM_OnePulse_InitTypeDef.
3111 * @param htim: TIM One Pulse handle
3112 * @param sConfig: TIM One Pulse configuration structure
3113 * @param OutputChannel : TIM Channels to be enabled
3114 * This parameter can be one of the following values:
3115 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3116 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3117 * @param InputChannel : TIM Channels to be enabled
3118 * This parameter can be one of the following values:
3119 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3120 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3121 * @retval HAL status
3123 HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef* sConfig, uint32_t OutputChannel, uint32_t InputChannel)
3125 TIM_OC_InitTypeDef temp1;
3127 /* Check the parameters */
3128 assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
3129 assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
3131 if(OutputChannel != InputChannel)
3135 htim->State = HAL_TIM_STATE_BUSY;
3137 /* Extract the Ouput compare configuration from sConfig structure */
3138 temp1.OCMode = sConfig->OCMode;
3139 temp1.Pulse = sConfig->Pulse;
3140 temp1.OCPolarity = sConfig->OCPolarity;
3141 temp1.OCNPolarity = sConfig->OCNPolarity;
3142 temp1.OCIdleState = sConfig->OCIdleState;
3143 temp1.OCNIdleState = sConfig->OCNIdleState;
3145 switch (OutputChannel)
3149 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3151 TIM_OC1_SetConfig(htim->Instance, &temp1);
3156 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3158 TIM_OC2_SetConfig(htim->Instance, &temp1);
3164 switch (InputChannel)
3168 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3170 TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
3171 sConfig->ICSelection, sConfig->ICFilter);
3173 /* Reset the IC1PSC Bits */
3174 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
3176 /* Select the Trigger source */
3177 htim->Instance->SMCR &= ~TIM_SMCR_TS;
3178 htim->Instance->SMCR |= TIM_TS_TI1FP1;
3180 /* Select the Slave Mode */
3181 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
3182 htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
3187 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3189 TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
3190 sConfig->ICSelection, sConfig->ICFilter);
3192 /* Reset the IC2PSC Bits */
3193 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
3195 /* Select the Trigger source */
3196 htim->Instance->SMCR &= ~TIM_SMCR_TS;
3197 htim->Instance->SMCR |= TIM_TS_TI2FP2;
3199 /* Select the Slave Mode */
3200 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
3201 htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
3209 htim->State = HAL_TIM_STATE_READY;
3222 * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
3223 * @param htim: TIM handle
3224 * @param BurstBaseAddress: TIM Base address from when the DMA will starts the Data write
3225 * This parameters can be on of the following values:
3226 * @arg TIM_DMABase_CR1
3227 * @arg TIM_DMABase_CR2
3228 * @arg TIM_DMABase_SMCR
3229 * @arg TIM_DMABase_DIER
3230 * @arg TIM_DMABase_SR
3231 * @arg TIM_DMABase_EGR
3232 * @arg TIM_DMABase_CCMR1
3233 * @arg TIM_DMABase_CCMR2
3234 * @arg TIM_DMABase_CCER
3235 * @arg TIM_DMABase_CNT
3236 * @arg TIM_DMABase_PSC
3237 * @arg TIM_DMABase_ARR
3238 * @arg TIM_DMABase_RCR
3239 * @arg TIM_DMABase_CCR1
3240 * @arg TIM_DMABase_CCR2
3241 * @arg TIM_DMABase_CCR3
3242 * @arg TIM_DMABase_CCR4
3243 * @arg TIM_DMABase_BDTR
3244 * @arg TIM_DMABase_DCR
3245 * @param BurstRequestSrc: TIM DMA Request sources
3246 * This parameters can be on of the following values:
3247 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
3248 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
3249 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
3250 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
3251 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
3252 * @arg TIM_DMA_COM: TIM Commutation DMA source
3253 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
3254 * @param BurstBuffer: The Buffer address.
3255 * @param BurstLength: DMA Burst length. This parameter can be one value
3256 * between: TIM_DMABurstLength_1Transfer and TIM_DMABurstLength_18Transfers.
3257 * @retval HAL status
3259 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress, uint32_t BurstRequestSrc,
3260 uint32_t* BurstBuffer, uint32_t BurstLength)
3262 /* Check the parameters */
3263 assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
3264 assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
3265 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
3266 assert_param(IS_TIM_DMA_LENGTH(BurstLength));
3268 if((htim->State == HAL_TIM_STATE_BUSY))
3272 else if((htim->State == HAL_TIM_STATE_READY))
3274 if((BurstBuffer == 0 ) && (BurstLength > 0))
3280 htim->State = HAL_TIM_STATE_BUSY;
3283 switch(BurstRequestSrc)
3285 case TIM_DMA_UPDATE:
3287 /* Set the DMA Period elapsed callback */
3288 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
3290 /* Set the DMA error callback */
3291 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = HAL_TIM_DMAError ;
3293 /* Enable the DMA channel */
3294 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
3299 /* Set the DMA Period elapsed callback */
3300 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
3302 /* Set the DMA error callback */
3303 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
3305 /* Enable the DMA channel */
3306 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
3311 /* Set the DMA Period elapsed callback */
3312 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
3314 /* Set the DMA error callback */
3315 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
3317 /* Enable the DMA channel */
3318 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
3323 /* Set the DMA Period elapsed callback */
3324 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
3326 /* Set the DMA error callback */
3327 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
3329 /* Enable the DMA channel */
3330 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
3335 /* Set the DMA Period elapsed callback */
3336 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
3338 /* Set the DMA error callback */
3339 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
3341 /* Enable the DMA channel */
3342 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
3347 /* Set the DMA Period elapsed callback */
3348 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = HAL_TIMEx_DMACommutationCplt;
3350 /* Set the DMA error callback */
3351 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = HAL_TIM_DMAError ;
3353 /* Enable the DMA channel */
3354 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
3357 case TIM_DMA_TRIGGER:
3359 /* Set the DMA Period elapsed callback */
3360 htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
3362 /* Set the DMA error callback */
3363 htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = HAL_TIM_DMAError ;
3365 /* Enable the DMA channel */
3366 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
3372 /* configure the DMA Burst Mode */
3373 htim->Instance->DCR = BurstBaseAddress | BurstLength;
3375 /* Enable the TIM DMA Request */
3376 __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
3378 htim->State = HAL_TIM_STATE_READY;
3380 /* Return function status */
3385 * @brief Stops the TIM DMA Burst mode
3386 * @param htim: TIM handle
3387 * @param BurstRequestSrc: TIM DMA Request sources to disable
3388 * @retval HAL status
3390 HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
3392 /* Check the parameters */
3393 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
3395 /* Abort the DMA transfer (at least disable the DMA channel) */
3396 switch(BurstRequestSrc)
3398 case TIM_DMA_UPDATE:
3400 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_UPDATE]);
3405 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC1]);
3410 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC2]);
3415 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC3]);
3420 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC4]);
3425 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_COMMUTATION]);
3428 case TIM_DMA_TRIGGER:
3430 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_TRIGGER]);
3437 /* Disable the TIM Update DMA request */
3438 __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
3440 /* Return function status */
3445 * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
3446 * @param htim: TIM handle
3447 * @param BurstBaseAddress: TIM Base address from when the DMA will starts the Data read
3448 * This parameters can be on of the following values:
3449 * @arg TIM_DMABase_CR1
3450 * @arg TIM_DMABase_CR2
3451 * @arg TIM_DMABase_SMCR
3452 * @arg TIM_DMABase_DIER
3453 * @arg TIM_DMABase_SR
3454 * @arg TIM_DMABase_EGR
3455 * @arg TIM_DMABase_CCMR1
3456 * @arg TIM_DMABase_CCMR2
3457 * @arg TIM_DMABase_CCER
3458 * @arg TIM_DMABase_CNT
3459 * @arg TIM_DMABase_PSC
3460 * @arg TIM_DMABase_ARR
3461 * @arg TIM_DMABase_RCR
3462 * @arg TIM_DMABase_CCR1
3463 * @arg TIM_DMABase_CCR2
3464 * @arg TIM_DMABase_CCR3
3465 * @arg TIM_DMABase_CCR4
3466 * @arg TIM_DMABase_BDTR
3467 * @arg TIM_DMABase_DCR
3468 * @param BurstRequestSrc: TIM DMA Request sources
3469 * This parameters can be on of the following values:
3470 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
3471 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
3472 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
3473 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
3474 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
3475 * @arg TIM_DMA_COM: TIM Commutation DMA source
3476 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
3477 * @param BurstBuffer: The Buffer address.
3478 * @param BurstLength: DMA Burst length. This parameter can be one value
3479 * between: TIM_DMABurstLength_1Transfer and TIM_DMABurstLength_18Transfers.
3480 * @retval HAL status
3482 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress, uint32_t BurstRequestSrc,
3483 uint32_t *BurstBuffer, uint32_t BurstLength)
3485 /* Check the parameters */
3486 assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
3487 assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
3488 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
3489 assert_param(IS_TIM_DMA_LENGTH(BurstLength));
3491 if((htim->State == HAL_TIM_STATE_BUSY))
3495 else if((htim->State == HAL_TIM_STATE_READY))
3497 if((BurstBuffer == 0 ) && (BurstLength > 0))
3503 htim->State = HAL_TIM_STATE_BUSY;
3506 switch(BurstRequestSrc)
3508 case TIM_DMA_UPDATE:
3510 /* Set the DMA Period elapsed callback */
3511 htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
3513 /* Set the DMA error callback */
3514 htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = HAL_TIM_DMAError ;
3516 /* Enable the DMA channel */
3517 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
3522 /* Set the DMA Period elapsed callback */
3523 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
3525 /* Set the DMA error callback */
3526 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
3528 /* Enable the DMA channel */
3529 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
3534 /* Set the DMA Period elapsed callback */
3535 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
3537 /* Set the DMA error callback */
3538 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
3540 /* Enable the DMA channel */
3541 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
3546 /* Set the DMA Period elapsed callback */
3547 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
3549 /* Set the DMA error callback */
3550 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
3552 /* Enable the DMA channel */
3553 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
3558 /* Set the DMA Period elapsed callback */
3559 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
3561 /* Set the DMA error callback */
3562 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
3564 /* Enable the DMA channel */
3565 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
3570 /* Set the DMA Period elapsed callback */
3571 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = HAL_TIMEx_DMACommutationCplt;
3573 /* Set the DMA error callback */
3574 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = HAL_TIM_DMAError ;
3576 /* Enable the DMA channel */
3577 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
3580 case TIM_DMA_TRIGGER:
3582 /* Set the DMA Period elapsed callback */
3583 htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
3585 /* Set the DMA error callback */
3586 htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = HAL_TIM_DMAError ;
3588 /* Enable the DMA channel */
3589 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
3596 /* configure the DMA Burst Mode */
3597 htim->Instance->DCR = BurstBaseAddress | BurstLength;
3599 /* Enable the TIM DMA Request */
3600 __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
3602 htim->State = HAL_TIM_STATE_READY;
3604 /* Return function status */
3609 * @brief Stop the DMA burst reading
3610 * @param htim: TIM handle
3611 * @param BurstRequestSrc: TIM DMA Request sources to disable.
3612 * @retval HAL status
3614 HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
3616 /* Check the parameters */
3617 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
3619 /* Abort the DMA transfer (at least disable the DMA channel) */
3620 switch(BurstRequestSrc)
3622 case TIM_DMA_UPDATE:
3624 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_UPDATE]);
3629 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC1]);
3634 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC2]);
3639 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC3]);
3644 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC4]);
3649 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_COMMUTATION]);
3652 case TIM_DMA_TRIGGER:
3654 HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_TRIGGER]);
3661 /* Disable the TIM Update DMA request */
3662 __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
3664 /* Return function status */
3669 * @brief Generate a software event
3670 * @param htim: TIM handle
3671 * @param EventSource: specifies the event source.
3672 * This parameter can be one of the following values:
3673 * @arg TIM_EventSource_Update: Timer update Event source
3674 * @arg TIM_EventSource_CC1: Timer Capture Compare 1 Event source
3675 * @arg TIM_EventSource_CC2: Timer Capture Compare 2 Event source
3676 * @arg TIM_EventSource_CC3: Timer Capture Compare 3 Event source
3677 * @arg TIM_EventSource_CC4: Timer Capture Compare 4 Event source
3678 * @arg TIM_EventSource_COM: Timer COM event source
3679 * @arg TIM_EventSource_Trigger: Timer Trigger Event source
3680 * @arg TIM_EventSource_Break: Timer Break event source
3681 * @arg TIM_EventSource_Break2: Timer Break2 event source
3683 * @note TIM_EventSource_Break2 isn't relevant for STM32F37xx and STM32F38xx
3687 HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
3689 /* Check the parameters */
3690 assert_param(IS_TIM_INSTANCE(htim->Instance));
3691 assert_param(IS_TIM_EVENT_SOURCE(EventSource));
3693 /* Process Locked */
3696 /* Change the TIM state */
3697 htim->State = HAL_TIM_STATE_BUSY;
3699 /* Set the event sources */
3700 htim->Instance->EGR = EventSource;
3702 /* Change the TIM state */
3703 htim->State = HAL_TIM_STATE_READY;
3707 /* Return function status */
3712 * @brief Configures the OCRef clear feature
3713 * @param htim: TIM handle
3714 * @param sClearInputConfig: pointer to a TIM_ClearInputConfigTypeDef structure that
3715 * contains the OCREF clear feature and parameters for the TIM peripheral.
3716 * @param Channel: specifies the TIM Channel
3717 * This parameter can be one of the following values:
3718 * @arg TIM_Channel_1: TIM Channel 1
3719 * @arg TIM_Channel_2: TIM Channel 2
3720 * @arg TIM_Channel_3: TIM Channel 3
3721 * @arg TIM_Channel_4: TIM Channel 4
3722 * @retval HAL status
3724 __weak HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim, TIM_ClearInputConfigTypeDef * sClearInputConfig, uint32_t Channel)
3726 /* Check the parameters */
3727 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
3728 assert_param(IS_TIM_CHANNELS(Channel));
3729 assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
3731 /* Process Locked */
3734 htim->State = HAL_TIM_STATE_BUSY;
3736 if(sClearInputConfig->ClearInputSource == TIM_CLEARINPUTSOURCE_ETR)
3738 /* Check the parameters */
3739 assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
3740 assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
3741 assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
3743 TIM_ETR_SetConfig(htim->Instance,
3744 sClearInputConfig->ClearInputPrescaler,
3745 sClearInputConfig->ClearInputPolarity,
3746 sClearInputConfig->ClearInputFilter);
3753 if(sClearInputConfig->ClearInputState != RESET)
3755 /* Enable the Ocref clear feature for Channel 1 */
3756 htim->Instance->CCMR1 |= TIM_CCMR1_OC1CE;
3760 /* Disable the Ocref clear feature for Channel 1 */
3761 htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1CE;
3767 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
3768 if(sClearInputConfig->ClearInputState != RESET)
3770 /* Enable the Ocref clear feature for Channel 2 */
3771 htim->Instance->CCMR1 |= TIM_CCMR1_OC2CE;
3775 /* Disable the Ocref clear feature for Channel 2 */
3776 htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2CE;
3782 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
3783 if(sClearInputConfig->ClearInputState != RESET)
3785 /* Enable the Ocref clear feature for Channel 3 */
3786 htim->Instance->CCMR2 |= TIM_CCMR2_OC3CE;
3790 /* Disable the Ocref clear feature for Channel 3 */
3791 htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3CE;
3797 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
3798 if(sClearInputConfig->ClearInputState != RESET)
3800 /* Enable the Ocref clear feature for Channel 4 */
3801 htim->Instance->CCMR2 |= TIM_CCMR2_OC4CE;
3805 /* Disable the Ocref clear feature for Channel 4 */
3806 htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4CE;
3814 htim->State = HAL_TIM_STATE_READY;
3822 * @brief Configures the clock source to be used
3823 * @param htim: TIM handle
3824 * @param sClockSourceConfig: pointer to a TIM_ClockConfigTypeDef structure that
3825 * contains the clock source information for the TIM peripheral.
3826 * @retval HAL status
3828 HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, TIM_ClockConfigTypeDef * sClockSourceConfig)
3830 uint32_t tmpsmcr = 0;
3832 /* Process Locked */
3835 htim->State = HAL_TIM_STATE_BUSY;
3837 /* Check the parameters */
3838 assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
3839 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
3840 assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
3841 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
3843 /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
3844 tmpsmcr = htim->Instance->SMCR;
3845 tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
3846 tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
3847 htim->Instance->SMCR = tmpsmcr;
3849 switch (sClockSourceConfig->ClockSource)
3851 case TIM_CLOCKSOURCE_INTERNAL:
3853 assert_param(IS_TIM_INSTANCE(htim->Instance));
3854 /* Disable slave mode to clock the prescaler directly with the internal clock */
3855 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
3859 case TIM_CLOCKSOURCE_ETRMODE1:
3861 /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
3862 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
3864 /* Configure the ETR Clock source */
3865 TIM_ETR_SetConfig(htim->Instance,
3866 sClockSourceConfig->ClockPrescaler,
3867 sClockSourceConfig->ClockPolarity,
3868 sClockSourceConfig->ClockFilter);
3869 /* Get the TIMx SMCR register value */
3870 tmpsmcr = htim->Instance->SMCR;
3871 /* Reset the SMS and TS Bits */
3872 tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
3873 /* Select the External clock mode1 and the ETRF trigger */
3874 tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
3875 /* Write to TIMx SMCR */
3876 htim->Instance->SMCR = tmpsmcr;
3880 case TIM_CLOCKSOURCE_ETRMODE2:
3882 /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
3883 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
3885 /* Configure the ETR Clock source */
3886 TIM_ETR_SetConfig(htim->Instance,
3887 sClockSourceConfig->ClockPrescaler,
3888 sClockSourceConfig->ClockPolarity,
3889 sClockSourceConfig->ClockFilter);
3890 /* Enable the External clock mode2 */
3891 htim->Instance->SMCR |= TIM_SMCR_ECE;
3895 case TIM_CLOCKSOURCE_TI1:
3897 /* Check whether or not the timer instance supports external clock mode 1 */
3898 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
3900 TIM_TI1_ConfigInputStage(htim->Instance,
3901 sClockSourceConfig->ClockPolarity,
3902 sClockSourceConfig->ClockFilter);
3903 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
3906 case TIM_CLOCKSOURCE_TI2:
3908 /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
3909 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
3911 TIM_TI2_ConfigInputStage(htim->Instance,
3912 sClockSourceConfig->ClockPolarity,
3913 sClockSourceConfig->ClockFilter);
3914 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
3917 case TIM_CLOCKSOURCE_TI1ED:
3919 /* Check whether or not the timer instance supports external clock mode 1 */
3920 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
3922 TIM_TI1_ConfigInputStage(htim->Instance,
3923 sClockSourceConfig->ClockPolarity,
3924 sClockSourceConfig->ClockFilter);
3925 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
3928 case TIM_CLOCKSOURCE_ITR0:
3930 /* Check whether or not the timer instance supports external clock mode 1 */
3931 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
3933 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR0);
3936 case TIM_CLOCKSOURCE_ITR1:
3938 /* Check whether or not the timer instance supports external clock mode 1 */
3939 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
3941 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR1);
3944 case TIM_CLOCKSOURCE_ITR2:
3946 /* Check whether or not the timer instance supports external clock mode 1 */
3947 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
3949 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR2);
3952 case TIM_CLOCKSOURCE_ITR3:
3954 /* Check whether or not the timer instance supports external clock mode 1 */
3955 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
3957 TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR3);
3964 htim->State = HAL_TIM_STATE_READY;
3972 * @brief Selects the signal connected to the TI1 input: direct from CH1_input
3973 * or a XOR combination between CH1_input, CH2_input & CH3_input
3974 * @param htim: TIM handle.
3975 * @param TI1_Selection: Indicate whether or not channel 1 is connected to the
3976 * output of a XOR gate.
3977 * This parameter can be one of the following values:
3978 * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
3979 * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
3980 * pins are connected to the TI1 input (XOR combination)
3981 * @retval HAL status
3983 HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
3985 uint32_t tmpcr2 = 0;
3987 /* Check the parameters */
3988 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
3989 assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
3991 /* Get the TIMx CR2 register value */
3992 tmpcr2 = htim->Instance->CR2;
3994 /* Reset the TI1 selection */
3995 tmpcr2 &= ~TIM_CR2_TI1S;
3997 /* Set the the TI1 selection */
3998 tmpcr2 |= TI1_Selection;
4000 /* Write to TIMxCR2 */
4001 htim->Instance->CR2 = tmpcr2;
4007 * @brief Configures the TIM in Slave mode
4008 * @param htim: TIM handle.
4009 * @param sSlaveConfig: pointer to a TIM_SlaveConfigTypeDef structure that
4010 * contains the selected trigger (internal trigger input, filtered
4011 * timer input or external trigger input) and the ) and the Slave
4012 * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
4013 * @retval HAL status
4015 HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchronization(TIM_HandleTypeDef *htim, TIM_SlaveConfigTypeDef * sSlaveConfig)
4017 uint32_t tmpsmcr = 0;
4018 uint32_t tmpccmr1 = 0;
4019 uint32_t tmpccer = 0;
4021 /* Check the parameters */
4022 assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
4023 assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
4024 assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
4028 htim->State = HAL_TIM_STATE_BUSY;
4030 /* Get the TIMx SMCR register value */
4031 tmpsmcr = htim->Instance->SMCR;
4033 /* Reset the Trigger Selection Bits */
4034 tmpsmcr &= ~TIM_SMCR_TS;
4035 /* Set the Input Trigger source */
4036 tmpsmcr |= sSlaveConfig->InputTrigger;
4038 /* Reset the slave mode Bits */
4039 tmpsmcr &= ~TIM_SMCR_SMS;
4040 /* Set the slave mode */
4041 tmpsmcr |= sSlaveConfig->SlaveMode;
4043 /* Write to TIMx SMCR */
4044 htim->Instance->SMCR = tmpsmcr;
4046 /* Configure the trigger prescaler, filter, and polarity */
4047 switch (sSlaveConfig->InputTrigger)
4051 /* Check the parameters */
4052 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
4053 assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
4054 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
4055 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
4056 /* Configure the ETR Trigger source */
4057 TIM_ETR_SetConfig(htim->Instance,
4058 sSlaveConfig->TriggerPrescaler,
4059 sSlaveConfig->TriggerPolarity,
4060 sSlaveConfig->TriggerFilter);
4064 case TIM_TS_TI1F_ED:
4066 /* Check the parameters */
4067 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4068 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
4069 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
4071 /* Disable the Channel 1: Reset the CC1E Bit */
4072 tmpccer = htim->Instance->CCER;
4073 htim->Instance->CCER &= ~TIM_CCER_CC1E;
4074 tmpccmr1 = htim->Instance->CCMR1;
4076 /* Set the filter */
4077 tmpccmr1 &= ~TIM_CCMR1_IC1F;
4078 tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4);
4080 /* Write to TIMx CCMR1 and CCER registers */
4081 htim->Instance->CCMR1 = tmpccmr1;
4082 htim->Instance->CCER = tmpccer;
4089 /* Check the parameters */
4090 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4091 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
4092 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
4094 /* Configure TI1 Filter and Polarity */
4095 TIM_TI1_ConfigInputStage(htim->Instance,
4096 sSlaveConfig->TriggerPolarity,
4097 sSlaveConfig->TriggerFilter);
4103 /* Check the parameters */
4104 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4105 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
4106 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
4108 /* Configure TI2 Filter and Polarity */
4109 TIM_TI2_ConfigInputStage(htim->Instance,
4110 sSlaveConfig->TriggerPolarity,
4111 sSlaveConfig->TriggerFilter);
4117 /* Check the parameter */
4118 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4124 /* Check the parameter */
4125 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4131 /* Check the parameter */
4132 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4138 /* Check the parameter */
4139 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4147 htim->State = HAL_TIM_STATE_READY;
4155 * @brief Read the captured value from Capture Compare unit
4156 * @param htim: TIM handle.
4157 * @param Channel : TIM Channels to be enabled
4158 * This parameter can be one of the following values:
4159 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
4160 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
4161 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
4162 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
4163 * @retval Captured value
4165 uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)
4167 uint32_t tmpreg = 0;
4175 /* Check the parameters */
4176 assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
4178 /* Return the capture 1 value */
4179 tmpreg = htim->Instance->CCR1;
4185 /* Check the parameters */
4186 assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
4188 /* Return the capture 2 value */
4189 tmpreg = htim->Instance->CCR2;
4196 /* Check the parameters */
4197 assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
4199 /* Return the capture 3 value */
4200 tmpreg = htim->Instance->CCR3;
4207 /* Check the parameters */
4208 assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
4210 /* Return the capture 4 value */
4211 tmpreg = htim->Instance->CCR4;
4228 /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
4229 * @brief TIM Callbacks functions
4232 ==============================================================================
4233 ##### TIM Callbacks functions #####
4234 ==============================================================================
4236 This section provides TIM callback functions:
4237 (+) Timer Period elapsed callback
4238 (+) Timer Output Compare callback
4239 (+) Timer Input capture callback
4240 (+) Timer Trigger callback
4241 (+) Timer Error callback
4248 * @brief Period elapsed callback in non blocking mode
4249 * @param htim : TIM handle
4252 __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
4254 /* NOTE : This function Should not be modified, when the callback is needed,
4255 the __HAL_TIM_PeriodElapsedCallback could be implemented in the user file
4260 * @brief Output Compare callback in non blocking mode
4261 * @param htim : TIM OC handle
4264 __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
4266 /* NOTE : This function Should not be modified, when the callback is needed,
4267 the __HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
4271 * @brief Input Capture callback in non blocking mode
4272 * @param htim : TIM IC handle
4275 __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
4277 /* NOTE : This function Should not be modified, when the callback is needed,
4278 the __HAL_TIM_IC_CaptureCallback could be implemented in the user file
4283 * @brief PWM Pulse finished callback in non blocking mode
4284 * @param htim : TIM handle
4287 __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
4289 /* NOTE : This function Should not be modified, when the callback is needed,
4290 the __HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
4295 * @brief Hall Trigger detection callback in non blocking mode
4296 * @param htim : TIM handle
4299 __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
4301 /* NOTE : This function Should not be modified, when the callback is needed,
4302 the HAL_TIM_TriggerCallback could be implemented in the user file
4307 * @brief Timer error callback in non blocking mode
4308 * @param htim : TIM handle
4311 __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
4313 /* NOTE : This function Should not be modified, when the callback is needed,
4314 the HAL_TIM_ErrorCallback could be implemented in the user file
4322 /** @defgroup TIM_Exported_Functions_Group10 Peripheral State functions
4323 * @brief Peripheral State functions
4326 ==============================================================================
4327 ##### Peripheral State functions #####
4328 ==============================================================================
4330 This subsection permit to get in run-time the status of the peripheral
4338 * @brief Return the TIM Base state
4339 * @param htim: TIM Base handle
4342 HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(TIM_HandleTypeDef *htim)
4348 * @brief Return the TIM OC state
4349 * @param htim: TIM Ouput Compare handle
4352 HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(TIM_HandleTypeDef *htim)
4358 * @brief Return the TIM PWM state
4359 * @param htim: TIM handle
4362 HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(TIM_HandleTypeDef *htim)
4368 * @brief Return the TIM Input Capture state
4369 * @param htim: TIM IC handle
4372 HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(TIM_HandleTypeDef *htim)
4378 * @brief Return the TIM One Pulse Mode state
4379 * @param htim: TIM OPM handle
4382 HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(TIM_HandleTypeDef *htim)
4388 * @brief Return the TIM Encoder Mode state
4389 * @param htim: TIM Encoder handle
4392 HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(TIM_HandleTypeDef *htim)
4402 * @brief TIM DMA error callback
4403 * @param hdma : pointer to DMA handle.
4406 void HAL_TIM_DMAError(DMA_HandleTypeDef *hdma)
4408 TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
4410 htim->State= HAL_TIM_STATE_READY;
4412 HAL_TIM_ErrorCallback(htim);
4416 * @brief TIM DMA Delay Pulse complete callback.
4417 * @param hdma : pointer to DMA handle.
4420 void HAL_TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
4422 TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
4424 htim->State= HAL_TIM_STATE_READY;
4426 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
4428 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
4430 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
4432 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
4434 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
4436 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
4438 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
4440 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
4443 HAL_TIM_PWM_PulseFinishedCallback(htim);
4445 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
4448 * @brief TIM DMA Capture complete callback.
4449 * @param hdma : pointer to DMA handle.
4452 void HAL_TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
4454 TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
4456 htim->State= HAL_TIM_STATE_READY;
4458 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
4460 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
4462 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
4464 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
4466 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
4468 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
4470 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
4472 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
4475 HAL_TIM_IC_CaptureCallback(htim);
4477 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
4481 * @brief TIM DMA Period Elapse complete callback.
4482 * @param hdma : pointer to DMA handle.
4485 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
4487 TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
4489 htim->State= HAL_TIM_STATE_READY;
4491 HAL_TIM_PeriodElapsedCallback(htim);
4495 * @brief TIM DMA Trigger callback.
4496 * @param hdma : pointer to DMA handle.
4499 static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
4501 TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
4503 htim->State= HAL_TIM_STATE_READY;
4505 HAL_TIM_TriggerCallback(htim);
4509 * @brief Time Base configuration
4510 * @param TIMx: TIM periheral
4511 * @param Structure: TIM Base configuration structure
4514 void TIM_Base_SetConfig(TIM_TypeDef *TIMx, TIM_Base_InitTypeDef *Structure)
4516 uint32_t tmpcr1 = 0;
4519 /* Set TIM Time Base Unit parameters ---------------------------------------*/
4520 if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
4522 /* Select the Counter Mode */
4523 tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
4524 tmpcr1 |= Structure->CounterMode;
4527 if(IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
4529 /* Set the clock division */
4530 tmpcr1 &= ~TIM_CR1_CKD;
4531 tmpcr1 |= (uint32_t)Structure->ClockDivision;
4536 /* Set the Autoreload value */
4537 TIMx->ARR = (uint32_t)Structure->Period ;
4539 /* Set the Prescaler value */
4540 TIMx->PSC = (uint32_t)Structure->Prescaler;
4542 if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
4544 /* Set the Repetition Counter value */
4545 TIMx->RCR = Structure->RepetitionCounter;
4548 /* Generate an update event to reload the Prescaler
4549 and the repetition counter(only for TIM1 and TIM8) value immediatly */
4550 TIMx->EGR = TIM_EGR_UG;
4554 * @brief Time Ouput Compare 1 configuration
4555 * @param TIMx to select the TIM peripheral
4556 * @param OC_Config: The ouput configuration structure
4559 void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
4561 uint32_t tmpccmrx = 0;
4562 uint32_t tmpccer = 0;
4563 uint32_t tmpcr2 = 0;
4565 /* Disable the Channel 1: Reset the CC1E Bit */
4566 TIMx->CCER &= ~TIM_CCER_CC1E;
4568 /* Get the TIMx CCER register value */
4569 tmpccer = TIMx->CCER;
4570 /* Get the TIMx CR2 register value */
4573 /* Get the TIMx CCMR1 register value */
4574 tmpccmrx = TIMx->CCMR1;
4576 /* Reset the Output Compare Mode Bits */
4577 tmpccmrx &= ~TIM_CCMR1_OC1M;
4578 tmpccmrx &= ~TIM_CCMR1_CC1S;
4579 /* Select the Output Compare Mode */
4580 tmpccmrx |= OC_Config->OCMode;
4582 /* Reset the Output Polarity level */
4583 tmpccer &= ~TIM_CCER_CC1P;
4584 /* Set the Output Compare Polarity */
4585 tmpccer |= OC_Config->OCPolarity;
4587 if(IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_1))
4589 /* Check parameters */
4590 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
4592 /* Reset the Output N Polarity level */
4593 tmpccer &= ~TIM_CCER_CC1NP;
4594 /* Set the Output N Polarity */
4595 tmpccer |= OC_Config->OCNPolarity;
4596 /* Reset the Output N State */
4597 tmpccer &= ~TIM_CCER_CC1NE;
4600 if(IS_TIM_BREAK_INSTANCE(TIMx))
4602 /* Check parameters */
4603 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
4604 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
4606 /* Reset the Output Compare and Output Compare N IDLE State */
4607 tmpcr2 &= ~TIM_CR2_OIS1;
4608 tmpcr2 &= ~TIM_CR2_OIS1N;
4609 /* Set the Output Idle state */
4610 tmpcr2 |= OC_Config->OCIdleState;
4611 /* Set the Output N Idle state */
4612 tmpcr2 |= OC_Config->OCNIdleState;
4614 /* Write to TIMx CR2 */
4617 /* Write to TIMx CCMR1 */
4618 TIMx->CCMR1 = tmpccmrx;
4620 /* Set the Capture Compare Register value */
4621 TIMx->CCR1 = OC_Config->Pulse;
4623 /* Write to TIMx CCER */
4624 TIMx->CCER = tmpccer;
4628 * @brief Time Ouput Compare 2 configuration
4629 * @param TIMx to select the TIM peripheral
4630 * @param OC_Config: The ouput configuration structure
4633 void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
4635 uint32_t tmpccmrx = 0;
4636 uint32_t tmpccer = 0;
4637 uint32_t tmpcr2 = 0;
4639 /* Disable the Channel 2: Reset the CC2E Bit */
4640 TIMx->CCER &= ~TIM_CCER_CC2E;
4642 /* Get the TIMx CCER register value */
4643 tmpccer = TIMx->CCER;
4644 /* Get the TIMx CR2 register value */
4647 /* Get the TIMx CCMR1 register value */
4648 tmpccmrx = TIMx->CCMR1;
4650 /* Reset the Output Compare mode and Capture/Compare selection Bits */
4651 tmpccmrx &= ~TIM_CCMR1_OC2M;
4652 tmpccmrx &= ~TIM_CCMR1_CC2S;
4654 /* Select the Output Compare Mode */
4655 tmpccmrx |= (OC_Config->OCMode << 8);
4657 /* Reset the Output Polarity level */
4658 tmpccer &= ~TIM_CCER_CC2P;
4659 /* Set the Output Compare Polarity */
4660 tmpccer |= (OC_Config->OCPolarity << 4);
4662 if(IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_2))
4664 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
4665 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
4666 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
4668 /* Reset the Output N Polarity level */
4669 tmpccer &= ~TIM_CCER_CC2NP;
4670 /* Set the Output N Polarity */
4671 tmpccer |= (OC_Config->OCNPolarity << 4);
4672 /* Reset the Output N State */
4673 tmpccer &= ~TIM_CCER_CC2NE;
4677 if(IS_TIM_BREAK_INSTANCE(TIMx))
4679 /* Check parameters */
4680 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
4681 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
4683 /* Reset the Output Compare and Output Compare N IDLE State */
4684 tmpcr2 &= ~TIM_CR2_OIS2;
4685 tmpcr2 &= ~TIM_CR2_OIS2N;
4686 /* Set the Output Idle state */
4687 tmpcr2 |= (OC_Config->OCIdleState << 2);
4688 /* Set the Output N Idle state */
4689 tmpcr2 |= (OC_Config->OCNIdleState << 2);
4692 /* Write to TIMx CR2 */
4695 /* Write to TIMx CCMR1 */
4696 TIMx->CCMR1 = tmpccmrx;
4698 /* Set the Capture Compare Register value */
4699 TIMx->CCR2 = OC_Config->Pulse;
4701 /* Write to TIMx CCER */
4702 TIMx->CCER = tmpccer;
4706 * @brief Time Ouput Compare 3 configuration
4707 * @param TIMx to select the TIM peripheral
4708 * @param OC_Config: The ouput configuration structure
4711 void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
4713 uint32_t tmpccmrx = 0;
4714 uint32_t tmpccer = 0;
4715 uint32_t tmpcr2 = 0;
4717 /* Disable the Channel 3: Reset the CC2E Bit */
4718 TIMx->CCER &= ~TIM_CCER_CC3E;
4720 /* Get the TIMx CCER register value */
4721 tmpccer = TIMx->CCER;
4722 /* Get the TIMx CR2 register value */
4725 /* Get the TIMx CCMR2 register value */
4726 tmpccmrx = TIMx->CCMR2;
4728 /* Reset the Output Compare mode and Capture/Compare selection Bits */
4729 tmpccmrx &= ~TIM_CCMR2_OC3M;
4730 tmpccmrx &= ~TIM_CCMR2_CC3S;
4731 /* Select the Output Compare Mode */
4732 tmpccmrx |= OC_Config->OCMode;
4734 /* Reset the Output Polarity level */
4735 tmpccer &= ~TIM_CCER_CC3P;
4736 /* Set the Output Compare Polarity */
4737 tmpccer |= (OC_Config->OCPolarity << 8);
4739 if(IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_3))
4741 assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
4742 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
4743 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
4745 /* Reset the Output N Polarity level */
4746 tmpccer &= ~TIM_CCER_CC3NP;
4747 /* Set the Output N Polarity */
4748 tmpccer |= (OC_Config->OCNPolarity << 8);
4749 /* Reset the Output N State */
4750 tmpccer &= ~TIM_CCER_CC3NE;
4753 if(IS_TIM_BREAK_INSTANCE(TIMx))
4755 /* Check parameters */
4756 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
4757 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
4759 /* Reset the Output Compare and Output Compare N IDLE State */
4760 tmpcr2 &= ~TIM_CR2_OIS3;
4761 tmpcr2 &= ~TIM_CR2_OIS3N;
4762 /* Set the Output Idle state */
4763 tmpcr2 |= (OC_Config->OCIdleState << 4);
4764 /* Set the Output N Idle state */
4765 tmpcr2 |= (OC_Config->OCNIdleState << 4);
4768 /* Write to TIMx CR2 */
4771 /* Write to TIMx CCMR2 */
4772 TIMx->CCMR2 = tmpccmrx;
4774 /* Set the Capture Compare Register value */
4775 TIMx->CCR3 = OC_Config->Pulse;
4777 /* Write to TIMx CCER */
4778 TIMx->CCER = tmpccer;
4782 * @brief Time Ouput Compare 4 configuration
4783 * @param TIMx to select the TIM peripheral
4784 * @param OC_Config: The ouput configuration structure
4787 void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
4789 uint32_t tmpccmrx = 0;
4790 uint32_t tmpccer = 0;
4791 uint32_t tmpcr2 = 0;
4793 /* Disable the Channel 4: Reset the CC4E Bit */
4794 TIMx->CCER &= ~TIM_CCER_CC4E;
4796 /* Get the TIMx CCER register value */
4797 tmpccer = TIMx->CCER;
4798 /* Get the TIMx CR2 register value */
4801 /* Get the TIMx CCMR2 register value */
4802 tmpccmrx = TIMx->CCMR2;
4804 /* Reset the Output Compare mode and Capture/Compare selection Bits */
4805 tmpccmrx &= ~TIM_CCMR2_OC4M;
4806 tmpccmrx &= ~TIM_CCMR2_CC4S;
4808 /* Select the Output Compare Mode */
4809 tmpccmrx |= (OC_Config->OCMode << 8);
4811 /* Reset the Output Polarity level */
4812 tmpccer &= ~TIM_CCER_CC4P;
4813 /* Set the Output Compare Polarity */
4814 tmpccer |= (OC_Config->OCPolarity << 12);
4816 if(IS_TIM_BREAK_INSTANCE(TIMx))
4818 assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
4820 /* Reset the Output Compare IDLE State */
4821 tmpcr2 &= ~TIM_CR2_OIS4;
4822 /* Set the Output Idle state */
4823 tmpcr2 |= (OC_Config->OCIdleState << 6);
4826 /* Write to TIMx CR2 */
4829 /* Write to TIMx CCMR2 */
4830 TIMx->CCMR2 = tmpccmrx;
4832 /* Set the Capture Compare Register value */
4833 TIMx->CCR4 = OC_Config->Pulse;
4835 /* Write to TIMx CCER */
4836 TIMx->CCER = tmpccer;
4840 * @brief Configure the TI1 as Input.
4841 * @param TIMx to select the TIM peripheral.
4842 * @param TIM_ICPolarity : The Input Polarity.
4843 * This parameter can be one of the following values:
4844 * @arg TIM_ICPolarity_Rising
4845 * @arg TIM_ICPolarity_Falling
4846 * @arg TIM_ICPolarity_BothEdge
4847 * @param TIM_ICSelection: specifies the input to be used.
4848 * This parameter can be one of the following values:
4849 * @arg TIM_ICSelection_DirectTI: TIM Input 1 is selected to be connected to IC1.
4850 * @arg TIM_ICSelection_IndirectTI: TIM Input 1 is selected to be connected to IC2.
4851 * @arg TIM_ICSelection_TRC: TIM Input 1 is selected to be connected to TRC.
4852 * @param TIM_ICFilter: Specifies the Input Capture Filter.
4853 * This parameter must be a value between 0x00 and 0x0F.
4855 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
4856 * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
4857 * protected against un-initialized filter and polarity values.
4859 void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
4860 uint32_t TIM_ICFilter)
4862 uint32_t tmpccmr1 = 0;
4863 uint32_t tmpccer = 0;
4865 /* Disable the Channel 1: Reset the CC1E Bit */
4866 TIMx->CCER &= ~TIM_CCER_CC1E;
4867 tmpccmr1 = TIMx->CCMR1;
4868 tmpccer = TIMx->CCER;
4870 /* Select the Input */
4871 if(IS_TIM_CC2_INSTANCE(TIMx) != RESET)
4873 tmpccmr1 &= ~TIM_CCMR1_CC1S;
4874 tmpccmr1 |= TIM_ICSelection;
4878 tmpccmr1 |= TIM_CCMR1_CC1S_0;
4881 /* Set the filter */
4882 tmpccmr1 &= ~TIM_CCMR1_IC1F;
4883 tmpccmr1 |= ((TIM_ICFilter << 4) & TIM_CCMR1_IC1F);
4885 /* Select the Polarity and set the CC1E Bit */
4886 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
4887 tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
4889 /* Write to TIMx CCMR1 and CCER registers */
4890 TIMx->CCMR1 = tmpccmr1;
4891 TIMx->CCER = tmpccer;
4895 * @brief Configure the Polarity and Filter for TI1.
4896 * @param TIMx to select the TIM peripheral.
4897 * @param TIM_ICPolarity : The Input Polarity.
4898 * This parameter can be one of the following values:
4899 * @arg TIM_ICPolarity_Rising
4900 * @arg TIM_ICPolarity_Falling
4901 * @arg TIM_ICPolarity_BothEdge
4902 * @param TIM_ICFilter: Specifies the Input Capture Filter.
4903 * This parameter must be a value between 0x00 and 0x0F.
4906 static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
4908 uint32_t tmpccmr1 = 0;
4909 uint32_t tmpccer = 0;
4911 /* Disable the Channel 1: Reset the CC1E Bit */
4912 tmpccer = TIMx->CCER;
4913 TIMx->CCER &= ~TIM_CCER_CC1E;
4914 tmpccmr1 = TIMx->CCMR1;
4916 /* Set the filter */
4917 tmpccmr1 &= ~TIM_CCMR1_IC1F;
4918 tmpccmr1 |= (TIM_ICFilter << 4);
4920 /* Select the Polarity and set the CC1E Bit */
4921 tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
4922 tmpccer |= TIM_ICPolarity;
4924 /* Write to TIMx CCMR1 and CCER registers */
4925 TIMx->CCMR1 = tmpccmr1;
4926 TIMx->CCER = tmpccer;
4930 * @brief Configure the TI2 as Input.
4931 * @param TIMx to select the TIM peripheral
4932 * @param TIM_ICPolarity : The Input Polarity.
4933 * This parameter can be one of the following values:
4934 * @arg TIM_ICPolarity_Rising
4935 * @arg TIM_ICPolarity_Falling
4936 * @arg TIM_ICPolarity_BothEdge
4937 * @param TIM_ICSelection: specifies the input to be used.
4938 * This parameter can be one of the following values:
4939 * @arg TIM_ICSelection_DirectTI: TIM Input 2 is selected to be connected to IC2.
4940 * @arg TIM_ICSelection_IndirectTI: TIM Input 2 is selected to be connected to IC1.
4941 * @arg TIM_ICSelection_TRC: TIM Input 2 is selected to be connected to TRC.
4942 * @param TIM_ICFilter: Specifies the Input Capture Filter.
4943 * This parameter must be a value between 0x00 and 0x0F.
4945 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
4946 * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
4947 * protected against un-initialized filter and polarity values.
4949 static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
4950 uint32_t TIM_ICFilter)
4952 uint32_t tmpccmr1 = 0;
4953 uint32_t tmpccer = 0;
4955 /* Disable the Channel 2: Reset the CC2E Bit */
4956 TIMx->CCER &= ~TIM_CCER_CC2E;
4957 tmpccmr1 = TIMx->CCMR1;
4958 tmpccer = TIMx->CCER;
4960 /* Select the Input */
4961 tmpccmr1 &= ~TIM_CCMR1_CC2S;
4962 tmpccmr1 |= (TIM_ICSelection << 8);
4964 /* Set the filter */
4965 tmpccmr1 &= ~TIM_CCMR1_IC2F;
4966 tmpccmr1 |= ((TIM_ICFilter << 12) & TIM_CCMR1_IC2F);
4968 /* Select the Polarity and set the CC2E Bit */
4969 tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
4970 tmpccer |= ((TIM_ICPolarity << 4) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
4972 /* Write to TIMx CCMR1 and CCER registers */
4973 TIMx->CCMR1 = tmpccmr1 ;
4974 TIMx->CCER = tmpccer;
4978 * @brief Configure the Polarity and Filter for TI2.
4979 * @param TIMx to select the TIM peripheral.
4980 * @param TIM_ICPolarity : The Input Polarity.
4981 * This parameter can be one of the following values:
4982 * @arg TIM_ICPolarity_Rising
4983 * @arg TIM_ICPolarity_Falling
4984 * @arg TIM_ICPolarity_BothEdge
4985 * @param TIM_ICFilter: Specifies the Input Capture Filter.
4986 * This parameter must be a value between 0x00 and 0x0F.
4989 static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
4991 uint32_t tmpccmr1 = 0;
4992 uint32_t tmpccer = 0;
4994 /* Disable the Channel 2: Reset the CC2E Bit */
4995 TIMx->CCER &= ~TIM_CCER_CC2E;
4996 tmpccmr1 = TIMx->CCMR1;
4997 tmpccer = TIMx->CCER;
4999 /* Set the filter */
5000 tmpccmr1 &= ~TIM_CCMR1_IC2F;
5001 tmpccmr1 |= (TIM_ICFilter << 12);
5003 /* Select the Polarity and set the CC2E Bit */
5004 tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
5005 tmpccer |= (TIM_ICPolarity << 4);
5007 /* Write to TIMx CCMR1 and CCER registers */
5008 TIMx->CCMR1 = tmpccmr1 ;
5009 TIMx->CCER = tmpccer;
5013 * @brief Configure the TI3 as Input.
5014 * @param TIMx to select the TIM peripheral
5015 * @param TIM_ICPolarity : The Input Polarity.
5016 * This parameter can be one of the following values:
5017 * @arg TIM_ICPolarity_Rising
5018 * @arg TIM_ICPolarity_Falling
5019 * @arg TIM_ICPolarity_BothEdge
5020 * @param TIM_ICSelection: specifies the input to be used.
5021 * This parameter can be one of the following values:
5022 * @arg TIM_ICSelection_DirectTI: TIM Input 3 is selected to be connected to IC3.
5023 * @arg TIM_ICSelection_IndirectTI: TIM Input 3 is selected to be connected to IC4.
5024 * @arg TIM_ICSelection_TRC: TIM Input 3 is selected to be connected to TRC.
5025 * @param TIM_ICFilter: Specifies the Input Capture Filter.
5026 * This parameter must be a value between 0x00 and 0x0F.
5028 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
5029 * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
5030 * protected against un-initialized filter and polarity values.
5032 static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
5033 uint32_t TIM_ICFilter)
5035 uint32_t tmpccmr2 = 0;
5036 uint32_t tmpccer = 0;
5038 /* Disable the Channel 3: Reset the CC3E Bit */
5039 TIMx->CCER &= ~TIM_CCER_CC3E;
5040 tmpccmr2 = TIMx->CCMR2;
5041 tmpccer = TIMx->CCER;
5043 /* Select the Input */
5044 tmpccmr2 &= ~TIM_CCMR2_CC3S;
5045 tmpccmr2 |= TIM_ICSelection;
5047 /* Set the filter */
5048 tmpccmr2 &= ~TIM_CCMR2_IC3F;
5049 tmpccmr2 |= ((TIM_ICFilter << 4) & TIM_CCMR2_IC3F);
5051 /* Select the Polarity and set the CC3E Bit */
5052 tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
5053 tmpccer |= ((TIM_ICPolarity << 8) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
5055 /* Write to TIMx CCMR2 and CCER registers */
5056 TIMx->CCMR2 = tmpccmr2;
5057 TIMx->CCER = tmpccer;
5061 * @brief Configure the TI4 as Input.
5062 * @param TIMx to select the TIM peripheral
5063 * @param TIM_ICPolarity : The Input Polarity.
5064 * This parameter can be one of the following values:
5065 * @arg TIM_ICPolarity_Rising
5066 * @arg TIM_ICPolarity_Falling
5067 * @arg TIM_ICPolarity_BothEdge
5068 * @param TIM_ICSelection: specifies the input to be used.
5069 * This parameter can be one of the following values:
5070 * @arg TIM_ICSelection_DirectTI: TIM Input 4 is selected to be connected to IC4.
5071 * @arg TIM_ICSelection_IndirectTI: TIM Input 4 is selected to be connected to IC3.
5072 * @arg TIM_ICSelection_TRC: TIM Input 4 is selected to be connected to TRC.
5073 * @param TIM_ICFilter: Specifies the Input Capture Filter.
5074 * This parameter must be a value between 0x00 and 0x0F.
5075 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
5076 * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
5077 * protected against un-initialized filter and polarity values.
5080 static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
5081 uint32_t TIM_ICFilter)
5083 uint32_t tmpccmr2 = 0;
5084 uint32_t tmpccer = 0;
5086 /* Disable the Channel 4: Reset the CC4E Bit */
5087 TIMx->CCER &= ~TIM_CCER_CC4E;
5088 tmpccmr2 = TIMx->CCMR2;
5089 tmpccer = TIMx->CCER;
5091 /* Select the Input */
5092 tmpccmr2 &= ~TIM_CCMR2_CC4S;
5093 tmpccmr2 |= (TIM_ICSelection << 8);
5095 /* Set the filter */
5096 tmpccmr2 &= ~TIM_CCMR2_IC4F;
5097 tmpccmr2 |= ((TIM_ICFilter << 12) & TIM_CCMR2_IC4F);
5099 /* Select the Polarity and set the CC4E Bit */
5100 tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
5101 tmpccer |= ((TIM_ICPolarity << 12) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
5103 /* Write to TIMx CCMR2 and CCER registers */
5104 TIMx->CCMR2 = tmpccmr2;
5105 TIMx->CCER = tmpccer ;
5109 * @brief Selects the Input Trigger source
5110 * @param TIMx to select the TIM peripheral
5111 * @param InputTriggerSource: The Input Trigger source.
5112 * This parameter can be one of the following values:
5113 * @arg TIM_TS_ITR0: Internal Trigger 0
5114 * @arg TIM_TS_ITR1: Internal Trigger 1
5115 * @arg TIM_TS_ITR2: Internal Trigger 2
5116 * @arg TIM_TS_ITR3: Internal Trigger 3
5117 * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
5118 * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
5119 * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
5120 * @arg TIM_TS_ETRF: External Trigger input
5123 static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint16_t InputTriggerSource)
5125 uint32_t tmpsmcr = 0;
5127 /* Get the TIMx SMCR register value */
5128 tmpsmcr = TIMx->SMCR;
5129 /* Reset the TS Bits */
5130 tmpsmcr &= ~TIM_SMCR_TS;
5131 /* Set the Input Trigger source and the slave mode*/
5132 tmpsmcr |= InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1;
5133 /* Write to TIMx SMCR */
5134 TIMx->SMCR = tmpsmcr;
5137 * @brief Configures the TIMx External Trigger (ETR).
5138 * @param TIMx to select the TIM peripheral
5139 * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
5140 * This parameter can be one of the following values:
5141 * @arg TIM_ExtTRGPSC_DIV1: ETRP Prescaler OFF.
5142 * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
5143 * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
5144 * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
5145 * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
5146 * This parameter can be one of the following values:
5147 * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
5148 * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
5149 * @param ExtTRGFilter: External Trigger Filter.
5150 * This parameter must be a value between 0x00 and 0x0F
5153 void TIM_ETR_SetConfig(TIM_TypeDef* TIMx, uint32_t TIM_ExtTRGPrescaler,
5154 uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
5156 uint32_t tmpsmcr = 0;
5158 tmpsmcr = TIMx->SMCR;
5160 /* Reset the ETR Bits */
5161 tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
5163 /* Set the Prescaler, the Filter value and the Polarity */
5164 tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8)));
5166 /* Write to TIMx SMCR */
5167 TIMx->SMCR = tmpsmcr;
5171 * @brief Enables or disables the TIM Capture Compare Channel x.
5172 * @param TIMx to select the TIM peripheral
5173 * @param Channel: specifies the TIM Channel
5174 * This parameter can be one of the following values:
5175 * @arg TIM_Channel_1: TIM Channel 1
5176 * @arg TIM_Channel_2: TIM Channel 2
5177 * @arg TIM_Channel_3: TIM Channel 3
5178 * @arg TIM_Channel_4: TIM Channel 4
5179 * @param ChannelState: specifies the TIM Channel CCxE bit new state.
5180 * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_Disable.
5183 void TIM_CCxChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelState)
5187 /* Check the parameters */
5188 assert_param(IS_TIM_CC1_INSTANCE(TIMx));
5189 assert_param(IS_TIM_CHANNELS(Channel));
5191 tmp = TIM_CCER_CC1E << Channel;
5193 /* Reset the CCxE Bit */
5196 /* Set or reset the CCxE Bit */
5197 TIMx->CCER |= (uint32_t)(ChannelState << Channel);
5205 #endif /* HAL_TIM_MODULE_ENABLED */
5213 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/