stm32f7xx_hal_tim_ex.c 86 KB

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