stm32f4xx_hal_lptim.c 74 KB

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