stm32g0xx_hal_lptim.c 77 KB

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