stm32l4xx_hal_tim_ex.c 99 KB

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