Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_hal_rtc.c
blobd16a7c68b0657e1942f06546ca1b4a80a00f6d09
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_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/de-initialization functions
9 * + Calendar (Time and Date) configuration
10 * + Alarms (Alarm A and Alarm B) configuration
11 * + WakeUp Timer configuration
12 * + TimeStamp configuration
13 * + Tampers configuration
14 * + Backup Data Registers configuration
15 * + RTC Tamper and TimeStamp Pins Selection
16 * + Interrupts and flags management
18 @verbatim
19 ===============================================================================
20 ##### RTC Operating Condition #####
21 ===============================================================================
22 [..] The real-time clock (RTC) and the RTC backup registers can be powered
23 from the VBAT voltage when the main VDD supply is powered off.
24 To retain the content of the RTC backup registers and supply the RTC
25 when VDD is turned off, VBAT pin can be connected to an optional
26 standby voltage supplied by a battery or by another source.
28 ##### Backup Domain Reset #####
29 ===============================================================================
30 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
31 to their reset values.
32 A backup domain reset is generated when one of the following events occurs:
33 (#) Software reset, triggered by setting the BDRST bit in the
34 RCC Backup domain control register (RCC_BDCR).
35 (#) VDD or VBAT power on, if both supplies have previously been powered off.
36 (#) Tamper detection event resets all data backup registers.
38 ##### Backup Domain Access #####
39 ==================================================================
40 [..] After reset, the backup domain (RTC registers and RTC backup data registers)
41 is protected against possible unwanted write accesses.
42 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
43 (+) Enable the Power Controller (PWR) APB1 interface clock using the
44 __HAL_RCC_PWR_CLK_ENABLE() function.
45 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
46 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
47 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
49 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
50 (#) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
51 PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
52 (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro.
54 ##### How to use RTC Driver #####
55 ===================================================================
56 [..]
57 (+) Enable the RTC domain access (see description in the section above).
58 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
59 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.
68 *** Alarm configuration ***
69 ===========================
70 [..]
71 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
72 You can also configure the RTC Alarm with interrupt mode using the
73 HAL_RTC_SetAlarm_IT() function.
74 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
76 ##### RTC and low power modes #####
77 ==================================================================
78 [..] The MCU can be woken up from a low power mode by an RTC alternate
79 function.
80 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
81 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
82 These RTC alternate functions can wake up the system from the Stop and
83 Standby low power modes.
84 [..] The system can also wake up from low power modes without depending
85 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
86 or the RTC wakeup events.
87 [..] The RTC provides a programmable time base for waking up from the
88 Stop or Standby mode at regular intervals.
89 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
90 is LSE or LSI.
92 *** Callback registration ***
93 =============================================
94 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
95 not defined, the callback registration feature is not available and all callbacks
96 are set to the corresponding weak functions. This is the recommended configuration
97 in order to optimize memory/code consumption footprint/performances.
99 The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
100 allows the user to configure dynamically the driver callbacks.
101 Use Function @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
103 Function @ref HAL_RTC_RegisterCallback() allows to register following callbacks:
104 (+) AlarmAEventCallback : RTC Alarm A Event callback.
105 (+) AlarmBEventCallback : RTC Alarm B Event callback.
106 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
107 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
108 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
109 (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
110 (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
111 (+) Tamper4EventCallback : RTC Tamper 4 Event callback.
112 (+) Tamper5EventCallback : RTC Tamper 5 Event callback.
113 (+) Tamper6EventCallback : RTC Tamper 6 Event callback.
114 (+) Tamper7EventCallback : RTC Tamper 7 Event callback.
115 (+) Tamper8EventCallback : RTC Tamper 8 Event callback.
116 (+) InternalTamper1EventCallback : RTC InternalTamper 1 Event callback.
117 (+) InternalTamper2EventCallback : RTC InternalTamper 2 Event callback.
118 (+) InternalTamper3EventCallback : RTC InternalTamper 3 Event callback.
119 (+) InternalTamper5EventCallback : RTC InternalTamper 5 Event callback.
120 (+) InternalTamper8EventCallback : RTC InternalTamper 8 Event callback.
121 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
122 (+) AlarmAEventCallback_S : RTC Alarm A Event callback_S
123 (+) AlarmBEventCallback_S : RTC Alarm B Event callback_S.
124 (+) TimeStampEventCallback_S : RTC TimeStampEvent callback_S.
125 (+) WakeUpTimerEventCallback_S : RTC WakeUpTimerEvent callback_S.
126 (+) Tamper1EventCallback_S : RTC Tamper 1 Event callback_S.
127 (+) Tamper2EventCallback_S : RTC Tamper 2 Event callback_S.
128 (+) Tamper3EventCallback_S : RTC Tamper 3 Event callback_S.
129 (+) Tamper4EventCallback_S : RTC Tamper 4 Event callback_S.
130 (+) Tamper5EventCallback_S : RTC Tamper 5 Event callback_S.
131 (+) Tamper6EventCallback_S : RTC Tamper 6 Event callback_S.
132 (+) Tamper7EventCallback_S : RTC Tamper 7 Event callback_S.
133 (+) Tamper8EventCallback_S : RTC Tamper 8 Event callback_S.
134 (+) InternalTamper1EventCallback_S : RTC InternalTamper 1 Event callback_S.
135 (+) InternalTamper2EventCallback_S : RTC InternalTamper 2 Event callback_S.
136 (+) InternalTamper3EventCallback_S : RTC InternalTamper 3 Event callback_S.
137 (+) InternalTamper5EventCallback_S : RTC InternalTamper 5 Event callback_S.
138 (+) InternalTamper8EventCallback_S : RTC InternalTamper 8 Event callback_S.
139 #endif
140 (+) MspInitCallback : RTC MspInit callback.
141 (+) MspDeInitCallback : RTC MspDeInit callback.
142 This function takes as parameters the HAL peripheral handle, the Callback ID
143 and a pointer to the user callback function.
145 Use function @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
146 weak function.
147 @ref HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
148 and the Callback ID.
149 This function allows to reset following callbacks:
150 (+) AlarmAEventCallback : RTC Alarm A Event callback.
151 (+) AlarmBEventCallback : RTC Alarm B Event callback.
152 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
153 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
154 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
155 (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
156 (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
157 (+) Tamper4EventCallback : RTC Tamper 4 Event callback.
158 (+) Tamper5EventCallback : RTC Tamper 5 Event callback.
159 (+) Tamper6EventCallback : RTC Tamper 6 Event callback.
160 (+) Tamper7EventCallback : RTC Tamper 7 Event callback.
161 (+) Tamper8EventCallback : RTC Tamper 8 Event callback.
162 (+) InternalTamper1EventCallback : RTC Internal Tamper 1 Event callback.
163 (+) InternalTamper2EventCallback : RTC Internal Tamper 2 Event callback.
164 (+) InternalTamper3EventCallback : RTC Internal Tamper 3 Event callback.
165 (+) InternalTamper4EventCallback : RTC Internal Tamper 4 Event callback.
166 (+) InternalTamper5EventCallback : RTC Internal Tamper 5 Event callback.
167 (+) InternalTamper6EventCallback : RTC Internal Tamper 6 Event callback.
168 (+) InternalTamper8EventCallback : RTC Internal Tamper 8 Event callback.
169 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
170 (+) AlarmAEventCallback_S : RTC Alarm A Event callback secure.
171 (+) AlarmBEventCallback_S : RTC Alarm B Event callback secure.
172 (+) TimeStampEventCallback_S : RTC TimeStamp Event callback secure.
173 (+) WakeUpTimerEventCallback_S : RTC WakeUpTimer Event callback secure.
174 (+) Tamper1EventCallback_S : RTC Tamper 1 Event callback secure.
175 (+) Tamper2EventCallback_S : RTC Tamper 2 Event callback secure.
176 (+) Tamper3EventCallback_S : RTC Tamper 3 Event callback secure.
177 (+) Tamper4EventCallback_S : RTC Tamper 4 Event callback secure.
178 (+) Tamper5EventCallback_S : RTC Tamper 5 Event callback secure.
179 (+) Tamper6EventCallback_S : RTC Tamper 6 Event callback secure.
180 (+) Tamper7EventCallback_S : RTC Tamper 7 Event callback secure.
181 (+) Tamper8EventCallback_S : RTC Tamper 8 Event callback secure.
182 (+) InternalTamper1EventCallback_S : RTC Internal Tamper 1 Event callback secure.
183 (+) InternalTamper2EventCallback_S : RTC Internal Tamper 2 Event callback secure.
184 (+) InternalTamper3EventCallback_S : RTC Internal Tamper 3 Event callback secure.
185 (+) InternalTamper4EventCallback_S : RTC Internal Tamper 4 Event callback secure.
186 (+) InternalTamper5EventCallback_S : RTC Internal Tamper 5 Event callback secure.
187 (+) InternalTamper6EventCallback_S : RTC Internal Tamper 6 Event callback secure.
188 (+) InternalTamper8EventCallback_S : RTC Internal Tamper 8 Event callback secure.
189 #endif
190 (+) MspInitCallback : RTC MspInit callback.
191 (+) MspDeInitCallback : RTC MspDeInit callback.
193 By default, after the @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
194 all callbacks are set to the corresponding weak functions :
195 examples @ref AlarmAEventCallback(), @ref TimeStampEventCallback().
196 Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
197 in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
198 (not registered beforehand).
199 If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit()
200 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
202 Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
203 Exception done MspInit/MspDeInit that can be registered/unregistered
204 in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
205 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
206 In that case first register the MspInit/MspDeInit user callbacks
207 using @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
208 or @ref HAL_RTC_Init() function.
210 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
211 not defined, the callback registration feature is not available and all callbacks
212 are set to the corresponding weak functions.
214 @endverbatim
215 ******************************************************************************
216 * @attention
218 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
220 * This software component is licensed by ST under BSD 3-Clause license,
221 * the "License"; You may not use this file except in compliance with the
222 * License. You may obtain a copy of the License at:
223 * opensource.org/licenses/BSD-3-Clause
225 ******************************************************************************
228 /* Includes ------------------------------------------------------------------*/
229 #include "stm32g4xx_hal.h"
231 /** @addtogroup STM32G4xx_HAL_Driver
232 * @{
236 /** @addtogroup RTC
237 * @brief RTC HAL module driver
238 * @{
241 #ifdef HAL_RTC_MODULE_ENABLED
243 /* Private typedef -----------------------------------------------------------*/
244 /* Private define ------------------------------------------------------------*/
245 /* Private macro -------------------------------------------------------------*/
246 /* Private variables ---------------------------------------------------------*/
247 /* Private function prototypes -----------------------------------------------*/
248 /* Exported functions --------------------------------------------------------*/
250 /** @addtogroup RTC_Exported_Functions
251 * @{
254 /** @addtogroup RTC_Exported_Functions_Group1
255 * @brief Initialization and Configuration functions
257 @verbatim
258 ===============================================================================
259 ##### Initialization and de-initialization functions #####
260 ===============================================================================
261 [..] This section provides functions allowing to initialize and configure the
262 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
263 RTC registers Write protection, enter and exit the RTC initialization mode,
264 RTC registers synchronization check and reference clock detection enable.
265 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
266 It is split into 2 programmable prescalers to minimize power consumption.
267 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
268 (++) When both prescalers are used, it is recommended to configure the
269 asynchronous prescaler to a high value to minimize power consumption.
270 (#) All RTC registers are Write protected. Writing to the RTC registers
271 is enabled by writing a key into the Write Protection register, RTC_WPR.
272 (#) To configure the RTC Calendar, user application should enter
273 initialization mode. In this mode, the calendar counter is stopped
274 and its value can be updated. When the initialization sequence is
275 complete, the calendar restarts counting after 4 RTCCLK cycles.
276 (#) To read the calendar through the shadow registers after Calendar
277 initialization, calendar update or after wakeup from low power modes
278 the software must first clear the RSF flag. The software must then
279 wait until it is set again before reading the calendar, which means
280 that the calendar registers have been correctly copied into the
281 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
282 implements the above software sequence (RSF clear and RSF check).
284 @endverbatim
285 * @{
289 * @brief Initialize the RTC peripheral
290 * @param hrtc RTC handle
291 * @retval HAL status
293 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
295 HAL_StatusTypeDef status = HAL_ERROR;
297 /* Check the RTC peripheral state */
298 if (hrtc != NULL)
300 status = HAL_OK;
301 /* Check the parameters */
302 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
303 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
304 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
305 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
306 assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
307 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
308 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
309 assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
311 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
312 if (hrtc->State == HAL_RTC_STATE_RESET)
314 /* Allocate lock resource and initialize it */
315 hrtc->Lock = HAL_UNLOCKED;
317 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
318 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
319 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
320 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
321 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
322 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
323 #if (RTC_TAMP_NB == 3)
324 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
325 #endif /* RTC_TAMP_NB */
326 #ifdef RTC_TAMP_INT_1_SUPPORT
327 hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback; /*!< Legacy weak InternalTamper1EventCallback */
328 #endif /* RTC_TAMP_INT_1_SUPPORT */
329 #ifdef RTC_TAMP_INT_2_SUPPORT
330 hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback; /*!< Legacy weak InternalTamper2EventCallback */
331 #endif /* RTC_TAMP_INT_2_SUPPORT */
332 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback; /*!< Legacy weak InternalTamper3EventCallback */
333 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback; /*!< Legacy weak InternalTamper4EventCallback */
334 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback; /*!< Legacy weak InternalTamper5EventCallback */
335 #ifdef RTC_TAMP_INT_6_SUPPORT
336 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback; /*!< Legacy weak InternalTamper6EventCallback */
337 #endif /* RTC_TAMP_INT_6_SUPPORT */
338 #ifdef RTC_TAMP_INT_7_SUPPORT
339 hrtc->InternalTamper7EventCallback = HAL_RTCEx_InternalTamper7EventCallback; /*!< Legacy weak InternalTamper7EventCallback */
340 #endif /* RTC_TAMP_INT_7_SUPPORT */
342 if (hrtc->MspInitCallback == NULL)
344 hrtc->MspInitCallback = HAL_RTC_MspInit;
346 /* Init the low level hardware */
347 hrtc->MspInitCallback(hrtc);
349 if (hrtc->MspDeInitCallback == NULL)
351 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
354 #else
355 if (hrtc->State == HAL_RTC_STATE_RESET)
357 /* Allocate lock resource and initialize it */
358 hrtc->Lock = HAL_UNLOCKED;
360 /* Initialize RTC MSP */
361 HAL_RTC_MspInit(hrtc);
363 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
365 /* Set RTC state */
366 hrtc->State = HAL_RTC_STATE_BUSY;
368 /* Disable the write protection for RTC registers */
369 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
371 /* Set Initialization mode */
372 if (RTC_EnterInitMode(hrtc) != HAL_OK)
374 /* Enable the write protection for RTC registers */
375 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
377 /* Set RTC state */
378 hrtc->State = HAL_RTC_STATE_ERROR;
380 status = HAL_ERROR;
382 else
384 /* Clear RTC_CR FMT, OSEL and POL Bits */
385 CLEAR_BIT(RTC->CR, (RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE));
386 /* Set RTC_CR register */
387 SET_BIT(RTC->CR, (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity));
389 /* Configure the RTC PRER */
390 WRITE_REG(RTC->PRER, ((hrtc->Init.SynchPrediv) | (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos)));
392 /* Exit Initialization mode */
393 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
395 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
396 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
398 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
400 /* Enable the write protection for RTC registers */
401 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
403 hrtc->State = HAL_RTC_STATE_ERROR;
404 status = HAL_ERROR;
408 if (status == HAL_OK)
410 MODIFY_REG(RTC->CR, \
411 RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN, \
412 hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
414 /* Enable the write protection for RTC registers */
415 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
417 /* Set RTC state */
418 hrtc->State = HAL_RTC_STATE_READY;
422 return status;
426 * @brief DeInitialize the RTC peripheral.
427 * @note This function does not reset the RTC Backup Data registers.
428 * @param hrtc RTC handle
429 * @retval HAL status
431 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
433 HAL_StatusTypeDef status;
435 /* Set RTC state */
436 hrtc->State = HAL_RTC_STATE_BUSY;
438 /* Disable the write protection for RTC registers */
439 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
441 status = RTC_EnterInitMode(hrtc);
443 /* Set Initialization mode */
444 if (status != HAL_OK)
446 /* Set RTC state */
447 hrtc->State = HAL_RTC_STATE_ERROR;
449 else
451 /* Reset all RTC CR register bits */
452 CLEAR_REG(RTC->CR);
453 WRITE_REG(RTC->DR, (uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
454 CLEAR_REG(RTC->TR);
455 WRITE_REG(RTC->WUTR, RTC_WUTR_WUT);
456 WRITE_REG(RTC->PRER, ((uint32_t)(RTC_PRER_PREDIV_A | 0xFFU)));
457 CLEAR_REG(RTC->ALRMAR);
458 CLEAR_REG(RTC->ALRMBR);
459 CLEAR_REG(RTC->SHIFTR);
460 CLEAR_REG(RTC->CALR);
461 CLEAR_REG(RTC->ALRMASSR);
462 CLEAR_REG(RTC->ALRMBSSR);
463 WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSOVF | RTC_SCR_CTSF | RTC_SCR_CWUTF | RTC_SCR_CALRBF | RTC_SCR_CALRAF);
465 /* Exit initialization mode */
466 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
468 status = HAL_RTC_WaitForSynchro(hrtc);
470 if (status != HAL_OK)
472 hrtc->State = HAL_RTC_STATE_ERROR;
474 else
476 /* Reset TAMP registers */
477 WRITE_REG(TAMP->CR1, RTC_INT_TAMPER_ALL);
478 CLEAR_REG(TAMP->CR2);
479 CLEAR_REG(TAMP->FLTCR);
483 /* Enable the write protection for RTC registers */
484 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
486 if (status == HAL_OK)
488 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
489 if (hrtc->MspDeInitCallback == NULL)
491 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
494 /* DeInit the low level hardware: CLOCK, NVIC.*/
495 hrtc->MspDeInitCallback(hrtc);
497 #else
498 /* De-Initialize RTC MSP */
499 HAL_RTC_MspDeInit(hrtc);
500 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
502 hrtc->State = HAL_RTC_STATE_RESET;
505 /* Release Lock */
506 __HAL_UNLOCK(hrtc);
508 return status;
511 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
513 * @brief Register a User RTC Callback
514 * To be used instead of the weak predefined callback
515 * @param hrtc RTC handle
516 * @param CallbackID ID of the callback to be registered
517 * This parameter can be one of the following values:
518 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
519 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
520 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
521 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
522 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
523 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
524 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
525 * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
526 * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
527 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
528 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
529 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
530 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
531 * @arg @ref HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID Internal Tamper 7 Callback ID
532 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
533 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
534 * @param pCallback pointer to the Callback function
535 * @retval HAL status
537 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID,
538 pRTC_CallbackTypeDef pCallback)
540 HAL_StatusTypeDef status = HAL_OK;
542 if (pCallback == NULL)
544 return HAL_ERROR;
547 /* Process locked */
548 __HAL_LOCK(hrtc);
550 if (HAL_RTC_STATE_READY == hrtc->State)
552 switch (CallbackID)
554 case HAL_RTC_ALARM_A_EVENT_CB_ID :
555 hrtc->AlarmAEventCallback = pCallback;
556 break;
558 case HAL_RTC_ALARM_B_EVENT_CB_ID :
559 hrtc->AlarmBEventCallback = pCallback;
560 break;
562 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
563 hrtc->TimeStampEventCallback = pCallback;
564 break;
566 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
567 hrtc->WakeUpTimerEventCallback = pCallback;
568 break;
570 case HAL_RTC_TAMPER1_EVENT_CB_ID :
571 hrtc->Tamper1EventCallback = pCallback;
572 break;
574 case HAL_RTC_TAMPER2_EVENT_CB_ID :
575 hrtc->Tamper2EventCallback = pCallback;
576 break;
578 #if (RTC_TAMP_NB == 3)
579 case HAL_RTC_TAMPER3_EVENT_CB_ID :
580 hrtc->Tamper3EventCallback = pCallback;
581 break;
583 #endif /* RTC_TAMP_NB */
585 #ifdef RTC_TAMP_INT_1_SUPPORT
586 case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
587 hrtc->InternalTamper1EventCallback = pCallback;
588 break;
589 #endif /* RTC_TAMP_INT_1_SUPPORT */
591 #ifdef RTC_TAMP_INT_2_SUPPORT
592 case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
593 hrtc->InternalTamper2EventCallback = pCallback;
594 break;
596 #endif /* RTC_TAMP_INT_2_SUPPORT */
597 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
598 hrtc->InternalTamper3EventCallback = pCallback;
599 break;
601 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
602 hrtc->InternalTamper4EventCallback = pCallback;
603 break;
605 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
606 hrtc->InternalTamper5EventCallback = pCallback;
607 break;
609 #ifdef RTC_TAMP_INT_6_SUPPORT
610 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
611 hrtc->InternalTamper6EventCallback = pCallback;
612 break;
614 #endif /* RTC_TAMP_INT_6_SUPPORT */
615 #ifdef RTC_TAMP_INT_7_SUPPORT
616 case HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID :
617 hrtc->InternalTamper7EventCallback = pCallback;
618 break;
620 #endif /* RTC_TAMP_INT_7_SUPPORT */
621 case HAL_RTC_MSPINIT_CB_ID :
622 hrtc->MspInitCallback = pCallback;
623 break;
625 case HAL_RTC_MSPDEINIT_CB_ID :
626 hrtc->MspDeInitCallback = pCallback;
627 break;
629 default :
630 /* Return error status */
631 status = HAL_ERROR;
632 break;
635 else if (HAL_RTC_STATE_RESET == hrtc->State)
637 switch (CallbackID)
639 case HAL_RTC_MSPINIT_CB_ID :
640 hrtc->MspInitCallback = pCallback;
641 break;
643 case HAL_RTC_MSPDEINIT_CB_ID :
644 hrtc->MspDeInitCallback = pCallback;
645 break;
647 default :
648 /* Return error status */
649 status = HAL_ERROR;
650 break;
653 else
655 /* Return error status */
656 status = HAL_ERROR;
659 /* Release Lock */
660 __HAL_UNLOCK(hrtc);
662 return status;
666 * @brief Unregister an RTC Callback
667 * RTC callback is redirected to the weak predefined callback
668 * @param hrtc RTC handle
669 * @param CallbackID ID of the callback to be unregistered
670 * This parameter can be one of the following values:
671 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
672 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
673 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
674 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
675 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
676 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
677 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
678 * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
679 * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
680 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
681 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
682 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
683 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
684 * @arg @ref HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID Internal Tamper 7 Callback ID
685 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
686 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
687 * @retval HAL status
689 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
691 HAL_StatusTypeDef status = HAL_OK;
693 /* Process locked */
694 __HAL_LOCK(hrtc);
696 if (HAL_RTC_STATE_READY == hrtc->State)
698 switch (CallbackID)
700 case HAL_RTC_ALARM_A_EVENT_CB_ID :
701 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
702 break;
704 case HAL_RTC_ALARM_B_EVENT_CB_ID :
705 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
706 break;
708 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
709 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
710 break;
712 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
713 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
714 break;
716 case HAL_RTC_TAMPER1_EVENT_CB_ID :
717 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
718 break;
720 case HAL_RTC_TAMPER2_EVENT_CB_ID :
721 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
722 break;
724 #if (RTC_TAMP_NB == 3)
725 case HAL_RTC_TAMPER3_EVENT_CB_ID :
726 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
727 break;
728 #endif /* RTC_TAMP_NB */
729 #ifdef RTC_TAMP_INT_1_SUPPORT
730 case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
731 hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback; /* Legacy weak InternalTamper1EventCallback */
732 break;
733 #endif /* RTC_TAMP_INT_1_SUPPORT */
735 #ifdef RTC_TAMP_INT_2_SUPPORT
736 case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
737 hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback; /* Legacy weak InternalTamper2EventCallback */
738 break;
740 #endif /* RTC_TAMP_INT_2_SUPPORT */
741 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
742 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback; /* Legacy weak InternalTamper3EventCallback */
743 break;
745 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
746 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback; /* Legacy weak InternalTamper4EventCallback */
747 break;
749 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
750 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback; /* Legacy weak InternalTamper5EventCallback */
751 break;
753 #ifdef RTC_TAMP_INT_6_SUPPORT
754 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
755 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback; /* Legacy weak InternalTamper6EventCallback */
756 break;
758 #endif /* RTC_TAMP_INT_6_SUPPORT */
759 #ifdef RTC_TAMP_INT_7_SUPPORT
760 case HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID :
761 hrtc->InternalTamper7EventCallback = HAL_RTCEx_InternalTamper7EventCallback; /* Legacy weak InternalTamper7EventCallback */
762 break;
764 #endif /* RTC_TAMP_INT_7_SUPPORT */
765 case HAL_RTC_MSPINIT_CB_ID :
766 hrtc->MspInitCallback = HAL_RTC_MspInit;
767 break;
769 case HAL_RTC_MSPDEINIT_CB_ID :
770 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
771 break;
773 default :
774 /* Return error status */
775 status = HAL_ERROR;
776 break;
779 else if (HAL_RTC_STATE_RESET == hrtc->State)
781 switch (CallbackID)
783 case HAL_RTC_MSPINIT_CB_ID :
784 hrtc->MspInitCallback = HAL_RTC_MspInit;
785 break;
787 case HAL_RTC_MSPDEINIT_CB_ID :
788 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
789 break;
791 default :
792 /* Return error status */
793 status = HAL_ERROR;
794 break;
797 else
799 /* Return error status */
800 status = HAL_ERROR;
803 /* Release Lock */
804 __HAL_UNLOCK(hrtc);
806 return status;
808 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
811 * @brief Initialize the RTC MSP.
812 * @param hrtc RTC handle
813 * @retval None
815 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
817 /* Prevent unused argument(s) compilation warning */
818 UNUSED(hrtc);
820 /* NOTE : This function should not be modified, when the callback is needed,
821 the HAL_RTC_MspInit could be implemented in the user file
826 * @brief DeInitialize the RTC MSP.
827 * @param hrtc RTC handle
828 * @retval None
830 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
832 /* Prevent unused argument(s) compilation warning */
833 UNUSED(hrtc);
835 /* NOTE : This function should not be modified, when the callback is needed,
836 the HAL_RTC_MspDeInit could be implemented in the user file
841 * @}
844 /** @addtogroup RTC_Exported_Functions_Group2
845 * @brief RTC Time and Date functions
847 @verbatim
848 ===============================================================================
849 ##### RTC Time and Date functions #####
850 ===============================================================================
852 [..] This section provides functions allowing to configure Time and Date features
854 @endverbatim
855 * @{
859 * @brief Set RTC current time.
860 * @param hrtc RTC handle
861 * @param sTime Pointer to Time structure
862 * @param Format Specifies the format of the entered parameters.
863 * This parameter can be one of the following values:
864 * @arg RTC_FORMAT_BIN: Binary data format
865 * @arg RTC_FORMAT_BCD: BCD data format
866 * @retval HAL status
868 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
870 uint32_t tmpreg;
872 /* Check the parameters */
873 assert_param(IS_RTC_FORMAT(Format));
874 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
875 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
877 /* Process Locked */
878 __HAL_LOCK(hrtc);
880 hrtc->State = HAL_RTC_STATE_BUSY;
882 /* Disable the write protection for RTC registers */
883 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
885 /* Set Initialization mode */
886 if (RTC_EnterInitMode(hrtc) != HAL_OK)
888 /* Enable the write protection for RTC registers */
889 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
891 /* Set RTC state */
892 hrtc->State = HAL_RTC_STATE_ERROR;
894 /* Process Unlocked */
895 __HAL_UNLOCK(hrtc);
897 return HAL_ERROR;
899 else
901 if (Format == RTC_FORMAT_BIN)
903 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
905 assert_param(IS_RTC_HOUR12(sTime->Hours));
906 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
908 else
910 sTime->TimeFormat = 0x00U;
911 assert_param(IS_RTC_HOUR24(sTime->Hours));
913 assert_param(IS_RTC_MINUTES(sTime->Minutes));
914 assert_param(IS_RTC_SECONDS(sTime->Seconds));
916 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
917 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
918 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
919 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
921 else
923 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
925 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
926 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
928 else
930 sTime->TimeFormat = 0x00U;
931 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
933 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
934 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
935 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
936 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
937 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
938 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
941 /* Set the RTC_TR register */
942 WRITE_REG(RTC->TR, (tmpreg & RTC_TR_RESERVED_MASK));
944 /* Clear the bits to be configured */
945 CLEAR_BIT(RTC->CR, RTC_CR_BKP);
947 /* Configure the RTC_CR register */
948 SET_BIT(RTC->CR, (sTime->DayLightSaving | sTime->StoreOperation));
950 /* Exit Initialization mode */
951 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
953 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
954 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
956 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
958 /* Enable the write protection for RTC registers */
959 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
961 hrtc->State = HAL_RTC_STATE_ERROR;
963 /* Process Unlocked */
964 __HAL_UNLOCK(hrtc);
966 return HAL_ERROR;
970 /* Enable the write protection for RTC registers */
971 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
973 hrtc->State = HAL_RTC_STATE_READY;
975 __HAL_UNLOCK(hrtc);
977 return HAL_OK;
982 * @brief Get RTC current time.
983 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
984 * value in second fraction ratio with time unit following generic formula:
985 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
986 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
987 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
988 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
989 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
990 * to ensure consistency between the time and date values.
991 * @param hrtc RTC handle
992 * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
993 * with input format (BIN or BCD), also SubSeconds field returning the
994 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
995 * factor to be used for second fraction ratio computation.
996 * @param Format Specifies the format of the entered parameters.
997 * This parameter can be one of the following values:
998 * @arg RTC_FORMAT_BIN: Binary data format
999 * @arg RTC_FORMAT_BCD: BCD data format
1000 * @retval HAL status
1002 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
1004 uint32_t tmpreg;
1006 UNUSED(hrtc);
1008 /* Check the parameters */
1009 assert_param(IS_RTC_FORMAT(Format));
1011 /* Get subseconds structure field from the corresponding register*/
1012 sTime->SubSeconds = READ_REG(RTC->SSR);
1014 /* Get SecondFraction structure field from the corresponding register field*/
1015 sTime->SecondFraction = (uint32_t)(READ_REG(RTC->PRER) & RTC_PRER_PREDIV_S);
1017 /* Get the TR register */
1018 tmpreg = (uint32_t)(READ_REG(RTC->TR) & RTC_TR_RESERVED_MASK);
1020 /* Fill the structure fields with the read parameters */
1021 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
1022 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
1023 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
1024 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
1026 /* Check the input parameters format */
1027 if (Format == RTC_FORMAT_BIN)
1029 /* Convert the time structure parameters to Binary format */
1030 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
1031 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
1032 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
1035 return HAL_OK;
1039 * @brief Set RTC current date.
1040 * @param hrtc RTC handle
1041 * @param sDate Pointer to date structure
1042 * @param Format specifies the format of the entered parameters.
1043 * This parameter can be one of the following values:
1044 * @arg RTC_FORMAT_BIN: Binary data format
1045 * @arg RTC_FORMAT_BCD: BCD data format
1046 * @retval HAL status
1048 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1050 uint32_t datetmpreg;
1052 /* Check the parameters */
1053 assert_param(IS_RTC_FORMAT(Format));
1055 /* Process Locked */
1056 __HAL_LOCK(hrtc);
1058 hrtc->State = HAL_RTC_STATE_BUSY;
1060 if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
1062 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
1065 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
1067 if (Format == RTC_FORMAT_BIN)
1069 assert_param(IS_RTC_YEAR(sDate->Year));
1070 assert_param(IS_RTC_MONTH(sDate->Month));
1071 assert_param(IS_RTC_DATE(sDate->Date));
1073 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
1074 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
1075 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
1076 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
1078 else
1080 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
1081 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
1082 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
1084 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
1085 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
1086 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
1087 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
1090 /* Disable the write protection for RTC registers */
1091 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1093 /* Set Initialization mode */
1094 if (RTC_EnterInitMode(hrtc) != HAL_OK)
1096 /* Enable the write protection for RTC registers */
1097 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1099 /* Set RTC state*/
1100 hrtc->State = HAL_RTC_STATE_ERROR;
1102 /* Process Unlocked */
1103 __HAL_UNLOCK(hrtc);
1105 return HAL_ERROR;
1107 else
1109 /* Set the RTC_DR register */
1110 WRITE_REG(RTC->DR, (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK));
1112 /* Exit Initialization mode */
1113 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
1115 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1116 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1118 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1120 /* Enable the write protection for RTC registers */
1121 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1123 hrtc->State = HAL_RTC_STATE_ERROR;
1125 /* Process Unlocked */
1126 __HAL_UNLOCK(hrtc);
1128 return HAL_ERROR;
1132 /* Enable the write protection for RTC registers */
1133 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1135 hrtc->State = HAL_RTC_STATE_READY ;
1137 /* Process Unlocked */
1138 __HAL_UNLOCK(hrtc);
1140 return HAL_OK;
1145 * @brief Get RTC current date.
1146 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1147 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1148 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1149 * @param hrtc RTC handle
1150 * @param sDate Pointer to Date structure
1151 * @param Format Specifies the format of the entered parameters.
1152 * This parameter can be one of the following values:
1153 * @arg RTC_FORMAT_BIN: Binary data format
1154 * @arg RTC_FORMAT_BCD: BCD data format
1155 * @retval HAL status
1157 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1159 uint32_t datetmpreg;
1161 UNUSED(hrtc);
1163 /* Check the parameters */
1164 assert_param(IS_RTC_FORMAT(Format));
1166 /* Get the DR register */
1167 datetmpreg = (uint32_t)(READ_REG(RTC->DR) & RTC_DR_RESERVED_MASK);
1169 /* Fill the structure fields with the read parameters */
1170 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1171 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1172 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1173 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
1175 /* Check the input parameters format */
1176 if (Format == RTC_FORMAT_BIN)
1178 /* Convert the date structure parameters to Binary format */
1179 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1180 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1181 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1183 return HAL_OK;
1187 * @}
1190 /** @addtogroup RTC_Exported_Functions_Group3
1191 * @brief RTC Alarm functions
1193 @verbatim
1194 ===============================================================================
1195 ##### RTC Alarm functions #####
1196 ===============================================================================
1198 [..] This section provides functions allowing to configure Alarm feature
1200 @endverbatim
1201 * @{
1204 * @brief Set the specified RTC Alarm.
1205 * @param hrtc RTC handle
1206 * @param sAlarm Pointer to Alarm structure
1207 * @param Format Specifies the format of the entered parameters.
1208 * This parameter can be one of the following values:
1209 * @arg RTC_FORMAT_BIN: Binary data format
1210 * @arg RTC_FORMAT_BCD: BCD data format
1211 * @retval HAL status
1213 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1215 uint32_t tickstart;
1216 uint32_t tmpreg;
1217 uint32_t subsecondtmpreg;
1219 /* Check the parameters */
1220 assert_param(IS_RTC_FORMAT(Format));
1221 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1222 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1223 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1224 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1225 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1227 /* Process Locked */
1228 __HAL_LOCK(hrtc);
1230 hrtc->State = HAL_RTC_STATE_BUSY;
1232 if (Format == RTC_FORMAT_BIN)
1234 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1236 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1237 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1239 else
1241 sAlarm->AlarmTime.TimeFormat = 0x00U;
1242 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1244 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1245 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1247 #ifdef USE_FULL_ASSERT
1248 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1250 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1252 else
1254 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1256 #endif /* USE_FULL_ASSERT*/
1257 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1258 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1259 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1260 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1261 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1262 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1263 ((uint32_t)sAlarm->AlarmMask));
1265 else /* format BCD */
1267 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1269 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1270 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1272 else
1274 sAlarm->AlarmTime.TimeFormat = 0x00U;
1275 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1278 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1279 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1281 #ifdef USE_FULL_ASSERT
1282 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1284 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1286 else
1288 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1291 #endif /* USE_FULL_ASSERT */
1292 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1293 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1294 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1295 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1296 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1297 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1298 ((uint32_t)sAlarm->AlarmMask));
1301 /* Configure the Alarm A or Alarm B Sub Second registers */
1302 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1304 /* Disable the write protection for RTC registers */
1305 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1307 /* Configure the Alarm register */
1308 if (sAlarm->Alarm == RTC_ALARM_A)
1310 /* Disable the Alarm A interrupt */
1311 /* In case of interrupt mode is used, the interrupt source must disabled */
1312 CLEAR_BIT(RTC->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
1314 /* Clear flag alarm A */
1315 WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1317 tickstart = HAL_GetTick();
1318 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1319 while (READ_BIT(RTC->ICSR, RTC_ICSR_ALRAWF) == 0U)
1321 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1323 /* Enable the write protection for RTC registers */
1324 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1326 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1328 /* Process Unlocked */
1329 __HAL_UNLOCK(hrtc);
1331 return HAL_TIMEOUT;
1335 WRITE_REG(RTC->ALRMAR, tmpreg);
1336 /* Configure the Alarm A Sub Second register */
1337 WRITE_REG(RTC->ALRMASSR, subsecondtmpreg);
1338 /* Configure the Alarm state: Enable Alarm */
1339 SET_BIT(RTC->CR, RTC_CR_ALRAE);
1341 else
1343 /* Disable the Alarm B interrupt */
1344 /* In case of interrupt mode is used, the interrupt source must disabled */
1345 CLEAR_BIT(RTC->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
1347 /* Clear flag alarm B */
1348 WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1350 tickstart = HAL_GetTick();
1351 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1352 while (READ_BIT(RTC->ICSR, RTC_ICSR_ALRBWF) == 0U)
1354 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1356 /* Enable the write protection for RTC registers */
1357 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1359 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1361 /* Process Unlocked */
1362 __HAL_UNLOCK(hrtc);
1364 return HAL_TIMEOUT;
1368 WRITE_REG(RTC->ALRMBR, tmpreg);
1369 /* Configure the Alarm B Sub Second register */
1370 WRITE_REG(RTC->ALRMBSSR, subsecondtmpreg);
1371 /* Configure the Alarm state: Enable Alarm */
1372 SET_BIT(RTC->CR, RTC_CR_ALRBE);
1375 /* Enable the write protection for RTC registers */
1376 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1378 /* Change RTC state */
1379 hrtc->State = HAL_RTC_STATE_READY;
1381 /* Process Unlocked */
1382 __HAL_UNLOCK(hrtc);
1384 return HAL_OK;
1388 * @brief Set the specified RTC Alarm with Interrupt.
1389 * @note The Alarm register can only be written when the corresponding Alarm
1390 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
1391 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1392 * @param hrtc RTC handle
1393 * @param sAlarm Pointer to Alarm structure
1394 * @param Format Specifies the format of the entered parameters.
1395 * This parameter can be one of the following values:
1396 * @arg RTC_FORMAT_BIN: Binary data format
1397 * @arg RTC_FORMAT_BCD: BCD data format
1398 * @retval HAL status
1400 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1402 uint32_t tickstart;
1403 uint32_t tmpreg;
1404 uint32_t subsecondtmpreg;
1406 /* Check the parameters */
1407 assert_param(IS_RTC_FORMAT(Format));
1408 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1409 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1410 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1411 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1412 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1414 /* Process Locked */
1415 __HAL_LOCK(hrtc);
1417 hrtc->State = HAL_RTC_STATE_BUSY;
1419 if (Format == RTC_FORMAT_BIN)
1421 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1423 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1424 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1426 else
1428 sAlarm->AlarmTime.TimeFormat = 0x00U;
1429 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1431 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1432 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1434 #ifdef USE_FULL_ASSERT
1435 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1437 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1439 else
1441 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1443 #endif /* USE_FULL_ASSERT */
1444 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1445 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1446 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1447 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1448 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1449 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1450 ((uint32_t)sAlarm->AlarmMask));
1452 else /* Format BCD */
1454 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1456 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1457 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1459 else
1461 sAlarm->AlarmTime.TimeFormat = 0x00U;
1462 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1465 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1466 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1468 #ifdef USE_FULL_ASSERT
1469 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1471 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1473 else
1475 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1478 #endif /* USE_FULL_ASSERT */
1479 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1480 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1481 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1482 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1483 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1484 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1485 ((uint32_t)sAlarm->AlarmMask));
1487 /* Configure the Alarm A or Alarm B Sub Second registers */
1488 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1490 /* Disable the write protection for RTC registers */
1491 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1493 /* Configure the Alarm register */
1494 if (sAlarm->Alarm == RTC_ALARM_A)
1496 /* Disable the Alarm A interrupt */
1497 CLEAR_BIT(RTC->CR, RTC_CR_ALRAIE);
1498 /* Clear flag alarm A */
1499 WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1500 __HAL_RTC_ALARM_EXTI_CLEAR_IT();
1502 tickstart = HAL_GetTick();
1503 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1504 while (READ_BIT(RTC->ICSR, RTC_ICSR_ALRAWF) == 0U)
1506 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1508 /* Enable the write protection for RTC registers */
1509 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1511 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1513 /* Process Unlocked */
1514 __HAL_UNLOCK(hrtc);
1516 return HAL_TIMEOUT;
1520 WRITE_REG(RTC->ALRMAR, tmpreg);
1521 /* Configure the Alarm A Sub Second register */
1522 WRITE_REG(RTC->ALRMASSR, subsecondtmpreg);
1523 /* Configure the Alarm interrupt : Enable Alarm */
1524 SET_BIT(RTC->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
1526 else
1528 /* Disable the Alarm B interrupt */
1529 CLEAR_BIT(RTC->CR, RTC_CR_ALRBIE);
1530 /* Clear flag alarm B */
1531 WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1532 __HAL_RTC_ALARM_EXTI_CLEAR_IT();
1534 tickstart = HAL_GetTick();
1535 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1536 while (READ_BIT(RTC->ICSR, RTC_ICSR_ALRBWF) == 0U)
1538 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1540 /* Enable the write protection for RTC registers */
1541 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1543 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1545 /* Process Unlocked */
1546 __HAL_UNLOCK(hrtc);
1548 return HAL_TIMEOUT;
1552 WRITE_REG(RTC->ALRMBR, tmpreg);
1553 /* Configure the Alarm B Sub Second register */
1554 WRITE_REG(RTC->ALRMBSSR, subsecondtmpreg);
1555 /* Configure the Alarm B interrupt : Enable Alarm */
1556 SET_BIT(RTC->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
1559 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1560 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1561 __HAL_RTC_ALARM_EXTI_RISING_IT();
1563 /* Enable the write protection for RTC registers */
1564 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1566 hrtc->State = HAL_RTC_STATE_READY;
1568 /* Process Unlocked */
1569 __HAL_UNLOCK(hrtc);
1571 return HAL_OK;
1575 * @brief Deactivate the specified RTC Alarm.
1576 * @param hrtc RTC handle
1577 * @param Alarm Specifies the Alarm.
1578 * This parameter can be one of the following values:
1579 * @arg RTC_ALARM_A: AlarmA
1580 * @arg RTC_ALARM_B: AlarmB
1581 * @retval HAL status
1583 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1585 uint32_t tickstart;
1587 /* Check the parameters */
1588 assert_param(IS_RTC_ALARM(Alarm));
1590 /* Process Locked */
1591 __HAL_LOCK(hrtc);
1593 hrtc->State = HAL_RTC_STATE_BUSY;
1595 /* Disable the write protection for RTC registers */
1596 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1598 if (Alarm == RTC_ALARM_A)
1600 /* AlarmA */
1601 /* In case of interrupt mode is used, the interrupt source must disabled */
1602 CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1603 __HAL_RTC_ALARM_EXTI_CLEAR_IT();
1605 tickstart = HAL_GetTick();
1607 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1608 while (READ_BIT(RTC->ICSR, RTC_ICSR_ALRAWF) == 0U)
1610 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1612 /* Enable the write protection for RTC registers */
1613 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1615 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1617 /* Process Unlocked */
1618 __HAL_UNLOCK(hrtc);
1620 return HAL_TIMEOUT;
1624 else
1626 /* AlarmB */
1627 /* In case of interrupt mode is used, the interrupt source must disabled */
1628 CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1629 __HAL_RTC_ALARM_EXTI_CLEAR_IT();
1631 tickstart = HAL_GetTick();
1633 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1634 while (READ_BIT(RTC->ICSR, RTC_ICSR_ALRBWF) == 0U)
1636 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1638 /* Enable the write protection for RTC registers */
1639 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1641 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1643 /* Process Unlocked */
1644 __HAL_UNLOCK(hrtc);
1646 return HAL_TIMEOUT;
1650 /* Enable the write protection for RTC registers */
1651 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1653 hrtc->State = HAL_RTC_STATE_READY;
1655 /* Process Unlocked */
1656 __HAL_UNLOCK(hrtc);
1658 return HAL_OK;
1662 * @brief Get the RTC Alarm value and masks.
1663 * @param hrtc RTC handle
1664 * @param sAlarm Pointer to Date structure
1665 * @param Alarm Specifies the Alarm.
1666 * This parameter can be one of the following values:
1667 * @arg RTC_ALARM_A: AlarmA
1668 * @arg RTC_ALARM_B: AlarmB
1669 * @param Format Specifies the format of the entered parameters.
1670 * This parameter can be one of the following values:
1671 * @arg RTC_FORMAT_BIN: Binary data format
1672 * @arg RTC_FORMAT_BCD: BCD data format
1673 * @retval HAL status
1675 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1677 uint32_t tmpreg, subsecondtmpreg;
1679 UNUSED(hrtc);
1681 /* Check the parameters */
1682 assert_param(IS_RTC_FORMAT(Format));
1683 assert_param(IS_RTC_ALARM(Alarm));
1685 if (Alarm == RTC_ALARM_A)
1687 /* AlarmA */
1688 sAlarm->Alarm = RTC_ALARM_A;
1690 tmpreg = READ_REG(RTC->ALRMAR);
1691 subsecondtmpreg = (uint32_t)(READ_REG(RTC->ALRMASSR) & RTC_ALRMASSR_SS);
1693 /* Fill the structure with the read parameters */
1694 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1695 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1696 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1697 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
1698 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1699 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1700 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1701 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1703 else
1705 sAlarm->Alarm = RTC_ALARM_B;
1707 tmpreg = READ_REG(RTC->ALRMBR);
1708 subsecondtmpreg = (uint32_t)(READ_REG(RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
1710 /* Fill the structure with the read parameters */
1711 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1712 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1713 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1714 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1715 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1716 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1717 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1718 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1721 if (Format == RTC_FORMAT_BIN)
1723 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1724 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1725 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1726 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1729 return HAL_OK;
1733 * @brief Handle Alarm interrupt request.
1734 * @param hrtc RTC handle
1735 * @retval None
1737 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1739 /* Get interrupt status */
1740 uint32_t tmp = READ_REG(RTC->MISR);
1742 if ((tmp & RTC_MISR_ALRAMF) != 0U)
1744 /* Clear the AlarmA interrupt pending bit */
1745 WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1746 __HAL_RTC_ALARM_EXTI_CLEAR_IT();
1748 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1749 /* Call Compare Match registered Callback */
1750 hrtc->AlarmAEventCallback(hrtc);
1751 #else
1752 HAL_RTC_AlarmAEventCallback(hrtc);
1753 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1756 if ((tmp & RTC_MISR_ALRBMF) != 0U)
1758 /* Clear the AlarmB interrupt pending bit */
1759 WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1760 __HAL_RTC_ALARM_EXTI_CLEAR_IT();
1762 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1763 /* Call Compare Match registered Callback */
1764 hrtc->AlarmBEventCallback(hrtc);
1765 #else
1766 HAL_RTCEx_AlarmBEventCallback(hrtc);
1767 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1770 /* Change RTC state */
1771 hrtc->State = HAL_RTC_STATE_READY;
1775 * @brief Alarm A callback.
1776 * @param hrtc RTC handle
1777 * @retval None
1779 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1781 /* Prevent unused argument(s) compilation warning */
1782 UNUSED(hrtc);
1784 /* NOTE : This function should not be modified, when the callback is needed,
1785 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1790 * @brief Handle AlarmA Polling request.
1791 * @param hrtc RTC handle
1792 * @param Timeout Timeout duration
1793 * @retval HAL status
1795 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1798 uint32_t tickstart = HAL_GetTick();
1800 while (READ_BIT(RTC->SR, RTC_SR_ALRAF) == 0U)
1802 if (Timeout != HAL_MAX_DELAY)
1804 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1806 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1807 return HAL_TIMEOUT;
1812 /* Clear the Alarm interrupt pending bit */
1813 WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1815 /* Change RTC state */
1816 hrtc->State = HAL_RTC_STATE_READY;
1818 return HAL_OK;
1822 * @}
1825 /** @addtogroup RTC_Exported_Functions_Group4
1826 * @brief Peripheral Control functions
1828 @verbatim
1829 ===============================================================================
1830 ##### Peripheral Control functions #####
1831 ===============================================================================
1832 [..]
1833 This subsection provides functions allowing to
1834 (+) Wait for RTC Time and Date Synchronization
1836 @endverbatim
1837 * @{
1841 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1842 * synchronized with RTC APB clock.
1843 * @note The RTC Resynchronization mode is write protected, use the
1844 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1845 * @note To read the calendar through the shadow registers after Calendar
1846 * initialization, calendar update or after wakeup from low power modes
1847 * the software must first clear the RSF flag.
1848 * The software must then wait until it is set again before reading
1849 * the calendar, which means that the calendar registers have been
1850 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1851 * @param hrtc RTC handle
1852 * @retval HAL status
1854 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
1856 uint32_t tickstart;
1858 UNUSED(hrtc);
1860 /* Clear RSF flag */
1861 SET_BIT(RTC->ICSR, RTC_RSF_MASK);
1863 tickstart = HAL_GetTick();
1865 /* Wait the registers to be synchronised */
1866 while (READ_BIT(RTC->ICSR, RTC_ICSR_RSF) == 0U)
1868 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1870 return HAL_TIMEOUT;
1874 return HAL_OK;
1878 * @}
1881 /** @addtogroup RTC_Exported_Functions_Group5
1882 * @brief Peripheral State functions
1884 @verbatim
1885 ===============================================================================
1886 ##### Peripheral State functions #####
1887 ===============================================================================
1888 [..]
1889 This subsection provides functions allowing to
1890 (+) Get RTC state
1892 @endverbatim
1893 * @{
1896 * @brief Return the RTC handle state.
1897 * @param hrtc RTC handle
1898 * @retval HAL state
1900 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
1902 /* Return RTC handle state */
1903 return hrtc->State;
1907 * @}
1910 * @}
1913 /** @addtogroup RTC_Private_Functions
1914 * @{
1917 * @brief Enter the RTC Initialization mode.
1918 * @note The RTC Initialization mode is write protected, use the
1919 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1920 * @param hrtc RTC handle
1921 * @retval HAL status
1923 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
1925 uint32_t tickstart;
1927 UNUSED(hrtc);
1929 /* Check if the Initialization mode is set */
1930 if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
1932 /* Set the Initialization mode */
1933 SET_BIT(RTC->ICSR, RTC_ICSR_INIT);
1935 tickstart = HAL_GetTick();
1936 /* Wait till RTC is in INIT state and if Time out is reached exit */
1937 while (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
1939 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1941 return HAL_TIMEOUT;
1946 return HAL_OK;
1951 * @brief Convert a 2 digit decimal to BCD format.
1952 * @param Value Byte to be converted
1953 * @retval Converted byte
1955 uint8_t RTC_ByteToBcd2(uint8_t Value)
1957 uint32_t bcdhigh = 0U;
1958 uint8_t tmp_Value = Value;
1960 while (tmp_Value >= 10U)
1962 bcdhigh++;
1963 tmp_Value -= 10U;
1966 return ((uint8_t)(bcdhigh << 4U) | tmp_Value);
1970 * @brief Convert from 2 digit BCD to Binary.
1971 * @param Value BCD value to be converted
1972 * @retval Converted word
1974 uint8_t RTC_Bcd2ToByte(uint8_t Value)
1976 uint32_t tmp;
1977 tmp = (((uint32_t)Value & 0xF0U) >> 4) * 10U;
1978 return (uint8_t)(tmp + ((uint32_t)Value & 0x0FU));
1982 * @}
1985 #endif /* HAL_RTC_MODULE_ENABLED */
1987 * @}
1991 * @}
1994 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/