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.

stm32f4xx_hal_tim_ex.c 64KB

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