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.
 
 
 

2013 lines
64 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_tim_ex.c
  4. * @author MCD Application Team
  5. * @brief TIM HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Timer Extended peripheral:
  8. * + Time Hall Sensor Interface Initialization
  9. * + Time Hall Sensor Interface Start
  10. * + Time Complementary signal bread and dead time configuration
  11. * + Time Master and Slave synchronization configuration
  12. * + Timer remapping capabilities configuration
  13. @verbatim
  14. ==============================================================================
  15. ##### TIMER Extended features #####
  16. ==============================================================================
  17. [..]
  18. The Timer Extended features include:
  19. (#) Complementary outputs with programmable dead-time for :
  20. (++) Output Compare
  21. (++) PWM generation (Edge and Center-aligned Mode)
  22. (++) One-pulse mode output
  23. (#) Synchronization circuit to control the timer with external signals and to
  24. interconnect several timers together.
  25. (#) Break input to put the timer output signals in reset state or in a known state.
  26. (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
  27. positioning purposes
  28. ##### How to use this driver #####
  29. ==============================================================================
  30. [..]
  31. (#) Initialize the TIM low level resources by implementing the following functions
  32. depending from feature used :
  33. (++) Complementary Output Compare : HAL_TIM_OC_MspInit()
  34. (++) Complementary PWM generation : HAL_TIM_PWM_MspInit()
  35. (++) Complementary One-pulse mode output : HAL_TIM_OnePulse_MspInit()
  36. (++) Hall Sensor output : HAL_TIM_HallSensor_MspInit()
  37. (#) Initialize the TIM low level resources :
  38. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  39. (##) TIM pins configuration
  40. (+++) Enable the clock for the TIM GPIOs using the following function:
  41. __HAL_RCC_GPIOx_CLK_ENABLE();
  42. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  43. (#) The external Clock can be configured, if needed (the default clock is the
  44. internal clock from the APBx), using the following function:
  45. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  46. any start function.
  47. (#) Configure the TIM in the desired functioning mode using one of the
  48. initialization function of this driver:
  49. (++) HAL_TIMEx_HallSensor_Init and HAL_TIMEx_ConfigCommutationEvent: to use the
  50. Timer Hall Sensor Interface and the commutation event with the corresponding
  51. Interrupt and DMA request if needed (Note that One Timer is used to interface
  52. with the Hall sensor Interface and another Timer should be used to use
  53. the commutation event).
  54. (#) Activate the TIM peripheral using one of the start functions:
  55. (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(), HAL_TIMEx_OCN_Start_IT()
  56. (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(), HAL_TIMEx_PWMN_Start_IT()
  57. (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
  58. (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(), HAL_TIMEx_HallSensor_Start_IT().
  59. @endverbatim
  60. ******************************************************************************
  61. * @attention
  62. *
  63. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  64. *
  65. * Redistribution and use in source and binary forms, with or without modification,
  66. * are permitted provided that the following conditions are met:
  67. * 1. Redistributions of source code must retain the above copyright notice,
  68. * this list of conditions and the following disclaimer.
  69. * 2. Redistributions in binary form must reproduce the above copyright notice,
  70. * this list of conditions and the following disclaimer in the documentation
  71. * and/or other materials provided with the distribution.
  72. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  73. * may be used to endorse or promote products derived from this software
  74. * without specific prior written permission.
  75. *
  76. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  77. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  78. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  79. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  80. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  81. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  83. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  84. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  85. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  86. *
  87. ******************************************************************************
  88. */
  89. /* Includes ------------------------------------------------------------------*/
  90. #include "stm32f0xx_hal.h"
  91. /** @addtogroup STM32F0xx_HAL_Driver
  92. * @{
  93. */
  94. /** @defgroup TIMEx TIMEx
  95. * @brief TIM Extended HAL module driver
  96. * @{
  97. */
  98. #ifdef HAL_TIM_MODULE_ENABLED
  99. /* Private typedef -----------------------------------------------------------*/
  100. /* Private define ------------------------------------------------------------*/
  101. /* Private macro -------------------------------------------------------------*/
  102. /* Private variables ---------------------------------------------------------*/
  103. /* Private function prototypes -----------------------------------------------*/
  104. /** @defgroup TIMEx_Private_Functions TIMEx Private Functions
  105. * @{
  106. */
  107. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState);
  108. /**
  109. * @}
  110. */
  111. /* Exported functions ---------------------------------------------------------*/
  112. /** @defgroup TIMEx_Exported_Functions TIMEx Exported Functions
  113. * @{
  114. */
  115. /** @defgroup TIMEx_Exported_Functions_Group1 Timer Hall Sensor functions
  116. * @brief Timer Hall Sensor functions
  117. *
  118. @verbatim
  119. ==============================================================================
  120. ##### Timer Hall Sensor functions #####
  121. ==============================================================================
  122. [..]
  123. This section provides functions allowing to:
  124. (+) Initialize and configure TIM HAL Sensor.
  125. (+) De-initialize TIM HAL Sensor.
  126. (+) Start the Hall Sensor Interface.
  127. (+) Stop the Hall Sensor Interface.
  128. (+) Start the Hall Sensor Interface and enable interrupts.
  129. (+) Stop the Hall Sensor Interface and disable interrupts.
  130. (+) Start the Hall Sensor Interface and enable DMA transfers.
  131. (+) Stop the Hall Sensor Interface and disable DMA transfers.
  132. @endverbatim
  133. * @{
  134. */
  135. /**
  136. * @brief Initializes the TIM Hall Sensor Interface and create the associated handle.
  137. * @param htim TIM Encoder Interface handle
  138. * @param sConfig TIM Hall Sensor configuration structure
  139. * @retval HAL status
  140. */
  141. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef* sConfig)
  142. {
  143. TIM_OC_InitTypeDef OC_Config;
  144. /* Check the TIM handle allocation */
  145. if(htim == NULL)
  146. {
  147. return HAL_ERROR;
  148. }
  149. assert_param(IS_TIM_HALL_INTERFACE_INSTANCE(htim->Instance));
  150. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  151. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  152. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  153. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  154. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  155. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  156. if(htim->State == HAL_TIM_STATE_RESET)
  157. {
  158. /* Allocate lock resource and initialize it */
  159. htim->Lock = HAL_UNLOCKED;
  160. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  161. HAL_TIMEx_HallSensor_MspInit(htim);
  162. }
  163. /* Set the TIM state */
  164. htim->State= HAL_TIM_STATE_BUSY;
  165. /* Configure the Time base in the Encoder Mode */
  166. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  167. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  168. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  169. /* Reset the IC1PSC Bits */
  170. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  171. /* Set the IC1PSC value */
  172. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  173. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  174. htim->Instance->CR2 |= TIM_CR2_TI1S;
  175. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  176. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  177. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  178. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  179. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  180. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  181. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  182. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  183. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  184. OC_Config.OCMode = TIM_OCMODE_PWM2;
  185. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  186. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  187. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  188. OC_Config.Pulse = sConfig->Commutation_Delay;
  189. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  190. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  191. register to 101 */
  192. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  193. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  194. /* Initialize the TIM state*/
  195. htim->State= HAL_TIM_STATE_READY;
  196. return HAL_OK;
  197. }
  198. /**
  199. * @brief DeInitializes the TIM Hall Sensor interface
  200. * @param htim TIM Hall Sensor handle
  201. * @retval HAL status
  202. */
  203. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  204. {
  205. /* Check the parameters */
  206. assert_param(IS_TIM_INSTANCE(htim->Instance));
  207. htim->State = HAL_TIM_STATE_BUSY;
  208. /* Disable the TIM Peripheral Clock */
  209. __HAL_TIM_DISABLE(htim);
  210. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  211. HAL_TIMEx_HallSensor_MspDeInit(htim);
  212. /* Change TIM state */
  213. htim->State = HAL_TIM_STATE_RESET;
  214. /* Release Lock */
  215. __HAL_UNLOCK(htim);
  216. return HAL_OK;
  217. }
  218. /**
  219. * @brief Initializes the TIM Hall Sensor MSP.
  220. * @param htim TIM handle
  221. * @retval None
  222. */
  223. __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
  224. {
  225. /* Prevent unused argument(s) compilation warning */
  226. UNUSED(htim);
  227. /* NOTE : This function Should not be modified, when the callback is needed,
  228. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  229. */
  230. }
  231. /**
  232. * @brief DeInitializes TIM Hall Sensor MSP.
  233. * @param htim TIM handle
  234. * @retval None
  235. */
  236. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  237. {
  238. /* Prevent unused argument(s) compilation warning */
  239. UNUSED(htim);
  240. /* NOTE : This function Should not be modified, when the callback is needed,
  241. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  242. */
  243. }
  244. /**
  245. * @brief Starts the TIM Hall Sensor Interface.
  246. * @param htim TIM Hall Sensor handle
  247. * @retval HAL status
  248. */
  249. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  250. {
  251. /* Check the parameters */
  252. assert_param(IS_TIM_HALL_INTERFACE_INSTANCE(htim->Instance));
  253. /* Enable the Input Capture channel 1
  254. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  255. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  256. /* Enable the Peripheral */
  257. __HAL_TIM_ENABLE(htim);
  258. /* Return function status */
  259. return HAL_OK;
  260. }
  261. /**
  262. * @brief Stops the TIM Hall sensor Interface.
  263. * @param htim TIM Hall Sensor handle
  264. * @retval HAL status
  265. */
  266. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  267. {
  268. /* Check the parameters */
  269. assert_param(IS_TIM_HALL_INTERFACE_INSTANCE(htim->Instance));
  270. /* Disable the Input Capture channels 1, 2 and 3
  271. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  272. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  273. /* Disable the Peripheral */
  274. __HAL_TIM_DISABLE(htim);
  275. /* Return function status */
  276. return HAL_OK;
  277. }
  278. /**
  279. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  280. * @param htim TIM Hall Sensor handle
  281. * @retval HAL status
  282. */
  283. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  284. {
  285. /* Check the parameters */
  286. assert_param(IS_TIM_HALL_INTERFACE_INSTANCE(htim->Instance));
  287. /* Enable the capture compare Interrupts 1 event */
  288. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  289. /* Enable the Input Capture channel 1
  290. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  291. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  292. /* Enable the Peripheral */
  293. __HAL_TIM_ENABLE(htim);
  294. /* Return function status */
  295. return HAL_OK;
  296. }
  297. /**
  298. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  299. * @param htim TIM handle
  300. * @retval HAL status
  301. */
  302. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  303. {
  304. /* Check the parameters */
  305. assert_param(IS_TIM_HALL_INTERFACE_INSTANCE(htim->Instance));
  306. /* Disable the Input Capture channel 1
  307. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  308. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  309. /* Disable the capture compare Interrupts event */
  310. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  311. /* Disable the Peripheral */
  312. __HAL_TIM_DISABLE(htim);
  313. /* Return function status */
  314. return HAL_OK;
  315. }
  316. /**
  317. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  318. * @param htim TIM Hall Sensor handle
  319. * @param pData The destination Buffer address.
  320. * @param Length The length of data to be transferred from TIM peripheral to memory.
  321. * @retval HAL status
  322. */
  323. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  324. {
  325. /* Check the parameters */
  326. assert_param(IS_TIM_HALL_INTERFACE_INSTANCE(htim->Instance));
  327. if((htim->State == HAL_TIM_STATE_BUSY))
  328. {
  329. return HAL_BUSY;
  330. }
  331. else if((htim->State == HAL_TIM_STATE_READY))
  332. {
  333. if(((uint32_t)pData == 0U ) && (Length > 0U))
  334. {
  335. return HAL_ERROR;
  336. }
  337. else
  338. {
  339. htim->State = HAL_TIM_STATE_BUSY;
  340. }
  341. }
  342. /* Enable the Input Capture channel 1
  343. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  344. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  345. /* Set the DMA Input Capture 1 Callback */
  346. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  347. /* Set the DMA error callback */
  348. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  349. /* Enable the DMA channel for Capture 1*/
  350. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
  351. /* Enable the capture compare 1 Interrupt */
  352. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  353. /* Enable the Peripheral */
  354. __HAL_TIM_ENABLE(htim);
  355. /* Return function status */
  356. return HAL_OK;
  357. }
  358. /**
  359. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  360. * @param htim TIM handle
  361. * @retval HAL status
  362. */
  363. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  364. {
  365. /* Check the parameters */
  366. assert_param(IS_TIM_HALL_INTERFACE_INSTANCE(htim->Instance));
  367. /* Disable the Input Capture channel 1
  368. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  369. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  370. /* Disable the capture compare Interrupts 1 event */
  371. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  372. /* Disable the Peripheral */
  373. __HAL_TIM_DISABLE(htim);
  374. /* Return function status */
  375. return HAL_OK;
  376. }
  377. /**
  378. * @}
  379. */
  380. /** @defgroup TIMEx_Exported_Functions_Group2 Timer Complementary Output Compare functions
  381. * @brief Timer Complementary Output Compare functions
  382. *
  383. @verbatim
  384. ==============================================================================
  385. ##### Timer Complementary Output Compare functions #####
  386. ==============================================================================
  387. [..]
  388. This section provides functions allowing to:
  389. (+) Start the Complementary Output Compare/PWM.
  390. (+) Stop the Complementary Output Compare/PWM.
  391. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  392. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  393. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  394. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  395. @endverbatim
  396. * @{
  397. */
  398. /**
  399. * @brief Starts the TIM Output Compare signal generation on the complementary
  400. * output.
  401. * @param htim TIM Output Compare handle
  402. * @param Channel TIM Channel to be enabled
  403. * This parameter can be one of the following values:
  404. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  405. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  406. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  407. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  408. * @retval HAL status
  409. */
  410. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  411. {
  412. /* Check the parameters */
  413. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  414. /* Enable the Capture compare channel N */
  415. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  416. /* Enable the Main Ouput */
  417. __HAL_TIM_MOE_ENABLE(htim);
  418. /* Enable the Peripheral */
  419. __HAL_TIM_ENABLE(htim);
  420. /* Return function status */
  421. return HAL_OK;
  422. }
  423. /**
  424. * @brief Stops the TIM Output Compare signal generation on the complementary
  425. * output.
  426. * @param htim TIM handle
  427. * @param Channel TIM Channel to be disabled
  428. * This parameter can be one of the following values:
  429. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  430. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  431. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  432. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  436. {
  437. /* Check the parameters */
  438. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  439. /* Disable the Capture compare channel N */
  440. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  441. /* Disable the Main Ouput */
  442. __HAL_TIM_MOE_DISABLE(htim);
  443. /* Disable the Peripheral */
  444. __HAL_TIM_DISABLE(htim);
  445. /* Return function status */
  446. return HAL_OK;
  447. }
  448. /**
  449. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  450. * on the complementary output.
  451. * @param htim TIM OC handle
  452. * @param Channel TIM Channel to be enabled
  453. * This parameter can be one of the following values:
  454. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  455. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  456. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  457. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  458. * @retval HAL status
  459. */
  460. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  461. {
  462. /* Check the parameters */
  463. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  464. switch (Channel)
  465. {
  466. case TIM_CHANNEL_1:
  467. {
  468. /* Enable the TIM Output Compare interrupt */
  469. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  470. }
  471. break;
  472. case TIM_CHANNEL_2:
  473. {
  474. /* Enable the TIM Output Compare interrupt */
  475. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  476. }
  477. break;
  478. case TIM_CHANNEL_3:
  479. {
  480. /* Enable the TIM Output Compare interrupt */
  481. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  482. }
  483. break;
  484. case TIM_CHANNEL_4:
  485. {
  486. /* Enable the TIM Output Compare interrupt */
  487. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  488. }
  489. break;
  490. default:
  491. break;
  492. }
  493. /* Enable the TIM Break interrupt */
  494. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  495. /* Enable the Capture compare channel N */
  496. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  497. /* Enable the Main Ouput */
  498. __HAL_TIM_MOE_ENABLE(htim);
  499. /* Enable the Peripheral */
  500. __HAL_TIM_ENABLE(htim);
  501. /* Return function status */
  502. return HAL_OK;
  503. }
  504. /**
  505. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  506. * on the complementary output.
  507. * @param htim TIM Output Compare handle
  508. * @param Channel TIM Channel to be disabled
  509. * This parameter can be one of the following values:
  510. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  511. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  512. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  513. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  514. * @retval HAL status
  515. */
  516. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  517. {
  518. uint32_t tmpccer = 0U;
  519. /* Check the parameters */
  520. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  521. switch (Channel)
  522. {
  523. case TIM_CHANNEL_1:
  524. {
  525. /* Disable the TIM Output Compare interrupt */
  526. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  527. }
  528. break;
  529. case TIM_CHANNEL_2:
  530. {
  531. /* Disable the TIM Output Compare interrupt */
  532. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  533. }
  534. break;
  535. case TIM_CHANNEL_3:
  536. {
  537. /* Disable the TIM Output Compare interrupt */
  538. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  539. }
  540. break;
  541. case TIM_CHANNEL_4:
  542. {
  543. /* Disable the TIM Output Compare interrupt */
  544. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  545. }
  546. break;
  547. default:
  548. break;
  549. }
  550. /* Disable the Capture compare channel N */
  551. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  552. /* Disable the TIM Break interrupt (only if no more channel is active) */
  553. tmpccer = htim->Instance->CCER;
  554. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  555. {
  556. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  557. }
  558. /* Disable the Main Ouput */
  559. __HAL_TIM_MOE_DISABLE(htim);
  560. /* Disable the Peripheral */
  561. __HAL_TIM_DISABLE(htim);
  562. /* Return function status */
  563. return HAL_OK;
  564. }
  565. /**
  566. * @brief Starts the TIM Output Compare signal generation in DMA mode
  567. * on the complementary output.
  568. * @param htim TIM Output Compare handle
  569. * @param Channel TIM Channel to be enabled
  570. * This parameter can be one of the following values:
  571. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  572. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  573. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  574. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  575. * @param pData The source Buffer address.
  576. * @param Length The length of data to be transferred from memory to TIM peripheral
  577. * @retval HAL status
  578. */
  579. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  580. {
  581. /* Check the parameters */
  582. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  583. if((htim->State == HAL_TIM_STATE_BUSY))
  584. {
  585. return HAL_BUSY;
  586. }
  587. else if((htim->State == HAL_TIM_STATE_READY))
  588. {
  589. if(((uint32_t)pData == 0U ) && (Length > 0U))
  590. {
  591. return HAL_ERROR;
  592. }
  593. else
  594. {
  595. htim->State = HAL_TIM_STATE_BUSY;
  596. }
  597. }
  598. switch (Channel)
  599. {
  600. case TIM_CHANNEL_1:
  601. {
  602. /* Set the DMA Period elapsed callback */
  603. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  604. /* Set the DMA error callback */
  605. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  606. /* Enable the DMA channel */
  607. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  608. /* Enable the TIM Output Compare DMA request */
  609. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  610. }
  611. break;
  612. case TIM_CHANNEL_2:
  613. {
  614. /* Set the DMA Period elapsed callback */
  615. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  616. /* Set the DMA error callback */
  617. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  618. /* Enable the DMA channel */
  619. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  620. /* Enable the TIM Output Compare DMA request */
  621. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  622. }
  623. break;
  624. case TIM_CHANNEL_3:
  625. {
  626. /* Set the DMA Period elapsed callback */
  627. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  628. /* Set the DMA error callback */
  629. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  630. /* Enable the DMA channel */
  631. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  632. /* Enable the TIM Output Compare DMA request */
  633. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  634. }
  635. break;
  636. case TIM_CHANNEL_4:
  637. {
  638. /* Set the DMA Period elapsed callback */
  639. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  640. /* Set the DMA error callback */
  641. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  642. /* Enable the DMA channel */
  643. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  644. /* Enable the TIM Output Compare DMA request */
  645. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  646. }
  647. break;
  648. default:
  649. break;
  650. }
  651. /* Enable the Capture compare channel N */
  652. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  653. /* Enable the Main Ouput */
  654. __HAL_TIM_MOE_ENABLE(htim);
  655. /* Enable the Peripheral */
  656. __HAL_TIM_ENABLE(htim);
  657. /* Return function status */
  658. return HAL_OK;
  659. }
  660. /**
  661. * @brief Stops the TIM Output Compare signal generation in DMA mode
  662. * on the complementary output.
  663. * @param htim TIM Output Compare handle
  664. * @param Channel TIM Channel to be disabled
  665. * This parameter can be one of the following values:
  666. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  667. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  668. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  669. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  670. * @retval HAL status
  671. */
  672. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  673. {
  674. /* Check the parameters */
  675. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  676. switch (Channel)
  677. {
  678. case TIM_CHANNEL_1:
  679. {
  680. /* Disable the TIM Output Compare DMA request */
  681. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  682. }
  683. break;
  684. case TIM_CHANNEL_2:
  685. {
  686. /* Disable the TIM Output Compare DMA request */
  687. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  688. }
  689. break;
  690. case TIM_CHANNEL_3:
  691. {
  692. /* Disable the TIM Output Compare DMA request */
  693. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  694. }
  695. break;
  696. case TIM_CHANNEL_4:
  697. {
  698. /* Disable the TIM Output Compare interrupt */
  699. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  700. }
  701. break;
  702. default:
  703. break;
  704. }
  705. /* Disable the Capture compare channel N */
  706. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  707. /* Disable the Main Ouput */
  708. __HAL_TIM_MOE_DISABLE(htim);
  709. /* Disable the Peripheral */
  710. __HAL_TIM_DISABLE(htim);
  711. /* Change the htim state */
  712. htim->State = HAL_TIM_STATE_READY;
  713. /* Return function status */
  714. return HAL_OK;
  715. }
  716. /**
  717. * @}
  718. */
  719. /** @defgroup TIMEx_Exported_Functions_Group3 Timer Complementary PWM functions
  720. * @brief Timer Complementary PWM functions
  721. *
  722. @verbatim
  723. ==============================================================================
  724. ##### Timer Complementary PWM functions #####
  725. ==============================================================================
  726. [..]
  727. This section provides functions allowing to:
  728. (+) Start the Complementary PWM.
  729. (+) Stop the Complementary PWM.
  730. (+) Start the Complementary PWM and enable interrupts.
  731. (+) Stop the Complementary PWM and disable interrupts.
  732. (+) Start the Complementary PWM and enable DMA transfers.
  733. (+) Stop the Complementary PWM and disable DMA transfers.
  734. (+) Start the Complementary Input Capture measurement.
  735. (+) Stop the Complementary Input Capture.
  736. (+) Start the Complementary Input Capture and enable interrupts.
  737. (+) Stop the Complementary Input Capture and disable interrupts.
  738. (+) Start the Complementary Input Capture and enable DMA transfers.
  739. (+) Stop the Complementary Input Capture and disable DMA transfers.
  740. (+) Start the Complementary One Pulse generation.
  741. (+) Stop the Complementary One Pulse.
  742. (+) Start the Complementary One Pulse and enable interrupts.
  743. (+) Stop the Complementary One Pulse and disable interrupts.
  744. @endverbatim
  745. * @{
  746. */
  747. /**
  748. * @brief Starts the PWM signal generation on the complementary output.
  749. * @param htim TIM handle
  750. * @param Channel TIM Channel to be enabled
  751. * This parameter can be one of the following values:
  752. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  753. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  754. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  755. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  756. * @retval HAL status
  757. */
  758. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  759. {
  760. /* Check the parameters */
  761. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  762. /* Enable the complementary PWM output */
  763. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  764. /* Enable the Main Ouput */
  765. __HAL_TIM_MOE_ENABLE(htim);
  766. /* Enable the Peripheral */
  767. __HAL_TIM_ENABLE(htim);
  768. /* Return function status */
  769. return HAL_OK;
  770. }
  771. /**
  772. * @brief Stops the PWM signal generation on the complementary output.
  773. * @param htim TIM handle
  774. * @param Channel TIM Channel to be disabled
  775. * This parameter can be one of the following values:
  776. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  777. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  778. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  779. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  780. * @retval HAL status
  781. */
  782. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  783. {
  784. /* Check the parameters */
  785. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  786. /* Disable the complementary PWM output */
  787. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  788. /* Disable the Main Ouput */
  789. __HAL_TIM_MOE_DISABLE(htim);
  790. /* Disable the Peripheral */
  791. __HAL_TIM_DISABLE(htim);
  792. /* Return function status */
  793. return HAL_OK;
  794. }
  795. /**
  796. * @brief Starts the PWM signal generation in interrupt mode on the
  797. * complementary output.
  798. * @param htim TIM handle
  799. * @param Channel TIM Channel to be disabled
  800. * This parameter can be one of the following values:
  801. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  802. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  803. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  804. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  805. * @retval HAL status
  806. */
  807. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  808. {
  809. /* Check the parameters */
  810. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  811. switch (Channel)
  812. {
  813. case TIM_CHANNEL_1:
  814. {
  815. /* Enable the TIM Capture/Compare 1 interrupt */
  816. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  817. }
  818. break;
  819. case TIM_CHANNEL_2:
  820. {
  821. /* Enable the TIM Capture/Compare 2 interrupt */
  822. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  823. }
  824. break;
  825. case TIM_CHANNEL_3:
  826. {
  827. /* Enable the TIM Capture/Compare 3 interrupt */
  828. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  829. }
  830. break;
  831. case TIM_CHANNEL_4:
  832. {
  833. /* Enable the TIM Capture/Compare 4 interrupt */
  834. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  835. }
  836. break;
  837. default:
  838. break;
  839. }
  840. /* Enable the TIM Break interrupt */
  841. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  842. /* Enable the complementary PWM output */
  843. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  844. /* Enable the Main Ouput */
  845. __HAL_TIM_MOE_ENABLE(htim);
  846. /* Enable the Peripheral */
  847. __HAL_TIM_ENABLE(htim);
  848. /* Return function status */
  849. return HAL_OK;
  850. }
  851. /**
  852. * @brief Stops the PWM signal generation in interrupt mode on the
  853. * complementary output.
  854. * @param htim TIM handle
  855. * @param Channel TIM Channel to be disabled
  856. * This parameter can be one of the following values:
  857. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  858. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  859. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  860. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  861. * @retval HAL status
  862. */
  863. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
  864. {
  865. uint32_t tmpccer = 0U;
  866. /* Check the parameters */
  867. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  868. switch (Channel)
  869. {
  870. case TIM_CHANNEL_1:
  871. {
  872. /* Disable the TIM Capture/Compare 1 interrupt */
  873. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  874. }
  875. break;
  876. case TIM_CHANNEL_2:
  877. {
  878. /* Disable the TIM Capture/Compare 2 interrupt */
  879. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  880. }
  881. break;
  882. case TIM_CHANNEL_3:
  883. {
  884. /* Disable the TIM Capture/Compare 3 interrupt */
  885. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  886. }
  887. break;
  888. case TIM_CHANNEL_4:
  889. {
  890. /* Disable the TIM Capture/Compare 3 interrupt */
  891. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  892. }
  893. break;
  894. default:
  895. break;
  896. }
  897. /* Disable the complementary PWM output */
  898. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  899. /* Disable the TIM Break interrupt (only if no more channel is active) */
  900. tmpccer = htim->Instance->CCER;
  901. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  902. {
  903. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  904. }
  905. /* Disable the Main Ouput */
  906. __HAL_TIM_MOE_DISABLE(htim);
  907. /* Disable the Peripheral */
  908. __HAL_TIM_DISABLE(htim);
  909. /* Return function status */
  910. return HAL_OK;
  911. }
  912. /**
  913. * @brief Starts the TIM PWM signal generation in DMA mode on the
  914. * complementary output
  915. * @param htim TIM handle
  916. * @param Channel TIM Channel to be enabled
  917. * This parameter can be one of the following values:
  918. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  919. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  920. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  921. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  922. * @param pData The source Buffer address.
  923. * @param Length The length of data to be transferred from memory to TIM peripheral
  924. * @retval HAL status
  925. */
  926. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  927. {
  928. /* Check the parameters */
  929. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  930. if((htim->State == HAL_TIM_STATE_BUSY))
  931. {
  932. return HAL_BUSY;
  933. }
  934. else if((htim->State == HAL_TIM_STATE_READY))
  935. {
  936. if(((uint32_t)pData == 0U ) && (Length > 0U))
  937. {
  938. return HAL_ERROR;
  939. }
  940. else
  941. {
  942. htim->State = HAL_TIM_STATE_BUSY;
  943. }
  944. }
  945. switch (Channel)
  946. {
  947. case TIM_CHANNEL_1:
  948. {
  949. /* Set the DMA Period elapsed callback */
  950. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  951. /* Set the DMA error callback */
  952. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  953. /* Enable the DMA channel */
  954. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  955. /* Enable the TIM Capture/Compare 1 DMA request */
  956. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  957. }
  958. break;
  959. case TIM_CHANNEL_2:
  960. {
  961. /* Set the DMA Period elapsed callback */
  962. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  963. /* Set the DMA error callback */
  964. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  965. /* Enable the DMA channel */
  966. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  967. /* Enable the TIM Capture/Compare 2 DMA request */
  968. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  969. }
  970. break;
  971. case TIM_CHANNEL_3:
  972. {
  973. /* Set the DMA Period elapsed callback */
  974. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  975. /* Set the DMA error callback */
  976. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  977. /* Enable the DMA channel */
  978. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  979. /* Enable the TIM Capture/Compare 3 DMA request */
  980. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  981. }
  982. break;
  983. case TIM_CHANNEL_4:
  984. {
  985. /* Set the DMA Period elapsed callback */
  986. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  987. /* Set the DMA error callback */
  988. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  989. /* Enable the DMA channel */
  990. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  991. /* Enable the TIM Capture/Compare 4 DMA request */
  992. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  993. }
  994. break;
  995. default:
  996. break;
  997. }
  998. /* Enable the complementary PWM output */
  999. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1000. /* Enable the Main Ouput */
  1001. __HAL_TIM_MOE_ENABLE(htim);
  1002. /* Enable the Peripheral */
  1003. __HAL_TIM_ENABLE(htim);
  1004. /* Return function status */
  1005. return HAL_OK;
  1006. }
  1007. /**
  1008. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  1009. * output
  1010. * @param htim TIM handle
  1011. * @param Channel TIM Channel to be disabled
  1012. * This parameter can be one of the following values:
  1013. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1014. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1015. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1016. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1017. * @retval HAL status
  1018. */
  1019. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1020. {
  1021. /* Check the parameters */
  1022. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1023. switch (Channel)
  1024. {
  1025. case TIM_CHANNEL_1:
  1026. {
  1027. /* Disable the TIM Capture/Compare 1 DMA request */
  1028. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1029. }
  1030. break;
  1031. case TIM_CHANNEL_2:
  1032. {
  1033. /* Disable the TIM Capture/Compare 2 DMA request */
  1034. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1035. }
  1036. break;
  1037. case TIM_CHANNEL_3:
  1038. {
  1039. /* Disable the TIM Capture/Compare 3 DMA request */
  1040. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1041. }
  1042. break;
  1043. case TIM_CHANNEL_4:
  1044. {
  1045. /* Disable the TIM Capture/Compare 4 DMA request */
  1046. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1047. }
  1048. break;
  1049. default:
  1050. break;
  1051. }
  1052. /* Disable the complementary PWM output */
  1053. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1054. /* Disable the Main Ouput */
  1055. __HAL_TIM_MOE_DISABLE(htim);
  1056. /* Disable the Peripheral */
  1057. __HAL_TIM_DISABLE(htim);
  1058. /* Change the htim state */
  1059. htim->State = HAL_TIM_STATE_READY;
  1060. /* Return function status */
  1061. return HAL_OK;
  1062. }
  1063. /**
  1064. * @}
  1065. */
  1066. /** @defgroup TIMEx_Exported_Functions_Group4 Timer Complementary One Pulse functions
  1067. * @brief Timer Complementary One Pulse functions
  1068. *
  1069. @verbatim
  1070. ==============================================================================
  1071. ##### Timer Complementary One Pulse functions #####
  1072. ==============================================================================
  1073. [..]
  1074. This section provides functions allowing to:
  1075. (+) Start the Complementary One Pulse generation.
  1076. (+) Stop the Complementary One Pulse.
  1077. (+) Start the Complementary One Pulse and enable interrupts.
  1078. (+) Stop the Complementary One Pulse and disable interrupts.
  1079. @endverbatim
  1080. * @{
  1081. */
  1082. /**
  1083. * @brief Starts the TIM One Pulse signal generation on the complemetary
  1084. * output.
  1085. * @param htim TIM One Pulse handle
  1086. * @param OutputChannel TIM Channel to be enabled
  1087. * This parameter can be one of the following values:
  1088. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1089. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1090. * @retval HAL status
  1091. */
  1092. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1093. {
  1094. /* Check the parameters */
  1095. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1096. /* Enable the complementary One Pulse output */
  1097. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1098. /* Enable the Main Ouput */
  1099. __HAL_TIM_MOE_ENABLE(htim);
  1100. /* Return function status */
  1101. return HAL_OK;
  1102. }
  1103. /**
  1104. * @brief Stops the TIM One Pulse signal generation on the complementary
  1105. * output.
  1106. * @param htim TIM One Pulse handle
  1107. * @param OutputChannel TIM Channel to be disabled
  1108. * This parameter can be one of the following values:
  1109. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1110. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1111. * @retval HAL status
  1112. */
  1113. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1114. {
  1115. /* Check the parameters */
  1116. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1117. /* Disable the complementary One Pulse output */
  1118. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1119. /* Disable the Main Ouput */
  1120. __HAL_TIM_MOE_DISABLE(htim);
  1121. /* Disable the Peripheral */
  1122. __HAL_TIM_DISABLE(htim);
  1123. /* Return function status */
  1124. return HAL_OK;
  1125. }
  1126. /**
  1127. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1128. * complementary channel.
  1129. * @param htim TIM One Pulse handle
  1130. * @param OutputChannel TIM Channel to be enabled
  1131. * This parameter can be one of the following values:
  1132. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1133. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1134. * @retval HAL status
  1135. */
  1136. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1137. {
  1138. /* Check the parameters */
  1139. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1140. /* Enable the TIM Capture/Compare 1 interrupt */
  1141. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1142. /* Enable the TIM Capture/Compare 2 interrupt */
  1143. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1144. /* Enable the complementary One Pulse output */
  1145. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1146. /* Enable the Main Ouput */
  1147. __HAL_TIM_MOE_ENABLE(htim);
  1148. /* Return function status */
  1149. return HAL_OK;
  1150. }
  1151. /**
  1152. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1153. * complementary channel.
  1154. * @param htim TIM One Pulse handle
  1155. * @param OutputChannel TIM Channel to be disabled
  1156. * This parameter can be one of the following values:
  1157. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1158. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1159. * @retval HAL status
  1160. */
  1161. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1162. {
  1163. /* Check the parameters */
  1164. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1165. /* Disable the TIM Capture/Compare 1 interrupt */
  1166. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1167. /* Disable the TIM Capture/Compare 2 interrupt */
  1168. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1169. /* Disable the complementary One Pulse output */
  1170. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1171. /* Disable the Main Ouput */
  1172. __HAL_TIM_MOE_DISABLE(htim);
  1173. /* Disable the Peripheral */
  1174. __HAL_TIM_DISABLE(htim);
  1175. /* Return function status */
  1176. return HAL_OK;
  1177. }
  1178. /**
  1179. * @}
  1180. */
  1181. /** @defgroup TIMEx_Exported_Functions_Group5 Peripheral Control functions
  1182. * @brief Peripheral Control functions
  1183. *
  1184. @verbatim
  1185. ==============================================================================
  1186. ##### Peripheral Control functions #####
  1187. ==============================================================================
  1188. [..]
  1189. This section provides functions allowing to:
  1190. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1191. (+) Configure Complementary channels, break features and dead time.
  1192. (+) Configure Master synchronization.
  1193. (+) Configure timer remapping capabilities.
  1194. @endverbatim
  1195. * @{
  1196. */
  1197. /**
  1198. * @brief Configure the TIM commutation event sequence.
  1199. * @note: this function is mandatory to use the commutation event in order to
  1200. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1201. * the typical use of this feature is with the use of another Timer(interface Timer)
  1202. * configured in Hall sensor interface, this interface Timer will generate the
  1203. * commutation at its TRGO output (connected to Timer used in this function) each time
  1204. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1205. * @param htim TIM handle
  1206. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1207. * This parameter can be one of the following values:
  1208. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1209. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1210. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1211. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1212. * @arg TIM_TS_NONE: No trigger is needed
  1213. * @param CommutationSource the Commutation Event source
  1214. * This parameter can be one of the following values:
  1215. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1216. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1217. * @retval HAL status
  1218. */
  1219. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1220. {
  1221. /* Check the parameters */
  1222. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1223. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1224. __HAL_LOCK(htim);
  1225. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1226. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1227. {
  1228. /* Select the Input trigger */
  1229. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1230. htim->Instance->SMCR |= InputTrigger;
  1231. }
  1232. /* Select the Capture Compare preload feature */
  1233. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1234. /* Select the Commutation event source */
  1235. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1236. htim->Instance->CR2 |= CommutationSource;
  1237. __HAL_UNLOCK(htim);
  1238. return HAL_OK;
  1239. }
  1240. /**
  1241. * @brief Configure the TIM commutation event sequence with interrupt.
  1242. * @note: this function is mandatory to use the commutation event in order to
  1243. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1244. * the typical use of this feature is with the use of another Timer(interface Timer)
  1245. * configured in Hall sensor interface, this interface Timer will generate the
  1246. * commutation at its TRGO output (connected to Timer used in this function) each time
  1247. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1248. * @param htim TIM handle
  1249. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1250. * This parameter can be one of the following values:
  1251. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1252. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1253. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1254. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1255. * @arg TIM_TS_NONE: No trigger is needed
  1256. * @param CommutationSource the Commutation Event source
  1257. * This parameter can be one of the following values:
  1258. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1259. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1260. * @retval HAL status
  1261. */
  1262. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1263. {
  1264. /* Check the parameters */
  1265. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1266. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1267. __HAL_LOCK(htim);
  1268. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1269. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1270. {
  1271. /* Select the Input trigger */
  1272. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1273. htim->Instance->SMCR |= InputTrigger;
  1274. }
  1275. /* Select the Capture Compare preload feature */
  1276. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1277. /* Select the Commutation event source */
  1278. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1279. htim->Instance->CR2 |= CommutationSource;
  1280. /* Enable the Commutation Interrupt Request */
  1281. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1282. __HAL_UNLOCK(htim);
  1283. return HAL_OK;
  1284. }
  1285. /**
  1286. * @brief Configure the TIM commutation event sequence with DMA.
  1287. * @note: this function is mandatory to use the commutation event in order to
  1288. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1289. * the typical use of this feature is with the use of another Timer(interface Timer)
  1290. * configured in Hall sensor interface, this interface Timer will generate the
  1291. * commutation at its TRGO output (connected to Timer used in this function) each time
  1292. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1293. * @note: The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1294. * @param htim TIM handle
  1295. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1296. * This parameter can be one of the following values:
  1297. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1298. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1299. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1300. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1301. * @arg TIM_TS_NONE: No trigger is needed
  1302. * @param CommutationSource the Commutation Event source
  1303. * This parameter can be one of the following values:
  1304. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1305. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1306. * @retval HAL status
  1307. */
  1308. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1309. {
  1310. /* Check the parameters */
  1311. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1312. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1313. __HAL_LOCK(htim);
  1314. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1315. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1316. {
  1317. /* Select the Input trigger */
  1318. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1319. htim->Instance->SMCR |= InputTrigger;
  1320. }
  1321. /* Select the Capture Compare preload feature */
  1322. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1323. /* Select the Commutation event source */
  1324. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1325. htim->Instance->CR2 |= CommutationSource;
  1326. /* Enable the Commutation DMA Request */
  1327. /* Set the DMA Commutation Callback */
  1328. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1329. /* Set the DMA error callback */
  1330. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1331. /* Enable the Commutation DMA Request */
  1332. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1333. __HAL_UNLOCK(htim);
  1334. return HAL_OK;
  1335. }
  1336. /**
  1337. * @brief Configures the TIM in master mode.
  1338. * @param htim TIM handle.
  1339. * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
  1340. * contains the selected trigger output (TRGO) and the Master/Slave
  1341. * mode.
  1342. * @retval HAL status
  1343. */
  1344. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, TIM_MasterConfigTypeDef * sMasterConfig)
  1345. {
  1346. /* Check the parameters */
  1347. assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
  1348. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1349. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1350. __HAL_LOCK(htim);
  1351. htim->State = HAL_TIM_STATE_BUSY;
  1352. /* Reset the MMS Bits */
  1353. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  1354. /* Select the TRGO source */
  1355. htim->Instance->CR2 |= sMasterConfig->MasterOutputTrigger;
  1356. /* Reset the MSM Bit */
  1357. htim->Instance->SMCR &= ~TIM_SMCR_MSM;
  1358. /* Set or Reset the MSM Bit */
  1359. htim->Instance->SMCR |= sMasterConfig->MasterSlaveMode;
  1360. htim->State = HAL_TIM_STATE_READY;
  1361. __HAL_UNLOCK(htim);
  1362. return HAL_OK;
  1363. }
  1364. /**
  1365. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1366. * and the AOE(automatic output enable).
  1367. * @param htim TIM handle
  1368. * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
  1369. * contains the BDTR Register configuration information for the TIM peripheral.
  1370. * @retval HAL status
  1371. */
  1372. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1373. TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
  1374. {
  1375. uint32_t tmpbdtr = 0;
  1376. /* Check the parameters */
  1377. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1378. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1379. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1380. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1381. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1382. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1383. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1384. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1385. /* Process Locked */
  1386. __HAL_LOCK(htim);
  1387. htim->State = HAL_TIM_STATE_BUSY;
  1388. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1389. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1390. /* Set the BDTR bits */
  1391. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  1392. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  1393. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  1394. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  1395. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  1396. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  1397. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  1398. MODIFY_REG(tmpbdtr, TIM_BDTR_MOE, sBreakDeadTimeConfig->AutomaticOutput);
  1399. /* Set TIMx_BDTR */
  1400. htim->Instance->BDTR = tmpbdtr;
  1401. htim->State = HAL_TIM_STATE_READY;
  1402. __HAL_UNLOCK(htim);
  1403. return HAL_OK;
  1404. }
  1405. /**
  1406. * @brief Configures the TIM14 Remapping input capabilities.
  1407. * @param htim TIM handle.
  1408. * @param Remap specifies the TIM remapping source.
  1409. * This parameter can be one of the following values:
  1410. * @arg TIM_TIM14_GPIO: TIM14 TI1 is connected to GPIO
  1411. * @arg TIM_TIM14_RTC: TIM14 TI1 is connected to RTC_clock
  1412. * @arg TIM_TIM14_HSE: TIM14 TI1 is connected to HSE/32
  1413. * @arg TIM_TIM14_MCO: TIM14 TI1 is connected to MCO
  1414. * @retval HAL status
  1415. */
  1416. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1417. {
  1418. __HAL_LOCK(htim);
  1419. /* Check parameters */
  1420. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  1421. assert_param(IS_TIM_REMAP(Remap));
  1422. /* Set the Timer remapping configuration */
  1423. htim->Instance->OR = Remap;
  1424. htim->State = HAL_TIM_STATE_READY;
  1425. __HAL_UNLOCK(htim);
  1426. return HAL_OK;
  1427. }
  1428. /**
  1429. * @}
  1430. */
  1431. /** @addtogroup TIM_Exported_Functions_Group8
  1432. * @{
  1433. */
  1434. #if defined(STM32F051x8) || defined(STM32F058xx) || \
  1435. defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || \
  1436. defined(STM32F091xC) || defined (STM32F098xx)
  1437. /**
  1438. * @brief Configures the OCRef clear feature
  1439. * @param htim TIM handle
  1440. * @param sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
  1441. * contains the OCREF clear feature and parameters for the TIM peripheral.
  1442. * @param Channel specifies the TIM Channel
  1443. * This parameter can be one of the following values:
  1444. * @arg TIM_CHANNEL_1: TIM Channel 1
  1445. * @arg TIM_CHANNEL_2: TIM Channel 2
  1446. * @arg TIM_CHANNEL_3: TIM Channel 3
  1447. * @arg TIM_CHANNEL_4: TIM Channel 4
  1448. * @arg TIM_Channel_5: TIM Channel 5
  1449. * @retval None
  1450. */
  1451. HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
  1452. TIM_ClearInputConfigTypeDef *sClearInputConfig,
  1453. uint32_t Channel)
  1454. {
  1455. uint32_t tmpsmcr = 0U;
  1456. /* Check the parameters */
  1457. assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
  1458. assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
  1459. /* Check input state */
  1460. __HAL_LOCK(htim);
  1461. htim->State = HAL_TIM_STATE_BUSY;
  1462. switch (sClearInputConfig->ClearInputSource)
  1463. {
  1464. case TIM_CLEARINPUTSOURCE_NONE:
  1465. {
  1466. /* Get the TIMx SMCR register value */
  1467. tmpsmcr = htim->Instance->SMCR;
  1468. /* Clear the OCREF clear selection bit */
  1469. tmpsmcr &= ~TIM_SMCR_OCCS;
  1470. /* Clear the ETR Bits */
  1471. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  1472. /* Set TIMx_SMCR */
  1473. htim->Instance->SMCR = tmpsmcr;
  1474. }
  1475. break;
  1476. case TIM_CLEARINPUTSOURCE_OCREFCLR:
  1477. {
  1478. /* Clear the OCREF clear selection bit */
  1479. htim->Instance->SMCR &= ~TIM_SMCR_OCCS;
  1480. }
  1481. break;
  1482. case TIM_CLEARINPUTSOURCE_ETR:
  1483. {
  1484. /* Check the parameters */
  1485. assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
  1486. assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
  1487. assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
  1488. TIM_ETR_SetConfig(htim->Instance,
  1489. sClearInputConfig->ClearInputPrescaler,
  1490. sClearInputConfig->ClearInputPolarity,
  1491. sClearInputConfig->ClearInputFilter);
  1492. /* Set the OCREF clear selection bit */
  1493. htim->Instance->SMCR |= TIM_SMCR_OCCS;
  1494. }
  1495. break;
  1496. default:
  1497. break;
  1498. }
  1499. switch (Channel)
  1500. {
  1501. case TIM_CHANNEL_1:
  1502. {
  1503. if(sClearInputConfig->ClearInputState != RESET)
  1504. {
  1505. /* Enable the Ocref clear feature for Channel 1 */
  1506. htim->Instance->CCMR1 |= TIM_CCMR1_OC1CE;
  1507. }
  1508. else
  1509. {
  1510. /* Disable the Ocref clear feature for Channel 1 */
  1511. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1CE;
  1512. }
  1513. }
  1514. break;
  1515. case TIM_CHANNEL_2:
  1516. {
  1517. if(sClearInputConfig->ClearInputState != RESET)
  1518. {
  1519. /* Enable the Ocref clear feature for Channel 2 */
  1520. htim->Instance->CCMR1 |= TIM_CCMR1_OC2CE;
  1521. }
  1522. else
  1523. {
  1524. /* Disable the Ocref clear feature for Channel 2 */
  1525. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2CE;
  1526. }
  1527. }
  1528. break;
  1529. case TIM_CHANNEL_3:
  1530. {
  1531. if(sClearInputConfig->ClearInputState != RESET)
  1532. {
  1533. /* Enable the Ocref clear feature for Channel 3 */
  1534. htim->Instance->CCMR2 |= TIM_CCMR2_OC3CE;
  1535. }
  1536. else
  1537. {
  1538. /* Disable the Ocref clear feature for Channel 3 */
  1539. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3CE;
  1540. }
  1541. }
  1542. break;
  1543. case TIM_CHANNEL_4:
  1544. {
  1545. if(sClearInputConfig->ClearInputState != RESET)
  1546. {
  1547. /* Enable the Ocref clear feature for Channel 4 */
  1548. htim->Instance->CCMR2 |= TIM_CCMR2_OC4CE;
  1549. }
  1550. else
  1551. {
  1552. /* Disable the Ocref clear feature for Channel 4 */
  1553. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4CE;
  1554. }
  1555. }
  1556. break;
  1557. default:
  1558. break;
  1559. }
  1560. htim->State = HAL_TIM_STATE_READY;
  1561. __HAL_UNLOCK(htim);
  1562. return HAL_OK;
  1563. }
  1564. #endif /* STM32F051x8 || STM32F058xx || */
  1565. /* STM32F071xB || STM32F072xB || STM32F078xx || */
  1566. /* STM32F091xC || STM32F098xx */
  1567. /**
  1568. * @}
  1569. */
  1570. /** @defgroup TIMEx_Exported_Functions_Group6 Extension Callbacks functions
  1571. * @brief Extension Callbacks functions
  1572. *
  1573. @verbatim
  1574. ==============================================================================
  1575. ##### Extension Callbacks functions #####
  1576. ==============================================================================
  1577. [..]
  1578. This section provides Extension TIM callback functions:
  1579. (+) Timer Commutation callback
  1580. (+) Timer Break callback
  1581. @endverbatim
  1582. * @{
  1583. */
  1584. /**
  1585. * @brief Hall commutation changed callback in non blocking mode
  1586. * @param htim TIM handle
  1587. * @retval None
  1588. */
  1589. __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)
  1590. {
  1591. /* Prevent unused argument(s) compilation warning */
  1592. UNUSED(htim);
  1593. /* NOTE : This function Should not be modified, when the callback is needed,
  1594. the HAL_TIMEx_CommutationCallback could be implemented in the user file
  1595. */
  1596. }
  1597. /**
  1598. * @brief Hall Break detection callback in non blocking mode
  1599. * @param htim TIM handle
  1600. * @retval None
  1601. */
  1602. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  1603. {
  1604. /* Prevent unused argument(s) compilation warning */
  1605. UNUSED(htim);
  1606. /* NOTE : This function Should not be modified, when the callback is needed,
  1607. the HAL_TIMEx_BreakCallback could be implemented in the user file
  1608. */
  1609. }
  1610. /**
  1611. * @brief TIM DMA Commutation callback.
  1612. * @param hdma pointer to DMA handle.
  1613. * @retval None
  1614. */
  1615. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  1616. {
  1617. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1618. htim->State= HAL_TIM_STATE_READY;
  1619. HAL_TIMEx_CommutationCallback(htim);
  1620. }
  1621. /**
  1622. * @}
  1623. */
  1624. /** @defgroup TIMEx_Exported_Functions_Group7 Extension Peripheral State functions
  1625. * @brief Extension Peripheral State functions
  1626. *
  1627. @verbatim
  1628. ==============================================================================
  1629. ##### Extension Peripheral State functions #####
  1630. ==============================================================================
  1631. [..]
  1632. This subsection permit to get in run-time the status of the peripheral
  1633. and the data flow.
  1634. @endverbatim
  1635. * @{
  1636. */
  1637. /**
  1638. * @brief Return the TIM Hall Sensor interface state
  1639. * @param htim TIM Hall Sensor handle
  1640. * @retval HAL state
  1641. */
  1642. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
  1643. {
  1644. return htim->State;
  1645. }
  1646. /**
  1647. * @}
  1648. */
  1649. /**
  1650. * @}
  1651. */
  1652. /** @addtogroup TIMEx_Private_Functions
  1653. * @{
  1654. */
  1655. /**
  1656. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1657. * @param TIMx to select the TIM peripheral
  1658. * @param Channel specifies the TIM Channel
  1659. * This parameter can be one of the following values:
  1660. * @arg TIM_CHANNEL_1: TIM Channel 1
  1661. * @arg TIM_CHANNEL_2: TIM Channel 2
  1662. * @arg TIM_CHANNEL_3: TIM Channel 3
  1663. * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
  1664. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  1665. * @retval None
  1666. */
  1667. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState)
  1668. {
  1669. uint32_t tmp = 0U;
  1670. tmp = TIM_CCER_CC1NE << Channel;
  1671. /* Reset the CCxNE Bit */
  1672. TIMx->CCER &= ~tmp;
  1673. /* Set or reset the CCxNE Bit */
  1674. TIMx->CCER |= (uint32_t)(ChannelNState << Channel);
  1675. }
  1676. /**
  1677. * @}
  1678. */
  1679. #endif /* HAL_TIM_MODULE_ENABLED */
  1680. /**
  1681. * @}
  1682. */
  1683. /**
  1684. * @}
  1685. */
  1686. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/