You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1866 lines
74 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_rtc.c
  4. * @author MCD Application Team
  5. * @brief RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real Time Clock (RTC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + RTC Time and Date functions
  10. * + RTC Alarm functions
  11. * + Peripheral Control functions
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ===============================================================================
  16. ##### RTC Operating Condition #####
  17. ===============================================================================
  18. [..] The real-time clock (RTC) and the RTC backup registers can be powered
  19. from the VBAT voltage when the main VDD supply is powered off.
  20. To retain the content of the RTC backup registers and supply the RTC
  21. when VDD is turned off, VBAT pin can be connected to an optional
  22. standby voltage supplied by a battery or by another source.
  23. [..] To allow the RTC operating even when the main digital supply (VDD) is turned
  24. off, the VBAT pin powers the following blocks:
  25. (#) The RTC
  26. (#) The LSE oscillator
  27. (#) PC13 to PC15 I/Os (when available)
  28. [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
  29. the following pins are available:
  30. (#) PC14 and PC15 can be used as either GPIO or LSE pins
  31. (#) PC13 can be used as a GPIO or as the RTC_AF1 pin
  32. [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
  33. because VDD is not present), the following pins are available:
  34. (#) PC14 and PC15 can be used as LSE pins only
  35. (#) PC13 can be used as the RTC_AF1 pin
  36. ##### Backup Domain Reset #####
  37. ===============================================================================
  38. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  39. to their reset values.
  40. [..] A backup domain reset is generated when one of the following events occurs:
  41. (#) Software reset, triggered by setting the BDRST bit in the
  42. RCC Backup domain control register (RCC_BDCR).
  43. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  44. ##### Backup Domain Access #####
  45. ===================================================================
  46. [..] After reset, the backup domain (RTC registers, RTC backup data
  47. registers and backup SRAM) is protected against possible unwanted write
  48. accesses.
  49. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  50. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  51. __HAL_RCC_PWR_CLK_ENABLE() function.
  52. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  53. (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
  54. (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
  55. ##### How to use RTC Driver #####
  56. ===================================================================
  57. [..]
  58. (+) Enable the RTC domain access (see description in the section above).
  59. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  60. format using the HAL_RTC_Init() function.
  61. *** Time and Date configuration ***
  62. ===================================
  63. [..]
  64. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  65. and HAL_RTC_SetDate() functions.
  66. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  67. *** Alarm configuration ***
  68. ===========================
  69. [..]
  70. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  71. You can also configure the RTC Alarm with interrupt mode using the
  72. HAL_RTC_SetAlarm_IT() function.
  73. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  74. ##### RTC and low power modes #####
  75. ==================================================================
  76. [..] The MCU can be woken up from a low power mode by an RTC alternate
  77. function.
  78. [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
  79. RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
  80. These RTC alternate functions can wake up the system from the Stop and
  81. Standby low power modes.
  82. [..] The system can also wake up from low power modes without depending
  83. on an external interrupt (Auto-wakeup mode), by using the RTC alarm
  84. or the RTC wakeup events.
  85. [..] The RTC provides a programmable time base for waking up from the
  86. Stop or Standby mode at regular intervals.
  87. Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
  88. is LSE or LSI.
  89. *** Callback registration ***
  90. =============================================
  91. [..]
  92. The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
  93. allows the user to configure dynamically the driver callbacks.
  94. Use Function @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
  95. [..]
  96. Function @ref HAL_RTC_RegisterCallback() allows to register following callbacks:
  97. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  98. (+) AlarmBEventCallback : RTC Alarm B Event callback.
  99. (+) TimeStampEventCallback : RTC TimeStamp Event callback.
  100. (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
  101. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  102. (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
  103. (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
  104. (+) MspInitCallback : RTC MspInit callback.
  105. (+) MspDeInitCallback : RTC MspDeInit callback.
  106. [..]
  107. This function takes as parameters the HAL peripheral handle, the Callback ID
  108. and a pointer to the user callback function.
  109. [..]
  110. Use function @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
  111. weak function.
  112. @ref HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  113. and the Callback ID.
  114. This function allows to reset following callbacks:
  115. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  116. (+) AlarmBEventCallback : RTC Alarm B Event callback.
  117. (+) TimeStampEventCallback : RTC TimeStamp Event callback.
  118. (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
  119. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  120. (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
  121. (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
  122. (+) MspInitCallback : RTC MspInit callback.
  123. (+) MspDeInitCallback : RTC MspDeInit callback.
  124. [..]
  125. By default, after the @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
  126. all callbacks are set to the corresponding weak functions :
  127. examples @ref AlarmAEventCallback(), @ref WakeUpTimerEventCallback().
  128. Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
  129. in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
  130. (not registered beforehand).
  131. If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit()
  132. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  133. [..]
  134. Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
  135. Exception done MspInit/MspDeInit that can be registered/unregistered
  136. in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
  137. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  138. In that case first register the MspInit/MspDeInit user callbacks
  139. using @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
  140. or @ref HAL_RTC_Init() function.
  141. [..]
  142. When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
  143. not defined, the callback registration feature is not available and all callbacks
  144. are set to the corresponding weak functions.
  145. @endverbatim
  146. ******************************************************************************
  147. * @attention
  148. *
  149. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  150. * All rights reserved.</center></h2>
  151. *
  152. * This software component is licensed by ST under BSD 3-Clause license,
  153. * the "License"; You may not use this file except in compliance with the
  154. * License. You may obtain a copy of the License at:
  155. * opensource.org/licenses/BSD-3-Clause
  156. *
  157. ******************************************************************************
  158. */
  159. /* Includes ------------------------------------------------------------------*/
  160. #include "stm32l1xx_hal.h"
  161. /** @addtogroup STM32L1xx_HAL_Driver
  162. * @{
  163. */
  164. /** @addtogroup RTC
  165. * @brief RTC HAL module driver
  166. * @{
  167. */
  168. #ifdef HAL_RTC_MODULE_ENABLED
  169. /* Private typedef -----------------------------------------------------------*/
  170. /* Private define ------------------------------------------------------------*/
  171. /* Private macro -------------------------------------------------------------*/
  172. /* Private variables ---------------------------------------------------------*/
  173. /* Private function prototypes -----------------------------------------------*/
  174. /* Exported functions --------------------------------------------------------*/
  175. /** @addtogroup RTC_Exported_Functions
  176. * @{
  177. */
  178. /** @addtogroup RTC_Exported_Functions_Group1
  179. * @brief Initialization and Configuration functions
  180. *
  181. @verbatim
  182. ===============================================================================
  183. ##### Initialization and de-initialization functions #####
  184. ===============================================================================
  185. [..] This section provides functions allowing to initialize and configure the
  186. RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
  187. RTC registers Write protection, enter and exit the RTC initialization mode,
  188. RTC registers synchronization check and reference clock detection enable.
  189. (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
  190. It is split into 2 programmable prescalers to minimize power consumption.
  191. (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
  192. (++) When both prescalers are used, it is recommended to configure the
  193. asynchronous prescaler to a high value to minimize power consumption.
  194. (#) All RTC registers are Write protected. Writing to the RTC registers
  195. is enabled by writing a key into the Write Protection register, RTC_WPR.
  196. (#) To configure the RTC Calendar, user application should enter
  197. initialization mode. In this mode, the calendar counter is stopped
  198. and its value can be updated. When the initialization sequence is
  199. complete, the calendar restarts counting after 4 RTCCLK cycles.
  200. (#) To read the calendar through the shadow registers after Calendar
  201. initialization, calendar update or after wakeup from low power modes
  202. the software must first clear the RSF flag. The software must then
  203. wait until it is set again before reading the calendar, which means
  204. that the calendar registers have been correctly copied into the
  205. RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
  206. implements the above software sequence (RSF clear and RSF check).
  207. @endverbatim
  208. * @{
  209. */
  210. /**
  211. * @brief Initialize the RTC peripheral
  212. * @param hrtc RTC handle
  213. * @retval HAL status
  214. */
  215. HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
  216. {
  217. /* Check the RTC peripheral state */
  218. if (hrtc == NULL)
  219. {
  220. return HAL_ERROR;
  221. }
  222. /* Check the parameters */
  223. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  224. assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
  225. assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
  226. assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
  227. assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
  228. assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
  229. assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
  230. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  231. if (hrtc->State == HAL_RTC_STATE_RESET)
  232. {
  233. /* Allocate lock resource and initialize it */
  234. hrtc->Lock = HAL_UNLOCKED;
  235. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  236. hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
  237. hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
  238. hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
  239. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  240. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  241. hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
  242. hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
  243. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  244. if (hrtc->MspInitCallback == NULL)
  245. {
  246. hrtc->MspInitCallback = HAL_RTC_MspInit;
  247. }
  248. /* Init the low level hardware */
  249. hrtc->MspInitCallback(hrtc);
  250. if (hrtc->MspDeInitCallback == NULL)
  251. {
  252. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  253. }
  254. }
  255. #else
  256. if (hrtc->State == HAL_RTC_STATE_RESET)
  257. {
  258. /* Allocate lock resource and initialize it */
  259. hrtc->Lock = HAL_UNLOCKED;
  260. /* Initialize RTC MSP */
  261. HAL_RTC_MspInit(hrtc);
  262. }
  263. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  264. /* Set RTC state */
  265. hrtc->State = HAL_RTC_STATE_BUSY;
  266. /* Disable the write protection for RTC registers */
  267. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  268. /* Set Initialization mode */
  269. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  270. {
  271. /* Enable the write protection for RTC registers */
  272. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  273. /* Set RTC state */
  274. hrtc->State = HAL_RTC_STATE_ERROR;
  275. return HAL_ERROR;
  276. }
  277. else
  278. {
  279. /* Clear RTC_CR FMT, OSEL and POL Bits */
  280. hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
  281. /* Set RTC_CR register */
  282. hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
  283. /* Configure the RTC PRER */
  284. hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
  285. hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16U);
  286. /* Exit Initialization mode */
  287. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  288. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
  289. hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
  290. /* Enable the write protection for RTC registers */
  291. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  292. /* Set RTC state */
  293. hrtc->State = HAL_RTC_STATE_READY;
  294. return HAL_OK;
  295. }
  296. }
  297. /**
  298. * @brief DeInitialize the RTC peripheral.
  299. * @param hrtc RTC handle
  300. * @note This function does not reset the RTC Backup Data registers.
  301. * @retval HAL status
  302. */
  303. HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  304. {
  305. uint32_t tickstart;
  306. /* Check the parameters */
  307. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  308. /* Set RTC state */
  309. hrtc->State = HAL_RTC_STATE_BUSY;
  310. /* Disable the write protection for RTC registers */
  311. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  312. /* Set Initialization mode */
  313. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  314. {
  315. /* Enable the write protection for RTC registers */
  316. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  317. /* Set RTC state */
  318. hrtc->State = HAL_RTC_STATE_ERROR;
  319. return HAL_ERROR;
  320. }
  321. else
  322. {
  323. /* Reset TR, DR and CR registers */
  324. hrtc->Instance->TR = 0x00000000U;
  325. hrtc->Instance->DR = 0x00002101U;
  326. /* Reset All CR bits except CR[2:0] */
  327. hrtc->Instance->CR &= 0x00000007U;
  328. tickstart = HAL_GetTick();
  329. /* Wait till WUTWF flag is set and if Time out is reached exit */
  330. while (((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == 0U)
  331. {
  332. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  333. {
  334. /* Enable the write protection for RTC registers */
  335. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  336. /* Set RTC state */
  337. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  338. return HAL_TIMEOUT;
  339. }
  340. }
  341. /* Reset all RTC CR register bits */
  342. hrtc->Instance->CR &= 0x00000000U;
  343. hrtc->Instance->WUTR = 0x0000FFFFU;
  344. hrtc->Instance->PRER = 0x007F00FFU;
  345. hrtc->Instance->CALIBR = 0x00000000U;
  346. hrtc->Instance->ALRMAR = 0x00000000U;
  347. hrtc->Instance->ALRMBR = 0x00000000U;
  348. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  349. hrtc->Instance->SHIFTR = 0x00000000U;
  350. hrtc->Instance->CALR = 0x00000000U;
  351. hrtc->Instance->ALRMASSR = 0x00000000U;
  352. hrtc->Instance->ALRMBSSR = 0x00000000U;
  353. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  354. /* Reset ISR register and exit initialization mode */
  355. hrtc->Instance->ISR = 0x00000000U;
  356. /* Reset Tamper and alternate functions configuration register */
  357. hrtc->Instance->TAFCR = 0x00000000U;
  358. /* Wait for synchro */
  359. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  360. {
  361. /* Enable the write protection for RTC registers */
  362. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  363. hrtc->State = HAL_RTC_STATE_ERROR;
  364. return HAL_ERROR;
  365. }
  366. }
  367. /* Enable the write protection for RTC registers */
  368. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  369. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  370. if (hrtc->MspDeInitCallback == NULL)
  371. {
  372. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  373. }
  374. /* DeInit the low level hardware: CLOCK, NVIC.*/
  375. hrtc->MspDeInitCallback(hrtc);
  376. #else
  377. /* De-Initialize RTC MSP */
  378. HAL_RTC_MspDeInit(hrtc);
  379. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  380. hrtc->State = HAL_RTC_STATE_RESET;
  381. /* Release Lock */
  382. __HAL_UNLOCK(hrtc);
  383. return HAL_OK;
  384. }
  385. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  386. /**
  387. * @brief Register a User RTC Callback
  388. * To be used instead of the weak predefined callback
  389. * @param hrtc RTC handle
  390. * @param CallbackID ID of the callback to be registered
  391. * This parameter can be one of the following values:
  392. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  393. * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
  394. * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
  395. * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
  396. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  397. * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
  398. * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
  399. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  400. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  401. * @param pCallback pointer to the Callback function
  402. * @retval HAL status
  403. */
  404. HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
  405. {
  406. HAL_StatusTypeDef status = HAL_OK;
  407. if (pCallback == NULL)
  408. {
  409. return HAL_ERROR;
  410. }
  411. /* Process locked */
  412. __HAL_LOCK(hrtc);
  413. if (HAL_RTC_STATE_READY == hrtc->State)
  414. {
  415. switch (CallbackID)
  416. {
  417. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  418. hrtc->AlarmAEventCallback = pCallback;
  419. break;
  420. case HAL_RTC_ALARM_B_EVENT_CB_ID :
  421. hrtc->AlarmBEventCallback = pCallback;
  422. break;
  423. case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
  424. hrtc->TimeStampEventCallback = pCallback;
  425. break;
  426. case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
  427. hrtc->WakeUpTimerEventCallback = pCallback;
  428. break;
  429. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  430. hrtc->Tamper1EventCallback = pCallback;
  431. break;
  432. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  433. case HAL_RTC_TAMPER2_EVENT_CB_ID :
  434. hrtc->Tamper2EventCallback = pCallback;
  435. break;
  436. case HAL_RTC_TAMPER3_EVENT_CB_ID :
  437. hrtc->Tamper3EventCallback = pCallback;
  438. break;
  439. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  440. case HAL_RTC_MSPINIT_CB_ID :
  441. hrtc->MspInitCallback = pCallback;
  442. break;
  443. case HAL_RTC_MSPDEINIT_CB_ID :
  444. hrtc->MspDeInitCallback = pCallback;
  445. break;
  446. default :
  447. /* Return error status */
  448. status = HAL_ERROR;
  449. break;
  450. }
  451. }
  452. else if (HAL_RTC_STATE_RESET == hrtc->State)
  453. {
  454. switch (CallbackID)
  455. {
  456. case HAL_RTC_MSPINIT_CB_ID :
  457. hrtc->MspInitCallback = pCallback;
  458. break;
  459. case HAL_RTC_MSPDEINIT_CB_ID :
  460. hrtc->MspDeInitCallback = pCallback;
  461. break;
  462. default :
  463. /* Return error status */
  464. status = HAL_ERROR;
  465. break;
  466. }
  467. }
  468. else
  469. {
  470. /* Return error status */
  471. status = HAL_ERROR;
  472. }
  473. /* Release Lock */
  474. __HAL_UNLOCK(hrtc);
  475. return status;
  476. }
  477. /**
  478. * @brief Unregister an RTC Callback
  479. * RTC callabck is redirected to the weak predefined callback
  480. * @param hrtc RTC handle
  481. * @param CallbackID ID of the callback to be unregistered
  482. * This parameter can be one of the following values:
  483. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  484. * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
  485. * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
  486. * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
  487. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  488. * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
  489. * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
  490. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  491. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  492. * @retval HAL status
  493. */
  494. HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
  495. {
  496. HAL_StatusTypeDef status = HAL_OK;
  497. /* Process locked */
  498. __HAL_LOCK(hrtc);
  499. if (HAL_RTC_STATE_READY == hrtc->State)
  500. {
  501. switch (CallbackID)
  502. {
  503. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  504. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  505. break;
  506. case HAL_RTC_ALARM_B_EVENT_CB_ID :
  507. hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
  508. break;
  509. case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
  510. hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
  511. break;
  512. case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
  513. hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
  514. break;
  515. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  516. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  517. break;
  518. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  519. case HAL_RTC_TAMPER2_EVENT_CB_ID :
  520. hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
  521. break;
  522. case HAL_RTC_TAMPER3_EVENT_CB_ID :
  523. hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
  524. break;
  525. #endif
  526. case HAL_RTC_MSPINIT_CB_ID :
  527. hrtc->MspInitCallback = HAL_RTC_MspInit;
  528. break;
  529. case HAL_RTC_MSPDEINIT_CB_ID :
  530. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  531. break;
  532. default :
  533. /* Return error status */
  534. status = HAL_ERROR;
  535. break;
  536. }
  537. }
  538. else if (HAL_RTC_STATE_RESET == hrtc->State)
  539. {
  540. switch (CallbackID)
  541. {
  542. case HAL_RTC_MSPINIT_CB_ID :
  543. hrtc->MspInitCallback = HAL_RTC_MspInit;
  544. break;
  545. case HAL_RTC_MSPDEINIT_CB_ID :
  546. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  547. break;
  548. default :
  549. /* Return error status */
  550. status = HAL_ERROR;
  551. break;
  552. }
  553. }
  554. else
  555. {
  556. /* Return error status */
  557. status = HAL_ERROR;
  558. }
  559. /* Release Lock */
  560. __HAL_UNLOCK(hrtc);
  561. return status;
  562. }
  563. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  564. /**
  565. * @brief Initialize the RTC MSP.
  566. * @param hrtc RTC handle
  567. * @retval None
  568. */
  569. __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
  570. {
  571. /* Prevent unused argument(s) compilation warning */
  572. UNUSED(hrtc);
  573. /* NOTE : This function Should not be modified, when the callback is needed,
  574. the HAL_RTC_MspInit could be implemented in the user file
  575. */
  576. }
  577. /**
  578. * @brief DeInitialize the RTC MSP.
  579. * @param hrtc RTC handle
  580. * @retval None
  581. */
  582. __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
  583. {
  584. /* Prevent unused argument(s) compilation warning */
  585. UNUSED(hrtc);
  586. /* NOTE : This function Should not be modified, when the callback is needed,
  587. the HAL_RTC_MspDeInit could be implemented in the user file
  588. */
  589. }
  590. /**
  591. * @}
  592. */
  593. /** @addtogroup RTC_Exported_Functions_Group2
  594. * @brief RTC Time and Date functions
  595. *
  596. @verbatim
  597. ===============================================================================
  598. ##### RTC Time and Date functions #####
  599. ===============================================================================
  600. [..] This section provides functions allowing to configure Time and Date features
  601. @endverbatim
  602. * @{
  603. */
  604. /**
  605. * @brief Set RTC current time.
  606. * @param hrtc RTC handle
  607. * @param sTime Pointer to Time structure
  608. * @param Format Specifies the format of the entered parameters.
  609. * This parameter can be one of the following values:
  610. * @arg RTC_FORMAT_BIN: Binary data format
  611. * @arg RTC_FORMAT_BCD: BCD data format
  612. * @retval HAL status
  613. */
  614. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  615. {
  616. uint32_t tmpreg;
  617. /* Check the parameters */
  618. assert_param(IS_RTC_FORMAT(Format));
  619. assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
  620. assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
  621. /* Process Locked */
  622. __HAL_LOCK(hrtc);
  623. hrtc->State = HAL_RTC_STATE_BUSY;
  624. if (Format == RTC_FORMAT_BIN)
  625. {
  626. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  627. {
  628. assert_param(IS_RTC_HOUR12(sTime->Hours));
  629. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  630. }
  631. else
  632. {
  633. sTime->TimeFormat = 0x00U;
  634. assert_param(IS_RTC_HOUR24(sTime->Hours));
  635. }
  636. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  637. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  638. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16U) | \
  639. ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8U) | \
  640. ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
  641. (((uint32_t)sTime->TimeFormat) << 16U));
  642. }
  643. else
  644. {
  645. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  646. {
  647. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
  648. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  649. }
  650. else
  651. {
  652. sTime->TimeFormat = 0x00U;
  653. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  654. }
  655. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  656. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  657. tmpreg = (((uint32_t)(sTime->Hours) << 16U) | \
  658. ((uint32_t)(sTime->Minutes) << 8U) | \
  659. ((uint32_t)sTime->Seconds) | \
  660. ((uint32_t)(sTime->TimeFormat) << 16U));
  661. }
  662. UNUSED(tmpreg);
  663. /* Disable the write protection for RTC registers */
  664. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  665. /* Set Initialization mode */
  666. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  667. {
  668. /* Enable the write protection for RTC registers */
  669. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  670. /* Set RTC state */
  671. hrtc->State = HAL_RTC_STATE_ERROR;
  672. /* Process Unlocked */
  673. __HAL_UNLOCK(hrtc);
  674. return HAL_ERROR;
  675. }
  676. else
  677. {
  678. /* Set the RTC_TR register */
  679. hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  680. /* Clear the bits to be configured */
  681. hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
  682. /* Configure the RTC_CR register */
  683. hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
  684. /* Exit Initialization mode */
  685. hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
  686. /* Wait for synchro */
  687. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  688. {
  689. /* Enable the write protection for RTC registers */
  690. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  691. hrtc->State = HAL_RTC_STATE_ERROR;
  692. /* Process Unlocked */
  693. __HAL_UNLOCK(hrtc);
  694. return HAL_ERROR;
  695. }
  696. /* Enable the write protection for RTC registers */
  697. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  698. hrtc->State = HAL_RTC_STATE_READY;
  699. __HAL_UNLOCK(hrtc);
  700. return HAL_OK;
  701. }
  702. }
  703. /**
  704. * @brief Get RTC current time.
  705. * @param hrtc RTC handle
  706. * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
  707. * with input format (BIN or BCD), also SubSeconds field (if availabale) returning the
  708. * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
  709. * factor to be used for second fraction ratio computation.
  710. * @param Format Specifies the format of the entered parameters.
  711. * This parameter can be one of the following values:
  712. * @arg RTC_FORMAT_BIN: Binary data format
  713. * @arg RTC_FORMAT_BCD: BCD data format
  714. * @note If available, you can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  715. * value in second fraction ratio with time unit following generic formula:
  716. * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  717. * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  718. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  719. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  720. * Reading RTC current time locks the values in calendar shadow registers until Current date is read
  721. * to ensure consistency between the time and date values.
  722. * @retval HAL status
  723. */
  724. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  725. {
  726. uint32_t tmpreg;
  727. /* Check the parameters */
  728. assert_param(IS_RTC_FORMAT(Format));
  729. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  730. /* Get subseconds structure field from the corresponding register*/
  731. sTime->SubSeconds = (uint32_t)((hrtc->Instance->SSR) & RTC_SSR_SS);
  732. /* Get SecondFraction structure field from the corresponding register field*/
  733. sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
  734. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  735. /* Get the TR register */
  736. tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
  737. /* Fill the structure fields with the read parameters */
  738. sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
  739. sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
  740. sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
  741. sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U);
  742. /* Check the input parameters format */
  743. if (Format == RTC_FORMAT_BIN)
  744. {
  745. /* Convert the time structure parameters to Binary format */
  746. sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
  747. sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
  748. sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
  749. }
  750. return HAL_OK;
  751. }
  752. /**
  753. * @brief Set RTC current date.
  754. * @param hrtc RTC handle
  755. * @param sDate Pointer to date structure
  756. * @param Format specifies the format of the entered parameters.
  757. * This parameter can be one of the following values:
  758. * @arg RTC_FORMAT_BIN: Binary data format
  759. * @arg RTC_FORMAT_BCD: BCD data format
  760. * @retval HAL status
  761. */
  762. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  763. {
  764. uint32_t datetmpreg;
  765. /* Check the parameters */
  766. assert_param(IS_RTC_FORMAT(Format));
  767. /* Process Locked */
  768. __HAL_LOCK(hrtc);
  769. hrtc->State = HAL_RTC_STATE_BUSY;
  770. if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
  771. {
  772. sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
  773. }
  774. assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
  775. if (Format == RTC_FORMAT_BIN)
  776. {
  777. assert_param(IS_RTC_YEAR(sDate->Year));
  778. assert_param(IS_RTC_MONTH(sDate->Month));
  779. assert_param(IS_RTC_DATE(sDate->Date));
  780. datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16U) | \
  781. ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8U) | \
  782. ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
  783. ((uint32_t)sDate->WeekDay << 13U));
  784. }
  785. else
  786. {
  787. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  788. assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
  789. assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
  790. datetmpreg = ((((uint32_t)sDate->Year) << 16U) | \
  791. (((uint32_t)sDate->Month) << 8U) | \
  792. ((uint32_t)sDate->Date) | \
  793. (((uint32_t)sDate->WeekDay) << 13U));
  794. }
  795. /* Disable the write protection for RTC registers */
  796. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  797. /* Set Initialization mode */
  798. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  799. {
  800. /* Enable the write protection for RTC registers */
  801. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  802. /* Set RTC state*/
  803. hrtc->State = HAL_RTC_STATE_ERROR;
  804. /* Process Unlocked */
  805. __HAL_UNLOCK(hrtc);
  806. return HAL_ERROR;
  807. }
  808. else
  809. {
  810. /* Set the RTC_DR register */
  811. hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
  812. /* Exit Initialization mode */
  813. hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
  814. /* Wait for synchro */
  815. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  816. {
  817. /* Enable the write protection for RTC registers */
  818. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  819. hrtc->State = HAL_RTC_STATE_ERROR;
  820. /* Process Unlocked */
  821. __HAL_UNLOCK(hrtc);
  822. return HAL_ERROR;
  823. }
  824. /* Enable the write protection for RTC registers */
  825. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  826. hrtc->State = HAL_RTC_STATE_READY ;
  827. /* Process Unlocked */
  828. __HAL_UNLOCK(hrtc);
  829. return HAL_OK;
  830. }
  831. }
  832. /**
  833. * @brief Get RTC current date.
  834. * @param hrtc RTC handle
  835. * @param sDate Pointer to Date structure
  836. * @param Format Specifies the format of the entered parameters.
  837. * This parameter can be one of the following values:
  838. * @arg RTC_FORMAT_BIN: Binary data format
  839. * @arg RTC_FORMAT_BCD: BCD data format
  840. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  841. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  842. * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  846. {
  847. uint32_t datetmpreg;
  848. /* Check the parameters */
  849. assert_param(IS_RTC_FORMAT(Format));
  850. /* Get the DR register */
  851. datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
  852. /* Fill the structure fields with the read parameters */
  853. sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16U);
  854. sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
  855. sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
  856. sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13U);
  857. /* Check the input parameters format */
  858. if (Format == RTC_FORMAT_BIN)
  859. {
  860. /* Convert the date structure parameters to Binary format */
  861. sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
  862. sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
  863. sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
  864. }
  865. return HAL_OK;
  866. }
  867. /**
  868. * @}
  869. */
  870. /** @addtogroup RTC_Exported_Functions_Group3
  871. * @brief RTC Alarm functions
  872. *
  873. @verbatim
  874. ===============================================================================
  875. ##### RTC Alarm functions #####
  876. ===============================================================================
  877. [..] This section provides functions allowing to configure Alarm feature
  878. @endverbatim
  879. * @{
  880. */
  881. /**
  882. * @brief Set the specified RTC Alarm.
  883. * @param hrtc RTC handle
  884. * @param sAlarm Pointer to Alarm structure
  885. * @param Format Specifies the format of the entered parameters.
  886. * This parameter can be one of the following values:
  887. * @arg RTC_FORMAT_BIN: Binary data format
  888. * @arg RTC_FORMAT_BCD: BCD data format
  889. * @retval HAL status
  890. */
  891. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  892. {
  893. uint32_t tickstart;
  894. uint32_t tmpreg;
  895. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  896. uint32_t subsecondtmpreg = 0;
  897. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  898. /* Check the parameters */
  899. assert_param(IS_RTC_FORMAT(Format));
  900. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  901. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  902. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  903. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  904. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  905. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  906. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  907. /* Process Locked */
  908. __HAL_LOCK(hrtc);
  909. hrtc->State = HAL_RTC_STATE_BUSY;
  910. if (Format == RTC_FORMAT_BIN)
  911. {
  912. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  913. {
  914. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  915. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  916. }
  917. else
  918. {
  919. sAlarm->AlarmTime.TimeFormat = 0x00U;
  920. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  921. }
  922. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  923. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  924. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  925. {
  926. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  927. }
  928. else
  929. {
  930. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  931. }
  932. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
  933. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
  934. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
  935. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  936. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
  937. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  938. ((uint32_t)sAlarm->AlarmMask));
  939. }
  940. else
  941. {
  942. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  943. {
  944. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  945. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  946. }
  947. else
  948. {
  949. sAlarm->AlarmTime.TimeFormat = 0x00U;
  950. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  951. }
  952. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  953. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  954. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  955. {
  956. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  957. }
  958. else
  959. {
  960. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  961. }
  962. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
  963. ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
  964. ((uint32_t) sAlarm->AlarmTime.Seconds) | \
  965. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  966. ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
  967. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  968. ((uint32_t)sAlarm->AlarmMask));
  969. }
  970. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  971. /* Configure the Alarm A or Alarm B Sub Second registers */
  972. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  973. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  974. /* Disable the write protection for RTC registers */
  975. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  976. /* Configure the Alarm register */
  977. if (sAlarm->Alarm == RTC_ALARM_A)
  978. {
  979. /* Disable the Alarm A interrupt */
  980. __HAL_RTC_ALARMA_DISABLE(hrtc);
  981. /* In case of interrupt mode is used, the interrupt source must disabled */
  982. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  983. tickstart = HAL_GetTick();
  984. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  985. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
  986. {
  987. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  988. {
  989. /* Enable the write protection for RTC registers */
  990. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  991. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  992. /* Process Unlocked */
  993. __HAL_UNLOCK(hrtc);
  994. return HAL_TIMEOUT;
  995. }
  996. }
  997. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  998. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  999. /* Configure the Alarm A Sub Second register */
  1000. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  1001. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1002. /* Configure the Alarm state: Enable Alarm */
  1003. __HAL_RTC_ALARMA_ENABLE(hrtc);
  1004. }
  1005. else
  1006. {
  1007. /* Disable the Alarm B interrupt */
  1008. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1009. /* In case of interrupt mode is used, the interrupt source must disabled */
  1010. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
  1011. tickstart = HAL_GetTick();
  1012. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1013. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
  1014. {
  1015. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1016. {
  1017. /* Enable the write protection for RTC registers */
  1018. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1019. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1020. /* Process Unlocked */
  1021. __HAL_UNLOCK(hrtc);
  1022. return HAL_TIMEOUT;
  1023. }
  1024. }
  1025. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  1026. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1027. /* Configure the Alarm B Sub Second register */
  1028. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  1029. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1030. /* Configure the Alarm state: Enable Alarm */
  1031. __HAL_RTC_ALARMB_ENABLE(hrtc);
  1032. }
  1033. /* Enable the write protection for RTC registers */
  1034. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1035. /* Change RTC state */
  1036. hrtc->State = HAL_RTC_STATE_READY;
  1037. /* Process Unlocked */
  1038. __HAL_UNLOCK(hrtc);
  1039. return HAL_OK;
  1040. }
  1041. /**
  1042. * @brief Set the specified RTC Alarm with Interrupt.
  1043. * @param hrtc RTC handle
  1044. * @param sAlarm Pointer to Alarm structure
  1045. * @param Format Specifies the format of the entered parameters.
  1046. * This parameter can be one of the following values:
  1047. * @arg RTC_FORMAT_BIN: Binary data format
  1048. * @arg RTC_FORMAT_BCD: BCD data format
  1049. * @note The Alarm register can only be written when the corresponding Alarm
  1050. * is disabled (Use the HAL_RTC_DeactivateAlarm()).
  1051. * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
  1052. * @retval HAL status
  1053. */
  1054. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  1055. {
  1056. uint32_t tickstart = 0;
  1057. uint32_t tmpreg = 0;
  1058. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1059. uint32_t subsecondtmpreg = 0;
  1060. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1061. /* Check the parameters */
  1062. assert_param(IS_RTC_FORMAT(Format));
  1063. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1064. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  1065. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  1066. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1067. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  1068. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  1069. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1070. /* Process Locked */
  1071. __HAL_LOCK(hrtc);
  1072. hrtc->State = HAL_RTC_STATE_BUSY;
  1073. if (Format == RTC_FORMAT_BIN)
  1074. {
  1075. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  1076. {
  1077. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  1078. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1079. }
  1080. else
  1081. {
  1082. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1083. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  1084. }
  1085. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  1086. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  1087. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1088. {
  1089. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  1090. }
  1091. else
  1092. {
  1093. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  1094. }
  1095. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
  1096. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
  1097. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
  1098. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  1099. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
  1100. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1101. ((uint32_t)sAlarm->AlarmMask));
  1102. }
  1103. else
  1104. {
  1105. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  1106. {
  1107. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1108. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1109. }
  1110. else
  1111. {
  1112. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1113. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1114. }
  1115. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  1116. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1117. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1118. {
  1119. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1120. }
  1121. else
  1122. {
  1123. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1124. }
  1125. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
  1126. ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
  1127. ((uint32_t) sAlarm->AlarmTime.Seconds) | \
  1128. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  1129. ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
  1130. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1131. ((uint32_t)sAlarm->AlarmMask));
  1132. }
  1133. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1134. /* Configure the Alarm A or Alarm B Sub Second registers */
  1135. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  1136. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1137. /* Disable the write protection for RTC registers */
  1138. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1139. /* Configure the Alarm register */
  1140. if (sAlarm->Alarm == RTC_ALARM_A)
  1141. {
  1142. /* Disable the Alarm A interrupt */
  1143. __HAL_RTC_ALARMA_DISABLE(hrtc);
  1144. /* Clear flag alarm A */
  1145. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1146. tickstart = HAL_GetTick();
  1147. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  1148. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
  1149. {
  1150. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1151. {
  1152. /* Enable the write protection for RTC registers */
  1153. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1154. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1155. /* Process Unlocked */
  1156. __HAL_UNLOCK(hrtc);
  1157. return HAL_TIMEOUT;
  1158. }
  1159. }
  1160. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  1161. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1162. /* Configure the Alarm A Sub Second register */
  1163. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  1164. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1165. /* Configure the Alarm state: Enable Alarm */
  1166. __HAL_RTC_ALARMA_ENABLE(hrtc);
  1167. /* Configure the Alarm interrupt */
  1168. __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
  1169. }
  1170. else
  1171. {
  1172. /* Disable the Alarm B interrupt */
  1173. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1174. /* Clear flag alarm B */
  1175. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1176. tickstart = HAL_GetTick();
  1177. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1178. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
  1179. {
  1180. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1181. {
  1182. /* Enable the write protection for RTC registers */
  1183. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1184. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1185. /* Process Unlocked */
  1186. __HAL_UNLOCK(hrtc);
  1187. return HAL_TIMEOUT;
  1188. }
  1189. }
  1190. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  1191. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1192. /* Configure the Alarm B Sub Second register */
  1193. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  1194. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1195. /* Configure the Alarm state: Enable Alarm */
  1196. __HAL_RTC_ALARMB_ENABLE(hrtc);
  1197. /* Configure the Alarm interrupt */
  1198. __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
  1199. }
  1200. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  1201. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  1202. __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
  1203. /* Enable the write protection for RTC registers */
  1204. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1205. hrtc->State = HAL_RTC_STATE_READY;
  1206. /* Process Unlocked */
  1207. __HAL_UNLOCK(hrtc);
  1208. return HAL_OK;
  1209. }
  1210. /**
  1211. * @brief Deactivate the specified RTC Alarm.
  1212. * @param hrtc RTC handle
  1213. * @param Alarm Specifies the Alarm.
  1214. * This parameter can be one of the following values:
  1215. * @arg RTC_ALARM_A: AlarmA
  1216. * @arg RTC_ALARM_B: AlarmB
  1217. * @retval HAL status
  1218. */
  1219. HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
  1220. {
  1221. uint32_t tickstart;
  1222. /* Check the parameters */
  1223. assert_param(IS_RTC_ALARM(Alarm));
  1224. /* Process Locked */
  1225. __HAL_LOCK(hrtc);
  1226. hrtc->State = HAL_RTC_STATE_BUSY;
  1227. /* Disable the write protection for RTC registers */
  1228. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1229. if (Alarm == RTC_ALARM_A)
  1230. {
  1231. /* AlarmA */
  1232. __HAL_RTC_ALARMA_DISABLE(hrtc);
  1233. /* In case of interrupt mode is used, the interrupt source must disabled */
  1234. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  1235. tickstart = HAL_GetTick();
  1236. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1237. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
  1238. {
  1239. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1240. {
  1241. /* Enable the write protection for RTC registers */
  1242. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1243. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1244. /* Process Unlocked */
  1245. __HAL_UNLOCK(hrtc);
  1246. return HAL_TIMEOUT;
  1247. }
  1248. }
  1249. }
  1250. else
  1251. {
  1252. /* AlarmB */
  1253. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1254. /* In case of interrupt mode is used, the interrupt source must disabled */
  1255. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
  1256. tickstart = HAL_GetTick();
  1257. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1258. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
  1259. {
  1260. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1261. {
  1262. /* Enable the write protection for RTC registers */
  1263. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1264. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1265. /* Process Unlocked */
  1266. __HAL_UNLOCK(hrtc);
  1267. return HAL_TIMEOUT;
  1268. }
  1269. }
  1270. }
  1271. /* Enable the write protection for RTC registers */
  1272. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1273. hrtc->State = HAL_RTC_STATE_READY;
  1274. /* Process Unlocked */
  1275. __HAL_UNLOCK(hrtc);
  1276. return HAL_OK;
  1277. }
  1278. /**
  1279. * @brief Get the RTC Alarm value and masks.
  1280. * @param hrtc RTC handle
  1281. * @param sAlarm Pointer to Date structure
  1282. * @param Alarm Specifies the Alarm.
  1283. * This parameter can be one of the following values:
  1284. * @arg RTC_ALARM_A: AlarmA
  1285. * @arg RTC_ALARM_B: AlarmB
  1286. * @param Format Specifies the format of the entered parameters.
  1287. * This parameter can be one of the following values:
  1288. * @arg RTC_FORMAT_BIN: Binary data format
  1289. * @arg RTC_FORMAT_BCD: BCD data format
  1290. * @retval HAL status
  1291. */
  1292. HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
  1293. {
  1294. uint32_t tmpreg;
  1295. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1296. uint32_t subsecondtmpreg;
  1297. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1298. /* Check the parameters */
  1299. assert_param(IS_RTC_FORMAT(Format));
  1300. assert_param(IS_RTC_ALARM(Alarm));
  1301. if (Alarm == RTC_ALARM_A)
  1302. {
  1303. /* AlarmA */
  1304. sAlarm->Alarm = RTC_ALARM_A;
  1305. tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
  1306. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1307. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
  1308. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1309. }
  1310. else
  1311. {
  1312. sAlarm->Alarm = RTC_ALARM_B;
  1313. tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
  1314. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1315. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
  1316. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1317. }
  1318. /* Fill the structure with the read parameters */
  1319. sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16U);
  1320. sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8U);
  1321. sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
  1322. sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16U);
  1323. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1324. sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
  1325. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1326. sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
  1327. sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
  1328. sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
  1329. if (Format == RTC_FORMAT_BIN)
  1330. {
  1331. sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  1332. sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
  1333. sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
  1334. sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  1335. }
  1336. return HAL_OK;
  1337. }
  1338. /**
  1339. * @brief Handle Alarm interrupt request.
  1340. * @param hrtc RTC handle
  1341. * @retval None
  1342. */
  1343. void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
  1344. {
  1345. /* Get the AlarmA interrupt source enable status */
  1346. if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
  1347. {
  1348. /* Get the pending status of the AlarmA Interrupt */
  1349. if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
  1350. {
  1351. /* AlarmA callback */
  1352. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1353. hrtc->AlarmAEventCallback(hrtc);
  1354. #else
  1355. HAL_RTC_AlarmAEventCallback(hrtc);
  1356. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1357. /* Clear the AlarmA interrupt pending bit */
  1358. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1359. }
  1360. }
  1361. /* Get the AlarmB interrupt source enable status */
  1362. if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
  1363. {
  1364. /* Get the pending status of the AlarmB Interrupt */
  1365. if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
  1366. {
  1367. /* AlarmB callback */
  1368. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1369. hrtc->AlarmBEventCallback(hrtc);
  1370. #else
  1371. HAL_RTCEx_AlarmBEventCallback(hrtc);
  1372. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1373. /* Clear the AlarmB interrupt pending bit */
  1374. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1375. }
  1376. }
  1377. /* Clear the EXTI's line Flag for RTC Alarm */
  1378. __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
  1379. /* Change RTC state */
  1380. hrtc->State = HAL_RTC_STATE_READY;
  1381. }
  1382. /**
  1383. * @brief Alarm A callback.
  1384. * @param hrtc RTC handle
  1385. * @retval None
  1386. */
  1387. __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  1388. {
  1389. /* Prevent unused argument(s) compilation warning */
  1390. UNUSED(hrtc);
  1391. /* NOTE : This function Should not be modified, when the callback is needed,
  1392. the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  1393. */
  1394. }
  1395. /**
  1396. * @brief Handle AlarmA Polling request.
  1397. * @param hrtc RTC handle
  1398. * @param Timeout Timeout duration
  1399. * @retval HAL status
  1400. */
  1401. HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1402. {
  1403. uint32_t tickstart = HAL_GetTick();
  1404. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
  1405. {
  1406. if (Timeout != HAL_MAX_DELAY)
  1407. {
  1408. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1409. {
  1410. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1411. return HAL_TIMEOUT;
  1412. }
  1413. }
  1414. }
  1415. /* Clear the Alarm interrupt pending bit */
  1416. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1417. /* Change RTC state */
  1418. hrtc->State = HAL_RTC_STATE_READY;
  1419. return HAL_OK;
  1420. }
  1421. /**
  1422. * @}
  1423. */
  1424. /** @addtogroup RTC_Exported_Functions_Group4
  1425. * @brief Peripheral Control functions
  1426. *
  1427. @verbatim
  1428. ===============================================================================
  1429. ##### Peripheral Control functions #####
  1430. ===============================================================================
  1431. [..]
  1432. This subsection provides functions allowing to
  1433. (+) Wait for RTC Time and Date Synchronization
  1434. @endverbatim
  1435. * @{
  1436. */
  1437. /**
  1438. * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
  1439. * synchronized with RTC APB clock.
  1440. * @note The RTC Resynchronization mode is write protected, use the
  1441. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  1442. * @note To read the calendar through the shadow registers after Calendar
  1443. * initialization, calendar update or after wakeup from low power modes
  1444. * the software must first clear the RSF flag.
  1445. * The software must then wait until it is set again before reading
  1446. * the calendar, which means that the calendar registers have been
  1447. * correctly copied into the RTC_TR and RTC_DR shadow registers.
  1448. * @param hrtc RTC handle
  1449. * @retval HAL status
  1450. */
  1451. HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
  1452. {
  1453. uint32_t tickstart;
  1454. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1455. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  1456. if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  1457. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1458. {
  1459. /* Clear RSF flag */
  1460. hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
  1461. tickstart = HAL_GetTick();
  1462. /* Wait the registers to be synchronised */
  1463. while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
  1464. {
  1465. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1466. {
  1467. return HAL_TIMEOUT;
  1468. }
  1469. }
  1470. }
  1471. return HAL_OK;
  1472. }
  1473. /**
  1474. * @}
  1475. */
  1476. /** @addtogroup RTC_Exported_Functions_Group5
  1477. * @brief Peripheral State functions
  1478. *
  1479. @verbatim
  1480. ===============================================================================
  1481. ##### Peripheral State functions #####
  1482. ===============================================================================
  1483. [..]
  1484. This subsection provides functions allowing to
  1485. (+) Get RTC state
  1486. @endverbatim
  1487. * @{
  1488. */
  1489. /**
  1490. * @brief Return the RTC handle state.
  1491. * @param hrtc RTC handle
  1492. * @retval HAL state
  1493. */
  1494. HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
  1495. {
  1496. /* Return RTC handle state */
  1497. return hrtc->State;
  1498. }
  1499. /**
  1500. * @}
  1501. */
  1502. /**
  1503. * @}
  1504. */
  1505. /** @addtogroup RTC_Private_Functions
  1506. * @{
  1507. */
  1508. /**
  1509. * @brief Enter the RTC Initialization mode.
  1510. * @note The RTC Initialization mode is write protected, use the
  1511. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  1512. * @param hrtc RTC handle
  1513. * @retval HAL status
  1514. */
  1515. HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
  1516. {
  1517. uint32_t tickstart;
  1518. /* Check if the Initialization mode is set */
  1519. if ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
  1520. {
  1521. /* Set the Initialization mode */
  1522. hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
  1523. tickstart = HAL_GetTick();
  1524. /* Wait till RTC is in INIT state and if Time out is reached exit */
  1525. while ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
  1526. {
  1527. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1528. {
  1529. return HAL_TIMEOUT;
  1530. }
  1531. }
  1532. }
  1533. return HAL_OK;
  1534. }
  1535. /**
  1536. * @brief Convert a 2 digit decimal to BCD format.
  1537. * @param Value Byte to be converted
  1538. * @retval Converted byte
  1539. */
  1540. uint8_t RTC_ByteToBcd2(uint8_t Value)
  1541. {
  1542. uint32_t bcdhigh = 0U;
  1543. uint8_t Param = Value;
  1544. while (Param >= 10U)
  1545. {
  1546. bcdhigh++;
  1547. Param -= 10U;
  1548. }
  1549. return ((uint8_t)(bcdhigh << 4U) | Param);
  1550. }
  1551. /**
  1552. * @brief Convert from 2 digit BCD to Binary.
  1553. * @param Value BCD value to be converted
  1554. * @retval Converted word
  1555. */
  1556. uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1557. {
  1558. uint32_t tmp;
  1559. tmp = (((uint32_t)Value & 0xF0U) >> 4U) * 10U;
  1560. return (uint8_t)(tmp + ((uint32_t)Value & 0x0FU));
  1561. }
  1562. /**
  1563. * @}
  1564. */
  1565. #endif /* HAL_RTC_MODULE_ENABLED */
  1566. /**
  1567. * @}
  1568. */
  1569. /**
  1570. * @}
  1571. */
  1572. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/