Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:09

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_tim_ex.c
0004   * @author  MCD Application Team
0005   * @brief   TIM HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the Timer Extended peripheral:
0008   *           + Time Hall Sensor Interface Initialization
0009   *           + Time Hall Sensor Interface Start
0010   *           + Time Complementary signal break and dead time configuration
0011   *           + Time Master and Slave synchronization configuration
0012   *           + Time Output Compare/PWM Channel Configuration (for channels 5 and 6)
0013   *           + Timer remapping capabilities configuration
0014   ******************************************************************************
0015   * @attention
0016   *
0017   * Copyright (c) 2017 STMicroelectronics.
0018   * All rights reserved.
0019   *
0020   * This software is licensed under terms that can be found in the LICENSE file
0021   * in the root directory of this software component.
0022   * If no LICENSE file comes with this software, it is provided AS-IS.
0023   *
0024   ******************************************************************************
0025   @verbatim
0026   ==============================================================================
0027                       ##### TIMER Extended features #####
0028   ==============================================================================
0029   [..]
0030     The Timer Extended features include:
0031     (#) Complementary outputs with programmable dead-time for :
0032         (++) Output Compare
0033         (++) PWM generation (Edge and Center-aligned Mode)
0034         (++) One-pulse mode output
0035     (#) Synchronization circuit to control the timer with external signals and to
0036         interconnect several timers together.
0037     (#) Break input to put the timer output signals in reset state or in a known state.
0038     (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
0039         positioning purposes
0040 
0041             ##### How to use this driver #####
0042   ==============================================================================
0043     [..]
0044      (#) Initialize the TIM low level resources by implementing the following functions
0045          depending on the selected feature:
0046            (++) Hall Sensor output : HAL_TIMEx_HallSensor_MspInit()
0047 
0048      (#) Initialize the TIM low level resources :
0049         (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
0050         (##) TIM pins configuration
0051             (+++) Enable the clock for the TIM GPIOs using the following function:
0052               __HAL_RCC_GPIOx_CLK_ENABLE();
0053             (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
0054 
0055      (#) The external Clock can be configured, if needed (the default clock is the
0056          internal clock from the APBx), using the following function:
0057          HAL_TIM_ConfigClockSource, the clock configuration should be done before
0058          any start function.
0059 
0060      (#) Configure the TIM in the desired functioning mode using one of the
0061          initialization function of this driver:
0062           (++) HAL_TIMEx_HallSensor_Init() and HAL_TIMEx_ConfigCommutEvent(): to use the
0063                Timer Hall Sensor Interface and the commutation event with the corresponding
0064                Interrupt and DMA request if needed (Note that One Timer is used to interface
0065                with the Hall sensor Interface and another Timer should be used to use
0066                the commutation event).
0067 
0068      (#) Activate the TIM peripheral using one of the start functions:
0069            (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(),
0070                 HAL_TIMEx_OCN_Start_IT()
0071            (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(),
0072                 HAL_TIMEx_PWMN_Start_IT()
0073            (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
0074            (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(),
0075                 HAL_TIMEx_HallSensor_Start_IT().
0076 
0077   @endverbatim
0078   ******************************************************************************
0079   */
0080 
0081 /* Includes ------------------------------------------------------------------*/
0082 #include "stm32h7xx_hal.h"
0083 
0084 /** @addtogroup STM32H7xx_HAL_Driver
0085   * @{
0086   */
0087 
0088 /** @defgroup TIMEx TIMEx
0089   * @ingroup RTEMSBSPsARMSTM32H7
0090   * @brief TIM Extended HAL module driver
0091   * @{
0092   */
0093 
0094 #ifdef HAL_TIM_MODULE_ENABLED
0095 
0096 /* Private typedef -----------------------------------------------------------*/
0097 /* Private define ------------------------------------------------------------*/
0098 #if defined(TIM_BDTR_BKBID)
0099 /* Private constants ---------------------------------------------------------*/
0100 /** @defgroup TIMEx_Private_Constants TIM Extended Private Constants
0101   * @ingroup RTEMSBSPsARMSTM32H7
0102   * @{
0103   */
0104 /* Timeout for break input rearm */
0105 #define TIM_BREAKINPUT_REARM_TIMEOUT    5UL /* 5 milliseconds */
0106 /**
0107   * @}
0108   */
0109 /* End of private constants --------------------------------------------------*/
0110 
0111 #endif /* TIM_BDTR_BKBID */
0112 /* Private macros ------------------------------------------------------------*/
0113 /* Private variables ---------------------------------------------------------*/
0114 /* Private function prototypes -----------------------------------------------*/
0115 static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma);
0116 static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma);
0117 static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState);
0118 
0119 /* Exported functions --------------------------------------------------------*/
0120 /** @defgroup TIMEx_Exported_Functions TIM Extended Exported Functions
0121   * @ingroup RTEMSBSPsARMSTM32H7
0122   * @{
0123   */
0124 
0125 /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions
0126   * @ingroup RTEMSBSPsARMSTM32H7
0127   * @brief    Timer Hall Sensor functions
0128   *
0129 @verbatim
0130   ==============================================================================
0131                       ##### Timer Hall Sensor functions #####
0132   ==============================================================================
0133   [..]
0134     This section provides functions allowing to:
0135     (+) Initialize and configure TIM HAL Sensor.
0136     (+) De-initialize TIM HAL Sensor.
0137     (+) Start the Hall Sensor Interface.
0138     (+) Stop the Hall Sensor Interface.
0139     (+) Start the Hall Sensor Interface and enable interrupts.
0140     (+) Stop the Hall Sensor Interface and disable interrupts.
0141     (+) Start the Hall Sensor Interface and enable DMA transfers.
0142     (+) Stop the Hall Sensor Interface and disable DMA transfers.
0143 
0144 @endverbatim
0145   * @{
0146   */
0147 /**
0148   * @brief  Initializes the TIM Hall Sensor Interface and initialize the associated handle.
0149   * @note   When the timer instance is initialized in Hall Sensor Interface mode,
0150   *         timer channels 1 and channel 2 are reserved and cannot be used for
0151   *         other purpose.
0152   * @param  htim TIM Hall Sensor Interface handle
0153   * @param  sConfig TIM Hall Sensor configuration structure
0154   * @retval HAL status
0155   */
0156 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, const TIM_HallSensor_InitTypeDef *sConfig)
0157 {
0158   TIM_OC_InitTypeDef OC_Config;
0159 
0160   /* Check the TIM handle allocation */
0161   if (htim == NULL)
0162   {
0163     return HAL_ERROR;
0164   }
0165 
0166   /* Check the parameters */
0167   assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
0168   assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
0169   assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
0170   assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
0171   assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
0172   assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
0173   assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
0174   assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
0175 
0176   if (htim->State == HAL_TIM_STATE_RESET)
0177   {
0178     /* Allocate lock resource and initialize it */
0179     htim->Lock = HAL_UNLOCKED;
0180 
0181 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
0182     /* Reset interrupt callbacks to legacy week callbacks */
0183     TIM_ResetCallback(htim);
0184 
0185     if (htim->HallSensor_MspInitCallback == NULL)
0186     {
0187       htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit;
0188     }
0189     /* Init the low level hardware : GPIO, CLOCK, NVIC */
0190     htim->HallSensor_MspInitCallback(htim);
0191 #else
0192     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
0193     HAL_TIMEx_HallSensor_MspInit(htim);
0194 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
0195   }
0196 
0197   /* Set the TIM state */
0198   htim->State = HAL_TIM_STATE_BUSY;
0199 
0200   /* Configure the Time base in the Encoder Mode */
0201   TIM_Base_SetConfig(htim->Instance, &htim->Init);
0202 
0203   /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the  Hall sensor */
0204   TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
0205 
0206   /* Reset the IC1PSC Bits */
0207   htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
0208   /* Set the IC1PSC value */
0209   htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
0210 
0211   /* Enable the Hall sensor interface (XOR function of the three inputs) */
0212   htim->Instance->CR2 |= TIM_CR2_TI1S;
0213 
0214   /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
0215   htim->Instance->SMCR &= ~TIM_SMCR_TS;
0216   htim->Instance->SMCR |= TIM_TS_TI1F_ED;
0217 
0218   /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
0219   htim->Instance->SMCR &= ~TIM_SMCR_SMS;
0220   htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
0221 
0222   /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
0223   OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
0224   OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
0225   OC_Config.OCMode = TIM_OCMODE_PWM2;
0226   OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
0227   OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
0228   OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
0229   OC_Config.Pulse = sConfig->Commutation_Delay;
0230 
0231   TIM_OC2_SetConfig(htim->Instance, &OC_Config);
0232 
0233   /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
0234     register to 101 */
0235   htim->Instance->CR2 &= ~TIM_CR2_MMS;
0236   htim->Instance->CR2 |= TIM_TRGO_OC2REF;
0237 
0238   /* Initialize the DMA burst operation state */
0239   htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
0240 
0241   /* Initialize the TIM channels state */
0242   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
0243   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
0244   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
0245   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
0246 
0247   /* Initialize the TIM state*/
0248   htim->State = HAL_TIM_STATE_READY;
0249 
0250   return HAL_OK;
0251 }
0252 
0253 /**
0254   * @brief  DeInitializes the TIM Hall Sensor interface
0255   * @param  htim TIM Hall Sensor Interface handle
0256   * @retval HAL status
0257   */
0258 HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
0259 {
0260   /* Check the parameters */
0261   assert_param(IS_TIM_INSTANCE(htim->Instance));
0262 
0263   htim->State = HAL_TIM_STATE_BUSY;
0264 
0265   /* Disable the TIM Peripheral Clock */
0266   __HAL_TIM_DISABLE(htim);
0267 
0268 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
0269   if (htim->HallSensor_MspDeInitCallback == NULL)
0270   {
0271     htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit;
0272   }
0273   /* DeInit the low level hardware */
0274   htim->HallSensor_MspDeInitCallback(htim);
0275 #else
0276   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
0277   HAL_TIMEx_HallSensor_MspDeInit(htim);
0278 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
0279 
0280   /* Change the DMA burst operation state */
0281   htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
0282 
0283   /* Change the TIM channels state */
0284   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
0285   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
0286   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
0287   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
0288 
0289   /* Change TIM state */
0290   htim->State = HAL_TIM_STATE_RESET;
0291 
0292   /* Release Lock */
0293   __HAL_UNLOCK(htim);
0294 
0295   return HAL_OK;
0296 }
0297 
0298 /**
0299   * @brief  Initializes the TIM Hall Sensor MSP.
0300   * @param  htim TIM Hall Sensor Interface handle
0301   * @retval None
0302   */
0303 __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
0304 {
0305   /* Prevent unused argument(s) compilation warning */
0306   UNUSED(htim);
0307 
0308   /* NOTE : This function should not be modified, when the callback is needed,
0309             the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
0310    */
0311 }
0312 
0313 /**
0314   * @brief  DeInitializes TIM Hall Sensor MSP.
0315   * @param  htim TIM Hall Sensor Interface handle
0316   * @retval None
0317   */
0318 __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
0319 {
0320   /* Prevent unused argument(s) compilation warning */
0321   UNUSED(htim);
0322 
0323   /* NOTE : This function should not be modified, when the callback is needed,
0324             the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
0325    */
0326 }
0327 
0328 /**
0329   * @brief  Starts the TIM Hall Sensor Interface.
0330   * @param  htim TIM Hall Sensor Interface handle
0331   * @retval HAL status
0332   */
0333 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
0334 {
0335   uint32_t tmpsmcr;
0336   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
0337   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
0338   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
0339   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
0340 
0341   /* Check the parameters */
0342   assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
0343 
0344   /* Check the TIM channels state */
0345   if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
0346       || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
0347       || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
0348       || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
0349   {
0350     return HAL_ERROR;
0351   }
0352 
0353   /* Set the TIM channels state */
0354   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
0355   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
0356   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
0357   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
0358 
0359   /* Enable the Input Capture channel 1
0360   (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
0361   TIM_CHANNEL_2 and TIM_CHANNEL_3) */
0362   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
0363 
0364   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
0365   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
0366   {
0367     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
0368     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
0369     {
0370       __HAL_TIM_ENABLE(htim);
0371     }
0372   }
0373   else
0374   {
0375     __HAL_TIM_ENABLE(htim);
0376   }
0377 
0378   /* Return function status */
0379   return HAL_OK;
0380 }
0381 
0382 /**
0383   * @brief  Stops the TIM Hall sensor Interface.
0384   * @param  htim TIM Hall Sensor Interface handle
0385   * @retval HAL status
0386   */
0387 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
0388 {
0389   /* Check the parameters */
0390   assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
0391 
0392   /* Disable the Input Capture channels 1, 2 and 3
0393   (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
0394   TIM_CHANNEL_2 and TIM_CHANNEL_3) */
0395   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
0396 
0397   /* Disable the Peripheral */
0398   __HAL_TIM_DISABLE(htim);
0399 
0400   /* Set the TIM channels state */
0401   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
0402   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
0403   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
0404   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
0405 
0406   /* Return function status */
0407   return HAL_OK;
0408 }
0409 
0410 /**
0411   * @brief  Starts the TIM Hall Sensor Interface in interrupt mode.
0412   * @param  htim TIM Hall Sensor Interface handle
0413   * @retval HAL status
0414   */
0415 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
0416 {
0417   uint32_t tmpsmcr;
0418   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
0419   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
0420   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
0421   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
0422 
0423   /* Check the parameters */
0424   assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
0425 
0426   /* Check the TIM channels state */
0427   if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
0428       || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
0429       || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
0430       || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
0431   {
0432     return HAL_ERROR;
0433   }
0434 
0435   /* Set the TIM channels state */
0436   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
0437   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
0438   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
0439   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
0440 
0441   /* Enable the capture compare Interrupts 1 event */
0442   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
0443 
0444   /* Enable the Input Capture channel 1
0445   (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
0446   TIM_CHANNEL_2 and TIM_CHANNEL_3) */
0447   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
0448 
0449   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
0450   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
0451   {
0452     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
0453     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
0454     {
0455       __HAL_TIM_ENABLE(htim);
0456     }
0457   }
0458   else
0459   {
0460     __HAL_TIM_ENABLE(htim);
0461   }
0462 
0463   /* Return function status */
0464   return HAL_OK;
0465 }
0466 
0467 /**
0468   * @brief  Stops the TIM Hall Sensor Interface in interrupt mode.
0469   * @param  htim TIM Hall Sensor Interface handle
0470   * @retval HAL status
0471   */
0472 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
0473 {
0474   /* Check the parameters */
0475   assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
0476 
0477   /* Disable the Input Capture channel 1
0478   (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
0479   TIM_CHANNEL_2 and TIM_CHANNEL_3) */
0480   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
0481 
0482   /* Disable the capture compare Interrupts event */
0483   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
0484 
0485   /* Disable the Peripheral */
0486   __HAL_TIM_DISABLE(htim);
0487 
0488   /* Set the TIM channels state */
0489   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
0490   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
0491   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
0492   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
0493 
0494   /* Return function status */
0495   return HAL_OK;
0496 }
0497 
0498 /**
0499   * @brief  Starts the TIM Hall Sensor Interface in DMA mode.
0500   * @param  htim TIM Hall Sensor Interface handle
0501   * @param  pData The destination Buffer address.
0502   * @param  Length The length of data to be transferred from TIM peripheral to memory.
0503   * @retval HAL status
0504   */
0505 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
0506 {
0507   uint32_t tmpsmcr;
0508   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
0509   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
0510 
0511   /* Check the parameters */
0512   assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
0513 
0514   /* Set the TIM channel state */
0515   if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
0516       || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY))
0517   {
0518     return HAL_BUSY;
0519   }
0520   else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
0521            && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY))
0522   {
0523     if ((pData == NULL) || (Length == 0U))
0524     {
0525       return HAL_ERROR;
0526     }
0527     else
0528     {
0529       TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
0530       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
0531     }
0532   }
0533   else
0534   {
0535     return HAL_ERROR;
0536   }
0537 
0538   /* Enable the Input Capture channel 1
0539   (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
0540   TIM_CHANNEL_2 and TIM_CHANNEL_3) */
0541   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
0542 
0543   /* Set the DMA Input Capture 1 Callbacks */
0544   htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
0545   htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
0546   /* Set the DMA error callback */
0547   htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
0548 
0549   /* Enable the DMA stream for Capture 1*/
0550   if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
0551   {
0552     /* Return error status */
0553     return HAL_ERROR;
0554   }
0555   /* Enable the capture compare 1 Interrupt */
0556   __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
0557 
0558   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
0559   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
0560   {
0561     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
0562     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
0563     {
0564       __HAL_TIM_ENABLE(htim);
0565     }
0566   }
0567   else
0568   {
0569     __HAL_TIM_ENABLE(htim);
0570   }
0571 
0572   /* Return function status */
0573   return HAL_OK;
0574 }
0575 
0576 /**
0577   * @brief  Stops the TIM Hall Sensor Interface in DMA mode.
0578   * @param  htim TIM Hall Sensor Interface handle
0579   * @retval HAL status
0580   */
0581 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
0582 {
0583   /* Check the parameters */
0584   assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
0585 
0586   /* Disable the Input Capture channel 1
0587   (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
0588   TIM_CHANNEL_2 and TIM_CHANNEL_3) */
0589   TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
0590 
0591 
0592   /* Disable the capture compare Interrupts 1 event */
0593   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
0594 
0595   (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
0596 
0597   /* Disable the Peripheral */
0598   __HAL_TIM_DISABLE(htim);
0599 
0600   /* Set the TIM channel state */
0601   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
0602   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
0603 
0604   /* Return function status */
0605   return HAL_OK;
0606 }
0607 
0608 /**
0609   * @}
0610   */
0611 
0612 /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
0613   * @ingroup RTEMSBSPsARMSTM32H7
0614   *  @brief   Timer Complementary Output Compare functions
0615   *
0616 @verbatim
0617   ==============================================================================
0618               ##### Timer Complementary Output Compare functions #####
0619   ==============================================================================
0620   [..]
0621     This section provides functions allowing to:
0622     (+) Start the Complementary Output Compare/PWM.
0623     (+) Stop the Complementary Output Compare/PWM.
0624     (+) Start the Complementary Output Compare/PWM and enable interrupts.
0625     (+) Stop the Complementary Output Compare/PWM and disable interrupts.
0626     (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
0627     (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
0628 
0629 @endverbatim
0630   * @{
0631   */
0632 
0633 /**
0634   * @brief  Starts the TIM Output Compare signal generation on the complementary
0635   *         output.
0636   * @param  htim TIM Output Compare handle
0637   * @param  Channel TIM Channel to be enabled
0638   *          This parameter can be one of the following values:
0639   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
0640   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
0641   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
0642   * @retval HAL status
0643   */
0644 HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
0645 {
0646   uint32_t tmpsmcr;
0647 
0648   /* Check the parameters */
0649   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
0650 
0651   /* Check the TIM complementary channel state */
0652   if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
0653   {
0654     return HAL_ERROR;
0655   }
0656 
0657   /* Set the TIM complementary channel state */
0658   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
0659 
0660   /* Enable the Capture compare channel N */
0661   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
0662 
0663   /* Enable the Main Output */
0664   __HAL_TIM_MOE_ENABLE(htim);
0665 
0666   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
0667   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
0668   {
0669     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
0670     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
0671     {
0672       __HAL_TIM_ENABLE(htim);
0673     }
0674   }
0675   else
0676   {
0677     __HAL_TIM_ENABLE(htim);
0678   }
0679 
0680   /* Return function status */
0681   return HAL_OK;
0682 }
0683 
0684 /**
0685   * @brief  Stops the TIM Output Compare signal generation on the complementary
0686   *         output.
0687   * @param  htim TIM handle
0688   * @param  Channel TIM Channel to be disabled
0689   *          This parameter can be one of the following values:
0690   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
0691   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
0692   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
0693   * @retval HAL status
0694   */
0695 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
0696 {
0697   /* Check the parameters */
0698   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
0699 
0700   /* Disable the Capture compare channel N */
0701   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
0702 
0703   /* Disable the Main Output */
0704   __HAL_TIM_MOE_DISABLE(htim);
0705 
0706   /* Disable the Peripheral */
0707   __HAL_TIM_DISABLE(htim);
0708 
0709   /* Set the TIM complementary channel state */
0710   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
0711 
0712   /* Return function status */
0713   return HAL_OK;
0714 }
0715 
0716 /**
0717   * @brief  Starts the TIM Output Compare signal generation in interrupt mode
0718   *         on the complementary output.
0719   * @param  htim TIM OC handle
0720   * @param  Channel TIM Channel to be enabled
0721   *          This parameter can be one of the following values:
0722   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
0723   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
0724   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
0725   * @retval HAL status
0726   */
0727 HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
0728 {
0729   HAL_StatusTypeDef status = HAL_OK;
0730   uint32_t tmpsmcr;
0731 
0732   /* Check the parameters */
0733   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
0734 
0735   /* Check the TIM complementary channel state */
0736   if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
0737   {
0738     return HAL_ERROR;
0739   }
0740 
0741   /* Set the TIM complementary channel state */
0742   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
0743 
0744   switch (Channel)
0745   {
0746     case TIM_CHANNEL_1:
0747     {
0748       /* Enable the TIM Output Compare interrupt */
0749       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
0750       break;
0751     }
0752 
0753     case TIM_CHANNEL_2:
0754     {
0755       /* Enable the TIM Output Compare interrupt */
0756       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
0757       break;
0758     }
0759 
0760     case TIM_CHANNEL_3:
0761     {
0762       /* Enable the TIM Output Compare interrupt */
0763       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
0764       break;
0765     }
0766 
0767 
0768     default:
0769       status = HAL_ERROR;
0770       break;
0771   }
0772 
0773   if (status == HAL_OK)
0774   {
0775     /* Enable the TIM Break interrupt */
0776     __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
0777 
0778     /* Enable the Capture compare channel N */
0779     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
0780 
0781     /* Enable the Main Output */
0782     __HAL_TIM_MOE_ENABLE(htim);
0783 
0784     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
0785     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
0786     {
0787       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
0788       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
0789       {
0790         __HAL_TIM_ENABLE(htim);
0791       }
0792     }
0793     else
0794     {
0795       __HAL_TIM_ENABLE(htim);
0796     }
0797   }
0798 
0799   /* Return function status */
0800   return status;
0801 }
0802 
0803 /**
0804   * @brief  Stops the TIM Output Compare signal generation in interrupt mode
0805   *         on the complementary output.
0806   * @param  htim TIM Output Compare handle
0807   * @param  Channel TIM Channel to be disabled
0808   *          This parameter can be one of the following values:
0809   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
0810   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
0811   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
0812   * @retval HAL status
0813   */
0814 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
0815 {
0816   HAL_StatusTypeDef status = HAL_OK;
0817   uint32_t tmpccer;
0818 
0819   /* Check the parameters */
0820   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
0821 
0822   switch (Channel)
0823   {
0824     case TIM_CHANNEL_1:
0825     {
0826       /* Disable the TIM Output Compare interrupt */
0827       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
0828       break;
0829     }
0830 
0831     case TIM_CHANNEL_2:
0832     {
0833       /* Disable the TIM Output Compare interrupt */
0834       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
0835       break;
0836     }
0837 
0838     case TIM_CHANNEL_3:
0839     {
0840       /* Disable the TIM Output Compare interrupt */
0841       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
0842       break;
0843     }
0844 
0845     default:
0846       status = HAL_ERROR;
0847       break;
0848   }
0849 
0850   if (status == HAL_OK)
0851   {
0852     /* Disable the Capture compare channel N */
0853     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
0854 
0855     /* Disable the TIM Break interrupt (only if no more channel is active) */
0856     tmpccer = htim->Instance->CCER;
0857     if ((tmpccer & TIM_CCER_CCxNE_MASK) == (uint32_t)RESET)
0858     {
0859       __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
0860     }
0861 
0862     /* Disable the Main Output */
0863     __HAL_TIM_MOE_DISABLE(htim);
0864 
0865     /* Disable the Peripheral */
0866     __HAL_TIM_DISABLE(htim);
0867 
0868     /* Set the TIM complementary channel state */
0869     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
0870   }
0871 
0872   /* Return function status */
0873   return status;
0874 }
0875 
0876 /**
0877   * @brief  Starts the TIM Output Compare signal generation in DMA mode
0878   *         on the complementary output.
0879   * @param  htim TIM Output Compare handle
0880   * @param  Channel TIM Channel to be enabled
0881   *          This parameter can be one of the following values:
0882   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
0883   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
0884   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
0885   * @param  pData The source Buffer address.
0886   * @param  Length The length of data to be transferred from memory to TIM peripheral
0887   * @retval HAL status
0888   */
0889 HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
0890                                           uint16_t Length)
0891 {
0892   HAL_StatusTypeDef status = HAL_OK;
0893   uint32_t tmpsmcr;
0894 
0895   /* Check the parameters */
0896   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
0897 
0898   /* Set the TIM complementary channel state */
0899   if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
0900   {
0901     return HAL_BUSY;
0902   }
0903   else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
0904   {
0905     if ((pData == NULL) || (Length == 0U))
0906     {
0907       return HAL_ERROR;
0908     }
0909     else
0910     {
0911       TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
0912     }
0913   }
0914   else
0915   {
0916     return HAL_ERROR;
0917   }
0918 
0919   switch (Channel)
0920   {
0921     case TIM_CHANNEL_1:
0922     {
0923       /* Set the DMA compare callbacks */
0924       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
0925       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
0926 
0927       /* Set the DMA error callback */
0928       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
0929 
0930       /* Enable the DMA stream */
0931       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
0932                            Length) != HAL_OK)
0933       {
0934         /* Return error status */
0935         return HAL_ERROR;
0936       }
0937       /* Enable the TIM Output Compare DMA request */
0938       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
0939       break;
0940     }
0941 
0942     case TIM_CHANNEL_2:
0943     {
0944       /* Set the DMA compare callbacks */
0945       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
0946       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
0947 
0948       /* Set the DMA error callback */
0949       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
0950 
0951       /* Enable the DMA stream */
0952       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
0953                            Length) != HAL_OK)
0954       {
0955         /* Return error status */
0956         return HAL_ERROR;
0957       }
0958       /* Enable the TIM Output Compare DMA request */
0959       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
0960       break;
0961     }
0962 
0963     case TIM_CHANNEL_3:
0964     {
0965       /* Set the DMA compare callbacks */
0966       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
0967       htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
0968 
0969       /* Set the DMA error callback */
0970       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
0971 
0972       /* Enable the DMA stream */
0973       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
0974                            Length) != HAL_OK)
0975       {
0976         /* Return error status */
0977         return HAL_ERROR;
0978       }
0979       /* Enable the TIM Output Compare DMA request */
0980       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
0981       break;
0982     }
0983 
0984     default:
0985       status = HAL_ERROR;
0986       break;
0987   }
0988 
0989   if (status == HAL_OK)
0990   {
0991     /* Enable the Capture compare channel N */
0992     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
0993 
0994     /* Enable the Main Output */
0995     __HAL_TIM_MOE_ENABLE(htim);
0996 
0997     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
0998     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
0999     {
1000       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1001       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1002       {
1003         __HAL_TIM_ENABLE(htim);
1004       }
1005     }
1006     else
1007     {
1008       __HAL_TIM_ENABLE(htim);
1009     }
1010   }
1011 
1012   /* Return function status */
1013   return status;
1014 }
1015 
1016 /**
1017   * @brief  Stops the TIM Output Compare signal generation in DMA mode
1018   *         on the complementary output.
1019   * @param  htim TIM Output Compare handle
1020   * @param  Channel TIM Channel to be disabled
1021   *          This parameter can be one of the following values:
1022   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1023   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1024   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1025   * @retval HAL status
1026   */
1027 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1028 {
1029   HAL_StatusTypeDef status = HAL_OK;
1030 
1031   /* Check the parameters */
1032   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1033 
1034   switch (Channel)
1035   {
1036     case TIM_CHANNEL_1:
1037     {
1038       /* Disable the TIM Output Compare DMA request */
1039       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1040       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1041       break;
1042     }
1043 
1044     case TIM_CHANNEL_2:
1045     {
1046       /* Disable the TIM Output Compare DMA request */
1047       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1048       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1049       break;
1050     }
1051 
1052     case TIM_CHANNEL_3:
1053     {
1054       /* Disable the TIM Output Compare DMA request */
1055       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1056       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1057       break;
1058     }
1059 
1060     default:
1061       status = HAL_ERROR;
1062       break;
1063   }
1064 
1065   if (status == HAL_OK)
1066   {
1067     /* Disable the Capture compare channel N */
1068     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1069 
1070     /* Disable the Main Output */
1071     __HAL_TIM_MOE_DISABLE(htim);
1072 
1073     /* Disable the Peripheral */
1074     __HAL_TIM_DISABLE(htim);
1075 
1076     /* Set the TIM complementary channel state */
1077     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1078   }
1079 
1080   /* Return function status */
1081   return status;
1082 }
1083 
1084 /**
1085   * @}
1086   */
1087 
1088 /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
1089   * @ingroup RTEMSBSPsARMSTM32H7
1090   * @brief    Timer Complementary PWM functions
1091   *
1092 @verbatim
1093   ==============================================================================
1094                  ##### Timer Complementary PWM functions #####
1095   ==============================================================================
1096   [..]
1097     This section provides functions allowing to:
1098     (+) Start the Complementary PWM.
1099     (+) Stop the Complementary PWM.
1100     (+) Start the Complementary PWM and enable interrupts.
1101     (+) Stop the Complementary PWM and disable interrupts.
1102     (+) Start the Complementary PWM and enable DMA transfers.
1103     (+) Stop the Complementary PWM and disable DMA transfers.
1104 @endverbatim
1105   * @{
1106   */
1107 
1108 /**
1109   * @brief  Starts the PWM signal generation on the complementary output.
1110   * @param  htim TIM handle
1111   * @param  Channel TIM Channel to be enabled
1112   *          This parameter can be one of the following values:
1113   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1114   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1115   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1116   * @retval HAL status
1117   */
1118 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
1119 {
1120   uint32_t tmpsmcr;
1121 
1122   /* Check the parameters */
1123   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1124 
1125   /* Check the TIM complementary channel state */
1126   if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
1127   {
1128     return HAL_ERROR;
1129   }
1130 
1131   /* Set the TIM complementary channel state */
1132   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1133 
1134   /* Enable the complementary PWM output  */
1135   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1136 
1137   /* Enable the Main Output */
1138   __HAL_TIM_MOE_ENABLE(htim);
1139 
1140   /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1141   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1142   {
1143     tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1144     if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1145     {
1146       __HAL_TIM_ENABLE(htim);
1147     }
1148   }
1149   else
1150   {
1151     __HAL_TIM_ENABLE(htim);
1152   }
1153 
1154   /* Return function status */
1155   return HAL_OK;
1156 }
1157 
1158 /**
1159   * @brief  Stops the PWM signal generation on the complementary output.
1160   * @param  htim TIM handle
1161   * @param  Channel TIM Channel to be disabled
1162   *          This parameter can be one of the following values:
1163   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1164   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1165   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1166   * @retval HAL status
1167   */
1168 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
1169 {
1170   /* Check the parameters */
1171   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1172 
1173   /* Disable the complementary PWM output  */
1174   TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1175 
1176   /* Disable the Main Output */
1177   __HAL_TIM_MOE_DISABLE(htim);
1178 
1179   /* Disable the Peripheral */
1180   __HAL_TIM_DISABLE(htim);
1181 
1182   /* Set the TIM complementary channel state */
1183   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1184 
1185   /* Return function status */
1186   return HAL_OK;
1187 }
1188 
1189 /**
1190   * @brief  Starts the PWM signal generation in interrupt mode on the
1191   *         complementary output.
1192   * @param  htim TIM handle
1193   * @param  Channel TIM Channel to be disabled
1194   *          This parameter can be one of the following values:
1195   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1196   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1197   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1198   * @retval HAL status
1199   */
1200 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1201 {
1202   HAL_StatusTypeDef status = HAL_OK;
1203   uint32_t tmpsmcr;
1204 
1205   /* Check the parameters */
1206   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1207 
1208   /* Check the TIM complementary channel state */
1209   if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
1210   {
1211     return HAL_ERROR;
1212   }
1213 
1214   /* Set the TIM complementary channel state */
1215   TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1216 
1217   switch (Channel)
1218   {
1219     case TIM_CHANNEL_1:
1220     {
1221       /* Enable the TIM Capture/Compare 1 interrupt */
1222       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1223       break;
1224     }
1225 
1226     case TIM_CHANNEL_2:
1227     {
1228       /* Enable the TIM Capture/Compare 2 interrupt */
1229       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1230       break;
1231     }
1232 
1233     case TIM_CHANNEL_3:
1234     {
1235       /* Enable the TIM Capture/Compare 3 interrupt */
1236       __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1237       break;
1238     }
1239 
1240     default:
1241       status = HAL_ERROR;
1242       break;
1243   }
1244 
1245   if (status == HAL_OK)
1246   {
1247     /* Enable the TIM Break interrupt */
1248     __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
1249 
1250     /* Enable the complementary PWM output  */
1251     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1252 
1253     /* Enable the Main Output */
1254     __HAL_TIM_MOE_ENABLE(htim);
1255 
1256     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1257     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1258     {
1259       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1260       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1261       {
1262         __HAL_TIM_ENABLE(htim);
1263       }
1264     }
1265     else
1266     {
1267       __HAL_TIM_ENABLE(htim);
1268     }
1269   }
1270 
1271   /* Return function status */
1272   return status;
1273 }
1274 
1275 /**
1276   * @brief  Stops the PWM signal generation in interrupt mode on the
1277   *         complementary output.
1278   * @param  htim TIM handle
1279   * @param  Channel TIM Channel to be disabled
1280   *          This parameter can be one of the following values:
1281   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1282   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1283   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1284   * @retval HAL status
1285   */
1286 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1287 {
1288   HAL_StatusTypeDef status = HAL_OK;
1289   uint32_t tmpccer;
1290 
1291   /* Check the parameters */
1292   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1293 
1294   switch (Channel)
1295   {
1296     case TIM_CHANNEL_1:
1297     {
1298       /* Disable the TIM Capture/Compare 1 interrupt */
1299       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1300       break;
1301     }
1302 
1303     case TIM_CHANNEL_2:
1304     {
1305       /* Disable the TIM Capture/Compare 2 interrupt */
1306       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1307       break;
1308     }
1309 
1310     case TIM_CHANNEL_3:
1311     {
1312       /* Disable the TIM Capture/Compare 3 interrupt */
1313       __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1314       break;
1315     }
1316 
1317     default:
1318       status = HAL_ERROR;
1319       break;
1320   }
1321 
1322   if (status == HAL_OK)
1323   {
1324     /* Disable the complementary PWM output  */
1325     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1326 
1327     /* Disable the TIM Break interrupt (only if no more channel is active) */
1328     tmpccer = htim->Instance->CCER;
1329     if ((tmpccer & TIM_CCER_CCxNE_MASK) == (uint32_t)RESET)
1330     {
1331       __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
1332     }
1333 
1334     /* Disable the Main Output */
1335     __HAL_TIM_MOE_DISABLE(htim);
1336 
1337     /* Disable the Peripheral */
1338     __HAL_TIM_DISABLE(htim);
1339 
1340     /* Set the TIM complementary channel state */
1341     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1342   }
1343 
1344   /* Return function status */
1345   return status;
1346 }
1347 
1348 /**
1349   * @brief  Starts the TIM PWM signal generation in DMA mode on the
1350   *         complementary output
1351   * @param  htim TIM handle
1352   * @param  Channel TIM Channel to be enabled
1353   *          This parameter can be one of the following values:
1354   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1355   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1356   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1357   * @param  pData The source Buffer address.
1358   * @param  Length The length of data to be transferred from memory to TIM peripheral
1359   * @retval HAL status
1360   */
1361 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
1362                                            uint16_t Length)
1363 {
1364   HAL_StatusTypeDef status = HAL_OK;
1365   uint32_t tmpsmcr;
1366 
1367   /* Check the parameters */
1368   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1369 
1370   /* Set the TIM complementary channel state */
1371   if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
1372   {
1373     return HAL_BUSY;
1374   }
1375   else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
1376   {
1377     if ((pData == NULL) || (Length == 0U))
1378     {
1379       return HAL_ERROR;
1380     }
1381     else
1382     {
1383       TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1384     }
1385   }
1386   else
1387   {
1388     return HAL_ERROR;
1389   }
1390 
1391   switch (Channel)
1392   {
1393     case TIM_CHANNEL_1:
1394     {
1395       /* Set the DMA compare callbacks */
1396       htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
1397       htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1398 
1399       /* Set the DMA error callback */
1400       htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
1401 
1402       /* Enable the DMA stream */
1403       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
1404                            Length) != HAL_OK)
1405       {
1406         /* Return error status */
1407         return HAL_ERROR;
1408       }
1409       /* Enable the TIM Capture/Compare 1 DMA request */
1410       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1411       break;
1412     }
1413 
1414     case TIM_CHANNEL_2:
1415     {
1416       /* Set the DMA compare callbacks */
1417       htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
1418       htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1419 
1420       /* Set the DMA error callback */
1421       htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
1422 
1423       /* Enable the DMA stream */
1424       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
1425                            Length) != HAL_OK)
1426       {
1427         /* Return error status */
1428         return HAL_ERROR;
1429       }
1430       /* Enable the TIM Capture/Compare 2 DMA request */
1431       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1432       break;
1433     }
1434 
1435     case TIM_CHANNEL_3:
1436     {
1437       /* Set the DMA compare callbacks */
1438       htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
1439       htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1440 
1441       /* Set the DMA error callback */
1442       htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
1443 
1444       /* Enable the DMA stream */
1445       if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
1446                            Length) != HAL_OK)
1447       {
1448         /* Return error status */
1449         return HAL_ERROR;
1450       }
1451       /* Enable the TIM Capture/Compare 3 DMA request */
1452       __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1453       break;
1454     }
1455 
1456     default:
1457       status = HAL_ERROR;
1458       break;
1459   }
1460 
1461   if (status == HAL_OK)
1462   {
1463     /* Enable the complementary PWM output  */
1464     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1465 
1466     /* Enable the Main Output */
1467     __HAL_TIM_MOE_ENABLE(htim);
1468 
1469     /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1470     if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1471     {
1472       tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1473       if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1474       {
1475         __HAL_TIM_ENABLE(htim);
1476       }
1477     }
1478     else
1479     {
1480       __HAL_TIM_ENABLE(htim);
1481     }
1482   }
1483 
1484   /* Return function status */
1485   return status;
1486 }
1487 
1488 /**
1489   * @brief  Stops the TIM PWM signal generation in DMA mode on the complementary
1490   *         output
1491   * @param  htim TIM handle
1492   * @param  Channel TIM Channel to be disabled
1493   *          This parameter can be one of the following values:
1494   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1495   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1496   *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
1497   * @retval HAL status
1498   */
1499 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1500 {
1501   HAL_StatusTypeDef status = HAL_OK;
1502 
1503   /* Check the parameters */
1504   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1505 
1506   switch (Channel)
1507   {
1508     case TIM_CHANNEL_1:
1509     {
1510       /* Disable the TIM Capture/Compare 1 DMA request */
1511       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1512       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1513       break;
1514     }
1515 
1516     case TIM_CHANNEL_2:
1517     {
1518       /* Disable the TIM Capture/Compare 2 DMA request */
1519       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1520       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1521       break;
1522     }
1523 
1524     case TIM_CHANNEL_3:
1525     {
1526       /* Disable the TIM Capture/Compare 3 DMA request */
1527       __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1528       (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1529       break;
1530     }
1531 
1532     default:
1533       status = HAL_ERROR;
1534       break;
1535   }
1536 
1537   if (status == HAL_OK)
1538   {
1539     /* Disable the complementary PWM output */
1540     TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1541 
1542     /* Disable the Main Output */
1543     __HAL_TIM_MOE_DISABLE(htim);
1544 
1545     /* Disable the Peripheral */
1546     __HAL_TIM_DISABLE(htim);
1547 
1548     /* Set the TIM complementary channel state */
1549     TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1550   }
1551 
1552   /* Return function status */
1553   return status;
1554 }
1555 
1556 /**
1557   * @}
1558   */
1559 
1560 /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
1561   * @ingroup RTEMSBSPsARMSTM32H7
1562   * @brief    Timer Complementary One Pulse functions
1563   *
1564 @verbatim
1565   ==============================================================================
1566                 ##### Timer Complementary One Pulse functions #####
1567   ==============================================================================
1568   [..]
1569     This section provides functions allowing to:
1570     (+) Start the Complementary One Pulse generation.
1571     (+) Stop the Complementary One Pulse.
1572     (+) Start the Complementary One Pulse and enable interrupts.
1573     (+) Stop the Complementary One Pulse and disable interrupts.
1574 
1575 @endverbatim
1576   * @{
1577   */
1578 
1579 /**
1580   * @brief  Starts the TIM One Pulse signal generation on the complementary
1581   *         output.
1582   * @note OutputChannel must match the pulse output channel chosen when calling
1583   *       @ref HAL_TIM_OnePulse_ConfigChannel().
1584   * @param  htim TIM One Pulse handle
1585   * @param  OutputChannel pulse output channel to enable
1586   *          This parameter can be one of the following values:
1587   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1588   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1589   * @retval HAL status
1590   */
1591 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1592 {
1593   uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
1594   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
1595   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
1596   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
1597   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
1598 
1599   /* Check the parameters */
1600   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1601 
1602   /* Check the TIM channels state */
1603   if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
1604       || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
1605       || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
1606       || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
1607   {
1608     return HAL_ERROR;
1609   }
1610 
1611   /* Set the TIM channels state */
1612   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
1613   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
1614   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
1615   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
1616 
1617   /* Enable the complementary One Pulse output channel and the Input Capture channel */
1618   TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
1619   TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
1620 
1621   /* Enable the Main Output */
1622   __HAL_TIM_MOE_ENABLE(htim);
1623 
1624   /* Return function status */
1625   return HAL_OK;
1626 }
1627 
1628 /**
1629   * @brief  Stops the TIM One Pulse signal generation on the complementary
1630   *         output.
1631   * @note OutputChannel must match the pulse output channel chosen when calling
1632   *       @ref HAL_TIM_OnePulse_ConfigChannel().
1633   * @param  htim TIM One Pulse handle
1634   * @param  OutputChannel pulse output channel to disable
1635   *          This parameter can be one of the following values:
1636   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1637   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1638   * @retval HAL status
1639   */
1640 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1641 {
1642   uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
1643 
1644   /* Check the parameters */
1645   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1646 
1647   /* Disable the complementary One Pulse output channel and the Input Capture channel */
1648   TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
1649   TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
1650 
1651   /* Disable the Main Output */
1652   __HAL_TIM_MOE_DISABLE(htim);
1653 
1654   /* Disable the Peripheral */
1655   __HAL_TIM_DISABLE(htim);
1656 
1657   /* Set the TIM  channels state */
1658   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
1659   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
1660   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
1661   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
1662 
1663   /* Return function status */
1664   return HAL_OK;
1665 }
1666 
1667 /**
1668   * @brief  Starts the TIM One Pulse signal generation in interrupt mode on the
1669   *         complementary channel.
1670   * @note OutputChannel must match the pulse output channel chosen when calling
1671   *       @ref HAL_TIM_OnePulse_ConfigChannel().
1672   * @param  htim TIM One Pulse handle
1673   * @param  OutputChannel pulse output channel to enable
1674   *          This parameter can be one of the following values:
1675   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1676   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1677   * @retval HAL status
1678   */
1679 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1680 {
1681   uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
1682   HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
1683   HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
1684   HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
1685   HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
1686 
1687   /* Check the parameters */
1688   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1689 
1690   /* Check the TIM channels state */
1691   if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
1692       || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
1693       || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
1694       || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
1695   {
1696     return HAL_ERROR;
1697   }
1698 
1699   /* Set the TIM channels state */
1700   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
1701   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
1702   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
1703   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
1704 
1705   /* Enable the TIM Capture/Compare 1 interrupt */
1706   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1707 
1708   /* Enable the TIM Capture/Compare 2 interrupt */
1709   __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1710 
1711   /* Enable the complementary One Pulse output channel and the Input Capture channel */
1712   TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
1713   TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
1714 
1715   /* Enable the Main Output */
1716   __HAL_TIM_MOE_ENABLE(htim);
1717 
1718   /* Return function status */
1719   return HAL_OK;
1720 }
1721 
1722 /**
1723   * @brief  Stops the TIM One Pulse signal generation in interrupt mode on the
1724   *         complementary channel.
1725   * @note OutputChannel must match the pulse output channel chosen when calling
1726   *       @ref HAL_TIM_OnePulse_ConfigChannel().
1727   * @param  htim TIM One Pulse handle
1728   * @param  OutputChannel pulse output channel to disable
1729   *          This parameter can be one of the following values:
1730   *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
1731   *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
1732   * @retval HAL status
1733   */
1734 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1735 {
1736   uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
1737 
1738   /* Check the parameters */
1739   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1740 
1741   /* Disable the TIM Capture/Compare 1 interrupt */
1742   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1743 
1744   /* Disable the TIM Capture/Compare 2 interrupt */
1745   __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1746 
1747   /* Disable the complementary One Pulse output channel and the Input Capture channel */
1748   TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
1749   TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
1750 
1751   /* Disable the Main Output */
1752   __HAL_TIM_MOE_DISABLE(htim);
1753 
1754   /* Disable the Peripheral */
1755   __HAL_TIM_DISABLE(htim);
1756 
1757   /* Set the TIM  channels state */
1758   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
1759   TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
1760   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
1761   TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
1762 
1763   /* Return function status */
1764   return HAL_OK;
1765 }
1766 
1767 /**
1768   * @}
1769   */
1770 
1771 /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
1772   * @ingroup RTEMSBSPsARMSTM32H7
1773   * @brief    Peripheral Control functions
1774   *
1775 @verbatim
1776   ==============================================================================
1777                     ##### Peripheral Control functions #####
1778   ==============================================================================
1779   [..]
1780     This section provides functions allowing to:
1781       (+) Configure the commutation event in case of use of the Hall sensor interface.
1782       (+) Configure Output channels for OC and PWM mode.
1783 
1784       (+) Configure Complementary channels, break features and dead time.
1785       (+) Configure Master synchronization.
1786       (+) Configure timer remapping capabilities.
1787       (+) Select timer input source.
1788       (+) Enable or disable channel grouping.
1789 
1790 @endverbatim
1791   * @{
1792   */
1793 
1794 /**
1795   * @brief  Configure the TIM commutation event sequence.
1796   * @note  This function is mandatory to use the commutation event in order to
1797   *        update the configuration at each commutation detection on the TRGI input of the Timer,
1798   *        the typical use of this feature is with the use of another Timer(interface Timer)
1799   *        configured in Hall sensor interface, this interface Timer will generate the
1800   *        commutation at its TRGO output (connected to Timer used in this function) each time
1801   *        the TI1 of the Interface Timer detect a commutation at its input TI1.
1802   * @param  htim TIM handle
1803   * @param  InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
1804   *          This parameter can be one of the following values:
1805   *            @arg TIM_TS_ITR0: Internal trigger 0 selected
1806   *            @arg TIM_TS_ITR1: Internal trigger 1 selected
1807   *            @arg TIM_TS_ITR2: Internal trigger 2 selected
1808   *            @arg TIM_TS_ITR3: Internal trigger 3 selected
1809   *            @arg TIM_TS_ITR12: Internal trigger 12 selected (*)
1810   *            @arg TIM_TS_ITR13: Internal trigger 13 selected (*)
1811   *            @arg TIM_TS_NONE: No trigger is needed
1812   *
1813   *         (*)  Value not defined in all devices.
1814   *
1815   * @param  CommutationSource the Commutation Event source
1816   *          This parameter can be one of the following values:
1817   *            @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
1818   *            @arg TIM_COMMUTATION_SOFTWARE:  Commutation source is set by software using the COMG bit
1819   * @retval HAL status
1820   */
1821 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t  InputTrigger,
1822                                               uint32_t  CommutationSource)
1823 {
1824   /* Check the parameters */
1825   assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
1826   assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
1827 
1828   __HAL_LOCK(htim);
1829 
1830   if ((InputTrigger == TIM_TS_ITR0)  || (InputTrigger == TIM_TS_ITR1) ||
1831       (InputTrigger == TIM_TS_ITR2)  || (InputTrigger == TIM_TS_ITR3) ||
1832       (InputTrigger == TIM_TS_ITR12)  || (InputTrigger == TIM_TS_ITR13))
1833   {
1834     /* Select the Input trigger */
1835     htim->Instance->SMCR &= ~TIM_SMCR_TS;
1836     htim->Instance->SMCR |= InputTrigger;
1837   }
1838 
1839   /* Select the Capture Compare preload feature */
1840   htim->Instance->CR2 |= TIM_CR2_CCPC;
1841   /* Select the Commutation event source */
1842   htim->Instance->CR2 &= ~TIM_CR2_CCUS;
1843   htim->Instance->CR2 |= CommutationSource;
1844 
1845   /* Disable Commutation Interrupt */
1846   __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
1847 
1848   /* Disable Commutation DMA request */
1849   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
1850 
1851   __HAL_UNLOCK(htim);
1852 
1853   return HAL_OK;
1854 }
1855 
1856 /**
1857   * @brief  Configure the TIM commutation event sequence with interrupt.
1858   * @note  This function is mandatory to use the commutation event in order to
1859   *        update the configuration at each commutation detection on the TRGI input of the Timer,
1860   *        the typical use of this feature is with the use of another Timer(interface Timer)
1861   *        configured in Hall sensor interface, this interface Timer will generate the
1862   *        commutation at its TRGO output (connected to Timer used in this function) each time
1863   *        the TI1 of the Interface Timer detect a commutation at its input TI1.
1864   * @param  htim TIM handle
1865   * @param  InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
1866   *          This parameter can be one of the following values:
1867   *            @arg TIM_TS_ITR0: Internal trigger 0 selected
1868   *            @arg TIM_TS_ITR1: Internal trigger 1 selected
1869   *            @arg TIM_TS_ITR2: Internal trigger 2 selected
1870   *            @arg TIM_TS_ITR3: Internal trigger 3 selected
1871   *            @arg TIM_TS_ITR12: Internal trigger 12 selected (*)
1872   *            @arg TIM_TS_ITR13: Internal trigger 13 selected (*)
1873   *            @arg TIM_TS_NONE: No trigger is needed
1874   *
1875   *         (*)  Value not defined in all devices.
1876   *
1877   * @param  CommutationSource the Commutation Event source
1878   *          This parameter can be one of the following values:
1879   *            @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
1880   *            @arg TIM_COMMUTATION_SOFTWARE:  Commutation source is set by software using the COMG bit
1881   * @retval HAL status
1882   */
1883 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t  InputTrigger,
1884                                                  uint32_t  CommutationSource)
1885 {
1886   /* Check the parameters */
1887   assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
1888   assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
1889 
1890   __HAL_LOCK(htim);
1891 
1892   if ((InputTrigger == TIM_TS_ITR0)  || (InputTrigger == TIM_TS_ITR1) ||
1893       (InputTrigger == TIM_TS_ITR2)  || (InputTrigger == TIM_TS_ITR3) ||
1894       (InputTrigger == TIM_TS_ITR12)  || (InputTrigger == TIM_TS_ITR13))
1895   {
1896     /* Select the Input trigger */
1897     htim->Instance->SMCR &= ~TIM_SMCR_TS;
1898     htim->Instance->SMCR |= InputTrigger;
1899   }
1900 
1901   /* Select the Capture Compare preload feature */
1902   htim->Instance->CR2 |= TIM_CR2_CCPC;
1903   /* Select the Commutation event source */
1904   htim->Instance->CR2 &= ~TIM_CR2_CCUS;
1905   htim->Instance->CR2 |= CommutationSource;
1906 
1907   /* Disable Commutation DMA request */
1908   __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
1909 
1910   /* Enable the Commutation Interrupt */
1911   __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
1912 
1913   __HAL_UNLOCK(htim);
1914 
1915   return HAL_OK;
1916 }
1917 
1918 /**
1919   * @brief  Configure the TIM commutation event sequence with DMA.
1920   * @note  This function is mandatory to use the commutation event in order to
1921   *        update the configuration at each commutation detection on the TRGI input of the Timer,
1922   *        the typical use of this feature is with the use of another Timer(interface Timer)
1923   *        configured in Hall sensor interface, this interface Timer will generate the
1924   *        commutation at its TRGO output (connected to Timer used in this function) each time
1925   *        the TI1 of the Interface Timer detect a commutation at its input TI1.
1926   * @note  The user should configure the DMA in his own software, in This function only the COMDE bit is set
1927   * @param  htim TIM handle
1928   * @param  InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
1929   *          This parameter can be one of the following values:
1930   *            @arg TIM_TS_ITR0: Internal trigger 0 selected
1931   *            @arg TIM_TS_ITR1: Internal trigger 1 selected
1932   *            @arg TIM_TS_ITR2: Internal trigger 2 selected
1933   *            @arg TIM_TS_ITR3: Internal trigger 3 selected
1934   *            @arg TIM_TS_ITR12: Internal trigger 12 selected (*)
1935   *            @arg TIM_TS_ITR13: Internal trigger 13 selected (*)
1936   *            @arg TIM_TS_NONE: No trigger is needed
1937   *
1938   *         (*)  Value not defined in all devices.
1939   *
1940   * @param  CommutationSource the Commutation Event source
1941   *          This parameter can be one of the following values:
1942   *            @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
1943   *            @arg TIM_COMMUTATION_SOFTWARE:  Commutation source is set by software using the COMG bit
1944   * @retval HAL status
1945   */
1946 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t  InputTrigger,
1947                                                   uint32_t  CommutationSource)
1948 {
1949   /* Check the parameters */
1950   assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
1951   assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
1952 
1953   __HAL_LOCK(htim);
1954 
1955   if ((InputTrigger == TIM_TS_ITR0)  || (InputTrigger == TIM_TS_ITR1) ||
1956       (InputTrigger == TIM_TS_ITR2)  || (InputTrigger == TIM_TS_ITR3) ||
1957       (InputTrigger == TIM_TS_ITR12)  || (InputTrigger == TIM_TS_ITR13))
1958   {
1959     /* Select the Input trigger */
1960     htim->Instance->SMCR &= ~TIM_SMCR_TS;
1961     htim->Instance->SMCR |= InputTrigger;
1962   }
1963 
1964   /* Select the Capture Compare preload feature */
1965   htim->Instance->CR2 |= TIM_CR2_CCPC;
1966   /* Select the Commutation event source */
1967   htim->Instance->CR2 &= ~TIM_CR2_CCUS;
1968   htim->Instance->CR2 |= CommutationSource;
1969 
1970   /* Enable the Commutation DMA Request */
1971   /* Set the DMA Commutation Callback */
1972   htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
1973   htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
1974   /* Set the DMA error callback */
1975   htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
1976 
1977   /* Disable Commutation Interrupt */
1978   __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
1979 
1980   /* Enable the Commutation DMA Request */
1981   __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
1982 
1983   __HAL_UNLOCK(htim);
1984 
1985   return HAL_OK;
1986 }
1987 
1988 /**
1989   * @brief  Configures the TIM in master mode.
1990   * @param  htim TIM handle.
1991   * @param  sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
1992   *         contains the selected trigger output (TRGO) and the Master/Slave
1993   *         mode.
1994   * @retval HAL status
1995   */
1996 HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
1997                                                         const TIM_MasterConfigTypeDef *sMasterConfig)
1998 {
1999   uint32_t tmpcr2;
2000   uint32_t tmpsmcr;
2001 
2002   /* Check the parameters */
2003   assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
2004   assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
2005   assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
2006 
2007   /* Check input state */
2008   __HAL_LOCK(htim);
2009 
2010   /* Change the handler state */
2011   htim->State = HAL_TIM_STATE_BUSY;
2012 
2013   /* Get the TIMx CR2 register value */
2014   tmpcr2 = htim->Instance->CR2;
2015 
2016   /* Get the TIMx SMCR register value */
2017   tmpsmcr = htim->Instance->SMCR;
2018 
2019   /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
2020   if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
2021   {
2022     /* Check the parameters */
2023     assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
2024 
2025     /* Clear the MMS2 bits */
2026     tmpcr2 &= ~TIM_CR2_MMS2;
2027     /* Select the TRGO2 source*/
2028     tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
2029   }
2030 
2031   /* Reset the MMS Bits */
2032   tmpcr2 &= ~TIM_CR2_MMS;
2033   /* Select the TRGO source */
2034   tmpcr2 |=  sMasterConfig->MasterOutputTrigger;
2035 
2036   /* Update TIMx CR2 */
2037   htim->Instance->CR2 = tmpcr2;
2038 
2039   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
2040   {
2041     /* Reset the MSM Bit */
2042     tmpsmcr &= ~TIM_SMCR_MSM;
2043     /* Set master mode */
2044     tmpsmcr |= sMasterConfig->MasterSlaveMode;
2045 
2046     /* Update TIMx SMCR */
2047     htim->Instance->SMCR = tmpsmcr;
2048   }
2049 
2050   /* Change the htim state */
2051   htim->State = HAL_TIM_STATE_READY;
2052 
2053   __HAL_UNLOCK(htim);
2054 
2055   return HAL_OK;
2056 }
2057 
2058 /**
2059   * @brief  Configures the Break feature, dead time, Lock level, OSSI/OSSR State
2060   *         and the AOE(automatic output enable).
2061   * @param  htim TIM handle
2062   * @param  sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
2063   *         contains the BDTR Register configuration  information for the TIM peripheral.
2064   * @note   Interrupts can be generated when an active level is detected on the
2065   *         break input, the break 2 input or the system break input. Break
2066   *         interrupt can be enabled by calling the @ref __HAL_TIM_ENABLE_IT macro.
2067   * @retval HAL status
2068   */
2069 HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
2070                                                 const TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
2071 {
2072   /* Keep this variable initialized to 0 as it is used to configure BDTR register */
2073   uint32_t tmpbdtr = 0U;
2074 
2075   /* Check the parameters */
2076   assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2077   assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
2078   assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
2079   assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
2080   assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
2081   assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
2082   assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
2083   assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
2084   assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
2085 #if defined(TIM_BDTR_BKBID)
2086   assert_param(IS_TIM_BREAK_AFMODE(sBreakDeadTimeConfig->BreakAFMode));
2087 #endif /* TIM_BDTR_BKBID */
2088 
2089   /* Check input state */
2090   __HAL_LOCK(htim);
2091 
2092   /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
2093      the OSSI State, the dead time value and the Automatic Output Enable Bit */
2094 
2095   /* Set the BDTR bits */
2096   MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
2097   MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
2098   MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
2099   MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
2100   MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
2101   MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
2102   MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
2103   MODIFY_REG(tmpbdtr, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << TIM_BDTR_BKF_Pos));
2104 #if defined(TIM_BDTR_BKBID)
2105   MODIFY_REG(tmpbdtr, TIM_BDTR_BKBID, sBreakDeadTimeConfig->BreakAFMode);
2106 #endif /* TIM_BDTR_BKBID */
2107 
2108   if (IS_TIM_BKIN2_INSTANCE(htim->Instance))
2109   {
2110     /* Check the parameters */
2111     assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
2112     assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
2113     assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
2114 #if defined(TIM_BDTR_BKBID)
2115     assert_param(IS_TIM_BREAK2_AFMODE(sBreakDeadTimeConfig->Break2AFMode));
2116 #endif /* TIM_BDTR_BKBID */
2117 
2118     /* Set the BREAK2 input related BDTR bits */
2119     MODIFY_REG(tmpbdtr, TIM_BDTR_BK2F, (sBreakDeadTimeConfig->Break2Filter << TIM_BDTR_BK2F_Pos));
2120     MODIFY_REG(tmpbdtr, TIM_BDTR_BK2E, sBreakDeadTimeConfig->Break2State);
2121     MODIFY_REG(tmpbdtr, TIM_BDTR_BK2P, sBreakDeadTimeConfig->Break2Polarity);
2122 #if defined(TIM_BDTR_BKBID)
2123     MODIFY_REG(tmpbdtr, TIM_BDTR_BK2BID, sBreakDeadTimeConfig->Break2AFMode);
2124 #endif /* TIM_BDTR_BKBID */
2125   }
2126 
2127   /* Set TIMx_BDTR */
2128   htim->Instance->BDTR = tmpbdtr;
2129 
2130   __HAL_UNLOCK(htim);
2131 
2132   return HAL_OK;
2133 }
2134 #if defined(TIM_BREAK_INPUT_SUPPORT)
2135 
2136 /**
2137   * @brief  Configures the break input source.
2138   * @param  htim TIM handle.
2139   * @param  BreakInput Break input to configure
2140   *          This parameter can be one of the following values:
2141   *            @arg TIM_BREAKINPUT_BRK: Timer break input
2142   *            @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
2143   * @param  sBreakInputConfig Break input source configuration
2144   * @retval HAL status
2145   */
2146 HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim,
2147                                              uint32_t BreakInput,
2148                                              const TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
2149 {
2150   HAL_StatusTypeDef status = HAL_OK;
2151   uint32_t tmporx;
2152   uint32_t bkin_enable_mask;
2153   uint32_t bkin_polarity_mask;
2154   uint32_t bkin_enable_bitpos;
2155   uint32_t bkin_polarity_bitpos;
2156 
2157   /* Check the parameters */
2158   assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2159   assert_param(IS_TIM_BREAKINPUT(BreakInput));
2160   assert_param(IS_TIM_BREAKINPUTSOURCE(sBreakInputConfig->Source));
2161   assert_param(IS_TIM_BREAKINPUTSOURCE_STATE(sBreakInputConfig->Enable));
2162   if (sBreakInputConfig->Source != TIM_BREAKINPUTSOURCE_DFSDM1)
2163   {
2164     assert_param(IS_TIM_BREAKINPUTSOURCE_POLARITY(sBreakInputConfig->Polarity));
2165   }
2166 
2167   /* Check input state */
2168   __HAL_LOCK(htim);
2169 
2170   switch (sBreakInputConfig->Source)
2171   {
2172     case TIM_BREAKINPUTSOURCE_BKIN:
2173     {
2174       bkin_enable_mask = TIM1_AF1_BKINE;
2175       bkin_enable_bitpos = TIM1_AF1_BKINE_Pos;
2176       bkin_polarity_mask = TIM1_AF1_BKINP;
2177       bkin_polarity_bitpos = TIM1_AF1_BKINP_Pos;
2178       break;
2179     }
2180     case TIM_BREAKINPUTSOURCE_COMP1:
2181     {
2182       bkin_enable_mask = TIM1_AF1_BKCMP1E;
2183       bkin_enable_bitpos = TIM1_AF1_BKCMP1E_Pos;
2184       bkin_polarity_mask = TIM1_AF1_BKCMP1P;
2185       bkin_polarity_bitpos = TIM1_AF1_BKCMP1P_Pos;
2186       break;
2187     }
2188     case TIM_BREAKINPUTSOURCE_COMP2:
2189     {
2190       bkin_enable_mask = TIM1_AF1_BKCMP2E;
2191       bkin_enable_bitpos = TIM1_AF1_BKCMP2E_Pos;
2192       bkin_polarity_mask = TIM1_AF1_BKCMP2P;
2193       bkin_polarity_bitpos = TIM1_AF1_BKCMP2P_Pos;
2194       break;
2195     }
2196     case TIM_BREAKINPUTSOURCE_DFSDM1:
2197     {
2198       bkin_enable_mask = TIM1_AF1_BKDF1BK0E;
2199       bkin_enable_bitpos = TIM1_AF1_BKDF1BK0E_Pos;
2200       bkin_polarity_mask = 0U;
2201       bkin_polarity_bitpos = 0U;
2202       break;
2203     }
2204 
2205     default:
2206     {
2207       bkin_enable_mask = 0U;
2208       bkin_polarity_mask = 0U;
2209       bkin_enable_bitpos = 0U;
2210       bkin_polarity_bitpos = 0U;
2211       break;
2212     }
2213   }
2214 
2215   switch (BreakInput)
2216   {
2217     case TIM_BREAKINPUT_BRK:
2218     {
2219       /* Get the TIMx_AF1 register value */
2220       tmporx = htim->Instance->AF1;
2221 
2222       /* Enable the break input */
2223       tmporx &= ~bkin_enable_mask;
2224       tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
2225 
2226       /* Set the break input polarity */
2227       if (sBreakInputConfig->Source != TIM_BREAKINPUTSOURCE_DFSDM1)
2228       {
2229         tmporx &= ~bkin_polarity_mask;
2230         tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
2231       }
2232 
2233       /* Set TIMx_AF1 */
2234       htim->Instance->AF1 = tmporx;
2235       break;
2236     }
2237     case TIM_BREAKINPUT_BRK2:
2238     {
2239       /* Get the TIMx_AF2 register value */
2240       tmporx = htim->Instance->AF2;
2241 
2242       /* Enable the break input */
2243       tmporx &= ~bkin_enable_mask;
2244       tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
2245 
2246       /* Set the break input polarity */
2247       if (sBreakInputConfig->Source != TIM_BREAKINPUTSOURCE_DFSDM1)
2248       {
2249         tmporx &= ~bkin_polarity_mask;
2250         tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
2251       }
2252 
2253       /* Set TIMx_AF2 */
2254       htim->Instance->AF2 = tmporx;
2255       break;
2256     }
2257     default:
2258       status = HAL_ERROR;
2259       break;
2260   }
2261 
2262   __HAL_UNLOCK(htim);
2263 
2264   return status;
2265 }
2266 #endif /*TIM_BREAK_INPUT_SUPPORT */
2267 
2268 /**
2269   * @brief  Configures the TIMx Remapping input capabilities.
2270   * @param  htim TIM handle.
2271   * @param  Remap specifies the TIM remapping source.
2272   *         For TIM1, the parameter is one of the following values:
2273   *            @arg TIM_TIM1_ETR_GPIO:               TIM1_ETR is connected to GPIO
2274   *            @arg TIM_TIM1_ETR_COMP1:              TIM1_ETR is connected to COMP1 output
2275   *            @arg TIM_TIM1_ETR_COMP2:              TIM1_ETR is connected to COMP2 output
2276   *            @arg TIM_TIM1_ETR_ADC1_AWD1:          TIM1_ETR is connected to ADC1 AWD1
2277   *            @arg TIM_TIM1_ETR_ADC1_AWD2:          TIM1_ETR is connected to ADC1 AWD2
2278   *            @arg TIM_TIM1_ETR_ADC1_AWD3:          TIM1_ETR is connected to ADC1 AWD3
2279   *            @arg TIM_TIM1_ETR_ADC3_AWD1:          TIM1_ETR is connected to ADC3 AWD1
2280   *            @arg TIM_TIM1_ETR_ADC3_AWD2:          TIM1_ETR is connected to ADC3 AWD2
2281   *            @arg TIM_TIM1_ETR_ADC3_AWD3:          TIM1_ETR is connected to ADC3 AWD3
2282   *
2283   *         For TIM2, the parameter is one of the following values:
2284   *            @arg TIM_TIM2_ETR_GPIO:               TIM2_ETR is connected to GPIO
2285   *            @arg TIM_TIM2_ETR_COMP1:              TIM2_ETR is connected to COMP1 output
2286   *            @arg TIM_TIM2_ETR_COMP2:              TIM2_ETR is connected to COMP2 output
2287   *            @arg TIM_TIM2_ETR_LSE:                TIM2_ETR is connected to LSE
2288   *            @arg TIM_TIM2_ETR_SAI1_FSA:           TIM2_ETR is connected to SAI1 FS_A
2289   *            @arg TIM_TIM2_ETR_SAI1_FSB:           TIM2_ETR is connected to SAI1 FS_B
2290   *
2291   *         For TIM3, the parameter is one of the following values:
2292   *            @arg TIM_TIM3_ETR_GPIO:               TIM3_ETR is connected to GPIO
2293   *            @arg TIM_TIM3_ETR_COMP1:              TIM3_ETR is connected to COMP1 output
2294   *
2295   *         For TIM5, the parameter is one of the following values:
2296   *            @arg TIM_TIM5_ETR_GPIO:               TIM5_ETR is connected to GPIO
2297   *            @arg TIM_TIM5_ETR_SAI2_FSA:           TIM5_ETR is connected to SAI2 FS_A (*)
2298   *            @arg TIM_TIM5_ETR_SAI2_FSB:           TIM5_ETR is connected to SAI2 FS_B (*)
2299   *            @arg TIM_TIM5_ETR_SAI4_FSA:           TIM5_ETR is connected to SAI2 FS_A (*)
2300   *            @arg TIM_TIM5_ETR_SAI4_FSB:           TIM5_ETR is connected to SAI2 FS_B (*)
2301   *
2302   *         For TIM8, the parameter is one of the following values:
2303   *            @arg TIM_TIM8_ETR_GPIO:               TIM8_ETR is connected to GPIO
2304   *            @arg TIM_TIM8_ETR_COMP1:              TIM8_ETR is connected to COMP1 output
2305   *            @arg TIM_TIM8_ETR_COMP2:              TIM8_ETR is connected to COMP2 output
2306   *            @arg TIM_TIM8_ETR_ADC2_AWD1:          TIM8_ETR is connected to ADC2 AWD1
2307   *            @arg TIM_TIM8_ETR_ADC2_AWD2:          TIM8_ETR is connected to ADC2 AWD2
2308   *            @arg TIM_TIM8_ETR_ADC2_AWD3:          TIM8_ETR is connected to ADC2 AWD3
2309   *            @arg TIM_TIM8_ETR_ADC3_AWD1:          TIM8_ETR is connected to ADC3 AWD1
2310   *            @arg TIM_TIM8_ETR_ADC3_AWD2:          TIM8_ETR is connected to ADC3 AWD2
2311   *            @arg TIM_TIM8_ETR_ADC3_AWD3:          TIM8_ETR is connected to ADC3 AWD3
2312   *
2313   *         For TIM23, the parameter is one of the following values: (*)
2314   *            @arg TIM_TIM23_ETR_GPIO               TIM23_ETR is connected to GPIO
2315   *            @arg TIM_TIM23_ETR_COMP1              TIM23_ETR is connected to COMP1 output
2316   *            @arg TIM_TIM23_ETR_COMP2              TIM23_ETR is connected to COMP2 output
2317   *
2318   *         For TIM24, the parameter is one of the following values: (*)
2319   *           @arg TIM_TIM24_ETR_GPIO                TIM24_ETR is connected to GPIO
2320   *           @arg TIM_TIM24_ETR_SAI4_FSA            TIM24_ETR is connected to SAI4 FS_A
2321   *           @arg TIM_TIM24_ETR_SAI4_FSB            TIM24_ETR is connected to SAI4 FS_B
2322   *           @arg TIM_TIM24_ETR_SAI1_FSA            TIM24_ETR is connected to SAI1 FS_A
2323   *           @arg TIM_TIM24_ETR_SAI1_FSB            TIM24_ETR is connected to SAI1 FS_B
2324   *
2325   *         (*)  Value not defined in all devices.
2326   *
2327   * @retval HAL status
2328   */
2329 HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
2330 {
2331   /* Check parameters */
2332   assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
2333   assert_param(IS_TIM_REMAP(Remap));
2334 
2335   __HAL_LOCK(htim);
2336 
2337   MODIFY_REG(htim->Instance->AF1, TIM1_AF1_ETRSEL_Msk, Remap);
2338 
2339   __HAL_UNLOCK(htim);
2340 
2341   return HAL_OK;
2342 }
2343 
2344 /**
2345   * @brief  Select the timer input source
2346   * @param  htim TIM handle.
2347   * @param  Channel specifies the TIM Channel
2348   *          This parameter can be one of the following values:
2349   *            @arg TIM_CHANNEL_1: TI1 input channel
2350   *            @arg TIM_CHANNEL_2: TI2 input channel
2351   *            @arg TIM_CHANNEL_3: TIM Channel 3
2352   *            @arg TIM_CHANNEL_4: TIM Channel 4
2353   * @param  TISelection parameter of the TIM_TISelectionStruct structure is detailed as follows:
2354   *         For TIM1, the parameter is one of the following values:
2355   *            @arg TIM_TIM1_TI1_GPIO:                TIM1 TI1 is connected to GPIO
2356   *            @arg TIM_TIM1_TI1_COMP1:               TIM1 TI1 is connected to COMP1 output
2357   *
2358   *         For TIM2, the parameter is one of the following values:
2359   *            @arg TIM_TIM2_TI4_GPIO:                TIM2 TI4 is connected to GPIO
2360   *            @arg TIM_TIM2_TI4_COMP1:               TIM2 TI4 is connected to COMP1 output
2361   *            @arg TIM_TIM2_TI4_COMP2:               TIM2 TI4 is connected to COMP2 output
2362   *            @arg TIM_TIM2_TI4_COMP1_COMP2:         TIM2 TI4 is connected to logical OR between COMP1 and COMP2 output
2363   *
2364   *         For TIM3, the parameter is one of the following values:
2365   *            @arg TIM_TIM3_TI1_GPIO:                TIM3 TI1 is connected to GPIO
2366   *            @arg TIM_TIM3_TI1_COMP1:               TIM3 TI1 is connected to COMP1 output
2367   *            @arg TIM_TIM3_TI1_COMP2:               TIM3 TI1 is connected to COMP2 output
2368   *            @arg TIM_TIM3_TI1_COMP1_COMP2:         TIM3 TI1 is connected to logical OR between COMP1 and COMP2 output
2369   *
2370   *         For TIM5, the parameter is one of the following values:
2371   *            @arg TIM_TIM5_TI1_GPIO:                TIM5 TI1 is connected to GPIO
2372   *            @arg TIM_TIM5_TI1_CAN_TMP:             TIM5 TI1 is connected to CAN TMP
2373   *            @arg TIM_TIM5_TI1_CAN_RTP:             TIM5 TI1 is connected to CAN RTP
2374   *
2375   *         For TIM8, the parameter is one of the following values:
2376   *            @arg TIM_TIM8_TI1_GPIO:                TIM8 TI1 is connected to GPIO
2377   *            @arg TIM_TIM8_TI1_COMP2:               TIM8 TI1 is connected to COMP2 output
2378   *
2379   *         For TIM12, the parameter can have the following values: (*)
2380   *            @arg TIM_TIM12_TI1_GPIO:               TIM12 TI1 is connected to GPIO
2381   *            @arg TIM_TIM12_TI1_SPDIF_FS:           TIM12 TI1 is connected to SPDIF FS
2382   *
2383   *         For TIM15, the parameter is one of the following values:
2384   *            @arg TIM_TIM15_TI1_GPIO:               TIM15 TI1 is connected to GPIO
2385   *            @arg TIM_TIM15_TI1_TIM2_CH1:           TIM15 TI1 is connected to TIM2 CH1
2386   *            @arg TIM_TIM15_TI1_TIM3_CH1:           TIM15 TI1 is connected to TIM3 CH1
2387   *            @arg TIM_TIM15_TI1_TIM4_CH1:           TIM15 TI1 is connected to TIM4 CH1
2388   *            @arg TIM_TIM15_TI1_RCC_LSE:            TIM15 TI1 is connected to LSE
2389   *            @arg TIM_TIM15_TI1_RCC_CSI:            TIM15 TI1 is connected to CSI
2390   *            @arg TIM_TIM15_TI1_RCC_MCO2:           TIM15 TI1 is connected to MCO2
2391   *            @arg TIM_TIM15_TI2_GPIO:               TIM15 TI2 is connected to GPIO
2392   *            @arg TIM_TIM15_TI2_TIM2_CH2:           TIM15 TI2 is connected to TIM2 CH2
2393   *            @arg TIM_TIM15_TI2_TIM3_CH2:           TIM15 TI2 is connected to TIM3 CH2
2394   *            @arg TIM_TIM15_TI2_TIM4_CH2:           TIM15 TI2 is connected to TIM4 CH2
2395   *
2396   *         For TIM16, the parameter can have the following values:
2397   *            @arg TIM_TIM16_TI1_GPIO:               TIM16 TI1 is connected to GPIO
2398   *            @arg TIM_TIM16_TI1_RCC_LSI:            TIM16 TI1 is connected to LSI
2399   *            @arg TIM_TIM16_TI1_RCC_LSE:            TIM16 TI1 is connected to LSE
2400   *            @arg TIM_TIM16_TI1_WKUP_IT:            TIM16 TI1 is connected to RTC wakeup interrupt
2401   *
2402   *         For TIM17, the parameter can have the following values:
2403   *            @arg TIM_TIM17_TI1_GPIO:               TIM17 TI1 is connected to GPIO
2404   *            @arg TIM_TIM17_TI1_SPDIF_FS:           TIM17 TI1 is connected to SPDIF FS (*)
2405   *            @arg TIM_TIM17_TI1_RCC_HSE1MHZ:        TIM17 TI1 is connected to HSE 1MHz
2406   *            @arg TIM_TIM17_TI1_RCC_MCO1:           TIM17 TI1 is connected to MCO1
2407   *
2408   *         For TIM23, the parameter can have the following values: (*)
2409   *            @arg TIM_TIM23_TI4_GPIO                TIM23_TI4 is connected to GPIO
2410   *            @arg TIM_TIM23_TI4_COMP1               TIM23_TI4 is connected to COMP1 output
2411   *            @arg TIM_TIM23_TI4_COMP2               TIM23_TI4 is connected to COMP2 output
2412   *            @arg TIM_TIM23_TI4_COMP1_COMP2         TIM23_TI4 is connected to COMP2 output
2413   *
2414   *         For TIM24, the parameter can have the following values: (*)
2415   *            @arg TIM_TIM24_TI1_GPIO                TIM24_TI1 is connected to GPIO
2416   *            @arg TIM_TIM24_TI1_CAN_TMP             TIM24_TI1 is connected to CAN_TMP
2417   *            @arg TIM_TIM24_TI1_CAN_RTP             TIM24_TI1 is connected to CAN_RTP
2418   *            @arg TIM_TIM24_TI1_CAN_SOC             TIM24_TI1 is connected to CAN_SOC
2419   *
2420   *         (*)  Value not defined in all devices. \n
2421   * @retval HAL status
2422   */
2423 HAL_StatusTypeDef  HAL_TIMEx_TISelection(TIM_HandleTypeDef *htim, uint32_t TISelection, uint32_t Channel)
2424 {
2425   HAL_StatusTypeDef status = HAL_OK;
2426 
2427   /* Check parameters */
2428   assert_param(IS_TIM_TISEL_INSTANCE(htim->Instance));
2429   assert_param(IS_TIM_TISEL(TISelection));
2430 
2431   __HAL_LOCK(htim);
2432 
2433   switch (Channel)
2434   {
2435     case TIM_CHANNEL_1:
2436       MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI1SEL, TISelection);
2437       break;
2438     case TIM_CHANNEL_2:
2439       MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI2SEL, TISelection);
2440       break;
2441     case TIM_CHANNEL_3:
2442       MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI3SEL, TISelection);
2443       break;
2444     case TIM_CHANNEL_4:
2445       MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI4SEL, TISelection);
2446       break;
2447     default:
2448       status = HAL_ERROR;
2449       break;
2450   }
2451 
2452   __HAL_UNLOCK(htim);
2453 
2454   return status;
2455 }
2456 
2457 /**
2458   * @brief  Group channel 5 and channel 1, 2 or 3
2459   * @param  htim TIM handle.
2460   * @param  Channels specifies the reference signal(s) the OC5REF is combined with.
2461   *         This parameter can be any combination of the following values:
2462   *         TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
2463   *         TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
2464   *         TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
2465   *         TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
2466   * @retval HAL status
2467   */
2468 HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t Channels)
2469 {
2470   /* Check parameters */
2471   assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
2472   assert_param(IS_TIM_GROUPCH5(Channels));
2473 
2474   /* Process Locked */
2475   __HAL_LOCK(htim);
2476 
2477   htim->State = HAL_TIM_STATE_BUSY;
2478 
2479   /* Clear GC5Cx bit fields */
2480   htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3 | TIM_CCR5_GC5C2 | TIM_CCR5_GC5C1);
2481 
2482   /* Set GC5Cx bit fields */
2483   htim->Instance->CCR5 |= Channels;
2484 
2485   /* Change the htim state */
2486   htim->State = HAL_TIM_STATE_READY;
2487 
2488   __HAL_UNLOCK(htim);
2489 
2490   return HAL_OK;
2491 }
2492 #if defined(TIM_BDTR_BKBID)
2493 
2494 /**
2495   * @brief  Disarm the designated break input (when it operates in bidirectional mode).
2496   * @param  htim TIM handle.
2497   * @param  BreakInput Break input to disarm
2498   *          This parameter can be one of the following values:
2499   *            @arg TIM_BREAKINPUT_BRK: Timer break input
2500   *            @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
2501   * @note  The break input can be disarmed only when it is configured in
2502   *        bidirectional mode and when when MOE is reset.
2503   * @note  Purpose is to be able to have the input voltage back to high-state,
2504   *        whatever the time constant on the output .
2505   * @retval HAL status
2506   */
2507 HAL_StatusTypeDef HAL_TIMEx_DisarmBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput)
2508 {
2509   HAL_StatusTypeDef status = HAL_OK;
2510   uint32_t tmpbdtr;
2511 
2512   /* Check the parameters */
2513   assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2514   assert_param(IS_TIM_BREAKINPUT(BreakInput));
2515 
2516   switch (BreakInput)
2517   {
2518     case TIM_BREAKINPUT_BRK:
2519     {
2520       /* Check initial conditions */
2521       tmpbdtr = READ_REG(htim->Instance->BDTR);
2522       if ((READ_BIT(tmpbdtr, TIM_BDTR_BKBID) == TIM_BDTR_BKBID) &&
2523           (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
2524       {
2525         /* Break input BRK is disarmed */
2526         SET_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM);
2527       }
2528       break;
2529     }
2530     case TIM_BREAKINPUT_BRK2:
2531     {
2532       /* Check initial conditions */
2533       tmpbdtr = READ_REG(htim->Instance->BDTR);
2534       if ((READ_BIT(tmpbdtr, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID) &&
2535           (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
2536       {
2537         /* Break input BRK is disarmed */
2538         SET_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM);
2539       }
2540       break;
2541     }
2542     default:
2543       status = HAL_ERROR;
2544       break;
2545   }
2546 
2547   return status;
2548 }
2549 
2550 /**
2551   * @brief  Arm the designated break input (when it operates in bidirectional mode).
2552   * @param  htim TIM handle.
2553   * @param  BreakInput Break input to arm
2554   *          This parameter can be one of the following values:
2555   *            @arg TIM_BREAKINPUT_BRK: Timer break input
2556   *            @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
2557   * @note  Arming is possible at anytime, even if fault is present.
2558   * @note  Break input is automatically armed as soon as MOE bit is set.
2559   * @retval HAL status
2560   */
2561 HAL_StatusTypeDef HAL_TIMEx_ReArmBreakInput(const TIM_HandleTypeDef *htim, uint32_t BreakInput)
2562 {
2563   HAL_StatusTypeDef status = HAL_OK;
2564   uint32_t tickstart;
2565 
2566   /* Check the parameters */
2567   assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2568   assert_param(IS_TIM_BREAKINPUT(BreakInput));
2569 
2570   switch (BreakInput)
2571   {
2572     case TIM_BREAKINPUT_BRK:
2573     {
2574       /* Check initial conditions */
2575       if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKBID) == TIM_BDTR_BKBID)
2576       {
2577         /* Break input BRK is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
2578         /* Init tickstart for timeout management */
2579         tickstart = HAL_GetTick();
2580         while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
2581         {
2582           if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
2583           {
2584             /* New check to avoid false timeout detection in case of preemption */
2585             if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
2586             {
2587               return HAL_TIMEOUT;
2588             }
2589           }
2590         }
2591       }
2592       break;
2593     }
2594 
2595     case TIM_BREAKINPUT_BRK2:
2596     {
2597       /* Check initial conditions */
2598       if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID)
2599       {
2600         /* Break input BRK2 is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
2601         /* Init tickstart for timeout management */
2602         tickstart = HAL_GetTick();
2603         while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
2604         {
2605           if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
2606           {
2607             /* New check to avoid false timeout detection in case of preemption */
2608             if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
2609             {
2610               return HAL_TIMEOUT;
2611             }
2612           }
2613         }
2614       }
2615       break;
2616     }
2617     default:
2618       status = HAL_ERROR;
2619       break;
2620   }
2621 
2622   return status;
2623 }
2624 #endif /* TIM_BDTR_BKBID */
2625 
2626 /**
2627   * @}
2628   */
2629 
2630 /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
2631   * @ingroup RTEMSBSPsARMSTM32H7
2632   * @brief    Extended Callbacks functions
2633   *
2634 @verbatim
2635   ==============================================================================
2636                     ##### Extended Callbacks functions #####
2637   ==============================================================================
2638   [..]
2639     This section provides Extended TIM callback functions:
2640     (+) Timer Commutation callback
2641     (+) Timer Break callback
2642 
2643 @endverbatim
2644   * @{
2645   */
2646 
2647 /**
2648   * @brief  Commutation callback in non-blocking mode
2649   * @param  htim TIM handle
2650   * @retval None
2651   */
2652 __weak void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim)
2653 {
2654   /* Prevent unused argument(s) compilation warning */
2655   UNUSED(htim);
2656 
2657   /* NOTE : This function should not be modified, when the callback is needed,
2658             the HAL_TIMEx_CommutCallback could be implemented in the user file
2659    */
2660 }
2661 /**
2662   * @brief  Commutation half complete callback in non-blocking mode
2663   * @param  htim TIM handle
2664   * @retval None
2665   */
2666 __weak void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim)
2667 {
2668   /* Prevent unused argument(s) compilation warning */
2669   UNUSED(htim);
2670 
2671   /* NOTE : This function should not be modified, when the callback is needed,
2672             the HAL_TIMEx_CommutHalfCpltCallback could be implemented in the user file
2673    */
2674 }
2675 
2676 /**
2677   * @brief  Break detection callback in non-blocking mode
2678   * @param  htim TIM handle
2679   * @retval None
2680   */
2681 __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
2682 {
2683   /* Prevent unused argument(s) compilation warning */
2684   UNUSED(htim);
2685 
2686   /* NOTE : This function should not be modified, when the callback is needed,
2687             the HAL_TIMEx_BreakCallback could be implemented in the user file
2688    */
2689 }
2690 
2691 /**
2692   * @brief  Break2 detection callback in non blocking mode
2693   * @param  htim: TIM handle
2694   * @retval None
2695   */
2696 __weak void HAL_TIMEx_Break2Callback(TIM_HandleTypeDef *htim)
2697 {
2698   /* Prevent unused argument(s) compilation warning */
2699   UNUSED(htim);
2700 
2701   /* NOTE : This function Should not be modified, when the callback is needed,
2702             the HAL_TIMEx_Break2Callback could be implemented in the user file
2703    */
2704 }
2705 /**
2706   * @}
2707   */
2708 
2709 /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
2710   * @ingroup RTEMSBSPsARMSTM32H7
2711   * @brief    Extended Peripheral State functions
2712   *
2713 @verbatim
2714   ==============================================================================
2715                 ##### Extended Peripheral State functions #####
2716   ==============================================================================
2717   [..]
2718     This subsection permits to get in run-time the status of the peripheral
2719     and the data flow.
2720 
2721 @endverbatim
2722   * @{
2723   */
2724 
2725 /**
2726   * @brief  Return the TIM Hall Sensor interface handle state.
2727   * @param  htim TIM Hall Sensor handle
2728   * @retval HAL state
2729   */
2730 HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(const TIM_HandleTypeDef *htim)
2731 {
2732   return htim->State;
2733 }
2734 
2735 /**
2736   * @brief  Return actual state of the TIM complementary channel.
2737   * @param  htim TIM handle
2738   * @param  ChannelN TIM Complementary channel
2739   *          This parameter can be one of the following values:
2740   *            @arg TIM_CHANNEL_1: TIM Channel 1
2741   *            @arg TIM_CHANNEL_2: TIM Channel 2
2742   *            @arg TIM_CHANNEL_3: TIM Channel 3
2743   * @retval TIM Complementary channel state
2744   */
2745 HAL_TIM_ChannelStateTypeDef HAL_TIMEx_GetChannelNState(const TIM_HandleTypeDef *htim,  uint32_t ChannelN)
2746 {
2747   HAL_TIM_ChannelStateTypeDef channel_state;
2748 
2749   /* Check the parameters */
2750   assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, ChannelN));
2751 
2752   channel_state = TIM_CHANNEL_N_STATE_GET(htim, ChannelN);
2753 
2754   return channel_state;
2755 }
2756 /**
2757   * @}
2758   */
2759 
2760 /**
2761   * @}
2762   */
2763 
2764 /* Private functions ---------------------------------------------------------*/
2765 /** @defgroup TIMEx_Private_Functions TIM Extended Private Functions
2766   * @ingroup RTEMSBSPsARMSTM32H7
2767   * @{
2768   */
2769 
2770 /**
2771   * @brief  TIM DMA Commutation callback.
2772   * @param  hdma pointer to DMA handle.
2773   * @retval None
2774   */
2775 void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
2776 {
2777   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2778 
2779   /* Change the htim state */
2780   htim->State = HAL_TIM_STATE_READY;
2781 
2782 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2783   htim->CommutationCallback(htim);
2784 #else
2785   HAL_TIMEx_CommutCallback(htim);
2786 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2787 }
2788 
2789 /**
2790   * @brief  TIM DMA Commutation half complete callback.
2791   * @param  hdma pointer to DMA handle.
2792   * @retval None
2793   */
2794 void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma)
2795 {
2796   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2797 
2798   /* Change the htim state */
2799   htim->State = HAL_TIM_STATE_READY;
2800 
2801 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2802   htim->CommutationHalfCpltCallback(htim);
2803 #else
2804   HAL_TIMEx_CommutHalfCpltCallback(htim);
2805 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2806 }
2807 
2808 
2809 /**
2810   * @brief  TIM DMA Delay Pulse complete callback (complementary channel).
2811   * @param  hdma pointer to DMA handle.
2812   * @retval None
2813   */
2814 static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma)
2815 {
2816   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2817 
2818   if (hdma == htim->hdma[TIM_DMA_ID_CC1])
2819   {
2820     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
2821 
2822     if (hdma->Init.Mode == DMA_NORMAL)
2823     {
2824       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2825     }
2826   }
2827   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
2828   {
2829     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
2830 
2831     if (hdma->Init.Mode == DMA_NORMAL)
2832     {
2833       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2834     }
2835   }
2836   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
2837   {
2838     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
2839 
2840     if (hdma->Init.Mode == DMA_NORMAL)
2841     {
2842       TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
2843     }
2844   }
2845   else
2846   {
2847     /* nothing to do */
2848   }
2849 
2850 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2851   htim->PWM_PulseFinishedCallback(htim);
2852 #else
2853   HAL_TIM_PWM_PulseFinishedCallback(htim);
2854 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2855 
2856   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
2857 }
2858 
2859 /**
2860   * @brief  TIM DMA error callback (complementary channel)
2861   * @param  hdma pointer to DMA handle.
2862   * @retval None
2863   */
2864 static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma)
2865 {
2866   TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2867 
2868   if (hdma == htim->hdma[TIM_DMA_ID_CC1])
2869   {
2870     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
2871     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
2872   }
2873   else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
2874   {
2875     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
2876     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
2877   }
2878   else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
2879   {
2880     htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
2881     TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
2882   }
2883   else
2884   {
2885     /* nothing to do */
2886   }
2887 
2888 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
2889   htim->ErrorCallback(htim);
2890 #else
2891   HAL_TIM_ErrorCallback(htim);
2892 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
2893 
2894   htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
2895 }
2896 
2897 /**
2898   * @brief  Enables or disables the TIM Capture Compare Channel xN.
2899   * @param  TIMx to select the TIM peripheral
2900   * @param  Channel specifies the TIM Channel
2901   *          This parameter can be one of the following values:
2902   *            @arg TIM_CHANNEL_1: TIM Channel 1
2903   *            @arg TIM_CHANNEL_2: TIM Channel 2
2904   *            @arg TIM_CHANNEL_3: TIM Channel 3
2905   * @param  ChannelNState specifies the TIM Channel CCxNE bit new state.
2906   *          This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
2907   * @retval None
2908   */
2909 static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState)
2910 {
2911   uint32_t tmp;
2912 
2913   tmp = TIM_CCER_CC1NE << (Channel & 0xFU); /* 0xFU = 15 bits max shift */
2914 
2915   /* Reset the CCxNE Bit */
2916   TIMx->CCER &=  ~tmp;
2917 
2918   /* Set or reset the CCxNE Bit */
2919   TIMx->CCER |= (uint32_t)(ChannelNState << (Channel & 0xFU)); /* 0xFU = 15 bits max shift */
2920 }
2921 /**
2922   * @}
2923   */
2924 
2925 #endif /* HAL_TIM_MODULE_ENABLED */
2926 /**
2927   * @}
2928   */
2929 
2930 /**
2931   * @}
2932   */