Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_rtc.c
blobb158d38e3c3d6db6f6d1f4a818607360777de8ba
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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
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, RTC backup data
41 registers and backup SRAM) is protected against possible unwanted write
42 accesses.
44 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
45 (#) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
46 PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
47 (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro.
49 ##### How to use RTC Driver #####
50 ===================================================================
51 [..]
52 (+) Enable the RTC domain access (see description in the section above).
53 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
54 format using the HAL_RTC_Init() function.
56 *** Time and Date configuration ***
57 ===================================
58 [..]
59 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
60 and HAL_RTC_SetDate() functions.
61 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
63 *** Alarm configuration ***
64 ===========================
65 [..]
66 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
67 You can also configure the RTC Alarm with interrupt mode using the
68 HAL_RTC_SetAlarm_IT() function.
69 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
71 ##### RTC and low power modes #####
72 ===================================================================
73 [..] The MCU can be woken up from a low power mode by an RTC alternate
74 function.
75 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
76 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
77 These RTC alternate functions can wake up the system from the Stop and
78 Standby low power modes.
79 [..] The system can also wake up from low power modes without depending
80 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
81 or the RTC wakeup events.
82 [..] The RTC provides a programmable time base for waking up from the
83 Stop or Standby mode at regular intervals.
84 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
85 is LSE or LSI.
87 *** Callback registration ***
88 =============================================
89 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
90 not defined, the callback registration feature is not available and all callbacks
91 are set to the corresponding weak functions. This is the recommended configuration
92 in order to optimize memory/code consumption footprint/performances.
94 The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
95 allows the user to configure dynamically the driver callbacks.
96 Use Function @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
98 Function @ref HAL_RTC_RegisterCallback() allows to register following callbacks:
99 (+) AlarmAEventCallback : RTC Alarm A Event callback.
100 (+) AlarmBEventCallback : RTC Alarm B Event callback.
101 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
102 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
103 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
104 (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
105 (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
106 (+) MspInitCallback : RTC MspInit callback.
107 (+) MspDeInitCallback : RTC MspDeInit callback.
108 This function takes as parameters the HAL peripheral handle, the Callback ID
109 and a pointer to the user callback function.
111 Use function @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
112 weak function.
113 @ref HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
114 and the Callback ID.
115 This function allows to reset following callbacks:
116 (+) AlarmAEventCallback : RTC Alarm A Event callback.
117 (+) AlarmBEventCallback : RTC Alarm B Event callback.
118 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
119 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
120 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
121 (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
122 (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
123 (+) MspInitCallback : RTC MspInit callback.
124 (+) MspDeInitCallback : RTC MspDeInit callback.
126 By default, after the @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
127 all callbacks are set to the corresponding weak functions :
128 examples @ref AlarmAEventCallback(), @ref WakeUpTimerEventCallback().
129 Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
130 in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
131 (not registered beforehand).
132 If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit()
133 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
135 Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
136 Exception done MspInit/MspDeInit that can be registered/unregistered
137 in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
138 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
139 In that case first register the MspInit/MspDeInit user callbacks
140 using @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
141 or @ref HAL_RTC_Init() function.
143 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
144 not defined, the callback registration feature is not available and all callbacks
145 are set to the corresponding weak functions.
146 @endverbatim
148 ******************************************************************************
149 * @attention
151 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
152 * All rights reserved.</center></h2>
154 * This software component is licensed by ST under BSD 3-Clause license,
155 * the "License"; You may not use this file except in compliance with the
156 * License. You may obtain a copy of the License at:
157 * opensource.org/licenses/BSD-3-Clause
159 ******************************************************************************
162 /* Includes ------------------------------------------------------------------*/
163 #include "stm32h7xx_hal.h"
165 /** @addtogroup STM32H7xx_HAL_Driver
166 * @{
170 /** @addtogroup RTC
171 * @brief RTC HAL module driver
172 * @{
175 #ifdef HAL_RTC_MODULE_ENABLED
177 /* Private typedef -----------------------------------------------------------*/
178 /* Private define ------------------------------------------------------------*/
179 /* Private macro -------------------------------------------------------------*/
180 /* Private variables ---------------------------------------------------------*/
181 /* Private function prototypes -----------------------------------------------*/
182 /* Exported functions --------------------------------------------------------*/
184 /** @addtogroup RTC_Exported_Functions
185 * @{
188 /** @addtogroup RTC_Exported_Functions_Group1
189 * @brief Initialization and Configuration functions
191 @verbatim
192 ===============================================================================
193 ##### Initialization and de-initialization functions #####
194 ===============================================================================
195 [..] This section provides functions allowing to initialize and configure the
196 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
197 RTC registers Write protection, enter and exit the RTC initialization mode,
198 RTC registers synchronization check and reference clock detection enable.
199 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
200 It is split into 2 programmable prescalers to minimize power consumption.
201 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
202 (++) When both prescalers are used, it is recommended to configure the
203 asynchronous prescaler to a high value to minimize power consumption.
204 (#) All RTC registers are Write protected. Writing to the RTC registers
205 is enabled by writing a key into the Write Protection register, RTC_WPR.
206 (#) To configure the RTC Calendar, user application should enter
207 initialization mode. In this mode, the calendar counter is stopped
208 and its value can be updated. When the initialization sequence is
209 complete, the calendar restarts counting after 4 RTCCLK cycles.
210 (#) To read the calendar through the shadow registers after Calendar
211 initialization, calendar update or after wakeup from low power modes
212 the software must first clear the RSF flag. The software must then
213 wait until it is set again before reading the calendar, which means
214 that the calendar registers have been correctly copied into the
215 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
216 implements the above software sequence (RSF clear and RSF check).
218 @endverbatim
219 * @{
223 * @brief Initialize the RTC peripheral
224 * @param hrtc RTC handle
225 * @retval HAL status
227 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
229 HAL_StatusTypeDef status = HAL_ERROR;
231 /* Check RTC handler */
232 if(hrtc != NULL)
234 /* Check the parameters */
235 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
236 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
237 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
238 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
239 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
240 assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
241 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
242 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
243 #if defined(RTC_CR_TAMPALRM_PU)
244 assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
245 #endif /* RTC_CR_TAMPALRM_PU */
247 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
248 if(hrtc->State == HAL_RTC_STATE_RESET)
250 /* Allocate lock resource and initialize it */
251 hrtc->Lock = HAL_UNLOCKED;
253 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
254 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
255 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
256 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
257 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
258 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
259 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
261 #if defined(TAMP_CR1_ITAMP1E)
262 hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback;
263 #endif /* TAMP_CR1_ITAMP1E */
264 #if defined(TAMP_CR1_ITAMP2E)
265 hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback;
266 #endif /* TAMP_CR1_ITAMP2E */
267 #if defined(TAMP_CR1_ITAMP3E)
268 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback;
269 #endif /* TAMP_CR1_ITAMP3E */
270 #if defined(TAMP_CR1_ITAMP4E)
271 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback;
272 #endif /* TAMP_CR1_ITAMP4E */
273 #if defined(TAMP_CR1_ITAMP5E)
274 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback;
275 #endif /* TAMP_CR1_ITAMP5E */
276 #if defined(TAMP_CR1_ITAMP6E)
277 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback;
278 #endif /* TAMP_CR1_ITAMP6E */
279 #if defined(TAMP_CR1_ITAMP8E)
280 hrtc->InternalTamper8EventCallback = HAL_RTCEx_InternalTamper8EventCallback;
281 #endif /* TAMP_CR1_ITAMP8E */
284 if(hrtc->MspInitCallback == NULL)
286 hrtc->MspInitCallback = HAL_RTC_MspInit;
288 /* Init the low level hardware */
289 hrtc->MspInitCallback(hrtc);
291 if(hrtc->MspDeInitCallback == NULL)
293 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
296 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
297 if(hrtc->State == HAL_RTC_STATE_RESET)
299 /* Allocate lock resource and initialize it */
300 hrtc->Lock = HAL_UNLOCKED;
302 /* Initialize RTC MSP */
303 HAL_RTC_MspInit(hrtc);
305 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
307 /* Set RTC state */
308 hrtc->State = HAL_RTC_STATE_BUSY;
310 /* Disable the write protection for RTC registers */
311 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
313 /* Enter Initialization mode */
314 status = RTC_EnterInitMode(hrtc);
315 if (status == HAL_OK)
317 #if defined(RTC_CR_TAMPOE)
318 /* Clear RTC_CR FMT, OSEL, POL and TAMPOE Bits */
319 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE);
320 #else /* RTC_CR_TAMPOE */
321 /* Clear RTC_CR FMT, OSEL and POL Bits */
322 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL);
323 #endif /* RTC_CR_TAMPOE */
325 /* Set RTC_CR register */
326 hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
328 /* Configure the RTC PRER */
329 hrtc->Instance->PRER = (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos) | (hrtc->Init.SynchPrediv << RTC_PRER_PREDIV_S_Pos);
331 /* Exit Initialization mode */
332 status = RTC_ExitInitMode(hrtc);
334 if(status == HAL_OK)
336 #if defined(RTC_CR_TAMPALRM_PU) && defined(RTC_CR_TAMPALRM_TYPE) && defined(RTC_CR_OUT2EN)
337 hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
338 hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
339 #endif /* RTC_CR_TAMPALRM_TYPE && RTC_CR_OUT2EN && RTC_CR_TAMPALRM_PU */
341 #if defined(RTC_OR_ALARMOUTTYPE) && defined(RTC_OR_OUT_RMP)
342 hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
343 hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
344 #endif /* ALARMOUTTYPE && RTC_OR_OUT_RMP */
347 /* Enable the write protection for RTC registers */
348 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
349 if (status == HAL_OK)
351 /* Set RTC state */
352 hrtc->State = HAL_RTC_STATE_READY;
356 /* return status */
357 return status;
361 * @brief DeInitialize the RTC peripheral.
362 * @note This function doesn't reset the RTC Backup Data registers.
363 * @param hrtc RTC handle
364 * @retval HAL status
366 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
368 HAL_StatusTypeDef status = HAL_ERROR;
369 uint32_t tickstart;
371 /* Check RTC handler */
372 if(hrtc != NULL)
374 /* Check the parameters */
375 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
377 /* Set RTC state */
378 hrtc->State = HAL_RTC_STATE_BUSY;
380 /* Disable the write protection for RTC registers */
381 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
382 /* Enter Initialization mode */
383 status = RTC_EnterInitMode(hrtc);
384 if (status == HAL_OK)
386 /* Reset TR, DR and CR registers */
387 hrtc->Instance->TR = 0x00000000U;
388 hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
389 /* Reset All CR bits except CR[2:0] */
390 hrtc->Instance->CR &= RTC_CR_WUCKSEL;
392 tickstart = HAL_GetTick();
394 /* Wait till WUTWF flag is set and if Time out is reached exit */
395 #if defined(RTC_ICSR_WUTWF)
396 while (((hrtc->Instance->ICSR) & RTC_ICSR_WUTWF) == 0U)
397 #endif /* RTC_ICSR_WUTWF */
398 #if defined(RTC_ISR_WUTWF)
399 while (((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == 0U)
400 #endif /* RTC_ISR_WUTWF */
402 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
404 /* Enable the write protection for RTC registers */
405 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
407 /* Set RTC state */
408 hrtc->State = HAL_RTC_STATE_TIMEOUT;
413 /* Exit initialization mode */
414 status = RTC_ExitInitMode(hrtc);
415 if (status == HAL_OK)
417 /* Reset all RTC CR register bits */
418 hrtc->Instance->CR &= 0x00000000U;
420 /* Reset other RTC registers */
421 hrtc->Instance->WUTR = RTC_WUTR_WUT;
422 hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
423 hrtc->Instance->ALRMAR = 0x00000000U;
424 hrtc->Instance->ALRMBR = 0x00000000U;
425 hrtc->Instance->SHIFTR = 0x00000000U;
426 hrtc->Instance->CALR = 0x00000000U;
427 hrtc->Instance->ALRMASSR = 0x00000000U;
428 hrtc->Instance->ALRMBSSR = 0x00000000U;
430 #if defined(RTC_ISR_INIT)
431 /* Reset Tamper configuration register */
432 hrtc->Instance->TAMPCR = 0x00000000U;
434 /* Reset Option register */
435 hrtc->Instance->OR = 0x00000000U;
436 #endif /* RTC_ISR_INIT */
440 if(status == HAL_OK)
442 #if defined(TAMP_CR1_TAMP1E)
443 /* Reset TAMP registers */
444 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR1 = 0xFFFF0000U;
445 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR2 = 0x00000000U;
446 #endif /* TAMP_CR1_TAMP1E */
448 /* Enable the write protection for RTC registers */
449 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
451 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
452 if(hrtc->MspDeInitCallback == NULL)
454 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
457 /* DeInit the low level hardware: CLOCK, NVIC.*/
458 hrtc->MspDeInitCallback(hrtc);
459 #else
460 /* De-Initialize RTC MSP */
461 HAL_RTC_MspDeInit(hrtc);
462 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
464 hrtc->State = HAL_RTC_STATE_RESET;
466 /* Release Lock */
467 __HAL_UNLOCK(hrtc);
471 /* return status */
472 return status;
475 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
477 * @brief Register a User RTC Callback
478 * To be used instead of the weak predefined callback
479 * @param hrtc RTC handle
480 * @param CallbackID ID of the callback to be registered
481 * This parameter can be one of the following values:
482 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
483 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
484 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
485 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
486 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
487 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
488 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
489 * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
490 * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
491 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
492 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
493 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
494 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
495 * @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID
496 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
497 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
498 * @param pCallback pointer to the Callback function
499 * @retval HAL status
501 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
503 HAL_StatusTypeDef status = HAL_OK;
505 if(pCallback == NULL)
507 return HAL_ERROR;
510 /* Process locked */
511 __HAL_LOCK(hrtc);
513 if(HAL_RTC_STATE_READY == hrtc->State)
515 switch (CallbackID)
517 case HAL_RTC_ALARM_A_EVENT_CB_ID :
518 hrtc->AlarmAEventCallback = pCallback;
519 break;
521 case HAL_RTC_ALARM_B_EVENT_CB_ID :
522 hrtc->AlarmBEventCallback = pCallback;
523 break;
525 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
526 hrtc->TimeStampEventCallback = pCallback;
527 break;
529 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
530 hrtc->WakeUpTimerEventCallback = pCallback;
531 break;
533 case HAL_RTC_TAMPER1_EVENT_CB_ID :
534 hrtc->Tamper1EventCallback = pCallback;
535 break;
537 case HAL_RTC_TAMPER2_EVENT_CB_ID :
538 hrtc->Tamper2EventCallback = pCallback;
539 break;
541 case HAL_RTC_TAMPER3_EVENT_CB_ID :
542 hrtc->Tamper3EventCallback = pCallback;
543 break;
545 #if defined(TAMP_CR1_ITAMP1E)
546 case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
547 hrtc->InternalTamper1EventCallback = pCallback;
548 break;
549 #endif /* TAMP_CR1_ITAMP1E */
551 #if defined(TAMP_CR1_ITAMP2E)
552 case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
553 hrtc->InternalTamper2EventCallback = pCallback;
554 break;
555 #endif /* TAMP_CR1_ITAMP2E */
557 #if defined(TAMP_CR1_ITAMP3E)
558 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
559 hrtc->InternalTamper3EventCallback = pCallback;
560 break;
561 #endif /* TAMP_CR1_ITAMP3E */
563 #if defined(TAMP_CR1_ITAMP4E)
564 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
565 hrtc->InternalTamper4EventCallback = pCallback;
566 break;
567 #endif /* TAMP_CR1_ITAMP4E */
569 #if defined(TAMP_CR1_ITAMP5E)
570 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
571 hrtc->InternalTamper5EventCallback = pCallback;
572 break;
573 #endif /* TAMP_CR1_ITAMP5E */
575 #if defined(TAMP_CR1_ITAMP6E)
576 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
577 hrtc->InternalTamper6EventCallback = pCallback;
578 break;
579 #endif /* TAMP_CR1_ITAMP6E */
581 #if defined(TAMP_CR1_ITAMP8E)
582 case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
583 hrtc->InternalTamper8EventCallback = pCallback;
584 break;
585 #endif /* TAMP_CR1_ITAMP8E */
587 case HAL_RTC_MSPINIT_CB_ID :
588 hrtc->MspInitCallback = pCallback;
589 break;
591 case HAL_RTC_MSPDEINIT_CB_ID :
592 hrtc->MspDeInitCallback = pCallback;
593 break;
595 default :
596 /* Return error status */
597 status = HAL_ERROR;
598 break;
601 else if(HAL_RTC_STATE_RESET == hrtc->State)
603 switch (CallbackID)
605 case HAL_RTC_MSPINIT_CB_ID :
606 hrtc->MspInitCallback = pCallback;
607 break;
609 case HAL_RTC_MSPDEINIT_CB_ID :
610 hrtc->MspDeInitCallback = pCallback;
611 break;
613 default :
614 /* Return error status */
615 status = HAL_ERROR;
616 break;
619 else
621 /* Return error status */
622 status = HAL_ERROR;
625 /* Release Lock */
626 __HAL_UNLOCK(hrtc);
628 return status;
632 * @brief Unregister an RTC Callback
633 * RTC callabck is redirected to the weak predefined callback
634 * @param hrtc RTC handle
635 * @param CallbackID ID of the callback to be unregistered
636 * This parameter can be one of the following values:
637 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
638 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
639 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
640 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
641 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
642 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
643 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
644 * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
645 * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
646 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
647 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
648 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
649 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
650 * @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID
651 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
652 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
653 * @retval HAL status
655 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
657 HAL_StatusTypeDef status = HAL_OK;
659 /* Process locked */
660 __HAL_LOCK(hrtc);
662 if(HAL_RTC_STATE_READY == hrtc->State)
664 switch (CallbackID)
666 case HAL_RTC_ALARM_A_EVENT_CB_ID :
667 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
668 break;
670 case HAL_RTC_ALARM_B_EVENT_CB_ID :
671 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
672 break;
674 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
675 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
676 break;
678 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
679 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
680 break;
682 case HAL_RTC_TAMPER1_EVENT_CB_ID :
683 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
684 break;
686 case HAL_RTC_TAMPER2_EVENT_CB_ID :
687 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
688 break;
690 case HAL_RTC_TAMPER3_EVENT_CB_ID :
691 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
692 break;
694 #if defined(TAMP_CR1_ITAMP1E)
695 case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
696 hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback;
697 break;
698 #endif /* TAMP_CR1_ITAMP1E */
700 #if defined(TAMP_CR1_ITAMP2E)
701 case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
702 hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback;
703 break;
704 #endif /* TAMP_CR1_ITAMP2E */
706 #if defined(TAMP_CR1_ITAMP3E)
707 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
708 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback;
709 break;
710 #endif /* TAMP_CR1_ITAMP3E */
712 #if defined(TAMP_CR1_ITAMP4E)
713 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
714 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback;
715 break;
716 #endif /* TAMP_CR1_ITAMP4E */
718 #if defined(TAMP_CR1_ITAMP5E)
719 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
720 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback;
721 break;
722 #endif /* TAMP_CR1_ITAMP5E */
724 #if defined(TAMP_CR1_ITAMP6E)
725 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
726 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback;
727 break;
728 #endif /* TAMP_CR1_ITAMP6E */
730 #if defined(TAMP_CR1_ITAMP8E)
731 case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
732 hrtc->InternalTamper8EventCallback = HAL_RTCEx_InternalTamper8EventCallback;
733 break;
734 #endif /* TAMP_CR1_ITAMP8E */
736 case HAL_RTC_MSPINIT_CB_ID :
737 hrtc->MspInitCallback = HAL_RTC_MspInit;
738 break;
740 case HAL_RTC_MSPDEINIT_CB_ID :
741 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
742 break;
744 default :
745 /* Return error status */
746 status = HAL_ERROR;
747 break;
750 else if(HAL_RTC_STATE_RESET == hrtc->State)
752 switch (CallbackID)
754 case HAL_RTC_MSPINIT_CB_ID :
755 hrtc->MspInitCallback = HAL_RTC_MspInit;
756 break;
758 case HAL_RTC_MSPDEINIT_CB_ID :
759 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
760 break;
762 default :
763 /* Return error status */
764 status = HAL_ERROR;
765 break;
768 else
770 /* Return error status */
771 status = HAL_ERROR;
774 /* Release Lock */
775 __HAL_UNLOCK(hrtc);
777 return status;
779 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
782 * @brief Initialize the RTC MSP.
783 * @param hrtc RTC handle
784 * @retval None
786 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
788 /* Prevent unused argument(s) compilation warning */
789 UNUSED(hrtc);
791 /* NOTE : This function should not be modified, when the callback is needed,
792 the HAL_RTC_MspInit could be implemented in the user file
797 * @brief DeInitialize the RTC MSP.
798 * @param hrtc RTC handle
799 * @retval None
801 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
803 /* Prevent unused argument(s) compilation warning */
804 UNUSED(hrtc);
806 /* NOTE : This function should not be modified, when the callback is needed,
807 the HAL_RTC_MspDeInit could be implemented in the user file
812 * @}
815 /** @addtogroup RTC_Exported_Functions_Group2
816 * @brief RTC Time and Date functions
818 @verbatim
819 ===============================================================================
820 ##### RTC Time and Date functions #####
821 ===============================================================================
823 [..] This section provides functions allowing to configure Time and Date features
825 @endverbatim
826 * @{
830 * @brief Set RTC current time.
831 * @param hrtc RTC handle
832 * @param sTime Pointer to Time structure
833 * @param Format Specifies the format of the entered parameters.
834 * This parameter can be one of the following values:
835 * @arg RTC_FORMAT_BIN: Binary data format
836 * @arg RTC_FORMAT_BCD: BCD data format
837 * @retval HAL status
839 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
841 uint32_t tmpreg;
842 HAL_StatusTypeDef status;
844 /* Check the parameters */
845 assert_param(IS_RTC_FORMAT(Format));
846 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
847 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
849 /* Process Locked */
850 __HAL_LOCK(hrtc);
852 hrtc->State = HAL_RTC_STATE_BUSY;
854 /* Disable the write protection for RTC registers */
855 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
856 /* Enter Initialization mode */
857 status = RTC_EnterInitMode(hrtc);
858 if (status == HAL_OK)
860 if(Format == RTC_FORMAT_BIN)
862 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
864 assert_param(IS_RTC_HOUR12(sTime->Hours));
865 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
867 else
869 sTime->TimeFormat = 0x00U;
870 assert_param(IS_RTC_HOUR24(sTime->Hours));
872 assert_param(IS_RTC_MINUTES(sTime->Minutes));
873 assert_param(IS_RTC_SECONDS(sTime->Seconds));
875 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
876 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
877 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
878 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
880 else
882 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
884 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
885 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
887 else
889 sTime->TimeFormat = 0x00U;
890 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
892 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
893 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
894 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
895 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
896 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
897 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
900 /* Set the RTC_TR register */
901 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
903 /* Clear the bits to be configured */
904 hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
906 /* Configure the RTC_CR register */
907 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
909 /* Exit Initialization mode */
910 status = RTC_ExitInitMode(hrtc);
913 /* Enable the write protection for RTC registers */
914 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
916 if (status == HAL_OK)
918 hrtc->State = HAL_RTC_STATE_READY;
921 /* Process Unlocked */
922 __HAL_UNLOCK(hrtc);
924 return status;
928 * @brief Get RTC current time.
929 * @param hrtc RTC handle
930 * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
931 * with input format (BIN or BCD), also SubSeconds field returning the
932 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
933 * factor to be used for second fraction ratio computation.
934 * @param Format Specifies the format of the entered parameters.
935 * This parameter can be one of the following values:
936 * @arg RTC_FORMAT_BIN: Binary data format
937 * @arg RTC_FORMAT_BCD: BCD data format
938 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
939 * value in second fraction ratio with time unit following generic formula:
940 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
941 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
942 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
943 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
944 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
945 * to ensure consistency between the time and date values.
946 * @retval HAL status
948 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
950 uint32_t tmpreg;
952 /* Check the parameters */
953 assert_param(IS_RTC_FORMAT(Format));
955 /* Get subseconds structure field from the corresponding register*/
956 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
958 /* Get SecondFraction structure field from the corresponding register field*/
959 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
961 /* Get the TR register */
962 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
964 /* Fill the structure fields with the read parameters */
965 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
966 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
967 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
968 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
970 /* Check the input parameters format */
971 if(Format == RTC_FORMAT_BIN)
973 /* Convert the time structure parameters to Binary format */
974 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
975 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
976 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
979 return HAL_OK;
983 * @brief Set RTC current date.
984 * @param hrtc RTC handle
985 * @param sDate Pointer to date structure
986 * @param Format specifies the format of the entered parameters.
987 * This parameter can be one of the following values:
988 * @arg RTC_FORMAT_BIN: Binary data format
989 * @arg RTC_FORMAT_BCD: BCD data format
990 * @retval HAL status
992 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
994 uint32_t datetmpreg;
995 HAL_StatusTypeDef status;
997 /* Check the parameters */
998 assert_param(IS_RTC_FORMAT(Format));
1000 /* Process Locked */
1001 __HAL_LOCK(hrtc);
1003 hrtc->State = HAL_RTC_STATE_BUSY;
1005 if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
1007 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
1010 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
1012 if(Format == RTC_FORMAT_BIN)
1014 assert_param(IS_RTC_YEAR(sDate->Year));
1015 assert_param(IS_RTC_MONTH(sDate->Month));
1016 assert_param(IS_RTC_DATE(sDate->Date));
1018 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
1019 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
1020 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
1021 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
1023 else
1025 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
1026 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
1027 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
1029 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
1030 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
1031 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
1032 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
1035 /* Disable the write protection for RTC registers */
1036 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1039 /* Enter Initialization mode */
1040 status = RTC_EnterInitMode(hrtc);
1041 if (status == HAL_OK)
1043 /* Set the RTC_DR register */
1044 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
1047 /* Exit Initialization mode */
1048 status = RTC_ExitInitMode(hrtc);
1051 /* Enable the write protection for RTC registers */
1052 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1054 if (status == HAL_OK)
1056 hrtc->State = HAL_RTC_STATE_READY;
1059 /* Process Unlocked */
1060 __HAL_UNLOCK(hrtc);
1062 return status;
1068 * @brief Get RTC current date.
1069 * @param hrtc RTC handle
1070 * @param sDate Pointer to Date structure
1071 * @param Format Specifies the format of the entered parameters.
1072 * This parameter can be one of the following values:
1073 * @arg RTC_FORMAT_BIN: Binary data format
1074 * @arg RTC_FORMAT_BCD: BCD data format
1075 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1076 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1077 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1078 * @retval HAL status
1080 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1082 uint32_t datetmpreg;
1084 /* Check the parameters */
1085 assert_param(IS_RTC_FORMAT(Format));
1087 /* Get the DR register */
1088 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
1090 /* Fill the structure fields with the read parameters */
1091 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1092 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1093 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1094 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
1096 /* Check the input parameters format */
1097 if(Format == RTC_FORMAT_BIN)
1099 /* Convert the date structure parameters to Binary format */
1100 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1101 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1102 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1104 return HAL_OK;
1108 * @}
1111 /** @addtogroup RTC_Exported_Functions_Group3
1112 * @brief RTC Alarm functions
1114 @verbatim
1115 ===============================================================================
1116 ##### RTC Alarm functions #####
1117 ===============================================================================
1119 [..] This section provides functions allowing to configure Alarm feature
1121 @endverbatim
1122 * @{
1125 * @brief Set the specified RTC Alarm.
1126 * @param hrtc RTC handle
1127 * @param sAlarm Pointer to Alarm structure
1128 * @param Format Specifies the format of the entered parameters.
1129 * This parameter can be one of the following values:
1130 * @arg RTC_FORMAT_BIN: Binary data format
1131 * @arg RTC_FORMAT_BCD: BCD data format
1132 * @retval HAL status
1134 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1136 uint32_t tickstart;
1137 uint32_t tmpreg;
1138 uint32_t subsecondtmpreg;
1140 /* Check the parameters */
1141 assert_param(IS_RTC_FORMAT(Format));
1142 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1143 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1144 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1145 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1146 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1148 /* Process Locked */
1149 __HAL_LOCK(hrtc);
1151 hrtc->State = HAL_RTC_STATE_BUSY;
1153 if(Format == RTC_FORMAT_BIN)
1155 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1157 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1158 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1160 else
1162 sAlarm->AlarmTime.TimeFormat = 0x00U;
1163 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1165 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1166 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1168 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1170 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1172 else
1174 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1177 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1178 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1179 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1180 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1181 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1182 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1183 ((uint32_t)sAlarm->AlarmMask));
1185 else
1187 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1189 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1190 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1192 else
1194 sAlarm->AlarmTime.TimeFormat = 0x00U;
1195 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1198 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1199 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1201 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1203 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1205 else
1207 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1210 tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours << RTC_ALRMAR_HU_Pos) | \
1211 ((uint32_t)sAlarm->AlarmTime.Minutes << RTC_ALRMAR_MNU_Pos) | \
1212 ((uint32_t)sAlarm->AlarmTime.Seconds << RTC_ALRMAR_SU_Pos) | \
1213 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1214 ((uint32_t)sAlarm->AlarmDateWeekDay << RTC_ALRMAR_DU_Pos) | \
1215 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1216 ((uint32_t)sAlarm->AlarmMask));
1219 /* Configure the Alarm A or Alarm B Sub Second registers */
1220 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1222 /* Disable the write protection for RTC registers */
1223 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1225 /* Configure the Alarm register */
1226 if(sAlarm->Alarm == RTC_ALARM_A)
1228 /* Disable the Alarm A interrupt */
1229 __HAL_RTC_ALARMA_DISABLE(hrtc);
1230 /* Clear flag alarm A */
1231 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1232 /* In case of interrupt mode is used, the interrupt source must disabled */
1233 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1235 tickstart = HAL_GetTick();
1236 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1237 #if defined(RTC_ICSR_ALRAWF)
1238 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1239 #endif /* RTC_ICSR_ALRAWF */
1240 #if defined(RTC_ISR_ALRAWF)
1241 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1242 #endif /* RTC_ISR_ALRAWF */
1244 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1246 /* Enable the write protection for RTC registers */
1247 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1249 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1251 /* Process Unlocked */
1252 __HAL_UNLOCK(hrtc);
1254 return HAL_TIMEOUT;
1258 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1259 /* Configure the Alarm A Sub Second register */
1260 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1261 /* Configure the Alarm state: Enable Alarm */
1262 __HAL_RTC_ALARMA_ENABLE(hrtc);
1264 else
1266 /* Disable the Alarm B interrupt */
1267 __HAL_RTC_ALARMB_DISABLE(hrtc);
1268 /* Clear flag alarm B */
1269 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1270 /* In case of interrupt mode is used, the interrupt source must disabled */
1271 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1273 tickstart = HAL_GetTick();
1274 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1275 #if defined(RTC_ICSR_ALRBWF)
1276 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1277 #endif /* RTC_ICSR_ALRBWF */
1278 #if defined(RTC_ISR_ALRBWF)
1279 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1280 #endif /* RTC_ISR_ALRBWF */
1282 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1284 /* Enable the write protection for RTC registers */
1285 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1287 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1289 /* Process Unlocked */
1290 __HAL_UNLOCK(hrtc);
1292 return HAL_TIMEOUT;
1296 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1297 /* Configure the Alarm B Sub Second register */
1298 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1299 /* Configure the Alarm state: Enable Alarm */
1300 __HAL_RTC_ALARMB_ENABLE(hrtc);
1303 /* Enable the write protection for RTC registers */
1304 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1306 /* Change RTC state */
1307 hrtc->State = HAL_RTC_STATE_READY;
1309 /* Process Unlocked */
1310 __HAL_UNLOCK(hrtc);
1312 return HAL_OK;
1316 * @brief Set the specified RTC Alarm with Interrupt.
1317 * @param hrtc RTC handle
1318 * @param sAlarm Pointer to Alarm structure
1319 * @param Format Specifies the format of the entered parameters.
1320 * This parameter can be one of the following values:
1321 * @arg RTC_FORMAT_BIN: Binary data format
1322 * @arg RTC_FORMAT_BCD: BCD data format
1323 * @note The Alarm register can only be written when the corresponding Alarm
1324 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
1325 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1326 * @retval HAL status
1328 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1330 uint32_t tickstart;
1331 uint32_t tmpreg;
1332 uint32_t subsecondtmpreg;
1334 /* Check the parameters */
1335 assert_param(IS_RTC_FORMAT(Format));
1336 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1337 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1338 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1339 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1340 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1342 /* Process Locked */
1343 __HAL_LOCK(hrtc);
1345 hrtc->State = HAL_RTC_STATE_BUSY;
1347 if(Format == RTC_FORMAT_BIN)
1349 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1351 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1352 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1354 else
1356 sAlarm->AlarmTime.TimeFormat = 0x00U;
1357 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1359 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1360 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1362 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1364 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1366 else
1368 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1371 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1372 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1373 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1374 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1375 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1376 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1377 ((uint32_t)sAlarm->AlarmMask));
1379 else
1381 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1383 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1384 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1386 else
1388 sAlarm->AlarmTime.TimeFormat = 0x00U;
1389 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1392 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1393 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1395 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1397 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1399 else
1401 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1404 tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours << RTC_ALRMAR_HU_Pos) | \
1405 ((uint32_t)sAlarm->AlarmTime.Minutes << RTC_ALRMAR_MNU_Pos) | \
1406 ((uint32_t)sAlarm->AlarmTime.Seconds << RTC_ALRMAR_SU_Pos) | \
1407 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1408 ((uint32_t)sAlarm->AlarmDateWeekDay << RTC_ALRMAR_DU_Pos) | \
1409 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1410 ((uint32_t)sAlarm->AlarmMask));
1412 /* Configure the Alarm A or Alarm B Sub Second registers */
1413 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1415 /* Disable the write protection for RTC registers */
1416 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1418 /* Configure the Alarm register */
1419 if(sAlarm->Alarm == RTC_ALARM_A)
1421 /* Disable the Alarm A interrupt */
1422 __HAL_RTC_ALARMA_DISABLE(hrtc);
1424 /* Clear flag alarm A */
1425 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1427 tickstart = HAL_GetTick();
1428 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1429 #if defined(RTC_ICSR_ALRAWF)
1430 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1431 #endif /* RTC_ICSR_ALRAWF */
1432 #if defined(RTC_ISR_ALRAWF)
1433 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1434 #endif /* RTC_ISR_ALRAWF */
1436 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1438 /* Enable the write protection for RTC registers */
1439 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1441 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1443 /* Process Unlocked */
1444 __HAL_UNLOCK(hrtc);
1446 return HAL_TIMEOUT;
1450 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1451 /* Configure the Alarm A Sub Second register */
1452 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1453 /* Configure the Alarm state: Enable Alarm */
1454 __HAL_RTC_ALARMA_ENABLE(hrtc);
1455 /* Configure the Alarm interrupt */
1456 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
1458 else
1460 /* Disable the Alarm B interrupt */
1461 __HAL_RTC_ALARMB_DISABLE(hrtc);
1463 /* Clear flag alarm B */
1464 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1466 tickstart = HAL_GetTick();
1467 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1468 #if defined(RTC_ICSR_ALRBWF)
1469 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1470 #endif /* RTC_ICSR_ALRBWF */
1471 #if defined(RTC_ISR_ALRBWF)
1472 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1473 #endif /* RTC_ISR_ALRBWF */
1475 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1477 /* Enable the write protection for RTC registers */
1478 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1480 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1482 /* Process Unlocked */
1483 __HAL_UNLOCK(hrtc);
1485 return HAL_TIMEOUT;
1489 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1490 /* Configure the Alarm B Sub Second register */
1491 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1492 /* Configure the Alarm state: Enable Alarm */
1493 __HAL_RTC_ALARMB_ENABLE(hrtc);
1494 /* Configure the Alarm interrupt */
1495 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1498 #if !defined(DUAL_CORE)
1499 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1500 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1501 #endif
1503 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1505 /* Enable the write protection for RTC registers */
1506 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1508 hrtc->State = HAL_RTC_STATE_READY;
1510 /* Process Unlocked */
1511 __HAL_UNLOCK(hrtc);
1513 return HAL_OK;
1517 * @brief Deactivate the specified RTC Alarm.
1518 * @param hrtc RTC handle
1519 * @param Alarm Specifies the Alarm.
1520 * This parameter can be one of the following values:
1521 * @arg RTC_ALARM_A: AlarmA
1522 * @arg RTC_ALARM_B: AlarmB
1523 * @retval HAL status
1525 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1527 uint32_t tickstart;
1529 /* Check the parameters */
1530 assert_param(IS_RTC_ALARM(Alarm));
1532 /* Process Locked */
1533 __HAL_LOCK(hrtc);
1535 hrtc->State = HAL_RTC_STATE_BUSY;
1537 /* Disable the write protection for RTC registers */
1538 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1540 if(Alarm == RTC_ALARM_A)
1542 /* AlarmA */
1543 __HAL_RTC_ALARMA_DISABLE(hrtc);
1545 /* In case of interrupt mode is used, the interrupt source must disabled */
1546 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1548 tickstart = HAL_GetTick();
1550 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1551 #if defined(RTC_ICSR_ALRAWF)
1552 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1553 #endif /* RTC_ICSR_ALRAWF */
1554 #if defined(RTC_ISR_ALRAWF)
1555 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1556 #endif /* RTC_ISR_ALRAWF */
1558 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1560 /* Enable the write protection for RTC registers */
1561 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1563 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1565 /* Process Unlocked */
1566 __HAL_UNLOCK(hrtc);
1568 return HAL_TIMEOUT;
1572 else
1574 /* AlarmB */
1575 __HAL_RTC_ALARMB_DISABLE(hrtc);
1577 /* In case of interrupt mode is used, the interrupt source must disabled */
1578 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1580 tickstart = HAL_GetTick();
1582 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1583 #if defined(RTC_ICSR_ALRBWF)
1584 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1585 #endif /* RTC_ICSR_ALRBWF */
1586 #if defined(RTC_ISR_ALRBWF)
1587 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1588 #endif /* RTC_ISR_ALRBWF */
1590 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1592 /* Enable the write protection for RTC registers */
1593 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1595 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1597 /* Process Unlocked */
1598 __HAL_UNLOCK(hrtc);
1600 return HAL_TIMEOUT;
1604 /* Enable the write protection for RTC registers */
1605 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1607 hrtc->State = HAL_RTC_STATE_READY;
1609 /* Process Unlocked */
1610 __HAL_UNLOCK(hrtc);
1612 return HAL_OK;
1616 * @brief Get the RTC Alarm value and masks.
1617 * @param hrtc RTC handle
1618 * @param sAlarm Pointer to Date structure
1619 * @param Alarm Specifies the Alarm.
1620 * This parameter can be one of the following values:
1621 * @arg RTC_ALARM_A: AlarmA
1622 * @arg RTC_ALARM_B: AlarmB
1623 * @param Format Specifies the format of the entered parameters.
1624 * This parameter can be one of the following values:
1625 * @arg RTC_FORMAT_BIN: Binary data format
1626 * @arg RTC_FORMAT_BCD: BCD data format
1627 * @retval HAL status
1629 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1631 uint32_t tmpreg;
1632 uint32_t subsecondtmpreg;
1634 /* Check the parameters */
1635 assert_param(IS_RTC_FORMAT(Format));
1636 assert_param(IS_RTC_ALARM(Alarm));
1638 if(Alarm == RTC_ALARM_A)
1640 /* AlarmA */
1641 sAlarm->Alarm = RTC_ALARM_A;
1643 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1644 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
1646 /* Fill the structure with the read parameters */
1647 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1648 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1649 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1650 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
1651 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1652 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1653 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1654 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1656 else
1658 sAlarm->Alarm = RTC_ALARM_B;
1660 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1661 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1663 /* Fill the structure with the read parameters */
1664 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1665 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1666 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1667 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1668 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1669 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1670 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1671 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1674 if(Format == RTC_FORMAT_BIN)
1676 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1677 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1678 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1679 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1682 return HAL_OK;
1686 * @brief Handle Alarm interrupt request.
1687 * @param hrtc RTC handle
1688 * @retval None
1690 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1692 /* Clear the EXTI's line Flag for RTC Alarm */
1693 #if defined(DUAL_CORE)
1694 if(HAL_GetCurrentCPUID() == CM7_CPUID)
1696 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1698 else
1700 __HAL_RTC_ALARM_EXTID2_CLEAR_FLAG();
1702 #else /* SINGLE_CORE */
1703 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1704 #endif /* DUAL_CORE */
1706 #if defined(RTC_MISR_ALRAMF)
1707 /* Get interrupt status */
1708 uint32_t tmp = hrtc->Instance->MISR;
1710 if((tmp & RTC_MISR_ALRAMF) != 0u)
1712 /* Clear the AlarmA interrupt pending bit */
1713 hrtc->Instance->SCR = RTC_SCR_CALRAF;
1715 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1716 /* Call Compare Match registered Callback */
1717 hrtc->AlarmAEventCallback(hrtc);
1718 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1719 HAL_RTC_AlarmAEventCallback(hrtc);
1720 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1722 #endif /* RTC_MISR_ALRAMF */
1724 #if defined(RTC_MISR_ALRBMF)
1725 if((tmp & RTC_MISR_ALRBMF) != 0u)
1727 /* Clear the AlarmB interrupt pending bit */
1728 hrtc->Instance->SCR = RTC_SCR_CALRBF;
1730 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1731 /* Call Compare Match registered Callback */
1732 hrtc->AlarmBEventCallback(hrtc);
1733 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1734 HAL_RTCEx_AlarmBEventCallback(hrtc);
1735 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1737 #endif /* RTC_MISR_ALRBMF */
1739 #if defined(RTC_ISR_ALRAF)
1740 /* Get the AlarmA interrupt source enable status */
1741 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
1743 /* Get the pending status of the AlarmA Interrupt */
1744 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
1746 /* Clear the AlarmA interrupt pending bit */
1747 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1749 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1750 hrtc->AlarmAEventCallback(hrtc);
1751 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1752 HAL_RTC_AlarmAEventCallback(hrtc);
1753 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1756 #endif /* RTC_ISR_ALRAF */
1758 #if defined(RTC_ISR_ALRBF)
1759 /* Get the AlarmB interrupt source enable status */
1760 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
1762 /* Get the pending status of the AlarmB Interrupt */
1763 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
1765 /* Clear the AlarmB interrupt pending bit */
1766 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1768 /* AlarmB callback */
1769 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1770 hrtc->AlarmBEventCallback(hrtc);
1771 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1772 HAL_RTCEx_AlarmBEventCallback(hrtc);
1773 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1776 #endif /* RTC_ISR_ALRBF */
1778 /* Change RTC state */
1779 hrtc->State = HAL_RTC_STATE_READY;
1783 * @brief Alarm A callback.
1784 * @param hrtc RTC handle
1785 * @retval None
1787 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1789 /* Prevent unused argument(s) compilation warning */
1790 UNUSED(hrtc);
1792 /* NOTE : This function should not be modified, when the callback is needed,
1793 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1798 * @brief Handle AlarmA Polling request.
1799 * @param hrtc RTC handle
1800 * @param Timeout Timeout duration
1801 * @retval HAL status
1803 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1806 uint32_t tickstart = HAL_GetTick();
1808 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
1810 if(Timeout != HAL_MAX_DELAY)
1812 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1814 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1815 return HAL_TIMEOUT;
1820 /* Clear the Alarm interrupt pending bit */
1821 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1823 /* Change RTC state */
1824 hrtc->State = HAL_RTC_STATE_READY;
1826 return HAL_OK;
1830 * @}
1833 /** @addtogroup RTC_Exported_Functions_Group4
1834 * @brief Peripheral Control functions
1836 @verbatim
1837 ===============================================================================
1838 ##### Peripheral Control functions #####
1839 ===============================================================================
1840 [..]
1841 This subsection provides functions allowing to
1842 (+) Wait for RTC Time and Date Synchronization
1844 @endverbatim
1845 * @{
1849 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1850 * synchronized with RTC APB clock.
1851 * @note The RTC Resynchronization mode is write protected, use the
1852 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1853 * @note To read the calendar through the shadow registers after Calendar
1854 * initialization, calendar update or after wakeup from low power modes
1855 * the software must first clear the RSF flag.
1856 * The software must then wait until it is set again before reading
1857 * the calendar, which means that the calendar registers have been
1858 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1859 * @param hrtc RTC handle
1860 * @retval HAL status
1862 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
1864 uint32_t tickstart;
1866 /* Clear RSF flag */
1867 #if defined(RTC_ICSR_RSF)
1868 hrtc->Instance->ICSR &= (uint32_t)RTC_RSF_MASK;
1869 #endif /* RTC_ICSR_RSF */
1870 #if defined(RTC_ISR_RSF)
1871 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1872 #endif /* RTC_ISR_RSF */
1874 tickstart = HAL_GetTick();
1876 /* Wait the registers to be synchronised */
1877 #if defined(RTC_ICSR_RSF)
1878 while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
1879 #endif /* RTC_ICSR_RSF */
1880 #if defined(RTC_ISR_RSF)
1881 while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
1882 #endif /* RTC_ISR_RSF */
1884 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1886 return HAL_TIMEOUT;
1890 return HAL_OK;
1894 * @}
1897 /** @addtogroup RTC_Exported_Functions_Group5
1898 * @brief Peripheral State functions
1900 @verbatim
1901 ===============================================================================
1902 ##### Peripheral State functions #####
1903 ===============================================================================
1904 [..]
1905 This subsection provides functions allowing to
1906 (+) Get RTC state
1908 @endverbatim
1909 * @{
1912 * @brief Return the RTC handle state.
1913 * @param hrtc RTC handle
1914 * @retval HAL state
1916 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
1918 /* Return RTC handle state */
1919 return hrtc->State;
1923 * @}
1927 * @}
1930 /** @addtogroup RTC_Private_Functions
1931 * @{
1934 * @brief Enter the RTC Initialization mode.
1935 * @note The RTC Initialization mode is write protected, use the
1936 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1937 * @param hrtc RTC handle
1938 * @retval HAL status
1940 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
1942 uint32_t tickstart;
1943 HAL_StatusTypeDef status = HAL_OK;
1944 /* Check if the Initialization mode is set */
1945 #if defined(RTC_ICSR_INITF)
1946 if((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
1948 /* Set the Initialization mode */
1949 SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
1951 tickstart = HAL_GetTick();
1952 /* Wait till RTC is in INIT state and if Time out is reached exit */
1953 while ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
1955 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1957 status = HAL_TIMEOUT;
1958 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1962 #endif /* RTC_ICSR_INITF */
1963 #if defined(RTC_ISR_INITF)
1964 if((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
1966 /* Set the Initialization mode */
1967 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1969 tickstart = HAL_GetTick();
1970 /* Wait till RTC is in INIT state and if Time out is reached exit */
1971 while ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
1973 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1975 status = HAL_TIMEOUT;
1976 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1980 #endif /* RTC_ISR_INITF */
1982 return status;
1986 * @brief Exit the RTC Initialization mode.
1987 * @param hrtc RTC handle
1988 * @retval HAL status
1990 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
1992 HAL_StatusTypeDef status = HAL_OK;
1994 /* Check if the Initialization mode is set */
1995 #if defined(RTC_ICSR_INITF)
1997 /* Exit Initialization mode */
1998 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
2000 #elif defined(RTC_ISR_INITF)
2002 /* Exit Initialization mode */
2003 CLEAR_BIT(RTC->ISR, RTC_ISR_INITF);
2005 #endif /* RTC_ISR_INITF */
2006 /* If CR_BYPSHAD bit = 0, wait for synchro */
2007 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
2009 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2011 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2012 status = HAL_TIMEOUT;
2015 else
2017 /* Clear BYPSHAD bit */
2018 CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
2019 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2021 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2022 status = HAL_TIMEOUT;
2024 /* Restore BYPSHAD bit */
2025 SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
2028 return status;
2032 * @brief Convert a 2 digit decimal to BCD format.
2033 * @param Value Byte to be converted
2034 * @retval Converted byte
2036 uint8_t RTC_ByteToBcd2(uint8_t Value)
2038 uint32_t bcdhigh = 0U;
2039 uint8_t bcdlow = Value;
2041 while (bcdlow >= 10U)
2043 bcdhigh++;
2044 bcdlow -= 10U;
2047 return ((uint8_t)(bcdhigh << 4U) | bcdlow);
2051 * @brief Convert from 2 digit BCD to Binary.
2052 * @param Value BCD value to be converted
2053 * @retval Converted word
2055 uint8_t RTC_Bcd2ToByte(uint8_t Value)
2057 uint8_t tmp;
2058 tmp = ((Value & 0xF0U) >> 4U) * 10U;
2059 return (tmp + (Value & 0x0FU));
2063 * @}
2066 #endif /* HAL_RTC_MODULE_ENABLED */
2068 * @}
2072 * @}
2075 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/