stm32l4xx_hal_tim_ex.c 82 KB

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