stm32g4xx_hal_lptim.c 73 KB

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