stm32f4xx_hal_lptim.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_lptim.c
  4. * @author MCD Application Team
  5. * @brief LPTIM HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Low Power Timer (LPTIM) peripheral:
  8. * + Initialization and de-initialization functions.
  9. * + Start/Stop operation functions in polling mode.
  10. * + Start/Stop operation functions in interrupt mode.
  11. * + Reading operation functions.
  12. * + Peripheral State functions.
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The LPTIM HAL driver can be used as follows:
  20. (#)Initialize the LPTIM low level resources by implementing the
  21. HAL_LPTIM_MspInit():
  22. (++) Enable the LPTIM interface clock using __HAL_RCC_LPTIMx_CLK_ENABLE().
  23. (++) In case of using interrupts (e.g. HAL_LPTIM_PWM_Start_IT()):
  24. (+++) Configure the LPTIM interrupt priority using HAL_NVIC_SetPriority().
  25. (+++) Enable the LPTIM IRQ handler using HAL_NVIC_EnableIRQ().
  26. (+++) In LPTIM IRQ handler, call HAL_LPTIM_IRQHandler().
  27. (#)Initialize the LPTIM HAL using HAL_LPTIM_Init(). This function
  28. configures mainly:
  29. (++) The instance: LPTIM1.
  30. (++) Clock: the counter clock.
  31. (+++) Source : it can be either the ULPTIM input (IN1) or one of
  32. the internal clock; (APB, LSE or LSI).
  33. (+++) Prescaler: select the clock divider.
  34. (++) UltraLowPowerClock : To be used only if the ULPTIM is selected
  35. as counter clock source.
  36. (+++) Polarity: polarity of the active edge for the counter unit
  37. if the ULPTIM input is selected.
  38. (+++) SampleTime: clock sampling time to configure the clock glitch
  39. filter.
  40. (++) Trigger: How the counter start.
  41. (+++) Source: trigger can be software or one of the hardware triggers.
  42. (+++) ActiveEdge : only for hardware trigger.
  43. (+++) SampleTime : trigger sampling time to configure the trigger
  44. glitch filter.
  45. (++) OutputPolarity : 2 opposite polarities are possible.
  46. (++) UpdateMode: specifies whether the update of the autoreload and
  47. the compare values is done immediately or after the end of current
  48. period.
  49. (#)Six modes are available:
  50. (++) PWM Mode: To generate a PWM signal with specified period and pulse,
  51. call HAL_LPTIM_PWM_Start() or HAL_LPTIM_PWM_Start_IT() for interruption
  52. mode.
  53. (++) One Pulse Mode: To generate pulse with specified width in response
  54. to a stimulus, call HAL_LPTIM_OnePulse_Start() or
  55. HAL_LPTIM_OnePulse_Start_IT() for interruption mode.
  56. (++) Set once Mode: In this mode, the output changes the level (from
  57. low level to high level if the output polarity is configured high, else
  58. the opposite) when a compare match occurs. To start this mode, call
  59. HAL_LPTIM_SetOnce_Start() or HAL_LPTIM_SetOnce_Start_IT() for
  60. interruption mode.
  61. (++) Encoder Mode: To use the encoder interface call
  62. HAL_LPTIM_Encoder_Start() or HAL_LPTIM_Encoder_Start_IT() for
  63. interruption mode. Only available for LPTIM1 instance.
  64. (++) Time out Mode: an active edge on one selected trigger input rests
  65. the counter. The first trigger event will start the timer, any
  66. successive trigger event will reset the counter and the timer will
  67. restart. To start this mode call HAL_LPTIM_TimeOut_Start_IT() or
  68. HAL_LPTIM_TimeOut_Start_IT() for interruption mode.
  69. (++) Counter Mode: counter can be used to count external events on
  70. the LPTIM Input1 or it can be used to count internal clock cycles.
  71. To start this mode, call HAL_LPTIM_Counter_Start() or
  72. HAL_LPTIM_Counter_Start_IT() for interruption mode.
  73. (#) User can stop any process by calling the corresponding API:
  74. HAL_LPTIM_Xxx_Stop() or HAL_LPTIM_Xxx_Stop_IT() if the process is
  75. already started in interruption mode.
  76. (#) De-initialize the LPTIM peripheral using HAL_LPTIM_DeInit().
  77. *** Callback registration ***
  78. =============================================
  79. [..]
  80. The compilation define USE_HAL_LPTIM_REGISTER_CALLBACKS when set to 1
  81. allows the user to configure dynamically the driver callbacks.
  82. [..]
  83. Use Function @ref HAL_LPTIM_RegisterCallback() to register a callback.
  84. @ref HAL_LPTIM_RegisterCallback() takes as parameters the HAL peripheral handle,
  85. the Callback ID and a pointer to the user callback function.
  86. [..]
  87. Use function @ref HAL_LPTIM_UnRegisterCallback() to reset a callback to the
  88. default weak function.
  89. @ref HAL_LPTIM_UnRegisterCallback takes as parameters the HAL peripheral handle,
  90. and the Callback ID.
  91. [..]
  92. These functions allow to register/unregister following callbacks:
  93. (+) MspInitCallback : LPTIM Base Msp Init Callback.
  94. (+) MspDeInitCallback : LPTIM Base Msp DeInit Callback.
  95. (+) CompareMatchCallback : Compare match Callback.
  96. (+) AutoReloadMatchCallback : Auto-reload match Callback.
  97. (+) TriggerCallback : External trigger event detection Callback.
  98. (+) CompareWriteCallback : Compare register write complete Callback.
  99. (+) AutoReloadWriteCallback : Auto-reload register write complete Callback.
  100. (+) DirectionUpCallback : Up-counting direction change Callback.
  101. (+) DirectionDownCallback : Down-counting direction change Callback.
  102. [..]
  103. By default, after the Init and when the state is HAL_LPTIM_STATE_RESET
  104. all interrupt callbacks are set to the corresponding weak functions:
  105. examples @ref HAL_LPTIM_TriggerCallback(), @ref HAL_LPTIM_CompareMatchCallback().
  106. [..]
  107. Exception done for MspInit and MspDeInit functions that are reset to the legacy weak
  108. functionalities in the Init/DeInit only when these callbacks are null
  109. (not registered beforehand). If not, MspInit or MspDeInit are not null, the Init/DeInit
  110. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  111. [..]
  112. Callbacks can be registered/unregistered in HAL_LPTIM_STATE_READY state only.
  113. Exception done MspInit/MspDeInit that can be registered/unregistered
  114. in HAL_LPTIM_STATE_READY or HAL_LPTIM_STATE_RESET state,
  115. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  116. In that case first register the MspInit/MspDeInit user callbacks
  117. using @ref HAL_LPTIM_RegisterCallback() before calling DeInit or Init function.
  118. [..]
  119. When The compilation define USE_HAL_LPTIM_REGISTER_CALLBACKS is set to 0 or
  120. not defined, the callback registration feature is not available and all callbacks
  121. are set to the corresponding weak functions.
  122. @endverbatim
  123. ******************************************************************************
  124. * @attention
  125. *
  126. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  127. * All rights reserved.</center></h2>
  128. *
  129. * This software component is licensed by ST under BSD 3-Clause license,
  130. * the "License"; You may not use this file except in compliance with the
  131. * License. You may obtain a copy of the License at:
  132. * opensource.org/licenses/BSD-3-Clause
  133. *
  134. ******************************************************************************
  135. */
  136. /* Includes ------------------------------------------------------------------*/
  137. #include "stm32f4xx_hal.h"
  138. /** @addtogroup STM32F4xx_HAL_Driver
  139. * @{
  140. */
  141. /** @defgroup LPTIM LPTIM
  142. * @brief LPTIM HAL module driver.
  143. * @{
  144. */
  145. #ifdef HAL_LPTIM_MODULE_ENABLED
  146. #if defined (LPTIM1)
  147. /* Private typedef -----------------------------------------------------------*/
  148. /* Private define ------------------------------------------------------------*/
  149. /** @addtogroup LPTIM_Private_Constants
  150. * @{
  151. */
  152. #define TIMEOUT 1000UL /* Timeout is 1s */
  153. /**
  154. * @}
  155. */
  156. /* Private macro -------------------------------------------------------------*/
  157. /* Private variables ---------------------------------------------------------*/
  158. /* Private function prototypes -----------------------------------------------*/
  159. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  160. static void LPTIM_ResetCallback(LPTIM_HandleTypeDef *lptim);
  161. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  162. static HAL_StatusTypeDef LPTIM_WaitForFlag(LPTIM_HandleTypeDef *hlptim, uint32_t flag);
  163. /* Exported functions --------------------------------------------------------*/
  164. /** @defgroup LPTIM_Exported_Functions LPTIM Exported Functions
  165. * @{
  166. */
  167. /** @defgroup LPTIM_Exported_Functions_Group1 Initialization/de-initialization functions
  168. * @brief Initialization and Configuration functions.
  169. *
  170. @verbatim
  171. ==============================================================================
  172. ##### Initialization and de-initialization functions #####
  173. ==============================================================================
  174. [..] This section provides functions allowing to:
  175. (+) Initialize the LPTIM according to the specified parameters in the
  176. LPTIM_InitTypeDef and initialize the associated handle.
  177. (+) DeInitialize the LPTIM peripheral.
  178. (+) Initialize the LPTIM MSP.
  179. (+) DeInitialize the LPTIM MSP.
  180. @endverbatim
  181. * @{
  182. */
  183. /**
  184. * @brief Initialize the LPTIM according to the specified parameters in the
  185. * LPTIM_InitTypeDef and initialize the associated handle.
  186. * @param hlptim LPTIM handle
  187. * @retval HAL status
  188. */
  189. HAL_StatusTypeDef HAL_LPTIM_Init(LPTIM_HandleTypeDef *hlptim)
  190. {
  191. uint32_t tmpcfgr;
  192. /* Check the LPTIM handle allocation */
  193. if (hlptim == NULL)
  194. {
  195. return HAL_ERROR;
  196. }
  197. /* Check the parameters */
  198. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  199. assert_param(IS_LPTIM_CLOCK_SOURCE(hlptim->Init.Clock.Source));
  200. assert_param(IS_LPTIM_CLOCK_PRESCALER(hlptim->Init.Clock.Prescaler));
  201. if ((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM)
  202. {
  203. assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
  204. assert_param(IS_LPTIM_CLOCK_SAMPLE_TIME(hlptim->Init.UltraLowPowerClock.SampleTime));
  205. }
  206. assert_param(IS_LPTIM_TRG_SOURCE(hlptim->Init.Trigger.Source));
  207. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  208. {
  209. assert_param(IS_LPTIM_TRIG_SAMPLE_TIME(hlptim->Init.Trigger.SampleTime));
  210. assert_param(IS_LPTIM_EXT_TRG_POLARITY(hlptim->Init.Trigger.ActiveEdge));
  211. }
  212. assert_param(IS_LPTIM_OUTPUT_POLARITY(hlptim->Init.OutputPolarity));
  213. assert_param(IS_LPTIM_UPDATE_MODE(hlptim->Init.UpdateMode));
  214. assert_param(IS_LPTIM_COUNTER_SOURCE(hlptim->Init.CounterSource));
  215. if (hlptim->State == HAL_LPTIM_STATE_RESET)
  216. {
  217. /* Allocate lock resource and initialize it */
  218. hlptim->Lock = HAL_UNLOCKED;
  219. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  220. /* Reset interrupt callbacks to legacy weak callbacks */
  221. LPTIM_ResetCallback(hlptim);
  222. if (hlptim->MspInitCallback == NULL)
  223. {
  224. hlptim->MspInitCallback = HAL_LPTIM_MspInit;
  225. }
  226. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  227. hlptim->MspInitCallback(hlptim);
  228. #else
  229. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  230. HAL_LPTIM_MspInit(hlptim);
  231. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  232. }
  233. /* Change the LPTIM state */
  234. hlptim->State = HAL_LPTIM_STATE_BUSY;
  235. /* Get the LPTIMx CFGR value */
  236. tmpcfgr = hlptim->Instance->CFGR;
  237. if (((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM) || ((hlptim->Init.CounterSource) == LPTIM_COUNTERSOURCE_EXTERNAL))
  238. {
  239. tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKPOL | LPTIM_CFGR_CKFLT));
  240. }
  241. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  242. {
  243. tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_TRGFLT | LPTIM_CFGR_TRIGSEL));
  244. }
  245. /* Clear CKSEL, CKPOL, PRESC, TRIGEN, TRGFLT, WAVPOL, PRELOAD & COUNTMODE bits */
  246. tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKSEL | LPTIM_CFGR_CKPOL | LPTIM_CFGR_TRIGEN | LPTIM_CFGR_PRELOAD |
  247. LPTIM_CFGR_WAVPOL | LPTIM_CFGR_PRESC | LPTIM_CFGR_COUNTMODE));
  248. /* Set initialization parameters */
  249. tmpcfgr |= (hlptim->Init.Clock.Source |
  250. hlptim->Init.Clock.Prescaler |
  251. hlptim->Init.OutputPolarity |
  252. hlptim->Init.UpdateMode |
  253. hlptim->Init.CounterSource);
  254. if (((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM) || ((hlptim->Init.CounterSource) == LPTIM_COUNTERSOURCE_EXTERNAL))
  255. {
  256. tmpcfgr |= (hlptim->Init.UltraLowPowerClock.Polarity |
  257. hlptim->Init.UltraLowPowerClock.SampleTime);
  258. }
  259. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  260. {
  261. /* Enable External trigger and set the trigger source */
  262. tmpcfgr |= (hlptim->Init.Trigger.Source |
  263. hlptim->Init.Trigger.ActiveEdge |
  264. hlptim->Init.Trigger.SampleTime);
  265. }
  266. /* Write to LPTIMx CFGR */
  267. hlptim->Instance->CFGR = tmpcfgr;
  268. /* Change the LPTIM state */
  269. hlptim->State = HAL_LPTIM_STATE_READY;
  270. /* Return function status */
  271. return HAL_OK;
  272. }
  273. /**
  274. * @brief DeInitialize the LPTIM peripheral.
  275. * @param hlptim LPTIM handle
  276. * @retval HAL status
  277. */
  278. HAL_StatusTypeDef HAL_LPTIM_DeInit(LPTIM_HandleTypeDef *hlptim)
  279. {
  280. /* Check the LPTIM handle allocation */
  281. if (hlptim == NULL)
  282. {
  283. return HAL_ERROR;
  284. }
  285. /* Change the LPTIM state */
  286. hlptim->State = HAL_LPTIM_STATE_BUSY;
  287. /* Disable the LPTIM Peripheral Clock */
  288. __HAL_LPTIM_DISABLE(hlptim);
  289. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  290. {
  291. return HAL_TIMEOUT;
  292. }
  293. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  294. if (hlptim->MspDeInitCallback == NULL)
  295. {
  296. hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit;
  297. }
  298. /* DeInit the low level hardware: CLOCK, NVIC.*/
  299. hlptim->MspDeInitCallback(hlptim);
  300. #else
  301. /* DeInit the low level hardware: CLOCK, NVIC.*/
  302. HAL_LPTIM_MspDeInit(hlptim);
  303. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  304. /* Change the LPTIM state */
  305. hlptim->State = HAL_LPTIM_STATE_RESET;
  306. /* Release Lock */
  307. __HAL_UNLOCK(hlptim);
  308. /* Return function status */
  309. return HAL_OK;
  310. }
  311. /**
  312. * @brief Initialize the LPTIM MSP.
  313. * @param hlptim LPTIM handle
  314. * @retval None
  315. */
  316. __weak void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef *hlptim)
  317. {
  318. /* Prevent unused argument(s) compilation warning */
  319. UNUSED(hlptim);
  320. /* NOTE : This function should not be modified, when the callback is needed,
  321. the HAL_LPTIM_MspInit could be implemented in the user file
  322. */
  323. }
  324. /**
  325. * @brief DeInitialize LPTIM MSP.
  326. * @param hlptim LPTIM handle
  327. * @retval None
  328. */
  329. __weak void HAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef *hlptim)
  330. {
  331. /* Prevent unused argument(s) compilation warning */
  332. UNUSED(hlptim);
  333. /* NOTE : This function should not be modified, when the callback is needed,
  334. the HAL_LPTIM_MspDeInit could be implemented in the user file
  335. */
  336. }
  337. /**
  338. * @}
  339. */
  340. /** @defgroup LPTIM_Exported_Functions_Group2 LPTIM Start-Stop operation functions
  341. * @brief Start-Stop operation functions.
  342. *
  343. @verbatim
  344. ==============================================================================
  345. ##### LPTIM Start Stop operation functions #####
  346. ==============================================================================
  347. [..] This section provides functions allowing to:
  348. (+) Start the PWM mode.
  349. (+) Stop the PWM mode.
  350. (+) Start the One pulse mode.
  351. (+) Stop the One pulse mode.
  352. (+) Start the Set once mode.
  353. (+) Stop the Set once mode.
  354. (+) Start the Encoder mode.
  355. (+) Stop the Encoder mode.
  356. (+) Start the Timeout mode.
  357. (+) Stop the Timeout mode.
  358. (+) Start the Counter mode.
  359. (+) Stop the Counter mode.
  360. @endverbatim
  361. * @{
  362. */
  363. /**
  364. * @brief Start the LPTIM PWM generation.
  365. * @param hlptim LPTIM handle
  366. * @param Period Specifies the Autoreload value.
  367. * This parameter must be a value between 0x0000 and 0xFFFF.
  368. * @param Pulse Specifies the compare value.
  369. * This parameter must be a value between 0x0000 and 0xFFFF.
  370. * @retval HAL status
  371. */
  372. HAL_StatusTypeDef HAL_LPTIM_PWM_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  373. {
  374. /* Check the parameters */
  375. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  376. assert_param(IS_LPTIM_PERIOD(Period));
  377. assert_param(IS_LPTIM_PULSE(Pulse));
  378. /* Set the LPTIM state */
  379. hlptim->State = HAL_LPTIM_STATE_BUSY;
  380. /* Reset WAVE bit to set PWM mode */
  381. hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
  382. /* Enable the Peripheral */
  383. __HAL_LPTIM_ENABLE(hlptim);
  384. /* Clear flag */
  385. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  386. /* Load the period value in the autoreload register */
  387. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  388. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  389. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  390. {
  391. return HAL_TIMEOUT;
  392. }
  393. /* Clear flag */
  394. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  395. /* Load the pulse value in the compare register */
  396. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  397. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  398. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  399. {
  400. return HAL_TIMEOUT;
  401. }
  402. /* Start timer in continuous mode */
  403. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  404. /* Change the TIM state*/
  405. hlptim->State = HAL_LPTIM_STATE_READY;
  406. /* Return function status */
  407. return HAL_OK;
  408. }
  409. /**
  410. * @brief Stop the LPTIM PWM generation.
  411. * @param hlptim LPTIM handle
  412. * @retval HAL status
  413. */
  414. HAL_StatusTypeDef HAL_LPTIM_PWM_Stop(LPTIM_HandleTypeDef *hlptim)
  415. {
  416. /* Check the parameters */
  417. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  418. /* Set the LPTIM state */
  419. hlptim->State = HAL_LPTIM_STATE_BUSY;
  420. /* Disable the Peripheral */
  421. __HAL_LPTIM_DISABLE(hlptim);
  422. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  423. {
  424. return HAL_TIMEOUT;
  425. }
  426. /* Change the TIM state*/
  427. hlptim->State = HAL_LPTIM_STATE_READY;
  428. /* Return function status */
  429. return HAL_OK;
  430. }
  431. /**
  432. * @brief Start the LPTIM PWM generation in interrupt mode.
  433. * @param hlptim LPTIM handle
  434. * @param Period Specifies the Autoreload value.
  435. * This parameter must be a value between 0x0000 and 0xFFFF
  436. * @param Pulse Specifies the compare value.
  437. * This parameter must be a value between 0x0000 and 0xFFFF
  438. * @retval HAL status
  439. */
  440. HAL_StatusTypeDef HAL_LPTIM_PWM_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  441. {
  442. /* Check the parameters */
  443. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  444. assert_param(IS_LPTIM_PERIOD(Period));
  445. assert_param(IS_LPTIM_PULSE(Pulse));
  446. /* Set the LPTIM state */
  447. hlptim->State = HAL_LPTIM_STATE_BUSY;
  448. /* Reset WAVE bit to set PWM mode */
  449. hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
  450. /* Enable the Peripheral */
  451. __HAL_LPTIM_ENABLE(hlptim);
  452. /* Clear flag */
  453. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  454. /* Load the period value in the autoreload register */
  455. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  456. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  457. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  458. {
  459. return HAL_TIMEOUT;
  460. }
  461. /* Clear flag */
  462. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  463. /* Load the pulse value in the compare register */
  464. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  465. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  466. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  467. {
  468. return HAL_TIMEOUT;
  469. }
  470. /* Disable the Peripheral */
  471. __HAL_LPTIM_DISABLE(hlptim);
  472. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  473. {
  474. return HAL_TIMEOUT;
  475. }
  476. /* Enable Autoreload write complete interrupt */
  477. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
  478. /* Enable Compare write complete interrupt */
  479. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
  480. /* Enable Autoreload match interrupt */
  481. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  482. /* Enable Compare match interrupt */
  483. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
  484. /* If external trigger source is used, then enable external trigger interrupt */
  485. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  486. {
  487. /* Enable external trigger interrupt */
  488. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  489. }
  490. /* Enable the Peripheral */
  491. __HAL_LPTIM_ENABLE(hlptim);
  492. /* Start timer in continuous mode */
  493. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  494. /* Change the TIM state*/
  495. hlptim->State = HAL_LPTIM_STATE_READY;
  496. /* Return function status */
  497. return HAL_OK;
  498. }
  499. /**
  500. * @brief Stop the LPTIM PWM generation in interrupt mode.
  501. * @param hlptim LPTIM handle
  502. * @retval HAL status
  503. */
  504. HAL_StatusTypeDef HAL_LPTIM_PWM_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  505. {
  506. /* Check the parameters */
  507. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  508. /* Set the LPTIM state */
  509. hlptim->State = HAL_LPTIM_STATE_BUSY;
  510. /* Disable the Peripheral */
  511. __HAL_LPTIM_DISABLE(hlptim);
  512. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  513. {
  514. return HAL_TIMEOUT;
  515. }
  516. /* Disable Autoreload write complete interrupt */
  517. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
  518. /* Disable Compare write complete interrupt */
  519. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
  520. /* Disable Autoreload match interrupt */
  521. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  522. /* Disable Compare match interrupt */
  523. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
  524. /* If external trigger source is used, then disable external trigger interrupt */
  525. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  526. {
  527. /* Disable external trigger interrupt */
  528. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  529. }
  530. /* Change the TIM state*/
  531. hlptim->State = HAL_LPTIM_STATE_READY;
  532. /* Return function status */
  533. return HAL_OK;
  534. }
  535. /**
  536. * @brief Start the LPTIM One pulse generation.
  537. * @param hlptim LPTIM handle
  538. * @param Period Specifies the Autoreload value.
  539. * This parameter must be a value between 0x0000 and 0xFFFF.
  540. * @param Pulse Specifies the compare value.
  541. * This parameter must be a value between 0x0000 and 0xFFFF.
  542. * @retval HAL status
  543. */
  544. HAL_StatusTypeDef HAL_LPTIM_OnePulse_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  545. {
  546. /* Check the parameters */
  547. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  548. assert_param(IS_LPTIM_PERIOD(Period));
  549. assert_param(IS_LPTIM_PULSE(Pulse));
  550. /* Set the LPTIM state */
  551. hlptim->State = HAL_LPTIM_STATE_BUSY;
  552. /* Reset WAVE bit to set one pulse mode */
  553. hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
  554. /* Enable the Peripheral */
  555. __HAL_LPTIM_ENABLE(hlptim);
  556. /* Clear flag */
  557. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  558. /* Load the period value in the autoreload register */
  559. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  560. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  561. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  562. {
  563. return HAL_TIMEOUT;
  564. }
  565. /* Clear flag */
  566. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  567. /* Load the pulse value in the compare register */
  568. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  569. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  570. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  571. {
  572. return HAL_TIMEOUT;
  573. }
  574. /* Start timer in single (one shot) mode */
  575. __HAL_LPTIM_START_SINGLE(hlptim);
  576. /* Change the TIM state*/
  577. hlptim->State = HAL_LPTIM_STATE_READY;
  578. /* Return function status */
  579. return HAL_OK;
  580. }
  581. /**
  582. * @brief Stop the LPTIM One pulse generation.
  583. * @param hlptim LPTIM handle
  584. * @retval HAL status
  585. */
  586. HAL_StatusTypeDef HAL_LPTIM_OnePulse_Stop(LPTIM_HandleTypeDef *hlptim)
  587. {
  588. /* Check the parameters */
  589. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  590. /* Set the LPTIM state */
  591. hlptim->State = HAL_LPTIM_STATE_BUSY;
  592. /* Disable the Peripheral */
  593. __HAL_LPTIM_DISABLE(hlptim);
  594. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  595. {
  596. return HAL_TIMEOUT;
  597. }
  598. /* Change the TIM state*/
  599. hlptim->State = HAL_LPTIM_STATE_READY;
  600. /* Return function status */
  601. return HAL_OK;
  602. }
  603. /**
  604. * @brief Start the LPTIM One pulse generation in interrupt mode.
  605. * @param hlptim LPTIM handle
  606. * @param Period Specifies the Autoreload value.
  607. * This parameter must be a value between 0x0000 and 0xFFFF.
  608. * @param Pulse Specifies the compare value.
  609. * This parameter must be a value between 0x0000 and 0xFFFF.
  610. * @retval HAL status
  611. */
  612. HAL_StatusTypeDef HAL_LPTIM_OnePulse_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  613. {
  614. /* Check the parameters */
  615. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  616. assert_param(IS_LPTIM_PERIOD(Period));
  617. assert_param(IS_LPTIM_PULSE(Pulse));
  618. /* Set the LPTIM state */
  619. hlptim->State = HAL_LPTIM_STATE_BUSY;
  620. /* Reset WAVE bit to set one pulse mode */
  621. hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
  622. /* Enable the Peripheral */
  623. __HAL_LPTIM_ENABLE(hlptim);
  624. /* Clear flag */
  625. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  626. /* Load the period value in the autoreload register */
  627. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  628. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  629. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  630. {
  631. return HAL_TIMEOUT;
  632. }
  633. /* Clear flag */
  634. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  635. /* Load the pulse value in the compare register */
  636. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  637. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  638. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  639. {
  640. return HAL_TIMEOUT;
  641. }
  642. /* Disable the Peripheral */
  643. __HAL_LPTIM_DISABLE(hlptim);
  644. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  645. {
  646. return HAL_TIMEOUT;
  647. }
  648. /* Enable Autoreload write complete interrupt */
  649. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
  650. /* Enable Compare write complete interrupt */
  651. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
  652. /* Enable Autoreload match interrupt */
  653. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  654. /* Enable Compare match interrupt */
  655. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
  656. /* If external trigger source is used, then enable external trigger interrupt */
  657. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  658. {
  659. /* Enable external trigger interrupt */
  660. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  661. }
  662. /* Enable the Peripheral */
  663. __HAL_LPTIM_ENABLE(hlptim);
  664. /* Start timer in single (one shot) mode */
  665. __HAL_LPTIM_START_SINGLE(hlptim);
  666. /* Change the TIM state*/
  667. hlptim->State = HAL_LPTIM_STATE_READY;
  668. /* Return function status */
  669. return HAL_OK;
  670. }
  671. /**
  672. * @brief Stop the LPTIM One pulse generation in interrupt mode.
  673. * @param hlptim LPTIM handle
  674. * @retval HAL status
  675. */
  676. HAL_StatusTypeDef HAL_LPTIM_OnePulse_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  677. {
  678. /* Check the parameters */
  679. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  680. /* Set the LPTIM state */
  681. hlptim->State = HAL_LPTIM_STATE_BUSY;
  682. /* Disable the Peripheral */
  683. __HAL_LPTIM_DISABLE(hlptim);
  684. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  685. {
  686. return HAL_TIMEOUT;
  687. }
  688. /* Disable Autoreload write complete interrupt */
  689. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
  690. /* Disable Compare write complete interrupt */
  691. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
  692. /* Disable Autoreload match interrupt */
  693. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  694. /* Disable Compare match interrupt */
  695. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
  696. /* If external trigger source is used, then disable external trigger interrupt */
  697. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  698. {
  699. /* Disable external trigger interrupt */
  700. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  701. }
  702. /* Change the TIM state*/
  703. hlptim->State = HAL_LPTIM_STATE_READY;
  704. /* Return function status */
  705. return HAL_OK;
  706. }
  707. /**
  708. * @brief Start the LPTIM in Set once mode.
  709. * @param hlptim LPTIM handle
  710. * @param Period Specifies the Autoreload value.
  711. * This parameter must be a value between 0x0000 and 0xFFFF.
  712. * @param Pulse Specifies the compare value.
  713. * This parameter must be a value between 0x0000 and 0xFFFF.
  714. * @retval HAL status
  715. */
  716. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  717. {
  718. /* Check the parameters */
  719. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  720. assert_param(IS_LPTIM_PERIOD(Period));
  721. assert_param(IS_LPTIM_PULSE(Pulse));
  722. /* Set the LPTIM state */
  723. hlptim->State = HAL_LPTIM_STATE_BUSY;
  724. /* Set WAVE bit to enable the set once mode */
  725. hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
  726. /* Enable the Peripheral */
  727. __HAL_LPTIM_ENABLE(hlptim);
  728. /* Clear flag */
  729. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  730. /* Load the period value in the autoreload register */
  731. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  732. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  733. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  734. {
  735. return HAL_TIMEOUT;
  736. }
  737. /* Clear flag */
  738. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  739. /* Load the pulse value in the compare register */
  740. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  741. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  742. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  743. {
  744. return HAL_TIMEOUT;
  745. }
  746. /* Start timer in single (one shot) mode */
  747. __HAL_LPTIM_START_SINGLE(hlptim);
  748. /* Change the TIM state*/
  749. hlptim->State = HAL_LPTIM_STATE_READY;
  750. /* Return function status */
  751. return HAL_OK;
  752. }
  753. /**
  754. * @brief Stop the LPTIM Set once mode.
  755. * @param hlptim LPTIM handle
  756. * @retval HAL status
  757. */
  758. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop(LPTIM_HandleTypeDef *hlptim)
  759. {
  760. /* Check the parameters */
  761. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  762. /* Set the LPTIM state */
  763. hlptim->State = HAL_LPTIM_STATE_BUSY;
  764. /* Disable the Peripheral */
  765. __HAL_LPTIM_DISABLE(hlptim);
  766. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  767. {
  768. return HAL_TIMEOUT;
  769. }
  770. /* Change the TIM state*/
  771. hlptim->State = HAL_LPTIM_STATE_READY;
  772. /* Return function status */
  773. return HAL_OK;
  774. }
  775. /**
  776. * @brief Start the LPTIM Set once mode in interrupt mode.
  777. * @param hlptim LPTIM handle
  778. * @param Period Specifies the Autoreload value.
  779. * This parameter must be a value between 0x0000 and 0xFFFF.
  780. * @param Pulse Specifies the compare value.
  781. * This parameter must be a value between 0x0000 and 0xFFFF.
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  785. {
  786. /* Check the parameters */
  787. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  788. assert_param(IS_LPTIM_PERIOD(Period));
  789. assert_param(IS_LPTIM_PULSE(Pulse));
  790. /* Set the LPTIM state */
  791. hlptim->State = HAL_LPTIM_STATE_BUSY;
  792. /* Set WAVE bit to enable the set once mode */
  793. hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
  794. /* Enable the Peripheral */
  795. __HAL_LPTIM_ENABLE(hlptim);
  796. /* Clear flag */
  797. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  798. /* Load the period value in the autoreload register */
  799. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  800. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  801. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  802. {
  803. return HAL_TIMEOUT;
  804. }
  805. /* Clear flag */
  806. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  807. /* Load the pulse value in the compare register */
  808. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  809. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  810. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  811. {
  812. return HAL_TIMEOUT;
  813. }
  814. /* Disable the Peripheral */
  815. __HAL_LPTIM_DISABLE(hlptim);
  816. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  817. {
  818. return HAL_TIMEOUT;
  819. }
  820. /* Enable Autoreload write complete interrupt */
  821. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
  822. /* Enable Compare write complete interrupt */
  823. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
  824. /* Enable Autoreload match interrupt */
  825. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  826. /* Enable Compare match interrupt */
  827. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
  828. /* If external trigger source is used, then enable external trigger interrupt */
  829. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  830. {
  831. /* Enable external trigger interrupt */
  832. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  833. }
  834. /* Enable the Peripheral */
  835. __HAL_LPTIM_ENABLE(hlptim);
  836. /* Start timer in single (one shot) mode */
  837. __HAL_LPTIM_START_SINGLE(hlptim);
  838. /* Change the TIM state*/
  839. hlptim->State = HAL_LPTIM_STATE_READY;
  840. /* Return function status */
  841. return HAL_OK;
  842. }
  843. /**
  844. * @brief Stop the LPTIM Set once mode in interrupt mode.
  845. * @param hlptim LPTIM handle
  846. * @retval HAL status
  847. */
  848. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  849. {
  850. /* Check the parameters */
  851. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  852. /* Set the LPTIM state */
  853. hlptim->State = HAL_LPTIM_STATE_BUSY;
  854. /* Disable the Peripheral */
  855. __HAL_LPTIM_DISABLE(hlptim);
  856. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  857. {
  858. return HAL_TIMEOUT;
  859. }
  860. /* Disable Autoreload write complete interrupt */
  861. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
  862. /* Disable Compare write complete interrupt */
  863. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
  864. /* Disable Autoreload match interrupt */
  865. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  866. /* Disable Compare match interrupt */
  867. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
  868. /* If external trigger source is used, then disable external trigger interrupt */
  869. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  870. {
  871. /* Disable external trigger interrupt */
  872. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  873. }
  874. /* Change the TIM state*/
  875. hlptim->State = HAL_LPTIM_STATE_READY;
  876. /* Return function status */
  877. return HAL_OK;
  878. }
  879. /**
  880. * @brief Start the Encoder interface.
  881. * @param hlptim LPTIM handle
  882. * @param Period Specifies the Autoreload value.
  883. * This parameter must be a value between 0x0000 and 0xFFFF.
  884. * @retval HAL status
  885. */
  886. HAL_StatusTypeDef HAL_LPTIM_Encoder_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  887. {
  888. uint32_t tmpcfgr;
  889. /* Check the parameters */
  890. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  891. assert_param(IS_LPTIM_PERIOD(Period));
  892. assert_param(hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC);
  893. assert_param(hlptim->Init.Clock.Prescaler == LPTIM_PRESCALER_DIV1);
  894. assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
  895. /* Set the LPTIM state */
  896. hlptim->State = HAL_LPTIM_STATE_BUSY;
  897. /* Get the LPTIMx CFGR value */
  898. tmpcfgr = hlptim->Instance->CFGR;
  899. /* Clear CKPOL bits */
  900. tmpcfgr &= (uint32_t)(~LPTIM_CFGR_CKPOL);
  901. /* Set Input polarity */
  902. tmpcfgr |= hlptim->Init.UltraLowPowerClock.Polarity;
  903. /* Write to LPTIMx CFGR */
  904. hlptim->Instance->CFGR = tmpcfgr;
  905. /* Set ENC bit to enable the encoder interface */
  906. hlptim->Instance->CFGR |= LPTIM_CFGR_ENC;
  907. /* Enable the Peripheral */
  908. __HAL_LPTIM_ENABLE(hlptim);
  909. /* Clear flag */
  910. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  911. /* Load the period value in the autoreload register */
  912. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  913. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  914. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  915. {
  916. return HAL_TIMEOUT;
  917. }
  918. /* Start timer in continuous mode */
  919. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  920. /* Change the TIM state*/
  921. hlptim->State = HAL_LPTIM_STATE_READY;
  922. /* Return function status */
  923. return HAL_OK;
  924. }
  925. /**
  926. * @brief Stop the Encoder interface.
  927. * @param hlptim LPTIM handle
  928. * @retval HAL status
  929. */
  930. HAL_StatusTypeDef HAL_LPTIM_Encoder_Stop(LPTIM_HandleTypeDef *hlptim)
  931. {
  932. /* Check the parameters */
  933. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  934. /* Set the LPTIM state */
  935. hlptim->State = HAL_LPTIM_STATE_BUSY;
  936. /* Disable the Peripheral */
  937. __HAL_LPTIM_DISABLE(hlptim);
  938. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  939. {
  940. return HAL_TIMEOUT;
  941. }
  942. /* Reset ENC bit to disable the encoder interface */
  943. hlptim->Instance->CFGR &= ~LPTIM_CFGR_ENC;
  944. /* Change the TIM state*/
  945. hlptim->State = HAL_LPTIM_STATE_READY;
  946. /* Return function status */
  947. return HAL_OK;
  948. }
  949. /**
  950. * @brief Start the Encoder interface in interrupt mode.
  951. * @param hlptim LPTIM handle
  952. * @param Period Specifies the Autoreload value.
  953. * This parameter must be a value between 0x0000 and 0xFFFF.
  954. * @retval HAL status
  955. */
  956. HAL_StatusTypeDef HAL_LPTIM_Encoder_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  957. {
  958. uint32_t tmpcfgr;
  959. /* Check the parameters */
  960. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  961. assert_param(IS_LPTIM_PERIOD(Period));
  962. assert_param(hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC);
  963. assert_param(hlptim->Init.Clock.Prescaler == LPTIM_PRESCALER_DIV1);
  964. assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
  965. /* Set the LPTIM state */
  966. hlptim->State = HAL_LPTIM_STATE_BUSY;
  967. /* Configure edge sensitivity for encoder mode */
  968. /* Get the LPTIMx CFGR value */
  969. tmpcfgr = hlptim->Instance->CFGR;
  970. /* Clear CKPOL bits */
  971. tmpcfgr &= (uint32_t)(~LPTIM_CFGR_CKPOL);
  972. /* Set Input polarity */
  973. tmpcfgr |= hlptim->Init.UltraLowPowerClock.Polarity;
  974. /* Write to LPTIMx CFGR */
  975. hlptim->Instance->CFGR = tmpcfgr;
  976. /* Set ENC bit to enable the encoder interface */
  977. hlptim->Instance->CFGR |= LPTIM_CFGR_ENC;
  978. /* Enable the Peripheral */
  979. __HAL_LPTIM_ENABLE(hlptim);
  980. /* Clear flag */
  981. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  982. /* Load the period value in the autoreload register */
  983. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  984. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  985. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  986. {
  987. return HAL_TIMEOUT;
  988. }
  989. /* Disable the Peripheral */
  990. __HAL_LPTIM_DISABLE(hlptim);
  991. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  992. {
  993. return HAL_TIMEOUT;
  994. }
  995. /* Enable "switch to down direction" interrupt */
  996. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_DOWN);
  997. /* Enable "switch to up direction" interrupt */
  998. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_UP);
  999. /* Enable the Peripheral */
  1000. __HAL_LPTIM_ENABLE(hlptim);
  1001. /* Start timer in continuous mode */
  1002. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  1003. /* Change the TIM state*/
  1004. hlptim->State = HAL_LPTIM_STATE_READY;
  1005. /* Return function status */
  1006. return HAL_OK;
  1007. }
  1008. /**
  1009. * @brief Stop the Encoder interface in interrupt mode.
  1010. * @param hlptim LPTIM handle
  1011. * @retval HAL status
  1012. */
  1013. HAL_StatusTypeDef HAL_LPTIM_Encoder_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  1014. {
  1015. /* Check the parameters */
  1016. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1017. /* Set the LPTIM state */
  1018. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1019. /* Disable the Peripheral */
  1020. __HAL_LPTIM_DISABLE(hlptim);
  1021. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  1022. {
  1023. return HAL_TIMEOUT;
  1024. }
  1025. /* Reset ENC bit to disable the encoder interface */
  1026. hlptim->Instance->CFGR &= ~LPTIM_CFGR_ENC;
  1027. /* Disable "switch to down direction" interrupt */
  1028. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_DOWN);
  1029. /* Disable "switch to up direction" interrupt */
  1030. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_UP);
  1031. /* Change the TIM state*/
  1032. hlptim->State = HAL_LPTIM_STATE_READY;
  1033. /* Return function status */
  1034. return HAL_OK;
  1035. }
  1036. /**
  1037. * @brief Start the Timeout function.
  1038. * @note The first trigger event will start the timer, any successive
  1039. * trigger event will reset the counter and the timer restarts.
  1040. * @param hlptim LPTIM handle
  1041. * @param Period Specifies the Autoreload value.
  1042. * This parameter must be a value between 0x0000 and 0xFFFF.
  1043. * @param Timeout Specifies the TimeOut value to reset the counter.
  1044. * This parameter must be a value between 0x0000 and 0xFFFF.
  1045. * @retval HAL status
  1046. */
  1047. HAL_StatusTypeDef HAL_LPTIM_TimeOut_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Timeout)
  1048. {
  1049. /* Check the parameters */
  1050. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1051. assert_param(IS_LPTIM_PERIOD(Period));
  1052. assert_param(IS_LPTIM_PULSE(Timeout));
  1053. /* Set the LPTIM state */
  1054. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1055. /* Set TIMOUT bit to enable the timeout function */
  1056. hlptim->Instance->CFGR |= LPTIM_CFGR_TIMOUT;
  1057. /* Enable the Peripheral */
  1058. __HAL_LPTIM_ENABLE(hlptim);
  1059. /* Clear flag */
  1060. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  1061. /* Load the period value in the autoreload register */
  1062. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  1063. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  1064. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  1065. {
  1066. return HAL_TIMEOUT;
  1067. }
  1068. /* Clear flag */
  1069. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  1070. /* Load the Timeout value in the compare register */
  1071. __HAL_LPTIM_COMPARE_SET(hlptim, Timeout);
  1072. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  1073. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  1074. {
  1075. return HAL_TIMEOUT;
  1076. }
  1077. /* Start timer in continuous mode */
  1078. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  1079. /* Change the TIM state*/
  1080. hlptim->State = HAL_LPTIM_STATE_READY;
  1081. /* Return function status */
  1082. return HAL_OK;
  1083. }
  1084. /**
  1085. * @brief Stop the Timeout function.
  1086. * @param hlptim LPTIM handle
  1087. * @retval HAL status
  1088. */
  1089. HAL_StatusTypeDef HAL_LPTIM_TimeOut_Stop(LPTIM_HandleTypeDef *hlptim)
  1090. {
  1091. /* Check the parameters */
  1092. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1093. /* Set the LPTIM state */
  1094. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1095. /* Disable the Peripheral */
  1096. __HAL_LPTIM_DISABLE(hlptim);
  1097. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  1098. {
  1099. return HAL_TIMEOUT;
  1100. }
  1101. /* Reset TIMOUT bit to enable the timeout function */
  1102. hlptim->Instance->CFGR &= ~LPTIM_CFGR_TIMOUT;
  1103. /* Change the TIM state*/
  1104. hlptim->State = HAL_LPTIM_STATE_READY;
  1105. /* Return function status */
  1106. return HAL_OK;
  1107. }
  1108. /**
  1109. * @brief Start the Timeout function in interrupt mode.
  1110. * @note The first trigger event will start the timer, any successive
  1111. * trigger event will reset the counter and the timer restarts.
  1112. * @param hlptim LPTIM handle
  1113. * @param Period Specifies the Autoreload value.
  1114. * This parameter must be a value between 0x0000 and 0xFFFF.
  1115. * @param Timeout Specifies the TimeOut value to reset the counter.
  1116. * This parameter must be a value between 0x0000 and 0xFFFF.
  1117. * @retval HAL status
  1118. */
  1119. HAL_StatusTypeDef HAL_LPTIM_TimeOut_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Timeout)
  1120. {
  1121. /* Check the parameters */
  1122. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1123. assert_param(IS_LPTIM_PERIOD(Period));
  1124. assert_param(IS_LPTIM_PULSE(Timeout));
  1125. /* Set the LPTIM state */
  1126. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1127. /* Enable EXTI Line interrupt on the LPTIM Wake-up Timer */
  1128. __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT();
  1129. #if defined(EXTI_IMR_MR23)
  1130. /* Enable rising edge trigger on the LPTIM Wake-up Timer Exti line */
  1131. __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  1132. #endif /* EXTI_IMR_MR23 */
  1133. /* Set TIMOUT bit to enable the timeout function */
  1134. hlptim->Instance->CFGR |= LPTIM_CFGR_TIMOUT;
  1135. /* Enable the Peripheral */
  1136. __HAL_LPTIM_ENABLE(hlptim);
  1137. /* Clear flag */
  1138. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  1139. /* Load the period value in the autoreload register */
  1140. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  1141. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  1142. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  1143. {
  1144. return HAL_TIMEOUT;
  1145. }
  1146. /* Clear flag */
  1147. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  1148. /* Load the Timeout value in the compare register */
  1149. __HAL_LPTIM_COMPARE_SET(hlptim, Timeout);
  1150. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  1151. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  1152. {
  1153. return HAL_TIMEOUT;
  1154. }
  1155. /* Disable the Peripheral */
  1156. __HAL_LPTIM_DISABLE(hlptim);
  1157. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  1158. {
  1159. return HAL_TIMEOUT;
  1160. }
  1161. /* Enable Compare match interrupt */
  1162. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
  1163. /* Enable the Peripheral */
  1164. __HAL_LPTIM_ENABLE(hlptim);
  1165. /* Start timer in continuous mode */
  1166. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  1167. /* Change the TIM state*/
  1168. hlptim->State = HAL_LPTIM_STATE_READY;
  1169. /* Return function status */
  1170. return HAL_OK;
  1171. }
  1172. /**
  1173. * @brief Stop the Timeout function in interrupt mode.
  1174. * @param hlptim LPTIM handle
  1175. * @retval HAL status
  1176. */
  1177. HAL_StatusTypeDef HAL_LPTIM_TimeOut_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  1178. {
  1179. /* Check the parameters */
  1180. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1181. /* Set the LPTIM state */
  1182. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1183. #if defined(EXTI_IMR_MR23)
  1184. /* Disable rising edge trigger on the LPTIM Wake-up Timer Exti line */
  1185. __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_RISING_EDGE();
  1186. #endif /* EXTI_IMR_MR23 */
  1187. /* Disable EXTI Line interrupt on the LPTIM Wake-up Timer */
  1188. __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_IT();
  1189. /* Disable the Peripheral */
  1190. __HAL_LPTIM_DISABLE(hlptim);
  1191. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  1192. {
  1193. return HAL_TIMEOUT;
  1194. }
  1195. /* Reset TIMOUT bit to enable the timeout function */
  1196. hlptim->Instance->CFGR &= ~LPTIM_CFGR_TIMOUT;
  1197. /* Disable Compare match interrupt */
  1198. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
  1199. /* Change the TIM state*/
  1200. hlptim->State = HAL_LPTIM_STATE_READY;
  1201. /* Return function status */
  1202. return HAL_OK;
  1203. }
  1204. /**
  1205. * @brief Start the Counter mode.
  1206. * @param hlptim LPTIM handle
  1207. * @param Period Specifies the Autoreload value.
  1208. * This parameter must be a value between 0x0000 and 0xFFFF.
  1209. * @retval HAL status
  1210. */
  1211. HAL_StatusTypeDef HAL_LPTIM_Counter_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  1212. {
  1213. /* Check the parameters */
  1214. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1215. assert_param(IS_LPTIM_PERIOD(Period));
  1216. /* Set the LPTIM state */
  1217. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1218. /* If clock source is not ULPTIM clock and counter source is external, then it must not be prescaled */
  1219. if ((hlptim->Init.Clock.Source != LPTIM_CLOCKSOURCE_ULPTIM) && (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
  1220. {
  1221. /* Check if clock is prescaled */
  1222. assert_param(IS_LPTIM_CLOCK_PRESCALERDIV1(hlptim->Init.Clock.Prescaler));
  1223. /* Set clock prescaler to 0 */
  1224. hlptim->Instance->CFGR &= ~LPTIM_CFGR_PRESC;
  1225. }
  1226. /* Enable the Peripheral */
  1227. __HAL_LPTIM_ENABLE(hlptim);
  1228. /* Clear flag */
  1229. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  1230. /* Load the period value in the autoreload register */
  1231. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  1232. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  1233. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  1234. {
  1235. return HAL_TIMEOUT;
  1236. }
  1237. /* Start timer in continuous mode */
  1238. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  1239. /* Change the TIM state*/
  1240. hlptim->State = HAL_LPTIM_STATE_READY;
  1241. /* Return function status */
  1242. return HAL_OK;
  1243. }
  1244. /**
  1245. * @brief Stop the Counter mode.
  1246. * @param hlptim LPTIM handle
  1247. * @retval HAL status
  1248. */
  1249. HAL_StatusTypeDef HAL_LPTIM_Counter_Stop(LPTIM_HandleTypeDef *hlptim)
  1250. {
  1251. /* Check the parameters */
  1252. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1253. /* Set the LPTIM state */
  1254. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1255. /* Disable the Peripheral */
  1256. __HAL_LPTIM_DISABLE(hlptim);
  1257. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  1258. {
  1259. return HAL_TIMEOUT;
  1260. }
  1261. /* Change the TIM state*/
  1262. hlptim->State = HAL_LPTIM_STATE_READY;
  1263. /* Return function status */
  1264. return HAL_OK;
  1265. }
  1266. /**
  1267. * @brief Start the Counter mode in interrupt mode.
  1268. * @param hlptim LPTIM handle
  1269. * @param Period Specifies the Autoreload value.
  1270. * This parameter must be a value between 0x0000 and 0xFFFF.
  1271. * @retval HAL status
  1272. */
  1273. HAL_StatusTypeDef HAL_LPTIM_Counter_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  1274. {
  1275. /* Check the parameters */
  1276. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1277. assert_param(IS_LPTIM_PERIOD(Period));
  1278. /* Set the LPTIM state */
  1279. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1280. /* Enable EXTI Line interrupt on the LPTIM Wake-up Timer */
  1281. __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT();
  1282. #if defined(EXTI_IMR_MR23)
  1283. /* Enable rising edge trigger on the LPTIM Wake-up Timer Exti line */
  1284. __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  1285. #endif /* EXTI_IMR_MR23 */
  1286. /* If clock source is not ULPTIM clock and counter source is external, then it must not be prescaled */
  1287. if ((hlptim->Init.Clock.Source != LPTIM_CLOCKSOURCE_ULPTIM) && (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
  1288. {
  1289. /* Check if clock is prescaled */
  1290. assert_param(IS_LPTIM_CLOCK_PRESCALERDIV1(hlptim->Init.Clock.Prescaler));
  1291. /* Set clock prescaler to 0 */
  1292. hlptim->Instance->CFGR &= ~LPTIM_CFGR_PRESC;
  1293. }
  1294. /* Enable the Peripheral */
  1295. __HAL_LPTIM_ENABLE(hlptim);
  1296. /* Clear flag */
  1297. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  1298. /* Load the period value in the autoreload register */
  1299. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  1300. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  1301. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  1302. {
  1303. return HAL_TIMEOUT;
  1304. }
  1305. /* Disable the Peripheral */
  1306. __HAL_LPTIM_DISABLE(hlptim);
  1307. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  1308. {
  1309. return HAL_TIMEOUT;
  1310. }
  1311. /* Enable Autoreload write complete interrupt */
  1312. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
  1313. /* Enable Autoreload match interrupt */
  1314. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  1315. /* Enable the Peripheral */
  1316. __HAL_LPTIM_ENABLE(hlptim);
  1317. /* Start timer in continuous mode */
  1318. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  1319. /* Change the TIM state*/
  1320. hlptim->State = HAL_LPTIM_STATE_READY;
  1321. /* Return function status */
  1322. return HAL_OK;
  1323. }
  1324. /**
  1325. * @brief Stop the Counter mode in interrupt mode.
  1326. * @param hlptim LPTIM handle
  1327. * @retval HAL status
  1328. */
  1329. HAL_StatusTypeDef HAL_LPTIM_Counter_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  1330. {
  1331. /* Check the parameters */
  1332. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1333. /* Set the LPTIM state */
  1334. hlptim->State = HAL_LPTIM_STATE_BUSY;
  1335. #if defined(EXTI_IMR_MR23)
  1336. /* Disable rising edge trigger on the LPTIM Wake-up Timer Exti line */
  1337. __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_RISING_EDGE();
  1338. #endif /* EXTI_IMR_MR23 */
  1339. /* Disable EXTI Line interrupt on the LPTIM Wake-up Timer */
  1340. __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_IT();
  1341. /* Disable the Peripheral */
  1342. __HAL_LPTIM_DISABLE(hlptim);
  1343. if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
  1344. {
  1345. return HAL_TIMEOUT;
  1346. }
  1347. /* Disable Autoreload write complete interrupt */
  1348. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
  1349. /* Disable Autoreload match interrupt */
  1350. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  1351. /* Change the TIM state*/
  1352. hlptim->State = HAL_LPTIM_STATE_READY;
  1353. /* Return function status */
  1354. return HAL_OK;
  1355. }
  1356. /**
  1357. * @}
  1358. */
  1359. /** @defgroup LPTIM_Exported_Functions_Group3 LPTIM Read operation functions
  1360. * @brief Read operation functions.
  1361. *
  1362. @verbatim
  1363. ==============================================================================
  1364. ##### LPTIM Read operation functions #####
  1365. ==============================================================================
  1366. [..] This section provides LPTIM Reading functions.
  1367. (+) Read the counter value.
  1368. (+) Read the period (Auto-reload) value.
  1369. (+) Read the pulse (Compare)value.
  1370. @endverbatim
  1371. * @{
  1372. */
  1373. /**
  1374. * @brief Return the current counter value.
  1375. * @param hlptim LPTIM handle
  1376. * @retval Counter value.
  1377. */
  1378. uint32_t HAL_LPTIM_ReadCounter(LPTIM_HandleTypeDef *hlptim)
  1379. {
  1380. /* Check the parameters */
  1381. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1382. return (hlptim->Instance->CNT);
  1383. }
  1384. /**
  1385. * @brief Return the current Autoreload (Period) value.
  1386. * @param hlptim LPTIM handle
  1387. * @retval Autoreload value.
  1388. */
  1389. uint32_t HAL_LPTIM_ReadAutoReload(LPTIM_HandleTypeDef *hlptim)
  1390. {
  1391. /* Check the parameters */
  1392. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1393. return (hlptim->Instance->ARR);
  1394. }
  1395. /**
  1396. * @brief Return the current Compare (Pulse) value.
  1397. * @param hlptim LPTIM handle
  1398. * @retval Compare value.
  1399. */
  1400. uint32_t HAL_LPTIM_ReadCompare(LPTIM_HandleTypeDef *hlptim)
  1401. {
  1402. /* Check the parameters */
  1403. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1404. return (hlptim->Instance->CMP);
  1405. }
  1406. /**
  1407. * @}
  1408. */
  1409. /** @defgroup LPTIM_Exported_Functions_Group4 LPTIM IRQ handler and callbacks
  1410. * @brief LPTIM IRQ handler.
  1411. *
  1412. @verbatim
  1413. ==============================================================================
  1414. ##### LPTIM IRQ handler and callbacks #####
  1415. ==============================================================================
  1416. [..] This section provides LPTIM IRQ handler and callback functions called within
  1417. the IRQ handler:
  1418. (+) LPTIM interrupt request handler
  1419. (+) Compare match Callback
  1420. (+) Auto-reload match Callback
  1421. (+) External trigger event detection Callback
  1422. (+) Compare register write complete Callback
  1423. (+) Auto-reload register write complete Callback
  1424. (+) Up-counting direction change Callback
  1425. (+) Down-counting direction change Callback
  1426. @endverbatim
  1427. * @{
  1428. */
  1429. /**
  1430. * @brief Handle LPTIM interrupt request.
  1431. * @param hlptim LPTIM handle
  1432. * @retval None
  1433. */
  1434. void HAL_LPTIM_IRQHandler(LPTIM_HandleTypeDef *hlptim)
  1435. {
  1436. /* Compare match interrupt */
  1437. if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_CMPM) != RESET)
  1438. {
  1439. if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_CMPM) != RESET)
  1440. {
  1441. /* Clear Compare match flag */
  1442. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPM);
  1443. /* Compare match Callback */
  1444. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1445. hlptim->CompareMatchCallback(hlptim);
  1446. #else
  1447. HAL_LPTIM_CompareMatchCallback(hlptim);
  1448. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1449. }
  1450. }
  1451. /* Autoreload match interrupt */
  1452. if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARRM) != RESET)
  1453. {
  1454. if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARRM) != RESET)
  1455. {
  1456. /* Clear Autoreload match flag */
  1457. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARRM);
  1458. /* Autoreload match Callback */
  1459. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1460. hlptim->AutoReloadMatchCallback(hlptim);
  1461. #else
  1462. HAL_LPTIM_AutoReloadMatchCallback(hlptim);
  1463. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1464. }
  1465. }
  1466. /* Trigger detected interrupt */
  1467. if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_EXTTRIG) != RESET)
  1468. {
  1469. if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_EXTTRIG) != RESET)
  1470. {
  1471. /* Clear Trigger detected flag */
  1472. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_EXTTRIG);
  1473. /* Trigger detected callback */
  1474. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1475. hlptim->TriggerCallback(hlptim);
  1476. #else
  1477. HAL_LPTIM_TriggerCallback(hlptim);
  1478. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1479. }
  1480. }
  1481. /* Compare write interrupt */
  1482. if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_CMPOK) != RESET)
  1483. {
  1484. if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_CMPOK) != RESET)
  1485. {
  1486. /* Clear Compare write flag */
  1487. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  1488. /* Compare write Callback */
  1489. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1490. hlptim->CompareWriteCallback(hlptim);
  1491. #else
  1492. HAL_LPTIM_CompareWriteCallback(hlptim);
  1493. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1494. }
  1495. }
  1496. /* Autoreload write interrupt */
  1497. if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARROK) != RESET)
  1498. {
  1499. if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARROK) != RESET)
  1500. {
  1501. /* Clear Autoreload write flag */
  1502. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  1503. /* Autoreload write Callback */
  1504. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1505. hlptim->AutoReloadWriteCallback(hlptim);
  1506. #else
  1507. HAL_LPTIM_AutoReloadWriteCallback(hlptim);
  1508. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1509. }
  1510. }
  1511. /* Direction counter changed from Down to Up interrupt */
  1512. if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_UP) != RESET)
  1513. {
  1514. if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_UP) != RESET)
  1515. {
  1516. /* Clear Direction counter changed from Down to Up flag */
  1517. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_UP);
  1518. /* Direction counter changed from Down to Up Callback */
  1519. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1520. hlptim->DirectionUpCallback(hlptim);
  1521. #else
  1522. HAL_LPTIM_DirectionUpCallback(hlptim);
  1523. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1524. }
  1525. }
  1526. /* Direction counter changed from Up to Down interrupt */
  1527. if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_DOWN) != RESET)
  1528. {
  1529. if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_DOWN) != RESET)
  1530. {
  1531. /* Clear Direction counter changed from Up to Down flag */
  1532. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_DOWN);
  1533. /* Direction counter changed from Up to Down Callback */
  1534. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1535. hlptim->DirectionDownCallback(hlptim);
  1536. #else
  1537. HAL_LPTIM_DirectionDownCallback(hlptim);
  1538. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1539. }
  1540. }
  1541. #if defined(EXTI_IMR_MR23)
  1542. __HAL_LPTIM_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  1543. #endif /* EXTI_IMR_MR23 */
  1544. }
  1545. /**
  1546. * @brief Compare match callback in non-blocking mode.
  1547. * @param hlptim LPTIM handle
  1548. * @retval None
  1549. */
  1550. __weak void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
  1551. {
  1552. /* Prevent unused argument(s) compilation warning */
  1553. UNUSED(hlptim);
  1554. /* NOTE : This function should not be modified, when the callback is needed,
  1555. the HAL_LPTIM_CompareMatchCallback could be implemented in the user file
  1556. */
  1557. }
  1558. /**
  1559. * @brief Autoreload match callback in non-blocking mode.
  1560. * @param hlptim LPTIM handle
  1561. * @retval None
  1562. */
  1563. __weak void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
  1564. {
  1565. /* Prevent unused argument(s) compilation warning */
  1566. UNUSED(hlptim);
  1567. /* NOTE : This function should not be modified, when the callback is needed,
  1568. the HAL_LPTIM_AutoReloadMatchCallback could be implemented in the user file
  1569. */
  1570. }
  1571. /**
  1572. * @brief Trigger detected callback in non-blocking mode.
  1573. * @param hlptim LPTIM handle
  1574. * @retval None
  1575. */
  1576. __weak void HAL_LPTIM_TriggerCallback(LPTIM_HandleTypeDef *hlptim)
  1577. {
  1578. /* Prevent unused argument(s) compilation warning */
  1579. UNUSED(hlptim);
  1580. /* NOTE : This function should not be modified, when the callback is needed,
  1581. the HAL_LPTIM_TriggerCallback could be implemented in the user file
  1582. */
  1583. }
  1584. /**
  1585. * @brief Compare write callback in non-blocking mode.
  1586. * @param hlptim LPTIM handle
  1587. * @retval None
  1588. */
  1589. __weak void HAL_LPTIM_CompareWriteCallback(LPTIM_HandleTypeDef *hlptim)
  1590. {
  1591. /* Prevent unused argument(s) compilation warning */
  1592. UNUSED(hlptim);
  1593. /* NOTE : This function should not be modified, when the callback is needed,
  1594. the HAL_LPTIM_CompareWriteCallback could be implemented in the user file
  1595. */
  1596. }
  1597. /**
  1598. * @brief Autoreload write callback in non-blocking mode.
  1599. * @param hlptim LPTIM handle
  1600. * @retval None
  1601. */
  1602. __weak void HAL_LPTIM_AutoReloadWriteCallback(LPTIM_HandleTypeDef *hlptim)
  1603. {
  1604. /* Prevent unused argument(s) compilation warning */
  1605. UNUSED(hlptim);
  1606. /* NOTE : This function should not be modified, when the callback is needed,
  1607. the HAL_LPTIM_AutoReloadWriteCallback could be implemented in the user file
  1608. */
  1609. }
  1610. /**
  1611. * @brief Direction counter changed from Down to Up callback in non-blocking mode.
  1612. * @param hlptim LPTIM handle
  1613. * @retval None
  1614. */
  1615. __weak void HAL_LPTIM_DirectionUpCallback(LPTIM_HandleTypeDef *hlptim)
  1616. {
  1617. /* Prevent unused argument(s) compilation warning */
  1618. UNUSED(hlptim);
  1619. /* NOTE : This function should not be modified, when the callback is needed,
  1620. the HAL_LPTIM_DirectionUpCallback could be implemented in the user file
  1621. */
  1622. }
  1623. /**
  1624. * @brief Direction counter changed from Up to Down callback in non-blocking mode.
  1625. * @param hlptim LPTIM handle
  1626. * @retval None
  1627. */
  1628. __weak void HAL_LPTIM_DirectionDownCallback(LPTIM_HandleTypeDef *hlptim)
  1629. {
  1630. /* Prevent unused argument(s) compilation warning */
  1631. UNUSED(hlptim);
  1632. /* NOTE : This function should not be modified, when the callback is needed,
  1633. the HAL_LPTIM_DirectionDownCallback could be implemented in the user file
  1634. */
  1635. }
  1636. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1637. /**
  1638. * @brief Register a User LPTIM callback to be used instead of the weak predefined callback
  1639. * @param hlptim LPTIM handle
  1640. * @param CallbackID ID of the callback to be registered
  1641. * This parameter can be one of the following values:
  1642. * @arg @ref HAL_LPTIM_MSPINIT_CB_ID LPTIM Base Msp Init Callback ID
  1643. * @arg @ref HAL_LPTIM_MSPDEINIT_CB_ID LPTIM Base Msp DeInit Callback ID
  1644. * @arg @ref HAL_LPTIM_COMPARE_MATCH_CB_ID Compare match Callback ID
  1645. * @arg @ref HAL_LPTIM_AUTORELOAD_MATCH_CB_ID Auto-reload match Callback ID
  1646. * @arg @ref HAL_LPTIM_TRIGGER_CB_ID External trigger event detection Callback ID
  1647. * @arg @ref HAL_LPTIM_COMPARE_WRITE_CB_ID Compare register write complete Callback ID
  1648. * @arg @ref HAL_LPTIM_AUTORELOAD_WRITE_CB_ID Auto-reload register write complete Callback ID
  1649. * @arg @ref HAL_LPTIM_DIRECTION_UP_CB_ID Up-counting direction change Callback ID
  1650. * @arg @ref HAL_LPTIM_DIRECTION_DOWN_CB_ID Down-counting direction change Callback ID
  1651. * @param pCallback pointer to the callback function
  1652. * @retval status
  1653. */
  1654. HAL_StatusTypeDef HAL_LPTIM_RegisterCallback(LPTIM_HandleTypeDef *hlptim,
  1655. HAL_LPTIM_CallbackIDTypeDef CallbackID,
  1656. pLPTIM_CallbackTypeDef pCallback)
  1657. {
  1658. HAL_StatusTypeDef status = HAL_OK;
  1659. if (pCallback == NULL)
  1660. {
  1661. return HAL_ERROR;
  1662. }
  1663. /* Process locked */
  1664. __HAL_LOCK(hlptim);
  1665. if (hlptim->State == HAL_LPTIM_STATE_READY)
  1666. {
  1667. switch (CallbackID)
  1668. {
  1669. case HAL_LPTIM_MSPINIT_CB_ID :
  1670. hlptim->MspInitCallback = pCallback;
  1671. break;
  1672. case HAL_LPTIM_MSPDEINIT_CB_ID :
  1673. hlptim->MspDeInitCallback = pCallback;
  1674. break;
  1675. case HAL_LPTIM_COMPARE_MATCH_CB_ID :
  1676. hlptim->CompareMatchCallback = pCallback;
  1677. break;
  1678. case HAL_LPTIM_AUTORELOAD_MATCH_CB_ID :
  1679. hlptim->AutoReloadMatchCallback = pCallback;
  1680. break;
  1681. case HAL_LPTIM_TRIGGER_CB_ID :
  1682. hlptim->TriggerCallback = pCallback;
  1683. break;
  1684. case HAL_LPTIM_COMPARE_WRITE_CB_ID :
  1685. hlptim->CompareWriteCallback = pCallback;
  1686. break;
  1687. case HAL_LPTIM_AUTORELOAD_WRITE_CB_ID :
  1688. hlptim->AutoReloadWriteCallback = pCallback;
  1689. break;
  1690. case HAL_LPTIM_DIRECTION_UP_CB_ID :
  1691. hlptim->DirectionUpCallback = pCallback;
  1692. break;
  1693. case HAL_LPTIM_DIRECTION_DOWN_CB_ID :
  1694. hlptim->DirectionDownCallback = pCallback;
  1695. break;
  1696. default :
  1697. /* Return error status */
  1698. status = HAL_ERROR;
  1699. break;
  1700. }
  1701. }
  1702. else if (hlptim->State == HAL_LPTIM_STATE_RESET)
  1703. {
  1704. switch (CallbackID)
  1705. {
  1706. case HAL_LPTIM_MSPINIT_CB_ID :
  1707. hlptim->MspInitCallback = pCallback;
  1708. break;
  1709. case HAL_LPTIM_MSPDEINIT_CB_ID :
  1710. hlptim->MspDeInitCallback = pCallback;
  1711. break;
  1712. default :
  1713. /* Return error status */
  1714. status = HAL_ERROR;
  1715. break;
  1716. }
  1717. }
  1718. else
  1719. {
  1720. /* Return error status */
  1721. status = HAL_ERROR;
  1722. }
  1723. /* Release Lock */
  1724. __HAL_UNLOCK(hlptim);
  1725. return status;
  1726. }
  1727. /**
  1728. * @brief Unregister a LPTIM callback
  1729. * LLPTIM callback is redirected to the weak predefined callback
  1730. * @param hlptim LPTIM handle
  1731. * @param CallbackID ID of the callback to be unregistered
  1732. * This parameter can be one of the following values:
  1733. * @arg @ref HAL_LPTIM_MSPINIT_CB_ID LPTIM Base Msp Init Callback ID
  1734. * @arg @ref HAL_LPTIM_MSPDEINIT_CB_ID LPTIM Base Msp DeInit Callback ID
  1735. * @arg @ref HAL_LPTIM_COMPARE_MATCH_CB_ID Compare match Callback ID
  1736. * @arg @ref HAL_LPTIM_AUTORELOAD_MATCH_CB_ID Auto-reload match Callback ID
  1737. * @arg @ref HAL_LPTIM_TRIGGER_CB_ID External trigger event detection Callback ID
  1738. * @arg @ref HAL_LPTIM_COMPARE_WRITE_CB_ID Compare register write complete Callback ID
  1739. * @arg @ref HAL_LPTIM_AUTORELOAD_WRITE_CB_ID Auto-reload register write complete Callback ID
  1740. * @arg @ref HAL_LPTIM_DIRECTION_UP_CB_ID Up-counting direction change Callback ID
  1741. * @arg @ref HAL_LPTIM_DIRECTION_DOWN_CB_ID Down-counting direction change Callback ID
  1742. * @retval status
  1743. */
  1744. HAL_StatusTypeDef HAL_LPTIM_UnRegisterCallback(LPTIM_HandleTypeDef *hlptim,
  1745. HAL_LPTIM_CallbackIDTypeDef CallbackID)
  1746. {
  1747. HAL_StatusTypeDef status = HAL_OK;
  1748. /* Process locked */
  1749. __HAL_LOCK(hlptim);
  1750. if (hlptim->State == HAL_LPTIM_STATE_READY)
  1751. {
  1752. switch (CallbackID)
  1753. {
  1754. case HAL_LPTIM_MSPINIT_CB_ID :
  1755. hlptim->MspInitCallback = HAL_LPTIM_MspInit; /* Legacy weak MspInit Callback */
  1756. break;
  1757. case HAL_LPTIM_MSPDEINIT_CB_ID :
  1758. hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit; /* Legacy weak Msp DeInit Callback */
  1759. break;
  1760. case HAL_LPTIM_COMPARE_MATCH_CB_ID :
  1761. hlptim->CompareMatchCallback = HAL_LPTIM_CompareMatchCallback; /* Legacy weak Compare match Callback */
  1762. break;
  1763. case HAL_LPTIM_AUTORELOAD_MATCH_CB_ID :
  1764. hlptim->AutoReloadMatchCallback = HAL_LPTIM_AutoReloadMatchCallback; /* Legacy weak Auto-reload match Callback */
  1765. break;
  1766. case HAL_LPTIM_TRIGGER_CB_ID :
  1767. hlptim->TriggerCallback = HAL_LPTIM_TriggerCallback; /* Legacy weak External trigger event detection Callback */
  1768. break;
  1769. case HAL_LPTIM_COMPARE_WRITE_CB_ID :
  1770. hlptim->CompareWriteCallback = HAL_LPTIM_CompareWriteCallback; /* Legacy weak Compare register write complete Callback */
  1771. break;
  1772. case HAL_LPTIM_AUTORELOAD_WRITE_CB_ID :
  1773. hlptim->AutoReloadWriteCallback = HAL_LPTIM_AutoReloadWriteCallback; /* Legacy weak Auto-reload register write complete Callback */
  1774. break;
  1775. case HAL_LPTIM_DIRECTION_UP_CB_ID :
  1776. hlptim->DirectionUpCallback = HAL_LPTIM_DirectionUpCallback; /* Legacy weak Up-counting direction change Callback */
  1777. break;
  1778. case HAL_LPTIM_DIRECTION_DOWN_CB_ID :
  1779. hlptim->DirectionDownCallback = HAL_LPTIM_DirectionDownCallback; /* Legacy weak Down-counting direction change Callback */
  1780. break;
  1781. default :
  1782. /* Return error status */
  1783. status = HAL_ERROR;
  1784. break;
  1785. }
  1786. }
  1787. else if (hlptim->State == HAL_LPTIM_STATE_RESET)
  1788. {
  1789. switch (CallbackID)
  1790. {
  1791. case HAL_LPTIM_MSPINIT_CB_ID :
  1792. hlptim->MspInitCallback = HAL_LPTIM_MspInit; /* Legacy weak MspInit Callback */
  1793. break;
  1794. case HAL_LPTIM_MSPDEINIT_CB_ID :
  1795. hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit; /* Legacy weak Msp DeInit Callback */
  1796. break;
  1797. default :
  1798. /* Return error status */
  1799. status = HAL_ERROR;
  1800. break;
  1801. }
  1802. }
  1803. else
  1804. {
  1805. /* Return error status */
  1806. status = HAL_ERROR;
  1807. }
  1808. /* Release Lock */
  1809. __HAL_UNLOCK(hlptim);
  1810. return status;
  1811. }
  1812. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1813. /**
  1814. * @}
  1815. */
  1816. /** @defgroup LPTIM_Group5 Peripheral State functions
  1817. * @brief Peripheral State functions.
  1818. *
  1819. @verbatim
  1820. ==============================================================================
  1821. ##### Peripheral State functions #####
  1822. ==============================================================================
  1823. [..]
  1824. This subsection permits to get in run-time the status of the peripheral.
  1825. @endverbatim
  1826. * @{
  1827. */
  1828. /**
  1829. * @brief Return the LPTIM handle state.
  1830. * @param hlptim LPTIM handle
  1831. * @retval HAL state
  1832. */
  1833. HAL_LPTIM_StateTypeDef HAL_LPTIM_GetState(LPTIM_HandleTypeDef *hlptim)
  1834. {
  1835. /* Return LPTIM handle state */
  1836. return hlptim->State;
  1837. }
  1838. /**
  1839. * @}
  1840. */
  1841. /**
  1842. * @}
  1843. */
  1844. /* Private functions ---------------------------------------------------------*/
  1845. /** @defgroup LPTIM_Private_Functions LPTIM Private Functions
  1846. * @{
  1847. */
  1848. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  1849. /**
  1850. * @brief Reset interrupt callbacks to the legacy weak callbacks.
  1851. * @param lptim pointer to a LPTIM_HandleTypeDef structure that contains
  1852. * the configuration information for LPTIM module.
  1853. * @retval None
  1854. */
  1855. static void LPTIM_ResetCallback(LPTIM_HandleTypeDef *lptim)
  1856. {
  1857. /* Reset the LPTIM callback to the legacy weak callbacks */
  1858. lptim->CompareMatchCallback = HAL_LPTIM_CompareMatchCallback; /* Compare match Callback */
  1859. lptim->AutoReloadMatchCallback = HAL_LPTIM_AutoReloadMatchCallback; /* Auto-reload match Callback */
  1860. lptim->TriggerCallback = HAL_LPTIM_TriggerCallback; /* External trigger event detection Callback */
  1861. lptim->CompareWriteCallback = HAL_LPTIM_CompareWriteCallback; /* Compare register write complete Callback */
  1862. lptim->AutoReloadWriteCallback = HAL_LPTIM_AutoReloadWriteCallback; /* Auto-reload register write complete Callback */
  1863. lptim->DirectionUpCallback = HAL_LPTIM_DirectionUpCallback; /* Up-counting direction change Callback */
  1864. lptim->DirectionDownCallback = HAL_LPTIM_DirectionDownCallback; /* Down-counting direction change Callback */
  1865. }
  1866. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  1867. /**
  1868. * @brief LPTimer Wait for flag set
  1869. * @param hlptim pointer to a LPTIM_HandleTypeDef structure that contains
  1870. * the configuration information for LPTIM module.
  1871. * @param flag The lptim flag
  1872. * @retval HAL status
  1873. */
  1874. static HAL_StatusTypeDef LPTIM_WaitForFlag(LPTIM_HandleTypeDef *hlptim, uint32_t flag)
  1875. {
  1876. HAL_StatusTypeDef result = HAL_OK;
  1877. uint32_t count = TIMEOUT * (SystemCoreClock / 20UL / 1000UL);
  1878. do
  1879. {
  1880. count--;
  1881. if (count == 0UL)
  1882. {
  1883. result = HAL_TIMEOUT;
  1884. }
  1885. }
  1886. while ((!(__HAL_LPTIM_GET_FLAG((hlptim), (flag)))) && (count != 0UL));
  1887. return result;
  1888. }
  1889. /**
  1890. * @brief Disable LPTIM HW instance.
  1891. * @param hlptim pointer to a LPTIM_HandleTypeDef structure that contains
  1892. * the configuration information for LPTIM module.
  1893. * @note The following sequence is required to solve LPTIM disable HW limitation.
  1894. * Please check Errata Sheet ES0335 for more details under "MCU may remain
  1895. * stuck in LPTIM interrupt when entering Stop mode" section.
  1896. * @retval None
  1897. */
  1898. void LPTIM_Disable(LPTIM_HandleTypeDef *hlptim)
  1899. {
  1900. uint32_t tmpclksource = 0;
  1901. uint32_t tmpIER;
  1902. uint32_t tmpCFGR;
  1903. uint32_t tmpCMP;
  1904. uint32_t tmpARR;
  1905. uint32_t tmpOR;
  1906. __disable_irq();
  1907. /*********** Save LPTIM Config ***********/
  1908. /* Save LPTIM source clock */
  1909. switch ((uint32_t)hlptim->Instance)
  1910. {
  1911. case LPTIM1_BASE:
  1912. tmpclksource = __HAL_RCC_GET_LPTIM1_SOURCE();
  1913. break;
  1914. default:
  1915. break;
  1916. }
  1917. /* Save LPTIM configuration registers */
  1918. tmpIER = hlptim->Instance->IER;
  1919. tmpCFGR = hlptim->Instance->CFGR;
  1920. tmpCMP = hlptim->Instance->CMP;
  1921. tmpARR = hlptim->Instance->ARR;
  1922. tmpOR = hlptim->Instance->OR;
  1923. /*********** Reset LPTIM ***********/
  1924. switch ((uint32_t)hlptim->Instance)
  1925. {
  1926. case LPTIM1_BASE:
  1927. __HAL_RCC_LPTIM1_FORCE_RESET();
  1928. __HAL_RCC_LPTIM1_RELEASE_RESET();
  1929. break;
  1930. default:
  1931. break;
  1932. }
  1933. /*********** Restore LPTIM Config ***********/
  1934. if ((tmpCMP != 0UL) || (tmpARR != 0UL))
  1935. {
  1936. /* Force LPTIM source kernel clock from APB */
  1937. switch ((uint32_t)hlptim->Instance)
  1938. {
  1939. case LPTIM1_BASE:
  1940. __HAL_RCC_LPTIM1_CONFIG(RCC_LPTIM1CLKSOURCE_PCLK1);
  1941. break;
  1942. default:
  1943. break;
  1944. }
  1945. if (tmpCMP != 0UL)
  1946. {
  1947. /* Restore CMP register (LPTIM should be enabled first) */
  1948. hlptim->Instance->CR |= LPTIM_CR_ENABLE;
  1949. hlptim->Instance->CMP = tmpCMP;
  1950. /* Wait for the completion of the write operation to the LPTIM_CMP register */
  1951. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
  1952. {
  1953. hlptim->State = HAL_LPTIM_STATE_TIMEOUT;
  1954. }
  1955. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  1956. }
  1957. if (tmpARR != 0UL)
  1958. {
  1959. /* Restore ARR register (LPTIM should be enabled first) */
  1960. hlptim->Instance->CR |= LPTIM_CR_ENABLE;
  1961. hlptim->Instance->ARR = tmpARR;
  1962. /* Wait for the completion of the write operation to the LPTIM_ARR register */
  1963. if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
  1964. {
  1965. hlptim->State = HAL_LPTIM_STATE_TIMEOUT;
  1966. }
  1967. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  1968. }
  1969. /* Restore LPTIM source kernel clock */
  1970. switch ((uint32_t)hlptim->Instance)
  1971. {
  1972. case LPTIM1_BASE:
  1973. __HAL_RCC_LPTIM1_CONFIG(tmpclksource);
  1974. break;
  1975. default:
  1976. break;
  1977. }
  1978. }
  1979. /* Restore configuration registers (LPTIM should be disabled first) */
  1980. hlptim->Instance->CR &= ~(LPTIM_CR_ENABLE);
  1981. hlptim->Instance->IER = tmpIER;
  1982. hlptim->Instance->CFGR = tmpCFGR;
  1983. hlptim->Instance->OR = tmpOR;
  1984. __enable_irq();
  1985. }
  1986. /**
  1987. * @}
  1988. */
  1989. #endif /* LPTIM1 */
  1990. #endif /* HAL_LPTIM_MODULE_ENABLED */
  1991. /**
  1992. * @}
  1993. */
  1994. /**
  1995. * @}
  1996. */
  1997. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/