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.
 
 
 

5522 lines
170 KiB

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