stm32l4xx_hal_tim_ex.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_tim_ex.c
  4. * @author MCD Application Team
  5. * @brief TIM HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Timer Extended peripheral:
  8. * + Time Hall Sensor Interface Initialization
  9. * + Time Hall Sensor Interface Start
  10. * + Time Complementary signal break and dead time configuration
  11. * + Time Master and Slave synchronization configuration
  12. * + Time Output Compare/PWM Channel Configuration (for channels 5 and 6)
  13. * + Time OCRef clear configuration
  14. * + Timer remapping capabilities configuration
  15. @verbatim
  16. ==============================================================================
  17. ##### TIMER Extended features #####
  18. ==============================================================================
  19. [..]
  20. The Timer Extended features include:
  21. (#) Complementary outputs with programmable dead-time for :
  22. (++) Output Compare
  23. (++) PWM generation (Edge and Center-aligned Mode)
  24. (++) One-pulse mode output
  25. (#) Synchronization circuit to control the timer with external signals and to
  26. interconnect several timers together.
  27. (#) Break input to put the timer output signals in reset state or in a known state.
  28. (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
  29. positioning purposes
  30. ##### How to use this driver #####
  31. ==============================================================================
  32. [..]
  33. (#) Initialize the TIM low level resources by implementing the following functions
  34. depending on the selected feature:
  35. (++) Hall Sensor output : HAL_TIMEx_HallSensor_MspInit()
  36. (#) Initialize the TIM low level resources :
  37. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  38. (##) TIM pins configuration
  39. (+++) Enable the clock for the TIM GPIOs using the following function:
  40. __HAL_RCC_GPIOx_CLK_ENABLE();
  41. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  42. (#) The external Clock can be configured, if needed (the default clock is the
  43. internal clock from the APBx), using the following function:
  44. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  45. any start function.
  46. (#) Configure the TIM in the desired functioning mode using one of the
  47. initialization function of this driver:
  48. (++) HAL_TIMEx_HallSensor_Init() and HAL_TIMEx_ConfigCommutationEvent(): to use the
  49. Timer Hall Sensor Interface and the commutation event with the corresponding
  50. Interrupt and DMA request if needed (Note that One Timer is used to interface
  51. with the Hall sensor Interface and another Timer should be used to use
  52. the commutation event).
  53. (#) Activate the TIM peripheral using one of the start functions:
  54. (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(), HAL_TIMEx_OC_Start_IT()
  55. (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(), HAL_TIMEx_PWMN_Start_IT()
  56. (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
  57. (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(), HAL_TIMEx_HallSensor_Start_IT().
  58. @endverbatim
  59. ******************************************************************************
  60. * @attention
  61. *
  62. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  63. *
  64. * Redistribution and use in source and binary forms, with or without modification,
  65. * are permitted provided that the following conditions are met:
  66. * 1. Redistributions of source code must retain the above copyright notice,
  67. * this list of conditions and the following disclaimer.
  68. * 2. Redistributions in binary form must reproduce the above copyright notice,
  69. * this list of conditions and the following disclaimer in the documentation
  70. * and/or other materials provided with the distribution.
  71. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  72. * may be used to endorse or promote products derived from this software
  73. * without specific prior written permission.
  74. *
  75. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  76. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  77. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  78. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  79. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  80. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  81. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  83. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  84. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  85. *
  86. ******************************************************************************
  87. */
  88. /* Includes ------------------------------------------------------------------*/
  89. #include "stm32l4xx_hal.h"
  90. /** @addtogroup STM32L4xx_HAL_Driver
  91. * @{
  92. */
  93. /** @defgroup TIMEx TIMEx
  94. * @brief TIM Extended HAL module driver
  95. * @{
  96. */
  97. #ifdef HAL_TIM_MODULE_ENABLED
  98. /* Private typedef -----------------------------------------------------------*/
  99. /* Private define ------------------------------------------------------------*/
  100. #define BDTR_BKF_SHIFT (16)
  101. #define BDTR_BK2F_SHIFT (20)
  102. #define TIMx_ETRSEL_MASK ((uint32_t)0x0003C000)
  103. /* Private macro -------------------------------------------------------------*/
  104. /* Private variables ---------------------------------------------------------*/
  105. /* Private function prototypes -----------------------------------------------*/
  106. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState);
  107. /* Private functions ---------------------------------------------------------*/
  108. /* Exported functions --------------------------------------------------------*/
  109. /** @defgroup TIMEx_Exported_Functions TIM Extended Exported Functions
  110. * @{
  111. */
  112. /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions
  113. * @brief Timer Hall Sensor functions
  114. *
  115. @verbatim
  116. ==============================================================================
  117. ##### Timer Hall Sensor functions #####
  118. ==============================================================================
  119. [..]
  120. This section provides functions allowing to:
  121. (+) Initialize and configure TIM HAL Sensor.
  122. (+) De-initialize TIM HAL Sensor.
  123. (+) Start the Hall Sensor Interface.
  124. (+) Stop the Hall Sensor Interface.
  125. (+) Start the Hall Sensor Interface and enable interrupts.
  126. (+) Stop the Hall Sensor Interface and disable interrupts.
  127. (+) Start the Hall Sensor Interface and enable DMA transfers.
  128. (+) Stop the Hall Sensor Interface and disable DMA transfers.
  129. @endverbatim
  130. * @{
  131. */
  132. /**
  133. * @brief Initializes the TIM Hall Sensor Interface and initialize the associated handle.
  134. * @param htim TIM Encoder Interface handle
  135. * @param sConfig TIM Hall Sensor configuration structure
  136. * @retval HAL status
  137. */
  138. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef* sConfig)
  139. {
  140. TIM_OC_InitTypeDef OC_Config;
  141. /* Check the TIM handle allocation */
  142. if(htim == NULL)
  143. {
  144. return HAL_ERROR;
  145. }
  146. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  147. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  148. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  149. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  150. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  151. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  152. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  153. if(htim->State == HAL_TIM_STATE_RESET)
  154. {
  155. /* Allocate lock resource and initialize it */
  156. htim->Lock = HAL_UNLOCKED;
  157. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  158. HAL_TIMEx_HallSensor_MspInit(htim);
  159. }
  160. /* Set the TIM state */
  161. htim->State = HAL_TIM_STATE_BUSY;
  162. /* Configure the Time base in the Encoder Mode */
  163. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  164. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  165. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  166. /* Reset the IC1PSC Bits */
  167. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  168. /* Set the IC1PSC value */
  169. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  170. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  171. htim->Instance->CR2 |= TIM_CR2_TI1S;
  172. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  173. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  174. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  175. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  176. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  177. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  178. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  179. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  180. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  181. OC_Config.OCMode = TIM_OCMODE_PWM2;
  182. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  183. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  184. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  185. OC_Config.Pulse = sConfig->Commutation_Delay;
  186. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  187. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  188. register to 101 */
  189. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  190. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  191. /* Initialize the TIM state*/
  192. htim->State= HAL_TIM_STATE_READY;
  193. return HAL_OK;
  194. }
  195. /**
  196. * @brief DeInitialize the TIM Hall Sensor interface
  197. * @param htim TIM Hall Sensor handle
  198. * @retval HAL status
  199. */
  200. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  201. {
  202. /* Check the parameters */
  203. assert_param(IS_TIM_INSTANCE(htim->Instance));
  204. htim->State = HAL_TIM_STATE_BUSY;
  205. /* Disable the TIM Peripheral Clock */
  206. __HAL_TIM_DISABLE(htim);
  207. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  208. HAL_TIMEx_HallSensor_MspDeInit(htim);
  209. /* Change TIM state */
  210. htim->State = HAL_TIM_STATE_RESET;
  211. /* Release Lock */
  212. __HAL_UNLOCK(htim);
  213. return HAL_OK;
  214. }
  215. /**
  216. * @brief Initializes the TIM Hall Sensor MSP.
  217. * @param htim TIM handle
  218. * @retval None
  219. */
  220. __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
  221. {
  222. /* Prevent unused argument(s) compilation warning */
  223. UNUSED(htim);
  224. /* NOTE : This function should not be modified, when the callback is needed,
  225. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  226. */
  227. }
  228. /**
  229. * @brief DeInitialize TIM Hall Sensor MSP.
  230. * @param htim TIM handle
  231. * @retval None
  232. */
  233. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  234. {
  235. /* Prevent unused argument(s) compilation warning */
  236. UNUSED(htim);
  237. /* NOTE : This function should not be modified, when the callback is needed,
  238. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  239. */
  240. }
  241. /**
  242. * @brief Starts the TIM Hall Sensor Interface.
  243. * @param htim TIM Hall Sensor handle
  244. * @retval HAL status
  245. */
  246. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  247. {
  248. /* Check the parameters */
  249. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  250. /* Enable the Input Capture channel 1
  251. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  252. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  253. /* Enable the Peripheral */
  254. __HAL_TIM_ENABLE(htim);
  255. /* Return function status */
  256. return HAL_OK;
  257. }
  258. /**
  259. * @brief Stops the TIM Hall sensor Interface.
  260. * @param htim TIM Hall Sensor handle
  261. * @retval HAL status
  262. */
  263. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  264. {
  265. /* Check the parameters */
  266. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  267. /* Disable the Input Capture channels 1, 2 and 3
  268. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  269. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  270. /* Disable the Peripheral */
  271. __HAL_TIM_DISABLE(htim);
  272. /* Return function status */
  273. return HAL_OK;
  274. }
  275. /**
  276. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  277. * @param htim TIM Hall Sensor handle
  278. * @retval HAL status
  279. */
  280. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  281. {
  282. /* Check the parameters */
  283. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  284. /* Enable the capture compare Interrupts 1 event */
  285. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  286. /* Enable the Input Capture channel 1
  287. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  288. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  289. /* Enable the Peripheral */
  290. __HAL_TIM_ENABLE(htim);
  291. /* Return function status */
  292. return HAL_OK;
  293. }
  294. /**
  295. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  296. * @param htim TIM handle
  297. * @retval HAL status
  298. */
  299. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  300. {
  301. /* Check the parameters */
  302. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  303. /* Disable the Input Capture channel 1
  304. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  305. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  306. /* Disable the capture compare Interrupts event */
  307. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  308. /* Disable the Peripheral */
  309. __HAL_TIM_DISABLE(htim);
  310. /* Return function status */
  311. return HAL_OK;
  312. }
  313. /**
  314. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  315. * @param htim TIM Hall Sensor handle
  316. * @param pData The destination Buffer address.
  317. * @param Length The length of data to be transferred from TIM peripheral to memory.
  318. * @retval HAL status
  319. */
  320. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  321. {
  322. /* Check the parameters */
  323. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  324. if((htim->State == HAL_TIM_STATE_BUSY))
  325. {
  326. return HAL_BUSY;
  327. }
  328. else if((htim->State == HAL_TIM_STATE_READY))
  329. {
  330. if(((uint32_t)pData == 0 ) && (Length > 0))
  331. {
  332. return HAL_ERROR;
  333. }
  334. else
  335. {
  336. htim->State = HAL_TIM_STATE_BUSY;
  337. }
  338. }
  339. /* Enable the Input Capture channel 1
  340. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  341. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  342. /* Set the DMA Input Capture 1 Callback */
  343. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  344. /* Set the DMA error callback */
  345. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  346. /* Enable the DMA channel for Capture 1*/
  347. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
  348. /* Enable the capture compare 1 Interrupt */
  349. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  350. /* Enable the Peripheral */
  351. __HAL_TIM_ENABLE(htim);
  352. /* Return function status */
  353. return HAL_OK;
  354. }
  355. /**
  356. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  357. * @param htim TIM handle
  358. * @retval HAL status
  359. */
  360. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  361. {
  362. /* Check the parameters */
  363. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
  364. /* Disable the Input Capture channel 1
  365. (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  366. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  367. /* Disable the capture compare Interrupts 1 event */
  368. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  369. /* Disable the Peripheral */
  370. __HAL_TIM_DISABLE(htim);
  371. /* Return function status */
  372. return HAL_OK;
  373. }
  374. /**
  375. * @}
  376. */
  377. /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
  378. * @brief Timer Complementary Output Compare functions
  379. *
  380. @verbatim
  381. ==============================================================================
  382. ##### Timer Complementary Output Compare functions #####
  383. ==============================================================================
  384. [..]
  385. This section provides functions allowing to:
  386. (+) Start the Complementary Output Compare/PWM.
  387. (+) Stop the Complementary Output Compare/PWM.
  388. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  389. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  390. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  391. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  392. @endverbatim
  393. * @{
  394. */
  395. /**
  396. * @brief Starts the TIM Output Compare signal generation on the complementary
  397. * output.
  398. * @param htim TIM Output Compare handle
  399. * @param Channel TIM Channel to be enabled
  400. * This parameter can be one of the following values:
  401. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  402. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  403. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  404. * @retval HAL status
  405. */
  406. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  407. {
  408. /* Check the parameters */
  409. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  410. /* Enable the Capture compare channel N */
  411. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  412. /* Enable the Main Ouput */
  413. __HAL_TIM_MOE_ENABLE(htim);
  414. /* Enable the Peripheral */
  415. __HAL_TIM_ENABLE(htim);
  416. /* Return function status */
  417. return HAL_OK;
  418. }
  419. /**
  420. * @brief Stops the TIM Output Compare signal generation on the complementary
  421. * output.
  422. * @param htim TIM handle
  423. * @param Channel TIM Channel to be disabled
  424. * This parameter can be one of the following values:
  425. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  426. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  427. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  428. * @retval HAL status
  429. */
  430. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  431. {
  432. /* Check the parameters */
  433. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  434. /* Disable the Capture compare channel N */
  435. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  436. /* Disable the Main Ouput */
  437. __HAL_TIM_MOE_DISABLE(htim);
  438. /* Disable the Peripheral */
  439. __HAL_TIM_DISABLE(htim);
  440. /* Return function status */
  441. return HAL_OK;
  442. }
  443. /**
  444. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  445. * on the complementary output.
  446. * @param htim TIM OC handle
  447. * @param Channel TIM Channel to be enabled
  448. * This parameter can be one of the following values:
  449. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  450. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  451. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  452. * @retval HAL status
  453. */
  454. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  455. {
  456. /* Check the parameters */
  457. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  458. switch (Channel)
  459. {
  460. case TIM_CHANNEL_1:
  461. {
  462. /* Enable the TIM Output Compare interrupt */
  463. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  464. }
  465. break;
  466. case TIM_CHANNEL_2:
  467. {
  468. /* Enable the TIM Output Compare interrupt */
  469. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  470. }
  471. break;
  472. case TIM_CHANNEL_3:
  473. {
  474. /* Enable the TIM Output Compare interrupt */
  475. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  476. }
  477. break;
  478. case TIM_CHANNEL_4:
  479. {
  480. /* Enable the TIM Output Compare interrupt */
  481. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  482. }
  483. break;
  484. default:
  485. break;
  486. }
  487. /* Enable the TIM Break interrupt */
  488. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  489. /* Enable the Capture compare channel N */
  490. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  491. /* Enable the Main Ouput */
  492. __HAL_TIM_MOE_ENABLE(htim);
  493. /* Enable the Peripheral */
  494. __HAL_TIM_ENABLE(htim);
  495. /* Return function status */
  496. return HAL_OK;
  497. }
  498. /**
  499. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  500. * on the complementary output.
  501. * @param htim TIM Output Compare handle
  502. * @param Channel TIM Channel to be disabled
  503. * This parameter can be one of the following values:
  504. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  505. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  506. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  507. * @retval HAL status
  508. */
  509. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  510. {
  511. uint32_t tmpccer = 0;
  512. /* Check the parameters */
  513. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  514. switch (Channel)
  515. {
  516. case TIM_CHANNEL_1:
  517. {
  518. /* Disable the TIM Output Compare interrupt */
  519. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  520. }
  521. break;
  522. case TIM_CHANNEL_2:
  523. {
  524. /* Disable the TIM Output Compare interrupt */
  525. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  526. }
  527. break;
  528. case TIM_CHANNEL_3:
  529. {
  530. /* Disable the TIM Output Compare interrupt */
  531. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  532. }
  533. break;
  534. case TIM_CHANNEL_4:
  535. {
  536. /* Disable the TIM Output Compare interrupt */
  537. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  538. }
  539. break;
  540. default:
  541. break;
  542. }
  543. /* Disable the Capture compare channel N */
  544. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  545. /* Disable the TIM Break interrupt (only if no more channel is active) */
  546. tmpccer = htim->Instance->CCER;
  547. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  548. {
  549. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  550. }
  551. /* Disable the Main Ouput */
  552. __HAL_TIM_MOE_DISABLE(htim);
  553. /* Disable the Peripheral */
  554. __HAL_TIM_DISABLE(htim);
  555. /* Return function status */
  556. return HAL_OK;
  557. }
  558. /**
  559. * @brief Starts the TIM Output Compare signal generation in DMA mode
  560. * on the complementary output.
  561. * @param htim TIM Output Compare handle
  562. * @param Channel TIM Channel to be enabled
  563. * This parameter can be one of the following values:
  564. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  565. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  566. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  567. * @param pData The source Buffer address.
  568. * @param Length The length of data to be transferred from memory to TIM peripheral
  569. * @retval HAL status
  570. */
  571. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  572. {
  573. /* Check the parameters */
  574. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  575. if((htim->State == HAL_TIM_STATE_BUSY))
  576. {
  577. return HAL_BUSY;
  578. }
  579. else if((htim->State == HAL_TIM_STATE_READY))
  580. {
  581. if(((uint32_t)pData == 0 ) && (Length > 0))
  582. {
  583. return HAL_ERROR;
  584. }
  585. else
  586. {
  587. htim->State = HAL_TIM_STATE_BUSY;
  588. }
  589. }
  590. switch (Channel)
  591. {
  592. case TIM_CHANNEL_1:
  593. {
  594. /* Set the DMA Period elapsed callback */
  595. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  596. /* Set the DMA error callback */
  597. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  598. /* Enable the DMA channel */
  599. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  600. /* Enable the TIM Output Compare DMA request */
  601. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  602. }
  603. break;
  604. case TIM_CHANNEL_2:
  605. {
  606. /* Set the DMA Period elapsed callback */
  607. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  608. /* Set the DMA error callback */
  609. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  610. /* Enable the DMA channel */
  611. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  612. /* Enable the TIM Output Compare DMA request */
  613. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  614. }
  615. break;
  616. case TIM_CHANNEL_3:
  617. {
  618. /* Set the DMA Period elapsed callback */
  619. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  620. /* Set the DMA error callback */
  621. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  622. /* Enable the DMA channel */
  623. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  624. /* Enable the TIM Output Compare DMA request */
  625. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  626. }
  627. break;
  628. case TIM_CHANNEL_4:
  629. {
  630. /* Set the DMA Period elapsed callback */
  631. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  632. /* Set the DMA error callback */
  633. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  634. /* Enable the DMA channel */
  635. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  636. /* Enable the TIM Output Compare DMA request */
  637. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  638. }
  639. break;
  640. default:
  641. break;
  642. }
  643. /* Enable the Capture compare channel N */
  644. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  645. /* Enable the Main Ouput */
  646. __HAL_TIM_MOE_ENABLE(htim);
  647. /* Enable the Peripheral */
  648. __HAL_TIM_ENABLE(htim);
  649. /* Return function status */
  650. return HAL_OK;
  651. }
  652. /**
  653. * @brief Stops the TIM Output Compare signal generation in DMA mode
  654. * on the complementary output.
  655. * @param htim TIM Output Compare handle
  656. * @param Channel TIM Channel to be disabled
  657. * This parameter can be one of the following values:
  658. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  659. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  660. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  661. * @retval HAL status
  662. */
  663. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  664. {
  665. /* Check the parameters */
  666. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  667. switch (Channel)
  668. {
  669. case TIM_CHANNEL_1:
  670. {
  671. /* Disable the TIM Output Compare DMA request */
  672. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  673. }
  674. break;
  675. case TIM_CHANNEL_2:
  676. {
  677. /* Disable the TIM Output Compare DMA request */
  678. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  679. }
  680. break;
  681. case TIM_CHANNEL_3:
  682. {
  683. /* Disable the TIM Output Compare DMA request */
  684. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  685. }
  686. break;
  687. case TIM_CHANNEL_4:
  688. {
  689. /* Disable the TIM Output Compare interrupt */
  690. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  691. }
  692. break;
  693. default:
  694. break;
  695. }
  696. /* Disable the Capture compare channel N */
  697. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  698. /* Disable the Main Ouput */
  699. __HAL_TIM_MOE_DISABLE(htim);
  700. /* Disable the Peripheral */
  701. __HAL_TIM_DISABLE(htim);
  702. /* Change the htim state */
  703. htim->State = HAL_TIM_STATE_READY;
  704. /* Return function status */
  705. return HAL_OK;
  706. }
  707. /**
  708. * @}
  709. */
  710. /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
  711. * @brief Timer Complementary PWM functions
  712. *
  713. @verbatim
  714. ==============================================================================
  715. ##### Timer Complementary PWM functions #####
  716. ==============================================================================
  717. [..]
  718. This section provides functions allowing to:
  719. (+) Start the Complementary PWM.
  720. (+) Stop the Complementary PWM.
  721. (+) Start the Complementary PWM and enable interrupts.
  722. (+) Stop the Complementary PWM and disable interrupts.
  723. (+) Start the Complementary PWM and enable DMA transfers.
  724. (+) Stop the Complementary PWM and disable DMA transfers.
  725. (+) Start the Complementary Input Capture measurement.
  726. (+) Stop the Complementary Input Capture.
  727. (+) Start the Complementary Input Capture and enable interrupts.
  728. (+) Stop the Complementary Input Capture and disable interrupts.
  729. (+) Start the Complementary Input Capture and enable DMA transfers.
  730. (+) Stop the Complementary Input Capture and disable DMA transfers.
  731. (+) Start the Complementary One Pulse generation.
  732. (+) Stop the Complementary One Pulse.
  733. (+) Start the Complementary One Pulse and enable interrupts.
  734. (+) Stop the Complementary One Pulse and disable interrupts.
  735. @endverbatim
  736. * @{
  737. */
  738. /**
  739. * @brief Starts the PWM signal generation on the complementary output.
  740. * @param htim TIM handle
  741. * @param Channel TIM Channel to be enabled
  742. * This parameter can be one of the following values:
  743. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  744. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  745. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  746. * @retval HAL status
  747. */
  748. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  749. {
  750. /* Check the parameters */
  751. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  752. /* Enable the complementary PWM output */
  753. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  754. /* Enable the Main Ouput */
  755. __HAL_TIM_MOE_ENABLE(htim);
  756. /* Enable the Peripheral */
  757. __HAL_TIM_ENABLE(htim);
  758. /* Return function status */
  759. return HAL_OK;
  760. }
  761. /**
  762. * @brief Stops the PWM signal generation on the complementary output.
  763. * @param htim TIM handle
  764. * @param Channel TIM Channel to be disabled
  765. * This parameter can be one of the following values:
  766. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  767. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  768. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  769. * @retval HAL status
  770. */
  771. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  772. {
  773. /* Check the parameters */
  774. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  775. /* Disable the complementary PWM output */
  776. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  777. /* Disable the Main Ouput */
  778. __HAL_TIM_MOE_DISABLE(htim);
  779. /* Disable the Peripheral */
  780. __HAL_TIM_DISABLE(htim);
  781. /* Return function status */
  782. return HAL_OK;
  783. }
  784. /**
  785. * @brief Starts the PWM signal generation in interrupt mode on the
  786. * complementary output.
  787. * @param htim TIM handle
  788. * @param Channel TIM Channel to be disabled
  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_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  796. {
  797. /* Check the parameters */
  798. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  799. switch (Channel)
  800. {
  801. case TIM_CHANNEL_1:
  802. {
  803. /* Enable the TIM Capture/Compare 1 interrupt */
  804. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  805. }
  806. break;
  807. case TIM_CHANNEL_2:
  808. {
  809. /* Enable the TIM Capture/Compare 2 interrupt */
  810. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  811. }
  812. break;
  813. case TIM_CHANNEL_3:
  814. {
  815. /* Enable the TIM Capture/Compare 3 interrupt */
  816. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  817. }
  818. break;
  819. case TIM_CHANNEL_4:
  820. {
  821. /* Enable the TIM Capture/Compare 4 interrupt */
  822. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  823. }
  824. break;
  825. default:
  826. break;
  827. }
  828. /* Enable the TIM Break interrupt */
  829. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  830. /* Enable the complementary PWM output */
  831. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  832. /* Enable the Main Ouput */
  833. __HAL_TIM_MOE_ENABLE(htim);
  834. /* Enable the Peripheral */
  835. __HAL_TIM_ENABLE(htim);
  836. /* Return function status */
  837. return HAL_OK;
  838. }
  839. /**
  840. * @brief Stops the PWM signal generation in interrupt mode on the
  841. * complementary output.
  842. * @param htim TIM handle
  843. * @param Channel TIM Channel to be disabled
  844. * This parameter can be one of the following values:
  845. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  846. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  847. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  848. * @retval HAL status
  849. */
  850. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
  851. {
  852. uint32_t tmpccer = 0;
  853. /* Check the parameters */
  854. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  855. switch (Channel)
  856. {
  857. case TIM_CHANNEL_1:
  858. {
  859. /* Disable the TIM Capture/Compare 1 interrupt */
  860. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  861. }
  862. break;
  863. case TIM_CHANNEL_2:
  864. {
  865. /* Disable the TIM Capture/Compare 2 interrupt */
  866. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  867. }
  868. break;
  869. case TIM_CHANNEL_3:
  870. {
  871. /* Disable the TIM Capture/Compare 3 interrupt */
  872. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  873. }
  874. break;
  875. case TIM_CHANNEL_4:
  876. {
  877. /* Disable the TIM Capture/Compare 3 interrupt */
  878. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  879. }
  880. break;
  881. default:
  882. break;
  883. }
  884. /* Disable the complementary PWM output */
  885. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  886. /* Disable the TIM Break interrupt (only if no more channel is active) */
  887. tmpccer = htim->Instance->CCER;
  888. if ((tmpccer & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  889. {
  890. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  891. }
  892. /* Disable the Main Ouput */
  893. __HAL_TIM_MOE_DISABLE(htim);
  894. /* Disable the Peripheral */
  895. __HAL_TIM_DISABLE(htim);
  896. /* Return function status */
  897. return HAL_OK;
  898. }
  899. /**
  900. * @brief Starts the TIM PWM signal generation in DMA mode on the
  901. * complementary output
  902. * @param htim TIM handle
  903. * @param Channel TIM Channel to be enabled
  904. * This parameter can be one of the following values:
  905. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  906. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  907. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  908. * @param pData The source Buffer address.
  909. * @param Length The length of data to be transferred from memory to TIM peripheral
  910. * @retval HAL status
  911. */
  912. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  913. {
  914. /* Check the parameters */
  915. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  916. if((htim->State == HAL_TIM_STATE_BUSY))
  917. {
  918. return HAL_BUSY;
  919. }
  920. else if((htim->State == HAL_TIM_STATE_READY))
  921. {
  922. if(((uint32_t)pData == 0 ) && (Length > 0))
  923. {
  924. return HAL_ERROR;
  925. }
  926. else
  927. {
  928. htim->State = HAL_TIM_STATE_BUSY;
  929. }
  930. }
  931. switch (Channel)
  932. {
  933. case TIM_CHANNEL_1:
  934. {
  935. /* Set the DMA Period elapsed callback */
  936. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  937. /* Set the DMA error callback */
  938. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  939. /* Enable the DMA channel */
  940. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  941. /* Enable the TIM Capture/Compare 1 DMA request */
  942. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  943. }
  944. break;
  945. case TIM_CHANNEL_2:
  946. {
  947. /* Set the DMA Period elapsed callback */
  948. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  949. /* Set the DMA error callback */
  950. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  951. /* Enable the DMA channel */
  952. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  953. /* Enable the TIM Capture/Compare 2 DMA request */
  954. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  955. }
  956. break;
  957. case TIM_CHANNEL_3:
  958. {
  959. /* Set the DMA Period elapsed callback */
  960. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  961. /* Set the DMA error callback */
  962. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  963. /* Enable the DMA channel */
  964. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  965. /* Enable the TIM Capture/Compare 3 DMA request */
  966. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  967. }
  968. break;
  969. case TIM_CHANNEL_4:
  970. {
  971. /* Set the DMA Period elapsed callback */
  972. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  973. /* Set the DMA error callback */
  974. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  975. /* Enable the DMA channel */
  976. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  977. /* Enable the TIM Capture/Compare 4 DMA request */
  978. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  979. }
  980. break;
  981. default:
  982. break;
  983. }
  984. /* Enable the complementary PWM output */
  985. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  986. /* Enable the Main Ouput */
  987. __HAL_TIM_MOE_ENABLE(htim);
  988. /* Enable the Peripheral */
  989. __HAL_TIM_ENABLE(htim);
  990. /* Return function status */
  991. return HAL_OK;
  992. }
  993. /**
  994. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  995. * output
  996. * @param htim TIM handle
  997. * @param Channel TIM Channel to be disabled
  998. * This parameter can be one of the following values:
  999. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1000. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1001. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1002. * @retval HAL status
  1003. */
  1004. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1005. {
  1006. /* Check the parameters */
  1007. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1008. switch (Channel)
  1009. {
  1010. case TIM_CHANNEL_1:
  1011. {
  1012. /* Disable the TIM Capture/Compare 1 DMA request */
  1013. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1014. }
  1015. break;
  1016. case TIM_CHANNEL_2:
  1017. {
  1018. /* Disable the TIM Capture/Compare 2 DMA request */
  1019. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1020. }
  1021. break;
  1022. case TIM_CHANNEL_3:
  1023. {
  1024. /* Disable the TIM Capture/Compare 3 DMA request */
  1025. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1026. }
  1027. break;
  1028. case TIM_CHANNEL_4:
  1029. {
  1030. /* Disable the TIM Capture/Compare 4 DMA request */
  1031. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1032. }
  1033. break;
  1034. default:
  1035. break;
  1036. }
  1037. /* Disable the complementary PWM output */
  1038. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1039. /* Disable the Main Ouput */
  1040. __HAL_TIM_MOE_DISABLE(htim);
  1041. /* Disable the Peripheral */
  1042. __HAL_TIM_DISABLE(htim);
  1043. /* Change the htim state */
  1044. htim->State = HAL_TIM_STATE_READY;
  1045. /* Return function status */
  1046. return HAL_OK;
  1047. }
  1048. /**
  1049. * @}
  1050. */
  1051. /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
  1052. * @brief Timer Complementary One Pulse functions
  1053. *
  1054. @verbatim
  1055. ==============================================================================
  1056. ##### Timer Complementary One Pulse functions #####
  1057. ==============================================================================
  1058. [..]
  1059. This section provides functions allowing to:
  1060. (+) Start the Complementary One Pulse generation.
  1061. (+) Stop the Complementary One Pulse.
  1062. (+) Start the Complementary One Pulse and enable interrupts.
  1063. (+) Stop the Complementary One Pulse and disable interrupts.
  1064. @endverbatim
  1065. * @{
  1066. */
  1067. /**
  1068. * @brief Starts the TIM One Pulse signal generation on the complementary
  1069. * output.
  1070. * @param htim TIM One Pulse handle
  1071. * @param OutputChannel TIM Channel to be enabled
  1072. * This parameter can be one of the following values:
  1073. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1074. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1075. * @retval HAL status
  1076. */
  1077. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1078. {
  1079. /* Check the parameters */
  1080. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1081. /* Enable the complementary One Pulse output */
  1082. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1083. /* Enable the Main Ouput */
  1084. __HAL_TIM_MOE_ENABLE(htim);
  1085. /* Return function status */
  1086. return HAL_OK;
  1087. }
  1088. /**
  1089. * @brief Stops the TIM One Pulse signal generation on the complementary
  1090. * output.
  1091. * @param htim TIM One Pulse handle
  1092. * @param OutputChannel TIM Channel to be disabled
  1093. * This parameter can be one of the following values:
  1094. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1095. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1096. * @retval HAL status
  1097. */
  1098. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1099. {
  1100. /* Check the parameters */
  1101. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1102. /* Disable the complementary One Pulse output */
  1103. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1104. /* Disable the Main Ouput */
  1105. __HAL_TIM_MOE_DISABLE(htim);
  1106. /* Disable the Peripheral */
  1107. __HAL_TIM_DISABLE(htim);
  1108. /* Return function status */
  1109. return HAL_OK;
  1110. }
  1111. /**
  1112. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1113. * complementary channel.
  1114. * @param htim TIM One Pulse handle
  1115. * @param OutputChannel TIM Channel to be enabled
  1116. * This parameter can be one of the following values:
  1117. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1118. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1119. * @retval HAL status
  1120. */
  1121. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1122. {
  1123. /* Check the parameters */
  1124. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1125. /* Enable the TIM Capture/Compare 1 interrupt */
  1126. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1127. /* Enable the TIM Capture/Compare 2 interrupt */
  1128. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1129. /* Enable the complementary One Pulse output */
  1130. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1131. /* Enable the Main Ouput */
  1132. __HAL_TIM_MOE_ENABLE(htim);
  1133. /* Return function status */
  1134. return HAL_OK;
  1135. }
  1136. /**
  1137. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1138. * complementary channel.
  1139. * @param htim TIM One Pulse handle
  1140. * @param OutputChannel TIM Channel to be disabled
  1141. * This parameter can be one of the following values:
  1142. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1143. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1144. * @retval HAL status
  1145. */
  1146. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1147. {
  1148. /* Check the parameters */
  1149. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1150. /* Disable the TIM Capture/Compare 1 interrupt */
  1151. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1152. /* Disable the TIM Capture/Compare 2 interrupt */
  1153. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1154. /* Disable the complementary One Pulse output */
  1155. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1156. /* Disable the Main Ouput */
  1157. __HAL_TIM_MOE_DISABLE(htim);
  1158. /* Disable the Peripheral */
  1159. __HAL_TIM_DISABLE(htim);
  1160. /* Return function status */
  1161. return HAL_OK;
  1162. }
  1163. /**
  1164. * @}
  1165. */
  1166. /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
  1167. * @brief Peripheral Control functions
  1168. *
  1169. @verbatim
  1170. ==============================================================================
  1171. ##### Peripheral Control functions #####
  1172. ==============================================================================
  1173. [..]
  1174. This section provides functions allowing to:
  1175. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1176. (+) Configure Output channels for OC and PWM mode.
  1177. (+) Configure Complementary channels, break features and dead time.
  1178. (+) Configure Master synchronization.
  1179. (+) Configure timer remapping capabilities.
  1180. (+) Enable or disable channel grouping
  1181. @endverbatim
  1182. * @{
  1183. */
  1184. /**
  1185. * @brief Configure the TIM commutation event sequence.
  1186. * @note This function is mandatory to use the commutation event in order to
  1187. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1188. * the typical use of this feature is with the use of another Timer(interface Timer)
  1189. * configured in Hall sensor interface, this interface Timer will generate the
  1190. * commutation at its TRGO output (connected to Timer used in this function) each time
  1191. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1192. * @param htim TIM handle
  1193. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1194. * This parameter can be one of the following values:
  1195. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1196. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1197. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1198. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1199. * @arg TIM_TS_NONE: No trigger is needed
  1200. * @param CommutationSource the Commutation Event source
  1201. * This parameter can be one of the following values:
  1202. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1203. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1204. * @retval HAL status
  1205. */
  1206. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1207. {
  1208. /* Check the parameters */
  1209. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1210. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1211. __HAL_LOCK(htim);
  1212. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1213. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1214. {
  1215. /* Select the Input trigger */
  1216. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1217. htim->Instance->SMCR |= InputTrigger;
  1218. }
  1219. /* Select the Capture Compare preload feature */
  1220. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1221. /* Select the Commutation event source */
  1222. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1223. htim->Instance->CR2 |= CommutationSource;
  1224. __HAL_UNLOCK(htim);
  1225. return HAL_OK;
  1226. }
  1227. /**
  1228. * @brief Configure the TIM commutation event sequence with interrupt.
  1229. * @note This function is mandatory to use the commutation event in order to
  1230. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1231. * the typical use of this feature is with the use of another Timer(interface Timer)
  1232. * configured in Hall sensor interface, this interface Timer will generate the
  1233. * commutation at its TRGO output (connected to Timer used in this function) each time
  1234. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1235. * @param htim TIM handle
  1236. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1237. * This parameter can be one of the following values:
  1238. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1239. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1240. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1241. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1242. * @arg TIM_TS_NONE: No trigger is needed
  1243. * @param CommutationSource the Commutation Event source
  1244. * This parameter can be one of the following values:
  1245. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1246. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1247. * @retval HAL status
  1248. */
  1249. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1250. {
  1251. /* Check the parameters */
  1252. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1253. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1254. __HAL_LOCK(htim);
  1255. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1256. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1257. {
  1258. /* Select the Input trigger */
  1259. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1260. htim->Instance->SMCR |= InputTrigger;
  1261. }
  1262. /* Select the Capture Compare preload feature */
  1263. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1264. /* Select the Commutation event source */
  1265. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1266. htim->Instance->CR2 |= CommutationSource;
  1267. /* Enable the Commutation Interrupt Request */
  1268. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1269. __HAL_UNLOCK(htim);
  1270. return HAL_OK;
  1271. }
  1272. /**
  1273. * @brief Configure the TIM commutation event sequence with DMA.
  1274. * @note This function is mandatory to use the commutation event in order to
  1275. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1276. * the typical use of this feature is with the use of another Timer(interface Timer)
  1277. * configured in Hall sensor interface, this interface Timer will generate the
  1278. * commutation at its TRGO output (connected to Timer used in this function) each time
  1279. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1280. * @note The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1281. * @param htim TIM handle
  1282. * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
  1283. * This parameter can be one of the following values:
  1284. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1285. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1286. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1287. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1288. * @arg TIM_TS_NONE: No trigger is needed
  1289. * @param CommutationSource the Commutation Event source
  1290. * This parameter can be one of the following values:
  1291. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1292. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1293. * @retval HAL status
  1294. */
  1295. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1296. {
  1297. /* Check the parameters */
  1298. assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
  1299. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1300. __HAL_LOCK(htim);
  1301. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1302. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1303. {
  1304. /* Select the Input trigger */
  1305. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1306. htim->Instance->SMCR |= InputTrigger;
  1307. }
  1308. /* Select the Capture Compare preload feature */
  1309. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1310. /* Select the Commutation event source */
  1311. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1312. htim->Instance->CR2 |= CommutationSource;
  1313. /* Enable the Commutation DMA Request */
  1314. /* Set the DMA Commutation Callback */
  1315. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1316. /* Set the DMA error callback */
  1317. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1318. /* Enable the Commutation DMA Request */
  1319. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1320. __HAL_UNLOCK(htim);
  1321. return HAL_OK;
  1322. }
  1323. /**
  1324. * @brief Configures the TIM in master mode.
  1325. * @param htim TIM handle.
  1326. * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
  1327. * contains the selected trigger output (TRGO) and the Master/Slave
  1328. * mode.
  1329. * @retval HAL status
  1330. */
  1331. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
  1332. TIM_MasterConfigTypeDef * sMasterConfig)
  1333. {
  1334. uint32_t tmpcr2;
  1335. uint32_t tmpsmcr;
  1336. /* Check the parameters */
  1337. assert_param(IS_TIM_SYNCHRO_INSTANCE(htim->Instance));
  1338. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1339. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1340. /* Check input state */
  1341. __HAL_LOCK(htim);
  1342. /* Get the TIMx CR2 register value */
  1343. tmpcr2 = htim->Instance->CR2;
  1344. /* Get the TIMx SMCR register value */
  1345. tmpsmcr = htim->Instance->SMCR;
  1346. /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
  1347. if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
  1348. {
  1349. /* Check the parameters */
  1350. assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
  1351. /* Clear the MMS2 bits */
  1352. tmpcr2 &= ~TIM_CR2_MMS2;
  1353. /* Select the TRGO2 source*/
  1354. tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
  1355. }
  1356. /* Reset the MMS Bits */
  1357. tmpcr2 &= ~TIM_CR2_MMS;
  1358. /* Select the TRGO source */
  1359. tmpcr2 |= sMasterConfig->MasterOutputTrigger;
  1360. /* Reset the MSM Bit */
  1361. tmpsmcr &= ~TIM_SMCR_MSM;
  1362. /* Set master mode */
  1363. tmpsmcr |= sMasterConfig->MasterSlaveMode;
  1364. /* Update TIMx CR2 */
  1365. htim->Instance->CR2 = tmpcr2;
  1366. /* Update TIMx SMCR */
  1367. htim->Instance->SMCR = tmpsmcr;
  1368. __HAL_UNLOCK(htim);
  1369. return HAL_OK;
  1370. }
  1371. /**
  1372. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1373. * and the AOE(automatic output enable).
  1374. * @param htim TIM handle
  1375. * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
  1376. * contains the BDTR Register configuration information for the TIM peripheral.
  1377. * @retval HAL status
  1378. */
  1379. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1380. TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)
  1381. {
  1382. uint32_t tmpbdtr = 0;
  1383. /* Check the parameters */
  1384. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1385. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1386. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1387. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1388. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1389. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1390. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1391. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
  1392. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1393. /* Check input state */
  1394. __HAL_LOCK(htim);
  1395. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1396. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1397. /* Set the BDTR bits */
  1398. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  1399. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  1400. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  1401. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  1402. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  1403. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  1404. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  1405. MODIFY_REG(tmpbdtr, TIM_BDTR_MOE, sBreakDeadTimeConfig->AutomaticOutput);
  1406. MODIFY_REG(tmpbdtr, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << BDTR_BKF_SHIFT));
  1407. if (IS_TIM_BKIN2_INSTANCE(htim->Instance))
  1408. {
  1409. /* Check the parameters */
  1410. assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
  1411. assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
  1412. assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
  1413. /* Set the BREAK2 input related BDTR bits */
  1414. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2F, (sBreakDeadTimeConfig->Break2Filter << BDTR_BK2F_SHIFT));
  1415. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2E, sBreakDeadTimeConfig->Break2State);
  1416. MODIFY_REG(tmpbdtr, TIM_BDTR_BK2P, sBreakDeadTimeConfig->Break2Polarity);
  1417. }
  1418. /* Set TIMx_BDTR */
  1419. htim->Instance->BDTR = tmpbdtr;
  1420. __HAL_UNLOCK(htim);
  1421. return HAL_OK;
  1422. }
  1423. /**
  1424. * @brief Configures the break input source.
  1425. * @param htim TIM handle.
  1426. * @param BreakInput Break input to configure
  1427. * This parameter can be one of the following values:
  1428. * @arg TIM_BREAKINPUT_BRK: Timer break input
  1429. * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
  1430. * @param sBreakInputConfig Break input source configuration
  1431. * @retval HAL status
  1432. */
  1433. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim,
  1434. uint32_t BreakInput,
  1435. TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
  1436. {
  1437. uint32_t tmporx = 0;
  1438. uint32_t bkin_enable_mask = 0;
  1439. uint32_t bkin_polarity_mask = 0;
  1440. uint32_t bkin_enable_bitpos = 0;
  1441. uint32_t bkin_polarity_bitpos = 0;
  1442. /* Check the parameters */
  1443. assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
  1444. assert_param(IS_TIM_BREAKINPUT(BreakInput));
  1445. assert_param(IS_TIM_BREAKINPUTSOURCE(sBreakInputConfig->Source));
  1446. assert_param(IS_TIM_BREAKINPUTSOURCE_STATE(sBreakInputConfig->Enable));
  1447. #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  1448. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  1449. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  1450. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  1451. if (sBreakInputConfig->Source != TIM_BREAKINPUTSOURCE_DFSDM1)
  1452. {
  1453. assert_param(IS_TIM_BREAKINPUTSOURCE_POLARITY(sBreakInputConfig->Polarity));
  1454. }
  1455. #else
  1456. assert_param(IS_TIM_BREAKINPUTSOURCE_POLARITY(sBreakInputConfig->Polarity));
  1457. #endif /* STM32L451xx || STM32L452xx || STM32L462xx || */
  1458. /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  1459. /* STM32L496xx || STM32L4A6xx || */
  1460. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1461. /* Check input state */
  1462. __HAL_LOCK(htim);
  1463. switch(sBreakInputConfig->Source)
  1464. {
  1465. case TIM_BREAKINPUTSOURCE_BKIN:
  1466. {
  1467. bkin_enable_mask = TIM1_OR2_BKINE;
  1468. bkin_enable_bitpos = 0;
  1469. bkin_polarity_mask = TIM1_OR2_BKINP;
  1470. bkin_polarity_bitpos = 9;
  1471. }
  1472. break;
  1473. case TIM_BREAKINPUTSOURCE_COMP1:
  1474. {
  1475. bkin_enable_mask = TIM1_OR2_BKCMP1E;
  1476. bkin_enable_bitpos = 1;
  1477. bkin_polarity_mask = TIM1_OR2_BKCMP1P;
  1478. bkin_polarity_bitpos = 10;
  1479. }
  1480. break;
  1481. case TIM_BREAKINPUTSOURCE_COMP2:
  1482. {
  1483. bkin_enable_mask = TIM1_OR2_BKCMP2E;
  1484. bkin_enable_bitpos = 2;
  1485. bkin_polarity_mask = TIM1_OR2_BKCMP2P;
  1486. bkin_polarity_bitpos = 11;
  1487. }
  1488. break;
  1489. #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  1490. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  1491. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  1492. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  1493. case TIM_BREAKINPUTSOURCE_DFSDM1:
  1494. {
  1495. bkin_enable_mask = TIM1_OR2_BKDF1BK0E;
  1496. bkin_enable_bitpos = 8;
  1497. }
  1498. break;
  1499. #endif /* STM32L451xx || STM32L452xx || STM32L462xx || */
  1500. /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  1501. /* STM32L496xx || STM32L4A6xx || */
  1502. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1503. default:
  1504. break;
  1505. }
  1506. switch(BreakInput)
  1507. {
  1508. case TIM_BREAKINPUT_BRK:
  1509. {
  1510. /* Get the TIMx_OR2 register value */
  1511. tmporx = htim->Instance->OR2;
  1512. /* Enable the break input */
  1513. tmporx &= ~bkin_enable_mask;
  1514. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1515. /* Set the break input polarity */
  1516. #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  1517. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  1518. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  1519. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  1520. if (sBreakInputConfig->Source != TIM_BREAKINPUTSOURCE_DFSDM1)
  1521. #endif /* STM32L451xx || STM32L452xx || STM32L462xx || */
  1522. /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  1523. /* STM32L496xx || STM32L4A6xx || */
  1524. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1525. {
  1526. tmporx &= ~bkin_polarity_mask;
  1527. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  1528. }
  1529. /* Set TIMx_OR2 */
  1530. htim->Instance->OR2 = tmporx;
  1531. }
  1532. break;
  1533. case TIM_BREAKINPUT_BRK2:
  1534. {
  1535. /* Get the TIMx_OR3 register value */
  1536. tmporx = htim->Instance->OR3;
  1537. /* Enable the break input */
  1538. tmporx &= ~bkin_enable_mask;
  1539. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  1540. /* Set the break input polarity */
  1541. #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  1542. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  1543. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  1544. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  1545. if (sBreakInputConfig->Source != TIM_BREAKINPUTSOURCE_DFSDM1)
  1546. #endif /* STM32L451xx || STM32L452xx || STM32L462xx */
  1547. /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  1548. /* STM32L496xx || STM32L4A6xx */
  1549. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1550. {
  1551. tmporx &= ~bkin_polarity_mask;
  1552. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  1553. }
  1554. /* Set TIMx_OR3 */
  1555. htim->Instance->OR3 = tmporx;
  1556. }
  1557. break;
  1558. default:
  1559. break;
  1560. }
  1561. __HAL_UNLOCK(htim);
  1562. return HAL_OK;
  1563. }
  1564. /**
  1565. * @brief Configures the TIMx Remapping input capabilities.
  1566. * @param htim TIM handle.
  1567. * @param Remap: specifies the TIM remapping source.
  1568. *
  1569. @if STM32L486xx
  1570. * For TIM1, the parameter is a combination of 4 fields (field1 | field2 | field3 | field4):
  1571. *
  1572. * field1 can have the following values:
  1573. * @arg TIM_TIM1_ETR_ADC1_NONE: TIM1_ETR is not connected to any ADC1 AWD (analog watchdog)
  1574. * @arg TIM_TIM1_ETR_ADC1_AWD1: TIM1_ETR is connected to ADC1 AWD1
  1575. * @arg TIM_TIM1_ETR_ADC1_AWD2: TIM1_ETR is connected to ADC1 AWD2
  1576. * @arg TIM_TIM1_ETR_ADC1_AWD3: TIM1_ETR is connected to ADC1 AWD3
  1577. *
  1578. * field2 can have the following values:
  1579. * @arg TIM_TIM1_ETR_ADC3_NONE: TIM1_ETR is not connected to any ADC3 AWD (analog watchdog)
  1580. * @arg TIM_TIM1_ETR_ADC3_AWD1: TIM1_ETR is connected to ADC3 AWD1
  1581. * @arg TIM_TIM1_ETR_ADC3_AWD2: TIM1_ETR is connected to ADC3 AWD2
  1582. * @arg TIM_TIM1_ETR_ADC3_AWD3: TIM1_ETR is connected to ADC3 AWD3
  1583. *
  1584. * field3 can have the following values:
  1585. * @arg TIM_TIM1_TI1_GPIO: TIM1 TI1 is connected to GPIO
  1586. * @arg TIM_TIM1_TI1_COMP1: TIM1 TI1 is connected to COMP1 output
  1587. *
  1588. * field4 can have the following values:
  1589. * @arg TIM_TIM1_ETR_COMP1: TIM1_ETR is connected to COMP1 output
  1590. * @arg TIM_TIM1_ETR_COMP2: TIM1_ETR is connected to COMP2 output
  1591. * @note When field4 is set to TIM_TIM1_ETR_COMP1 or TIM_TIM1_ETR_COMP2 field1 and field2 values are not significant
  1592. @endif
  1593. @if STM32L443xx
  1594. * For TIM1, the parameter is a combination of 3 fields (field1 | field2 | field3):
  1595. *
  1596. * field1 can have the following values:
  1597. * @arg TIM_TIM1_ETR_ADC1_NONE: TIM1_ETR is not connected to any ADC1 AWD (analog watchdog)
  1598. * @arg TIM_TIM1_ETR_ADC1_AWD1: TIM1_ETR is connected to ADC1 AWD1
  1599. * @arg TIM_TIM1_ETR_ADC1_AWD2: TIM1_ETR is connected to ADC1 AWD2
  1600. * @arg TIM_TIM1_ETR_ADC1_AWD3: TIM1_ETR is connected to ADC1 AWD3
  1601. *
  1602. * field2 can have the following values:
  1603. * @arg TIM_TIM1_TI1_GPIO: TIM1 TI1 is connected to GPIO
  1604. * @arg TIM_TIM1_TI1_COMP1: TIM1 TI1 is connected to COMP1 output
  1605. *
  1606. * field3 can have the following values:
  1607. * @arg TIM_TIM1_ETR_COMP1: TIM1_ETR is connected to COMP1 output
  1608. * @arg TIM_TIM1_ETR_COMP2: TIM1_ETR is connected to COMP2 output
  1609. *
  1610. * @note When field3 is set to TIM_TIM1_ETR_COMP1 or TIM_TIM1_ETR_COMP2 field1 values is not significant
  1611. *
  1612. @endif
  1613. @if STM32L486xx
  1614. * For TIM2, the parameter is a combination of 3 fields (field1 | field2 | field3):
  1615. *
  1616. * field1 can have the following values:
  1617. * @arg TIM_TIM2_ITR1_TIM8_TRGO: TIM2_ITR1 is connected to TIM8_TRGO
  1618. * @arg TIM_TIM2_ITR1_OTG_FS_SOF: TIM2_ITR1 is connected to OTG_FS SOF
  1619. *
  1620. * field2 can have the following values:
  1621. * @arg TIM_TIM2_ETR_GPIO: TIM2_ETR is connected to GPIO
  1622. * @arg TIM_TIM2_ETR_LSE: TIM2_ETR is connected to LSE
  1623. * @arg TIM_TIM2_ETR_COMP1: TIM2_ETR is connected to COMP1 output
  1624. * @arg TIM_TIM2_ETR_COMP2: TIM2_ETR is connected to COMP2 output
  1625. *
  1626. * field3 can have the following values:
  1627. * @arg TIM_TIM2_TI4_GPIO: TIM2 TI4 is connected to GPIO
  1628. * @arg TIM_TIM2_TI4_COMP1: TIM2 TI4 is connected to COMP1 output
  1629. * @arg TIM_TIM2_TI4_COMP2: TIM2 TI4 is connected to COMP2 output
  1630. * @arg TIM_TIM2_TI4_COMP1_COMP2: TIM2 TI4 is connected to logical OR between COMP1 and COMP2 output
  1631. @endif
  1632. @if STM32L443xx
  1633. * For TIM2, the parameter is a combination of 3 fields (field1 | field2 | field3):
  1634. *
  1635. * field1 can have the following values:
  1636. * @arg TIM_TIM2_ITR1_NONE: No internal trigger on TIM2_ITR1
  1637. * @arg TIM_TIM2_ITR1_USB_SOF: TIM2_ITR1 is connected to USB SOF
  1638. *
  1639. * field2 can have the following values:
  1640. * @arg TIM_TIM2_ETR_GPIO: TIM2_ETR is connected to GPIO
  1641. * @arg TIM_TIM2_ETR_LSE: TIM2_ETR is connected to LSE
  1642. * @arg TIM_TIM2_ETR_COMP1: TIM2_ETR is connected to COMP1 output
  1643. * @arg TIM_TIM2_ETR_COMP2: TIM2_ETR is connected to COMP2 output
  1644. *
  1645. * field3 can have the following values:
  1646. * @arg TIM_TIM2_TI4_GPIO: TIM2 TI4 is connected to GPIO
  1647. * @arg TIM_TIM2_TI4_COMP1: TIM2 TI4 is connected to COMP1 output
  1648. * @arg TIM_TIM2_TI4_COMP2: TIM2 TI4 is connected to COMP2 output
  1649. * @arg TIM_TIM2_TI4_COMP1_COMP2: TIM2 TI4 is connected to logical OR between COMP1 and COMP2 output
  1650. *
  1651. @endif
  1652. @if STM32L486xx
  1653. * For TIM3, the parameter is a combination 2 fields(field1 | field2):
  1654. *
  1655. * field1 can have the following values:
  1656. * @arg TIM_TIM3_TI1_GPIO: TIM3 TI1 is connected to GPIO
  1657. * @arg TIM_TIM3_TI1_COMP1: TIM3 TI1 is connected to COMP1 output
  1658. * @arg TIM_TIM3_TI1_COMP2: TIM3 TI1 is connected to COMP2 output
  1659. * @arg TIM_TIM3_TI1_COMP1_COMP2: TIM3 TI1 is connected to logical OR between COMP1 and COMP2 output
  1660. *
  1661. * field2 can have the following values:
  1662. * @arg TIM_TIM3_ETR_GPIO: TIM3_ETR is connected to GPIO
  1663. * @arg TIM_TIM3_ETR_COMP1: TIM3_ETR is connected to COMP1 output
  1664. *
  1665. @endif
  1666. @if STM32L486xx
  1667. * For TIM8, the parameter is a combination of 3 fields (field1 | field2 | field3):
  1668. *
  1669. * field1 can have the following values:
  1670. * @arg TIM_TIM8_ETR_ADC2_NONE: TIM8_ETR is not connected to any ADC2 AWD (analog watchdog)
  1671. * @arg TIM_TIM8_ETR_ADC2_AWD1: TIM8_ETR is connected to ADC2 AWD1
  1672. * @arg TIM_TIM8_ETR_ADC2_AWD2: TIM8_ETR is connected to ADC2 AWD2
  1673. * @arg TIM_TIM8_ETR_ADC2_AWD3: TIM8_ETR is connected to ADC2 AWD3
  1674. *
  1675. * field2 can have the following values:
  1676. * @arg TIM_TIM8_ETR_ADC3_NONE: TIM8_ETR is not connected to any ADC3 AWD (analog watchdog)
  1677. * @arg TIM_TIM8_ETR_ADC3_AWD1: TIM8_ETR is connected to ADC3 AWD1
  1678. * @arg TIM_TIM8_ETR_ADC3_AWD2: TIM8_ETR is connected to ADC3 AWD2
  1679. * @arg TIM_TIM8_ETR_ADC3_AWD3: TIM8_ETR is connected to ADC3 AWD3
  1680. *
  1681. * field3 can have the following values:
  1682. * @arg TIM_TIM8_TI1_GPIO: TIM8 TI1 is connected to GPIO
  1683. * @arg TIM_TIM8_TI1_COMP2: TIM8 TI1 is connected to COMP2 output
  1684. *
  1685. * field4 can have the following values:
  1686. * @arg TIM_TIM8_ETR_COMP1: TIM8_ETR is connected to COMP1 output
  1687. * @arg TIM_TIM8_ETR_COMP2: TIM8_ETR is connected to COMP2 output
  1688. * @note When field4 is set to TIM_TIM8_ETR_COMP1 or TIM_TIM8_ETR_COMP2 field1 and field2 values are not significant
  1689. *
  1690. @endif
  1691. * For TIM15, the parameter is a combination of 3 fields (field1 | field2):
  1692. *
  1693. * field1 can have the following values:
  1694. * @arg TIM_TIM15_TI1_GPIO: TIM15 TI1 is connected to GPIO
  1695. * @arg TIM_TIM15_TI1_LSE: TIM15 TI1 is connected to LSE
  1696. *
  1697. * field2 can have the following values:
  1698. * @arg TIM_TIM15_ENCODERMODE_NONE: No redirection
  1699. * @arg TIM_TIM15_ENCODERMODE_TIM2: TIM2 IC1 and TIM2 IC2 are connected to TIM15 IC1 and TIM15 IC2 respectively
  1700. * @arg TIM_TIM15_ENCODERMODE_TIM3: TIM3 IC1 and TIM3 IC2 are connected to TIM15 IC1 and TIM15 IC2 respectively
  1701. * @arg TIM_TIM15_ENCODERMODE_TIM4: TIM4 IC1 and TIM4 IC2 are connected to TIM15 IC1 and TIM15 IC2 respectively
  1702. *
  1703. @if STM32L486xx
  1704. * @arg TIM_TIM16_TI1_GPIO: TIM16 TI1 is connected to GPIO
  1705. * @arg TIM_TIM16_TI1_LSI: TIM16 TI1 is connected to LSI
  1706. * @arg TIM_TIM16_TI1_LSE: TIM16 TI1 is connected to LSE
  1707. * @arg TIM_TIM16_TI1_RTC: TIM16 TI1 is connected to RTC wakeup interrupt
  1708. *
  1709. @endif
  1710. @if STM32L443xx
  1711. * For TIM16, the parameter can have the following values:
  1712. * @arg TIM_TIM16_TI1_GPIO: TIM16 TI1 is connected to GPIO
  1713. * @arg TIM_TIM16_TI1_LSI: TIM16 TI1 is connected to LSI
  1714. * @arg TIM_TIM16_TI1_LSE: TIM16 TI1 is connected to LSE
  1715. * @arg TIM_TIM16_TI1_RTC: TIM16 TI1 is connected to RTC wakeup interrupt
  1716. * @arg TIM_TIM16_TI1_MSI: TIM16 TI1 is connected to MSI (contraints: MSI clock < 1/4 TIM APB clock)
  1717. * @arg TIM_TIM16_TI1_HSE_32: TIM16 TI1 is connected to HSE div 32 (note that HSE div 32 must be selected as RTC clock source)
  1718. * @arg TIM_TIM16_TI1_MCO: TIM16 TI1 is connected to MCO
  1719. *
  1720. @endif
  1721. @if STM32L486xx
  1722. * For TIM17, the parameter can have the following values:
  1723. * @arg TIM_TIM17_TI1_GPIO: TIM17 TI1 is connected to GPIO
  1724. * @arg TIM_TIM17_TI1_MSI: TIM17 TI1 is connected to MSI (contraints: MSI clock < 1/4 TIM APB clock)
  1725. * @arg TIM_TIM17_TI1_HSE_32: TIM17 TI1 is connected to HSE div 32
  1726. * @arg TIM_TIM17_TI1_MCO: TIM17 TI1 is connected to MCO
  1727. @endif
  1728. *
  1729. * @retval HAL status
  1730. */
  1731. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1732. {
  1733. uint32_t tmpor1 = 0;
  1734. uint32_t tmpor2 = 0;
  1735. __HAL_LOCK(htim);
  1736. /* Check parameters */
  1737. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  1738. assert_param(IS_TIM_REMAP(Remap));
  1739. /* Set ETR_SEL bit field (if required) */
  1740. if (IS_TIM_ETRSEL_INSTANCE(htim->Instance))
  1741. {
  1742. tmpor2 = htim->Instance->OR2;
  1743. tmpor2 &= ~TIMx_ETRSEL_MASK;
  1744. tmpor2 |= (Remap & TIMx_ETRSEL_MASK);
  1745. /* Set TIMx_OR2 */
  1746. htim->Instance->OR2 = tmpor2;
  1747. }
  1748. /* Set other remapping capabilities */
  1749. tmpor1 = Remap;
  1750. tmpor1 &= ~TIMx_ETRSEL_MASK;
  1751. /* Set TIMx_OR1 */
  1752. htim->Instance->OR1 = tmpor1;
  1753. htim->State = HAL_TIM_STATE_READY;
  1754. __HAL_UNLOCK(htim);
  1755. return HAL_OK;
  1756. }
  1757. /**
  1758. * @brief Group channel 5 and channel 1, 2 or 3
  1759. * @param htim TIM handle.
  1760. * @param Channels specifies the reference signal(s) the OC5REF is combined with.
  1761. * This parameter can be any combination of the following values:
  1762. * TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
  1763. * TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
  1764. * TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
  1765. * TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
  1766. * @retval HAL status
  1767. */
  1768. HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t Channels)
  1769. {
  1770. /* Check parameters */
  1771. assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
  1772. assert_param(IS_TIM_GROUPCH5(Channels));
  1773. /* Process Locked */
  1774. __HAL_LOCK(htim);
  1775. htim->State = HAL_TIM_STATE_BUSY;
  1776. /* Clear GC5Cx bit fields */
  1777. htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3|TIM_CCR5_GC5C2|TIM_CCR5_GC5C1);
  1778. /* Set GC5Cx bit fields */
  1779. htim->Instance->CCR5 |= Channels;
  1780. htim->State = HAL_TIM_STATE_READY;
  1781. __HAL_UNLOCK(htim);
  1782. return HAL_OK;
  1783. }
  1784. /**
  1785. * @}
  1786. */
  1787. /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
  1788. * @brief Extended Callbacks functions
  1789. *
  1790. @verbatim
  1791. ==============================================================================
  1792. ##### Extended Callbacks functions #####
  1793. ==============================================================================
  1794. [..]
  1795. This section provides Extended TIM callback functions:
  1796. (+) Timer Commutation callback
  1797. (+) Timer Break callback
  1798. @endverbatim
  1799. * @{
  1800. */
  1801. /**
  1802. * @brief Hall commutation changed callback in non-blocking mode
  1803. * @param htim TIM handle
  1804. * @retval None
  1805. */
  1806. __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)
  1807. {
  1808. /* Prevent unused argument(s) compilation warning */
  1809. UNUSED(htim);
  1810. /* NOTE : This function should not be modified, when the callback is needed,
  1811. the HAL_TIMEx_CommutationCallback could be implemented in the user file
  1812. */
  1813. }
  1814. /**
  1815. * @brief Hall Break detection callback in non-blocking mode
  1816. * @param htim TIM handle
  1817. * @retval None
  1818. */
  1819. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  1820. {
  1821. /* Prevent unused argument(s) compilation warning */
  1822. UNUSED(htim);
  1823. /* NOTE : This function should not be modified, when the callback is needed,
  1824. the HAL_TIMEx_BreakCallback could be implemented in the user file
  1825. */
  1826. }
  1827. /**
  1828. * @}
  1829. */
  1830. /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
  1831. * @brief Extended Peripheral State functions
  1832. *
  1833. @verbatim
  1834. ==============================================================================
  1835. ##### Extended Peripheral State functions #####
  1836. ==============================================================================
  1837. [..]
  1838. This subsection permits to get in run-time the status of the peripheral
  1839. and the data flow.
  1840. @endverbatim
  1841. * @{
  1842. */
  1843. /**
  1844. * @brief Return the TIM Hall Sensor interface handle state.
  1845. * @param htim TIM Hall Sensor handle
  1846. * @retval HAL state
  1847. */
  1848. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
  1849. {
  1850. return htim->State;
  1851. }
  1852. /**
  1853. * @}
  1854. */
  1855. /**
  1856. * @brief TIM DMA Commutation callback.
  1857. * @param hdma pointer to DMA handle.
  1858. * @retval None
  1859. */
  1860. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  1861. {
  1862. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1863. htim->State= HAL_TIM_STATE_READY;
  1864. HAL_TIMEx_CommutationCallback(htim);
  1865. }
  1866. /**
  1867. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1868. * @param TIMx to select the TIM peripheral
  1869. * @param Channel specifies the TIM Channel
  1870. * This parameter can be one of the following values:
  1871. * @arg TIM_Channel_1: TIM Channel 1
  1872. * @arg TIM_Channel_2: TIM Channel 2
  1873. * @arg TIM_Channel_3: TIM Channel 3
  1874. * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
  1875. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  1876. * @retval None
  1877. */
  1878. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState)
  1879. {
  1880. uint32_t tmp = 0;
  1881. tmp = TIM_CCER_CC1NE << Channel;
  1882. /* Reset the CCxNE Bit */
  1883. TIMx->CCER &= ~tmp;
  1884. /* Set or reset the CCxNE Bit */
  1885. TIMx->CCER |= (uint32_t)(ChannelNState << Channel);
  1886. }
  1887. /**
  1888. * @}
  1889. */
  1890. #endif /* HAL_TIM_MODULE_ENABLED */
  1891. /**
  1892. * @}
  1893. */
  1894. /**
  1895. * @}
  1896. */
  1897. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/