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.
 
 
 

5400 lines
168 KiB

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