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

7709 lines
244 KiB

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