stm32h7xx_hal_tim_ex.c 100 KB

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