2 ******************************************************************************
3 * @file stm32f1xx_hal_tim.c
4 * @author MCD Application Team
6 * @date 15-December-2014
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 __HAL_RCC_TIMx_CLK_ENABLE();
62 (##) TIM pins configuration
63 (+++) Enable the clock for the TIM GPIOs using the following function:
64 __HAL_GPIOx_CLK_ENABLE();
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 "stm32f1xx_hal.h"
131 /** @addtogroup STM32F1xx_HAL_Driver
135 /** @defgroup TIM TIM
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 -----------------------------------------------*/
148 /** @defgroup TIM_Private_Functions TIM Private Functions
151 static void TIM_OC1_SetConfig(TIM_TypeDef
*TIMx
, TIM_OC_InitTypeDef
*OC_Config
);
152 static void TIM_OC3_SetConfig(TIM_TypeDef
*TIMx
, TIM_OC_InitTypeDef
*OC_Config
);
153 static void TIM_OC4_SetConfig(TIM_TypeDef
*TIMx
, TIM_OC_InitTypeDef
*OC_Config
);
154 static void TIM_TI1_ConfigInputStage(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICFilter
);
155 static void TIM_TI2_SetConfig(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICSelection
,
156 uint32_t TIM_ICFilter
);
157 static void TIM_TI2_ConfigInputStage(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICFilter
);
158 static void TIM_TI3_SetConfig(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICSelection
,
159 uint32_t TIM_ICFilter
);
160 static void TIM_TI4_SetConfig(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICSelection
,
161 uint32_t TIM_ICFilter
);
162 static void TIM_ETR_SetConfig(TIM_TypeDef
* TIMx
, uint32_t TIM_ExtTRGPrescaler
,
163 uint32_t TIM_ExtTRGPolarity
, uint32_t ExtTRGFilter
);
164 static void TIM_ITRx_SetConfig(TIM_TypeDef
* TIMx
, uint16_t InputTriggerSource
);
165 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef
*hdma
);
166 static void TIM_DMATriggerCplt(DMA_HandleTypeDef
*hdma
);
167 static void TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef
*htim
,
168 TIM_SlaveConfigTypeDef
* sSlaveConfig
);
174 /* Exported functions ---------------------------------------------------------*/
176 /** @defgroup TIM_Exported_Functions TIM Exported Functions
180 /** @defgroup TIM_Exported_Functions_Group1 Time Base functions
181 * @brief Time Base functions
184 ==============================================================================
185 ##### Time Base functions #####
186 ==============================================================================
188 This section provides functions allowing to:
189 (+) Initialize and configure the TIM base.
190 (+) De-initialize the TIM base.
191 (+) Start the Time Base.
192 (+) Stop the Time Base.
193 (+) Start the Time Base and enable interrupt.
194 (+) Stop the Time Base and disable interrupt.
195 (+) Start the Time Base and enable DMA transfer.
196 (+) Stop the Time Base and disable DMA transfer.
202 * @brief Initializes the TIM Time base Unit according to the specified
203 * parameters in the TIM_HandleTypeDef and create the associated handle.
204 * @param htim : TIM Base handle
207 HAL_StatusTypeDef
HAL_TIM_Base_Init(TIM_HandleTypeDef
*htim
)
209 /* Check the TIM handle allocation */
215 /* Check the parameters */
216 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
217 assert_param(IS_TIM_COUNTER_MODE(htim
->Init
.CounterMode
));
218 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim
->Init
.ClockDivision
));
220 if(htim
->State
== HAL_TIM_STATE_RESET
)
222 /* Allocate lock resource and initialize it */
223 htim
-> Lock
= HAL_UNLOCKED
;
225 /* Init the low level hardware : GPIO, CLOCK, NVIC */
226 HAL_TIM_Base_MspInit(htim
);
229 /* Set the TIM state */
230 htim
->State
= HAL_TIM_STATE_BUSY
;
232 /* Set the Time Base configuration */
233 TIM_Base_SetConfig(htim
->Instance
, &htim
->Init
);
235 /* Initialize the TIM state*/
236 htim
->State
= HAL_TIM_STATE_READY
;
242 * @brief DeInitializes the TIM Base peripheral
243 * @param htim : TIM Base handle
246 HAL_StatusTypeDef
HAL_TIM_Base_DeInit(TIM_HandleTypeDef
*htim
)
248 /* Check the parameters */
249 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
251 htim
->State
= HAL_TIM_STATE_BUSY
;
253 /* Disable the TIM Peripheral Clock */
254 __HAL_TIM_DISABLE(htim
);
256 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
257 HAL_TIM_Base_MspDeInit(htim
);
259 /* Change TIM state */
260 htim
->State
= HAL_TIM_STATE_RESET
;
269 * @brief Initializes the TIM Base MSP.
270 * @param htim : TIM handle
273 __weak
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef
*htim
)
275 /* NOTE : This function Should not be modified, when the callback is needed,
276 the HAL_TIM_Base_MspInit could be implemented in the user file
281 * @brief DeInitializes TIM Base MSP.
282 * @param htim : TIM handle
285 __weak
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef
*htim
)
287 /* NOTE : This function Should not be modified, when the callback is needed,
288 the HAL_TIM_Base_MspDeInit could be implemented in the user file
294 * @brief Starts the TIM Base generation.
295 * @param htim : TIM handle
298 HAL_StatusTypeDef
HAL_TIM_Base_Start(TIM_HandleTypeDef
*htim
)
300 /* Check the parameters */
301 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
303 /* Set the TIM state */
304 htim
->State
= HAL_TIM_STATE_BUSY
;
306 /* Enable the Peripheral */
307 __HAL_TIM_ENABLE(htim
);
309 /* Change the TIM state*/
310 htim
->State
= HAL_TIM_STATE_READY
;
312 /* Return function status */
317 * @brief Stops the TIM Base generation.
318 * @param htim : TIM handle
321 HAL_StatusTypeDef
HAL_TIM_Base_Stop(TIM_HandleTypeDef
*htim
)
323 /* Check the parameters */
324 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
326 /* Set the TIM state */
327 htim
->State
= HAL_TIM_STATE_BUSY
;
329 /* Disable the Peripheral */
330 __HAL_TIM_DISABLE(htim
);
332 /* Change the TIM state*/
333 htim
->State
= HAL_TIM_STATE_READY
;
335 /* Return function status */
340 * @brief Starts the TIM Base generation in interrupt mode.
341 * @param htim : TIM handle
344 HAL_StatusTypeDef
HAL_TIM_Base_Start_IT(TIM_HandleTypeDef
*htim
)
346 /* Check the parameters */
347 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
349 /* Enable the TIM Update interrupt */
350 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_UPDATE
);
352 /* Enable the Peripheral */
353 __HAL_TIM_ENABLE(htim
);
355 /* Return function status */
360 * @brief Stops the TIM Base generation in interrupt mode.
361 * @param htim : TIM handle
364 HAL_StatusTypeDef
HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef
*htim
)
366 /* Check the parameters */
367 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
368 /* Disable the TIM Update interrupt */
369 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_UPDATE
);
371 /* Disable the Peripheral */
372 __HAL_TIM_DISABLE(htim
);
374 /* Return function status */
379 * @brief Starts the TIM Base generation in DMA mode.
380 * @param htim : TIM handle
381 * @param pData : The source Buffer address.
382 * @param Length : The length of data to be transferred from memory to peripheral.
385 HAL_StatusTypeDef
HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef
*htim
, uint32_t *pData
, uint16_t Length
)
387 /* Check the parameters */
388 assert_param(IS_TIM_DMA_INSTANCE(htim
->Instance
));
390 if((htim
->State
== HAL_TIM_STATE_BUSY
))
394 else if((htim
->State
== HAL_TIM_STATE_READY
))
396 if((pData
== 0 ) && (Length
> 0))
402 htim
->State
= HAL_TIM_STATE_BUSY
;
405 /* Set the DMA Period elapsed callback */
406 htim
->hdma
[TIM_DMA_ID_UPDATE
]->XferCpltCallback
= TIM_DMAPeriodElapsedCplt
;
408 /* Set the DMA error callback */
409 htim
->hdma
[TIM_DMA_ID_UPDATE
]->XferErrorCallback
= TIM_DMAError
;
411 /* Enable the DMA channel */
412 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_UPDATE
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->ARR
, Length
);
414 /* Enable the TIM Update DMA request */
415 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_UPDATE
);
417 /* Enable the Peripheral */
418 __HAL_TIM_ENABLE(htim
);
420 /* Return function status */
425 * @brief Stops the TIM Base generation in DMA mode.
426 * @param htim : TIM handle
429 HAL_StatusTypeDef
HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef
*htim
)
431 /* Check the parameters */
432 assert_param(IS_TIM_DMA_INSTANCE(htim
->Instance
));
434 /* Disable the TIM Update DMA request */
435 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_UPDATE
);
437 /* Disable the Peripheral */
438 __HAL_TIM_DISABLE(htim
);
440 /* Change the htim state */
441 htim
->State
= HAL_TIM_STATE_READY
;
443 /* Return function status */
451 /** @defgroup TIM_Exported_Functions_Group2 Time Output Compare functions
452 * @brief Time Output Compare functions
455 ==============================================================================
456 ##### Time Output Compare functions #####
457 ==============================================================================
459 This section provides functions allowing to:
460 (+) Initialize and configure the TIM Output Compare.
461 (+) De-initialize the TIM Output Compare.
462 (+) Start the Time Output Compare.
463 (+) Stop the Time Output Compare.
464 (+) Start the Time Output Compare and enable interrupt.
465 (+) Stop the Time Output Compare and disable interrupt.
466 (+) Start the Time Output Compare and enable DMA transfer.
467 (+) Stop the Time Output Compare and disable DMA transfer.
473 * @brief Initializes the TIM Output Compare according to the specified
474 * parameters in the TIM_HandleTypeDef and create the associated handle.
475 * @param htim : TIM Output Compare handle
478 HAL_StatusTypeDef
HAL_TIM_OC_Init(TIM_HandleTypeDef
* htim
)
480 /* Check the TIM handle allocation */
486 /* Check the parameters */
487 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
488 assert_param(IS_TIM_COUNTER_MODE(htim
->Init
.CounterMode
));
489 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim
->Init
.ClockDivision
));
491 if(htim
->State
== HAL_TIM_STATE_RESET
)
493 /* Allocate lock resource and initialize it */
494 htim
-> Lock
= HAL_UNLOCKED
;
496 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
497 HAL_TIM_OC_MspInit(htim
);
500 /* Set the TIM state */
501 htim
->State
= HAL_TIM_STATE_BUSY
;
503 /* Init the base time for the Output Compare */
504 TIM_Base_SetConfig(htim
->Instance
, &htim
->Init
);
506 /* Initialize the TIM state*/
507 htim
->State
= HAL_TIM_STATE_READY
;
513 * @brief DeInitializes the TIM peripheral
514 * @param htim : TIM Output Compare handle
517 HAL_StatusTypeDef
HAL_TIM_OC_DeInit(TIM_HandleTypeDef
*htim
)
519 /* Check the parameters */
520 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
522 htim
->State
= HAL_TIM_STATE_BUSY
;
524 /* Disable the TIM Peripheral Clock */
525 __HAL_TIM_DISABLE(htim
);
527 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
528 HAL_TIM_OC_MspDeInit(htim
);
530 /* Change TIM state */
531 htim
->State
= HAL_TIM_STATE_RESET
;
540 * @brief Initializes the TIM Output Compare MSP.
541 * @param htim : TIM handle
544 __weak
void HAL_TIM_OC_MspInit(TIM_HandleTypeDef
*htim
)
546 /* NOTE : This function Should not be modified, when the callback is needed,
547 the HAL_TIM_OC_MspInit could be implemented in the user file
552 * @brief DeInitializes TIM Output Compare MSP.
553 * @param htim : TIM handle
556 __weak
void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef
*htim
)
558 /* NOTE : This function Should not be modified, when the callback is needed,
559 the HAL_TIM_OC_MspDeInit could be implemented in the user file
564 * @brief Starts the TIM Output Compare signal generation.
565 * @param htim : TIM Output Compare handle
566 * @param Channel : TIM Channel to be enabled
567 * This parameter can be one of the following values:
568 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
569 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
570 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
571 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
574 HAL_StatusTypeDef
HAL_TIM_OC_Start(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
576 /* Check the parameters */
577 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
579 /* Enable the Output compare channel */
580 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
582 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
584 /* Enable the main output */
585 __HAL_TIM_MOE_ENABLE(htim
);
588 /* Enable the Peripheral */
589 __HAL_TIM_ENABLE(htim
);
591 /* Return function status */
596 * @brief Stops the TIM Output Compare signal generation.
597 * @param htim : TIM handle
598 * @param Channel : TIM Channel to be disabled
599 * This parameter can be one of the following values:
600 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
601 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
602 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
603 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
606 HAL_StatusTypeDef
HAL_TIM_OC_Stop(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
608 /* Check the parameters */
609 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
611 /* Disable the Output compare channel */
612 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
614 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
616 /* Disable the Main Ouput */
617 __HAL_TIM_MOE_DISABLE(htim
);
620 /* Disable the Peripheral */
621 __HAL_TIM_DISABLE(htim
);
623 /* Return function status */
628 * @brief Starts the TIM Output Compare signal generation in interrupt mode.
629 * @param htim : TIM OC handle
630 * @param Channel : TIM Channel to be enabled
631 * This parameter can be one of the following values:
632 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
633 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
634 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
635 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
638 HAL_StatusTypeDef
HAL_TIM_OC_Start_IT(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
640 /* Check the parameters */
641 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
647 /* Enable the TIM Capture/Compare 1 interrupt */
648 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC1
);
654 /* Enable the TIM Capture/Compare 2 interrupt */
655 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC2
);
661 /* Enable the TIM Capture/Compare 3 interrupt */
662 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC3
);
668 /* Enable the TIM Capture/Compare 4 interrupt */
669 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC4
);
677 /* Enable the Output compare channel */
678 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
680 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
682 /* Enable the main output */
683 __HAL_TIM_MOE_ENABLE(htim
);
686 /* Enable the Peripheral */
687 __HAL_TIM_ENABLE(htim
);
689 /* Return function status */
694 * @brief Stops the TIM Output Compare signal generation in interrupt mode.
695 * @param htim : TIM Output Compare handle
696 * @param Channel : TIM Channel to be disabled
697 * This parameter can be one of the following values:
698 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
699 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
700 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
701 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
704 HAL_StatusTypeDef
HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
706 /* Check the parameters */
707 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
713 /* Disable the TIM Capture/Compare 1 interrupt */
714 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC1
);
720 /* Disable the TIM Capture/Compare 2 interrupt */
721 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC2
);
727 /* Disable the TIM Capture/Compare 3 interrupt */
728 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC3
);
734 /* Disable the TIM Capture/Compare 4 interrupt */
735 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC4
);
743 /* Disable the Output compare channel */
744 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
746 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
748 /* Disable the Main Ouput */
749 __HAL_TIM_MOE_DISABLE(htim
);
752 /* Disable the Peripheral */
753 __HAL_TIM_DISABLE(htim
);
755 /* Return function status */
760 * @brief Starts the TIM Output Compare signal generation in DMA mode.
761 * @param htim : TIM Output Compare handle
762 * @param Channel : TIM Channel to be enabled
763 * This parameter can be one of the following values:
764 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
765 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
766 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
767 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
768 * @param pData : The source Buffer address.
769 * @param Length : The length of data to be transferred from memory to TIM peripheral
772 HAL_StatusTypeDef
HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef
*htim
, uint32_t Channel
, uint32_t *pData
, uint16_t Length
)
774 /* Check the parameters */
775 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
777 if((htim
->State
== HAL_TIM_STATE_BUSY
))
781 else if((htim
->State
== HAL_TIM_STATE_READY
))
783 if(((uint32_t)pData
== 0 ) && (Length
> 0))
789 htim
->State
= HAL_TIM_STATE_BUSY
;
796 /* Set the DMA Period elapsed callback */
797 htim
->hdma
[TIM_DMA_ID_CC1
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
799 /* Set the DMA error callback */
800 htim
->hdma
[TIM_DMA_ID_CC1
]->XferErrorCallback
= TIM_DMAError
;
802 /* Enable the DMA channel */
803 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC1
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->CCR1
, Length
);
805 /* Enable the TIM Capture/Compare 1 DMA request */
806 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC1
);
812 /* Set the DMA Period elapsed callback */
813 htim
->hdma
[TIM_DMA_ID_CC2
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
815 /* Set the DMA error callback */
816 htim
->hdma
[TIM_DMA_ID_CC2
]->XferErrorCallback
= TIM_DMAError
;
818 /* Enable the DMA channel */
819 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC2
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->CCR2
, Length
);
821 /* Enable the TIM Capture/Compare 2 DMA request */
822 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC2
);
828 /* Set the DMA Period elapsed callback */
829 htim
->hdma
[TIM_DMA_ID_CC3
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
831 /* Set the DMA error callback */
832 htim
->hdma
[TIM_DMA_ID_CC3
]->XferErrorCallback
= TIM_DMAError
;
834 /* Enable the DMA channel */
835 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC3
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->CCR3
,Length
);
837 /* Enable the TIM Capture/Compare 3 DMA request */
838 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC3
);
844 /* Set the DMA Period elapsed callback */
845 htim
->hdma
[TIM_DMA_ID_CC4
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
847 /* Set the DMA error callback */
848 htim
->hdma
[TIM_DMA_ID_CC4
]->XferErrorCallback
= TIM_DMAError
;
850 /* Enable the DMA channel */
851 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC4
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->CCR4
, Length
);
853 /* Enable the TIM Capture/Compare 4 DMA request */
854 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC4
);
862 /* Enable the Output compare channel */
863 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
865 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
867 /* Enable the main output */
868 __HAL_TIM_MOE_ENABLE(htim
);
871 /* Enable the Peripheral */
872 __HAL_TIM_ENABLE(htim
);
874 /* Return function status */
879 * @brief Stops the TIM Output Compare signal generation in DMA mode.
880 * @param htim : TIM Output Compare handle
881 * @param Channel : TIM Channel to be disabled
882 * This parameter can be one of the following values:
883 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
884 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
885 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
886 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
889 HAL_StatusTypeDef
HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
891 /* Check the parameters */
892 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
898 /* Disable the TIM Capture/Compare 1 DMA request */
899 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC1
);
905 /* Disable the TIM Capture/Compare 2 DMA request */
906 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC2
);
912 /* Disable the TIM Capture/Compare 3 DMA request */
913 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC3
);
919 /* Disable the TIM Capture/Compare 4 interrupt */
920 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC4
);
928 /* Disable the Output compare channel */
929 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
931 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
933 /* Disable the Main Ouput */
934 __HAL_TIM_MOE_DISABLE(htim
);
937 /* Disable the Peripheral */
938 __HAL_TIM_DISABLE(htim
);
940 /* Change the htim state */
941 htim
->State
= HAL_TIM_STATE_READY
;
943 /* Return function status */
951 /** @defgroup TIM_Exported_Functions_Group3 Time PWM functions
952 * @brief Time PWM functions
955 ==============================================================================
956 ##### Time PWM functions #####
957 ==============================================================================
959 This section provides functions allowing to:
960 (+) Initialize and configure the TIM PWM.
961 (+) De-initialize the TIM PWM.
962 (+) Start the Time PWM.
963 (+) Stop the Time PWM.
964 (+) Start the Time PWM and enable interrupt.
965 (+) Stop the Time PWM and disable interrupt.
966 (+) Start the Time PWM and enable DMA transfer.
967 (+) Stop the Time PWM and disable DMA transfer.
973 * @brief Initializes the TIM PWM Time Base according to the specified
974 * parameters in the TIM_HandleTypeDef and create the associated handle.
975 * @param htim : TIM handle
978 HAL_StatusTypeDef
HAL_TIM_PWM_Init(TIM_HandleTypeDef
*htim
)
980 /* Check the TIM handle allocation */
986 /* Check the parameters */
987 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
988 assert_param(IS_TIM_COUNTER_MODE(htim
->Init
.CounterMode
));
989 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim
->Init
.ClockDivision
));
991 if(htim
->State
== HAL_TIM_STATE_RESET
)
993 /* Allocate lock resource and initialize it */
994 htim
-> Lock
= HAL_UNLOCKED
;
996 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
997 HAL_TIM_PWM_MspInit(htim
);
1000 /* Set the TIM state */
1001 htim
->State
= HAL_TIM_STATE_BUSY
;
1003 /* Init the base time for the PWM */
1004 TIM_Base_SetConfig(htim
->Instance
, &htim
->Init
);
1006 /* Initialize the TIM state*/
1007 htim
->State
= HAL_TIM_STATE_READY
;
1013 * @brief DeInitializes the TIM peripheral
1014 * @param htim : TIM handle
1015 * @retval HAL status
1017 HAL_StatusTypeDef
HAL_TIM_PWM_DeInit(TIM_HandleTypeDef
*htim
)
1019 /* Check the parameters */
1020 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
1022 htim
->State
= HAL_TIM_STATE_BUSY
;
1024 /* Disable the TIM Peripheral Clock */
1025 __HAL_TIM_DISABLE(htim
);
1027 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1028 HAL_TIM_PWM_MspDeInit(htim
);
1030 /* Change TIM state */
1031 htim
->State
= HAL_TIM_STATE_RESET
;
1040 * @brief Initializes the TIM PWM MSP.
1041 * @param htim : TIM handle
1044 __weak
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef
*htim
)
1046 /* NOTE : This function Should not be modified, when the callback is needed,
1047 the HAL_TIM_PWM_MspInit could be implemented in the user file
1052 * @brief DeInitializes TIM PWM MSP.
1053 * @param htim : TIM handle
1056 __weak
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef
*htim
)
1058 /* NOTE : This function Should not be modified, when the callback is needed,
1059 the HAL_TIM_PWM_MspDeInit could be implemented in the user file
1064 * @brief Starts the PWM signal generation.
1065 * @param htim : TIM handle
1066 * @param Channel : TIM Channels to be enabled
1067 * This parameter can be one of the following values:
1068 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1069 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1070 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1071 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1072 * @retval HAL status
1074 HAL_StatusTypeDef
HAL_TIM_PWM_Start(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1076 /* Check the parameters */
1077 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1079 /* Enable the Capture compare channel */
1080 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
1082 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
1084 /* Enable the main output */
1085 __HAL_TIM_MOE_ENABLE(htim
);
1088 /* Enable the Peripheral */
1089 __HAL_TIM_ENABLE(htim
);
1091 /* Return function status */
1096 * @brief Stops the PWM signal generation.
1097 * @param htim : TIM handle
1098 * @param Channel : TIM Channels to be disabled
1099 * This parameter can be one of the following values:
1100 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1101 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1102 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1103 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1104 * @retval HAL status
1106 HAL_StatusTypeDef
HAL_TIM_PWM_Stop(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1108 /* Check the parameters */
1109 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1111 /* Disable the Capture compare channel */
1112 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
1114 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
1116 /* Disable the Main Ouput */
1117 __HAL_TIM_MOE_DISABLE(htim
);
1120 /* Disable the Peripheral */
1121 __HAL_TIM_DISABLE(htim
);
1123 /* Change the htim state */
1124 htim
->State
= HAL_TIM_STATE_READY
;
1126 /* Return function status */
1131 * @brief Starts the PWM signal generation in interrupt mode.
1132 * @param htim : TIM handle
1133 * @param Channel : TIM Channel to be disabled
1134 * This parameter can be one of the following values:
1135 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1136 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1137 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1138 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1139 * @retval HAL status
1141 HAL_StatusTypeDef
HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1143 /* Check the parameters */
1144 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1150 /* Enable the TIM Capture/Compare 1 interrupt */
1151 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC1
);
1157 /* Enable the TIM Capture/Compare 2 interrupt */
1158 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC2
);
1164 /* Enable the TIM Capture/Compare 3 interrupt */
1165 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC3
);
1171 /* Enable the TIM Capture/Compare 4 interrupt */
1172 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC4
);
1180 /* Enable the Capture compare channel */
1181 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
1183 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
1185 /* Enable the main output */
1186 __HAL_TIM_MOE_ENABLE(htim
);
1189 /* Enable the Peripheral */
1190 __HAL_TIM_ENABLE(htim
);
1192 /* Return function status */
1197 * @brief Stops the PWM signal generation in interrupt mode.
1198 * @param htim : TIM handle
1199 * @param Channel : TIM Channels to be disabled
1200 * This parameter can be one of the following values:
1201 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1202 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1203 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1204 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1205 * @retval HAL status
1207 HAL_StatusTypeDef
HAL_TIM_PWM_Stop_IT (TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1209 /* Check the parameters */
1210 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1216 /* Disable the TIM Capture/Compare 1 interrupt */
1217 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC1
);
1223 /* Disable the TIM Capture/Compare 2 interrupt */
1224 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC2
);
1230 /* Disable the TIM Capture/Compare 3 interrupt */
1231 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC3
);
1237 /* Disable the TIM Capture/Compare 4 interrupt */
1238 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC4
);
1246 /* Disable the Capture compare channel */
1247 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
1249 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
1251 /* Disable the Main Ouput */
1252 __HAL_TIM_MOE_DISABLE(htim
);
1255 /* Disable the Peripheral */
1256 __HAL_TIM_DISABLE(htim
);
1258 /* Return function status */
1263 * @brief Starts the TIM PWM signal generation in DMA mode.
1264 * @param htim : TIM handle
1265 * @param Channel : TIM Channels to be enabled
1266 * This parameter can be one of the following values:
1267 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1268 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1269 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1270 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1271 * @param pData : The source Buffer address.
1272 * @param Length : The length of data to be transferred from memory to TIM peripheral
1273 * @retval HAL status
1275 HAL_StatusTypeDef
HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef
*htim
, uint32_t Channel
, uint32_t *pData
, uint16_t Length
)
1277 /* Check the parameters */
1278 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1280 if((htim
->State
== HAL_TIM_STATE_BUSY
))
1284 else if((htim
->State
== HAL_TIM_STATE_READY
))
1286 if(((uint32_t)pData
== 0 ) && (Length
> 0))
1292 htim
->State
= HAL_TIM_STATE_BUSY
;
1299 /* Set the DMA Period elapsed callback */
1300 htim
->hdma
[TIM_DMA_ID_CC1
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
1302 /* Set the DMA error callback */
1303 htim
->hdma
[TIM_DMA_ID_CC1
]->XferErrorCallback
= TIM_DMAError
;
1305 /* Enable the DMA channel */
1306 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC1
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->CCR1
, Length
);
1308 /* Enable the TIM Capture/Compare 1 DMA request */
1309 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC1
);
1315 /* Set the DMA Period elapsed callback */
1316 htim
->hdma
[TIM_DMA_ID_CC2
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
1318 /* Set the DMA error callback */
1319 htim
->hdma
[TIM_DMA_ID_CC2
]->XferErrorCallback
= TIM_DMAError
;
1321 /* Enable the DMA channel */
1322 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC2
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->CCR2
, Length
);
1324 /* Enable the TIM Capture/Compare 2 DMA request */
1325 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC2
);
1331 /* Set the DMA Period elapsed callback */
1332 htim
->hdma
[TIM_DMA_ID_CC3
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
1334 /* Set the DMA error callback */
1335 htim
->hdma
[TIM_DMA_ID_CC3
]->XferErrorCallback
= TIM_DMAError
;
1337 /* Enable the DMA channel */
1338 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC3
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->CCR3
,Length
);
1340 /* Enable the TIM Output Capture/Compare 3 request */
1341 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC3
);
1347 /* Set the DMA Period elapsed callback */
1348 htim
->hdma
[TIM_DMA_ID_CC4
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
1350 /* Set the DMA error callback */
1351 htim
->hdma
[TIM_DMA_ID_CC4
]->XferErrorCallback
= TIM_DMAError
;
1353 /* Enable the DMA channel */
1354 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC4
], (uint32_t)pData
, (uint32_t)&htim
->Instance
->CCR4
, Length
);
1356 /* Enable the TIM Capture/Compare 4 DMA request */
1357 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC4
);
1365 /* Enable the Capture compare channel */
1366 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
1368 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
1370 /* Enable the main output */
1371 __HAL_TIM_MOE_ENABLE(htim
);
1374 /* Enable the Peripheral */
1375 __HAL_TIM_ENABLE(htim
);
1377 /* Return function status */
1382 * @brief Stops the TIM PWM signal generation in DMA mode.
1383 * @param htim : TIM handle
1384 * @param Channel : TIM Channels to be disabled
1385 * This parameter can be one of the following values:
1386 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1387 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1388 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1389 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1390 * @retval HAL status
1392 HAL_StatusTypeDef
HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1394 /* Check the parameters */
1395 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1401 /* Disable the TIM Capture/Compare 1 DMA request */
1402 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC1
);
1408 /* Disable the TIM Capture/Compare 2 DMA request */
1409 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC2
);
1415 /* Disable the TIM Capture/Compare 3 DMA request */
1416 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC3
);
1422 /* Disable the TIM Capture/Compare 4 interrupt */
1423 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC4
);
1431 /* Disable the Capture compare channel */
1432 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
1434 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
1436 /* Disable the Main Ouput */
1437 __HAL_TIM_MOE_DISABLE(htim
);
1440 /* Disable the Peripheral */
1441 __HAL_TIM_DISABLE(htim
);
1443 /* Change the htim state */
1444 htim
->State
= HAL_TIM_STATE_READY
;
1446 /* Return function status */
1454 /** @defgroup TIM_Exported_Functions_Group4 Time Input Capture functions
1455 * @brief Time Input Capture functions
1458 ==============================================================================
1459 ##### Time Input Capture functions #####
1460 ==============================================================================
1462 This section provides functions allowing to:
1463 (+) Initialize and configure the TIM Input Capture.
1464 (+) De-initialize the TIM Input Capture.
1465 (+) Start the Time Input Capture.
1466 (+) Stop the Time Input Capture.
1467 (+) Start the Time Input Capture and enable interrupt.
1468 (+) Stop the Time Input Capture and disable interrupt.
1469 (+) Start the Time Input Capture and enable DMA transfer.
1470 (+) Stop the Time Input Capture and disable DMA transfer.
1476 * @brief Initializes the TIM Input Capture Time base according to the specified
1477 * parameters in the TIM_HandleTypeDef and create the associated handle.
1478 * @param htim : TIM Input Capture handle
1479 * @retval HAL status
1481 HAL_StatusTypeDef
HAL_TIM_IC_Init(TIM_HandleTypeDef
*htim
)
1483 /* Check the TIM handle allocation */
1489 /* Check the parameters */
1490 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
1491 assert_param(IS_TIM_COUNTER_MODE(htim
->Init
.CounterMode
));
1492 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim
->Init
.ClockDivision
));
1494 if(htim
->State
== HAL_TIM_STATE_RESET
)
1496 /* Allocate lock resource and initialize it */
1497 htim
-> Lock
= HAL_UNLOCKED
;
1499 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1500 HAL_TIM_IC_MspInit(htim
);
1503 /* Set the TIM state */
1504 htim
->State
= HAL_TIM_STATE_BUSY
;
1506 /* Init the base time for the input capture */
1507 TIM_Base_SetConfig(htim
->Instance
, &htim
->Init
);
1509 /* Initialize the TIM state*/
1510 htim
->State
= HAL_TIM_STATE_READY
;
1516 * @brief DeInitializes the TIM peripheral
1517 * @param htim : TIM Input Capture handle
1518 * @retval HAL status
1520 HAL_StatusTypeDef
HAL_TIM_IC_DeInit(TIM_HandleTypeDef
*htim
)
1522 /* Check the parameters */
1523 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
1525 htim
->State
= HAL_TIM_STATE_BUSY
;
1527 /* Disable the TIM Peripheral Clock */
1528 __HAL_TIM_DISABLE(htim
);
1530 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
1531 HAL_TIM_IC_MspDeInit(htim
);
1533 /* Change TIM state */
1534 htim
->State
= HAL_TIM_STATE_RESET
;
1543 * @brief Initializes the TIM Input Capture MSP.
1544 * @param htim : TIM handle
1547 __weak
void HAL_TIM_IC_MspInit(TIM_HandleTypeDef
*htim
)
1549 /* NOTE : This function Should not be modified, when the callback is needed,
1550 the HAL_TIM_IC_MspInit could be implemented in the user file
1555 * @brief DeInitializes TIM Input Capture MSP.
1556 * @param htim : TIM handle
1559 __weak
void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef
*htim
)
1561 /* NOTE : This function Should not be modified, when the callback is needed,
1562 the HAL_TIM_IC_MspDeInit could be implemented in the user file
1567 * @brief Starts the TIM Input Capture measurement.
1568 * @param htim : TIM Input Capture handle
1569 * @param Channel : TIM Channels to be enabled
1570 * This parameter can be one of the following values:
1571 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1572 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1573 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1574 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1575 * @retval HAL status
1577 HAL_StatusTypeDef
HAL_TIM_IC_Start (TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1579 /* Check the parameters */
1580 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1582 /* Enable the Input Capture channel */
1583 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
1585 /* Enable the Peripheral */
1586 __HAL_TIM_ENABLE(htim
);
1588 /* Return function status */
1593 * @brief Stops the TIM Input Capture measurement.
1594 * @param htim : TIM handle
1595 * @param Channel : TIM Channels to be disabled
1596 * This parameter can be one of the following values:
1597 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1598 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1599 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1600 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1601 * @retval HAL status
1603 HAL_StatusTypeDef
HAL_TIM_IC_Stop(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1605 /* Check the parameters */
1606 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1608 /* Disable the Input Capture channel */
1609 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
1611 /* Disable the Peripheral */
1612 __HAL_TIM_DISABLE(htim
);
1614 /* Return function status */
1619 * @brief Starts the TIM Input Capture measurement in interrupt mode.
1620 * @param htim : TIM Input Capture handle
1621 * @param Channel : TIM Channels to be enabled
1622 * This parameter can be one of the following values:
1623 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1624 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1625 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1626 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1627 * @retval HAL status
1629 HAL_StatusTypeDef
HAL_TIM_IC_Start_IT (TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1631 /* Check the parameters */
1632 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1638 /* Enable the TIM Capture/Compare 1 interrupt */
1639 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC1
);
1645 /* Enable the TIM Capture/Compare 2 interrupt */
1646 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC2
);
1652 /* Enable the TIM Capture/Compare 3 interrupt */
1653 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC3
);
1659 /* Enable the TIM Capture/Compare 4 interrupt */
1660 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC4
);
1667 /* Enable the Input Capture channel */
1668 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
1670 /* Enable the Peripheral */
1671 __HAL_TIM_ENABLE(htim
);
1673 /* Return function status */
1678 * @brief Stops the TIM Input Capture measurement in interrupt mode.
1679 * @param htim : TIM handle
1680 * @param Channel : TIM Channels to be disabled
1681 * This parameter can be one of the following values:
1682 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1683 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1684 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1685 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1686 * @retval HAL status
1688 HAL_StatusTypeDef
HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1690 /* Check the parameters */
1691 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1697 /* Disable the TIM Capture/Compare 1 interrupt */
1698 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC1
);
1704 /* Disable the TIM Capture/Compare 2 interrupt */
1705 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC2
);
1711 /* Disable the TIM Capture/Compare 3 interrupt */
1712 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC3
);
1718 /* Disable the TIM Capture/Compare 4 interrupt */
1719 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC4
);
1727 /* Disable the Input Capture channel */
1728 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
1730 /* Disable the Peripheral */
1731 __HAL_TIM_DISABLE(htim
);
1733 /* Return function status */
1738 * @brief Starts the TIM Input Capture measurement in DMA mode.
1739 * @param htim : TIM Input Capture handle
1740 * @param Channel : TIM Channels to be enabled
1741 * This parameter can be one of the following values:
1742 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1743 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1744 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1745 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1746 * @param pData : The destination Buffer address.
1747 * @param Length : The length of data to be transferred from TIM peripheral to memory.
1748 * @retval HAL status
1750 HAL_StatusTypeDef
HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef
*htim
, uint32_t Channel
, uint32_t *pData
, uint16_t Length
)
1752 /* Check the parameters */
1753 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1754 assert_param(IS_TIM_DMA_CC_INSTANCE(htim
->Instance
));
1756 if((htim
->State
== HAL_TIM_STATE_BUSY
))
1760 else if((htim
->State
== HAL_TIM_STATE_READY
))
1762 if((pData
== 0 ) && (Length
> 0))
1768 htim
->State
= HAL_TIM_STATE_BUSY
;
1776 /* Set the DMA Period elapsed callback */
1777 htim
->hdma
[TIM_DMA_ID_CC1
]->XferCpltCallback
= TIM_DMACaptureCplt
;
1779 /* Set the DMA error callback */
1780 htim
->hdma
[TIM_DMA_ID_CC1
]->XferErrorCallback
= TIM_DMAError
;
1782 /* Enable the DMA channel */
1783 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC1
], (uint32_t)&htim
->Instance
->CCR1
, (uint32_t)pData
, Length
);
1785 /* Enable the TIM Capture/Compare 1 DMA request */
1786 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC1
);
1792 /* Set the DMA Period elapsed callback */
1793 htim
->hdma
[TIM_DMA_ID_CC2
]->XferCpltCallback
= TIM_DMACaptureCplt
;
1795 /* Set the DMA error callback */
1796 htim
->hdma
[TIM_DMA_ID_CC2
]->XferErrorCallback
= TIM_DMAError
;
1798 /* Enable the DMA channel */
1799 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC2
], (uint32_t)&htim
->Instance
->CCR2
, (uint32_t)pData
, Length
);
1801 /* Enable the TIM Capture/Compare 2 DMA request */
1802 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC2
);
1808 /* Set the DMA Period elapsed callback */
1809 htim
->hdma
[TIM_DMA_ID_CC3
]->XferCpltCallback
= TIM_DMACaptureCplt
;
1811 /* Set the DMA error callback */
1812 htim
->hdma
[TIM_DMA_ID_CC3
]->XferErrorCallback
= TIM_DMAError
;
1814 /* Enable the DMA channel */
1815 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC3
], (uint32_t)&htim
->Instance
->CCR3
, (uint32_t)pData
, Length
);
1817 /* Enable the TIM Capture/Compare 3 DMA request */
1818 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC3
);
1824 /* Set the DMA Period elapsed callback */
1825 htim
->hdma
[TIM_DMA_ID_CC4
]->XferCpltCallback
= TIM_DMACaptureCplt
;
1827 /* Set the DMA error callback */
1828 htim
->hdma
[TIM_DMA_ID_CC4
]->XferErrorCallback
= TIM_DMAError
;
1830 /* Enable the DMA channel */
1831 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC4
], (uint32_t)&htim
->Instance
->CCR4
, (uint32_t)pData
, Length
);
1833 /* Enable the TIM Capture/Compare 4 DMA request */
1834 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC4
);
1842 /* Enable the Input Capture channel */
1843 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_ENABLE
);
1845 /* Enable the Peripheral */
1846 __HAL_TIM_ENABLE(htim
);
1848 /* Return function status */
1853 * @brief Stops the TIM Input Capture measurement in DMA mode.
1854 * @param htim : TIM Input Capture handle
1855 * @param Channel : TIM Channels to be disabled
1856 * This parameter can be one of the following values:
1857 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1858 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1859 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1860 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1861 * @retval HAL status
1863 HAL_StatusTypeDef
HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
1865 /* Check the parameters */
1866 assert_param(IS_TIM_CCX_INSTANCE(htim
->Instance
, Channel
));
1867 assert_param(IS_TIM_DMA_CC_INSTANCE(htim
->Instance
));
1873 /* Disable the TIM Capture/Compare 1 DMA request */
1874 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC1
);
1880 /* Disable the TIM Capture/Compare 2 DMA request */
1881 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC2
);
1887 /* Disable the TIM Capture/Compare 3 DMA request */
1888 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC3
);
1894 /* Disable the TIM Capture/Compare 4 DMA request */
1895 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC4
);
1903 /* Disable the Input Capture channel */
1904 TIM_CCxChannelCmd(htim
->Instance
, Channel
, TIM_CCx_DISABLE
);
1906 /* Disable the Peripheral */
1907 __HAL_TIM_DISABLE(htim
);
1909 /* Change the htim state */
1910 htim
->State
= HAL_TIM_STATE_READY
;
1912 /* Return function status */
1919 /** @defgroup TIM_Exported_Functions_Group5 Time One Pulse functions
1920 * @brief Time One Pulse functions
1923 ==============================================================================
1924 ##### Time One Pulse functions #####
1925 ==============================================================================
1927 This section provides functions allowing to:
1928 (+) Initialize and configure the TIM One Pulse.
1929 (+) De-initialize the TIM One Pulse.
1930 (+) Start the Time One Pulse.
1931 (+) Stop the Time One Pulse.
1932 (+) Start the Time One Pulse and enable interrupt.
1933 (+) Stop the Time One Pulse and disable interrupt.
1934 (+) Start the Time One Pulse and enable DMA transfer.
1935 (+) Stop the Time One Pulse and disable DMA transfer.
1941 * @brief Initializes the TIM One Pulse Time Base according to the specified
1942 * parameters in the TIM_HandleTypeDef and create the associated handle.
1943 * @param htim : TIM OnePulse handle
1944 * @param OnePulseMode : Select the One pulse mode.
1945 * This parameter can be one of the following values:
1946 * @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
1947 * @arg TIM_OPMODE_REPETITIVE: Repetitive pulses wil be generated.
1948 * @retval HAL status
1950 HAL_StatusTypeDef
HAL_TIM_OnePulse_Init(TIM_HandleTypeDef
*htim
, uint32_t OnePulseMode
)
1952 /* Check the TIM handle allocation */
1958 /* Check the parameters */
1959 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
1960 assert_param(IS_TIM_COUNTER_MODE(htim
->Init
.CounterMode
));
1961 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim
->Init
.ClockDivision
));
1962 assert_param(IS_TIM_OPM_MODE(OnePulseMode
));
1964 if(htim
->State
== HAL_TIM_STATE_RESET
)
1966 /* Allocate lock resource and initialize it */
1967 htim
-> Lock
= HAL_UNLOCKED
;
1969 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
1970 HAL_TIM_OnePulse_MspInit(htim
);
1973 /* Set the TIM state */
1974 htim
->State
= HAL_TIM_STATE_BUSY
;
1976 /* Configure the Time base in the One Pulse Mode */
1977 TIM_Base_SetConfig(htim
->Instance
, &htim
->Init
);
1979 /* Reset the OPM Bit */
1980 htim
->Instance
->CR1
&= ~TIM_CR1_OPM
;
1982 /* Configure the OPM Mode */
1983 htim
->Instance
->CR1
|= OnePulseMode
;
1985 /* Initialize the TIM state*/
1986 htim
->State
= HAL_TIM_STATE_READY
;
1992 * @brief DeInitializes the TIM One Pulse
1993 * @param htim : TIM One Pulse handle
1994 * @retval HAL status
1996 HAL_StatusTypeDef
HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef
*htim
)
1998 /* Check the parameters */
1999 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
2001 htim
->State
= HAL_TIM_STATE_BUSY
;
2003 /* Disable the TIM Peripheral Clock */
2004 __HAL_TIM_DISABLE(htim
);
2006 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
2007 HAL_TIM_OnePulse_MspDeInit(htim
);
2009 /* Change TIM state */
2010 htim
->State
= HAL_TIM_STATE_RESET
;
2019 * @brief Initializes the TIM One Pulse MSP.
2020 * @param htim : TIM handle
2023 __weak
void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef
*htim
)
2025 /* NOTE : This function Should not be modified, when the callback is needed,
2026 the HAL_TIM_OnePulse_MspInit could be implemented in the user file
2031 * @brief DeInitializes TIM One Pulse MSP.
2032 * @param htim : TIM handle
2035 __weak
void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef
*htim
)
2037 /* NOTE : This function Should not be modified, when the callback is needed,
2038 the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
2043 * @brief Starts the TIM One Pulse signal generation.
2044 * @param htim : TIM One Pulse handle
2045 * @param OutputChannel : TIM Channels to be enabled
2046 * This parameter can be one of the following values:
2047 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2048 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2049 * @retval HAL status
2051 HAL_StatusTypeDef
HAL_TIM_OnePulse_Start(TIM_HandleTypeDef
*htim
, uint32_t OutputChannel
)
2053 /* Enable the Capture compare and the Input Capture channels
2054 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2055 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2056 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2057 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2059 No need to enable the counter, it's enabled automatically by hardware
2060 (the counter starts in response to a stimulus and generate a pulse */
2062 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_ENABLE
);
2063 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_ENABLE
);
2065 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
2067 /* Enable the main output */
2068 __HAL_TIM_MOE_ENABLE(htim
);
2071 /* Return function status */
2076 * @brief Stops the TIM One Pulse signal generation.
2077 * @param htim : TIM One Pulse handle
2078 * @param OutputChannel : TIM Channels to be disable
2079 * This parameter can be one of the following values:
2080 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2081 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2082 * @retval HAL status
2084 HAL_StatusTypeDef
HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef
*htim
, uint32_t OutputChannel
)
2086 /* Disable the Capture compare and the Input Capture channels
2087 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2088 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2089 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2090 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2092 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_DISABLE
);
2093 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_DISABLE
);
2095 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
2097 /* Disable the Main Ouput */
2098 __HAL_TIM_MOE_DISABLE(htim
);
2101 /* Disable the Peripheral */
2102 __HAL_TIM_DISABLE(htim
);
2104 /* Return function status */
2109 * @brief Starts the TIM One Pulse signal generation in interrupt mode.
2110 * @param htim : TIM One Pulse handle
2111 * @param OutputChannel : TIM Channels to be enabled
2112 * This parameter can be one of the following values:
2113 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2114 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2115 * @retval HAL status
2117 HAL_StatusTypeDef
HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef
*htim
, uint32_t OutputChannel
)
2119 /* Enable the Capture compare and the Input Capture channels
2120 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2121 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2122 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2123 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
2125 No need to enable the counter, it's enabled automatically by hardware
2126 (the counter starts in response to a stimulus and generate a pulse */
2128 /* Enable the TIM Capture/Compare 1 interrupt */
2129 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC1
);
2131 /* Enable the TIM Capture/Compare 2 interrupt */
2132 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC2
);
2134 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_ENABLE
);
2135 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_ENABLE
);
2137 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
2139 /* Enable the main output */
2140 __HAL_TIM_MOE_ENABLE(htim
);
2143 /* Return function status */
2148 * @brief Stops the TIM One Pulse signal generation in interrupt mode.
2149 * @param htim : TIM One Pulse handle
2150 * @param OutputChannel : TIM Channels to be enabled
2151 * This parameter can be one of the following values:
2152 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2153 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2154 * @retval HAL status
2156 HAL_StatusTypeDef
HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef
*htim
, uint32_t OutputChannel
)
2158 /* Disable the TIM Capture/Compare 1 interrupt */
2159 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC1
);
2161 /* Disable the TIM Capture/Compare 2 interrupt */
2162 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC2
);
2164 /* Disable the Capture compare and the Input Capture channels
2165 (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
2166 if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
2167 if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
2168 in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
2169 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_DISABLE
);
2170 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_DISABLE
);
2172 if(IS_TIM_BREAK_INSTANCE(htim
->Instance
) != RESET
)
2174 /* Disable the Main Ouput */
2175 __HAL_TIM_MOE_DISABLE(htim
);
2178 /* Disable the Peripheral */
2179 __HAL_TIM_DISABLE(htim
);
2181 /* Return function status */
2189 /** @defgroup TIM_Exported_Functions_Group6 Time Encoder functions
2190 * @brief Time Encoder functions
2193 ==============================================================================
2194 ##### Time Encoder functions #####
2195 ==============================================================================
2197 This section provides functions allowing to:
2198 (+) Initialize and configure the TIM Encoder.
2199 (+) De-initialize the TIM Encoder.
2200 (+) Start the Time Encoder.
2201 (+) Stop the Time Encoder.
2202 (+) Start the Time Encoder and enable interrupt.
2203 (+) Stop the Time Encoder and disable interrupt.
2204 (+) Start the Time Encoder and enable DMA transfer.
2205 (+) Stop the Time Encoder and disable DMA transfer.
2211 * @brief Initializes the TIM Encoder Interface and create the associated handle.
2212 * @param htim : TIM Encoder Interface handle
2213 * @param sConfig : TIM Encoder Interface configuration structure
2214 * @retval HAL status
2216 HAL_StatusTypeDef
HAL_TIM_Encoder_Init(TIM_HandleTypeDef
*htim
, TIM_Encoder_InitTypeDef
* sConfig
)
2218 uint32_t tmpsmcr
= 0;
2219 uint32_t tmpccmr1
= 0;
2220 uint32_t tmpccer
= 0;
2222 /* Check the TIM handle allocation */
2228 /* Check the parameters */
2229 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
2230 assert_param(IS_TIM_ENCODER_MODE(sConfig
->EncoderMode
));
2231 assert_param(IS_TIM_IC_SELECTION(sConfig
->IC1Selection
));
2232 assert_param(IS_TIM_IC_SELECTION(sConfig
->IC2Selection
));
2233 assert_param(IS_TIM_IC_POLARITY(sConfig
->IC1Polarity
));
2234 assert_param(IS_TIM_IC_POLARITY(sConfig
->IC2Polarity
));
2235 assert_param(IS_TIM_IC_PRESCALER(sConfig
->IC1Prescaler
));
2236 assert_param(IS_TIM_IC_PRESCALER(sConfig
->IC2Prescaler
));
2237 assert_param(IS_TIM_IC_FILTER(sConfig
->IC1Filter
));
2238 assert_param(IS_TIM_IC_FILTER(sConfig
->IC2Filter
));
2240 if(htim
->State
== HAL_TIM_STATE_RESET
)
2242 /* Allocate lock resource and initialize it */
2243 htim
-> Lock
= HAL_UNLOCKED
;
2245 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
2246 HAL_TIM_Encoder_MspInit(htim
);
2249 /* Set the TIM state */
2250 htim
->State
= HAL_TIM_STATE_BUSY
;
2252 /* Reset the SMS bits */
2253 htim
->Instance
->SMCR
&= ~TIM_SMCR_SMS
;
2255 /* Configure the Time base in the Encoder Mode */
2256 TIM_Base_SetConfig(htim
->Instance
, &htim
->Init
);
2258 /* Get the TIMx SMCR register value */
2259 tmpsmcr
= htim
->Instance
->SMCR
;
2261 /* Get the TIMx CCMR1 register value */
2262 tmpccmr1
= htim
->Instance
->CCMR1
;
2264 /* Get the TIMx CCER register value */
2265 tmpccer
= htim
->Instance
->CCER
;
2267 /* Set the encoder Mode */
2268 tmpsmcr
|= sConfig
->EncoderMode
;
2270 /* Select the Capture Compare 1 and the Capture Compare 2 as input */
2271 tmpccmr1
&= ~(TIM_CCMR1_CC1S
| TIM_CCMR1_CC2S
);
2272 tmpccmr1
|= (sConfig
->IC1Selection
| (sConfig
->IC2Selection
<< 8));
2274 /* Set the the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
2275 tmpccmr1
&= ~(TIM_CCMR1_IC1PSC
| TIM_CCMR1_IC2PSC
);
2276 tmpccmr1
&= ~(TIM_CCMR1_IC1F
| TIM_CCMR1_IC2F
);
2277 tmpccmr1
|= sConfig
->IC1Prescaler
| (sConfig
->IC2Prescaler
<< 8);
2278 tmpccmr1
|= (sConfig
->IC1Filter
<< 4) | (sConfig
->IC2Filter
<< 12);
2280 /* Set the TI1 and the TI2 Polarities */
2281 tmpccer
&= ~(TIM_CCER_CC1P
| TIM_CCER_CC2P
);
2282 tmpccer
&= ~(TIM_CCER_CC1NP
| TIM_CCER_CC2NP
);
2283 tmpccer
|= sConfig
->IC1Polarity
| (sConfig
->IC2Polarity
<< 4);
2285 /* Write to TIMx SMCR */
2286 htim
->Instance
->SMCR
= tmpsmcr
;
2288 /* Write to TIMx CCMR1 */
2289 htim
->Instance
->CCMR1
= tmpccmr1
;
2291 /* Write to TIMx CCER */
2292 htim
->Instance
->CCER
= tmpccer
;
2294 /* Initialize the TIM state*/
2295 htim
->State
= HAL_TIM_STATE_READY
;
2302 * @brief DeInitializes the TIM Encoder interface
2303 * @param htim : TIM Encoder handle
2304 * @retval HAL status
2306 HAL_StatusTypeDef
HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef
*htim
)
2308 /* Check the parameters */
2309 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
2311 htim
->State
= HAL_TIM_STATE_BUSY
;
2313 /* Disable the TIM Peripheral Clock */
2314 __HAL_TIM_DISABLE(htim
);
2316 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
2317 HAL_TIM_Encoder_MspDeInit(htim
);
2319 /* Change TIM state */
2320 htim
->State
= HAL_TIM_STATE_RESET
;
2329 * @brief Initializes the TIM Encoder Interface MSP.
2330 * @param htim : TIM handle
2333 __weak
void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef
*htim
)
2335 /* NOTE : This function Should not be modified, when the callback is needed,
2336 the HAL_TIM_Encoder_MspInit could be implemented in the user file
2341 * @brief DeInitializes TIM Encoder Interface MSP.
2342 * @param htim : TIM handle
2345 __weak
void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef
*htim
)
2347 /* NOTE : This function Should not be modified, when the callback is needed,
2348 the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
2353 * @brief Starts the TIM Encoder Interface.
2354 * @param htim : TIM Encoder Interface handle
2355 * @param Channel : TIM Channels to be enabled
2356 * This parameter can be one of the following values:
2357 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2358 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2359 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2360 * @retval HAL status
2362 HAL_StatusTypeDef
HAL_TIM_Encoder_Start(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
2364 /* Check the parameters */
2365 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
2367 /* Enable the encoder interface channels */
2372 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_ENABLE
);
2377 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_ENABLE
);
2382 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_ENABLE
);
2383 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_ENABLE
);
2387 /* Enable the Peripheral */
2388 __HAL_TIM_ENABLE(htim
);
2390 /* Return function status */
2395 * @brief Stops the TIM Encoder Interface.
2396 * @param htim : TIM Encoder Interface handle
2397 * @param Channel : TIM Channels to be disabled
2398 * This parameter can be one of the following values:
2399 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2400 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2401 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2402 * @retval HAL status
2404 HAL_StatusTypeDef
HAL_TIM_Encoder_Stop(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
2406 /* Check the parameters */
2407 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
2409 /* Disable the Input Capture channels 1 and 2
2410 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
2415 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_DISABLE
);
2420 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_DISABLE
);
2425 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_DISABLE
);
2426 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_DISABLE
);
2431 /* Disable the Peripheral */
2432 __HAL_TIM_DISABLE(htim
);
2434 /* Return function status */
2439 * @brief Starts the TIM Encoder Interface in interrupt mode.
2440 * @param htim : TIM Encoder Interface handle
2441 * @param Channel : TIM Channels to be enabled
2442 * This parameter can be one of the following values:
2443 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2444 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2445 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2446 * @retval HAL status
2448 HAL_StatusTypeDef
HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
2450 /* Check the parameters */
2451 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
2453 /* Enable the encoder interface channels */
2454 /* Enable the capture compare Interrupts 1 and/or 2 */
2459 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_ENABLE
);
2460 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC1
);
2465 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_ENABLE
);
2466 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC2
);
2471 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_ENABLE
);
2472 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_ENABLE
);
2473 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC1
);
2474 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_CC2
);
2479 /* Enable the Peripheral */
2480 __HAL_TIM_ENABLE(htim
);
2482 /* Return function status */
2487 * @brief Stops the TIM Encoder Interface in interrupt mode.
2488 * @param htim : TIM Encoder Interface handle
2489 * @param Channel : TIM Channels to be disabled
2490 * This parameter can be one of the following values:
2491 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2492 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2493 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2494 * @retval HAL status
2496 HAL_StatusTypeDef
HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
2498 /* Check the parameters */
2499 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
2501 /* Disable the Input Capture channels 1 and 2
2502 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
2503 if(Channel
== TIM_CHANNEL_1
)
2505 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_DISABLE
);
2507 /* Disable the capture compare Interrupts 1 */
2508 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC1
);
2510 else if(Channel
== TIM_CHANNEL_2
)
2512 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_DISABLE
);
2514 /* Disable the capture compare Interrupts 2 */
2515 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC2
);
2519 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_DISABLE
);
2520 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_DISABLE
);
2522 /* Disable the capture compare Interrupts 1 and 2 */
2523 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC1
);
2524 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_CC2
);
2527 /* Disable the Peripheral */
2528 __HAL_TIM_DISABLE(htim
);
2530 /* Change the htim state */
2531 htim
->State
= HAL_TIM_STATE_READY
;
2533 /* Return function status */
2538 * @brief Starts the TIM Encoder Interface in DMA mode.
2539 * @param htim : TIM Encoder Interface handle
2540 * @param Channel : TIM Channels to be enabled
2541 * This parameter can be one of the following values:
2542 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2543 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2544 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2545 * @param pData1 : The destination Buffer address for IC1.
2546 * @param pData2 : The destination Buffer address for IC2.
2547 * @param Length : The length of data to be transferred from TIM peripheral to memory.
2548 * @retval HAL status
2550 HAL_StatusTypeDef
HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef
*htim
, uint32_t Channel
, uint32_t *pData1
, uint32_t *pData2
, uint16_t Length
)
2552 /* Check the parameters */
2553 assert_param(IS_TIM_DMA_CC_INSTANCE(htim
->Instance
));
2555 if((htim
->State
== HAL_TIM_STATE_BUSY
))
2559 else if((htim
->State
== HAL_TIM_STATE_READY
))
2561 if((((pData1
== 0) || (pData2
== 0) )) && (Length
> 0))
2567 htim
->State
= HAL_TIM_STATE_BUSY
;
2575 /* Set the DMA Period elapsed callback */
2576 htim
->hdma
[TIM_DMA_ID_CC1
]->XferCpltCallback
= TIM_DMACaptureCplt
;
2578 /* Set the DMA error callback */
2579 htim
->hdma
[TIM_DMA_ID_CC1
]->XferErrorCallback
= TIM_DMAError
;
2581 /* Enable the DMA channel */
2582 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC1
], (uint32_t)&htim
->Instance
->CCR1
, (uint32_t )pData1
, Length
);
2584 /* Enable the TIM Input Capture DMA request */
2585 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC1
);
2587 /* Enable the Peripheral */
2588 __HAL_TIM_ENABLE(htim
);
2590 /* Enable the Capture compare channel */
2591 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_ENABLE
);
2597 /* Set the DMA Period elapsed callback */
2598 htim
->hdma
[TIM_DMA_ID_CC2
]->XferCpltCallback
= TIM_DMACaptureCplt
;
2600 /* Set the DMA error callback */
2601 htim
->hdma
[TIM_DMA_ID_CC2
]->XferErrorCallback
= TIM_DMAError
;
2602 /* Enable the DMA channel */
2603 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC2
], (uint32_t)&htim
->Instance
->CCR2
, (uint32_t)pData2
, Length
);
2605 /* Enable the TIM Input Capture DMA request */
2606 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC2
);
2608 /* Enable the Peripheral */
2609 __HAL_TIM_ENABLE(htim
);
2611 /* Enable the Capture compare channel */
2612 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_ENABLE
);
2616 case TIM_CHANNEL_ALL
:
2618 /* Set the DMA Period elapsed callback */
2619 htim
->hdma
[TIM_DMA_ID_CC1
]->XferCpltCallback
= TIM_DMACaptureCplt
;
2621 /* Set the DMA error callback */
2622 htim
->hdma
[TIM_DMA_ID_CC1
]->XferErrorCallback
= TIM_DMAError
;
2624 /* Enable the DMA channel */
2625 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC1
], (uint32_t)&htim
->Instance
->CCR1
, (uint32_t)pData1
, Length
);
2627 /* Set the DMA Period elapsed callback */
2628 htim
->hdma
[TIM_DMA_ID_CC2
]->XferCpltCallback
= TIM_DMACaptureCplt
;
2630 /* Set the DMA error callback */
2631 htim
->hdma
[TIM_DMA_ID_CC2
]->XferErrorCallback
= TIM_DMAError
;
2633 /* Enable the DMA channel */
2634 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC2
], (uint32_t)&htim
->Instance
->CCR2
, (uint32_t)pData2
, Length
);
2636 /* Enable the Peripheral */
2637 __HAL_TIM_ENABLE(htim
);
2639 /* Enable the Capture compare channel */
2640 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_ENABLE
);
2641 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_ENABLE
);
2643 /* Enable the TIM Input Capture DMA request */
2644 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC1
);
2645 /* Enable the TIM Input Capture DMA request */
2646 __HAL_TIM_ENABLE_DMA(htim
, TIM_DMA_CC2
);
2653 /* Return function status */
2658 * @brief Stops the TIM Encoder Interface in DMA mode.
2659 * @param htim : TIM Encoder Interface handle
2660 * @param Channel : TIM Channels to be enabled
2661 * This parameter can be one of the following values:
2662 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2663 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2664 * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
2665 * @retval HAL status
2667 HAL_StatusTypeDef
HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
2669 /* Check the parameters */
2670 assert_param(IS_TIM_DMA_CC_INSTANCE(htim
->Instance
));
2672 /* Disable the Input Capture channels 1 and 2
2673 (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
2674 if(Channel
== TIM_CHANNEL_1
)
2676 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_DISABLE
);
2678 /* Disable the capture compare DMA Request 1 */
2679 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC1
);
2681 else if(Channel
== TIM_CHANNEL_2
)
2683 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_DISABLE
);
2685 /* Disable the capture compare DMA Request 2 */
2686 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC2
);
2690 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_1
, TIM_CCx_DISABLE
);
2691 TIM_CCxChannelCmd(htim
->Instance
, TIM_CHANNEL_2
, TIM_CCx_DISABLE
);
2693 /* Disable the capture compare DMA Request 1 and 2 */
2694 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC1
);
2695 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_CC2
);
2698 /* Disable the Peripheral */
2699 __HAL_TIM_DISABLE(htim
);
2701 /* Change the htim state */
2702 htim
->State
= HAL_TIM_STATE_READY
;
2704 /* Return function status */
2711 /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
2712 * @brief IRQ handler management
2715 ==============================================================================
2716 ##### IRQ handler management #####
2717 ==============================================================================
2719 This section provides Timer IRQ handler function.
2725 * @brief This function handles TIM interrupts requests.
2726 * @param htim : TIM handle
2729 void HAL_TIM_IRQHandler(TIM_HandleTypeDef
*htim
)
2731 /* Capture compare 1 event */
2732 if(__HAL_TIM_GET_FLAG(htim
, TIM_FLAG_CC1
) != RESET
)
2734 if(__HAL_TIM_GET_IT_SOURCE(htim
, TIM_IT_CC1
) !=RESET
)
2737 __HAL_TIM_CLEAR_IT(htim
, TIM_IT_CC1
);
2738 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_1
;
2740 /* Input capture event */
2741 if((htim
->Instance
->CCMR1
& TIM_CCMR1_CC1S
) != 0x00)
2743 HAL_TIM_IC_CaptureCallback(htim
);
2745 /* Output compare event */
2748 HAL_TIM_OC_DelayElapsedCallback(htim
);
2749 HAL_TIM_PWM_PulseFinishedCallback(htim
);
2751 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_CLEARED
;
2755 /* Capture compare 2 event */
2756 if(__HAL_TIM_GET_FLAG(htim
, TIM_FLAG_CC2
) != RESET
)
2758 if(__HAL_TIM_GET_IT_SOURCE(htim
, TIM_IT_CC2
) !=RESET
)
2760 __HAL_TIM_CLEAR_IT(htim
, TIM_IT_CC2
);
2761 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_2
;
2762 /* Input capture event */
2763 if((htim
->Instance
->CCMR1
& TIM_CCMR1_CC2S
) != 0x00)
2765 HAL_TIM_IC_CaptureCallback(htim
);
2767 /* Output compare event */
2770 HAL_TIM_OC_DelayElapsedCallback(htim
);
2771 HAL_TIM_PWM_PulseFinishedCallback(htim
);
2773 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_CLEARED
;
2776 /* Capture compare 3 event */
2777 if(__HAL_TIM_GET_FLAG(htim
, TIM_FLAG_CC3
) != RESET
)
2779 if(__HAL_TIM_GET_IT_SOURCE(htim
, TIM_IT_CC3
) !=RESET
)
2781 __HAL_TIM_CLEAR_IT(htim
, TIM_IT_CC3
);
2782 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_3
;
2783 /* Input capture event */
2784 if((htim
->Instance
->CCMR2
& TIM_CCMR2_CC3S
) != 0x00)
2786 HAL_TIM_IC_CaptureCallback(htim
);
2788 /* Output compare event */
2791 HAL_TIM_OC_DelayElapsedCallback(htim
);
2792 HAL_TIM_PWM_PulseFinishedCallback(htim
);
2794 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_CLEARED
;
2797 /* Capture compare 4 event */
2798 if(__HAL_TIM_GET_FLAG(htim
, TIM_FLAG_CC4
) != RESET
)
2800 if(__HAL_TIM_GET_IT_SOURCE(htim
, TIM_IT_CC4
) !=RESET
)
2802 __HAL_TIM_CLEAR_IT(htim
, TIM_IT_CC4
);
2803 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_4
;
2804 /* Input capture event */
2805 if((htim
->Instance
->CCMR2
& TIM_CCMR2_CC4S
) != 0x00)
2807 HAL_TIM_IC_CaptureCallback(htim
);
2809 /* Output compare event */
2812 HAL_TIM_OC_DelayElapsedCallback(htim
);
2813 HAL_TIM_PWM_PulseFinishedCallback(htim
);
2815 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_CLEARED
;
2818 /* TIM Update event */
2819 if(__HAL_TIM_GET_FLAG(htim
, TIM_FLAG_UPDATE
) != RESET
)
2821 if(__HAL_TIM_GET_IT_SOURCE(htim
, TIM_IT_UPDATE
) !=RESET
)
2823 __HAL_TIM_CLEAR_IT(htim
, TIM_IT_UPDATE
);
2824 HAL_TIM_PeriodElapsedCallback(htim
);
2827 /* TIM Break input event */
2828 if(__HAL_TIM_GET_FLAG(htim
, TIM_FLAG_BREAK
) != RESET
)
2830 if(__HAL_TIM_GET_IT_SOURCE(htim
, TIM_IT_BREAK
) !=RESET
)
2832 __HAL_TIM_CLEAR_IT(htim
, TIM_IT_BREAK
);
2833 HAL_TIMEx_BreakCallback(htim
);
2836 /* TIM Trigger detection event */
2837 if(__HAL_TIM_GET_FLAG(htim
, TIM_FLAG_TRIGGER
) != RESET
)
2839 if(__HAL_TIM_GET_IT_SOURCE(htim
, TIM_IT_TRIGGER
) !=RESET
)
2841 __HAL_TIM_CLEAR_IT(htim
, TIM_IT_TRIGGER
);
2842 HAL_TIM_TriggerCallback(htim
);
2845 /* TIM commutation event */
2846 if(__HAL_TIM_GET_FLAG(htim
, TIM_FLAG_COM
) != RESET
)
2848 if(__HAL_TIM_GET_IT_SOURCE(htim
, TIM_IT_COM
) !=RESET
)
2850 __HAL_TIM_CLEAR_IT(htim
, TIM_FLAG_COM
);
2851 HAL_TIMEx_CommutationCallback(htim
);
2860 /** @defgroup TIM_Exported_Functions_Group8 Peripheral Control functions
2861 * @brief Peripheral Control functions
2864 ==============================================================================
2865 ##### Peripheral Control functions #####
2866 ==============================================================================
2868 This section provides functions allowing to:
2869 (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
2870 (+) Configure External Clock source.
2871 (+) Configure Complementary channels, break features and dead time.
2872 (+) Configure Master and the Slave synchronization.
2873 (+) Configure the DMA Burst Mode.
2880 * @brief Initializes the TIM Output Compare Channels according to the specified
2881 * parameters in the TIM_OC_InitTypeDef.
2882 * @param htim : TIM Output Compare handle
2883 * @param sConfig : TIM Output Compare configuration structure
2884 * @param Channel : TIM Channels to be enabled
2885 * This parameter can be one of the following values:
2886 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2887 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2888 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2889 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2890 * @retval HAL status
2892 HAL_StatusTypeDef
HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef
*htim
, TIM_OC_InitTypeDef
* sConfig
, uint32_t Channel
)
2894 /* Check the parameters */
2895 assert_param(IS_TIM_CHANNELS(Channel
));
2896 assert_param(IS_TIM_OC_MODE(sConfig
->OCMode
));
2897 assert_param(IS_TIM_OC_POLARITY(sConfig
->OCPolarity
));
2898 assert_param(IS_TIM_OCN_POLARITY(sConfig
->OCNPolarity
));
2899 assert_param(IS_TIM_OCNIDLE_STATE(sConfig
->OCNIdleState
));
2900 assert_param(IS_TIM_OCIDLE_STATE(sConfig
->OCIdleState
));
2902 /* Check input state */
2905 htim
->State
= HAL_TIM_STATE_BUSY
;
2911 assert_param(IS_TIM_CC1_INSTANCE(htim
->Instance
));
2912 /* Configure the TIM Channel 1 in Output Compare */
2913 TIM_OC1_SetConfig(htim
->Instance
, sConfig
);
2919 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
2920 /* Configure the TIM Channel 2 in Output Compare */
2921 TIM_OC2_SetConfig(htim
->Instance
, sConfig
);
2927 assert_param(IS_TIM_CC3_INSTANCE(htim
->Instance
));
2928 /* Configure the TIM Channel 3 in Output Compare */
2929 TIM_OC3_SetConfig(htim
->Instance
, sConfig
);
2935 assert_param(IS_TIM_CC4_INSTANCE(htim
->Instance
));
2936 /* Configure the TIM Channel 4 in Output Compare */
2937 TIM_OC4_SetConfig(htim
->Instance
, sConfig
);
2944 htim
->State
= HAL_TIM_STATE_READY
;
2952 * @brief Initializes the TIM Input Capture Channels according to the specified
2953 * parameters in the TIM_IC_InitTypeDef.
2954 * @param htim : TIM IC handle
2955 * @param sConfig : TIM Input Capture configuration structure
2956 * @param Channel : TIM Channels to be enabled
2957 * This parameter can be one of the following values:
2958 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
2959 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
2960 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
2961 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
2962 * @retval HAL status
2964 HAL_StatusTypeDef
HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef
*htim
, TIM_IC_InitTypeDef
* sConfig
, uint32_t Channel
)
2966 /* Check the parameters */
2967 assert_param(IS_TIM_CC1_INSTANCE(htim
->Instance
));
2968 assert_param(IS_TIM_IC_POLARITY(sConfig
->ICPolarity
));
2969 assert_param(IS_TIM_IC_SELECTION(sConfig
->ICSelection
));
2970 assert_param(IS_TIM_IC_PRESCALER(sConfig
->ICPrescaler
));
2971 assert_param(IS_TIM_IC_FILTER(sConfig
->ICFilter
));
2975 htim
->State
= HAL_TIM_STATE_BUSY
;
2977 if (Channel
== TIM_CHANNEL_1
)
2979 /* TI1 Configuration */
2980 TIM_TI1_SetConfig(htim
->Instance
,
2981 sConfig
->ICPolarity
,
2982 sConfig
->ICSelection
,
2985 /* Reset the IC1PSC Bits */
2986 htim
->Instance
->CCMR1
&= ~TIM_CCMR1_IC1PSC
;
2988 /* Set the IC1PSC value */
2989 htim
->Instance
->CCMR1
|= sConfig
->ICPrescaler
;
2991 else if (Channel
== TIM_CHANNEL_2
)
2993 /* TI2 Configuration */
2994 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
2996 TIM_TI2_SetConfig(htim
->Instance
,
2997 sConfig
->ICPolarity
,
2998 sConfig
->ICSelection
,
3001 /* Reset the IC2PSC Bits */
3002 htim
->Instance
->CCMR1
&= ~TIM_CCMR1_IC2PSC
;
3004 /* Set the IC2PSC value */
3005 htim
->Instance
->CCMR1
|= (sConfig
->ICPrescaler
<< 8);
3007 else if (Channel
== TIM_CHANNEL_3
)
3009 /* TI3 Configuration */
3010 assert_param(IS_TIM_CC3_INSTANCE(htim
->Instance
));
3012 TIM_TI3_SetConfig(htim
->Instance
,
3013 sConfig
->ICPolarity
,
3014 sConfig
->ICSelection
,
3017 /* Reset the IC3PSC Bits */
3018 htim
->Instance
->CCMR2
&= ~TIM_CCMR2_IC3PSC
;
3020 /* Set the IC3PSC value */
3021 htim
->Instance
->CCMR2
|= sConfig
->ICPrescaler
;
3025 /* TI4 Configuration */
3026 assert_param(IS_TIM_CC4_INSTANCE(htim
->Instance
));
3028 TIM_TI4_SetConfig(htim
->Instance
,
3029 sConfig
->ICPolarity
,
3030 sConfig
->ICSelection
,
3033 /* Reset the IC4PSC Bits */
3034 htim
->Instance
->CCMR2
&= ~TIM_CCMR2_IC4PSC
;
3036 /* Set the IC4PSC value */
3037 htim
->Instance
->CCMR2
|= (sConfig
->ICPrescaler
<< 8);
3040 htim
->State
= HAL_TIM_STATE_READY
;
3048 * @brief Initializes the TIM PWM channels according to the specified
3049 * parameters in the TIM_OC_InitTypeDef.
3050 * @param htim : TIM handle
3051 * @param sConfig : TIM PWM configuration structure
3052 * @param Channel : TIM Channels to be enabled
3053 * This parameter can be one of the following values:
3054 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3055 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3056 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
3057 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
3058 * @retval HAL status
3060 HAL_StatusTypeDef
HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef
*htim
, TIM_OC_InitTypeDef
* sConfig
, uint32_t Channel
)
3064 /* Check the parameters */
3065 assert_param(IS_TIM_CHANNELS(Channel
));
3066 assert_param(IS_TIM_PWM_MODE(sConfig
->OCMode
));
3067 assert_param(IS_TIM_OC_POLARITY(sConfig
->OCPolarity
));
3068 assert_param(IS_TIM_OCN_POLARITY(sConfig
->OCNPolarity
));
3069 assert_param(IS_TIM_FAST_STATE(sConfig
->OCFastMode
));
3070 assert_param(IS_TIM_OCNIDLE_STATE(sConfig
->OCNIdleState
));
3071 assert_param(IS_TIM_OCIDLE_STATE(sConfig
->OCIdleState
));
3073 htim
->State
= HAL_TIM_STATE_BUSY
;
3079 assert_param(IS_TIM_CC1_INSTANCE(htim
->Instance
));
3080 /* Configure the Channel 1 in PWM mode */
3081 TIM_OC1_SetConfig(htim
->Instance
, sConfig
);
3083 /* Set the Preload enable bit for channel1 */
3084 htim
->Instance
->CCMR1
|= TIM_CCMR1_OC1PE
;
3086 /* Configure the Output Fast mode */
3087 htim
->Instance
->CCMR1
&= ~TIM_CCMR1_OC1FE
;
3088 htim
->Instance
->CCMR1
|= sConfig
->OCFastMode
;
3094 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
3095 /* Configure the Channel 2 in PWM mode */
3096 TIM_OC2_SetConfig(htim
->Instance
, sConfig
);
3098 /* Set the Preload enable bit for channel2 */
3099 htim
->Instance
->CCMR1
|= TIM_CCMR1_OC2PE
;
3101 /* Configure the Output Fast mode */
3102 htim
->Instance
->CCMR1
&= ~TIM_CCMR1_OC2FE
;
3103 htim
->Instance
->CCMR1
|= sConfig
->OCFastMode
<< 8;
3109 assert_param(IS_TIM_CC3_INSTANCE(htim
->Instance
));
3110 /* Configure the Channel 3 in PWM mode */
3111 TIM_OC3_SetConfig(htim
->Instance
, sConfig
);
3113 /* Set the Preload enable bit for channel3 */
3114 htim
->Instance
->CCMR2
|= TIM_CCMR2_OC3PE
;
3116 /* Configure the Output Fast mode */
3117 htim
->Instance
->CCMR2
&= ~TIM_CCMR2_OC3FE
;
3118 htim
->Instance
->CCMR2
|= sConfig
->OCFastMode
;
3124 assert_param(IS_TIM_CC4_INSTANCE(htim
->Instance
));
3125 /* Configure the Channel 4 in PWM mode */
3126 TIM_OC4_SetConfig(htim
->Instance
, sConfig
);
3128 /* Set the Preload enable bit for channel4 */
3129 htim
->Instance
->CCMR2
|= TIM_CCMR2_OC4PE
;
3131 /* Configure the Output Fast mode */
3132 htim
->Instance
->CCMR2
&= ~TIM_CCMR2_OC4FE
;
3133 htim
->Instance
->CCMR2
|= sConfig
->OCFastMode
<< 8;
3141 htim
->State
= HAL_TIM_STATE_READY
;
3149 * @brief Initializes the TIM One Pulse Channels according to the specified
3150 * parameters in the TIM_OnePulse_InitTypeDef.
3151 * @param htim : TIM One Pulse handle
3152 * @param sConfig : TIM One Pulse configuration structure
3153 * @param OutputChannel : TIM Channels to be enabled
3154 * This parameter can be one of the following values:
3155 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3156 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3157 * @param InputChannel : TIM Channels to be enabled
3158 * This parameter can be one of the following values:
3159 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
3160 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
3161 * @retval HAL status
3163 HAL_StatusTypeDef
HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef
*htim
, TIM_OnePulse_InitTypeDef
* sConfig
, uint32_t OutputChannel
, uint32_t InputChannel
)
3165 TIM_OC_InitTypeDef temp1
;
3167 /* Check the parameters */
3168 assert_param(IS_TIM_OPM_CHANNELS(OutputChannel
));
3169 assert_param(IS_TIM_OPM_CHANNELS(InputChannel
));
3171 if(OutputChannel
!= InputChannel
)
3175 htim
->State
= HAL_TIM_STATE_BUSY
;
3177 /* Extract the Ouput compare configuration from sConfig structure */
3178 temp1
.OCMode
= sConfig
->OCMode
;
3179 temp1
.Pulse
= sConfig
->Pulse
;
3180 temp1
.OCPolarity
= sConfig
->OCPolarity
;
3181 temp1
.OCNPolarity
= sConfig
->OCNPolarity
;
3182 temp1
.OCIdleState
= sConfig
->OCIdleState
;
3183 temp1
.OCNIdleState
= sConfig
->OCNIdleState
;
3185 switch (OutputChannel
)
3189 assert_param(IS_TIM_CC1_INSTANCE(htim
->Instance
));
3191 TIM_OC1_SetConfig(htim
->Instance
, &temp1
);
3196 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
3198 TIM_OC2_SetConfig(htim
->Instance
, &temp1
);
3204 switch (InputChannel
)
3208 assert_param(IS_TIM_CC1_INSTANCE(htim
->Instance
));
3210 TIM_TI1_SetConfig(htim
->Instance
, sConfig
->ICPolarity
,
3211 sConfig
->ICSelection
, sConfig
->ICFilter
);
3213 /* Reset the IC1PSC Bits */
3214 htim
->Instance
->CCMR1
&= ~TIM_CCMR1_IC1PSC
;
3216 /* Select the Trigger source */
3217 htim
->Instance
->SMCR
&= ~TIM_SMCR_TS
;
3218 htim
->Instance
->SMCR
|= TIM_TS_TI1FP1
;
3220 /* Select the Slave Mode */
3221 htim
->Instance
->SMCR
&= ~TIM_SMCR_SMS
;
3222 htim
->Instance
->SMCR
|= TIM_SLAVEMODE_TRIGGER
;
3227 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
3229 TIM_TI2_SetConfig(htim
->Instance
, sConfig
->ICPolarity
,
3230 sConfig
->ICSelection
, sConfig
->ICFilter
);
3232 /* Reset the IC2PSC Bits */
3233 htim
->Instance
->CCMR1
&= ~TIM_CCMR1_IC2PSC
;
3235 /* Select the Trigger source */
3236 htim
->Instance
->SMCR
&= ~TIM_SMCR_TS
;
3237 htim
->Instance
->SMCR
|= TIM_TS_TI2FP2
;
3239 /* Select the Slave Mode */
3240 htim
->Instance
->SMCR
&= ~TIM_SMCR_SMS
;
3241 htim
->Instance
->SMCR
|= TIM_SLAVEMODE_TRIGGER
;
3249 htim
->State
= HAL_TIM_STATE_READY
;
3262 * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
3263 * @param htim : TIM handle
3264 * @param BurstBaseAddress : TIM Base address from where the DMA will start the Data write
3265 * This parameter can be one of the following values:
3266 * @arg TIM_DMABASE_CR1
3267 * @arg TIM_DMABASE_CR2
3268 * @arg TIM_DMABASE_SMCR
3269 * @arg TIM_DMABASE_DIER
3270 * @arg TIM_DMABASE_SR
3271 * @arg TIM_DMABASE_EGR
3272 * @arg TIM_DMABASE_CCMR1
3273 * @arg TIM_DMABASE_CCMR2
3274 * @arg TIM_DMABASE_CCER
3275 * @arg TIM_DMABASE_CNT
3276 * @arg TIM_DMABASE_PSC
3277 * @arg TIM_DMABASE_ARR
3278 * @arg TIM_DMABASE_RCR
3279 * @arg TIM_DMABASE_CCR1
3280 * @arg TIM_DMABASE_CCR2
3281 * @arg TIM_DMABASE_CCR3
3282 * @arg TIM_DMABASE_CCR4
3283 * @arg TIM_DMABASE_BDTR
3284 * @arg TIM_DMABASE_DCR
3285 * @param BurstRequestSrc : TIM DMA Request sources
3286 * This parameter can be one of the following values:
3287 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
3288 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
3289 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
3290 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
3291 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
3292 * @arg TIM_DMA_COM: TIM Commutation DMA source
3293 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
3294 * @param BurstBuffer : The Buffer address.
3295 * @param BurstLength : DMA Burst length. This parameter can be one value
3296 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
3297 * @retval HAL status
3299 HAL_StatusTypeDef
HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef
*htim
, uint32_t BurstBaseAddress
, uint32_t BurstRequestSrc
,
3300 uint32_t* BurstBuffer
, uint32_t BurstLength
)
3302 /* Check the parameters */
3303 assert_param(IS_TIM_DMABURST_INSTANCE(htim
->Instance
));
3304 assert_param(IS_TIM_DMA_BASE(BurstBaseAddress
));
3305 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc
));
3306 assert_param(IS_TIM_DMA_LENGTH(BurstLength
));
3308 if((htim
->State
== HAL_TIM_STATE_BUSY
))
3312 else if((htim
->State
== HAL_TIM_STATE_READY
))
3314 if((BurstBuffer
== 0 ) && (BurstLength
> 0))
3320 htim
->State
= HAL_TIM_STATE_BUSY
;
3323 switch(BurstRequestSrc
)
3325 case TIM_DMA_UPDATE
:
3327 /* Set the DMA Period elapsed callback */
3328 htim
->hdma
[TIM_DMA_ID_UPDATE
]->XferCpltCallback
= TIM_DMAPeriodElapsedCplt
;
3330 /* Set the DMA error callback */
3331 htim
->hdma
[TIM_DMA_ID_UPDATE
]->XferErrorCallback
= TIM_DMAError
;
3333 /* Enable the DMA channel */
3334 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_UPDATE
], (uint32_t)BurstBuffer
, (uint32_t)&htim
->Instance
->DMAR
, ((BurstLength
) >> 8) + 1);
3339 /* Set the DMA Period elapsed callback */
3340 htim
->hdma
[TIM_DMA_ID_CC1
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
3342 /* Set the DMA error callback */
3343 htim
->hdma
[TIM_DMA_ID_CC1
]->XferErrorCallback
= TIM_DMAError
;
3345 /* Enable the DMA channel */
3346 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC1
], (uint32_t)BurstBuffer
, (uint32_t)&htim
->Instance
->DMAR
, ((BurstLength
) >> 8) + 1);
3351 /* Set the DMA Period elapsed callback */
3352 htim
->hdma
[TIM_DMA_ID_CC2
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
3354 /* Set the DMA error callback */
3355 htim
->hdma
[TIM_DMA_ID_CC2
]->XferErrorCallback
= TIM_DMAError
;
3357 /* Enable the DMA channel */
3358 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC2
], (uint32_t)BurstBuffer
, (uint32_t)&htim
->Instance
->DMAR
, ((BurstLength
) >> 8) + 1);
3363 /* Set the DMA Period elapsed callback */
3364 htim
->hdma
[TIM_DMA_ID_CC3
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
3366 /* Set the DMA error callback */
3367 htim
->hdma
[TIM_DMA_ID_CC3
]->XferErrorCallback
= TIM_DMAError
;
3369 /* Enable the DMA channel */
3370 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC3
], (uint32_t)BurstBuffer
, (uint32_t)&htim
->Instance
->DMAR
, ((BurstLength
) >> 8) + 1);
3375 /* Set the DMA Period elapsed callback */
3376 htim
->hdma
[TIM_DMA_ID_CC4
]->XferCpltCallback
= TIM_DMADelayPulseCplt
;
3378 /* Set the DMA error callback */
3379 htim
->hdma
[TIM_DMA_ID_CC4
]->XferErrorCallback
= TIM_DMAError
;
3381 /* Enable the DMA channel */
3382 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC4
], (uint32_t)BurstBuffer
, (uint32_t)&htim
->Instance
->DMAR
, ((BurstLength
) >> 8) + 1);
3387 /* Set the DMA Period elapsed callback */
3388 htim
->hdma
[TIM_DMA_ID_COMMUTATION
]->XferCpltCallback
= TIMEx_DMACommutationCplt
;
3390 /* Set the DMA error callback */
3391 htim
->hdma
[TIM_DMA_ID_COMMUTATION
]->XferErrorCallback
= TIM_DMAError
;
3393 /* Enable the DMA channel */
3394 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_COMMUTATION
], (uint32_t)BurstBuffer
, (uint32_t)&htim
->Instance
->DMAR
, ((BurstLength
) >> 8) + 1);
3397 case TIM_DMA_TRIGGER
:
3399 /* Set the DMA Period elapsed callback */
3400 htim
->hdma
[TIM_DMA_ID_TRIGGER
]->XferCpltCallback
= TIM_DMATriggerCplt
;
3402 /* Set the DMA error callback */
3403 htim
->hdma
[TIM_DMA_ID_TRIGGER
]->XferErrorCallback
= TIM_DMAError
;
3405 /* Enable the DMA channel */
3406 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_TRIGGER
], (uint32_t)BurstBuffer
, (uint32_t)&htim
->Instance
->DMAR
, ((BurstLength
) >> 8) + 1);
3412 /* configure the DMA Burst Mode */
3413 htim
->Instance
->DCR
= BurstBaseAddress
| BurstLength
;
3415 /* Enable the TIM DMA Request */
3416 __HAL_TIM_ENABLE_DMA(htim
, BurstRequestSrc
);
3418 htim
->State
= HAL_TIM_STATE_READY
;
3420 /* Return function status */
3425 * @brief Stops the TIM DMA Burst mode
3426 * @param htim : TIM handle
3427 * @param BurstRequestSrc : TIM DMA Request sources to disable
3428 * @retval HAL status
3430 HAL_StatusTypeDef
HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef
*htim
, uint32_t BurstRequestSrc
)
3432 /* Check the parameters */
3433 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc
));
3435 /* Abort the DMA transfer (at least disable the DMA channel) */
3436 switch(BurstRequestSrc
)
3438 case TIM_DMA_UPDATE
:
3440 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_UPDATE
]);
3445 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_CC1
]);
3450 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_CC2
]);
3455 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_CC3
]);
3460 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_CC4
]);
3465 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_COMMUTATION
]);
3468 case TIM_DMA_TRIGGER
:
3470 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_TRIGGER
]);
3477 /* Disable the TIM Update DMA request */
3478 __HAL_TIM_DISABLE_DMA(htim
, BurstRequestSrc
);
3480 /* Return function status */
3485 * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
3486 * @param htim : TIM handle
3487 * @param BurstBaseAddress : TIM Base address from where the DMA will starts the Data read
3488 * This parameter can be one of the following values:
3489 * @arg TIM_DMABASE_CR1
3490 * @arg TIM_DMABASE_CR2
3491 * @arg TIM_DMABASE_SMCR
3492 * @arg TIM_DMABASE_DIER
3493 * @arg TIM_DMABASE_SR
3494 * @arg TIM_DMABASE_EGR
3495 * @arg TIM_DMABASE_CCMR1
3496 * @arg TIM_DMABASE_CCMR2
3497 * @arg TIM_DMABASE_CCER
3498 * @arg TIM_DMABASE_CNT
3499 * @arg TIM_DMABASE_PSC
3500 * @arg TIM_DMABASE_ARR
3501 * @arg TIM_DMABASE_RCR
3502 * @arg TIM_DMABASE_CCR1
3503 * @arg TIM_DMABASE_CCR2
3504 * @arg TIM_DMABASE_CCR3
3505 * @arg TIM_DMABASE_CCR4
3506 * @arg TIM_DMABASE_BDTR
3507 * @arg TIM_DMABASE_DCR
3508 * @param BurstRequestSrc : TIM DMA Request sources
3509 * This parameter can be one of the following values:
3510 * @arg TIM_DMA_UPDATE: TIM update Interrupt source
3511 * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
3512 * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
3513 * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
3514 * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
3515 * @arg TIM_DMA_COM: TIM Commutation DMA source
3516 * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
3517 * @param BurstBuffer : The Buffer address.
3518 * @param BurstLength : DMA Burst length. This parameter can be one value
3519 * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
3520 * @retval HAL status
3522 HAL_StatusTypeDef
HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef
*htim
, uint32_t BurstBaseAddress
, uint32_t BurstRequestSrc
,
3523 uint32_t *BurstBuffer
, uint32_t BurstLength
)
3525 /* Check the parameters */
3526 assert_param(IS_TIM_DMABURST_INSTANCE(htim
->Instance
));
3527 assert_param(IS_TIM_DMA_BASE(BurstBaseAddress
));
3528 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc
));
3529 assert_param(IS_TIM_DMA_LENGTH(BurstLength
));
3531 if((htim
->State
== HAL_TIM_STATE_BUSY
))
3535 else if((htim
->State
== HAL_TIM_STATE_READY
))
3537 if((BurstBuffer
== 0 ) && (BurstLength
> 0))
3543 htim
->State
= HAL_TIM_STATE_BUSY
;
3546 switch(BurstRequestSrc
)
3548 case TIM_DMA_UPDATE
:
3550 /* Set the DMA Period elapsed callback */
3551 htim
->hdma
[TIM_DMA_ID_UPDATE
]->XferCpltCallback
= TIM_DMAPeriodElapsedCplt
;
3553 /* Set the DMA error callback */
3554 htim
->hdma
[TIM_DMA_ID_UPDATE
]->XferErrorCallback
= TIM_DMAError
;
3556 /* Enable the DMA channel */
3557 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_UPDATE
], (uint32_t)&htim
->Instance
->DMAR
, (uint32_t)BurstBuffer
, ((BurstLength
) >> 8) + 1);
3562 /* Set the DMA Period elapsed callback */
3563 htim
->hdma
[TIM_DMA_ID_CC1
]->XferCpltCallback
= TIM_DMACaptureCplt
;
3565 /* Set the DMA error callback */
3566 htim
->hdma
[TIM_DMA_ID_CC1
]->XferErrorCallback
= TIM_DMAError
;
3568 /* Enable the DMA channel */
3569 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC1
], (uint32_t)&htim
->Instance
->DMAR
, (uint32_t)BurstBuffer
, ((BurstLength
) >> 8) + 1);
3574 /* Set the DMA Period elapsed callback */
3575 htim
->hdma
[TIM_DMA_ID_CC2
]->XferCpltCallback
= TIM_DMACaptureCplt
;
3577 /* Set the DMA error callback */
3578 htim
->hdma
[TIM_DMA_ID_CC2
]->XferErrorCallback
= TIM_DMAError
;
3580 /* Enable the DMA channel */
3581 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC2
], (uint32_t)&htim
->Instance
->DMAR
, (uint32_t)BurstBuffer
, ((BurstLength
) >> 8) + 1);
3586 /* Set the DMA Period elapsed callback */
3587 htim
->hdma
[TIM_DMA_ID_CC3
]->XferCpltCallback
= TIM_DMACaptureCplt
;
3589 /* Set the DMA error callback */
3590 htim
->hdma
[TIM_DMA_ID_CC3
]->XferErrorCallback
= TIM_DMAError
;
3592 /* Enable the DMA channel */
3593 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC3
], (uint32_t)&htim
->Instance
->DMAR
, (uint32_t)BurstBuffer
, ((BurstLength
) >> 8) + 1);
3598 /* Set the DMA Period elapsed callback */
3599 htim
->hdma
[TIM_DMA_ID_CC4
]->XferCpltCallback
= TIM_DMACaptureCplt
;
3601 /* Set the DMA error callback */
3602 htim
->hdma
[TIM_DMA_ID_CC4
]->XferErrorCallback
= TIM_DMAError
;
3604 /* Enable the DMA channel */
3605 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_CC4
], (uint32_t)&htim
->Instance
->DMAR
, (uint32_t)BurstBuffer
, ((BurstLength
) >> 8) + 1);
3610 /* Set the DMA Period elapsed callback */
3611 htim
->hdma
[TIM_DMA_ID_COMMUTATION
]->XferCpltCallback
= TIMEx_DMACommutationCplt
;
3613 /* Set the DMA error callback */
3614 htim
->hdma
[TIM_DMA_ID_COMMUTATION
]->XferErrorCallback
= TIM_DMAError
;
3616 /* Enable the DMA channel */
3617 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_COMMUTATION
], (uint32_t)&htim
->Instance
->DMAR
, (uint32_t)BurstBuffer
, ((BurstLength
) >> 8) + 1);
3620 case TIM_DMA_TRIGGER
:
3622 /* Set the DMA Period elapsed callback */
3623 htim
->hdma
[TIM_DMA_ID_TRIGGER
]->XferCpltCallback
= TIM_DMATriggerCplt
;
3625 /* Set the DMA error callback */
3626 htim
->hdma
[TIM_DMA_ID_TRIGGER
]->XferErrorCallback
= TIM_DMAError
;
3628 /* Enable the DMA channel */
3629 HAL_DMA_Start_IT(htim
->hdma
[TIM_DMA_ID_TRIGGER
], (uint32_t)&htim
->Instance
->DMAR
, (uint32_t)BurstBuffer
, ((BurstLength
) >> 8) + 1);
3636 /* configure the DMA Burst Mode */
3637 htim
->Instance
->DCR
= BurstBaseAddress
| BurstLength
;
3639 /* Enable the TIM DMA Request */
3640 __HAL_TIM_ENABLE_DMA(htim
, BurstRequestSrc
);
3642 htim
->State
= HAL_TIM_STATE_READY
;
3644 /* Return function status */
3649 * @brief Stop the DMA burst reading
3650 * @param htim : TIM handle
3651 * @param BurstRequestSrc : TIM DMA Request sources to disable.
3652 * @retval HAL status
3654 HAL_StatusTypeDef
HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef
*htim
, uint32_t BurstRequestSrc
)
3656 /* Check the parameters */
3657 assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc
));
3659 /* Abort the DMA transfer (at least disable the DMA channel) */
3660 switch(BurstRequestSrc
)
3662 case TIM_DMA_UPDATE
:
3664 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_UPDATE
]);
3669 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_CC1
]);
3674 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_CC2
]);
3679 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_CC3
]);
3684 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_CC4
]);
3689 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_COMMUTATION
]);
3692 case TIM_DMA_TRIGGER
:
3694 HAL_DMA_Abort(htim
->hdma
[TIM_DMA_ID_TRIGGER
]);
3701 /* Disable the TIM Update DMA request */
3702 __HAL_TIM_DISABLE_DMA(htim
, BurstRequestSrc
);
3704 /* Return function status */
3709 * @brief Generate a software event
3710 * @param htim : TIM handle
3711 * @param EventSource : specifies the event source.
3712 * This parameter can be one of the following values:
3713 * @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
3714 * @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
3715 * @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
3716 * @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
3717 * @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
3718 * @arg TIM_EVENTSOURCE_COM: Timer COM event source
3719 * @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
3720 * @arg TIM_EVENTSOURCE_BREAK: Timer Break event source
3721 * @note TIM6 and TIM7 can only generate an update event.
3722 * @note TIM_EVENTSOURCE_COM and TIM_EVENTSOURCE_BREAK are used only with TIM1, TIM15, TIM16 and TIM17.
3723 * @retval HAL status
3726 HAL_StatusTypeDef
HAL_TIM_GenerateEvent(TIM_HandleTypeDef
*htim
, uint32_t EventSource
)
3728 /* Check the parameters */
3729 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
3730 assert_param(IS_TIM_EVENT_SOURCE(EventSource
));
3732 /* Process Locked */
3735 /* Change the TIM state */
3736 htim
->State
= HAL_TIM_STATE_BUSY
;
3738 /* Set the event sources */
3739 htim
->Instance
->EGR
= EventSource
;
3741 /* Change the TIM state */
3742 htim
->State
= HAL_TIM_STATE_READY
;
3746 /* Return function status */
3751 * @brief Configures the OCRef clear feature
3752 * @param htim : TIM handle
3753 * @param sClearInputConfig : pointer to a TIM_ClearInputConfigTypeDef structure that
3754 * contains the OCREF clear feature and parameters for the TIM peripheral.
3755 * @param Channel : specifies the TIM Channel
3756 * This parameter can be one of the following values:
3757 * @arg TIM_Channel_1: TIM Channel 1
3758 * @arg TIM_Channel_2: TIM Channel 2
3759 * @arg TIM_Channel_3: TIM Channel 3
3760 * @arg TIM_Channel_4: TIM Channel 4
3761 * @retval HAL status
3763 HAL_StatusTypeDef
HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef
*htim
, TIM_ClearInputConfigTypeDef
* sClearInputConfig
, uint32_t Channel
)
3765 uint32_t tmpsmcr
= 0;
3767 /* Check the parameters */
3768 assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim
->Instance
));
3769 assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig
->ClearInputSource
));
3770 assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig
->ClearInputPolarity
));
3771 assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig
->ClearInputPrescaler
));
3772 assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig
->ClearInputFilter
));
3774 /* Process Locked */
3777 htim
->State
= HAL_TIM_STATE_BUSY
;
3779 switch (sClearInputConfig
->ClearInputSource
)
3781 case TIM_CLEARINPUTSOURCE_NONE
:
3783 /* Clear the OCREF clear selection bit */
3784 tmpsmcr
&= ~TIM_SMCR_OCCS
;
3786 /* Clear the ETR Bits */
3787 tmpsmcr
&= ~(TIM_SMCR_ETF
| TIM_SMCR_ETPS
| TIM_SMCR_ECE
| TIM_SMCR_ETP
);
3790 htim
->Instance
->SMCR
= tmpsmcr
;
3794 case TIM_CLEARINPUTSOURCE_ETR
:
3796 TIM_ETR_SetConfig(htim
->Instance
,
3797 sClearInputConfig
->ClearInputPrescaler
,
3798 sClearInputConfig
->ClearInputPolarity
,
3799 sClearInputConfig
->ClearInputFilter
);
3801 /* Set the OCREF clear selection bit */
3802 htim
->Instance
->SMCR
|= TIM_SMCR_OCCS
;
3813 if(sClearInputConfig
->ClearInputState
!= RESET
)
3815 /* Enable the Ocref clear feature for Channel 1 */
3816 htim
->Instance
->CCMR1
|= TIM_CCMR1_OC1CE
;
3820 /* Disable the Ocref clear feature for Channel 1 */
3821 htim
->Instance
->CCMR1
&= ~TIM_CCMR1_OC1CE
;
3827 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
3828 if(sClearInputConfig
->ClearInputState
!= RESET
)
3830 /* Enable the Ocref clear feature for Channel 2 */
3831 htim
->Instance
->CCMR1
|= TIM_CCMR1_OC2CE
;
3835 /* Disable the Ocref clear feature for Channel 2 */
3836 htim
->Instance
->CCMR1
&= ~TIM_CCMR1_OC2CE
;
3842 assert_param(IS_TIM_CC3_INSTANCE(htim
->Instance
));
3843 if(sClearInputConfig
->ClearInputState
!= RESET
)
3845 /* Enable the Ocref clear feature for Channel 3 */
3846 htim
->Instance
->CCMR2
|= TIM_CCMR2_OC3CE
;
3850 /* Disable the Ocref clear feature for Channel 3 */
3851 htim
->Instance
->CCMR2
&= ~TIM_CCMR2_OC3CE
;
3857 assert_param(IS_TIM_CC4_INSTANCE(htim
->Instance
));
3858 if(sClearInputConfig
->ClearInputState
!= RESET
)
3860 /* Enable the Ocref clear feature for Channel 4 */
3861 htim
->Instance
->CCMR2
|= TIM_CCMR2_OC4CE
;
3865 /* Disable the Ocref clear feature for Channel 4 */
3866 htim
->Instance
->CCMR2
&= ~TIM_CCMR2_OC4CE
;
3874 htim
->State
= HAL_TIM_STATE_READY
;
3882 * @brief Configures the clock source to be used
3883 * @param htim : TIM handle
3884 * @param sClockSourceConfig : pointer to a TIM_ClockConfigTypeDef structure that
3885 * contains the clock source information for the TIM peripheral.
3886 * @retval HAL status
3888 HAL_StatusTypeDef
HAL_TIM_ConfigClockSource(TIM_HandleTypeDef
*htim
, TIM_ClockConfigTypeDef
* sClockSourceConfig
)
3890 uint32_t tmpsmcr
= 0;
3892 /* Process Locked */
3895 htim
->State
= HAL_TIM_STATE_BUSY
;
3897 /* Check the parameters */
3898 assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig
->ClockSource
));
3899 assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig
->ClockPolarity
));
3900 assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig
->ClockPrescaler
));
3901 assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig
->ClockFilter
));
3903 /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
3904 tmpsmcr
= htim
->Instance
->SMCR
;
3905 tmpsmcr
&= ~(TIM_SMCR_SMS
| TIM_SMCR_TS
);
3906 tmpsmcr
&= ~(TIM_SMCR_ETF
| TIM_SMCR_ETPS
| TIM_SMCR_ECE
| TIM_SMCR_ETP
);
3907 htim
->Instance
->SMCR
= tmpsmcr
;
3909 switch (sClockSourceConfig
->ClockSource
)
3911 case TIM_CLOCKSOURCE_INTERNAL
:
3913 assert_param(IS_TIM_INSTANCE(htim
->Instance
));
3914 /* Disable slave mode to clock the prescaler directly with the internal clock */
3915 htim
->Instance
->SMCR
&= ~TIM_SMCR_SMS
;
3919 case TIM_CLOCKSOURCE_ETRMODE1
:
3921 /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
3922 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim
->Instance
));
3924 /* Configure the ETR Clock source */
3925 TIM_ETR_SetConfig(htim
->Instance
,
3926 sClockSourceConfig
->ClockPrescaler
,
3927 sClockSourceConfig
->ClockPolarity
,
3928 sClockSourceConfig
->ClockFilter
);
3929 /* Get the TIMx SMCR register value */
3930 tmpsmcr
= htim
->Instance
->SMCR
;
3931 /* Reset the SMS and TS Bits */
3932 tmpsmcr
&= ~(TIM_SMCR_SMS
| TIM_SMCR_TS
);
3933 /* Select the External clock mode1 and the ETRF trigger */
3934 tmpsmcr
|= (TIM_SLAVEMODE_EXTERNAL1
| TIM_CLOCKSOURCE_ETRMODE1
);
3935 /* Write to TIMx SMCR */
3936 htim
->Instance
->SMCR
= tmpsmcr
;
3940 case TIM_CLOCKSOURCE_ETRMODE2
:
3942 /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
3943 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim
->Instance
));
3945 /* Configure the ETR Clock source */
3946 TIM_ETR_SetConfig(htim
->Instance
,
3947 sClockSourceConfig
->ClockPrescaler
,
3948 sClockSourceConfig
->ClockPolarity
,
3949 sClockSourceConfig
->ClockFilter
);
3950 /* Enable the External clock mode2 */
3951 htim
->Instance
->SMCR
|= TIM_SMCR_ECE
;
3955 case TIM_CLOCKSOURCE_TI1
:
3957 /* Check whether or not the timer instance supports external clock mode 1 */
3958 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim
->Instance
));
3960 TIM_TI1_ConfigInputStage(htim
->Instance
,
3961 sClockSourceConfig
->ClockPolarity
,
3962 sClockSourceConfig
->ClockFilter
);
3963 TIM_ITRx_SetConfig(htim
->Instance
, TIM_CLOCKSOURCE_TI1
);
3966 case TIM_CLOCKSOURCE_TI2
:
3968 /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
3969 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim
->Instance
));
3971 TIM_TI2_ConfigInputStage(htim
->Instance
,
3972 sClockSourceConfig
->ClockPolarity
,
3973 sClockSourceConfig
->ClockFilter
);
3974 TIM_ITRx_SetConfig(htim
->Instance
, TIM_CLOCKSOURCE_TI2
);
3977 case TIM_CLOCKSOURCE_TI1ED
:
3979 /* Check whether or not the timer instance supports external clock mode 1 */
3980 assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim
->Instance
));
3982 TIM_TI1_ConfigInputStage(htim
->Instance
,
3983 sClockSourceConfig
->ClockPolarity
,
3984 sClockSourceConfig
->ClockFilter
);
3985 TIM_ITRx_SetConfig(htim
->Instance
, TIM_CLOCKSOURCE_TI1ED
);
3988 case TIM_CLOCKSOURCE_ITR0
:
3990 /* Check whether or not the timer instance supports external clock mode 1 */
3991 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim
->Instance
));
3993 TIM_ITRx_SetConfig(htim
->Instance
, TIM_CLOCKSOURCE_ITR0
);
3996 case TIM_CLOCKSOURCE_ITR1
:
3998 /* Check whether or not the timer instance supports external clock mode 1 */
3999 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim
->Instance
));
4001 TIM_ITRx_SetConfig(htim
->Instance
, TIM_CLOCKSOURCE_ITR1
);
4004 case TIM_CLOCKSOURCE_ITR2
:
4006 /* Check whether or not the timer instance supports external clock mode 1 */
4007 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim
->Instance
));
4009 TIM_ITRx_SetConfig(htim
->Instance
, TIM_CLOCKSOURCE_ITR2
);
4012 case TIM_CLOCKSOURCE_ITR3
:
4014 /* Check whether or not the timer instance supports external clock mode 1 */
4015 assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim
->Instance
));
4017 TIM_ITRx_SetConfig(htim
->Instance
, TIM_CLOCKSOURCE_ITR3
);
4024 htim
->State
= HAL_TIM_STATE_READY
;
4032 * @brief Selects the signal connected to the TI1 input: direct from CH1_input
4033 * or a XOR combination between CH1_input, CH2_input & CH3_input
4034 * @param htim : TIM handle.
4035 * @param TI1_Selection : Indicate whether or not channel 1 is connected to the
4036 * output of a XOR gate.
4037 * This parameter can be one of the following values:
4038 * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
4039 * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
4040 * pins are connected to the TI1 input (XOR combination)
4041 * @retval HAL status
4043 HAL_StatusTypeDef
HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef
*htim
, uint32_t TI1_Selection
)
4045 uint32_t tmpcr2
= 0;
4047 /* Check the parameters */
4048 assert_param(IS_TIM_XOR_INSTANCE(htim
->Instance
));
4049 assert_param(IS_TIM_TI1SELECTION(TI1_Selection
));
4051 /* Get the TIMx CR2 register value */
4052 tmpcr2
= htim
->Instance
->CR2
;
4054 /* Reset the TI1 selection */
4055 tmpcr2
&= ~TIM_CR2_TI1S
;
4057 /* Set the the TI1 selection */
4058 tmpcr2
|= TI1_Selection
;
4060 /* Write to TIMxCR2 */
4061 htim
->Instance
->CR2
= tmpcr2
;
4067 * @brief Configures the TIM in Slave mode
4068 * @param htim : TIM handle.
4069 * @param sSlaveConfig : pointer to a TIM_SlaveConfigTypeDef structure that
4070 * contains the selected trigger (internal trigger input, filtered
4071 * timer input or external trigger input) and the ) and the Slave
4072 * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
4073 * @retval HAL status
4075 HAL_StatusTypeDef
HAL_TIM_SlaveConfigSynchronization(TIM_HandleTypeDef
*htim
, TIM_SlaveConfigTypeDef
* sSlaveConfig
)
4077 /* Check the parameters */
4078 assert_param(IS_TIM_SLAVE_INSTANCE(htim
->Instance
));
4079 assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig
->SlaveMode
));
4080 assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig
->InputTrigger
));
4084 htim
->State
= HAL_TIM_STATE_BUSY
;
4086 TIM_SlaveTimer_SetConfig(htim
, sSlaveConfig
);
4088 /* Disable Trigger Interrupt */
4089 __HAL_TIM_DISABLE_IT(htim
, TIM_IT_TRIGGER
);
4091 /* Disable Trigger DMA request */
4092 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_TRIGGER
);
4094 htim
->State
= HAL_TIM_STATE_READY
;
4102 * @brief Configures the TIM in Slave mode in interrupt mode
4103 * @param htim: TIM handle.
4104 * @param sSlaveConfig: pointer to a TIM_SlaveConfigTypeDef structure that
4105 * contains the selected trigger (internal trigger input, filtered
4106 * timer input or external trigger input) and the ) and the Slave
4107 * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
4108 * @retval HAL status
4110 HAL_StatusTypeDef
HAL_TIM_SlaveConfigSynchronization_IT(TIM_HandleTypeDef
*htim
,
4111 TIM_SlaveConfigTypeDef
* sSlaveConfig
)
4113 /* Check the parameters */
4114 assert_param(IS_TIM_SLAVE_INSTANCE(htim
->Instance
));
4115 assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig
->SlaveMode
));
4116 assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig
->InputTrigger
));
4120 htim
->State
= HAL_TIM_STATE_BUSY
;
4122 TIM_SlaveTimer_SetConfig(htim
, sSlaveConfig
);
4124 /* Enable Trigger Interrupt */
4125 __HAL_TIM_ENABLE_IT(htim
, TIM_IT_TRIGGER
);
4127 /* Disable Trigger DMA request */
4128 __HAL_TIM_DISABLE_DMA(htim
, TIM_DMA_TRIGGER
);
4130 htim
->State
= HAL_TIM_STATE_READY
;
4138 * @brief Read the captured value from Capture Compare unit
4139 * @param htim : TIM handle.
4140 * @param Channel : TIM Channels to be enabled
4141 * This parameter can be one of the following values:
4142 * @arg TIM_CHANNEL_1 : TIM Channel 1 selected
4143 * @arg TIM_CHANNEL_2 : TIM Channel 2 selected
4144 * @arg TIM_CHANNEL_3 : TIM Channel 3 selected
4145 * @arg TIM_CHANNEL_4 : TIM Channel 4 selected
4146 * @retval Captured value
4148 uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef
*htim
, uint32_t Channel
)
4150 uint32_t tmpreg
= 0;
4158 /* Check the parameters */
4159 assert_param(IS_TIM_CC1_INSTANCE(htim
->Instance
));
4161 /* Return the capture 1 value */
4162 tmpreg
= htim
->Instance
->CCR1
;
4168 /* Check the parameters */
4169 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
4171 /* Return the capture 2 value */
4172 tmpreg
= htim
->Instance
->CCR2
;
4179 /* Check the parameters */
4180 assert_param(IS_TIM_CC3_INSTANCE(htim
->Instance
));
4182 /* Return the capture 3 value */
4183 tmpreg
= htim
->Instance
->CCR3
;
4190 /* Check the parameters */
4191 assert_param(IS_TIM_CC4_INSTANCE(htim
->Instance
));
4193 /* Return the capture 4 value */
4194 tmpreg
= htim
->Instance
->CCR4
;
4211 /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
4212 * @brief TIM Callbacks functions
4215 ==============================================================================
4216 ##### TIM Callbacks functions #####
4217 ==============================================================================
4219 This section provides TIM callback functions:
4220 (+) Timer Period elapsed callback
4221 (+) Timer Output Compare callback
4222 (+) Timer Input capture callback
4223 (+) Timer Trigger callback
4224 (+) Timer Error callback
4231 * @brief Period elapsed callback in non blocking mode
4232 * @param htim : TIM handle
4235 __weak
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef
*htim
)
4237 /* NOTE : This function Should not be modified, when the callback is needed,
4238 the __HAL_TIM_PeriodElapsedCallback could be implemented in the user file
4243 * @brief Output Compare callback in non blocking mode
4244 * @param htim : TIM OC handle
4247 __weak
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef
*htim
)
4249 /* NOTE : This function Should not be modified, when the callback is needed,
4250 the __HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
4254 * @brief Input Capture callback in non blocking mode
4255 * @param htim : TIM IC handle
4258 __weak
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef
*htim
)
4260 /* NOTE : This function Should not be modified, when the callback is needed,
4261 the __HAL_TIM_IC_CaptureCallback could be implemented in the user file
4266 * @brief PWM Pulse finished callback in non blocking mode
4267 * @param htim : TIM handle
4270 __weak
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef
*htim
)
4272 /* NOTE : This function Should not be modified, when the callback is needed,
4273 the __HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
4278 * @brief Hall Trigger detection callback in non blocking mode
4279 * @param htim : TIM handle
4282 __weak
void HAL_TIM_TriggerCallback(TIM_HandleTypeDef
*htim
)
4284 /* NOTE : This function Should not be modified, when the callback is needed,
4285 the HAL_TIM_TriggerCallback could be implemented in the user file
4290 * @brief Timer error callback in non blocking mode
4291 * @param htim : TIM handle
4294 __weak
void HAL_TIM_ErrorCallback(TIM_HandleTypeDef
*htim
)
4296 /* NOTE : This function Should not be modified, when the callback is needed,
4297 the HAL_TIM_ErrorCallback could be implemented in the user file
4305 /** @defgroup TIM_Exported_Functions_Group10 Peripheral State functions
4306 * @brief Peripheral State functions
4309 ==============================================================================
4310 ##### Peripheral State functions #####
4311 ==============================================================================
4313 This subsection permit to get in run-time the status of the peripheral
4321 * @brief Return the TIM Base state
4322 * @param htim : TIM Base handle
4325 HAL_TIM_StateTypeDef
HAL_TIM_Base_GetState(TIM_HandleTypeDef
*htim
)
4331 * @brief Return the TIM OC state
4332 * @param htim : TIM Ouput Compare handle
4335 HAL_TIM_StateTypeDef
HAL_TIM_OC_GetState(TIM_HandleTypeDef
*htim
)
4341 * @brief Return the TIM PWM state
4342 * @param htim : TIM handle
4345 HAL_TIM_StateTypeDef
HAL_TIM_PWM_GetState(TIM_HandleTypeDef
*htim
)
4351 * @brief Return the TIM Input Capture state
4352 * @param htim : TIM IC handle
4355 HAL_TIM_StateTypeDef
HAL_TIM_IC_GetState(TIM_HandleTypeDef
*htim
)
4361 * @brief Return the TIM One Pulse Mode state
4362 * @param htim : TIM OPM handle
4365 HAL_TIM_StateTypeDef
HAL_TIM_OnePulse_GetState(TIM_HandleTypeDef
*htim
)
4371 * @brief Return the TIM Encoder Mode state
4372 * @param htim : TIM Encoder handle
4375 HAL_TIM_StateTypeDef
HAL_TIM_Encoder_GetState(TIM_HandleTypeDef
*htim
)
4388 /** @addtogroup TIM_Private_Functions TIM_Private_Functions
4393 * @brief TIM DMA error callback
4394 * @param hdma : pointer to DMA handle.
4397 void TIM_DMAError(DMA_HandleTypeDef
*hdma
)
4399 TIM_HandleTypeDef
* htim
= ( TIM_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
4401 htim
->State
= HAL_TIM_STATE_READY
;
4403 HAL_TIM_ErrorCallback(htim
);
4407 * @brief TIM DMA Delay Pulse complete callback.
4408 * @param hdma : pointer to DMA handle.
4411 void TIM_DMADelayPulseCplt(DMA_HandleTypeDef
*hdma
)
4413 TIM_HandleTypeDef
* htim
= ( TIM_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
4415 htim
->State
= HAL_TIM_STATE_READY
;
4417 if (hdma
== htim
->hdma
[TIM_DMA_ID_CC1
])
4419 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_1
;
4421 else if (hdma
== htim
->hdma
[TIM_DMA_ID_CC2
])
4423 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_2
;
4425 else if (hdma
== htim
->hdma
[TIM_DMA_ID_CC3
])
4427 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_3
;
4429 else if (hdma
== htim
->hdma
[TIM_DMA_ID_CC4
])
4431 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_4
;
4434 HAL_TIM_PWM_PulseFinishedCallback(htim
);
4436 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_CLEARED
;
4439 * @brief TIM DMA Capture complete callback.
4440 * @param hdma : pointer to DMA handle.
4443 void TIM_DMACaptureCplt(DMA_HandleTypeDef
*hdma
)
4445 TIM_HandleTypeDef
* htim
= ( TIM_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
4447 htim
->State
= HAL_TIM_STATE_READY
;
4449 if (hdma
== htim
->hdma
[TIM_DMA_ID_CC1
])
4451 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_1
;
4453 else if (hdma
== htim
->hdma
[TIM_DMA_ID_CC2
])
4455 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_2
;
4457 else if (hdma
== htim
->hdma
[TIM_DMA_ID_CC3
])
4459 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_3
;
4461 else if (hdma
== htim
->hdma
[TIM_DMA_ID_CC4
])
4463 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_4
;
4466 HAL_TIM_IC_CaptureCallback(htim
);
4468 htim
->Channel
= HAL_TIM_ACTIVE_CHANNEL_CLEARED
;
4472 * @brief TIM DMA Period Elapse complete callback.
4473 * @param hdma : pointer to DMA handle.
4476 static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef
*hdma
)
4478 TIM_HandleTypeDef
* htim
= ( TIM_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
4480 htim
->State
= HAL_TIM_STATE_READY
;
4482 HAL_TIM_PeriodElapsedCallback(htim
);
4486 * @brief TIM DMA Trigger callback.
4487 * @param hdma : pointer to DMA handle.
4490 static void TIM_DMATriggerCplt(DMA_HandleTypeDef
*hdma
)
4492 TIM_HandleTypeDef
* htim
= ( TIM_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
4494 htim
->State
= HAL_TIM_STATE_READY
;
4496 HAL_TIM_TriggerCallback(htim
);
4500 * @brief Time Base configuration
4501 * @param TIMx : TIM periheral
4502 * @param Structure : TIM Base configuration structure
4505 void TIM_Base_SetConfig(TIM_TypeDef
*TIMx
, TIM_Base_InitTypeDef
*Structure
)
4507 uint32_t tmpcr1
= 0;
4510 /* Set TIM Time Base Unit parameters ---------------------------------------*/
4511 if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx
))
4513 /* Select the Counter Mode */
4514 tmpcr1
&= ~(TIM_CR1_DIR
| TIM_CR1_CMS
);
4515 tmpcr1
|= Structure
->CounterMode
;
4518 if(IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx
))
4520 /* Set the clock division */
4521 tmpcr1
&= ~TIM_CR1_CKD
;
4522 tmpcr1
|= (uint32_t)Structure
->ClockDivision
;
4527 /* Set the Autoreload value */
4528 TIMx
->ARR
= (uint32_t)Structure
->Period
;
4530 /* Set the Prescaler value */
4531 TIMx
->PSC
= (uint32_t)Structure
->Prescaler
;
4533 if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx
))
4535 /* Set the Repetition Counter value */
4536 TIMx
->RCR
= Structure
->RepetitionCounter
;
4539 /* Generate an update event to reload the Prescaler
4540 and the repetition counter(only for TIM1 and TIM8) value immediatly */
4541 TIMx
->EGR
= TIM_EGR_UG
;
4545 * @brief Time Ouput Compare 1 configuration
4546 * @param TIMx to select the TIM peripheral
4547 * @param OC_Config : The ouput configuration structure
4550 static void TIM_OC1_SetConfig(TIM_TypeDef
*TIMx
, TIM_OC_InitTypeDef
*OC_Config
)
4552 uint32_t tmpccmrx
= 0;
4553 uint32_t tmpccer
= 0;
4554 uint32_t tmpcr2
= 0;
4556 /* Disable the Channel 1: Reset the CC1E Bit */
4557 TIMx
->CCER
&= ~TIM_CCER_CC1E
;
4559 /* Get the TIMx CCER register value */
4560 tmpccer
= TIMx
->CCER
;
4561 /* Get the TIMx CR2 register value */
4564 /* Get the TIMx CCMR1 register value */
4565 tmpccmrx
= TIMx
->CCMR1
;
4567 /* Reset the Output Compare Mode Bits */
4568 tmpccmrx
&= ~TIM_CCMR1_OC1M
;
4569 tmpccmrx
&= ~TIM_CCMR1_CC1S
;
4570 /* Select the Output Compare Mode */
4571 tmpccmrx
|= OC_Config
->OCMode
;
4573 /* Reset the Output Polarity level */
4574 tmpccer
&= ~TIM_CCER_CC1P
;
4575 /* Set the Output Compare Polarity */
4576 tmpccer
|= OC_Config
->OCPolarity
;
4578 if(IS_TIM_CCXN_INSTANCE(TIMx
, TIM_CHANNEL_1
))
4580 /* Check parameters */
4581 assert_param(IS_TIM_OCN_POLARITY(OC_Config
->OCNPolarity
));
4583 /* Reset the Output N Polarity level */
4584 tmpccer
&= ~TIM_CCER_CC1NP
;
4585 /* Set the Output N Polarity */
4586 tmpccer
|= OC_Config
->OCNPolarity
;
4587 /* Reset the Output N State */
4588 tmpccer
&= ~TIM_CCER_CC1NE
;
4591 if(IS_TIM_BREAK_INSTANCE(TIMx
))
4593 /* Check parameters */
4594 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config
->OCNIdleState
));
4595 assert_param(IS_TIM_OCIDLE_STATE(OC_Config
->OCIdleState
));
4597 /* Reset the Output Compare and Output Compare N IDLE State */
4598 tmpcr2
&= ~TIM_CR2_OIS1
;
4599 tmpcr2
&= ~TIM_CR2_OIS1N
;
4600 /* Set the Output Idle state */
4601 tmpcr2
|= OC_Config
->OCIdleState
;
4602 /* Set the Output N Idle state */
4603 tmpcr2
|= OC_Config
->OCNIdleState
;
4605 /* Write to TIMx CR2 */
4608 /* Write to TIMx CCMR1 */
4609 TIMx
->CCMR1
= tmpccmrx
;
4611 /* Set the Capture Compare Register value */
4612 TIMx
->CCR1
= OC_Config
->Pulse
;
4614 /* Write to TIMx CCER */
4615 TIMx
->CCER
= tmpccer
;
4619 * @brief Time Ouput Compare 2 configuration
4620 * @param TIMx to select the TIM peripheral
4621 * @param OC_Config : The ouput configuration structure
4624 void TIM_OC2_SetConfig(TIM_TypeDef
*TIMx
, TIM_OC_InitTypeDef
*OC_Config
)
4626 uint32_t tmpccmrx
= 0;
4627 uint32_t tmpccer
= 0;
4628 uint32_t tmpcr2
= 0;
4630 /* Disable the Channel 2: Reset the CC2E Bit */
4631 TIMx
->CCER
&= ~TIM_CCER_CC2E
;
4633 /* Get the TIMx CCER register value */
4634 tmpccer
= TIMx
->CCER
;
4635 /* Get the TIMx CR2 register value */
4638 /* Get the TIMx CCMR1 register value */
4639 tmpccmrx
= TIMx
->CCMR1
;
4641 /* Reset the Output Compare mode and Capture/Compare selection Bits */
4642 tmpccmrx
&= ~TIM_CCMR1_OC2M
;
4643 tmpccmrx
&= ~TIM_CCMR1_CC2S
;
4645 /* Select the Output Compare Mode */
4646 tmpccmrx
|= (OC_Config
->OCMode
<< 8);
4648 /* Reset the Output Polarity level */
4649 tmpccer
&= ~TIM_CCER_CC2P
;
4650 /* Set the Output Compare Polarity */
4651 tmpccer
|= (OC_Config
->OCPolarity
<< 4);
4653 if(IS_TIM_CCXN_INSTANCE(TIMx
, TIM_CHANNEL_2
))
4655 assert_param(IS_TIM_OCN_POLARITY(OC_Config
->OCNPolarity
));
4656 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config
->OCNIdleState
));
4657 assert_param(IS_TIM_OCIDLE_STATE(OC_Config
->OCIdleState
));
4659 /* Reset the Output N Polarity level */
4660 tmpccer
&= ~TIM_CCER_CC2NP
;
4661 /* Set the Output N Polarity */
4662 tmpccer
|= (OC_Config
->OCNPolarity
<< 4);
4663 /* Reset the Output N State */
4664 tmpccer
&= ~TIM_CCER_CC2NE
;
4668 if(IS_TIM_BREAK_INSTANCE(TIMx
))
4670 /* Check parameters */
4671 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config
->OCNIdleState
));
4672 assert_param(IS_TIM_OCIDLE_STATE(OC_Config
->OCIdleState
));
4674 /* Reset the Output Compare and Output Compare N IDLE State */
4675 tmpcr2
&= ~TIM_CR2_OIS2
;
4676 tmpcr2
&= ~TIM_CR2_OIS2N
;
4677 /* Set the Output Idle state */
4678 tmpcr2
|= (OC_Config
->OCIdleState
<< 2);
4679 /* Set the Output N Idle state */
4680 tmpcr2
|= (OC_Config
->OCNIdleState
<< 2);
4683 /* Write to TIMx CR2 */
4686 /* Write to TIMx CCMR1 */
4687 TIMx
->CCMR1
= tmpccmrx
;
4689 /* Set the Capture Compare Register value */
4690 TIMx
->CCR2
= OC_Config
->Pulse
;
4692 /* Write to TIMx CCER */
4693 TIMx
->CCER
= tmpccer
;
4697 * @brief Time Ouput Compare 3 configuration
4698 * @param TIMx to select the TIM peripheral
4699 * @param OC_Config : The ouput configuration structure
4702 static void TIM_OC3_SetConfig(TIM_TypeDef
*TIMx
, TIM_OC_InitTypeDef
*OC_Config
)
4704 uint32_t tmpccmrx
= 0;
4705 uint32_t tmpccer
= 0;
4706 uint32_t tmpcr2
= 0;
4708 /* Disable the Channel 3: Reset the CC2E Bit */
4709 TIMx
->CCER
&= ~TIM_CCER_CC3E
;
4711 /* Get the TIMx CCER register value */
4712 tmpccer
= TIMx
->CCER
;
4713 /* Get the TIMx CR2 register value */
4716 /* Get the TIMx CCMR2 register value */
4717 tmpccmrx
= TIMx
->CCMR2
;
4719 /* Reset the Output Compare mode and Capture/Compare selection Bits */
4720 tmpccmrx
&= ~TIM_CCMR2_OC3M
;
4721 tmpccmrx
&= ~TIM_CCMR2_CC3S
;
4722 /* Select the Output Compare Mode */
4723 tmpccmrx
|= OC_Config
->OCMode
;
4725 /* Reset the Output Polarity level */
4726 tmpccer
&= ~TIM_CCER_CC3P
;
4727 /* Set the Output Compare Polarity */
4728 tmpccer
|= (OC_Config
->OCPolarity
<< 8);
4730 if(IS_TIM_CCXN_INSTANCE(TIMx
, TIM_CHANNEL_3
))
4732 assert_param(IS_TIM_OCN_POLARITY(OC_Config
->OCNPolarity
));
4733 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config
->OCNIdleState
));
4734 assert_param(IS_TIM_OCIDLE_STATE(OC_Config
->OCIdleState
));
4736 /* Reset the Output N Polarity level */
4737 tmpccer
&= ~TIM_CCER_CC3NP
;
4738 /* Set the Output N Polarity */
4739 tmpccer
|= (OC_Config
->OCNPolarity
<< 8);
4740 /* Reset the Output N State */
4741 tmpccer
&= ~TIM_CCER_CC3NE
;
4744 if(IS_TIM_BREAK_INSTANCE(TIMx
))
4746 /* Check parameters */
4747 assert_param(IS_TIM_OCNIDLE_STATE(OC_Config
->OCNIdleState
));
4748 assert_param(IS_TIM_OCIDLE_STATE(OC_Config
->OCIdleState
));
4750 /* Reset the Output Compare and Output Compare N IDLE State */
4751 tmpcr2
&= ~TIM_CR2_OIS3
;
4752 tmpcr2
&= ~TIM_CR2_OIS3N
;
4753 /* Set the Output Idle state */
4754 tmpcr2
|= (OC_Config
->OCIdleState
<< 4);
4755 /* Set the Output N Idle state */
4756 tmpcr2
|= (OC_Config
->OCNIdleState
<< 4);
4759 /* Write to TIMx CR2 */
4762 /* Write to TIMx CCMR2 */
4763 TIMx
->CCMR2
= tmpccmrx
;
4765 /* Set the Capture Compare Register value */
4766 TIMx
->CCR3
= OC_Config
->Pulse
;
4768 /* Write to TIMx CCER */
4769 TIMx
->CCER
= tmpccer
;
4773 * @brief Time Ouput Compare 4 configuration
4774 * @param TIMx to select the TIM peripheral
4775 * @param OC_Config : The ouput configuration structure
4778 static void TIM_OC4_SetConfig(TIM_TypeDef
*TIMx
, TIM_OC_InitTypeDef
*OC_Config
)
4780 uint32_t tmpccmrx
= 0;
4781 uint32_t tmpccer
= 0;
4782 uint32_t tmpcr2
= 0;
4784 /* Disable the Channel 4: Reset the CC4E Bit */
4785 TIMx
->CCER
&= ~TIM_CCER_CC4E
;
4787 /* Get the TIMx CCER register value */
4788 tmpccer
= TIMx
->CCER
;
4789 /* Get the TIMx CR2 register value */
4792 /* Get the TIMx CCMR2 register value */
4793 tmpccmrx
= TIMx
->CCMR2
;
4795 /* Reset the Output Compare mode and Capture/Compare selection Bits */
4796 tmpccmrx
&= ~TIM_CCMR2_OC4M
;
4797 tmpccmrx
&= ~TIM_CCMR2_CC4S
;
4799 /* Select the Output Compare Mode */
4800 tmpccmrx
|= (OC_Config
->OCMode
<< 8);
4802 /* Reset the Output Polarity level */
4803 tmpccer
&= ~TIM_CCER_CC4P
;
4804 /* Set the Output Compare Polarity */
4805 tmpccer
|= (OC_Config
->OCPolarity
<< 12);
4807 if(IS_TIM_BREAK_INSTANCE(TIMx
))
4809 assert_param(IS_TIM_OCIDLE_STATE(OC_Config
->OCIdleState
));
4811 /* Reset the Output Compare IDLE State */
4812 tmpcr2
&= ~TIM_CR2_OIS4
;
4813 /* Set the Output Idle state */
4814 tmpcr2
|= (OC_Config
->OCIdleState
<< 6);
4817 /* Write to TIMx CR2 */
4820 /* Write to TIMx CCMR2 */
4821 TIMx
->CCMR2
= tmpccmrx
;
4823 /* Set the Capture Compare Register value */
4824 TIMx
->CCR4
= OC_Config
->Pulse
;
4826 /* Write to TIMx CCER */
4827 TIMx
->CCER
= tmpccer
;
4830 static void TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef
*htim
,
4831 TIM_SlaveConfigTypeDef
* sSlaveConfig
)
4833 uint32_t tmpsmcr
= 0;
4834 uint32_t tmpccmr1
= 0;
4835 uint32_t tmpccer
= 0;
4837 /* Get the TIMx SMCR register value */
4838 tmpsmcr
= htim
->Instance
->SMCR
;
4840 /* Reset the Trigger Selection Bits */
4841 tmpsmcr
&= ~TIM_SMCR_TS
;
4842 /* Set the Input Trigger source */
4843 tmpsmcr
|= sSlaveConfig
->InputTrigger
;
4845 /* Reset the slave mode Bits */
4846 tmpsmcr
&= ~TIM_SMCR_SMS
;
4847 /* Set the slave mode */
4848 tmpsmcr
|= sSlaveConfig
->SlaveMode
;
4850 /* Write to TIMx SMCR */
4851 htim
->Instance
->SMCR
= tmpsmcr
;
4853 /* Configure the trigger prescaler, filter, and polarity */
4854 switch (sSlaveConfig
->InputTrigger
)
4858 /* Check the parameters */
4859 assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim
->Instance
));
4860 assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig
->TriggerPrescaler
));
4861 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig
->TriggerPolarity
));
4862 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig
->TriggerFilter
));
4863 /* Configure the ETR Trigger source */
4864 TIM_ETR_SetConfig(htim
->Instance
,
4865 sSlaveConfig
->TriggerPrescaler
,
4866 sSlaveConfig
->TriggerPolarity
,
4867 sSlaveConfig
->TriggerFilter
);
4871 case TIM_TS_TI1F_ED
:
4873 /* Check the parameters */
4874 assert_param(IS_TIM_CC1_INSTANCE(htim
->Instance
));
4875 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig
->TriggerPolarity
));
4876 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig
->TriggerFilter
));
4878 /* Disable the Channel 1: Reset the CC1E Bit */
4879 tmpccer
= htim
->Instance
->CCER
;
4880 htim
->Instance
->CCER
&= ~TIM_CCER_CC1E
;
4881 tmpccmr1
= htim
->Instance
->CCMR1
;
4883 /* Set the filter */
4884 tmpccmr1
&= ~TIM_CCMR1_IC1F
;
4885 tmpccmr1
|= ((sSlaveConfig
->TriggerFilter
) << 4);
4887 /* Write to TIMx CCMR1 and CCER registers */
4888 htim
->Instance
->CCMR1
= tmpccmr1
;
4889 htim
->Instance
->CCER
= tmpccer
;
4896 /* Check the parameters */
4897 assert_param(IS_TIM_CC1_INSTANCE(htim
->Instance
));
4898 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig
->TriggerPolarity
));
4899 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig
->TriggerFilter
));
4901 /* Configure TI1 Filter and Polarity */
4902 TIM_TI1_ConfigInputStage(htim
->Instance
,
4903 sSlaveConfig
->TriggerPolarity
,
4904 sSlaveConfig
->TriggerFilter
);
4910 /* Check the parameters */
4911 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
4912 assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig
->TriggerPolarity
));
4913 assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig
->TriggerFilter
));
4915 /* Configure TI2 Filter and Polarity */
4916 TIM_TI2_ConfigInputStage(htim
->Instance
,
4917 sSlaveConfig
->TriggerPolarity
,
4918 sSlaveConfig
->TriggerFilter
);
4924 /* Check the parameter */
4925 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
4931 /* Check the parameter */
4932 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
4938 /* Check the parameter */
4939 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
4945 /* Check the parameter */
4946 assert_param(IS_TIM_CC2_INSTANCE(htim
->Instance
));
4956 * @brief Configure the TI1 as Input.
4957 * @param TIMx to select the TIM peripheral.
4958 * @param TIM_ICPolarity : The Input Polarity.
4959 * This parameter can be one of the following values:
4960 * @arg TIM_ICPolarity_Rising
4961 * @arg TIM_ICPolarity_Falling
4962 * @arg TIM_ICPolarity_BothEdge
4963 * @param TIM_ICSelection : specifies the input to be used.
4964 * This parameter can be one of the following values:
4965 * @arg TIM_ICSelection_DirectTI : TIM Input 1 is selected to be connected to IC1.
4966 * @arg TIM_ICSelection_IndirectTI : TIM Input 1 is selected to be connected to IC2.
4967 * @arg TIM_ICSelection_TRC : TIM Input 1 is selected to be connected to TRC.
4968 * @param TIM_ICFilter : Specifies the Input Capture Filter.
4969 * This parameter must be a value between 0x00 and 0x0F.
4971 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
4972 * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
4973 * protected against un-initialized filter and polarity values.
4975 void TIM_TI1_SetConfig(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICSelection
,
4976 uint32_t TIM_ICFilter
)
4978 uint32_t tmpccmr1
= 0;
4979 uint32_t tmpccer
= 0;
4981 /* Disable the Channel 1: Reset the CC1E Bit */
4982 TIMx
->CCER
&= ~TIM_CCER_CC1E
;
4983 tmpccmr1
= TIMx
->CCMR1
;
4984 tmpccer
= TIMx
->CCER
;
4986 /* Select the Input */
4987 if(IS_TIM_CC2_INSTANCE(TIMx
) != RESET
)
4989 tmpccmr1
&= ~TIM_CCMR1_CC1S
;
4990 tmpccmr1
|= TIM_ICSelection
;
4994 tmpccmr1
|= TIM_CCMR1_CC1S_0
;
4997 /* Set the filter */
4998 tmpccmr1
&= ~TIM_CCMR1_IC1F
;
4999 tmpccmr1
|= ((TIM_ICFilter
<< 4) & TIM_CCMR1_IC1F
);
5001 /* Select the Polarity and set the CC1E Bit */
5002 tmpccer
&= ~(TIM_CCER_CC1P
| TIM_CCER_CC1NP
);
5003 tmpccer
|= (TIM_ICPolarity
& (TIM_CCER_CC1P
| TIM_CCER_CC1NP
));
5005 /* Write to TIMx CCMR1 and CCER registers */
5006 TIMx
->CCMR1
= tmpccmr1
;
5007 TIMx
->CCER
= tmpccer
;
5011 * @brief Configure the Polarity and Filter for TI1.
5012 * @param TIMx to select the TIM peripheral.
5013 * @param TIM_ICPolarity : The Input Polarity.
5014 * This parameter can be one of the following values:
5015 * @arg TIM_ICPolarity_Rising
5016 * @arg TIM_ICPolarity_Falling
5017 * @arg TIM_ICPolarity_BothEdge
5018 * @param TIM_ICFilter : Specifies the Input Capture Filter.
5019 * This parameter must be a value between 0x00 and 0x0F.
5022 static void TIM_TI1_ConfigInputStage(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICFilter
)
5024 uint32_t tmpccmr1
= 0;
5025 uint32_t tmpccer
= 0;
5027 /* Disable the Channel 1: Reset the CC1E Bit */
5028 tmpccer
= TIMx
->CCER
;
5029 TIMx
->CCER
&= ~TIM_CCER_CC1E
;
5030 tmpccmr1
= TIMx
->CCMR1
;
5032 /* Set the filter */
5033 tmpccmr1
&= ~TIM_CCMR1_IC1F
;
5034 tmpccmr1
|= (TIM_ICFilter
<< 4);
5036 /* Select the Polarity and set the CC1E Bit */
5037 tmpccer
&= ~(TIM_CCER_CC1P
| TIM_CCER_CC1NP
);
5038 tmpccer
|= TIM_ICPolarity
;
5040 /* Write to TIMx CCMR1 and CCER registers */
5041 TIMx
->CCMR1
= tmpccmr1
;
5042 TIMx
->CCER
= tmpccer
;
5046 * @brief Configure the TI2 as Input.
5047 * @param TIMx to select the TIM peripheral
5048 * @param TIM_ICPolarity : The Input Polarity.
5049 * This parameter can be one of the following values:
5050 * @arg TIM_ICPolarity_Rising
5051 * @arg TIM_ICPolarity_Falling
5052 * @arg TIM_ICPolarity_BothEdge
5053 * @param TIM_ICSelection : specifies the input to be used.
5054 * This parameter can be one of the following values:
5055 * @arg TIM_ICSelection_DirectTI : TIM Input 2 is selected to be connected to IC2.
5056 * @arg TIM_ICSelection_IndirectTI : TIM Input 2 is selected to be connected to IC1.
5057 * @arg TIM_ICSelection_TRC : TIM Input 2 is selected to be connected to TRC.
5058 * @param TIM_ICFilter : Specifies the Input Capture Filter.
5059 * This parameter must be a value between 0x00 and 0x0F.
5061 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
5062 * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
5063 * protected against un-initialized filter and polarity values.
5065 static void TIM_TI2_SetConfig(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICSelection
,
5066 uint32_t TIM_ICFilter
)
5068 uint32_t tmpccmr1
= 0;
5069 uint32_t tmpccer
= 0;
5071 /* Disable the Channel 2: Reset the CC2E Bit */
5072 TIMx
->CCER
&= ~TIM_CCER_CC2E
;
5073 tmpccmr1
= TIMx
->CCMR1
;
5074 tmpccer
= TIMx
->CCER
;
5076 /* Select the Input */
5077 tmpccmr1
&= ~TIM_CCMR1_CC2S
;
5078 tmpccmr1
|= (TIM_ICSelection
<< 8);
5080 /* Set the filter */
5081 tmpccmr1
&= ~TIM_CCMR1_IC2F
;
5082 tmpccmr1
|= ((TIM_ICFilter
<< 12) & TIM_CCMR1_IC2F
);
5084 /* Select the Polarity and set the CC2E Bit */
5085 tmpccer
&= ~(TIM_CCER_CC2P
| TIM_CCER_CC2NP
);
5086 tmpccer
|= ((TIM_ICPolarity
<< 4) & (TIM_CCER_CC2P
| TIM_CCER_CC2NP
));
5088 /* Write to TIMx CCMR1 and CCER registers */
5089 TIMx
->CCMR1
= tmpccmr1
;
5090 TIMx
->CCER
= tmpccer
;
5094 * @brief Configure the Polarity and Filter for TI2.
5095 * @param TIMx to select the TIM peripheral.
5096 * @param TIM_ICPolarity : The Input Polarity.
5097 * This parameter can be one of the following values:
5098 * @arg TIM_ICPolarity_Rising
5099 * @arg TIM_ICPolarity_Falling
5100 * @arg TIM_ICPolarity_BothEdge
5101 * @param TIM_ICFilter : Specifies the Input Capture Filter.
5102 * This parameter must be a value between 0x00 and 0x0F.
5105 static void TIM_TI2_ConfigInputStage(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICFilter
)
5107 uint32_t tmpccmr1
= 0;
5108 uint32_t tmpccer
= 0;
5110 /* Disable the Channel 2: Reset the CC2E Bit */
5111 TIMx
->CCER
&= ~TIM_CCER_CC2E
;
5112 tmpccmr1
= TIMx
->CCMR1
;
5113 tmpccer
= TIMx
->CCER
;
5115 /* Set the filter */
5116 tmpccmr1
&= ~TIM_CCMR1_IC2F
;
5117 tmpccmr1
|= (TIM_ICFilter
<< 12);
5119 /* Select the Polarity and set the CC2E Bit */
5120 tmpccer
&= ~(TIM_CCER_CC2P
| TIM_CCER_CC2NP
);
5121 tmpccer
|= (TIM_ICPolarity
<< 4);
5123 /* Write to TIMx CCMR1 and CCER registers */
5124 TIMx
->CCMR1
= tmpccmr1
;
5125 TIMx
->CCER
= tmpccer
;
5129 * @brief Configure the TI3 as Input.
5130 * @param TIMx to select the TIM peripheral
5131 * @param TIM_ICPolarity : The Input Polarity.
5132 * This parameter can be one of the following values:
5133 * @arg TIM_ICPolarity_Rising
5134 * @arg TIM_ICPolarity_Falling
5135 * @arg TIM_ICPolarity_BothEdge
5136 * @param TIM_ICSelection : specifies the input to be used.
5137 * This parameter can be one of the following values:
5138 * @arg TIM_ICSelection_DirectTI : TIM Input 3 is selected to be connected to IC3.
5139 * @arg TIM_ICSelection_IndirectTI : TIM Input 3 is selected to be connected to IC4.
5140 * @arg TIM_ICSelection_TRC : TIM Input 3 is selected to be connected to TRC.
5141 * @param TIM_ICFilter : Specifies the Input Capture Filter.
5142 * This parameter must be a value between 0x00 and 0x0F.
5144 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
5145 * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
5146 * protected against un-initialized filter and polarity values.
5148 static void TIM_TI3_SetConfig(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICSelection
,
5149 uint32_t TIM_ICFilter
)
5151 uint32_t tmpccmr2
= 0;
5152 uint32_t tmpccer
= 0;
5154 /* Disable the Channel 3: Reset the CC3E Bit */
5155 TIMx
->CCER
&= ~TIM_CCER_CC3E
;
5156 tmpccmr2
= TIMx
->CCMR2
;
5157 tmpccer
= TIMx
->CCER
;
5159 /* Select the Input */
5160 tmpccmr2
&= ~TIM_CCMR2_CC3S
;
5161 tmpccmr2
|= TIM_ICSelection
;
5163 /* Set the filter */
5164 tmpccmr2
&= ~TIM_CCMR2_IC3F
;
5165 tmpccmr2
|= ((TIM_ICFilter
<< 4) & TIM_CCMR2_IC3F
);
5167 /* Select the Polarity and set the CC3E Bit */
5168 tmpccer
&= ~(TIM_CCER_CC3P
| TIM_CCER_CC3NP
);
5169 tmpccer
|= ((TIM_ICPolarity
<< 8) & (TIM_CCER_CC3P
| TIM_CCER_CC3NP
));
5171 /* Write to TIMx CCMR2 and CCER registers */
5172 TIMx
->CCMR2
= tmpccmr2
;
5173 TIMx
->CCER
= tmpccer
;
5177 * @brief Configure the TI4 as Input.
5178 * @param TIMx to select the TIM peripheral
5179 * @param TIM_ICPolarity : The Input Polarity.
5180 * This parameter can be one of the following values:
5181 * @arg TIM_ICPolarity_Rising
5182 * @arg TIM_ICPolarity_Falling
5183 * @arg TIM_ICPolarity_BothEdge
5184 * @param TIM_ICSelection : specifies the input to be used.
5185 * This parameter can be one of the following values:
5186 * @arg TIM_ICSelection_DirectTI : TIM Input 4 is selected to be connected to IC4.
5187 * @arg TIM_ICSelection_IndirectTI : TIM Input 4 is selected to be connected to IC3.
5188 * @arg TIM_ICSelection_TRC : TIM Input 4 is selected to be connected to TRC.
5189 * @param TIM_ICFilter : Specifies the Input Capture Filter.
5190 * This parameter must be a value between 0x00 and 0x0F.
5191 * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
5192 * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
5193 * protected against un-initialized filter and polarity values.
5196 static void TIM_TI4_SetConfig(TIM_TypeDef
*TIMx
, uint32_t TIM_ICPolarity
, uint32_t TIM_ICSelection
,
5197 uint32_t TIM_ICFilter
)
5199 uint32_t tmpccmr2
= 0;
5200 uint32_t tmpccer
= 0;
5202 /* Disable the Channel 4: Reset the CC4E Bit */
5203 TIMx
->CCER
&= ~TIM_CCER_CC4E
;
5204 tmpccmr2
= TIMx
->CCMR2
;
5205 tmpccer
= TIMx
->CCER
;
5207 /* Select the Input */
5208 tmpccmr2
&= ~TIM_CCMR2_CC4S
;
5209 tmpccmr2
|= (TIM_ICSelection
<< 8);
5211 /* Set the filter */
5212 tmpccmr2
&= ~TIM_CCMR2_IC4F
;
5213 tmpccmr2
|= ((TIM_ICFilter
<< 12) & TIM_CCMR2_IC4F
);
5215 /* Select the Polarity and set the CC4E Bit */
5216 tmpccer
&= ~(TIM_CCER_CC4P
| TIM_CCER_CC4NP
);
5217 tmpccer
|= ((TIM_ICPolarity
<< 12) & (TIM_CCER_CC4P
| TIM_CCER_CC4NP
));
5219 /* Write to TIMx CCMR2 and CCER registers */
5220 TIMx
->CCMR2
= tmpccmr2
;
5221 TIMx
->CCER
= tmpccer
;
5225 * @brief Selects the Input Trigger source
5226 * @param TIMx to select the TIM peripheral
5227 * @param InputTriggerSource : The Input Trigger source.
5228 * This parameter can be one of the following values:
5229 * @arg TIM_TS_ITR0 : Internal Trigger 0
5230 * @arg TIM_TS_ITR1 : Internal Trigger 1
5231 * @arg TIM_TS_ITR2 : Internal Trigger 2
5232 * @arg TIM_TS_ITR3 : Internal Trigger 3
5233 * @arg TIM_TS_TI1F_ED : TI1 Edge Detector
5234 * @arg TIM_TS_TI1FP1 : Filtered Timer Input 1
5235 * @arg TIM_TS_TI2FP2 : Filtered Timer Input 2
5236 * @arg TIM_TS_ETRF : External Trigger input
5239 static void TIM_ITRx_SetConfig(TIM_TypeDef
*TIMx
, uint16_t InputTriggerSource
)
5241 uint32_t tmpsmcr
= 0;
5243 /* Get the TIMx SMCR register value */
5244 tmpsmcr
= TIMx
->SMCR
;
5245 /* Reset the TS Bits */
5246 tmpsmcr
&= ~TIM_SMCR_TS
;
5247 /* Set the Input Trigger source and the slave mode*/
5248 tmpsmcr
|= InputTriggerSource
| TIM_SLAVEMODE_EXTERNAL1
;
5249 /* Write to TIMx SMCR */
5250 TIMx
->SMCR
= tmpsmcr
;
5253 * @brief Configures the TIMx External Trigger (ETR).
5254 * @param TIMx to select the TIM peripheral
5255 * @param TIM_ExtTRGPrescaler : The external Trigger Prescaler.
5256 * This parameter can be one of the following values:
5257 * @arg TIM_ExtTRGPSC_DIV1 : ETRP Prescaler OFF.
5258 * @arg TIM_ExtTRGPSC_DIV2 : ETRP frequency divided by 2.
5259 * @arg TIM_ExtTRGPSC_DIV4 : ETRP frequency divided by 4.
5260 * @arg TIM_ExtTRGPSC_DIV8 : ETRP frequency divided by 8.
5261 * @param TIM_ExtTRGPolarity : The external Trigger Polarity.
5262 * This parameter can be one of the following values:
5263 * @arg TIM_ExtTRGPolarity_Inverted : active low or falling edge active.
5264 * @arg TIM_ExtTRGPolarity_NonInverted : active high or rising edge active.
5265 * @param ExtTRGFilter : External Trigger Filter.
5266 * This parameter must be a value between 0x00 and 0x0F
5269 static void TIM_ETR_SetConfig(TIM_TypeDef
* TIMx
, uint32_t TIM_ExtTRGPrescaler
,
5270 uint32_t TIM_ExtTRGPolarity
, uint32_t ExtTRGFilter
)
5272 uint32_t tmpsmcr
= 0;
5274 tmpsmcr
= TIMx
->SMCR
;
5276 /* Reset the ETR Bits */
5277 tmpsmcr
&= ~(TIM_SMCR_ETF
| TIM_SMCR_ETPS
| TIM_SMCR_ECE
| TIM_SMCR_ETP
);
5279 /* Set the Prescaler, the Filter value and the Polarity */
5280 tmpsmcr
|= (uint32_t)(TIM_ExtTRGPrescaler
| (TIM_ExtTRGPolarity
| (ExtTRGFilter
<< 8)));
5282 /* Write to TIMx SMCR */
5283 TIMx
->SMCR
= tmpsmcr
;
5287 * @brief Enables or disables the TIM Capture Compare Channel x.
5288 * @param TIMx to select the TIM peripheral
5289 * @param Channel : specifies the TIM Channel
5290 * This parameter can be one of the following values:
5291 * @arg TIM_Channel_1 : TIM Channel 1
5292 * @arg TIM_Channel_2 : TIM Channel 2
5293 * @arg TIM_Channel_3 : TIM Channel 3
5294 * @arg TIM_Channel_4 : TIM Channel 4
5295 * @param ChannelState : specifies the TIM Channel CCxE bit new state.
5296 * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_Disable.
5299 void TIM_CCxChannelCmd(TIM_TypeDef
* TIMx
, uint32_t Channel
, uint32_t ChannelState
)
5303 /* Check the parameters */
5304 assert_param(IS_TIM_CC1_INSTANCE(TIMx
));
5305 assert_param(IS_TIM_CHANNELS(Channel
));
5307 tmp
= TIM_CCER_CC1E
<< Channel
;
5309 /* Reset the CCxE Bit */
5312 /* Set or reset the CCxE Bit */
5313 TIMx
->CCER
|= (uint32_t)(ChannelState
<< Channel
);
5321 #endif /* HAL_TIM_MODULE_ENABLED */
5329 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/