You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2743 lines
90 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_tim_ex.c
  4. * @author MCD Application Team
  5. * @brief TIM HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Timer Extended peripheral:
  8. * + Time Hall Sensor Interface Initialization
  9. * + Time Hall Sensor Interface Start
  10. * + Time Complementary signal break and dead time configuration
  11. * + Time Master and Slave synchronization configuration
  12. * + Time Output Compare/PWM Channel Configuration (for channels 5 and 6)
  13. * + Time OCRef clear configuration
  14. * + Timer remapping capabilities configuration
  15. @verbatim
  16. ==============================================================================
  17. ##### TIMER Extended features #####
  18. ==============================================================================
  19. [..]
  20. The Timer Extended features include:
  21. (#) Complementary outputs with programmable dead-time for :
  22. (++) Output Compare
  23. (++) PWM generation (Edge and Center-aligned Mode)
  24. (++) One-pulse mode output
  25. (#) Synchronization circuit to control the timer with external signals and to
  26. interconnect several timers together.
  27. (#) Break input to put the timer output signals in reset state or in a known state.
  28. (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
  29. positioning purposes
  30. ##### How to use this driver #####
  31. ==============================================================================
  32. [..]
  33. (#) Initialize the TIM low level resources by implementing the following functions
  34. depending on the selected feature:
  35. (++) Hall Sensor output : HAL_TIMEx_HallSensor_MspInit()
  36. (#) Initialize the TIM low level resources :
  37. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  38. (##) TIM pins configuration
  39. (+++) Enable the clock for the TIM GPIOs using the following function:
  40. __HAL_RCC_GPIOx_CLK_ENABLE();
  41. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  42. (#) The external Clock can be configured, if needed (the default clock is the
  43. internal clock from the APBx), using the following function:
  44. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  45. any start function.
  46. (#) Configure the TIM in the desired functioning mode using one of the
  47. initialization function of this driver:
  48. (++) HAL_TIMEx_HallSensor_Init() and HAL_TIMEx_ConfigCommutEvent(): to use the
  49. Timer Hall Sensor Interface and the commutation event with the corresponding
  50. Interrupt and DMA request if needed (Note that One Timer is used to interface
  51. with the Hall sensor Interface and another Timer should be used to use
  52. the commutation event).
  53. (#) Activate the TIM peripheral using one of the start functions:
  54. (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(), HAL_TIMEx_OCN_Start_IT()
  55. (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(), HAL_TIMEx_PWMN_Start_IT()
  56. (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
  57. (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(), HAL_TIMEx_HallSensor_Start_IT().
  58. @endverbatim
  59. ******************************************************************************
  60. * @attention
  61. *
  62. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  63. * All rights reserved.</center></h2>
  64. *
  65. * This software component is licensed by ST under BSD 3-Clause license,
  66. * the "License"; You may not use this file except in compliance with the
  67. * License. You may obtain a copy of the License at:
  68. * opensource.org/licenses/BSD-3-Clause
  69. *
  70. ******************************************************************************
  71. */
  72. /* Includes ------------------------------------------------------------------*/
  73. #include "stm32wbxx_hal.h"
  74. /** @addtogroup STM32WBxx_HAL_Driver
  75. * @{
  76. */
  77. /** @defgroup TIMEx TIMEx
  78. * @brief TIM Extended HAL module driver
  79. * @{
  80. */
  81. #ifdef HAL_TIM_MODULE_ENABLED
  82. /* Private typedef -----------------------------------------------------------*/
  83. /* Private define ------------------------------------------------------------*/
  84. /* Private constants ---------------------------------------------------------*/
  85. /** @defgroup TIMEx_Private_Constants TIM Extended Private Constants
  86. * @{
  87. */
  88. /* Timeout for break input rearm */
  89. #define TIM_BREAKINPUT_REARM_TIMEOUT 5UL /* 5 milliseconds */
  90. /**
  91. * @}
  92. */
  93. /* End of private constants --------------------------------------------------*/
  94. /* Private macros ------------------------------------------------------------*/
  95. /** @addtogroup TIMEx_Private_Macros
  96. * @{
  97. */
  98. #if defined(STM32WB55xx) || defined(STM32WB5Mxx) || defined(STM32WB35xx)
  99. #define TIM_GET_OR_MASK(__INSTANCE__) \
  100. (((__INSTANCE__) == TIM1) ? (TIM1_OR_ETR_ADC1_RMP | TIM1_OR_TI1_RMP) : \
  101. ((__INSTANCE__) == TIM2) ? (TIM2_OR_TI4_RMP | TIM2_OR_ETR_RMP | TIM2_OR_ITR1_RMP) : \
  102. ((__INSTANCE__) == TIM16) ? TIM16_OR_TI1_RMP : TIM17_OR_TI1_RMP)
  103. #else
  104. #define TIM_GET_OR_MASK(__INSTANCE__) \
  105. (((__INSTANCE__) == TIM1) ? (TIM1_OR_ETR_ADC1_RMP | TIM1_OR_TI1_RMP) : \
  106. ((__INSTANCE__) == TIM2) ? TIM2_OR_ETR_RMP : \
  107. ((__INSTANCE__) == TIM16) ? TIM16_OR_TI1_RMP : TIM17_OR_TI1_RMP)
  108. #endif
  109. /**
  110. * @}
  111. */
  112. /* Private variables ---------------------------------------------------------*/
  113. /* Private function prototypes -----------------------------------------------*/
  114. static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma);
  115. static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma);
  116. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState);
  117. /* Exported functions --------------------------------------------------------*/
  118. /** @defgroup TIMEx_Exported_Functions TIM Extended Exported Functions
  119. * @{
  120. */
  121. /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions
  122. * @brief Timer Hall Sensor functions
  123. *
  124. @verbatim
  125. ==============================================================================
  126. ##### Timer Hall Sensor functions #####
  127. ==============================================================================
  128. [..]
  129. This section provides functions allowing to:
  130. (+) Initialize and configure TIM HAL Sensor.
  131. (+) De-initialize TIM HAL Sensor.
  132. (+) Start the Hall Sensor Interface.
  133. (+) Stop the Hall Sensor Interface.
  134. (+) Start the Hall Sensor Interface and enable interrupts.
  135. (+) Stop the Hall Sensor Interface and disable interrupts.
  136. (+) Start the Hall Sensor Interface and enable DMA transfers.
  137. (+) Stop the Hall Sensor Interface and disable DMA transfers.
  138. @endverbatim
  139. * @{
  140. */
  141. /**
  142. * @brief Initializes the TIM Hall Sensor Interface and initialize the associated handle.
  143. * @note When the timer instance is initialized in Hall Sensor Interface mode,
  144. * timer channels 1 and channel 2 are reserved and cannot be used for
  145. * other purpose.
  146. * @param htim TIM Hall Sensor Interface handle
  147. * @param sConfig TIM Hall Sensor configuration structure
  148. * @retval HAL status
  149. */
  150. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef *sConfig)
  151. {
  152. TIM_OC_InitTypeDef OC_Config;
  153. /* Check the TIM handle allocation */
  154. if (htim == NULL)
  155. {
  156. return HAL_ERROR;
  157. }
  158. /* Check the parameters */
  159. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  160. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  161. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  162. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  163. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  164. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  165. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  166. if (htim->State == HAL_TIM_STATE_RESET)
  167. {
  168. /* Allocate lock resource and initialize it */
  169. htim->Lock = HAL_UNLOCKED;
  170. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  171. /* Reset interrupt callbacks to legacy week callbacks */
  172. TIM_ResetCallback(htim);
  173. if (htim->HallSensor_MspInitCallback == NULL)
  174. {
  175. htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit;
  176. }
  177. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  178. htim->HallSensor_MspInitCallback(htim);
  179. #else
  180. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  181. HAL_TIMEx_HallSensor_MspInit(htim);
  182. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  183. }
  184. /* Set the TIM state */
  185. htim->State = HAL_TIM_STATE_BUSY;
  186. /* Configure the Time base in the Encoder Mode */
  187. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  188. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  189. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  190. /* Reset the IC1PSC Bits */
  191. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  192. /* Set the IC1PSC value */
  193. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  194. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  195. htim->Instance->CR2 |= TIM_CR2_TI1S;
  196. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  197. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  198. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  199. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  200. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  201. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  202. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  203. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  204. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  205. OC_Config.OCMode = TIM_OCMODE_PWM2;
  206. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  207. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  208. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  209. OC_Config.Pulse = sConfig->Commutation_Delay;
  210. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  211. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  212. register to 101 */
  213. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  214. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  215. /* Initialize the DMA burst operation state */
  216. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  217. /* Initialize the TIM channels state */
  218. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  219. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  220. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  221. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  222. /* Initialize the TIM state*/
  223. htim->State = HAL_TIM_STATE_READY;
  224. return HAL_OK;
  225. }
  226. /**
  227. * @brief DeInitializes the TIM Hall Sensor interface
  228. * @param htim TIM Hall Sensor Interface handle
  229. * @retval HAL status
  230. */
  231. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  232. {
  233. /* Check the parameters */
  234. assert_param(IS_TIM_INSTANCE(htim->Instance));
  235. htim->State = HAL_TIM_STATE_BUSY;
  236. /* Disable the TIM Peripheral Clock */
  237. __HAL_TIM_DISABLE(htim);
  238. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  239. if (htim->HallSensor_MspDeInitCallback == NULL)
  240. {
  241. htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit;
  242. }
  243. /* DeInit the low level hardware */
  244. htim->HallSensor_MspDeInitCallback(htim);
  245. #else
  246. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  247. HAL_TIMEx_HallSensor_MspDeInit(htim);
  248. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  249. /* Change the DMA burst operation state */
  250. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  251. /* Change the TIM channels state */
  252. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
  253. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
  254. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
  255. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
  256. /* Change TIM state */
  257. htim->State = HAL_TIM_STATE_RESET;
  258. /* Release Lock */
  259. __HAL_UNLOCK(htim);
  260. return HAL_OK;
  261. }
  262. /**
  263. * @brief Initializes the TIM Hall Sensor MSP.
  264. * @param htim TIM Hall Sensor Interface handle
  265. * @retval None
  266. */
  267. __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
  268. {
  269. /* Prevent unused argument(s) compilation warning */
  270. UNUSED(htim);
  271. /* NOTE : This function should not be modified, when the callback is needed,
  272. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  273. */
  274. }
  275. /**
  276. * @brief DeInitializes TIM Hall Sensor MSP.
  277. * @param htim TIM Hall Sensor Interface handle
  278. * @retval None
  279. */
  280. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  281. {
  282. /* Prevent unused argument(s) compilation warning */
  283. UNUSED(htim);
  284. /* NOTE : This function should not be modified, when the callback is needed,
  285. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  286. */
  287. }
  288. /**
  289. * @brief Starts the TIM Hall Sensor Interface.
  290. * @param htim TIM Hall Sensor Interface handle
  291. * @retval HAL status
  292. */
  293. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  294. {
  295. uint32_t tmpsmcr;
  296. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  297. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  298. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  299. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  300. /* Check the parameters */
  301. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  302. /* Check the TIM channels state */
  303. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  304. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  305. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  306. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  307. {
  308. return HAL_ERROR;
  309. }
  310. /* Set the TIM channels state */
  311. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  312. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  313. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  314. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  315. /* Enable the Input Capture channel 1
  316. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  317. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  318. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  319. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  320. {
  321. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  322. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  323. {
  324. __HAL_TIM_ENABLE(htim);
  325. }
  326. }
  327. else
  328. {
  329. __HAL_TIM_ENABLE(htim);
  330. }
  331. /* Return function status */
  332. return HAL_OK;
  333. }
  334. /**
  335. * @brief Stops the TIM Hall sensor Interface.
  336. * @param htim TIM Hall Sensor Interface handle
  337. * @retval HAL status
  338. */
  339. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  340. {
  341. /* Check the parameters */
  342. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  343. /* Disable the Input Capture channels 1, 2 and 3
  344. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  345. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  346. /* Disable the Peripheral */
  347. __HAL_TIM_DISABLE(htim);
  348. /* Set the TIM channels state */
  349. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  350. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  351. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  352. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  353. /* Return function status */
  354. return HAL_OK;
  355. }
  356. /**
  357. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  358. * @param htim TIM Hall Sensor Interface handle
  359. * @retval HAL status
  360. */
  361. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  362. {
  363. uint32_t tmpsmcr;
  364. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  365. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  366. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  367. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  368. /* Check the parameters */
  369. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  370. /* Check the TIM channels state */
  371. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  372. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  373. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  374. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  375. {
  376. return HAL_ERROR;
  377. }
  378. /* Set the TIM channels state */
  379. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  380. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  381. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  382. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  383. /* Enable the capture compare Interrupts 1 event */
  384. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  385. /* Enable the Input Capture channel 1
  386. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  387. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  388. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  389. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  390. {
  391. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  392. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  393. {
  394. __HAL_TIM_ENABLE(htim);
  395. }
  396. }
  397. else
  398. {
  399. __HAL_TIM_ENABLE(htim);
  400. }
  401. /* Return function status */
  402. return HAL_OK;
  403. }
  404. /**
  405. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  406. * @param htim TIM Hall Sensor Interface handle
  407. * @retval HAL status
  408. */
  409. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  410. {
  411. /* Check the parameters */
  412. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  413. /* Disable the Input Capture channel 1
  414. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  415. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  416. /* Disable the capture compare Interrupts event */
  417. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  418. /* Disable the Peripheral */
  419. __HAL_TIM_DISABLE(htim);
  420. /* Set the TIM channels state */
  421. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  422. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  423. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  424. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  425. /* Return function status */
  426. return HAL_OK;
  427. }
  428. /**
  429. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  430. * @param htim TIM Hall Sensor Interface handle
  431. * @param pData The destination Buffer address.
  432. * @param Length The length of data to be transferred from TIM peripheral to memory.
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  436. {
  437. uint32_t tmpsmcr;
  438. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  439. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  440. /* Check the parameters */
  441. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  442. /* Set the TIM channel state */
  443. if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
  444. || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY))
  445. {
  446. return HAL_BUSY;
  447. }
  448. else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
  449. && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY))
  450. {
  451. if ((pData == NULL) && (Length > 0U))
  452. {
  453. return HAL_ERROR;
  454. }
  455. else
  456. {
  457. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  458. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  459. }
  460. }
  461. else
  462. {
  463. return HAL_ERROR;
  464. }
  465. /* Enable the Input Capture channel 1
  466. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  467. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  468. /* Set the DMA Input Capture 1 Callbacks */
  469. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  470. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  471. /* Set the DMA error callback */
  472. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  473. /* Enable the DMA channel for Capture 1*/
  474. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
  475. {
  476. /* Return error status */
  477. return HAL_ERROR;
  478. }
  479. /* Enable the capture compare 1 Interrupt */
  480. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  481. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  482. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  483. {
  484. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  485. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  486. {
  487. __HAL_TIM_ENABLE(htim);
  488. }
  489. }
  490. else
  491. {
  492. __HAL_TIM_ENABLE(htim);
  493. }
  494. /* Return function status */
  495. return HAL_OK;
  496. }
  497. /**
  498. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  499. * @param htim TIM Hall Sensor Interface handle
  500. * @retval HAL status
  501. */
  502. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  503. {
  504. /* Check the parameters */
  505. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  506. /* Disable the Input Capture channel 1
  507. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  508. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  509. /* Disable the capture compare Interrupts 1 event */
  510. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  511. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  512. /* Disable the Peripheral */
  513. __HAL_TIM_DISABLE(htim);
  514. /* Set the TIM channel state */
  515. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  516. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  517. /* Return function status */
  518. return HAL_OK;
  519. }
  520. /**
  521. * @}
  522. */
  523. /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
  524. * @brief Timer Complementary Output Compare functions
  525. *
  526. @verbatim
  527. ==============================================================================
  528. ##### Timer Complementary Output Compare functions #####
  529. ==============================================================================
  530. [..]
  531. This section provides functions allowing to:
  532. (+) Start the Complementary Output Compare/PWM.
  533. (+) Stop the Complementary Output Compare/PWM.
  534. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  535. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  536. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  537. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  538. @endverbatim
  539. * @{
  540. */
  541. /**
  542. * @brief Starts the TIM Output Compare signal generation on the complementary
  543. * output.
  544. * @param htim TIM Output Compare handle
  545. * @param Channel TIM Channel to be enabled
  546. * This parameter can be one of the following values:
  547. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  548. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  549. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  550. * @retval HAL status
  551. */
  552. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  553. {
  554. uint32_t tmpsmcr;
  555. /* Check the parameters */
  556. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  557. /* Check the TIM complementary channel state */
  558. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  559. {
  560. return HAL_ERROR;
  561. }
  562. /* Set the TIM complementary channel state */
  563. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  564. /* Enable the Capture compare channel N */
  565. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  566. /* Enable the Main Output */
  567. __HAL_TIM_MOE_ENABLE(htim);
  568. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  569. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  570. {
  571. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  572. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  573. {
  574. __HAL_TIM_ENABLE(htim);
  575. }
  576. }
  577. else
  578. {
  579. __HAL_TIM_ENABLE(htim);
  580. }
  581. /* Return function status */
  582. return HAL_OK;
  583. }
  584. /**
  585. * @brief Stops the TIM Output Compare signal generation on the complementary
  586. * output.
  587. * @param htim TIM handle
  588. * @param Channel TIM Channel to be disabled
  589. * This parameter can be one of the following values:
  590. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  591. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  592. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  593. * @retval HAL status
  594. */
  595. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  596. {
  597. /* Check the parameters */
  598. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  599. /* Disable the Capture compare channel N */
  600. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  601. /* Disable the Main Output */
  602. __HAL_TIM_MOE_DISABLE(htim);
  603. /* Disable the Peripheral */
  604. __HAL_TIM_DISABLE(htim);
  605. /* Set the TIM complementary channel state */
  606. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  607. /* Return function status */
  608. return HAL_OK;
  609. }
  610. /**
  611. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  612. * on the complementary output.
  613. * @param htim TIM OC handle
  614. * @param Channel TIM Channel to be enabled
  615. * This parameter can be one of the following values:
  616. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  617. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  618. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  619. * @retval HAL status
  620. */
  621. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  622. {
  623. uint32_t tmpsmcr;
  624. /* Check the parameters */
  625. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  626. /* Check the TIM complementary channel state */
  627. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  628. {
  629. return HAL_ERROR;
  630. }
  631. /* Set the TIM complementary channel state */
  632. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  633. switch (Channel)
  634. {
  635. case TIM_CHANNEL_1:
  636. {
  637. /* Enable the TIM Output Compare interrupt */
  638. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  639. break;
  640. }
  641. case TIM_CHANNEL_2:
  642. {
  643. /* Enable the TIM Output Compare interrupt */
  644. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  645. break;
  646. }
  647. case TIM_CHANNEL_3:
  648. {
  649. /* Enable the TIM Output Compare interrupt */
  650. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  651. break;
  652. }
  653. default:
  654. break;
  655. }
  656. /* Enable the TIM Break interrupt */
  657. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  658. /* Enable the Capture compare channel N */
  659. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  660. /* Enable the Main Output */
  661. __HAL_TIM_MOE_ENABLE(htim);
  662. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  663. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  664. {
  665. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  666. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  667. {
  668. __HAL_TIM_ENABLE(htim);
  669. }
  670. }
  671. else
  672. {
  673. __HAL_TIM_ENABLE(htim);
  674. }
  675. /* Return function status */
  676. return HAL_OK;
  677. }
  678. /**
  679. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  680. * on the complementary output.
  681. * @param htim TIM Output Compare handle
  682. * @param Channel TIM Channel to be disabled
  683. * This parameter can be one of the following values:
  684. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  685. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  686. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  687. * @retval HAL status
  688. */
  689. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  690. {
  691. uint32_t tmpccer;
  692. /* Check the parameters */
  693. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  694. switch (Channel)
  695. {
  696. case TIM_CHANNEL_1:
  697. {
  698. /* Disable the TIM Output Compare interrupt */
  699. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  700. break;
  701. }
  702. case TIM_CHANNEL_2:
  703. {
  704. /* Disable the TIM Output Compare interrupt */
  705. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  706. break;
  707. }
  708. case TIM_CHANNEL_3:
  709. {
  710. /* Disable the TIM Output Compare interrupt */
  711. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  712. break;
  713. }
  714. default:
  715. break;
  716. }
  717. /* Disable the Capture compare channel N */
  718. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  719. /* Disable the TIM Break interrupt (only if no more channel is active) */
  720. tmpccer = htim->Instance->CCER;
  721. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == (uint32_t)RESET)
  722. {
  723. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  724. }
  725. /* Disable the Main Output */
  726. __HAL_TIM_MOE_DISABLE(htim);
  727. /* Disable the Peripheral */
  728. __HAL_TIM_DISABLE(htim);
  729. /* Set the TIM complementary channel state */
  730. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  731. /* Return function status */
  732. return HAL_OK;
  733. }
  734. /**
  735. * @brief Starts the TIM Output Compare signal generation in DMA mode
  736. * on the complementary output.
  737. * @param htim TIM Output Compare handle
  738. * @param Channel TIM Channel to be enabled
  739. * This parameter can be one of the following values:
  740. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  741. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  742. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  743. * @param pData The source Buffer address.
  744. * @param Length The length of data to be transferred from memory to TIM peripheral
  745. * @retval HAL status
  746. */
  747. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  748. {
  749. uint32_t tmpsmcr;
  750. /* Check the parameters */
  751. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  752. /* Set the TIM complementary channel state */
  753. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
  754. {
  755. return HAL_BUSY;
  756. }
  757. else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
  758. {
  759. if ((pData == NULL) && (Length > 0U))
  760. {
  761. return HAL_ERROR;
  762. }
  763. else
  764. {
  765. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  766. }
  767. }
  768. else
  769. {
  770. return HAL_ERROR;
  771. }
  772. switch (Channel)
  773. {
  774. case TIM_CHANNEL_1:
  775. {
  776. /* Set the DMA compare callbacks */
  777. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  778. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  779. /* Set the DMA error callback */
  780. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
  781. /* Enable the DMA channel */
  782. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
  783. {
  784. /* Return error status */
  785. return HAL_ERROR;
  786. }
  787. /* Enable the TIM Output Compare DMA request */
  788. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  789. break;
  790. }
  791. case TIM_CHANNEL_2:
  792. {
  793. /* Set the DMA compare callbacks */
  794. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  795. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  796. /* Set the DMA error callback */
  797. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
  798. /* Enable the DMA channel */
  799. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
  800. {
  801. /* Return error status */
  802. return HAL_ERROR;
  803. }
  804. /* Enable the TIM Output Compare DMA request */
  805. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  806. break;
  807. }
  808. case TIM_CHANNEL_3:
  809. {
  810. /* Set the DMA compare callbacks */
  811. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  812. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  813. /* Set the DMA error callback */
  814. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
  815. /* Enable the DMA channel */
  816. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
  817. {
  818. /* Return error status */
  819. return HAL_ERROR;
  820. }
  821. /* Enable the TIM Output Compare DMA request */
  822. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  823. break;
  824. }
  825. default:
  826. break;
  827. }
  828. /* Enable the Capture compare channel N */
  829. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  830. /* Enable the Main Output */
  831. __HAL_TIM_MOE_ENABLE(htim);
  832. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  833. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  834. {
  835. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  836. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  837. {
  838. __HAL_TIM_ENABLE(htim);
  839. }
  840. }
  841. else
  842. {
  843. __HAL_TIM_ENABLE(htim);
  844. }
  845. /* Return function status */
  846. return HAL_OK;
  847. }
  848. /**
  849. * @brief Stops the TIM Output Compare signal generation in DMA mode
  850. * on the complementary output.
  851. * @param htim TIM Output Compare handle
  852. * @param Channel TIM Channel to be disabled
  853. * This parameter can be one of the following values:
  854. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  855. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  856. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  857. * @retval HAL status
  858. */
  859. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  860. {
  861. /* Check the parameters */
  862. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  863. switch (Channel)
  864. {
  865. case TIM_CHANNEL_1:
  866. {
  867. /* Disable the TIM Output Compare DMA request */
  868. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  869. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  870. break;
  871. }
  872. case TIM_CHANNEL_2:
  873. {
  874. /* Disable the TIM Output Compare DMA request */
  875. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  876. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  877. break;
  878. }
  879. case TIM_CHANNEL_3:
  880. {
  881. /* Disable the TIM Output Compare DMA request */
  882. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  883. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  884. break;
  885. }
  886. default:
  887. break;
  888. }
  889. /* Disable the Capture compare channel N */
  890. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  891. /* Disable the Main Output */
  892. __HAL_TIM_MOE_DISABLE(htim);
  893. /* Disable the Peripheral */
  894. __HAL_TIM_DISABLE(htim);
  895. /* Set the TIM complementary channel state */
  896. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  897. /* Return function status */
  898. return HAL_OK;
  899. }
  900. /**
  901. * @}
  902. */
  903. /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
  904. * @brief Timer Complementary PWM functions
  905. *
  906. @verbatim
  907. ==============================================================================
  908. ##### Timer Complementary PWM functions #####
  909. ==============================================================================
  910. [..]
  911. This section provides functions allowing to:
  912. (+) Start the Complementary PWM.
  913. (+) Stop the Complementary PWM.
  914. (+) Start the Complementary PWM and enable interrupts.
  915. (+) Stop the Complementary PWM and disable interrupts.
  916. (+) Start the Complementary PWM and enable DMA transfers.
  917. (+) Stop the Complementary PWM and disable DMA transfers.
  918. (+) Start the Complementary Input Capture measurement.
  919. (+) Stop the Complementary Input Capture.
  920. (+) Start the Complementary Input Capture and enable interrupts.
  921. (+) Stop the Complementary Input Capture and disable interrupts.
  922. (+) Start the Complementary Input Capture and enable DMA transfers.
  923. (+) Stop the Complementary Input Capture and disable DMA transfers.
  924. (+) Start the Complementary One Pulse generation.
  925. (+) Stop the Complementary One Pulse.
  926. (+) Start the Complementary One Pulse and enable interrupts.
  927. (+) Stop the Complementary One Pulse and disable interrupts.
  928. @endverbatim
  929. * @{
  930. */
  931. /**
  932. * @brief Starts the PWM signal generation on the complementary output.
  933. * @param htim TIM handle
  934. * @param Channel TIM Channel to be enabled
  935. * This parameter can be one of the following values:
  936. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  937. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  938. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  939. * @retval HAL status
  940. */
  941. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  942. {
  943. uint32_t tmpsmcr;
  944. /* Check the parameters */
  945. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  946. /* Check the TIM complementary channel state */
  947. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  948. {
  949. return HAL_ERROR;
  950. }
  951. /* Set the TIM complementary channel state */
  952. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  953. /* Enable the complementary PWM output */
  954. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  955. /* Enable the Main Output */
  956. __HAL_TIM_MOE_ENABLE(htim);
  957. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  958. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  959. {
  960. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  961. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  962. {
  963. __HAL_TIM_ENABLE(htim);
  964. }
  965. }
  966. else
  967. {
  968. __HAL_TIM_ENABLE(htim);
  969. }
  970. /* Return function status */
  971. return HAL_OK;
  972. }
  973. /**
  974. * @brief Stops the PWM signal generation on the complementary output.
  975. * @param htim TIM handle
  976. * @param Channel TIM Channel to be disabled
  977. * This parameter can be one of the following values:
  978. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  979. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  980. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  981. * @retval HAL status
  982. */
  983. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  984. {
  985. /* Check the parameters */
  986. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  987. /* Disable the complementary PWM output */
  988. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  989. /* Disable the Main Output */
  990. __HAL_TIM_MOE_DISABLE(htim);
  991. /* Disable the Peripheral */
  992. __HAL_TIM_DISABLE(htim);
  993. /* Set the TIM complementary channel state */
  994. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  995. /* Return function status */
  996. return HAL_OK;
  997. }
  998. /**
  999. * @brief Starts the PWM signal generation in interrupt mode on the
  1000. * complementary output.
  1001. * @param htim TIM handle
  1002. * @param Channel TIM Channel to be disabled
  1003. * This parameter can be one of the following values:
  1004. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1005. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1006. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1007. * @retval HAL status
  1008. */
  1009. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1010. {
  1011. uint32_t tmpsmcr;
  1012. /* Check the parameters */
  1013. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1014. /* Check the TIM complementary channel state */
  1015. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  1016. {
  1017. return HAL_ERROR;
  1018. }
  1019. /* Set the TIM complementary channel state */
  1020. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1021. switch (Channel)
  1022. {
  1023. case TIM_CHANNEL_1:
  1024. {
  1025. /* Enable the TIM Capture/Compare 1 interrupt */
  1026. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1027. break;
  1028. }
  1029. case TIM_CHANNEL_2:
  1030. {
  1031. /* Enable the TIM Capture/Compare 2 interrupt */
  1032. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1033. break;
  1034. }
  1035. case TIM_CHANNEL_3:
  1036. {
  1037. /* Enable the TIM Capture/Compare 3 interrupt */
  1038. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1039. break;
  1040. }
  1041. default:
  1042. break;
  1043. }
  1044. /* Enable the TIM Break interrupt */
  1045. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  1046. /* Enable the complementary PWM output */
  1047. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1048. /* Enable the Main Output */
  1049. __HAL_TIM_MOE_ENABLE(htim);
  1050. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1051. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1052. {
  1053. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1054. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1055. {
  1056. __HAL_TIM_ENABLE(htim);
  1057. }
  1058. }
  1059. else
  1060. {
  1061. __HAL_TIM_ENABLE(htim);
  1062. }
  1063. /* Return function status */
  1064. return HAL_OK;
  1065. }
  1066. /**
  1067. * @brief Stops the PWM signal generation in interrupt mode on the
  1068. * complementary output.
  1069. * @param htim TIM handle
  1070. * @param Channel TIM Channel to be disabled
  1071. * This parameter can be one of the following values:
  1072. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1073. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1074. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1075. * @retval HAL status
  1076. */
  1077. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1078. {
  1079. uint32_t tmpccer;
  1080. /* Check the parameters */
  1081. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1082. switch (Channel)
  1083. {
  1084. case TIM_CHANNEL_1:
  1085. {
  1086. /* Disable the TIM Capture/Compare 1 interrupt */
  1087. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1088. break;
  1089. }
  1090. case TIM_CHANNEL_2:
  1091. {
  1092. /* Disable the TIM Capture/Compare 2 interrupt */
  1093. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1094. break;
  1095. }
  1096. case TIM_CHANNEL_3:
  1097. {
  1098. /* Disable the TIM Capture/Compare 3 interrupt */
  1099. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1100. break;
  1101. }
  1102. default:
  1103. break;
  1104. }
  1105. /* Disable the complementary PWM output */
  1106. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1107. /* Disable the TIM Break interrupt (only if no more channel is active) */
  1108. tmpccer = htim->Instance->CCER;
  1109. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == (uint32_t)RESET)
  1110. {
  1111. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  1112. }
  1113. /* Disable the Main Output */
  1114. __HAL_TIM_MOE_DISABLE(htim);
  1115. /* Disable the Peripheral */
  1116. __HAL_TIM_DISABLE(htim);
  1117. /* Set the TIM complementary channel state */
  1118. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1119. /* Return function status */
  1120. return HAL_OK;
  1121. }
  1122. /**
  1123. * @brief Starts the TIM PWM signal generation in DMA mode on the
  1124. * complementary output
  1125. * @param htim TIM handle
  1126. * @param Channel TIM Channel to be enabled
  1127. * This parameter can be one of the following values:
  1128. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1129. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1130. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1131. * @param pData The source Buffer address.
  1132. * @param Length The length of data to be transferred from memory to TIM peripheral
  1133. * @retval HAL status
  1134. */
  1135. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  1136. {
  1137. uint32_t tmpsmcr;
  1138. /* Check the parameters */
  1139. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1140. /* Set the TIM complementary channel state */
  1141. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
  1142. {
  1143. return HAL_BUSY;
  1144. }
  1145. else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
  1146. {
  1147. if ((pData == NULL) && (Length > 0U))
  1148. {
  1149. return HAL_ERROR;
  1150. }
  1151. else
  1152. {
  1153. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1154. }
  1155. }
  1156. else
  1157. {
  1158. return HAL_ERROR;
  1159. }
  1160. switch (Channel)
  1161. {
  1162. case TIM_CHANNEL_1:
  1163. {
  1164. /* Set the DMA compare callbacks */
  1165. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1166. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1167. /* Set the DMA error callback */
  1168. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1169. /* Enable the DMA channel */
  1170. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
  1171. {
  1172. /* Return error status */
  1173. return HAL_ERROR;
  1174. }
  1175. /* Enable the TIM Capture/Compare 1 DMA request */
  1176. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1177. break;
  1178. }
  1179. case TIM_CHANNEL_2:
  1180. {
  1181. /* Set the DMA compare callbacks */
  1182. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1183. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1184. /* Set the DMA error callback */
  1185. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1186. /* Enable the DMA channel */
  1187. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
  1188. {
  1189. /* Return error status */
  1190. return HAL_ERROR;
  1191. }
  1192. /* Enable the TIM Capture/Compare 2 DMA request */
  1193. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1194. break;
  1195. }
  1196. case TIM_CHANNEL_3:
  1197. {
  1198. /* Set the DMA compare callbacks */
  1199. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1200. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1201. /* Set the DMA error callback */
  1202. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1203. /* Enable the DMA channel */
  1204. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
  1205. {
  1206. /* Return error status */
  1207. return HAL_ERROR;
  1208. }
  1209. /* Enable the TIM Capture/Compare 3 DMA request */
  1210. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1211. break;
  1212. }
  1213. default:
  1214. break;
  1215. }
  1216. /* Enable the complementary PWM output */
  1217. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1218. /* Enable the Main Output */
  1219. __HAL_TIM_MOE_ENABLE(htim);
  1220. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1221. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1222. {
  1223. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1224. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1225. {
  1226. __HAL_TIM_ENABLE(htim);
  1227. }
  1228. }
  1229. else
  1230. {
  1231. __HAL_TIM_ENABLE(htim);
  1232. }
  1233. /* Return function status */
  1234. return HAL_OK;
  1235. }
  1236. /**
  1237. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  1238. * output
  1239. * @param htim TIM handle
  1240. * @param Channel TIM Channel to be disabled
  1241. * This parameter can be one of the following values:
  1242. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1243. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1244. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1245. * @retval HAL status
  1246. */
  1247. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1248. {
  1249. /* Check the parameters */
  1250. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1251. switch (Channel)
  1252. {
  1253. case TIM_CHANNEL_1:
  1254. {
  1255. /* Disable the TIM Capture/Compare 1 DMA request */
  1256. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1257. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1258. break;
  1259. }
  1260. case TIM_CHANNEL_2:
  1261. {
  1262. /* Disable the TIM Capture/Compare 2 DMA request */
  1263. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1264. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1265. break;
  1266. }
  1267. case TIM_CHANNEL_3:
  1268. {
  1269. /* Disable the TIM Capture/Compare 3 DMA request */
  1270. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1271. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1272. break;
  1273. }
  1274. default:
  1275. break;
  1276. }
  1277. /* Disable the complementary PWM output */
  1278. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1279. /* Disable the Main Output */
  1280. __HAL_TIM_MOE_DISABLE(htim);
  1281. /* Disable the Peripheral */
  1282. __HAL_TIM_DISABLE(htim);
  1283. /* Set the TIM complementary channel state */
  1284. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1285. /* Return function status */
  1286. return HAL_OK;
  1287. }
  1288. /**
  1289. * @}
  1290. */
  1291. /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
  1292. * @brief Timer Complementary One Pulse functions
  1293. *
  1294. @verbatim
  1295. ==============================================================================
  1296. ##### Timer Complementary One Pulse functions #####
  1297. ==============================================================================
  1298. [..]
  1299. This section provides functions allowing to:
  1300. (+) Start the Complementary One Pulse generation.
  1301. (+) Stop the Complementary One Pulse.
  1302. (+) Start the Complementary One Pulse and enable interrupts.
  1303. (+) Stop the Complementary One Pulse and disable interrupts.
  1304. @endverbatim
  1305. * @{
  1306. */
  1307. /**
  1308. * @brief Starts the TIM One Pulse signal generation on the complementary
  1309. * output.
  1310. * @param htim TIM One Pulse handle
  1311. * @param OutputChannel TIM Channel to be enabled
  1312. * This parameter can be one of the following values:
  1313. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1314. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1315. * @retval HAL status
  1316. */
  1317. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1318. {
  1319. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1320. HAL_TIM_ChannelStateTypeDef input_channel_state = TIM_CHANNEL_STATE_GET(htim, input_channel);
  1321. HAL_TIM_ChannelStateTypeDef output_channel_state = TIM_CHANNEL_N_STATE_GET(htim, OutputChannel);
  1322. /* Check the parameters */
  1323. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1324. /* Check the TIM channels state */
  1325. if ((output_channel_state != HAL_TIM_CHANNEL_STATE_READY)
  1326. || (input_channel_state != HAL_TIM_CHANNEL_STATE_READY))
  1327. {
  1328. return HAL_ERROR;
  1329. }
  1330. /* Set the TIM channels state */
  1331. TIM_CHANNEL_N_STATE_SET(htim, OutputChannel, HAL_TIM_CHANNEL_STATE_BUSY);
  1332. TIM_CHANNEL_STATE_SET(htim, input_channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1333. /* Enable the complementary One Pulse output channel and the Input Capture channel */
  1334. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1335. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
  1336. /* Enable the Main Output */
  1337. __HAL_TIM_MOE_ENABLE(htim);
  1338. /* Return function status */
  1339. return HAL_OK;
  1340. }
  1341. /**
  1342. * @brief Stops the TIM One Pulse signal generation on the complementary
  1343. * output.
  1344. * @param htim TIM One Pulse handle
  1345. * @param OutputChannel TIM Channel to be disabled
  1346. * This parameter can be one of the following values:
  1347. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1348. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1349. * @retval HAL status
  1350. */
  1351. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1352. {
  1353. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1354. /* Check the parameters */
  1355. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1356. /* Disable the complementary One Pulse output channel and the Input Capture channel */
  1357. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1358. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
  1359. /* Disable the Main Output */
  1360. __HAL_TIM_MOE_DISABLE(htim);
  1361. /* Disable the Peripheral */
  1362. __HAL_TIM_DISABLE(htim);
  1363. /* Set the TIM channels state */
  1364. TIM_CHANNEL_N_STATE_SET(htim, OutputChannel, HAL_TIM_CHANNEL_STATE_READY);
  1365. TIM_CHANNEL_STATE_SET(htim, input_channel, HAL_TIM_CHANNEL_STATE_READY);
  1366. /* Return function status */
  1367. return HAL_OK;
  1368. }
  1369. /**
  1370. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1371. * complementary channel.
  1372. * @param htim TIM One Pulse handle
  1373. * @param OutputChannel TIM Channel to be enabled
  1374. * This parameter can be one of the following values:
  1375. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1376. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1377. * @retval HAL status
  1378. */
  1379. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1380. {
  1381. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1382. HAL_TIM_ChannelStateTypeDef input_channel_state = TIM_CHANNEL_STATE_GET(htim, input_channel);
  1383. HAL_TIM_ChannelStateTypeDef output_channel_state = TIM_CHANNEL_N_STATE_GET(htim, OutputChannel);
  1384. /* Check the parameters */
  1385. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1386. /* Check the TIM channels state */
  1387. if ((output_channel_state != HAL_TIM_CHANNEL_STATE_READY)
  1388. || (input_channel_state != HAL_TIM_CHANNEL_STATE_READY))
  1389. {
  1390. return HAL_ERROR;
  1391. }
  1392. /* Set the TIM channels state */
  1393. TIM_CHANNEL_N_STATE_SET(htim, OutputChannel, HAL_TIM_CHANNEL_STATE_BUSY);
  1394. TIM_CHANNEL_STATE_SET(htim, input_channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1395. /* Enable the TIM Capture/Compare 1 interrupt */
  1396. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1397. /* Enable the TIM Capture/Compare 2 interrupt */
  1398. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1399. /* Enable the complementary One Pulse output channel and the Input Capture channel */
  1400. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1401. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
  1402. /* Enable the Main Output */
  1403. __HAL_TIM_MOE_ENABLE(htim);
  1404. /* Return function status */
  1405. return HAL_OK;
  1406. }
  1407. /**
  1408. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1409. * complementary channel.
  1410. * @param htim TIM One Pulse handle
  1411. * @param OutputChannel TIM Channel to be disabled
  1412. * This parameter can be one of the following values:
  1413. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1414. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1415. * @retval HAL status
  1416. */
  1417. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1418. {
  1419. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1420. /* Check the parameters */
  1421. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1422. /* Disable the TIM Capture/Compare 1 interrupt */
  1423. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1424. /* Disable the TIM Capture/Compare 2 interrupt */
  1425. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1426. /* Disable the complementary One Pulse output channel and the Input Capture channel */
  1427. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1428. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
  1429. /* Disable the Main Output */
  1430. __HAL_TIM_MOE_DISABLE(htim);
  1431. /* Disable the Peripheral */
  1432. __HAL_TIM_DISABLE(htim);
  1433. /* Set the TIM channels state */
  1434. TIM_CHANNEL_N_STATE_SET(htim, OutputChannel, HAL_TIM_CHANNEL_STATE_READY);
  1435. TIM_CHANNEL_STATE_SET(htim, input_channel, HAL_TIM_CHANNEL_STATE_READY);
  1436. /* Return function status */
  1437. return HAL_OK;
  1438. }
  1439. /**
  1440. * @}
  1441. */
  1442. /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
  1443. * @brief Peripheral Control functions
  1444. *
  1445. @verbatim
  1446. ==============================================================================
  1447. ##### Peripheral Control functions #####
  1448. ==============================================================================
  1449. [..]
  1450. This section provides functions allowing to:
  1451. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1452. (+) Configure Output channels for OC and PWM mode.
  1453. (+) Configure Complementary channels, break features and dead time.
  1454. (+) Configure Master synchronization.
  1455. (+) Configure timer remapping capabilities.
  1456. (+) Enable or disable channel grouping.
  1457. @endverbatim
  1458. * @{
  1459. */
  1460. /**
  1461. * @brief Configure the TIM commutation event sequence.
  1462. * @note This function is mandatory to use the commutation event in order to
  1463. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1464. * the typical use of this feature is with the use of another Timer(interface Timer)
  1465. * configured in Hall sensor interface, this interface Timer will generate the
  1466. * commutation at its TRGO output (connected to Timer used in this function) each time
  1467. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1468. * @param htim TIM handle
  1469. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1470. * This parameter can be one of the following values:
  1471. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1472. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1473. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1474. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1475. * @arg TIM_TS_NONE: No trigger is needed
  1476. * @param CommutationSource the Commutation Event source
  1477. * This parameter can be one of the following values:
  1478. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1479. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1480. * @retval HAL status
  1481. */
  1482. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1483. uint32_t CommutationSource)
  1484. {
  1485. /* Check the parameters */
  1486. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1487. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1488. __HAL_LOCK(htim);
  1489. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1490. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1491. {
  1492. /* Select the Input trigger */
  1493. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1494. htim->Instance->SMCR |= InputTrigger;
  1495. }
  1496. /* Select the Capture Compare preload feature */
  1497. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1498. /* Select the Commutation event source */
  1499. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1500. htim->Instance->CR2 |= CommutationSource;
  1501. /* Disable Commutation Interrupt */
  1502. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1503. /* Disable Commutation DMA request */
  1504. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1505. __HAL_UNLOCK(htim);
  1506. return HAL_OK;
  1507. }
  1508. /**
  1509. * @brief Configure the TIM commutation event sequence with interrupt.
  1510. * @note This function is mandatory to use the commutation event in order to
  1511. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1512. * the typical use of this feature is with the use of another Timer(interface Timer)
  1513. * configured in Hall sensor interface, this interface Timer will generate the
  1514. * commutation at its TRGO output (connected to Timer used in this function) each time
  1515. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1516. * @param htim TIM handle
  1517. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1518. * This parameter can be one of the following values:
  1519. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1520. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1521. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1522. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1523. * @arg TIM_TS_NONE: No trigger is needed
  1524. * @param CommutationSource the Commutation Event source
  1525. * This parameter can be one of the following values:
  1526. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1527. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1528. * @retval HAL status
  1529. */
  1530. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1531. uint32_t CommutationSource)
  1532. {
  1533. /* Check the parameters */
  1534. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1535. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1536. __HAL_LOCK(htim);
  1537. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1538. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1539. {
  1540. /* Select the Input trigger */
  1541. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1542. htim->Instance->SMCR |= InputTrigger;
  1543. }
  1544. /* Select the Capture Compare preload feature */
  1545. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1546. /* Select the Commutation event source */
  1547. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1548. htim->Instance->CR2 |= CommutationSource;
  1549. /* Disable Commutation DMA request */
  1550. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1551. /* Enable the Commutation Interrupt */
  1552. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1553. __HAL_UNLOCK(htim);
  1554. return HAL_OK;
  1555. }
  1556. /**
  1557. * @brief Configure the TIM commutation event sequence with DMA.
  1558. * @note This function is mandatory to use the commutation event in order to
  1559. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1560. * the typical use of this feature is with the use of another Timer(interface Timer)
  1561. * configured in Hall sensor interface, this interface Timer will generate the
  1562. * commutation at its TRGO output (connected to Timer used in this function) each time
  1563. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1564. * @note The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1565. * @param htim TIM handle
  1566. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1567. * This parameter can be one of the following values:
  1568. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1569. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1570. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1571. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1572. * @arg TIM_TS_NONE: No trigger is needed
  1573. * @param CommutationSource the Commutation Event source
  1574. * This parameter can be one of the following values:
  1575. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1576. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1577. * @retval HAL status
  1578. */
  1579. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1580. uint32_t CommutationSource)
  1581. {
  1582. /* Check the parameters */
  1583. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1584. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1585. __HAL_LOCK(htim);
  1586. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1587. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1588. {
  1589. /* Select the Input trigger */
  1590. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1591. htim->Instance->SMCR |= InputTrigger;
  1592. }
  1593. /* Select the Capture Compare preload feature */
  1594. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1595. /* Select the Commutation event source */
  1596. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1597. htim->Instance->CR2 |= CommutationSource;
  1598. /* Enable the Commutation DMA Request */
  1599. /* Set the DMA Commutation Callback */
  1600. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1601. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  1602. /* Set the DMA error callback */
  1603. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1604. /* Disable Commutation Interrupt */
  1605. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1606. /* Enable the Commutation DMA Request */
  1607. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1608. __HAL_UNLOCK(htim);
  1609. return HAL_OK;
  1610. }
  1611. /**
  1612. * @brief Configures the TIM in master mode.
  1613. * @param htim TIM handle.
  1614. * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
  1615. * contains the selected trigger output (TRGO) and the Master/Slave
  1616. * mode.
  1617. * @retval HAL status
  1618. */
  1619. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
  1620. TIM_MasterConfigTypeDef *sMasterConfig)
  1621. {
  1622. uint32_t tmpcr2;
  1623. uint32_t tmpsmcr;
  1624. /* Check the parameters */
  1625. assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
  1626. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1627. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1628. /* Check input state */
  1629. __HAL_LOCK(htim);
  1630. /* Change the handler state */
  1631. htim->State = HAL_TIM_STATE_BUSY;
  1632. /* Get the TIMx CR2 register value */
  1633. tmpcr2 = htim->Instance->CR2;
  1634. /* Get the TIMx SMCR register value */
  1635. tmpsmcr = htim->Instance->SMCR;
  1636. /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
  1637. if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
  1638. {
  1639. /* Check the parameters */
  1640. assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
  1641. /* Clear the MMS2 bits */
  1642. tmpcr2 &= ~TIM_CR2_MMS2;
  1643. /* Select the TRGO2 source*/
  1644. tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
  1645. }
  1646. /* Reset the MMS Bits */
  1647. tmpcr2 &= ~TIM_CR2_MMS;
  1648. /* Select the TRGO source */
  1649. tmpcr2 |= sMasterConfig->MasterOutputTrigger;
  1650. /* Update TIMx CR2 */
  1651. htim->Instance->CR2 = tmpcr2;
  1652. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1653. {
  1654. /* Reset the MSM Bit */
  1655. tmpsmcr &= ~TIM_SMCR_MSM;
  1656. /* Set master mode */
  1657. tmpsmcr |= sMasterConfig->MasterSlaveMode;
  1658. /* Update TIMx SMCR */
  1659. htim->Instance->SMCR = tmpsmcr;
  1660. }
  1661. /* Change the htim state */
  1662. htim->State = HAL_TIM_STATE_READY;
  1663. __HAL_UNLOCK(htim);
  1664. return HAL_OK;
  1665. }
  1666. /**
  1667. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1668. * and the AOE(automatic output enable).
  1669. * @param htim TIM handle
  1670. * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
  1671. * contains the BDTR Register configuration information for the TIM peripheral.
  1672. * @note Interrupts can be generated when an active level is detected on the
  1673. * break input, the break 2 input or the system break input. Break
  1674. * interrupt can be enabled by calling the @ref __HAL_TIM_ENABLE_IT macro.
  1675. * @retval HAL status
  1676. */
  1677. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1678. TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
  1679. {
  1680. /* Keep this variable initialized to 0 as it is used to configure BDTR register */
  1681. uint32_t tmpbdtr = 0U;
  1682. /* Check the parameters */
  1683. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1684. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1685. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1686. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1687. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1688. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1689. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1690. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
  1691. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1692. /* Check input state */
  1693. __HAL_LOCK(htim);
  1694. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1695. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1696. /* Set the BDTR bits */
  1697. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  1698. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  1699. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  1700. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  1701. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  1702. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  1703. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  1704. MODIFY_REG(tmpbdtr, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << TIM_BDTR_BKF_Pos));
  1705. if (IS_TIM_ADVANCED_INSTANCE(htim->Instance))
  1706. {
  1707. /* Check the parameters */
  1708. assert_param(IS_TIM_BREAK_AFMODE(sBreakDeadTimeConfig->BreakAFMode));
  1709. /* Set BREAK AF mode */
  1710. MODIFY_REG(tmpbdtr, TIM_BDTR_BKBID, sBreakDeadTimeConfig->BreakAFMode);
  1711. }
  1712. if (IS_TIM_BKIN2_INSTANCE(htim->Instance))
  1713. {
  1714. /* Check the parameters */
  1715. assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
  1716. assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
  1717. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
  1718. /* Set the BREAK2 input related BDTR bits */
  1719. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2F, (sBreakDeadTimeConfig->Break2Filter << TIM_BDTR_BK2F_Pos));
  1720. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2E, sBreakDeadTimeConfig->Break2State);
  1721. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2P, sBreakDeadTimeConfig->Break2Polarity);
  1722. if (IS_TIM_ADVANCED_INSTANCE(htim->Instance))
  1723. {
  1724. /* Check the parameters */
  1725. assert_param(IS_TIM_BREAK2_AFMODE(sBreakDeadTimeConfig->Break2AFMode));
  1726. /* Set BREAK2 AF mode */
  1727. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2BID, sBreakDeadTimeConfig->Break2AFMode);
  1728. }
  1729. }
  1730. /* Set TIMx_BDTR */
  1731. htim->Instance->BDTR = tmpbdtr;
  1732. __HAL_UNLOCK(htim);
  1733. return HAL_OK;
  1734. }
  1735. /**
  1736. * @brief Configures the break input source.
  1737. * @param htim TIM handle.
  1738. * @param BreakInput Break input to configure
  1739. * This parameter can be one of the following values:
  1740. * @arg TIM_BREAKINPUT_BRK: Timer break input
  1741. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  1742. * @param sBreakInputConfig Break input source configuration
  1743. * @retval HAL status
  1744. */
  1745. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim,
  1746. uint32_t BreakInput,
  1747. TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
  1748. {
  1749. uint32_t tmporx;
  1750. uint32_t bkin_enable_mask;
  1751. uint32_t bkin_polarity_mask;
  1752. uint32_t bkin_enable_bitpos;
  1753. uint32_t bkin_polarity_bitpos;
  1754. /* Check the parameters */
  1755. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1756. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  1757. assert_param(IS_TIM_BREAKINPUTSOURCE(sBreakInputConfig->Source));
  1758. assert_param(IS_TIM_BREAKINPUTSOURCE_STATE(sBreakInputConfig->Enable));
  1759. assert_param(IS_TIM_BREAKINPUTSOURCE_POLARITY(sBreakInputConfig->Polarity));
  1760. /* Check input state */
  1761. __HAL_LOCK(htim);
  1762. switch (sBreakInputConfig->Source)
  1763. {
  1764. case TIM_BREAKINPUTSOURCE_BKIN:
  1765. {
  1766. bkin_enable_mask = TIM1_AF1_BKINE;
  1767. bkin_enable_bitpos = TIM1_AF1_BKINE_Pos;
  1768. bkin_polarity_mask = TIM1_AF1_BKINP;
  1769. bkin_polarity_bitpos = TIM1_AF1_BKINP_Pos;
  1770. break;
  1771. }
  1772. #if defined(COMP1) && defined(COMP2)
  1773. case TIM_BREAKINPUTSOURCE_COMP1:
  1774. {
  1775. bkin_enable_mask = TIM1_AF1_BKCMP1E;
  1776. bkin_enable_bitpos = TIM1_AF1_BKCMP1E_Pos;
  1777. bkin_polarity_mask = TIM1_AF1_BKCMP1P;
  1778. bkin_polarity_bitpos = TIM1_AF1_BKCMP1P_Pos;
  1779. break;
  1780. }
  1781. case TIM_BREAKINPUTSOURCE_COMP2:
  1782. {
  1783. bkin_enable_mask = TIM1_AF1_BKCMP2E;
  1784. bkin_enable_bitpos = TIM1_AF1_BKCMP2E_Pos;
  1785. bkin_polarity_mask = TIM1_AF1_BKCMP2P;
  1786. bkin_polarity_bitpos = TIM1_AF1_BKCMP2P_Pos;
  1787. break;
  1788. }
  1789. #endif /* COMP1 && COMP2 */
  1790. default:
  1791. {
  1792. bkin_enable_mask = 0U;
  1793. bkin_polarity_mask = 0U;
  1794. bkin_enable_bitpos = 0U;
  1795. bkin_polarity_bitpos = 0U;
  1796. break;
  1797. }
  1798. }
  1799. switch (BreakInput)
  1800. {
  1801. case TIM_BREAKINPUT_BRK:
  1802. {
  1803. /* Get the TIMx_AF1 register value */
  1804. tmporx = htim->Instance->AF1;
  1805. /* Enable the break input */
  1806. tmporx &= ~bkin_enable_mask;
  1807. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1808. /* Set the break input polarity */
  1809. tmporx &= ~bkin_polarity_mask;
  1810. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  1811. /* Set TIMx_AF1 */
  1812. htim->Instance->AF1 = tmporx;
  1813. break;
  1814. }
  1815. case TIM_BREAKINPUT_BRK2:
  1816. {
  1817. /* Get the TIMx_AF2 register value */
  1818. tmporx = htim->Instance->AF2;
  1819. /* Enable the break input */
  1820. tmporx &= ~bkin_enable_mask;
  1821. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1822. /* Set the break input polarity */
  1823. tmporx &= ~bkin_polarity_mask;
  1824. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  1825. /* Set TIMx_AF2 */
  1826. htim->Instance->AF2 = tmporx;
  1827. break;
  1828. }
  1829. default:
  1830. break;
  1831. }
  1832. __HAL_UNLOCK(htim);
  1833. return HAL_OK;
  1834. }
  1835. /**
  1836. * @brief Configures the TIMx Remapping input capabilities.
  1837. * @param htim TIM handle.
  1838. * @param Remap specifies the TIM remapping source.
  1839. * For TIM1, the parameter is a combination of 2 fields (field1 | field2):
  1840. *
  1841. * field1 can have the following values:
  1842. * @arg TIM_TIM1_ETR_ADC1_GPIO: TIM1_ETR is connected to I/O
  1843. * @arg TIM_TIM1_ETR_ADC1_AWD1: TIM1_ETR is connected to ADC1 AWD1
  1844. * @arg TIM_TIM1_ETR_ADC1_AWD2: TIM1_ETR is connected to ADC1 AWD2 (*)
  1845. * @arg TIM_TIM1_ETR_ADC1_AWD3: TIM1_ETR is connected to ADC1 AWD3 (*)
  1846. * @arg TIM_TIM1_ETR_COMP1: TIM1_ETR is connected to COMP1 output (*)
  1847. * @arg TIM_TIM1_ETR_COMP2: TIM1_ETR is connected to COMP2 output (*)
  1848. * field2 can have the following values:
  1849. * @arg TIM_TIM1_TI1_GPIO: TIM1 TI1 is connected to I/O
  1850. * @arg TIM_TIM1_TI1_COMP1: TIM1 TI1 is connected to COMP1 output (*)
  1851. *
  1852. * For TIM2, the parameter is a combination of 3 fields (field1 | field2 | field3):
  1853. *
  1854. * field1 can have the following values:
  1855. * @arg TIM_TIM2_ITR1_NONE: No internal trigger on TIM2_ITR1
  1856. * @arg TIM_TIM2_ITR1_USB: TIM2_ITR1 is connected to USB SOF (*)
  1857. *
  1858. * field2 can have the following values:
  1859. * @arg TIM_TIM2_ETR_GPIO: TIM2_ETR is connected to I/O
  1860. * @arg TIM_TIM2_ETR_LSE: TIM2_ETR is connected to LSE
  1861. * @arg TIM_TIM2_ETR_COMP1: TIM2_ETR is connected to COMP1 output (*)
  1862. * @arg TIM_TIM2_ETR_COMP2: TIM2_ETR is connected to COMP2 output (*)
  1863. *
  1864. * field3 can have the following values:
  1865. * @arg TIM_TIM2_TI4_GPIO: TIM2 TI4 is connected to I/O
  1866. * @arg TIM_TIM2_TI4_COMP1: TIM2 TI4 is connected to COMP1 output (*)
  1867. * @arg TIM_TIM2_TI4_COMP2: TIM2 TI4 is connected to COMP2 output (*)
  1868. * @arg TIM_TIM2_TI4_COMP1_COMP2: TIM2 TI4 is connected to logical OR between COMP1 and COMP2 output (*)
  1869. *
  1870. * For TIM16, the parameter can have the following values:
  1871. * @arg TIM_TIM16_TI1_GPIO: TIM16 TI1 is connected to I/O
  1872. * @arg TIM_TIM16_TI1_LSI: TIM16 TI1 is connected to LSI
  1873. * @arg TIM_TIM16_TI1_LSE: TIM16 TI1 is connected to LSE
  1874. * @arg TIM_TIM16_TI1_RTC: TIM16 TI1 is connected to RTC wakeup interrupt
  1875. *
  1876. * For TIM17, the parameter can have the following values:
  1877. * @arg TIM_TIM17_TI1_GPIO: TIM17 TI1 is connected to I/O
  1878. * @arg TIM_TIM17_TI1_MSI: TIM17 TI1 is connected to MSI (constraint: MSI clock < 1/4 TIM APB clock)
  1879. * @arg TIM_TIM17_TI1_HSE: TIM17 TI1 is connected to HSE div 32
  1880. * @arg TIM_TIM17_TI1_MCO: TIM17 TI1 is connected to MCO
  1881. *
  1882. * (*) Value not defined in all devices.
  1883. *
  1884. * @retval HAL status
  1885. */
  1886. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1887. {
  1888. uint32_t tmpor;
  1889. uint32_t tmpaf1;
  1890. /* Check parameters */
  1891. assert_param(IS_TIM_REMAP(htim->Instance, Remap));
  1892. __HAL_LOCK(htim);
  1893. /* Read TIMx_OR */
  1894. tmpor = READ_REG(htim->Instance->OR);
  1895. /* Read TIMx_AF1 */
  1896. tmpaf1 = READ_REG(htim->Instance->AF1);
  1897. /* Set ETR_SEL bit field (if required) */
  1898. if (IS_TIM_ETRSEL_INSTANCE(htim->Instance))
  1899. {
  1900. if ((Remap & TIM1_AF1_ETRSEL) != (uint32_t)RESET)
  1901. {
  1902. /* COMP1 output or COMP2 output connected to ETR input */
  1903. MODIFY_REG(tmpaf1, TIM1_AF1_ETRSEL, (Remap & TIM1_AF1_ETRSEL));
  1904. }
  1905. else
  1906. {
  1907. /* ETR legacy mode */
  1908. MODIFY_REG(tmpaf1, TIM1_AF1_ETRSEL, 0U);
  1909. }
  1910. /* Set TIMx_AF1 */
  1911. WRITE_REG(htim->Instance->AF1, tmpaf1);
  1912. }
  1913. /* Set other remapping capabilities */
  1914. MODIFY_REG(tmpor, TIM_GET_OR_MASK(htim->Instance), (Remap & (~TIM1_AF1_ETRSEL)));
  1915. /* Set TIMx_OR */
  1916. WRITE_REG(htim->Instance->OR, tmpor);
  1917. __HAL_UNLOCK(htim);
  1918. return HAL_OK;
  1919. }
  1920. /**
  1921. * @brief Group channel 5 and channel 1, 2 or 3
  1922. * @param htim TIM handle.
  1923. * @param Channels specifies the reference signal(s) the OC5REF is combined with.
  1924. * This parameter can be any combination of the following values:
  1925. * TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
  1926. * TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
  1927. * TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
  1928. * TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
  1929. * @retval HAL status
  1930. */
  1931. HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t Channels)
  1932. {
  1933. /* Check parameters */
  1934. assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
  1935. assert_param(IS_TIM_GROUPCH5(Channels));
  1936. /* Process Locked */
  1937. __HAL_LOCK(htim);
  1938. htim->State = HAL_TIM_STATE_BUSY;
  1939. /* Clear GC5Cx bit fields */
  1940. htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3 | TIM_CCR5_GC5C2 | TIM_CCR5_GC5C1);
  1941. /* Set GC5Cx bit fields */
  1942. htim->Instance->CCR5 |= Channels;
  1943. /* Change the htim state */
  1944. htim->State = HAL_TIM_STATE_READY;
  1945. __HAL_UNLOCK(htim);
  1946. return HAL_OK;
  1947. }
  1948. /**
  1949. * @brief Disarm the designated break input (when it operates in bidirectional mode).
  1950. * @param htim TIM handle.
  1951. * @param BreakInput Break input to disarm
  1952. * This parameter can be one of the following values:
  1953. * @arg TIM_BREAKINPUT_BRK: Timer break input
  1954. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  1955. * @note The break input can be disarmed only when it is configured in
  1956. * bidirectional mode and when when MOE is reset.
  1957. * @note Purpose is to be able to have the input voltage back to high-state,
  1958. * whatever the time constant on the output .
  1959. * @retval HAL status
  1960. */
  1961. HAL_StatusTypeDef HAL_TIMEx_DisarmBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput)
  1962. {
  1963. uint32_t tmpbdtr;
  1964. /* Check the parameters */
  1965. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1966. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  1967. switch (BreakInput)
  1968. {
  1969. case TIM_BREAKINPUT_BRK:
  1970. {
  1971. /* Check initial conditions */
  1972. tmpbdtr = READ_REG(htim->Instance->BDTR);
  1973. if ((READ_BIT(tmpbdtr, TIM_BDTR_BKBID) == TIM_BDTR_BKBID) &&
  1974. (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
  1975. {
  1976. /* Break input BRK is disarmed */
  1977. SET_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM);
  1978. }
  1979. break;
  1980. }
  1981. case TIM_BREAKINPUT_BRK2:
  1982. {
  1983. /* Check initial conditions */
  1984. tmpbdtr = READ_REG(htim->Instance->BDTR);
  1985. if ((READ_BIT(tmpbdtr, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID) &&
  1986. (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
  1987. {
  1988. /* Break input BRK is disarmed */
  1989. SET_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM);
  1990. }
  1991. break;
  1992. }
  1993. default:
  1994. break;
  1995. }
  1996. return HAL_OK;
  1997. }
  1998. /**
  1999. * @brief Arm the designated break input (when it operates in bidirectional mode).
  2000. * @param htim TIM handle.
  2001. * @param BreakInput Break input to arm
  2002. * This parameter can be one of the following values:
  2003. * @arg TIM_BREAKINPUT_BRK: Timer break input
  2004. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  2005. * @note Arming is possible at anytime, even if fault is present.
  2006. * @note Break input is automatically armed as soon as MOE bit is set.
  2007. * @retval HAL status
  2008. */
  2009. HAL_StatusTypeDef HAL_TIMEx_ReArmBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput)
  2010. {
  2011. uint32_t tickstart;
  2012. /* Check the parameters */
  2013. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  2014. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  2015. switch (BreakInput)
  2016. {
  2017. case TIM_BREAKINPUT_BRK:
  2018. {
  2019. /* Check initial conditions */
  2020. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKBID) == TIM_BDTR_BKBID)
  2021. {
  2022. /* Break input BRK is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
  2023. /* Init tickstart for timeout management */
  2024. tickstart = HAL_GetTick();
  2025. do
  2026. {
  2027. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != TIM_BDTR_BKDSRM)
  2028. {
  2029. return HAL_OK;
  2030. }
  2031. } while ((HAL_GetTick() - tickstart) <= TIM_BREAKINPUT_REARM_TIMEOUT);
  2032. return HAL_TIMEOUT;
  2033. }
  2034. break;
  2035. }
  2036. case TIM_BREAKINPUT_BRK2:
  2037. {
  2038. /* Check initial conditions */
  2039. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID)
  2040. {
  2041. /* Break input BRK2 is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
  2042. /* Init tickstart for timeout management */
  2043. tickstart = HAL_GetTick();
  2044. do
  2045. {
  2046. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != TIM_BDTR_BK2DSRM)
  2047. {
  2048. return HAL_OK;
  2049. }
  2050. } while ((HAL_GetTick() - tickstart) <= TIM_BREAKINPUT_REARM_TIMEOUT);
  2051. return HAL_TIMEOUT;
  2052. }
  2053. break;
  2054. }
  2055. default:
  2056. break;
  2057. }
  2058. return HAL_OK;
  2059. }
  2060. /**
  2061. * @}
  2062. */
  2063. /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
  2064. * @brief Extended Callbacks functions
  2065. *
  2066. @verbatim
  2067. ==============================================================================
  2068. ##### Extended Callbacks functions #####
  2069. ==============================================================================
  2070. [..]
  2071. This section provides Extended TIM callback functions:
  2072. (+) Timer Commutation callback
  2073. (+) Timer Break callback
  2074. @endverbatim
  2075. * @{
  2076. */
  2077. /**
  2078. * @brief Hall commutation changed callback in non-blocking mode
  2079. * @param htim TIM handle
  2080. * @retval None
  2081. */
  2082. __weak void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim)
  2083. {
  2084. /* Prevent unused argument(s) compilation warning */
  2085. UNUSED(htim);
  2086. /* NOTE : This function should not be modified, when the callback is needed,
  2087. the HAL_TIMEx_CommutCallback could be implemented in the user file
  2088. */
  2089. }
  2090. /**
  2091. * @brief Hall commutation changed half complete callback in non-blocking mode
  2092. * @param htim TIM handle
  2093. * @retval None
  2094. */
  2095. __weak void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim)
  2096. {
  2097. /* Prevent unused argument(s) compilation warning */
  2098. UNUSED(htim);
  2099. /* NOTE : This function should not be modified, when the callback is needed,
  2100. the HAL_TIMEx_CommutHalfCpltCallback could be implemented in the user file
  2101. */
  2102. }
  2103. /**
  2104. * @brief Hall Break detection callback in non-blocking mode
  2105. * @param htim TIM handle
  2106. * @retval None
  2107. */
  2108. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  2109. {
  2110. /* Prevent unused argument(s) compilation warning */
  2111. UNUSED(htim);
  2112. /* NOTE : This function should not be modified, when the callback is needed,
  2113. the HAL_TIMEx_BreakCallback could be implemented in the user file
  2114. */
  2115. }
  2116. /**
  2117. * @brief Hall Break2 detection callback in non blocking mode
  2118. * @param htim: TIM handle
  2119. * @retval None
  2120. */
  2121. __weak void HAL_TIMEx_Break2Callback(TIM_HandleTypeDef *htim)
  2122. {
  2123. /* Prevent unused argument(s) compilation warning */
  2124. UNUSED(htim);
  2125. /* NOTE : This function Should not be modified, when the callback is needed,
  2126. the HAL_TIMEx_Break2Callback could be implemented in the user file
  2127. */
  2128. }
  2129. /**
  2130. * @}
  2131. */
  2132. /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
  2133. * @brief Extended Peripheral State functions
  2134. *
  2135. @verbatim
  2136. ==============================================================================
  2137. ##### Extended Peripheral State functions #####
  2138. ==============================================================================
  2139. [..]
  2140. This subsection permits to get in run-time the status of the peripheral
  2141. and the data flow.
  2142. @endverbatim
  2143. * @{
  2144. */
  2145. /**
  2146. * @brief Return the TIM Hall Sensor interface handle state.
  2147. * @param htim TIM Hall Sensor handle
  2148. * @retval HAL state
  2149. */
  2150. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
  2151. {
  2152. return htim->State;
  2153. }
  2154. /**
  2155. * @brief Return actual state of the TIM complementary channel.
  2156. * @param htim TIM handle
  2157. * @param ChannelN TIM Complementary channel
  2158. * This parameter can be one of the following values:
  2159. * @arg TIM_CHANNEL_1: TIM Channel 1
  2160. * @arg TIM_CHANNEL_2: TIM Channel 2
  2161. * @arg TIM_CHANNEL_3: TIM Channel 3
  2162. * @retval TIM Complementary channel state
  2163. */
  2164. HAL_TIM_ChannelStateTypeDef HAL_TIMEx_GetChannelNState(TIM_HandleTypeDef *htim, uint32_t ChannelN)
  2165. {
  2166. HAL_TIM_ChannelStateTypeDef channel_state;
  2167. /* Check the parameters */
  2168. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, ChannelN));
  2169. channel_state = TIM_CHANNEL_N_STATE_GET(htim, ChannelN);
  2170. return channel_state;
  2171. }
  2172. /**
  2173. * @}
  2174. */
  2175. /**
  2176. * @}
  2177. */
  2178. /* Private functions ---------------------------------------------------------*/
  2179. /** @defgroup TIMEx_Private_Functions TIMEx Private Functions
  2180. * @{
  2181. */
  2182. /**
  2183. * @brief TIM DMA Commutation callback.
  2184. * @param hdma pointer to DMA handle.
  2185. * @retval None
  2186. */
  2187. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  2188. {
  2189. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2190. /* Change the htim state */
  2191. htim->State = HAL_TIM_STATE_READY;
  2192. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2193. htim->CommutationCallback(htim);
  2194. #else
  2195. HAL_TIMEx_CommutCallback(htim);
  2196. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2197. }
  2198. /**
  2199. * @brief TIM DMA Commutation half complete callback.
  2200. * @param hdma pointer to DMA handle.
  2201. * @retval None
  2202. */
  2203. void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma)
  2204. {
  2205. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2206. /* Change the htim state */
  2207. htim->State = HAL_TIM_STATE_READY;
  2208. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2209. htim->CommutationHalfCpltCallback(htim);
  2210. #else
  2211. HAL_TIMEx_CommutHalfCpltCallback(htim);
  2212. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2213. }
  2214. /**
  2215. * @brief TIM DMA Delay Pulse complete callback (complementary channel).
  2216. * @param hdma pointer to DMA handle.
  2217. * @retval None
  2218. */
  2219. static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma)
  2220. {
  2221. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2222. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  2223. {
  2224. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2225. if (hdma->Init.Mode == DMA_NORMAL)
  2226. {
  2227. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2228. }
  2229. }
  2230. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  2231. {
  2232. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2233. if (hdma->Init.Mode == DMA_NORMAL)
  2234. {
  2235. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2236. }
  2237. }
  2238. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  2239. {
  2240. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2241. if (hdma->Init.Mode == DMA_NORMAL)
  2242. {
  2243. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  2244. }
  2245. }
  2246. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  2247. {
  2248. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  2249. if (hdma->Init.Mode == DMA_NORMAL)
  2250. {
  2251. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  2252. }
  2253. }
  2254. else
  2255. {
  2256. /* nothing to do */
  2257. }
  2258. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2259. htim->PWM_PulseFinishedCallback(htim);
  2260. #else
  2261. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2262. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2263. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2264. }
  2265. /**
  2266. * @brief TIM DMA error callback (complementary channel)
  2267. * @param hdma pointer to DMA handle.
  2268. * @retval None
  2269. */
  2270. static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma)
  2271. {
  2272. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2273. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  2274. {
  2275. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2276. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2277. }
  2278. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  2279. {
  2280. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2281. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2282. }
  2283. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  2284. {
  2285. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2286. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  2287. }
  2288. else
  2289. {
  2290. /* nothing to do */
  2291. }
  2292. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2293. htim->ErrorCallback(htim);
  2294. #else
  2295. HAL_TIM_ErrorCallback(htim);
  2296. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2297. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2298. }
  2299. /**
  2300. * @brief Enables or disables the TIM Capture Compare Channel xN.
  2301. * @param TIMx to select the TIM peripheral
  2302. * @param Channel specifies the TIM Channel
  2303. * This parameter can be one of the following values:
  2304. * @arg TIM_CHANNEL_1: TIM Channel 1
  2305. * @arg TIM_CHANNEL_2: TIM Channel 2
  2306. * @arg TIM_CHANNEL_3: TIM Channel 3
  2307. * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
  2308. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  2309. * @retval None
  2310. */
  2311. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState)
  2312. {
  2313. uint32_t tmp;
  2314. tmp = TIM_CCER_CC1NE << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
  2315. /* Reset the CCxNE Bit */
  2316. TIMx->CCER &= ~tmp;
  2317. /* Set or reset the CCxNE Bit */
  2318. TIMx->CCER |= (uint32_t)(ChannelNState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
  2319. }
  2320. /**
  2321. * @}
  2322. */
  2323. #endif /* HAL_TIM_MODULE_ENABLED */
  2324. /**
  2325. * @}
  2326. */
  2327. /**
  2328. * @}
  2329. */
  2330. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/