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.
 
 
 

6298 lines
196 KiB

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