2 ******************************************************************************
3 * @file stm32f1xx_hal_tim_ex.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 Extended peripheral:
10 * + Time Hall Sensor Interface Initialization
11 * + Time Hall Sensor Interface Start
12 * + Time Complementary signal bread and dead time configuration
13 * + Time Master and Slave synchronization configuration
14 * + Timer remapping capabilities configuration
16 ==============================================================================
17 ##### TIMER Extended features #####
18 ==============================================================================
20 The Timer Extended features include:
21 (#) Complementary outputs with programmable dead-time for :
23 (++) PWM generation (Edge and Center-aligned Mode)
24 (++) One-pulse mode output
25 (#) Synchronization circuit to control the timer with external signals and to
26 interconnect several timers together.
27 (#) Break input to put the timer output signals in reset state or in a known state.
28 (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
31 ##### How to use this driver #####
32 ==============================================================================
34 (#) Initialize the TIM low level resources by implementing the following functions
35 depending from feature used :
36 (++) Complementary Output Compare : HAL_TIM_OC_MspInit()
37 (++) Complementary PWM generation : HAL_TIM_PWM_MspInit()
38 (++) Complementary One-pulse mode output : HAL_TIM_OnePulse_MspInit()
39 (++) Hall Sensor output : HAL_TIMEx_HallSensor_MspInit()
41 (#) Initialize the TIM low level resources :
42 (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
43 (##) TIM pins configuration
44 (+++) Enable the clock for the TIM GPIOs using the following function:
45 __HAL_GPIOx_CLK_ENABLE();
46 (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
48 (#) The external Clock can be configured, if needed (the default clock is the
49 internal clock from the APBx), using the following function:
50 HAL_TIM_ConfigClockSource, the clock configuration should be done before
53 (#) Configure the TIM in the desired functioning mode using one of the
54 initialization function of this driver:
55 (++) HAL_TIMEx_HallSensor_Init and HAL_TIMEx_ConfigCommutationEvent: to use the
56 Timer Hall Sensor Interface and the commutation event with the corresponding
57 Interrupt and DMA request if needed (Note that One Timer is used to interface
58 with the Hall sensor Interface and another Timer should be used to use
59 the commutation event).
61 (#) Activate the TIM peripheral using one of the start functions:
62 (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(), HAL_TIMEx_OCN_Start_IT()
63 (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(), HAL_TIMEx_PWMN_Start_IT()
64 (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
65 (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(), HAL_TIMEx_HallSensor_Start_IT().
69 ******************************************************************************
72 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
74 * Redistribution and use in source and binary forms, with or without modification,
75 * are permitted provided that the following conditions are met:
76 * 1. Redistributions of source code must retain the above copyright notice,
77 * this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright notice,
79 * this list of conditions and the following disclaimer in the documentation
80 * and/or other materials provided with the distribution.
81 * 3. Neither the name of STMicroelectronics nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
86 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
88 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
91 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
92 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
93 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
94 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96 ******************************************************************************
99 /* Includes ------------------------------------------------------------------*/
100 #include "stm32f1xx_hal.h"
102 /** @addtogroup STM32F1xx_HAL_Driver
106 /** @defgroup TIMEx TIMEx
107 * @brief TIM Extended HAL module driver
111 #ifdef HAL_TIM_MODULE_ENABLED
113 /* Private typedef -----------------------------------------------------------*/
114 /* Private define ------------------------------------------------------------*/
115 /* Private macro -------------------------------------------------------------*/
116 /* Private variables ---------------------------------------------------------*/
117 /* Private function prototypes -----------------------------------------------*/
119 #if defined (STM32F100xB) || defined (STM32F100xE) || \
120 defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F103xE) || defined (STM32F103xG) || \
121 defined (STM32F105xC) || defined (STM32F107xC)
122 /** @defgroup TIMEx_Private_Functions TIMEx Private Functions
125 static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState);
129 #endif /* defined(STM32F100xB) || defined(STM32F100xE) || */
130 /* defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || */
131 /* defined(STM32F105xC) || defined(STM32F107xC) */
133 /* Exported functions ---------------------------------------------------------*/
135 /** @defgroup TIMEx_Exported_Functions TIMEx Exported Functions
139 #if defined (STM32F100xB) || defined (STM32F100xE) || \
140 defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F103xE) || defined (STM32F103xG) || \
141 defined (STM32F105xC) || defined (STM32F107xC)
143 /** @defgroup TIMEx_Exported_Functions_Group1 Timer Hall Sensor functions
144 * @brief Timer Hall Sensor functions
147 ==============================================================================
148 ##### Timer Hall Sensor functions #####
149 ==============================================================================
151 This section provides functions allowing to:
152 (+) Initialize and configure TIM HAL Sensor.
153 (+) De-initialize TIM HAL Sensor.
154 (+) Start the Hall Sensor Interface.
155 (+) Stop the Hall Sensor Interface.
156 (+) Start the Hall Sensor Interface and enable interrupts.
157 (+) Stop the Hall Sensor Interface and disable interrupts.
158 (+) Start the Hall Sensor Interface and enable DMA transfers.
159 (+) Stop the Hall Sensor Interface and disable DMA transfers.
165 * @brief Initializes the TIM Hall Sensor Interface and create the associated handle.
166 * @param htim : TIM Encoder Interface handle
167 * @param sConfig : TIM Hall Sensor configuration structure
170 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef* sConfig)
172 TIM_OC_InitTypeDef OC_Config;
174 /* Check the TIM handle allocation */
180 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
181 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
182 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
183 assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
184 assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
185 assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
187 if(htim->State == HAL_TIM_STATE_RESET)
189 /* Allocate lock resource and initialize it */
190 htim-> Lock = HAL_UNLOCKED;
192 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
193 HAL_TIMEx_HallSensor_MspInit(htim);
196 /* Set the TIM state */
197 htim->State= HAL_TIM_STATE_BUSY;
199 /* Configure the Time base in the Encoder Mode */
200 TIM_Base_SetConfig(htim->Instance, &htim->Init);
202 /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
203 TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
205 /* Reset the IC1PSC Bits */
206 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
207 /* Set the IC1PSC value */
208 htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
210 /* Enable the Hall sensor interface (XOR function of the three inputs) */
211 htim->Instance->CR2 |= TIM_CR2_TI1S;
213 /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
214 htim->Instance->SMCR &= ~TIM_SMCR_TS;
215 htim->Instance->SMCR |= TIM_TS_TI1F_ED;
217 /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
218 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
219 htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
221 /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
222 OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
223 OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
224 OC_Config.OCMode = TIM_OCMODE_PWM2;
225 OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
226 OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
227 OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
228 OC_Config.Pulse = sConfig->Commutation_Delay;
230 TIM_OC2_SetConfig(htim->Instance, &OC_Config);
232 /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
234 htim->Instance->CR2 &= ~TIM_CR2_MMS;
235 htim->Instance->CR2 |= TIM_TRGO_OC2REF;
237 /* Initialize the TIM state*/
238 htim->State= HAL_TIM_STATE_READY;
244 * @brief DeInitializes the TIM Hall Sensor interface
245 * @param htim : TIM Hall Sensor handle
248 HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
250 /* Check the parameters */
251 assert_param(IS_TIM_INSTANCE(htim->Instance));
253 htim->State = HAL_TIM_STATE_BUSY;
255 /* Disable the TIM Peripheral Clock */
256 __HAL_TIM_DISABLE(htim);
258 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
259 HAL_TIMEx_HallSensor_MspDeInit(htim);
261 /* Change TIM state */
262 htim->State = HAL_TIM_STATE_RESET;
271 * @brief Initializes the TIM Hall Sensor MSP.
272 * @param htim : TIM handle
275 __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
277 /* NOTE : This function Should not be modified, when the callback is needed,
278 the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
283 * @brief DeInitializes TIM Hall Sensor MSP.
284 * @param htim : TIM handle
287 __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
289 /* NOTE : This function Should not be modified, when the callback is needed,
290 the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
295 * @brief Starts the TIM Hall Sensor Interface.
296 * @param htim : TIM Hall Sensor handle
299 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
301 /* Check the parameters */
302 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
304 /* Enable the Input Capture channel 1
305 (in the Hall Sensor Interface the 3 possible channels that are used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
306 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
308 /* Enable the Peripheral */
309 __HAL_TIM_ENABLE(htim);
311 /* Return function status */
316 * @brief Stops the TIM Hall sensor Interface.
317 * @param htim : TIM Hall Sensor handle
320 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
322 /* Check the parameters */
323 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
325 /* Disable the Input Capture channel 1
326 (in the Hall Sensor Interface the 3 possible channels that are used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
327 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
329 /* Disable the Peripheral */
330 __HAL_TIM_DISABLE(htim);
332 /* Return function status */
337 * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
338 * @param htim : TIM Hall Sensor handle
341 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
343 /* Check the parameters */
344 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
346 /* Enable the capture compare Interrupts 1 event */
347 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
349 /* Enable the Input Capture channel 1
350 (in the Hall Sensor Interface the 3 possible channels that are used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
351 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
353 /* Enable the Peripheral */
354 __HAL_TIM_ENABLE(htim);
356 /* Return function status */
361 * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
362 * @param htim : TIM handle
365 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
367 /* Check the parameters */
368 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
370 /* Disable the Input Capture channel 1
371 (in the Hall Sensor Interface the 3 possible channels that are used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
372 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
374 /* Disable the capture compare Interrupts event */
375 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
377 /* Disable the Peripheral */
378 __HAL_TIM_DISABLE(htim);
380 /* Return function status */
385 * @brief Starts the TIM Hall Sensor Interface in DMA mode.
386 * @param htim : TIM Hall Sensor handle
387 * @param pData : The destination Buffer address.
388 * @param Length : The length of data to be transferred from TIM peripheral to memory.
391 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
393 /* Check the parameters */
394 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
396 if((htim->State == HAL_TIM_STATE_BUSY))
400 else if((htim->State == HAL_TIM_STATE_READY))
402 if(((uint32_t)pData == 0 ) && (Length > 0))
408 htim->State = HAL_TIM_STATE_BUSY;
411 /* Enable the Input Capture channel 1
412 (in the Hall Sensor Interface the 3 possible channels that are used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
413 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
415 /* Set the DMA Input Capture 1 Callback */
416 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
417 /* Set the DMA error callback */
418 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
420 /* Enable the DMA channel for Capture 1*/
421 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
423 /* Enable the capture compare 1 Interrupt */
424 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
426 /* Enable the Peripheral */
427 __HAL_TIM_ENABLE(htim);
429 /* Return function status */
434 * @brief Stops the TIM Hall Sensor Interface in DMA mode.
435 * @param htim : TIM handle
438 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
440 /* Check the parameters */
441 assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
443 /* Disable the Input Capture channel 1
444 (in the Hall Sensor Interface the 3 possible channels that are used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
445 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
448 /* Disable the capture compare Interrupts 1 event */
449 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
451 /* Disable the Peripheral */
452 __HAL_TIM_DISABLE(htim);
454 /* Return function status */
462 /** @defgroup TIMEx_Exported_Functions_Group2 Timer Complementary Output Compare functions
463 * @brief Timer Complementary Output Compare functions
466 ==============================================================================
467 ##### Timer Complementary Output Compare functions #####
468 ==============================================================================
470 This section provides functions allowing to:
471 (+) Start the Complementary Output Compare/PWM.
472 (+) Stop the Complementary Output Compare/PWM.
473 (+) Start the Complementary Output Compare/PWM and enable interrupts.
474 (+) Stop the Complementary Output Compare/PWM and disable interrupts.
475 (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
476 (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
483 * @brief Starts the TIM Output Compare signal generation on the complementary
485 * @param htim : TIM Output Compare handle
486 * @param Channel : TIM Channel to be enabled
487 * This parameter can be one of the following values:
488 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
489 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
490 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
491 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
494 HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
496 /* Check the parameters */
497 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
499 /* Enable the Capture compare channel N */
500 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
502 /* Enable the Main Ouput */
503 __HAL_TIM_MOE_ENABLE(htim);
505 /* Enable the Peripheral */
506 __HAL_TIM_ENABLE(htim);
508 /* Return function status */
513 * @brief Stops the TIM Output Compare signal generation on the complementary
515 * @param htim : TIM handle
516 * @param Channel : TIM Channel to be disabled
517 * This parameter can be one of the following values:
518 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
519 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
520 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
521 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
524 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
526 /* Check the parameters */
527 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
529 /* Disable the Capture compare channel N */
530 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
532 /* Disable the Main Ouput */
533 __HAL_TIM_MOE_DISABLE(htim);
535 /* Disable the Peripheral */
536 __HAL_TIM_DISABLE(htim);
538 /* Return function status */
543 * @brief Starts the TIM Output Compare signal generation in interrupt mode
544 * on the complementary output.
545 * @param htim : TIM OC handle
546 * @param Channel : TIM Channel to be enabled
547 * This parameter can be one of the following values:
548 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
549 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
550 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
551 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
554 HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
556 /* Check the parameters */
557 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
563 /* Enable the TIM Output Compare interrupt */
564 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
570 /* Enable the TIM Output Compare interrupt */
571 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
577 /* Enable the TIM Output Compare interrupt */
578 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
584 /* Enable the TIM Output Compare interrupt */
585 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
593 /* Enable the TIM Break interrupt */
594 __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
596 /* Enable the Capture compare channel N */
597 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
599 /* Enable the Main Ouput */
600 __HAL_TIM_MOE_ENABLE(htim);
602 /* Enable the Peripheral */
603 __HAL_TIM_ENABLE(htim);
605 /* Return function status */
610 * @brief Stops the TIM Output Compare signal generation in interrupt mode
611 * on the complementary output.
612 * @param htim : TIM Output Compare handle
613 * @param Channel : TIM Channel to be disabled
614 * This parameter can be one of the following values:
615 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
616 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
617 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
618 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
621 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
623 uint32_t tmpccer = 0;
625 /* Check the parameters */
626 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
632 /* Disable the TIM Output Compare interrupt */
633 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
639 /* Disable the TIM Output Compare interrupt */
640 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
646 /* Disable the TIM Output Compare interrupt */
647 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
653 /* Disable the TIM Output Compare interrupt */
654 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
662 /* Disable the Capture compare channel N */
663 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
665 /* Disable the TIM Break interrupt (only if no more channel is active) */
666 tmpccer = htim->Instance->CCER;
667 if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
669 __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
672 /* Disable the Main Ouput */
673 __HAL_TIM_MOE_DISABLE(htim);
675 /* Disable the Peripheral */
676 __HAL_TIM_DISABLE(htim);
678 /* Return function status */
683 * @brief Starts the TIM Output Compare signal generation in DMA mode
684 * on the complementary output.
685 * @param htim : TIM Output Compare handle
686 * @param Channel : TIM Channel to be enabled
687 * This parameter can be one of the following values:
688 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
689 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
690 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
691 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
692 * @param pData : The source Buffer address.
693 * @param Length : The length of data to be transferred from memory to TIM peripheral
696 HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
698 /* Check the parameters */
699 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
701 if((htim->State == HAL_TIM_STATE_BUSY))
705 else if((htim->State == HAL_TIM_STATE_READY))
707 if(((uint32_t)pData == 0 ) && (Length > 0))
713 htim->State = HAL_TIM_STATE_BUSY;
720 /* Set the DMA Period elapsed callback */
721 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
723 /* Set the DMA error callback */
724 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
726 /* Enable the DMA channel */
727 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
729 /* Enable the TIM Output Compare DMA request */
730 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
736 /* Set the DMA Period elapsed callback */
737 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
739 /* Set the DMA error callback */
740 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
742 /* Enable the DMA channel */
743 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
745 /* Enable the TIM Output Compare DMA request */
746 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
752 /* Set the DMA Period elapsed callback */
753 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
755 /* Set the DMA error callback */
756 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
758 /* Enable the DMA channel */
759 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
761 /* Enable the TIM Output Compare DMA request */
762 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
768 /* Set the DMA Period elapsed callback */
769 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
771 /* Set the DMA error callback */
772 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
774 /* Enable the DMA channel */
775 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
777 /* Enable the TIM Output Compare DMA request */
778 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
786 /* Enable the Capture compare channel N */
787 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
789 /* Enable the Main Ouput */
790 __HAL_TIM_MOE_ENABLE(htim);
792 /* Enable the Peripheral */
793 __HAL_TIM_ENABLE(htim);
795 /* Return function status */
800 * @brief Stops the TIM Output Compare signal generation in DMA mode
801 * on the complementary output.
802 * @param htim : TIM Output Compare handle
803 * @param Channel : TIM Channel to be disabled
804 * This parameter can be one of the following values:
805 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
806 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
807 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
808 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
811 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
813 /* Check the parameters */
814 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
820 /* Disable the TIM Output Compare DMA request */
821 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
827 /* Disable the TIM Output Compare DMA request */
828 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
834 /* Disable the TIM Output Compare DMA request */
835 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
841 /* Disable the TIM Output Compare interrupt */
842 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
850 /* Disable the Capture compare channel N */
851 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
853 /* Disable the Main Ouput */
854 __HAL_TIM_MOE_DISABLE(htim);
856 /* Disable the Peripheral */
857 __HAL_TIM_DISABLE(htim);
859 /* Change the htim state */
860 htim->State = HAL_TIM_STATE_READY;
862 /* Return function status */
870 /** @defgroup TIMEx_Exported_Functions_Group3 Timer Complementary PWM functions
871 * @brief Timer Complementary PWM functions
874 ==============================================================================
875 ##### Timer Complementary PWM functions #####
876 ==============================================================================
878 This section provides functions allowing to:
879 (+) Start the Complementary PWM.
880 (+) Stop the Complementary PWM.
881 (+) Start the Complementary PWM and enable interrupts.
882 (+) Stop the Complementary PWM and disable interrupts.
883 (+) Start the Complementary PWM and enable DMA transfers.
884 (+) Stop the Complementary PWM and disable DMA transfers.
885 (+) Start the Complementary Input Capture measurement.
886 (+) Stop the Complementary Input Capture.
887 (+) Start the Complementary Input Capture and enable interrupts.
888 (+) Stop the Complementary Input Capture and disable interrupts.
889 (+) Start the Complementary Input Capture and enable DMA transfers.
890 (+) Stop the Complementary Input Capture and disable DMA transfers.
891 (+) Start the Complementary One Pulse generation.
892 (+) Stop the Complementary One Pulse.
893 (+) Start the Complementary One Pulse and enable interrupts.
894 (+) Stop the Complementary One Pulse and disable interrupts.
901 * @brief Starts the PWM signal generation on the complementary output.
902 * @param htim : TIM handle
903 * @param Channel : TIM Channel to be enabled
904 * This parameter can be one of the following values:
905 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
906 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
907 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
908 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
911 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
913 /* Check the parameters */
914 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
916 /* Enable the complementary PWM output */
917 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
919 /* Enable the Main Ouput */
920 __HAL_TIM_MOE_ENABLE(htim);
922 /* Enable the Peripheral */
923 __HAL_TIM_ENABLE(htim);
925 /* Return function status */
930 * @brief Stops the PWM signal generation on the complementary output.
931 * @param htim : TIM handle
932 * @param Channel : TIM Channel to be disabled
933 * This parameter can be one of the following values:
934 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
935 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
936 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
937 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
940 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
942 /* Check the parameters */
943 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
945 /* Disable the complementary PWM output */
946 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
948 /* Disable the Main Ouput */
949 __HAL_TIM_MOE_DISABLE(htim);
951 /* Disable the Peripheral */
952 __HAL_TIM_DISABLE(htim);
954 /* Return function status */
959 * @brief Starts the PWM signal generation in interrupt mode on the
960 * complementary output.
961 * @param htim : TIM handle
962 * @param Channel : TIM Channel to be disabled
963 * This parameter can be one of the following values:
964 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
965 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
966 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
967 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
970 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
972 /* Check the parameters */
973 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
979 /* Enable the TIM Capture/Compare 1 interrupt */
980 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
986 /* Enable the TIM Capture/Compare 2 interrupt */
987 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
993 /* Enable the TIM Capture/Compare 3 interrupt */
994 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1000 /* Enable the TIM Capture/Compare 4 interrupt */
1001 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
1009 /* Enable the TIM Break interrupt */
1010 __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
1012 /* Enable the complementary PWM output */
1013 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1015 /* Enable the Main Ouput */
1016 __HAL_TIM_MOE_ENABLE(htim);
1018 /* Enable the Peripheral */
1019 __HAL_TIM_ENABLE(htim);
1021 /* Return function status */
1026 * @brief Stops the PWM signal generation in interrupt mode on the
1027 * complementary output.
1028 * @param htim : TIM handle
1029 * @param Channel : TIM Channel to be disabled
1030 * This parameter can be one of the following values:
1031 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1032 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1033 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1034 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1035 * @retval HAL status
1037 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
1039 uint32_t tmpccer = 0;
1041 /* Check the parameters */
1042 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1048 /* Disable the TIM Capture/Compare 1 interrupt */
1049 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1055 /* Disable the TIM Capture/Compare 2 interrupt */
1056 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1062 /* Disable the TIM Capture/Compare 3 interrupt */
1063 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1069 /* Disable the TIM Capture/Compare 3 interrupt */
1070 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
1078 /* Disable the complementary PWM output */
1079 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1081 /* Disable the TIM Break interrupt (only if no more channel is active) */
1082 tmpccer = htim->Instance->CCER;
1083 if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
1085 __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
1088 /* Disable the Main Ouput */
1089 __HAL_TIM_MOE_DISABLE(htim);
1091 /* Disable the Peripheral */
1092 __HAL_TIM_DISABLE(htim);
1094 /* Return function status */
1099 * @brief Starts the TIM PWM signal generation in DMA mode on the
1100 * complementary output
1101 * @param htim : TIM handle
1102 * @param Channel : TIM Channel to be enabled
1103 * This parameter can be one of the following values:
1104 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1105 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1106 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1107 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1108 * @param pData : The source Buffer address.
1109 * @param Length : The length of data to be transferred from memory to TIM peripheral
1110 * @retval HAL status
1112 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
1114 /* Check the parameters */
1115 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1117 if((htim->State == HAL_TIM_STATE_BUSY))
1121 else if((htim->State == HAL_TIM_STATE_READY))
1123 if(((uint32_t)pData == 0 ) && (Length > 0))
1129 htim->State = HAL_TIM_STATE_BUSY;
1136 /* Set the DMA Period elapsed callback */
1137 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
1139 /* Set the DMA error callback */
1140 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
1142 /* Enable the DMA channel */
1143 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
1145 /* Enable the TIM Capture/Compare 1 DMA request */
1146 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1152 /* Set the DMA Period elapsed callback */
1153 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
1155 /* Set the DMA error callback */
1156 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
1158 /* Enable the DMA channel */
1159 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
1161 /* Enable the TIM Capture/Compare 2 DMA request */
1162 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1168 /* Set the DMA Period elapsed callback */
1169 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
1171 /* Set the DMA error callback */
1172 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
1174 /* Enable the DMA channel */
1175 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
1177 /* Enable the TIM Capture/Compare 3 DMA request */
1178 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1184 /* Set the DMA Period elapsed callback */
1185 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
1187 /* Set the DMA error callback */
1188 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
1190 /* Enable the DMA channel */
1191 HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
1193 /* Enable the TIM Capture/Compare 4 DMA request */
1194 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1202 /* Enable the complementary PWM output */
1203 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1205 /* Enable the Main Ouput */
1206 __HAL_TIM_MOE_ENABLE(htim);
1208 /* Enable the Peripheral */
1209 __HAL_TIM_ENABLE(htim);
1211 /* Return function status */
1216 * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
1218 * @param htim : TIM handle
1219 * @param Channel : TIM Channel to be disabled
1220 * This parameter can be one of the following values:
1221 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1222 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1223 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1224 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1225 * @retval HAL status
1227 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1229 /* Check the parameters */
1230 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1236 /* Disable the TIM Capture/Compare 1 DMA request */
1237 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1243 /* Disable the TIM Capture/Compare 2 DMA request */
1244 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1250 /* Disable the TIM Capture/Compare 3 DMA request */
1251 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1257 /* Disable the TIM Capture/Compare 4 DMA request */
1258 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1266 /* Disable the complementary PWM output */
1267 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1269 /* Disable the Main Ouput */
1270 __HAL_TIM_MOE_DISABLE(htim);
1272 /* Disable the Peripheral */
1273 __HAL_TIM_DISABLE(htim);
1275 /* Change the htim state */
1276 htim->State = HAL_TIM_STATE_READY;
1278 /* Return function status */
1286 /** @defgroup TIMEx_Exported_Functions_Group4 Timer Complementary One Pulse functions
1287 * @brief Timer Complementary One Pulse functions
1290 ==============================================================================
1291 ##### Timer Complementary One Pulse functions #####
1292 ==============================================================================
1294 This section provides functions allowing to:
1295 (+) Start the Complementary One Pulse generation.
1296 (+) Stop the Complementary One Pulse.
1297 (+) Start the Complementary One Pulse and enable interrupts.
1298 (+) Stop the Complementary One Pulse and disable interrupts.
1305 * @brief Starts the TIM One Pulse signal generation on the complemetary
1307 * @param htim : TIM One Pulse handle
1308 * @param OutputChannel : TIM Channel to be enabled
1309 * This parameter can be one of the following values:
1310 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1311 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1312 * @retval HAL status
1314 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1316 /* Check the parameters */
1317 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1319 /* Enable the complementary One Pulse output */
1320 TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
1322 /* Enable the Main Ouput */
1323 __HAL_TIM_MOE_ENABLE(htim);
1325 /* Return function status */
1330 * @brief Stops the TIM One Pulse signal generation on the complementary
1332 * @param htim : TIM One Pulse handle
1333 * @param OutputChannel : TIM Channel to be disabled
1334 * This parameter can be one of the following values:
1335 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1336 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1337 * @retval HAL status
1339 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1342 /* Check the parameters */
1343 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1345 /* Disable the complementary One Pulse output */
1346 TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
1348 /* Disable the Main Ouput */
1349 __HAL_TIM_MOE_DISABLE(htim);
1351 /* Disable the Peripheral */
1352 __HAL_TIM_DISABLE(htim);
1354 /* Return function status */
1359 * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
1360 * complementary channel.
1361 * @param htim : TIM One Pulse handle
1362 * @param OutputChannel : TIM Channel to be enabled
1363 * This parameter can be one of the following values:
1364 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1365 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1366 * @retval HAL status
1368 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1370 /* Check the parameters */
1371 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1373 /* Enable the TIM Capture/Compare 1 interrupt */
1374 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1376 /* Enable the TIM Capture/Compare 2 interrupt */
1377 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1379 /* Enable the complementary One Pulse output */
1380 TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
1382 /* Enable the Main Ouput */
1383 __HAL_TIM_MOE_ENABLE(htim);
1385 /* Return function status */
1390 * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
1391 * complementary channel.
1392 * @param htim : TIM One Pulse handle
1393 * @param OutputChannel : TIM Channel to be disabled
1394 * This parameter can be one of the following values:
1395 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1396 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1397 * @retval HAL status
1399 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1401 /* Check the parameters */
1402 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1404 /* Disable the TIM Capture/Compare 1 interrupt */
1405 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1407 /* Disable the TIM Capture/Compare 2 interrupt */
1408 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1410 /* Disable the complementary One Pulse output */
1411 TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
1413 /* Disable the Main Ouput */
1414 __HAL_TIM_MOE_DISABLE(htim);
1416 /* Disable the Peripheral */
1417 __HAL_TIM_DISABLE(htim);
1419 /* Return function status */
1427 #endif /* defined(STM32F100xB) || defined(STM32F100xE) || */
1428 /* defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || */
1429 /* defined(STM32F105xC) || defined(STM32F107xC) */
1431 /** @defgroup TIMEx_Exported_Functions_Group5 Peripheral Control functions
1432 * @brief Peripheral Control functions
1435 ==============================================================================
1436 ##### Peripheral Control functions #####
1437 ==============================================================================
1439 This section provides functions allowing to:
1440 (+) Configure the commutation event in case of use of the Hall sensor interface.
1441 (+) Configure Complementary channels, break features and dead time.
1442 (+) Configure Master synchronization.
1448 #if defined (STM32F100xB) || defined (STM32F100xE) || \
1449 defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F103xE) || defined (STM32F103xG) || \
1450 defined (STM32F105xC) || defined (STM32F107xC)
1453 * @brief Configure the TIM commutation event sequence.
1454 * @note: this function is mandatory to use the commutation event in order to
1455 * update the configuration at each commutation detection on the TRGI input of the Timer,
1456 * the typical use of this feature is with the use of another Timer(interface Timer)
1457 * configured in Hall sensor interface, this interface Timer will generate the
1458 * commutation at its TRGO output (connected to Timer used in this function) each time
1459 * the TI1 of the Interface Timer detect a commutation at its input TI1.
1460 * @param htim : TIM handle
1461 * @param InputTrigger : the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
1462 * This parameter can be one of the following values:
1463 * @arg TIM_TS_ITR0: Internal trigger 0 selected
1464 * @arg TIM_TS_ITR1: Internal trigger 1 selected
1465 * @arg TIM_TS_ITR2: Internal trigger 2 selected
1466 * @arg TIM_TS_ITR3: Internal trigger 3 selected
1467 * @arg TIM_TS_NONE: No trigger is needed
1468 * @param CommutationSource : the Commutation Event source
1469 * This parameter can be one of the following values:
1470 * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
1471 * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
1472 * @retval HAL status
1474 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
1476 /* Check the parameters */
1477 assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
1478 assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
1482 if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
1483 (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
1485 /* Select the Input trigger */
1486 htim->Instance->SMCR &= ~TIM_SMCR_TS;
1487 htim->Instance->SMCR |= InputTrigger;
1490 /* Select the Capture Compare preload feature */
1491 htim->Instance->CR2 |= TIM_CR2_CCPC;
1492 /* Select the Commutation event source */
1493 htim->Instance->CR2 &= ~TIM_CR2_CCUS;
1494 htim->Instance->CR2 |= CommutationSource;
1502 * @brief Configure the TIM commutation event sequence with interrupt.
1503 * @note: this function is mandatory to use the commutation event in order to
1504 * update the configuration at each commutation detection on the TRGI input of the Timer,
1505 * the typical use of this feature is with the use of another Timer(interface Timer)
1506 * configured in Hall sensor interface, this interface Timer will generate the
1507 * commutation at its TRGO output (connected to Timer used in this function) each time
1508 * the TI1 of the Interface Timer detect a commutation at its input TI1.
1509 * @param htim : TIM handle
1510 * @param InputTrigger : the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
1511 * This parameter can be one of the following values:
1512 * @arg TIM_TS_ITR0: Internal trigger 0 selected
1513 * @arg TIM_TS_ITR1: Internal trigger 1 selected
1514 * @arg TIM_TS_ITR2: Internal trigger 2 selected
1515 * @arg TIM_TS_ITR3: Internal trigger 3 selected
1516 * @arg TIM_TS_NONE: No trigger is needed
1517 * @param CommutationSource : the Commutation Event source
1518 * This parameter can be one of the following values:
1519 * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
1520 * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
1521 * @retval HAL status
1523 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
1525 /* Check the parameters */
1526 assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
1527 assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
1531 if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
1532 (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
1534 /* Select the Input trigger */
1535 htim->Instance->SMCR &= ~TIM_SMCR_TS;
1536 htim->Instance->SMCR |= InputTrigger;
1539 /* Select the Capture Compare preload feature */
1540 htim->Instance->CR2 |= TIM_CR2_CCPC;
1541 /* Select the Commutation event source */
1542 htim->Instance->CR2 &= ~TIM_CR2_CCUS;
1543 htim->Instance->CR2 |= CommutationSource;
1545 /* Enable the Commutation Interrupt Request */
1546 __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
1554 * @brief Configure the TIM commutation event sequence with DMA.
1555 * @note: this function is mandatory to use the commutation event in order to
1556 * update the configuration at each commutation detection on the TRGI input of the Timer,
1557 * the typical use of this feature is with the use of another Timer(interface Timer)
1558 * configured in Hall sensor interface, this interface Timer will generate the
1559 * commutation at its TRGO output (connected to Timer used in this function) each time
1560 * the TI1 of the Interface Timer detect a commutation at its input TI1.
1561 * @note: The user should configure the DMA in his own software, in This function only the COMDE bit is set
1562 * @param htim : TIM handle
1563 * @param InputTrigger : the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
1564 * This parameter can be one of the following values:
1565 * @arg TIM_TS_ITR0: Internal trigger 0 selected
1566 * @arg TIM_TS_ITR1: Internal trigger 1 selected
1567 * @arg TIM_TS_ITR2: Internal trigger 2 selected
1568 * @arg TIM_TS_ITR3: Internal trigger 3 selected
1569 * @arg TIM_TS_NONE: No trigger is needed
1570 * @param CommutationSource : the Commutation Event source
1571 * This parameter can be one of the following values:
1572 * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
1573 * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
1574 * @retval HAL status
1576 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
1578 /* Check the parameters */
1579 assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
1580 assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
1584 if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
1585 (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
1587 /* Select the Input trigger */
1588 htim->Instance->SMCR &= ~TIM_SMCR_TS;
1589 htim->Instance->SMCR |= InputTrigger;
1592 /* Select the Capture Compare preload feature */
1593 htim->Instance->CR2 |= TIM_CR2_CCPC;
1594 /* Select the Commutation event source */
1595 htim->Instance->CR2 &= ~TIM_CR2_CCUS;
1596 htim->Instance->CR2 |= CommutationSource;
1598 /* Enable the Commutation DMA Request */
1599 /* Set the DMA Commutation Callback */
1600 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
1601 /* Set the DMA error callback */
1602 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
1604 /* Enable the Commutation DMA Request */
1605 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
1613 * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
1614 * and the AOE(automatic output enable).
1615 * @param htim : TIM handle
1616 * @param sBreakDeadTimeConfig : pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
1617 * contains the BDTR Register configuration information for the TIM peripheral.
1618 * @retval HAL status
1620 HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
1621 TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
1623 /* Check the parameters */
1624 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
1625 assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
1626 assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
1627 assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
1628 assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
1629 assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
1630 assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
1631 assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
1633 /* Process Locked */
1636 htim->State = HAL_TIM_STATE_BUSY;
1638 /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
1639 the OSSI State, the dead time value and the Automatic Output Enable Bit */
1640 htim->Instance->BDTR = (uint32_t)sBreakDeadTimeConfig->OffStateRunMode |
1641 sBreakDeadTimeConfig->OffStateIDLEMode |
1642 sBreakDeadTimeConfig->LockLevel |
1643 sBreakDeadTimeConfig->DeadTime |
1644 sBreakDeadTimeConfig->BreakState |
1645 sBreakDeadTimeConfig->BreakPolarity |
1646 sBreakDeadTimeConfig->AutomaticOutput;
1649 htim->State = HAL_TIM_STATE_READY;
1656 #endif /* defined(STM32F100xB) || defined(STM32F100xE) || */
1657 /* defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || */
1658 /* defined(STM32F105xC) || defined(STM32F107xC) */
1661 * @brief Configures the TIM in master mode.
1662 * @param htim : TIM handle.
1663 * @param sMasterConfig : pointer to a TIM_MasterConfigTypeDef structure that
1664 * contains the selected trigger output (TRGO) and the Master/Slave
1666 * @retval HAL status
1668 HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, TIM_MasterConfigTypeDef * sMasterConfig)
1670 /* Check the parameters */
1671 assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
1672 assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
1673 assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
1677 htim->State = HAL_TIM_STATE_BUSY;
1679 /* Reset the MMS Bits */
1680 htim->Instance->CR2 &= ~TIM_CR2_MMS;
1681 /* Select the TRGO source */
1682 htim->Instance->CR2 |= sMasterConfig->MasterOutputTrigger;
1684 /* Reset the MSM Bit */
1685 htim->Instance->SMCR &= ~TIM_SMCR_MSM;
1686 /* Set or Reset the MSM Bit */
1687 htim->Instance->SMCR |= sMasterConfig->MasterSlaveMode;
1689 htim->State = HAL_TIM_STATE_READY;
1700 /** @defgroup TIMEx_Exported_Functions_Group6 Extension Callbacks functions
1701 * @brief Extension Callbacks functions
1704 ==============================================================================
1705 ##### Extension Callbacks functions #####
1706 ==============================================================================
1708 This section provides Extension TIM callback functions:
1709 (+) Timer Commutation callback
1710 (+) Timer Break callback
1717 * @brief Hall commutation changed callback in non blocking mode
1718 * @param htim : TIM handle
1721 __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)
1723 /* NOTE : This function Should not be modified, when the callback is needed,
1724 the HAL_TIMEx_CommutationCallback could be implemented in the user file
1729 * @brief Hall Break detection callback in non blocking mode
1730 * @param htim : TIM handle
1733 __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
1735 /* NOTE : This function Should not be modified, when the callback is needed,
1736 the HAL_TIMEx_BreakCallback could be implemented in the user file
1741 * @brief TIM DMA Commutation callback.
1742 * @param hdma : pointer to DMA handle.
1745 void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
1747 TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1749 htim->State= HAL_TIM_STATE_READY;
1751 HAL_TIMEx_CommutationCallback(htim);
1758 #if defined (STM32F100xB) || defined (STM32F100xE) || \
1759 defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F103xE) || defined (STM32F103xG) || \
1760 defined (STM32F105xC) || defined (STM32F107xC)
1762 /** @defgroup TIMEx_Exported_Functions_Group7 Extension Peripheral State functions
1763 * @brief Extension Peripheral State functions
1766 ==============================================================================
1767 ##### Extension Peripheral State functions #####
1768 ==============================================================================
1770 This subsection permit to get in run-time the status of the peripheral
1778 * @brief Return the TIM Hall Sensor interface state
1779 * @param htim : TIM Hall Sensor handle
1782 HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
1790 #endif /* defined(STM32F100xB) || defined(STM32F100xE) || */
1791 /* defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || */
1792 /* defined(STM32F105xC) || defined(STM32F107xC) */
1798 #if defined (STM32F100xB) || defined (STM32F100xE) || \
1799 defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F103xE) || defined (STM32F103xG) || \
1800 defined (STM32F105xC) || defined (STM32F107xC)
1802 /** @addtogroup TIMEx_Private_Functions
1807 * @brief Enables or disables the TIM Capture Compare Channel xN.
1808 * @param TIMx to select the TIM peripheral
1809 * @param Channel : specifies the TIM Channel
1810 * This parameter can be one of the following values:
1811 * @arg TIM_Channel_1: TIM Channel 1
1812 * @arg TIM_Channel_2: TIM Channel 2
1813 * @arg TIM_Channel_3: TIM Channel 3
1814 * @param ChannelNState : specifies the TIM Channel CCxNE bit new state.
1815 * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
1818 static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState)
1822 tmp = TIM_CCER_CC1NE << Channel;
1824 /* Reset the CCxNE Bit */
1827 /* Set or reset the CCxNE Bit */
1828 TIMx->CCER |= (uint32_t)(ChannelNState << Channel);
1835 #endif /* defined(STM32F100xB) || defined(STM32F100xE) || */
1836 /* defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || */
1837 /* defined(STM32F105xC) || defined(STM32F107xC) */
1839 #endif /* HAL_TIM_MODULE_ENABLED */
1848 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/