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.
 
 
 

5496 lines
164 KiB

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