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.
 
 
 

2712 lines
88 KiB

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