stm32h7xx_hal_tim_ex.c 103 KB

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