stm32h5xx_hal_tim_ex.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_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. * + Timer encoder index configuration
  16. ******************************************************************************
  17. * @attention
  18. *
  19. * Copyright (c) 2023 STMicroelectronics.
  20. * All rights reserved.
  21. *
  22. * This software is licensed under terms that can be found in the LICENSE file
  23. * in the root directory of this software component.
  24. * If no LICENSE file comes with this software, it is provided AS-IS.
  25. *
  26. ******************************************************************************
  27. @verbatim
  28. ==============================================================================
  29. ##### TIMER Extended features #####
  30. ==============================================================================
  31. [..]
  32. The Timer Extended features include:
  33. (#) Complementary outputs with programmable dead-time for :
  34. (++) Output Compare
  35. (++) PWM generation (Edge and Center-aligned Mode)
  36. (++) One-pulse mode output
  37. (#) Synchronization circuit to control the timer with external signals and to
  38. interconnect several timers together.
  39. (#) Break input to put the timer output signals in reset state or in a known state.
  40. (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
  41. positioning purposes
  42. (#) In case of Pulse on compare, configure pulse length and delay
  43. (#) Encoder index configuration
  44. ##### How to use this driver #####
  45. ==============================================================================
  46. [..]
  47. (#) Initialize the TIM low level resources by implementing the following functions
  48. depending on the selected feature:
  49. (++) Hall Sensor output : HAL_TIMEx_HallSensor_MspInit()
  50. (#) Initialize the TIM low level resources :
  51. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  52. (##) TIM pins configuration
  53. (+++) Enable the clock for the TIM GPIOs using the following function:
  54. __HAL_RCC_GPIOx_CLK_ENABLE();
  55. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  56. (#) The external Clock can be configured, if needed (the default clock is the
  57. internal clock from the APBx), using the following function:
  58. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  59. any start function.
  60. (#) Configure the TIM in the desired functioning mode using one of the
  61. initialization function of this driver:
  62. (++) HAL_TIMEx_HallSensor_Init() and HAL_TIMEx_ConfigCommutEvent(): to use the
  63. Timer Hall Sensor Interface and the commutation event with the corresponding
  64. Interrupt and DMA request if needed (Note that One Timer is used to interface
  65. with the Hall sensor Interface and another Timer should be used to use
  66. the commutation event).
  67. (#) In case of Pulse On Compare:
  68. (++) HAL_TIMEx_OC_ConfigPulseOnCompare(): to configure pulse width and prescaler
  69. (#) Activate the TIM peripheral using one of the start functions:
  70. (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(),
  71. HAL_TIMEx_OCN_Start_IT()
  72. (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(),
  73. HAL_TIMEx_PWMN_Start_IT()
  74. (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
  75. (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(),
  76. HAL_TIMEx_HallSensor_Start_IT().
  77. @endverbatim
  78. ******************************************************************************
  79. */
  80. /* Includes ------------------------------------------------------------------*/
  81. #include "stm32h5xx_hal.h"
  82. /** @addtogroup STM32H5xx_HAL_Driver
  83. * @{
  84. */
  85. /** @defgroup TIMEx TIMEx
  86. * @brief TIM Extended HAL module driver
  87. * @{
  88. */
  89. #ifdef HAL_TIM_MODULE_ENABLED
  90. /* Private typedef -----------------------------------------------------------*/
  91. /* Private define ------------------------------------------------------------*/
  92. /* Private constants ---------------------------------------------------------*/
  93. /** @defgroup TIMEx_Private_Constants TIM Extended Private Constants
  94. * @{
  95. */
  96. /* Timeout for break input rearm */
  97. #define TIM_BREAKINPUT_REARM_TIMEOUT 5UL /* 5 milliseconds */
  98. /**
  99. * @}
  100. */
  101. /* End of private constants --------------------------------------------------*/
  102. /* Private macros ------------------------------------------------------------*/
  103. /* Private variables ---------------------------------------------------------*/
  104. /* Private function prototypes -----------------------------------------------*/
  105. static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma);
  106. static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma);
  107. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState);
  108. /* Exported functions --------------------------------------------------------*/
  109. /** @defgroup TIMEx_Exported_Functions TIM Extended Exported Functions
  110. * @{
  111. */
  112. /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions
  113. * @brief Timer Hall Sensor functions
  114. *
  115. @verbatim
  116. ==============================================================================
  117. ##### Timer Hall Sensor functions #####
  118. ==============================================================================
  119. [..]
  120. This section provides functions allowing to:
  121. (+) Initialize and configure TIM HAL Sensor.
  122. (+) De-initialize TIM HAL Sensor.
  123. (+) Start the Hall Sensor Interface.
  124. (+) Stop the Hall Sensor Interface.
  125. (+) Start the Hall Sensor Interface and enable interrupts.
  126. (+) Stop the Hall Sensor Interface and disable interrupts.
  127. (+) Start the Hall Sensor Interface and enable DMA transfers.
  128. (+) Stop the Hall Sensor Interface and disable DMA transfers.
  129. @endverbatim
  130. * @{
  131. */
  132. /**
  133. * @brief Initializes the TIM Hall Sensor Interface and initialize the associated handle.
  134. * @note When the timer instance is initialized in Hall Sensor Interface mode,
  135. * timer channels 1 and channel 2 are reserved and cannot be used for
  136. * other purpose.
  137. * @param htim TIM Hall Sensor Interface handle
  138. * @param sConfig TIM Hall Sensor configuration structure
  139. * @retval HAL status
  140. */
  141. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, const TIM_HallSensor_InitTypeDef *sConfig)
  142. {
  143. TIM_OC_InitTypeDef OC_Config;
  144. /* Check the TIM handle allocation */
  145. if (htim == NULL)
  146. {
  147. return HAL_ERROR;
  148. }
  149. /* Check the parameters */
  150. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  151. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  152. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  153. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  154. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  155. assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
  156. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  157. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  158. if (htim->State == HAL_TIM_STATE_RESET)
  159. {
  160. /* Allocate lock resource and initialize it */
  161. htim->Lock = HAL_UNLOCKED;
  162. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  163. /* Reset interrupt callbacks to legacy week callbacks */
  164. TIM_ResetCallback(htim);
  165. if (htim->HallSensor_MspInitCallback == NULL)
  166. {
  167. htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit;
  168. }
  169. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  170. htim->HallSensor_MspInitCallback(htim);
  171. #else
  172. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  173. HAL_TIMEx_HallSensor_MspInit(htim);
  174. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  175. }
  176. /* Set the TIM state */
  177. htim->State = HAL_TIM_STATE_BUSY;
  178. /* Configure the Time base in the Encoder Mode */
  179. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  180. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  181. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  182. /* Reset the IC1PSC Bits */
  183. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  184. /* Set the IC1PSC value */
  185. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  186. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  187. htim->Instance->CR2 |= TIM_CR2_TI1S;
  188. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  189. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  190. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  191. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  192. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  193. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  194. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  195. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  196. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  197. OC_Config.OCMode = TIM_OCMODE_PWM2;
  198. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  199. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  200. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  201. OC_Config.Pulse = sConfig->Commutation_Delay;
  202. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  203. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  204. register to 101 */
  205. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  206. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  207. /* Initialize the DMA burst operation state */
  208. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  209. /* Initialize the TIM channels state */
  210. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  211. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  212. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  213. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  214. /* Initialize the TIM state*/
  215. htim->State = HAL_TIM_STATE_READY;
  216. return HAL_OK;
  217. }
  218. /**
  219. * @brief DeInitializes the TIM Hall Sensor interface
  220. * @param htim TIM Hall Sensor Interface handle
  221. * @retval HAL status
  222. */
  223. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  224. {
  225. /* Check the parameters */
  226. assert_param(IS_TIM_INSTANCE(htim->Instance));
  227. htim->State = HAL_TIM_STATE_BUSY;
  228. /* Disable the TIM Peripheral Clock */
  229. __HAL_TIM_DISABLE(htim);
  230. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  231. if (htim->HallSensor_MspDeInitCallback == NULL)
  232. {
  233. htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit;
  234. }
  235. /* DeInit the low level hardware */
  236. htim->HallSensor_MspDeInitCallback(htim);
  237. #else
  238. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  239. HAL_TIMEx_HallSensor_MspDeInit(htim);
  240. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  241. /* Change the DMA burst operation state */
  242. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  243. /* Change the TIM channels state */
  244. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
  245. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
  246. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
  247. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
  248. /* Change TIM state */
  249. htim->State = HAL_TIM_STATE_RESET;
  250. /* Release Lock */
  251. __HAL_UNLOCK(htim);
  252. return HAL_OK;
  253. }
  254. /**
  255. * @brief Initializes the TIM Hall Sensor MSP.
  256. * @param htim TIM Hall Sensor Interface handle
  257. * @retval None
  258. */
  259. __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
  260. {
  261. /* Prevent unused argument(s) compilation warning */
  262. UNUSED(htim);
  263. /* NOTE : This function should not be modified, when the callback is needed,
  264. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  265. */
  266. }
  267. /**
  268. * @brief DeInitializes TIM Hall Sensor MSP.
  269. * @param htim TIM Hall Sensor Interface handle
  270. * @retval None
  271. */
  272. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  273. {
  274. /* Prevent unused argument(s) compilation warning */
  275. UNUSED(htim);
  276. /* NOTE : This function should not be modified, when the callback is needed,
  277. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  278. */
  279. }
  280. /**
  281. * @brief Starts the TIM Hall Sensor Interface.
  282. * @param htim TIM Hall Sensor Interface handle
  283. * @retval HAL status
  284. */
  285. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  286. {
  287. uint32_t tmpsmcr;
  288. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  289. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  290. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  291. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  292. /* Check the parameters */
  293. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  294. /* Check the TIM channels state */
  295. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  296. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  297. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  298. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  299. {
  300. return HAL_ERROR;
  301. }
  302. /* Set the TIM channels state */
  303. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  304. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  305. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  306. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  307. /* Enable the Input Capture channel 1
  308. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  309. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  310. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  311. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  312. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  313. {
  314. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  315. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  316. {
  317. __HAL_TIM_ENABLE(htim);
  318. }
  319. }
  320. else
  321. {
  322. __HAL_TIM_ENABLE(htim);
  323. }
  324. /* Return function status */
  325. return HAL_OK;
  326. }
  327. /**
  328. * @brief Stops the TIM Hall sensor Interface.
  329. * @param htim TIM Hall Sensor Interface handle
  330. * @retval HAL status
  331. */
  332. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  333. {
  334. /* Check the parameters */
  335. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  336. /* Disable the Input Capture channels 1, 2 and 3
  337. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  338. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  339. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  340. /* Disable the Peripheral */
  341. __HAL_TIM_DISABLE(htim);
  342. /* Set the TIM channels state */
  343. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  344. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  345. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  346. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  347. /* Return function status */
  348. return HAL_OK;
  349. }
  350. /**
  351. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  352. * @param htim TIM Hall Sensor Interface handle
  353. * @retval HAL status
  354. */
  355. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  356. {
  357. uint32_t tmpsmcr;
  358. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  359. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  360. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  361. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  362. /* Check the parameters */
  363. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  364. /* Check the TIM channels state */
  365. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  366. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  367. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  368. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  369. {
  370. return HAL_ERROR;
  371. }
  372. /* Set the TIM channels state */
  373. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  374. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  375. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  376. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  377. /* Enable the capture compare Interrupts 1 event */
  378. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  379. /* Enable the Input Capture channel 1
  380. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  381. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  382. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  383. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  384. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  385. {
  386. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  387. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  388. {
  389. __HAL_TIM_ENABLE(htim);
  390. }
  391. }
  392. else
  393. {
  394. __HAL_TIM_ENABLE(htim);
  395. }
  396. /* Return function status */
  397. return HAL_OK;
  398. }
  399. /**
  400. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  401. * @param htim TIM Hall Sensor Interface handle
  402. * @retval HAL status
  403. */
  404. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  405. {
  406. /* Check the parameters */
  407. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  408. /* Disable the Input Capture channel 1
  409. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  410. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  411. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  412. /* Disable the capture compare Interrupts event */
  413. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  414. /* Disable the Peripheral */
  415. __HAL_TIM_DISABLE(htim);
  416. /* Set the TIM channels state */
  417. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  418. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  419. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  420. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  421. /* Return function status */
  422. return HAL_OK;
  423. }
  424. /**
  425. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  426. * @param htim TIM Hall Sensor Interface handle
  427. * @param pData The destination Buffer address.
  428. * @param Length The length of data to be transferred from TIM peripheral to memory.
  429. * @retval HAL status
  430. */
  431. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  432. {
  433. uint32_t tmpsmcr;
  434. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  435. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  436. /* Check the parameters */
  437. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  438. /* Set the TIM channel state */
  439. if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
  440. || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY))
  441. {
  442. return HAL_BUSY;
  443. }
  444. else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
  445. && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY))
  446. {
  447. if ((pData == NULL) || (Length == 0U))
  448. {
  449. return HAL_ERROR;
  450. }
  451. else
  452. {
  453. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  454. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  455. }
  456. }
  457. else
  458. {
  459. return HAL_ERROR;
  460. }
  461. /* Enable the Input Capture channel 1
  462. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  463. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  464. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  465. /* Set the DMA Input Capture 1 Callbacks */
  466. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  467. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  468. /* Set the DMA error callback */
  469. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  470. /* Enable the DMA channel for Capture 1*/
  471. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
  472. {
  473. /* Return error status */
  474. return HAL_ERROR;
  475. }
  476. /* Enable the capture compare 1 Interrupt */
  477. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  478. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  479. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  480. {
  481. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  482. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  483. {
  484. __HAL_TIM_ENABLE(htim);
  485. }
  486. }
  487. else
  488. {
  489. __HAL_TIM_ENABLE(htim);
  490. }
  491. /* Return function status */
  492. return HAL_OK;
  493. }
  494. /**
  495. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  496. * @param htim TIM Hall Sensor Interface handle
  497. * @retval HAL status
  498. */
  499. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  500. {
  501. /* Check the parameters */
  502. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  503. /* Disable the Input Capture channel 1
  504. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
  505. TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  506. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  507. /* Disable the capture compare Interrupts 1 event */
  508. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  509. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  510. /* Disable the Peripheral */
  511. __HAL_TIM_DISABLE(htim);
  512. /* Set the TIM channel state */
  513. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  514. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  515. /* Return function status */
  516. return HAL_OK;
  517. }
  518. /**
  519. * @}
  520. */
  521. /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
  522. * @brief Timer Complementary Output Compare functions
  523. *
  524. @verbatim
  525. ==============================================================================
  526. ##### Timer Complementary Output Compare functions #####
  527. ==============================================================================
  528. [..]
  529. This section provides functions allowing to:
  530. (+) Start the Complementary Output Compare/PWM.
  531. (+) Stop the Complementary Output Compare/PWM.
  532. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  533. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  534. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  535. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  536. @endverbatim
  537. * @{
  538. */
  539. /**
  540. * @brief Starts the TIM Output Compare signal generation on the complementary
  541. * output.
  542. * @param htim TIM Output Compare handle
  543. * @param Channel TIM Channel to be enabled
  544. * This parameter can be one of the following values:
  545. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  546. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  547. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  548. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  549. * @retval HAL status
  550. */
  551. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  552. {
  553. uint32_t tmpsmcr;
  554. /* Check the parameters */
  555. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  556. /* Check the TIM complementary channel state */
  557. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  558. {
  559. return HAL_ERROR;
  560. }
  561. /* Set the TIM complementary channel state */
  562. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  563. /* Enable the Capture compare channel N */
  564. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  565. /* Enable the Main Output */
  566. __HAL_TIM_MOE_ENABLE(htim);
  567. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  568. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  569. {
  570. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  571. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  572. {
  573. __HAL_TIM_ENABLE(htim);
  574. }
  575. }
  576. else
  577. {
  578. __HAL_TIM_ENABLE(htim);
  579. }
  580. /* Return function status */
  581. return HAL_OK;
  582. }
  583. /**
  584. * @brief Stops the TIM Output Compare signal generation on the complementary
  585. * output.
  586. * @param htim TIM handle
  587. * @param Channel TIM Channel to be disabled
  588. * This parameter can be one of the following values:
  589. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  590. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  591. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  592. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  593. * @retval HAL status
  594. */
  595. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  596. {
  597. /* Check the parameters */
  598. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  599. /* Disable the Capture compare channel N */
  600. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  601. /* Disable the Main Output */
  602. __HAL_TIM_MOE_DISABLE(htim);
  603. /* Disable the Peripheral */
  604. __HAL_TIM_DISABLE(htim);
  605. /* Set the TIM complementary channel state */
  606. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  607. /* Return function status */
  608. return HAL_OK;
  609. }
  610. /**
  611. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  612. * on the complementary output.
  613. * @param htim TIM OC handle
  614. * @param Channel TIM Channel to be enabled
  615. * This parameter can be one of the following values:
  616. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  617. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  618. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  619. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  620. * @retval HAL status
  621. */
  622. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  623. {
  624. HAL_StatusTypeDef status = HAL_OK;
  625. uint32_t tmpsmcr;
  626. /* Check the parameters */
  627. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  628. /* Check the TIM complementary channel state */
  629. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  630. {
  631. return HAL_ERROR;
  632. }
  633. /* Set the TIM complementary channel state */
  634. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  635. switch (Channel)
  636. {
  637. case TIM_CHANNEL_1:
  638. {
  639. /* Enable the TIM Output Compare interrupt */
  640. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  641. break;
  642. }
  643. case TIM_CHANNEL_2:
  644. {
  645. /* Enable the TIM Output Compare interrupt */
  646. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  647. break;
  648. }
  649. case TIM_CHANNEL_3:
  650. {
  651. /* Enable the TIM Output Compare interrupt */
  652. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  653. break;
  654. }
  655. case TIM_CHANNEL_4:
  656. {
  657. /* Enable the TIM Output Compare interrupt */
  658. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  659. break;
  660. }
  661. default:
  662. status = HAL_ERROR;
  663. break;
  664. }
  665. if (status == HAL_OK)
  666. {
  667. /* Enable the TIM Break interrupt */
  668. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  669. /* Enable the Capture compare channel N */
  670. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  671. /* Enable the Main Output */
  672. __HAL_TIM_MOE_ENABLE(htim);
  673. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  674. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  675. {
  676. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  677. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  678. {
  679. __HAL_TIM_ENABLE(htim);
  680. }
  681. }
  682. else
  683. {
  684. __HAL_TIM_ENABLE(htim);
  685. }
  686. }
  687. /* Return function status */
  688. return status;
  689. }
  690. /**
  691. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  692. * on the complementary output.
  693. * @param htim TIM Output Compare handle
  694. * @param Channel TIM Channel to be disabled
  695. * This parameter can be one of the following values:
  696. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  697. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  698. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  699. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  700. * @retval HAL status
  701. */
  702. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  703. {
  704. HAL_StatusTypeDef status = HAL_OK;
  705. uint32_t tmpccer;
  706. /* Check the parameters */
  707. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  708. switch (Channel)
  709. {
  710. case TIM_CHANNEL_1:
  711. {
  712. /* Disable the TIM Output Compare interrupt */
  713. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  714. break;
  715. }
  716. case TIM_CHANNEL_2:
  717. {
  718. /* Disable the TIM Output Compare interrupt */
  719. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  720. break;
  721. }
  722. case TIM_CHANNEL_3:
  723. {
  724. /* Disable the TIM Output Compare interrupt */
  725. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  726. break;
  727. }
  728. case TIM_CHANNEL_4:
  729. {
  730. /* Disable the TIM Output Compare interrupt */
  731. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  732. break;
  733. }
  734. default:
  735. status = HAL_ERROR;
  736. break;
  737. }
  738. if (status == HAL_OK)
  739. {
  740. /* Disable the Capture compare channel N */
  741. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  742. /* Disable the TIM Break interrupt (only if no more channel is active) */
  743. tmpccer = htim->Instance->CCER;
  744. if ((tmpccer & TIM_CCER_CCxNE_MASK) == (uint32_t)RESET)
  745. {
  746. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  747. }
  748. /* Disable the Main Output */
  749. __HAL_TIM_MOE_DISABLE(htim);
  750. /* Disable the Peripheral */
  751. __HAL_TIM_DISABLE(htim);
  752. /* Set the TIM complementary channel state */
  753. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  754. }
  755. /* Return function status */
  756. return status;
  757. }
  758. /**
  759. * @brief Starts the TIM Output Compare signal generation in DMA mode
  760. * on the complementary output.
  761. * @param htim TIM Output Compare handle
  762. * @param Channel TIM Channel to be enabled
  763. * This parameter can be one of the following values:
  764. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  765. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  766. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  767. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  768. * @param pData The source Buffer address.
  769. * @param Length The length of data to be transferred from memory to TIM peripheral
  770. * @retval HAL status
  771. */
  772. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
  773. uint16_t Length)
  774. {
  775. HAL_StatusTypeDef status = HAL_OK;
  776. uint32_t tmpsmcr;
  777. /* Check the parameters */
  778. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  779. /* Set the TIM complementary channel state */
  780. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
  781. {
  782. return HAL_BUSY;
  783. }
  784. else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
  785. {
  786. if ((pData == NULL) || (Length == 0U))
  787. {
  788. return HAL_ERROR;
  789. }
  790. else
  791. {
  792. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  793. }
  794. }
  795. else
  796. {
  797. return HAL_ERROR;
  798. }
  799. switch (Channel)
  800. {
  801. case TIM_CHANNEL_1:
  802. {
  803. /* Set the DMA compare callbacks */
  804. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  805. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  806. /* Set the DMA error callback */
  807. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
  808. /* Enable the DMA channel */
  809. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
  810. Length) != HAL_OK)
  811. {
  812. /* Return error status */
  813. return HAL_ERROR;
  814. }
  815. /* Enable the TIM Output Compare DMA request */
  816. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  817. break;
  818. }
  819. case TIM_CHANNEL_2:
  820. {
  821. /* Set the DMA compare callbacks */
  822. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  823. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  824. /* Set the DMA error callback */
  825. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
  826. /* Enable the DMA channel */
  827. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
  828. Length) != HAL_OK)
  829. {
  830. /* Return error status */
  831. return HAL_ERROR;
  832. }
  833. /* Enable the TIM Output Compare DMA request */
  834. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  835. break;
  836. }
  837. case TIM_CHANNEL_3:
  838. {
  839. /* Set the DMA compare callbacks */
  840. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  841. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  842. /* Set the DMA error callback */
  843. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
  844. /* Enable the DMA channel */
  845. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
  846. Length) != HAL_OK)
  847. {
  848. /* Return error status */
  849. return HAL_ERROR;
  850. }
  851. /* Enable the TIM Output Compare DMA request */
  852. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  853. break;
  854. }
  855. case TIM_CHANNEL_4:
  856. {
  857. /* Set the DMA compare callbacks */
  858. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  859. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  860. /* Set the DMA error callback */
  861. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAErrorCCxN ;
  862. /* Enable the DMA channel */
  863. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4,
  864. Length) != HAL_OK)
  865. {
  866. /* Return error status */
  867. return HAL_ERROR;
  868. }
  869. /* Enable the TIM Output Compare DMA request */
  870. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  871. break;
  872. }
  873. default:
  874. status = HAL_ERROR;
  875. break;
  876. }
  877. if (status == HAL_OK)
  878. {
  879. /* Enable the Capture compare channel N */
  880. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  881. /* Enable the Main Output */
  882. __HAL_TIM_MOE_ENABLE(htim);
  883. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  884. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  885. {
  886. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  887. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  888. {
  889. __HAL_TIM_ENABLE(htim);
  890. }
  891. }
  892. else
  893. {
  894. __HAL_TIM_ENABLE(htim);
  895. }
  896. }
  897. /* Return function status */
  898. return status;
  899. }
  900. /**
  901. * @brief Stops the TIM Output Compare signal generation in DMA mode
  902. * on the complementary output.
  903. * @param htim TIM Output Compare handle
  904. * @param Channel TIM Channel to be disabled
  905. * This parameter can be one of the following values:
  906. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  907. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  908. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  909. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  910. * @retval HAL status
  911. */
  912. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  913. {
  914. HAL_StatusTypeDef status = HAL_OK;
  915. /* Check the parameters */
  916. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  917. switch (Channel)
  918. {
  919. case TIM_CHANNEL_1:
  920. {
  921. /* Disable the TIM Output Compare DMA request */
  922. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  923. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  924. break;
  925. }
  926. case TIM_CHANNEL_2:
  927. {
  928. /* Disable the TIM Output Compare DMA request */
  929. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  930. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  931. break;
  932. }
  933. case TIM_CHANNEL_3:
  934. {
  935. /* Disable the TIM Output Compare DMA request */
  936. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  937. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  938. break;
  939. }
  940. case TIM_CHANNEL_4:
  941. {
  942. /* Disable the TIM Output Compare interrupt */
  943. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  944. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  945. break;
  946. }
  947. default:
  948. status = HAL_ERROR;
  949. break;
  950. }
  951. if (status == HAL_OK)
  952. {
  953. /* Disable the Capture compare channel N */
  954. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  955. /* Disable the Main Output */
  956. __HAL_TIM_MOE_DISABLE(htim);
  957. /* Disable the Peripheral */
  958. __HAL_TIM_DISABLE(htim);
  959. /* Set the TIM complementary channel state */
  960. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  961. }
  962. /* Return function status */
  963. return status;
  964. }
  965. /**
  966. * @}
  967. */
  968. /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
  969. * @brief Timer Complementary PWM functions
  970. *
  971. @verbatim
  972. ==============================================================================
  973. ##### Timer Complementary PWM functions #####
  974. ==============================================================================
  975. [..]
  976. This section provides functions allowing to:
  977. (+) Start the Complementary PWM.
  978. (+) Stop the Complementary PWM.
  979. (+) Start the Complementary PWM and enable interrupts.
  980. (+) Stop the Complementary PWM and disable interrupts.
  981. (+) Start the Complementary PWM and enable DMA transfers.
  982. (+) Stop the Complementary PWM and disable DMA transfers.
  983. @endverbatim
  984. * @{
  985. */
  986. /**
  987. * @brief Starts the PWM signal generation on the complementary output.
  988. * @param htim TIM handle
  989. * @param Channel TIM Channel to be enabled
  990. * This parameter can be one of the following values:
  991. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  992. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  993. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  994. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  995. * @retval HAL status
  996. */
  997. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  998. {
  999. uint32_t tmpsmcr;
  1000. /* Check the parameters */
  1001. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1002. /* Check the TIM complementary channel state */
  1003. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  1004. {
  1005. return HAL_ERROR;
  1006. }
  1007. /* Set the TIM complementary channel state */
  1008. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1009. /* Enable the complementary PWM output */
  1010. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1011. /* Enable the Main Output */
  1012. __HAL_TIM_MOE_ENABLE(htim);
  1013. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1014. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1015. {
  1016. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1017. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1018. {
  1019. __HAL_TIM_ENABLE(htim);
  1020. }
  1021. }
  1022. else
  1023. {
  1024. __HAL_TIM_ENABLE(htim);
  1025. }
  1026. /* Return function status */
  1027. return HAL_OK;
  1028. }
  1029. /**
  1030. * @brief Stops the PWM signal generation on the complementary output.
  1031. * @param htim TIM handle
  1032. * @param Channel TIM Channel to be disabled
  1033. * This parameter can be one of the following values:
  1034. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1035. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1036. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1037. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1038. * @retval HAL status
  1039. */
  1040. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  1041. {
  1042. /* Check the parameters */
  1043. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1044. /* Disable the complementary PWM output */
  1045. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1046. /* Disable the Main Output */
  1047. __HAL_TIM_MOE_DISABLE(htim);
  1048. /* Disable the Peripheral */
  1049. __HAL_TIM_DISABLE(htim);
  1050. /* Set the TIM complementary channel state */
  1051. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1052. /* Return function status */
  1053. return HAL_OK;
  1054. }
  1055. /**
  1056. * @brief Starts the PWM signal generation in interrupt mode on the
  1057. * complementary output.
  1058. * @param htim TIM handle
  1059. * @param Channel TIM Channel to be disabled
  1060. * This parameter can be one of the following values:
  1061. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1062. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1063. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1064. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1065. * @retval HAL status
  1066. */
  1067. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1068. {
  1069. HAL_StatusTypeDef status = HAL_OK;
  1070. uint32_t tmpsmcr;
  1071. /* Check the parameters */
  1072. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1073. /* Check the TIM complementary channel state */
  1074. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  1075. {
  1076. return HAL_ERROR;
  1077. }
  1078. /* Set the TIM complementary channel state */
  1079. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1080. switch (Channel)
  1081. {
  1082. case TIM_CHANNEL_1:
  1083. {
  1084. /* Enable the TIM Capture/Compare 1 interrupt */
  1085. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1086. break;
  1087. }
  1088. case TIM_CHANNEL_2:
  1089. {
  1090. /* Enable the TIM Capture/Compare 2 interrupt */
  1091. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1092. break;
  1093. }
  1094. case TIM_CHANNEL_3:
  1095. {
  1096. /* Enable the TIM Capture/Compare 3 interrupt */
  1097. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1098. break;
  1099. }
  1100. case TIM_CHANNEL_4:
  1101. {
  1102. /* Enable the TIM Capture/Compare 4 interrupt */
  1103. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1104. break;
  1105. }
  1106. default:
  1107. status = HAL_ERROR;
  1108. break;
  1109. }
  1110. if (status == HAL_OK)
  1111. {
  1112. /* Enable the TIM Break interrupt */
  1113. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  1114. /* Enable the complementary PWM output */
  1115. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1116. /* Enable the Main Output */
  1117. __HAL_TIM_MOE_ENABLE(htim);
  1118. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1119. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1120. {
  1121. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1122. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1123. {
  1124. __HAL_TIM_ENABLE(htim);
  1125. }
  1126. }
  1127. else
  1128. {
  1129. __HAL_TIM_ENABLE(htim);
  1130. }
  1131. }
  1132. /* Return function status */
  1133. return status;
  1134. }
  1135. /**
  1136. * @brief Stops the PWM signal generation in interrupt mode on the
  1137. * complementary output.
  1138. * @param htim TIM handle
  1139. * @param Channel TIM Channel to be disabled
  1140. * This parameter can be one of the following values:
  1141. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1142. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1143. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1144. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1145. * @retval HAL status
  1146. */
  1147. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1148. {
  1149. HAL_StatusTypeDef status = HAL_OK;
  1150. uint32_t tmpccer;
  1151. /* Check the parameters */
  1152. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1153. switch (Channel)
  1154. {
  1155. case TIM_CHANNEL_1:
  1156. {
  1157. /* Disable the TIM Capture/Compare 1 interrupt */
  1158. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1159. break;
  1160. }
  1161. case TIM_CHANNEL_2:
  1162. {
  1163. /* Disable the TIM Capture/Compare 2 interrupt */
  1164. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1165. break;
  1166. }
  1167. case TIM_CHANNEL_3:
  1168. {
  1169. /* Disable the TIM Capture/Compare 3 interrupt */
  1170. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1171. break;
  1172. }
  1173. case TIM_CHANNEL_4:
  1174. {
  1175. /* Disable the TIM Capture/Compare 4 interrupt */
  1176. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1177. break;
  1178. }
  1179. default:
  1180. status = HAL_ERROR;
  1181. break;
  1182. }
  1183. if (status == HAL_OK)
  1184. {
  1185. /* Disable the complementary PWM output */
  1186. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1187. /* Disable the TIM Break interrupt (only if no more channel is active) */
  1188. tmpccer = htim->Instance->CCER;
  1189. if ((tmpccer & TIM_CCER_CCxNE_MASK) == (uint32_t)RESET)
  1190. {
  1191. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  1192. }
  1193. /* Disable the Main Output */
  1194. __HAL_TIM_MOE_DISABLE(htim);
  1195. /* Disable the Peripheral */
  1196. __HAL_TIM_DISABLE(htim);
  1197. /* Set the TIM complementary channel state */
  1198. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1199. }
  1200. /* Return function status */
  1201. return status;
  1202. }
  1203. /**
  1204. * @brief Starts the TIM PWM signal generation in DMA mode on the
  1205. * complementary output
  1206. * @param htim TIM handle
  1207. * @param Channel TIM Channel to be enabled
  1208. * This parameter can be one of the following values:
  1209. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1210. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1211. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1212. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1213. * @param pData The source Buffer address.
  1214. * @param Length The length of data to be transferred from memory to TIM peripheral
  1215. * @retval HAL status
  1216. */
  1217. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
  1218. uint16_t Length)
  1219. {
  1220. HAL_StatusTypeDef status = HAL_OK;
  1221. uint32_t tmpsmcr;
  1222. /* Check the parameters */
  1223. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1224. /* Set the TIM complementary channel state */
  1225. if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
  1226. {
  1227. return HAL_BUSY;
  1228. }
  1229. else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
  1230. {
  1231. if ((pData == NULL) || (Length == 0U))
  1232. {
  1233. return HAL_ERROR;
  1234. }
  1235. else
  1236. {
  1237. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1238. }
  1239. }
  1240. else
  1241. {
  1242. return HAL_ERROR;
  1243. }
  1244. switch (Channel)
  1245. {
  1246. case TIM_CHANNEL_1:
  1247. {
  1248. /* Set the DMA compare callbacks */
  1249. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1250. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1251. /* Set the DMA error callback */
  1252. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1253. /* Enable the DMA channel */
  1254. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
  1255. Length) != HAL_OK)
  1256. {
  1257. /* Return error status */
  1258. return HAL_ERROR;
  1259. }
  1260. /* Enable the TIM Capture/Compare 1 DMA request */
  1261. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1262. break;
  1263. }
  1264. case TIM_CHANNEL_2:
  1265. {
  1266. /* Set the DMA compare callbacks */
  1267. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1268. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1269. /* Set the DMA error callback */
  1270. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1271. /* Enable the DMA channel */
  1272. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
  1273. Length) != HAL_OK)
  1274. {
  1275. /* Return error status */
  1276. return HAL_ERROR;
  1277. }
  1278. /* Enable the TIM Capture/Compare 2 DMA request */
  1279. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1280. break;
  1281. }
  1282. case TIM_CHANNEL_3:
  1283. {
  1284. /* Set the DMA compare callbacks */
  1285. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1286. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1287. /* Set the DMA error callback */
  1288. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1289. /* Enable the DMA channel */
  1290. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
  1291. Length) != HAL_OK)
  1292. {
  1293. /* Return error status */
  1294. return HAL_ERROR;
  1295. }
  1296. /* Enable the TIM Capture/Compare 3 DMA request */
  1297. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1298. break;
  1299. }
  1300. case TIM_CHANNEL_4:
  1301. {
  1302. /* Set the DMA compare callbacks */
  1303. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseNCplt;
  1304. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1305. /* Set the DMA error callback */
  1306. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAErrorCCxN ;
  1307. /* Enable the DMA channel */
  1308. if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4,
  1309. Length) != HAL_OK)
  1310. {
  1311. /* Return error status */
  1312. return HAL_ERROR;
  1313. }
  1314. /* Enable the TIM Capture/Compare 4 DMA request */
  1315. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1316. break;
  1317. }
  1318. default:
  1319. status = HAL_ERROR;
  1320. break;
  1321. }
  1322. if (status == HAL_OK)
  1323. {
  1324. /* Enable the complementary PWM output */
  1325. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1326. /* Enable the Main Output */
  1327. __HAL_TIM_MOE_ENABLE(htim);
  1328. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1329. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1330. {
  1331. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1332. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1333. {
  1334. __HAL_TIM_ENABLE(htim);
  1335. }
  1336. }
  1337. else
  1338. {
  1339. __HAL_TIM_ENABLE(htim);
  1340. }
  1341. }
  1342. /* Return function status */
  1343. return status;
  1344. }
  1345. /**
  1346. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  1347. * output
  1348. * @param htim TIM handle
  1349. * @param Channel TIM Channel to be disabled
  1350. * This parameter can be one of the following values:
  1351. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1352. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1353. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1354. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1355. * @retval HAL status
  1356. */
  1357. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1358. {
  1359. HAL_StatusTypeDef status = HAL_OK;
  1360. /* Check the parameters */
  1361. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1362. switch (Channel)
  1363. {
  1364. case TIM_CHANNEL_1:
  1365. {
  1366. /* Disable the TIM Capture/Compare 1 DMA request */
  1367. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1368. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1369. break;
  1370. }
  1371. case TIM_CHANNEL_2:
  1372. {
  1373. /* Disable the TIM Capture/Compare 2 DMA request */
  1374. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1375. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1376. break;
  1377. }
  1378. case TIM_CHANNEL_3:
  1379. {
  1380. /* Disable the TIM Capture/Compare 3 DMA request */
  1381. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1382. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1383. break;
  1384. }
  1385. case TIM_CHANNEL_4:
  1386. {
  1387. /* Disable the TIM Capture/Compare 4 DMA request */
  1388. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1389. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  1390. break;
  1391. }
  1392. default:
  1393. status = HAL_ERROR;
  1394. break;
  1395. }
  1396. if (status == HAL_OK)
  1397. {
  1398. /* Disable the complementary PWM output */
  1399. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_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 complementary channel state */
  1405. TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1406. }
  1407. /* Return function status */
  1408. return status;
  1409. }
  1410. /**
  1411. * @}
  1412. */
  1413. /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
  1414. * @brief Timer Complementary One Pulse functions
  1415. *
  1416. @verbatim
  1417. ==============================================================================
  1418. ##### Timer Complementary One Pulse functions #####
  1419. ==============================================================================
  1420. [..]
  1421. This section provides functions allowing to:
  1422. (+) Start the Complementary One Pulse generation.
  1423. (+) Stop the Complementary One Pulse.
  1424. (+) Start the Complementary One Pulse and enable interrupts.
  1425. (+) Stop the Complementary One Pulse and disable interrupts.
  1426. @endverbatim
  1427. * @{
  1428. */
  1429. /**
  1430. * @brief Starts the TIM One Pulse signal generation on the complementary
  1431. * output.
  1432. * @note OutputChannel must match the pulse output channel chosen when calling
  1433. * @ref HAL_TIM_OnePulse_ConfigChannel().
  1434. * @param htim TIM One Pulse handle
  1435. * @param OutputChannel pulse output channel to enable
  1436. * This parameter can be one of the following values:
  1437. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1438. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1439. * @retval HAL status
  1440. */
  1441. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1442. {
  1443. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1444. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  1445. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  1446. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  1447. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  1448. /* Check the parameters */
  1449. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1450. /* Check the TIM channels state */
  1451. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  1452. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  1453. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  1454. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  1455. {
  1456. return HAL_ERROR;
  1457. }
  1458. /* Set the TIM channels state */
  1459. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  1460. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  1461. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  1462. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  1463. /* Enable the complementary One Pulse output channel and the Input Capture channel */
  1464. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1465. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
  1466. /* Enable the Main Output */
  1467. __HAL_TIM_MOE_ENABLE(htim);
  1468. /* Return function status */
  1469. return HAL_OK;
  1470. }
  1471. /**
  1472. * @brief Stops the TIM One Pulse signal generation on the complementary
  1473. * output.
  1474. * @note OutputChannel must match the pulse output channel chosen when calling
  1475. * @ref HAL_TIM_OnePulse_ConfigChannel().
  1476. * @param htim TIM One Pulse handle
  1477. * @param OutputChannel pulse output channel to disable
  1478. * This parameter can be one of the following values:
  1479. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1480. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1481. * @retval HAL status
  1482. */
  1483. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1484. {
  1485. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1486. /* Check the parameters */
  1487. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1488. /* Disable the complementary One Pulse output channel and the Input Capture channel */
  1489. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1490. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
  1491. /* Disable the Main Output */
  1492. __HAL_TIM_MOE_DISABLE(htim);
  1493. /* Disable the Peripheral */
  1494. __HAL_TIM_DISABLE(htim);
  1495. /* Set the TIM channels state */
  1496. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  1497. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  1498. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  1499. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  1500. /* Return function status */
  1501. return HAL_OK;
  1502. }
  1503. /**
  1504. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1505. * complementary channel.
  1506. * @note OutputChannel must match the pulse output channel chosen when calling
  1507. * @ref HAL_TIM_OnePulse_ConfigChannel().
  1508. * @param htim TIM One Pulse handle
  1509. * @param OutputChannel pulse output channel to enable
  1510. * This parameter can be one of the following values:
  1511. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1512. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1513. * @retval HAL status
  1514. */
  1515. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1516. {
  1517. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1518. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  1519. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  1520. HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
  1521. HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
  1522. /* Check the parameters */
  1523. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1524. /* Check the TIM channels state */
  1525. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  1526. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  1527. || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  1528. || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  1529. {
  1530. return HAL_ERROR;
  1531. }
  1532. /* Set the TIM channels state */
  1533. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  1534. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  1535. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  1536. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  1537. /* Enable the TIM Capture/Compare 1 interrupt */
  1538. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1539. /* Enable the TIM Capture/Compare 2 interrupt */
  1540. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1541. /* Enable the complementary One Pulse output channel and the Input Capture channel */
  1542. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1543. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
  1544. /* Enable the Main Output */
  1545. __HAL_TIM_MOE_ENABLE(htim);
  1546. /* Return function status */
  1547. return HAL_OK;
  1548. }
  1549. /**
  1550. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1551. * complementary channel.
  1552. * @note OutputChannel must match the pulse output channel chosen when calling
  1553. * @ref HAL_TIM_OnePulse_ConfigChannel().
  1554. * @param htim TIM One Pulse handle
  1555. * @param OutputChannel pulse output channel to disable
  1556. * This parameter can be one of the following values:
  1557. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1558. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1559. * @retval HAL status
  1560. */
  1561. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1562. {
  1563. uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
  1564. /* Check the parameters */
  1565. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1566. /* Disable the TIM Capture/Compare 1 interrupt */
  1567. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1568. /* Disable the TIM Capture/Compare 2 interrupt */
  1569. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1570. /* Disable the complementary One Pulse output channel and the Input Capture channel */
  1571. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1572. TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
  1573. /* Disable the Main Output */
  1574. __HAL_TIM_MOE_DISABLE(htim);
  1575. /* Disable the Peripheral */
  1576. __HAL_TIM_DISABLE(htim);
  1577. /* Set the TIM channels state */
  1578. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  1579. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  1580. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  1581. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  1582. /* Return function status */
  1583. return HAL_OK;
  1584. }
  1585. /**
  1586. * @}
  1587. */
  1588. /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
  1589. * @brief Peripheral Control functions
  1590. *
  1591. @verbatim
  1592. ==============================================================================
  1593. ##### Peripheral Control functions #####
  1594. ==============================================================================
  1595. [..]
  1596. This section provides functions allowing to:
  1597. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1598. (+) Configure Output channels for OC and PWM mode.
  1599. (+) Configure Complementary channels, break features and dead time.
  1600. (+) Configure Master synchronization.
  1601. (+) Configure timer remapping capabilities.
  1602. (+) Select timer input source.
  1603. (+) Enable or disable channel grouping.
  1604. (+) Configure Pulse on compare.
  1605. (+) Configure Encoder index.
  1606. @endverbatim
  1607. * @{
  1608. */
  1609. /**
  1610. * @brief Configure the TIM commutation event sequence.
  1611. * @note This function is mandatory to use the commutation event in order to
  1612. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1613. * the typical use of this feature is with the use of another Timer(interface Timer)
  1614. * configured in Hall sensor interface, this interface Timer will generate the
  1615. * commutation at its TRGO output (connected to Timer used in this function) each time
  1616. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1617. * @param htim TIM handle
  1618. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1619. * This parameter can be one of the following values:
  1620. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1621. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1622. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1623. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1624. * @arg TIM_TS_ITR4: Internal trigger 4 selected
  1625. * @arg TIM_TS_ITR5: Internal trigger 5 selected
  1626. * @arg TIM_TS_ITR6: Internal trigger 6 selected
  1627. * @arg TIM_TS_ITR7: Internal trigger 7 selected
  1628. * @arg TIM_TS_ITR8: Internal trigger 8 selected
  1629. * @arg TIM_TS_ITR9: Internal trigger 9 selected
  1630. * @arg TIM_TS_ITR10: Internal trigger 10 selected
  1631. * @arg TIM_TS_ITR11: Internal trigger 11 selected
  1632. * @arg TIM_TS_ITR12: Internal trigger 12 selected
  1633. * @arg TIM_TS_NONE: No trigger is needed
  1634. * @param CommutationSource the Commutation Event source
  1635. * This parameter can be one of the following values:
  1636. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1637. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1638. * @retval HAL status
  1639. */
  1640. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1641. uint32_t CommutationSource)
  1642. {
  1643. /* Check the parameters */
  1644. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1645. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
  1646. __HAL_LOCK(htim);
  1647. if (CommutationSource == TIM_COMMUTATION_TRGI)
  1648. {
  1649. /* Select the Input trigger */
  1650. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1651. htim->Instance->SMCR |= InputTrigger;
  1652. }
  1653. /* Select the Capture Compare preload feature */
  1654. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1655. /* Select the Commutation event source */
  1656. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1657. htim->Instance->CR2 |= CommutationSource;
  1658. /* Disable Commutation Interrupt */
  1659. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1660. /* Disable Commutation DMA request */
  1661. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1662. __HAL_UNLOCK(htim);
  1663. return HAL_OK;
  1664. }
  1665. /**
  1666. * @brief Configure the TIM commutation event sequence with interrupt.
  1667. * @note This function is mandatory to use the commutation event in order to
  1668. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1669. * the typical use of this feature is with the use of another Timer(interface Timer)
  1670. * configured in Hall sensor interface, this interface Timer will generate the
  1671. * commutation at its TRGO output (connected to Timer used in this function) each time
  1672. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1673. * @param htim TIM handle
  1674. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1675. * This parameter can be one of the following values:
  1676. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1677. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1678. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1679. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1680. * @arg TIM_TS_ITR4: Internal trigger 4 selected
  1681. * @arg TIM_TS_ITR5: Internal trigger 5 selected
  1682. * @arg TIM_TS_ITR6: Internal trigger 6 selected
  1683. * @arg TIM_TS_ITR7: Internal trigger 7 selected
  1684. * @arg TIM_TS_ITR8: Internal trigger 8 selected
  1685. * @arg TIM_TS_ITR9: Internal trigger 9 selected
  1686. * @arg TIM_TS_ITR10: Internal trigger 10 selected
  1687. * @arg TIM_TS_ITR11: Internal trigger 11 selected
  1688. * @arg TIM_TS_ITR12: Internal trigger 12 selected
  1689. * @arg TIM_TS_NONE: No trigger is needed
  1690. * @param CommutationSource the Commutation Event source
  1691. * This parameter can be one of the following values:
  1692. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1693. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1694. * @retval HAL status
  1695. */
  1696. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1697. uint32_t CommutationSource)
  1698. {
  1699. /* Check the parameters */
  1700. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1701. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
  1702. __HAL_LOCK(htim);
  1703. if (CommutationSource == TIM_COMMUTATION_TRGI)
  1704. {
  1705. /* Select the Input trigger */
  1706. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1707. htim->Instance->SMCR |= InputTrigger;
  1708. }
  1709. /* Select the Capture Compare preload feature */
  1710. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1711. /* Select the Commutation event source */
  1712. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1713. htim->Instance->CR2 |= CommutationSource;
  1714. /* Disable Commutation DMA request */
  1715. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1716. /* Enable the Commutation Interrupt */
  1717. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1718. __HAL_UNLOCK(htim);
  1719. return HAL_OK;
  1720. }
  1721. /**
  1722. * @brief Configure the TIM commutation event sequence with DMA.
  1723. * @note This function is mandatory to use the commutation event in order to
  1724. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1725. * the typical use of this feature is with the use of another Timer(interface Timer)
  1726. * configured in Hall sensor interface, this interface Timer will generate the
  1727. * commutation at its TRGO output (connected to Timer used in this function) each time
  1728. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1729. * @note The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1730. * @param htim TIM handle
  1731. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1732. * This parameter can be one of the following values:
  1733. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1734. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1735. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1736. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1737. * @arg TIM_TS_ITR4: Internal trigger 4 selected
  1738. * @arg TIM_TS_ITR5: Internal trigger 5 selected
  1739. * @arg TIM_TS_ITR6: Internal trigger 6 selected
  1740. * @arg TIM_TS_ITR7: Internal trigger 7 selected
  1741. * @arg TIM_TS_ITR8: Internal trigger 8 selected
  1742. * @arg TIM_TS_ITR9: Internal trigger 9 selected
  1743. * @arg TIM_TS_ITR10: Internal trigger 10 selected
  1744. * @arg TIM_TS_ITR11: Internal trigger 11 selected
  1745. * @arg TIM_TS_ITR12: Internal trigger 12 selected
  1746. * @arg TIM_TS_NONE: No trigger is needed
  1747. * @param CommutationSource the Commutation Event source
  1748. * This parameter can be one of the following values:
  1749. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1750. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1751. * @retval HAL status
  1752. */
  1753. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1754. uint32_t CommutationSource)
  1755. {
  1756. /* Check the parameters */
  1757. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1758. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
  1759. __HAL_LOCK(htim);
  1760. if (CommutationSource == TIM_COMMUTATION_TRGI)
  1761. {
  1762. /* Select the Input trigger */
  1763. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1764. htim->Instance->SMCR |= InputTrigger;
  1765. }
  1766. /* Select the Capture Compare preload feature */
  1767. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1768. /* Select the Commutation event source */
  1769. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1770. htim->Instance->CR2 |= CommutationSource;
  1771. /* Enable the Commutation DMA Request */
  1772. /* Set the DMA Commutation Callback */
  1773. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1774. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  1775. /* Set the DMA error callback */
  1776. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1777. /* Disable Commutation Interrupt */
  1778. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1779. /* Enable the Commutation DMA Request */
  1780. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1781. __HAL_UNLOCK(htim);
  1782. return HAL_OK;
  1783. }
  1784. /**
  1785. * @brief Configures the TIM in master mode.
  1786. * @param htim TIM handle.
  1787. * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
  1788. * contains the selected trigger output (TRGO) and the Master/Slave
  1789. * mode.
  1790. * @retval HAL status
  1791. */
  1792. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
  1793. const TIM_MasterConfigTypeDef *sMasterConfig)
  1794. {
  1795. uint32_t tmpcr2;
  1796. uint32_t tmpsmcr;
  1797. /* Check the parameters */
  1798. assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
  1799. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1800. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1801. /* Check input state */
  1802. __HAL_LOCK(htim);
  1803. /* Change the handler state */
  1804. htim->State = HAL_TIM_STATE_BUSY;
  1805. /* Get the TIMx CR2 register value */
  1806. tmpcr2 = htim->Instance->CR2;
  1807. /* Get the TIMx SMCR register value */
  1808. tmpsmcr = htim->Instance->SMCR;
  1809. /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
  1810. if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
  1811. {
  1812. /* Check the parameters */
  1813. assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
  1814. /* Clear the MMS2 bits */
  1815. tmpcr2 &= ~TIM_CR2_MMS2;
  1816. /* Select the TRGO2 source*/
  1817. tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
  1818. }
  1819. /* Reset the MMS Bits */
  1820. tmpcr2 &= ~TIM_CR2_MMS;
  1821. /* Select the TRGO source */
  1822. tmpcr2 |= sMasterConfig->MasterOutputTrigger;
  1823. /* Update TIMx CR2 */
  1824. htim->Instance->CR2 = tmpcr2;
  1825. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1826. {
  1827. /* Reset the MSM Bit */
  1828. tmpsmcr &= ~TIM_SMCR_MSM;
  1829. /* Set master mode */
  1830. tmpsmcr |= sMasterConfig->MasterSlaveMode;
  1831. /* Update TIMx SMCR */
  1832. htim->Instance->SMCR = tmpsmcr;
  1833. }
  1834. /* Change the htim state */
  1835. htim->State = HAL_TIM_STATE_READY;
  1836. __HAL_UNLOCK(htim);
  1837. return HAL_OK;
  1838. }
  1839. /**
  1840. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1841. * and the AOE(automatic output enable).
  1842. * @param htim TIM handle
  1843. * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
  1844. * contains the BDTR Register configuration information for the TIM peripheral.
  1845. * @note Interrupts can be generated when an active level is detected on the
  1846. * break input, the break 2 input or the system break input. Break
  1847. * interrupt can be enabled by calling the @ref __HAL_TIM_ENABLE_IT macro.
  1848. * @retval HAL status
  1849. */
  1850. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1851. const TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
  1852. {
  1853. /* Keep this variable initialized to 0 as it is used to configure BDTR register */
  1854. uint32_t tmpbdtr = 0U;
  1855. /* Check the parameters */
  1856. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1857. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1858. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1859. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1860. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1861. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1862. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1863. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
  1864. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1865. assert_param(IS_TIM_BREAK_AFMODE(sBreakDeadTimeConfig->BreakAFMode));
  1866. /* Check input state */
  1867. __HAL_LOCK(htim);
  1868. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1869. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1870. /* Set the BDTR bits */
  1871. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  1872. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  1873. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  1874. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  1875. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  1876. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  1877. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  1878. MODIFY_REG(tmpbdtr, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << TIM_BDTR_BKF_Pos));
  1879. MODIFY_REG(tmpbdtr, TIM_BDTR_BKBID, sBreakDeadTimeConfig->BreakAFMode);
  1880. if (IS_TIM_BKIN2_INSTANCE(htim->Instance))
  1881. {
  1882. /* Check the parameters */
  1883. assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
  1884. assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
  1885. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
  1886. assert_param(IS_TIM_BREAK2_AFMODE(sBreakDeadTimeConfig->Break2AFMode));
  1887. /* Set the BREAK2 input related BDTR bits */
  1888. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2F, (sBreakDeadTimeConfig->Break2Filter << TIM_BDTR_BK2F_Pos));
  1889. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2E, sBreakDeadTimeConfig->Break2State);
  1890. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2P, sBreakDeadTimeConfig->Break2Polarity);
  1891. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2BID, sBreakDeadTimeConfig->Break2AFMode);
  1892. }
  1893. /* Set TIMx_BDTR */
  1894. htim->Instance->BDTR = tmpbdtr;
  1895. __HAL_UNLOCK(htim);
  1896. return HAL_OK;
  1897. }
  1898. /**
  1899. * @brief Configures the break input source.
  1900. * @param htim TIM handle.
  1901. * @param BreakInput Break input to configure
  1902. * This parameter can be one of the following values:
  1903. * @arg TIM_BREAKINPUT_BRK: Timer break input
  1904. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  1905. * @param sBreakInputConfig Break input source configuration
  1906. * @retval HAL status
  1907. */
  1908. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim,
  1909. uint32_t BreakInput,
  1910. const TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
  1911. {
  1912. HAL_StatusTypeDef status = HAL_OK;
  1913. uint32_t tmporx;
  1914. uint32_t bkin_enable_mask;
  1915. uint32_t bkin_polarity_mask;
  1916. uint32_t bkin_enable_bitpos;
  1917. uint32_t bkin_polarity_bitpos;
  1918. /* Check the parameters */
  1919. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1920. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  1921. assert_param(IS_TIM_BREAKINPUTSOURCE(sBreakInputConfig->Source));
  1922. assert_param(IS_TIM_BREAKINPUTSOURCE_STATE(sBreakInputConfig->Enable));
  1923. assert_param(IS_TIM_BREAKINPUTSOURCE_POLARITY(sBreakInputConfig->Polarity));
  1924. /* Check input state */
  1925. __HAL_LOCK(htim);
  1926. switch (sBreakInputConfig->Source)
  1927. {
  1928. case TIM_BREAKINPUTSOURCE_BKIN:
  1929. {
  1930. bkin_enable_mask = TIM1_AF1_BKINE;
  1931. bkin_enable_bitpos = TIM1_AF1_BKINE_Pos;
  1932. bkin_polarity_mask = TIM1_AF1_BKINP;
  1933. bkin_polarity_bitpos = TIM1_AF1_BKINP_Pos;
  1934. break;
  1935. }
  1936. #if defined(COMP1)
  1937. case TIM_BREAKINPUTSOURCE_COMP1:
  1938. {
  1939. bkin_enable_mask = TIM1_AF1_BKCMP1E;
  1940. bkin_enable_bitpos = TIM1_AF1_BKCMP1E_Pos;
  1941. bkin_polarity_mask = TIM1_AF1_BKCMP1P;
  1942. bkin_polarity_bitpos = TIM1_AF1_BKCMP1P_Pos;
  1943. break;
  1944. }
  1945. #endif /* COMP1 */
  1946. default:
  1947. {
  1948. bkin_enable_mask = 0U;
  1949. bkin_polarity_mask = 0U;
  1950. bkin_enable_bitpos = 0U;
  1951. bkin_polarity_bitpos = 0U;
  1952. break;
  1953. }
  1954. }
  1955. switch (BreakInput)
  1956. {
  1957. case TIM_BREAKINPUT_BRK:
  1958. {
  1959. /* Get the TIMx_AF1 register value */
  1960. tmporx = htim->Instance->AF1;
  1961. /* Enable the break input */
  1962. tmporx &= ~bkin_enable_mask;
  1963. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1964. /* Set the break input polarity */
  1965. tmporx &= ~bkin_polarity_mask;
  1966. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  1967. /* Set TIMx_AF1 */
  1968. htim->Instance->AF1 = tmporx;
  1969. break;
  1970. }
  1971. case TIM_BREAKINPUT_BRK2:
  1972. {
  1973. /* Get the TIMx_AF2 register value */
  1974. tmporx = htim->Instance->AF2;
  1975. /* Enable the break input */
  1976. tmporx &= ~bkin_enable_mask;
  1977. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1978. /* Set the break input polarity */
  1979. tmporx &= ~bkin_polarity_mask;
  1980. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  1981. /* Set TIMx_AF2 */
  1982. htim->Instance->AF2 = tmporx;
  1983. break;
  1984. }
  1985. default:
  1986. status = HAL_ERROR;
  1987. break;
  1988. }
  1989. __HAL_UNLOCK(htim);
  1990. return status;
  1991. }
  1992. /**
  1993. * @brief Configures the TIMx Remapping input capabilities.
  1994. * @param htim TIM handle.
  1995. * @param Remap specifies the TIM remapping source.
  1996. * For TIM1, the parameter can take one of the following values:
  1997. * @arg TIM_TIM1_ETR_GPIO TIM1 ETR is connected to GPIO
  1998. * @arg TIM_TIM1_ETR_COMP1 TIM1 ETR is connected to COMP1 output (*)
  1999. * @arg TIM_TIM1_ETR_COMP2 TIM1 ETR is connected to COMP2 output (*)
  2000. * @arg TIM_TIM1_ETR_ADC1_AWD1 TIM1 ETR is connected to ADC1 AWD1
  2001. * @arg TIM_TIM1_ETR_ADC1_AWD2 TIM1 ETR is connected to ADC1 AWD2
  2002. * @arg TIM_TIM1_ETR_ADC1_AWD3 TIM1 ETR is connected to ADC1 AWD3
  2003. *
  2004. * For TIM2, the parameter can take one of the following values:
  2005. * @arg TIM_TIM2_ETR_GPIO TIM2 ETR is connected to GPIO
  2006. * @arg TIM_TIM2_ETR_COMP1 TIM2 ETR is connected to COMP1 output (*)
  2007. * @arg TIM_TIM2_ETR_COMP2 TIM2 ETR is connected to COMP2 output (*)
  2008. * @arg TIM_TIM2_ETR_LSE TIM2 ETR is connected to LSE
  2009. * @arg TIM_TIM2_ETR_SAI1_FSA TIM2 ETR is connected to SAI1 FSA (*)
  2010. * @arg TIM_TIM2_ETR_SAI1_FSB TIM2 ETR is connected to SAI1 FSB (*)
  2011. * @arg TIM_TIM2_ETR_TIM3_ETR TIM2 ETR is connected to TIM3 ETR pin
  2012. * @arg TIM_TIM2_ETR_TIM4_ETR TIM2 ETR is connected to TIM4 ETR pin (*)
  2013. * @arg TIM_TIM2_ETR_TIM5_ETR TIM2 ETR is connected to TIM5 ETR pin (*)
  2014. * @arg TIM_TIM2_ETR_USB_SOF TIM2 ETR is connected to USB SOF (*)
  2015. * @arg TIM_TIM2_ETR_USBHS_SOF TIM2 ETR is connected to USBHS OTG SOF (*)
  2016. * @arg TIM_TIM2_ETR_USBFS_SOF TIM2 ETR is connected to USBFS OTG SOF (*)
  2017. * @arg TIM_TIM2_ETR_ETH_PPS TIM2 ETR is connected to ETH PPS (*)
  2018. * @arg TIM_TIM2_ETR_PLAY1_OUT0 TIM2 ETR is connected to PLAY1 output 0 (*)
  2019. *
  2020. * For TIM3, the parameter can take one of the following values:
  2021. * @arg TIM_TIM3_ETR_GPIO TIM3 ETR is connected to GPIO
  2022. * @arg TIM_TIM3_ETR_COMP1 TIM3 ETR is connected to COMP1 output (*)
  2023. * @arg TIM_TIM3_ETR_COMP2 TIM3 ETR is connected to COMP2 output (*)
  2024. * @arg TIM_TIM3_ETR_ADC2_AWD1 TIM3 ETR is connected to ADC2 AWD1 (*)
  2025. * @arg TIM_TIM3_ETR_ADC2_AWD2 TIM3 ETR is connected to ADC2 AWD2 (*)
  2026. * @arg TIM_TIM3_ETR_ADC2_AWD3 TIM3 ETR is connected to ADC2 AWD3 (*)
  2027. * @arg TIM_TIM3_ETR_TIM2_ETR TIM3 ETR is connected to TIM2 ETR pin
  2028. * @arg TIM_TIM3_ETR_TIM4_ETR TIM3 ETR is connected to TIM4 ETR pin (*)
  2029. * @arg TIM_TIM3_ETR_TIM5_ETR TIM3 ETR is connected to TIM5 ETR pin (*)
  2030. * @arg TIM_TIM3_ETR_ETH_PPS TIM3 ETR is connected to ETH PPS (*)
  2031. * @arg TIM_TIM3_ETR_PLAY1_OUT0 TIM3 ETR is connected to PLAY1 output 0 (*)
  2032. *
  2033. * For TIM4, the parameter can take one of the following values: (**)
  2034. * @arg TIM_TIM4_ETR_GPIO TIM4 ETR is connected to GPIO
  2035. * @arg TIM_TIM4_ETR_COMP1 TIM4 ETR is connected to COMP1 output (*)
  2036. * @arg TIM_TIM4_ETR_COMP2 TIM4 ETR is connected to COMP2 output (*)
  2037. * @arg TIM_TIM4_ETR_TIM2_ETR TIM4 ETR is connected to TIM2 ETR pin
  2038. * @arg TIM_TIM4_ETR_TIM3_ETR TIM4 ETR is connected to TIM3 ETR pin
  2039. * @arg TIM_TIM4_ETR_TIM5_ETR TIM4 ETR is connected to TIM5 ETR pin
  2040. *
  2041. * For TIM5, the parameter can take one of the following values: (**)
  2042. * @arg TIM_TIM5_ETR_GPIO TIM5 ETR is connected to GPIO
  2043. * @arg TIM_TIM5_ETR_SAI2_FSA TIM5 ETR is connected to SAI2 FSA
  2044. * @arg TIM_TIM5_ETR_SAI2_FSB TIM5 ETR is connected to SAI2 FSB
  2045. * @arg TIM_TIM5_ETR_COMP1 TIM5 ETR is connected to COMP1 output (*)
  2046. * @arg TIM_TIM5_ETR_COMP2 TIM5 ETR is connected to COMP2 output (*)
  2047. * @arg TIM_TIM5_ETR_TIM2_ETR TIM5 ETR is connected to TIM2 ETR pin
  2048. * @arg TIM_TIM5_ETR_TIM3_ETR TIM5 ETR is connected to TIM3 ETR pin
  2049. * @arg TIM_TIM5_ETR_TIM4_ETR TIM5 ETR is connected to TIM4 ETR pin
  2050. * @arg TIM_TIM5_ETR_USB_SOF TIM5 ETR is connected to USB SOF (*)
  2051. * @arg TIM_TIM5_ETR_USBHS_SOF TIM5 ETR is connected to USBHS OTG SOF (*)
  2052. * @arg TIM_TIM5_ETR_USBFS_SOF TIM5 ETR is connected to USBFS OTG SOF (*)
  2053. *
  2054. * For TIM8, the parameter can take one of the following values: (**)
  2055. * @arg TIM_TIM8_ETR_GPIO TIM8 ETR is connected to GPIO
  2056. * @arg TIM_TIM8_ETR_COMP1 TIM8 ETR is connected to COMP1 output (*)
  2057. * @arg TIM_TIM8_ETR_COMP2 TIM8 ETR is connected to COMP2 output (*)
  2058. * @arg TIM_TIM8_ETR_ADC2_AWD1 TIM8 ETR is connected to ADC2 AWD1
  2059. * @arg TIM_TIM8_ETR_ADC2_AWD2 TIM8 ETR is connected to ADC2 AWD2
  2060. * @arg TIM_TIM8_ETR_ADC2_AWD3 TIM8 ETR is connected to ADC2 AWD3
  2061. * @arg TIM_TIM8_ETR_ADC3_AWD1 TIM8 ETR is connected to ADC3 AWD1 (*)
  2062. * @arg TIM_TIM8_ETR_ADC3_AWD2 TIM8 ETR is connected to ADC3 AWD2 (*)
  2063. * @arg TIM_TIM8_ETR_ADC3_AWD3 TIM8 ETR is connected to ADC3 AWD3 (*)
  2064. *
  2065. * (*) Value not defined in all devices.
  2066. * (**) Timer instance not available on all devices. \n
  2067. *
  2068. * @retval HAL status
  2069. */
  2070. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  2071. {
  2072. /* Check parameters */
  2073. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  2074. assert_param(IS_TIM_REMAP(Remap));
  2075. __HAL_LOCK(htim);
  2076. MODIFY_REG(htim->Instance->AF1, TIM1_AF1_ETRSEL_Msk, Remap);
  2077. __HAL_UNLOCK(htim);
  2078. return HAL_OK;
  2079. }
  2080. /**
  2081. * @brief Select the timer input source
  2082. * @param htim TIM handle.
  2083. * @param Channel specifies the TIM Channel
  2084. * This parameter can be one of the following values:
  2085. * @arg TIM_CHANNEL_1: TI1 input channel
  2086. * @arg TIM_CHANNEL_2: TI2 input channel
  2087. * @arg TIM_CHANNEL_4: TI4 input channel
  2088. * @param TISelection parameter of the TIM_TISelectionStruct structure is detailed as follows:
  2089. * For TIM1, the parameter is one of the following values:
  2090. * @arg TIM_TIM1_TI1_GPIO: TIM1 TI1 is connected to GPIO
  2091. * @arg TIM_TIM1_TI1_COMP1: TIM1 TI1 is connected to COMP1 output (*)
  2092. * @arg TIM_TIM1_TI1_COMP2: TIM1 TI1 is connected to COMP2 output (*)
  2093. * @arg TIM_TIM1_TI2_GPIO: TIM1 TI2 is connected to GPIO
  2094. * @arg TIM_TIM1_TI3_GPIO: TIM1 TI3 is connected to GPIO
  2095. * @arg TIM_TIM1_TI4_GPIO: TIM1 TI4 is connected to GPIO
  2096. *
  2097. * For TIM2, the parameter is one of the following values:
  2098. * @arg TIM_TIM2_TI1_GPIO: TIM2 TI1 is connected to GPIO
  2099. * @arg TIM_TIM2_TI1_LSI: TIM2 TI1 is connected to LSI (*)
  2100. * @arg TIM_TIM2_TI1_LSE: TIM2 TI1 is connected to LSE (*)
  2101. * @arg TIM_TIM2_TI1_RTC_WKUP: TIM2 TI2 is connected to RTC_WKUP (*)
  2102. * @arg TIM_TIM2_TI1_TIM3_TI1: TIM2 TI2 is connected to TIM3_TI1 (*)
  2103. * @arg TIM_TIM2_TI1_ETH_PPS TIM2 TI1 is connected to ETH PPS (*)
  2104. * @arg TIM_TIM2_TI1_COMP1 TIM2 TI1 is connected to COMP1 output (*)
  2105. * @arg TIM_TIM2_TI1_COMP2 TIM2 TI1 is connected to COMP2 output (*)
  2106. * @arg TIM_TIM2_TI1_PLAY1_OUT3 TIM2 TI1 is connected to PLAY1 output 3 (*)
  2107. * @arg TIM_TIM2_TI2_GPIO: TIM2 TI2 is connected to GPIO
  2108. * @arg TIM_TIM2_TI2_HSI_1024: TIM2 TI2 is connected to HSI/1024 (*)
  2109. * @arg TIM_TIM2_TI2_CSI_128: TIM2 TI2 is connected to CSI/128 (*)
  2110. * @arg TIM_TIM2_TI2_MCO2: TIM2 TI2 is connected to MCO2 (*)
  2111. * @arg TIM_TIM2_TI2_MCO1: TIM2 TI2 is connected to MCO1 (*)
  2112. * @arg TIM_TIM2_TI2_COMP1: TIM2 TI2 is connected to COMP1 output (*)
  2113. * @arg TIM_TIM2_TI2_COMP2: TIM2 TI2 is connected to COMP2 output (*)
  2114. * @arg TIM_TIM2_TI3_GPIO: TIM2 TI3 is connected to GPIO
  2115. * @arg TIM_TIM2_TI4_GPIO: TIM2 TI4 is connected to GPIO
  2116. * @arg TIM_TIM2_TI4_COMP1: TIM2 TI4 is connected to COMP1 output (*)
  2117. *
  2118. * For TIM3, the parameter is one of the following values:
  2119. * @arg TIM_TIM3_TI1_GPIO: TIM3 TI1 is connected to GPIO
  2120. * @arg TIM_TIM3_TI1_COMP1: TIM3 TI1 is connected to COMP1 output (*)
  2121. * @arg TIM_TIM3_TI1_MCO1: TIM3 TI2 is connected to MCO1 (*)
  2122. * @arg TIM_TIM3_TI1_TIM2_TI1: TIM3 TI2 is connected to TIM2 TI1 (*)
  2123. * @arg TIM_TIM3_TI1_HSE_1MHZ: TIM3 TI2 is connected to HSE_1MHZ (*)
  2124. * @arg TIM_TIM3_TI1_ETH_PPS TIM3 TI1 is connected to ETH PPS (*)
  2125. * @arg TIM_TIM3_TI1_COMP1 TIM3 TI1 is connected to COMP1 output (*)
  2126. * @arg TIM_TIM3_TI1_COMP2 TIM3 TI1 is connected to COMP2 output (*)
  2127. * @arg TIM_TIM3_TI1_PLAY1_OUT3 TIM3 TI1 is connected to PLAY1 output 3 (*)
  2128. * @arg TIM_TIM3_TI2_GPIO: TIM3 TI2 is connected to GPIO
  2129. * @arg TIM_TIM3_TI2_CSI_128: TIM3 TI2 is connected to CSI_128 (*)
  2130. * @arg TIM_TIM3_TI2_MCO2: TIM3 TI2 is connected to MCO2 (*)
  2131. * @arg TIM_TIM3_TI2_HSI_1024: TIM3 TI2 is connected to HSI_1024 (*)
  2132. * @arg TIM_TIM3_TI2_COMP1: TIM3 TI2 is connected to COMP1 output (*)
  2133. * @arg TIM_TIM3_TI2_COMP2: TIM3 TI2 is connected to COMP2 output (*)
  2134. * @arg TIM_TIM3_TI3_GPIO: TIM3 TI2 is connected to GPIO
  2135. * @arg TIM_TIM3_TI4_GPIO: TIM3 TI2 is connected to GPIO
  2136. *
  2137. * For TIM4, the parameter is one of the following values: (**)
  2138. * @arg TIM_TIM4_TI1_GPIO: TIM4 TI1 is connected to GPIO
  2139. * @arg TIM_TIM4_TI1_COMP1 TIM4 TI1 is connected to COMP1 output (*)
  2140. * @arg TIM_TIM4_TI1_COMP2 TIM4 TI1 is connected to COMP2 output (*)
  2141. * @arg TIM_TIM4_TI2_GPIO: TIM4 TI2 is connected to GPIO
  2142. * @arg TIM_TIM4_TI3_GPIO: TIM4 TI3 is connected to GPIO
  2143. * @arg TIM_TIM4_TI4_GPIO: TIM4 TI4 is connected to GPIO
  2144. *
  2145. * For TIM5, the parameter is one of the following values: (**)
  2146. * @arg TIM_TIM5_TI1_GPIO: TIM5 TI1 is connected to GPIO
  2147. * @arg TIM_TIM5_TI1_COMP1 TIM5 TI1 is connected to COMP1 output (*)
  2148. * @arg TIM_TIM5_TI1_COMP2 TIM5 TI1 is connected to COMP2 output (*)
  2149. * @arg TIM_TIM5_TI2_GPIO: TIM5 TI2 is connected to GPIO
  2150. * @arg TIM_TIM5_TI3_GPIO: TIM5 TI3 is connected to GPIO
  2151. * @arg TIM_TIM5_TI4_GPIO: TIM5 TI4 is connected to GPIO
  2152. *
  2153. * For TIM8, the parameter is one of the following values: (**)
  2154. * @arg TIM_TIM8_TI1_GPIO: TIM8 TI1 is connected to GPIO
  2155. * @arg TIM_TIM8_TI1_COMP1 TIM8 TI1 is connected to COMP1 output (*)
  2156. * @arg TIM_TIM8_TI1_COMP2 TIM8 TI1 is connected to COMP2 output (*)
  2157. * @arg TIM_TIM8_TI2_GPIO: TIM8 TI2 is connected to GPIO
  2158. * @arg TIM_TIM8_TI3_GPIO: TIM8 TI3 is connected to GPIO
  2159. * @arg TIM_TIM8_TI4_GPIO: TIM8 TI4 is connected to GPIO
  2160. *
  2161. * For TIM12, the parameter is one of the following values: (**)
  2162. * @arg TIM_TIM12_TI1_GPIO: TIM12 TI1 is connected to GPIO
  2163. * @arg TIM_TIM12_TI1_COMP1 TIM12 TI1 is connected to COMP1 output (*)
  2164. * @arg TIM_TIM12_TI1_COMP2 TIM12 TI1 is connected to COMP2 output (*)
  2165. * @arg TIM_TIM12_TI1_HSI_1024: TIM12 TI1 is connected to HSI/1024
  2166. * @arg TIM_TIM12_TI1_CSI_128: TIM12 TI1 is connected to CSI/128
  2167. * @arg TIM_TIM12_TI2_GPIO: TIM12 TI2 is connected to GPIO
  2168. * @arg TIM_TIM12_TI2_COMP2 TIM12 TI2 is connected to COMP2 output (*)
  2169. *
  2170. * For TIM13, the parameter is one of the following values: (**)
  2171. * @arg TIM_TIM13_TI1_GPIO: TIM13 TI1 is connected to GPIO
  2172. * @arg TIM_TIM13_TI1_I3C1_IBIACK TIM13 TI1 is connected to I3C1 IBI ACK (*)
  2173. * @arg TIM_TIM13_TI1_COMP1 TIM13 TI1 is connected to COMP1 output (*)
  2174. * @arg TIM_TIM13_TI1_COMP2 TIM13 TI1 is connected to COMP2 output (*)
  2175. *
  2176. * For TIM14, the parameter is one of the following values: (**)
  2177. * @arg TIM_TIM14_TI1_GPIO: TIM14 TI1 is connected to GPIO
  2178. * @arg TIM_TIM14_TI1_I3C2_IBIACK TIM14 TI1 is connected to I3C2 IBI ACK (*)
  2179. * @arg TIM_TIM14_TI1_COMP1 TIM14 TI1 is connected to COMP1 output (*)
  2180. * @arg TIM_TIM14_TI1_COMP2 TIM14 TI1 is connected to COMP2 output (*)
  2181. *
  2182. * For TIM15, the parameter can have the following values: (**)
  2183. * @arg TIM_TIM15_TI1_GPIO: TIM15 TI1 is connected to GPIO
  2184. * @arg TIM_TIM15_TI1_TIM2: TIM15 TI1 is connected to TIM2
  2185. * @arg TIM_TIM15_TI1_TIM3: TIM15 TI1 is connected to TIM3
  2186. * @arg TIM_TIM15_TI1_TIM4: TIM15 TI1 is connected to TIM4
  2187. * @arg TIM_TIM15_TI1_LSE: TIM15 TI1 is connected to LSE
  2188. * @arg TIM_TIM15_TI1_CSI_128: TIM15 TI1 is connected to CSI/128
  2189. * @arg TIM_TIM15_TI1_MCO2: TIM15 TI1 is connected to MCO2
  2190. * @arg TIM_TIM15_TI1_COMP1 TIM15 TI1 is connected to COMP1 output (*)
  2191. * @arg TIM_TIM15_TI1_COMP2 TIM15 TI1 is connected to COMP2 output (*)
  2192. * @arg TIM_TIM15_TI2_GPIO: TIM15 TI1 is connected to GPIO
  2193. * @arg TIM_TIM15_TI2_TIM2: TIM15 TI1 is connected to TIM2
  2194. * @arg TIM_TIM15_TI2_TIM3: TIM15 TI1 is connected to TIM3
  2195. * @arg TIM_TIM15_TI2_TIM4: TIM15 TI1 is connected to TIM4
  2196. * @arg TIM_TIM15_TI2_COMP1 TIM15 TI2 is connected to COMP1 output (*)
  2197. * @arg TIM_TIM15_TI2_COMP2 TIM15 TI2 is connected to COMP2 output (*)
  2198. *
  2199. * For TIM16, the parameter is one of the following values: (**)
  2200. * @arg TIM_TIM16_TI1_GPIO: TIM16 TI1 is connected to GPIO
  2201. * @arg TIM_TIM16_TI1_LSI: TIM16 TI1 is connected to LSI
  2202. * @arg TIM_TIM16_TI1_LSE: TIM16 TI1 is connected to LSE
  2203. * @arg TIM_TIM16_TI1_RTC_WKUP: TIM16 TI1 is connected to RTCWKUP
  2204. * @arg TIM_TIM16_TI1_COMP1 TIM16 TI1 is connected to COMP1 output (*)
  2205. * @arg TIM_TIM16_TI1_COMP2 TIM16 TI1 is connected to COMP2 output (*)
  2206. *
  2207. * For TIM17, the parameter can have the following values: (**)
  2208. * @arg TIM_TIM17_TI1_GPIO: TIM17 TI1 is connected to GPIO
  2209. * @arg TIM_TIM17_TI1_HSE_1MHZ: TIM17 TI1 is connected to HSE_1MHZ
  2210. * @arg TIM_TIM17_TI1_MCO1: TIM17 TI1 is connected to MCO1
  2211. * @arg TIM_TIM17_TI1_COMP1 TIM17 TI1 is connected to COMP1 output (*)
  2212. * @arg TIM_TIM17_TI1_COMP2 TIM17 TI1 is connected to COMP2 output (*)
  2213. *
  2214. * (*) Value not defined in all devices. \n
  2215. * (**) Timer instance not available on all devices. \n
  2216. * @retval HAL status
  2217. */
  2218. HAL_StatusTypeDef HAL_TIMEx_TISelection(TIM_HandleTypeDef *htim, uint32_t TISelection, uint32_t Channel)
  2219. {
  2220. HAL_StatusTypeDef status = HAL_OK;
  2221. /* Check parameters */
  2222. assert_param(IS_TIM_TISEL_INSTANCE(htim->Instance));
  2223. assert_param(IS_TIM_TISEL(TISelection));
  2224. __HAL_LOCK(htim);
  2225. switch (Channel)
  2226. {
  2227. case TIM_CHANNEL_1:
  2228. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI1SEL, TISelection);
  2229. #if defined(TIM17)
  2230. /* If required, set OR1 bit to request HSE 1MHz clock */
  2231. if ((IS_TIM_RTCPREEN_INSTANCE(htim->Instance)) && (IS_TIM_RTCPREEN_SELECTION(TISelection)))
  2232. {
  2233. SET_BIT(htim->Instance->OR1, TIM_OR1_RTCPREEN);
  2234. }
  2235. else
  2236. {
  2237. CLEAR_BIT(htim->Instance->OR1, TIM_OR1_RTCPREEN);
  2238. }
  2239. #endif /* TIM17 */
  2240. break;
  2241. case TIM_CHANNEL_2:
  2242. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI2SEL, TISelection);
  2243. break;
  2244. case TIM_CHANNEL_4:
  2245. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI4SEL, TISelection);
  2246. break;
  2247. default:
  2248. status = HAL_ERROR;
  2249. break;
  2250. }
  2251. __HAL_UNLOCK(htim);
  2252. return status;
  2253. }
  2254. /**
  2255. * @brief Group channel 5 and channel 1, 2 or 3
  2256. * @param htim TIM handle.
  2257. * @param Channels specifies the reference signal(s) the OC5REF is combined with.
  2258. * This parameter can be any combination of the following values:
  2259. * TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
  2260. * TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
  2261. * TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
  2262. * TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
  2263. * @retval HAL status
  2264. */
  2265. HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t Channels)
  2266. {
  2267. /* Check parameters */
  2268. assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
  2269. assert_param(IS_TIM_GROUPCH5(Channels));
  2270. /* Process Locked */
  2271. __HAL_LOCK(htim);
  2272. htim->State = HAL_TIM_STATE_BUSY;
  2273. /* Clear GC5Cx bit fields */
  2274. htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3 | TIM_CCR5_GC5C2 | TIM_CCR5_GC5C1);
  2275. /* Set GC5Cx bit fields */
  2276. htim->Instance->CCR5 |= Channels;
  2277. /* Change the htim state */
  2278. htim->State = HAL_TIM_STATE_READY;
  2279. __HAL_UNLOCK(htim);
  2280. return HAL_OK;
  2281. }
  2282. /**
  2283. * @brief Disarm the designated break input (when it operates in bidirectional mode).
  2284. * @param htim TIM handle.
  2285. * @param BreakInput Break input to disarm
  2286. * This parameter can be one of the following values:
  2287. * @arg TIM_BREAKINPUT_BRK: Timer break input
  2288. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  2289. * @note The break input can be disarmed only when it is configured in
  2290. * bidirectional mode and when when MOE is reset.
  2291. * @note Purpose is to be able to have the input voltage back to high-state,
  2292. * whatever the time constant on the output .
  2293. * @retval HAL status
  2294. */
  2295. HAL_StatusTypeDef HAL_TIMEx_DisarmBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput)
  2296. {
  2297. HAL_StatusTypeDef status = HAL_OK;
  2298. uint32_t tmpbdtr;
  2299. /* Check the parameters */
  2300. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2301. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  2302. switch (BreakInput)
  2303. {
  2304. case TIM_BREAKINPUT_BRK:
  2305. {
  2306. /* Check initial conditions */
  2307. tmpbdtr = READ_REG(htim->Instance->BDTR);
  2308. if ((READ_BIT(tmpbdtr, TIM_BDTR_BKBID) == TIM_BDTR_BKBID) &&
  2309. (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
  2310. {
  2311. /* Break input BRK is disarmed */
  2312. SET_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM);
  2313. }
  2314. break;
  2315. }
  2316. case TIM_BREAKINPUT_BRK2:
  2317. {
  2318. /* Check initial conditions */
  2319. tmpbdtr = READ_REG(htim->Instance->BDTR);
  2320. if ((READ_BIT(tmpbdtr, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID) &&
  2321. (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
  2322. {
  2323. /* Break input BRK is disarmed */
  2324. SET_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM);
  2325. }
  2326. break;
  2327. }
  2328. default:
  2329. status = HAL_ERROR;
  2330. break;
  2331. }
  2332. return status;
  2333. }
  2334. /**
  2335. * @brief Arm the designated break input (when it operates in bidirectional mode).
  2336. * @param htim TIM handle.
  2337. * @param BreakInput Break input to arm
  2338. * This parameter can be one of the following values:
  2339. * @arg TIM_BREAKINPUT_BRK: Timer break input
  2340. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  2341. * @note Arming is possible at anytime, even if fault is present.
  2342. * @note Break input is automatically armed as soon as MOE bit is set.
  2343. * @retval HAL status
  2344. */
  2345. HAL_StatusTypeDef HAL_TIMEx_ReArmBreakInput(const TIM_HandleTypeDef *htim, uint32_t BreakInput)
  2346. {
  2347. HAL_StatusTypeDef status = HAL_OK;
  2348. uint32_t tickstart;
  2349. /* Check the parameters */
  2350. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2351. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  2352. switch (BreakInput)
  2353. {
  2354. case TIM_BREAKINPUT_BRK:
  2355. {
  2356. /* Check initial conditions */
  2357. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKBID) == TIM_BDTR_BKBID)
  2358. {
  2359. /* Break input BRK is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
  2360. /* Init tickstart for timeout management */
  2361. tickstart = HAL_GetTick();
  2362. while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
  2363. {
  2364. if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
  2365. {
  2366. /* New check to avoid false timeout detection in case of preemption */
  2367. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
  2368. {
  2369. return HAL_TIMEOUT;
  2370. }
  2371. }
  2372. }
  2373. }
  2374. break;
  2375. }
  2376. case TIM_BREAKINPUT_BRK2:
  2377. {
  2378. /* Check initial conditions */
  2379. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID)
  2380. {
  2381. /* Break input BRK2 is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
  2382. /* Init tickstart for timeout management */
  2383. tickstart = HAL_GetTick();
  2384. while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
  2385. {
  2386. if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
  2387. {
  2388. /* New check to avoid false timeout detection in case of preemption */
  2389. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
  2390. {
  2391. return HAL_TIMEOUT;
  2392. }
  2393. }
  2394. }
  2395. }
  2396. break;
  2397. }
  2398. default:
  2399. status = HAL_ERROR;
  2400. break;
  2401. }
  2402. return status;
  2403. }
  2404. /**
  2405. * @brief Enable dithering
  2406. * @param htim TIM handle
  2407. * @note Main usage is PWM mode
  2408. * @note This function must be called when timer is stopped or disabled (CEN =0)
  2409. * @note If dithering is activated, pay attention to ARR, CCRx, CNT interpretation:
  2410. * - CNT: only CNT[11:0] holds the non-dithered part for 16b timers (or CNT[26:0] for 32b timers)
  2411. * - ARR: ARR[15:4] holds the non-dithered part, and ARR[3:0] the dither part for 16b timers
  2412. * - CCRx: CCRx[15:4] holds the non-dithered part, and CCRx[3:0] the dither part for 16b timers
  2413. * - ARR and CCRx values are limited to 0xFFEF in dithering mode for 16b timers
  2414. * (corresponds to 4094 for the integer part and 15 for the dithered part).
  2415. * @note Macros @ref __HAL_TIM_CALC_PERIOD_DITHER() __HAL_TIM_CALC_DELAY_DITHER() __HAL_TIM_CALC_PULSE_DITHER()
  2416. * can be used to calculate period (ARR) and delay (CCRx) value.
  2417. * @note Enabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
  2418. * @note Enabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
  2419. * So it may be necessary to read ARR value or CCRx value with macros @ref __HAL_TIM_GET_AUTORELOAD()
  2420. * __HAL_TIM_GET_COMPARE() and if necessary update Init structure field htim->Init.Period .
  2421. * @retval HAL status
  2422. */
  2423. HAL_StatusTypeDef HAL_TIMEx_DitheringEnable(TIM_HandleTypeDef *htim)
  2424. {
  2425. /* Check the parameters */
  2426. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2427. SET_BIT(htim->Instance->CR1, TIM_CR1_DITHEN);
  2428. return HAL_OK;
  2429. }
  2430. /**
  2431. * @brief Disable dithering
  2432. * @param htim TIM handle
  2433. * @note This function must be called when timer is stopped or disabled (CEN =0)
  2434. * @note If dithering is activated, pay attention to ARR, CCRx, CNT interpretation:
  2435. * - CNT: only CNT[11:0] holds the non-dithered part for 16b timers (or CNT[26:0] for 32b timers)
  2436. * - ARR: ARR[15:4] holds the non-dithered part, and ARR[3:0] the dither part for 16b timers
  2437. * - CCRx: CCRx[15:4] holds the non-dithered part, and CCRx[3:0] the dither part for 16b timers
  2438. * - ARR and CCRx values are limited to 0xFFEF in dithering mode
  2439. * (corresponds to 4094 for the integer part and 15 for the dithered part).
  2440. * @note Disabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
  2441. * So it may be necessary to read ARR value or CCRx value with macros @ref __HAL_TIM_GET_AUTORELOAD()
  2442. * __HAL_TIM_GET_COMPARE() and if necessary update Init structure field htim->Init.Period .
  2443. * @retval HAL status
  2444. */
  2445. HAL_StatusTypeDef HAL_TIMEx_DitheringDisable(TIM_HandleTypeDef *htim)
  2446. {
  2447. /* Check the parameters */
  2448. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2449. CLEAR_BIT(htim->Instance->CR1, TIM_CR1_DITHEN);
  2450. return HAL_OK;
  2451. }
  2452. /**
  2453. * @brief Initializes the pulse on compare pulse width and pulse prescaler
  2454. * @param htim TIM Output Compare handle
  2455. * @param PulseWidthPrescaler Pulse width prescaler
  2456. * This parameter can be a number between Min_Data = 0x0 and Max_Data = 0x7
  2457. * @param PulseWidth Pulse width
  2458. * This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
  2459. * @retval HAL status
  2460. */
  2461. HAL_StatusTypeDef HAL_TIMEx_OC_ConfigPulseOnCompare(TIM_HandleTypeDef *htim,
  2462. uint32_t PulseWidthPrescaler,
  2463. uint32_t PulseWidth)
  2464. {
  2465. uint32_t tmpecr;
  2466. /* Check the parameters */
  2467. assert_param(IS_TIM_PULSEONCOMPARE_INSTANCE(htim->Instance));
  2468. assert_param(IS_TIM_PULSEONCOMPARE_WIDTH(PulseWidth));
  2469. assert_param(IS_TIM_PULSEONCOMPARE_WIDTHPRESCALER(PulseWidthPrescaler));
  2470. /* Process Locked */
  2471. __HAL_LOCK(htim);
  2472. /* Set the TIM state */
  2473. htim->State = HAL_TIM_STATE_BUSY;
  2474. /* Get the TIMx ECR register value */
  2475. tmpecr = htim->Instance->ECR;
  2476. /* Reset the Pulse width prescaler and the Pulse width */
  2477. tmpecr &= ~(TIM_ECR_PWPRSC | TIM_ECR_PW);
  2478. /* Set the Pulse width prescaler and Pulse width*/
  2479. tmpecr |= PulseWidthPrescaler << TIM_ECR_PWPRSC_Pos;
  2480. tmpecr |= PulseWidth << TIM_ECR_PW_Pos;
  2481. /* Write to TIMx ECR */
  2482. htim->Instance->ECR = tmpecr;
  2483. /* Change the TIM state */
  2484. htim->State = HAL_TIM_STATE_READY;
  2485. /* Release Lock */
  2486. __HAL_UNLOCK(htim);
  2487. return HAL_OK;
  2488. }
  2489. /**
  2490. * @brief Configure preload source of Slave Mode Selection bitfield (SMS in SMCR register)
  2491. * @param htim TIM handle
  2492. * @param Source Source of slave mode selection preload
  2493. * This parameter can be one of the following values:
  2494. * @arg TIM_SMS_PRELOAD_SOURCE_UPDATE: Timer update event is used as source of Slave Mode Selection preload
  2495. * @arg TIM_SMS_PRELOAD_SOURCE_INDEX: Timer index event is used as source of Slave Mode Selection preload
  2496. * @retval HAL status
  2497. */
  2498. HAL_StatusTypeDef HAL_TIMEx_ConfigSlaveModePreload(TIM_HandleTypeDef *htim, uint32_t Source)
  2499. {
  2500. /* Check the parameters */
  2501. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  2502. assert_param(IS_TIM_SLAVE_PRELOAD_SOURCE(Source));
  2503. MODIFY_REG(htim->Instance->SMCR, TIM_SMCR_SMSPS, Source);
  2504. return HAL_OK;
  2505. }
  2506. /**
  2507. * @brief Enable preload of Slave Mode Selection bitfield (SMS in SMCR register)
  2508. * @param htim TIM handle
  2509. * @retval HAL status
  2510. */
  2511. HAL_StatusTypeDef HAL_TIMEx_EnableSlaveModePreload(TIM_HandleTypeDef *htim)
  2512. {
  2513. /* Check the parameters */
  2514. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  2515. SET_BIT(htim->Instance->SMCR, TIM_SMCR_SMSPE);
  2516. return HAL_OK;
  2517. }
  2518. /**
  2519. * @brief Disable preload of Slave Mode Selection bitfield (SMS in SMCR register)
  2520. * @param htim TIM handle
  2521. * @retval HAL status
  2522. */
  2523. HAL_StatusTypeDef HAL_TIMEx_DisableSlaveModePreload(TIM_HandleTypeDef *htim)
  2524. {
  2525. /* Check the parameters */
  2526. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  2527. CLEAR_BIT(htim->Instance->SMCR, TIM_SMCR_SMSPE);
  2528. return HAL_OK;
  2529. }
  2530. /**
  2531. * @brief Enable deadtime preload
  2532. * @param htim TIM handle
  2533. * @retval HAL status
  2534. */
  2535. HAL_StatusTypeDef HAL_TIMEx_EnableDeadTimePreload(TIM_HandleTypeDef *htim)
  2536. {
  2537. /* Check the parameters */
  2538. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2539. SET_BIT(htim->Instance->DTR2, TIM_DTR2_DTPE);
  2540. return HAL_OK;
  2541. }
  2542. /**
  2543. * @brief Disable deadtime preload
  2544. * @param htim TIM handle
  2545. * @retval HAL status
  2546. */
  2547. HAL_StatusTypeDef HAL_TIMEx_DisableDeadTimePreload(TIM_HandleTypeDef *htim)
  2548. {
  2549. /* Check the parameters */
  2550. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2551. CLEAR_BIT(htim->Instance->DTR2, TIM_DTR2_DTPE);
  2552. return HAL_OK;
  2553. }
  2554. /**
  2555. * @brief Configure deadtime
  2556. * @param htim TIM handle
  2557. * @param Deadtime Deadtime value
  2558. * @note This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
  2559. * @retval HAL status
  2560. */
  2561. HAL_StatusTypeDef HAL_TIMEx_ConfigDeadTime(TIM_HandleTypeDef *htim, uint32_t Deadtime)
  2562. {
  2563. /* Check the parameters */
  2564. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2565. assert_param(IS_TIM_DEADTIME(Deadtime));
  2566. MODIFY_REG(htim->Instance->BDTR, TIM_BDTR_DTG, Deadtime);
  2567. return HAL_OK;
  2568. }
  2569. /**
  2570. * @brief Configure asymmetrical deadtime
  2571. * @param htim TIM handle
  2572. * @param FallingDeadtime Falling edge deadtime value
  2573. * @note This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
  2574. * @retval HAL status
  2575. */
  2576. HAL_StatusTypeDef HAL_TIMEx_ConfigAsymmetricalDeadTime(TIM_HandleTypeDef *htim, uint32_t FallingDeadtime)
  2577. {
  2578. /* Check the parameters */
  2579. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2580. assert_param(IS_TIM_DEADTIME(FallingDeadtime));
  2581. MODIFY_REG(htim->Instance->DTR2, TIM_DTR2_DTGF, FallingDeadtime);
  2582. return HAL_OK;
  2583. }
  2584. /**
  2585. * @brief Enable asymmetrical deadtime
  2586. * @param htim TIM handle
  2587. * @retval HAL status
  2588. */
  2589. HAL_StatusTypeDef HAL_TIMEx_EnableAsymmetricalDeadTime(TIM_HandleTypeDef *htim)
  2590. {
  2591. /* Check the parameters */
  2592. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2593. SET_BIT(htim->Instance->DTR2, TIM_DTR2_DTAE);
  2594. return HAL_OK;
  2595. }
  2596. /**
  2597. * @brief Disable asymmetrical deadtime
  2598. * @param htim TIM handle
  2599. * @retval HAL status
  2600. */
  2601. HAL_StatusTypeDef HAL_TIMEx_DisableAsymmetricalDeadTime(TIM_HandleTypeDef *htim)
  2602. {
  2603. /* Check the parameters */
  2604. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2605. CLEAR_BIT(htim->Instance->DTR2, TIM_DTR2_DTAE);
  2606. return HAL_OK;
  2607. }
  2608. /**
  2609. * @brief Configures the encoder index.
  2610. * @note warning in case of encoder mode clock plus direction
  2611. * @ref TIM_ENCODERMODE_CLOCKPLUSDIRECTION_X1 or @ref TIM_ENCODERMODE_CLOCKPLUSDIRECTION_X2
  2612. * Direction must be set to @ref TIM_ENCODERINDEX_DIRECTION_UP_DOWN
  2613. * @param htim TIM handle.
  2614. * @param sEncoderIndexConfig Encoder index configuration
  2615. * @retval HAL status
  2616. */
  2617. HAL_StatusTypeDef HAL_TIMEx_ConfigEncoderIndex(TIM_HandleTypeDef *htim,
  2618. TIMEx_EncoderIndexConfigTypeDef *sEncoderIndexConfig)
  2619. {
  2620. /* Check the parameters */
  2621. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2622. assert_param(IS_TIM_ENCODERINDEX_POLARITY(sEncoderIndexConfig->Polarity));
  2623. assert_param(IS_TIM_ENCODERINDEX_PRESCALER(sEncoderIndexConfig->Prescaler));
  2624. assert_param(IS_TIM_ENCODERINDEX_FILTER(sEncoderIndexConfig->Filter));
  2625. assert_param(IS_TIM_ENCODERINDEX_BLANKING(sEncoderIndexConfig->Blanking));
  2626. assert_param(IS_FUNCTIONAL_STATE(sEncoderIndexConfig->FirstIndexEnable));
  2627. assert_param(IS_TIM_ENCODERINDEX_POSITION(sEncoderIndexConfig->Position));
  2628. assert_param(IS_TIM_ENCODERINDEX_DIRECTION(sEncoderIndexConfig->Direction));
  2629. /* Process Locked */
  2630. __HAL_LOCK(htim);
  2631. /* Configures the TIMx External Trigger (ETR) which is used as Index input */
  2632. TIM_ETR_SetConfig(htim->Instance,
  2633. sEncoderIndexConfig->Prescaler,
  2634. sEncoderIndexConfig->Polarity,
  2635. sEncoderIndexConfig->Filter);
  2636. /* Configures the encoder index */
  2637. MODIFY_REG(htim->Instance->ECR,
  2638. TIM_ECR_IDIR_Msk | TIM_ECR_IBLK_Msk | TIM_ECR_FIDX_Msk | TIM_ECR_IPOS_Msk,
  2639. (sEncoderIndexConfig->Direction |
  2640. (sEncoderIndexConfig->Blanking) |
  2641. ((sEncoderIndexConfig->FirstIndexEnable == ENABLE) ? (0x1U << TIM_ECR_FIDX_Pos) : 0U) |
  2642. sEncoderIndexConfig->Position |
  2643. TIM_ECR_IE));
  2644. __HAL_UNLOCK(htim);
  2645. return HAL_OK;
  2646. }
  2647. /**
  2648. * @brief Enable encoder index
  2649. * @param htim TIM handle
  2650. * @retval HAL status
  2651. */
  2652. HAL_StatusTypeDef HAL_TIMEx_EnableEncoderIndex(TIM_HandleTypeDef *htim)
  2653. {
  2654. /* Check the parameters */
  2655. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2656. SET_BIT(htim->Instance->ECR, TIM_ECR_IE);
  2657. return HAL_OK;
  2658. }
  2659. /**
  2660. * @brief Disable encoder index
  2661. * @param htim TIM handle
  2662. * @retval HAL status
  2663. */
  2664. HAL_StatusTypeDef HAL_TIMEx_DisableEncoderIndex(TIM_HandleTypeDef *htim)
  2665. {
  2666. /* Check the parameters */
  2667. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2668. CLEAR_BIT(htim->Instance->ECR, TIM_ECR_IE);
  2669. return HAL_OK;
  2670. }
  2671. /**
  2672. * @brief Enable encoder first index
  2673. * @param htim TIM handle
  2674. * @retval HAL status
  2675. */
  2676. HAL_StatusTypeDef HAL_TIMEx_EnableEncoderFirstIndex(TIM_HandleTypeDef *htim)
  2677. {
  2678. /* Check the parameters */
  2679. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2680. SET_BIT(htim->Instance->ECR, TIM_ECR_FIDX);
  2681. return HAL_OK;
  2682. }
  2683. /**
  2684. * @brief Disable encoder first index
  2685. * @param htim TIM handle
  2686. * @retval HAL status
  2687. */
  2688. HAL_StatusTypeDef HAL_TIMEx_DisableEncoderFirstIndex(TIM_HandleTypeDef *htim)
  2689. {
  2690. /* Check the parameters */
  2691. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2692. CLEAR_BIT(htim->Instance->ECR, TIM_ECR_FIDX);
  2693. return HAL_OK;
  2694. }
  2695. /**
  2696. * @}
  2697. */
  2698. /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
  2699. * @brief Extended Callbacks functions
  2700. *
  2701. @verbatim
  2702. ==============================================================================
  2703. ##### Extended Callbacks functions #####
  2704. ==============================================================================
  2705. [..]
  2706. This section provides Extended TIM callback functions:
  2707. (+) Timer Commutation callback
  2708. (+) Timer Break callback
  2709. @endverbatim
  2710. * @{
  2711. */
  2712. /**
  2713. * @brief Commutation callback in non-blocking mode
  2714. * @param htim TIM handle
  2715. * @retval None
  2716. */
  2717. __weak void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim)
  2718. {
  2719. /* Prevent unused argument(s) compilation warning */
  2720. UNUSED(htim);
  2721. /* NOTE : This function should not be modified, when the callback is needed,
  2722. the HAL_TIMEx_CommutCallback could be implemented in the user file
  2723. */
  2724. }
  2725. /**
  2726. * @brief Commutation half complete callback in non-blocking mode
  2727. * @param htim TIM handle
  2728. * @retval None
  2729. */
  2730. __weak void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim)
  2731. {
  2732. /* Prevent unused argument(s) compilation warning */
  2733. UNUSED(htim);
  2734. /* NOTE : This function should not be modified, when the callback is needed,
  2735. the HAL_TIMEx_CommutHalfCpltCallback could be implemented in the user file
  2736. */
  2737. }
  2738. /**
  2739. * @brief Break detection callback in non-blocking mode
  2740. * @param htim TIM handle
  2741. * @retval None
  2742. */
  2743. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  2744. {
  2745. /* Prevent unused argument(s) compilation warning */
  2746. UNUSED(htim);
  2747. /* NOTE : This function should not be modified, when the callback is needed,
  2748. the HAL_TIMEx_BreakCallback could be implemented in the user file
  2749. */
  2750. }
  2751. /**
  2752. * @brief Break2 detection callback in non blocking mode
  2753. * @param htim: TIM handle
  2754. * @retval None
  2755. */
  2756. __weak void HAL_TIMEx_Break2Callback(TIM_HandleTypeDef *htim)
  2757. {
  2758. /* Prevent unused argument(s) compilation warning */
  2759. UNUSED(htim);
  2760. /* NOTE : This function Should not be modified, when the callback is needed,
  2761. the HAL_TIMEx_Break2Callback could be implemented in the user file
  2762. */
  2763. }
  2764. /**
  2765. * @brief Encoder index callback in non-blocking mode
  2766. * @param htim TIM handle
  2767. * @retval None
  2768. */
  2769. __weak void HAL_TIMEx_EncoderIndexCallback(TIM_HandleTypeDef *htim)
  2770. {
  2771. /* Prevent unused argument(s) compilation warning */
  2772. UNUSED(htim);
  2773. /* NOTE : This function should not be modified, when the callback is needed,
  2774. the HAL_TIMEx_EncoderIndexCallback could be implemented in the user file
  2775. */
  2776. }
  2777. /**
  2778. * @brief Direction change callback in non-blocking mode
  2779. * @param htim TIM handle
  2780. * @retval None
  2781. */
  2782. __weak void HAL_TIMEx_DirectionChangeCallback(TIM_HandleTypeDef *htim)
  2783. {
  2784. /* Prevent unused argument(s) compilation warning */
  2785. UNUSED(htim);
  2786. /* NOTE : This function should not be modified, when the callback is needed,
  2787. the HAL_TIMEx_DirectionChangeCallback could be implemented in the user file
  2788. */
  2789. }
  2790. /**
  2791. * @brief Index error callback in non-blocking mode
  2792. * @param htim TIM handle
  2793. * @retval None
  2794. */
  2795. __weak void HAL_TIMEx_IndexErrorCallback(TIM_HandleTypeDef *htim)
  2796. {
  2797. /* Prevent unused argument(s) compilation warning */
  2798. UNUSED(htim);
  2799. /* NOTE : This function should not be modified, when the callback is needed,
  2800. the HAL_TIMEx_IndexErrorCallback could be implemented in the user file
  2801. */
  2802. }
  2803. /**
  2804. * @brief Transition error callback in non-blocking mode
  2805. * @param htim TIM handle
  2806. * @retval None
  2807. */
  2808. __weak void HAL_TIMEx_TransitionErrorCallback(TIM_HandleTypeDef *htim)
  2809. {
  2810. /* Prevent unused argument(s) compilation warning */
  2811. UNUSED(htim);
  2812. /* NOTE : This function should not be modified, when the callback is needed,
  2813. the HAL_TIMEx_TransitionErrorCallback could be implemented in the user file
  2814. */
  2815. }
  2816. /**
  2817. * @}
  2818. */
  2819. /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
  2820. * @brief Extended Peripheral State functions
  2821. *
  2822. @verbatim
  2823. ==============================================================================
  2824. ##### Extended Peripheral State functions #####
  2825. ==============================================================================
  2826. [..]
  2827. This subsection permits to get in run-time the status of the peripheral
  2828. and the data flow.
  2829. @endverbatim
  2830. * @{
  2831. */
  2832. /**
  2833. * @brief Return the TIM Hall Sensor interface handle state.
  2834. * @param htim TIM Hall Sensor handle
  2835. * @retval HAL state
  2836. */
  2837. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(const TIM_HandleTypeDef *htim)
  2838. {
  2839. return htim->State;
  2840. }
  2841. /**
  2842. * @brief Return actual state of the TIM complementary channel.
  2843. * @param htim TIM handle
  2844. * @param ChannelN TIM Complementary channel
  2845. * This parameter can be one of the following values:
  2846. * @arg TIM_CHANNEL_1: TIM Channel 1
  2847. * @arg TIM_CHANNEL_2: TIM Channel 2
  2848. * @arg TIM_CHANNEL_3: TIM Channel 3
  2849. * @arg TIM_CHANNEL_4: TIM Channel 4
  2850. * @retval TIM Complementary channel state
  2851. */
  2852. HAL_TIM_ChannelStateTypeDef HAL_TIMEx_GetChannelNState(const TIM_HandleTypeDef *htim, uint32_t ChannelN)
  2853. {
  2854. HAL_TIM_ChannelStateTypeDef channel_state;
  2855. /* Check the parameters */
  2856. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, ChannelN));
  2857. channel_state = TIM_CHANNEL_N_STATE_GET(htim, ChannelN);
  2858. return channel_state;
  2859. }
  2860. /**
  2861. * @}
  2862. */
  2863. /**
  2864. * @}
  2865. */
  2866. /* Private functions ---------------------------------------------------------*/
  2867. /** @defgroup TIMEx_Private_Functions TIM Extended Private Functions
  2868. * @{
  2869. */
  2870. /**
  2871. * @brief TIM DMA Commutation callback.
  2872. * @param hdma pointer to DMA handle.
  2873. * @retval None
  2874. */
  2875. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  2876. {
  2877. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2878. /* Change the htim state */
  2879. htim->State = HAL_TIM_STATE_READY;
  2880. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2881. htim->CommutationCallback(htim);
  2882. #else
  2883. HAL_TIMEx_CommutCallback(htim);
  2884. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2885. }
  2886. /**
  2887. * @brief TIM DMA Commutation half complete callback.
  2888. * @param hdma pointer to DMA handle.
  2889. * @retval None
  2890. */
  2891. void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma)
  2892. {
  2893. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2894. /* Change the htim state */
  2895. htim->State = HAL_TIM_STATE_READY;
  2896. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2897. htim->CommutationHalfCpltCallback(htim);
  2898. #else
  2899. HAL_TIMEx_CommutHalfCpltCallback(htim);
  2900. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2901. }
  2902. /**
  2903. * @brief TIM DMA Delay Pulse complete callback (complementary channel).
  2904. * @param hdma pointer to DMA handle.
  2905. * @retval None
  2906. */
  2907. static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma)
  2908. {
  2909. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2910. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  2911. {
  2912. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2913. }
  2914. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  2915. {
  2916. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2917. }
  2918. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  2919. {
  2920. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2921. }
  2922. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  2923. {
  2924. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  2925. }
  2926. else
  2927. {
  2928. /* nothing to do */
  2929. }
  2930. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2931. htim->PWM_PulseFinishedCallback(htim);
  2932. #else
  2933. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2934. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2935. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2936. }
  2937. /**
  2938. * @brief TIM DMA error callback (complementary channel)
  2939. * @param hdma pointer to DMA handle.
  2940. * @retval None
  2941. */
  2942. static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma)
  2943. {
  2944. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2945. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  2946. {
  2947. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2948. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2949. }
  2950. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  2951. {
  2952. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2953. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2954. }
  2955. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  2956. {
  2957. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2958. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  2959. }
  2960. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  2961. {
  2962. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  2963. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  2964. }
  2965. else
  2966. {
  2967. /* nothing to do */
  2968. }
  2969. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2970. htim->ErrorCallback(htim);
  2971. #else
  2972. HAL_TIM_ErrorCallback(htim);
  2973. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2974. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2975. }
  2976. /**
  2977. * @brief Enables or disables the TIM Capture Compare Channel xN.
  2978. * @param TIMx to select the TIM peripheral
  2979. * @param Channel specifies the TIM Channel
  2980. * This parameter can be one of the following values:
  2981. * @arg TIM_CHANNEL_1: TIM Channel 1
  2982. * @arg TIM_CHANNEL_2: TIM Channel 2
  2983. * @arg TIM_CHANNEL_3: TIM Channel 3
  2984. * @arg TIM_CHANNEL_4: TIM Channel 4
  2985. * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
  2986. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  2987. * @retval None
  2988. */
  2989. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState)
  2990. {
  2991. uint32_t tmp;
  2992. tmp = TIM_CCER_CC1NE << (Channel & 0xFU); /* 0xFU = 15 bits max shift */
  2993. /* Reset the CCxNE Bit */
  2994. TIMx->CCER &= ~tmp;
  2995. /* Set or reset the CCxNE Bit */
  2996. TIMx->CCER |= (uint32_t)(ChannelNState << (Channel & 0xFU)); /* 0xFU = 15 bits max shift */
  2997. }
  2998. /**
  2999. * @}
  3000. */
  3001. #endif /* HAL_TIM_MODULE_ENABLED */
  3002. /**
  3003. * @}
  3004. */
  3005. /**
  3006. * @}
  3007. */