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.
 
 
 

2566 lines
84 KiB

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