stm32f0xx_hal_tim_ex.c 78 KB

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