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.
 
 
 

5460 lines
169 KiB

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