stm32l4xx_hal_lptim.c 81 KB

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