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.
 
 
 

5384 lines
160 KiB

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