py32f0xx_hal_lptim.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_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 PY32F0xx_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. @endverbatim
  294. * @{
  295. */
  296. /**
  297. * @brief Start the LPTIM in Set once mode.
  298. * @param hlptim LPTIM handle
  299. * @param Period Specifies the Autoreload value.
  300. * This parameter must be a value between 0x0000 and 0xFFFF.
  301. * @retval HAL status
  302. */
  303. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  304. {
  305. /* Check the parameters */
  306. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  307. assert_param(IS_LPTIM_PERIOD(Period));
  308. /* Set the LPTIM state */
  309. hlptim->State= HAL_LPTIM_STATE_BUSY;
  310. /* Enable the Peripheral */
  311. __HAL_LPTIM_ENABLE(hlptim);
  312. /* Load the period value in the autoreload register */
  313. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  314. /* Start timer in single mode */
  315. __HAL_LPTIM_START_SINGLE(hlptim);
  316. /* Change the TIM state*/
  317. hlptim->State= HAL_LPTIM_STATE_READY;
  318. /* Return function status */
  319. return HAL_OK;
  320. }
  321. /**
  322. * @brief Stop the LPTIM Set once mode.
  323. * @param hlptim LPTIM handle
  324. * @retval HAL status
  325. */
  326. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop(LPTIM_HandleTypeDef *hlptim)
  327. {
  328. /* Check the parameters */
  329. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  330. /* Set the LPTIM state */
  331. hlptim->State= HAL_LPTIM_STATE_BUSY;
  332. /* Disable the Peripheral */
  333. __HAL_LPTIM_DISABLE(hlptim);
  334. /* Change the TIM state*/
  335. hlptim->State= HAL_LPTIM_STATE_READY;
  336. /* Return function status */
  337. return HAL_OK;
  338. }
  339. /**
  340. * @brief Start the LPTIM Set once mode in interrupt mode.
  341. * @param hlptim LPTIM handle
  342. * @param Period Specifies the Autoreload value.
  343. * This parameter must be a value between 0x0000 and 0xFFFF.
  344. * @retval HAL status
  345. */
  346. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  347. {
  348. /* Check the parameters */
  349. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  350. assert_param(IS_LPTIM_PERIOD(Period));
  351. /* Set the LPTIM state */
  352. hlptim->State= HAL_LPTIM_STATE_BUSY;
  353. /* Enable Autoreload match interrupt */
  354. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  355. /* Enable the Peripheral */
  356. __HAL_LPTIM_ENABLE(hlptim);
  357. /* Load the period value in the autoreload register */
  358. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  359. /* Start timer in single mode */
  360. __HAL_LPTIM_START_SINGLE(hlptim);
  361. /* Change the TIM state*/
  362. hlptim->State= HAL_LPTIM_STATE_READY;
  363. /* Return function status */
  364. return HAL_OK;
  365. }
  366. /**
  367. * @brief Stop the LPTIM Set once mode in interrupt mode.
  368. * @param hlptim LPTIM handle
  369. * @retval HAL status
  370. */
  371. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  372. {
  373. /* Check the parameters */
  374. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  375. /* Set the LPTIM state */
  376. hlptim->State= HAL_LPTIM_STATE_BUSY;
  377. /* Disable the Peripheral */
  378. __HAL_LPTIM_DISABLE(hlptim);
  379. /* Disable Autoreload match interrupt */
  380. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  381. /* Change the TIM state*/
  382. hlptim->State= HAL_LPTIM_STATE_READY;
  383. /* Return function status */
  384. return HAL_OK;
  385. }
  386. /**
  387. * @}
  388. */
  389. /** @defgroup LPTIM_Exported_Functions_Group3 LPTIM Read operation functions
  390. * @brief Read operation functions.
  391. *
  392. @verbatim
  393. ==============================================================================
  394. ##### LPTIM Read operation functions #####
  395. ==============================================================================
  396. [..] This section provides LPTIM Reading functions.
  397. (+) Read the counter value.
  398. (+) Read the period (Auto-reload) value.
  399. (+) Read the pulse (Compare)value.
  400. @endverbatim
  401. * @{
  402. */
  403. /**
  404. * @brief Return the current counter value.
  405. * @param hlptim LPTIM handle
  406. * @retval Counter value.
  407. */
  408. uint32_t HAL_LPTIM_ReadCounter(LPTIM_HandleTypeDef *hlptim)
  409. {
  410. /* Check the parameters */
  411. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  412. return (hlptim->Instance->CNT);
  413. }
  414. /**
  415. * @brief Return the current Autoreload (Period) value.
  416. * @param hlptim LPTIM handle
  417. * @retval Autoreload value.
  418. */
  419. uint32_t HAL_LPTIM_ReadAutoReload(LPTIM_HandleTypeDef *hlptim)
  420. {
  421. /* Check the parameters */
  422. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  423. return (hlptim->Instance->ARR);
  424. }
  425. /**
  426. * @}
  427. */
  428. /** @defgroup LPTIM_Exported_Functions_Group4 LPTIM IRQ handler and callbacks
  429. * @brief LPTIM IRQ handler.
  430. *
  431. @verbatim
  432. ==============================================================================
  433. ##### LPTIM IRQ handler and callbacks #####
  434. ==============================================================================
  435. [..] This section provides LPTIM IRQ handler and callback functions called within
  436. the IRQ handler:
  437. (+) LPTIM interrupt request handler
  438. (+) Compare match Callback
  439. (+) Auto-reload match Callback
  440. (+) External trigger event detection Callback
  441. (+) Compare register write complete Callback
  442. (+) Auto-reload register write complete Callback
  443. (+) Up-counting direction change Callback
  444. (+) Down-counting direction change Callback
  445. @endverbatim
  446. * @{
  447. */
  448. /**
  449. * @brief Handle LPTIM interrupt request.
  450. * @param hlptim LPTIM handle
  451. * @retval None
  452. */
  453. void HAL_LPTIM_IRQHandler(LPTIM_HandleTypeDef *hlptim)
  454. {
  455. /* Autoreload match interrupt */
  456. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARRM) != RESET)
  457. {
  458. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARRM) != RESET)
  459. {
  460. /* Clear Autoreload match flag */
  461. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARRM);
  462. /* Autoreload match Callback */
  463. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  464. hlptim->AutoReloadMatchCallback(hlptim);
  465. #else
  466. HAL_LPTIM_AutoReloadMatchCallback(hlptim);
  467. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  468. }
  469. }
  470. }
  471. /**
  472. * @brief Autoreload match callback in non-blocking mode.
  473. * @param hlptim LPTIM handle
  474. * @retval None
  475. */
  476. __weak void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
  477. {
  478. /* Prevent unused argument(s) compilation warning */
  479. UNUSED(hlptim);
  480. /* NOTE : This function should not be modified, when the callback is needed,
  481. the HAL_LPTIM_AutoReloadMatchCallback could be implemented in the user file
  482. */
  483. }
  484. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  485. /**
  486. * @brief Register a User LPTIM callback to be used instead of the weak predefined callback
  487. * @param hlptim LPTIM handle
  488. * @param CallbackID ID of the callback to be registered
  489. * This parameter can be one of the following values:
  490. * @arg @ref HAL_LPTIM_MSPINIT_CB_ID LPTIM Base Msp Init Callback ID
  491. * @arg @ref HAL_LPTIM_MSPDEINIT_CB_ID LPTIM Base Msp DeInit Callback ID
  492. * @arg @ref HAL_LPTIM_AUTORELOAD_MATCH_CB_ID Auto-reload match Callback ID
  493. * @param pCallback pointer to the callback function
  494. * @retval status
  495. */
  496. HAL_StatusTypeDef HAL_LPTIM_RegisterCallback(LPTIM_HandleTypeDef * hlptim,
  497. HAL_LPTIM_CallbackIDTypeDef CallbackID,
  498. pLPTIM_CallbackTypeDef pCallback)
  499. {
  500. HAL_StatusTypeDef status = HAL_OK;
  501. if(pCallback == NULL)
  502. {
  503. return HAL_ERROR;
  504. }
  505. /* Process locked */
  506. __HAL_LOCK(hlptim);
  507. if(hlptim->State == HAL_LPTIM_STATE_READY)
  508. {
  509. switch (CallbackID)
  510. {
  511. case HAL_LPTIM_MSPINIT_CB_ID :
  512. hlptim->MspInitCallback = pCallback;
  513. break;
  514. case HAL_LPTIM_MSPDEINIT_CB_ID :
  515. hlptim->MspDeInitCallback = pCallback;
  516. break;
  517. case HAL_LPTIM_AUTORELOAD_MATCH_CB_ID :
  518. hlptim->AutoReloadMatchCallback = pCallback;
  519. break;
  520. default :
  521. /* Return error status */
  522. status = HAL_ERROR;
  523. break;
  524. }
  525. }
  526. else if(hlptim->State == HAL_LPTIM_STATE_RESET)
  527. {
  528. switch (CallbackID)
  529. {
  530. case HAL_LPTIM_MSPINIT_CB_ID :
  531. hlptim->MspInitCallback = pCallback;
  532. break;
  533. case HAL_LPTIM_MSPDEINIT_CB_ID :
  534. hlptim->MspDeInitCallback = pCallback;
  535. break;
  536. default :
  537. /* Return error status */
  538. status = HAL_ERROR;
  539. break;
  540. }
  541. }
  542. else
  543. {
  544. /* Return error status */
  545. status = HAL_ERROR;
  546. }
  547. /* Release Lock */
  548. __HAL_UNLOCK(hlptim);
  549. return status;
  550. }
  551. /**
  552. * @brief Unregister a LPTIM callback
  553. * LLPTIM callback is redirected to the weak predefined callback
  554. * @param hlptim LPTIM handle
  555. * @param CallbackID ID of the callback to be unregistered
  556. * This parameter can be one of the following values:
  557. * @arg @ref HAL_LPTIM_MSPINIT_CB_ID LPTIM Base Msp Init Callback ID
  558. * @arg @ref HAL_LPTIM_MSPDEINIT_CB_ID LPTIM Base Msp DeInit Callback ID
  559. * @arg @ref HAL_LPTIM_AUTORELOAD_MATCH_CB_ID Auto-reload match Callback ID
  560. * @retval status
  561. */
  562. HAL_StatusTypeDef HAL_LPTIM_UnRegisterCallback(LPTIM_HandleTypeDef * hlptim,
  563. HAL_LPTIM_CallbackIDTypeDef CallbackID)
  564. {
  565. HAL_StatusTypeDef status = HAL_OK;
  566. /* Process locked */
  567. __HAL_LOCK(hlptim);
  568. if(hlptim->State == HAL_LPTIM_STATE_READY)
  569. {
  570. switch (CallbackID)
  571. {
  572. case HAL_LPTIM_MSPINIT_CB_ID :
  573. hlptim->MspInitCallback = HAL_LPTIM_MspInit; /* Legacy weak MspInit Callback */
  574. break;
  575. case HAL_LPTIM_MSPDEINIT_CB_ID :
  576. hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit; /* Legacy weak Msp DeInit Callback */
  577. break;
  578. case HAL_LPTIM_AUTORELOAD_MATCH_CB_ID :
  579. hlptim->AutoReloadMatchCallback = HAL_LPTIM_AutoReloadMatchCallback; /* Legacy weak IC Msp DeInit Callback */
  580. break;
  581. default :
  582. /* Return error status */
  583. status = HAL_ERROR;
  584. break;
  585. }
  586. }
  587. else if(hlptim->State == HAL_LPTIM_STATE_RESET)
  588. {
  589. switch (CallbackID)
  590. {
  591. case HAL_LPTIM_MSPINIT_CB_ID :
  592. hlptim->MspInitCallback = HAL_LPTIM_MspInit; /* Legacy weak MspInit Callback */
  593. break;
  594. case HAL_LPTIM_MSPDEINIT_CB_ID :
  595. hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit; /* Legacy weak Msp DeInit Callback */
  596. break;
  597. default :
  598. /* Return error status */
  599. status = HAL_ERROR;
  600. break;
  601. }
  602. }
  603. else
  604. {
  605. /* Return error status */
  606. status = HAL_ERROR;
  607. }
  608. /* Release Lock */
  609. __HAL_UNLOCK(hlptim);
  610. return status;
  611. }
  612. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  613. /**
  614. * @}
  615. */
  616. /** @defgroup LPTIM_Group5 Peripheral State functions
  617. * @brief Peripheral State functions.
  618. *
  619. @verbatim
  620. ==============================================================================
  621. ##### Peripheral State functions #####
  622. ==============================================================================
  623. [..]
  624. This subsection permits to get in run-time the status of the peripheral.
  625. @endverbatim
  626. * @{
  627. */
  628. /**
  629. * @brief Return the LPTIM handle state.
  630. * @param hlptim LPTIM handle
  631. * @retval HAL state
  632. */
  633. HAL_LPTIM_StateTypeDef HAL_LPTIM_GetState(LPTIM_HandleTypeDef *hlptim)
  634. {
  635. /* Return LPTIM handle state */
  636. return hlptim->State;
  637. }
  638. /**
  639. * @}
  640. */
  641. /**
  642. * @}
  643. */
  644. /* Private functions ---------------------------------------------------------*/
  645. /** @defgroup LPTIM_Private_Functions LPTIM Private Functions
  646. * @{
  647. */
  648. #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
  649. /**
  650. * @brief Reset interrupt callbacks to the legacy weak callbacks.
  651. * @param lptim pointer to a LPTIM_HandleTypeDef structure that contains
  652. * the configuration information for LPTIM module.
  653. * @retval None
  654. */
  655. static void LPTIM_ResetCallback(LPTIM_HandleTypeDef *lptim)
  656. {
  657. /* Reset the LPTIM callback to the legacy weak callbacks */
  658. lptim->AutoReloadMatchCallback = HAL_LPTIM_AutoReloadMatchCallback; /* Auto-reload match Callback */
  659. }
  660. #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
  661. /**
  662. * @}
  663. */
  664. #endif /* HAL_LPTIM_MODULE_ENABLED */
  665. /**
  666. * @}
  667. */
  668. /**
  669. * @}
  670. */
  671. /************************ (C) COPYRIGHT Puya *****END OF FILE****/