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.
 
 
 

7048 lines
220 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_tim.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 (TIM) peripheral:
  8. * + TIM Time Base Initialization
  9. * + TIM Time Base Start
  10. * + TIM Time Base Start Interruption
  11. * + TIM Time Base Start DMA
  12. * + TIM Output Compare/PWM Initialization
  13. * + TIM Output Compare/PWM Channel Configuration
  14. * + TIM Output Compare/PWM Start
  15. * + TIM Output Compare/PWM Start Interruption
  16. * + TIM Output Compare/PWM Start DMA
  17. * + TIM Input Capture Initialization
  18. * + TIM Input Capture Channel Configuration
  19. * + TIM Input Capture Start
  20. * + TIM Input Capture Start Interruption
  21. * + TIM Input Capture Start DMA
  22. * + TIM One Pulse Initialization
  23. * + TIM One Pulse Channel Configuration
  24. * + TIM One Pulse Start
  25. * + TIM Encoder Interface Initialization
  26. * + TIM Encoder Interface Start
  27. * + TIM Encoder Interface Start Interruption
  28. * + TIM Encoder Interface Start DMA
  29. * + Commutation Event configuration with Interruption and DMA
  30. * + TIM OCRef clear configuration
  31. * + TIM External Clock configuration
  32. @verbatim
  33. ==============================================================================
  34. ##### TIMER Generic features #####
  35. ==============================================================================
  36. [..] The Timer features include:
  37. (#) 16-bit up, down, up/down auto-reload counter.
  38. (#) 16-bit programmable prescaler allowing dividing (also on the fly) the
  39. counter clock frequency either by any factor between 1 and 65536.
  40. (#) Up to 4 independent channels for:
  41. (++) Input Capture
  42. (++) Output Compare
  43. (++) PWM generation (Edge and Center-aligned Mode)
  44. (++) One-pulse mode output
  45. (#) Synchronization circuit to control the timer with external signals and to interconnect
  46. several timers together.
  47. (#) Supports incremental encoder for positioning purposes
  48. ##### How to use this driver #####
  49. ==============================================================================
  50. [..]
  51. (#) Initialize the TIM low level resources by implementing the following functions
  52. depending on the selected feature:
  53. (++) Time Base : HAL_TIM_Base_MspInit()
  54. (++) Input Capture : HAL_TIM_IC_MspInit()
  55. (++) Output Compare : HAL_TIM_OC_MspInit()
  56. (++) PWM generation : HAL_TIM_PWM_MspInit()
  57. (++) One-pulse mode output : HAL_TIM_OnePulse_MspInit()
  58. (++) Encoder mode output : HAL_TIM_Encoder_MspInit()
  59. (#) Initialize the TIM low level resources :
  60. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  61. (##) TIM pins configuration
  62. (+++) Enable the clock for the TIM GPIOs using the following function:
  63. __HAL_RCC_GPIOx_CLK_ENABLE();
  64. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  65. (#) The external Clock can be configured, if needed (the default clock is the
  66. internal clock from the APBx), using the following function:
  67. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  68. any start function.
  69. (#) Configure the TIM in the desired functioning mode using one of the
  70. Initialization function of this driver:
  71. (++) HAL_TIM_Base_Init: to use the Timer to generate a simple time base
  72. (++) HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an
  73. Output Compare signal.
  74. (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a
  75. PWM signal.
  76. (++) HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an
  77. external signal.
  78. (++) HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer
  79. in One Pulse Mode.
  80. (++) HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
  81. (#) Activate the TIM peripheral using one of the start functions depending from the feature used:
  82. (++) Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
  83. (++) Input Capture : HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
  84. (++) Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
  85. (++) PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
  86. (++) One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
  87. (++) Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(), HAL_TIM_Encoder_Start_IT().
  88. (#) The DMA Burst is managed with the two following functions:
  89. HAL_TIM_DMABurst_WriteStart()
  90. HAL_TIM_DMABurst_ReadStart()
  91. *** Callback registration ***
  92. =============================================
  93. [..]
  94. The compilation define USE_HAL_TIM_REGISTER_CALLBACKS when set to 1
  95. allows the user to configure dynamically the driver callbacks.
  96. [..]
  97. Use Function @ref HAL_TIM_RegisterCallback() to register a callback.
  98. @ref HAL_TIM_RegisterCallback() takes as parameters the HAL peripheral handle,
  99. the Callback ID and a pointer to the user callback function.
  100. [..]
  101. Use function @ref HAL_TIM_UnRegisterCallback() to reset a callback to the default
  102. weak function.
  103. @ref HAL_TIM_UnRegisterCallback takes as parameters the HAL peripheral handle,
  104. and the Callback ID.
  105. [..]
  106. These functions allow to register/unregister following callbacks:
  107. (+) Base_MspInitCallback : TIM Base Msp Init Callback.
  108. (+) Base_MspDeInitCallback : TIM Base Msp DeInit Callback.
  109. (+) IC_MspInitCallback : TIM IC Msp Init Callback.
  110. (+) IC_MspDeInitCallback : TIM IC Msp DeInit Callback.
  111. (+) OC_MspInitCallback : TIM OC Msp Init Callback.
  112. (+) OC_MspDeInitCallback : TIM OC Msp DeInit Callback.
  113. (+) PWM_MspInitCallback : TIM PWM Msp Init Callback.
  114. (+) PWM_MspDeInitCallback : TIM PWM Msp DeInit Callback.
  115. (+) OnePulse_MspInitCallback : TIM One Pulse Msp Init Callback.
  116. (+) OnePulse_MspDeInitCallback : TIM One Pulse Msp DeInit Callback.
  117. (+) Encoder_MspInitCallback : TIM Encoder Msp Init Callback.
  118. (+) Encoder_MspDeInitCallback : TIM Encoder Msp DeInit Callback.
  119. (+) HallSensor_MspInitCallback : TIM Hall Sensor Msp Init Callback.
  120. (+) HallSensor_MspDeInitCallback : TIM Hall Sensor Msp DeInit Callback.
  121. (+) PeriodElapsedCallback : TIM Period Elapsed Callback.
  122. (+) PeriodElapsedHalfCpltCallback : TIM Period Elapsed half complete Callback.
  123. (+) TriggerCallback : TIM Trigger Callback.
  124. (+) TriggerHalfCpltCallback : TIM Trigger half complete Callback.
  125. (+) IC_CaptureCallback : TIM Input Capture Callback.
  126. (+) IC_CaptureHalfCpltCallback : TIM Input Capture half complete Callback.
  127. (+) OC_DelayElapsedCallback : TIM Output Compare Delay Elapsed Callback.
  128. (+) PWM_PulseFinishedCallback : TIM PWM Pulse Finished Callback.
  129. (+) PWM_PulseFinishedHalfCpltCallback : TIM PWM Pulse Finished half complete Callback.
  130. (+) ErrorCallback : TIM Error Callback.
  131. (+) CommutationCallback : TIM Commutation Callback.
  132. (+) CommutationHalfCpltCallback : TIM Commutation half complete Callback.
  133. (+) BreakCallback : TIM Break Callback.
  134. (+) Break2Callback : TIM Break2 Callback.
  135. [..]
  136. By default, after the Init and when the state is HAL_TIM_STATE_RESET
  137. all interrupt callbacks are set to the corresponding weak functions:
  138. examples @ref HAL_TIM_TriggerCallback(), @ref HAL_TIM_ErrorCallback().
  139. [..]
  140. Exception done for MspInit and MspDeInit functions that are reset to the legacy weak
  141. functionalities in the Init / DeInit only when these callbacks are null
  142. (not registered beforehand). If not, MspInit or MspDeInit are not null, the Init / DeInit
  143. keep and use the user MspInit / MspDeInit callbacks(registered beforehand)
  144. [..]
  145. Callbacks can be registered / unregistered in HAL_TIM_STATE_READY state only.
  146. Exception done MspInit / MspDeInit that can be registered / unregistered
  147. in HAL_TIM_STATE_READY or HAL_TIM_STATE_RESET state,
  148. thus registered(user) MspInit / DeInit callbacks can be used during the Init / DeInit.
  149. In that case first register the MspInit/MspDeInit user callbacks
  150. using @ref HAL_TIM_RegisterCallback() before calling DeInit or Init function.
  151. [..]
  152. When The compilation define USE_HAL_TIM_REGISTER_CALLBACKS is set to 0 or
  153. not defined, the callback registration feature is not available and all callbacks
  154. are set to the corresponding weak functions.
  155. @endverbatim
  156. ******************************************************************************
  157. * @attention
  158. *
  159. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  160. * All rights reserved.</center></h2>
  161. *
  162. * This software component is licensed by ST under BSD 3-Clause license,
  163. * the "License"; You may not use this file except in compliance with the
  164. * License. You may obtain a copy of the License at:
  165. * opensource.org/licenses/BSD-3-Clause
  166. *
  167. ******************************************************************************
  168. */
  169. /* Includes ------------------------------------------------------------------*/
  170. #include "stm32h7xx_hal.h"
  171. /** @addtogroup STM32H7xx_HAL_Driver
  172. * @{
  173. */
  174. /** @defgroup TIM TIM
  175. * @brief TIM HAL module driver
  176. * @{
  177. */
  178. #ifdef HAL_TIM_MODULE_ENABLED
  179. /* Private typedef -----------------------------------------------------------*/
  180. /* Private define ------------------------------------------------------------*/
  181. /* Private macro -------------------------------------------------------------*/
  182. /* Private variables ---------------------------------------------------------*/
  183. /* Private function prototypes -----------------------------------------------*/
  184. /** @addtogroup TIM_Private_Functions
  185. * @{
  186. */
  187. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  188. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  189. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  190. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  191. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  192. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
  193. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  194. uint32_t TIM_ICFilter);
  195. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
  196. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  197. uint32_t TIM_ICFilter);
  198. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  199. uint32_t TIM_ICFilter);
  200. static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource);
  201. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma);
  202. static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma);
  203. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);
  204. static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma);
  205. static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  206. TIM_SlaveConfigTypeDef *sSlaveConfig);
  207. /**
  208. * @}
  209. */
  210. /* Exported functions --------------------------------------------------------*/
  211. /** @defgroup TIM_Exported_Functions TIM Exported Functions
  212. * @{
  213. */
  214. /** @defgroup TIM_Exported_Functions_Group1 TIM Time Base functions
  215. * @brief Time Base functions
  216. *
  217. @verbatim
  218. ==============================================================================
  219. ##### Time Base functions #####
  220. ==============================================================================
  221. [..]
  222. This section provides functions allowing to:
  223. (+) Initialize and configure the TIM base.
  224. (+) De-initialize the TIM base.
  225. (+) Start the Time Base.
  226. (+) Stop the Time Base.
  227. (+) Start the Time Base and enable interrupt.
  228. (+) Stop the Time Base and disable interrupt.
  229. (+) Start the Time Base and enable DMA transfer.
  230. (+) Stop the Time Base and disable DMA transfer.
  231. @endverbatim
  232. * @{
  233. */
  234. /**
  235. * @brief Initializes the TIM Time base Unit according to the specified
  236. * parameters in the TIM_HandleTypeDef and initialize the associated handle.
  237. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  238. * requires a timer reset to avoid unexpected direction
  239. * due to DIR bit readonly in center aligned mode.
  240. * Ex: call @ref HAL_TIM_Base_DeInit() before HAL_TIM_Base_Init()
  241. * @param htim TIM Base handle
  242. * @retval HAL status
  243. */
  244. HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
  245. {
  246. /* Check the TIM handle allocation */
  247. if (htim == NULL)
  248. {
  249. return HAL_ERROR;
  250. }
  251. /* Check the parameters */
  252. assert_param(IS_TIM_INSTANCE(htim->Instance));
  253. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  254. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  255. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  256. if (htim->State == HAL_TIM_STATE_RESET)
  257. {
  258. /* Allocate lock resource and initialize it */
  259. htim->Lock = HAL_UNLOCKED;
  260. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  261. /* Reset interrupt callbacks to legacy weak callbacks */
  262. TIM_ResetCallback(htim);
  263. if (htim->Base_MspInitCallback == NULL)
  264. {
  265. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
  266. }
  267. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  268. htim->Base_MspInitCallback(htim);
  269. #else
  270. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  271. HAL_TIM_Base_MspInit(htim);
  272. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  273. }
  274. /* Set the TIM state */
  275. htim->State = HAL_TIM_STATE_BUSY;
  276. /* Set the Time Base configuration */
  277. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  278. /* Initialize the TIM state*/
  279. htim->State = HAL_TIM_STATE_READY;
  280. return HAL_OK;
  281. }
  282. /**
  283. * @brief DeInitializes the TIM Base peripheral
  284. * @param htim TIM Base handle
  285. * @retval HAL status
  286. */
  287. HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)
  288. {
  289. /* Check the parameters */
  290. assert_param(IS_TIM_INSTANCE(htim->Instance));
  291. htim->State = HAL_TIM_STATE_BUSY;
  292. /* Disable the TIM Peripheral Clock */
  293. __HAL_TIM_DISABLE(htim);
  294. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  295. if (htim->Base_MspDeInitCallback == NULL)
  296. {
  297. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
  298. }
  299. /* DeInit the low level hardware */
  300. htim->Base_MspDeInitCallback(htim);
  301. #else
  302. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  303. HAL_TIM_Base_MspDeInit(htim);
  304. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  305. /* Change TIM state */
  306. htim->State = HAL_TIM_STATE_RESET;
  307. /* Release Lock */
  308. __HAL_UNLOCK(htim);
  309. return HAL_OK;
  310. }
  311. /**
  312. * @brief Initializes the TIM Base MSP.
  313. * @param htim TIM Base handle
  314. * @retval None
  315. */
  316. __weak void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
  317. {
  318. /* Prevent unused argument(s) compilation warning */
  319. UNUSED(htim);
  320. /* NOTE : This function should not be modified, when the callback is needed,
  321. the HAL_TIM_Base_MspInit could be implemented in the user file
  322. */
  323. }
  324. /**
  325. * @brief DeInitializes TIM Base MSP.
  326. * @param htim TIM Base handle
  327. * @retval None
  328. */
  329. __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
  330. {
  331. /* Prevent unused argument(s) compilation warning */
  332. UNUSED(htim);
  333. /* NOTE : This function should not be modified, when the callback is needed,
  334. the HAL_TIM_Base_MspDeInit could be implemented in the user file
  335. */
  336. }
  337. /**
  338. * @brief Starts the TIM Base generation.
  339. * @param htim TIM Base handle
  340. * @retval HAL status
  341. */
  342. HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)
  343. {
  344. uint32_t tmpsmcr;
  345. /* Check the parameters */
  346. assert_param(IS_TIM_INSTANCE(htim->Instance));
  347. /* Set the TIM state */
  348. htim->State = HAL_TIM_STATE_BUSY;
  349. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  350. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  351. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  352. {
  353. __HAL_TIM_ENABLE(htim);
  354. }
  355. /* Change the TIM state*/
  356. htim->State = HAL_TIM_STATE_READY;
  357. /* Return function status */
  358. return HAL_OK;
  359. }
  360. /**
  361. * @brief Stops the TIM Base generation.
  362. * @param htim TIM Base handle
  363. * @retval HAL status
  364. */
  365. HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)
  366. {
  367. /* Check the parameters */
  368. assert_param(IS_TIM_INSTANCE(htim->Instance));
  369. /* Set the TIM state */
  370. htim->State = HAL_TIM_STATE_BUSY;
  371. /* Disable the Peripheral */
  372. __HAL_TIM_DISABLE(htim);
  373. /* Change the TIM state*/
  374. htim->State = HAL_TIM_STATE_READY;
  375. /* Return function status */
  376. return HAL_OK;
  377. }
  378. /**
  379. * @brief Starts the TIM Base generation in interrupt mode.
  380. * @param htim TIM Base handle
  381. * @retval HAL status
  382. */
  383. HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
  384. {
  385. uint32_t tmpsmcr;
  386. /* Check the parameters */
  387. assert_param(IS_TIM_INSTANCE(htim->Instance));
  388. /* Enable the TIM Update interrupt */
  389. __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
  390. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  391. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  392. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  393. {
  394. __HAL_TIM_ENABLE(htim);
  395. }
  396. /* Return function status */
  397. return HAL_OK;
  398. }
  399. /**
  400. * @brief Stops the TIM Base generation in interrupt mode.
  401. * @param htim TIM Base handle
  402. * @retval HAL status
  403. */
  404. HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)
  405. {
  406. /* Check the parameters */
  407. assert_param(IS_TIM_INSTANCE(htim->Instance));
  408. /* Disable the TIM Update interrupt */
  409. __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);
  410. /* Disable the Peripheral */
  411. __HAL_TIM_DISABLE(htim);
  412. /* Return function status */
  413. return HAL_OK;
  414. }
  415. /**
  416. * @brief Starts the TIM Base generation in DMA mode.
  417. * @param htim TIM Base handle
  418. * @param pData The source Buffer address.
  419. * @param Length The length of data to be transferred from memory to peripheral.
  420. * @retval HAL status
  421. */
  422. HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  423. {
  424. uint32_t tmpsmcr;
  425. /* Check the parameters */
  426. assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
  427. if (htim->State == HAL_TIM_STATE_BUSY)
  428. {
  429. return HAL_BUSY;
  430. }
  431. else if (htim->State == HAL_TIM_STATE_READY)
  432. {
  433. if ((pData == NULL) && (Length > 0U))
  434. {
  435. return HAL_ERROR;
  436. }
  437. else
  438. {
  439. htim->State = HAL_TIM_STATE_BUSY;
  440. }
  441. }
  442. else
  443. {
  444. /* nothing to do */
  445. }
  446. /* Set the DMA Period elapsed callbacks */
  447. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  448. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  449. /* Set the DMA error callback */
  450. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  451. /* Enable the DMA stream */
  452. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR, Length) != HAL_OK)
  453. {
  454. return HAL_ERROR;
  455. }
  456. /* Enable the TIM Update DMA request */
  457. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);
  458. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  459. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  460. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  461. {
  462. __HAL_TIM_ENABLE(htim);
  463. }
  464. /* Return function status */
  465. return HAL_OK;
  466. }
  467. /**
  468. * @brief Stops the TIM Base generation in DMA mode.
  469. * @param htim TIM Base handle
  470. * @retval HAL status
  471. */
  472. HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)
  473. {
  474. /* Check the parameters */
  475. assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
  476. /* Disable the TIM Update DMA request */
  477. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);
  478. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  479. /* Disable the Peripheral */
  480. __HAL_TIM_DISABLE(htim);
  481. /* Change the htim state */
  482. htim->State = HAL_TIM_STATE_READY;
  483. /* Return function status */
  484. return HAL_OK;
  485. }
  486. /**
  487. * @}
  488. */
  489. /** @defgroup TIM_Exported_Functions_Group2 TIM Output Compare functions
  490. * @brief TIM Output Compare functions
  491. *
  492. @verbatim
  493. ==============================================================================
  494. ##### TIM Output Compare functions #####
  495. ==============================================================================
  496. [..]
  497. This section provides functions allowing to:
  498. (+) Initialize and configure the TIM Output Compare.
  499. (+) De-initialize the TIM Output Compare.
  500. (+) Start the TIM Output Compare.
  501. (+) Stop the TIM Output Compare.
  502. (+) Start the TIM Output Compare and enable interrupt.
  503. (+) Stop the TIM Output Compare and disable interrupt.
  504. (+) Start the TIM Output Compare and enable DMA transfer.
  505. (+) Stop the TIM Output Compare and disable DMA transfer.
  506. @endverbatim
  507. * @{
  508. */
  509. /**
  510. * @brief Initializes the TIM Output Compare according to the specified
  511. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  512. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  513. * requires a timer reset to avoid unexpected direction
  514. * due to DIR bit readonly in center aligned mode.
  515. * Ex: call @ref HAL_TIM_OC_DeInit() before HAL_TIM_OC_Init()
  516. * @param htim TIM Output Compare handle
  517. * @retval HAL status
  518. */
  519. HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim)
  520. {
  521. /* Check the TIM handle allocation */
  522. if (htim == NULL)
  523. {
  524. return HAL_ERROR;
  525. }
  526. /* Check the parameters */
  527. assert_param(IS_TIM_INSTANCE(htim->Instance));
  528. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  529. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  530. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  531. if (htim->State == HAL_TIM_STATE_RESET)
  532. {
  533. /* Allocate lock resource and initialize it */
  534. htim->Lock = HAL_UNLOCKED;
  535. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  536. /* Reset interrupt callbacks to legacy weak callbacks */
  537. TIM_ResetCallback(htim);
  538. if (htim->OC_MspInitCallback == NULL)
  539. {
  540. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
  541. }
  542. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  543. htim->OC_MspInitCallback(htim);
  544. #else
  545. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  546. HAL_TIM_OC_MspInit(htim);
  547. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  548. }
  549. /* Set the TIM state */
  550. htim->State = HAL_TIM_STATE_BUSY;
  551. /* Init the base time for the Output Compare */
  552. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  553. /* Initialize the TIM state*/
  554. htim->State = HAL_TIM_STATE_READY;
  555. return HAL_OK;
  556. }
  557. /**
  558. * @brief DeInitializes the TIM peripheral
  559. * @param htim TIM Output Compare handle
  560. * @retval HAL status
  561. */
  562. HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)
  563. {
  564. /* Check the parameters */
  565. assert_param(IS_TIM_INSTANCE(htim->Instance));
  566. htim->State = HAL_TIM_STATE_BUSY;
  567. /* Disable the TIM Peripheral Clock */
  568. __HAL_TIM_DISABLE(htim);
  569. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  570. if (htim->OC_MspDeInitCallback == NULL)
  571. {
  572. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
  573. }
  574. /* DeInit the low level hardware */
  575. htim->OC_MspDeInitCallback(htim);
  576. #else
  577. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  578. HAL_TIM_OC_MspDeInit(htim);
  579. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  580. /* Change TIM state */
  581. htim->State = HAL_TIM_STATE_RESET;
  582. /* Release Lock */
  583. __HAL_UNLOCK(htim);
  584. return HAL_OK;
  585. }
  586. /**
  587. * @brief Initializes the TIM Output Compare MSP.
  588. * @param htim TIM Output Compare handle
  589. * @retval None
  590. */
  591. __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
  592. {
  593. /* Prevent unused argument(s) compilation warning */
  594. UNUSED(htim);
  595. /* NOTE : This function should not be modified, when the callback is needed,
  596. the HAL_TIM_OC_MspInit could be implemented in the user file
  597. */
  598. }
  599. /**
  600. * @brief DeInitializes TIM Output Compare MSP.
  601. * @param htim TIM Output Compare handle
  602. * @retval None
  603. */
  604. __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
  605. {
  606. /* Prevent unused argument(s) compilation warning */
  607. UNUSED(htim);
  608. /* NOTE : This function should not be modified, when the callback is needed,
  609. the HAL_TIM_OC_MspDeInit could be implemented in the user file
  610. */
  611. }
  612. /**
  613. * @brief Starts the TIM Output Compare signal generation.
  614. * @param htim TIM Output Compare handle
  615. * @param Channel TIM Channel to be enabled
  616. * This parameter can be one of the following values:
  617. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  618. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  619. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  620. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  621. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  622. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  623. * @retval HAL status
  624. */
  625. HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  626. {
  627. uint32_t tmpsmcr;
  628. /* Check the parameters */
  629. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  630. /* Enable the Output compare channel */
  631. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  632. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  633. {
  634. /* Enable the main output */
  635. __HAL_TIM_MOE_ENABLE(htim);
  636. }
  637. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  638. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  639. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  640. {
  641. __HAL_TIM_ENABLE(htim);
  642. }
  643. /* Return function status */
  644. return HAL_OK;
  645. }
  646. /**
  647. * @brief Stops the TIM Output Compare signal generation.
  648. * @param htim TIM Output Compare handle
  649. * @param Channel TIM Channel to be disabled
  650. * This parameter can be one of the following values:
  651. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  652. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  653. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  654. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  655. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  656. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  657. * @retval HAL status
  658. */
  659. HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  660. {
  661. /* Check the parameters */
  662. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  663. /* Disable the Output compare channel */
  664. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  665. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  666. {
  667. /* Disable the Main Output */
  668. __HAL_TIM_MOE_DISABLE(htim);
  669. }
  670. /* Disable the Peripheral */
  671. __HAL_TIM_DISABLE(htim);
  672. /* Return function status */
  673. return HAL_OK;
  674. }
  675. /**
  676. * @brief Starts the TIM Output Compare signal generation in interrupt mode.
  677. * @param htim TIM Output Compare handle
  678. * @param Channel TIM Channel to be enabled
  679. * This parameter can be one of the following values:
  680. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  681. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  682. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  683. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  684. * @retval HAL status
  685. */
  686. HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  687. {
  688. uint32_t tmpsmcr;
  689. /* Check the parameters */
  690. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  691. switch (Channel)
  692. {
  693. case TIM_CHANNEL_1:
  694. {
  695. /* Enable the TIM Capture/Compare 1 interrupt */
  696. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  697. break;
  698. }
  699. case TIM_CHANNEL_2:
  700. {
  701. /* Enable the TIM Capture/Compare 2 interrupt */
  702. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  703. break;
  704. }
  705. case TIM_CHANNEL_3:
  706. {
  707. /* Enable the TIM Capture/Compare 3 interrupt */
  708. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  709. break;
  710. }
  711. case TIM_CHANNEL_4:
  712. {
  713. /* Enable the TIM Capture/Compare 4 interrupt */
  714. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  715. break;
  716. }
  717. default:
  718. break;
  719. }
  720. /* Enable the Output compare channel */
  721. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  722. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  723. {
  724. /* Enable the main output */
  725. __HAL_TIM_MOE_ENABLE(htim);
  726. }
  727. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  728. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  729. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  730. {
  731. __HAL_TIM_ENABLE(htim);
  732. }
  733. /* Return function status */
  734. return HAL_OK;
  735. }
  736. /**
  737. * @brief Stops the TIM Output Compare signal generation in interrupt mode.
  738. * @param htim TIM Output Compare handle
  739. * @param Channel TIM Channel to be disabled
  740. * This parameter can be one of the following values:
  741. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  742. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  743. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  744. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  745. * @retval HAL status
  746. */
  747. HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  748. {
  749. /* Check the parameters */
  750. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  751. switch (Channel)
  752. {
  753. case TIM_CHANNEL_1:
  754. {
  755. /* Disable the TIM Capture/Compare 1 interrupt */
  756. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  757. break;
  758. }
  759. case TIM_CHANNEL_2:
  760. {
  761. /* Disable the TIM Capture/Compare 2 interrupt */
  762. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  763. break;
  764. }
  765. case TIM_CHANNEL_3:
  766. {
  767. /* Disable the TIM Capture/Compare 3 interrupt */
  768. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  769. break;
  770. }
  771. case TIM_CHANNEL_4:
  772. {
  773. /* Disable the TIM Capture/Compare 4 interrupt */
  774. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  775. break;
  776. }
  777. default:
  778. break;
  779. }
  780. /* Disable the Output compare channel */
  781. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  782. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  783. {
  784. /* Disable the Main Output */
  785. __HAL_TIM_MOE_DISABLE(htim);
  786. }
  787. /* Disable the Peripheral */
  788. __HAL_TIM_DISABLE(htim);
  789. /* Return function status */
  790. return HAL_OK;
  791. }
  792. /**
  793. * @brief Starts the TIM Output Compare signal generation in DMA mode.
  794. * @param htim TIM Output Compare handle
  795. * @param Channel TIM Channel to be enabled
  796. * This parameter can be one of the following values:
  797. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  798. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  799. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  800. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  801. * @param pData The source Buffer address.
  802. * @param Length The length of data to be transferred from memory to TIM peripheral
  803. * @retval HAL status
  804. */
  805. HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  806. {
  807. uint32_t tmpsmcr;
  808. /* Check the parameters */
  809. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  810. if (htim->State == HAL_TIM_STATE_BUSY)
  811. {
  812. return HAL_BUSY;
  813. }
  814. else if (htim->State == HAL_TIM_STATE_READY)
  815. {
  816. if ((pData == NULL) && (Length > 0U))
  817. {
  818. return HAL_ERROR;
  819. }
  820. else
  821. {
  822. htim->State = HAL_TIM_STATE_BUSY;
  823. }
  824. }
  825. else
  826. {
  827. /* nothing to do */
  828. }
  829. switch (Channel)
  830. {
  831. case TIM_CHANNEL_1:
  832. {
  833. /* Set the DMA compare callbacks */
  834. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  835. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  836. /* Set the DMA error callback */
  837. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  838. /* Enable the DMA stream */
  839. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
  840. {
  841. return HAL_ERROR;
  842. }
  843. /* Enable the TIM Capture/Compare 1 DMA request */
  844. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  845. break;
  846. }
  847. case TIM_CHANNEL_2:
  848. {
  849. /* Set the DMA compare callbacks */
  850. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  851. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  852. /* Set the DMA error callback */
  853. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  854. /* Enable the DMA stream */
  855. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
  856. {
  857. return HAL_ERROR;
  858. }
  859. /* Enable the TIM Capture/Compare 2 DMA request */
  860. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  861. break;
  862. }
  863. case TIM_CHANNEL_3:
  864. {
  865. /* Set the DMA compare callbacks */
  866. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  867. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  868. /* Set the DMA error callback */
  869. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  870. /* Enable the DMA stream */
  871. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
  872. {
  873. return HAL_ERROR;
  874. }
  875. /* Enable the TIM Capture/Compare 3 DMA request */
  876. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  877. break;
  878. }
  879. case TIM_CHANNEL_4:
  880. {
  881. /* Set the DMA compare callbacks */
  882. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  883. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  884. /* Set the DMA error callback */
  885. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  886. /* Enable the DMA stream */
  887. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length) != HAL_OK)
  888. {
  889. return HAL_ERROR;
  890. }
  891. /* Enable the TIM Capture/Compare 4 DMA request */
  892. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  893. break;
  894. }
  895. default:
  896. break;
  897. }
  898. /* Enable the Output compare channel */
  899. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  900. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  901. {
  902. /* Enable the main output */
  903. __HAL_TIM_MOE_ENABLE(htim);
  904. }
  905. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  906. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  907. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  908. {
  909. __HAL_TIM_ENABLE(htim);
  910. }
  911. /* Return function status */
  912. return HAL_OK;
  913. }
  914. /**
  915. * @brief Stops the TIM Output Compare signal generation in DMA mode.
  916. * @param htim TIM Output Compare handle
  917. * @param Channel TIM Channel to be disabled
  918. * This parameter can be one of the following values:
  919. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  920. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  921. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  922. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  923. * @retval HAL status
  924. */
  925. HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  926. {
  927. /* Check the parameters */
  928. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  929. switch (Channel)
  930. {
  931. case TIM_CHANNEL_1:
  932. {
  933. /* Disable the TIM Capture/Compare 1 DMA request */
  934. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  935. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  936. break;
  937. }
  938. case TIM_CHANNEL_2:
  939. {
  940. /* Disable the TIM Capture/Compare 2 DMA request */
  941. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  942. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  943. break;
  944. }
  945. case TIM_CHANNEL_3:
  946. {
  947. /* Disable the TIM Capture/Compare 3 DMA request */
  948. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  949. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  950. break;
  951. }
  952. case TIM_CHANNEL_4:
  953. {
  954. /* Disable the TIM Capture/Compare 4 interrupt */
  955. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  956. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  957. break;
  958. }
  959. default:
  960. break;
  961. }
  962. /* Disable the Output compare channel */
  963. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  964. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  965. {
  966. /* Disable the Main Output */
  967. __HAL_TIM_MOE_DISABLE(htim);
  968. }
  969. /* Disable the Peripheral */
  970. __HAL_TIM_DISABLE(htim);
  971. /* Change the htim state */
  972. htim->State = HAL_TIM_STATE_READY;
  973. /* Return function status */
  974. return HAL_OK;
  975. }
  976. /**
  977. * @}
  978. */
  979. /** @defgroup TIM_Exported_Functions_Group3 TIM PWM functions
  980. * @brief TIM PWM functions
  981. *
  982. @verbatim
  983. ==============================================================================
  984. ##### TIM PWM functions #####
  985. ==============================================================================
  986. [..]
  987. This section provides functions allowing to:
  988. (+) Initialize and configure the TIM PWM.
  989. (+) De-initialize the TIM PWM.
  990. (+) Start the TIM PWM.
  991. (+) Stop the TIM PWM.
  992. (+) Start the TIM PWM and enable interrupt.
  993. (+) Stop the TIM PWM and disable interrupt.
  994. (+) Start the TIM PWM and enable DMA transfer.
  995. (+) Stop the TIM PWM and disable DMA transfer.
  996. @endverbatim
  997. * @{
  998. */
  999. /**
  1000. * @brief Initializes the TIM PWM Time Base according to the specified
  1001. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  1002. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1003. * requires a timer reset to avoid unexpected direction
  1004. * due to DIR bit readonly in center aligned mode.
  1005. * Ex: call @ref HAL_TIM_PWM_DeInit() before HAL_TIM_PWM_Init()
  1006. * @param htim TIM PWM handle
  1007. * @retval HAL status
  1008. */
  1009. HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)
  1010. {
  1011. /* Check the TIM handle allocation */
  1012. if (htim == NULL)
  1013. {
  1014. return HAL_ERROR;
  1015. }
  1016. /* Check the parameters */
  1017. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1018. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1019. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1020. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1021. if (htim->State == HAL_TIM_STATE_RESET)
  1022. {
  1023. /* Allocate lock resource and initialize it */
  1024. htim->Lock = HAL_UNLOCKED;
  1025. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1026. /* Reset interrupt callbacks to legacy weak callbacks */
  1027. TIM_ResetCallback(htim);
  1028. if (htim->PWM_MspInitCallback == NULL)
  1029. {
  1030. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
  1031. }
  1032. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  1033. htim->PWM_MspInitCallback(htim);
  1034. #else
  1035. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1036. HAL_TIM_PWM_MspInit(htim);
  1037. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1038. }
  1039. /* Set the TIM state */
  1040. htim->State = HAL_TIM_STATE_BUSY;
  1041. /* Init the base time for the PWM */
  1042. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1043. /* Initialize the TIM state*/
  1044. htim->State = HAL_TIM_STATE_READY;
  1045. return HAL_OK;
  1046. }
  1047. /**
  1048. * @brief DeInitializes the TIM peripheral
  1049. * @param htim TIM PWM handle
  1050. * @retval HAL status
  1051. */
  1052. HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)
  1053. {
  1054. /* Check the parameters */
  1055. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1056. htim->State = HAL_TIM_STATE_BUSY;
  1057. /* Disable the TIM Peripheral Clock */
  1058. __HAL_TIM_DISABLE(htim);
  1059. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1060. if (htim->PWM_MspDeInitCallback == NULL)
  1061. {
  1062. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
  1063. }
  1064. /* DeInit the low level hardware */
  1065. htim->PWM_MspDeInitCallback(htim);
  1066. #else
  1067. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  1068. HAL_TIM_PWM_MspDeInit(htim);
  1069. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1070. /* Change TIM state */
  1071. htim->State = HAL_TIM_STATE_RESET;
  1072. /* Release Lock */
  1073. __HAL_UNLOCK(htim);
  1074. return HAL_OK;
  1075. }
  1076. /**
  1077. * @brief Initializes the TIM PWM MSP.
  1078. * @param htim TIM PWM handle
  1079. * @retval None
  1080. */
  1081. __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
  1082. {
  1083. /* Prevent unused argument(s) compilation warning */
  1084. UNUSED(htim);
  1085. /* NOTE : This function should not be modified, when the callback is needed,
  1086. the HAL_TIM_PWM_MspInit could be implemented in the user file
  1087. */
  1088. }
  1089. /**
  1090. * @brief DeInitializes TIM PWM MSP.
  1091. * @param htim TIM PWM handle
  1092. * @retval None
  1093. */
  1094. __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)
  1095. {
  1096. /* Prevent unused argument(s) compilation warning */
  1097. UNUSED(htim);
  1098. /* NOTE : This function should not be modified, when the callback is needed,
  1099. the HAL_TIM_PWM_MspDeInit could be implemented in the user file
  1100. */
  1101. }
  1102. /**
  1103. * @brief Starts the PWM signal generation.
  1104. * @param htim TIM handle
  1105. * @param Channel TIM Channels to be enabled
  1106. * This parameter can be one of the following values:
  1107. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1108. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1109. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1110. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1111. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  1112. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  1113. * @retval HAL status
  1114. */
  1115. HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  1116. {
  1117. uint32_t tmpsmcr;
  1118. /* Check the parameters */
  1119. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1120. /* Enable the Capture compare channel */
  1121. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1122. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1123. {
  1124. /* Enable the main output */
  1125. __HAL_TIM_MOE_ENABLE(htim);
  1126. }
  1127. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1128. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1129. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1130. {
  1131. __HAL_TIM_ENABLE(htim);
  1132. }
  1133. /* Return function status */
  1134. return HAL_OK;
  1135. }
  1136. /**
  1137. * @brief Stops the PWM signal generation.
  1138. * @param htim TIM PWM handle
  1139. * @param Channel TIM Channels to be disabled
  1140. * This parameter can be one of the following values:
  1141. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1142. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1143. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1144. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1145. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  1146. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  1147. * @retval HAL status
  1148. */
  1149. HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  1150. {
  1151. /* Check the parameters */
  1152. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1153. /* Disable the Capture compare channel */
  1154. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1155. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1156. {
  1157. /* Disable the Main Output */
  1158. __HAL_TIM_MOE_DISABLE(htim);
  1159. }
  1160. /* Disable the Peripheral */
  1161. __HAL_TIM_DISABLE(htim);
  1162. /* Change the htim state */
  1163. htim->State = HAL_TIM_STATE_READY;
  1164. /* Return function status */
  1165. return HAL_OK;
  1166. }
  1167. /**
  1168. * @brief Starts the PWM signal generation in interrupt mode.
  1169. * @param htim TIM PWM handle
  1170. * @param Channel TIM Channel to be enabled
  1171. * This parameter can be one of the following values:
  1172. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1173. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1174. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1175. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1176. * @retval HAL status
  1177. */
  1178. HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1179. {
  1180. uint32_t tmpsmcr;
  1181. /* Check the parameters */
  1182. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1183. switch (Channel)
  1184. {
  1185. case TIM_CHANNEL_1:
  1186. {
  1187. /* Enable the TIM Capture/Compare 1 interrupt */
  1188. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1189. break;
  1190. }
  1191. case TIM_CHANNEL_2:
  1192. {
  1193. /* Enable the TIM Capture/Compare 2 interrupt */
  1194. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1195. break;
  1196. }
  1197. case TIM_CHANNEL_3:
  1198. {
  1199. /* Enable the TIM Capture/Compare 3 interrupt */
  1200. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1201. break;
  1202. }
  1203. case TIM_CHANNEL_4:
  1204. {
  1205. /* Enable the TIM Capture/Compare 4 interrupt */
  1206. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1207. break;
  1208. }
  1209. default:
  1210. break;
  1211. }
  1212. /* Enable the Capture compare channel */
  1213. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1214. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1215. {
  1216. /* Enable the main output */
  1217. __HAL_TIM_MOE_ENABLE(htim);
  1218. }
  1219. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1220. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1221. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1222. {
  1223. __HAL_TIM_ENABLE(htim);
  1224. }
  1225. /* Return function status */
  1226. return HAL_OK;
  1227. }
  1228. /**
  1229. * @brief Stops the PWM signal generation in interrupt mode.
  1230. * @param htim TIM PWM handle
  1231. * @param Channel TIM Channels to be disabled
  1232. * This parameter can be one of the following values:
  1233. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1234. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1235. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1236. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1237. * @retval HAL status
  1238. */
  1239. HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1240. {
  1241. /* Check the parameters */
  1242. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1243. switch (Channel)
  1244. {
  1245. case TIM_CHANNEL_1:
  1246. {
  1247. /* Disable the TIM Capture/Compare 1 interrupt */
  1248. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1249. break;
  1250. }
  1251. case TIM_CHANNEL_2:
  1252. {
  1253. /* Disable the TIM Capture/Compare 2 interrupt */
  1254. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1255. break;
  1256. }
  1257. case TIM_CHANNEL_3:
  1258. {
  1259. /* Disable the TIM Capture/Compare 3 interrupt */
  1260. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1261. break;
  1262. }
  1263. case TIM_CHANNEL_4:
  1264. {
  1265. /* Disable the TIM Capture/Compare 4 interrupt */
  1266. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1267. break;
  1268. }
  1269. default:
  1270. break;
  1271. }
  1272. /* Disable the Capture compare channel */
  1273. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1274. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1275. {
  1276. /* Disable the Main Output */
  1277. __HAL_TIM_MOE_DISABLE(htim);
  1278. }
  1279. /* Disable the Peripheral */
  1280. __HAL_TIM_DISABLE(htim);
  1281. /* Return function status */
  1282. return HAL_OK;
  1283. }
  1284. /**
  1285. * @brief Starts the TIM PWM signal generation in DMA mode.
  1286. * @param htim TIM PWM handle
  1287. * @param Channel TIM Channels to be enabled
  1288. * This parameter can be one of the following values:
  1289. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1290. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1291. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1292. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1293. * @param pData The source Buffer address.
  1294. * @param Length The length of data to be transferred from memory to TIM peripheral
  1295. * @retval HAL status
  1296. */
  1297. HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  1298. {
  1299. uint32_t tmpsmcr;
  1300. /* Check the parameters */
  1301. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1302. if (htim->State == HAL_TIM_STATE_BUSY)
  1303. {
  1304. return HAL_BUSY;
  1305. }
  1306. else if (htim->State == HAL_TIM_STATE_READY)
  1307. {
  1308. if ((pData == NULL) && (Length > 0U))
  1309. {
  1310. return HAL_ERROR;
  1311. }
  1312. else
  1313. {
  1314. htim->State = HAL_TIM_STATE_BUSY;
  1315. }
  1316. }
  1317. else
  1318. {
  1319. /* nothing to do */
  1320. }
  1321. switch (Channel)
  1322. {
  1323. case TIM_CHANNEL_1:
  1324. {
  1325. /* Set the DMA compare callbacks */
  1326. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1327. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1328. /* Set the DMA error callback */
  1329. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  1330. /* Enable the DMA stream */
  1331. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
  1332. {
  1333. return HAL_ERROR;
  1334. }
  1335. /* Enable the TIM Capture/Compare 1 DMA request */
  1336. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1337. break;
  1338. }
  1339. case TIM_CHANNEL_2:
  1340. {
  1341. /* Set the DMA compare callbacks */
  1342. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1343. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1344. /* Set the DMA error callback */
  1345. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  1346. /* Enable the DMA stream */
  1347. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
  1348. {
  1349. return HAL_ERROR;
  1350. }
  1351. /* Enable the TIM Capture/Compare 2 DMA request */
  1352. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1353. break;
  1354. }
  1355. case TIM_CHANNEL_3:
  1356. {
  1357. /* Set the DMA compare callbacks */
  1358. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1359. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1360. /* Set the DMA error callback */
  1361. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  1362. /* Enable the DMA stream */
  1363. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
  1364. {
  1365. return HAL_ERROR;
  1366. }
  1367. /* Enable the TIM Output Capture/Compare 3 request */
  1368. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1369. break;
  1370. }
  1371. case TIM_CHANNEL_4:
  1372. {
  1373. /* Set the DMA compare callbacks */
  1374. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1375. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1376. /* Set the DMA error callback */
  1377. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  1378. /* Enable the DMA stream */
  1379. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length) != HAL_OK)
  1380. {
  1381. return HAL_ERROR;
  1382. }
  1383. /* Enable the TIM Capture/Compare 4 DMA request */
  1384. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1385. break;
  1386. }
  1387. default:
  1388. break;
  1389. }
  1390. /* Enable the Capture compare channel */
  1391. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1392. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1393. {
  1394. /* Enable the main output */
  1395. __HAL_TIM_MOE_ENABLE(htim);
  1396. }
  1397. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1398. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1399. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1400. {
  1401. __HAL_TIM_ENABLE(htim);
  1402. }
  1403. /* Return function status */
  1404. return HAL_OK;
  1405. }
  1406. /**
  1407. * @brief Stops the TIM PWM signal generation in DMA mode.
  1408. * @param htim TIM PWM handle
  1409. * @param Channel TIM Channels to be disabled
  1410. * This parameter can be one of the following values:
  1411. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1412. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1413. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1414. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1415. * @retval HAL status
  1416. */
  1417. HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1418. {
  1419. /* Check the parameters */
  1420. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1421. switch (Channel)
  1422. {
  1423. case TIM_CHANNEL_1:
  1424. {
  1425. /* Disable the TIM Capture/Compare 1 DMA request */
  1426. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1427. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1428. break;
  1429. }
  1430. case TIM_CHANNEL_2:
  1431. {
  1432. /* Disable the TIM Capture/Compare 2 DMA request */
  1433. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1434. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1435. break;
  1436. }
  1437. case TIM_CHANNEL_3:
  1438. {
  1439. /* Disable the TIM Capture/Compare 3 DMA request */
  1440. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1441. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1442. break;
  1443. }
  1444. case TIM_CHANNEL_4:
  1445. {
  1446. /* Disable the TIM Capture/Compare 4 interrupt */
  1447. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1448. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  1449. break;
  1450. }
  1451. default:
  1452. break;
  1453. }
  1454. /* Disable the Capture compare channel */
  1455. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1456. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1457. {
  1458. /* Disable the Main Output */
  1459. __HAL_TIM_MOE_DISABLE(htim);
  1460. }
  1461. /* Disable the Peripheral */
  1462. __HAL_TIM_DISABLE(htim);
  1463. /* Change the htim state */
  1464. htim->State = HAL_TIM_STATE_READY;
  1465. /* Return function status */
  1466. return HAL_OK;
  1467. }
  1468. /**
  1469. * @}
  1470. */
  1471. /** @defgroup TIM_Exported_Functions_Group4 TIM Input Capture functions
  1472. * @brief TIM Input Capture functions
  1473. *
  1474. @verbatim
  1475. ==============================================================================
  1476. ##### TIM Input Capture functions #####
  1477. ==============================================================================
  1478. [..]
  1479. This section provides functions allowing to:
  1480. (+) Initialize and configure the TIM Input Capture.
  1481. (+) De-initialize the TIM Input Capture.
  1482. (+) Start the TIM Input Capture.
  1483. (+) Stop the TIM Input Capture.
  1484. (+) Start the TIM Input Capture and enable interrupt.
  1485. (+) Stop the TIM Input Capture and disable interrupt.
  1486. (+) Start the TIM Input Capture and enable DMA transfer.
  1487. (+) Stop the TIM Input Capture and disable DMA transfer.
  1488. @endverbatim
  1489. * @{
  1490. */
  1491. /**
  1492. * @brief Initializes the TIM Input Capture Time base according to the specified
  1493. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  1494. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1495. * requires a timer reset to avoid unexpected direction
  1496. * due to DIR bit readonly in center aligned mode.
  1497. * Ex: call @ref HAL_TIM_IC_DeInit() before HAL_TIM_IC_Init()
  1498. * @param htim TIM Input Capture handle
  1499. * @retval HAL status
  1500. */
  1501. HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)
  1502. {
  1503. /* Check the TIM handle allocation */
  1504. if (htim == NULL)
  1505. {
  1506. return HAL_ERROR;
  1507. }
  1508. /* Check the parameters */
  1509. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1510. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1511. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1512. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1513. if (htim->State == HAL_TIM_STATE_RESET)
  1514. {
  1515. /* Allocate lock resource and initialize it */
  1516. htim->Lock = HAL_UNLOCKED;
  1517. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1518. /* Reset interrupt callbacks to legacy weak callbacks */
  1519. TIM_ResetCallback(htim);
  1520. if (htim->IC_MspInitCallback == NULL)
  1521. {
  1522. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
  1523. }
  1524. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  1525. htim->IC_MspInitCallback(htim);
  1526. #else
  1527. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1528. HAL_TIM_IC_MspInit(htim);
  1529. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1530. }
  1531. /* Set the TIM state */
  1532. htim->State = HAL_TIM_STATE_BUSY;
  1533. /* Init the base time for the input capture */
  1534. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1535. /* Initialize the TIM state*/
  1536. htim->State = HAL_TIM_STATE_READY;
  1537. return HAL_OK;
  1538. }
  1539. /**
  1540. * @brief DeInitializes the TIM peripheral
  1541. * @param htim TIM Input Capture handle
  1542. * @retval HAL status
  1543. */
  1544. HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)
  1545. {
  1546. /* Check the parameters */
  1547. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1548. htim->State = HAL_TIM_STATE_BUSY;
  1549. /* Disable the TIM Peripheral Clock */
  1550. __HAL_TIM_DISABLE(htim);
  1551. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1552. if (htim->IC_MspDeInitCallback == NULL)
  1553. {
  1554. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
  1555. }
  1556. /* DeInit the low level hardware */
  1557. htim->IC_MspDeInitCallback(htim);
  1558. #else
  1559. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  1560. HAL_TIM_IC_MspDeInit(htim);
  1561. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1562. /* Change TIM state */
  1563. htim->State = HAL_TIM_STATE_RESET;
  1564. /* Release Lock */
  1565. __HAL_UNLOCK(htim);
  1566. return HAL_OK;
  1567. }
  1568. /**
  1569. * @brief Initializes the TIM Input Capture MSP.
  1570. * @param htim TIM Input Capture handle
  1571. * @retval None
  1572. */
  1573. __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
  1574. {
  1575. /* Prevent unused argument(s) compilation warning */
  1576. UNUSED(htim);
  1577. /* NOTE : This function should not be modified, when the callback is needed,
  1578. the HAL_TIM_IC_MspInit could be implemented in the user file
  1579. */
  1580. }
  1581. /**
  1582. * @brief DeInitializes TIM Input Capture MSP.
  1583. * @param htim TIM handle
  1584. * @retval None
  1585. */
  1586. __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)
  1587. {
  1588. /* Prevent unused argument(s) compilation warning */
  1589. UNUSED(htim);
  1590. /* NOTE : This function should not be modified, when the callback is needed,
  1591. the HAL_TIM_IC_MspDeInit could be implemented in the user file
  1592. */
  1593. }
  1594. /**
  1595. * @brief Starts the TIM Input Capture measurement.
  1596. * @param htim TIM Input Capture handle
  1597. * @param Channel TIM Channels to be enabled
  1598. * This parameter can be one of the following values:
  1599. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1600. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1601. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1602. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1603. * @retval HAL status
  1604. */
  1605. HAL_StatusTypeDef HAL_TIM_IC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  1606. {
  1607. uint32_t tmpsmcr;
  1608. /* Check the parameters */
  1609. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1610. /* Enable the Input Capture channel */
  1611. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1612. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1613. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1614. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1615. {
  1616. __HAL_TIM_ENABLE(htim);
  1617. }
  1618. /* Return function status */
  1619. return HAL_OK;
  1620. }
  1621. /**
  1622. * @brief Stops the TIM Input Capture measurement.
  1623. * @param htim TIM Input Capture handle
  1624. * @param Channel TIM Channels to be disabled
  1625. * This parameter can be one of the following values:
  1626. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1627. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1628. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1629. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1630. * @retval HAL status
  1631. */
  1632. HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  1633. {
  1634. /* Check the parameters */
  1635. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1636. /* Disable the Input Capture channel */
  1637. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1638. /* Disable the Peripheral */
  1639. __HAL_TIM_DISABLE(htim);
  1640. /* Return function status */
  1641. return HAL_OK;
  1642. }
  1643. /**
  1644. * @brief Starts the TIM Input Capture measurement in interrupt mode.
  1645. * @param htim TIM Input Capture handle
  1646. * @param Channel TIM Channels to be enabled
  1647. * This parameter can be one of the following values:
  1648. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1649. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1650. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1651. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1652. * @retval HAL status
  1653. */
  1654. HAL_StatusTypeDef HAL_TIM_IC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1655. {
  1656. uint32_t tmpsmcr;
  1657. /* Check the parameters */
  1658. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1659. switch (Channel)
  1660. {
  1661. case TIM_CHANNEL_1:
  1662. {
  1663. /* Enable the TIM Capture/Compare 1 interrupt */
  1664. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1665. break;
  1666. }
  1667. case TIM_CHANNEL_2:
  1668. {
  1669. /* Enable the TIM Capture/Compare 2 interrupt */
  1670. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1671. break;
  1672. }
  1673. case TIM_CHANNEL_3:
  1674. {
  1675. /* Enable the TIM Capture/Compare 3 interrupt */
  1676. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1677. break;
  1678. }
  1679. case TIM_CHANNEL_4:
  1680. {
  1681. /* Enable the TIM Capture/Compare 4 interrupt */
  1682. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1683. break;
  1684. }
  1685. default:
  1686. break;
  1687. }
  1688. /* Enable the Input Capture channel */
  1689. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1690. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1691. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1692. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1693. {
  1694. __HAL_TIM_ENABLE(htim);
  1695. }
  1696. /* Return function status */
  1697. return HAL_OK;
  1698. }
  1699. /**
  1700. * @brief Stops the TIM Input Capture measurement in interrupt mode.
  1701. * @param htim TIM Input Capture handle
  1702. * @param Channel TIM Channels to be disabled
  1703. * This parameter can be one of the following values:
  1704. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1705. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1706. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1707. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1708. * @retval HAL status
  1709. */
  1710. HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1711. {
  1712. /* Check the parameters */
  1713. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1714. switch (Channel)
  1715. {
  1716. case TIM_CHANNEL_1:
  1717. {
  1718. /* Disable the TIM Capture/Compare 1 interrupt */
  1719. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1720. break;
  1721. }
  1722. case TIM_CHANNEL_2:
  1723. {
  1724. /* Disable the TIM Capture/Compare 2 interrupt */
  1725. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1726. break;
  1727. }
  1728. case TIM_CHANNEL_3:
  1729. {
  1730. /* Disable the TIM Capture/Compare 3 interrupt */
  1731. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1732. break;
  1733. }
  1734. case TIM_CHANNEL_4:
  1735. {
  1736. /* Disable the TIM Capture/Compare 4 interrupt */
  1737. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1738. break;
  1739. }
  1740. default:
  1741. break;
  1742. }
  1743. /* Disable the Input Capture channel */
  1744. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1745. /* Disable the Peripheral */
  1746. __HAL_TIM_DISABLE(htim);
  1747. /* Return function status */
  1748. return HAL_OK;
  1749. }
  1750. /**
  1751. * @brief Starts the TIM Input Capture measurement in DMA mode.
  1752. * @param htim TIM Input Capture handle
  1753. * @param Channel TIM Channels to be enabled
  1754. * This parameter can be one of the following values:
  1755. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1756. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1757. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1758. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1759. * @param pData The destination Buffer address.
  1760. * @param Length The length of data to be transferred from TIM peripheral to memory.
  1761. * @retval HAL status
  1762. */
  1763. HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  1764. {
  1765. uint32_t tmpsmcr;
  1766. /* Check the parameters */
  1767. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1768. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  1769. if (htim->State == HAL_TIM_STATE_BUSY)
  1770. {
  1771. return HAL_BUSY;
  1772. }
  1773. else if (htim->State == HAL_TIM_STATE_READY)
  1774. {
  1775. if ((pData == NULL) && (Length > 0U))
  1776. {
  1777. return HAL_ERROR;
  1778. }
  1779. else
  1780. {
  1781. htim->State = HAL_TIM_STATE_BUSY;
  1782. }
  1783. }
  1784. else
  1785. {
  1786. /* nothing to do */
  1787. }
  1788. switch (Channel)
  1789. {
  1790. case TIM_CHANNEL_1:
  1791. {
  1792. /* Set the DMA capture callbacks */
  1793. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  1794. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1795. /* Set the DMA error callback */
  1796. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  1797. /* Enable the DMA stream */
  1798. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
  1799. {
  1800. return HAL_ERROR;
  1801. }
  1802. /* Enable the TIM Capture/Compare 1 DMA request */
  1803. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1804. break;
  1805. }
  1806. case TIM_CHANNEL_2:
  1807. {
  1808. /* Set the DMA capture callbacks */
  1809. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  1810. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1811. /* Set the DMA error callback */
  1812. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  1813. /* Enable the DMA stream */
  1814. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData, Length) != HAL_OK)
  1815. {
  1816. return HAL_ERROR;
  1817. }
  1818. /* Enable the TIM Capture/Compare 2 DMA request */
  1819. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1820. break;
  1821. }
  1822. case TIM_CHANNEL_3:
  1823. {
  1824. /* Set the DMA capture callbacks */
  1825. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  1826. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1827. /* Set the DMA error callback */
  1828. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  1829. /* Enable the DMA stream */
  1830. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData, Length) != HAL_OK)
  1831. {
  1832. return HAL_ERROR;
  1833. }
  1834. /* Enable the TIM Capture/Compare 3 DMA request */
  1835. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1836. break;
  1837. }
  1838. case TIM_CHANNEL_4:
  1839. {
  1840. /* Set the DMA capture callbacks */
  1841. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  1842. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1843. /* Set the DMA error callback */
  1844. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  1845. /* Enable the DMA stream */
  1846. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData, Length) != HAL_OK)
  1847. {
  1848. return HAL_ERROR;
  1849. }
  1850. /* Enable the TIM Capture/Compare 4 DMA request */
  1851. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1852. break;
  1853. }
  1854. default:
  1855. break;
  1856. }
  1857. /* Enable the Input Capture channel */
  1858. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1859. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1860. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1861. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1862. {
  1863. __HAL_TIM_ENABLE(htim);
  1864. }
  1865. /* Return function status */
  1866. return HAL_OK;
  1867. }
  1868. /**
  1869. * @brief Stops the TIM Input Capture measurement in DMA mode.
  1870. * @param htim TIM Input Capture handle
  1871. * @param Channel TIM Channels to be disabled
  1872. * This parameter can be one of the following values:
  1873. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1874. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1875. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1876. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1877. * @retval HAL status
  1878. */
  1879. HAL_StatusTypeDef HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1880. {
  1881. /* Check the parameters */
  1882. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1883. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  1884. switch (Channel)
  1885. {
  1886. case TIM_CHANNEL_1:
  1887. {
  1888. /* Disable the TIM Capture/Compare 1 DMA request */
  1889. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1890. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1891. break;
  1892. }
  1893. case TIM_CHANNEL_2:
  1894. {
  1895. /* Disable the TIM Capture/Compare 2 DMA request */
  1896. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1897. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1898. break;
  1899. }
  1900. case TIM_CHANNEL_3:
  1901. {
  1902. /* Disable the TIM Capture/Compare 3 DMA request */
  1903. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1904. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1905. break;
  1906. }
  1907. case TIM_CHANNEL_4:
  1908. {
  1909. /* Disable the TIM Capture/Compare 4 DMA request */
  1910. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1911. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  1912. break;
  1913. }
  1914. default:
  1915. break;
  1916. }
  1917. /* Disable the Input Capture channel */
  1918. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1919. /* Disable the Peripheral */
  1920. __HAL_TIM_DISABLE(htim);
  1921. /* Change the htim state */
  1922. htim->State = HAL_TIM_STATE_READY;
  1923. /* Return function status */
  1924. return HAL_OK;
  1925. }
  1926. /**
  1927. * @}
  1928. */
  1929. /** @defgroup TIM_Exported_Functions_Group5 TIM One Pulse functions
  1930. * @brief TIM One Pulse functions
  1931. *
  1932. @verbatim
  1933. ==============================================================================
  1934. ##### TIM One Pulse functions #####
  1935. ==============================================================================
  1936. [..]
  1937. This section provides functions allowing to:
  1938. (+) Initialize and configure the TIM One Pulse.
  1939. (+) De-initialize the TIM One Pulse.
  1940. (+) Start the TIM One Pulse.
  1941. (+) Stop the TIM One Pulse.
  1942. (+) Start the TIM One Pulse and enable interrupt.
  1943. (+) Stop the TIM One Pulse and disable interrupt.
  1944. (+) Start the TIM One Pulse and enable DMA transfer.
  1945. (+) Stop the TIM One Pulse and disable DMA transfer.
  1946. @endverbatim
  1947. * @{
  1948. */
  1949. /**
  1950. * @brief Initializes the TIM One Pulse Time Base according to the specified
  1951. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  1952. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1953. * requires a timer reset to avoid unexpected direction
  1954. * due to DIR bit readonly in center aligned mode.
  1955. * Ex: call @ref HAL_TIM_OnePulse_DeInit() before HAL_TIM_OnePulse_Init()
  1956. * @param htim TIM One Pulse handle
  1957. * @param OnePulseMode Select the One pulse mode.
  1958. * This parameter can be one of the following values:
  1959. * @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
  1960. * @arg TIM_OPMODE_REPETITIVE: Repetitive pulses will be generated.
  1961. * @retval HAL status
  1962. */
  1963. HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)
  1964. {
  1965. /* Check the TIM handle allocation */
  1966. if (htim == NULL)
  1967. {
  1968. return HAL_ERROR;
  1969. }
  1970. /* Check the parameters */
  1971. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1972. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1973. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1974. assert_param(IS_TIM_OPM_MODE(OnePulseMode));
  1975. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1976. if (htim->State == HAL_TIM_STATE_RESET)
  1977. {
  1978. /* Allocate lock resource and initialize it */
  1979. htim->Lock = HAL_UNLOCKED;
  1980. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1981. /* Reset interrupt callbacks to legacy weak callbacks */
  1982. TIM_ResetCallback(htim);
  1983. if (htim->OnePulse_MspInitCallback == NULL)
  1984. {
  1985. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
  1986. }
  1987. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  1988. htim->OnePulse_MspInitCallback(htim);
  1989. #else
  1990. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1991. HAL_TIM_OnePulse_MspInit(htim);
  1992. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1993. }
  1994. /* Set the TIM state */
  1995. htim->State = HAL_TIM_STATE_BUSY;
  1996. /* Configure the Time base in the One Pulse Mode */
  1997. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1998. /* Reset the OPM Bit */
  1999. htim->Instance->CR1 &= ~TIM_CR1_OPM;
  2000. /* Configure the OPM Mode */
  2001. htim->Instance->CR1 |= OnePulseMode;
  2002. /* Initialize the TIM state*/
  2003. htim->State = HAL_TIM_STATE_READY;
  2004. return HAL_OK;
  2005. }
  2006. /**
  2007. * @brief DeInitializes the TIM One Pulse
  2008. * @param htim TIM One Pulse handle
  2009. * @retval HAL status
  2010. */
  2011. HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)
  2012. {
  2013. /* Check the parameters */
  2014. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2015. htim->State = HAL_TIM_STATE_BUSY;
  2016. /* Disable the TIM Peripheral Clock */
  2017. __HAL_TIM_DISABLE(htim);
  2018. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2019. if (htim->OnePulse_MspDeInitCallback == NULL)
  2020. {
  2021. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
  2022. }
  2023. /* DeInit the low level hardware */
  2024. htim->OnePulse_MspDeInitCallback(htim);
  2025. #else
  2026. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  2027. HAL_TIM_OnePulse_MspDeInit(htim);
  2028. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2029. /* Change TIM state */
  2030. htim->State = HAL_TIM_STATE_RESET;
  2031. /* Release Lock */
  2032. __HAL_UNLOCK(htim);
  2033. return HAL_OK;
  2034. }
  2035. /**
  2036. * @brief Initializes the TIM One Pulse MSP.
  2037. * @param htim TIM One Pulse handle
  2038. * @retval None
  2039. */
  2040. __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
  2041. {
  2042. /* Prevent unused argument(s) compilation warning */
  2043. UNUSED(htim);
  2044. /* NOTE : This function should not be modified, when the callback is needed,
  2045. the HAL_TIM_OnePulse_MspInit could be implemented in the user file
  2046. */
  2047. }
  2048. /**
  2049. * @brief DeInitializes TIM One Pulse MSP.
  2050. * @param htim TIM One Pulse handle
  2051. * @retval None
  2052. */
  2053. __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)
  2054. {
  2055. /* Prevent unused argument(s) compilation warning */
  2056. UNUSED(htim);
  2057. /* NOTE : This function should not be modified, when the callback is needed,
  2058. the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
  2059. */
  2060. }
  2061. /**
  2062. * @brief Starts the TIM One Pulse signal generation.
  2063. * @param htim TIM One Pulse handle
  2064. * @param OutputChannel TIM Channels to be enabled
  2065. * This parameter can be one of the following values:
  2066. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2067. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2068. * @retval HAL status
  2069. */
  2070. HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2071. {
  2072. /* Prevent unused argument(s) compilation warning */
  2073. UNUSED(OutputChannel);
  2074. /* Enable the Capture compare and the Input Capture channels
  2075. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2076. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2077. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2078. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
  2079. No need to enable the counter, it's enabled automatically by hardware
  2080. (the counter starts in response to a stimulus and generate a pulse */
  2081. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2082. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2083. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  2084. {
  2085. /* Enable the main output */
  2086. __HAL_TIM_MOE_ENABLE(htim);
  2087. }
  2088. /* Return function status */
  2089. return HAL_OK;
  2090. }
  2091. /**
  2092. * @brief Stops the TIM One Pulse signal generation.
  2093. * @param htim TIM One Pulse handle
  2094. * @param OutputChannel TIM Channels to be disable
  2095. * This parameter can be one of the following values:
  2096. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2097. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2098. * @retval HAL status
  2099. */
  2100. HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2101. {
  2102. /* Prevent unused argument(s) compilation warning */
  2103. UNUSED(OutputChannel);
  2104. /* Disable the Capture compare and the Input Capture channels
  2105. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2106. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2107. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2108. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  2109. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2110. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2111. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  2112. {
  2113. /* Disable the Main Output */
  2114. __HAL_TIM_MOE_DISABLE(htim);
  2115. }
  2116. /* Disable the Peripheral */
  2117. __HAL_TIM_DISABLE(htim);
  2118. /* Return function status */
  2119. return HAL_OK;
  2120. }
  2121. /**
  2122. * @brief Starts the TIM One Pulse signal generation in interrupt mode.
  2123. * @param htim TIM One Pulse handle
  2124. * @param OutputChannel TIM Channels to be enabled
  2125. * This parameter can be one of the following values:
  2126. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2127. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2128. * @retval HAL status
  2129. */
  2130. HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2131. {
  2132. /* Prevent unused argument(s) compilation warning */
  2133. UNUSED(OutputChannel);
  2134. /* Enable the Capture compare and the Input Capture channels
  2135. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2136. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2137. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2138. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
  2139. No need to enable the counter, it's enabled automatically by hardware
  2140. (the counter starts in response to a stimulus and generate a pulse */
  2141. /* Enable the TIM Capture/Compare 1 interrupt */
  2142. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2143. /* Enable the TIM Capture/Compare 2 interrupt */
  2144. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2145. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2146. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2147. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  2148. {
  2149. /* Enable the main output */
  2150. __HAL_TIM_MOE_ENABLE(htim);
  2151. }
  2152. /* Return function status */
  2153. return HAL_OK;
  2154. }
  2155. /**
  2156. * @brief Stops the TIM One Pulse signal generation in interrupt mode.
  2157. * @param htim TIM One Pulse handle
  2158. * @param OutputChannel TIM Channels to be enabled
  2159. * This parameter can be one of the following values:
  2160. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2161. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2162. * @retval HAL status
  2163. */
  2164. HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2165. {
  2166. /* Prevent unused argument(s) compilation warning */
  2167. UNUSED(OutputChannel);
  2168. /* Disable the TIM Capture/Compare 1 interrupt */
  2169. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2170. /* Disable the TIM Capture/Compare 2 interrupt */
  2171. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2172. /* Disable the Capture compare and the Input Capture channels
  2173. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2174. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2175. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2176. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  2177. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2178. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2179. if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  2180. {
  2181. /* Disable the Main Output */
  2182. __HAL_TIM_MOE_DISABLE(htim);
  2183. }
  2184. /* Disable the Peripheral */
  2185. __HAL_TIM_DISABLE(htim);
  2186. /* Return function status */
  2187. return HAL_OK;
  2188. }
  2189. /**
  2190. * @}
  2191. */
  2192. /** @defgroup TIM_Exported_Functions_Group6 TIM Encoder functions
  2193. * @brief TIM Encoder functions
  2194. *
  2195. @verbatim
  2196. ==============================================================================
  2197. ##### TIM Encoder functions #####
  2198. ==============================================================================
  2199. [..]
  2200. This section provides functions allowing to:
  2201. (+) Initialize and configure the TIM Encoder.
  2202. (+) De-initialize the TIM Encoder.
  2203. (+) Start the TIM Encoder.
  2204. (+) Stop the TIM Encoder.
  2205. (+) Start the TIM Encoder and enable interrupt.
  2206. (+) Stop the TIM Encoder and disable interrupt.
  2207. (+) Start the TIM Encoder and enable DMA transfer.
  2208. (+) Stop the TIM Encoder and disable DMA transfer.
  2209. @endverbatim
  2210. * @{
  2211. */
  2212. /**
  2213. * @brief Initializes the TIM Encoder Interface and initialize the associated handle.
  2214. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  2215. * requires a timer reset to avoid unexpected direction
  2216. * due to DIR bit readonly in center aligned mode.
  2217. * Ex: call @ref HAL_TIM_Encoder_DeInit() before HAL_TIM_Encoder_Init()
  2218. * @note Encoder mode and External clock mode 2 are not compatible and must not be selected together
  2219. * Ex: A call for @ref HAL_TIM_Encoder_Init will erase the settings of @ref HAL_TIM_ConfigClockSource
  2220. * using TIM_CLOCKSOURCE_ETRMODE2 and vice versa
  2221. * @param htim TIM Encoder Interface handle
  2222. * @param sConfig TIM Encoder Interface configuration structure
  2223. * @retval HAL status
  2224. */
  2225. HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim, TIM_Encoder_InitTypeDef *sConfig)
  2226. {
  2227. uint32_t tmpsmcr;
  2228. uint32_t tmpccmr1;
  2229. uint32_t tmpccer;
  2230. /* Check the TIM handle allocation */
  2231. if (htim == NULL)
  2232. {
  2233. return HAL_ERROR;
  2234. }
  2235. /* Check the parameters */
  2236. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  2237. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  2238. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  2239. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2240. assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));
  2241. assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));
  2242. assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));
  2243. assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC1Polarity));
  2244. assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC2Polarity));
  2245. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  2246. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));
  2247. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  2248. assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));
  2249. if (htim->State == HAL_TIM_STATE_RESET)
  2250. {
  2251. /* Allocate lock resource and initialize it */
  2252. htim->Lock = HAL_UNLOCKED;
  2253. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2254. /* Reset interrupt callbacks to legacy weak callbacks */
  2255. TIM_ResetCallback(htim);
  2256. if (htim->Encoder_MspInitCallback == NULL)
  2257. {
  2258. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
  2259. }
  2260. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  2261. htim->Encoder_MspInitCallback(htim);
  2262. #else
  2263. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  2264. HAL_TIM_Encoder_MspInit(htim);
  2265. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2266. }
  2267. /* Set the TIM state */
  2268. htim->State = HAL_TIM_STATE_BUSY;
  2269. /* Reset the SMS and ECE bits */
  2270. htim->Instance->SMCR &= ~(TIM_SMCR_SMS | TIM_SMCR_ECE);
  2271. /* Configure the Time base in the Encoder Mode */
  2272. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  2273. /* Get the TIMx SMCR register value */
  2274. tmpsmcr = htim->Instance->SMCR;
  2275. /* Get the TIMx CCMR1 register value */
  2276. tmpccmr1 = htim->Instance->CCMR1;
  2277. /* Get the TIMx CCER register value */
  2278. tmpccer = htim->Instance->CCER;
  2279. /* Set the encoder Mode */
  2280. tmpsmcr |= sConfig->EncoderMode;
  2281. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  2282. tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);
  2283. tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8U));
  2284. /* Set the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
  2285. tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);
  2286. tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);
  2287. tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8U);
  2288. tmpccmr1 |= (sConfig->IC1Filter << 4U) | (sConfig->IC2Filter << 12U);
  2289. /* Set the TI1 and the TI2 Polarities */
  2290. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
  2291. tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);
  2292. tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4U);
  2293. /* Write to TIMx SMCR */
  2294. htim->Instance->SMCR = tmpsmcr;
  2295. /* Write to TIMx CCMR1 */
  2296. htim->Instance->CCMR1 = tmpccmr1;
  2297. /* Write to TIMx CCER */
  2298. htim->Instance->CCER = tmpccer;
  2299. /* Initialize the TIM state*/
  2300. htim->State = HAL_TIM_STATE_READY;
  2301. return HAL_OK;
  2302. }
  2303. /**
  2304. * @brief DeInitializes the TIM Encoder interface
  2305. * @param htim TIM Encoder Interface handle
  2306. * @retval HAL status
  2307. */
  2308. HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)
  2309. {
  2310. /* Check the parameters */
  2311. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2312. htim->State = HAL_TIM_STATE_BUSY;
  2313. /* Disable the TIM Peripheral Clock */
  2314. __HAL_TIM_DISABLE(htim);
  2315. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2316. if (htim->Encoder_MspDeInitCallback == NULL)
  2317. {
  2318. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
  2319. }
  2320. /* DeInit the low level hardware */
  2321. htim->Encoder_MspDeInitCallback(htim);
  2322. #else
  2323. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  2324. HAL_TIM_Encoder_MspDeInit(htim);
  2325. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2326. /* Change TIM state */
  2327. htim->State = HAL_TIM_STATE_RESET;
  2328. /* Release Lock */
  2329. __HAL_UNLOCK(htim);
  2330. return HAL_OK;
  2331. }
  2332. /**
  2333. * @brief Initializes the TIM Encoder Interface MSP.
  2334. * @param htim TIM Encoder Interface handle
  2335. * @retval None
  2336. */
  2337. __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
  2338. {
  2339. /* Prevent unused argument(s) compilation warning */
  2340. UNUSED(htim);
  2341. /* NOTE : This function should not be modified, when the callback is needed,
  2342. the HAL_TIM_Encoder_MspInit could be implemented in the user file
  2343. */
  2344. }
  2345. /**
  2346. * @brief DeInitializes TIM Encoder Interface MSP.
  2347. * @param htim TIM Encoder Interface handle
  2348. * @retval None
  2349. */
  2350. __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)
  2351. {
  2352. /* Prevent unused argument(s) compilation warning */
  2353. UNUSED(htim);
  2354. /* NOTE : This function should not be modified, when the callback is needed,
  2355. the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
  2356. */
  2357. }
  2358. /**
  2359. * @brief Starts the TIM Encoder Interface.
  2360. * @param htim TIM Encoder Interface handle
  2361. * @param Channel TIM Channels to be enabled
  2362. * This parameter can be one of the following values:
  2363. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2364. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2365. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2366. * @retval HAL status
  2367. */
  2368. HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  2369. {
  2370. /* Check the parameters */
  2371. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2372. /* Enable the encoder interface channels */
  2373. switch (Channel)
  2374. {
  2375. case TIM_CHANNEL_1:
  2376. {
  2377. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2378. break;
  2379. }
  2380. case TIM_CHANNEL_2:
  2381. {
  2382. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2383. break;
  2384. }
  2385. default :
  2386. {
  2387. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2388. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2389. break;
  2390. }
  2391. }
  2392. /* Enable the Peripheral */
  2393. __HAL_TIM_ENABLE(htim);
  2394. /* Return function status */
  2395. return HAL_OK;
  2396. }
  2397. /**
  2398. * @brief Stops the TIM Encoder Interface.
  2399. * @param htim TIM Encoder Interface handle
  2400. * @param Channel TIM Channels to be disabled
  2401. * This parameter can be one of the following values:
  2402. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2403. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2404. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2405. * @retval HAL status
  2406. */
  2407. HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  2408. {
  2409. /* Check the parameters */
  2410. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2411. /* Disable the Input Capture channels 1 and 2
  2412. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2413. switch (Channel)
  2414. {
  2415. case TIM_CHANNEL_1:
  2416. {
  2417. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2418. break;
  2419. }
  2420. case TIM_CHANNEL_2:
  2421. {
  2422. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2423. break;
  2424. }
  2425. default :
  2426. {
  2427. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2428. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2429. break;
  2430. }
  2431. }
  2432. /* Disable the Peripheral */
  2433. __HAL_TIM_DISABLE(htim);
  2434. /* Return function status */
  2435. return HAL_OK;
  2436. }
  2437. /**
  2438. * @brief Starts the TIM Encoder Interface in interrupt mode.
  2439. * @param htim TIM Encoder Interface handle
  2440. * @param Channel TIM Channels to be enabled
  2441. * This parameter can be one of the following values:
  2442. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2443. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2444. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2445. * @retval HAL status
  2446. */
  2447. HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  2448. {
  2449. /* Check the parameters */
  2450. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2451. /* Enable the encoder interface channels */
  2452. /* Enable the capture compare Interrupts 1 and/or 2 */
  2453. switch (Channel)
  2454. {
  2455. case TIM_CHANNEL_1:
  2456. {
  2457. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2458. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2459. break;
  2460. }
  2461. case TIM_CHANNEL_2:
  2462. {
  2463. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2464. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2465. break;
  2466. }
  2467. default :
  2468. {
  2469. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2470. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2471. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2472. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2473. break;
  2474. }
  2475. }
  2476. /* Enable the Peripheral */
  2477. __HAL_TIM_ENABLE(htim);
  2478. /* Return function status */
  2479. return HAL_OK;
  2480. }
  2481. /**
  2482. * @brief Stops the TIM Encoder Interface in interrupt mode.
  2483. * @param htim TIM Encoder Interface handle
  2484. * @param Channel TIM Channels to be disabled
  2485. * This parameter can be one of the following values:
  2486. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2487. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2488. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2489. * @retval HAL status
  2490. */
  2491. HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  2492. {
  2493. /* Check the parameters */
  2494. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2495. /* Disable the Input Capture channels 1 and 2
  2496. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2497. if (Channel == TIM_CHANNEL_1)
  2498. {
  2499. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2500. /* Disable the capture compare Interrupts 1 */
  2501. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2502. }
  2503. else if (Channel == TIM_CHANNEL_2)
  2504. {
  2505. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2506. /* Disable the capture compare Interrupts 2 */
  2507. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2508. }
  2509. else
  2510. {
  2511. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2512. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2513. /* Disable the capture compare Interrupts 1 and 2 */
  2514. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2515. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2516. }
  2517. /* Disable the Peripheral */
  2518. __HAL_TIM_DISABLE(htim);
  2519. /* Change the htim state */
  2520. htim->State = HAL_TIM_STATE_READY;
  2521. /* Return function status */
  2522. return HAL_OK;
  2523. }
  2524. /**
  2525. * @brief Starts the TIM Encoder Interface in DMA mode.
  2526. * @param htim TIM Encoder Interface handle
  2527. * @param Channel TIM Channels to be enabled
  2528. * This parameter can be one of the following values:
  2529. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2530. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2531. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2532. * @param pData1 The destination Buffer address for IC1.
  2533. * @param pData2 The destination Buffer address for IC2.
  2534. * @param Length The length of data to be transferred from TIM peripheral to memory.
  2535. * @retval HAL status
  2536. */
  2537. HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1,
  2538. uint32_t *pData2, uint16_t Length)
  2539. {
  2540. /* Check the parameters */
  2541. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  2542. if (htim->State == HAL_TIM_STATE_BUSY)
  2543. {
  2544. return HAL_BUSY;
  2545. }
  2546. else if (htim->State == HAL_TIM_STATE_READY)
  2547. {
  2548. if ((((pData1 == NULL) || (pData2 == NULL))) && (Length > 0U))
  2549. {
  2550. return HAL_ERROR;
  2551. }
  2552. else
  2553. {
  2554. htim->State = HAL_TIM_STATE_BUSY;
  2555. }
  2556. }
  2557. else
  2558. {
  2559. /* nothing to do */
  2560. }
  2561. switch (Channel)
  2562. {
  2563. case TIM_CHANNEL_1:
  2564. {
  2565. /* Set the DMA capture callbacks */
  2566. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  2567. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2568. /* Set the DMA error callback */
  2569. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  2570. /* Enable the DMA stream */
  2571. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length) != HAL_OK)
  2572. {
  2573. return HAL_ERROR;
  2574. }
  2575. /* Enable the TIM Input Capture DMA request */
  2576. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  2577. /* Enable the Peripheral */
  2578. __HAL_TIM_ENABLE(htim);
  2579. /* Enable the Capture compare channel */
  2580. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2581. break;
  2582. }
  2583. case TIM_CHANNEL_2:
  2584. {
  2585. /* Set the DMA capture callbacks */
  2586. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  2587. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2588. /* Set the DMA error callback */
  2589. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError;
  2590. /* Enable the DMA stream */
  2591. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length) != HAL_OK)
  2592. {
  2593. return HAL_ERROR;
  2594. }
  2595. /* Enable the TIM Input Capture DMA request */
  2596. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  2597. /* Enable the Peripheral */
  2598. __HAL_TIM_ENABLE(htim);
  2599. /* Enable the Capture compare channel */
  2600. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2601. break;
  2602. }
  2603. case TIM_CHANNEL_ALL:
  2604. {
  2605. /* Set the DMA capture callbacks */
  2606. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  2607. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2608. /* Set the DMA error callback */
  2609. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  2610. /* Enable the DMA stream */
  2611. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length) != HAL_OK)
  2612. {
  2613. return HAL_ERROR;
  2614. }
  2615. /* Set the DMA capture callbacks */
  2616. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  2617. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2618. /* Set the DMA error callback */
  2619. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  2620. /* Enable the DMA stream */
  2621. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length) != HAL_OK)
  2622. {
  2623. return HAL_ERROR;
  2624. }
  2625. /* Enable the Peripheral */
  2626. __HAL_TIM_ENABLE(htim);
  2627. /* Enable the Capture compare channel */
  2628. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2629. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2630. /* Enable the TIM Input Capture DMA request */
  2631. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  2632. /* Enable the TIM Input Capture DMA request */
  2633. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  2634. break;
  2635. }
  2636. default:
  2637. break;
  2638. }
  2639. /* Return function status */
  2640. return HAL_OK;
  2641. }
  2642. /**
  2643. * @brief Stops the TIM Encoder Interface in DMA mode.
  2644. * @param htim TIM Encoder Interface handle
  2645. * @param Channel TIM Channels to be enabled
  2646. * This parameter can be one of the following values:
  2647. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2648. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2649. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2650. * @retval HAL status
  2651. */
  2652. HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  2653. {
  2654. /* Check the parameters */
  2655. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  2656. /* Disable the Input Capture channels 1 and 2
  2657. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2658. if (Channel == TIM_CHANNEL_1)
  2659. {
  2660. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2661. /* Disable the capture compare DMA Request 1 */
  2662. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  2663. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  2664. }
  2665. else if (Channel == TIM_CHANNEL_2)
  2666. {
  2667. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2668. /* Disable the capture compare DMA Request 2 */
  2669. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  2670. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  2671. }
  2672. else
  2673. {
  2674. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2675. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2676. /* Disable the capture compare DMA Request 1 and 2 */
  2677. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  2678. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  2679. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  2680. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  2681. }
  2682. /* Disable the Peripheral */
  2683. __HAL_TIM_DISABLE(htim);
  2684. /* Change the htim state */
  2685. htim->State = HAL_TIM_STATE_READY;
  2686. /* Return function status */
  2687. return HAL_OK;
  2688. }
  2689. /**
  2690. * @}
  2691. */
  2692. /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
  2693. * @brief TIM IRQ handler management
  2694. *
  2695. @verbatim
  2696. ==============================================================================
  2697. ##### IRQ handler management #####
  2698. ==============================================================================
  2699. [..]
  2700. This section provides Timer IRQ handler function.
  2701. @endverbatim
  2702. * @{
  2703. */
  2704. /**
  2705. * @brief This function handles TIM interrupts requests.
  2706. * @param htim TIM handle
  2707. * @retval None
  2708. */
  2709. void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
  2710. {
  2711. /* Capture compare 1 event */
  2712. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)
  2713. {
  2714. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) != RESET)
  2715. {
  2716. {
  2717. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
  2718. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2719. /* Input capture event */
  2720. if ((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U)
  2721. {
  2722. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2723. htim->IC_CaptureCallback(htim);
  2724. #else
  2725. HAL_TIM_IC_CaptureCallback(htim);
  2726. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2727. }
  2728. /* Output compare event */
  2729. else
  2730. {
  2731. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2732. htim->OC_DelayElapsedCallback(htim);
  2733. htim->PWM_PulseFinishedCallback(htim);
  2734. #else
  2735. HAL_TIM_OC_DelayElapsedCallback(htim);
  2736. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2737. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2738. }
  2739. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2740. }
  2741. }
  2742. }
  2743. /* Capture compare 2 event */
  2744. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET)
  2745. {
  2746. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) != RESET)
  2747. {
  2748. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);
  2749. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2750. /* Input capture event */
  2751. if ((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U)
  2752. {
  2753. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2754. htim->IC_CaptureCallback(htim);
  2755. #else
  2756. HAL_TIM_IC_CaptureCallback(htim);
  2757. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2758. }
  2759. /* Output compare event */
  2760. else
  2761. {
  2762. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2763. htim->OC_DelayElapsedCallback(htim);
  2764. htim->PWM_PulseFinishedCallback(htim);
  2765. #else
  2766. HAL_TIM_OC_DelayElapsedCallback(htim);
  2767. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2768. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2769. }
  2770. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2771. }
  2772. }
  2773. /* Capture compare 3 event */
  2774. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET)
  2775. {
  2776. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) != RESET)
  2777. {
  2778. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);
  2779. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2780. /* Input capture event */
  2781. if ((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U)
  2782. {
  2783. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2784. htim->IC_CaptureCallback(htim);
  2785. #else
  2786. HAL_TIM_IC_CaptureCallback(htim);
  2787. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2788. }
  2789. /* Output compare event */
  2790. else
  2791. {
  2792. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2793. htim->OC_DelayElapsedCallback(htim);
  2794. htim->PWM_PulseFinishedCallback(htim);
  2795. #else
  2796. HAL_TIM_OC_DelayElapsedCallback(htim);
  2797. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2798. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2799. }
  2800. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2801. }
  2802. }
  2803. /* Capture compare 4 event */
  2804. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET)
  2805. {
  2806. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) != RESET)
  2807. {
  2808. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
  2809. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  2810. /* Input capture event */
  2811. if ((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U)
  2812. {
  2813. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2814. htim->IC_CaptureCallback(htim);
  2815. #else
  2816. HAL_TIM_IC_CaptureCallback(htim);
  2817. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2818. }
  2819. /* Output compare event */
  2820. else
  2821. {
  2822. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2823. htim->OC_DelayElapsedCallback(htim);
  2824. htim->PWM_PulseFinishedCallback(htim);
  2825. #else
  2826. HAL_TIM_OC_DelayElapsedCallback(htim);
  2827. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2828. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2829. }
  2830. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2831. }
  2832. }
  2833. /* TIM Update event */
  2834. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)
  2835. {
  2836. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_UPDATE) != RESET)
  2837. {
  2838. __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
  2839. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2840. htim->PeriodElapsedCallback(htim);
  2841. #else
  2842. HAL_TIM_PeriodElapsedCallback(htim);
  2843. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2844. }
  2845. }
  2846. /* TIM Break input event */
  2847. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK) != RESET)
  2848. {
  2849. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) != RESET)
  2850. {
  2851. __HAL_TIM_CLEAR_IT(htim, TIM_IT_BREAK);
  2852. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2853. htim->BreakCallback(htim);
  2854. #else
  2855. HAL_TIMEx_BreakCallback(htim);
  2856. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2857. }
  2858. }
  2859. /* TIM Break2 input event */
  2860. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK2) != RESET)
  2861. {
  2862. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) != RESET)
  2863. {
  2864. __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_BREAK2);
  2865. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2866. htim->Break2Callback(htim);
  2867. #else
  2868. HAL_TIMEx_Break2Callback(htim);
  2869. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2870. }
  2871. }
  2872. /* TIM Trigger detection event */
  2873. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_TRIGGER) != RESET)
  2874. {
  2875. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_TRIGGER) != RESET)
  2876. {
  2877. __HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);
  2878. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2879. htim->TriggerCallback(htim);
  2880. #else
  2881. HAL_TIM_TriggerCallback(htim);
  2882. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2883. }
  2884. }
  2885. /* TIM commutation event */
  2886. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_COM) != RESET)
  2887. {
  2888. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_COM) != RESET)
  2889. {
  2890. __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_COM);
  2891. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2892. htim->CommutationCallback(htim);
  2893. #else
  2894. HAL_TIMEx_CommutCallback(htim);
  2895. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2896. }
  2897. }
  2898. }
  2899. /**
  2900. * @}
  2901. */
  2902. /** @defgroup TIM_Exported_Functions_Group8 TIM Peripheral Control functions
  2903. * @brief TIM Peripheral Control functions
  2904. *
  2905. @verbatim
  2906. ==============================================================================
  2907. ##### Peripheral Control functions #####
  2908. ==============================================================================
  2909. [..]
  2910. This section provides functions allowing to:
  2911. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  2912. (+) Configure External Clock source.
  2913. (+) Configure Complementary channels, break features and dead time.
  2914. (+) Configure Master and the Slave synchronization.
  2915. (+) Configure the DMA Burst Mode.
  2916. @endverbatim
  2917. * @{
  2918. */
  2919. /**
  2920. * @brief Initializes the TIM Output Compare Channels according to the specified
  2921. * parameters in the TIM_OC_InitTypeDef.
  2922. * @param htim TIM Output Compare handle
  2923. * @param sConfig TIM Output Compare configuration structure
  2924. * @param Channel TIM Channels to configure
  2925. * This parameter can be one of the following values:
  2926. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2927. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2928. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  2929. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  2930. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  2931. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  2932. * @retval HAL status
  2933. */
  2934. HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim,
  2935. TIM_OC_InitTypeDef *sConfig,
  2936. uint32_t Channel)
  2937. {
  2938. /* Check the parameters */
  2939. assert_param(IS_TIM_CHANNELS(Channel));
  2940. assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
  2941. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  2942. /* Process Locked */
  2943. __HAL_LOCK(htim);
  2944. htim->State = HAL_TIM_STATE_BUSY;
  2945. switch (Channel)
  2946. {
  2947. case TIM_CHANNEL_1:
  2948. {
  2949. /* Check the parameters */
  2950. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  2951. /* Configure the TIM Channel 1 in Output Compare */
  2952. TIM_OC1_SetConfig(htim->Instance, sConfig);
  2953. break;
  2954. }
  2955. case TIM_CHANNEL_2:
  2956. {
  2957. /* Check the parameters */
  2958. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2959. /* Configure the TIM Channel 2 in Output Compare */
  2960. TIM_OC2_SetConfig(htim->Instance, sConfig);
  2961. break;
  2962. }
  2963. case TIM_CHANNEL_3:
  2964. {
  2965. /* Check the parameters */
  2966. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  2967. /* Configure the TIM Channel 3 in Output Compare */
  2968. TIM_OC3_SetConfig(htim->Instance, sConfig);
  2969. break;
  2970. }
  2971. case TIM_CHANNEL_4:
  2972. {
  2973. /* Check the parameters */
  2974. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  2975. /* Configure the TIM Channel 4 in Output Compare */
  2976. TIM_OC4_SetConfig(htim->Instance, sConfig);
  2977. break;
  2978. }
  2979. case TIM_CHANNEL_5:
  2980. {
  2981. /* Check the parameters */
  2982. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  2983. /* Configure the TIM Channel 5 in Output Compare */
  2984. TIM_OC5_SetConfig(htim->Instance, sConfig);
  2985. break;
  2986. }
  2987. case TIM_CHANNEL_6:
  2988. {
  2989. /* Check the parameters */
  2990. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  2991. /* Configure the TIM Channel 6 in Output Compare */
  2992. TIM_OC6_SetConfig(htim->Instance, sConfig);
  2993. break;
  2994. }
  2995. default:
  2996. break;
  2997. }
  2998. htim->State = HAL_TIM_STATE_READY;
  2999. __HAL_UNLOCK(htim);
  3000. return HAL_OK;
  3001. }
  3002. /**
  3003. * @brief Initializes the TIM Input Capture Channels according to the specified
  3004. * parameters in the TIM_IC_InitTypeDef.
  3005. * @param htim TIM IC handle
  3006. * @param sConfig TIM Input Capture configuration structure
  3007. * @param Channel TIM Channel to configure
  3008. * This parameter can be one of the following values:
  3009. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3010. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3011. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3012. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3013. * @retval HAL status
  3014. */
  3015. HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_IC_InitTypeDef *sConfig, uint32_t Channel)
  3016. {
  3017. /* Check the parameters */
  3018. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3019. assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
  3020. assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
  3021. assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
  3022. assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
  3023. /* Process Locked */
  3024. __HAL_LOCK(htim);
  3025. htim->State = HAL_TIM_STATE_BUSY;
  3026. if (Channel == TIM_CHANNEL_1)
  3027. {
  3028. /* TI1 Configuration */
  3029. TIM_TI1_SetConfig(htim->Instance,
  3030. sConfig->ICPolarity,
  3031. sConfig->ICSelection,
  3032. sConfig->ICFilter);
  3033. /* Reset the IC1PSC Bits */
  3034. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  3035. /* Set the IC1PSC value */
  3036. htim->Instance->CCMR1 |= sConfig->ICPrescaler;
  3037. }
  3038. else if (Channel == TIM_CHANNEL_2)
  3039. {
  3040. /* TI2 Configuration */
  3041. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3042. TIM_TI2_SetConfig(htim->Instance,
  3043. sConfig->ICPolarity,
  3044. sConfig->ICSelection,
  3045. sConfig->ICFilter);
  3046. /* Reset the IC2PSC Bits */
  3047. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  3048. /* Set the IC2PSC value */
  3049. htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8U);
  3050. }
  3051. else if (Channel == TIM_CHANNEL_3)
  3052. {
  3053. /* TI3 Configuration */
  3054. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3055. TIM_TI3_SetConfig(htim->Instance,
  3056. sConfig->ICPolarity,
  3057. sConfig->ICSelection,
  3058. sConfig->ICFilter);
  3059. /* Reset the IC3PSC Bits */
  3060. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
  3061. /* Set the IC3PSC value */
  3062. htim->Instance->CCMR2 |= sConfig->ICPrescaler;
  3063. }
  3064. else
  3065. {
  3066. /* TI4 Configuration */
  3067. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3068. TIM_TI4_SetConfig(htim->Instance,
  3069. sConfig->ICPolarity,
  3070. sConfig->ICSelection,
  3071. sConfig->ICFilter);
  3072. /* Reset the IC4PSC Bits */
  3073. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
  3074. /* Set the IC4PSC value */
  3075. htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8U);
  3076. }
  3077. htim->State = HAL_TIM_STATE_READY;
  3078. __HAL_UNLOCK(htim);
  3079. return HAL_OK;
  3080. }
  3081. /**
  3082. * @brief Initializes the TIM PWM channels according to the specified
  3083. * parameters in the TIM_OC_InitTypeDef.
  3084. * @param htim TIM PWM handle
  3085. * @param sConfig TIM PWM configuration structure
  3086. * @param Channel TIM Channels to be configured
  3087. * This parameter can be one of the following values:
  3088. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3089. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3090. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3091. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3092. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  3093. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  3094. * @retval HAL status
  3095. */
  3096. HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
  3097. TIM_OC_InitTypeDef *sConfig,
  3098. uint32_t Channel)
  3099. {
  3100. /* Check the parameters */
  3101. assert_param(IS_TIM_CHANNELS(Channel));
  3102. assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
  3103. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  3104. assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
  3105. /* Process Locked */
  3106. __HAL_LOCK(htim);
  3107. htim->State = HAL_TIM_STATE_BUSY;
  3108. switch (Channel)
  3109. {
  3110. case TIM_CHANNEL_1:
  3111. {
  3112. /* Check the parameters */
  3113. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3114. /* Configure the Channel 1 in PWM mode */
  3115. TIM_OC1_SetConfig(htim->Instance, sConfig);
  3116. /* Set the Preload enable bit for channel1 */
  3117. htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
  3118. /* Configure the Output Fast mode */
  3119. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
  3120. htim->Instance->CCMR1 |= sConfig->OCFastMode;
  3121. break;
  3122. }
  3123. case TIM_CHANNEL_2:
  3124. {
  3125. /* Check the parameters */
  3126. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3127. /* Configure the Channel 2 in PWM mode */
  3128. TIM_OC2_SetConfig(htim->Instance, sConfig);
  3129. /* Set the Preload enable bit for channel2 */
  3130. htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
  3131. /* Configure the Output Fast mode */
  3132. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
  3133. htim->Instance->CCMR1 |= sConfig->OCFastMode << 8U;
  3134. break;
  3135. }
  3136. case TIM_CHANNEL_3:
  3137. {
  3138. /* Check the parameters */
  3139. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3140. /* Configure the Channel 3 in PWM mode */
  3141. TIM_OC3_SetConfig(htim->Instance, sConfig);
  3142. /* Set the Preload enable bit for channel3 */
  3143. htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
  3144. /* Configure the Output Fast mode */
  3145. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
  3146. htim->Instance->CCMR2 |= sConfig->OCFastMode;
  3147. break;
  3148. }
  3149. case TIM_CHANNEL_4:
  3150. {
  3151. /* Check the parameters */
  3152. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3153. /* Configure the Channel 4 in PWM mode */
  3154. TIM_OC4_SetConfig(htim->Instance, sConfig);
  3155. /* Set the Preload enable bit for channel4 */
  3156. htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
  3157. /* Configure the Output Fast mode */
  3158. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
  3159. htim->Instance->CCMR2 |= sConfig->OCFastMode << 8U;
  3160. break;
  3161. }
  3162. case TIM_CHANNEL_5:
  3163. {
  3164. /* Check the parameters */
  3165. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  3166. /* Configure the Channel 5 in PWM mode */
  3167. TIM_OC5_SetConfig(htim->Instance, sConfig);
  3168. /* Set the Preload enable bit for channel5*/
  3169. htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
  3170. /* Configure the Output Fast mode */
  3171. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;
  3172. htim->Instance->CCMR3 |= sConfig->OCFastMode;
  3173. break;
  3174. }
  3175. case TIM_CHANNEL_6:
  3176. {
  3177. /* Check the parameters */
  3178. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  3179. /* Configure the Channel 6 in PWM mode */
  3180. TIM_OC6_SetConfig(htim->Instance, sConfig);
  3181. /* Set the Preload enable bit for channel6 */
  3182. htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
  3183. /* Configure the Output Fast mode */
  3184. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;
  3185. htim->Instance->CCMR3 |= sConfig->OCFastMode << 8U;
  3186. break;
  3187. }
  3188. default:
  3189. break;
  3190. }
  3191. htim->State = HAL_TIM_STATE_READY;
  3192. __HAL_UNLOCK(htim);
  3193. return HAL_OK;
  3194. }
  3195. /**
  3196. * @brief Initializes the TIM One Pulse Channels according to the specified
  3197. * parameters in the TIM_OnePulse_InitTypeDef.
  3198. * @param htim TIM One Pulse handle
  3199. * @param sConfig TIM One Pulse configuration structure
  3200. * @param OutputChannel TIM output channel to configure
  3201. * This parameter can be one of the following values:
  3202. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3203. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3204. * @param InputChannel TIM input Channel to configure
  3205. * This parameter can be one of the following values:
  3206. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3207. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3208. * @note To output a waveform with a minimum delay user can enable the fast
  3209. * mode by calling the @ref __HAL_TIM_ENABLE_OCxFAST macro. Then CCx
  3210. * output is forced in response to the edge detection on TIx input,
  3211. * without taking in account the comparison.
  3212. * @retval HAL status
  3213. */
  3214. HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef *sConfig,
  3215. uint32_t OutputChannel, uint32_t InputChannel)
  3216. {
  3217. TIM_OC_InitTypeDef temp1;
  3218. /* Check the parameters */
  3219. assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
  3220. assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
  3221. if (OutputChannel != InputChannel)
  3222. {
  3223. /* Process Locked */
  3224. __HAL_LOCK(htim);
  3225. htim->State = HAL_TIM_STATE_BUSY;
  3226. /* Extract the Output compare configuration from sConfig structure */
  3227. temp1.OCMode = sConfig->OCMode;
  3228. temp1.Pulse = sConfig->Pulse;
  3229. temp1.OCPolarity = sConfig->OCPolarity;
  3230. temp1.OCNPolarity = sConfig->OCNPolarity;
  3231. temp1.OCIdleState = sConfig->OCIdleState;
  3232. temp1.OCNIdleState = sConfig->OCNIdleState;
  3233. switch (OutputChannel)
  3234. {
  3235. case TIM_CHANNEL_1:
  3236. {
  3237. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3238. TIM_OC1_SetConfig(htim->Instance, &temp1);
  3239. break;
  3240. }
  3241. case TIM_CHANNEL_2:
  3242. {
  3243. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3244. TIM_OC2_SetConfig(htim->Instance, &temp1);
  3245. break;
  3246. }
  3247. default:
  3248. break;
  3249. }
  3250. switch (InputChannel)
  3251. {
  3252. case TIM_CHANNEL_1:
  3253. {
  3254. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3255. TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
  3256. sConfig->ICSelection, sConfig->ICFilter);
  3257. /* Reset the IC1PSC Bits */
  3258. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  3259. /* Select the Trigger source */
  3260. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  3261. htim->Instance->SMCR |= TIM_TS_TI1FP1;
  3262. /* Select the Slave Mode */
  3263. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3264. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  3265. break;
  3266. }
  3267. case TIM_CHANNEL_2:
  3268. {
  3269. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3270. TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
  3271. sConfig->ICSelection, sConfig->ICFilter);
  3272. /* Reset the IC2PSC Bits */
  3273. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  3274. /* Select the Trigger source */
  3275. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  3276. htim->Instance->SMCR |= TIM_TS_TI2FP2;
  3277. /* Select the Slave Mode */
  3278. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3279. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  3280. break;
  3281. }
  3282. default:
  3283. break;
  3284. }
  3285. htim->State = HAL_TIM_STATE_READY;
  3286. __HAL_UNLOCK(htim);
  3287. return HAL_OK;
  3288. }
  3289. else
  3290. {
  3291. return HAL_ERROR;
  3292. }
  3293. }
  3294. /**
  3295. * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
  3296. * @param htim TIM handle
  3297. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
  3298. * This parameter can be one of the following values:
  3299. * @arg TIM_DMABASE_CR1
  3300. * @arg TIM_DMABASE_CR2
  3301. * @arg TIM_DMABASE_SMCR
  3302. * @arg TIM_DMABASE_DIER
  3303. * @arg TIM_DMABASE_SR
  3304. * @arg TIM_DMABASE_EGR
  3305. * @arg TIM_DMABASE_CCMR1
  3306. * @arg TIM_DMABASE_CCMR2
  3307. * @arg TIM_DMABASE_CCER
  3308. * @arg TIM_DMABASE_CNT
  3309. * @arg TIM_DMABASE_PSC
  3310. * @arg TIM_DMABASE_ARR
  3311. * @arg TIM_DMABASE_RCR
  3312. * @arg TIM_DMABASE_CCR1
  3313. * @arg TIM_DMABASE_CCR2
  3314. * @arg TIM_DMABASE_CCR3
  3315. * @arg TIM_DMABASE_CCR4
  3316. * @arg TIM_DMABASE_BDTR
  3317. * @arg TIM_DMABASE_CCMR3
  3318. * @arg TIM_DMABASE_CCR5
  3319. * @arg TIM_DMABASE_CCR6
  3320. * @arg TIM_DMABASE_AF1
  3321. * @arg TIM_DMABASE_AF2
  3322. * @arg TIM_DMABASE_TISEL
  3323. *
  3324. * @param BurstRequestSrc TIM DMA Request sources
  3325. * This parameter can be one of the following values:
  3326. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3327. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3328. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3329. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3330. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3331. * @arg TIM_DMA_COM: TIM Commutation DMA source
  3332. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3333. * @param BurstBuffer The Buffer address.
  3334. * @param BurstLength DMA Burst length. This parameter can be one value
  3335. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3336. * @note This function should be used only when BurstLength is equal to DMA data transfer length.
  3337. * @retval HAL status
  3338. */
  3339. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3340. uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
  3341. {
  3342. return HAL_TIM_DMABurst_MultiWriteStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
  3343. ((BurstLength) >> 8U) + 1U);
  3344. }
  3345. /**
  3346. * @brief Configure the DMA Burst to transfer multiple Data from the memory to the TIM peripheral
  3347. * @param htim TIM handle
  3348. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
  3349. * This parameter can be one of the following values:
  3350. * @arg TIM_DMABASE_CR1
  3351. * @arg TIM_DMABASE_CR2
  3352. * @arg TIM_DMABASE_SMCR
  3353. * @arg TIM_DMABASE_DIER
  3354. * @arg TIM_DMABASE_SR
  3355. * @arg TIM_DMABASE_EGR
  3356. * @arg TIM_DMABASE_CCMR1
  3357. * @arg TIM_DMABASE_CCMR2
  3358. * @arg TIM_DMABASE_CCER
  3359. * @arg TIM_DMABASE_CNT
  3360. * @arg TIM_DMABASE_PSC
  3361. * @arg TIM_DMABASE_ARR
  3362. * @arg TIM_DMABASE_RCR
  3363. * @arg TIM_DMABASE_CCR1
  3364. * @arg TIM_DMABASE_CCR2
  3365. * @arg TIM_DMABASE_CCR3
  3366. * @arg TIM_DMABASE_CCR4
  3367. * @arg TIM_DMABASE_BDTR
  3368. * @arg TIM_DMABASE_CCMR3
  3369. * @arg TIM_DMABASE_CCR5
  3370. * @arg TIM_DMABASE_CCR6
  3371. * @arg TIM_DMABASE_AF1
  3372. * @arg TIM_DMABASE_AF2
  3373. * @arg TIM_DMABASE_TISEL
  3374. *
  3375. * @param BurstRequestSrc TIM DMA Request sources
  3376. * This parameter can be one of the following values:
  3377. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3378. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3379. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3380. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3381. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3382. * @arg TIM_DMA_COM: TIM Commutation DMA source
  3383. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3384. * @param BurstBuffer The Buffer address.
  3385. * @param BurstLength DMA Burst length. This parameter can be one value
  3386. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3387. * @param DataLength Data length. This parameter can be one value
  3388. * between 1 and 0xFFFF.
  3389. * @retval HAL status
  3390. */
  3391. HAL_StatusTypeDef HAL_TIM_DMABurst_MultiWriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3392. uint32_t BurstRequestSrc, uint32_t *BurstBuffer,
  3393. uint32_t BurstLength, uint32_t DataLength)
  3394. {
  3395. /* Check the parameters */
  3396. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  3397. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  3398. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3399. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  3400. assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
  3401. if (htim->State == HAL_TIM_STATE_BUSY)
  3402. {
  3403. return HAL_BUSY;
  3404. }
  3405. else if (htim->State == HAL_TIM_STATE_READY)
  3406. {
  3407. if ((BurstBuffer == NULL) && (BurstLength > 0U))
  3408. {
  3409. return HAL_ERROR;
  3410. }
  3411. else
  3412. {
  3413. htim->State = HAL_TIM_STATE_BUSY;
  3414. }
  3415. }
  3416. else
  3417. {
  3418. /* nothing to do */
  3419. }
  3420. switch (BurstRequestSrc)
  3421. {
  3422. case TIM_DMA_UPDATE:
  3423. {
  3424. /* Set the DMA Period elapsed callbacks */
  3425. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  3426. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  3427. /* Set the DMA error callback */
  3428. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  3429. /* Enable the DMA stream */
  3430. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer,
  3431. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3432. {
  3433. return HAL_ERROR;
  3434. }
  3435. break;
  3436. }
  3437. case TIM_DMA_CC1:
  3438. {
  3439. /* Set the DMA compare callbacks */
  3440. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3441. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3442. /* Set the DMA error callback */
  3443. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  3444. /* Enable the DMA stream */
  3445. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer,
  3446. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3447. {
  3448. return HAL_ERROR;
  3449. }
  3450. break;
  3451. }
  3452. case TIM_DMA_CC2:
  3453. {
  3454. /* Set the DMA compare callbacks */
  3455. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3456. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3457. /* Set the DMA error callback */
  3458. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  3459. /* Enable the DMA stream */
  3460. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer,
  3461. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3462. {
  3463. return HAL_ERROR;
  3464. }
  3465. break;
  3466. }
  3467. case TIM_DMA_CC3:
  3468. {
  3469. /* Set the DMA compare callbacks */
  3470. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3471. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3472. /* Set the DMA error callback */
  3473. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  3474. /* Enable the DMA stream */
  3475. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer,
  3476. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3477. {
  3478. return HAL_ERROR;
  3479. }
  3480. break;
  3481. }
  3482. case TIM_DMA_CC4:
  3483. {
  3484. /* Set the DMA compare callbacks */
  3485. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3486. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3487. /* Set the DMA error callback */
  3488. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  3489. /* Enable the DMA stream */
  3490. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer,
  3491. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3492. {
  3493. return HAL_ERROR;
  3494. }
  3495. break;
  3496. }
  3497. case TIM_DMA_COM:
  3498. {
  3499. /* Set the DMA commutation callbacks */
  3500. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  3501. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  3502. /* Set the DMA error callback */
  3503. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
  3504. /* Enable the DMA stream */
  3505. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)BurstBuffer,
  3506. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3507. {
  3508. return HAL_ERROR;
  3509. }
  3510. break;
  3511. }
  3512. case TIM_DMA_TRIGGER:
  3513. {
  3514. /* Set the DMA trigger callbacks */
  3515. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  3516. htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
  3517. /* Set the DMA error callback */
  3518. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  3519. /* Enable the DMA stream */
  3520. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer,
  3521. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3522. {
  3523. return HAL_ERROR;
  3524. }
  3525. break;
  3526. }
  3527. default:
  3528. break;
  3529. }
  3530. /* Configure the DMA Burst Mode */
  3531. htim->Instance->DCR = (BurstBaseAddress | BurstLength);
  3532. /* Enable the TIM DMA Request */
  3533. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  3534. htim->State = HAL_TIM_STATE_READY;
  3535. /* Return function status */
  3536. return HAL_OK;
  3537. }
  3538. /**
  3539. * @brief Stops the TIM DMA Burst mode
  3540. * @param htim TIM handle
  3541. * @param BurstRequestSrc TIM DMA Request sources to disable
  3542. * @retval HAL status
  3543. */
  3544. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  3545. {
  3546. HAL_StatusTypeDef status = HAL_OK;
  3547. /* Check the parameters */
  3548. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3549. /* Abort the DMA transfer (at least disable the DMA stream) */
  3550. switch (BurstRequestSrc)
  3551. {
  3552. case TIM_DMA_UPDATE:
  3553. {
  3554. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  3555. break;
  3556. }
  3557. case TIM_DMA_CC1:
  3558. {
  3559. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  3560. break;
  3561. }
  3562. case TIM_DMA_CC2:
  3563. {
  3564. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  3565. break;
  3566. }
  3567. case TIM_DMA_CC3:
  3568. {
  3569. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  3570. break;
  3571. }
  3572. case TIM_DMA_CC4:
  3573. {
  3574. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  3575. break;
  3576. }
  3577. case TIM_DMA_COM:
  3578. {
  3579. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
  3580. break;
  3581. }
  3582. case TIM_DMA_TRIGGER:
  3583. {
  3584. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
  3585. break;
  3586. }
  3587. default:
  3588. break;
  3589. }
  3590. if (HAL_OK == status)
  3591. {
  3592. /* Disable the TIM Update DMA request */
  3593. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  3594. }
  3595. /* Return function status */
  3596. return status;
  3597. }
  3598. /**
  3599. * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
  3600. * @param htim TIM handle
  3601. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
  3602. * This parameter can be one of the following values:
  3603. * @arg TIM_DMABASE_CR1
  3604. * @arg TIM_DMABASE_CR2
  3605. * @arg TIM_DMABASE_SMCR
  3606. * @arg TIM_DMABASE_DIER
  3607. * @arg TIM_DMABASE_SR
  3608. * @arg TIM_DMABASE_EGR
  3609. * @arg TIM_DMABASE_CCMR1
  3610. * @arg TIM_DMABASE_CCMR2
  3611. * @arg TIM_DMABASE_CCER
  3612. * @arg TIM_DMABASE_CNT
  3613. * @arg TIM_DMABASE_PSC
  3614. * @arg TIM_DMABASE_ARR
  3615. * @arg TIM_DMABASE_RCR
  3616. * @arg TIM_DMABASE_CCR1
  3617. * @arg TIM_DMABASE_CCR2
  3618. * @arg TIM_DMABASE_CCR3
  3619. * @arg TIM_DMABASE_CCR4
  3620. * @arg TIM_DMABASE_BDTR
  3621. * @arg TIM_DMABASE_CCMR3
  3622. * @arg TIM_DMABASE_CCR5
  3623. * @arg TIM_DMABASE_CCR6
  3624. * @arg TIM_DMABASE_AF1
  3625. * @arg TIM_DMABASE_AF2
  3626. * @arg TIM_DMABASE_TISEL
  3627. *
  3628. * @param BurstRequestSrc TIM DMA Request sources
  3629. * This parameter can be one of the following values:
  3630. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3631. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3632. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3633. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3634. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3635. * @arg TIM_DMA_COM: TIM Commutation DMA source
  3636. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3637. * @param BurstBuffer The Buffer address.
  3638. * @param BurstLength DMA Burst length. This parameter can be one value
  3639. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3640. * @note This function should be used only when BurstLength is equal to DMA data transfer length.
  3641. * @retval HAL status
  3642. */
  3643. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3644. uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
  3645. {
  3646. return HAL_TIM_DMABurst_MultiReadStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
  3647. ((BurstLength) >> 8U) + 1U);
  3648. }
  3649. /**
  3650. * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
  3651. * @param htim TIM handle
  3652. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
  3653. * This parameter can be one of the following values:
  3654. * @arg TIM_DMABASE_CR1
  3655. * @arg TIM_DMABASE_CR2
  3656. * @arg TIM_DMABASE_SMCR
  3657. * @arg TIM_DMABASE_DIER
  3658. * @arg TIM_DMABASE_SR
  3659. * @arg TIM_DMABASE_EGR
  3660. * @arg TIM_DMABASE_CCMR1
  3661. * @arg TIM_DMABASE_CCMR2
  3662. * @arg TIM_DMABASE_CCER
  3663. * @arg TIM_DMABASE_CNT
  3664. * @arg TIM_DMABASE_PSC
  3665. * @arg TIM_DMABASE_ARR
  3666. * @arg TIM_DMABASE_RCR
  3667. * @arg TIM_DMABASE_CCR1
  3668. * @arg TIM_DMABASE_CCR2
  3669. * @arg TIM_DMABASE_CCR3
  3670. * @arg TIM_DMABASE_CCR4
  3671. * @arg TIM_DMABASE_BDTR
  3672. * @arg TIM_DMABASE_CCMR3
  3673. * @arg TIM_DMABASE_CCR5
  3674. * @arg TIM_DMABASE_CCR6
  3675. * @arg TIM_DMABASE_AF1
  3676. * @arg TIM_DMABASE_AF2
  3677. * @arg TIM_DMABASE_TISEL
  3678. *
  3679. * @param BurstRequestSrc TIM DMA Request sources
  3680. * This parameter can be one of the following values:
  3681. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3682. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3683. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3684. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3685. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3686. * @arg TIM_DMA_COM: TIM Commutation DMA source
  3687. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3688. * @param BurstBuffer The Buffer address.
  3689. * @param BurstLength DMA Burst length. This parameter can be one value
  3690. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3691. * @param DataLength Data length. This parameter can be one value
  3692. * between 1 and 0xFFFF.
  3693. * @retval HAL status
  3694. */
  3695. HAL_StatusTypeDef HAL_TIM_DMABurst_MultiReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3696. uint32_t BurstRequestSrc, uint32_t *BurstBuffer,
  3697. uint32_t BurstLength, uint32_t DataLength)
  3698. {
  3699. /* Check the parameters */
  3700. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  3701. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  3702. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3703. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  3704. assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
  3705. if (htim->State == HAL_TIM_STATE_BUSY)
  3706. {
  3707. return HAL_BUSY;
  3708. }
  3709. else if (htim->State == HAL_TIM_STATE_READY)
  3710. {
  3711. if ((BurstBuffer == NULL) && (BurstLength > 0U))
  3712. {
  3713. return HAL_ERROR;
  3714. }
  3715. else
  3716. {
  3717. htim->State = HAL_TIM_STATE_BUSY;
  3718. }
  3719. }
  3720. else
  3721. {
  3722. /* nothing to do */
  3723. }
  3724. switch (BurstRequestSrc)
  3725. {
  3726. case TIM_DMA_UPDATE:
  3727. {
  3728. /* Set the DMA Period elapsed callbacks */
  3729. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  3730. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  3731. /* Set the DMA error callback */
  3732. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  3733. /* Enable the DMA stream */
  3734. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  3735. DataLength) != HAL_OK)
  3736. {
  3737. return HAL_ERROR;
  3738. }
  3739. break;
  3740. }
  3741. case TIM_DMA_CC1:
  3742. {
  3743. /* Set the DMA capture callbacks */
  3744. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  3745. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3746. /* Set the DMA error callback */
  3747. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  3748. /* Enable the DMA stream */
  3749. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  3750. DataLength) != HAL_OK)
  3751. {
  3752. return HAL_ERROR;
  3753. }
  3754. break;
  3755. }
  3756. case TIM_DMA_CC2:
  3757. {
  3758. /* Set the DMA capture callbacks */
  3759. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  3760. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3761. /* Set the DMA error callback */
  3762. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  3763. /* Enable the DMA stream */
  3764. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  3765. DataLength) != HAL_OK)
  3766. {
  3767. return HAL_ERROR;
  3768. }
  3769. break;
  3770. }
  3771. case TIM_DMA_CC3:
  3772. {
  3773. /* Set the DMA capture callbacks */
  3774. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  3775. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3776. /* Set the DMA error callback */
  3777. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  3778. /* Enable the DMA stream */
  3779. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  3780. DataLength) != HAL_OK)
  3781. {
  3782. return HAL_ERROR;
  3783. }
  3784. break;
  3785. }
  3786. case TIM_DMA_CC4:
  3787. {
  3788. /* Set the DMA capture callbacks */
  3789. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  3790. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3791. /* Set the DMA error callback */
  3792. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  3793. /* Enable the DMA stream */
  3794. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  3795. DataLength) != HAL_OK)
  3796. {
  3797. return HAL_ERROR;
  3798. }
  3799. break;
  3800. }
  3801. case TIM_DMA_COM:
  3802. {
  3803. /* Set the DMA commutation callbacks */
  3804. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  3805. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  3806. /* Set the DMA error callback */
  3807. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
  3808. /* Enable the DMA stream */
  3809. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  3810. DataLength) != HAL_OK)
  3811. {
  3812. return HAL_ERROR;
  3813. }
  3814. break;
  3815. }
  3816. case TIM_DMA_TRIGGER:
  3817. {
  3818. /* Set the DMA trigger callbacks */
  3819. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  3820. htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
  3821. /* Set the DMA error callback */
  3822. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  3823. /* Enable the DMA stream */
  3824. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  3825. DataLength) != HAL_OK)
  3826. {
  3827. return HAL_ERROR;
  3828. }
  3829. break;
  3830. }
  3831. default:
  3832. break;
  3833. }
  3834. /* Configure the DMA Burst Mode */
  3835. htim->Instance->DCR = (BurstBaseAddress | BurstLength);
  3836. /* Enable the TIM DMA Request */
  3837. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  3838. htim->State = HAL_TIM_STATE_READY;
  3839. /* Return function status */
  3840. return HAL_OK;
  3841. }
  3842. /**
  3843. * @brief Stop the DMA burst reading
  3844. * @param htim TIM handle
  3845. * @param BurstRequestSrc TIM DMA Request sources to disable.
  3846. * @retval HAL status
  3847. */
  3848. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  3849. {
  3850. HAL_StatusTypeDef status = HAL_OK;
  3851. /* Check the parameters */
  3852. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3853. /* Abort the DMA transfer (at least disable the DMA stream) */
  3854. switch (BurstRequestSrc)
  3855. {
  3856. case TIM_DMA_UPDATE:
  3857. {
  3858. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  3859. break;
  3860. }
  3861. case TIM_DMA_CC1:
  3862. {
  3863. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  3864. break;
  3865. }
  3866. case TIM_DMA_CC2:
  3867. {
  3868. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  3869. break;
  3870. }
  3871. case TIM_DMA_CC3:
  3872. {
  3873. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  3874. break;
  3875. }
  3876. case TIM_DMA_CC4:
  3877. {
  3878. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  3879. break;
  3880. }
  3881. case TIM_DMA_COM:
  3882. {
  3883. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_COMMUTATION]);
  3884. break;
  3885. }
  3886. case TIM_DMA_TRIGGER:
  3887. {
  3888. status = HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
  3889. break;
  3890. }
  3891. default:
  3892. break;
  3893. }
  3894. if (HAL_OK == status)
  3895. {
  3896. /* Disable the TIM Update DMA request */
  3897. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  3898. }
  3899. /* Return function status */
  3900. return status;
  3901. }
  3902. /**
  3903. * @brief Generate a software event
  3904. * @param htim TIM handle
  3905. * @param EventSource specifies the event source.
  3906. * This parameter can be one of the following values:
  3907. * @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
  3908. * @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
  3909. * @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
  3910. * @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
  3911. * @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
  3912. * @arg TIM_EVENTSOURCE_COM: Timer COM event source
  3913. * @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
  3914. * @arg TIM_EVENTSOURCE_BREAK: Timer Break event source
  3915. * @arg TIM_EVENTSOURCE_BREAK2: Timer Break2 event source
  3916. * @note Basic timers can only generate an update event.
  3917. * @note TIM_EVENTSOURCE_COM is relevant only with advanced timer instances.
  3918. * @note TIM_EVENTSOURCE_BREAK and TIM_EVENTSOURCE_BREAK2 are relevant
  3919. * only for timer instances supporting break input(s).
  3920. * @retval HAL status
  3921. */
  3922. HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
  3923. {
  3924. /* Check the parameters */
  3925. assert_param(IS_TIM_INSTANCE(htim->Instance));
  3926. assert_param(IS_TIM_EVENT_SOURCE(EventSource));
  3927. /* Process Locked */
  3928. __HAL_LOCK(htim);
  3929. /* Change the TIM state */
  3930. htim->State = HAL_TIM_STATE_BUSY;
  3931. /* Set the event sources */
  3932. htim->Instance->EGR = EventSource;
  3933. /* Change the TIM state */
  3934. htim->State = HAL_TIM_STATE_READY;
  3935. __HAL_UNLOCK(htim);
  3936. /* Return function status */
  3937. return HAL_OK;
  3938. }
  3939. /**
  3940. * @brief Configures the OCRef clear feature
  3941. * @param htim TIM handle
  3942. * @param sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
  3943. * contains the OCREF clear feature and parameters for the TIM peripheral.
  3944. * @param Channel specifies the TIM Channel
  3945. * This parameter can be one of the following values:
  3946. * @arg TIM_CHANNEL_1: TIM Channel 1
  3947. * @arg TIM_CHANNEL_2: TIM Channel 2
  3948. * @arg TIM_CHANNEL_3: TIM Channel 3
  3949. * @arg TIM_CHANNEL_4: TIM Channel 4
  3950. * @arg TIM_CHANNEL_5: TIM Channel 5
  3951. * @arg TIM_CHANNEL_6: TIM Channel 6
  3952. * @retval HAL status
  3953. */
  3954. HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
  3955. TIM_ClearInputConfigTypeDef *sClearInputConfig,
  3956. uint32_t Channel)
  3957. {
  3958. /* Check the parameters */
  3959. assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
  3960. assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
  3961. /* Process Locked */
  3962. __HAL_LOCK(htim);
  3963. htim->State = HAL_TIM_STATE_BUSY;
  3964. switch (sClearInputConfig->ClearInputSource)
  3965. {
  3966. case TIM_CLEARINPUTSOURCE_NONE:
  3967. {
  3968. /* Clear the OCREF clear selection bit and the the ETR Bits */
  3969. CLEAR_BIT(htim->Instance->SMCR, (TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP));
  3970. break;
  3971. }
  3972. case TIM_CLEARINPUTSOURCE_ETR:
  3973. {
  3974. /* Check the parameters */
  3975. assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
  3976. assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
  3977. assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
  3978. /* When OCRef clear feature is used with ETR source, ETR prescaler must be off */
  3979. if (sClearInputConfig->ClearInputPrescaler != TIM_CLEARINPUTPRESCALER_DIV1)
  3980. {
  3981. htim->State = HAL_TIM_STATE_READY;
  3982. __HAL_UNLOCK(htim);
  3983. return HAL_ERROR;
  3984. }
  3985. TIM_ETR_SetConfig(htim->Instance,
  3986. sClearInputConfig->ClearInputPrescaler,
  3987. sClearInputConfig->ClearInputPolarity,
  3988. sClearInputConfig->ClearInputFilter);
  3989. break;
  3990. }
  3991. default:
  3992. break;
  3993. }
  3994. switch (Channel)
  3995. {
  3996. case TIM_CHANNEL_1:
  3997. {
  3998. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  3999. {
  4000. /* Enable the OCREF clear feature for Channel 1 */
  4001. SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
  4002. }
  4003. else
  4004. {
  4005. /* Disable the OCREF clear feature for Channel 1 */
  4006. CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
  4007. }
  4008. break;
  4009. }
  4010. case TIM_CHANNEL_2:
  4011. {
  4012. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4013. {
  4014. /* Enable the OCREF clear feature for Channel 2 */
  4015. SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
  4016. }
  4017. else
  4018. {
  4019. /* Disable the OCREF clear feature for Channel 2 */
  4020. CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
  4021. }
  4022. break;
  4023. }
  4024. case TIM_CHANNEL_3:
  4025. {
  4026. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4027. {
  4028. /* Enable the OCREF clear feature for Channel 3 */
  4029. SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
  4030. }
  4031. else
  4032. {
  4033. /* Disable the OCREF clear feature for Channel 3 */
  4034. CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
  4035. }
  4036. break;
  4037. }
  4038. case TIM_CHANNEL_4:
  4039. {
  4040. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4041. {
  4042. /* Enable the OCREF clear feature for Channel 4 */
  4043. SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
  4044. }
  4045. else
  4046. {
  4047. /* Disable the OCREF clear feature for Channel 4 */
  4048. CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
  4049. }
  4050. break;
  4051. }
  4052. case TIM_CHANNEL_5:
  4053. {
  4054. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4055. {
  4056. /* Enable the OCREF clear feature for Channel 5 */
  4057. SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
  4058. }
  4059. else
  4060. {
  4061. /* Disable the OCREF clear feature for Channel 5 */
  4062. CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC5CE);
  4063. }
  4064. break;
  4065. }
  4066. case TIM_CHANNEL_6:
  4067. {
  4068. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4069. {
  4070. /* Enable the OCREF clear feature for Channel 6 */
  4071. SET_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
  4072. }
  4073. else
  4074. {
  4075. /* Disable the OCREF clear feature for Channel 6 */
  4076. CLEAR_BIT(htim->Instance->CCMR3, TIM_CCMR3_OC6CE);
  4077. }
  4078. break;
  4079. }
  4080. default:
  4081. break;
  4082. }
  4083. htim->State = HAL_TIM_STATE_READY;
  4084. __HAL_UNLOCK(htim);
  4085. return HAL_OK;
  4086. }
  4087. /**
  4088. * @brief Configures the clock source to be used
  4089. * @param htim TIM handle
  4090. * @param sClockSourceConfig pointer to a TIM_ClockConfigTypeDef structure that
  4091. * contains the clock source information for the TIM peripheral.
  4092. * @retval HAL status
  4093. */
  4094. HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, TIM_ClockConfigTypeDef *sClockSourceConfig)
  4095. {
  4096. uint32_t tmpsmcr;
  4097. /* Process Locked */
  4098. __HAL_LOCK(htim);
  4099. htim->State = HAL_TIM_STATE_BUSY;
  4100. /* Check the parameters */
  4101. assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
  4102. /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
  4103. tmpsmcr = htim->Instance->SMCR;
  4104. tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
  4105. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  4106. htim->Instance->SMCR = tmpsmcr;
  4107. switch (sClockSourceConfig->ClockSource)
  4108. {
  4109. case TIM_CLOCKSOURCE_INTERNAL:
  4110. {
  4111. assert_param(IS_TIM_INSTANCE(htim->Instance));
  4112. break;
  4113. }
  4114. case TIM_CLOCKSOURCE_ETRMODE1:
  4115. {
  4116. /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
  4117. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  4118. /* Check ETR input conditioning related parameters */
  4119. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  4120. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4121. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4122. /* Configure the ETR Clock source */
  4123. TIM_ETR_SetConfig(htim->Instance,
  4124. sClockSourceConfig->ClockPrescaler,
  4125. sClockSourceConfig->ClockPolarity,
  4126. sClockSourceConfig->ClockFilter);
  4127. /* Select the External clock mode1 and the ETRF trigger */
  4128. tmpsmcr = htim->Instance->SMCR;
  4129. tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
  4130. /* Write to TIMx SMCR */
  4131. htim->Instance->SMCR = tmpsmcr;
  4132. break;
  4133. }
  4134. case TIM_CLOCKSOURCE_ETRMODE2:
  4135. {
  4136. /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
  4137. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
  4138. /* Check ETR input conditioning related parameters */
  4139. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  4140. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4141. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4142. /* Configure the ETR Clock source */
  4143. TIM_ETR_SetConfig(htim->Instance,
  4144. sClockSourceConfig->ClockPrescaler,
  4145. sClockSourceConfig->ClockPolarity,
  4146. sClockSourceConfig->ClockFilter);
  4147. /* Enable the External clock mode2 */
  4148. htim->Instance->SMCR |= TIM_SMCR_ECE;
  4149. break;
  4150. }
  4151. case TIM_CLOCKSOURCE_TI1:
  4152. {
  4153. /* Check whether or not the timer instance supports external clock mode 1 */
  4154. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4155. /* Check TI1 input conditioning related parameters */
  4156. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4157. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4158. TIM_TI1_ConfigInputStage(htim->Instance,
  4159. sClockSourceConfig->ClockPolarity,
  4160. sClockSourceConfig->ClockFilter);
  4161. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
  4162. break;
  4163. }
  4164. case TIM_CLOCKSOURCE_TI2:
  4165. {
  4166. /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
  4167. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4168. /* Check TI2 input conditioning related parameters */
  4169. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4170. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4171. TIM_TI2_ConfigInputStage(htim->Instance,
  4172. sClockSourceConfig->ClockPolarity,
  4173. sClockSourceConfig->ClockFilter);
  4174. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
  4175. break;
  4176. }
  4177. case TIM_CLOCKSOURCE_TI1ED:
  4178. {
  4179. /* Check whether or not the timer instance supports external clock mode 1 */
  4180. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4181. /* Check TI1 input conditioning related parameters */
  4182. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4183. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4184. TIM_TI1_ConfigInputStage(htim->Instance,
  4185. sClockSourceConfig->ClockPolarity,
  4186. sClockSourceConfig->ClockFilter);
  4187. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
  4188. break;
  4189. }
  4190. case TIM_CLOCKSOURCE_ITR0:
  4191. case TIM_CLOCKSOURCE_ITR1:
  4192. case TIM_CLOCKSOURCE_ITR2:
  4193. case TIM_CLOCKSOURCE_ITR3:
  4194. case TIM_CLOCKSOURCE_ITR4:
  4195. case TIM_CLOCKSOURCE_ITR5:
  4196. case TIM_CLOCKSOURCE_ITR6:
  4197. case TIM_CLOCKSOURCE_ITR7:
  4198. case TIM_CLOCKSOURCE_ITR8:
  4199. {
  4200. /* Check whether or not the timer instance supports internal trigger input */
  4201. assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
  4202. TIM_ITRx_SetConfig(htim->Instance, sClockSourceConfig->ClockSource);
  4203. break;
  4204. }
  4205. default:
  4206. break;
  4207. }
  4208. htim->State = HAL_TIM_STATE_READY;
  4209. __HAL_UNLOCK(htim);
  4210. return HAL_OK;
  4211. }
  4212. /**
  4213. * @brief Selects the signal connected to the TI1 input: direct from CH1_input
  4214. * or a XOR combination between CH1_input, CH2_input & CH3_input
  4215. * @param htim TIM handle.
  4216. * @param TI1_Selection Indicate whether or not channel 1 is connected to the
  4217. * output of a XOR gate.
  4218. * This parameter can be one of the following values:
  4219. * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
  4220. * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
  4221. * pins are connected to the TI1 input (XOR combination)
  4222. * @retval HAL status
  4223. */
  4224. HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
  4225. {
  4226. uint32_t tmpcr2;
  4227. /* Check the parameters */
  4228. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  4229. assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
  4230. /* Get the TIMx CR2 register value */
  4231. tmpcr2 = htim->Instance->CR2;
  4232. /* Reset the TI1 selection */
  4233. tmpcr2 &= ~TIM_CR2_TI1S;
  4234. /* Set the TI1 selection */
  4235. tmpcr2 |= TI1_Selection;
  4236. /* Write to TIMxCR2 */
  4237. htim->Instance->CR2 = tmpcr2;
  4238. return HAL_OK;
  4239. }
  4240. /**
  4241. * @brief Configures the TIM in Slave mode
  4242. * @param htim TIM handle.
  4243. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  4244. * contains the selected trigger (internal trigger input, filtered
  4245. * timer input or external trigger input) and the Slave mode
  4246. * (Disable, Reset, Gated, Trigger, External clock mode 1).
  4247. * @retval HAL status
  4248. */
  4249. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro(TIM_HandleTypeDef *htim, TIM_SlaveConfigTypeDef *sSlaveConfig)
  4250. {
  4251. /* Check the parameters */
  4252. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  4253. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  4254. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  4255. __HAL_LOCK(htim);
  4256. htim->State = HAL_TIM_STATE_BUSY;
  4257. if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
  4258. {
  4259. htim->State = HAL_TIM_STATE_READY;
  4260. __HAL_UNLOCK(htim);
  4261. return HAL_ERROR;
  4262. }
  4263. /* Disable Trigger Interrupt */
  4264. __HAL_TIM_DISABLE_IT(htim, TIM_IT_TRIGGER);
  4265. /* Disable Trigger DMA request */
  4266. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  4267. htim->State = HAL_TIM_STATE_READY;
  4268. __HAL_UNLOCK(htim);
  4269. return HAL_OK;
  4270. }
  4271. /**
  4272. * @brief Configures the TIM in Slave mode in interrupt mode
  4273. * @param htim TIM handle.
  4274. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  4275. * contains the selected trigger (internal trigger input, filtered
  4276. * timer input or external trigger input) and the Slave mode
  4277. * (Disable, Reset, Gated, Trigger, External clock mode 1).
  4278. * @retval HAL status
  4279. */
  4280. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro_IT(TIM_HandleTypeDef *htim,
  4281. TIM_SlaveConfigTypeDef *sSlaveConfig)
  4282. {
  4283. /* Check the parameters */
  4284. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  4285. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  4286. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  4287. __HAL_LOCK(htim);
  4288. htim->State = HAL_TIM_STATE_BUSY;
  4289. if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
  4290. {
  4291. htim->State = HAL_TIM_STATE_READY;
  4292. __HAL_UNLOCK(htim);
  4293. return HAL_ERROR;
  4294. }
  4295. /* Enable Trigger Interrupt */
  4296. __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);
  4297. /* Disable Trigger DMA request */
  4298. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  4299. htim->State = HAL_TIM_STATE_READY;
  4300. __HAL_UNLOCK(htim);
  4301. return HAL_OK;
  4302. }
  4303. /**
  4304. * @brief Read the captured value from Capture Compare unit
  4305. * @param htim TIM handle.
  4306. * @param Channel TIM Channels to be enabled
  4307. * This parameter can be one of the following values:
  4308. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  4309. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  4310. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  4311. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  4312. * @retval Captured value
  4313. */
  4314. uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)
  4315. {
  4316. uint32_t tmpreg = 0U;
  4317. switch (Channel)
  4318. {
  4319. case TIM_CHANNEL_1:
  4320. {
  4321. /* Check the parameters */
  4322. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  4323. /* Return the capture 1 value */
  4324. tmpreg = htim->Instance->CCR1;
  4325. break;
  4326. }
  4327. case TIM_CHANNEL_2:
  4328. {
  4329. /* Check the parameters */
  4330. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4331. /* Return the capture 2 value */
  4332. tmpreg = htim->Instance->CCR2;
  4333. break;
  4334. }
  4335. case TIM_CHANNEL_3:
  4336. {
  4337. /* Check the parameters */
  4338. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  4339. /* Return the capture 3 value */
  4340. tmpreg = htim->Instance->CCR3;
  4341. break;
  4342. }
  4343. case TIM_CHANNEL_4:
  4344. {
  4345. /* Check the parameters */
  4346. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  4347. /* Return the capture 4 value */
  4348. tmpreg = htim->Instance->CCR4;
  4349. break;
  4350. }
  4351. default:
  4352. break;
  4353. }
  4354. return tmpreg;
  4355. }
  4356. /**
  4357. * @}
  4358. */
  4359. /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
  4360. * @brief TIM Callbacks functions
  4361. *
  4362. @verbatim
  4363. ==============================================================================
  4364. ##### TIM Callbacks functions #####
  4365. ==============================================================================
  4366. [..]
  4367. This section provides TIM callback functions:
  4368. (+) TIM Period elapsed callback
  4369. (+) TIM Output Compare callback
  4370. (+) TIM Input capture callback
  4371. (+) TIM Trigger callback
  4372. (+) TIM Error callback
  4373. @endverbatim
  4374. * @{
  4375. */
  4376. /**
  4377. * @brief Period elapsed callback in non-blocking mode
  4378. * @param htim TIM handle
  4379. * @retval None
  4380. */
  4381. __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  4382. {
  4383. /* Prevent unused argument(s) compilation warning */
  4384. UNUSED(htim);
  4385. /* NOTE : This function should not be modified, when the callback is needed,
  4386. the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
  4387. */
  4388. }
  4389. /**
  4390. * @brief Period elapsed half complete callback in non-blocking mode
  4391. * @param htim TIM handle
  4392. * @retval None
  4393. */
  4394. __weak void HAL_TIM_PeriodElapsedHalfCpltCallback(TIM_HandleTypeDef *htim)
  4395. {
  4396. /* Prevent unused argument(s) compilation warning */
  4397. UNUSED(htim);
  4398. /* NOTE : This function should not be modified, when the callback is needed,
  4399. the HAL_TIM_PeriodElapsedHalfCpltCallback could be implemented in the user file
  4400. */
  4401. }
  4402. /**
  4403. * @brief Output Compare callback in non-blocking mode
  4404. * @param htim TIM OC handle
  4405. * @retval None
  4406. */
  4407. __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
  4408. {
  4409. /* Prevent unused argument(s) compilation warning */
  4410. UNUSED(htim);
  4411. /* NOTE : This function should not be modified, when the callback is needed,
  4412. the HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
  4413. */
  4414. }
  4415. /**
  4416. * @brief Input Capture callback in non-blocking mode
  4417. * @param htim TIM IC handle
  4418. * @retval None
  4419. */
  4420. __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  4421. {
  4422. /* Prevent unused argument(s) compilation warning */
  4423. UNUSED(htim);
  4424. /* NOTE : This function should not be modified, when the callback is needed,
  4425. the HAL_TIM_IC_CaptureCallback could be implemented in the user file
  4426. */
  4427. }
  4428. /**
  4429. * @brief Input Capture half complete callback in non-blocking mode
  4430. * @param htim TIM IC handle
  4431. * @retval None
  4432. */
  4433. __weak void HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef *htim)
  4434. {
  4435. /* Prevent unused argument(s) compilation warning */
  4436. UNUSED(htim);
  4437. /* NOTE : This function should not be modified, when the callback is needed,
  4438. the HAL_TIM_IC_CaptureHalfCpltCallback could be implemented in the user file
  4439. */
  4440. }
  4441. /**
  4442. * @brief PWM Pulse finished callback in non-blocking mode
  4443. * @param htim TIM handle
  4444. * @retval None
  4445. */
  4446. __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  4447. {
  4448. /* Prevent unused argument(s) compilation warning */
  4449. UNUSED(htim);
  4450. /* NOTE : This function should not be modified, when the callback is needed,
  4451. the HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
  4452. */
  4453. }
  4454. /**
  4455. * @brief PWM Pulse finished half complete callback in non-blocking mode
  4456. * @param htim TIM handle
  4457. * @retval None
  4458. */
  4459. __weak void HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef *htim)
  4460. {
  4461. /* Prevent unused argument(s) compilation warning */
  4462. UNUSED(htim);
  4463. /* NOTE : This function should not be modified, when the callback is needed,
  4464. the HAL_TIM_PWM_PulseFinishedHalfCpltCallback could be implemented in the user file
  4465. */
  4466. }
  4467. /**
  4468. * @brief Hall Trigger detection callback in non-blocking mode
  4469. * @param htim TIM handle
  4470. * @retval None
  4471. */
  4472. __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
  4473. {
  4474. /* Prevent unused argument(s) compilation warning */
  4475. UNUSED(htim);
  4476. /* NOTE : This function should not be modified, when the callback is needed,
  4477. the HAL_TIM_TriggerCallback could be implemented in the user file
  4478. */
  4479. }
  4480. /**
  4481. * @brief Hall Trigger detection half complete callback in non-blocking mode
  4482. * @param htim TIM handle
  4483. * @retval None
  4484. */
  4485. __weak void HAL_TIM_TriggerHalfCpltCallback(TIM_HandleTypeDef *htim)
  4486. {
  4487. /* Prevent unused argument(s) compilation warning */
  4488. UNUSED(htim);
  4489. /* NOTE : This function should not be modified, when the callback is needed,
  4490. the HAL_TIM_TriggerHalfCpltCallback could be implemented in the user file
  4491. */
  4492. }
  4493. /**
  4494. * @brief Timer error callback in non-blocking mode
  4495. * @param htim TIM handle
  4496. * @retval None
  4497. */
  4498. __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
  4499. {
  4500. /* Prevent unused argument(s) compilation warning */
  4501. UNUSED(htim);
  4502. /* NOTE : This function should not be modified, when the callback is needed,
  4503. the HAL_TIM_ErrorCallback could be implemented in the user file
  4504. */
  4505. }
  4506. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4507. /**
  4508. * @brief Register a User TIM callback to be used instead of the weak predefined callback
  4509. * @param htim tim handle
  4510. * @param CallbackID ID of the callback to be registered
  4511. * This parameter can be one of the following values:
  4512. * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
  4513. * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
  4514. * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
  4515. * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
  4516. * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
  4517. * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
  4518. * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
  4519. * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
  4520. * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
  4521. * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
  4522. * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
  4523. * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
  4524. * @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
  4525. * @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
  4526. * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
  4527. * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
  4528. * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
  4529. * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
  4530. * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
  4531. * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
  4532. * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
  4533. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
  4534. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
  4535. * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
  4536. * @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
  4537. * @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
  4538. * @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
  4539. * @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
  4540. * @param pCallback pointer to the callback function
  4541. * @retval status
  4542. */
  4543. HAL_StatusTypeDef HAL_TIM_RegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID,
  4544. pTIM_CallbackTypeDef pCallback)
  4545. {
  4546. HAL_StatusTypeDef status = HAL_OK;
  4547. if (pCallback == NULL)
  4548. {
  4549. return HAL_ERROR;
  4550. }
  4551. /* Process locked */
  4552. __HAL_LOCK(htim);
  4553. if (htim->State == HAL_TIM_STATE_READY)
  4554. {
  4555. switch (CallbackID)
  4556. {
  4557. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4558. htim->Base_MspInitCallback = pCallback;
  4559. break;
  4560. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4561. htim->Base_MspDeInitCallback = pCallback;
  4562. break;
  4563. case HAL_TIM_IC_MSPINIT_CB_ID :
  4564. htim->IC_MspInitCallback = pCallback;
  4565. break;
  4566. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4567. htim->IC_MspDeInitCallback = pCallback;
  4568. break;
  4569. case HAL_TIM_OC_MSPINIT_CB_ID :
  4570. htim->OC_MspInitCallback = pCallback;
  4571. break;
  4572. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4573. htim->OC_MspDeInitCallback = pCallback;
  4574. break;
  4575. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4576. htim->PWM_MspInitCallback = pCallback;
  4577. break;
  4578. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4579. htim->PWM_MspDeInitCallback = pCallback;
  4580. break;
  4581. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4582. htim->OnePulse_MspInitCallback = pCallback;
  4583. break;
  4584. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4585. htim->OnePulse_MspDeInitCallback = pCallback;
  4586. break;
  4587. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4588. htim->Encoder_MspInitCallback = pCallback;
  4589. break;
  4590. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4591. htim->Encoder_MspDeInitCallback = pCallback;
  4592. break;
  4593. case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
  4594. htim->HallSensor_MspInitCallback = pCallback;
  4595. break;
  4596. case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
  4597. htim->HallSensor_MspDeInitCallback = pCallback;
  4598. break;
  4599. case HAL_TIM_PERIOD_ELAPSED_CB_ID :
  4600. htim->PeriodElapsedCallback = pCallback;
  4601. break;
  4602. case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
  4603. htim->PeriodElapsedHalfCpltCallback = pCallback;
  4604. break;
  4605. case HAL_TIM_TRIGGER_CB_ID :
  4606. htim->TriggerCallback = pCallback;
  4607. break;
  4608. case HAL_TIM_TRIGGER_HALF_CB_ID :
  4609. htim->TriggerHalfCpltCallback = pCallback;
  4610. break;
  4611. case HAL_TIM_IC_CAPTURE_CB_ID :
  4612. htim->IC_CaptureCallback = pCallback;
  4613. break;
  4614. case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
  4615. htim->IC_CaptureHalfCpltCallback = pCallback;
  4616. break;
  4617. case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
  4618. htim->OC_DelayElapsedCallback = pCallback;
  4619. break;
  4620. case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
  4621. htim->PWM_PulseFinishedCallback = pCallback;
  4622. break;
  4623. case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
  4624. htim->PWM_PulseFinishedHalfCpltCallback = pCallback;
  4625. break;
  4626. case HAL_TIM_ERROR_CB_ID :
  4627. htim->ErrorCallback = pCallback;
  4628. break;
  4629. case HAL_TIM_COMMUTATION_CB_ID :
  4630. htim->CommutationCallback = pCallback;
  4631. break;
  4632. case HAL_TIM_COMMUTATION_HALF_CB_ID :
  4633. htim->CommutationHalfCpltCallback = pCallback;
  4634. break;
  4635. case HAL_TIM_BREAK_CB_ID :
  4636. htim->BreakCallback = pCallback;
  4637. break;
  4638. case HAL_TIM_BREAK2_CB_ID :
  4639. htim->Break2Callback = pCallback;
  4640. break;
  4641. default :
  4642. /* Return error status */
  4643. status = HAL_ERROR;
  4644. break;
  4645. }
  4646. }
  4647. else if (htim->State == HAL_TIM_STATE_RESET)
  4648. {
  4649. switch (CallbackID)
  4650. {
  4651. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4652. htim->Base_MspInitCallback = pCallback;
  4653. break;
  4654. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4655. htim->Base_MspDeInitCallback = pCallback;
  4656. break;
  4657. case HAL_TIM_IC_MSPINIT_CB_ID :
  4658. htim->IC_MspInitCallback = pCallback;
  4659. break;
  4660. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4661. htim->IC_MspDeInitCallback = pCallback;
  4662. break;
  4663. case HAL_TIM_OC_MSPINIT_CB_ID :
  4664. htim->OC_MspInitCallback = pCallback;
  4665. break;
  4666. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4667. htim->OC_MspDeInitCallback = pCallback;
  4668. break;
  4669. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4670. htim->PWM_MspInitCallback = pCallback;
  4671. break;
  4672. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4673. htim->PWM_MspDeInitCallback = pCallback;
  4674. break;
  4675. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4676. htim->OnePulse_MspInitCallback = pCallback;
  4677. break;
  4678. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4679. htim->OnePulse_MspDeInitCallback = pCallback;
  4680. break;
  4681. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4682. htim->Encoder_MspInitCallback = pCallback;
  4683. break;
  4684. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4685. htim->Encoder_MspDeInitCallback = pCallback;
  4686. break;
  4687. case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
  4688. htim->HallSensor_MspInitCallback = pCallback;
  4689. break;
  4690. case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
  4691. htim->HallSensor_MspDeInitCallback = pCallback;
  4692. break;
  4693. default :
  4694. /* Return error status */
  4695. status = HAL_ERROR;
  4696. break;
  4697. }
  4698. }
  4699. else
  4700. {
  4701. /* Return error status */
  4702. status = HAL_ERROR;
  4703. }
  4704. /* Release Lock */
  4705. __HAL_UNLOCK(htim);
  4706. return status;
  4707. }
  4708. /**
  4709. * @brief Unregister a TIM callback
  4710. * TIM callback is redirected to the weak predefined callback
  4711. * @param htim tim handle
  4712. * @param CallbackID ID of the callback to be unregistered
  4713. * This parameter can be one of the following values:
  4714. * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
  4715. * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
  4716. * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
  4717. * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
  4718. * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
  4719. * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
  4720. * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
  4721. * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
  4722. * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
  4723. * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
  4724. * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
  4725. * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
  4726. * @arg @ref HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID Hall Sensor MspInit Callback ID
  4727. * @arg @ref HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID Hall Sensor MspDeInit Callback ID
  4728. * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
  4729. * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
  4730. * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
  4731. * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
  4732. * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
  4733. * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
  4734. * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
  4735. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
  4736. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
  4737. * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
  4738. * @arg @ref HAL_TIM_COMMUTATION_CB_ID Commutation Callback ID
  4739. * @arg @ref HAL_TIM_COMMUTATION_HALF_CB_ID Commutation half complete Callback ID
  4740. * @arg @ref HAL_TIM_BREAK_CB_ID Break Callback ID
  4741. * @arg @ref HAL_TIM_BREAK2_CB_ID Break2 Callback ID
  4742. * @retval status
  4743. */
  4744. HAL_StatusTypeDef HAL_TIM_UnRegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID)
  4745. {
  4746. HAL_StatusTypeDef status = HAL_OK;
  4747. /* Process locked */
  4748. __HAL_LOCK(htim);
  4749. if (htim->State == HAL_TIM_STATE_READY)
  4750. {
  4751. switch (CallbackID)
  4752. {
  4753. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4754. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit; /* Legacy weak Base MspInit Callback */
  4755. break;
  4756. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4757. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit; /* Legacy weak Base Msp DeInit Callback */
  4758. break;
  4759. case HAL_TIM_IC_MSPINIT_CB_ID :
  4760. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit; /* Legacy weak IC Msp Init Callback */
  4761. break;
  4762. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4763. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit; /* Legacy weak IC Msp DeInit Callback */
  4764. break;
  4765. case HAL_TIM_OC_MSPINIT_CB_ID :
  4766. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit; /* Legacy weak OC Msp Init Callback */
  4767. break;
  4768. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4769. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit; /* Legacy weak OC Msp DeInit Callback */
  4770. break;
  4771. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4772. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit; /* Legacy weak PWM Msp Init Callback */
  4773. break;
  4774. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4775. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit; /* Legacy weak PWM Msp DeInit Callback */
  4776. break;
  4777. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4778. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit; /* Legacy weak One Pulse Msp Init Callback */
  4779. break;
  4780. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4781. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit; /* Legacy weak One Pulse Msp DeInit Callback */
  4782. break;
  4783. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4784. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit; /* Legacy weak Encoder Msp Init Callback */
  4785. break;
  4786. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4787. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit; /* Legacy weak Encoder Msp DeInit Callback */
  4788. break;
  4789. case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
  4790. htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit; /* Legacy weak Hall Sensor Msp Init Callback */
  4791. break;
  4792. case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
  4793. htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit; /* Legacy weak Hall Sensor Msp DeInit Callback */
  4794. break;
  4795. case HAL_TIM_PERIOD_ELAPSED_CB_ID :
  4796. htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback; /* Legacy weak Period Elapsed Callback */
  4797. break;
  4798. case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
  4799. htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback; /* Legacy weak Period Elapsed half complete Callback */
  4800. break;
  4801. case HAL_TIM_TRIGGER_CB_ID :
  4802. htim->TriggerCallback = HAL_TIM_TriggerCallback; /* Legacy weak Trigger Callback */
  4803. break;
  4804. case HAL_TIM_TRIGGER_HALF_CB_ID :
  4805. htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback; /* Legacy weak Trigger half complete Callback */
  4806. break;
  4807. case HAL_TIM_IC_CAPTURE_CB_ID :
  4808. htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback; /* Legacy weak IC Capture Callback */
  4809. break;
  4810. case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
  4811. htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback; /* Legacy weak IC Capture half complete Callback */
  4812. break;
  4813. case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
  4814. htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback; /* Legacy weak OC Delay Elapsed Callback */
  4815. break;
  4816. case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
  4817. htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback; /* Legacy weak PWM Pulse Finished Callback */
  4818. break;
  4819. case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
  4820. htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback; /* Legacy weak PWM Pulse Finished half complete Callback */
  4821. break;
  4822. case HAL_TIM_ERROR_CB_ID :
  4823. htim->ErrorCallback = HAL_TIM_ErrorCallback; /* Legacy weak Error Callback */
  4824. break;
  4825. case HAL_TIM_COMMUTATION_CB_ID :
  4826. htim->CommutationCallback = HAL_TIMEx_CommutCallback; /* Legacy weak Commutation Callback */
  4827. break;
  4828. case HAL_TIM_COMMUTATION_HALF_CB_ID :
  4829. htim->CommutationHalfCpltCallback = HAL_TIMEx_CommutHalfCpltCallback; /* Legacy weak Commutation half complete Callback */
  4830. break;
  4831. case HAL_TIM_BREAK_CB_ID :
  4832. htim->BreakCallback = HAL_TIMEx_BreakCallback; /* Legacy weak Break Callback */
  4833. break;
  4834. case HAL_TIM_BREAK2_CB_ID :
  4835. htim->Break2Callback = HAL_TIMEx_Break2Callback; /* Legacy weak Break2 Callback */
  4836. break;
  4837. default :
  4838. /* Return error status */
  4839. status = HAL_ERROR;
  4840. break;
  4841. }
  4842. }
  4843. else if (htim->State == HAL_TIM_STATE_RESET)
  4844. {
  4845. switch (CallbackID)
  4846. {
  4847. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4848. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit; /* Legacy weak Base MspInit Callback */
  4849. break;
  4850. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4851. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit; /* Legacy weak Base Msp DeInit Callback */
  4852. break;
  4853. case HAL_TIM_IC_MSPINIT_CB_ID :
  4854. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit; /* Legacy weak IC Msp Init Callback */
  4855. break;
  4856. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4857. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit; /* Legacy weak IC Msp DeInit Callback */
  4858. break;
  4859. case HAL_TIM_OC_MSPINIT_CB_ID :
  4860. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit; /* Legacy weak OC Msp Init Callback */
  4861. break;
  4862. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4863. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit; /* Legacy weak OC Msp DeInit Callback */
  4864. break;
  4865. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4866. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit; /* Legacy weak PWM Msp Init Callback */
  4867. break;
  4868. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4869. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit; /* Legacy weak PWM Msp DeInit Callback */
  4870. break;
  4871. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4872. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit; /* Legacy weak One Pulse Msp Init Callback */
  4873. break;
  4874. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4875. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit; /* Legacy weak One Pulse Msp DeInit Callback */
  4876. break;
  4877. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4878. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit; /* Legacy weak Encoder Msp Init Callback */
  4879. break;
  4880. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4881. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit; /* Legacy weak Encoder Msp DeInit Callback */
  4882. break;
  4883. case HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID :
  4884. htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit; /* Legacy weak Hall Sensor Msp Init Callback */
  4885. break;
  4886. case HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID :
  4887. htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit; /* Legacy weak Hall Sensor Msp DeInit Callback */
  4888. break;
  4889. default :
  4890. /* Return error status */
  4891. status = HAL_ERROR;
  4892. break;
  4893. }
  4894. }
  4895. else
  4896. {
  4897. /* Return error status */
  4898. status = HAL_ERROR;
  4899. }
  4900. /* Release Lock */
  4901. __HAL_UNLOCK(htim);
  4902. return status;
  4903. }
  4904. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  4905. /**
  4906. * @}
  4907. */
  4908. /** @defgroup TIM_Exported_Functions_Group10 TIM Peripheral State functions
  4909. * @brief TIM Peripheral State functions
  4910. *
  4911. @verbatim
  4912. ==============================================================================
  4913. ##### Peripheral State functions #####
  4914. ==============================================================================
  4915. [..]
  4916. This subsection permits to get in run-time the status of the peripheral
  4917. and the data flow.
  4918. @endverbatim
  4919. * @{
  4920. */
  4921. /**
  4922. * @brief Return the TIM Base handle state.
  4923. * @param htim TIM Base handle
  4924. * @retval HAL state
  4925. */
  4926. HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(TIM_HandleTypeDef *htim)
  4927. {
  4928. return htim->State;
  4929. }
  4930. /**
  4931. * @brief Return the TIM OC handle state.
  4932. * @param htim TIM Output Compare handle
  4933. * @retval HAL state
  4934. */
  4935. HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(TIM_HandleTypeDef *htim)
  4936. {
  4937. return htim->State;
  4938. }
  4939. /**
  4940. * @brief Return the TIM PWM handle state.
  4941. * @param htim TIM handle
  4942. * @retval HAL state
  4943. */
  4944. HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(TIM_HandleTypeDef *htim)
  4945. {
  4946. return htim->State;
  4947. }
  4948. /**
  4949. * @brief Return the TIM Input Capture handle state.
  4950. * @param htim TIM IC handle
  4951. * @retval HAL state
  4952. */
  4953. HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(TIM_HandleTypeDef *htim)
  4954. {
  4955. return htim->State;
  4956. }
  4957. /**
  4958. * @brief Return the TIM One Pulse Mode handle state.
  4959. * @param htim TIM OPM handle
  4960. * @retval HAL state
  4961. */
  4962. HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(TIM_HandleTypeDef *htim)
  4963. {
  4964. return htim->State;
  4965. }
  4966. /**
  4967. * @brief Return the TIM Encoder Mode handle state.
  4968. * @param htim TIM Encoder Interface handle
  4969. * @retval HAL state
  4970. */
  4971. HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(TIM_HandleTypeDef *htim)
  4972. {
  4973. return htim->State;
  4974. }
  4975. /**
  4976. * @}
  4977. */
  4978. /**
  4979. * @}
  4980. */
  4981. /** @defgroup TIM_Private_Functions TIM Private Functions
  4982. * @{
  4983. */
  4984. /**
  4985. * @brief TIM DMA error callback
  4986. * @param hdma pointer to DMA handle.
  4987. * @retval None
  4988. */
  4989. void TIM_DMAError(DMA_HandleTypeDef *hdma)
  4990. {
  4991. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  4992. htim->State = HAL_TIM_STATE_READY;
  4993. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4994. htim->ErrorCallback(htim);
  4995. #else
  4996. HAL_TIM_ErrorCallback(htim);
  4997. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  4998. }
  4999. /**
  5000. * @brief TIM DMA Delay Pulse complete callback.
  5001. * @param hdma pointer to DMA handle.
  5002. * @retval None
  5003. */
  5004. void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
  5005. {
  5006. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5007. htim->State = HAL_TIM_STATE_READY;
  5008. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5009. {
  5010. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5011. }
  5012. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5013. {
  5014. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5015. }
  5016. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5017. {
  5018. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5019. }
  5020. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5021. {
  5022. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5023. }
  5024. else
  5025. {
  5026. /* nothing to do */
  5027. }
  5028. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5029. htim->PWM_PulseFinishedCallback(htim);
  5030. #else
  5031. HAL_TIM_PWM_PulseFinishedCallback(htim);
  5032. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5033. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5034. }
  5035. /**
  5036. * @brief TIM DMA Delay Pulse half complete callback.
  5037. * @param hdma pointer to DMA handle.
  5038. * @retval None
  5039. */
  5040. void TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef *hdma)
  5041. {
  5042. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5043. htim->State = HAL_TIM_STATE_READY;
  5044. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5045. {
  5046. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5047. }
  5048. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5049. {
  5050. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5051. }
  5052. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5053. {
  5054. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5055. }
  5056. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5057. {
  5058. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5059. }
  5060. else
  5061. {
  5062. /* nothing to do */
  5063. }
  5064. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5065. htim->PWM_PulseFinishedHalfCpltCallback(htim);
  5066. #else
  5067. HAL_TIM_PWM_PulseFinishedHalfCpltCallback(htim);
  5068. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5069. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5070. }
  5071. /**
  5072. * @brief TIM DMA Capture complete callback.
  5073. * @param hdma pointer to DMA handle.
  5074. * @retval None
  5075. */
  5076. void TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
  5077. {
  5078. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5079. htim->State = HAL_TIM_STATE_READY;
  5080. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5081. {
  5082. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5083. }
  5084. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5085. {
  5086. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5087. }
  5088. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5089. {
  5090. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5091. }
  5092. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5093. {
  5094. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5095. }
  5096. else
  5097. {
  5098. /* nothing to do */
  5099. }
  5100. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5101. htim->IC_CaptureCallback(htim);
  5102. #else
  5103. HAL_TIM_IC_CaptureCallback(htim);
  5104. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5105. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5106. }
  5107. /**
  5108. * @brief TIM DMA Capture half complete callback.
  5109. * @param hdma pointer to DMA handle.
  5110. * @retval None
  5111. */
  5112. void TIM_DMACaptureHalfCplt(DMA_HandleTypeDef *hdma)
  5113. {
  5114. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5115. htim->State = HAL_TIM_STATE_READY;
  5116. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5117. {
  5118. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5119. }
  5120. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5121. {
  5122. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5123. }
  5124. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5125. {
  5126. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5127. }
  5128. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5129. {
  5130. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5131. }
  5132. else
  5133. {
  5134. /* nothing to do */
  5135. }
  5136. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5137. htim->IC_CaptureHalfCpltCallback(htim);
  5138. #else
  5139. HAL_TIM_IC_CaptureHalfCpltCallback(htim);
  5140. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5141. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5142. }
  5143. /**
  5144. * @brief TIM DMA Period Elapse complete callback.
  5145. * @param hdma pointer to DMA handle.
  5146. * @retval None
  5147. */
  5148. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
  5149. {
  5150. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5151. htim->State = HAL_TIM_STATE_READY;
  5152. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5153. htim->PeriodElapsedCallback(htim);
  5154. #else
  5155. HAL_TIM_PeriodElapsedCallback(htim);
  5156. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5157. }
  5158. /**
  5159. * @brief TIM DMA Period Elapse half complete callback.
  5160. * @param hdma pointer to DMA handle.
  5161. * @retval None
  5162. */
  5163. static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma)
  5164. {
  5165. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5166. htim->State = HAL_TIM_STATE_READY;
  5167. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5168. htim->PeriodElapsedHalfCpltCallback(htim);
  5169. #else
  5170. HAL_TIM_PeriodElapsedHalfCpltCallback(htim);
  5171. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5172. }
  5173. /**
  5174. * @brief TIM DMA Trigger callback.
  5175. * @param hdma pointer to DMA handle.
  5176. * @retval None
  5177. */
  5178. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
  5179. {
  5180. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5181. htim->State = HAL_TIM_STATE_READY;
  5182. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5183. htim->TriggerCallback(htim);
  5184. #else
  5185. HAL_TIM_TriggerCallback(htim);
  5186. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5187. }
  5188. /**
  5189. * @brief TIM DMA Trigger half complete callback.
  5190. * @param hdma pointer to DMA handle.
  5191. * @retval None
  5192. */
  5193. static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma)
  5194. {
  5195. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5196. htim->State = HAL_TIM_STATE_READY;
  5197. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5198. htim->TriggerHalfCpltCallback(htim);
  5199. #else
  5200. HAL_TIM_TriggerHalfCpltCallback(htim);
  5201. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5202. }
  5203. /**
  5204. * @brief Time Base configuration
  5205. * @param TIMx TIM peripheral
  5206. * @param Structure TIM Base configuration structure
  5207. * @retval None
  5208. */
  5209. void TIM_Base_SetConfig(TIM_TypeDef *TIMx, TIM_Base_InitTypeDef *Structure)
  5210. {
  5211. uint32_t tmpcr1;
  5212. tmpcr1 = TIMx->CR1;
  5213. /* Set TIM Time Base Unit parameters ---------------------------------------*/
  5214. if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
  5215. {
  5216. /* Select the Counter Mode */
  5217. tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
  5218. tmpcr1 |= Structure->CounterMode;
  5219. }
  5220. if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
  5221. {
  5222. /* Set the clock division */
  5223. tmpcr1 &= ~TIM_CR1_CKD;
  5224. tmpcr1 |= (uint32_t)Structure->ClockDivision;
  5225. }
  5226. /* Set the auto-reload preload */
  5227. MODIFY_REG(tmpcr1, TIM_CR1_ARPE, Structure->AutoReloadPreload);
  5228. TIMx->CR1 = tmpcr1;
  5229. /* Set the Autoreload value */
  5230. TIMx->ARR = (uint32_t)Structure->Period ;
  5231. /* Set the Prescaler value */
  5232. TIMx->PSC = Structure->Prescaler;
  5233. if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
  5234. {
  5235. /* Set the Repetition Counter value */
  5236. TIMx->RCR = Structure->RepetitionCounter;
  5237. }
  5238. /* Generate an update event to reload the Prescaler
  5239. and the repetition counter (only for advanced timer) value immediately */
  5240. TIMx->EGR = TIM_EGR_UG;
  5241. }
  5242. /**
  5243. * @brief Timer Output Compare 1 configuration
  5244. * @param TIMx to select the TIM peripheral
  5245. * @param OC_Config The ouput configuration structure
  5246. * @retval None
  5247. */
  5248. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  5249. {
  5250. uint32_t tmpccmrx;
  5251. uint32_t tmpccer;
  5252. uint32_t tmpcr2;
  5253. /* Disable the Channel 1: Reset the CC1E Bit */
  5254. TIMx->CCER &= ~TIM_CCER_CC1E;
  5255. /* Get the TIMx CCER register value */
  5256. tmpccer = TIMx->CCER;
  5257. /* Get the TIMx CR2 register value */
  5258. tmpcr2 = TIMx->CR2;
  5259. /* Get the TIMx CCMR1 register value */
  5260. tmpccmrx = TIMx->CCMR1;
  5261. /* Reset the Output Compare Mode Bits */
  5262. tmpccmrx &= ~TIM_CCMR1_OC1M;
  5263. tmpccmrx &= ~TIM_CCMR1_CC1S;
  5264. /* Select the Output Compare Mode */
  5265. tmpccmrx |= OC_Config->OCMode;
  5266. /* Reset the Output Polarity level */
  5267. tmpccer &= ~TIM_CCER_CC1P;
  5268. /* Set the Output Compare Polarity */
  5269. tmpccer |= OC_Config->OCPolarity;
  5270. if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_1))
  5271. {
  5272. /* Check parameters */
  5273. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  5274. /* Reset the Output N Polarity level */
  5275. tmpccer &= ~TIM_CCER_CC1NP;
  5276. /* Set the Output N Polarity */
  5277. tmpccer |= OC_Config->OCNPolarity;
  5278. /* Reset the Output N State */
  5279. tmpccer &= ~TIM_CCER_CC1NE;
  5280. }
  5281. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5282. {
  5283. /* Check parameters */
  5284. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  5285. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  5286. /* Reset the Output Compare and Output Compare N IDLE State */
  5287. tmpcr2 &= ~TIM_CR2_OIS1;
  5288. tmpcr2 &= ~TIM_CR2_OIS1N;
  5289. /* Set the Output Idle state */
  5290. tmpcr2 |= OC_Config->OCIdleState;
  5291. /* Set the Output N Idle state */
  5292. tmpcr2 |= OC_Config->OCNIdleState;
  5293. }
  5294. /* Write to TIMx CR2 */
  5295. TIMx->CR2 = tmpcr2;
  5296. /* Write to TIMx CCMR1 */
  5297. TIMx->CCMR1 = tmpccmrx;
  5298. /* Set the Capture Compare Register value */
  5299. TIMx->CCR1 = OC_Config->Pulse;
  5300. /* Write to TIMx CCER */
  5301. TIMx->CCER = tmpccer;
  5302. }
  5303. /**
  5304. * @brief Timer Output Compare 2 configuration
  5305. * @param TIMx to select the TIM peripheral
  5306. * @param OC_Config The ouput configuration structure
  5307. * @retval None
  5308. */
  5309. void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  5310. {
  5311. uint32_t tmpccmrx;
  5312. uint32_t tmpccer;
  5313. uint32_t tmpcr2;
  5314. /* Disable the Channel 2: Reset the CC2E Bit */
  5315. TIMx->CCER &= ~TIM_CCER_CC2E;
  5316. /* Get the TIMx CCER register value */
  5317. tmpccer = TIMx->CCER;
  5318. /* Get the TIMx CR2 register value */
  5319. tmpcr2 = TIMx->CR2;
  5320. /* Get the TIMx CCMR1 register value */
  5321. tmpccmrx = TIMx->CCMR1;
  5322. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5323. tmpccmrx &= ~TIM_CCMR1_OC2M;
  5324. tmpccmrx &= ~TIM_CCMR1_CC2S;
  5325. /* Select the Output Compare Mode */
  5326. tmpccmrx |= (OC_Config->OCMode << 8U);
  5327. /* Reset the Output Polarity level */
  5328. tmpccer &= ~TIM_CCER_CC2P;
  5329. /* Set the Output Compare Polarity */
  5330. tmpccer |= (OC_Config->OCPolarity << 4U);
  5331. if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_2))
  5332. {
  5333. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  5334. /* Reset the Output N Polarity level */
  5335. tmpccer &= ~TIM_CCER_CC2NP;
  5336. /* Set the Output N Polarity */
  5337. tmpccer |= (OC_Config->OCNPolarity << 4U);
  5338. /* Reset the Output N State */
  5339. tmpccer &= ~TIM_CCER_CC2NE;
  5340. }
  5341. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5342. {
  5343. /* Check parameters */
  5344. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  5345. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  5346. /* Reset the Output Compare and Output Compare N IDLE State */
  5347. tmpcr2 &= ~TIM_CR2_OIS2;
  5348. tmpcr2 &= ~TIM_CR2_OIS2N;
  5349. /* Set the Output Idle state */
  5350. tmpcr2 |= (OC_Config->OCIdleState << 2U);
  5351. /* Set the Output N Idle state */
  5352. tmpcr2 |= (OC_Config->OCNIdleState << 2U);
  5353. }
  5354. /* Write to TIMx CR2 */
  5355. TIMx->CR2 = tmpcr2;
  5356. /* Write to TIMx CCMR1 */
  5357. TIMx->CCMR1 = tmpccmrx;
  5358. /* Set the Capture Compare Register value */
  5359. TIMx->CCR2 = OC_Config->Pulse;
  5360. /* Write to TIMx CCER */
  5361. TIMx->CCER = tmpccer;
  5362. }
  5363. /**
  5364. * @brief Timer Output Compare 3 configuration
  5365. * @param TIMx to select the TIM peripheral
  5366. * @param OC_Config The ouput configuration structure
  5367. * @retval None
  5368. */
  5369. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  5370. {
  5371. uint32_t tmpccmrx;
  5372. uint32_t tmpccer;
  5373. uint32_t tmpcr2;
  5374. /* Disable the Channel 3: Reset the CC2E Bit */
  5375. TIMx->CCER &= ~TIM_CCER_CC3E;
  5376. /* Get the TIMx CCER register value */
  5377. tmpccer = TIMx->CCER;
  5378. /* Get the TIMx CR2 register value */
  5379. tmpcr2 = TIMx->CR2;
  5380. /* Get the TIMx CCMR2 register value */
  5381. tmpccmrx = TIMx->CCMR2;
  5382. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5383. tmpccmrx &= ~TIM_CCMR2_OC3M;
  5384. tmpccmrx &= ~TIM_CCMR2_CC3S;
  5385. /* Select the Output Compare Mode */
  5386. tmpccmrx |= OC_Config->OCMode;
  5387. /* Reset the Output Polarity level */
  5388. tmpccer &= ~TIM_CCER_CC3P;
  5389. /* Set the Output Compare Polarity */
  5390. tmpccer |= (OC_Config->OCPolarity << 8U);
  5391. if (IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_3))
  5392. {
  5393. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  5394. /* Reset the Output N Polarity level */
  5395. tmpccer &= ~TIM_CCER_CC3NP;
  5396. /* Set the Output N Polarity */
  5397. tmpccer |= (OC_Config->OCNPolarity << 8U);
  5398. /* Reset the Output N State */
  5399. tmpccer &= ~TIM_CCER_CC3NE;
  5400. }
  5401. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5402. {
  5403. /* Check parameters */
  5404. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  5405. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  5406. /* Reset the Output Compare and Output Compare N IDLE State */
  5407. tmpcr2 &= ~TIM_CR2_OIS3;
  5408. tmpcr2 &= ~TIM_CR2_OIS3N;
  5409. /* Set the Output Idle state */
  5410. tmpcr2 |= (OC_Config->OCIdleState << 4U);
  5411. /* Set the Output N Idle state */
  5412. tmpcr2 |= (OC_Config->OCNIdleState << 4U);
  5413. }
  5414. /* Write to TIMx CR2 */
  5415. TIMx->CR2 = tmpcr2;
  5416. /* Write to TIMx CCMR2 */
  5417. TIMx->CCMR2 = tmpccmrx;
  5418. /* Set the Capture Compare Register value */
  5419. TIMx->CCR3 = OC_Config->Pulse;
  5420. /* Write to TIMx CCER */
  5421. TIMx->CCER = tmpccer;
  5422. }
  5423. /**
  5424. * @brief Timer Output Compare 4 configuration
  5425. * @param TIMx to select the TIM peripheral
  5426. * @param OC_Config The ouput configuration structure
  5427. * @retval None
  5428. */
  5429. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  5430. {
  5431. uint32_t tmpccmrx;
  5432. uint32_t tmpccer;
  5433. uint32_t tmpcr2;
  5434. /* Disable the Channel 4: Reset the CC4E Bit */
  5435. TIMx->CCER &= ~TIM_CCER_CC4E;
  5436. /* Get the TIMx CCER register value */
  5437. tmpccer = TIMx->CCER;
  5438. /* Get the TIMx CR2 register value */
  5439. tmpcr2 = TIMx->CR2;
  5440. /* Get the TIMx CCMR2 register value */
  5441. tmpccmrx = TIMx->CCMR2;
  5442. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5443. tmpccmrx &= ~TIM_CCMR2_OC4M;
  5444. tmpccmrx &= ~TIM_CCMR2_CC4S;
  5445. /* Select the Output Compare Mode */
  5446. tmpccmrx |= (OC_Config->OCMode << 8U);
  5447. /* Reset the Output Polarity level */
  5448. tmpccer &= ~TIM_CCER_CC4P;
  5449. /* Set the Output Compare Polarity */
  5450. tmpccer |= (OC_Config->OCPolarity << 12U);
  5451. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5452. {
  5453. /* Check parameters */
  5454. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  5455. /* Reset the Output Compare IDLE State */
  5456. tmpcr2 &= ~TIM_CR2_OIS4;
  5457. /* Set the Output Idle state */
  5458. tmpcr2 |= (OC_Config->OCIdleState << 6U);
  5459. }
  5460. /* Write to TIMx CR2 */
  5461. TIMx->CR2 = tmpcr2;
  5462. /* Write to TIMx CCMR2 */
  5463. TIMx->CCMR2 = tmpccmrx;
  5464. /* Set the Capture Compare Register value */
  5465. TIMx->CCR4 = OC_Config->Pulse;
  5466. /* Write to TIMx CCER */
  5467. TIMx->CCER = tmpccer;
  5468. }
  5469. /**
  5470. * @brief Timer Output Compare 5 configuration
  5471. * @param TIMx to select the TIM peripheral
  5472. * @param OC_Config The ouput configuration structure
  5473. * @retval None
  5474. */
  5475. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx,
  5476. TIM_OC_InitTypeDef *OC_Config)
  5477. {
  5478. uint32_t tmpccmrx;
  5479. uint32_t tmpccer;
  5480. uint32_t tmpcr2;
  5481. /* Disable the output: Reset the CCxE Bit */
  5482. TIMx->CCER &= ~TIM_CCER_CC5E;
  5483. /* Get the TIMx CCER register value */
  5484. tmpccer = TIMx->CCER;
  5485. /* Get the TIMx CR2 register value */
  5486. tmpcr2 = TIMx->CR2;
  5487. /* Get the TIMx CCMR1 register value */
  5488. tmpccmrx = TIMx->CCMR3;
  5489. /* Reset the Output Compare Mode Bits */
  5490. tmpccmrx &= ~(TIM_CCMR3_OC5M);
  5491. /* Select the Output Compare Mode */
  5492. tmpccmrx |= OC_Config->OCMode;
  5493. /* Reset the Output Polarity level */
  5494. tmpccer &= ~TIM_CCER_CC5P;
  5495. /* Set the Output Compare Polarity */
  5496. tmpccer |= (OC_Config->OCPolarity << 16U);
  5497. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5498. {
  5499. /* Reset the Output Compare IDLE State */
  5500. tmpcr2 &= ~TIM_CR2_OIS5;
  5501. /* Set the Output Idle state */
  5502. tmpcr2 |= (OC_Config->OCIdleState << 8U);
  5503. }
  5504. /* Write to TIMx CR2 */
  5505. TIMx->CR2 = tmpcr2;
  5506. /* Write to TIMx CCMR3 */
  5507. TIMx->CCMR3 = tmpccmrx;
  5508. /* Set the Capture Compare Register value */
  5509. TIMx->CCR5 = OC_Config->Pulse;
  5510. /* Write to TIMx CCER */
  5511. TIMx->CCER = tmpccer;
  5512. }
  5513. /**
  5514. * @brief Timer Output Compare 6 configuration
  5515. * @param TIMx to select the TIM peripheral
  5516. * @param OC_Config The ouput configuration structure
  5517. * @retval None
  5518. */
  5519. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx,
  5520. TIM_OC_InitTypeDef *OC_Config)
  5521. {
  5522. uint32_t tmpccmrx;
  5523. uint32_t tmpccer;
  5524. uint32_t tmpcr2;
  5525. /* Disable the output: Reset the CCxE Bit */
  5526. TIMx->CCER &= ~TIM_CCER_CC6E;
  5527. /* Get the TIMx CCER register value */
  5528. tmpccer = TIMx->CCER;
  5529. /* Get the TIMx CR2 register value */
  5530. tmpcr2 = TIMx->CR2;
  5531. /* Get the TIMx CCMR1 register value */
  5532. tmpccmrx = TIMx->CCMR3;
  5533. /* Reset the Output Compare Mode Bits */
  5534. tmpccmrx &= ~(TIM_CCMR3_OC6M);
  5535. /* Select the Output Compare Mode */
  5536. tmpccmrx |= (OC_Config->OCMode << 8U);
  5537. /* Reset the Output Polarity level */
  5538. tmpccer &= (uint32_t)~TIM_CCER_CC6P;
  5539. /* Set the Output Compare Polarity */
  5540. tmpccer |= (OC_Config->OCPolarity << 20U);
  5541. if (IS_TIM_BREAK_INSTANCE(TIMx))
  5542. {
  5543. /* Reset the Output Compare IDLE State */
  5544. tmpcr2 &= ~TIM_CR2_OIS6;
  5545. /* Set the Output Idle state */
  5546. tmpcr2 |= (OC_Config->OCIdleState << 10U);
  5547. }
  5548. /* Write to TIMx CR2 */
  5549. TIMx->CR2 = tmpcr2;
  5550. /* Write to TIMx CCMR3 */
  5551. TIMx->CCMR3 = tmpccmrx;
  5552. /* Set the Capture Compare Register value */
  5553. TIMx->CCR6 = OC_Config->Pulse;
  5554. /* Write to TIMx CCER */
  5555. TIMx->CCER = tmpccer;
  5556. }
  5557. /**
  5558. * @brief Slave Timer configuration function
  5559. * @param htim TIM handle
  5560. * @param sSlaveConfig Slave timer configuration
  5561. * @retval None
  5562. */
  5563. static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  5564. TIM_SlaveConfigTypeDef *sSlaveConfig)
  5565. {
  5566. uint32_t tmpsmcr;
  5567. uint32_t tmpccmr1;
  5568. uint32_t tmpccer;
  5569. /* Get the TIMx SMCR register value */
  5570. tmpsmcr = htim->Instance->SMCR;
  5571. /* Reset the Trigger Selection Bits */
  5572. tmpsmcr &= ~TIM_SMCR_TS;
  5573. /* Set the Input Trigger source */
  5574. tmpsmcr |= sSlaveConfig->InputTrigger;
  5575. /* Reset the slave mode Bits */
  5576. tmpsmcr &= ~TIM_SMCR_SMS;
  5577. /* Set the slave mode */
  5578. tmpsmcr |= sSlaveConfig->SlaveMode;
  5579. /* Write to TIMx SMCR */
  5580. htim->Instance->SMCR = tmpsmcr;
  5581. /* Configure the trigger prescaler, filter, and polarity */
  5582. switch (sSlaveConfig->InputTrigger)
  5583. {
  5584. case TIM_TS_ETRF:
  5585. {
  5586. /* Check the parameters */
  5587. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  5588. assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
  5589. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5590. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5591. /* Configure the ETR Trigger source */
  5592. TIM_ETR_SetConfig(htim->Instance,
  5593. sSlaveConfig->TriggerPrescaler,
  5594. sSlaveConfig->TriggerPolarity,
  5595. sSlaveConfig->TriggerFilter);
  5596. break;
  5597. }
  5598. case TIM_TS_TI1F_ED:
  5599. {
  5600. /* Check the parameters */
  5601. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  5602. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5603. if(sSlaveConfig->SlaveMode == TIM_SLAVEMODE_GATED)
  5604. {
  5605. return HAL_ERROR;
  5606. }
  5607. /* Disable the Channel 1: Reset the CC1E Bit */
  5608. tmpccer = htim->Instance->CCER;
  5609. htim->Instance->CCER &= ~TIM_CCER_CC1E;
  5610. tmpccmr1 = htim->Instance->CCMR1;
  5611. /* Set the filter */
  5612. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5613. tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4U);
  5614. /* Write to TIMx CCMR1 and CCER registers */
  5615. htim->Instance->CCMR1 = tmpccmr1;
  5616. htim->Instance->CCER = tmpccer;
  5617. break;
  5618. }
  5619. case TIM_TS_TI1FP1:
  5620. {
  5621. /* Check the parameters */
  5622. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  5623. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5624. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5625. /* Configure TI1 Filter and Polarity */
  5626. TIM_TI1_ConfigInputStage(htim->Instance,
  5627. sSlaveConfig->TriggerPolarity,
  5628. sSlaveConfig->TriggerFilter);
  5629. break;
  5630. }
  5631. case TIM_TS_TI2FP2:
  5632. {
  5633. /* Check the parameters */
  5634. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  5635. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5636. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5637. /* Configure TI2 Filter and Polarity */
  5638. TIM_TI2_ConfigInputStage(htim->Instance,
  5639. sSlaveConfig->TriggerPolarity,
  5640. sSlaveConfig->TriggerFilter);
  5641. break;
  5642. }
  5643. case TIM_TS_ITR0:
  5644. case TIM_TS_ITR1:
  5645. case TIM_TS_ITR2:
  5646. case TIM_TS_ITR3:
  5647. case TIM_TS_ITR4:
  5648. case TIM_TS_ITR5:
  5649. case TIM_TS_ITR6:
  5650. case TIM_TS_ITR7:
  5651. case TIM_TS_ITR8:
  5652. case TIM_TS_ITR9:
  5653. case TIM_TS_ITR10:
  5654. case TIM_TS_ITR11:
  5655. case TIM_TS_ITR12:
  5656. case TIM_TS_ITR13:
  5657. {
  5658. /* Check the parameter */
  5659. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  5660. break;
  5661. }
  5662. default:
  5663. break;
  5664. }
  5665. return HAL_OK;
  5666. }
  5667. /**
  5668. * @brief Configure the TI1 as Input.
  5669. * @param TIMx to select the TIM peripheral.
  5670. * @param TIM_ICPolarity The Input Polarity.
  5671. * This parameter can be one of the following values:
  5672. * @arg TIM_ICPOLARITY_RISING
  5673. * @arg TIM_ICPOLARITY_FALLING
  5674. * @arg TIM_ICPOLARITY_BOTHEDGE
  5675. * @param TIM_ICSelection specifies the input to be used.
  5676. * This parameter can be one of the following values:
  5677. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 1 is selected to be connected to IC1.
  5678. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 1 is selected to be connected to IC2.
  5679. * @arg TIM_ICSELECTION_TRC: TIM Input 1 is selected to be connected to TRC.
  5680. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5681. * This parameter must be a value between 0x00 and 0x0F.
  5682. * @retval None
  5683. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
  5684. * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
  5685. * protected against un-initialized filter and polarity values.
  5686. */
  5687. void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5688. uint32_t TIM_ICFilter)
  5689. {
  5690. uint32_t tmpccmr1;
  5691. uint32_t tmpccer;
  5692. /* Disable the Channel 1: Reset the CC1E Bit */
  5693. TIMx->CCER &= ~TIM_CCER_CC1E;
  5694. tmpccmr1 = TIMx->CCMR1;
  5695. tmpccer = TIMx->CCER;
  5696. /* Select the Input */
  5697. if (IS_TIM_CC2_INSTANCE(TIMx) != RESET)
  5698. {
  5699. tmpccmr1 &= ~TIM_CCMR1_CC1S;
  5700. tmpccmr1 |= TIM_ICSelection;
  5701. }
  5702. else
  5703. {
  5704. tmpccmr1 |= TIM_CCMR1_CC1S_0;
  5705. }
  5706. /* Set the filter */
  5707. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5708. tmpccmr1 |= ((TIM_ICFilter << 4U) & TIM_CCMR1_IC1F);
  5709. /* Select the Polarity and set the CC1E Bit */
  5710. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  5711. tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
  5712. /* Write to TIMx CCMR1 and CCER registers */
  5713. TIMx->CCMR1 = tmpccmr1;
  5714. TIMx->CCER = tmpccer;
  5715. }
  5716. /**
  5717. * @brief Configure the Polarity and Filter for TI1.
  5718. * @param TIMx to select the TIM peripheral.
  5719. * @param TIM_ICPolarity The Input Polarity.
  5720. * This parameter can be one of the following values:
  5721. * @arg TIM_ICPOLARITY_RISING
  5722. * @arg TIM_ICPOLARITY_FALLING
  5723. * @arg TIM_ICPOLARITY_BOTHEDGE
  5724. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5725. * This parameter must be a value between 0x00 and 0x0F.
  5726. * @retval None
  5727. */
  5728. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  5729. {
  5730. uint32_t tmpccmr1;
  5731. uint32_t tmpccer;
  5732. /* Disable the Channel 1: Reset the CC1E Bit */
  5733. tmpccer = TIMx->CCER;
  5734. TIMx->CCER &= ~TIM_CCER_CC1E;
  5735. tmpccmr1 = TIMx->CCMR1;
  5736. /* Set the filter */
  5737. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5738. tmpccmr1 |= (TIM_ICFilter << 4U);
  5739. /* Select the Polarity and set the CC1E Bit */
  5740. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  5741. tmpccer |= TIM_ICPolarity;
  5742. /* Write to TIMx CCMR1 and CCER registers */
  5743. TIMx->CCMR1 = tmpccmr1;
  5744. TIMx->CCER = tmpccer;
  5745. }
  5746. /**
  5747. * @brief Configure the TI2 as Input.
  5748. * @param TIMx to select the TIM peripheral
  5749. * @param TIM_ICPolarity The Input Polarity.
  5750. * This parameter can be one of the following values:
  5751. * @arg TIM_ICPOLARITY_RISING
  5752. * @arg TIM_ICPOLARITY_FALLING
  5753. * @arg TIM_ICPOLARITY_BOTHEDGE
  5754. * @param TIM_ICSelection specifies the input to be used.
  5755. * This parameter can be one of the following values:
  5756. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 2 is selected to be connected to IC2.
  5757. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 2 is selected to be connected to IC1.
  5758. * @arg TIM_ICSELECTION_TRC: TIM Input 2 is selected to be connected to TRC.
  5759. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5760. * This parameter must be a value between 0x00 and 0x0F.
  5761. * @retval None
  5762. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
  5763. * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
  5764. * protected against un-initialized filter and polarity values.
  5765. */
  5766. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5767. uint32_t TIM_ICFilter)
  5768. {
  5769. uint32_t tmpccmr1;
  5770. uint32_t tmpccer;
  5771. /* Disable the Channel 2: Reset the CC2E Bit */
  5772. TIMx->CCER &= ~TIM_CCER_CC2E;
  5773. tmpccmr1 = TIMx->CCMR1;
  5774. tmpccer = TIMx->CCER;
  5775. /* Select the Input */
  5776. tmpccmr1 &= ~TIM_CCMR1_CC2S;
  5777. tmpccmr1 |= (TIM_ICSelection << 8U);
  5778. /* Set the filter */
  5779. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  5780. tmpccmr1 |= ((TIM_ICFilter << 12U) & TIM_CCMR1_IC2F);
  5781. /* Select the Polarity and set the CC2E Bit */
  5782. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  5783. tmpccer |= ((TIM_ICPolarity << 4U) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
  5784. /* Write to TIMx CCMR1 and CCER registers */
  5785. TIMx->CCMR1 = tmpccmr1 ;
  5786. TIMx->CCER = tmpccer;
  5787. }
  5788. /**
  5789. * @brief Configure the Polarity and Filter for TI2.
  5790. * @param TIMx to select the TIM peripheral.
  5791. * @param TIM_ICPolarity The Input Polarity.
  5792. * This parameter can be one of the following values:
  5793. * @arg TIM_ICPOLARITY_RISING
  5794. * @arg TIM_ICPOLARITY_FALLING
  5795. * @arg TIM_ICPOLARITY_BOTHEDGE
  5796. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5797. * This parameter must be a value between 0x00 and 0x0F.
  5798. * @retval None
  5799. */
  5800. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  5801. {
  5802. uint32_t tmpccmr1;
  5803. uint32_t tmpccer;
  5804. /* Disable the Channel 2: Reset the CC2E Bit */
  5805. TIMx->CCER &= ~TIM_CCER_CC2E;
  5806. tmpccmr1 = TIMx->CCMR1;
  5807. tmpccer = TIMx->CCER;
  5808. /* Set the filter */
  5809. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  5810. tmpccmr1 |= (TIM_ICFilter << 12U);
  5811. /* Select the Polarity and set the CC2E Bit */
  5812. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  5813. tmpccer |= (TIM_ICPolarity << 4U);
  5814. /* Write to TIMx CCMR1 and CCER registers */
  5815. TIMx->CCMR1 = tmpccmr1 ;
  5816. TIMx->CCER = tmpccer;
  5817. }
  5818. /**
  5819. * @brief Configure the TI3 as Input.
  5820. * @param TIMx to select the TIM peripheral
  5821. * @param TIM_ICPolarity The Input Polarity.
  5822. * This parameter can be one of the following values:
  5823. * @arg TIM_ICPOLARITY_RISING
  5824. * @arg TIM_ICPOLARITY_FALLING
  5825. * @arg TIM_ICPOLARITY_BOTHEDGE
  5826. * @param TIM_ICSelection specifies the input to be used.
  5827. * This parameter can be one of the following values:
  5828. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 3 is selected to be connected to IC3.
  5829. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 3 is selected to be connected to IC4.
  5830. * @arg TIM_ICSELECTION_TRC: TIM Input 3 is selected to be connected to TRC.
  5831. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5832. * This parameter must be a value between 0x00 and 0x0F.
  5833. * @retval None
  5834. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
  5835. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  5836. * protected against un-initialized filter and polarity values.
  5837. */
  5838. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5839. uint32_t TIM_ICFilter)
  5840. {
  5841. uint32_t tmpccmr2;
  5842. uint32_t tmpccer;
  5843. /* Disable the Channel 3: Reset the CC3E Bit */
  5844. TIMx->CCER &= ~TIM_CCER_CC3E;
  5845. tmpccmr2 = TIMx->CCMR2;
  5846. tmpccer = TIMx->CCER;
  5847. /* Select the Input */
  5848. tmpccmr2 &= ~TIM_CCMR2_CC3S;
  5849. tmpccmr2 |= TIM_ICSelection;
  5850. /* Set the filter */
  5851. tmpccmr2 &= ~TIM_CCMR2_IC3F;
  5852. tmpccmr2 |= ((TIM_ICFilter << 4U) & TIM_CCMR2_IC3F);
  5853. /* Select the Polarity and set the CC3E Bit */
  5854. tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
  5855. tmpccer |= ((TIM_ICPolarity << 8U) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
  5856. /* Write to TIMx CCMR2 and CCER registers */
  5857. TIMx->CCMR2 = tmpccmr2;
  5858. TIMx->CCER = tmpccer;
  5859. }
  5860. /**
  5861. * @brief Configure the TI4 as Input.
  5862. * @param TIMx to select the TIM peripheral
  5863. * @param TIM_ICPolarity The Input Polarity.
  5864. * This parameter can be one of the following values:
  5865. * @arg TIM_ICPOLARITY_RISING
  5866. * @arg TIM_ICPOLARITY_FALLING
  5867. * @arg TIM_ICPOLARITY_BOTHEDGE
  5868. * @param TIM_ICSelection specifies the input to be used.
  5869. * This parameter can be one of the following values:
  5870. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 4 is selected to be connected to IC4.
  5871. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 4 is selected to be connected to IC3.
  5872. * @arg TIM_ICSELECTION_TRC: TIM Input 4 is selected to be connected to TRC.
  5873. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5874. * This parameter must be a value between 0x00 and 0x0F.
  5875. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
  5876. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  5877. * protected against un-initialized filter and polarity values.
  5878. * @retval None
  5879. */
  5880. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5881. uint32_t TIM_ICFilter)
  5882. {
  5883. uint32_t tmpccmr2;
  5884. uint32_t tmpccer;
  5885. /* Disable the Channel 4: Reset the CC4E Bit */
  5886. TIMx->CCER &= ~TIM_CCER_CC4E;
  5887. tmpccmr2 = TIMx->CCMR2;
  5888. tmpccer = TIMx->CCER;
  5889. /* Select the Input */
  5890. tmpccmr2 &= ~TIM_CCMR2_CC4S;
  5891. tmpccmr2 |= (TIM_ICSelection << 8U);
  5892. /* Set the filter */
  5893. tmpccmr2 &= ~TIM_CCMR2_IC4F;
  5894. tmpccmr2 |= ((TIM_ICFilter << 12U) & TIM_CCMR2_IC4F);
  5895. /* Select the Polarity and set the CC4E Bit */
  5896. tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
  5897. tmpccer |= ((TIM_ICPolarity << 12U) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
  5898. /* Write to TIMx CCMR2 and CCER registers */
  5899. TIMx->CCMR2 = tmpccmr2;
  5900. TIMx->CCER = tmpccer ;
  5901. }
  5902. /**
  5903. * @brief Selects the Input Trigger source
  5904. * @param TIMx to select the TIM peripheral
  5905. * @param InputTriggerSource The Input Trigger source.
  5906. * This parameter can be one of the following values:
  5907. * @arg TIM_TS_ITR0: Internal Trigger 0
  5908. * @arg TIM_TS_ITR1: Internal Trigger 1
  5909. * @arg TIM_TS_ITR2: Internal Trigger 2
  5910. * @arg TIM_TS_ITR3: Internal Trigger 3
  5911. * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  5912. * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  5913. * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  5914. * @arg TIM_TS_ETRF: External Trigger input
  5915. * @arg TIM_TS_ITR4: Internal Trigger 4 (*)
  5916. * @arg TIM_TS_ITR5: Internal Trigger 5
  5917. * @arg TIM_TS_ITR6: Internal Trigger 6
  5918. * @arg TIM_TS_ITR7: Internal Trigger 7
  5919. * @arg TIM_TS_ITR8: Internal Trigger 8 (*)
  5920. * @arg TIM_TS_ITR9: Internal Trigger 9 (*)
  5921. * @arg TIM_TS_ITR10: Internal Trigger 10 (*)
  5922. * @arg TIM_TS_ITR11: Internal Trigger 11 (*)
  5923. * @arg TIM_TS_ITR12: Internal Trigger 12 (*)
  5924. * @arg TIM_TS_ITR13: Internal Trigger 13 (*)
  5925. *
  5926. * (*) Value not defined in all devices.
  5927. *
  5928. * @retval None
  5929. */
  5930. static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource)
  5931. {
  5932. uint32_t tmpsmcr;
  5933. /* Get the TIMx SMCR register value */
  5934. tmpsmcr = TIMx->SMCR;
  5935. /* Reset the TS Bits */
  5936. tmpsmcr &= ~TIM_SMCR_TS;
  5937. /* Set the Input Trigger source and the slave mode*/
  5938. tmpsmcr |= (InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1);
  5939. /* Write to TIMx SMCR */
  5940. TIMx->SMCR = tmpsmcr;
  5941. }
  5942. /**
  5943. * @brief Configures the TIMx External Trigger (ETR).
  5944. * @param TIMx to select the TIM peripheral
  5945. * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
  5946. * This parameter can be one of the following values:
  5947. * @arg TIM_ETRPRESCALER_DIV1: ETRP Prescaler OFF.
  5948. * @arg TIM_ETRPRESCALER_DIV2: ETRP frequency divided by 2.
  5949. * @arg TIM_ETRPRESCALER_DIV4: ETRP frequency divided by 4.
  5950. * @arg TIM_ETRPRESCALER_DIV8: ETRP frequency divided by 8.
  5951. * @param TIM_ExtTRGPolarity The external Trigger Polarity.
  5952. * This parameter can be one of the following values:
  5953. * @arg TIM_ETRPOLARITY_INVERTED: active low or falling edge active.
  5954. * @arg TIM_ETRPOLARITY_NONINVERTED: active high or rising edge active.
  5955. * @param ExtTRGFilter External Trigger Filter.
  5956. * This parameter must be a value between 0x00 and 0x0F
  5957. * @retval None
  5958. */
  5959. void TIM_ETR_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler,
  5960. uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
  5961. {
  5962. uint32_t tmpsmcr;
  5963. tmpsmcr = TIMx->SMCR;
  5964. /* Reset the ETR Bits */
  5965. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  5966. /* Set the Prescaler, the Filter value and the Polarity */
  5967. tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8U)));
  5968. /* Write to TIMx SMCR */
  5969. TIMx->SMCR = tmpsmcr;
  5970. }
  5971. /**
  5972. * @brief Enables or disables the TIM Capture Compare Channel x.
  5973. * @param TIMx to select the TIM peripheral
  5974. * @param Channel specifies the TIM Channel
  5975. * This parameter can be one of the following values:
  5976. * @arg TIM_CHANNEL_1: TIM Channel 1
  5977. * @arg TIM_CHANNEL_2: TIM Channel 2
  5978. * @arg TIM_CHANNEL_3: TIM Channel 3
  5979. * @arg TIM_CHANNEL_4: TIM Channel 4
  5980. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  5981. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  5982. * @param ChannelState specifies the TIM Channel CCxE bit new state.
  5983. * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
  5984. * @retval None
  5985. */
  5986. void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
  5987. {
  5988. uint32_t tmp;
  5989. /* Check the parameters */
  5990. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  5991. assert_param(IS_TIM_CHANNELS(Channel));
  5992. tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
  5993. /* Reset the CCxE Bit */
  5994. TIMx->CCER &= ~tmp;
  5995. /* Set or reset the CCxE Bit */
  5996. TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
  5997. }
  5998. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5999. /**
  6000. * @brief Reset interrupt callbacks to the legacy weak callbacks.
  6001. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  6002. * the configuration information for TIM module.
  6003. * @retval None
  6004. */
  6005. void TIM_ResetCallback(TIM_HandleTypeDef *htim)
  6006. {
  6007. /* Reset the TIM callback to the legacy weak callbacks */
  6008. htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback; /* Legacy weak PeriodElapsedCallback */
  6009. htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback; /* Legacy weak PeriodElapsedHalfCpltCallback */
  6010. htim->TriggerCallback = HAL_TIM_TriggerCallback; /* Legacy weak TriggerCallback */
  6011. htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback; /* Legacy weak TriggerHalfCpltCallback */
  6012. htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback; /* Legacy weak IC_CaptureCallback */
  6013. htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback; /* Legacy weak IC_CaptureHalfCpltCallback */
  6014. htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback; /* Legacy weak OC_DelayElapsedCallback */
  6015. htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback; /* Legacy weak PWM_PulseFinishedCallback */
  6016. htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback; /* Legacy weak PWM_PulseFinishedHalfCpltCallback */
  6017. htim->ErrorCallback = HAL_TIM_ErrorCallback; /* Legacy weak ErrorCallback */
  6018. htim->CommutationCallback = HAL_TIMEx_CommutCallback; /* Legacy weak CommutationCallback */
  6019. htim->CommutationHalfCpltCallback = HAL_TIMEx_CommutHalfCpltCallback; /* Legacy weak CommutationHalfCpltCallback */
  6020. htim->BreakCallback = HAL_TIMEx_BreakCallback; /* Legacy weak BreakCallback */
  6021. htim->Break2Callback = HAL_TIMEx_Break2Callback; /* Legacy weak Break2Callback */
  6022. }
  6023. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  6024. /**
  6025. * @}
  6026. */
  6027. #endif /* HAL_TIM_MODULE_ENABLED */
  6028. /**
  6029. * @}
  6030. */
  6031. /**
  6032. * @}
  6033. */
  6034. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/