py32f002b_hal_tim_ex.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_tim_ex.c
  4. * @author MCU 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(), HAL_TIMEx_OC_Start_IT()
  54. (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(), HAL_TIMEx_PWMN_Start_IT()
  55. (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
  56. (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(), HAL_TIMEx_HallSensor_Start_IT().
  57. @endverbatim
  58. ******************************************************************************
  59. * @attention
  60. *
  61. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  62. * All rights reserved.</center></h2>
  63. *
  64. * This software component is licensed by Puya under BSD 3-Clause license,
  65. * the "License"; You may not use this file except in compliance with the
  66. * License. You may obtain a copy of the License at:
  67. * opensource.org/licenses/BSD-3-Clause
  68. *
  69. ******************************************************************************
  70. * @attention
  71. *
  72. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  73. * All rights reserved.</center></h2>
  74. *
  75. * This software component is licensed by ST under BSD 3-Clause license,
  76. * the "License"; You may not use this file except in compliance with the
  77. * License. You may obtain a copy of the License at:
  78. * opensource.org/licenses/BSD-3-Clause
  79. *
  80. ******************************************************************************
  81. */
  82. /* Includes ------------------------------------------------------------------*/
  83. #include "py32f0xx_hal.h"
  84. /** @addtogroup PY32F002B_HAL_Driver
  85. * @{
  86. */
  87. /** @defgroup TIMEx TIMEx
  88. * @brief TIM Extended HAL module driver
  89. * @{
  90. */
  91. #ifdef HAL_TIM_MODULE_ENABLED
  92. /* Private typedef -----------------------------------------------------------*/
  93. /* Private define ------------------------------------------------------------*/
  94. /* Private macro -------------------------------------------------------------*/
  95. /* Private variables ---------------------------------------------------------*/
  96. /* Private function prototypes -----------------------------------------------*/
  97. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState);
  98. /* Exported functions --------------------------------------------------------*/
  99. /** @defgroup TIMEx_Exported_Functions TIM Extended Exported Functions
  100. * @{
  101. */
  102. /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions
  103. * @brief Timer Hall Sensor functions
  104. *
  105. @verbatim
  106. ==============================================================================
  107. ##### Timer Hall Sensor functions #####
  108. ==============================================================================
  109. [..]
  110. This section provides functions allowing to:
  111. (+) Initialize and configure TIM HAL Sensor.
  112. (+) De-initialize TIM HAL Sensor.
  113. (+) Start the Hall Sensor Interface.
  114. (+) Stop the Hall Sensor Interface.
  115. (+) Start the Hall Sensor Interface and enable interrupts.
  116. (+) Stop the Hall Sensor Interface and disable interrupts.
  117. (+) Start the Hall Sensor Interface and enable DMA transfers.
  118. (+) Stop the Hall Sensor Interface and disable DMA transfers.
  119. @endverbatim
  120. * @{
  121. */
  122. /**
  123. * @brief Initializes the TIM Hall Sensor Interface and initialize the associated handle.
  124. * @param htim TIM Hall Sensor Interface handle
  125. * @param sConfig TIM Hall Sensor configuration structure
  126. * @retval HAL status
  127. */
  128. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef *sConfig)
  129. {
  130. TIM_OC_InitTypeDef OC_Config;
  131. /* Check the TIM handle allocation */
  132. if (htim == NULL)
  133. {
  134. return HAL_ERROR;
  135. }
  136. /* Check the parameters */
  137. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  138. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  139. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  140. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  141. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  142. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  143. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  144. if (htim->State == HAL_TIM_STATE_RESET)
  145. {
  146. /* Allocate lock resource and initialize it */
  147. htim->Lock = HAL_UNLOCKED;
  148. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  149. /* Reset interrupt callbacks to legacy week callbacks */
  150. TIM_ResetCallback(htim);
  151. if (htim->HallSensor_MspInitCallback == NULL)
  152. {
  153. htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit;
  154. }
  155. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  156. htim->HallSensor_MspInitCallback(htim);
  157. #else
  158. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  159. HAL_TIMEx_HallSensor_MspInit(htim);
  160. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  161. }
  162. /* Set the TIM state */
  163. htim->State = HAL_TIM_STATE_BUSY;
  164. /* Configure the Time base in the Encoder Mode */
  165. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  166. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  167. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  168. /* Reset the IC1PSC Bits */
  169. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  170. /* Set the IC1PSC value */
  171. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  172. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  173. htim->Instance->CR2 |= TIM_CR2_TI1S;
  174. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  175. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  176. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  177. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  178. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  179. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  180. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  181. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  182. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  183. OC_Config.OCMode = TIM_OCMODE_PWM2;
  184. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  185. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  186. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  187. OC_Config.Pulse = sConfig->Commutation_Delay;
  188. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  189. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  190. register to 101 */
  191. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  192. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  193. /* Initialize the TIM state*/
  194. htim->State = HAL_TIM_STATE_READY;
  195. return HAL_OK;
  196. }
  197. /**
  198. * @brief DeInitializes the TIM Hall Sensor interface
  199. * @param htim TIM Hall Sensor Interface handle
  200. * @retval HAL status
  201. */
  202. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  203. {
  204. /* Check the parameters */
  205. assert_param(IS_TIM_INSTANCE(htim->Instance));
  206. htim->State = HAL_TIM_STATE_BUSY;
  207. /* Disable the TIM Peripheral Clock */
  208. __HAL_TIM_DISABLE(htim);
  209. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  210. if (htim->HallSensor_MspDeInitCallback == NULL)
  211. {
  212. htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit;
  213. }
  214. /* DeInit the low level hardware */
  215. htim->HallSensor_MspDeInitCallback(htim);
  216. #else
  217. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  218. HAL_TIMEx_HallSensor_MspDeInit(htim);
  219. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  220. /* Change TIM state */
  221. htim->State = HAL_TIM_STATE_RESET;
  222. /* Release Lock */
  223. __HAL_UNLOCK(htim);
  224. return HAL_OK;
  225. }
  226. /**
  227. * @brief Initializes the TIM Hall Sensor MSP.
  228. * @param htim TIM Hall Sensor Interface handle
  229. * @retval None
  230. */
  231. __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
  232. {
  233. /* Prevent unused argument(s) compilation warning */
  234. UNUSED(htim);
  235. /* NOTE : This function should not be modified, when the callback is needed,
  236. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  237. */
  238. }
  239. /**
  240. * @brief DeInitializes TIM Hall Sensor MSP.
  241. * @param htim TIM Hall Sensor Interface handle
  242. * @retval None
  243. */
  244. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  245. {
  246. /* Prevent unused argument(s) compilation warning */
  247. UNUSED(htim);
  248. /* NOTE : This function should not be modified, when the callback is needed,
  249. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  250. */
  251. }
  252. /**
  253. * @brief Starts the TIM Hall Sensor Interface.
  254. * @param htim TIM Hall Sensor Interface handle
  255. * @retval HAL status
  256. */
  257. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  258. {
  259. uint32_t tmpsmcr;
  260. /* Check the parameters */
  261. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  262. /* Enable the Input Capture channel 1
  263. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  264. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  265. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  266. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  267. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  268. {
  269. __HAL_TIM_ENABLE(htim);
  270. }
  271. /* Return function status */
  272. return HAL_OK;
  273. }
  274. /**
  275. * @brief Stops the TIM Hall sensor Interface.
  276. * @param htim TIM Hall Sensor Interface handle
  277. * @retval HAL status
  278. */
  279. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  280. {
  281. /* Check the parameters */
  282. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  283. /* Disable the Input Capture channels 1, 2 and 3
  284. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  285. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  286. /* Disable the Peripheral */
  287. __HAL_TIM_DISABLE(htim);
  288. /* Return function status */
  289. return HAL_OK;
  290. }
  291. /**
  292. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  293. * @param htim TIM Hall Sensor Interface handle
  294. * @retval HAL status
  295. */
  296. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  297. {
  298. uint32_t tmpsmcr;
  299. /* Check the parameters */
  300. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  301. /* Enable the capture compare Interrupts 1 event */
  302. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  303. /* Enable the Input Capture channel 1
  304. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  305. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  306. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  307. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  308. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  309. {
  310. __HAL_TIM_ENABLE(htim);
  311. }
  312. /* Return function status */
  313. return HAL_OK;
  314. }
  315. /**
  316. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  317. * @param htim TIM Hall Sensor Interface handle
  318. * @retval HAL status
  319. */
  320. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  321. {
  322. /* Check the parameters */
  323. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  324. /* Disable the Input Capture channel 1
  325. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  326. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  327. /* Disable the capture compare Interrupts event */
  328. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  329. /* Disable the Peripheral */
  330. __HAL_TIM_DISABLE(htim);
  331. /* Return function status */
  332. return HAL_OK;
  333. }
  334. #if (defined(DMA) || defined(DMA1))
  335. /**
  336. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  337. * @param htim TIM Hall Sensor Interface handle
  338. * @param pData The destination Buffer address.
  339. * @param Length The length of data to be transferred from TIM peripheral to memory.
  340. * @retval HAL status
  341. */
  342. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  343. {
  344. uint32_t tmpsmcr;
  345. /* Check the parameters */
  346. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  347. if (htim->State == HAL_TIM_STATE_BUSY)
  348. {
  349. return HAL_BUSY;
  350. }
  351. else if (htim->State == HAL_TIM_STATE_READY)
  352. {
  353. if (((uint32_t)pData == 0U) && (Length > 0U))
  354. {
  355. return HAL_ERROR;
  356. }
  357. else
  358. {
  359. htim->State = HAL_TIM_STATE_BUSY;
  360. }
  361. }
  362. else
  363. {
  364. /* nothing to do */
  365. }
  366. /* Enable the Input Capture channel 1
  367. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  368. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  369. /* Set the DMA Input Capture 1 Callbacks */
  370. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  371. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  372. /* Set the DMA error callback */
  373. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  374. /* Enable the DMA channel for Capture 1*/
  375. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
  376. {
  377. return HAL_ERROR;
  378. }
  379. /* Enable the capture compare 1 Interrupt */
  380. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  381. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  382. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  383. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  384. {
  385. __HAL_TIM_ENABLE(htim);
  386. }
  387. /* Return function status */
  388. return HAL_OK;
  389. }
  390. /**
  391. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  392. * @param htim TIM Hall Sensor Interface handle
  393. * @retval HAL status
  394. */
  395. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  396. {
  397. /* Check the parameters */
  398. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  399. /* Disable the Input Capture channel 1
  400. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  401. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  402. /* Disable the capture compare Interrupts 1 event */
  403. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  404. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  405. /* Disable the Peripheral */
  406. __HAL_TIM_DISABLE(htim);
  407. /* Return function status */
  408. return HAL_OK;
  409. }
  410. #endif
  411. /**
  412. * @}
  413. */
  414. /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
  415. * @brief Timer Complementary Output Compare functions
  416. *
  417. @verbatim
  418. ==============================================================================
  419. ##### Timer Complementary Output Compare functions #####
  420. ==============================================================================
  421. [..]
  422. This section provides functions allowing to:
  423. (+) Start the Complementary Output Compare/PWM.
  424. (+) Stop the Complementary Output Compare/PWM.
  425. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  426. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  427. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  428. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  429. @endverbatim
  430. * @{
  431. */
  432. /**
  433. * @brief Starts the TIM Output Compare signal generation on the complementary
  434. * output.
  435. * @param htim TIM Output Compare handle
  436. * @param Channel TIM Channel to be enabled
  437. * This parameter can be one of the following values:
  438. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  439. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  440. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  441. * @retval HAL status
  442. */
  443. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  444. {
  445. uint32_t tmpsmcr;
  446. /* Check the parameters */
  447. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  448. /* Enable the Capture compare channel N */
  449. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  450. /* Enable the Main Output */
  451. __HAL_TIM_MOE_ENABLE(htim);
  452. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  453. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  454. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  455. {
  456. __HAL_TIM_ENABLE(htim);
  457. }
  458. /* Return function status */
  459. return HAL_OK;
  460. }
  461. /**
  462. * @brief Stops the TIM Output Compare signal generation on the complementary
  463. * output.
  464. * @param htim TIM handle
  465. * @param Channel TIM Channel to be disabled
  466. * This parameter can be one of the following values:
  467. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  468. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  469. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  470. * @retval HAL status
  471. */
  472. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  473. {
  474. /* Check the parameters */
  475. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  476. /* Disable the Capture compare channel N */
  477. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  478. /* Disable the Main Output */
  479. __HAL_TIM_MOE_DISABLE(htim);
  480. /* Disable the Peripheral */
  481. __HAL_TIM_DISABLE(htim);
  482. /* Return function status */
  483. return HAL_OK;
  484. }
  485. /**
  486. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  487. * on the complementary output.
  488. * @param htim TIM OC handle
  489. * @param Channel TIM Channel to be enabled
  490. * This parameter can be one of the following values:
  491. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  492. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  493. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  494. * @retval HAL status
  495. */
  496. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  497. {
  498. uint32_t tmpsmcr;
  499. /* Check the parameters */
  500. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  501. switch (Channel)
  502. {
  503. case TIM_CHANNEL_1:
  504. {
  505. /* Enable the TIM Output Compare interrupt */
  506. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  507. break;
  508. }
  509. case TIM_CHANNEL_2:
  510. {
  511. /* Enable the TIM Output Compare interrupt */
  512. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  513. break;
  514. }
  515. case TIM_CHANNEL_3:
  516. {
  517. /* Enable the TIM Output Compare interrupt */
  518. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  519. break;
  520. }
  521. default:
  522. break;
  523. }
  524. /* Enable the TIM Break interrupt */
  525. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  526. /* Enable the Capture compare channel N */
  527. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  528. /* Enable the Main Output */
  529. __HAL_TIM_MOE_ENABLE(htim);
  530. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  531. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  532. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  533. {
  534. __HAL_TIM_ENABLE(htim);
  535. }
  536. /* Return function status */
  537. return HAL_OK;
  538. }
  539. /**
  540. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  541. * on the complementary output.
  542. * @param htim TIM Output Compare handle
  543. * @param Channel TIM Channel to be disabled
  544. * This parameter can be one of the following values:
  545. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  546. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  547. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  548. * @retval HAL status
  549. */
  550. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  551. {
  552. uint32_t tmpccer;
  553. /* Check the parameters */
  554. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  555. switch (Channel)
  556. {
  557. case TIM_CHANNEL_1:
  558. {
  559. /* Disable the TIM Output Compare interrupt */
  560. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  561. break;
  562. }
  563. case TIM_CHANNEL_2:
  564. {
  565. /* Disable the TIM Output Compare interrupt */
  566. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  567. break;
  568. }
  569. case TIM_CHANNEL_3:
  570. {
  571. /* Disable the TIM Output Compare interrupt */
  572. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  573. break;
  574. }
  575. default:
  576. break;
  577. }
  578. /* Disable the Capture compare channel N */
  579. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  580. /* Disable the TIM Break interrupt (only if no more channel is active) */
  581. tmpccer = htim->Instance->CCER;
  582. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == (uint32_t)RESET)
  583. {
  584. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  585. }
  586. /* Disable the Main Output */
  587. __HAL_TIM_MOE_DISABLE(htim);
  588. /* Disable the Peripheral */
  589. __HAL_TIM_DISABLE(htim);
  590. /* Return function status */
  591. return HAL_OK;
  592. }
  593. #if (defined(DMA) || defined(DMA1))
  594. /**
  595. * @brief Starts the TIM Output Compare signal generation in DMA mode
  596. * on the complementary output.
  597. * @param htim TIM Output Compare handle
  598. * @param Channel TIM Channel to be enabled
  599. * This parameter can be one of the following values:
  600. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  601. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  602. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  603. * @param pData The source Buffer address.
  604. * @param Length The length of data to be transferred from memory to TIM peripheral
  605. * @retval HAL status
  606. */
  607. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  608. {
  609. uint32_t tmpsmcr;
  610. /* Check the parameters */
  611. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  612. if (htim->State == HAL_TIM_STATE_BUSY)
  613. {
  614. return HAL_BUSY;
  615. }
  616. else if (htim->State == HAL_TIM_STATE_READY)
  617. {
  618. if (((uint32_t)pData == 0U) && (Length > 0U))
  619. {
  620. return HAL_ERROR;
  621. }
  622. else
  623. {
  624. htim->State = HAL_TIM_STATE_BUSY;
  625. }
  626. }
  627. else
  628. {
  629. /* nothing to do */
  630. }
  631. switch (Channel)
  632. {
  633. case TIM_CHANNEL_1:
  634. {
  635. /* Set the DMA compare callbacks */
  636. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  637. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  638. /* Set the DMA error callback */
  639. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  640. /* Enable the DMA channel */
  641. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
  642. {
  643. return HAL_ERROR;
  644. }
  645. /* Enable the TIM Output Compare DMA request */
  646. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  647. break;
  648. }
  649. case TIM_CHANNEL_2:
  650. {
  651. /* Set the DMA compare callbacks */
  652. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  653. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  654. /* Set the DMA error callback */
  655. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  656. /* Enable the DMA channel */
  657. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
  658. {
  659. return HAL_ERROR;
  660. }
  661. /* Enable the TIM Output Compare DMA request */
  662. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  663. break;
  664. }
  665. case TIM_CHANNEL_3:
  666. {
  667. /* Set the DMA compare callbacks */
  668. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  669. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  670. /* Set the DMA error callback */
  671. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  672. /* Enable the DMA channel */
  673. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
  674. {
  675. return HAL_ERROR;
  676. }
  677. /* Enable the TIM Output Compare DMA request */
  678. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  679. break;
  680. }
  681. default:
  682. break;
  683. }
  684. /* Enable the Capture compare channel N */
  685. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  686. /* Enable the Main Output */
  687. __HAL_TIM_MOE_ENABLE(htim);
  688. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  689. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  690. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  691. {
  692. __HAL_TIM_ENABLE(htim);
  693. }
  694. /* Return function status */
  695. return HAL_OK;
  696. }
  697. /**
  698. * @brief Stops the TIM Output Compare signal generation in DMA mode
  699. * on the complementary output.
  700. * @param htim TIM Output Compare handle
  701. * @param Channel TIM Channel to be disabled
  702. * This parameter can be one of the following values:
  703. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  704. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  705. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  706. * @retval HAL status
  707. */
  708. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  709. {
  710. /* Check the parameters */
  711. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  712. switch (Channel)
  713. {
  714. case TIM_CHANNEL_1:
  715. {
  716. /* Disable the TIM Output Compare DMA request */
  717. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  718. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  719. break;
  720. }
  721. case TIM_CHANNEL_2:
  722. {
  723. /* Disable the TIM Output Compare DMA request */
  724. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  725. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  726. break;
  727. }
  728. case TIM_CHANNEL_3:
  729. {
  730. /* Disable the TIM Output Compare DMA request */
  731. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  732. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  733. break;
  734. }
  735. default:
  736. break;
  737. }
  738. /* Disable the Capture compare channel N */
  739. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  740. /* Disable the Main Output */
  741. __HAL_TIM_MOE_DISABLE(htim);
  742. /* Disable the Peripheral */
  743. __HAL_TIM_DISABLE(htim);
  744. /* Change the htim state */
  745. htim->State = HAL_TIM_STATE_READY;
  746. /* Return function status */
  747. return HAL_OK;
  748. }
  749. #endif
  750. /**
  751. * @}
  752. */
  753. /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
  754. * @brief Timer Complementary PWM functions
  755. *
  756. @verbatim
  757. ==============================================================================
  758. ##### Timer Complementary PWM functions #####
  759. ==============================================================================
  760. [..]
  761. This section provides functions allowing to:
  762. (+) Start the Complementary PWM.
  763. (+) Stop the Complementary PWM.
  764. (+) Start the Complementary PWM and enable interrupts.
  765. (+) Stop the Complementary PWM and disable interrupts.
  766. (+) Start the Complementary PWM and enable DMA transfers.
  767. (+) Stop the Complementary PWM and disable DMA transfers.
  768. (+) Start the Complementary Input Capture measurement.
  769. (+) Stop the Complementary Input Capture.
  770. (+) Start the Complementary Input Capture and enable interrupts.
  771. (+) Stop the Complementary Input Capture and disable interrupts.
  772. (+) Start the Complementary Input Capture and enable DMA transfers.
  773. (+) Stop the Complementary Input Capture and disable DMA transfers.
  774. (+) Start the Complementary One Pulse generation.
  775. (+) Stop the Complementary One Pulse.
  776. (+) Start the Complementary One Pulse and enable interrupts.
  777. (+) Stop the Complementary One Pulse and disable interrupts.
  778. @endverbatim
  779. * @{
  780. */
  781. /**
  782. * @brief Starts the PWM signal generation on the complementary output.
  783. * @param htim TIM handle
  784. * @param Channel TIM Channel to be enabled
  785. * This parameter can be one of the following values:
  786. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  787. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  788. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  789. * @retval HAL status
  790. */
  791. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  792. {
  793. uint32_t tmpsmcr;
  794. /* Check the parameters */
  795. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  796. /* Enable the complementary PWM output */
  797. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  798. /* Enable the Main Output */
  799. __HAL_TIM_MOE_ENABLE(htim);
  800. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  801. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  802. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  803. {
  804. __HAL_TIM_ENABLE(htim);
  805. }
  806. /* Return function status */
  807. return HAL_OK;
  808. }
  809. /**
  810. * @brief Stops the PWM signal generation on the complementary output.
  811. * @param htim TIM handle
  812. * @param Channel TIM Channel to be disabled
  813. * This parameter can be one of the following values:
  814. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  815. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  816. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  817. * @retval HAL status
  818. */
  819. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  820. {
  821. /* Check the parameters */
  822. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  823. /* Disable the complementary PWM output */
  824. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  825. /* Disable the Main Output */
  826. __HAL_TIM_MOE_DISABLE(htim);
  827. /* Disable the Peripheral */
  828. __HAL_TIM_DISABLE(htim);
  829. /* Return function status */
  830. return HAL_OK;
  831. }
  832. /**
  833. * @brief Starts the PWM signal generation in interrupt mode on the
  834. * complementary output.
  835. * @param htim TIM handle
  836. * @param Channel TIM Channel to be disabled
  837. * This parameter can be one of the following values:
  838. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  839. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  840. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  841. * @retval HAL status
  842. */
  843. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  844. {
  845. uint32_t tmpsmcr;
  846. /* Check the parameters */
  847. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  848. switch (Channel)
  849. {
  850. case TIM_CHANNEL_1:
  851. {
  852. /* Enable the TIM Capture/Compare 1 interrupt */
  853. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  854. break;
  855. }
  856. case TIM_CHANNEL_2:
  857. {
  858. /* Enable the TIM Capture/Compare 2 interrupt */
  859. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  860. break;
  861. }
  862. case TIM_CHANNEL_3:
  863. {
  864. /* Enable the TIM Capture/Compare 3 interrupt */
  865. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  866. break;
  867. }
  868. default:
  869. break;
  870. }
  871. /* Enable the TIM Break interrupt */
  872. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  873. /* Enable the complementary PWM output */
  874. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  875. /* Enable the Main Output */
  876. __HAL_TIM_MOE_ENABLE(htim);
  877. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  878. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  879. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  880. {
  881. __HAL_TIM_ENABLE(htim);
  882. }
  883. /* Return function status */
  884. return HAL_OK;
  885. }
  886. /**
  887. * @brief Stops the PWM signal generation in interrupt mode on the
  888. * complementary output.
  889. * @param htim TIM handle
  890. * @param Channel TIM Channel to be disabled
  891. * This parameter can be one of the following values:
  892. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  893. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  894. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  895. * @retval HAL status
  896. */
  897. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  898. {
  899. uint32_t tmpccer;
  900. /* Check the parameters */
  901. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  902. switch (Channel)
  903. {
  904. case TIM_CHANNEL_1:
  905. {
  906. /* Disable the TIM Capture/Compare 1 interrupt */
  907. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  908. break;
  909. }
  910. case TIM_CHANNEL_2:
  911. {
  912. /* Disable the TIM Capture/Compare 2 interrupt */
  913. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  914. break;
  915. }
  916. case TIM_CHANNEL_3:
  917. {
  918. /* Disable the TIM Capture/Compare 3 interrupt */
  919. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  920. break;
  921. }
  922. default:
  923. break;
  924. }
  925. /* Disable the complementary PWM output */
  926. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  927. /* Disable the TIM Break interrupt (only if no more channel is active) */
  928. tmpccer = htim->Instance->CCER;
  929. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == (uint32_t)RESET)
  930. {
  931. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  932. }
  933. /* Disable the Main Output */
  934. __HAL_TIM_MOE_DISABLE(htim);
  935. /* Disable the Peripheral */
  936. __HAL_TIM_DISABLE(htim);
  937. /* Return function status */
  938. return HAL_OK;
  939. }
  940. #if (defined(DMA) || defined(DMA1))
  941. /**
  942. * @brief Starts the TIM PWM signal generation in DMA mode on the
  943. * complementary output
  944. * @param htim TIM handle
  945. * @param Channel TIM Channel to be enabled
  946. * This parameter can be one of the following values:
  947. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  948. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  949. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  950. * @param pData The source Buffer address.
  951. * @param Length The length of data to be transferred from memory to TIM peripheral
  952. * @retval HAL status
  953. */
  954. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  955. {
  956. uint32_t tmpsmcr;
  957. /* Check the parameters */
  958. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  959. if (htim->State == HAL_TIM_STATE_BUSY)
  960. {
  961. return HAL_BUSY;
  962. }
  963. else if (htim->State == HAL_TIM_STATE_READY)
  964. {
  965. if (((uint32_t)pData == 0U) && (Length > 0U))
  966. {
  967. return HAL_ERROR;
  968. }
  969. else
  970. {
  971. htim->State = HAL_TIM_STATE_BUSY;
  972. }
  973. }
  974. else
  975. {
  976. /* nothing to do */
  977. }
  978. switch (Channel)
  979. {
  980. case TIM_CHANNEL_1:
  981. {
  982. /* Set the DMA compare callbacks */
  983. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  984. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  985. /* Set the DMA error callback */
  986. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  987. /* Enable the DMA channel */
  988. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
  989. {
  990. return HAL_ERROR;
  991. }
  992. /* Enable the TIM Capture/Compare 1 DMA request */
  993. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  994. break;
  995. }
  996. case TIM_CHANNEL_2:
  997. {
  998. /* Set the DMA compare callbacks */
  999. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1000. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1001. /* Set the DMA error callback */
  1002. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  1003. /* Enable the DMA channel */
  1004. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length) != HAL_OK)
  1005. {
  1006. return HAL_ERROR;
  1007. }
  1008. /* Enable the TIM Capture/Compare 2 DMA request */
  1009. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1010. break;
  1011. }
  1012. case TIM_CHANNEL_3:
  1013. {
  1014. /* Set the DMA compare callbacks */
  1015. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1016. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1017. /* Set the DMA error callback */
  1018. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  1019. /* Enable the DMA channel */
  1020. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3, Length) != HAL_OK)
  1021. {
  1022. return HAL_ERROR;
  1023. }
  1024. /* Enable the TIM Capture/Compare 3 DMA request */
  1025. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1026. break;
  1027. }
  1028. default:
  1029. break;
  1030. }
  1031. /* Enable the complementary PWM output */
  1032. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1033. /* Enable the Main Output */
  1034. __HAL_TIM_MOE_ENABLE(htim);
  1035. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1036. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1037. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1038. {
  1039. __HAL_TIM_ENABLE(htim);
  1040. }
  1041. /* Return function status */
  1042. return HAL_OK;
  1043. }
  1044. /**
  1045. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  1046. * output
  1047. * @param htim TIM handle
  1048. * @param Channel TIM Channel to be disabled
  1049. * This parameter can be one of the following values:
  1050. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1051. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1052. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1053. * @retval HAL status
  1054. */
  1055. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1056. {
  1057. /* Check the parameters */
  1058. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1059. switch (Channel)
  1060. {
  1061. case TIM_CHANNEL_1:
  1062. {
  1063. /* Disable the TIM Capture/Compare 1 DMA request */
  1064. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1065. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1066. break;
  1067. }
  1068. case TIM_CHANNEL_2:
  1069. {
  1070. /* Disable the TIM Capture/Compare 2 DMA request */
  1071. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1072. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1073. break;
  1074. }
  1075. case TIM_CHANNEL_3:
  1076. {
  1077. /* Disable the TIM Capture/Compare 3 DMA request */
  1078. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1079. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1080. break;
  1081. }
  1082. default:
  1083. break;
  1084. }
  1085. /* Disable the complementary PWM output */
  1086. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1087. /* Disable the Main Output */
  1088. __HAL_TIM_MOE_DISABLE(htim);
  1089. /* Disable the Peripheral */
  1090. __HAL_TIM_DISABLE(htim);
  1091. /* Change the htim state */
  1092. htim->State = HAL_TIM_STATE_READY;
  1093. /* Return function status */
  1094. return HAL_OK;
  1095. }
  1096. #endif
  1097. /**
  1098. * @}
  1099. */
  1100. /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
  1101. * @brief Timer Complementary One Pulse functions
  1102. *
  1103. @verbatim
  1104. ==============================================================================
  1105. ##### Timer Complementary One Pulse functions #####
  1106. ==============================================================================
  1107. [..]
  1108. This section provides functions allowing to:
  1109. (+) Start the Complementary One Pulse generation.
  1110. (+) Stop the Complementary One Pulse.
  1111. (+) Start the Complementary One Pulse and enable interrupts.
  1112. (+) Stop the Complementary One Pulse and disable interrupts.
  1113. @endverbatim
  1114. * @{
  1115. */
  1116. /**
  1117. * @brief Starts the TIM One Pulse signal generation on the complementary
  1118. * output.
  1119. * @param htim TIM One Pulse handle
  1120. * @param OutputChannel TIM Channel to be enabled
  1121. * This parameter can be one of the following values:
  1122. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1123. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1124. * @retval HAL status
  1125. */
  1126. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1127. {
  1128. /* Check the parameters */
  1129. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1130. /* Enable the complementary One Pulse output */
  1131. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1132. /* Enable the Main Output */
  1133. __HAL_TIM_MOE_ENABLE(htim);
  1134. /* Return function status */
  1135. return HAL_OK;
  1136. }
  1137. /**
  1138. * @brief Stops the TIM One Pulse signal generation on the complementary
  1139. * output.
  1140. * @param htim TIM One Pulse handle
  1141. * @param OutputChannel TIM Channel to be disabled
  1142. * This parameter can be one of the following values:
  1143. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1144. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1145. * @retval HAL status
  1146. */
  1147. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1148. {
  1149. /* Check the parameters */
  1150. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1151. /* Disable the complementary One Pulse output */
  1152. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1153. /* Disable the Main Output */
  1154. __HAL_TIM_MOE_DISABLE(htim);
  1155. /* Disable the Peripheral */
  1156. __HAL_TIM_DISABLE(htim);
  1157. /* Return function status */
  1158. return HAL_OK;
  1159. }
  1160. /**
  1161. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1162. * complementary channel.
  1163. * @param htim TIM One Pulse handle
  1164. * @param OutputChannel TIM Channel to be enabled
  1165. * This parameter can be one of the following values:
  1166. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1167. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1168. * @retval HAL status
  1169. */
  1170. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1171. {
  1172. /* Check the parameters */
  1173. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1174. /* Enable the TIM Capture/Compare 1 interrupt */
  1175. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1176. /* Enable the TIM Capture/Compare 2 interrupt */
  1177. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1178. /* Enable the complementary One Pulse output */
  1179. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1180. /* Enable the Main Output */
  1181. __HAL_TIM_MOE_ENABLE(htim);
  1182. /* Return function status */
  1183. return HAL_OK;
  1184. }
  1185. /**
  1186. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1187. * complementary channel.
  1188. * @param htim TIM One Pulse handle
  1189. * @param OutputChannel TIM Channel to be disabled
  1190. * This parameter can be one of the following values:
  1191. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1192. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1193. * @retval HAL status
  1194. */
  1195. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1196. {
  1197. /* Check the parameters */
  1198. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1199. /* Disable the TIM Capture/Compare 1 interrupt */
  1200. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1201. /* Disable the TIM Capture/Compare 2 interrupt */
  1202. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1203. /* Disable the complementary One Pulse output */
  1204. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1205. /* Disable the Main Output */
  1206. __HAL_TIM_MOE_DISABLE(htim);
  1207. /* Disable the Peripheral */
  1208. __HAL_TIM_DISABLE(htim);
  1209. /* Return function status */
  1210. return HAL_OK;
  1211. }
  1212. /**
  1213. * @}
  1214. */
  1215. /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
  1216. * @brief Peripheral Control functions
  1217. *
  1218. @verbatim
  1219. ==============================================================================
  1220. ##### Peripheral Control functions #####
  1221. ==============================================================================
  1222. [..]
  1223. This section provides functions allowing to:
  1224. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1225. (+) Configure Output channels for OC and PWM mode.
  1226. (+) Configure Complementary channels, break features and dead time.
  1227. (+) Configure Master synchronization.
  1228. (+) Configure timer remapping capabilities.
  1229. @endverbatim
  1230. * @{
  1231. */
  1232. /**
  1233. * @brief Configure the TIM commutation event sequence.
  1234. * @note This function is mandatory to use the commutation event in order to
  1235. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1236. * the typical use of this feature is with the use of another Timer(interface Timer)
  1237. * configured in Hall sensor interface, this interface Timer will generate the
  1238. * commutation at its TRGO output (connected to Timer used in this function) each time
  1239. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1240. * @param htim TIM handle
  1241. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1242. * This parameter can be one of the following values:
  1243. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1244. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1245. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1246. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1247. * @arg TIM_TS_NONE: No trigger is needed
  1248. * @param CommutationSource the Commutation Event source
  1249. * This parameter can be one of the following values:
  1250. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1251. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1252. * @retval HAL status
  1253. */
  1254. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1255. uint32_t CommutationSource)
  1256. {
  1257. /* Check the parameters */
  1258. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1259. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1260. __HAL_LOCK(htim);
  1261. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1262. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1263. {
  1264. /* Select the Input trigger */
  1265. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1266. htim->Instance->SMCR |= InputTrigger;
  1267. }
  1268. /* Select the Capture Compare preload feature */
  1269. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1270. /* Select the Commutation event source */
  1271. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1272. htim->Instance->CR2 |= CommutationSource;
  1273. /* Disable Commutation Interrupt */
  1274. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1275. #if (defined(DMA) || defined(DNA1))
  1276. /* Disable Commutation DMA request */
  1277. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1278. #endif
  1279. __HAL_UNLOCK(htim);
  1280. return HAL_OK;
  1281. }
  1282. /**
  1283. * @brief Configure the TIM commutation event sequence with interrupt.
  1284. * @note This function is mandatory to use the commutation event in order to
  1285. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1286. * the typical use of this feature is with the use of another Timer(interface Timer)
  1287. * configured in Hall sensor interface, this interface Timer will generate the
  1288. * commutation at its TRGO output (connected to Timer used in this function) each time
  1289. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1290. * @param htim TIM handle
  1291. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1292. * This parameter can be one of the following values:
  1293. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1294. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1295. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1296. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1297. * @arg TIM_TS_NONE: No trigger is needed
  1298. * @param CommutationSource the Commutation Event source
  1299. * This parameter can be one of the following values:
  1300. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1301. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1302. * @retval HAL status
  1303. */
  1304. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1305. uint32_t CommutationSource)
  1306. {
  1307. /* Check the parameters */
  1308. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1309. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1310. __HAL_LOCK(htim);
  1311. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1312. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1313. {
  1314. /* Select the Input trigger */
  1315. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1316. htim->Instance->SMCR |= InputTrigger;
  1317. }
  1318. /* Select the Capture Compare preload feature */
  1319. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1320. /* Select the Commutation event source */
  1321. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1322. htim->Instance->CR2 |= CommutationSource;
  1323. #if (defined(DMA) || defined(DMA1))
  1324. /* Disable Commutation DMA request */
  1325. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  1326. #endif
  1327. /* Enable the Commutation Interrupt */
  1328. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1329. __HAL_UNLOCK(htim);
  1330. return HAL_OK;
  1331. }
  1332. #if (defined(DMA) || defined(DMA1))
  1333. /**
  1334. * @brief Configure the TIM commutation event sequence with DMA.
  1335. * @note This function is mandatory to use the commutation event in order to
  1336. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1337. * the typical use of this feature is with the use of another Timer(interface Timer)
  1338. * configured in Hall sensor interface, this interface Timer will generate the
  1339. * commutation at its TRGO output (connected to Timer used in this function) each time
  1340. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1341. * @note The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1342. * @param htim TIM handle
  1343. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1344. * This parameter can be one of the following values:
  1345. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1346. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1347. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1348. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1349. * @arg TIM_TS_NONE: No trigger is needed
  1350. * @param CommutationSource the Commutation Event source
  1351. * This parameter can be one of the following values:
  1352. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1353. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1354. * @retval HAL status
  1355. */
  1356. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
  1357. uint32_t CommutationSource)
  1358. {
  1359. /* Check the parameters */
  1360. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1361. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1362. __HAL_LOCK(htim);
  1363. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1364. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1365. {
  1366. /* Select the Input trigger */
  1367. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1368. htim->Instance->SMCR |= InputTrigger;
  1369. }
  1370. /* Select the Capture Compare preload feature */
  1371. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1372. /* Select the Commutation event source */
  1373. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1374. htim->Instance->CR2 |= CommutationSource;
  1375. /* Enable the Commutation DMA Request */
  1376. /* Set the DMA Commutation Callback */
  1377. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1378. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
  1379. /* Set the DMA error callback */
  1380. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1381. /* Disable Commutation Interrupt */
  1382. __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  1383. /* Enable the Commutation DMA Request */
  1384. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1385. __HAL_UNLOCK(htim);
  1386. return HAL_OK;
  1387. }
  1388. #endif
  1389. /**
  1390. * @brief Configures the TIM in master mode.
  1391. * @param htim TIM handle.
  1392. * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
  1393. * contains the selected trigger output (TRGO) and the Master/Slave
  1394. * mode.
  1395. * @retval HAL status
  1396. */
  1397. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
  1398. TIM_MasterConfigTypeDef *sMasterConfig)
  1399. {
  1400. uint32_t tmpcr2;
  1401. uint32_t tmpsmcr;
  1402. /* Check the parameters */
  1403. assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
  1404. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1405. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1406. /* Check input state */
  1407. __HAL_LOCK(htim);
  1408. /* Change the handler state */
  1409. htim->State = HAL_TIM_STATE_BUSY;
  1410. /* Get the TIMx CR2 register value */
  1411. tmpcr2 = htim->Instance->CR2;
  1412. /* Get the TIMx SMCR register value */
  1413. tmpsmcr = htim->Instance->SMCR;
  1414. /* Reset the MMS Bits */
  1415. tmpcr2 &= ~TIM_CR2_MMS;
  1416. /* Select the TRGO source */
  1417. tmpcr2 |= sMasterConfig->MasterOutputTrigger;
  1418. /* Update TIMx CR2 */
  1419. htim->Instance->CR2 = tmpcr2;
  1420. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1421. {
  1422. /* Reset the MSM Bit */
  1423. tmpsmcr &= ~TIM_SMCR_MSM;
  1424. /* Set master mode */
  1425. tmpsmcr |= sMasterConfig->MasterSlaveMode;
  1426. /* Update TIMx SMCR */
  1427. htim->Instance->SMCR = tmpsmcr;
  1428. }
  1429. /* Change the htim state */
  1430. htim->State = HAL_TIM_STATE_READY;
  1431. __HAL_UNLOCK(htim);
  1432. return HAL_OK;
  1433. }
  1434. /**
  1435. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1436. * and the AOE(automatic output enable).
  1437. * @param htim TIM handle
  1438. * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
  1439. * contains the BDTR Register configuration information for the TIM peripheral.
  1440. * @note Interrupts can be generated when an active level is detected on the
  1441. * break input, the break 2 input or the system break input. Break
  1442. * interrupt can be enabled by calling the @ref __HAL_TIM_ENABLE_IT macro.
  1443. * @retval HAL status
  1444. */
  1445. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1446. TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
  1447. {
  1448. /* Keep this variable initialized to 0 as it is used to configure BDTR register */
  1449. uint32_t tmpbdtr = 0U;
  1450. /* Check the parameters */
  1451. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1452. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1453. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1454. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1455. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1456. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1457. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1458. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1459. /* Check input state */
  1460. __HAL_LOCK(htim);
  1461. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1462. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1463. /* Set the BDTR bits */
  1464. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  1465. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  1466. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  1467. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  1468. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  1469. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  1470. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  1471. /* Set TIMx_BDTR */
  1472. htim->Instance->BDTR = tmpbdtr;
  1473. __HAL_UNLOCK(htim);
  1474. return HAL_OK;
  1475. }
  1476. #if defined(TIM14)
  1477. /**
  1478. * @brief Configures the TIMx Remapping input capabilities.
  1479. * @param htim TIM handle.
  1480. * @param Remap specifies the TIM remapping source.
  1481. * For TIM14, the parameter can have the following values:
  1482. * @arg TIM_TIM14_GPIO: TIM14 TI1 is connected to GPIO
  1483. * @arg TIM_TIM14_RTC: TIM14 TI1 is connected to RTC_clock
  1484. * @arg TIM_TIM14_HSE: TIM14 TI1 is connected to HSE/32
  1485. * @arg TIM_TIM14_MCO: TIM14 TI1 is connected to MCO
  1486. *
  1487. * @retval HAL status
  1488. */
  1489. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1490. {
  1491. __HAL_LOCK(htim);
  1492. /* Check parameters */
  1493. assert_param(IS_TIM_REMAP(htim->Instance, Remap));
  1494. /* Set the Timer remapping configuration */
  1495. WRITE_REG(htim->Instance->OR, Remap);
  1496. __HAL_UNLOCK(htim);
  1497. return HAL_OK;
  1498. }
  1499. #endif
  1500. /**
  1501. * @}
  1502. */
  1503. /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
  1504. * @brief Extended Callbacks functions
  1505. *
  1506. @verbatim
  1507. ==============================================================================
  1508. ##### Extended Callbacks functions #####
  1509. ==============================================================================
  1510. [..]
  1511. This section provides Extended TIM callback functions:
  1512. (+) Timer Commutation callback
  1513. (+) Timer Break callback
  1514. @endverbatim
  1515. * @{
  1516. */
  1517. /**
  1518. * @brief Hall commutation changed callback in non-blocking mode
  1519. * @param htim TIM handle
  1520. * @retval None
  1521. */
  1522. __weak void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim)
  1523. {
  1524. /* Prevent unused argument(s) compilation warning */
  1525. UNUSED(htim);
  1526. /* NOTE : This function should not be modified, when the callback is needed,
  1527. the HAL_TIMEx_CommutCallback could be implemented in the user file
  1528. */
  1529. }
  1530. /**
  1531. * @brief Hall commutation changed half complete callback in non-blocking mode
  1532. * @param htim TIM handle
  1533. * @retval None
  1534. */
  1535. __weak void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim)
  1536. {
  1537. /* Prevent unused argument(s) compilation warning */
  1538. UNUSED(htim);
  1539. /* NOTE : This function should not be modified, when the callback is needed,
  1540. the HAL_TIMEx_CommutHalfCpltCallback could be implemented in the user file
  1541. */
  1542. }
  1543. /**
  1544. * @brief Hall Break detection callback in non-blocking mode
  1545. * @param htim TIM handle
  1546. * @retval None
  1547. */
  1548. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  1549. {
  1550. /* Prevent unused argument(s) compilation warning */
  1551. UNUSED(htim);
  1552. /* NOTE : This function should not be modified, when the callback is needed,
  1553. the HAL_TIMEx_BreakCallback could be implemented in the user file
  1554. */
  1555. }
  1556. /**
  1557. * @}
  1558. */
  1559. /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
  1560. * @brief Extended Peripheral State functions
  1561. *
  1562. @verbatim
  1563. ==============================================================================
  1564. ##### Extended Peripheral State functions #####
  1565. ==============================================================================
  1566. [..]
  1567. This subsection permits to get in run-time the status of the peripheral
  1568. and the data flow.
  1569. @endverbatim
  1570. * @{
  1571. */
  1572. /**
  1573. * @brief Return the TIM Hall Sensor interface handle state.
  1574. * @param htim TIM Hall Sensor handle
  1575. * @retval HAL state
  1576. */
  1577. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
  1578. {
  1579. return htim->State;
  1580. }
  1581. /**
  1582. * @}
  1583. */
  1584. /**
  1585. * @}
  1586. */
  1587. /* Private functions ---------------------------------------------------------*/
  1588. /** @defgroup TIMEx_Private_Functions TIM Extended Private Functions
  1589. * @{
  1590. */
  1591. #if (defined(DMA) || defined(DMA1))
  1592. /**
  1593. * @brief TIM DMA Commutation callback.
  1594. * @param hdma pointer to DMA handle.
  1595. * @retval None
  1596. */
  1597. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  1598. {
  1599. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1600. /* Change the htim state */
  1601. htim->State = HAL_TIM_STATE_READY;
  1602. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1603. htim->CommutationCallback(htim);
  1604. #else
  1605. HAL_TIMEx_CommutCallback(htim);
  1606. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1607. }
  1608. /**
  1609. * @brief TIM DMA Commutation half complete callback.
  1610. * @param hdma pointer to DMA handle.
  1611. * @retval None
  1612. */
  1613. void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma)
  1614. {
  1615. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1616. /* Change the htim state */
  1617. htim->State = HAL_TIM_STATE_READY;
  1618. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1619. htim->CommutationHalfCpltCallback(htim);
  1620. #else
  1621. HAL_TIMEx_CommutHalfCpltCallback(htim);
  1622. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1623. }
  1624. #endif
  1625. /**
  1626. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1627. * @param TIMx to select the TIM peripheral
  1628. * @param Channel specifies the TIM Channel
  1629. * This parameter can be one of the following values:
  1630. * @arg TIM_CHANNEL_1: TIM Channel 1
  1631. * @arg TIM_CHANNEL_2: TIM Channel 2
  1632. * @arg TIM_CHANNEL_3: TIM Channel 3
  1633. * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
  1634. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  1635. * @retval None
  1636. */
  1637. static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState)
  1638. {
  1639. uint32_t tmp;
  1640. tmp = TIM_CCER_CC1NE << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
  1641. /* Reset the CCxNE Bit */
  1642. TIMx->CCER &= ~tmp;
  1643. /* Set or reset the CCxNE Bit */
  1644. TIMx->CCER |= (uint32_t)(ChannelNState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
  1645. }
  1646. /**
  1647. * @}
  1648. */
  1649. #endif /* HAL_TIM_MODULE_ENABLED */
  1650. /**
  1651. * @}
  1652. */
  1653. /**
  1654. * @}
  1655. */
  1656. /************************ (C) COPYRIGHT Puya *****END OF FILE****/