stm32g0xx_hal_tim_ex.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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. ******************************************************************************
  16. * @attention
  17. *
  18. * Copyright (c) 2018 STMicroelectronics.
  19. * All rights reserved.
  20. *
  21. * This software is licensed under terms that can be found in the LICENSE file
  22. * in the root directory of this software component.
  23. * If no LICENSE file comes with this software, it is provided AS-IS.
  24. *
  25. ******************************************************************************
  26. @verbatim
  27. ==============================================================================
  28. ##### TIMER Extended features #####
  29. ==============================================================================
  30. [..]
  31. The Timer Extended features include:
  32. (#) Complementary outputs with programmable dead-time for :
  33. (++) Output Compare
  34. (++) PWM generation (Edge and Center-aligned Mode)
  35. (++) One-pulse mode output
  36. (#) Synchronization circuit to control the timer with external signals and to
  37. interconnect several timers together.
  38. (#) Break input to put the timer output signals in reset state or in a known state.
  39. (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
  40. positioning purposes
  41. ##### How to use this driver #####
  42. ==============================================================================
  43. [..]
  44. (#) Initialize the TIM low level resources by implementing the following functions
  45. depending on the selected feature:
  46. (++) Hall Sensor output : HAL_TIMEx_HallSensor_MspInit()
  47. (#) Initialize the TIM low level resources :
  48. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  49. (##) TIM pins configuration
  50. (+++) Enable the clock for the TIM GPIOs using the following function:
  51. __HAL_RCC_GPIOx_CLK_ENABLE();
  52. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  53. (#) The external Clock can be configured, if needed (the default clock is the
  54. internal clock from the APBx), using the following function:
  55. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  56. any start function.
  57. (#) Configure the TIM in the desired functioning mode using one of the
  58. initialization function of this driver:
  59. (++) HAL_TIMEx_HallSensor_Init() and HAL_TIMEx_ConfigCommutEvent(): to use the
  60. Timer Hall Sensor Interface and the commutation event with the corresponding
  61. Interrupt and DMA request if needed (Note that One Timer is used to interface
  62. with the Hall sensor Interface and another Timer should be used to use
  63. the commutation event).
  64. (#) Activate the TIM peripheral using one of the start functions:
  65. (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(),
  66. HAL_TIMEx_OCN_Start_IT()
  67. (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(),
  68. HAL_TIMEx_PWMN_Start_IT()
  69. (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
  70. (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(),
  71. HAL_TIMEx_HallSensor_Start_IT().
  72. @endverbatim
  73. ******************************************************************************
  74. */
  75. /* Includes ------------------------------------------------------------------*/
  76. #include "stm32g0xx_hal.h"
  77. /** @addtogroup STM32G0xx_HAL_Driver
  78. * @{
  79. */
  80. /** @defgroup TIMEx TIMEx
  81. * @brief TIM Extended HAL module driver
  82. * @{
  83. */
  84. #ifdef HAL_TIM_MODULE_ENABLED
  85. /* Private typedef -----------------------------------------------------------*/
  86. /* Private define ------------------------------------------------------------*/
  87. /* Private constants ---------------------------------------------------------*/
  88. /** @defgroup TIMEx_Private_Constants TIM Extended Private Constants
  89. * @{
  90. */
  91. /* Timeout for break input rearm */
  92. #define TIM_BREAKINPUT_REARM_TIMEOUT 5UL /* 5 milliseconds */
  93. /**
  94. * @}
  95. */
  96. /* End of private constants --------------------------------------------------*/
  97. /* Private macros ------------------------------------------------------------*/
  98. /* Private variables ---------------------------------------------------------*/
  99. /* Private function prototypes -----------------------------------------------*/
  100. static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma);
  101. static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma);
  102. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState);
  103. /* Exported functions --------------------------------------------------------*/
  104. /** @defgroup TIMEx_Exported_Functions TIM Extended Exported Functions
  105. * @{
  106. */
  107. /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions
  108. * @brief Timer Hall Sensor functions
  109. *
  110. @verbatim
  111. ==============================================================================
  112. ##### Timer Hall Sensor functions #####
  113. ==============================================================================
  114. [..]
  115. This section provides functions allowing to:
  116. (+) Initialize and configure TIM HAL Sensor.
  117. (+) De-initialize TIM HAL Sensor.
  118. (+) Start the Hall Sensor Interface.
  119. (+) Stop the Hall Sensor Interface.
  120. (+) Start the Hall Sensor Interface and enable interrupts.
  121. (+) Stop the Hall Sensor Interface and disable interrupts.
  122. (+) Start the Hall Sensor Interface and enable DMA transfers.
  123. (+) Stop the Hall Sensor Interface and disable DMA transfers.
  124. @endverbatim
  125. * @{
  126. */
  127. /**
  128. * @brief Initializes the TIM Hall Sensor Interface and initialize the associated handle.
  129. * @note When the timer instance is initialized in Hall Sensor Interface mode,
  130. * timer channels 1 and channel 2 are reserved and cannot be used for
  131. * other purpose.
  132. * @param htim TIM Hall Sensor Interface handle
  133. * @param sConfig TIM Hall Sensor configuration structure
  134. * @retval HAL status
  135. */
  136. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, const TIM_HallSensor_InitTypeDef *sConfig)
  137. {
  138. TIM_OC_InitTypeDef OC_Config;
  139. /* Check the TIM handle allocation */
  140. if (htim == NULL)
  141. {
  142. return HAL_ERROR;
  143. }
  144. /* Check the parameters */
  145. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  146. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  147. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  148. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  149. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  150. assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
  151. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  152. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  153. if (htim->State == HAL_TIM_STATE_RESET)
  154. {
  155. /* Allocate lock resource and initialize it */
  156. htim->Lock = HAL_UNLOCKED;
  157. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  158. /* Reset interrupt callbacks to legacy week callbacks */
  159. TIM_ResetCallback(htim);
  160. if (htim->HallSensor_MspInitCallback == NULL)
  161. {
  162. htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit;
  163. }
  164. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  165. htim->HallSensor_MspInitCallback(htim);
  166. #else
  167. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  168. HAL_TIMEx_HallSensor_MspInit(htim);
  169. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  170. }
  171. /* Set the TIM state */
  172. htim->State = HAL_TIM_STATE_BUSY;
  173. /* Configure the Time base in the Encoder Mode */
  174. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  175. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  176. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  177. /* Reset the IC1PSC Bits */
  178. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  179. /* Set the IC1PSC value */
  180. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  181. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  182. htim->Instance->CR2 |= TIM_CR2_TI1S;
  183. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  184. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  185. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  186. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  187. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  188. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  189. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  190. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  191. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  192. OC_Config.OCMode = TIM_OCMODE_PWM2;
  193. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  194. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  195. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  196. OC_Config.Pulse = sConfig->Commutation_Delay;
  197. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  198. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  199. register to 101 */
  200. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  201. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  202. /* Initialize the DMA burst operation state */
  203. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  204. /* Initialize the TIM channels state */
  205. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  206. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  207. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  208. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  209. /* Initialize the TIM state*/
  210. htim->State = HAL_TIM_STATE_READY;
  211. return HAL_OK;
  212. }
  213. /**
  214. * @brief DeInitializes the TIM Hall Sensor interface
  215. * @param htim TIM Hall Sensor Interface handle
  216. * @retval HAL status
  217. */
  218. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  219. {
  220. /* Check the parameters */
  221. assert_param(IS_TIM_INSTANCE(htim->Instance));
  222. htim->State = HAL_TIM_STATE_BUSY;
  223. /* Disable the TIM Peripheral Clock */
  224. __HAL_TIM_DISABLE(htim);
  225. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  226. if (htim->HallSensor_MspDeInitCallback == NULL)
  227. {
  228. htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit;
  229. }
  230. /* DeInit the low level hardware */
  231. htim->HallSensor_MspDeInitCallback(htim);
  232. #else
  233. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  234. HAL_TIMEx_HallSensor_MspDeInit(htim);
  235. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  236. /* Change the DMA burst operation state */
  237. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  238. /* Change the TIM channels state */
  239. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
  240. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
  241. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
  242. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
  243. /* Change TIM state */
  244. htim->State = HAL_TIM_STATE_RESET;
  245. /* Release Lock */
  246. __HAL_UNLOCK(htim);
  247. return HAL_OK;
  248. }
  249. /**
  250. * @brief Initializes the TIM Hall Sensor MSP.
  251. * @param htim TIM Hall Sensor Interface handle
  252. * @retval None
  253. */
  254. __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
  255. {
  256. /* Prevent unused argument(s) compilation warning */
  257. UNUSED(htim);
  258. /* NOTE : This function should not be modified, when the callback is needed,
  259. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  260. */
  261. }
  262. /**
  263. * @brief DeInitializes TIM Hall Sensor MSP.
  264. * @param htim TIM Hall Sensor Interface handle
  265. * @retval None
  266. */
  267. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  268. {
  269. /* Prevent unused argument(s) compilation warning */
  270. UNUSED(htim);
  271. /* NOTE : This function should not be modified, when the callback is needed,
  272. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  273. */
  274. }
  275. /**
  276. * @brief Starts the TIM Hall Sensor Interface.
  277. * @param htim TIM Hall Sensor Interface handle
  278. * @retval HAL status
  279. */
  280. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  281. {
  282. uint32_t tmpsmcr;
  283. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  284. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  285. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  286. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  287. /* Check the parameters */
  288. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  289. /* Check the TIM channels state */
  290. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  291. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  292. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  293. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  294. {
  295. return HAL_ERROR;
  296. }
  297. /* Set the TIM channels state */
  298. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  299. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  300. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  301. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  302. /* Enable the Input Capture channel 1
  303. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  304. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  305. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  306. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  307. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  308. {
  309. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  310. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  311. {
  312. __HAL_TIM_ENABLE(htim);
  313. }
  314. }
  315. else
  316. {
  317. __HAL_TIM_ENABLE(htim);
  318. }
  319. /* Return function status */
  320. return HAL_OK;
  321. }
  322. /**
  323. * @brief Stops the TIM Hall sensor Interface.
  324. * @param htim TIM Hall Sensor Interface handle
  325. * @retval HAL status
  326. */
  327. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  328. {
  329. /* Check the parameters */
  330. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  331. /* Disable the Input Capture channels 1, 2 and 3
  332. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  333. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  334. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  335. /* Disable the Peripheral */
  336. __HAL_TIM_DISABLE(htim);
  337. /* Set the TIM channels state */
  338. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  339. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  340. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  341. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  342. /* Return function status */
  343. return HAL_OK;
  344. }
  345. /**
  346. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  347. * @param htim TIM Hall Sensor Interface handle
  348. * @retval HAL status
  349. */
  350. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  351. {
  352. uint32_t tmpsmcr;
  353. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  354. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  355. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  356. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  357. /* Check the parameters */
  358. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  359. /* Check the TIM channels state */
  360. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  361. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  362. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  363. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  364. {
  365. return HAL_ERROR;
  366. }
  367. /* Set the TIM channels state */
  368. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  369. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  370. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  371. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  372. /* Enable the capture compare Interrupts 1 event */
  373. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  374. /* Enable the Input Capture channel 1
  375. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  376. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  377. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  378. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  379. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  380. {
  381. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  382. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  383. {
  384. __HAL_TIM_ENABLE(htim);
  385. }
  386. }
  387. else
  388. {
  389. __HAL_TIM_ENABLE(htim);
  390. }
  391. /* Return function status */
  392. return HAL_OK;
  393. }
  394. /**
  395. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  396. * @param htim TIM Hall Sensor Interface handle
  397. * @retval HAL status
  398. */
  399. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  400. {
  401. /* Check the parameters */
  402. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  403. /* Disable the Input Capture channel 1
  404. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  405. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  406. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  407. /* Disable the capture compare Interrupts event */
  408. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  409. /* Disable the Peripheral */
  410. __HAL_TIM_DISABLE(htim);
  411. /* Set the TIM channels state */
  412. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  413. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  414. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  415. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  416. /* Return function status */
  417. return HAL_OK;
  418. }
  419. /**
  420. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  421. * @param htim TIM Hall Sensor Interface handle
  422. * @param pData The destination Buffer address.
  423. * @param Length The length of data to be transferred from TIM peripheral to memory.
  424. * @retval HAL status
  425. */
  426. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  427. {
  428. uint32_t tmpsmcr;
  429. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  430. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  431. /* Check the parameters */
  432. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  433. /* Set the TIM channel state */
  434. if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
  435. || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY))
  436. {
  437. return HAL_BUSY;
  438. }
  439. else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
  440. && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY))
  441. {
  442. if ((pData == NULL) || (Length == 0U))
  443. {
  444. return HAL_ERROR;
  445. }
  446. else
  447. {
  448. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  449. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  450. }
  451. }
  452. else
  453. {
  454. return HAL_ERROR;
  455. }
  456. /* Enable the Input Capture channel 1
  457. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  458. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  459. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  460. /* Set the DMA Input Capture 1 Callbacks */
  461. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  462. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  463. /* Set the DMA error callback */
  464. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  465. /* Enable the DMA channel for Capture 1*/
  466. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
  467. {
  468. /* Return error status */
  469. return HAL_ERROR;
  470. }
  471. /* Enable the capture compare 1 Interrupt */
  472. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  473. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  474. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  475. {
  476. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  477. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  478. {
  479. __HAL_TIM_ENABLE(htim);
  480. }
  481. }
  482. else
  483. {
  484. __HAL_TIM_ENABLE(htim);
  485. }
  486. /* Return function status */
  487. return HAL_OK;
  488. }
  489. /**
  490. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  491. * @param htim TIM Hall Sensor Interface handle
  492. * @retval HAL status
  493. */
  494. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  495. {
  496. /* Check the parameters */
  497. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  498. /* Disable the Input Capture channel 1
  499. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  500. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  501. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  502. /* Disable the capture compare Interrupts 1 event */
  503. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  504. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  505. /* Disable the Peripheral */
  506. __HAL_TIM_DISABLE(htim);
  507. /* Set the TIM channel state */
  508. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  509. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  510. /* Return function status */
  511. return HAL_OK;
  512. }
  513. /**
  514. * @}
  515. */
  516. /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
  517. * @brief Timer Complementary Output Compare functions
  518. *
  519. @verbatim
  520. ==============================================================================
  521. ##### Timer Complementary Output Compare functions #####
  522. ==============================================================================
  523. [..]
  524. This section provides functions allowing to:
  525. (+) Start the Complementary Output Compare/PWM.
  526. (+) Stop the Complementary Output Compare/PWM.
  527. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  528. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  529. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  530. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  531. @endverbatim
  532. * @{
  533. */
  534. /**
  535. * @brief Starts the TIM Output Compare signal generation on the complementary
  536. * output.
  537. * @param htim TIM Output Compare handle
  538. * @param Channel TIM Channel to be enabled
  539. * This parameter can be one of the following values:
  540. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  541. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  542. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  543. * @retval HAL status
  544. */
  545. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  546. {
  547. uint32_t tmpsmcr;
  548. /* Check the parameters */
  549. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  550. /* Check the TIM complementary channel state */
  551. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  552. {
  553. return HAL_ERROR;
  554. }
  555. /* Set the TIM complementary channel state */
  556. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  557. /* Enable the Capture compare channel N */
  558. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  559. /* Enable the Main Output */
  560. __HAL_TIM_MOE_ENABLE(htim);
  561. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  562. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  563. {
  564. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  565. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  566. {
  567. __HAL_TIM_ENABLE(htim);
  568. }
  569. }
  570. else
  571. {
  572. __HAL_TIM_ENABLE(htim);
  573. }
  574. /* Return function status */
  575. return HAL_OK;
  576. }
  577. /**
  578. * @brief Stops the TIM Output Compare signal generation on the complementary
  579. * output.
  580. * @param htim TIM handle
  581. * @param Channel TIM Channel to be disabled
  582. * This parameter can be one of the following values:
  583. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  584. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  585. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  586. * @retval HAL status
  587. */
  588. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  589. {
  590. /* Check the parameters */
  591. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  592. /* Disable the Capture compare channel N */
  593. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  594. /* Disable the Main Output */
  595. __HAL_TIM_MOE_DISABLE(htim);
  596. /* Disable the Peripheral */
  597. __HAL_TIM_DISABLE(htim);
  598. /* Set the TIM complementary channel state */
  599. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  600. /* Return function status */
  601. return HAL_OK;
  602. }
  603. /**
  604. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  605. * on the complementary output.
  606. * @param htim TIM OC handle
  607. * @param Channel TIM Channel to be enabled
  608. * This parameter can be one of the following values:
  609. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  610. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  611. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  612. * @retval HAL status
  613. */
  614. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  615. {
  616. HAL_StatusTypeDef status = HAL_OK;
  617. uint32_t tmpsmcr;
  618. /* Check the parameters */
  619. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  620. /* Check the TIM complementary channel state */
  621. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  622. {
  623. return HAL_ERROR;
  624. }
  625. /* Set the TIM complementary channel state */
  626. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  627. switch (Channel)
  628. {
  629. case TIM_CHANNEL_1:
  630. {
  631. /* Enable the TIM Output Compare interrupt */
  632. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  633. break;
  634. }
  635. case TIM_CHANNEL_2:
  636. {
  637. /* Enable the TIM Output Compare interrupt */
  638. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  639. break;
  640. }
  641. case TIM_CHANNEL_3:
  642. {
  643. /* Enable the TIM Output Compare interrupt */
  644. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  645. break;
  646. }
  647. default:
  648. status = HAL_ERROR;
  649. break;
  650. }
  651. if (status == HAL_OK)
  652. {
  653. /* Enable the TIM Break interrupt */
  654. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  655. /* Enable the Capture compare channel N */
  656. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  657. /* Enable the Main Output */
  658. __HAL_TIM_MOE_ENABLE(htim);
  659. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  660. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  661. {
  662. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  663. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  664. {
  665. __HAL_TIM_ENABLE(htim);
  666. }
  667. }
  668. else
  669. {
  670. __HAL_TIM_ENABLE(htim);
  671. }
  672. }
  673. /* Return function status */
  674. return status;
  675. }
  676. /**
  677. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  678. * on the complementary output.
  679. * @param htim TIM Output Compare handle
  680. * @param Channel TIM Channel to be disabled
  681. * This parameter can be one of the following values:
  682. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  683. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  684. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  685. * @retval HAL status
  686. */
  687. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  688. {
  689. HAL_StatusTypeDef status = HAL_OK;
  690. uint32_t tmpccer;
  691. /* Check the parameters */
  692. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  693. switch (Channel)
  694. {
  695. case TIM_CHANNEL_1:
  696. {
  697. /* Disable the TIM Output Compare interrupt */
  698. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  699. break;
  700. }
  701. case TIM_CHANNEL_2:
  702. {
  703. /* Disable the TIM Output Compare interrupt */
  704. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  705. break;
  706. }
  707. case TIM_CHANNEL_3:
  708. {
  709. /* Disable the TIM Output Compare interrupt */
  710. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  711. break;
  712. }
  713. default:
  714. status = HAL_ERROR;
  715. break;
  716. }
  717. if (status == HAL_OK)
  718. {
  719. /* Disable the Capture compare channel N */
  720. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  721. /* Disable the TIM Break interrupt (only if no more channel is active) */
  722. tmpccer = htim->Instance->CCER;
  723. if ((tmpccer & TIM_CCER_CCxNE_MASK) == (uint32_t)RESET)
  724. {
  725. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  726. }
  727. /* Disable the Main Output */
  728. __HAL_TIM_MOE_DISABLE(htim);
  729. /* Disable the Peripheral */
  730. __HAL_TIM_DISABLE(htim);
  731. /* Set the TIM complementary channel state */
  732. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  733. }
  734. /* Return function status */
  735. return status;
  736. }
  737. /**
  738. * @brief Starts the TIM Output Compare signal generation in DMA mode
  739. * on the complementary output.
  740. * @param htim TIM Output Compare handle
  741. * @param Channel TIM Channel to be enabled
  742. * This parameter can be one of the following values:
  743. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  744. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  745. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  746. * @param pData The source Buffer address.
  747. * @param Length The length of data to be transferred from memory to TIM peripheral
  748. * @retval HAL status
  749. */
  750. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
  751. uint16_t Length)
  752. {
  753. HAL_StatusTypeDef status = HAL_OK;
  754. uint32_t tmpsmcr;
  755. /* Check the parameters */
  756. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  757. /* Set the TIM complementary channel state */
  758. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
  759. {
  760. return HAL_BUSY;
  761. }
  762. else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
  763. {
  764. if ((pData == NULL) || (Length == 0U))
  765. {
  766. return HAL_ERROR;
  767. }
  768. else
  769. {
  770. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  771. }
  772. }
  773. else
  774. {
  775. return HAL_ERROR;
  776. }
  777. switch (Channel)
  778. {
  779. case TIM_CHANNEL_1:
  780. {
  781. /* Set the DMA compare callbacks */
  782. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  783. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  784. /* Set the DMA error callback */
  785. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
  786. /* Enable the DMA channel */
  787. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
  788. Length) != HAL_OK)
  789. {
  790. /* Return error status */
  791. return HAL_ERROR;
  792. }
  793. /* Enable the TIM Output Compare DMA request */
  794. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  795. break;
  796. }
  797. case TIM_CHANNEL_2:
  798. {
  799. /* Set the DMA compare callbacks */
  800. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  801. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  802. /* Set the DMA error callback */
  803. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
  804. /* Enable the DMA channel */
  805. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
  806. Length) != HAL_OK)
  807. {
  808. /* Return error status */
  809. return HAL_ERROR;
  810. }
  811. /* Enable the TIM Output Compare DMA request */
  812. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  813. break;
  814. }
  815. case TIM_CHANNEL_3:
  816. {
  817. /* Set the DMA compare callbacks */
  818. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  819. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  820. /* Set the DMA error callback */
  821. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
  822. /* Enable the DMA channel */
  823. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
  824. Length) != HAL_OK)
  825. {
  826. /* Return error status */
  827. return HAL_ERROR;
  828. }
  829. /* Enable the TIM Output Compare DMA request */
  830. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  831. break;
  832. }
  833. default:
  834. status = HAL_ERROR;
  835. break;
  836. }
  837. if (status == HAL_OK)
  838. {
  839. /* Enable the Capture compare channel N */
  840. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  841. /* Enable the Main Output */
  842. __HAL_TIM_MOE_ENABLE(htim);
  843. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  844. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  845. {
  846. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  847. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  848. {
  849. __HAL_TIM_ENABLE(htim);
  850. }
  851. }
  852. else
  853. {
  854. __HAL_TIM_ENABLE(htim);
  855. }
  856. }
  857. /* Return function status */
  858. return status;
  859. }
  860. /**
  861. * @brief Stops the TIM Output Compare signal generation in DMA mode
  862. * on the complementary output.
  863. * @param htim TIM Output Compare handle
  864. * @param Channel TIM Channel to be disabled
  865. * This parameter can be one of the following values:
  866. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  867. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  868. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  869. * @retval HAL status
  870. */
  871. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  872. {
  873. HAL_StatusTypeDef status = HAL_OK;
  874. /* Check the parameters */
  875. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  876. switch (Channel)
  877. {
  878. case TIM_CHANNEL_1:
  879. {
  880. /* Disable the TIM Output Compare DMA request */
  881. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  882. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  883. break;
  884. }
  885. case TIM_CHANNEL_2:
  886. {
  887. /* Disable the TIM Output Compare DMA request */
  888. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  889. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  890. break;
  891. }
  892. case TIM_CHANNEL_3:
  893. {
  894. /* Disable the TIM Output Compare DMA request */
  895. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  896. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  897. break;
  898. }
  899. default:
  900. status = HAL_ERROR;
  901. break;
  902. }
  903. if (status == HAL_OK)
  904. {
  905. /* Disable the Capture compare channel N */
  906. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  907. /* Disable the Main Output */
  908. __HAL_TIM_MOE_DISABLE(htim);
  909. /* Disable the Peripheral */
  910. __HAL_TIM_DISABLE(htim);
  911. /* Set the TIM complementary channel state */
  912. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  913. }
  914. /* Return function status */
  915. return status;
  916. }
  917. /**
  918. * @}
  919. */
  920. /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
  921. * @brief Timer Complementary PWM functions
  922. *
  923. @verbatim
  924. ==============================================================================
  925. ##### Timer Complementary PWM functions #####
  926. ==============================================================================
  927. [..]
  928. This section provides functions allowing to:
  929. (+) Start the Complementary PWM.
  930. (+) Stop the Complementary PWM.
  931. (+) Start the Complementary PWM and enable interrupts.
  932. (+) Stop the Complementary PWM and disable interrupts.
  933. (+) Start the Complementary PWM and enable DMA transfers.
  934. (+) Stop the Complementary PWM and disable DMA transfers.
  935. @endverbatim
  936. * @{
  937. */
  938. /**
  939. * @brief Starts the PWM signal generation on the complementary output.
  940. * @param htim TIM handle
  941. * @param Channel TIM Channel to be enabled
  942. * This parameter can be one of the following values:
  943. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  944. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  945. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  946. * @retval HAL status
  947. */
  948. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  949. {
  950. uint32_t tmpsmcr;
  951. /* Check the parameters */
  952. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  953. /* Check the TIM complementary channel state */
  954. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  955. {
  956. return HAL_ERROR;
  957. }
  958. /* Set the TIM complementary channel state */
  959. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  960. /* Enable the complementary PWM output */
  961. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  962. /* Enable the Main Output */
  963. __HAL_TIM_MOE_ENABLE(htim);
  964. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  965. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  966. {
  967. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  968. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  969. {
  970. __HAL_TIM_ENABLE(htim);
  971. }
  972. }
  973. else
  974. {
  975. __HAL_TIM_ENABLE(htim);
  976. }
  977. /* Return function status */
  978. return HAL_OK;
  979. }
  980. /**
  981. * @brief Stops the PWM signal generation on the complementary output.
  982. * @param htim TIM handle
  983. * @param Channel TIM Channel to be disabled
  984. * This parameter can be one of the following values:
  985. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  986. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  987. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  988. * @retval HAL status
  989. */
  990. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  991. {
  992. /* Check the parameters */
  993. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  994. /* Disable the complementary PWM output */
  995. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  996. /* Disable the Main Output */
  997. __HAL_TIM_MOE_DISABLE(htim);
  998. /* Disable the Peripheral */
  999. __HAL_TIM_DISABLE(htim);
  1000. /* Set the TIM complementary channel state */
  1001. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1002. /* Return function status */
  1003. return HAL_OK;
  1004. }
  1005. /**
  1006. * @brief Starts the PWM signal generation in interrupt mode on the
  1007. * complementary output.
  1008. * @param htim TIM handle
  1009. * @param Channel TIM Channel to be disabled
  1010. * This parameter can be one of the following values:
  1011. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1012. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1013. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1014. * @retval HAL status
  1015. */
  1016. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1017. {
  1018. HAL_StatusTypeDef status = HAL_OK;
  1019. uint32_t tmpsmcr;
  1020. /* Check the parameters */
  1021. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1022. /* Check the TIM complementary channel state */
  1023. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  1024. {
  1025. return HAL_ERROR;
  1026. }
  1027. /* Set the TIM complementary channel state */
  1028. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1029. switch (Channel)
  1030. {
  1031. case TIM_CHANNEL_1:
  1032. {
  1033. /* Enable the TIM Capture/Compare 1 interrupt */
  1034. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1035. break;
  1036. }
  1037. case TIM_CHANNEL_2:
  1038. {
  1039. /* Enable the TIM Capture/Compare 2 interrupt */
  1040. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1041. break;
  1042. }
  1043. case TIM_CHANNEL_3:
  1044. {
  1045. /* Enable the TIM Capture/Compare 3 interrupt */
  1046. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1047. break;
  1048. }
  1049. default:
  1050. status = HAL_ERROR;
  1051. break;
  1052. }
  1053. if (status == HAL_OK)
  1054. {
  1055. /* Enable the TIM Break interrupt */
  1056. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  1057. /* Enable the complementary PWM output */
  1058. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1059. /* Enable the Main Output */
  1060. __HAL_TIM_MOE_ENABLE(htim);
  1061. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1062. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1063. {
  1064. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1065. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1066. {
  1067. __HAL_TIM_ENABLE(htim);
  1068. }
  1069. }
  1070. else
  1071. {
  1072. __HAL_TIM_ENABLE(htim);
  1073. }
  1074. }
  1075. /* Return function status */
  1076. return status;
  1077. }
  1078. /**
  1079. * @brief Stops the PWM signal generation in interrupt mode on the
  1080. * complementary output.
  1081. * @param htim TIM handle
  1082. * @param Channel TIM Channel to be disabled
  1083. * This parameter can be one of the following values:
  1084. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1085. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1086. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1087. * @retval HAL status
  1088. */
  1089. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1090. {
  1091. HAL_StatusTypeDef status = HAL_OK;
  1092. uint32_t tmpccer;
  1093. /* Check the parameters */
  1094. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1095. switch (Channel)
  1096. {
  1097. case TIM_CHANNEL_1:
  1098. {
  1099. /* Disable the TIM Capture/Compare 1 interrupt */
  1100. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1101. break;
  1102. }
  1103. case TIM_CHANNEL_2:
  1104. {
  1105. /* Disable the TIM Capture/Compare 2 interrupt */
  1106. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1107. break;
  1108. }
  1109. case TIM_CHANNEL_3:
  1110. {
  1111. /* Disable the TIM Capture/Compare 3 interrupt */
  1112. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1113. break;
  1114. }
  1115. default:
  1116. status = HAL_ERROR;
  1117. break;
  1118. }
  1119. if (status == HAL_OK)
  1120. {
  1121. /* Disable the complementary PWM output */
  1122. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1123. /* Disable the TIM Break interrupt (only if no more channel is active) */
  1124. tmpccer = htim->Instance->CCER;
  1125. if ((tmpccer & TIM_CCER_CCxNE_MASK) == (uint32_t)RESET)
  1126. {
  1127. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  1128. }
  1129. /* Disable the Main Output */
  1130. __HAL_TIM_MOE_DISABLE(htim);
  1131. /* Disable the Peripheral */
  1132. __HAL_TIM_DISABLE(htim);
  1133. /* Set the TIM complementary channel state */
  1134. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1135. }
  1136. /* Return function status */
  1137. return status;
  1138. }
  1139. /**
  1140. * @brief Starts the TIM PWM signal generation in DMA mode on the
  1141. * complementary output
  1142. * @param htim TIM handle
  1143. * @param Channel TIM Channel to be enabled
  1144. * This parameter can be one of the following values:
  1145. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1146. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1147. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1148. * @param pData The source Buffer address.
  1149. * @param Length The length of data to be transferred from memory to TIM peripheral
  1150. * @retval HAL status
  1151. */
  1152. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
  1153. uint16_t Length)
  1154. {
  1155. HAL_StatusTypeDef status = HAL_OK;
  1156. uint32_t tmpsmcr;
  1157. /* Check the parameters */
  1158. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1159. /* Set the TIM complementary channel state */
  1160. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
  1161. {
  1162. return HAL_BUSY;
  1163. }
  1164. else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
  1165. {
  1166. if ((pData == NULL) || (Length == 0U))
  1167. {
  1168. return HAL_ERROR;
  1169. }
  1170. else
  1171. {
  1172. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1173. }
  1174. }
  1175. else
  1176. {
  1177. return HAL_ERROR;
  1178. }
  1179. switch (Channel)
  1180. {
  1181. case TIM_CHANNEL_1:
  1182. {
  1183. /* Set the DMA compare callbacks */
  1184. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1185. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1186. /* Set the DMA error callback */
  1187. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1188. /* Enable the DMA channel */
  1189. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
  1190. Length) != HAL_OK)
  1191. {
  1192. /* Return error status */
  1193. return HAL_ERROR;
  1194. }
  1195. /* Enable the TIM Capture/Compare 1 DMA request */
  1196. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1197. break;
  1198. }
  1199. case TIM_CHANNEL_2:
  1200. {
  1201. /* Set the DMA compare callbacks */
  1202. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1203. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1204. /* Set the DMA error callback */
  1205. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1206. /* Enable the DMA channel */
  1207. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
  1208. Length) != HAL_OK)
  1209. {
  1210. /* Return error status */
  1211. return HAL_ERROR;
  1212. }
  1213. /* Enable the TIM Capture/Compare 2 DMA request */
  1214. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1215. break;
  1216. }
  1217. case TIM_CHANNEL_3:
  1218. {
  1219. /* Set the DMA compare callbacks */
  1220. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1221. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1222. /* Set the DMA error callback */
  1223. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1224. /* Enable the DMA channel */
  1225. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
  1226. Length) != HAL_OK)
  1227. {
  1228. /* Return error status */
  1229. return HAL_ERROR;
  1230. }
  1231. /* Enable the TIM Capture/Compare 3 DMA request */
  1232. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1233. break;
  1234. }
  1235. default:
  1236. status = HAL_ERROR;
  1237. break;
  1238. }
  1239. if (status == HAL_OK)
  1240. {
  1241. /* Enable the complementary PWM output */
  1242. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1243. /* Enable the Main Output */
  1244. __HAL_TIM_MOE_ENABLE(htim);
  1245. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1246. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1247. {
  1248. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1249. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1250. {
  1251. __HAL_TIM_ENABLE(htim);
  1252. }
  1253. }
  1254. else
  1255. {
  1256. __HAL_TIM_ENABLE(htim);
  1257. }
  1258. }
  1259. /* Return function status */
  1260. return status;
  1261. }
  1262. /**
  1263. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  1264. * output
  1265. * @param htim TIM handle
  1266. * @param Channel TIM Channel to be disabled
  1267. * This parameter can be one of the following values:
  1268. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1269. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1270. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1271. * @retval HAL status
  1272. */
  1273. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1274. {
  1275. HAL_StatusTypeDef status = HAL_OK;
  1276. /* Check the parameters */
  1277. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1278. switch (Channel)
  1279. {
  1280. case TIM_CHANNEL_1:
  1281. {
  1282. /* Disable the TIM Capture/Compare 1 DMA request */
  1283. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1284. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1285. break;
  1286. }
  1287. case TIM_CHANNEL_2:
  1288. {
  1289. /* Disable the TIM Capture/Compare 2 DMA request */
  1290. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1291. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1292. break;
  1293. }
  1294. case TIM_CHANNEL_3:
  1295. {
  1296. /* Disable the TIM Capture/Compare 3 DMA request */
  1297. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1298. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1299. break;
  1300. }
  1301. default:
  1302. status = HAL_ERROR;
  1303. break;
  1304. }
  1305. if (status == HAL_OK)
  1306. {
  1307. /* Disable the complementary PWM output */
  1308. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1309. /* Disable the Main Output */
  1310. __HAL_TIM_MOE_DISABLE(htim);
  1311. /* Disable the Peripheral */
  1312. __HAL_TIM_DISABLE(htim);
  1313. /* Set the TIM complementary channel state */
  1314. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1315. }
  1316. /* Return function status */
  1317. return status;
  1318. }
  1319. /**
  1320. * @}
  1321. */
  1322. /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
  1323. * @brief Timer Complementary One Pulse functions
  1324. *
  1325. @verbatim
  1326. ==============================================================================
  1327. ##### Timer Complementary One Pulse functions #####
  1328. ==============================================================================
  1329. [..]
  1330. This section provides functions allowing to:
  1331. (+) Start the Complementary One Pulse generation.
  1332. (+) Stop the Complementary One Pulse.
  1333. (+) Start the Complementary One Pulse and enable interrupts.
  1334. (+) Stop the Complementary One Pulse and disable interrupts.
  1335. @endverbatim
  1336. * @{
  1337. */
  1338. /**
  1339. * @brief Starts the TIM One Pulse signal generation on the complementary
  1340. * output.
  1341. * @note OutputChannel must match the pulse output channel chosen when calling
  1342. * @ref HAL_TIM_OnePulse_ConfigChannel().
  1343. * @param htim TIM One Pulse handle
  1344. * @param OutputChannel pulse output channel to enable
  1345. * This parameter can be one of the following values:
  1346. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1347. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1348. * @retval HAL status
  1349. */
  1350. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1351. {
  1352. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1353. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  1354. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  1355. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  1356. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  1357. /* Check the parameters */
  1358. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1359. /* Check the TIM channels state */
  1360. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  1361. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  1362. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  1363. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  1364. {
  1365. return HAL_ERROR;
  1366. }
  1367. /* Set the TIM channels state */
  1368. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  1369. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  1370. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  1371. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  1372. /* Enable the complementary One Pulse output channel and the Input Capture channel */
  1373. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1374. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
  1375. /* Enable the Main Output */
  1376. __HAL_TIM_MOE_ENABLE(htim);
  1377. /* Return function status */
  1378. return HAL_OK;
  1379. }
  1380. /**
  1381. * @brief Stops the TIM One Pulse signal generation on the complementary
  1382. * output.
  1383. * @note OutputChannel must match the pulse output channel chosen when calling
  1384. * @ref HAL_TIM_OnePulse_ConfigChannel().
  1385. * @param htim TIM One Pulse handle
  1386. * @param OutputChannel pulse output channel to disable
  1387. * This parameter can be one of the following values:
  1388. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1389. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1390. * @retval HAL status
  1391. */
  1392. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1393. {
  1394. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1395. /* Check the parameters */
  1396. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1397. /* Disable the complementary One Pulse output channel and the Input Capture channel */
  1398. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1399. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
  1400. /* Disable the Main Output */
  1401. __HAL_TIM_MOE_DISABLE(htim);
  1402. /* Disable the Peripheral */
  1403. __HAL_TIM_DISABLE(htim);
  1404. /* Set the TIM channels state */
  1405. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  1406. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  1407. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  1408. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  1409. /* Return function status */
  1410. return HAL_OK;
  1411. }
  1412. /**
  1413. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1414. * complementary channel.
  1415. * @note OutputChannel must match the pulse output channel chosen when calling
  1416. * @ref HAL_TIM_OnePulse_ConfigChannel().
  1417. * @param htim TIM One Pulse handle
  1418. * @param OutputChannel pulse output channel to enable
  1419. * This parameter can be one of the following values:
  1420. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1421. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1422. * @retval HAL status
  1423. */
  1424. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1425. {
  1426. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1427. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  1428. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  1429. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  1430. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  1431. /* Check the parameters */
  1432. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1433. /* Check the TIM channels state */
  1434. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  1435. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  1436. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  1437. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  1438. {
  1439. return HAL_ERROR;
  1440. }
  1441. /* Set the TIM channels state */
  1442. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  1443. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  1444. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  1445. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  1446. /* Enable the TIM Capture/Compare 1 interrupt */
  1447. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1448. /* Enable the TIM Capture/Compare 2 interrupt */
  1449. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1450. /* Enable the complementary One Pulse output channel and the Input Capture channel */
  1451. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1452. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
  1453. /* Enable the Main Output */
  1454. __HAL_TIM_MOE_ENABLE(htim);
  1455. /* Return function status */
  1456. return HAL_OK;
  1457. }
  1458. /**
  1459. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1460. * complementary channel.
  1461. * @note OutputChannel must match the pulse output channel chosen when calling
  1462. * @ref HAL_TIM_OnePulse_ConfigChannel().
  1463. * @param htim TIM One Pulse handle
  1464. * @param OutputChannel pulse output channel to disable
  1465. * This parameter can be one of the following values:
  1466. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1467. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1468. * @retval HAL status
  1469. */
  1470. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1471. {
  1472. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1473. /* Check the parameters */
  1474. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1475. /* Disable the TIM Capture/Compare 1 interrupt */
  1476. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1477. /* Disable the TIM Capture/Compare 2 interrupt */
  1478. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1479. /* Disable the complementary One Pulse output channel and the Input Capture channel */
  1480. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1481. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
  1482. /* Disable the Main Output */
  1483. __HAL_TIM_MOE_DISABLE(htim);
  1484. /* Disable the Peripheral */
  1485. __HAL_TIM_DISABLE(htim);
  1486. /* Set the TIM channels state */
  1487. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  1488. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  1489. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  1490. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  1491. /* Return function status */
  1492. return HAL_OK;
  1493. }
  1494. /**
  1495. * @}
  1496. */
  1497. /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
  1498. * @brief Peripheral Control functions
  1499. *
  1500. @verbatim
  1501. ==============================================================================
  1502. ##### Peripheral Control functions #####
  1503. ==============================================================================
  1504. [..]
  1505. This section provides functions allowing to:
  1506. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1507. (+) Configure Output channels for OC and PWM mode.
  1508. (+) Configure Complementary channels, break features and dead time.
  1509. (+) Configure Master synchronization.
  1510. (+) Configure timer remapping capabilities.
  1511. (+) Select timer input source.
  1512. (+) Enable or disable channel grouping.
  1513. @endverbatim
  1514. * @{
  1515. */
  1516. /**
  1517. * @brief Configure the TIM commutation event sequence.
  1518. * @note This function is mandatory to use the commutation event in order to
  1519. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1520. * the typical use of this feature is with the use of another Timer(interface Timer)
  1521. * configured in Hall sensor interface, this interface Timer will generate the
  1522. * commutation at its TRGO output (connected to Timer used in this function) each time
  1523. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1524. * @param htim TIM handle
  1525. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1526. * This parameter can be one of the following values:
  1527. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1528. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1529. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1530. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1531. * @arg TIM_TS_NONE: No trigger is needed
  1532. * @param CommutationSource the Commutation Event source
  1533. * This parameter can be one of the following values:
  1534. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1535. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1536. * @retval HAL status
  1537. */
  1538. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1539. uint32_t CommutationSource)
  1540. {
  1541. /* Check the parameters */
  1542. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1543. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1544. __HAL_LOCK(htim);
  1545. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1546. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1547. {
  1548. /* Select the Input trigger */
  1549. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1550. htim->Instance->SMCR |= InputTrigger;
  1551. }
  1552. /* Select the Capture Compare preload feature */
  1553. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1554. /* Select the Commutation event source */
  1555. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1556. htim->Instance->CR2 |= CommutationSource;
  1557. /* Disable Commutation Interrupt */
  1558. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1559. /* Disable Commutation DMA request */
  1560. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1561. __HAL_UNLOCK(htim);
  1562. return HAL_OK;
  1563. }
  1564. /**
  1565. * @brief Configure the TIM commutation event sequence with interrupt.
  1566. * @note This function is mandatory to use the commutation event in order to
  1567. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1568. * the typical use of this feature is with the use of another Timer(interface Timer)
  1569. * configured in Hall sensor interface, this interface Timer will generate the
  1570. * commutation at its TRGO output (connected to Timer used in this function) each time
  1571. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1572. * @param htim TIM handle
  1573. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1574. * This parameter can be one of the following values:
  1575. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1576. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1577. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1578. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1579. * @arg TIM_TS_NONE: No trigger is needed
  1580. * @param CommutationSource the Commutation Event source
  1581. * This parameter can be one of the following values:
  1582. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1583. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1584. * @retval HAL status
  1585. */
  1586. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1587. uint32_t CommutationSource)
  1588. {
  1589. /* Check the parameters */
  1590. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1591. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1592. __HAL_LOCK(htim);
  1593. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1594. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1595. {
  1596. /* Select the Input trigger */
  1597. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1598. htim->Instance->SMCR |= InputTrigger;
  1599. }
  1600. /* Select the Capture Compare preload feature */
  1601. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1602. /* Select the Commutation event source */
  1603. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1604. htim->Instance->CR2 |= CommutationSource;
  1605. /* Disable Commutation DMA request */
  1606. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1607. /* Enable the Commutation Interrupt */
  1608. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1609. __HAL_UNLOCK(htim);
  1610. return HAL_OK;
  1611. }
  1612. /**
  1613. * @brief Configure the TIM commutation event sequence with DMA.
  1614. * @note This function is mandatory to use the commutation event in order to
  1615. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1616. * the typical use of this feature is with the use of another Timer(interface Timer)
  1617. * configured in Hall sensor interface, this interface Timer will generate the
  1618. * commutation at its TRGO output (connected to Timer used in this function) each time
  1619. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1620. * @note The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1621. * @param htim TIM handle
  1622. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1623. * This parameter can be one of the following values:
  1624. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1625. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1626. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1627. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1628. * @arg TIM_TS_NONE: No trigger is needed
  1629. * @param CommutationSource the Commutation Event source
  1630. * This parameter can be one of the following values:
  1631. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1632. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1633. * @retval HAL status
  1634. */
  1635. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1636. uint32_t CommutationSource)
  1637. {
  1638. /* Check the parameters */
  1639. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1640. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1641. __HAL_LOCK(htim);
  1642. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1643. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1644. {
  1645. /* Select the Input trigger */
  1646. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1647. htim->Instance->SMCR |= InputTrigger;
  1648. }
  1649. /* Select the Capture Compare preload feature */
  1650. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1651. /* Select the Commutation event source */
  1652. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1653. htim->Instance->CR2 |= CommutationSource;
  1654. /* Enable the Commutation DMA Request */
  1655. /* Set the DMA Commutation Callback */
  1656. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1657. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  1658. /* Set the DMA error callback */
  1659. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1660. /* Disable Commutation Interrupt */
  1661. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1662. /* Enable the Commutation DMA Request */
  1663. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1664. __HAL_UNLOCK(htim);
  1665. return HAL_OK;
  1666. }
  1667. /**
  1668. * @brief Configures the TIM in master mode.
  1669. * @param htim TIM handle.
  1670. * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
  1671. * contains the selected trigger output (TRGO) and the Master/Slave
  1672. * mode.
  1673. * @retval HAL status
  1674. */
  1675. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
  1676. const TIM_MasterConfigTypeDef *sMasterConfig)
  1677. {
  1678. uint32_t tmpcr2;
  1679. uint32_t tmpsmcr;
  1680. /* Check the parameters */
  1681. assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
  1682. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1683. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1684. /* Check input state */
  1685. __HAL_LOCK(htim);
  1686. /* Change the handler state */
  1687. htim->State = HAL_TIM_STATE_BUSY;
  1688. /* Get the TIMx CR2 register value */
  1689. tmpcr2 = htim->Instance->CR2;
  1690. /* Get the TIMx SMCR register value */
  1691. tmpsmcr = htim->Instance->SMCR;
  1692. /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
  1693. if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
  1694. {
  1695. /* Check the parameters */
  1696. assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
  1697. /* Clear the MMS2 bits */
  1698. tmpcr2 &= ~TIM_CR2_MMS2;
  1699. /* Select the TRGO2 source*/
  1700. tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
  1701. }
  1702. /* Reset the MMS Bits */
  1703. tmpcr2 &= ~TIM_CR2_MMS;
  1704. /* Select the TRGO source */
  1705. tmpcr2 |= sMasterConfig->MasterOutputTrigger;
  1706. /* Update TIMx CR2 */
  1707. htim->Instance->CR2 = tmpcr2;
  1708. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1709. {
  1710. /* Reset the MSM Bit */
  1711. tmpsmcr &= ~TIM_SMCR_MSM;
  1712. /* Set master mode */
  1713. tmpsmcr |= sMasterConfig->MasterSlaveMode;
  1714. /* Update TIMx SMCR */
  1715. htim->Instance->SMCR = tmpsmcr;
  1716. }
  1717. /* Change the htim state */
  1718. htim->State = HAL_TIM_STATE_READY;
  1719. __HAL_UNLOCK(htim);
  1720. return HAL_OK;
  1721. }
  1722. /**
  1723. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1724. * and the AOE(automatic output enable).
  1725. * @param htim TIM handle
  1726. * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
  1727. * contains the BDTR Register configuration information for the TIM peripheral.
  1728. * @note Interrupts can be generated when an active level is detected on the
  1729. * break input, the break 2 input or the system break input. Break
  1730. * interrupt can be enabled by calling the @ref __HAL_TIM_ENABLE_IT macro.
  1731. * @retval HAL status
  1732. */
  1733. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1734. const TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
  1735. {
  1736. /* Keep this variable initialized to 0 as it is used to configure BDTR register */
  1737. uint32_t tmpbdtr = 0U;
  1738. /* Check the parameters */
  1739. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1740. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1741. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1742. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1743. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1744. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1745. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1746. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
  1747. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1748. assert_param(IS_TIM_BREAK_AFMODE(sBreakDeadTimeConfig->BreakAFMode));
  1749. /* Check input state */
  1750. __HAL_LOCK(htim);
  1751. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1752. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1753. /* Set the BDTR bits */
  1754. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  1755. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  1756. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  1757. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  1758. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  1759. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  1760. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  1761. MODIFY_REG(tmpbdtr, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << TIM_BDTR_BKF_Pos));
  1762. MODIFY_REG(tmpbdtr, TIM_BDTR_BKBID, sBreakDeadTimeConfig->BreakAFMode);
  1763. if (IS_TIM_BKIN2_INSTANCE(htim->Instance))
  1764. {
  1765. /* Check the parameters */
  1766. assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
  1767. assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
  1768. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
  1769. assert_param(IS_TIM_BREAK2_AFMODE(sBreakDeadTimeConfig->Break2AFMode));
  1770. /* Set the BREAK2 input related BDTR bits */
  1771. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2F, (sBreakDeadTimeConfig->Break2Filter << TIM_BDTR_BK2F_Pos));
  1772. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2E, sBreakDeadTimeConfig->Break2State);
  1773. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2P, sBreakDeadTimeConfig->Break2Polarity);
  1774. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2BID, sBreakDeadTimeConfig->Break2AFMode);
  1775. }
  1776. /* Set TIMx_BDTR */
  1777. htim->Instance->BDTR = tmpbdtr;
  1778. __HAL_UNLOCK(htim);
  1779. return HAL_OK;
  1780. }
  1781. /**
  1782. * @brief Configures the break input source.
  1783. * @param htim TIM handle.
  1784. * @param BreakInput Break input to configure
  1785. * This parameter can be one of the following values:
  1786. * @arg TIM_BREAKINPUT_BRK: Timer break input
  1787. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  1788. * @param sBreakInputConfig Break input source configuration
  1789. * @retval HAL status
  1790. */
  1791. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim,
  1792. uint32_t BreakInput,
  1793. const TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
  1794. {
  1795. HAL_StatusTypeDef status = HAL_OK;
  1796. uint32_t tmporx;
  1797. uint32_t bkin_enable_mask;
  1798. uint32_t bkin_polarity_mask;
  1799. uint32_t bkin_enable_bitpos;
  1800. uint32_t bkin_polarity_bitpos;
  1801. /* Check the parameters */
  1802. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1803. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  1804. assert_param(IS_TIM_BREAKINPUTSOURCE(sBreakInputConfig->Source));
  1805. assert_param(IS_TIM_BREAKINPUTSOURCE_STATE(sBreakInputConfig->Enable));
  1806. assert_param(IS_TIM_BREAKINPUTSOURCE_POLARITY(sBreakInputConfig->Polarity));
  1807. /* Check input state */
  1808. __HAL_LOCK(htim);
  1809. switch (sBreakInputConfig->Source)
  1810. {
  1811. case TIM_BREAKINPUTSOURCE_BKIN:
  1812. {
  1813. bkin_enable_mask = TIM1_AF1_BKINE;
  1814. bkin_enable_bitpos = TIM1_AF1_BKINE_Pos;
  1815. bkin_polarity_mask = TIM1_AF1_BKINP;
  1816. bkin_polarity_bitpos = TIM1_AF1_BKINP_Pos;
  1817. break;
  1818. }
  1819. #if defined(COMP1) && defined(COMP2)
  1820. case TIM_BREAKINPUTSOURCE_COMP1:
  1821. {
  1822. bkin_enable_mask = TIM1_AF1_BKCMP1E;
  1823. bkin_enable_bitpos = TIM1_AF1_BKCMP1E_Pos;
  1824. bkin_polarity_mask = TIM1_AF1_BKCMP1P;
  1825. bkin_polarity_bitpos = TIM1_AF1_BKCMP1P_Pos;
  1826. break;
  1827. }
  1828. case TIM_BREAKINPUTSOURCE_COMP2:
  1829. {
  1830. bkin_enable_mask = TIM1_AF1_BKCMP2E;
  1831. bkin_enable_bitpos = TIM1_AF1_BKCMP2E_Pos;
  1832. bkin_polarity_mask = TIM1_AF1_BKCMP2P;
  1833. bkin_polarity_bitpos = TIM1_AF1_BKCMP2P_Pos;
  1834. break;
  1835. }
  1836. #endif /* COMP1 && COMP2 */
  1837. #if defined(COMP3)
  1838. case TIM_BREAKINPUTSOURCE_COMP3:
  1839. {
  1840. bkin_enable_mask = TIM1_AF1_BKCMP3E;
  1841. bkin_enable_bitpos = TIM1_AF1_BKCMP3E_Pos;
  1842. bkin_polarity_mask = TIM1_AF1_BKCMP3P;
  1843. bkin_polarity_bitpos = TIM1_AF1_BKCMP3P_Pos;
  1844. break;
  1845. }
  1846. #endif /* COMP3 */
  1847. default:
  1848. {
  1849. bkin_enable_mask = 0U;
  1850. bkin_polarity_mask = 0U;
  1851. bkin_enable_bitpos = 0U;
  1852. bkin_polarity_bitpos = 0U;
  1853. break;
  1854. }
  1855. }
  1856. switch (BreakInput)
  1857. {
  1858. case TIM_BREAKINPUT_BRK:
  1859. {
  1860. /* Get the TIMx_AF1 register value */
  1861. tmporx = htim->Instance->AF1;
  1862. /* Enable the break input */
  1863. tmporx &= ~bkin_enable_mask;
  1864. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1865. /* Set the break input polarity */
  1866. tmporx &= ~bkin_polarity_mask;
  1867. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  1868. /* Set TIMx_AF1 */
  1869. htim->Instance->AF1 = tmporx;
  1870. break;
  1871. }
  1872. case TIM_BREAKINPUT_BRK2:
  1873. {
  1874. /* Get the TIMx_AF2 register value */
  1875. tmporx = htim->Instance->AF2;
  1876. /* Enable the break input */
  1877. tmporx &= ~bkin_enable_mask;
  1878. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1879. /* Set the break input polarity */
  1880. tmporx &= ~bkin_polarity_mask;
  1881. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  1882. /* Set TIMx_AF2 */
  1883. htim->Instance->AF2 = tmporx;
  1884. break;
  1885. }
  1886. default:
  1887. status = HAL_ERROR;
  1888. break;
  1889. }
  1890. __HAL_UNLOCK(htim);
  1891. return status;
  1892. }
  1893. /**
  1894. * @brief Configures the TIMx Remapping input capabilities.
  1895. * @param htim TIM handle.
  1896. * @param Remap specifies the TIM remapping source.
  1897. * For TIM1, the parameter can take one of the following values:
  1898. * @arg TIM_TIM1_ETR_GPIO: TIM1 ETR is is connected to GPIO
  1899. * @arg TIM_TIM1_ETR_COMP1: TIM1 ETR is connected to COMP1 output
  1900. * @arg TIM_TIM1_ETR_COMP2: TIM1 ETR is connected to COMP2 output
  1901. * @arg TIM_TIM1_ETR_COMP3: TIM1 ETR is connected to COMP3 output (**)
  1902. * @arg TIM_TIM1_ETR_ADC1_AWD1: TIM1 ETR is connected to ADC1 AWD1
  1903. * @arg TIM_TIM1_ETR_ADC1_AWD2: TIM1 ETR is connected to ADC1 AWD2
  1904. * @arg TIM_TIM1_ETR_ADC1_AWD3: TIM1 ETR is connected to ADC1 AWD3
  1905. *
  1906. * For TIM2, the parameter can take one of the following values: (*)
  1907. * @arg TIM_TIM2_ETR_GPIO: TIM2_ETR is connected to GPIO
  1908. * @arg TIM_TIM2_ETR_COMP1: TIM2_ETR is connected to COMP1 output
  1909. * @arg TIM_TIM2_ETR_COMP2: TIM2_ETR is connected to COMP2 output
  1910. * @arg TIM_TIM2_ETR_COMP3: TIM2_ETR is connected to COMP3 output (**)
  1911. * @arg TIM_TIM2_ETR_LSE: TIM2_ETR is connected to LSE
  1912. * @arg TIM_TIM2_ETR_MCO: TIM2_ETR is connected to MCO (**)
  1913. * @arg TIM_TIM2_ETR_MCO2: TIM2_ETR is connected to MCO2 (**)
  1914. *
  1915. * For TIM3, the parameter can take one of the following values:
  1916. * @arg TIM_TIM3_ETR_GPIO TIM3_ETR is connected to GPIO
  1917. * @arg TIM_TIM3_ETR_COMP1 TIM3_ETR is connected to COMP1 output
  1918. * @arg TIM_TIM3_ETR_COMP2 TIM3_ETR is connected to COMP2 output
  1919. * @arg TIM_TIM3_ETR_COMP3 TIM3_ETR is connected to COMP3 output (**)
  1920. *
  1921. * For TIM4, the parameter can take one of the following values:(*)
  1922. * @arg TIM_TIM4_ETR_GPIO TIM4_ETR is connected to GPIO
  1923. * @arg TIM_TIM4_ETR_COMP1 TIM4_ETR is connected to COMP1 output
  1924. * @arg TIM_TIM4_ETR_COMP2 TIM4_ETR is connected to COMP2 output
  1925. * @arg TIM_TIM4_ETR_COMP3 TIM4_ETR is connected to COMP3 output (**)
  1926. *
  1927. * (*) Timer instance not available on all devices \n
  1928. * (**) Value not defined in all devices. \n
  1929. *
  1930. * @retval HAL status
  1931. */
  1932. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1933. {
  1934. /* Check parameters */
  1935. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  1936. assert_param(IS_TIM_REMAP(Remap));
  1937. __HAL_LOCK(htim);
  1938. MODIFY_REG(htim->Instance->AF1, TIM1_AF1_ETRSEL_Msk, Remap);
  1939. __HAL_UNLOCK(htim);
  1940. return HAL_OK;
  1941. }
  1942. /**
  1943. * @brief Select the timer input source
  1944. * @param htim TIM handle.
  1945. * @param Channel specifies the TIM Channel
  1946. * This parameter can be one of the following values:
  1947. * @arg TIM_CHANNEL_1: TI1 input channel
  1948. * @arg TIM_CHANNEL_2: TI2 input channel
  1949. * @arg TIM_CHANNEL_3: TI3 input channel
  1950. * @param TISelection specifies the timer input source
  1951. *
  1952. * For TIM1 this parameter can be one of the following values:
  1953. * @arg TIM_TIM1_TI1_GPIO: TIM1 TI1 is connected to GPIO
  1954. * @arg TIM_TIM1_TI1_COMP1: TIM1 TI1 is connected to COMP1 output
  1955. * @arg TIM_TIM1_TI2_GPIO: TIM1 TI2 is connected to GPIO
  1956. * @arg TIM_TIM1_TI2_COMP2: TIM1 TI2 is connected to COMP2 output
  1957. * @arg TIM_TIM1_TI3_GPIO: TIM1 TI3 is connected to GPIO
  1958. * @arg TIM_TIM1_TI3_COMP3: TIM1 TI3 is connected to COMP3 output (**)
  1959. *
  1960. * For TIM2, the parameter is one of the following values: (*)
  1961. * @arg TIM_TIM2_TI1_GPIO: TIM2 TI1 is connected to GPIO
  1962. * @arg TIM_TIM2_TI1_COMP1: TIM2 TI1 is connected to COMP1 output
  1963. * @arg TIM_TIM2_TI2_GPIO: TIM2 TI2 is connected to GPIO
  1964. * @arg TIM_TIM2_TI2_COMP2: TIM2 TI2 is connected to COMP2 output
  1965. * @arg TIM_TIM2_TI3_GPIO: TIM2 TI3 is connected to GPIO
  1966. * @arg TIM_TIM2_TI3_COMP3: TIM2 TI3 is connected to COMP3 output (**)
  1967. *
  1968. * For TIM3, the parameter is one of the following values:
  1969. * @arg TIM_TIM3_TI1_GPIO: TIM3 TI1 is connected to GPIO
  1970. * @arg TIM_TIM3_TI1_COMP1: TIM3 TI1 is connected to COMP1 output
  1971. * @arg TIM_TIM3_TI2_GPIO: TIM3 TI2 is connected to GPIO
  1972. * @arg TIM_TIM3_TI2_COMP2: TIM3 TI2 is connected to COMP2 output
  1973. * @arg TIM_TIM3_TI3_GPIO: TIM3 TI3 is connected to GPIO
  1974. * @arg TIM_TIM3_TI3_COMP3: TIM3 TI3 is connected to COMP3 output (**)
  1975. *
  1976. * For TIM4, the parameter is one of the following values: (*)
  1977. * @arg TIM_TIM4_TI1_GPIO: TIM4 TI1 is connected to GPIO
  1978. * @arg TIM_TIM4_TI1_COMP1: TIM4 TI1 is connected to COMP1 output
  1979. * @arg TIM_TIM4_TI2_GPIO: TIM4 TI2 is connected to GPIO
  1980. * @arg TIM_TIM4_TI2_COMP2: TIM4 TI2 is connected to COMP2 output
  1981. * @arg TIM_TIM4_TI3_GPIO: TIM4 TI3 is connected to GPIO
  1982. * @arg TIM_TIM4_TI3_COMP3: TIM4 TI3 is connected to COMP3 output
  1983. *
  1984. * For TIM14, the parameter is one of the following values:
  1985. * @arg TIM_TIM14_TI1_GPIO: TIM14 TI1 is connected to GPIO
  1986. * @arg TIM_TIM14_TI1_RTC: TIM14 TI1 is connected to RTC clock
  1987. * @arg TIM_TIM14_TI1_HSE_32: TIM14 TI1 is connected to HSE div 32
  1988. * @arg TIM_TIM14_TI1_MCO: TIM14 TI1 is connected to MCO
  1989. * @arg TIM_TIM14_TI1_MCO2: TIM14 TI1 is connected to MCO2 (**)
  1990. *
  1991. * For TIM15, the parameter is one of the following values:
  1992. * @arg TIM_TIM15_TI1_GPIO: TIM15 TI1 is connected to GPIO
  1993. * @arg TIM_TIM15_TI1_TIM2_CH1: TIM15 TI1 is connected to TIM2 CH1
  1994. * @arg TIM_TIM15_TI1_TIM3_CH1: TIM15 TI1 is connected to TIM3 CH1
  1995. * @arg TIM_TIM15_TI2_GPIO: TIM15 TI2 is connected to GPIO
  1996. * @arg TIM_TIM15_TI2_TIM2_CH2: TIM15 TI2 is connected to TIM2 CH2
  1997. * @arg TIM_TIM15_TI2_TIM3_CH2: TIM15 TI2 is connected to TIM3 CH2
  1998. *
  1999. * For TIM16, the parameter can have the following values:
  2000. * @arg TIM_TIM16_TI1_GPIO: TIM16 TI1 is connected to GPIO
  2001. * @arg TIM_TIM16_TI1_LSI: TIM16 TI1 is connected to LSI
  2002. * @arg TIM_TIM16_TI1_LSE: TIM16 TI1 is connected to LSE
  2003. * @arg TIM_TIM16_TI1_RTC_WAKEUP: TIM16 TI1 is connected to TRC wakeup interrupt
  2004. * @arg TIM_TIM16_TI1_MCO2: TIM16 TI1 is connected to MCO2 (**)
  2005. *
  2006. * For TIM17, the parameter can have the following values:
  2007. * @arg TIM_TIM17_TI1_GPIO: TIM17 TI1 is connected to GPIO
  2008. * @arg TIM_TIM14_TI1_HSI: TIM17 TI1 is connected to HSI (**)
  2009. * @arg TIM_TIM17_TI1_HSE_32: TIM17 TI1 is connected to HSE div 32
  2010. * @arg TIM_TIM17_TI1_MCO: TIM17 TI1 is connected to MCO
  2011. * @arg TIM_TIM17_TI1_MCO2: TIM17 TI1 is connected to MCO2 (**)
  2012. *
  2013. * (*) Timer instance not available on all devices \n
  2014. * (**) Value not defined in all devices. \n
  2015. *
  2016. * @retval HAL status
  2017. */
  2018. HAL_StatusTypeDef HAL_TIMEx_TISelection(TIM_HandleTypeDef *htim, uint32_t TISelection, uint32_t Channel)
  2019. {
  2020. HAL_StatusTypeDef status = HAL_OK;
  2021. /* Check parameters */
  2022. assert_param(IS_TIM_TISEL_INSTANCE(htim->Instance));
  2023. assert_param(IS_TIM_TISEL(TISelection));
  2024. __HAL_LOCK(htim);
  2025. switch (Channel)
  2026. {
  2027. case TIM_CHANNEL_1:
  2028. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI1SEL, TISelection);
  2029. break;
  2030. case TIM_CHANNEL_2:
  2031. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI2SEL, TISelection);
  2032. break;
  2033. case TIM_CHANNEL_3:
  2034. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI3SEL, TISelection);
  2035. break;
  2036. default:
  2037. status = HAL_ERROR;
  2038. break;
  2039. }
  2040. __HAL_UNLOCK(htim);
  2041. return status;
  2042. }
  2043. /**
  2044. * @brief Group channel 5 and channel 1, 2 or 3
  2045. * @param htim TIM handle.
  2046. * @param Channels specifies the reference signal(s) the OC5REF is combined with.
  2047. * This parameter can be any combination of the following values:
  2048. * TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
  2049. * TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
  2050. * TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
  2051. * TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
  2052. * @retval HAL status
  2053. */
  2054. HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t Channels)
  2055. {
  2056. /* Check parameters */
  2057. assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
  2058. assert_param(IS_TIM_GROUPCH5(Channels));
  2059. /* Process Locked */
  2060. __HAL_LOCK(htim);
  2061. htim->State = HAL_TIM_STATE_BUSY;
  2062. /* Clear GC5Cx bit fields */
  2063. htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3 | TIM_CCR5_GC5C2 | TIM_CCR5_GC5C1);
  2064. /* Set GC5Cx bit fields */
  2065. htim->Instance->CCR5 |= Channels;
  2066. /* Change the htim state */
  2067. htim->State = HAL_TIM_STATE_READY;
  2068. __HAL_UNLOCK(htim);
  2069. return HAL_OK;
  2070. }
  2071. /**
  2072. * @brief Disarm the designated break input (when it operates in bidirectional mode).
  2073. * @param htim TIM handle.
  2074. * @param BreakInput Break input to disarm
  2075. * This parameter can be one of the following values:
  2076. * @arg TIM_BREAKINPUT_BRK: Timer break input
  2077. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  2078. * @note The break input can be disarmed only when it is configured in
  2079. * bidirectional mode and when when MOE is reset.
  2080. * @note Purpose is to be able to have the input voltage back to high-state,
  2081. * whatever the time constant on the output .
  2082. * @retval HAL status
  2083. */
  2084. HAL_StatusTypeDef HAL_TIMEx_DisarmBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput)
  2085. {
  2086. HAL_StatusTypeDef status = HAL_OK;
  2087. uint32_t tmpbdtr;
  2088. /* Check the parameters */
  2089. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2090. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  2091. switch (BreakInput)
  2092. {
  2093. case TIM_BREAKINPUT_BRK:
  2094. {
  2095. /* Check initial conditions */
  2096. tmpbdtr = READ_REG(htim->Instance->BDTR);
  2097. if ((READ_BIT(tmpbdtr, TIM_BDTR_BKBID) == TIM_BDTR_BKBID) &&
  2098. (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
  2099. {
  2100. /* Break input BRK is disarmed */
  2101. SET_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM);
  2102. }
  2103. break;
  2104. }
  2105. case TIM_BREAKINPUT_BRK2:
  2106. {
  2107. /* Check initial conditions */
  2108. tmpbdtr = READ_REG(htim->Instance->BDTR);
  2109. if ((READ_BIT(tmpbdtr, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID) &&
  2110. (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
  2111. {
  2112. /* Break input BRK is disarmed */
  2113. SET_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM);
  2114. }
  2115. break;
  2116. }
  2117. default:
  2118. status = HAL_ERROR;
  2119. break;
  2120. }
  2121. return status;
  2122. }
  2123. /**
  2124. * @brief Arm the designated break input (when it operates in bidirectional mode).
  2125. * @param htim TIM handle.
  2126. * @param BreakInput Break input to arm
  2127. * This parameter can be one of the following values:
  2128. * @arg TIM_BREAKINPUT_BRK: Timer break input
  2129. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  2130. * @note Arming is possible at anytime, even if fault is present.
  2131. * @note Break input is automatically armed as soon as MOE bit is set.
  2132. * @retval HAL status
  2133. */
  2134. HAL_StatusTypeDef HAL_TIMEx_ReArmBreakInput(const TIM_HandleTypeDef *htim, uint32_t BreakInput)
  2135. {
  2136. HAL_StatusTypeDef status = HAL_OK;
  2137. uint32_t tickstart;
  2138. /* Check the parameters */
  2139. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2140. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  2141. switch (BreakInput)
  2142. {
  2143. case TIM_BREAKINPUT_BRK:
  2144. {
  2145. /* Check initial conditions */
  2146. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKBID) == TIM_BDTR_BKBID)
  2147. {
  2148. /* Break input BRK is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
  2149. /* Init tickstart for timeout management */
  2150. tickstart = HAL_GetTick();
  2151. while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
  2152. {
  2153. if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
  2154. {
  2155. /* New check to avoid false timeout detection in case of preemption */
  2156. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
  2157. {
  2158. return HAL_TIMEOUT;
  2159. }
  2160. }
  2161. }
  2162. }
  2163. break;
  2164. }
  2165. case TIM_BREAKINPUT_BRK2:
  2166. {
  2167. /* Check initial conditions */
  2168. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID)
  2169. {
  2170. /* Break input BRK2 is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
  2171. /* Init tickstart for timeout management */
  2172. tickstart = HAL_GetTick();
  2173. while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
  2174. {
  2175. if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
  2176. {
  2177. /* New check to avoid false timeout detection in case of preemption */
  2178. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
  2179. {
  2180. return HAL_TIMEOUT;
  2181. }
  2182. }
  2183. }
  2184. }
  2185. break;
  2186. }
  2187. default:
  2188. status = HAL_ERROR;
  2189. break;
  2190. }
  2191. return status;
  2192. }
  2193. /**
  2194. * @}
  2195. */
  2196. /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
  2197. * @brief Extended Callbacks functions
  2198. *
  2199. @verbatim
  2200. ==============================================================================
  2201. ##### Extended Callbacks functions #####
  2202. ==============================================================================
  2203. [..]
  2204. This section provides Extended TIM callback functions:
  2205. (+) Timer Commutation callback
  2206. (+) Timer Break callback
  2207. @endverbatim
  2208. * @{
  2209. */
  2210. /**
  2211. * @brief Commutation callback in non-blocking mode
  2212. * @param htim TIM handle
  2213. * @retval None
  2214. */
  2215. __weak void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim)
  2216. {
  2217. /* Prevent unused argument(s) compilation warning */
  2218. UNUSED(htim);
  2219. /* NOTE : This function should not be modified, when the callback is needed,
  2220. the HAL_TIMEx_CommutCallback could be implemented in the user file
  2221. */
  2222. }
  2223. /**
  2224. * @brief Commutation half complete callback in non-blocking mode
  2225. * @param htim TIM handle
  2226. * @retval None
  2227. */
  2228. __weak void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim)
  2229. {
  2230. /* Prevent unused argument(s) compilation warning */
  2231. UNUSED(htim);
  2232. /* NOTE : This function should not be modified, when the callback is needed,
  2233. the HAL_TIMEx_CommutHalfCpltCallback could be implemented in the user file
  2234. */
  2235. }
  2236. /**
  2237. * @brief Break detection callback in non-blocking mode
  2238. * @param htim TIM handle
  2239. * @retval None
  2240. */
  2241. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  2242. {
  2243. /* Prevent unused argument(s) compilation warning */
  2244. UNUSED(htim);
  2245. /* NOTE : This function should not be modified, when the callback is needed,
  2246. the HAL_TIMEx_BreakCallback could be implemented in the user file
  2247. */
  2248. }
  2249. /**
  2250. * @brief Break2 detection callback in non blocking mode
  2251. * @param htim: TIM handle
  2252. * @retval None
  2253. */
  2254. __weak void HAL_TIMEx_Break2Callback(TIM_HandleTypeDef *htim)
  2255. {
  2256. /* Prevent unused argument(s) compilation warning */
  2257. UNUSED(htim);
  2258. /* NOTE : This function Should not be modified, when the callback is needed,
  2259. the HAL_TIMEx_Break2Callback could be implemented in the user file
  2260. */
  2261. }
  2262. /**
  2263. * @}
  2264. */
  2265. /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
  2266. * @brief Extended Peripheral State functions
  2267. *
  2268. @verbatim
  2269. ==============================================================================
  2270. ##### Extended Peripheral State functions #####
  2271. ==============================================================================
  2272. [..]
  2273. This subsection permits to get in run-time the status of the peripheral
  2274. and the data flow.
  2275. @endverbatim
  2276. * @{
  2277. */
  2278. /**
  2279. * @brief Return the TIM Hall Sensor interface handle state.
  2280. * @param htim TIM Hall Sensor handle
  2281. * @retval HAL state
  2282. */
  2283. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(const TIM_HandleTypeDef *htim)
  2284. {
  2285. return htim->State;
  2286. }
  2287. /**
  2288. * @brief Return actual state of the TIM complementary channel.
  2289. * @param htim TIM handle
  2290. * @param ChannelN TIM Complementary channel
  2291. * This parameter can be one of the following values:
  2292. * @arg TIM_CHANNEL_1: TIM Channel 1
  2293. * @arg TIM_CHANNEL_2: TIM Channel 2
  2294. * @arg TIM_CHANNEL_3: TIM Channel 3
  2295. * @retval TIM Complementary channel state
  2296. */
  2297. HAL_TIM_ChannelStateTypeDef HAL_TIMEx_GetChannelNState(const TIM_HandleTypeDef *htim, uint32_t ChannelN)
  2298. {
  2299. HAL_TIM_ChannelStateTypeDef channel_state;
  2300. /* Check the parameters */
  2301. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, ChannelN));
  2302. channel_state = TIM_CHANNEL_N_STATE_GET(htim, ChannelN);
  2303. return channel_state;
  2304. }
  2305. /**
  2306. * @}
  2307. */
  2308. /**
  2309. * @}
  2310. */
  2311. /* Private functions ---------------------------------------------------------*/
  2312. /** @defgroup TIMEx_Private_Functions TIM Extended Private Functions
  2313. * @{
  2314. */
  2315. /**
  2316. * @brief TIM DMA Commutation callback.
  2317. * @param hdma pointer to DMA handle.
  2318. * @retval None
  2319. */
  2320. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  2321. {
  2322. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2323. /* Change the htim state */
  2324. htim->State = HAL_TIM_STATE_READY;
  2325. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2326. htim->CommutationCallback(htim);
  2327. #else
  2328. HAL_TIMEx_CommutCallback(htim);
  2329. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2330. }
  2331. /**
  2332. * @brief TIM DMA Commutation half complete callback.
  2333. * @param hdma pointer to DMA handle.
  2334. * @retval None
  2335. */
  2336. void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma)
  2337. {
  2338. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2339. /* Change the htim state */
  2340. htim->State = HAL_TIM_STATE_READY;
  2341. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2342. htim->CommutationHalfCpltCallback(htim);
  2343. #else
  2344. HAL_TIMEx_CommutHalfCpltCallback(htim);
  2345. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2346. }
  2347. /**
  2348. * @brief TIM DMA Delay Pulse complete callback (complementary channel).
  2349. * @param hdma pointer to DMA handle.
  2350. * @retval None
  2351. */
  2352. static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma)
  2353. {
  2354. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2355. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  2356. {
  2357. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2358. if (hdma->Init.Mode == DMA_NORMAL)
  2359. {
  2360. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2361. }
  2362. }
  2363. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  2364. {
  2365. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2366. if (hdma->Init.Mode == DMA_NORMAL)
  2367. {
  2368. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2369. }
  2370. }
  2371. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  2372. {
  2373. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2374. if (hdma->Init.Mode == DMA_NORMAL)
  2375. {
  2376. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  2377. }
  2378. }
  2379. else
  2380. {
  2381. /* nothing to do */
  2382. }
  2383. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2384. htim->PWM_PulseFinishedCallback(htim);
  2385. #else
  2386. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2387. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2388. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2389. }
  2390. /**
  2391. * @brief TIM DMA error callback (complementary channel)
  2392. * @param hdma pointer to DMA handle.
  2393. * @retval None
  2394. */
  2395. static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma)
  2396. {
  2397. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2398. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  2399. {
  2400. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2401. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2402. }
  2403. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  2404. {
  2405. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2406. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2407. }
  2408. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  2409. {
  2410. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2411. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  2412. }
  2413. else
  2414. {
  2415. /* nothing to do */
  2416. }
  2417. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2418. htim->ErrorCallback(htim);
  2419. #else
  2420. HAL_TIM_ErrorCallback(htim);
  2421. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2422. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2423. }
  2424. /**
  2425. * @brief Enables or disables the TIM Capture Compare Channel xN.
  2426. * @param TIMx to select the TIM peripheral
  2427. * @param Channel specifies the TIM Channel
  2428. * This parameter can be one of the following values:
  2429. * @arg TIM_CHANNEL_1: TIM Channel 1
  2430. * @arg TIM_CHANNEL_2: TIM Channel 2
  2431. * @arg TIM_CHANNEL_3: TIM Channel 3
  2432. * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
  2433. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  2434. * @retval None
  2435. */
  2436. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState)
  2437. {
  2438. uint32_t tmp;
  2439. tmp = TIM_CCER_CC1NE << (Channel & 0xFU); /* 0xFU = 15 bits max shift */
  2440. /* Reset the CCxNE Bit */
  2441. TIMx->CCER &= ~tmp;
  2442. /* Set or reset the CCxNE Bit */
  2443. TIMx->CCER |= (uint32_t)(ChannelNState << (Channel & 0xFU)); /* 0xFU = 15 bits max shift */
  2444. }
  2445. /**
  2446. * @}
  2447. */
  2448. #endif /* HAL_TIM_MODULE_ENABLED */
  2449. /**
  2450. * @}
  2451. */
  2452. /**
  2453. * @}
  2454. */