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.
 
 
 

2575 lines
84 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_tim_ex.c
  4. * @author MCD Application Team
  5. * @version V1.1.2
  6. * @date 23-September-2016
  7. * @brief TIM HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Timer extension peripheral:
  10. * + Time Hall Sensor Interface Initialization
  11. * + Time Hall Sensor Interface Start
  12. * + Time Complementary signal bread and dead time configuration
  13. * + Time Master and Slave synchronization configuration
  14. * + Time Output Compare/PWM Channel Configuration (for channels 5 and 6)
  15. * + Time OCRef clear configuration
  16. * + Timer remapping capabilities configuration
  17. @verbatim
  18. ==============================================================================
  19. ##### TIMER Extended features #####
  20. ==============================================================================
  21. [..]
  22. The Timer Extension features include:
  23. (#) Complementary outputs with programmable dead-time for :
  24. (++) Input Capture
  25. (++) Output Compare
  26. (++) PWM generation (Edge and Center-aligned Mode)
  27. (++) One-pulse mode output
  28. (#) Synchronization circuit to control the timer with external signals and to
  29. interconnect several timers together.
  30. (#) Break input to put the timer output signals in reset state or in a known state.
  31. (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
  32. positioning purposes
  33. ##### How to use this driver #####
  34. ==============================================================================
  35. [..]
  36. (#) Initialize the TIM low level resources by implementing the following functions
  37. depending from feature used :
  38. (++) Complementary Output Compare : HAL_TIM_OC_MspInit()
  39. (++) Complementary PWM generation : HAL_TIM_PWM_MspInit()
  40. (++) Complementary One-pulse mode output : HAL_TIM_OnePulse_MspInit()
  41. (++) Hall Sensor output : HAL_TIM_HallSensor_MspInit()
  42. (#) Initialize the TIM low level resources :
  43. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  44. (##) TIM pins configuration
  45. (+++) Enable the clock for the TIM GPIOs using the following function:
  46. __HAL_RCC_GPIOx_CLK_ENABLE();
  47. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  48. (#) The external Clock can be configured, if needed (the default clock is the
  49. internal clock from the APBx), using the following function:
  50. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  51. any start function.
  52. (#) Configure the TIM in the desired functioning mode using one of the
  53. initialization function of this driver:
  54. (++) HAL_TIMEx_HallSensor_Init and HAL_TIMEx_ConfigCommutationEvent: to use the
  55. Timer Hall Sensor Interface and the commutation event with the corresponding
  56. Interrupt and DMA request if needed (Note that One Timer is used to interface
  57. with the Hall sensor Interface and another Timer should be used to use
  58. the commutation event).
  59. (#) Activate the TIM peripheral using one of the start functions:
  60. (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(), HAL_TIMEx_OC_Start_IT()
  61. (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(), HAL_TIMEx_PWMN_Start_IT()
  62. (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
  63. (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(), HAL_TIMEx_HallSensor_Start_IT().
  64. @endverbatim
  65. ******************************************************************************
  66. * @attention
  67. *
  68. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  69. *
  70. * Redistribution and use in source and binary forms, with or without modification,
  71. * are permitted provided that the following conditions are met:
  72. * 1. Redistributions of source code must retain the above copyright notice,
  73. * this list of conditions and the following disclaimer.
  74. * 2. Redistributions in binary form must reproduce the above copyright notice,
  75. * this list of conditions and the following disclaimer in the documentation
  76. * and/or other materials provided with the distribution.
  77. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  78. * may be used to endorse or promote products derived from this software
  79. * without specific prior written permission.
  80. *
  81. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  82. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  83. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  84. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  85. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  86. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  87. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  88. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  89. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  90. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  91. *
  92. ******************************************************************************
  93. */
  94. /* Includes ------------------------------------------------------------------*/
  95. #include "stm32f7xx_hal.h"
  96. /** @addtogroup STM32F7xx_HAL_Driver
  97. * @{
  98. */
  99. /** @defgroup TIMEx TIMEx
  100. * @brief TIM Extended HAL module driver
  101. * @{
  102. */
  103. #ifdef HAL_TIM_MODULE_ENABLED
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private define ------------------------------------------------------------*/
  106. #define BDTR_BKF_SHIFT (16)
  107. #define BDTR_BK2F_SHIFT (20)
  108. /* Private macro -------------------------------------------------------------*/
  109. /* Private variables ---------------------------------------------------------*/
  110. /* Private function prototypes -----------------------------------------------*/
  111. /** @addtogroup TIMEx_Private_Functions
  112. * @{
  113. */
  114. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState);
  115. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  116. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  117. /**
  118. * @}
  119. */
  120. /* Private functions ---------------------------------------------------------*/
  121. /** @defgroup TIMEx_Exported_Functions TIMEx Exported Functions
  122. * @{
  123. */
  124. /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions
  125. * @brief Timer Hall Sensor functions
  126. *
  127. @verbatim
  128. ==============================================================================
  129. ##### Timer Hall Sensor functions #####
  130. ==============================================================================
  131. [..]
  132. This section provides functions allowing to:
  133. (+) Initialize and configure TIM HAL Sensor.
  134. (+) De-initialize TIM HAL Sensor.
  135. (+) Start the Hall Sensor Interface.
  136. (+) Stop the Hall Sensor Interface.
  137. (+) Start the Hall Sensor Interface and enable interrupts.
  138. (+) Stop the Hall Sensor Interface and disable interrupts.
  139. (+) Start the Hall Sensor Interface and enable DMA transfers.
  140. (+) Stop the Hall Sensor Interface and disable DMA transfers.
  141. @endverbatim
  142. * @{
  143. */
  144. /**
  145. * @brief Initializes the TIM Hall Sensor Interface and create the associated handle.
  146. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  147. * the configuration information for TIM module.
  148. * @param sConfig: TIM Hall Sensor configuration structure
  149. * @retval HAL status
  150. */
  151. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef* sConfig)
  152. {
  153. TIM_OC_InitTypeDef OC_Config;
  154. /* Check the TIM handle allocation */
  155. if(htim == NULL)
  156. {
  157. return HAL_ERROR;
  158. }
  159. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  160. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  161. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  162. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  163. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  164. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  165. /* Set the TIM state */
  166. htim->State= HAL_TIM_STATE_BUSY;
  167. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  168. HAL_TIMEx_HallSensor_MspInit(htim);
  169. /* Configure the Time base in the Encoder Mode */
  170. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  171. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  172. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  173. /* Reset the IC1PSC Bits */
  174. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  175. /* Set the IC1PSC value */
  176. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  177. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  178. htim->Instance->CR2 |= TIM_CR2_TI1S;
  179. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  180. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  181. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  182. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  183. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  184. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  185. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  186. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  187. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  188. OC_Config.OCMode = TIM_OCMODE_PWM2;
  189. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  190. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  191. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  192. OC_Config.Pulse = sConfig->Commutation_Delay;
  193. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  194. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  195. register to 101 */
  196. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  197. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  198. /* Initialize the TIM state*/
  199. htim->State= HAL_TIM_STATE_READY;
  200. return HAL_OK;
  201. }
  202. /**
  203. * @brief DeInitializes the TIM Hall Sensor interface
  204. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  205. * the configuration information for TIM module.
  206. * @retval HAL status
  207. */
  208. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  209. {
  210. /* Check the parameters */
  211. assert_param(IS_TIM_INSTANCE(htim->Instance));
  212. htim->State = HAL_TIM_STATE_BUSY;
  213. /* Disable the TIM Peripheral Clock */
  214. __HAL_TIM_DISABLE(htim);
  215. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  216. HAL_TIMEx_HallSensor_MspDeInit(htim);
  217. /* Change TIM state */
  218. htim->State = HAL_TIM_STATE_RESET;
  219. /* Release Lock */
  220. __HAL_UNLOCK(htim);
  221. return HAL_OK;
  222. }
  223. /**
  224. * @brief Initializes the TIM Hall Sensor MSP.
  225. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  226. * the configuration information for TIM module.
  227. * @retval None
  228. */
  229. __weak void HAL_TIMEx_HallSensor_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_TIMEx_HallSensor_MspInit could be implemented in the user file
  235. */
  236. }
  237. /**
  238. * @brief DeInitializes TIM Hall Sensor MSP.
  239. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  240. * the configuration information for TIM module.
  241. * @retval None
  242. */
  243. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  244. {
  245. /* Prevent unused argument(s) compilation warning */
  246. UNUSED(htim);
  247. /* NOTE : This function Should not be modified, when the callback is needed,
  248. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  249. */
  250. }
  251. /**
  252. * @brief Starts the TIM Hall Sensor Interface.
  253. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  254. * the configuration information for TIM module.
  255. * @retval HAL status
  256. */
  257. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  258. {
  259. /* Check the parameters */
  260. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  261. /* Enable the Input Capture channels 1
  262. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  263. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  264. /* Enable the Peripheral */
  265. __HAL_TIM_ENABLE(htim);
  266. /* Return function status */
  267. return HAL_OK;
  268. }
  269. /**
  270. * @brief Stops the TIM Hall sensor Interface.
  271. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  272. * the configuration information for TIM module.
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  276. {
  277. /* Check the parameters */
  278. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  279. /* Disable the Input Capture channels 1, 2 and 3
  280. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  281. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  282. /* Disable the Peripheral */
  283. __HAL_TIM_DISABLE(htim);
  284. /* Return function status */
  285. return HAL_OK;
  286. }
  287. /**
  288. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  289. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  290. * the configuration information for TIM module.
  291. * @retval HAL status
  292. */
  293. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  294. {
  295. /* Check the parameters */
  296. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  297. /* Enable the capture compare Interrupts 1 event */
  298. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  299. /* Enable the Input Capture channels 1
  300. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  301. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  302. /* Enable the Peripheral */
  303. __HAL_TIM_ENABLE(htim);
  304. /* Return function status */
  305. return HAL_OK;
  306. }
  307. /**
  308. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  309. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  310. * the configuration information for TIM module.
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  314. {
  315. /* Check the parameters */
  316. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  317. /* Disable the Input Capture channels 1
  318. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  319. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  320. /* Disable the capture compare Interrupts event */
  321. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  322. /* Disable the Peripheral */
  323. __HAL_TIM_DISABLE(htim);
  324. /* Return function status */
  325. return HAL_OK;
  326. }
  327. /**
  328. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  329. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  330. * the configuration information for TIM module.
  331. * @param pData: The destination Buffer address.
  332. * @param Length: The length of data to be transferred from TIM peripheral to memory.
  333. * @retval HAL status
  334. */
  335. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  336. {
  337. /* Check the parameters */
  338. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  339. if((htim->State == HAL_TIM_STATE_BUSY))
  340. {
  341. return HAL_BUSY;
  342. }
  343. else if((htim->State == HAL_TIM_STATE_READY))
  344. {
  345. if(((uint32_t)pData == 0 ) && (Length > 0))
  346. {
  347. return HAL_ERROR;
  348. }
  349. else
  350. {
  351. htim->State = HAL_TIM_STATE_BUSY;
  352. }
  353. }
  354. /* Enable the Input Capture channels 1
  355. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  356. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  357. /* Set the DMA Input Capture 1 Callback */
  358. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
  359. /* Set the DMA error callback */
  360. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
  361. /* Enable the DMA Stream for Capture 1*/
  362. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
  363. /* Enable the capture compare 1 Interrupt */
  364. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  365. /* Enable the Peripheral */
  366. __HAL_TIM_ENABLE(htim);
  367. /* Return function status */
  368. return HAL_OK;
  369. }
  370. /**
  371. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  372. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  373. * the configuration information for TIM module.
  374. * @retval HAL status
  375. */
  376. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  377. {
  378. /* Check the parameters */
  379. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  380. /* Disable the Input Capture channels 1
  381. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  382. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  383. /* Disable the capture compare Interrupts 1 event */
  384. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  385. /* Disable the Peripheral */
  386. __HAL_TIM_DISABLE(htim);
  387. /* Return function status */
  388. return HAL_OK;
  389. }
  390. /**
  391. * @}
  392. */
  393. /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
  394. * @brief Timer Complementary Output Compare functions
  395. *
  396. @verbatim
  397. ==============================================================================
  398. ##### Timer Complementary Output Compare functions #####
  399. ==============================================================================
  400. [..]
  401. This section provides functions allowing to:
  402. (+) Start the Complementary Output Compare/PWM.
  403. (+) Stop the Complementary Output Compare/PWM.
  404. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  405. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  406. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  407. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  408. @endverbatim
  409. * @{
  410. */
  411. /**
  412. * @brief Starts the TIM Output Compare signal generation on the complementary
  413. * output.
  414. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  415. * the configuration information for TIM module.
  416. * @param Channel: TIM Channel to be enabled.
  417. * This parameter can be one of the following values:
  418. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  419. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  420. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  421. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  422. * @retval HAL status
  423. */
  424. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  425. {
  426. /* Check the parameters */
  427. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  428. /* Enable the Capture compare channel N */
  429. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  430. /* Enable the Main Output */
  431. __HAL_TIM_MOE_ENABLE(htim);
  432. /* Enable the Peripheral */
  433. __HAL_TIM_ENABLE(htim);
  434. /* Return function status */
  435. return HAL_OK;
  436. }
  437. /**
  438. * @brief Stops the TIM Output Compare signal generation on the complementary
  439. * output.
  440. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  441. * the configuration information for TIM module.
  442. * @param Channel: TIM Channel to be disabled.
  443. * This parameter can be one of the following values:
  444. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  445. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  446. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  447. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  448. * @retval HAL status
  449. */
  450. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  451. {
  452. /* Check the parameters */
  453. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  454. /* Disable the Capture compare channel N */
  455. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  456. /* Disable the Main Output */
  457. __HAL_TIM_MOE_DISABLE(htim);
  458. /* Disable the Peripheral */
  459. __HAL_TIM_DISABLE(htim);
  460. /* Return function status */
  461. return HAL_OK;
  462. }
  463. /**
  464. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  465. * on the complementary output.
  466. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  467. * the configuration information for TIM module.
  468. * @param Channel: TIM Channel to be enabled.
  469. * This parameter can be one of the following values:
  470. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  471. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  472. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  473. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  474. * @retval HAL status
  475. */
  476. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  477. {
  478. /* Check the parameters */
  479. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  480. switch (Channel)
  481. {
  482. case TIM_CHANNEL_1:
  483. {
  484. /* Enable the TIM Output Compare interrupt */
  485. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  486. }
  487. break;
  488. case TIM_CHANNEL_2:
  489. {
  490. /* Enable the TIM Output Compare interrupt */
  491. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  492. }
  493. break;
  494. case TIM_CHANNEL_3:
  495. {
  496. /* Enable the TIM Output Compare interrupt */
  497. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  498. }
  499. break;
  500. case TIM_CHANNEL_4:
  501. {
  502. /* Enable the TIM Output Compare interrupt */
  503. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  504. }
  505. break;
  506. default:
  507. break;
  508. }
  509. /* Enable the TIM Break interrupt */
  510. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  511. /* Enable the Capture compare channel N */
  512. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  513. /* Enable the Main Output */
  514. __HAL_TIM_MOE_ENABLE(htim);
  515. /* Enable the Peripheral */
  516. __HAL_TIM_ENABLE(htim);
  517. /* Return function status */
  518. return HAL_OK;
  519. }
  520. /**
  521. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  522. * on the complementary output.
  523. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  524. * the configuration information for TIM module.
  525. * @param Channel: TIM Channel to be disabled.
  526. * This parameter can be one of the following values:
  527. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  528. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  529. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  530. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  531. * @retval HAL status
  532. */
  533. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  534. {
  535. uint32_t tmpccer = 0;
  536. /* Check the parameters */
  537. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  538. switch (Channel)
  539. {
  540. case TIM_CHANNEL_1:
  541. {
  542. /* Disable the TIM Output Compare interrupt */
  543. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  544. }
  545. break;
  546. case TIM_CHANNEL_2:
  547. {
  548. /* Disable the TIM Output Compare interrupt */
  549. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  550. }
  551. break;
  552. case TIM_CHANNEL_3:
  553. {
  554. /* Disable the TIM Output Compare interrupt */
  555. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  556. }
  557. break;
  558. case TIM_CHANNEL_4:
  559. {
  560. /* Disable the TIM Output Compare interrupt */
  561. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  562. }
  563. break;
  564. default:
  565. break;
  566. }
  567. /* Disable the Capture compare channel N */
  568. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  569. /* Disable the TIM Break interrupt (only if no more channel is active) */
  570. tmpccer = htim->Instance->CCER;
  571. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  572. {
  573. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  574. }
  575. /* Disable the Main Output */
  576. __HAL_TIM_MOE_DISABLE(htim);
  577. /* Disable the Peripheral */
  578. __HAL_TIM_DISABLE(htim);
  579. /* Return function status */
  580. return HAL_OK;
  581. }
  582. /**
  583. * @brief Starts the TIM Output Compare signal generation in DMA mode
  584. * on the complementary output.
  585. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  586. * the configuration information for TIM module.
  587. * @param Channel: TIM Channel to be enabled.
  588. * This parameter can be one of the following values:
  589. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  590. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  591. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  592. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  593. * @param pData: The source Buffer address.
  594. * @param Length: The length of data to be transferred from memory to TIM peripheral
  595. * @retval HAL status
  596. */
  597. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  598. {
  599. /* Check the parameters */
  600. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  601. if((htim->State == HAL_TIM_STATE_BUSY))
  602. {
  603. return HAL_BUSY;
  604. }
  605. else if((htim->State == HAL_TIM_STATE_READY))
  606. {
  607. if(((uint32_t)pData == 0 ) && (Length > 0))
  608. {
  609. return HAL_ERROR;
  610. }
  611. else
  612. {
  613. htim->State = HAL_TIM_STATE_BUSY;
  614. }
  615. }
  616. switch (Channel)
  617. {
  618. case TIM_CHANNEL_1:
  619. {
  620. /* Set the DMA Period elapsed callback */
  621. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  622. /* Set the DMA error callback */
  623. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
  624. /* Enable the DMA Stream */
  625. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  626. /* Enable the TIM Output Compare DMA request */
  627. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  628. }
  629. break;
  630. case TIM_CHANNEL_2:
  631. {
  632. /* Set the DMA Period elapsed callback */
  633. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  634. /* Set the DMA error callback */
  635. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
  636. /* Enable the DMA Stream */
  637. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  638. /* Enable the TIM Output Compare DMA request */
  639. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  640. }
  641. break;
  642. case TIM_CHANNEL_3:
  643. {
  644. /* Set the DMA Period elapsed callback */
  645. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  646. /* Set the DMA error callback */
  647. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
  648. /* Enable the DMA Stream */
  649. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  650. /* Enable the TIM Output Compare DMA request */
  651. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  652. }
  653. break;
  654. case TIM_CHANNEL_4:
  655. {
  656. /* Set the DMA Period elapsed callback */
  657. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  658. /* Set the DMA error callback */
  659. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
  660. /* Enable the DMA Stream */
  661. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  662. /* Enable the TIM Output Compare DMA request */
  663. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  664. }
  665. break;
  666. default:
  667. break;
  668. }
  669. /* Enable the Capture compare channel N */
  670. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  671. /* Enable the Main Output */
  672. __HAL_TIM_MOE_ENABLE(htim);
  673. /* Enable the Peripheral */
  674. __HAL_TIM_ENABLE(htim);
  675. /* Return function status */
  676. return HAL_OK;
  677. }
  678. /**
  679. * @brief Stops the TIM Output Compare signal generation in DMA mode
  680. * on the complementary output.
  681. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  682. * the configuration information for TIM module.
  683. * @param Channel: TIM Channel to be disabled.
  684. * This parameter can be one of the following values:
  685. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  686. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  687. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  688. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  689. * @retval HAL status
  690. */
  691. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  692. {
  693. /* Check the parameters */
  694. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  695. switch (Channel)
  696. {
  697. case TIM_CHANNEL_1:
  698. {
  699. /* Disable the TIM Output Compare DMA request */
  700. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  701. }
  702. break;
  703. case TIM_CHANNEL_2:
  704. {
  705. /* Disable the TIM Output Compare DMA request */
  706. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  707. }
  708. break;
  709. case TIM_CHANNEL_3:
  710. {
  711. /* Disable the TIM Output Compare DMA request */
  712. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  713. }
  714. break;
  715. case TIM_CHANNEL_4:
  716. {
  717. /* Disable the TIM Output Compare interrupt */
  718. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  719. }
  720. break;
  721. default:
  722. break;
  723. }
  724. /* Disable the Capture compare channel N */
  725. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  726. /* Disable the Main Output */
  727. __HAL_TIM_MOE_DISABLE(htim);
  728. /* Disable the Peripheral */
  729. __HAL_TIM_DISABLE(htim);
  730. /* Change the htim state */
  731. htim->State = HAL_TIM_STATE_READY;
  732. /* Return function status */
  733. return HAL_OK;
  734. }
  735. /**
  736. * @}
  737. */
  738. /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
  739. * @brief Timer Complementary PWM functions
  740. *
  741. @verbatim
  742. ==============================================================================
  743. ##### Timer Complementary PWM functions #####
  744. ==============================================================================
  745. [..]
  746. This section provides functions allowing to:
  747. (+) Start the Complementary PWM.
  748. (+) Stop the Complementary PWM.
  749. (+) Start the Complementary PWM and enable interrupts.
  750. (+) Stop the Complementary PWM and disable interrupts.
  751. (+) Start the Complementary PWM and enable DMA transfers.
  752. (+) Stop the Complementary PWM and disable DMA transfers.
  753. (+) Start the Complementary Input Capture measurement.
  754. (+) Stop the Complementary Input Capture.
  755. (+) Start the Complementary Input Capture and enable interrupts.
  756. (+) Stop the Complementary Input Capture and disable interrupts.
  757. (+) Start the Complementary Input Capture and enable DMA transfers.
  758. (+) Stop the Complementary Input Capture and disable DMA transfers.
  759. (+) Start the Complementary One Pulse generation.
  760. (+) Stop the Complementary One Pulse.
  761. (+) Start the Complementary One Pulse and enable interrupts.
  762. (+) Stop the Complementary One Pulse and disable interrupts.
  763. @endverbatim
  764. * @{
  765. */
  766. /**
  767. * @brief Starts the PWM signal generation on the complementary output.
  768. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  769. * the configuration information for TIM module.
  770. * @param Channel: TIM Channel to be enabled.
  771. * This parameter can be one of the following values:
  772. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  773. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  774. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  775. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  776. * @retval HAL status
  777. */
  778. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  779. {
  780. /* Check the parameters */
  781. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  782. /* Enable the complementary PWM output */
  783. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  784. /* Enable the Main Output */
  785. __HAL_TIM_MOE_ENABLE(htim);
  786. /* Enable the Peripheral */
  787. __HAL_TIM_ENABLE(htim);
  788. /* Return function status */
  789. return HAL_OK;
  790. }
  791. /**
  792. * @brief Stops the PWM signal generation on the complementary output.
  793. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  794. * the configuration information for TIM module.
  795. * @param Channel: TIM Channel to be disabled.
  796. * This parameter can be one of the following values:
  797. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  798. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  799. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  800. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  801. * @retval HAL status
  802. */
  803. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  804. {
  805. /* Check the parameters */
  806. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  807. /* Disable the complementary PWM output */
  808. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  809. /* Disable the Main Output */
  810. __HAL_TIM_MOE_DISABLE(htim);
  811. /* Disable the Peripheral */
  812. __HAL_TIM_DISABLE(htim);
  813. /* Return function status */
  814. return HAL_OK;
  815. }
  816. /**
  817. * @brief Starts the PWM signal generation in interrupt mode on the
  818. * complementary output.
  819. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  820. * the configuration information for TIM module.
  821. * @param Channel: TIM Channel to be disabled.
  822. * This parameter can be one of the following values:
  823. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  824. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  825. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  826. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  827. * @retval HAL status
  828. */
  829. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  830. {
  831. /* Check the parameters */
  832. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  833. switch (Channel)
  834. {
  835. case TIM_CHANNEL_1:
  836. {
  837. /* Enable the TIM Capture/Compare 1 interrupt */
  838. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  839. }
  840. break;
  841. case TIM_CHANNEL_2:
  842. {
  843. /* Enable the TIM Capture/Compare 2 interrupt */
  844. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  845. }
  846. break;
  847. case TIM_CHANNEL_3:
  848. {
  849. /* Enable the TIM Capture/Compare 3 interrupt */
  850. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  851. }
  852. break;
  853. case TIM_CHANNEL_4:
  854. {
  855. /* Enable the TIM Capture/Compare 4 interrupt */
  856. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  857. }
  858. break;
  859. default:
  860. break;
  861. }
  862. /* Enable the TIM Break interrupt */
  863. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  864. /* Enable the complementary PWM output */
  865. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  866. /* Enable the Main Output */
  867. __HAL_TIM_MOE_ENABLE(htim);
  868. /* Enable the Peripheral */
  869. __HAL_TIM_ENABLE(htim);
  870. /* Return function status */
  871. return HAL_OK;
  872. }
  873. /**
  874. * @brief Stops the PWM signal generation in interrupt mode on the
  875. * complementary output.
  876. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  877. * the configuration information for TIM module.
  878. * @param Channel: TIM Channel to be disabled.
  879. * This parameter can be one of the following values:
  880. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  881. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  882. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  883. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  884. * @retval HAL status
  885. */
  886. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
  887. {
  888. uint32_t tmpccer = 0;
  889. /* Check the parameters */
  890. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  891. switch (Channel)
  892. {
  893. case TIM_CHANNEL_1:
  894. {
  895. /* Disable the TIM Capture/Compare 1 interrupt */
  896. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  897. }
  898. break;
  899. case TIM_CHANNEL_2:
  900. {
  901. /* Disable the TIM Capture/Compare 2 interrupt */
  902. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  903. }
  904. break;
  905. case TIM_CHANNEL_3:
  906. {
  907. /* Disable the TIM Capture/Compare 3 interrupt */
  908. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  909. }
  910. break;
  911. case TIM_CHANNEL_4:
  912. {
  913. /* Disable the TIM Capture/Compare 3 interrupt */
  914. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  915. }
  916. break;
  917. default:
  918. break;
  919. }
  920. /* Disable the complementary PWM output */
  921. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  922. /* Disable the TIM Break interrupt (only if no more channel is active) */
  923. tmpccer = htim->Instance->CCER;
  924. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  925. {
  926. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  927. }
  928. /* Disable the Main Output */
  929. __HAL_TIM_MOE_DISABLE(htim);
  930. /* Disable the Peripheral */
  931. __HAL_TIM_DISABLE(htim);
  932. /* Return function status */
  933. return HAL_OK;
  934. }
  935. /**
  936. * @brief Starts the TIM PWM signal generation in DMA mode on the
  937. * complementary output
  938. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  939. * the configuration information for TIM module.
  940. * @param Channel: TIM Channel to be enabled.
  941. * This parameter can be one of the following values:
  942. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  943. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  944. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  945. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  946. * @param pData: The source Buffer address.
  947. * @param Length: The length of data to be transferred from memory to TIM peripheral
  948. * @retval HAL status
  949. */
  950. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  951. {
  952. /* Check the parameters */
  953. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  954. if((htim->State == HAL_TIM_STATE_BUSY))
  955. {
  956. return HAL_BUSY;
  957. }
  958. else if((htim->State == HAL_TIM_STATE_READY))
  959. {
  960. if(((uint32_t)pData == 0 ) && (Length > 0))
  961. {
  962. return HAL_ERROR;
  963. }
  964. else
  965. {
  966. htim->State = HAL_TIM_STATE_BUSY;
  967. }
  968. }
  969. switch (Channel)
  970. {
  971. case TIM_CHANNEL_1:
  972. {
  973. /* Set the DMA Period elapsed callback */
  974. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  975. /* Set the DMA error callback */
  976. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
  977. /* Enable the DMA Stream */
  978. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  979. /* Enable the TIM Capture/Compare 1 DMA request */
  980. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  981. }
  982. break;
  983. case TIM_CHANNEL_2:
  984. {
  985. /* Set the DMA Period elapsed callback */
  986. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  987. /* Set the DMA error callback */
  988. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
  989. /* Enable the DMA Stream */
  990. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  991. /* Enable the TIM Capture/Compare 2 DMA request */
  992. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  993. }
  994. break;
  995. case TIM_CHANNEL_3:
  996. {
  997. /* Set the DMA Period elapsed callback */
  998. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  999. /* Set the DMA error callback */
  1000. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
  1001. /* Enable the DMA Stream */
  1002. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  1003. /* Enable the TIM Capture/Compare 3 DMA request */
  1004. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1005. }
  1006. break;
  1007. case TIM_CHANNEL_4:
  1008. {
  1009. /* Set the DMA Period elapsed callback */
  1010. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  1011. /* Set the DMA error callback */
  1012. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
  1013. /* Enable the DMA Stream */
  1014. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  1015. /* Enable the TIM Capture/Compare 4 DMA request */
  1016. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1017. }
  1018. break;
  1019. default:
  1020. break;
  1021. }
  1022. /* Enable the complementary PWM output */
  1023. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1024. /* Enable the Main Output */
  1025. __HAL_TIM_MOE_ENABLE(htim);
  1026. /* Enable the Peripheral */
  1027. __HAL_TIM_ENABLE(htim);
  1028. /* Return function status */
  1029. return HAL_OK;
  1030. }
  1031. /**
  1032. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  1033. * output
  1034. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1035. * the configuration information for TIM module.
  1036. * @param Channel: TIM Channel to be disabled.
  1037. * This parameter can be one of the following values:
  1038. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1039. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1040. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1041. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1042. * @retval HAL status
  1043. */
  1044. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1045. {
  1046. /* Check the parameters */
  1047. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1048. switch (Channel)
  1049. {
  1050. case TIM_CHANNEL_1:
  1051. {
  1052. /* Disable the TIM Capture/Compare 1 DMA request */
  1053. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1054. }
  1055. break;
  1056. case TIM_CHANNEL_2:
  1057. {
  1058. /* Disable the TIM Capture/Compare 2 DMA request */
  1059. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1060. }
  1061. break;
  1062. case TIM_CHANNEL_3:
  1063. {
  1064. /* Disable the TIM Capture/Compare 3 DMA request */
  1065. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1066. }
  1067. break;
  1068. case TIM_CHANNEL_4:
  1069. {
  1070. /* Disable the TIM Capture/Compare 4 DMA request */
  1071. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1072. }
  1073. break;
  1074. default:
  1075. break;
  1076. }
  1077. /* Disable the complementary PWM output */
  1078. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1079. /* Disable the Main Output */
  1080. __HAL_TIM_MOE_DISABLE(htim);
  1081. /* Disable the Peripheral */
  1082. __HAL_TIM_DISABLE(htim);
  1083. /* Change the htim state */
  1084. htim->State = HAL_TIM_STATE_READY;
  1085. /* Return function status */
  1086. return HAL_OK;
  1087. }
  1088. /**
  1089. * @}
  1090. */
  1091. /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
  1092. * @brief Timer Complementary One Pulse functions
  1093. *
  1094. @verbatim
  1095. ==============================================================================
  1096. ##### Timer Complementary One Pulse functions #####
  1097. ==============================================================================
  1098. [..]
  1099. This section provides functions allowing to:
  1100. (+) Start the Complementary One Pulse generation.
  1101. (+) Stop the Complementary One Pulse.
  1102. (+) Start the Complementary One Pulse and enable interrupts.
  1103. (+) Stop the Complementary One Pulse and disable interrupts.
  1104. @endverbatim
  1105. * @{
  1106. */
  1107. /**
  1108. * @brief Starts the TIM One Pulse signal generation on the complemetary
  1109. * output.
  1110. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1111. * the configuration information for TIM module.
  1112. * @param OutputChannel: TIM Channel to be enabled.
  1113. * This parameter can be one of the following values:
  1114. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1115. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1116. * @retval HAL status
  1117. */
  1118. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1119. {
  1120. /* Check the parameters */
  1121. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1122. /* Enable the complementary One Pulse output */
  1123. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1124. /* Enable the Main Output */
  1125. __HAL_TIM_MOE_ENABLE(htim);
  1126. /* Return function status */
  1127. return HAL_OK;
  1128. }
  1129. /**
  1130. * @brief Stops the TIM One Pulse signal generation on the complementary
  1131. * output.
  1132. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1133. * the configuration information for TIM module.
  1134. * @param OutputChannel: TIM Channel to be disabled.
  1135. * This parameter can be one of the following values:
  1136. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1137. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1138. * @retval HAL status
  1139. */
  1140. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1141. {
  1142. /* Check the parameters */
  1143. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1144. /* Disable the complementary One Pulse output */
  1145. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1146. /* Disable the Main Output */
  1147. __HAL_TIM_MOE_DISABLE(htim);
  1148. /* Disable the Peripheral */
  1149. __HAL_TIM_DISABLE(htim);
  1150. /* Return function status */
  1151. return HAL_OK;
  1152. }
  1153. /**
  1154. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1155. * complementary channel.
  1156. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1157. * the configuration information for TIM module.
  1158. * @param OutputChannel: TIM Channel to be enabled.
  1159. * This parameter can be one of the following values:
  1160. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1161. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1162. * @retval HAL status
  1163. */
  1164. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1165. {
  1166. /* Check the parameters */
  1167. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1168. /* Enable the TIM Capture/Compare 1 interrupt */
  1169. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1170. /* Enable the TIM Capture/Compare 2 interrupt */
  1171. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1172. /* Enable the complementary One Pulse output */
  1173. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1174. /* Enable the Main Output */
  1175. __HAL_TIM_MOE_ENABLE(htim);
  1176. /* Return function status */
  1177. return HAL_OK;
  1178. }
  1179. /**
  1180. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1181. * complementary channel.
  1182. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1183. * the configuration information for TIM module.
  1184. * @param OutputChannel: TIM Channel to be disabled.
  1185. * This parameter can be one of the following values:
  1186. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1187. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1188. * @retval HAL status
  1189. */
  1190. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1191. {
  1192. /* Check the parameters */
  1193. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1194. /* Disable the TIM Capture/Compare 1 interrupt */
  1195. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1196. /* Disable the TIM Capture/Compare 2 interrupt */
  1197. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1198. /* Disable the complementary One Pulse output */
  1199. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1200. /* Disable the Main Output */
  1201. __HAL_TIM_MOE_DISABLE(htim);
  1202. /* Disable the Peripheral */
  1203. __HAL_TIM_DISABLE(htim);
  1204. /* Return function status */
  1205. return HAL_OK;
  1206. }
  1207. /**
  1208. * @}
  1209. */
  1210. /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
  1211. * @brief Peripheral Control functions
  1212. *
  1213. @verbatim
  1214. ==============================================================================
  1215. ##### Peripheral Control functions #####
  1216. ==============================================================================
  1217. [..]
  1218. This section provides functions allowing to:
  1219. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  1220. (+) Configure External Clock source.
  1221. (+) Configure Complementary channels, break features and dead time.
  1222. (+) Configure Master and the Slave synchronization.
  1223. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1224. (+) Configure the DMA Burst Mode.
  1225. @endverbatim
  1226. * @{
  1227. */
  1228. /**
  1229. * @brief Configure the TIM commutation event sequence.
  1230. * @note This function is mandatory to use the commutation event in order to
  1231. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1232. * the typical use of this feature is with the use of another Timer(interface Timer)
  1233. * configured in Hall sensor interface, this interface Timer will generate the
  1234. * commutation at its TRGO output (connected to Timer used in this function) each time
  1235. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1236. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1237. * the configuration information for TIM module.
  1238. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1239. * This parameter can be one of the following values:
  1240. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1241. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1242. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1243. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1244. * @arg TIM_TS_NONE: No trigger is needed
  1245. * @param CommutationSource: the Commutation Event source.
  1246. * This parameter can be one of the following values:
  1247. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1248. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1249. * @retval HAL status
  1250. */
  1251. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1252. {
  1253. /* Check the parameters */
  1254. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1255. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1256. __HAL_LOCK(htim);
  1257. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1258. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1259. {
  1260. /* Select the Input trigger */
  1261. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1262. htim->Instance->SMCR |= InputTrigger;
  1263. }
  1264. /* Select the Capture Compare preload feature */
  1265. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1266. /* Select the Commutation event source */
  1267. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1268. htim->Instance->CR2 |= CommutationSource;
  1269. __HAL_UNLOCK(htim);
  1270. return HAL_OK;
  1271. }
  1272. /**
  1273. * @brief Configure the TIM commutation event sequence with interrupt.
  1274. * @note This function is mandatory to use the commutation event in order to
  1275. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1276. * the typical use of this feature is with the use of another Timer(interface Timer)
  1277. * configured in Hall sensor interface, this interface Timer will generate the
  1278. * commutation at its TRGO output (connected to Timer used in this function) each time
  1279. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1280. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1281. * the configuration information for TIM module.
  1282. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1283. * This parameter can be one of the following values:
  1284. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1285. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1286. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1287. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1288. * @arg TIM_TS_NONE: No trigger is needed
  1289. * @param CommutationSource: the Commutation Event source.
  1290. * This parameter can be one of the following values:
  1291. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1292. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1293. * @retval HAL status
  1294. */
  1295. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1296. {
  1297. /* Check the parameters */
  1298. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1299. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1300. __HAL_LOCK(htim);
  1301. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1302. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1303. {
  1304. /* Select the Input trigger */
  1305. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1306. htim->Instance->SMCR |= InputTrigger;
  1307. }
  1308. /* Select the Capture Compare preload feature */
  1309. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1310. /* Select the Commutation event source */
  1311. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1312. htim->Instance->CR2 |= CommutationSource;
  1313. /* Enable the Commutation Interrupt Request */
  1314. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1315. __HAL_UNLOCK(htim);
  1316. return HAL_OK;
  1317. }
  1318. /**
  1319. * @brief Configure the TIM commutation event sequence with DMA.
  1320. * @note This function is mandatory to use the commutation event in order to
  1321. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1322. * the typical use of this feature is with the use of another Timer(interface Timer)
  1323. * configured in Hall sensor interface, this interface Timer will generate the
  1324. * commutation at its TRGO output (connected to Timer used in this function) each time
  1325. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1326. * @note: The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1327. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1328. * the configuration information for TIM module.
  1329. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1330. * This parameter can be one of the following values:
  1331. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1332. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1333. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1334. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1335. * @arg TIM_TS_NONE: No trigger is needed
  1336. * @param CommutationSource: the Commutation Event source.
  1337. * This parameter can be one of the following values:
  1338. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1339. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1340. * @retval HAL status
  1341. */
  1342. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1343. {
  1344. /* Check the parameters */
  1345. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1346. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1347. __HAL_LOCK(htim);
  1348. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1349. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1350. {
  1351. /* Select the Input trigger */
  1352. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1353. htim->Instance->SMCR |= InputTrigger;
  1354. }
  1355. /* Select the Capture Compare preload feature */
  1356. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1357. /* Select the Commutation event source */
  1358. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1359. htim->Instance->CR2 |= CommutationSource;
  1360. /* Enable the Commutation DMA Request */
  1361. /* Set the DMA Commutation Callback */
  1362. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = HAL_TIMEx_DMACommutationCplt;
  1363. /* Set the DMA error callback */
  1364. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = HAL_TIM_DMAError;
  1365. /* Enable the Commutation DMA Request */
  1366. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1367. __HAL_UNLOCK(htim);
  1368. return HAL_OK;
  1369. }
  1370. /**
  1371. * @brief Initializes the TIM Output Compare Channels according to the specified
  1372. * parameters in the TIM_OC_InitTypeDef.
  1373. * @param htim: TIM Output Compare handle
  1374. * @param sConfig: TIM Output Compare configuration structure
  1375. * @param Channel : TIM Channels to configure
  1376. * This parameter can be one of the following values:
  1377. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1378. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1379. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1380. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1381. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  1382. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  1383. * @retval HAL status
  1384. */
  1385. HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel)
  1386. {
  1387. /* Check the parameters */
  1388. assert_param(IS_TIM_CHANNELS(Channel));
  1389. assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
  1390. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  1391. /* Check input state */
  1392. __HAL_LOCK(htim);
  1393. htim->State = HAL_TIM_STATE_BUSY;
  1394. switch (Channel)
  1395. {
  1396. case TIM_CHANNEL_1:
  1397. {
  1398. /* Check the parameters */
  1399. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  1400. /* Configure the TIM Channel 1 in Output Compare */
  1401. TIM_OC1_SetConfig(htim->Instance, sConfig);
  1402. }
  1403. break;
  1404. case TIM_CHANNEL_2:
  1405. {
  1406. /* Check the parameters */
  1407. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  1408. /* Configure the TIM Channel 2 in Output Compare */
  1409. TIM_OC2_SetConfig(htim->Instance, sConfig);
  1410. }
  1411. break;
  1412. case TIM_CHANNEL_3:
  1413. {
  1414. /* Check the parameters */
  1415. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  1416. /* Configure the TIM Channel 3 in Output Compare */
  1417. TIM_OC3_SetConfig(htim->Instance, sConfig);
  1418. }
  1419. break;
  1420. case TIM_CHANNEL_4:
  1421. {
  1422. /* Check the parameters */
  1423. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  1424. /* Configure the TIM Channel 4 in Output Compare */
  1425. TIM_OC4_SetConfig(htim->Instance, sConfig);
  1426. }
  1427. break;
  1428. case TIM_CHANNEL_5:
  1429. {
  1430. /* Check the parameters */
  1431. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  1432. /* Configure the TIM Channel 5 in Output Compare */
  1433. TIM_OC5_SetConfig(htim->Instance, sConfig);
  1434. }
  1435. break;
  1436. case TIM_CHANNEL_6:
  1437. {
  1438. /* Check the parameters */
  1439. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  1440. /* Configure the TIM Channel 6 in Output Compare */
  1441. TIM_OC6_SetConfig(htim->Instance, sConfig);
  1442. }
  1443. break;
  1444. default:
  1445. break;
  1446. }
  1447. htim->State = HAL_TIM_STATE_READY;
  1448. __HAL_UNLOCK(htim);
  1449. return HAL_OK;
  1450. }
  1451. /**
  1452. * @brief Initializes the TIM PWM channels according to the specified
  1453. * parameters in the TIM_OC_InitTypeDef.
  1454. * @param htim: TIM PWM handle
  1455. * @param sConfig: TIM PWM configuration structure
  1456. * @param Channel : TIM Channels to be configured
  1457. * This parameter can be one of the following values:
  1458. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1459. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1460. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1461. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1462. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  1463. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  1464. * @retval HAL status
  1465. */
  1466. HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
  1467. TIM_OC_InitTypeDef* sConfig,
  1468. uint32_t Channel)
  1469. {
  1470. /* Check the parameters */
  1471. assert_param(IS_TIM_CHANNELS(Channel));
  1472. assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
  1473. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  1474. assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
  1475. /* Check input state */
  1476. __HAL_LOCK(htim);
  1477. htim->State = HAL_TIM_STATE_BUSY;
  1478. switch (Channel)
  1479. {
  1480. case TIM_CHANNEL_1:
  1481. {
  1482. /* Check the parameters */
  1483. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  1484. /* Configure the Channel 1 in PWM mode */
  1485. TIM_OC1_SetConfig(htim->Instance, sConfig);
  1486. /* Set the Preload enable bit for channel1 */
  1487. htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
  1488. /* Configure the Output Fast mode */
  1489. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
  1490. htim->Instance->CCMR1 |= sConfig->OCFastMode;
  1491. }
  1492. break;
  1493. case TIM_CHANNEL_2:
  1494. {
  1495. /* Check the parameters */
  1496. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  1497. /* Configure the Channel 2 in PWM mode */
  1498. TIM_OC2_SetConfig(htim->Instance, sConfig);
  1499. /* Set the Preload enable bit for channel2 */
  1500. htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
  1501. /* Configure the Output Fast mode */
  1502. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
  1503. htim->Instance->CCMR1 |= sConfig->OCFastMode << 8;
  1504. }
  1505. break;
  1506. case TIM_CHANNEL_3:
  1507. {
  1508. /* Check the parameters */
  1509. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  1510. /* Configure the Channel 3 in PWM mode */
  1511. TIM_OC3_SetConfig(htim->Instance, sConfig);
  1512. /* Set the Preload enable bit for channel3 */
  1513. htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
  1514. /* Configure the Output Fast mode */
  1515. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
  1516. htim->Instance->CCMR2 |= sConfig->OCFastMode;
  1517. }
  1518. break;
  1519. case TIM_CHANNEL_4:
  1520. {
  1521. /* Check the parameters */
  1522. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  1523. /* Configure the Channel 4 in PWM mode */
  1524. TIM_OC4_SetConfig(htim->Instance, sConfig);
  1525. /* Set the Preload enable bit for channel4 */
  1526. htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
  1527. /* Configure the Output Fast mode */
  1528. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
  1529. htim->Instance->CCMR2 |= sConfig->OCFastMode << 8;
  1530. }
  1531. break;
  1532. case TIM_CHANNEL_5:
  1533. {
  1534. /* Check the parameters */
  1535. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  1536. /* Configure the Channel 5 in PWM mode */
  1537. TIM_OC5_SetConfig(htim->Instance, sConfig);
  1538. /* Set the Preload enable bit for channel5*/
  1539. htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
  1540. /* Configure the Output Fast mode */
  1541. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;
  1542. htim->Instance->CCMR3 |= sConfig->OCFastMode;
  1543. }
  1544. break;
  1545. case TIM_CHANNEL_6:
  1546. {
  1547. /* Check the parameters */
  1548. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  1549. /* Configure the Channel 5 in PWM mode */
  1550. TIM_OC6_SetConfig(htim->Instance, sConfig);
  1551. /* Set the Preload enable bit for channel6 */
  1552. htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
  1553. /* Configure the Output Fast mode */
  1554. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;
  1555. htim->Instance->CCMR3 |= sConfig->OCFastMode << 8;
  1556. }
  1557. break;
  1558. default:
  1559. break;
  1560. }
  1561. htim->State = HAL_TIM_STATE_READY;
  1562. __HAL_UNLOCK(htim);
  1563. return HAL_OK;
  1564. }
  1565. /**
  1566. * @brief Configures the OCRef clear feature
  1567. * @param htim: TIM handle
  1568. * @param sClearInputConfig: pointer to a TIM_ClearInputConfigTypeDef structure that
  1569. * contains the OCREF clear feature and parameters for the TIM peripheral.
  1570. * @param Channel: specifies the TIM Channel
  1571. * This parameter can be one of the following values:
  1572. * @arg TIM_Channel_1: TIM Channel 1
  1573. * @arg TIM_Channel_2: TIM Channel 2
  1574. * @arg TIM_Channel_3: TIM Channel 3
  1575. * @arg TIM_Channel_4: TIM Channel 4
  1576. * @arg TIM_Channel_5: TIM Channel 5
  1577. * @arg TIM_Channel_6: TIM Channel 6
  1578. * @retval None
  1579. */
  1580. HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
  1581. TIM_ClearInputConfigTypeDef *sClearInputConfig,
  1582. uint32_t Channel)
  1583. {
  1584. uint32_t tmpsmcr = 0;
  1585. /* Check the parameters */
  1586. assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
  1587. assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
  1588. /* Check input state */
  1589. __HAL_LOCK(htim);
  1590. switch (sClearInputConfig->ClearInputSource)
  1591. {
  1592. case TIM_CLEARINPUTSOURCE_NONE:
  1593. {
  1594. /* Get the TIMx SMCR register value */
  1595. tmpsmcr = htim->Instance->SMCR;
  1596. /* Clear the OCREF clear selection bit */
  1597. tmpsmcr &= ~TIM_SMCR_OCCS;
  1598. /* Clear the ETR Bits */
  1599. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  1600. /* Set TIMx_SMCR */
  1601. htim->Instance->SMCR = tmpsmcr;
  1602. }
  1603. break;
  1604. case TIM_CLEARINPUTSOURCE_OCREFCLR:
  1605. {
  1606. /* Clear the OCREF clear selection bit */
  1607. htim->Instance->SMCR &= ~TIM_SMCR_OCCS;
  1608. }
  1609. break;
  1610. case TIM_CLEARINPUTSOURCE_ETR:
  1611. {
  1612. /* Check the parameters */
  1613. assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
  1614. assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
  1615. assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
  1616. TIM_ETR_SetConfig(htim->Instance,
  1617. sClearInputConfig->ClearInputPrescaler,
  1618. sClearInputConfig->ClearInputPolarity,
  1619. sClearInputConfig->ClearInputFilter);
  1620. /* Set the OCREF clear selection bit */
  1621. htim->Instance->SMCR |= TIM_SMCR_OCCS;
  1622. }
  1623. break;
  1624. default:
  1625. break;
  1626. }
  1627. switch (Channel)
  1628. {
  1629. case TIM_CHANNEL_1:
  1630. {
  1631. if(sClearInputConfig->ClearInputState != RESET)
  1632. {
  1633. /* Enable the Ocref clear feature for Channel 1 */
  1634. htim->Instance->CCMR1 |= TIM_CCMR1_OC1CE;
  1635. }
  1636. else
  1637. {
  1638. /* Disable the Ocref clear feature for Channel 1 */
  1639. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1CE;
  1640. }
  1641. }
  1642. break;
  1643. case TIM_CHANNEL_2:
  1644. {
  1645. if(sClearInputConfig->ClearInputState != RESET)
  1646. {
  1647. /* Enable the Ocref clear feature for Channel 2 */
  1648. htim->Instance->CCMR1 |= TIM_CCMR1_OC2CE;
  1649. }
  1650. else
  1651. {
  1652. /* Disable the Ocref clear feature for Channel 2 */
  1653. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2CE;
  1654. }
  1655. }
  1656. break;
  1657. case TIM_CHANNEL_3:
  1658. {
  1659. if(sClearInputConfig->ClearInputState != RESET)
  1660. {
  1661. /* Enable the Ocref clear feature for Channel 3 */
  1662. htim->Instance->CCMR2 |= TIM_CCMR2_OC3CE;
  1663. }
  1664. else
  1665. {
  1666. /* Disable the Ocref clear feature for Channel 3 */
  1667. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3CE;
  1668. }
  1669. }
  1670. break;
  1671. case TIM_CHANNEL_4:
  1672. {
  1673. if(sClearInputConfig->ClearInputState != RESET)
  1674. {
  1675. /* Enable the Ocref clear feature for Channel 4 */
  1676. htim->Instance->CCMR2 |= TIM_CCMR2_OC4CE;
  1677. }
  1678. else
  1679. {
  1680. /* Disable the Ocref clear feature for Channel 4 */
  1681. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4CE;
  1682. }
  1683. }
  1684. break;
  1685. case TIM_CHANNEL_5:
  1686. {
  1687. if(sClearInputConfig->ClearInputState != RESET)
  1688. {
  1689. /* Enable the Ocref clear feature for Channel 1 */
  1690. htim->Instance->CCMR3 |= TIM_CCMR3_OC5CE;
  1691. }
  1692. else
  1693. {
  1694. /* Disable the Ocref clear feature for Channel 1 */
  1695. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5CE;
  1696. }
  1697. }
  1698. break;
  1699. case TIM_CHANNEL_6:
  1700. {
  1701. if(sClearInputConfig->ClearInputState != RESET)
  1702. {
  1703. /* Enable the Ocref clear feature for Channel 1 */
  1704. htim->Instance->CCMR3 |= TIM_CCMR3_OC6CE;
  1705. }
  1706. else
  1707. {
  1708. /* Disable the Ocref clear feature for Channel 1 */
  1709. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6CE;
  1710. }
  1711. }
  1712. break;
  1713. default:
  1714. break;
  1715. }
  1716. __HAL_UNLOCK(htim);
  1717. return HAL_OK;
  1718. }
  1719. /**
  1720. * @brief Configures the TIM in master mode.
  1721. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1722. * the configuration information for TIM module.
  1723. * @param sMasterConfig: pointer to a TIM_MasterConfigTypeDef structure that
  1724. * contains the selected trigger output (TRGO) and the Master/Slave
  1725. * mode.
  1726. * @retval HAL status
  1727. */
  1728. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, TIM_MasterConfigTypeDef * sMasterConfig)
  1729. {
  1730. uint32_t tmpcr2;
  1731. uint32_t tmpsmcr;
  1732. /* Check the parameters */
  1733. assert_param(IS_TIM_SYNCHRO_INSTANCE(htim->Instance));
  1734. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1735. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1736. /* Check input state */
  1737. __HAL_LOCK(htim);
  1738. /* Get the TIMx CR2 register value */
  1739. tmpcr2 = htim->Instance->CR2;
  1740. /* Get the TIMx SMCR register value */
  1741. tmpsmcr = htim->Instance->SMCR;
  1742. /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
  1743. if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
  1744. {
  1745. /* Check the parameters */
  1746. assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
  1747. /* Clear the MMS2 bits */
  1748. tmpcr2 &= ~TIM_CR2_MMS2;
  1749. /* Select the TRGO2 source*/
  1750. tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
  1751. }
  1752. /* Reset the MMS Bits */
  1753. tmpcr2 &= ~TIM_CR2_MMS;
  1754. /* Select the TRGO source */
  1755. tmpcr2 |= sMasterConfig->MasterOutputTrigger;
  1756. /* Reset the MSM Bit */
  1757. tmpsmcr &= ~TIM_SMCR_MSM;
  1758. /* Set master mode */
  1759. tmpsmcr |= sMasterConfig->MasterSlaveMode;
  1760. /* Update TIMx CR2 */
  1761. htim->Instance->CR2 = tmpcr2;
  1762. /* Update TIMx SMCR */
  1763. htim->Instance->SMCR = tmpsmcr;
  1764. __HAL_UNLOCK(htim);
  1765. return HAL_OK;
  1766. }
  1767. /**
  1768. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1769. * and the AOE(automatic output enable).
  1770. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1771. * the configuration information for TIM module.
  1772. * @param sBreakDeadTimeConfig: pointer to a TIM_ConfigBreakDeadConfig_TypeDef structure that
  1773. * contains the BDTR Register configuration information for the TIM peripheral.
  1774. * @retval HAL status
  1775. */
  1776. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1777. TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)
  1778. {
  1779. uint32_t tmpbdtr = 0;
  1780. /* Check the parameters */
  1781. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1782. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1783. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1784. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1785. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1786. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1787. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1788. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
  1789. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1790. assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
  1791. assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
  1792. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
  1793. /* Check input state */
  1794. __HAL_LOCK(htim);
  1795. htim->State = HAL_TIM_STATE_BUSY;
  1796. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1797. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1798. /* Clear the BDTR bits */
  1799. tmpbdtr &= ~(TIM_BDTR_DTG | TIM_BDTR_LOCK | TIM_BDTR_OSSI |
  1800. TIM_BDTR_OSSR | TIM_BDTR_BKE | TIM_BDTR_BKP |
  1801. TIM_BDTR_AOE | TIM_BDTR_MOE | TIM_BDTR_BKF |
  1802. TIM_BDTR_BK2F | TIM_BDTR_BK2E | TIM_BDTR_BK2P);
  1803. /* Set the BDTR bits */
  1804. tmpbdtr |= sBreakDeadTimeConfig->DeadTime;
  1805. tmpbdtr |= sBreakDeadTimeConfig->LockLevel;
  1806. tmpbdtr |= sBreakDeadTimeConfig->OffStateIDLEMode;
  1807. tmpbdtr |= sBreakDeadTimeConfig->OffStateRunMode;
  1808. tmpbdtr |= sBreakDeadTimeConfig->BreakState;
  1809. tmpbdtr |= sBreakDeadTimeConfig->BreakPolarity;
  1810. tmpbdtr |= sBreakDeadTimeConfig->AutomaticOutput;
  1811. tmpbdtr |= (sBreakDeadTimeConfig->BreakFilter << BDTR_BKF_SHIFT);
  1812. tmpbdtr |= (sBreakDeadTimeConfig->Break2Filter << BDTR_BK2F_SHIFT);
  1813. tmpbdtr |= sBreakDeadTimeConfig->Break2State;
  1814. tmpbdtr |= sBreakDeadTimeConfig->Break2Polarity;
  1815. /* Set TIMx_BDTR */
  1816. htim->Instance->BDTR = tmpbdtr;
  1817. __HAL_UNLOCK(htim);
  1818. return HAL_OK;
  1819. }
  1820. #if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
  1821. /**
  1822. * @brief Configures the break input source.
  1823. * @param htim: TIM handle.
  1824. * @param BreakInput: Break input to configure
  1825. * This parameter can be one of the following values:
  1826. * @arg TIM_BREAKINPUT_BRK: Timer break input
  1827. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  1828. * @param sBreakInputConfig: Break input source configuration
  1829. * @retval HAL status
  1830. */
  1831. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim,
  1832. uint32_t BreakInput,
  1833. TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
  1834. {
  1835. uint32_t tmporx = 0;
  1836. uint32_t bkin_enable_mask = 0;
  1837. uint32_t bkin_enable_bitpos = 0;
  1838. /* Check the parameters */
  1839. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1840. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  1841. assert_param(IS_TIM_BREAKINPUTSOURCE(sBreakInputConfig->Source));
  1842. assert_param(IS_TIM_BREAKINPUTSOURCE_STATE(sBreakInputConfig->Enable));
  1843. /* Check input state */
  1844. __HAL_LOCK(htim);
  1845. switch(sBreakInputConfig->Source)
  1846. {
  1847. case TIM_BREAKINPUTSOURCE_BKIN:
  1848. {
  1849. bkin_enable_mask = TIM1_AF1_BKINE;
  1850. bkin_enable_bitpos = 0;
  1851. }
  1852. break;
  1853. case TIM_BREAKINPUTSOURCE_DFSDM1:
  1854. {
  1855. bkin_enable_mask = TIM1_AF1_BKDF1BKE;
  1856. bkin_enable_bitpos = 8;
  1857. }
  1858. break;
  1859. default:
  1860. break;
  1861. }
  1862. switch(BreakInput)
  1863. {
  1864. case TIM_BREAKINPUT_BRK:
  1865. {
  1866. /* Get the TIMx_AF1 register value */
  1867. tmporx = htim->Instance->AF1;
  1868. /* Enable the break input */
  1869. tmporx &= ~bkin_enable_mask;
  1870. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1871. /* Set TIMx_AF1 */
  1872. htim->Instance->AF1 = tmporx;
  1873. }
  1874. break;
  1875. case TIM_BREAKINPUT_BRK2:
  1876. {
  1877. /* Get the TIMx_AF2 register value */
  1878. tmporx = htim->Instance->AF2;
  1879. /* Enable the break input */
  1880. tmporx &= ~bkin_enable_mask;
  1881. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1882. /* Set TIMx_AF2 */
  1883. htim->Instance->AF2 = tmporx;
  1884. }
  1885. break;
  1886. default:
  1887. break;
  1888. }
  1889. __HAL_UNLOCK(htim);
  1890. return HAL_OK;
  1891. }
  1892. #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
  1893. /**
  1894. * @brief Configures the TIM2, TIM5 and TIM11 Remapping input capabilities.
  1895. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1896. * the configuration information for TIM module.
  1897. * @param Remap: specifies the TIM input remapping source.
  1898. * This parameter can be one of the following values:
  1899. * @arg TIM_TIM2_TIM8_TRGO: TIM2 ITR1 input is connected to TIM8 Trigger output(default)
  1900. * @arg TIM_TIM2_ETH_PTP: TIM2 ITR1 input is connected to ETH PTP trigger output.
  1901. * @arg TIM_TIM2_USBFS_SOF: TIM2 ITR1 input is connected to USB FS SOF.
  1902. * @arg TIM_TIM2_USBHS_SOF: TIM2 ITR1 input is connected to USB HS SOF.
  1903. * @arg TIM_TIM5_GPIO: TIM5 CH4 input is connected to dedicated Timer pin(default)
  1904. * @arg TIM_TIM5_LSI: TIM5 CH4 input is connected to LSI clock.
  1905. * @arg TIM_TIM5_LSE: TIM5 CH4 input is connected to LSE clock.
  1906. * @arg TIM_TIM5_RTC: TIM5 CH4 input is connected to RTC Output event.
  1907. * @arg TIM_TIM11_GPIO: TIM11 CH4 input is connected to dedicated Timer pin(default)
  1908. * @arg TIM_TIM11_SPDIF: SPDIF Frame synchronous
  1909. * @arg TIM_TIM11_HSE: TIM11 CH4 input is connected to HSE_RTC clock
  1910. * (HSE divided by a programmable prescaler)
  1911. * @arg TIM_TIM11_MCO1: TIM11 CH1 input is connected to MCO1
  1912. * @retval HAL status
  1913. */
  1914. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1915. {
  1916. __HAL_LOCK(htim);
  1917. /* Check parameters */
  1918. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  1919. assert_param(IS_TIM_REMAP(Remap));
  1920. /* Set the Timer remapping configuration */
  1921. htim->Instance->OR = Remap;
  1922. htim->State = HAL_TIM_STATE_READY;
  1923. __HAL_UNLOCK(htim);
  1924. return HAL_OK;
  1925. }
  1926. /**
  1927. * @brief Group channel 5 and channel 1, 2 or 3
  1928. * @param htim: TIM handle.
  1929. * @param OCRef: specifies the reference signal(s) the OC5REF is combined with.
  1930. * This parameter can be any combination of the following values:
  1931. * TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
  1932. * TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
  1933. * TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
  1934. * TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
  1935. * @retval HAL status
  1936. */
  1937. HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t OCRef)
  1938. {
  1939. /* Check parameters */
  1940. assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
  1941. assert_param(IS_TIM_GROUPCH5(OCRef));
  1942. /* Process Locked */
  1943. __HAL_LOCK(htim);
  1944. htim->State = HAL_TIM_STATE_BUSY;
  1945. /* Clear GC5Cx bit fields */
  1946. htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3|TIM_CCR5_GC5C2|TIM_CCR5_GC5C1);
  1947. /* Set GC5Cx bit fields */
  1948. htim->Instance->CCR5 |= OCRef;
  1949. htim->State = HAL_TIM_STATE_READY;
  1950. __HAL_UNLOCK(htim);
  1951. return HAL_OK;
  1952. }
  1953. /**
  1954. * @}
  1955. */
  1956. /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
  1957. * @brief Extended Callbacks functions
  1958. *
  1959. @verbatim
  1960. ==============================================================================
  1961. ##### Extension Callbacks functions #####
  1962. ==============================================================================
  1963. [..]
  1964. This section provides Extension TIM callback functions:
  1965. (+) Timer Commutation callback
  1966. (+) Timer Break callback
  1967. @endverbatim
  1968. * @{
  1969. */
  1970. /**
  1971. * @brief Hall commutation changed callback in non blocking mode
  1972. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1973. * the configuration information for TIM module.
  1974. * @retval None
  1975. */
  1976. __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)
  1977. {
  1978. /* Prevent unused argument(s) compilation warning */
  1979. UNUSED(htim);
  1980. /* NOTE : This function Should not be modified, when the callback is needed,
  1981. the HAL_TIMEx_CommutationCallback could be implemented in the user file
  1982. */
  1983. }
  1984. /**
  1985. * @brief Hall Break detection callback in non blocking mode
  1986. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1987. * the configuration information for TIM module.
  1988. * @retval None
  1989. */
  1990. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  1991. {
  1992. /* Prevent unused argument(s) compilation warning */
  1993. UNUSED(htim);
  1994. /* NOTE : This function Should not be modified, when the callback is needed,
  1995. the HAL_TIMEx_BreakCallback could be implemented in the user file
  1996. */
  1997. }
  1998. /**
  1999. * @}
  2000. */
  2001. /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
  2002. * @brief Extended Peripheral State functions
  2003. *
  2004. @verbatim
  2005. ==============================================================================
  2006. ##### Extension Peripheral State functions #####
  2007. ==============================================================================
  2008. [..]
  2009. This subsection permits to get in run-time the status of the peripheral
  2010. and the data flow.
  2011. @endverbatim
  2012. * @{
  2013. */
  2014. /**
  2015. * @brief Return the TIM Hall Sensor interface state
  2016. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  2017. * the configuration information for TIM module.
  2018. * @retval HAL state
  2019. */
  2020. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
  2021. {
  2022. return htim->State;
  2023. }
  2024. /**
  2025. * @}
  2026. */
  2027. /**
  2028. * @brief TIM DMA Commutation callback.
  2029. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  2030. * the configuration information for the specified DMA module.
  2031. * @retval None
  2032. */
  2033. void HAL_TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  2034. {
  2035. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2036. htim->State= HAL_TIM_STATE_READY;
  2037. HAL_TIMEx_CommutationCallback(htim);
  2038. }
  2039. /**
  2040. * @brief Enables or disables the TIM Capture Compare Channel xN.
  2041. * @param TIMx to select the TIM peripheral
  2042. * @param Channel: specifies the TIM Channel
  2043. * This parameter can be one of the following values:
  2044. * @arg TIM_Channel_1: TIM Channel 1
  2045. * @arg TIM_Channel_2: TIM Channel 2
  2046. * @arg TIM_Channel_3: TIM Channel 3
  2047. * @param ChannelNState: specifies the TIM Channel CCxNE bit new state.
  2048. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  2049. * @retval None
  2050. */
  2051. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState)
  2052. {
  2053. uint32_t tmp = 0;
  2054. /* Check the parameters */
  2055. assert_param(IS_TIM_ADVANCED_INSTANCE(TIMx));
  2056. assert_param(IS_TIM_COMPLEMENTARY_CHANNELS(Channel));
  2057. tmp = TIM_CCER_CC1NE << Channel;
  2058. /* Reset the CCxNE Bit */
  2059. TIMx->CCER &= ~tmp;
  2060. /* Set or reset the CCxNE Bit */
  2061. TIMx->CCER |= (uint32_t)(ChannelNState << Channel);
  2062. }
  2063. /**
  2064. * @brief Timer Output Compare 5 configuration
  2065. * @param TIMx to select the TIM peripheral
  2066. * @param OC_Config: The output configuration structure
  2067. * @retval None
  2068. */
  2069. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  2070. {
  2071. uint32_t tmpccmrx = 0;
  2072. uint32_t tmpccer = 0;
  2073. uint32_t tmpcr2 = 0;
  2074. /* Disable the output: Reset the CCxE Bit */
  2075. TIMx->CCER &= ~TIM_CCER_CC5E;
  2076. /* Get the TIMx CCER register value */
  2077. tmpccer = TIMx->CCER;
  2078. /* Get the TIMx CR2 register value */
  2079. tmpcr2 = TIMx->CR2;
  2080. /* Get the TIMx CCMR1 register value */
  2081. tmpccmrx = TIMx->CCMR3;
  2082. /* Reset the Output Compare Mode Bits */
  2083. tmpccmrx &= ~(TIM_CCMR3_OC5M);
  2084. /* Select the Output Compare Mode */
  2085. tmpccmrx |= OC_Config->OCMode;
  2086. /* Reset the Output Polarity level */
  2087. tmpccer &= ~TIM_CCER_CC5P;
  2088. /* Set the Output Compare Polarity */
  2089. tmpccer |= (OC_Config->OCPolarity << 16);
  2090. if(IS_TIM_BREAK_INSTANCE(TIMx))
  2091. {
  2092. /* Reset the Output Compare IDLE State */
  2093. tmpcr2 &= ~TIM_CR2_OIS5;
  2094. /* Set the Output Idle state */
  2095. tmpcr2 |= (OC_Config->OCIdleState << 8);
  2096. }
  2097. /* Write to TIMx CR2 */
  2098. TIMx->CR2 = tmpcr2;
  2099. /* Write to TIMx CCMR3 */
  2100. TIMx->CCMR3 = tmpccmrx;
  2101. /* Set the Capture Compare Register value */
  2102. TIMx->CCR5 = OC_Config->Pulse;
  2103. /* Write to TIMx CCER */
  2104. TIMx->CCER = tmpccer;
  2105. }
  2106. /**
  2107. * @brief Timer Output Compare 6 configuration
  2108. * @param TIMx to select the TIM peripheral
  2109. * @param OC_Config: The output configuration structure
  2110. * @retval None
  2111. */
  2112. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  2113. {
  2114. uint32_t tmpccmrx = 0;
  2115. uint32_t tmpccer = 0;
  2116. uint32_t tmpcr2 = 0;
  2117. /* Disable the output: Reset the CCxE Bit */
  2118. TIMx->CCER &= ~TIM_CCER_CC6E;
  2119. /* Get the TIMx CCER register value */
  2120. tmpccer = TIMx->CCER;
  2121. /* Get the TIMx CR2 register value */
  2122. tmpcr2 = TIMx->CR2;
  2123. /* Get the TIMx CCMR1 register value */
  2124. tmpccmrx = TIMx->CCMR3;
  2125. /* Reset the Output Compare Mode Bits */
  2126. tmpccmrx &= ~(TIM_CCMR3_OC6M);
  2127. /* Select the Output Compare Mode */
  2128. tmpccmrx |= (OC_Config->OCMode << 8);
  2129. /* Reset the Output Polarity level */
  2130. tmpccer &= (uint32_t)~TIM_CCER_CC6P;
  2131. /* Set the Output Compare Polarity */
  2132. tmpccer |= (OC_Config->OCPolarity << 20);
  2133. if(IS_TIM_BREAK_INSTANCE(TIMx))
  2134. {
  2135. /* Reset the Output Compare IDLE State */
  2136. tmpcr2 &= ~TIM_CR2_OIS6;
  2137. /* Set the Output Idle state */
  2138. tmpcr2 |= (OC_Config->OCIdleState << 10);
  2139. }
  2140. /* Write to TIMx CR2 */
  2141. TIMx->CR2 = tmpcr2;
  2142. /* Write to TIMx CCMR3 */
  2143. TIMx->CCMR3 = tmpccmrx;
  2144. /* Set the Capture Compare Register value */
  2145. TIMx->CCR6 = OC_Config->Pulse;
  2146. /* Write to TIMx CCER */
  2147. TIMx->CCER = tmpccer;
  2148. }
  2149. /**
  2150. * @}
  2151. */
  2152. #endif /* HAL_TIM_MODULE_ENABLED */
  2153. /**
  2154. * @}
  2155. */
  2156. /**
  2157. * @}
  2158. */
  2159. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/