py32f002b_hal_lptim.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_lptim.c
  4. * @author MCU Application Team
  5. * @brief LPTIM HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Low Power Timer (LPTIM) peripheral:
  8. * + Initialization and de-initialization functions.
  9. * + Start/Stop operation functions in polling mode.
  10. * + Start/Stop operation functions in interrupt mode.
  11. * + Reading operation functions.
  12. * + Peripheral State functions.
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The LPTIM HAL driver can be used as follows:
  20. (#)Initialize the LPTIM low level resources by implementing the
  21. HAL_LPTIM_MspInit():
  22. (++) Enable the LPTIM interface clock using __HAL_RCC_LPTIMx_CLK_ENABLE().
  23. (++) In case of using interrupts (e.g. HAL_LPTIM_PWM_Start_IT()):
  24. (+++) Configure the LPTIM interrupt priority using HAL_NVIC_SetPriority().
  25. (+++) Enable the LPTIM IRQ handler using HAL_NVIC_EnableIRQ().
  26. (+++) In LPTIM IRQ handler, call HAL_LPTIM_IRQHandler().
  27. (#)Initialize the LPTIM HAL using HAL_LPTIM_Init(). This function
  28. configures mainly:
  29. (++) The instance: LPTIM1 or LPTIM2.
  30. (++) Clock: the counter clock.
  31. (+++) Source : it can be either the ULPTIM input (IN1) or one of
  32. the internal clock; (APB, LSE, LSI or HSI).
  33. (+++) Prescaler: select the clock divider.
  34. (#)Six modes are available:
  35. (++) PWM Mode: To generate a PWM signal with specified period and pulse,
  36. call HAL_LPTIM_PWM_Start() or HAL_LPTIM_PWM_Start_IT() for interruption
  37. mode.
  38. (++) One Pulse Mode: To generate pulse with specified width in response
  39. to a stimulus, call HAL_LPTIM_OnePulse_Start() or
  40. HAL_LPTIM_OnePulse_Start_IT() for interruption mode.
  41. (++) Set once Mode: In this mode, the output changes the level (from
  42. low level to high level if the output polarity is configured high, else
  43. the opposite) when a compare match occurs. To start this mode, call
  44. HAL_LPTIM_SetOnce_Start() or HAL_LPTIM_SetOnce_Start_IT() for
  45. interruption mode.
  46. (++) Encoder Mode: To use the encoder interface call
  47. HAL_LPTIM_Encoder_Start() or HAL_LPTIM_Encoder_Start_IT() for
  48. interruption mode. Only available for LPTIM1 instance.
  49. (++) Time out Mode: an active edge on one selected trigger input rests
  50. the counter. The first trigger event will start the timer, any
  51. successive trigger event will reset the counter and the timer will
  52. restart. To start this mode call HAL_LPTIM_TimeOut_Start_IT() or
  53. HAL_LPTIM_TimeOut_Start_IT() for interruption mode.
  54. (++) Counter Mode: counter can be used to count external events on
  55. the LPTIM Input1 or it can be used to count internal clock cycles.
  56. To start this mode, call HAL_LPTIM_Counter_Start() or
  57. HAL_LPTIM_Counter_Start_IT() for interruption mode.
  58. (#) User can stop any process by calling the corresponding API:
  59. HAL_LPTIM_Xxx_Stop() or HAL_LPTIM_Xxx_Stop_IT() if the process is
  60. already started in interruption mode.
  61. (#) De-initialize the LPTIM peripheral using HAL_LPTIM_DeInit().
  62. *** Callback registration ***
  63. =============================================
  64. [..]
  65. The compilation define USE_HAL_LPTIM_REGISTER_CALLBACKS when set to 1
  66. allows the user to configure dynamically the driver callbacks.
  67. [..]
  68. Use Function @ref HAL_LPTIM_RegisterCallback() to register a callback.
  69. @ref HAL_LPTIM_RegisterCallback() takes as parameters the HAL peripheral handle,
  70. the Callback ID and a pointer to the user callback function.
  71. [..]
  72. Use function @ref HAL_LPTIM_UnRegisterCallback() to reset a callback to the
  73. default weak function.
  74. @ref HAL_LPTIM_UnRegisterCallback takes as parameters the HAL peripheral handle,
  75. and the Callback ID.
  76. [..]
  77. These functions allow to register/unregister following callbacks:
  78. (+) MspInitCallback : LPTIM Base Msp Init Callback.
  79. (+) MspDeInitCallback : LPTIM Base Msp DeInit Callback.
  80. (+) CompareMatchCallback : Compare match Callback.
  81. (+) AutoReloadMatchCallback : Auto-reload match Callback.
  82. (+) TriggerCallback : External trigger event detection Callback.
  83. (+) CompareWriteCallback : Compare register write complete Callback.
  84. (+) AutoReloadWriteCallback : Auto-reload register write complete Callback.
  85. (+) DirectionUpCallback : Up-counting direction change Callback.
  86. (+) DirectionDownCallback : Down-counting direction change Callback.
  87. [..]
  88. By default, after the Init and when the state is HAL_LPTIM_STATE_RESET
  89. all interrupt callbacks are set to the corresponding weak functions:
  90. examples @ref HAL_LPTIM_TriggerCallback(), @ref HAL_LPTIM_CompareMatchCallback().
  91. [..]
  92. Exception done for MspInit and MspDeInit functions that are reset to the legacy weak
  93. functionalities in the Init/DeInit only when these callbacks are null
  94. (not registered beforehand). If not, MspInit or MspDeInit are not null, the Init/DeInit
  95. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  96. [..]
  97. Callbacks can be registered/unregistered in HAL_LPTIM_STATE_READY state only.
  98. Exception done MspInit/MspDeInit that can be registered/unregistered
  99. in HAL_LPTIM_STATE_READY or HAL_LPTIM_STATE_RESET state,
  100. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  101. In that case first register the MspInit/MspDeInit user callbacks
  102. using @ref HAL_LPTIM_RegisterCallback() before calling DeInit or Init function.
  103. [..]
  104. When The compilation define USE_HAL_LPTIM_REGISTER_CALLBACKS is set to 0 or
  105. not defined, the callback registration feature is not available and all callbacks
  106. are set to the corresponding weak functions.
  107. @endverbatim
  108. ******************************************************************************
  109. * @attention
  110. *
  111. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  112. * All rights reserved.</center></h2>
  113. *
  114. * This software component is licensed by Puya under BSD 3-Clause license,
  115. * the "License"; You may not use this file except in compliance with the
  116. * License. You may obtain a copy of the License at:
  117. * opensource.org/licenses/BSD-3-Clause
  118. *
  119. ******************************************************************************
  120. * @attention
  121. *
  122. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  123. * All rights reserved.</center></h2>
  124. *
  125. * This software component is licensed by ST under BSD 3-Clause license,
  126. * the "License"; You may not use this file except in compliance with the
  127. * License. You may obtain a copy of the License at:
  128. * opensource.org/licenses/BSD-3-Clause
  129. *
  130. ******************************************************************************
  131. */
  132. /* Includes ------------------------------------------------------------------*/
  133. #include "py32f0xx_hal.h"
  134. /** @addtogroup PY32F002B_HAL_Driver
  135. * @{
  136. */
  137. /** @defgroup LPTIM LPTIM
  138. * @brief LPTIM HAL module driver.
  139. * @{
  140. */
  141. #ifdef HAL_LPTIM_MODULE_ENABLED
  142. /* Private typedef -----------------------------------------------------------*/
  143. /* Private define ------------------------------------------------------------*/
  144. /* Private macro -------------------------------------------------------------*/
  145. /* Private variables ---------------------------------------------------------*/
  146. /* Private function prototypes -----------------------------------------------*/
  147. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  148. static void LPTIM_ResetCallback(LPTIM_HandleTypeDef *lptim);
  149. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  150. /* Exported functions --------------------------------------------------------*/
  151. /** @defgroup LPTIM_Exported_Functions LPTIM Exported Functions
  152. * @{
  153. */
  154. /** @defgroup LPTIM_Exported_Functions_Group1 Initialization/de-initialization functions
  155. * @brief Initialization and Configuration functions.
  156. *
  157. @verbatim
  158. ==============================================================================
  159. ##### Initialization and de-initialization functions #####
  160. ==============================================================================
  161. [..] This section provides functions allowing to:
  162. (+) Initialize the LPTIM according to the specified parameters in the
  163. LPTIM_InitTypeDef and initialize the associated handle.
  164. (+) DeInitialize the LPTIM peripheral.
  165. (+) Initialize the LPTIM MSP.
  166. (+) DeInitialize the LPTIM MSP.
  167. @endverbatim
  168. * @{
  169. */
  170. /**
  171. * @brief Initialize the LPTIM according to the specified parameters in the
  172. * LPTIM_InitTypeDef and initialize the associated handle.
  173. * @param hlptim LPTIM handle
  174. * @retval HAL status
  175. */
  176. HAL_StatusTypeDef HAL_LPTIM_Init(LPTIM_HandleTypeDef *hlptim)
  177. {
  178. uint32_t tmpcfgr;
  179. /* Check the LPTIM handle allocation */
  180. if(hlptim == NULL)
  181. {
  182. return HAL_ERROR;
  183. }
  184. /* Check the parameters */
  185. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  186. assert_param(IS_LPTIM_CLOCK_PRESCALER(hlptim->Init.Prescaler));
  187. if(hlptim->State == HAL_LPTIM_STATE_RESET)
  188. {
  189. /* Allocate lock resource and initialize it */
  190. hlptim->Lock = HAL_UNLOCKED;
  191. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  192. /* Reset interrupt callbacks to legacy weak callbacks */
  193. LPTIM_ResetCallback(hlptim);
  194. if(hlptim->MspInitCallback == NULL)
  195. {
  196. hlptim->MspInitCallback = HAL_LPTIM_MspInit;
  197. }
  198. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  199. hlptim->MspInitCallback(hlptim);
  200. #else
  201. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  202. HAL_LPTIM_MspInit(hlptim);
  203. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  204. }
  205. /* Change the LPTIM state */
  206. hlptim->State = HAL_LPTIM_STATE_BUSY;
  207. /* Get the LPTIMx CFGR value */
  208. tmpcfgr = hlptim->Instance->CFGR;
  209. /* Clear PRESC, PRELOAD */
  210. tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_PRELOAD | LPTIM_CFGR_PRESC));
  211. /* Set initialization parameters */
  212. tmpcfgr |= (hlptim->Init.Prescaler|hlptim->Init.UpdateMode);
  213. /* Write to LPTIMx CFGR */
  214. hlptim->Instance->CFGR = tmpcfgr;
  215. /* Change the LPTIM state */
  216. hlptim->State = HAL_LPTIM_STATE_READY;
  217. /* Return function status */
  218. return HAL_OK;
  219. }
  220. /**
  221. * @brief DeInitialize the LPTIM peripheral.
  222. * @param hlptim LPTIM handle
  223. * @retval HAL status
  224. */
  225. HAL_StatusTypeDef HAL_LPTIM_DeInit(LPTIM_HandleTypeDef *hlptim)
  226. {
  227. /* Check the LPTIM handle allocation */
  228. if(hlptim == NULL)
  229. {
  230. return HAL_ERROR;
  231. }
  232. /* Change the LPTIM state */
  233. hlptim->State = HAL_LPTIM_STATE_BUSY;
  234. /* Disable the LPTIM Peripheral Clock */
  235. __HAL_LPTIM_DISABLE(hlptim);
  236. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  237. if(hlptim->MspDeInitCallback == NULL)
  238. {
  239. hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit;
  240. }
  241. /* DeInit the low level hardware: CLOCK, NVIC.*/
  242. hlptim->MspDeInitCallback(hlptim);
  243. #else
  244. /* DeInit the low level hardware: CLOCK, NVIC.*/
  245. HAL_LPTIM_MspDeInit(hlptim);
  246. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  247. /* Change the LPTIM state */
  248. hlptim->State = HAL_LPTIM_STATE_RESET;
  249. /* Release Lock */
  250. __HAL_UNLOCK(hlptim);
  251. /* Return function status */
  252. return HAL_OK;
  253. }
  254. /**
  255. * @brief Initialize the LPTIM MSP.
  256. * @param hlptim LPTIM handle
  257. * @retval None
  258. */
  259. __weak void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef *hlptim)
  260. {
  261. /* Prevent unused argument(s) compilation warning */
  262. UNUSED(hlptim);
  263. /* NOTE : This function should not be modified, when the callback is needed,
  264. the HAL_LPTIM_MspInit could be implemented in the user file
  265. */
  266. }
  267. /**
  268. * @brief DeInitialize LPTIM MSP.
  269. * @param hlptim LPTIM handle
  270. * @retval None
  271. */
  272. __weak void HAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef *hlptim)
  273. {
  274. /* Prevent unused argument(s) compilation warning */
  275. UNUSED(hlptim);
  276. /* NOTE : This function should not be modified, when the callback is needed,
  277. the HAL_LPTIM_MspDeInit could be implemented in the user file
  278. */
  279. }
  280. /**
  281. * @}
  282. */
  283. /** @defgroup LPTIM_Exported_Functions_Group2 LPTIM Start-Stop operation functions
  284. * @brief Start-Stop operation functions.
  285. *
  286. @verbatim
  287. ==============================================================================
  288. ##### LPTIM Start Stop operation functions #####
  289. ==============================================================================
  290. [..] This section provides functions allowing to:
  291. (+) Start the Set once mode.
  292. (+) Stop the Set once mode.
  293. (+) Start the Set continues mode.
  294. (+) Stop the Set continues mode.
  295. @endverbatim
  296. * @{
  297. */
  298. /**
  299. * @brief Start the LPTIM in Set once mode.
  300. * @param hlptim LPTIM handle
  301. * @param Period Specifies the Autoreload value.
  302. * This parameter must be a value between 0x0000 and 0xFFFF.
  303. * @retval HAL status
  304. */
  305. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  306. {
  307. /* Check the parameters */
  308. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  309. assert_param(IS_LPTIM_PERIOD(Period));
  310. /* Set the LPTIM state */
  311. hlptim->State= HAL_LPTIM_STATE_BUSY;
  312. /* Enable the Peripheral */
  313. __HAL_LPTIM_ENABLE(hlptim);
  314. /* Load the period value in the autoreload register */
  315. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  316. /* Start timer in single mode */
  317. __HAL_LPTIM_START_SINGLE(hlptim);
  318. /* Change the TIM state*/
  319. hlptim->State= HAL_LPTIM_STATE_READY;
  320. /* Return function status */
  321. return HAL_OK;
  322. }
  323. /**
  324. * @brief Stop the LPTIM Set once mode.
  325. * @param hlptim LPTIM handle
  326. * @retval HAL status
  327. */
  328. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop(LPTIM_HandleTypeDef *hlptim)
  329. {
  330. /* Check the parameters */
  331. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  332. /* Set the LPTIM state */
  333. hlptim->State= HAL_LPTIM_STATE_BUSY;
  334. /* Disable the Peripheral */
  335. __HAL_LPTIM_DISABLE(hlptim);
  336. /* Change the TIM state*/
  337. hlptim->State= HAL_LPTIM_STATE_READY;
  338. /* Return function status */
  339. return HAL_OK;
  340. }
  341. /**
  342. * @brief Start the LPTIM Set once mode in interrupt mode.
  343. * @param hlptim LPTIM handle
  344. * @param Period Specifies the Autoreload value.
  345. * This parameter must be a value between 0x0000 and 0xFFFF.
  346. * @retval HAL status
  347. */
  348. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  349. {
  350. /* Check the parameters */
  351. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  352. assert_param(IS_LPTIM_PERIOD(Period));
  353. /* Set the LPTIM state */
  354. hlptim->State= HAL_LPTIM_STATE_BUSY;
  355. /* Enable Autoreload match interrupt */
  356. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  357. /* Enable the Peripheral */
  358. __HAL_LPTIM_ENABLE(hlptim);
  359. /* Load the period value in the autoreload register */
  360. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  361. /* Start timer in single mode */
  362. __HAL_LPTIM_START_SINGLE(hlptim);
  363. /* Change the TIM state*/
  364. hlptim->State= HAL_LPTIM_STATE_READY;
  365. /* Return function status */
  366. return HAL_OK;
  367. }
  368. /**
  369. * @brief Stop the LPTIM Set once mode in interrupt mode.
  370. * @param hlptim LPTIM handle
  371. * @retval HAL status
  372. */
  373. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  374. {
  375. /* Check the parameters */
  376. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  377. /* Set the LPTIM state */
  378. hlptim->State= HAL_LPTIM_STATE_BUSY;
  379. /* Disable the Peripheral */
  380. __HAL_LPTIM_DISABLE(hlptim);
  381. /* Disable Autoreload match interrupt */
  382. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  383. /* Change the TIM state*/
  384. hlptim->State= HAL_LPTIM_STATE_READY;
  385. /* Return function status */
  386. return HAL_OK;
  387. }
  388. #if defined(LPTIM_CR_CNTSTRT)
  389. /**
  390. * @brief Start the LPTIM in Set continue mode.
  391. * @param hlptim LPTIM handle
  392. * @param Period Specifies the Autoreload value.
  393. * This parameter must be a value between 0x0000 and 0xFFFF.
  394. * @retval HAL status
  395. */
  396. HAL_StatusTypeDef HAL_LPTIM_SetContinue_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  397. {
  398. /* Check the parameters */
  399. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  400. assert_param(IS_LPTIM_PERIOD(Period));
  401. /* Set the LPTIM state */
  402. hlptim->State= HAL_LPTIM_STATE_BUSY;
  403. /* Enable the Peripheral */
  404. __HAL_LPTIM_ENABLE(hlptim);
  405. /* Load the period value in the autoreload register */
  406. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  407. /* Start timer in continue mode */
  408. __HAL_LPTIM_START_CONTINUE(hlptim);
  409. /* Change the TIM state*/
  410. hlptim->State= HAL_LPTIM_STATE_READY;
  411. /* Return function status */
  412. return HAL_OK;
  413. }
  414. /**
  415. * @brief Stop the LPTIM Set continue mode.
  416. * @param hlptim LPTIM handle
  417. * @retval HAL status
  418. */
  419. HAL_StatusTypeDef HAL_LPTIM_SetContinue_Stop(LPTIM_HandleTypeDef *hlptim)
  420. {
  421. /* Check the parameters */
  422. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  423. /* Set the LPTIM state */
  424. hlptim->State= HAL_LPTIM_STATE_BUSY;
  425. /* Disable the Peripheral */
  426. __HAL_LPTIM_DISABLE(hlptim);
  427. /* Change the TIM state*/
  428. hlptim->State= HAL_LPTIM_STATE_READY;
  429. /* Return function status */
  430. return HAL_OK;
  431. }
  432. /**
  433. * @brief Start the LPTIM Set continue mode in interrupt mode.
  434. * @param hlptim LPTIM handle
  435. * @param Period Specifies the Autoreload value.
  436. * This parameter must be a value between 0x0000 and 0xFFFF.
  437. * @retval HAL status
  438. */
  439. HAL_StatusTypeDef HAL_LPTIM_SetContinue_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  440. {
  441. /* Check the parameters */
  442. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  443. assert_param(IS_LPTIM_PERIOD(Period));
  444. /* Set the LPTIM state */
  445. hlptim->State= HAL_LPTIM_STATE_BUSY;
  446. /* Enable Autoreload match interrupt */
  447. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  448. /* Enable the Peripheral */
  449. __HAL_LPTIM_ENABLE(hlptim);
  450. /* Load the period value in the autoreload register */
  451. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  452. /* Start timer in continue mode */
  453. __HAL_LPTIM_START_CONTINUE(hlptim);
  454. /* Change the TIM state*/
  455. hlptim->State= HAL_LPTIM_STATE_READY;
  456. /* Return function status */
  457. return HAL_OK;
  458. }
  459. /**
  460. * @brief Stop the LPTIM Set continue mode in interrupt mode.
  461. * @param hlptim LPTIM handle
  462. * @retval HAL status
  463. */
  464. HAL_StatusTypeDef HAL_LPTIM_SetContinue_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  465. {
  466. /* Check the parameters */
  467. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  468. /* Set the LPTIM state */
  469. hlptim->State= HAL_LPTIM_STATE_BUSY;
  470. /* Disable the Peripheral */
  471. __HAL_LPTIM_DISABLE(hlptim);
  472. /* Disable Autoreload match interrupt */
  473. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  474. /* Change the TIM state*/
  475. hlptim->State= HAL_LPTIM_STATE_READY;
  476. /* Return function status */
  477. return HAL_OK;
  478. }
  479. #endif
  480. /**
  481. * @}
  482. */
  483. /** @defgroup LPTIM_Exported_Functions_Group3 LPTIM Read operation functions
  484. * @brief Read operation functions.
  485. *
  486. @verbatim
  487. ==============================================================================
  488. ##### LPTIM Read operation functions #####
  489. ==============================================================================
  490. [..] This section provides LPTIM Reading functions.
  491. (+) Read the counter value.
  492. (+) Read the period (Auto-reload) value.
  493. (+) Read the pulse (Compare)value.
  494. @endverbatim
  495. * @{
  496. */
  497. /**
  498. * @brief Return the current counter value.
  499. * @param hlptim LPTIM handle
  500. * @retval Counter value.
  501. */
  502. uint32_t HAL_LPTIM_ReadCounter(LPTIM_HandleTypeDef *hlptim)
  503. {
  504. /* Check the parameters */
  505. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  506. return (hlptim->Instance->CNT);
  507. }
  508. /**
  509. * @brief Return the current Autoreload (Period) value.
  510. * @param hlptim LPTIM handle
  511. * @retval Autoreload value.
  512. */
  513. uint32_t HAL_LPTIM_ReadAutoReload(LPTIM_HandleTypeDef *hlptim)
  514. {
  515. /* Check the parameters */
  516. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  517. return (hlptim->Instance->ARR);
  518. }
  519. /**
  520. * @brief Counter synchronous reset.
  521. * @param hlptim pointer to a LPTIM_HandleTypeDef structure that contains
  522. * the configuration information for LPTIM module.
  523. * @retval None
  524. */
  525. uint32_t HAL_LPTIM_ResetCounter(LPTIM_HandleTypeDef *hlptim)
  526. {
  527. /* Check the parameters */
  528. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  529. if(READ_BIT(hlptim->Instance->CR, LPTIM_CR_COUNTRST) == 0)
  530. {
  531. SET_BIT(hlptim->Instance->CR, LPTIM_CR_COUNTRST);
  532. }
  533. else
  534. {
  535. return HAL_ERROR;
  536. }
  537. return HAL_OK;
  538. }
  539. /**
  540. * @}
  541. */
  542. /** @defgroup LPTIM_Exported_Functions_Group4 LPTIM IRQ handler and callbacks
  543. * @brief LPTIM IRQ handler.
  544. *
  545. @verbatim
  546. ==============================================================================
  547. ##### LPTIM IRQ handler and callbacks #####
  548. ==============================================================================
  549. [..] This section provides LPTIM IRQ handler and callback functions called within
  550. the IRQ handler:
  551. (+) LPTIM interrupt request handler
  552. (+) Compare match Callback
  553. (+) Auto-reload match Callback
  554. (+) External trigger event detection Callback
  555. (+) Compare register write complete Callback
  556. (+) Auto-reload register write complete Callback
  557. (+) Up-counting direction change Callback
  558. (+) Down-counting direction change Callback
  559. @endverbatim
  560. * @{
  561. */
  562. /**
  563. * @brief Handle LPTIM interrupt request.
  564. * @param hlptim LPTIM handle
  565. * @retval None
  566. */
  567. void HAL_LPTIM_IRQHandler(LPTIM_HandleTypeDef *hlptim)
  568. {
  569. /* Autoreload match interrupt */
  570. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARRM) != RESET)
  571. {
  572. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARRM) != RESET)
  573. {
  574. /* Clear Autoreload match flag */
  575. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARRM);
  576. /* Autoreload match Callback */
  577. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  578. hlptim->AutoReloadMatchCallback(hlptim);
  579. #else
  580. HAL_LPTIM_AutoReloadMatchCallback(hlptim);
  581. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  582. }
  583. }
  584. /* Autoreload Update completed interrupt */
  585. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARROK) != RESET)
  586. {
  587. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARROK) != RESET)
  588. {
  589. /* Clear Autoreload Update completed flag */
  590. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  591. /* Autoreload Update completed Callback */
  592. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  593. hlptim->HAL_LPTIM_AutoReloadUpdateCompletedCallback(hlptim);
  594. #else
  595. HAL_LPTIM_AutoReloadUpdateCompletedCallback(hlptim);
  596. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  597. }
  598. }
  599. }
  600. /**
  601. * @brief Autoreload match callback in non-blocking mode.
  602. * @param hlptim LPTIM handle
  603. * @retval None
  604. */
  605. __weak void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
  606. {
  607. /* Prevent unused argument(s) compilation warning */
  608. UNUSED(hlptim);
  609. /* NOTE : This function should not be modified, when the callback is needed,
  610. the HAL_LPTIM_AutoReloadMatchCallback could be implemented in the user file
  611. */
  612. }
  613. /**
  614. * @brief Autoreload Update completed callback in non-blocking mode.
  615. * @param hlptim LPTIM handle
  616. * @retval None
  617. */
  618. __weak void HAL_LPTIM_AutoReloadUpdateCompletedCallback(LPTIM_HandleTypeDef *hlptim)
  619. {
  620. /* Prevent unused argument(s) compilation warning */
  621. UNUSED(hlptim);
  622. /* NOTE : This function should not be modified, when the callback is needed,
  623. the HAL_LPTIM_AutoReloadUpdateCompletedCallback could be implemented in the user file
  624. */
  625. }
  626. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  627. /**
  628. * @brief Register a User LPTIM callback to be used instead of the weak predefined callback
  629. * @param hlptim LPTIM handle
  630. * @param CallbackID ID of the callback to be registered
  631. * This parameter can be one of the following values:
  632. * @arg @ref HAL_LPTIM_MSPINIT_CB_ID LPTIM Base Msp Init Callback ID
  633. * @arg @ref HAL_LPTIM_MSPDEINIT_CB_ID LPTIM Base Msp DeInit Callback ID
  634. * @arg @ref HAL_LPTIM_AUTORELOAD_MATCH_CB_ID Auto-reload match Callback ID
  635. * @param pCallback pointer to the callback function
  636. * @retval status
  637. */
  638. HAL_StatusTypeDef HAL_LPTIM_RegisterCallback(LPTIM_HandleTypeDef * hlptim,
  639. HAL_LPTIM_CallbackIDTypeDef CallbackID,
  640. pLPTIM_CallbackTypeDef pCallback)
  641. {
  642. HAL_StatusTypeDef status = HAL_OK;
  643. if(pCallback == NULL)
  644. {
  645. return HAL_ERROR;
  646. }
  647. /* Process locked */
  648. __HAL_LOCK(hlptim);
  649. if(hlptim->State == HAL_LPTIM_STATE_READY)
  650. {
  651. switch (CallbackID)
  652. {
  653. case HAL_LPTIM_MSPINIT_CB_ID :
  654. hlptim->MspInitCallback = pCallback;
  655. break;
  656. case HAL_LPTIM_MSPDEINIT_CB_ID :
  657. hlptim->MspDeInitCallback = pCallback;
  658. break;
  659. case HAL_LPTIM_AUTORELOAD_MATCH_CB_ID :
  660. hlptim->AutoReloadMatchCallback = pCallback;
  661. break;
  662. default :
  663. /* Return error status */
  664. status = HAL_ERROR;
  665. break;
  666. }
  667. }
  668. else if(hlptim->State == HAL_LPTIM_STATE_RESET)
  669. {
  670. switch (CallbackID)
  671. {
  672. case HAL_LPTIM_MSPINIT_CB_ID :
  673. hlptim->MspInitCallback = pCallback;
  674. break;
  675. case HAL_LPTIM_MSPDEINIT_CB_ID :
  676. hlptim->MspDeInitCallback = pCallback;
  677. break;
  678. default :
  679. /* Return error status */
  680. status = HAL_ERROR;
  681. break;
  682. }
  683. }
  684. else
  685. {
  686. /* Return error status */
  687. status = HAL_ERROR;
  688. }
  689. /* Release Lock */
  690. __HAL_UNLOCK(hlptim);
  691. return status;
  692. }
  693. /**
  694. * @brief Unregister a LPTIM callback
  695. * LLPTIM callback is redirected to the weak predefined callback
  696. * @param hlptim LPTIM handle
  697. * @param CallbackID ID of the callback to be unregistered
  698. * This parameter can be one of the following values:
  699. * @arg @ref HAL_LPTIM_MSPINIT_CB_ID LPTIM Base Msp Init Callback ID
  700. * @arg @ref HAL_LPTIM_MSPDEINIT_CB_ID LPTIM Base Msp DeInit Callback ID
  701. * @arg @ref HAL_LPTIM_AUTORELOAD_MATCH_CB_ID Auto-reload match Callback ID
  702. * @retval status
  703. */
  704. HAL_StatusTypeDef HAL_LPTIM_UnRegisterCallback(LPTIM_HandleTypeDef * hlptim,
  705. HAL_LPTIM_CallbackIDTypeDef CallbackID)
  706. {
  707. HAL_StatusTypeDef status = HAL_OK;
  708. /* Process locked */
  709. __HAL_LOCK(hlptim);
  710. if(hlptim->State == HAL_LPTIM_STATE_READY)
  711. {
  712. switch (CallbackID)
  713. {
  714. case HAL_LPTIM_MSPINIT_CB_ID :
  715. hlptim->MspInitCallback = HAL_LPTIM_MspInit; /* Legacy weak MspInit Callback */
  716. break;
  717. case HAL_LPTIM_MSPDEINIT_CB_ID :
  718. hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit; /* Legacy weak Msp DeInit Callback */
  719. break;
  720. case HAL_LPTIM_AUTORELOAD_MATCH_CB_ID :
  721. hlptim->AutoReloadMatchCallback = HAL_LPTIM_AutoReloadMatchCallback; /* Legacy weak IC Msp DeInit Callback */
  722. break;
  723. default :
  724. /* Return error status */
  725. status = HAL_ERROR;
  726. break;
  727. }
  728. }
  729. else if(hlptim->State == HAL_LPTIM_STATE_RESET)
  730. {
  731. switch (CallbackID)
  732. {
  733. case HAL_LPTIM_MSPINIT_CB_ID :
  734. hlptim->MspInitCallback = HAL_LPTIM_MspInit; /* Legacy weak MspInit Callback */
  735. break;
  736. case HAL_LPTIM_MSPDEINIT_CB_ID :
  737. hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit; /* Legacy weak Msp DeInit Callback */
  738. break;
  739. default :
  740. /* Return error status */
  741. status = HAL_ERROR;
  742. break;
  743. }
  744. }
  745. else
  746. {
  747. /* Return error status */
  748. status = HAL_ERROR;
  749. }
  750. /* Release Lock */
  751. __HAL_UNLOCK(hlptim);
  752. return status;
  753. }
  754. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  755. /**
  756. * @}
  757. */
  758. /** @defgroup LPTIM_Group5 Peripheral State functions
  759. * @brief Peripheral State functions.
  760. *
  761. @verbatim
  762. ==============================================================================
  763. ##### Peripheral State functions #####
  764. ==============================================================================
  765. [..]
  766. This subsection permits to get in run-time the status of the peripheral.
  767. @endverbatim
  768. * @{
  769. */
  770. /**
  771. * @brief Return the LPTIM handle state.
  772. * @param hlptim LPTIM handle
  773. * @retval HAL state
  774. */
  775. HAL_LPTIM_StateTypeDef HAL_LPTIM_GetState(LPTIM_HandleTypeDef *hlptim)
  776. {
  777. /* Return LPTIM handle state */
  778. return hlptim->State;
  779. }
  780. /**
  781. * @}
  782. */
  783. /**
  784. * @}
  785. */
  786. /* Private functions ---------------------------------------------------------*/
  787. /** @defgroup LPTIM_Private_Functions LPTIM Private Functions
  788. * @{
  789. */
  790. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  791. /**
  792. * @brief Reset interrupt callbacks to the legacy weak callbacks.
  793. * @param lptim pointer to a LPTIM_HandleTypeDef structure that contains
  794. * the configuration information for LPTIM module.
  795. * @retval None
  796. */
  797. static void LPTIM_ResetCallback(LPTIM_HandleTypeDef *lptim)
  798. {
  799. /* Reset the LPTIM callback to the legacy weak callbacks */
  800. lptim->AutoReloadMatchCallback = HAL_LPTIM_AutoReloadMatchCallback; /* Auto-reload match Callback */
  801. }
  802. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  803. /**
  804. * @}
  805. */
  806. #endif /* HAL_LPTIM_MODULE_ENABLED */
  807. /**
  808. * @}
  809. */
  810. /**
  811. * @}
  812. */
  813. /************************ (C) COPYRIGHT Puya *****END OF FILE****/