25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

7627 satır
238 KiB

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