stm32wbxx_hal_tim_ex.c 94 KB

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