stm32f7xx_hal_tim_ex.c 88 KB

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