stm32f4xx_hal_tim_ex.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 extension 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. @verbatim
  13. ==============================================================================
  14. ##### TIMER Extended features #####
  15. ==============================================================================
  16. [..]
  17. The Timer Extension features include:
  18. (#) Complementary outputs with programmable dead-time for :
  19. (++) Input Capture
  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 __TIMx_CLK_ENABLE();
  39. (##) TIM pins configuration
  40. (+++) Enable the clock for the TIM GPIOs using the following function:
  41. __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_OC_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) 2017 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 "stm32f4xx_hal.h"
  91. /** @addtogroup STM32F4xx_HAL_Driver
  92. * @{
  93. */
  94. /** @defgroup TIMEx TIMEx
  95. * @brief TIM HAL module driver
  96. * @{
  97. */
  98. #ifdef HAL_TIM_MODULE_ENABLED
  99. /* Private typedef -----------------------------------------------------------*/
  100. /* Private define ------------------------------------------------------------*/
  101. /* Private macro -------------------------------------------------------------*/
  102. /* Private variables ---------------------------------------------------------*/
  103. /** @addtogroup TIMEx_Private_Functions
  104. * @{
  105. */
  106. /* Private function prototypes -----------------------------------------------*/
  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 TIM 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 pointer to a TIM_HandleTypeDef structure that contains
  138. * the configuration information for TIM module.
  139. * @param sConfig TIM Hall Sensor configuration structure
  140. * @retval HAL status
  141. */
  142. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef* sConfig)
  143. {
  144. TIM_OC_InitTypeDef OC_Config;
  145. /* Check the TIM handle allocation */
  146. if(htim == NULL)
  147. {
  148. return HAL_ERROR;
  149. }
  150. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  151. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  152. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  153. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  154. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  155. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  156. /* Set the TIM state */
  157. htim->State= HAL_TIM_STATE_BUSY;
  158. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  159. HAL_TIMEx_HallSensor_MspInit(htim);
  160. /* Configure the Time base in the Encoder Mode */
  161. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  162. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  163. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  164. /* Reset the IC1PSC Bits */
  165. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  166. /* Set the IC1PSC value */
  167. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  168. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  169. htim->Instance->CR2 |= TIM_CR2_TI1S;
  170. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  171. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  172. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  173. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  174. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  175. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  176. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  177. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  178. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  179. OC_Config.OCMode = TIM_OCMODE_PWM2;
  180. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  181. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  182. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  183. OC_Config.Pulse = sConfig->Commutation_Delay;
  184. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  185. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  186. register to 101 */
  187. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  188. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  189. /* Initialize the TIM state*/
  190. htim->State= HAL_TIM_STATE_READY;
  191. return HAL_OK;
  192. }
  193. /**
  194. * @brief DeInitializes the TIM Hall Sensor interface
  195. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  196. * the configuration information for TIM module.
  197. * @retval HAL status
  198. */
  199. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  200. {
  201. /* Check the parameters */
  202. assert_param(IS_TIM_INSTANCE(htim->Instance));
  203. htim->State = HAL_TIM_STATE_BUSY;
  204. /* Disable the TIM Peripheral Clock */
  205. __HAL_TIM_DISABLE(htim);
  206. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  207. HAL_TIMEx_HallSensor_MspDeInit(htim);
  208. /* Change TIM state */
  209. htim->State = HAL_TIM_STATE_RESET;
  210. /* Release Lock */
  211. __HAL_UNLOCK(htim);
  212. return HAL_OK;
  213. }
  214. /**
  215. * @brief Initializes the TIM Hall Sensor MSP.
  216. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  217. * the configuration information for TIM module.
  218. * @retval None
  219. */
  220. __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
  221. {
  222. /* Prevent unused argument(s) compilation warning */
  223. UNUSED(htim);
  224. /* NOTE : This function Should not be modified, when the callback is needed,
  225. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  226. */
  227. }
  228. /**
  229. * @brief DeInitializes TIM Hall Sensor MSP.
  230. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  231. * the configuration information for TIM module.
  232. * @retval None
  233. */
  234. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  235. {
  236. /* Prevent unused argument(s) compilation warning */
  237. UNUSED(htim);
  238. /* NOTE : This function Should not be modified, when the callback is needed,
  239. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  240. */
  241. }
  242. /**
  243. * @brief Starts the TIM Hall Sensor Interface.
  244. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  245. * the configuration information for TIM module.
  246. * @retval HAL status
  247. */
  248. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  249. {
  250. /* Check the parameters */
  251. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  252. /* Enable the Input Capture channels 1
  253. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  254. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  255. /* Enable the Peripheral */
  256. __HAL_TIM_ENABLE(htim);
  257. /* Return function status */
  258. return HAL_OK;
  259. }
  260. /**
  261. * @brief Stops the TIM Hall sensor Interface.
  262. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  263. * the configuration information for TIM module.
  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_XOR_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 pointer to a TIM_HandleTypeDef structure that contains
  281. * the configuration information for TIM module.
  282. * @retval HAL status
  283. */
  284. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  285. {
  286. /* Check the parameters */
  287. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  288. /* Enable the capture compare Interrupts 1 event */
  289. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  290. /* Enable the Input Capture channels 1
  291. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  292. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  293. /* Enable the Peripheral */
  294. __HAL_TIM_ENABLE(htim);
  295. /* Return function status */
  296. return HAL_OK;
  297. }
  298. /**
  299. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  300. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  301. * the configuration information for TIM module.
  302. * @retval HAL status
  303. */
  304. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  305. {
  306. /* Check the parameters */
  307. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  308. /* Disable the Input Capture channels 1
  309. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  310. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  311. /* Disable the capture compare Interrupts event */
  312. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  313. /* Disable the Peripheral */
  314. __HAL_TIM_DISABLE(htim);
  315. /* Return function status */
  316. return HAL_OK;
  317. }
  318. /**
  319. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  320. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  321. * the configuration information for TIM module.
  322. * @param pData The destination Buffer address.
  323. * @param Length The length of data to be transferred from TIM peripheral to memory.
  324. * @retval HAL status
  325. */
  326. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  327. {
  328. /* Check the parameters */
  329. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  330. if((htim->State == HAL_TIM_STATE_BUSY))
  331. {
  332. return HAL_BUSY;
  333. }
  334. else if((htim->State == HAL_TIM_STATE_READY))
  335. {
  336. if(((uint32_t)pData == 0U) && (Length > 0))
  337. {
  338. return HAL_ERROR;
  339. }
  340. else
  341. {
  342. htim->State = HAL_TIM_STATE_BUSY;
  343. }
  344. }
  345. /* Enable the Input Capture channels 1
  346. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  347. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  348. /* Set the DMA Input Capture 1 Callback */
  349. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  350. /* Set the DMA error callback */
  351. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  352. /* Enable the DMA Stream for Capture 1*/
  353. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
  354. /* Enable the capture compare 1 Interrupt */
  355. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  356. /* Enable the Peripheral */
  357. __HAL_TIM_ENABLE(htim);
  358. /* Return function status */
  359. return HAL_OK;
  360. }
  361. /**
  362. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  363. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  364. * the configuration information for TIM module.
  365. * @retval HAL status
  366. */
  367. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  368. {
  369. /* Check the parameters */
  370. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  371. /* Disable the Input Capture channels 1
  372. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  373. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  374. /* Disable the capture compare Interrupts 1 event */
  375. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  376. /* Disable the Peripheral */
  377. __HAL_TIM_DISABLE(htim);
  378. /* Return function status */
  379. return HAL_OK;
  380. }
  381. /**
  382. * @}
  383. */
  384. /** @defgroup TIMEx_Exported_Functions_Group2 Timer Complementary Output Compare functions
  385. * @brief Timer Complementary Output Compare functions
  386. *
  387. @verbatim
  388. ==============================================================================
  389. ##### Timer Complementary Output Compare functions #####
  390. ==============================================================================
  391. [..]
  392. This section provides functions allowing to:
  393. (+) Start the Complementary Output Compare/PWM.
  394. (+) Stop the Complementary Output Compare/PWM.
  395. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  396. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  397. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  398. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  399. @endverbatim
  400. * @{
  401. */
  402. /**
  403. * @brief Starts the TIM Output Compare signal generation on the complementary
  404. * output.
  405. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  406. * the configuration information for TIM module.
  407. * @param Channel TIM Channel to be enabled.
  408. * This parameter can be one of the following values:
  409. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  410. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  411. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  412. * @retval HAL status
  413. */
  414. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  415. {
  416. /* Check the parameters */
  417. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  418. /* Enable the Capture compare channel N */
  419. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  420. /* Enable the Main Output */
  421. __HAL_TIM_MOE_ENABLE(htim);
  422. /* Enable the Peripheral */
  423. __HAL_TIM_ENABLE(htim);
  424. /* Return function status */
  425. return HAL_OK;
  426. }
  427. /**
  428. * @brief Stops the TIM Output Compare signal generation on the complementary
  429. * output.
  430. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  431. * the configuration information for TIM module.
  432. * @param Channel TIM Channel to be disabled.
  433. * This parameter can be one of the following values:
  434. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  435. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  436. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  437. * @retval HAL status
  438. */
  439. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  440. {
  441. /* Check the parameters */
  442. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  443. /* Disable the Capture compare channel N */
  444. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  445. /* Disable the Main Output */
  446. __HAL_TIM_MOE_DISABLE(htim);
  447. /* Disable the Peripheral */
  448. __HAL_TIM_DISABLE(htim);
  449. /* Return function status */
  450. return HAL_OK;
  451. }
  452. /**
  453. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  454. * on the complementary output.
  455. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  456. * the configuration information for TIM module.
  457. * @param Channel TIM Channel to be enabled.
  458. * This parameter can be one of the following values:
  459. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  460. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  461. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  462. * @retval HAL status
  463. */
  464. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  465. {
  466. /* Check the parameters */
  467. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  468. switch (Channel)
  469. {
  470. case TIM_CHANNEL_1:
  471. {
  472. /* Enable the TIM Output Compare interrupt */
  473. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  474. }
  475. break;
  476. case TIM_CHANNEL_2:
  477. {
  478. /* Enable the TIM Output Compare interrupt */
  479. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  480. }
  481. break;
  482. case TIM_CHANNEL_3:
  483. {
  484. /* Enable the TIM Output Compare interrupt */
  485. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  486. }
  487. break;
  488. case TIM_CHANNEL_4:
  489. {
  490. /* Enable the TIM Output Compare interrupt */
  491. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  492. }
  493. break;
  494. default:
  495. break;
  496. }
  497. /* Enable the TIM Break interrupt */
  498. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  499. /* Enable the Capture compare channel N */
  500. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  501. /* Enable the Main Output */
  502. __HAL_TIM_MOE_ENABLE(htim);
  503. /* Enable the Peripheral */
  504. __HAL_TIM_ENABLE(htim);
  505. /* Return function status */
  506. return HAL_OK;
  507. }
  508. /**
  509. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  510. * on the complementary output.
  511. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  512. * the configuration information for TIM module.
  513. * @param Channel TIM Channel to be disabled.
  514. * This parameter can be one of the following values:
  515. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  516. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  517. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  518. * @retval HAL status
  519. */
  520. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  521. {
  522. /* Check the parameters */
  523. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  524. switch (Channel)
  525. {
  526. case TIM_CHANNEL_1:
  527. {
  528. /* Disable the TIM Output Compare interrupt */
  529. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  530. }
  531. break;
  532. case TIM_CHANNEL_2:
  533. {
  534. /* Disable the TIM Output Compare interrupt */
  535. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  536. }
  537. break;
  538. case TIM_CHANNEL_3:
  539. {
  540. /* Disable the TIM Output Compare interrupt */
  541. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  542. }
  543. break;
  544. case TIM_CHANNEL_4:
  545. {
  546. /* Disable the TIM Output Compare interrupt */
  547. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  548. }
  549. break;
  550. default:
  551. break;
  552. }
  553. /* Disable the Capture compare channel N */
  554. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  555. /* Disable the TIM Break interrupt (only if no more channel is active) */
  556. if((READ_REG(htim->Instance->CCER) & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  557. {
  558. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  559. }
  560. /* Disable the Main Output */
  561. __HAL_TIM_MOE_DISABLE(htim);
  562. /* Disable the Peripheral */
  563. __HAL_TIM_DISABLE(htim);
  564. /* Return function status */
  565. return HAL_OK;
  566. }
  567. /**
  568. * @brief Starts the TIM Output Compare signal generation in DMA mode
  569. * on the complementary output.
  570. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  571. * the configuration information for TIM module.
  572. * @param Channel TIM Channel to be enabled.
  573. * This parameter can be one of the following values:
  574. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  575. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  576. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  577. * @param pData The source Buffer address.
  578. * @param Length The length of data to be transferred from memory to TIM peripheral
  579. * @retval HAL status
  580. */
  581. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  582. {
  583. /* Check the parameters */
  584. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  585. if((htim->State == HAL_TIM_STATE_BUSY))
  586. {
  587. return HAL_BUSY;
  588. }
  589. else if((htim->State == HAL_TIM_STATE_READY))
  590. {
  591. if(((uint32_t)pData == 0U) && (Length > 0))
  592. {
  593. return HAL_ERROR;
  594. }
  595. else
  596. {
  597. htim->State = HAL_TIM_STATE_BUSY;
  598. }
  599. }
  600. switch (Channel)
  601. {
  602. case TIM_CHANNEL_1:
  603. {
  604. /* Set the DMA Period elapsed callback */
  605. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  606. /* Set the DMA error callback */
  607. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  608. /* Enable the DMA Stream */
  609. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  610. /* Enable the TIM Output Compare DMA request */
  611. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  612. }
  613. break;
  614. case TIM_CHANNEL_2:
  615. {
  616. /* Set the DMA Period elapsed callback */
  617. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  618. /* Set the DMA error callback */
  619. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  620. /* Enable the DMA Stream */
  621. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  622. /* Enable the TIM Output Compare DMA request */
  623. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  624. }
  625. break;
  626. case TIM_CHANNEL_3:
  627. {
  628. /* Set the DMA Period elapsed callback */
  629. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  630. /* Set the DMA error callback */
  631. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  632. /* Enable the DMA Stream */
  633. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  634. /* Enable the TIM Output Compare DMA request */
  635. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  636. }
  637. break;
  638. case TIM_CHANNEL_4:
  639. {
  640. /* Set the DMA Period elapsed callback */
  641. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  642. /* Set the DMA error callback */
  643. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  644. /* Enable the DMA Stream */
  645. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  646. /* Enable the TIM Output Compare DMA request */
  647. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  648. }
  649. break;
  650. default:
  651. break;
  652. }
  653. /* Enable the Capture compare channel N */
  654. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  655. /* Enable the Main Output */
  656. __HAL_TIM_MOE_ENABLE(htim);
  657. /* Enable the Peripheral */
  658. __HAL_TIM_ENABLE(htim);
  659. /* Return function status */
  660. return HAL_OK;
  661. }
  662. /**
  663. * @brief Stops the TIM Output Compare signal generation in DMA mode
  664. * on the complementary output.
  665. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  666. * the configuration information for TIM module.
  667. * @param Channel TIM Channel to be disabled.
  668. * This parameter can be one of the following values:
  669. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  670. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  671. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  672. * @retval HAL status
  673. */
  674. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  675. {
  676. /* Check the parameters */
  677. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  678. switch (Channel)
  679. {
  680. case TIM_CHANNEL_1:
  681. {
  682. /* Disable the TIM Output Compare DMA request */
  683. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  684. }
  685. break;
  686. case TIM_CHANNEL_2:
  687. {
  688. /* Disable the TIM Output Compare DMA request */
  689. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  690. }
  691. break;
  692. case TIM_CHANNEL_3:
  693. {
  694. /* Disable the TIM Output Compare DMA request */
  695. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  696. }
  697. break;
  698. case TIM_CHANNEL_4:
  699. {
  700. /* Disable the TIM Output Compare interrupt */
  701. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  702. }
  703. break;
  704. default:
  705. break;
  706. }
  707. /* Disable the Capture compare channel N */
  708. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  709. /* Disable the Main Output */
  710. __HAL_TIM_MOE_DISABLE(htim);
  711. /* Disable the Peripheral */
  712. __HAL_TIM_DISABLE(htim);
  713. /* Change the htim state */
  714. htim->State = HAL_TIM_STATE_READY;
  715. /* Return function status */
  716. return HAL_OK;
  717. }
  718. /**
  719. * @}
  720. */
  721. /** @defgroup TIMEx_Exported_Functions_Group3 Timer Complementary PWM functions
  722. * @brief Timer Complementary PWM functions
  723. *
  724. @verbatim
  725. ==============================================================================
  726. ##### Timer Complementary PWM functions #####
  727. ==============================================================================
  728. [..]
  729. This section provides functions allowing to:
  730. (+) Start the Complementary PWM.
  731. (+) Stop the Complementary PWM.
  732. (+) Start the Complementary PWM and enable interrupts.
  733. (+) Stop the Complementary PWM and disable interrupts.
  734. (+) Start the Complementary PWM and enable DMA transfers.
  735. (+) Stop the Complementary PWM and disable DMA transfers.
  736. (+) Start the Complementary Input Capture measurement.
  737. (+) Stop the Complementary Input Capture.
  738. (+) Start the Complementary Input Capture and enable interrupts.
  739. (+) Stop the Complementary Input Capture and disable interrupts.
  740. (+) Start the Complementary Input Capture and enable DMA transfers.
  741. (+) Stop the Complementary Input Capture and disable DMA transfers.
  742. (+) Start the Complementary One Pulse generation.
  743. (+) Stop the Complementary One Pulse.
  744. (+) Start the Complementary One Pulse and enable interrupts.
  745. (+) Stop the Complementary One Pulse and disable interrupts.
  746. @endverbatim
  747. * @{
  748. */
  749. /**
  750. * @brief Starts the PWM signal generation on the complementary output.
  751. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  752. * the configuration information for TIM module.
  753. * @param Channel TIM Channel to be enabled.
  754. * This parameter can be one of the following values:
  755. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  756. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  757. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  758. * @retval HAL status
  759. */
  760. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  761. {
  762. /* Check the parameters */
  763. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  764. /* Enable the complementary PWM output */
  765. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  766. /* Enable the Main Output */
  767. __HAL_TIM_MOE_ENABLE(htim);
  768. /* Enable the Peripheral */
  769. __HAL_TIM_ENABLE(htim);
  770. /* Return function status */
  771. return HAL_OK;
  772. }
  773. /**
  774. * @brief Stops the PWM signal generation on the complementary output.
  775. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  776. * the configuration information for TIM module.
  777. * @param Channel TIM Channel to be disabled.
  778. * This parameter can be one of the following values:
  779. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  780. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  781. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  785. {
  786. /* Check the parameters */
  787. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  788. /* Disable the complementary PWM output */
  789. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  790. /* Disable the Main Output */
  791. __HAL_TIM_MOE_DISABLE(htim);
  792. /* Disable the Peripheral */
  793. __HAL_TIM_DISABLE(htim);
  794. /* Return function status */
  795. return HAL_OK;
  796. }
  797. /**
  798. * @brief Starts the PWM signal generation in interrupt mode on the
  799. * complementary output.
  800. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  801. * the configuration information for TIM module.
  802. * @param Channel TIM Channel to be disabled.
  803. * This parameter can be one of the following values:
  804. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  805. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  806. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  807. * @retval HAL status
  808. */
  809. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  810. {
  811. /* Check the parameters */
  812. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  813. switch (Channel)
  814. {
  815. case TIM_CHANNEL_1:
  816. {
  817. /* Enable the TIM Capture/Compare 1 interrupt */
  818. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  819. }
  820. break;
  821. case TIM_CHANNEL_2:
  822. {
  823. /* Enable the TIM Capture/Compare 2 interrupt */
  824. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  825. }
  826. break;
  827. case TIM_CHANNEL_3:
  828. {
  829. /* Enable the TIM Capture/Compare 3 interrupt */
  830. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  831. }
  832. break;
  833. case TIM_CHANNEL_4:
  834. {
  835. /* Enable the TIM Capture/Compare 4 interrupt */
  836. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  837. }
  838. break;
  839. default:
  840. break;
  841. }
  842. /* Enable the TIM Break interrupt */
  843. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  844. /* Enable the complementary PWM output */
  845. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  846. /* Enable the Main Output */
  847. __HAL_TIM_MOE_ENABLE(htim);
  848. /* Enable the Peripheral */
  849. __HAL_TIM_ENABLE(htim);
  850. /* Return function status */
  851. return HAL_OK;
  852. }
  853. /**
  854. * @brief Stops the PWM signal generation in interrupt mode on the
  855. * complementary output.
  856. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  857. * the configuration information for TIM module.
  858. * @param Channel TIM Channel to be disabled.
  859. * This parameter can be one of the following values:
  860. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  861. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  862. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  863. * @retval HAL status
  864. */
  865. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
  866. {
  867. /* Check the parameters */
  868. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  869. switch (Channel)
  870. {
  871. case TIM_CHANNEL_1:
  872. {
  873. /* Disable the TIM Capture/Compare 1 interrupt */
  874. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  875. }
  876. break;
  877. case TIM_CHANNEL_2:
  878. {
  879. /* Disable the TIM Capture/Compare 2 interrupt */
  880. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  881. }
  882. break;
  883. case TIM_CHANNEL_3:
  884. {
  885. /* Disable the TIM Capture/Compare 3 interrupt */
  886. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  887. }
  888. break;
  889. case TIM_CHANNEL_4:
  890. {
  891. /* Disable the TIM Capture/Compare 3 interrupt */
  892. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  893. }
  894. break;
  895. default:
  896. break;
  897. }
  898. /* Disable the complementary PWM output */
  899. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  900. /* Disable the TIM Break interrupt (only if no more channel is active) */
  901. if((READ_REG(htim->Instance->CCER) & (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 Output */
  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 pointer to a TIM_HandleTypeDef structure that contains
  916. * the configuration information for TIM module.
  917. * @param Channel TIM Channel to be enabled.
  918. * This parameter can be one of the following values:
  919. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  920. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  921. * @arg TIM_CHANNEL_3: TIM Channel 3 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 > 0))
  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 Stream */
  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 Stream */
  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 Stream */
  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 Stream */
  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 Output */
  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 pointer to a TIM_HandleTypeDef structure that contains
  1011. * the configuration information for TIM module.
  1012. * @param Channel TIM Channel to be disabled.
  1013. * This parameter can be one of the following values:
  1014. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1015. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1016. * @arg TIM_CHANNEL_3: TIM Channel 3 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 Output */
  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 complementary
  1084. * output.
  1085. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1086. * the configuration information for TIM module.
  1087. * @param OutputChannel TIM Channel to be enabled.
  1088. * This parameter can be one of the following values:
  1089. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1090. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1091. * @retval HAL status
  1092. */
  1093. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1094. {
  1095. /* Check the parameters */
  1096. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1097. /* Enable the complementary One Pulse output */
  1098. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1099. /* Enable the Main Output */
  1100. __HAL_TIM_MOE_ENABLE(htim);
  1101. /* Return function status */
  1102. return HAL_OK;
  1103. }
  1104. /**
  1105. * @brief Stops the TIM One Pulse signal generation on the complementary
  1106. * output.
  1107. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1108. * the configuration information for TIM module.
  1109. * @param OutputChannel TIM Channel to be disabled.
  1110. * This parameter can be one of the following values:
  1111. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1112. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1113. * @retval HAL status
  1114. */
  1115. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1116. {
  1117. /* Check the parameters */
  1118. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1119. /* Disable the complementary One Pulse output */
  1120. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1121. /* Disable the Main Output */
  1122. __HAL_TIM_MOE_DISABLE(htim);
  1123. /* Disable the Peripheral */
  1124. __HAL_TIM_DISABLE(htim);
  1125. /* Return function status */
  1126. return HAL_OK;
  1127. }
  1128. /**
  1129. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1130. * complementary channel.
  1131. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1132. * the configuration information for TIM module.
  1133. * @param OutputChannel TIM Channel to be enabled.
  1134. * This parameter can be one of the following values:
  1135. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1136. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1137. * @retval HAL status
  1138. */
  1139. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1140. {
  1141. /* Check the parameters */
  1142. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1143. /* Enable the TIM Capture/Compare 1 interrupt */
  1144. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1145. /* Enable the TIM Capture/Compare 2 interrupt */
  1146. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1147. /* Enable the complementary One Pulse output */
  1148. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1149. /* Enable the Main Output */
  1150. __HAL_TIM_MOE_ENABLE(htim);
  1151. /* Return function status */
  1152. return HAL_OK;
  1153. }
  1154. /**
  1155. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1156. * complementary channel.
  1157. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1158. * the configuration information for TIM module.
  1159. * @param OutputChannel TIM Channel to be disabled.
  1160. * This parameter can be one of the following values:
  1161. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1162. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1163. * @retval HAL status
  1164. */
  1165. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1166. {
  1167. /* Check the parameters */
  1168. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1169. /* Disable the TIM Capture/Compare 1 interrupt */
  1170. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1171. /* Disable the TIM Capture/Compare 2 interrupt */
  1172. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1173. /* Disable the complementary One Pulse output */
  1174. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1175. /* Disable the Main Output */
  1176. __HAL_TIM_MOE_DISABLE(htim);
  1177. /* Disable the Peripheral */
  1178. __HAL_TIM_DISABLE(htim);
  1179. /* Return function status */
  1180. return HAL_OK;
  1181. }
  1182. /**
  1183. * @}
  1184. */
  1185. /** @defgroup TIMEx_Exported_Functions_Group5 Peripheral Control functions
  1186. * @brief Peripheral Control functions
  1187. *
  1188. @verbatim
  1189. ==============================================================================
  1190. ##### Peripheral Control functions #####
  1191. ==============================================================================
  1192. [..]
  1193. This section provides functions allowing to:
  1194. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  1195. (+) Configure External Clock source.
  1196. (+) Configure Complementary channels, break features and dead time.
  1197. (+) Configure Master and the Slave synchronization.
  1198. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1199. (+) Configure the DMA Burst Mode.
  1200. @endverbatim
  1201. * @{
  1202. */
  1203. /**
  1204. * @brief Configure the TIM commutation event sequence.
  1205. * @note This function is mandatory to use the commutation event in order to
  1206. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1207. * the typical use of this feature is with the use of another Timer(interface Timer)
  1208. * configured in Hall sensor interface, this interface Timer will generate the
  1209. * commutation at its TRGO output (connected to Timer used in this function) each time
  1210. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1211. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1212. * the configuration information for TIM module.
  1213. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1214. * This parameter can be one of the following values:
  1215. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1216. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1217. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1218. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1219. * @arg TIM_TS_NONE: No trigger is needed
  1220. * @param CommutationSource the Commutation Event source.
  1221. * This parameter can be one of the following values:
  1222. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1223. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1224. * @retval HAL status
  1225. */
  1226. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1227. {
  1228. /* Check the parameters */
  1229. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1230. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1231. __HAL_LOCK(htim);
  1232. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1233. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1234. {
  1235. /* Select the Input trigger */
  1236. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1237. htim->Instance->SMCR |= InputTrigger;
  1238. }
  1239. /* Select the Capture Compare preload feature */
  1240. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1241. /* Select the Commutation event source */
  1242. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1243. htim->Instance->CR2 |= CommutationSource;
  1244. __HAL_UNLOCK(htim);
  1245. return HAL_OK;
  1246. }
  1247. /**
  1248. * @brief Configure the TIM commutation event sequence with interrupt.
  1249. * @note This function is mandatory to use the commutation event in order to
  1250. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1251. * the typical use of this feature is with the use of another Timer(interface Timer)
  1252. * configured in Hall sensor interface, this interface Timer will generate the
  1253. * commutation at its TRGO output (connected to Timer used in this function) each time
  1254. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1255. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1256. * the configuration information for TIM module.
  1257. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1258. * This parameter can be one of the following values:
  1259. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1260. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1261. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1262. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1263. * @arg TIM_TS_NONE: No trigger is needed
  1264. * @param CommutationSource the Commutation Event source.
  1265. * This parameter can be one of the following values:
  1266. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1267. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1268. * @retval HAL status
  1269. */
  1270. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1271. {
  1272. /* Check the parameters */
  1273. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1274. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1275. __HAL_LOCK(htim);
  1276. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1277. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1278. {
  1279. /* Select the Input trigger */
  1280. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1281. htim->Instance->SMCR |= InputTrigger;
  1282. }
  1283. /* Select the Capture Compare preload feature */
  1284. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1285. /* Select the Commutation event source */
  1286. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1287. htim->Instance->CR2 |= CommutationSource;
  1288. /* Enable the Commutation Interrupt Request */
  1289. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1290. __HAL_UNLOCK(htim);
  1291. return HAL_OK;
  1292. }
  1293. /**
  1294. * @brief Configure the TIM commutation event sequence with DMA.
  1295. * @note This function is mandatory to use the commutation event in order to
  1296. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1297. * the typical use of this feature is with the use of another Timer(interface Timer)
  1298. * configured in Hall sensor interface, this interface Timer will generate the
  1299. * commutation at its TRGO output (connected to Timer used in this function) each time
  1300. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1301. * @note: The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1302. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1303. * the configuration information for TIM module.
  1304. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1305. * This parameter can be one of the following values:
  1306. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1307. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1308. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1309. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1310. * @arg TIM_TS_NONE: No trigger is needed
  1311. * @param CommutationSource the Commutation Event source.
  1312. * This parameter can be one of the following values:
  1313. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1314. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1315. * @retval HAL status
  1316. */
  1317. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1318. {
  1319. /* Check the parameters */
  1320. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1321. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1322. __HAL_LOCK(htim);
  1323. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1324. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1325. {
  1326. /* Select the Input trigger */
  1327. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1328. htim->Instance->SMCR |= InputTrigger;
  1329. }
  1330. /* Select the Capture Compare preload feature */
  1331. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1332. /* Select the Commutation event source */
  1333. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1334. htim->Instance->CR2 |= CommutationSource;
  1335. /* Enable the Commutation DMA Request */
  1336. /* Set the DMA Commutation Callback */
  1337. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1338. /* Set the DMA error callback */
  1339. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1340. /* Enable the Commutation DMA Request */
  1341. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1342. __HAL_UNLOCK(htim);
  1343. return HAL_OK;
  1344. }
  1345. /**
  1346. * @brief Configures the TIM in master mode.
  1347. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1348. * the configuration information for TIM module.
  1349. * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
  1350. * contains the selected trigger output (TRGO) and the Master/Slave
  1351. * mode.
  1352. * @retval HAL status
  1353. */
  1354. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, TIM_MasterConfigTypeDef * sMasterConfig)
  1355. {
  1356. /* Check the parameters */
  1357. assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
  1358. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1359. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1360. __HAL_LOCK(htim);
  1361. htim->State = HAL_TIM_STATE_BUSY;
  1362. /* Reset the MMS Bits */
  1363. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  1364. /* Select the TRGO source */
  1365. htim->Instance->CR2 |= sMasterConfig->MasterOutputTrigger;
  1366. /* Reset the MSM Bit */
  1367. htim->Instance->SMCR &= ~TIM_SMCR_MSM;
  1368. /* Set or Reset the MSM Bit */
  1369. htim->Instance->SMCR |= sMasterConfig->MasterSlaveMode;
  1370. htim->State = HAL_TIM_STATE_READY;
  1371. __HAL_UNLOCK(htim);
  1372. return HAL_OK;
  1373. }
  1374. /**
  1375. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1376. * and the AOE(automatic output enable).
  1377. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1378. * the configuration information for TIM module.
  1379. * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfig_TypeDef structure that
  1380. * contains the BDTR Register configuration information for the TIM peripheral.
  1381. * @retval HAL status
  1382. */
  1383. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1384. TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)
  1385. {
  1386. uint32_t tmpbdtr = 0U;
  1387. /* Check the parameters */
  1388. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1389. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1390. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1391. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1392. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1393. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1394. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1395. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1396. /* Check input state */
  1397. __HAL_LOCK(htim);
  1398. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1399. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1400. /* Set the BDTR bits */
  1401. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  1402. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  1403. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  1404. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  1405. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  1406. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  1407. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  1408. MODIFY_REG(tmpbdtr, TIM_BDTR_MOE, sBreakDeadTimeConfig->AutomaticOutput);
  1409. /* Set TIMx_BDTR */
  1410. htim->Instance->BDTR = tmpbdtr;
  1411. __HAL_UNLOCK(htim);
  1412. return HAL_OK;
  1413. }
  1414. /**
  1415. * @brief Configures the TIM2, TIM5 and TIM11 Remapping input capabilities.
  1416. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1417. * the configuration information for TIM module.
  1418. * @param Remap specifies the TIM input remapping source.
  1419. * This parameter can be one of the following values:
  1420. * @arg TIM_TIM2_TIM8_TRGO: TIM2 ITR1 input is connected to TIM8 Trigger output(default)
  1421. * @arg TIM_TIM2_ETH_PTP: TIM2 ITR1 input is connected to ETH PTP trigger output.
  1422. * @arg TIM_TIM2_USBFS_SOF: TIM2 ITR1 input is connected to USB FS SOF.
  1423. * @arg TIM_TIM2_USBHS_SOF: TIM2 ITR1 input is connected to USB HS SOF.
  1424. * @arg TIM_TIM5_GPIO: TIM5 CH4 input is connected to dedicated Timer pin(default)
  1425. * @arg TIM_TIM5_LSI: TIM5 CH4 input is connected to LSI clock.
  1426. * @arg TIM_TIM5_LSE: TIM5 CH4 input is connected to LSE clock.
  1427. * @arg TIM_TIM5_RTC: TIM5 CH4 input is connected to RTC Output event.
  1428. * @arg TIM_TIM11_GPIO: TIM11 CH4 input is connected to dedicated Timer pin(default)
  1429. * @arg TIM_TIM11_HSE: TIM11 CH4 input is connected to HSE_RTC clock
  1430. * (HSE divided by a programmable prescaler)
  1431. * @arg TIM_TIM9_TIM3_TRGO: TIM9 ITR1 input is connected to TIM3 Trigger output(default)
  1432. * @arg TIM_TIM9_LPTIM: TIM9 ITR1 input is connected to LPTIM.
  1433. * @arg TIM_TIM5_TIM3_TRGO: TIM5 ITR1 input is connected to TIM3 Trigger output(default)
  1434. * @arg TIM_TIM5_LPTIM: TIM5 ITR1 input is connected to LPTIM.
  1435. * @arg TIM_TIM1_TIM3_TRGO: TIM1 ITR2 input is connected to TIM3 Trigger output(default)
  1436. * @arg TIM_TIM1_LPTIM: TIM1 ITR2 input is connected to LPTIM.
  1437. * @retval HAL status
  1438. */
  1439. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1440. {
  1441. __HAL_LOCK(htim);
  1442. /* Check parameters */
  1443. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  1444. assert_param(IS_TIM_REMAP(Remap));
  1445. #if defined(LPTIM_OR_TIM1_ITR2_RMP)
  1446. if ((Remap == TIM_TIM9_TIM3_TRGO)|| (Remap == TIM_TIM9_LPTIM)||(Remap ==TIM_TIM5_TIM3_TRGO)||\
  1447. (Remap == TIM_TIM5_LPTIM)||(Remap == TIM_TIM1_TIM3_TRGO)|| (Remap == TIM_TIM1_LPTIM))
  1448. {
  1449. __HAL_RCC_LPTIM1_CLK_ENABLE();
  1450. LPTIM1->OR = (Remap& 0xEFFFFFFFU);
  1451. }
  1452. else
  1453. {
  1454. /* Set the Timer remapping configuration */
  1455. htim->Instance->OR = Remap;
  1456. }
  1457. #else
  1458. /* Set the Timer remapping configuration */
  1459. htim->Instance->OR = Remap;
  1460. #endif
  1461. htim->State = HAL_TIM_STATE_READY;
  1462. __HAL_UNLOCK(htim);
  1463. return HAL_OK;
  1464. }
  1465. /**
  1466. * @}
  1467. */
  1468. /** @defgroup TIMEx_Exported_Functions_Group6 Extension Callbacks functions
  1469. * @brief Extension Callbacks functions
  1470. *
  1471. @verbatim
  1472. ==============================================================================
  1473. ##### Extension Callbacks functions #####
  1474. ==============================================================================
  1475. [..]
  1476. This section provides Extension TIM callback functions:
  1477. (+) Timer Commutation callback
  1478. (+) Timer Break callback
  1479. @endverbatim
  1480. * @{
  1481. */
  1482. /**
  1483. * @brief Hall commutation changed callback in non blocking mode
  1484. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1485. * the configuration information for TIM module.
  1486. * @retval None
  1487. */
  1488. __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)
  1489. {
  1490. /* Prevent unused argument(s) compilation warning */
  1491. UNUSED(htim);
  1492. /* NOTE : This function Should not be modified, when the callback is needed,
  1493. the HAL_TIMEx_CommutationCallback could be implemented in the user file
  1494. */
  1495. }
  1496. /**
  1497. * @brief Hall Break detection callback in non blocking mode
  1498. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1499. * the configuration information for TIM module.
  1500. * @retval None
  1501. */
  1502. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  1503. {
  1504. /* Prevent unused argument(s) compilation warning */
  1505. UNUSED(htim);
  1506. /* NOTE : This function Should not be modified, when the callback is needed,
  1507. the HAL_TIMEx_BreakCallback could be implemented in the user file
  1508. */
  1509. }
  1510. /**
  1511. * @}
  1512. */
  1513. /** @defgroup TIMEx_Exported_Functions_Group7 Extension Peripheral State functions
  1514. * @brief Extension Peripheral State functions
  1515. *
  1516. @verbatim
  1517. ==============================================================================
  1518. ##### Extension Peripheral State functions #####
  1519. ==============================================================================
  1520. [..]
  1521. This subsection permits to get in run-time the status of the peripheral
  1522. and the data flow.
  1523. @endverbatim
  1524. * @{
  1525. */
  1526. /**
  1527. * @brief Return the TIM Hall Sensor interface state
  1528. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  1529. * the configuration information for TIM module.
  1530. * @retval HAL state
  1531. */
  1532. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
  1533. {
  1534. return htim->State;
  1535. }
  1536. /**
  1537. * @}
  1538. */
  1539. /**
  1540. * @brief TIM DMA Commutation callback.
  1541. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1542. * the configuration information for the specified DMA module.
  1543. * @retval None
  1544. */
  1545. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  1546. {
  1547. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1548. htim->State= HAL_TIM_STATE_READY;
  1549. HAL_TIMEx_CommutationCallback(htim);
  1550. }
  1551. /**
  1552. * @}
  1553. */
  1554. /**
  1555. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1556. * @param TIMx to select the TIM peripheral
  1557. * @param Channel specifies the TIM Channel
  1558. * This parameter can be one of the following values:
  1559. * @arg TIM_Channel_1: TIM Channel 1
  1560. * @arg TIM_Channel_2: TIM Channel 2
  1561. * @arg TIM_Channel_3: TIM Channel 3
  1562. * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
  1563. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  1564. * @retval None
  1565. */
  1566. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState)
  1567. {
  1568. uint32_t tmp = 0U;
  1569. /* Check the parameters */
  1570. assert_param(IS_TIM_CC4_INSTANCE(TIMx));
  1571. assert_param(IS_TIM_COMPLEMENTARY_CHANNELS(Channel));
  1572. tmp = TIM_CCER_CC1NE << Channel;
  1573. /* Reset the CCxNE Bit */
  1574. TIMx->CCER &= ~tmp;
  1575. /* Set or reset the CCxNE Bit */
  1576. TIMx->CCER |= (uint32_t)(ChannelNState << Channel);
  1577. }
  1578. /**
  1579. * @}
  1580. */
  1581. #endif /* HAL_TIM_MODULE_ENABLED */
  1582. /**
  1583. * @}
  1584. */
  1585. /**
  1586. * @}
  1587. */
  1588. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/