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.
 
 
 

2482 lines
81 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_tim_ex.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief TIM HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Timer 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 __TIMx_CLK_ENABLE();
  44. (##) TIM pins configuration
  45. (+++) Enable the clock for the TIM GPIOs using the following function:
  46. __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) 2015 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. /* NOTE : This function Should not be modified, when the callback is needed,
  232. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  233. */
  234. }
  235. /**
  236. * @brief DeInitializes TIM Hall Sensor MSP.
  237. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  238. * the configuration information for TIM module.
  239. * @retval None
  240. */
  241. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  242. {
  243. /* NOTE : This function Should not be modified, when the callback is needed,
  244. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  245. */
  246. }
  247. /**
  248. * @brief Starts the TIM Hall Sensor Interface.
  249. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  250. * the configuration information for TIM module.
  251. * @retval HAL status
  252. */
  253. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  254. {
  255. /* Check the parameters */
  256. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  257. /* Enable the Input Capture channels 1
  258. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  259. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  260. /* Enable the Peripheral */
  261. __HAL_TIM_ENABLE(htim);
  262. /* Return function status */
  263. return HAL_OK;
  264. }
  265. /**
  266. * @brief Stops the TIM Hall sensor Interface.
  267. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  268. * the configuration information for TIM module.
  269. * @retval HAL status
  270. */
  271. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  272. {
  273. /* Check the parameters */
  274. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  275. /* Disable the Input Capture channels 1, 2 and 3
  276. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  277. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  278. /* Disable the Peripheral */
  279. __HAL_TIM_DISABLE(htim);
  280. /* Return function status */
  281. return HAL_OK;
  282. }
  283. /**
  284. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  285. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  286. * the configuration information for TIM module.
  287. * @retval HAL status
  288. */
  289. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  290. {
  291. /* Check the parameters */
  292. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  293. /* Enable the capture compare Interrupts 1 event */
  294. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  295. /* Enable the Input Capture channels 1
  296. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  297. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  298. /* Enable the Peripheral */
  299. __HAL_TIM_ENABLE(htim);
  300. /* Return function status */
  301. return HAL_OK;
  302. }
  303. /**
  304. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  305. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  306. * the configuration information for TIM module.
  307. * @retval HAL status
  308. */
  309. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  310. {
  311. /* Check the parameters */
  312. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  313. /* Disable the Input Capture channels 1
  314. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  315. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  316. /* Disable the capture compare Interrupts event */
  317. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  318. /* Disable the Peripheral */
  319. __HAL_TIM_DISABLE(htim);
  320. /* Return function status */
  321. return HAL_OK;
  322. }
  323. /**
  324. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  325. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  326. * the configuration information for TIM module.
  327. * @param pData: The destination Buffer address.
  328. * @param Length: The length of data to be transferred from TIM peripheral to memory.
  329. * @retval HAL status
  330. */
  331. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  332. {
  333. /* Check the parameters */
  334. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  335. if((htim->State == HAL_TIM_STATE_BUSY))
  336. {
  337. return HAL_BUSY;
  338. }
  339. else if((htim->State == HAL_TIM_STATE_READY))
  340. {
  341. if(((uint32_t)pData == 0 ) && (Length > 0))
  342. {
  343. return HAL_ERROR;
  344. }
  345. else
  346. {
  347. htim->State = HAL_TIM_STATE_BUSY;
  348. }
  349. }
  350. /* Enable the Input Capture channels 1
  351. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  352. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  353. /* Set the DMA Input Capture 1 Callback */
  354. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMACaptureCplt;
  355. /* Set the DMA error callback */
  356. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
  357. /* Enable the DMA Stream for Capture 1*/
  358. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
  359. /* Enable the capture compare 1 Interrupt */
  360. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  361. /* Enable the Peripheral */
  362. __HAL_TIM_ENABLE(htim);
  363. /* Return function status */
  364. return HAL_OK;
  365. }
  366. /**
  367. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  368. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  369. * the configuration information for TIM module.
  370. * @retval HAL status
  371. */
  372. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  373. {
  374. /* Check the parameters */
  375. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  376. /* Disable the Input Capture channels 1
  377. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  378. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  379. /* Disable the capture compare Interrupts 1 event */
  380. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  381. /* Disable the Peripheral */
  382. __HAL_TIM_DISABLE(htim);
  383. /* Return function status */
  384. return HAL_OK;
  385. }
  386. /**
  387. * @}
  388. */
  389. /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
  390. * @brief Timer Complementary Output Compare functions
  391. *
  392. @verbatim
  393. ==============================================================================
  394. ##### Timer Complementary Output Compare functions #####
  395. ==============================================================================
  396. [..]
  397. This section provides functions allowing to:
  398. (+) Start the Complementary Output Compare/PWM.
  399. (+) Stop the Complementary Output Compare/PWM.
  400. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  401. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  402. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  403. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  404. @endverbatim
  405. * @{
  406. */
  407. /**
  408. * @brief Starts the TIM Output Compare signal generation on the complementary
  409. * output.
  410. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  411. * the configuration information for TIM module.
  412. * @param Channel: TIM Channel to be enabled.
  413. * This parameter can be one of the following values:
  414. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  415. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  416. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  417. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  418. * @retval HAL status
  419. */
  420. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  421. {
  422. /* Check the parameters */
  423. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  424. /* Enable the Capture compare channel N */
  425. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  426. /* Enable the Main Output */
  427. __HAL_TIM_MOE_ENABLE(htim);
  428. /* Enable the Peripheral */
  429. __HAL_TIM_ENABLE(htim);
  430. /* Return function status */
  431. return HAL_OK;
  432. }
  433. /**
  434. * @brief Stops the TIM Output Compare signal generation on the complementary
  435. * output.
  436. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  437. * the configuration information for TIM module.
  438. * @param Channel: TIM Channel to be disabled.
  439. * This parameter can be one of the following values:
  440. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  441. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  442. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  443. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  444. * @retval HAL status
  445. */
  446. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  447. {
  448. /* Check the parameters */
  449. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  450. /* Disable the Capture compare channel N */
  451. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  452. /* Disable the Main Output */
  453. __HAL_TIM_MOE_DISABLE(htim);
  454. /* Disable the Peripheral */
  455. __HAL_TIM_DISABLE(htim);
  456. /* Return function status */
  457. return HAL_OK;
  458. }
  459. /**
  460. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  461. * on the complementary output.
  462. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  463. * the configuration information for TIM module.
  464. * @param Channel: TIM Channel to be enabled.
  465. * This parameter can be one of the following values:
  466. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  467. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  468. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  469. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  470. * @retval HAL status
  471. */
  472. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  473. {
  474. /* Check the parameters */
  475. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  476. switch (Channel)
  477. {
  478. case TIM_CHANNEL_1:
  479. {
  480. /* Enable the TIM Output Compare interrupt */
  481. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  482. }
  483. break;
  484. case TIM_CHANNEL_2:
  485. {
  486. /* Enable the TIM Output Compare interrupt */
  487. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  488. }
  489. break;
  490. case TIM_CHANNEL_3:
  491. {
  492. /* Enable the TIM Output Compare interrupt */
  493. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  494. }
  495. break;
  496. case TIM_CHANNEL_4:
  497. {
  498. /* Enable the TIM Output Compare interrupt */
  499. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  500. }
  501. break;
  502. default:
  503. break;
  504. }
  505. /* Enable the TIM Break interrupt */
  506. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  507. /* Enable the Capture compare channel N */
  508. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  509. /* Enable the Main Output */
  510. __HAL_TIM_MOE_ENABLE(htim);
  511. /* Enable the Peripheral */
  512. __HAL_TIM_ENABLE(htim);
  513. /* Return function status */
  514. return HAL_OK;
  515. }
  516. /**
  517. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  518. * on the complementary output.
  519. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  520. * the configuration information for TIM module.
  521. * @param Channel: TIM Channel to be disabled.
  522. * This parameter can be one of the following values:
  523. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  524. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  525. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  526. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  527. * @retval HAL status
  528. */
  529. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  530. {
  531. uint32_t tmpccer = 0;
  532. /* Check the parameters */
  533. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  534. switch (Channel)
  535. {
  536. case TIM_CHANNEL_1:
  537. {
  538. /* Disable the TIM Output Compare interrupt */
  539. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  540. }
  541. break;
  542. case TIM_CHANNEL_2:
  543. {
  544. /* Disable the TIM Output Compare interrupt */
  545. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  546. }
  547. break;
  548. case TIM_CHANNEL_3:
  549. {
  550. /* Disable the TIM Output Compare interrupt */
  551. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  552. }
  553. break;
  554. case TIM_CHANNEL_4:
  555. {
  556. /* Disable the TIM Output Compare interrupt */
  557. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  558. }
  559. break;
  560. default:
  561. break;
  562. }
  563. /* Disable the Capture compare channel N */
  564. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  565. /* Disable the TIM Break interrupt (only if no more channel is active) */
  566. tmpccer = htim->Instance->CCER;
  567. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  568. {
  569. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  570. }
  571. /* Disable the Main Output */
  572. __HAL_TIM_MOE_DISABLE(htim);
  573. /* Disable the Peripheral */
  574. __HAL_TIM_DISABLE(htim);
  575. /* Return function status */
  576. return HAL_OK;
  577. }
  578. /**
  579. * @brief Starts the TIM Output Compare signal generation in DMA mode
  580. * on the complementary output.
  581. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  582. * the configuration information for TIM module.
  583. * @param Channel: TIM Channel to be enabled.
  584. * This parameter can be one of the following values:
  585. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  586. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  587. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  588. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  589. * @param pData: The source Buffer address.
  590. * @param Length: The length of data to be transferred from memory to TIM peripheral
  591. * @retval HAL status
  592. */
  593. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  594. {
  595. /* Check the parameters */
  596. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  597. if((htim->State == HAL_TIM_STATE_BUSY))
  598. {
  599. return HAL_BUSY;
  600. }
  601. else if((htim->State == HAL_TIM_STATE_READY))
  602. {
  603. if(((uint32_t)pData == 0 ) && (Length > 0))
  604. {
  605. return HAL_ERROR;
  606. }
  607. else
  608. {
  609. htim->State = HAL_TIM_STATE_BUSY;
  610. }
  611. }
  612. switch (Channel)
  613. {
  614. case TIM_CHANNEL_1:
  615. {
  616. /* Set the DMA Period elapsed callback */
  617. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  618. /* Set the DMA error callback */
  619. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
  620. /* Enable the DMA Stream */
  621. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  622. /* Enable the TIM Output Compare DMA request */
  623. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  624. }
  625. break;
  626. case TIM_CHANNEL_2:
  627. {
  628. /* Set the DMA Period elapsed callback */
  629. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  630. /* Set the DMA error callback */
  631. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
  632. /* Enable the DMA Stream */
  633. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  634. /* Enable the TIM Output Compare DMA request */
  635. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  636. }
  637. break;
  638. case TIM_CHANNEL_3:
  639. {
  640. /* Set the DMA Period elapsed callback */
  641. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  642. /* Set the DMA error callback */
  643. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
  644. /* Enable the DMA Stream */
  645. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  646. /* Enable the TIM Output Compare DMA request */
  647. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  648. }
  649. break;
  650. case TIM_CHANNEL_4:
  651. {
  652. /* Set the DMA Period elapsed callback */
  653. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  654. /* Set the DMA error callback */
  655. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
  656. /* Enable the DMA Stream */
  657. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  658. /* Enable the TIM Output Compare DMA request */
  659. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  660. }
  661. break;
  662. default:
  663. break;
  664. }
  665. /* Enable the Capture compare channel N */
  666. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  667. /* Enable the Main Output */
  668. __HAL_TIM_MOE_ENABLE(htim);
  669. /* Enable the Peripheral */
  670. __HAL_TIM_ENABLE(htim);
  671. /* Return function status */
  672. return HAL_OK;
  673. }
  674. /**
  675. * @brief Stops the TIM Output Compare signal generation in DMA mode
  676. * on the complementary output.
  677. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  678. * the configuration information for TIM module.
  679. * @param Channel: TIM Channel to be disabled.
  680. * This parameter can be one of the following values:
  681. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  682. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  683. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  684. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  685. * @retval HAL status
  686. */
  687. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  688. {
  689. /* Check the parameters */
  690. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  691. switch (Channel)
  692. {
  693. case TIM_CHANNEL_1:
  694. {
  695. /* Disable the TIM Output Compare DMA request */
  696. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  697. }
  698. break;
  699. case TIM_CHANNEL_2:
  700. {
  701. /* Disable the TIM Output Compare DMA request */
  702. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  703. }
  704. break;
  705. case TIM_CHANNEL_3:
  706. {
  707. /* Disable the TIM Output Compare DMA request */
  708. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  709. }
  710. break;
  711. case TIM_CHANNEL_4:
  712. {
  713. /* Disable the TIM Output Compare interrupt */
  714. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  715. }
  716. break;
  717. default:
  718. break;
  719. }
  720. /* Disable the Capture compare channel N */
  721. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  722. /* Disable the Main Output */
  723. __HAL_TIM_MOE_DISABLE(htim);
  724. /* Disable the Peripheral */
  725. __HAL_TIM_DISABLE(htim);
  726. /* Change the htim state */
  727. htim->State = HAL_TIM_STATE_READY;
  728. /* Return function status */
  729. return HAL_OK;
  730. }
  731. /**
  732. * @}
  733. */
  734. /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
  735. * @brief Timer Complementary PWM functions
  736. *
  737. @verbatim
  738. ==============================================================================
  739. ##### Timer Complementary PWM functions #####
  740. ==============================================================================
  741. [..]
  742. This section provides functions allowing to:
  743. (+) Start the Complementary PWM.
  744. (+) Stop the Complementary PWM.
  745. (+) Start the Complementary PWM and enable interrupts.
  746. (+) Stop the Complementary PWM and disable interrupts.
  747. (+) Start the Complementary PWM and enable DMA transfers.
  748. (+) Stop the Complementary PWM and disable DMA transfers.
  749. (+) Start the Complementary Input Capture measurement.
  750. (+) Stop the Complementary Input Capture.
  751. (+) Start the Complementary Input Capture and enable interrupts.
  752. (+) Stop the Complementary Input Capture and disable interrupts.
  753. (+) Start the Complementary Input Capture and enable DMA transfers.
  754. (+) Stop the Complementary Input Capture and disable DMA transfers.
  755. (+) Start the Complementary One Pulse generation.
  756. (+) Stop the Complementary One Pulse.
  757. (+) Start the Complementary One Pulse and enable interrupts.
  758. (+) Stop the Complementary One Pulse and disable interrupts.
  759. @endverbatim
  760. * @{
  761. */
  762. /**
  763. * @brief Starts the PWM signal generation on the complementary output.
  764. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  765. * the configuration information for TIM module.
  766. * @param Channel: TIM Channel to be enabled.
  767. * This parameter can be one of the following values:
  768. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  769. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  770. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  771. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  772. * @retval HAL status
  773. */
  774. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  775. {
  776. /* Check the parameters */
  777. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  778. /* Enable the complementary PWM output */
  779. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  780. /* Enable the Main Output */
  781. __HAL_TIM_MOE_ENABLE(htim);
  782. /* Enable the Peripheral */
  783. __HAL_TIM_ENABLE(htim);
  784. /* Return function status */
  785. return HAL_OK;
  786. }
  787. /**
  788. * @brief Stops the PWM signal generation on the complementary output.
  789. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  790. * the configuration information for TIM module.
  791. * @param Channel: TIM Channel to be disabled.
  792. * This parameter can be one of the following values:
  793. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  794. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  795. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  796. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  797. * @retval HAL status
  798. */
  799. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  800. {
  801. /* Check the parameters */
  802. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  803. /* Disable the complementary PWM output */
  804. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  805. /* Disable the Main Output */
  806. __HAL_TIM_MOE_DISABLE(htim);
  807. /* Disable the Peripheral */
  808. __HAL_TIM_DISABLE(htim);
  809. /* Return function status */
  810. return HAL_OK;
  811. }
  812. /**
  813. * @brief Starts the PWM signal generation in interrupt mode on the
  814. * complementary output.
  815. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  816. * the configuration information for TIM module.
  817. * @param Channel: TIM Channel to be disabled.
  818. * This parameter can be one of the following values:
  819. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  820. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  821. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  822. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  823. * @retval HAL status
  824. */
  825. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  826. {
  827. /* Check the parameters */
  828. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  829. switch (Channel)
  830. {
  831. case TIM_CHANNEL_1:
  832. {
  833. /* Enable the TIM Capture/Compare 1 interrupt */
  834. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  835. }
  836. break;
  837. case TIM_CHANNEL_2:
  838. {
  839. /* Enable the TIM Capture/Compare 2 interrupt */
  840. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  841. }
  842. break;
  843. case TIM_CHANNEL_3:
  844. {
  845. /* Enable the TIM Capture/Compare 3 interrupt */
  846. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  847. }
  848. break;
  849. case TIM_CHANNEL_4:
  850. {
  851. /* Enable the TIM Capture/Compare 4 interrupt */
  852. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  853. }
  854. break;
  855. default:
  856. break;
  857. }
  858. /* Enable the TIM Break interrupt */
  859. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  860. /* Enable the complementary PWM output */
  861. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  862. /* Enable the Main Output */
  863. __HAL_TIM_MOE_ENABLE(htim);
  864. /* Enable the Peripheral */
  865. __HAL_TIM_ENABLE(htim);
  866. /* Return function status */
  867. return HAL_OK;
  868. }
  869. /**
  870. * @brief Stops the PWM signal generation in interrupt mode on the
  871. * complementary output.
  872. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  873. * the configuration information for TIM module.
  874. * @param Channel: TIM Channel to be disabled.
  875. * This parameter can be one of the following values:
  876. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  877. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  878. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  879. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  880. * @retval HAL status
  881. */
  882. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
  883. {
  884. uint32_t tmpccer = 0;
  885. /* Check the parameters */
  886. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  887. switch (Channel)
  888. {
  889. case TIM_CHANNEL_1:
  890. {
  891. /* Disable the TIM Capture/Compare 1 interrupt */
  892. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  893. }
  894. break;
  895. case TIM_CHANNEL_2:
  896. {
  897. /* Disable the TIM Capture/Compare 2 interrupt */
  898. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  899. }
  900. break;
  901. case TIM_CHANNEL_3:
  902. {
  903. /* Disable the TIM Capture/Compare 3 interrupt */
  904. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  905. }
  906. break;
  907. case TIM_CHANNEL_4:
  908. {
  909. /* Disable the TIM Capture/Compare 3 interrupt */
  910. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  911. }
  912. break;
  913. default:
  914. break;
  915. }
  916. /* Disable the complementary PWM output */
  917. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  918. /* Disable the TIM Break interrupt (only if no more channel is active) */
  919. tmpccer = htim->Instance->CCER;
  920. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  921. {
  922. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  923. }
  924. /* Disable the Main Output */
  925. __HAL_TIM_MOE_DISABLE(htim);
  926. /* Disable the Peripheral */
  927. __HAL_TIM_DISABLE(htim);
  928. /* Return function status */
  929. return HAL_OK;
  930. }
  931. /**
  932. * @brief Starts the TIM PWM signal generation in DMA mode on the
  933. * complementary output
  934. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  935. * the configuration information for TIM module.
  936. * @param Channel: TIM Channel to be enabled.
  937. * This parameter can be one of the following values:
  938. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  939. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  940. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  941. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  942. * @param pData: The source Buffer address.
  943. * @param Length: The length of data to be transferred from memory to TIM peripheral
  944. * @retval HAL status
  945. */
  946. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  947. {
  948. /* Check the parameters */
  949. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  950. if((htim->State == HAL_TIM_STATE_BUSY))
  951. {
  952. return HAL_BUSY;
  953. }
  954. else if((htim->State == HAL_TIM_STATE_READY))
  955. {
  956. if(((uint32_t)pData == 0 ) && (Length > 0))
  957. {
  958. return HAL_ERROR;
  959. }
  960. else
  961. {
  962. htim->State = HAL_TIM_STATE_BUSY;
  963. }
  964. }
  965. switch (Channel)
  966. {
  967. case TIM_CHANNEL_1:
  968. {
  969. /* Set the DMA Period elapsed callback */
  970. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  971. /* Set the DMA error callback */
  972. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = HAL_TIM_DMAError ;
  973. /* Enable the DMA Stream */
  974. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  975. /* Enable the TIM Capture/Compare 1 DMA request */
  976. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  977. }
  978. break;
  979. case TIM_CHANNEL_2:
  980. {
  981. /* Set the DMA Period elapsed callback */
  982. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  983. /* Set the DMA error callback */
  984. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = HAL_TIM_DMAError ;
  985. /* Enable the DMA Stream */
  986. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  987. /* Enable the TIM Capture/Compare 2 DMA request */
  988. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  989. }
  990. break;
  991. case TIM_CHANNEL_3:
  992. {
  993. /* Set the DMA Period elapsed callback */
  994. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  995. /* Set the DMA error callback */
  996. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = HAL_TIM_DMAError ;
  997. /* Enable the DMA Stream */
  998. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  999. /* Enable the TIM Capture/Compare 3 DMA request */
  1000. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1001. }
  1002. break;
  1003. case TIM_CHANNEL_4:
  1004. {
  1005. /* Set the DMA Period elapsed callback */
  1006. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = HAL_TIM_DMADelayPulseCplt;
  1007. /* Set the DMA error callback */
  1008. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = HAL_TIM_DMAError ;
  1009. /* Enable the DMA Stream */
  1010. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  1011. /* Enable the TIM Capture/Compare 4 DMA request */
  1012. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1013. }
  1014. break;
  1015. default:
  1016. break;
  1017. }
  1018. /* Enable the complementary PWM output */
  1019. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1020. /* Enable the Main Output */
  1021. __HAL_TIM_MOE_ENABLE(htim);
  1022. /* Enable the Peripheral */
  1023. __HAL_TIM_ENABLE(htim);
  1024. /* Return function status */
  1025. return HAL_OK;
  1026. }
  1027. /**
  1028. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  1029. * output
  1030. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1031. * the configuration information for TIM module.
  1032. * @param Channel: TIM Channel to be disabled.
  1033. * This parameter can be one of the following values:
  1034. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1035. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1036. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1037. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1038. * @retval HAL status
  1039. */
  1040. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1041. {
  1042. /* Check the parameters */
  1043. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1044. switch (Channel)
  1045. {
  1046. case TIM_CHANNEL_1:
  1047. {
  1048. /* Disable the TIM Capture/Compare 1 DMA request */
  1049. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1050. }
  1051. break;
  1052. case TIM_CHANNEL_2:
  1053. {
  1054. /* Disable the TIM Capture/Compare 2 DMA request */
  1055. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1056. }
  1057. break;
  1058. case TIM_CHANNEL_3:
  1059. {
  1060. /* Disable the TIM Capture/Compare 3 DMA request */
  1061. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1062. }
  1063. break;
  1064. case TIM_CHANNEL_4:
  1065. {
  1066. /* Disable the TIM Capture/Compare 4 DMA request */
  1067. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1068. }
  1069. break;
  1070. default:
  1071. break;
  1072. }
  1073. /* Disable the complementary PWM output */
  1074. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1075. /* Disable the Main Output */
  1076. __HAL_TIM_MOE_DISABLE(htim);
  1077. /* Disable the Peripheral */
  1078. __HAL_TIM_DISABLE(htim);
  1079. /* Change the htim state */
  1080. htim->State = HAL_TIM_STATE_READY;
  1081. /* Return function status */
  1082. return HAL_OK;
  1083. }
  1084. /**
  1085. * @}
  1086. */
  1087. /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
  1088. * @brief Timer Complementary One Pulse functions
  1089. *
  1090. @verbatim
  1091. ==============================================================================
  1092. ##### Timer Complementary One Pulse functions #####
  1093. ==============================================================================
  1094. [..]
  1095. This section provides functions allowing to:
  1096. (+) Start the Complementary One Pulse generation.
  1097. (+) Stop the Complementary One Pulse.
  1098. (+) Start the Complementary One Pulse and enable interrupts.
  1099. (+) Stop the Complementary One Pulse and disable interrupts.
  1100. @endverbatim
  1101. * @{
  1102. */
  1103. /**
  1104. * @brief Starts the TIM One Pulse signal generation on the complemetary
  1105. * output.
  1106. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1107. * the configuration information for TIM module.
  1108. * @param OutputChannel: TIM Channel to be enabled.
  1109. * This parameter can be one of the following values:
  1110. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1111. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1112. * @retval HAL status
  1113. */
  1114. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1115. {
  1116. /* Check the parameters */
  1117. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1118. /* Enable the complementary One Pulse output */
  1119. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1120. /* Enable the Main Output */
  1121. __HAL_TIM_MOE_ENABLE(htim);
  1122. /* Return function status */
  1123. return HAL_OK;
  1124. }
  1125. /**
  1126. * @brief Stops the TIM One Pulse signal generation on the complementary
  1127. * output.
  1128. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1129. * the configuration information for TIM module.
  1130. * @param OutputChannel: TIM Channel to be disabled.
  1131. * This parameter can be one of the following values:
  1132. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1133. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1134. * @retval HAL status
  1135. */
  1136. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1137. {
  1138. /* Check the parameters */
  1139. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1140. /* Disable the complementary One Pulse output */
  1141. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1142. /* Disable the Main Output */
  1143. __HAL_TIM_MOE_DISABLE(htim);
  1144. /* Disable the Peripheral */
  1145. __HAL_TIM_DISABLE(htim);
  1146. /* Return function status */
  1147. return HAL_OK;
  1148. }
  1149. /**
  1150. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1151. * complementary channel.
  1152. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1153. * the configuration information for TIM module.
  1154. * @param OutputChannel: TIM Channel to be enabled.
  1155. * This parameter can be one of the following values:
  1156. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1157. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1158. * @retval HAL status
  1159. */
  1160. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1161. {
  1162. /* Check the parameters */
  1163. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1164. /* Enable the TIM Capture/Compare 1 interrupt */
  1165. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1166. /* Enable the TIM Capture/Compare 2 interrupt */
  1167. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1168. /* Enable the complementary One Pulse output */
  1169. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1170. /* Enable the Main Output */
  1171. __HAL_TIM_MOE_ENABLE(htim);
  1172. /* Return function status */
  1173. return HAL_OK;
  1174. }
  1175. /**
  1176. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1177. * complementary channel.
  1178. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1179. * the configuration information for TIM module.
  1180. * @param OutputChannel: TIM Channel to be disabled.
  1181. * This parameter can be one of the following values:
  1182. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1183. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1184. * @retval HAL status
  1185. */
  1186. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1187. {
  1188. /* Check the parameters */
  1189. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1190. /* Disable the TIM Capture/Compare 1 interrupt */
  1191. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1192. /* Disable the TIM Capture/Compare 2 interrupt */
  1193. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1194. /* Disable the complementary One Pulse output */
  1195. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1196. /* Disable the Main Output */
  1197. __HAL_TIM_MOE_DISABLE(htim);
  1198. /* Disable the Peripheral */
  1199. __HAL_TIM_DISABLE(htim);
  1200. /* Return function status */
  1201. return HAL_OK;
  1202. }
  1203. /**
  1204. * @}
  1205. */
  1206. /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
  1207. * @brief Peripheral Control functions
  1208. *
  1209. @verbatim
  1210. ==============================================================================
  1211. ##### Peripheral Control functions #####
  1212. ==============================================================================
  1213. [..]
  1214. This section provides functions allowing to:
  1215. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  1216. (+) Configure External Clock source.
  1217. (+) Configure Complementary channels, break features and dead time.
  1218. (+) Configure Master and the Slave synchronization.
  1219. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1220. (+) Configure the DMA Burst Mode.
  1221. @endverbatim
  1222. * @{
  1223. */
  1224. /**
  1225. * @brief Configure the TIM commutation event sequence.
  1226. * @note This function is mandatory to use the commutation event in order to
  1227. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1228. * the typical use of this feature is with the use of another Timer(interface Timer)
  1229. * configured in Hall sensor interface, this interface Timer will generate the
  1230. * commutation at its TRGO output (connected to Timer used in this function) each time
  1231. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1232. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1233. * the configuration information for TIM module.
  1234. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1235. * This parameter can be one of the following values:
  1236. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1237. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1238. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1239. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1240. * @arg TIM_TS_NONE: No trigger is needed
  1241. * @param CommutationSource: the Commutation Event source.
  1242. * This parameter can be one of the following values:
  1243. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1244. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1245. * @retval HAL status
  1246. */
  1247. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1248. {
  1249. /* Check the parameters */
  1250. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1251. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1252. __HAL_LOCK(htim);
  1253. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1254. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1255. {
  1256. /* Select the Input trigger */
  1257. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1258. htim->Instance->SMCR |= InputTrigger;
  1259. }
  1260. /* Select the Capture Compare preload feature */
  1261. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1262. /* Select the Commutation event source */
  1263. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1264. htim->Instance->CR2 |= CommutationSource;
  1265. __HAL_UNLOCK(htim);
  1266. return HAL_OK;
  1267. }
  1268. /**
  1269. * @brief Configure the TIM commutation event sequence with interrupt.
  1270. * @note This function is mandatory to use the commutation event in order to
  1271. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1272. * the typical use of this feature is with the use of another Timer(interface Timer)
  1273. * configured in Hall sensor interface, this interface Timer will generate the
  1274. * commutation at its TRGO output (connected to Timer used in this function) each time
  1275. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1276. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1277. * the configuration information for TIM module.
  1278. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1279. * This parameter can be one of the following values:
  1280. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1281. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1282. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1283. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1284. * @arg TIM_TS_NONE: No trigger is needed
  1285. * @param CommutationSource: the Commutation Event source.
  1286. * This parameter can be one of the following values:
  1287. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1288. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1289. * @retval HAL status
  1290. */
  1291. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1292. {
  1293. /* Check the parameters */
  1294. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1295. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1296. __HAL_LOCK(htim);
  1297. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1298. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1299. {
  1300. /* Select the Input trigger */
  1301. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1302. htim->Instance->SMCR |= InputTrigger;
  1303. }
  1304. /* Select the Capture Compare preload feature */
  1305. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1306. /* Select the Commutation event source */
  1307. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1308. htim->Instance->CR2 |= CommutationSource;
  1309. /* Enable the Commutation Interrupt Request */
  1310. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1311. __HAL_UNLOCK(htim);
  1312. return HAL_OK;
  1313. }
  1314. /**
  1315. * @brief Configure the TIM commutation event sequence with DMA.
  1316. * @note This function is mandatory to use the commutation event in order to
  1317. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1318. * the typical use of this feature is with the use of another Timer(interface Timer)
  1319. * configured in Hall sensor interface, this interface Timer will generate the
  1320. * commutation at its TRGO output (connected to Timer used in this function) each time
  1321. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1322. * @note: The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1323. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1324. * the configuration information for TIM module.
  1325. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1326. * This parameter can be one of the following values:
  1327. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1328. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1329. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1330. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1331. * @arg TIM_TS_NONE: No trigger is needed
  1332. * @param CommutationSource: the Commutation Event source.
  1333. * This parameter can be one of the following values:
  1334. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1335. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1336. * @retval HAL status
  1337. */
  1338. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1339. {
  1340. /* Check the parameters */
  1341. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1342. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1343. __HAL_LOCK(htim);
  1344. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1345. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1346. {
  1347. /* Select the Input trigger */
  1348. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1349. htim->Instance->SMCR |= InputTrigger;
  1350. }
  1351. /* Select the Capture Compare preload feature */
  1352. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1353. /* Select the Commutation event source */
  1354. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1355. htim->Instance->CR2 |= CommutationSource;
  1356. /* Enable the Commutation DMA Request */
  1357. /* Set the DMA Commutation Callback */
  1358. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = HAL_TIMEx_DMACommutationCplt;
  1359. /* Set the DMA error callback */
  1360. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = HAL_TIM_DMAError;
  1361. /* Enable the Commutation DMA Request */
  1362. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1363. __HAL_UNLOCK(htim);
  1364. return HAL_OK;
  1365. }
  1366. /**
  1367. * @brief Initializes the TIM Output Compare Channels according to the specified
  1368. * parameters in the TIM_OC_InitTypeDef.
  1369. * @param htim: TIM Output Compare handle
  1370. * @param sConfig: TIM Output Compare configuration structure
  1371. * @param Channel : TIM Channels to configure
  1372. * This parameter can be one of the following values:
  1373. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1374. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1375. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1376. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1377. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  1378. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  1379. * @arg TIM_CHANNEL_ALL: all output channels supported by the timer instance selected
  1380. * @retval HAL status
  1381. */
  1382. HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel)
  1383. {
  1384. /* Check the parameters */
  1385. assert_param(IS_TIM_CHANNELS(Channel));
  1386. assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
  1387. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  1388. assert_param(IS_TIM_OCN_POLARITY(sConfig->OCNPolarity));
  1389. assert_param(IS_TIM_OCNIDLE_STATE(sConfig->OCNIdleState));
  1390. assert_param(IS_TIM_OCIDLE_STATE(sConfig->OCIdleState));
  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. * @arg TIM_CHANNEL_ALL: all PWM channels supported by the timer instance selected
  1465. * @retval HAL status
  1466. */
  1467. HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
  1468. TIM_OC_InitTypeDef* sConfig,
  1469. uint32_t Channel)
  1470. {
  1471. /* Check the parameters */
  1472. assert_param(IS_TIM_CHANNELS(Channel));
  1473. assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
  1474. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  1475. assert_param(IS_TIM_OCN_POLARITY(sConfig->OCNPolarity));
  1476. assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
  1477. assert_param(IS_TIM_OCNIDLE_STATE(sConfig->OCNIdleState));
  1478. assert_param(IS_TIM_OCIDLE_STATE(sConfig->OCIdleState));
  1479. /* Check input state */
  1480. __HAL_LOCK(htim);
  1481. htim->State = HAL_TIM_STATE_BUSY;
  1482. switch (Channel)
  1483. {
  1484. case TIM_CHANNEL_1:
  1485. {
  1486. /* Check the parameters */
  1487. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  1488. /* Configure the Channel 1 in PWM mode */
  1489. TIM_OC1_SetConfig(htim->Instance, sConfig);
  1490. /* Set the Preload enable bit for channel1 */
  1491. htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
  1492. /* Configure the Output Fast mode */
  1493. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
  1494. htim->Instance->CCMR1 |= sConfig->OCFastMode;
  1495. }
  1496. break;
  1497. case TIM_CHANNEL_2:
  1498. {
  1499. /* Check the parameters */
  1500. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  1501. /* Configure the Channel 2 in PWM mode */
  1502. TIM_OC2_SetConfig(htim->Instance, sConfig);
  1503. /* Set the Preload enable bit for channel2 */
  1504. htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
  1505. /* Configure the Output Fast mode */
  1506. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
  1507. htim->Instance->CCMR1 |= sConfig->OCFastMode << 8;
  1508. }
  1509. break;
  1510. case TIM_CHANNEL_3:
  1511. {
  1512. /* Check the parameters */
  1513. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  1514. /* Configure the Channel 3 in PWM mode */
  1515. TIM_OC3_SetConfig(htim->Instance, sConfig);
  1516. /* Set the Preload enable bit for channel3 */
  1517. htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
  1518. /* Configure the Output Fast mode */
  1519. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
  1520. htim->Instance->CCMR2 |= sConfig->OCFastMode;
  1521. }
  1522. break;
  1523. case TIM_CHANNEL_4:
  1524. {
  1525. /* Check the parameters */
  1526. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  1527. /* Configure the Channel 4 in PWM mode */
  1528. TIM_OC4_SetConfig(htim->Instance, sConfig);
  1529. /* Set the Preload enable bit for channel4 */
  1530. htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
  1531. /* Configure the Output Fast mode */
  1532. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
  1533. htim->Instance->CCMR2 |= sConfig->OCFastMode << 8;
  1534. }
  1535. break;
  1536. case TIM_CHANNEL_5:
  1537. {
  1538. /* Check the parameters */
  1539. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  1540. /* Configure the Channel 5 in PWM mode */
  1541. TIM_OC5_SetConfig(htim->Instance, sConfig);
  1542. /* Set the Preload enable bit for channel5*/
  1543. htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
  1544. /* Configure the Output Fast mode */
  1545. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;
  1546. htim->Instance->CCMR3 |= sConfig->OCFastMode;
  1547. }
  1548. break;
  1549. case TIM_CHANNEL_6:
  1550. {
  1551. /* Check the parameters */
  1552. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  1553. /* Configure the Channel 5 in PWM mode */
  1554. TIM_OC6_SetConfig(htim->Instance, sConfig);
  1555. /* Set the Preload enable bit for channel6 */
  1556. htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
  1557. /* Configure the Output Fast mode */
  1558. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;
  1559. htim->Instance->CCMR3 |= sConfig->OCFastMode << 8;
  1560. }
  1561. break;
  1562. default:
  1563. break;
  1564. }
  1565. htim->State = HAL_TIM_STATE_READY;
  1566. __HAL_UNLOCK(htim);
  1567. return HAL_OK;
  1568. }
  1569. /**
  1570. * @brief Configures the OCRef clear feature
  1571. * @param htim: TIM handle
  1572. * @param sClearInputConfig: pointer to a TIM_ClearInputConfigTypeDef structure that
  1573. * contains the OCREF clear feature and parameters for the TIM peripheral.
  1574. * @param Channel: specifies the TIM Channel
  1575. * This parameter can be one of the following values:
  1576. * @arg TIM_Channel_1: TIM Channel 1
  1577. * @arg TIM_Channel_2: TIM Channel 2
  1578. * @arg TIM_Channel_3: TIM Channel 3
  1579. * @arg TIM_Channel_4: TIM Channel 4
  1580. * @arg TIM_Channel_5: TIM Channel 5
  1581. * @arg TIM_Channel_6: TIM Channel 6
  1582. * @retval None
  1583. */
  1584. HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
  1585. TIM_ClearInputConfigTypeDef *sClearInputConfig,
  1586. uint32_t Channel)
  1587. {
  1588. uint32_t tmpsmcr = 0;
  1589. /* Check the parameters */
  1590. assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
  1591. assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
  1592. /* Check input state */
  1593. __HAL_LOCK(htim);
  1594. switch (sClearInputConfig->ClearInputSource)
  1595. {
  1596. case TIM_CLEARINPUTSOURCE_NONE:
  1597. {
  1598. /* Clear the OCREF clear selection bit */
  1599. tmpsmcr &= ~TIM_SMCR_OCCS;
  1600. /* Clear the ETR Bits */
  1601. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  1602. /* Set TIMx_SMCR */
  1603. htim->Instance->SMCR = tmpsmcr;
  1604. }
  1605. break;
  1606. case TIM_CLEARINPUTSOURCE_OCREFCLR:
  1607. {
  1608. /* Clear the OCREF clear selection bit */
  1609. htim->Instance->SMCR &= ~TIM_SMCR_OCCS;
  1610. }
  1611. break;
  1612. case TIM_CLEARINPUTSOURCE_ETR:
  1613. {
  1614. /* Check the parameters */
  1615. assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
  1616. assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
  1617. assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
  1618. TIM_ETR_SetConfig(htim->Instance,
  1619. sClearInputConfig->ClearInputPrescaler,
  1620. sClearInputConfig->ClearInputPolarity,
  1621. sClearInputConfig->ClearInputFilter);
  1622. /* Set the OCREF clear selection bit */
  1623. htim->Instance->SMCR |= TIM_SMCR_OCCS;
  1624. }
  1625. break;
  1626. default:
  1627. break;
  1628. }
  1629. switch (Channel)
  1630. {
  1631. case TIM_CHANNEL_1:
  1632. {
  1633. if(sClearInputConfig->ClearInputState != RESET)
  1634. {
  1635. /* Enable the Ocref clear feature for Channel 1 */
  1636. htim->Instance->CCMR1 |= TIM_CCMR1_OC1CE;
  1637. }
  1638. else
  1639. {
  1640. /* Disable the Ocref clear feature for Channel 1 */
  1641. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1CE;
  1642. }
  1643. }
  1644. break;
  1645. case TIM_CHANNEL_2:
  1646. {
  1647. if(sClearInputConfig->ClearInputState != RESET)
  1648. {
  1649. /* Enable the Ocref clear feature for Channel 2 */
  1650. htim->Instance->CCMR1 |= TIM_CCMR1_OC2CE;
  1651. }
  1652. else
  1653. {
  1654. /* Disable the Ocref clear feature for Channel 2 */
  1655. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2CE;
  1656. }
  1657. }
  1658. break;
  1659. case TIM_CHANNEL_3:
  1660. {
  1661. if(sClearInputConfig->ClearInputState != RESET)
  1662. {
  1663. /* Enable the Ocref clear feature for Channel 3 */
  1664. htim->Instance->CCMR2 |= TIM_CCMR2_OC3CE;
  1665. }
  1666. else
  1667. {
  1668. /* Disable the Ocref clear feature for Channel 3 */
  1669. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3CE;
  1670. }
  1671. }
  1672. break;
  1673. case TIM_CHANNEL_4:
  1674. {
  1675. if(sClearInputConfig->ClearInputState != RESET)
  1676. {
  1677. /* Enable the Ocref clear feature for Channel 4 */
  1678. htim->Instance->CCMR2 |= TIM_CCMR2_OC4CE;
  1679. }
  1680. else
  1681. {
  1682. /* Disable the Ocref clear feature for Channel 4 */
  1683. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4CE;
  1684. }
  1685. }
  1686. break;
  1687. case TIM_CHANNEL_5:
  1688. {
  1689. if(sClearInputConfig->ClearInputState != RESET)
  1690. {
  1691. /* Enable the Ocref clear feature for Channel 1 */
  1692. htim->Instance->CCMR3 |= TIM_CCMR3_OC5CE;
  1693. }
  1694. else
  1695. {
  1696. /* Disable the Ocref clear feature for Channel 1 */
  1697. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5CE;
  1698. }
  1699. }
  1700. break;
  1701. case TIM_CHANNEL_6:
  1702. {
  1703. if(sClearInputConfig->ClearInputState != RESET)
  1704. {
  1705. /* Enable the Ocref clear feature for Channel 1 */
  1706. htim->Instance->CCMR3 |= TIM_CCMR3_OC6CE;
  1707. }
  1708. else
  1709. {
  1710. /* Disable the Ocref clear feature for Channel 1 */
  1711. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6CE;
  1712. }
  1713. }
  1714. break;
  1715. default:
  1716. break;
  1717. }
  1718. __HAL_UNLOCK(htim);
  1719. return HAL_OK;
  1720. }
  1721. /**
  1722. * @brief Configures the TIM in master mode.
  1723. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1724. * the configuration information for TIM module.
  1725. * @param sMasterConfig: pointer to a TIM_MasterConfigTypeDef structure that
  1726. * contains the selected trigger output (TRGO) and the Master/Slave
  1727. * mode.
  1728. * @retval HAL status
  1729. */
  1730. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, TIM_MasterConfigTypeDef * sMasterConfig)
  1731. {
  1732. uint32_t tmpcr2;
  1733. uint32_t tmpsmcr;
  1734. /* Check the parameters */
  1735. assert_param(IS_TIM_SYNCHRO_INSTANCE(htim->Instance));
  1736. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1737. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1738. /* Check input state */
  1739. __HAL_LOCK(htim);
  1740. /* Get the TIMx CR2 register value */
  1741. tmpcr2 = htim->Instance->CR2;
  1742. /* Get the TIMx SMCR register value */
  1743. tmpsmcr = htim->Instance->SMCR;
  1744. /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
  1745. if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
  1746. {
  1747. /* Check the parameters */
  1748. assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
  1749. /* Clear the MMS2 bits */
  1750. tmpcr2 &= ~TIM_CR2_MMS2;
  1751. /* Select the TRGO2 source*/
  1752. tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
  1753. }
  1754. /* Reset the MMS Bits */
  1755. tmpcr2 &= ~TIM_CR2_MMS;
  1756. /* Select the TRGO source */
  1757. tmpcr2 |= sMasterConfig->MasterOutputTrigger;
  1758. /* Reset the MSM Bit */
  1759. tmpsmcr &= ~TIM_SMCR_MSM;
  1760. /* Set master mode */
  1761. tmpsmcr |= sMasterConfig->MasterSlaveMode;
  1762. /* Update TIMx CR2 */
  1763. htim->Instance->CR2 = tmpcr2;
  1764. /* Update TIMx SMCR */
  1765. htim->Instance->SMCR = tmpsmcr;
  1766. __HAL_UNLOCK(htim);
  1767. return HAL_OK;
  1768. }
  1769. /**
  1770. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1771. * and the AOE(automatic output enable).
  1772. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1773. * the configuration information for TIM module.
  1774. * @param sBreakDeadTimeConfig: pointer to a TIM_ConfigBreakDeadConfig_TypeDef structure that
  1775. * contains the BDTR Register configuration information for the TIM peripheral.
  1776. * @retval HAL status
  1777. */
  1778. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1779. TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)
  1780. {
  1781. uint32_t tmpbdtr = 0;
  1782. /* Check the parameters */
  1783. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1784. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1785. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1786. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1787. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1788. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1789. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1790. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
  1791. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1792. assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
  1793. assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
  1794. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
  1795. /* Check input state */
  1796. __HAL_LOCK(htim);
  1797. htim->State = HAL_TIM_STATE_BUSY;
  1798. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1799. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1800. /* Clear the BDTR bits */
  1801. tmpbdtr &= ~(TIM_BDTR_DTG | TIM_BDTR_LOCK | TIM_BDTR_OSSI |
  1802. TIM_BDTR_OSSR | TIM_BDTR_BKE | TIM_BDTR_BKP |
  1803. TIM_BDTR_AOE | TIM_BDTR_MOE | TIM_BDTR_BKF |
  1804. TIM_BDTR_BK2F | TIM_BDTR_BK2E | TIM_BDTR_BK2P);
  1805. /* Set the BDTR bits */
  1806. tmpbdtr |= sBreakDeadTimeConfig->DeadTime;
  1807. tmpbdtr |= sBreakDeadTimeConfig->LockLevel;
  1808. tmpbdtr |= sBreakDeadTimeConfig->OffStateIDLEMode;
  1809. tmpbdtr |= sBreakDeadTimeConfig->OffStateRunMode;
  1810. tmpbdtr |= sBreakDeadTimeConfig->BreakState;
  1811. tmpbdtr |= sBreakDeadTimeConfig->BreakPolarity;
  1812. tmpbdtr |= sBreakDeadTimeConfig->AutomaticOutput;
  1813. tmpbdtr |= (sBreakDeadTimeConfig->BreakFilter << BDTR_BKF_SHIFT);
  1814. tmpbdtr |= (sBreakDeadTimeConfig->Break2Filter << BDTR_BK2F_SHIFT);
  1815. tmpbdtr |= sBreakDeadTimeConfig->Break2State;
  1816. tmpbdtr |= sBreakDeadTimeConfig->Break2Polarity;
  1817. /* Set TIMx_BDTR */
  1818. htim->Instance->BDTR = tmpbdtr;
  1819. __HAL_UNLOCK(htim);
  1820. return HAL_OK;
  1821. }
  1822. /**
  1823. * @brief Configures the TIM2, TIM5 and TIM11 Remapping input capabilities.
  1824. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1825. * the configuration information for TIM module.
  1826. * @param Remap: specifies the TIM input remapping source.
  1827. * This parameter can be one of the following values:
  1828. * @arg TIM_TIM2_TIM8_TRGO: TIM2 ITR1 input is connected to TIM8 Trigger output(default)
  1829. * @arg TIM_TIM2_ETH_PTP: TIM2 ITR1 input is connected to ETH PTP trigger output.
  1830. * @arg TIM_TIM2_USBFS_SOF: TIM2 ITR1 input is connected to USB FS SOF.
  1831. * @arg TIM_TIM2_USBHS_SOF: TIM2 ITR1 input is connected to USB HS SOF.
  1832. * @arg TIM_TIM5_GPIO: TIM5 CH4 input is connected to dedicated Timer pin(default)
  1833. * @arg TIM_TIM5_LSI: TIM5 CH4 input is connected to LSI clock.
  1834. * @arg TIM_TIM5_LSE: TIM5 CH4 input is connected to LSE clock.
  1835. * @arg TIM_TIM5_RTC: TIM5 CH4 input is connected to RTC Output event.
  1836. * @arg TIM_TIM11_GPIO: TIM11 CH4 input is connected to dedicated Timer pin(default)
  1837. * @arg TIM_TIM11_SPDIF: SPDIF Frame synchronous
  1838. * @arg TIM_TIM11_HSE: TIM11 CH4 input is connected to HSE_RTC clock
  1839. * (HSE divided by a programmable prescaler)
  1840. * @arg TIM_TIM11_MCO1: TIM11 CH1 input is connected to MCO1
  1841. * @retval HAL status
  1842. */
  1843. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1844. {
  1845. __HAL_LOCK(htim);
  1846. /* Check parameters */
  1847. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  1848. assert_param(IS_TIM_REMAP(Remap));
  1849. /* Set the Timer remapping configuration */
  1850. htim->Instance->OR = Remap;
  1851. htim->State = HAL_TIM_STATE_READY;
  1852. __HAL_UNLOCK(htim);
  1853. return HAL_OK;
  1854. }
  1855. /**
  1856. * @brief Group channel 5 and channel 1, 2 or 3
  1857. * @param htim: TIM handle.
  1858. * @param OCRef: specifies the reference signal(s) the OC5REF is combined with.
  1859. * This parameter can be any combination of the following values:
  1860. * TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
  1861. * TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
  1862. * TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
  1863. * TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
  1864. * @retval HAL status
  1865. */
  1866. HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t OCRef)
  1867. {
  1868. /* Check parameters */
  1869. assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
  1870. assert_param(IS_TIM_GROUPCH5(OCRef));
  1871. /* Process Locked */
  1872. __HAL_LOCK(htim);
  1873. htim->State = HAL_TIM_STATE_BUSY;
  1874. /* Clear GC5Cx bit fields */
  1875. htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3|TIM_CCR5_GC5C2|TIM_CCR5_GC5C1);
  1876. /* Set GC5Cx bit fields */
  1877. htim->Instance->CCR5 |= OCRef;
  1878. htim->State = HAL_TIM_STATE_READY;
  1879. __HAL_UNLOCK(htim);
  1880. return HAL_OK;
  1881. }
  1882. /**
  1883. * @}
  1884. */
  1885. /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
  1886. * @brief Extended Callbacks functions
  1887. *
  1888. @verbatim
  1889. ==============================================================================
  1890. ##### Extension Callbacks functions #####
  1891. ==============================================================================
  1892. [..]
  1893. This section provides Extension TIM callback functions:
  1894. (+) Timer Commutation callback
  1895. (+) Timer Break callback
  1896. @endverbatim
  1897. * @{
  1898. */
  1899. /**
  1900. * @brief Hall commutation changed callback in non blocking mode
  1901. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1902. * the configuration information for TIM module.
  1903. * @retval None
  1904. */
  1905. __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)
  1906. {
  1907. /* NOTE : This function Should not be modified, when the callback is needed,
  1908. the HAL_TIMEx_CommutationCallback could be implemented in the user file
  1909. */
  1910. }
  1911. /**
  1912. * @brief Hall Break detection callback in non blocking mode
  1913. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1914. * the configuration information for TIM module.
  1915. * @retval None
  1916. */
  1917. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  1918. {
  1919. /* NOTE : This function Should not be modified, when the callback is needed,
  1920. the HAL_TIMEx_BreakCallback could be implemented in the user file
  1921. */
  1922. }
  1923. /**
  1924. * @}
  1925. */
  1926. /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
  1927. * @brief Extended Peripheral State functions
  1928. *
  1929. @verbatim
  1930. ==============================================================================
  1931. ##### Extension Peripheral State functions #####
  1932. ==============================================================================
  1933. [..]
  1934. This subsection permits to get in run-time the status of the peripheral
  1935. and the data flow.
  1936. @endverbatim
  1937. * @{
  1938. */
  1939. /**
  1940. * @brief Return the TIM Hall Sensor interface state
  1941. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1942. * the configuration information for TIM module.
  1943. * @retval HAL state
  1944. */
  1945. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
  1946. {
  1947. return htim->State;
  1948. }
  1949. /**
  1950. * @}
  1951. */
  1952. /**
  1953. * @brief TIM DMA Commutation callback.
  1954. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1955. * the configuration information for the specified DMA module.
  1956. * @retval None
  1957. */
  1958. void HAL_TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  1959. {
  1960. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1961. htim->State= HAL_TIM_STATE_READY;
  1962. HAL_TIMEx_CommutationCallback(htim);
  1963. }
  1964. /**
  1965. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1966. * @param TIMx to select the TIM peripheral
  1967. * @param Channel: specifies the TIM Channel
  1968. * This parameter can be one of the following values:
  1969. * @arg TIM_Channel_1: TIM Channel 1
  1970. * @arg TIM_Channel_2: TIM Channel 2
  1971. * @arg TIM_Channel_3: TIM Channel 3
  1972. * @param ChannelNState: specifies the TIM Channel CCxNE bit new state.
  1973. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  1974. * @retval None
  1975. */
  1976. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState)
  1977. {
  1978. uint32_t tmp = 0;
  1979. /* Check the parameters */
  1980. assert_param(IS_TIM_ADVANCED_INSTANCE(TIMx));
  1981. assert_param(IS_TIM_COMPLEMENTARY_CHANNELS(Channel));
  1982. tmp = TIM_CCER_CC1NE << Channel;
  1983. /* Reset the CCxNE Bit */
  1984. TIMx->CCER &= ~tmp;
  1985. /* Set or reset the CCxNE Bit */
  1986. TIMx->CCER |= (uint32_t)(ChannelNState << Channel);
  1987. }
  1988. /**
  1989. * @brief Timer Output Compare 5 configuration
  1990. * @param TIMx to select the TIM peripheral
  1991. * @param OC_Config: The output configuration structure
  1992. * @retval None
  1993. */
  1994. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  1995. {
  1996. uint32_t tmpccmrx = 0;
  1997. uint32_t tmpccer = 0;
  1998. uint32_t tmpcr2 = 0;
  1999. /* Disable the output: Reset the CCxE Bit */
  2000. TIMx->CCER &= ~TIM_CCER_CC5E;
  2001. /* Get the TIMx CCER register value */
  2002. tmpccer = TIMx->CCER;
  2003. /* Get the TIMx CR2 register value */
  2004. tmpcr2 = TIMx->CR2;
  2005. /* Get the TIMx CCMR1 register value */
  2006. tmpccmrx = TIMx->CCMR3;
  2007. /* Reset the Output Compare Mode Bits */
  2008. tmpccmrx &= ~(TIM_CCMR3_OC5M);
  2009. /* Select the Output Compare Mode */
  2010. tmpccmrx |= OC_Config->OCMode;
  2011. /* Reset the Output Polarity level */
  2012. tmpccer &= ~TIM_CCER_CC5P;
  2013. /* Set the Output Compare Polarity */
  2014. tmpccer |= (OC_Config->OCPolarity << 16);
  2015. if(IS_TIM_BREAK_INSTANCE(TIMx))
  2016. {
  2017. /* Reset the Output Compare IDLE State */
  2018. tmpcr2 &= ~TIM_CR2_OIS5;
  2019. /* Set the Output Idle state */
  2020. tmpcr2 |= (OC_Config->OCIdleState << 8);
  2021. }
  2022. /* Write to TIMx CR2 */
  2023. TIMx->CR2 = tmpcr2;
  2024. /* Write to TIMx CCMR3 */
  2025. TIMx->CCMR3 = tmpccmrx;
  2026. /* Set the Capture Compare Register value */
  2027. TIMx->CCR5 = OC_Config->Pulse;
  2028. /* Write to TIMx CCER */
  2029. TIMx->CCER = tmpccer;
  2030. }
  2031. /**
  2032. * @brief Timer Output Compare 6 configuration
  2033. * @param TIMx to select the TIM peripheral
  2034. * @param OC_Config: The output configuration structure
  2035. * @retval None
  2036. */
  2037. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  2038. {
  2039. uint32_t tmpccmrx = 0;
  2040. uint32_t tmpccer = 0;
  2041. uint32_t tmpcr2 = 0;
  2042. /* Disable the output: Reset the CCxE Bit */
  2043. TIMx->CCER &= ~TIM_CCER_CC6E;
  2044. /* Get the TIMx CCER register value */
  2045. tmpccer = TIMx->CCER;
  2046. /* Get the TIMx CR2 register value */
  2047. tmpcr2 = TIMx->CR2;
  2048. /* Get the TIMx CCMR1 register value */
  2049. tmpccmrx = TIMx->CCMR3;
  2050. /* Reset the Output Compare Mode Bits */
  2051. tmpccmrx &= ~(TIM_CCMR3_OC6M);
  2052. /* Select the Output Compare Mode */
  2053. tmpccmrx |= (OC_Config->OCMode << 8);
  2054. /* Reset the Output Polarity level */
  2055. tmpccer &= (uint32_t)~TIM_CCER_CC6P;
  2056. /* Set the Output Compare Polarity */
  2057. tmpccer |= (OC_Config->OCPolarity << 20);
  2058. if(IS_TIM_BREAK_INSTANCE(TIMx))
  2059. {
  2060. /* Reset the Output Compare IDLE State */
  2061. tmpcr2 &= ~TIM_CR2_OIS6;
  2062. /* Set the Output Idle state */
  2063. tmpcr2 |= (OC_Config->OCIdleState << 10);
  2064. }
  2065. /* Write to TIMx CR2 */
  2066. TIMx->CR2 = tmpcr2;
  2067. /* Write to TIMx CCMR3 */
  2068. TIMx->CCMR3 = tmpccmrx;
  2069. /* Set the Capture Compare Register value */
  2070. TIMx->CCR6 = OC_Config->Pulse;
  2071. /* Write to TIMx CCER */
  2072. TIMx->CCER = tmpccer;
  2073. }
  2074. /**
  2075. * @}
  2076. */
  2077. #endif /* HAL_TIM_MODULE_ENABLED */
  2078. /**
  2079. * @}
  2080. */
  2081. /**
  2082. * @}
  2083. */
  2084. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/