stm32g4xx_hal_tim_ex.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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) 2019 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 "stm32g4xx_hal.h"
  82. /** @addtogroup STM32G4xx_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 (HAL_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 (HAL_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 (HAL_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 (HAL_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 (HAL_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 (HAL_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 (HAL_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 (HAL_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 (HAL_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_NONE: No trigger is needed
  1633. *
  1634. * (*) Value not defined in all devices.
  1635. *
  1636. * @param CommutationSource the Commutation Event source
  1637. * This parameter can be one of the following values:
  1638. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1639. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1640. * @retval HAL status
  1641. */
  1642. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1643. uint32_t CommutationSource)
  1644. {
  1645. /* Check the parameters */
  1646. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1647. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
  1648. __HAL_LOCK(htim);
  1649. #if defined(TIM5) && defined(TIM20)
  1650. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1651. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1652. (InputTrigger == TIM_TS_ITR4) || (InputTrigger == TIM_TS_ITR5) ||
  1653. (InputTrigger == TIM_TS_ITR6) || (InputTrigger == TIM_TS_ITR7) ||
  1654. (InputTrigger == TIM_TS_ITR8) || (InputTrigger == TIM_TS_ITR9) ||
  1655. (InputTrigger == TIM_TS_ITR10) || (InputTrigger == TIM_TS_ITR11))
  1656. #elif defined(TIM5)
  1657. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1658. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1659. (InputTrigger == TIM_TS_ITR4) || (InputTrigger == TIM_TS_ITR5) ||
  1660. (InputTrigger == TIM_TS_ITR6) || (InputTrigger == TIM_TS_ITR7) ||
  1661. (InputTrigger == TIM_TS_ITR8) || (InputTrigger == TIM_TS_ITR11))
  1662. #elif defined(TIM20)
  1663. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1664. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1665. (InputTrigger == TIM_TS_ITR5) || (InputTrigger == TIM_TS_ITR6) ||
  1666. (InputTrigger == TIM_TS_ITR7) || (InputTrigger == TIM_TS_ITR8) ||
  1667. (InputTrigger == TIM_TS_ITR9) || (InputTrigger == TIM_TS_ITR11))
  1668. #else
  1669. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1670. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1671. (InputTrigger == TIM_TS_ITR5) || (InputTrigger == TIM_TS_ITR6) ||
  1672. (InputTrigger == TIM_TS_ITR7) || (InputTrigger == TIM_TS_ITR8) ||
  1673. (InputTrigger == TIM_TS_ITR11))
  1674. #endif /* TIM5 && TIM20 */
  1675. {
  1676. /* Select the Input trigger */
  1677. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1678. htim->Instance->SMCR |= InputTrigger;
  1679. }
  1680. /* Select the Capture Compare preload feature */
  1681. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1682. /* Select the Commutation event source */
  1683. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1684. htim->Instance->CR2 |= CommutationSource;
  1685. /* Disable Commutation Interrupt */
  1686. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1687. /* Disable Commutation DMA request */
  1688. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1689. __HAL_UNLOCK(htim);
  1690. return HAL_OK;
  1691. }
  1692. /**
  1693. * @brief Configure the TIM commutation event sequence with interrupt.
  1694. * @note This function is mandatory to use the commutation event in order to
  1695. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1696. * the typical use of this feature is with the use of another Timer(interface Timer)
  1697. * configured in Hall sensor interface, this interface Timer will generate the
  1698. * commutation at its TRGO output (connected to Timer used in this function) each time
  1699. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1700. * @param htim TIM handle
  1701. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1702. * This parameter can be one of the following values:
  1703. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1704. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1705. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1706. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1707. * @arg TIM_TS_ITR4: Internal trigger 4 selected (*)
  1708. * @arg TIM_TS_ITR5: Internal trigger 5 selected
  1709. * @arg TIM_TS_ITR6: Internal trigger 6 selected
  1710. * @arg TIM_TS_ITR7: Internal trigger 7 selected
  1711. * @arg TIM_TS_ITR8: Internal trigger 8 selected
  1712. * @arg TIM_TS_ITR9: Internal trigger 9 selected (*)
  1713. * @arg TIM_TS_ITR10: Internal trigger 10 selected
  1714. * @arg TIM_TS_ITR11: Internal trigger 11 selected
  1715. * @arg TIM_TS_NONE: No trigger is needed
  1716. *
  1717. * (*) Value not defined in all devices.
  1718. *
  1719. * @param CommutationSource the Commutation Event source
  1720. * This parameter can be one of the following values:
  1721. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1722. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1723. * @retval HAL status
  1724. */
  1725. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1726. uint32_t CommutationSource)
  1727. {
  1728. /* Check the parameters */
  1729. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1730. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
  1731. __HAL_LOCK(htim);
  1732. #if defined(TIM5) && defined(TIM20)
  1733. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1734. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1735. (InputTrigger == TIM_TS_ITR4) || (InputTrigger == TIM_TS_ITR5) ||
  1736. (InputTrigger == TIM_TS_ITR6) || (InputTrigger == TIM_TS_ITR7) ||
  1737. (InputTrigger == TIM_TS_ITR8) || (InputTrigger == TIM_TS_ITR9) ||
  1738. (InputTrigger == TIM_TS_ITR10) || (InputTrigger == TIM_TS_ITR11))
  1739. #elif defined(TIM5)
  1740. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1741. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1742. (InputTrigger == TIM_TS_ITR4) || (InputTrigger == TIM_TS_ITR5) ||
  1743. (InputTrigger == TIM_TS_ITR6) || (InputTrigger == TIM_TS_ITR7) ||
  1744. (InputTrigger == TIM_TS_ITR8) || (InputTrigger == TIM_TS_ITR11))
  1745. #elif defined(TIM20)
  1746. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1747. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1748. (InputTrigger == TIM_TS_ITR5) || (InputTrigger == TIM_TS_ITR6) ||
  1749. (InputTrigger == TIM_TS_ITR7) || (InputTrigger == TIM_TS_ITR8) ||
  1750. (InputTrigger == TIM_TS_ITR9) || (InputTrigger == TIM_TS_ITR11))
  1751. #else
  1752. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1753. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1754. (InputTrigger == TIM_TS_ITR5) || (InputTrigger == TIM_TS_ITR6) ||
  1755. (InputTrigger == TIM_TS_ITR7) || (InputTrigger == TIM_TS_ITR8) ||
  1756. (InputTrigger == TIM_TS_ITR11))
  1757. #endif /* TIM5 && TIM20 */
  1758. {
  1759. /* Select the Input trigger */
  1760. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1761. htim->Instance->SMCR |= InputTrigger;
  1762. }
  1763. /* Select the Capture Compare preload feature */
  1764. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1765. /* Select the Commutation event source */
  1766. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1767. htim->Instance->CR2 |= CommutationSource;
  1768. /* Disable Commutation DMA request */
  1769. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1770. /* Enable the Commutation Interrupt */
  1771. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1772. __HAL_UNLOCK(htim);
  1773. return HAL_OK;
  1774. }
  1775. /**
  1776. * @brief Configure the TIM commutation event sequence with DMA.
  1777. * @note This function is mandatory to use the commutation event in order to
  1778. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1779. * the typical use of this feature is with the use of another Timer(interface Timer)
  1780. * configured in Hall sensor interface, this interface Timer will generate the
  1781. * commutation at its TRGO output (connected to Timer used in this function) each time
  1782. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1783. * @note The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1784. * @param htim TIM handle
  1785. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1786. * This parameter can be one of the following values:
  1787. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1788. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1789. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1790. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1791. * @arg TIM_TS_ITR4: Internal trigger 4 selected (*)
  1792. * @arg TIM_TS_ITR5: Internal trigger 5 selected
  1793. * @arg TIM_TS_ITR6: Internal trigger 6 selected
  1794. * @arg TIM_TS_ITR7: Internal trigger 7 selected
  1795. * @arg TIM_TS_ITR8: Internal trigger 8 selected
  1796. * @arg TIM_TS_ITR9: Internal trigger 9 selected (*)
  1797. * @arg TIM_TS_ITR10: Internal trigger 10 selected
  1798. * @arg TIM_TS_ITR11: Internal trigger 11 selected
  1799. * @arg TIM_TS_NONE: No trigger is needed
  1800. *
  1801. * (*) Value not defined in all devices.
  1802. *
  1803. * @param CommutationSource the Commutation Event source
  1804. * This parameter can be one of the following values:
  1805. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1806. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1807. * @retval HAL status
  1808. */
  1809. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1810. uint32_t CommutationSource)
  1811. {
  1812. /* Check the parameters */
  1813. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1814. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
  1815. __HAL_LOCK(htim);
  1816. #if defined(TIM5) && defined(TIM20)
  1817. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1818. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1819. (InputTrigger == TIM_TS_ITR4) || (InputTrigger == TIM_TS_ITR5) ||
  1820. (InputTrigger == TIM_TS_ITR6) || (InputTrigger == TIM_TS_ITR7) ||
  1821. (InputTrigger == TIM_TS_ITR8) || (InputTrigger == TIM_TS_ITR9) ||
  1822. (InputTrigger == TIM_TS_ITR10) || (InputTrigger == TIM_TS_ITR11))
  1823. #elif defined(TIM5)
  1824. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1825. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1826. (InputTrigger == TIM_TS_ITR4) || (InputTrigger == TIM_TS_ITR5) ||
  1827. (InputTrigger == TIM_TS_ITR6) || (InputTrigger == TIM_TS_ITR7) ||
  1828. (InputTrigger == TIM_TS_ITR8) || (InputTrigger == TIM_TS_ITR11))
  1829. #elif defined(TIM20)
  1830. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1831. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1832. (InputTrigger == TIM_TS_ITR5) || (InputTrigger == TIM_TS_ITR6) ||
  1833. (InputTrigger == TIM_TS_ITR7) || (InputTrigger == TIM_TS_ITR8) ||
  1834. (InputTrigger == TIM_TS_ITR9) || (InputTrigger == TIM_TS_ITR11))
  1835. #else
  1836. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1837. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3) ||
  1838. (InputTrigger == TIM_TS_ITR5) || (InputTrigger == TIM_TS_ITR6) ||
  1839. (InputTrigger == TIM_TS_ITR7) || (InputTrigger == TIM_TS_ITR8) ||
  1840. (InputTrigger == TIM_TS_ITR11))
  1841. #endif /* TIM5 && TIM20 */
  1842. {
  1843. /* Select the Input trigger */
  1844. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1845. htim->Instance->SMCR |= InputTrigger;
  1846. }
  1847. /* Select the Capture Compare preload feature */
  1848. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1849. /* Select the Commutation event source */
  1850. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1851. htim->Instance->CR2 |= CommutationSource;
  1852. /* Enable the Commutation DMA Request */
  1853. /* Set the DMA Commutation Callback */
  1854. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1855. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  1856. /* Set the DMA error callback */
  1857. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1858. /* Disable Commutation Interrupt */
  1859. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1860. /* Enable the Commutation DMA Request */
  1861. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1862. __HAL_UNLOCK(htim);
  1863. return HAL_OK;
  1864. }
  1865. /**
  1866. * @brief Configures the TIM in master mode.
  1867. * @param htim TIM handle.
  1868. * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
  1869. * contains the selected trigger output (TRGO) and the Master/Slave
  1870. * mode.
  1871. * @retval HAL status
  1872. */
  1873. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
  1874. const TIM_MasterConfigTypeDef *sMasterConfig)
  1875. {
  1876. uint32_t tmpcr2;
  1877. uint32_t tmpsmcr;
  1878. /* Check the parameters */
  1879. assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
  1880. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1881. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1882. /* Check input state */
  1883. __HAL_LOCK(htim);
  1884. /* Change the handler state */
  1885. htim->State = HAL_TIM_STATE_BUSY;
  1886. /* Get the TIMx CR2 register value */
  1887. tmpcr2 = htim->Instance->CR2;
  1888. /* Get the TIMx SMCR register value */
  1889. tmpsmcr = htim->Instance->SMCR;
  1890. /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
  1891. if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
  1892. {
  1893. /* Check the parameters */
  1894. assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
  1895. /* Clear the MMS2 bits */
  1896. tmpcr2 &= ~TIM_CR2_MMS2;
  1897. /* Select the TRGO2 source*/
  1898. tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
  1899. }
  1900. /* Reset the MMS Bits */
  1901. tmpcr2 &= ~TIM_CR2_MMS;
  1902. /* Select the TRGO source */
  1903. tmpcr2 |= sMasterConfig->MasterOutputTrigger;
  1904. /* Update TIMx CR2 */
  1905. htim->Instance->CR2 = tmpcr2;
  1906. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1907. {
  1908. /* Reset the MSM Bit */
  1909. tmpsmcr &= ~TIM_SMCR_MSM;
  1910. /* Set master mode */
  1911. tmpsmcr |= sMasterConfig->MasterSlaveMode;
  1912. /* Update TIMx SMCR */
  1913. htim->Instance->SMCR = tmpsmcr;
  1914. }
  1915. /* Change the htim state */
  1916. htim->State = HAL_TIM_STATE_READY;
  1917. __HAL_UNLOCK(htim);
  1918. return HAL_OK;
  1919. }
  1920. /**
  1921. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1922. * and the AOE(automatic output enable).
  1923. * @param htim TIM handle
  1924. * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
  1925. * contains the BDTR Register configuration information for the TIM peripheral.
  1926. * @note Interrupts can be generated when an active level is detected on the
  1927. * break input, the break 2 input or the system break input. Break
  1928. * interrupt can be enabled by calling the @ref __HAL_TIM_ENABLE_IT macro.
  1929. * @retval HAL status
  1930. */
  1931. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1932. const TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
  1933. {
  1934. /* Keep this variable initialized to 0 as it is used to configure BDTR register */
  1935. uint32_t tmpbdtr = 0U;
  1936. /* Check the parameters */
  1937. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1938. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1939. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1940. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1941. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1942. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1943. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1944. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
  1945. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1946. assert_param(IS_TIM_BREAK_AFMODE(sBreakDeadTimeConfig->BreakAFMode));
  1947. /* Check input state */
  1948. __HAL_LOCK(htim);
  1949. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1950. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1951. /* Set the BDTR bits */
  1952. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  1953. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  1954. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  1955. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  1956. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  1957. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  1958. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  1959. MODIFY_REG(tmpbdtr, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << TIM_BDTR_BKF_Pos));
  1960. MODIFY_REG(tmpbdtr, TIM_BDTR_BKBID, sBreakDeadTimeConfig->BreakAFMode);
  1961. if (IS_TIM_BKIN2_INSTANCE(htim->Instance))
  1962. {
  1963. /* Check the parameters */
  1964. assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
  1965. assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
  1966. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
  1967. assert_param(IS_TIM_BREAK2_AFMODE(sBreakDeadTimeConfig->Break2AFMode));
  1968. /* Set the BREAK2 input related BDTR bits */
  1969. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2F, (sBreakDeadTimeConfig->Break2Filter << TIM_BDTR_BK2F_Pos));
  1970. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2E, sBreakDeadTimeConfig->Break2State);
  1971. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2P, sBreakDeadTimeConfig->Break2Polarity);
  1972. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2BID, sBreakDeadTimeConfig->Break2AFMode);
  1973. }
  1974. /* Set TIMx_BDTR */
  1975. htim->Instance->BDTR = tmpbdtr;
  1976. __HAL_UNLOCK(htim);
  1977. return HAL_OK;
  1978. }
  1979. /**
  1980. * @brief Configures the break input source.
  1981. * @param htim TIM handle.
  1982. * @param BreakInput Break input to configure
  1983. * This parameter can be one of the following values:
  1984. * @arg TIM_BREAKINPUT_BRK: Timer break input
  1985. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  1986. * @param sBreakInputConfig Break input source configuration
  1987. * @retval HAL status
  1988. */
  1989. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim,
  1990. uint32_t BreakInput,
  1991. const TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
  1992. {
  1993. HAL_StatusTypeDef status = HAL_OK;
  1994. uint32_t tmporx;
  1995. uint32_t bkin_enable_mask;
  1996. uint32_t bkin_polarity_mask;
  1997. uint32_t bkin_enable_bitpos;
  1998. uint32_t bkin_polarity_bitpos;
  1999. /* Check the parameters */
  2000. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2001. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  2002. assert_param(IS_TIM_BREAKINPUTSOURCE(sBreakInputConfig->Source));
  2003. assert_param(IS_TIM_BREAKINPUTSOURCE_STATE(sBreakInputConfig->Enable));
  2004. assert_param(IS_TIM_BREAKINPUTSOURCE_POLARITY(sBreakInputConfig->Polarity));
  2005. /* Check input state */
  2006. __HAL_LOCK(htim);
  2007. switch (sBreakInputConfig->Source)
  2008. {
  2009. case TIM_BREAKINPUTSOURCE_BKIN:
  2010. {
  2011. bkin_enable_mask = TIM1_AF1_BKINE;
  2012. bkin_enable_bitpos = TIM1_AF1_BKINE_Pos;
  2013. bkin_polarity_mask = TIM1_AF1_BKINP;
  2014. bkin_polarity_bitpos = TIM1_AF1_BKINP_Pos;
  2015. break;
  2016. }
  2017. case TIM_BREAKINPUTSOURCE_COMP1:
  2018. {
  2019. bkin_enable_mask = TIM1_AF1_BKCMP1E;
  2020. bkin_enable_bitpos = TIM1_AF1_BKCMP1E_Pos;
  2021. bkin_polarity_mask = TIM1_AF1_BKCMP1P;
  2022. bkin_polarity_bitpos = TIM1_AF1_BKCMP1P_Pos;
  2023. break;
  2024. }
  2025. #if defined (COMP2)
  2026. case TIM_BREAKINPUTSOURCE_COMP2:
  2027. {
  2028. bkin_enable_mask = TIM1_AF1_BKCMP2E;
  2029. bkin_enable_bitpos = TIM1_AF1_BKCMP2E_Pos;
  2030. bkin_polarity_mask = TIM1_AF1_BKCMP2P;
  2031. bkin_polarity_bitpos = TIM1_AF1_BKCMP2P_Pos;
  2032. break;
  2033. }
  2034. #endif /* COMP2 */
  2035. case TIM_BREAKINPUTSOURCE_COMP3:
  2036. {
  2037. bkin_enable_mask = TIM1_AF1_BKCMP3E;
  2038. bkin_enable_bitpos = TIM1_AF1_BKCMP3E_Pos;
  2039. bkin_polarity_mask = TIM1_AF1_BKCMP3P;
  2040. bkin_polarity_bitpos = TIM1_AF1_BKCMP3P_Pos;
  2041. break;
  2042. }
  2043. #if defined (COMP4)
  2044. case TIM_BREAKINPUTSOURCE_COMP4:
  2045. {
  2046. bkin_enable_mask = TIM1_AF1_BKCMP4E;
  2047. bkin_enable_bitpos = TIM1_AF1_BKCMP4E_Pos;
  2048. bkin_polarity_mask = TIM1_AF1_BKCMP4P;
  2049. bkin_polarity_bitpos = TIM1_AF1_BKCMP4P_Pos;
  2050. break;
  2051. }
  2052. #endif /* COMP4 */
  2053. #if defined (COMP5)
  2054. case TIM_BREAKINPUTSOURCE_COMP5:
  2055. {
  2056. bkin_enable_mask = TIM1_AF1_BKCMP5E;
  2057. bkin_enable_bitpos = TIM1_AF1_BKCMP5E_Pos;
  2058. /* No palarity bit for this COMP. Variable bkin_polarity_mask keeps its default value 0 */
  2059. bkin_polarity_mask = 0U;
  2060. bkin_polarity_bitpos = 0U;
  2061. break;
  2062. }
  2063. #endif /* COMP5 */
  2064. #if defined (COMP6)
  2065. case TIM_BREAKINPUTSOURCE_COMP6:
  2066. {
  2067. bkin_enable_mask = TIM1_AF1_BKCMP6E;
  2068. bkin_enable_bitpos = TIM1_AF1_BKCMP6E_Pos;
  2069. /* No palarity bit for this COMP. Variable bkin_polarity_mask keeps its default value 0 */
  2070. bkin_polarity_mask = 0U;
  2071. bkin_polarity_bitpos = 0U;
  2072. break;
  2073. }
  2074. #endif /* COMP7 */
  2075. #if defined (COMP7)
  2076. case TIM_BREAKINPUTSOURCE_COMP7:
  2077. {
  2078. bkin_enable_mask = TIM1_AF1_BKCMP7E;
  2079. bkin_enable_bitpos = TIM1_AF1_BKCMP7E_Pos;
  2080. /* No palarity bit for this COMP. Variable bkin_polarity_mask keeps its default value 0 */
  2081. bkin_polarity_mask = 0U;
  2082. bkin_polarity_bitpos = 0U;
  2083. break;
  2084. }
  2085. #endif /* COMP7 */
  2086. default:
  2087. {
  2088. bkin_enable_mask = 0U;
  2089. bkin_polarity_mask = 0U;
  2090. bkin_enable_bitpos = 0U;
  2091. bkin_polarity_bitpos = 0U;
  2092. break;
  2093. }
  2094. }
  2095. switch (BreakInput)
  2096. {
  2097. case TIM_BREAKINPUT_BRK:
  2098. {
  2099. /* Get the TIMx_AF1 register value */
  2100. tmporx = htim->Instance->AF1;
  2101. /* Enable the break input */
  2102. tmporx &= ~bkin_enable_mask;
  2103. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  2104. /* Set the break input polarity */
  2105. tmporx &= ~bkin_polarity_mask;
  2106. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  2107. /* Set TIMx_AF1 */
  2108. htim->Instance->AF1 = tmporx;
  2109. break;
  2110. }
  2111. case TIM_BREAKINPUT_BRK2:
  2112. {
  2113. /* Get the TIMx_AF2 register value */
  2114. tmporx = htim->Instance->AF2;
  2115. /* Enable the break input */
  2116. tmporx &= ~bkin_enable_mask;
  2117. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  2118. /* Set the break input polarity */
  2119. tmporx &= ~bkin_polarity_mask;
  2120. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  2121. /* Set TIMx_AF2 */
  2122. htim->Instance->AF2 = tmporx;
  2123. break;
  2124. }
  2125. default:
  2126. status = HAL_ERROR;
  2127. break;
  2128. }
  2129. __HAL_UNLOCK(htim);
  2130. return status;
  2131. }
  2132. /**
  2133. * @brief Configures the TIMx Remapping input capabilities.
  2134. * @param htim TIM handle.
  2135. * @param Remap specifies the TIM remapping source.
  2136. * For TIM1, the parameter can take one of the following values:
  2137. * @arg TIM_TIM1_ETR_GPIO TIM1 ETR is connected to GPIO
  2138. * @arg TIM_TIM1_ETR_COMP1 TIM1 ETR is connected to COMP1 output
  2139. * @arg TIM_TIM1_ETR_COMP2 TIM1 ETR is connected to COMP2 output
  2140. * @arg TIM_TIM1_ETR_COMP3 TIM1 ETR is connected to COMP3 output
  2141. * @arg TIM_TIM1_ETR_COMP4 TIM1 ETR is connected to COMP4 output
  2142. * @arg TIM_TIM1_ETR_COMP5 TIM1 ETR is connected to COMP5 output (*)
  2143. * @arg TIM_TIM1_ETR_COMP6 TIM1 ETR is connected to COMP6 output (*)
  2144. * @arg TIM_TIM1_ETR_COMP7 TIM1 ETR is connected to COMP7 output (*)
  2145. * @arg TIM_TIM1_ETR_ADC1_AWD1 TIM1 ETR is connected to ADC1 AWD1
  2146. * @arg TIM_TIM1_ETR_ADC1_AWD2 TIM1 ETR is connected to ADC1 AWD2
  2147. * @arg TIM_TIM1_ETR_ADC1_AWD3 TIM1 ETR is connected to ADC1 AWD3
  2148. * @arg TIM_TIM1_ETR_ADC4_AWD1 TIM1 ETR is connected to ADC4 AWD1 (*)
  2149. * @arg TIM_TIM1_ETR_ADC4_AWD2 TIM1 ETR is connected to ADC4 AWD2 (*)
  2150. * @arg TIM_TIM1_ETR_ADC4_AWD3 TIM1 ETR is connected to ADC4 AWD3 (*)
  2151. *
  2152. * For TIM2, the parameter can take one of the following values:
  2153. * @arg TIM_TIM2_ETR_GPIO TIM2 ETR is connected to GPIO
  2154. * @arg TIM_TIM2_ETR_COMP1 TIM2 ETR is connected to COMP1 output
  2155. * @arg TIM_TIM2_ETR_COMP2 TIM2 ETR is connected to COMP2 output
  2156. * @arg TIM_TIM2_ETR_COMP3 TIM2 ETR is connected to COMP3 output
  2157. * @arg TIM_TIM2_ETR_COMP4 TIM2 ETR is connected to COMP4 output
  2158. * @arg TIM_TIM2_ETR_COMP5 TIM2 ETR is connected to COMP5 output (*)
  2159. * @arg TIM_TIM2_ETR_COMP6 TIM2 ETR is connected to COMP6 output (*)
  2160. * @arg TIM_TIM2_ETR_COMP7 TIM2 ETR is connected to COMP7 output (*)
  2161. * @arg TIM_TIM2_ETR_TIM3_ETR TIM2 ETR is connected to TIM3 ETR pin
  2162. * @arg TIM_TIM2_ETR_TIM4_ETR TIM2 ETR is connected to TIM4 ETR pin
  2163. * @arg TIM_TIM2_ETR_TIM5_ETR TIM2 ETR is connected to TIM5 ETR pin (*)
  2164. * @arg TIM_TIM2_ETR_LSE
  2165. *
  2166. * For TIM3, the parameter can take one of the following values:
  2167. * @arg TIM_TIM3_ETR_GPIO TIM3 ETR is connected to GPIO
  2168. * @arg TIM_TIM3_ETR_COMP1 TIM3 ETR is connected to COMP1 output
  2169. * @arg TIM_TIM3_ETR_COMP2 TIM3 ETR is connected to COMP2 output
  2170. * @arg TIM_TIM3_ETR_COMP3 TIM3 ETR is connected to COMP3 output
  2171. * @arg TIM_TIM3_ETR_COMP4 TIM3 ETR is connected to COMP4 output
  2172. * @arg TIM_TIM3_ETR_COMP5 TIM3 ETR is connected to COMP5 output (*)
  2173. * @arg TIM_TIM3_ETR_COMP6 TIM3 ETR is connected to COMP6 output (*)
  2174. * @arg TIM_TIM3_ETR_COMP7 TIM3 ETR is connected to COMP7 output (*)
  2175. * @arg TIM_TIM3_ETR_TIM2_ETR TIM3 ETR is connected to TIM2 ETR pin
  2176. * @arg TIM_TIM3_ETR_TIM4_ETR TIM3 ETR is connected to TIM4 ETR pin
  2177. * @arg TIM_TIM3_ETR_ADC2_AWD1 TIM3 ETR is connected to ADC2 AWD1
  2178. * @arg TIM_TIM3_ETR_ADC2_AWD2 TIM3 ETR is connected to ADC2 AWD2
  2179. * @arg TIM_TIM3_ETR_ADC2_AWD3 TIM3 ETR is connected to ADC2 AWD3
  2180. *
  2181. * For TIM4, the parameter can take one of the following values:
  2182. * @arg TIM_TIM4_ETR_GPIO TIM4 ETR is connected to GPIO
  2183. * @arg TIM_TIM4_ETR_COMP1 TIM4 ETR is connected to COMP1 output
  2184. * @arg TIM_TIM4_ETR_COMP2 TIM4 ETR is connected to COMP2 output
  2185. * @arg TIM_TIM4_ETR_COMP3 TIM4 ETR is connected to COMP3 output
  2186. * @arg TIM_TIM4_ETR_COMP4 TIM4 ETR is connected to COMP4 output
  2187. * @arg TIM_TIM4_ETR_COMP5 TIM4 ETR is connected to COMP5 output (*)
  2188. * @arg TIM_TIM4_ETR_COMP6 TIM4 ETR is connected to COMP6 output (*)
  2189. * @arg TIM_TIM4_ETR_COMP7 TIM4 ETR is connected to COMP7 output (*)
  2190. * @arg TIM_TIM4_ETR_TIM3_ETR TIM4 ETR is connected to TIM3 ETR pin
  2191. * @arg TIM_TIM4_ETR_TIM5_ETR TIM4 ETR is connected to TIM5 ETR pin (*)
  2192. *
  2193. * For TIM5, the parameter can take one of the following values: (**)
  2194. * @arg TIM_TIM5_ETR_GPIO TIM5 ETR is connected to GPIO (*)
  2195. * @arg TIM_TIM5_ETR_COMP1 TIM5 ETR is connected to COMP1 output (*)
  2196. * @arg TIM_TIM5_ETR_COMP2 TIM5 ETR is connected to COMP2 output (*)
  2197. * @arg TIM_TIM5_ETR_COMP3 TIM5 ETR is connected to COMP3 output (*)
  2198. * @arg TIM_TIM5_ETR_COMP4 TIM5 ETR is connected to COMP4 output (*)
  2199. * @arg TIM_TIM5_ETR_COMP5 TIM5 ETR is connected to COMP5 output (*)
  2200. * @arg TIM_TIM5_ETR_COMP6 TIM5 ETR is connected to COMP6 output (*)
  2201. * @arg TIM_TIM5_ETR_COMP7 TIM5 ETR is connected to COMP7 output (*)
  2202. * @arg TIM_TIM5_ETR_TIM2_ETR TIM5 ETR is connected to TIM2 ETR pin (*)
  2203. * @arg TIM_TIM5_ETR_TIM3_ETR TIM5 ETR is connected to TIM3 ETR pin (*)
  2204. *
  2205. * For TIM8, the parameter can take one of the following values:
  2206. * @arg TIM_TIM8_ETR_GPIO TIM8 ETR is connected to GPIO
  2207. * @arg TIM_TIM8_ETR_COMP1 TIM8 ETR is connected to COMP1 output
  2208. * @arg TIM_TIM8_ETR_COMP2 TIM8 ETR is connected to COMP2 output
  2209. * @arg TIM_TIM8_ETR_COMP3 TIM8 ETR is connected to COMP3 output
  2210. * @arg TIM_TIM8_ETR_COMP4 TIM8 ETR is connected to COMP4 output
  2211. * @arg TIM_TIM8_ETR_COMP5 TIM8 ETR is connected to COMP5 output (*)
  2212. * @arg TIM_TIM8_ETR_COMP6 TIM8 ETR is connected to COMP6 output (*)
  2213. * @arg TIM_TIM8_ETR_COMP7 TIM8 ETR is connected to COMP7 output (*)
  2214. * @arg TIM_TIM8_ETR_ADC2_AWD1 TIM8 ETR is connected to ADC2 AWD1
  2215. * @arg TIM_TIM8_ETR_ADC2_AWD2 TIM8 ETR is connected to ADC2 AWD2
  2216. * @arg TIM_TIM8_ETR_ADC2_AWD3 TIM8 ETR is connected to ADC2 AWD3
  2217. * @arg TIM_TIM8_ETR_ADC3_AWD1 TIM8 ETR is connected to ADC3 AWD1 (*)
  2218. * @arg TIM_TIM8_ETR_ADC3_AWD2 TIM8 ETR is connected to ADC3 AWD2 (*)
  2219. * @arg TIM_TIM8_ETR_ADC3_AWD3 TIM8 ETR is connected to ADC3 AWD3 (*)
  2220. *
  2221. * For TIM20, the parameter can take one of the following values: (**)
  2222. * @arg TIM_TIM20_ETR_GPIO TIM20 ETR is connected to GPIO
  2223. * @arg TIM_TIM20_ETR_COMP1 TIM20 ETR is connected to COMP1 output (*)
  2224. * @arg TIM_TIM20_ETR_COMP2 TIM20 ETR is connected to COMP2 output (*)
  2225. * @arg TIM_TIM20_ETR_COMP3 TIM20 ETR is connected to COMP3 output (*)
  2226. * @arg TIM_TIM20_ETR_COMP4 TIM20 ETR is connected to COMP4 output (*)
  2227. * @arg TIM_TIM20_ETR_COMP5 TIM20 ETR is connected to COMP5 output (*)
  2228. * @arg TIM_TIM20_ETR_COMP6 TIM20 ETR is connected to COMP6 output (*)
  2229. * @arg TIM_TIM20_ETR_COMP7 TIM20 ETR is connected to COMP7 output (*)
  2230. * @arg TIM_TIM20_ETR_ADC3_AWD1 TIM20 ETR is connected to ADC3 AWD1 (*)
  2231. * @arg TIM_TIM20_ETR_ADC3_AWD2 TIM20 ETR is connected to ADC3 AWD2 (*)
  2232. * @arg TIM_TIM20_ETR_ADC3_AWD3 TIM20 ETR is connected to ADC3 AWD3 (*)
  2233. * @arg TIM_TIM20_ETR_ADC5_AWD1 TIM20 ETR is connected to ADC5 AWD1 (*)
  2234. * @arg TIM_TIM20_ETR_ADC5_AWD2 TIM20 ETR is connected to ADC5 AWD2 (*)
  2235. * @arg TIM_TIM20_ETR_ADC5_AWD3 TIM20 ETR is connected to ADC5 AWD3 (*)
  2236. *
  2237. * (*) Value not defined in all devices. \n
  2238. * (**) Register not available in all devices.
  2239. *
  2240. * @retval HAL status
  2241. */
  2242. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  2243. {
  2244. /* Check parameters */
  2245. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  2246. assert_param(IS_TIM_REMAP(Remap));
  2247. __HAL_LOCK(htim);
  2248. MODIFY_REG(htim->Instance->AF1, TIM1_AF1_ETRSEL_Msk, Remap);
  2249. __HAL_UNLOCK(htim);
  2250. return HAL_OK;
  2251. }
  2252. /**
  2253. * @brief Select the timer input source
  2254. * @param htim TIM handle.
  2255. * @param Channel specifies the TIM Channel
  2256. * This parameter can be one of the following values:
  2257. * @arg TIM_CHANNEL_1: TI1 input channel
  2258. * @arg TIM_CHANNEL_2: TI2 input channel
  2259. * @arg TIM_CHANNEL_3: TI3 input channel
  2260. * @arg TIM_CHANNEL_4: TI4 input channel
  2261. * @param TISelection specifies the timer input source
  2262. * For TIM1 this parameter can be one of the following values:
  2263. * @arg TIM_TIM1_TI1_GPIO: TIM1 TI1 is connected to GPIO
  2264. * @arg TIM_TIM1_TI1_COMP1: TIM1 TI1 is connected to COMP1 output
  2265. * @arg TIM_TIM1_TI1_COMP2: TIM1 TI1 is connected to COMP2 output
  2266. * @arg TIM_TIM1_TI1_COMP3: TIM1 TI1 is connected to COMP3 output
  2267. * @arg TIM_TIM1_TI1_COMP4: TIM1 TI1 is connected to COMP4 output
  2268. *
  2269. * For TIM2 this parameter can be one of the following values:
  2270. * @arg TIM_TIM2_TI1_GPIO: TIM2 TI1 is connected to GPIO
  2271. * @arg TIM_TIM2_TI1_COMP1: TIM2 TI1 is connected to COMP1 output
  2272. * @arg TIM_TIM2_TI1_COMP2: TIM2 TI1 is connected to COMP2 output
  2273. * @arg TIM_TIM2_TI1_COMP3: TIM2 TI1 is connected to COMP3 output
  2274. * @arg TIM_TIM2_TI1_COMP4: TIM2 TI1 is connected to COMP4 output
  2275. * @arg TIM_TIM2_TI1_COMP5: TIM2 TI1 is connected to COMP5 output (*)
  2276. *
  2277. * @arg TIM_TIM2_TI2_GPIO: TIM1 TI2 is connected to GPIO
  2278. * @arg TIM_TIM2_TI2_COMP1: TIM2 TI2 is connected to COMP1 output
  2279. * @arg TIM_TIM2_TI2_COMP2: TIM2 TI2 is connected to COMP2 output
  2280. * @arg TIM_TIM2_TI2_COMP3: TIM2 TI2 is connected to COMP3 output
  2281. * @arg TIM_TIM2_TI2_COMP4: TIM2 TI2 is connected to COMP4 output
  2282. * @arg TIM_TIM2_TI2_COMP6: TIM2 TI2 is connected to COMP6 output (*)
  2283. *
  2284. * @arg TIM_TIM2_TI3_GPIO: TIM2 TI3 is connected to GPIO
  2285. * @arg TIM_TIM2_TI3_COMP4: TIM2 TI3 is connected to COMP4 output
  2286. *
  2287. * @arg TIM_TIM2_TI4_GPIO: TIM2 TI4 is connected to GPIO
  2288. * @arg TIM_TIM2_TI4_COMP1: TIM2 TI4 is connected to COMP1 output
  2289. * @arg TIM_TIM2_TI4_COMP2: TIM2 TI4 is connected to COMP2 output
  2290. *
  2291. * For TIM3 this parameter can be one of the following values:
  2292. * @arg TIM_TIM3_TI1_GPIO: TIM3 TI1 is connected to GPIO
  2293. * @arg TIM_TIM3_TI1_COMP1: TIM3 TI1 is connected to COMP1 output
  2294. * @arg TIM_TIM3_TI1_COMP2: TIM3 TI1 is connected to COMP2 output
  2295. * @arg TIM_TIM3_TI1_COMP3: TIM3 TI1 is connected to COMP3 output
  2296. * @arg TIM_TIM3_TI1_COMP4: TIM3 TI1 is connected to COMP4 output
  2297. * @arg TIM_TIM3_TI1_COMP5: TIM3 TI1 is connected to COMP5 output (*)
  2298. * @arg TIM_TIM3_TI1_COMP6: TIM3 TI1 is connected to COMP6 output (*)
  2299. * @arg TIM_TIM3_TI1_COMP7: TIM3 TI1 is connected to COMP7 output (*)
  2300. *
  2301. * @arg TIM_TIM3_TI2_GPIO: TIM3 TI2 is connected to GPIO
  2302. * @arg TIM_TIM3_TI2_COMP1: TIM3 TI2 is connected to COMP1 output
  2303. * @arg TIM_TIM3_TI2_COMP2: TIM3 TI2 is connected to COMP2 output
  2304. * @arg TIM_TIM3_TI2_COMP3: TIM3 TI2 is connected to COMP3 output
  2305. * @arg TIM_TIM3_TI2_COMP4: TIM3 TI2 is connected to COMP4 output
  2306. * @arg TIM_TIM3_TI2_COMP5: TIM3 TI2 is connected to COMP5 output (*)
  2307. * @arg TIM_TIM3_TI2_COMP6: TIM3 TI2 is connected to COMP6 output (*)
  2308. * @arg TIM_TIM3_TI2_COMP7: TIM3 TI2 is connected to COMP7 output (*)
  2309. *
  2310. * @arg TIM_TIM3_TI3_GPIO: TIM3 TI3 is connected to GPIO
  2311. * @arg TIM_TIM3_TI3_COMP3: TIM3 TI3 is connected to COMP3 output
  2312. *
  2313. * For TIM4 this parameter can be one of the following values:
  2314. * @arg TIM_TIM4_TI1_GPIO: TIM4 TI1 is connected to GPIO
  2315. * @arg TIM_TIM4_TI1_COMP1: TIM4 TI1 is connected to COMP1 output
  2316. * @arg TIM_TIM4_TI1_COMP2: TIM4 TI1 is connected to COMP2 output
  2317. * @arg TIM_TIM4_TI1_COMP3: TIM4 TI1 is connected to COMP3 output
  2318. * @arg TIM_TIM4_TI1_COMP4: TIM4 TI1 is connected to COMP4 output
  2319. * @arg TIM_TIM4_TI1_COMP5: TIM4 TI1 is connected to COMP5 output (*)
  2320. * @arg TIM_TIM4_TI1_COMP6: TIM4 TI1 is connected to COMP6 output (*)
  2321. * @arg TIM_TIM4_TI1_COMP7: TIM4 TI1 is connected to COMP7 output (*)
  2322. *
  2323. * @arg TIM_TIM4_TI2_GPIO: TIM4 TI2 is connected to GPIO
  2324. * @arg TIM_TIM4_TI2_COMP1: TIM4 TI2 is connected to COMP1 output
  2325. * @arg TIM_TIM4_TI2_COMP2: TIM4 TI2 is connected to COMP2 output
  2326. * @arg TIM_TIM4_TI2_COMP3: TIM4 TI2 is connected to COMP3 output
  2327. * @arg TIM_TIM4_TI2_COMP4: TIM4 TI2 is connected to COMP4 output
  2328. * @arg TIM_TIM4_TI2_COMP5: TIM4 TI2 is connected to COMP5 output (*)
  2329. * @arg TIM_TIM4_TI2_COMP6: TIM4 TI2 is connected to COMP6 output (*)
  2330. * @arg TIM_TIM4_TI2_COMP7: TIM4 TI2 is connected to COMP7 output (*)
  2331. *
  2332. * @arg TIM_TIM4_TI3_GPIO: TIM4 TI3 is connected to GPIO
  2333. * @arg TIM_TIM4_TI3_COMP5: TIM4 TI3 is connected to COMP5 output (*)
  2334. *
  2335. * @arg TIM_TIM4_TI4_GPIO: TIM4 TI4 is connected to GPIO
  2336. * @arg TIM_TIM4_TI4_COMP6: TIM4 TI4 is connected to COMP6 output (*)
  2337. *
  2338. * For TIM5 this parameter can be one of the following values: (**)
  2339. * @arg TIM_TIM5_TI1_GPIO: TIM5 TI1 is connected to GPIO
  2340. * @arg TIM_TIM5_TI1_LSI: TIM5 TI1 is connected to LSI clock (*)
  2341. * @arg TIM_TIM5_TI1_LSE: TIM5 TI1 is connected to LSE clock (*)
  2342. * @arg TIM_TIM5_TI1_RTC_WK: TIM5 TI1 is connected to RTC Wakeup (*)
  2343. * @arg TIM_TIM5_TI1_COMP1: TIM5 TI1 is connected to COMP1 output (*)
  2344. * @arg TIM_TIM5_TI1_COMP2: TIM5 TI1 is connected to COMP2 output (*)
  2345. * @arg TIM_TIM5_TI1_COMP3: TIM5 TI1 is connected to COMP3 output (*)
  2346. * @arg TIM_TIM5_TI1_COMP4: TIM5 TI1 is connected to COMP4 output (*)
  2347. * @arg TIM_TIM5_TI1_COMP5: TIM5 TI1 is connected to COMP5 output (*)
  2348. * @arg TIM_TIM5_TI1_COMP6: TIM5 TI1 is connected to COMP6 output (*)
  2349. * @arg TIM_TIM5_TI1_COMP7: TIM5 TI1 is connected to COMP7 output (*)
  2350. *
  2351. * @arg TIM_TIM5_TI2_GPIO: TIM5 TI2 is connected to GPIO
  2352. * @arg TIM_TIM5_TI2_COMP1: TIM5 TI2 is connected to COMP1 output
  2353. * @arg TIM_TIM5_TI2_COMP2: TIM5 TI2 is connected to COMP2 output
  2354. * @arg TIM_TIM5_TI2_COMP3: TIM5 TI2 is connected to COMP3 output
  2355. * @arg TIM_TIM5_TI2_COMP4: TIM5 TI2 is connected to COMP4 output
  2356. * @arg TIM_TIM5_TI2_COMP5: TIM5 TI2 is connected to COMP5 output (*)
  2357. * @arg TIM_TIM5_TI2_COMP6: TIM5 TI2 is connected to COMP6 output (*)
  2358. * @arg TIM_TIM5_TI2_COMP7: TIM5 TI2 is connected to COMP7 output (*)
  2359. *
  2360. * For TIM8 this parameter can be one of the following values:
  2361. * @arg TIM_TIM8_TI1_GPIO: TIM8 TI1 is connected to GPIO
  2362. * @arg TIM_TIM8_TI1_COMP1: TIM8 TI1 is connected to COMP1 output
  2363. * @arg TIM_TIM8_TI1_COMP2: TIM8 TI1 is connected to COMP2 output
  2364. * @arg TIM_TIM8_TI1_COMP3: TIM8 TI1 is connected to COMP3 output
  2365. * @arg TIM_TIM8_TI1_COMP4: TIM8 TI1 is connected to COMP4 output
  2366. *
  2367. * For TIM15 this parameter can be one of the following values:
  2368. * @arg TIM_TIM15_TI1_GPIO: TIM15 TI1 is connected to GPIO
  2369. * @arg TIM_TIM15_TI1_LSE: TIM15 TI1 is connected to LSE clock
  2370. * @arg TIM_TIM15_TI1_COMP1: TIM15 TI1 is connected to COMP1 output
  2371. * @arg TIM_TIM15_TI1_COMP2: TIM15 TI1 is connected to COMP2 output
  2372. * @arg TIM_TIM15_TI1_COMP5: TIM15 TI1 is connected to COMP5 output (*)
  2373. * @arg TIM_TIM15_TI1_COMP7: TIM15 TI1 is connected to COMP7 output (*)
  2374. *
  2375. * @arg TIM_TIM15_TI2_GPIO: TIM15 TI2 is connected to GPIO
  2376. * @arg TIM_TIM15_TI2_COMP2: TIM15 TI2 is connected to COMP2 output
  2377. * @arg TIM_TIM15_TI2_COMP3: TIM15 TI2 is connected to COMP3 output
  2378. * @arg TIM_TIM15_TI2_COMP6: TIM15 TI2 is connected to COMP6 output (*)
  2379. * @arg TIM_TIM15_TI2_COMP7: TIM15 TI2 is connected to COMP7 output (*)
  2380. *
  2381. * For TIM16 this parameter can be one of the following values:
  2382. * @arg TIM_TIM16_TI1_GPIO: TIM16 TI1 is connected to GPIO
  2383. * @arg TIM_TIM16_TI1_COMP6: TIM16 TI1 is connected to COMP6 output (*)
  2384. * @arg TIM_TIM16_TI1_MCO: TIM15 TI1 is connected to MCO output
  2385. * @arg TIM_TIM16_TI1_HSE_32: TIM15 TI1 is connected to HSE div 32
  2386. * @arg TIM_TIM16_TI1_RTC_WK: TIM15 TI1 is connected to RTC wakeup
  2387. * @arg TIM_TIM16_TI1_LSE: TIM15 TI1 is connected to LSE clock
  2388. * @arg TIM_TIM16_TI1_LSI: TIM15 TI1 is connected to LSI clock
  2389. *
  2390. * For TIM17 this parameter can be one of the following values:
  2391. * @arg TIM_TIM17_TI1_GPIO: TIM17 TI1 is connected to GPIO
  2392. * @arg TIM_TIM17_TI1_COMP5: TIM17 TI1 is connected to COMP5 output (*)
  2393. * @arg TIM_TIM17_TI1_MCO: TIM17 TI1 is connected to MCO output
  2394. * @arg TIM_TIM17_TI1_HSE_32: TIM17 TI1 is connected to HSE div 32
  2395. * @arg TIM_TIM17_TI1_RTC_WK: TIM17 TI1 is connected to RTC wakeup
  2396. * @arg TIM_TIM17_TI1_LSE: TIM17 TI1 is connected to LSE clock
  2397. * @arg TIM_TIM17_TI1_LSI: TIM17 TI1 is connected to LSI clock
  2398. * For TIM20 this parameter can be one of the following values: (**)
  2399. * @arg TIM_TIM20_TI1_GPIO: TIM20 TI1 is connected to GPIO
  2400. * @arg TIM_TIM20_TI1_COMP1: TIM20 TI1 is connected to COMP1 output (*)
  2401. * @arg TIM_TIM20_TI1_COMP2: TIM20 TI1 is connected to COMP2 output (*)
  2402. * @arg TIM_TIM20_TI1_COMP3: TIM20 TI1 is connected to COMP3 output (*)
  2403. * @arg TIM_TIM20_TI1_COMP4: TIM20 TI1 is connected to COMP4 output (*)
  2404. *
  2405. * (*) Value not defined in all devices. \n
  2406. * (**) Register not available in all devices.
  2407. *
  2408. * @retval HAL status
  2409. */
  2410. HAL_StatusTypeDef HAL_TIMEx_TISelection(TIM_HandleTypeDef *htim, uint32_t TISelection, uint32_t Channel)
  2411. {
  2412. HAL_StatusTypeDef status = HAL_OK;
  2413. /* Check parameters */
  2414. assert_param(IS_TIM_TISEL_TIX_INSTANCE(htim->Instance, Channel));
  2415. assert_param(IS_TIM_TISEL(TISelection));
  2416. __HAL_LOCK(htim);
  2417. switch (Channel)
  2418. {
  2419. case TIM_CHANNEL_1:
  2420. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI1SEL, TISelection);
  2421. /* If required, set OR bit to request HSE/32 clock */
  2422. if (IS_TIM_HSE32_INSTANCE(htim->Instance))
  2423. {
  2424. SET_BIT(htim->Instance->OR, TIM_OR_HSE32EN);
  2425. }
  2426. else
  2427. {
  2428. CLEAR_BIT(htim->Instance->OR, TIM_OR_HSE32EN);
  2429. }
  2430. break;
  2431. case TIM_CHANNEL_2:
  2432. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI2SEL, TISelection);
  2433. break;
  2434. case TIM_CHANNEL_3:
  2435. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI3SEL, TISelection);
  2436. break;
  2437. case TIM_CHANNEL_4:
  2438. MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI4SEL, TISelection);
  2439. break;
  2440. default:
  2441. status = HAL_ERROR;
  2442. break;
  2443. }
  2444. __HAL_UNLOCK(htim);
  2445. return status;
  2446. }
  2447. /**
  2448. * @brief Group channel 5 and channel 1, 2 or 3
  2449. * @param htim TIM handle.
  2450. * @param Channels specifies the reference signal(s) the OC5REF is combined with.
  2451. * This parameter can be any combination of the following values:
  2452. * TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
  2453. * TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
  2454. * TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
  2455. * TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
  2456. * @retval HAL status
  2457. */
  2458. HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t Channels)
  2459. {
  2460. /* Check parameters */
  2461. assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
  2462. assert_param(IS_TIM_GROUPCH5(Channels));
  2463. /* Process Locked */
  2464. __HAL_LOCK(htim);
  2465. htim->State = HAL_TIM_STATE_BUSY;
  2466. /* Clear GC5Cx bit fields */
  2467. htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3 | TIM_CCR5_GC5C2 | TIM_CCR5_GC5C1);
  2468. /* Set GC5Cx bit fields */
  2469. htim->Instance->CCR5 |= Channels;
  2470. /* Change the htim state */
  2471. htim->State = HAL_TIM_STATE_READY;
  2472. __HAL_UNLOCK(htim);
  2473. return HAL_OK;
  2474. }
  2475. /**
  2476. * @brief Disarm the designated break input (when it operates in bidirectional mode).
  2477. * @param htim TIM handle.
  2478. * @param BreakInput Break input to disarm
  2479. * This parameter can be one of the following values:
  2480. * @arg TIM_BREAKINPUT_BRK: Timer break input
  2481. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  2482. * @note The break input can be disarmed only when it is configured in
  2483. * bidirectional mode and when when MOE is reset.
  2484. * @note Purpose is to be able to have the input voltage back to high-state,
  2485. * whatever the time constant on the output .
  2486. * @retval HAL status
  2487. */
  2488. HAL_StatusTypeDef HAL_TIMEx_DisarmBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput)
  2489. {
  2490. HAL_StatusTypeDef status = HAL_OK;
  2491. uint32_t tmpbdtr;
  2492. /* Check the parameters */
  2493. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2494. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  2495. switch (BreakInput)
  2496. {
  2497. case TIM_BREAKINPUT_BRK:
  2498. {
  2499. /* Check initial conditions */
  2500. tmpbdtr = READ_REG(htim->Instance->BDTR);
  2501. if ((READ_BIT(tmpbdtr, TIM_BDTR_BKBID) == TIM_BDTR_BKBID) &&
  2502. (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
  2503. {
  2504. /* Break input BRK is disarmed */
  2505. SET_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM);
  2506. }
  2507. break;
  2508. }
  2509. case TIM_BREAKINPUT_BRK2:
  2510. {
  2511. /* Check initial conditions */
  2512. tmpbdtr = READ_REG(htim->Instance->BDTR);
  2513. if ((READ_BIT(tmpbdtr, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID) &&
  2514. (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
  2515. {
  2516. /* Break input BRK is disarmed */
  2517. SET_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM);
  2518. }
  2519. break;
  2520. }
  2521. default:
  2522. status = HAL_ERROR;
  2523. break;
  2524. }
  2525. return status;
  2526. }
  2527. /**
  2528. * @brief Arm the designated break input (when it operates in bidirectional mode).
  2529. * @param htim TIM handle.
  2530. * @param BreakInput Break input to arm
  2531. * This parameter can be one of the following values:
  2532. * @arg TIM_BREAKINPUT_BRK: Timer break input
  2533. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  2534. * @note Arming is possible at anytime, even if fault is present.
  2535. * @note Break input is automatically armed as soon as MOE bit is set.
  2536. * @retval HAL status
  2537. */
  2538. HAL_StatusTypeDef HAL_TIMEx_ReArmBreakInput(const TIM_HandleTypeDef *htim, uint32_t BreakInput)
  2539. {
  2540. HAL_StatusTypeDef status = HAL_OK;
  2541. uint32_t tickstart;
  2542. /* Check the parameters */
  2543. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2544. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  2545. switch (BreakInput)
  2546. {
  2547. case TIM_BREAKINPUT_BRK:
  2548. {
  2549. /* Check initial conditions */
  2550. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKBID) == TIM_BDTR_BKBID)
  2551. {
  2552. /* Break input BRK is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
  2553. /* Init tickstart for timeout management */
  2554. tickstart = HAL_GetTick();
  2555. while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
  2556. {
  2557. if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
  2558. {
  2559. /* New check to avoid false timeout detection in case of preemption */
  2560. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
  2561. {
  2562. return HAL_TIMEOUT;
  2563. }
  2564. }
  2565. }
  2566. }
  2567. break;
  2568. }
  2569. case TIM_BREAKINPUT_BRK2:
  2570. {
  2571. /* Check initial conditions */
  2572. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID)
  2573. {
  2574. /* Break input BRK2 is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
  2575. /* Init tickstart for timeout management */
  2576. tickstart = HAL_GetTick();
  2577. while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
  2578. {
  2579. if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
  2580. {
  2581. /* New check to avoid false timeout detection in case of preemption */
  2582. if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
  2583. {
  2584. return HAL_TIMEOUT;
  2585. }
  2586. }
  2587. }
  2588. }
  2589. break;
  2590. }
  2591. default:
  2592. status = HAL_ERROR;
  2593. break;
  2594. }
  2595. return status;
  2596. }
  2597. /**
  2598. * @brief Enable dithering
  2599. * @param htim TIM handle
  2600. * @note Main usage is PWM mode
  2601. * @note This function must be called when timer is stopped or disabled (CEN =0)
  2602. * @note If dithering is activated, pay attention to ARR, CCRx, CNT interpretation:
  2603. * - CNT: only CNT[11:0] holds the non-dithered part for 16b timers (or CNT[26:0] for 32b timers)
  2604. * - ARR: ARR[15:4] holds the non-dithered part, and ARR[3:0] the dither part for 16b timers
  2605. * - CCRx: CCRx[15:4] holds the non-dithered part, and CCRx[3:0] the dither part for 16b timers
  2606. * - ARR and CCRx values are limited to 0xFFEF in dithering mode for 16b timers
  2607. * (corresponds to 4094 for the integer part and 15 for the dithered part).
  2608. * @note Macros @ref __HAL_TIM_CALC_PERIOD_DITHER() __HAL_TIM_CALC_DELAY_DITHER() __HAL_TIM_CALC_PULSE_DITHER()
  2609. * can be used to calculate period (ARR) and delay (CCRx) value.
  2610. * @note Enabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
  2611. * @note Enabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
  2612. * So it may be necessary to read ARR value or CCRx value with macros @ref __HAL_TIM_GET_AUTORELOAD()
  2613. * __HAL_TIM_GET_COMPARE() and if necessary update Init structure field htim->Init.Period .
  2614. * @retval HAL status
  2615. */
  2616. HAL_StatusTypeDef HAL_TIMEx_DitheringEnable(TIM_HandleTypeDef *htim)
  2617. {
  2618. /* Check the parameters */
  2619. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2620. SET_BIT(htim->Instance->CR1, TIM_CR1_DITHEN);
  2621. return HAL_OK;
  2622. }
  2623. /**
  2624. * @brief Disable dithering
  2625. * @param htim TIM handle
  2626. * @note This function must be called when timer is stopped or disabled (CEN =0)
  2627. * @note If dithering is activated, pay attention to ARR, CCRx, CNT interpretation:
  2628. * - CNT: only CNT[11:0] holds the non-dithered part for 16b timers (or CNT[26:0] for 32b timers)
  2629. * - ARR: ARR[15:4] holds the non-dithered part, and ARR[3:0] the dither part for 16b timers
  2630. * - CCRx: CCRx[15:4] holds the non-dithered part, and CCRx[3:0] the dither part for 16b timers
  2631. * - ARR and CCRx values are limited to 0xFFEF in dithering mode
  2632. * (corresponds to 4094 for the integer part and 15 for the dithered part).
  2633. * @note Disabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
  2634. * So it may be necessary to read ARR value or CCRx value with macros @ref __HAL_TIM_GET_AUTORELOAD()
  2635. * __HAL_TIM_GET_COMPARE() and if necessary update Init structure field htim->Init.Period .
  2636. * @retval HAL status
  2637. */
  2638. HAL_StatusTypeDef HAL_TIMEx_DitheringDisable(TIM_HandleTypeDef *htim)
  2639. {
  2640. /* Check the parameters */
  2641. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2642. CLEAR_BIT(htim->Instance->CR1, TIM_CR1_DITHEN);
  2643. return HAL_OK;
  2644. }
  2645. /**
  2646. * @brief Initializes the pulse on compare pulse width and pulse prescaler
  2647. * @param htim TIM Output Compare handle
  2648. * @param PulseWidthPrescaler Pulse width prescaler
  2649. * This parameter can be a number between Min_Data = 0x0 and Max_Data = 0x7
  2650. * @param PulseWidth Pulse width
  2651. * This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
  2652. * @retval HAL status
  2653. */
  2654. HAL_StatusTypeDef HAL_TIMEx_OC_ConfigPulseOnCompare(TIM_HandleTypeDef *htim,
  2655. uint32_t PulseWidthPrescaler,
  2656. uint32_t PulseWidth)
  2657. {
  2658. uint32_t tmpecr;
  2659. /* Check the parameters */
  2660. assert_param(IS_TIM_PULSEONCOMPARE_INSTANCE(htim->Instance));
  2661. assert_param(IS_TIM_PULSEONCOMPARE_WIDTH(PulseWidth));
  2662. assert_param(IS_TIM_PULSEONCOMPARE_WIDTHPRESCALER(PulseWidthPrescaler));
  2663. /* Process Locked */
  2664. __HAL_LOCK(htim);
  2665. /* Set the TIM state */
  2666. htim->State = HAL_TIM_STATE_BUSY;
  2667. /* Get the TIMx ECR register value */
  2668. tmpecr = htim->Instance->ECR;
  2669. /* Reset the Pulse width prescaler and the Pulse width */
  2670. tmpecr &= ~(TIM_ECR_PWPRSC | TIM_ECR_PW);
  2671. /* Set the Pulse width prescaler and Pulse width*/
  2672. tmpecr |= PulseWidthPrescaler << TIM_ECR_PWPRSC_Pos;
  2673. tmpecr |= PulseWidth << TIM_ECR_PW_Pos;
  2674. /* Write to TIMx ECR */
  2675. htim->Instance->ECR = tmpecr;
  2676. /* Change the TIM state */
  2677. htim->State = HAL_TIM_STATE_READY;
  2678. /* Release Lock */
  2679. __HAL_UNLOCK(htim);
  2680. return HAL_OK;
  2681. }
  2682. /**
  2683. * @brief Configure preload source of Slave Mode Selection bitfield (SMS in SMCR register)
  2684. * @param htim TIM handle
  2685. * @param Source Source of slave mode selection preload
  2686. * This parameter can be one of the following values:
  2687. * @arg TIM_SMS_PRELOAD_SOURCE_UPDATE: Timer update event is used as source of Slave Mode Selection preload
  2688. * @arg TIM_SMS_PRELOAD_SOURCE_INDEX: Timer index event is used as source of Slave Mode Selection preload
  2689. * @retval HAL status
  2690. */
  2691. HAL_StatusTypeDef HAL_TIMEx_ConfigSlaveModePreload(TIM_HandleTypeDef *htim, uint32_t Source)
  2692. {
  2693. /* Check the parameters */
  2694. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  2695. assert_param(IS_TIM_SLAVE_PRELOAD_SOURCE(Source));
  2696. MODIFY_REG(htim->Instance->SMCR, TIM_SMCR_SMSPS, Source);
  2697. return HAL_OK;
  2698. }
  2699. /**
  2700. * @brief Enable preload of Slave Mode Selection bitfield (SMS in SMCR register)
  2701. * @param htim TIM handle
  2702. * @retval HAL status
  2703. */
  2704. HAL_StatusTypeDef HAL_TIMEx_EnableSlaveModePreload(TIM_HandleTypeDef *htim)
  2705. {
  2706. /* Check the parameters */
  2707. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  2708. SET_BIT(htim->Instance->SMCR, TIM_SMCR_SMSPE);
  2709. return HAL_OK;
  2710. }
  2711. /**
  2712. * @brief Disable preload of Slave Mode Selection bitfield (SMS in SMCR register)
  2713. * @param htim TIM handle
  2714. * @retval HAL status
  2715. */
  2716. HAL_StatusTypeDef HAL_TIMEx_DisableSlaveModePreload(TIM_HandleTypeDef *htim)
  2717. {
  2718. /* Check the parameters */
  2719. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  2720. CLEAR_BIT(htim->Instance->SMCR, TIM_SMCR_SMSPE);
  2721. return HAL_OK;
  2722. }
  2723. /**
  2724. * @brief Enable deadtime preload
  2725. * @param htim TIM handle
  2726. * @retval HAL status
  2727. */
  2728. HAL_StatusTypeDef HAL_TIMEx_EnableDeadTimePreload(TIM_HandleTypeDef *htim)
  2729. {
  2730. /* Check the parameters */
  2731. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2732. SET_BIT(htim->Instance->DTR2, TIM_DTR2_DTPE);
  2733. return HAL_OK;
  2734. }
  2735. /**
  2736. * @brief Disable deadtime preload
  2737. * @param htim TIM handle
  2738. * @retval HAL status
  2739. */
  2740. HAL_StatusTypeDef HAL_TIMEx_DisableDeadTimePreload(TIM_HandleTypeDef *htim)
  2741. {
  2742. /* Check the parameters */
  2743. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2744. CLEAR_BIT(htim->Instance->DTR2, TIM_DTR2_DTPE);
  2745. return HAL_OK;
  2746. }
  2747. /**
  2748. * @brief Configure deadtime
  2749. * @param htim TIM handle
  2750. * @param Deadtime Deadtime value
  2751. * @note This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
  2752. * @retval HAL status
  2753. */
  2754. HAL_StatusTypeDef HAL_TIMEx_ConfigDeadTime(TIM_HandleTypeDef *htim, uint32_t Deadtime)
  2755. {
  2756. /* Check the parameters */
  2757. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2758. assert_param(IS_TIM_DEADTIME(Deadtime));
  2759. MODIFY_REG(htim->Instance->BDTR, TIM_BDTR_DTG, Deadtime);
  2760. return HAL_OK;
  2761. }
  2762. /**
  2763. * @brief Configure asymmetrical deadtime
  2764. * @param htim TIM handle
  2765. * @param FallingDeadtime Falling edge deadtime value
  2766. * @note This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
  2767. * @retval HAL status
  2768. */
  2769. HAL_StatusTypeDef HAL_TIMEx_ConfigAsymmetricalDeadTime(TIM_HandleTypeDef *htim, uint32_t FallingDeadtime)
  2770. {
  2771. /* Check the parameters */
  2772. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2773. assert_param(IS_TIM_DEADTIME(FallingDeadtime));
  2774. MODIFY_REG(htim->Instance->DTR2, TIM_DTR2_DTGF, FallingDeadtime);
  2775. return HAL_OK;
  2776. }
  2777. /**
  2778. * @brief Enable asymmetrical deadtime
  2779. * @param htim TIM handle
  2780. * @retval HAL status
  2781. */
  2782. HAL_StatusTypeDef HAL_TIMEx_EnableAsymmetricalDeadTime(TIM_HandleTypeDef *htim)
  2783. {
  2784. /* Check the parameters */
  2785. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2786. SET_BIT(htim->Instance->DTR2, TIM_DTR2_DTAE);
  2787. return HAL_OK;
  2788. }
  2789. /**
  2790. * @brief Disable asymmetrical deadtime
  2791. * @param htim TIM handle
  2792. * @retval HAL status
  2793. */
  2794. HAL_StatusTypeDef HAL_TIMEx_DisableAsymmetricalDeadTime(TIM_HandleTypeDef *htim)
  2795. {
  2796. /* Check the parameters */
  2797. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  2798. CLEAR_BIT(htim->Instance->DTR2, TIM_DTR2_DTAE);
  2799. return HAL_OK;
  2800. }
  2801. /**
  2802. * @brief Configures the encoder index.
  2803. * @note warning in case of encoder mode clock plus direction
  2804. * @ref TIM_ENCODERMODE_CLOCKPLUSDIRECTION_X1 or @ref TIM_ENCODERMODE_CLOCKPLUSDIRECTION_X2
  2805. * Direction must be set to @ref TIM_ENCODERINDEX_DIRECTION_UP_DOWN
  2806. * @param htim TIM handle.
  2807. * @param sEncoderIndexConfig Encoder index configuration
  2808. * @retval HAL status
  2809. */
  2810. HAL_StatusTypeDef HAL_TIMEx_ConfigEncoderIndex(TIM_HandleTypeDef *htim,
  2811. TIMEx_EncoderIndexConfigTypeDef *sEncoderIndexConfig)
  2812. {
  2813. /* Check the parameters */
  2814. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2815. assert_param(IS_TIM_ENCODERINDEX_POLARITY(sEncoderIndexConfig->Polarity));
  2816. assert_param(IS_TIM_ENCODERINDEX_PRESCALER(sEncoderIndexConfig->Prescaler));
  2817. assert_param(IS_TIM_ENCODERINDEX_FILTER(sEncoderIndexConfig->Filter));
  2818. assert_param(IS_FUNCTIONAL_STATE(sEncoderIndexConfig->FirstIndexEnable));
  2819. assert_param(IS_TIM_ENCODERINDEX_POSITION(sEncoderIndexConfig->Position));
  2820. assert_param(IS_TIM_ENCODERINDEX_DIRECTION(sEncoderIndexConfig->Direction));
  2821. /* Process Locked */
  2822. __HAL_LOCK(htim);
  2823. /* Configures the TIMx External Trigger (ETR) which is used as Index input */
  2824. TIM_ETR_SetConfig(htim->Instance,
  2825. sEncoderIndexConfig->Prescaler,
  2826. sEncoderIndexConfig->Polarity,
  2827. sEncoderIndexConfig->Filter);
  2828. /* Configures the encoder index */
  2829. MODIFY_REG(htim->Instance->ECR,
  2830. TIM_ECR_IDIR_Msk | TIM_ECR_FIDX_Msk | TIM_ECR_IPOS_Msk,
  2831. (sEncoderIndexConfig->Direction |
  2832. ((sEncoderIndexConfig->FirstIndexEnable == ENABLE) ? (0x1U << TIM_ECR_FIDX_Pos) : 0U) |
  2833. sEncoderIndexConfig->Position |
  2834. TIM_ECR_IE));
  2835. __HAL_UNLOCK(htim);
  2836. return HAL_OK;
  2837. }
  2838. /**
  2839. * @brief Enable encoder index
  2840. * @param htim TIM handle
  2841. * @retval HAL status
  2842. */
  2843. HAL_StatusTypeDef HAL_TIMEx_EnableEncoderIndex(TIM_HandleTypeDef *htim)
  2844. {
  2845. /* Check the parameters */
  2846. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2847. SET_BIT(htim->Instance->ECR, TIM_ECR_IE);
  2848. return HAL_OK;
  2849. }
  2850. /**
  2851. * @brief Disable encoder index
  2852. * @param htim TIM handle
  2853. * @retval HAL status
  2854. */
  2855. HAL_StatusTypeDef HAL_TIMEx_DisableEncoderIndex(TIM_HandleTypeDef *htim)
  2856. {
  2857. /* Check the parameters */
  2858. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2859. CLEAR_BIT(htim->Instance->ECR, TIM_ECR_IE);
  2860. return HAL_OK;
  2861. }
  2862. /**
  2863. * @brief Enable encoder first index
  2864. * @param htim TIM handle
  2865. * @retval HAL status
  2866. */
  2867. HAL_StatusTypeDef HAL_TIMEx_EnableEncoderFirstIndex(TIM_HandleTypeDef *htim)
  2868. {
  2869. /* Check the parameters */
  2870. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2871. SET_BIT(htim->Instance->ECR, TIM_ECR_FIDX);
  2872. return HAL_OK;
  2873. }
  2874. /**
  2875. * @brief Disable encoder first index
  2876. * @param htim TIM handle
  2877. * @retval HAL status
  2878. */
  2879. HAL_StatusTypeDef HAL_TIMEx_DisableEncoderFirstIndex(TIM_HandleTypeDef *htim)
  2880. {
  2881. /* Check the parameters */
  2882. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2883. CLEAR_BIT(htim->Instance->ECR, TIM_ECR_FIDX);
  2884. return HAL_OK;
  2885. }
  2886. /**
  2887. * @}
  2888. */
  2889. /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
  2890. * @brief Extended Callbacks functions
  2891. *
  2892. @verbatim
  2893. ==============================================================================
  2894. ##### Extended Callbacks functions #####
  2895. ==============================================================================
  2896. [..]
  2897. This section provides Extended TIM callback functions:
  2898. (+) Timer Commutation callback
  2899. (+) Timer Break callback
  2900. @endverbatim
  2901. * @{
  2902. */
  2903. /**
  2904. * @brief Commutation callback in non-blocking mode
  2905. * @param htim TIM handle
  2906. * @retval None
  2907. */
  2908. __weak void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim)
  2909. {
  2910. /* Prevent unused argument(s) compilation warning */
  2911. UNUSED(htim);
  2912. /* NOTE : This function should not be modified, when the callback is needed,
  2913. the HAL_TIMEx_CommutCallback could be implemented in the user file
  2914. */
  2915. }
  2916. /**
  2917. * @brief Commutation half complete callback in non-blocking mode
  2918. * @param htim TIM handle
  2919. * @retval None
  2920. */
  2921. __weak void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim)
  2922. {
  2923. /* Prevent unused argument(s) compilation warning */
  2924. UNUSED(htim);
  2925. /* NOTE : This function should not be modified, when the callback is needed,
  2926. the HAL_TIMEx_CommutHalfCpltCallback could be implemented in the user file
  2927. */
  2928. }
  2929. /**
  2930. * @brief Break detection callback in non-blocking mode
  2931. * @param htim TIM handle
  2932. * @retval None
  2933. */
  2934. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  2935. {
  2936. /* Prevent unused argument(s) compilation warning */
  2937. UNUSED(htim);
  2938. /* NOTE : This function should not be modified, when the callback is needed,
  2939. the HAL_TIMEx_BreakCallback could be implemented in the user file
  2940. */
  2941. }
  2942. /**
  2943. * @brief Break2 detection callback in non blocking mode
  2944. * @param htim: TIM handle
  2945. * @retval None
  2946. */
  2947. __weak void HAL_TIMEx_Break2Callback(TIM_HandleTypeDef *htim)
  2948. {
  2949. /* Prevent unused argument(s) compilation warning */
  2950. UNUSED(htim);
  2951. /* NOTE : This function Should not be modified, when the callback is needed,
  2952. the HAL_TIMEx_Break2Callback could be implemented in the user file
  2953. */
  2954. }
  2955. /**
  2956. * @brief Encoder index callback in non-blocking mode
  2957. * @param htim TIM handle
  2958. * @retval None
  2959. */
  2960. __weak void HAL_TIMEx_EncoderIndexCallback(TIM_HandleTypeDef *htim)
  2961. {
  2962. /* Prevent unused argument(s) compilation warning */
  2963. UNUSED(htim);
  2964. /* NOTE : This function should not be modified, when the callback is needed,
  2965. the HAL_TIMEx_EncoderIndexCallback could be implemented in the user file
  2966. */
  2967. }
  2968. /**
  2969. * @brief Direction change callback in non-blocking mode
  2970. * @param htim TIM handle
  2971. * @retval None
  2972. */
  2973. __weak void HAL_TIMEx_DirectionChangeCallback(TIM_HandleTypeDef *htim)
  2974. {
  2975. /* Prevent unused argument(s) compilation warning */
  2976. UNUSED(htim);
  2977. /* NOTE : This function should not be modified, when the callback is needed,
  2978. the HAL_TIMEx_DirectionChangeCallback could be implemented in the user file
  2979. */
  2980. }
  2981. /**
  2982. * @brief Index error callback in non-blocking mode
  2983. * @param htim TIM handle
  2984. * @retval None
  2985. */
  2986. __weak void HAL_TIMEx_IndexErrorCallback(TIM_HandleTypeDef *htim)
  2987. {
  2988. /* Prevent unused argument(s) compilation warning */
  2989. UNUSED(htim);
  2990. /* NOTE : This function should not be modified, when the callback is needed,
  2991. the HAL_TIMEx_IndexErrorCallback could be implemented in the user file
  2992. */
  2993. }
  2994. /**
  2995. * @brief Transition error callback in non-blocking mode
  2996. * @param htim TIM handle
  2997. * @retval None
  2998. */
  2999. __weak void HAL_TIMEx_TransitionErrorCallback(TIM_HandleTypeDef *htim)
  3000. {
  3001. /* Prevent unused argument(s) compilation warning */
  3002. UNUSED(htim);
  3003. /* NOTE : This function should not be modified, when the callback is needed,
  3004. the HAL_TIMEx_TransitionErrorCallback could be implemented in the user file
  3005. */
  3006. }
  3007. /**
  3008. * @}
  3009. */
  3010. /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
  3011. * @brief Extended Peripheral State functions
  3012. *
  3013. @verbatim
  3014. ==============================================================================
  3015. ##### Extended Peripheral State functions #####
  3016. ==============================================================================
  3017. [..]
  3018. This subsection permits to get in run-time the status of the peripheral
  3019. and the data flow.
  3020. @endverbatim
  3021. * @{
  3022. */
  3023. /**
  3024. * @brief Return the TIM Hall Sensor interface handle state.
  3025. * @param htim TIM Hall Sensor handle
  3026. * @retval HAL state
  3027. */
  3028. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(const TIM_HandleTypeDef *htim)
  3029. {
  3030. return htim->State;
  3031. }
  3032. /**
  3033. * @brief Return actual state of the TIM complementary channel.
  3034. * @param htim TIM handle
  3035. * @param ChannelN TIM Complementary channel
  3036. * This parameter can be one of the following values:
  3037. * @arg TIM_CHANNEL_1: TIM Channel 1
  3038. * @arg TIM_CHANNEL_2: TIM Channel 2
  3039. * @arg TIM_CHANNEL_3: TIM Channel 3
  3040. * @arg TIM_CHANNEL_4: TIM Channel 4
  3041. * @retval TIM Complementary channel state
  3042. */
  3043. HAL_TIM_ChannelStateTypeDef HAL_TIMEx_GetChannelNState(const TIM_HandleTypeDef *htim, uint32_t ChannelN)
  3044. {
  3045. HAL_TIM_ChannelStateTypeDef channel_state;
  3046. /* Check the parameters */
  3047. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, ChannelN));
  3048. channel_state = TIM_CHANNEL_N_STATE_GET(htim, ChannelN);
  3049. return channel_state;
  3050. }
  3051. /**
  3052. * @}
  3053. */
  3054. /**
  3055. * @}
  3056. */
  3057. /* Private functions ---------------------------------------------------------*/
  3058. /** @defgroup TIMEx_Private_Functions TIM Extended Private Functions
  3059. * @{
  3060. */
  3061. /**
  3062. * @brief TIM DMA Commutation callback.
  3063. * @param hdma pointer to DMA handle.
  3064. * @retval None
  3065. */
  3066. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  3067. {
  3068. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3069. /* Change the htim state */
  3070. htim->State = HAL_TIM_STATE_READY;
  3071. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3072. htim->CommutationCallback(htim);
  3073. #else
  3074. HAL_TIMEx_CommutCallback(htim);
  3075. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3076. }
  3077. /**
  3078. * @brief TIM DMA Commutation half complete callback.
  3079. * @param hdma pointer to DMA handle.
  3080. * @retval None
  3081. */
  3082. void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma)
  3083. {
  3084. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3085. /* Change the htim state */
  3086. htim->State = HAL_TIM_STATE_READY;
  3087. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3088. htim->CommutationHalfCpltCallback(htim);
  3089. #else
  3090. HAL_TIMEx_CommutHalfCpltCallback(htim);
  3091. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3092. }
  3093. /**
  3094. * @brief TIM DMA Delay Pulse complete callback (complementary channel).
  3095. * @param hdma pointer to DMA handle.
  3096. * @retval None
  3097. */
  3098. static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma)
  3099. {
  3100. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3101. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  3102. {
  3103. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  3104. if (hdma->Init.Mode == DMA_NORMAL)
  3105. {
  3106. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  3107. }
  3108. }
  3109. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  3110. {
  3111. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  3112. if (hdma->Init.Mode == DMA_NORMAL)
  3113. {
  3114. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  3115. }
  3116. }
  3117. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  3118. {
  3119. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  3120. if (hdma->Init.Mode == DMA_NORMAL)
  3121. {
  3122. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  3123. }
  3124. }
  3125. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  3126. {
  3127. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  3128. if (hdma->Init.Mode == DMA_NORMAL)
  3129. {
  3130. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  3131. }
  3132. }
  3133. else
  3134. {
  3135. /* nothing to do */
  3136. }
  3137. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3138. htim->PWM_PulseFinishedCallback(htim);
  3139. #else
  3140. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3141. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3142. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3143. }
  3144. /**
  3145. * @brief TIM DMA error callback (complementary channel)
  3146. * @param hdma pointer to DMA handle.
  3147. * @retval None
  3148. */
  3149. static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma)
  3150. {
  3151. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3152. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  3153. {
  3154. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  3155. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  3156. }
  3157. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  3158. {
  3159. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  3160. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  3161. }
  3162. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  3163. {
  3164. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  3165. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  3166. }
  3167. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  3168. {
  3169. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  3170. TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  3171. }
  3172. else
  3173. {
  3174. /* nothing to do */
  3175. }
  3176. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3177. htim->ErrorCallback(htim);
  3178. #else
  3179. HAL_TIM_ErrorCallback(htim);
  3180. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3181. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3182. }
  3183. /**
  3184. * @brief Enables or disables the TIM Capture Compare Channel xN.
  3185. * @param TIMx to select the TIM peripheral
  3186. * @param Channel specifies the TIM Channel
  3187. * This parameter can be one of the following values:
  3188. * @arg TIM_CHANNEL_1: TIM Channel 1
  3189. * @arg TIM_CHANNEL_2: TIM Channel 2
  3190. * @arg TIM_CHANNEL_3: TIM Channel 3
  3191. * @arg TIM_CHANNEL_4: TIM Channel 4
  3192. * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
  3193. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  3194. * @retval None
  3195. */
  3196. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState)
  3197. {
  3198. uint32_t tmp;
  3199. tmp = TIM_CCER_CC1NE << (Channel & 0xFU); /* 0xFU = 15 bits max shift */
  3200. /* Reset the CCxNE Bit */
  3201. TIMx->CCER &= ~tmp;
  3202. /* Set or reset the CCxNE Bit */
  3203. TIMx->CCER |= (uint32_t)(ChannelNState << (Channel & 0xFU)); /* 0xFU = 15 bits max shift */
  3204. }
  3205. /**
  3206. * @}
  3207. */
  3208. #endif /* HAL_TIM_MODULE_ENABLED */
  3209. /**
  3210. * @}
  3211. */
  3212. /**
  3213. * @}
  3214. */