Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_rtc.c
blob970f650c3fd78ce57f00d7008868aa2bc958f4dc
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 status = HAL_OK;
236 /* Check the parameters */
237 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
238 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
239 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
240 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
241 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
242 assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
243 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
244 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
245 #if defined(RTC_CR_TAMPALRM_PU)
246 assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
247 #endif /* RTC_CR_TAMPALRM_PU */
249 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
250 if(hrtc->State == HAL_RTC_STATE_RESET)
252 /* Allocate lock resource and initialize it */
253 hrtc->Lock = HAL_UNLOCKED;
255 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
256 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
257 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
258 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
259 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
260 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
261 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
263 #if defined(TAMP_CR1_ITAMP1E)
264 hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback;
265 #endif /* TAMP_CR1_ITAMP1E */
266 #if defined(TAMP_CR1_ITAMP2E)
267 hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback;
268 #endif /* TAMP_CR1_ITAMP2E */
269 #if defined(TAMP_CR1_ITAMP3E)
270 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback;
271 #endif /* TAMP_CR1_ITAMP3E */
272 #if defined(TAMP_CR1_ITAMP4E)
273 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback;
274 #endif /* TAMP_CR1_ITAMP4E */
275 #if defined(TAMP_CR1_ITAMP5E)
276 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback;
277 #endif /* TAMP_CR1_ITAMP5E */
278 #if defined(TAMP_CR1_ITAMP6E)
279 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback;
280 #endif /* TAMP_CR1_ITAMP6E */
281 #if defined(TAMP_CR1_ITAMP8E)
282 hrtc->InternalTamper8EventCallback = HAL_RTCEx_InternalTamper8EventCallback;
283 #endif /* TAMP_CR1_ITAMP8E */
286 if(hrtc->MspInitCallback == NULL)
288 hrtc->MspInitCallback = HAL_RTC_MspInit;
290 /* Init the low level hardware */
291 hrtc->MspInitCallback(hrtc);
293 if(hrtc->MspDeInitCallback == NULL)
295 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
298 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
299 if(hrtc->State == HAL_RTC_STATE_RESET)
301 /* Allocate lock resource and initialize it */
302 hrtc->Lock = HAL_UNLOCKED;
304 /* Initialize RTC MSP */
305 HAL_RTC_MspInit(hrtc);
307 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
309 /* Set RTC state */
310 hrtc->State = HAL_RTC_STATE_BUSY;
312 /* Disable the write protection for RTC registers */
313 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
315 /* Set Initialization mode */
316 if(RTC_EnterInitMode(hrtc) != HAL_OK)
318 /* Enable the write protection for RTC registers */
319 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
321 /* Set RTC state */
322 hrtc->State = HAL_RTC_STATE_ERROR;
324 status = HAL_ERROR;
326 else
328 #if defined(RTC_CR_TAMPOE)
329 /* Clear RTC_CR FMT, OSEL, POL and TAMPOE Bits */
330 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE);
331 #else /* RTC_CR_TAMPOE */
332 /* Clear RTC_CR FMT, OSEL and POL Bits */
333 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL);
334 #endif /* RTC_CR_TAMPOE */
336 /* Set RTC_CR register */
337 hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
339 /* Configure the RTC PRER */
340 hrtc->Instance->PRER = (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos) | (hrtc->Init.SynchPrediv << RTC_PRER_PREDIV_S_Pos);
342 /* Exit Initialization mode */
343 #if defined(RTC_ICSR_INIT)
344 CLEAR_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
345 #endif /* RTC_ICSR_INIT */
346 #if defined(RTC_ISR_INIT)
347 CLEAR_BIT(hrtc->Instance->ISR, RTC_ISR_INIT);
348 #endif /* RTC_ISR_INIT */
350 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
351 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
353 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
355 /* Enable the write protection for RTC registers */
356 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
358 hrtc->State = HAL_RTC_STATE_ERROR;
359 status = HAL_ERROR;
363 if(status == HAL_OK)
365 #if defined(RTC_CR_TAMPALRM_PU) && defined(RTC_CR_TAMPALRM_TYPE) && defined(RTC_CR_OUT2EN)
366 hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
367 hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
368 #endif /* RTC_CR_TAMPALRM_TYPE && RTC_CR_OUT2EN && RTC_CR_TAMPALRM_PU */
370 #if defined(RTC_OR_ALARMOUTTYPE) && defined(RTC_OR_OUT_RMP)
371 hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
372 hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
373 #endif /* ALARMOUTTYPE && RTC_OR_OUT_RMP */
375 /* Enable the write protection for RTC registers */
376 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
378 /* Set RTC state */
379 hrtc->State = HAL_RTC_STATE_READY;
384 /* return status */
385 return status;
389 * @brief DeInitialize the RTC peripheral.
390 * @note This function doesn't reset the RTC Backup Data registers.
391 * @param hrtc RTC handle
392 * @retval HAL status
394 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
396 HAL_StatusTypeDef status = HAL_ERROR;
397 uint32_t tickstart;
399 /* Check RTC handler */
400 if(hrtc != NULL)
402 status = HAL_OK;
404 /* Check the parameters */
405 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
407 /* Set RTC state */
408 hrtc->State = HAL_RTC_STATE_BUSY;
410 /* Disable the write protection for RTC registers */
411 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
413 /* Set Initialization mode */
414 if(RTC_EnterInitMode(hrtc) != HAL_OK)
416 /* Enable the write protection for RTC registers */
417 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
419 /* Set RTC state */
420 hrtc->State = HAL_RTC_STATE_ERROR;
422 status = HAL_ERROR;
424 else
426 /* Reset TR, DR and CR registers */
427 hrtc->Instance->TR = 0x00000000U;
428 hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
429 /* Reset All CR bits except CR[2:0] */
430 hrtc->Instance->CR &= RTC_CR_WUCKSEL;
432 tickstart = HAL_GetTick();
434 /* Wait till WUTWF flag is set and if Time out is reached exit */
435 #if defined(RTC_ICSR_WUTWF)
436 while (((hrtc->Instance->ICSR) & RTC_ICSR_WUTWF) == 0U)
437 #endif /* RTC_ICSR_WUTWF */
438 #if defined(RTC_ISR_WUTWF)
439 while (((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == 0U)
440 #endif /* RTC_ISR_WUTWF */
442 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
444 /* Enable the write protection for RTC registers */
445 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
447 /* Set RTC state */
448 hrtc->State = HAL_RTC_STATE_TIMEOUT;
450 status = HAL_TIMEOUT;
454 if(status == HAL_OK)
456 /* Reset all RTC CR register bits */
457 hrtc->Instance->CR &= 0x00000000U;
459 /* Reset other RTC registers */
460 hrtc->Instance->WUTR = RTC_WUTR_WUT;
461 hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
462 hrtc->Instance->ALRMAR = 0x00000000U;
463 hrtc->Instance->ALRMBR = 0x00000000U;
464 hrtc->Instance->SHIFTR = 0x00000000U;
465 hrtc->Instance->CALR = 0x00000000U;
466 hrtc->Instance->ALRMASSR = 0x00000000U;
467 hrtc->Instance->ALRMBSSR = 0x00000000U;
469 #if defined(RTC_ICSR_INIT)
470 /* Exit initialization mode */
471 CLEAR_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
472 #endif /* RTC_ICSR_INIT */
473 #if defined(RTC_ISR_INIT)
474 /* Reset ISR register and exit initialization mode */
475 hrtc->Instance->ISR = 0x00000000U;
477 /* Reset Tamper configuration register */
478 hrtc->Instance->TAMPCR = 0x00000000U;
480 /* Reset Option register */
481 hrtc->Instance->OR = 0x00000000U;
482 #endif /* RTC_ISR_INIT */
484 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
485 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
487 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
489 /* Enable the write protection for RTC registers */
490 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
492 hrtc->State = HAL_RTC_STATE_ERROR;
494 status = HAL_ERROR;
500 if(status == HAL_OK)
502 #if defined(TAMP_CR1_TAMP1E)
503 /* Reset TAMP registers */
504 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR1 = 0xFFFF0000U;
505 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR2 = 0x00000000U;
506 #endif /* TAMP_CR1_TAMP1E */
508 /* Enable the write protection for RTC registers */
509 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
511 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
512 if(hrtc->MspDeInitCallback == NULL)
514 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
517 /* DeInit the low level hardware: CLOCK, NVIC.*/
518 hrtc->MspDeInitCallback(hrtc);
519 #else
520 /* De-Initialize RTC MSP */
521 HAL_RTC_MspDeInit(hrtc);
522 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
524 hrtc->State = HAL_RTC_STATE_RESET;
526 /* Release Lock */
527 __HAL_UNLOCK(hrtc);
531 /* return status */
532 return status;
535 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
537 * @brief Register a User RTC Callback
538 * To be used instead of the weak predefined callback
539 * @param hrtc RTC handle
540 * @param CallbackID ID of the callback to be registered
541 * This parameter can be one of the following values:
542 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
543 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
544 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
545 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
546 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
547 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
548 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
549 * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
550 * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
551 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
552 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
553 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
554 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
555 * @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID
556 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
557 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
558 * @param pCallback pointer to the Callback function
559 * @retval HAL status
561 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
563 HAL_StatusTypeDef status = HAL_OK;
565 if(pCallback == NULL)
567 return HAL_ERROR;
570 /* Process locked */
571 __HAL_LOCK(hrtc);
573 if(HAL_RTC_STATE_READY == hrtc->State)
575 switch (CallbackID)
577 case HAL_RTC_ALARM_A_EVENT_CB_ID :
578 hrtc->AlarmAEventCallback = pCallback;
579 break;
581 case HAL_RTC_ALARM_B_EVENT_CB_ID :
582 hrtc->AlarmBEventCallback = pCallback;
583 break;
585 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
586 hrtc->TimeStampEventCallback = pCallback;
587 break;
589 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
590 hrtc->WakeUpTimerEventCallback = pCallback;
591 break;
593 case HAL_RTC_TAMPER1_EVENT_CB_ID :
594 hrtc->Tamper1EventCallback = pCallback;
595 break;
597 case HAL_RTC_TAMPER2_EVENT_CB_ID :
598 hrtc->Tamper2EventCallback = pCallback;
599 break;
601 case HAL_RTC_TAMPER3_EVENT_CB_ID :
602 hrtc->Tamper3EventCallback = pCallback;
603 break;
605 #if defined(TAMP_CR1_ITAMP1E)
606 case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
607 hrtc->InternalTamper1EventCallback = pCallback;
608 break;
609 #endif /* TAMP_CR1_ITAMP1E */
611 #if defined(TAMP_CR1_ITAMP2E)
612 case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
613 hrtc->InternalTamper2EventCallback = pCallback;
614 break;
615 #endif /* TAMP_CR1_ITAMP2E */
617 #if defined(TAMP_CR1_ITAMP3E)
618 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
619 hrtc->InternalTamper3EventCallback = pCallback;
620 break;
621 #endif /* TAMP_CR1_ITAMP3E */
623 #if defined(TAMP_CR1_ITAMP4E)
624 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
625 hrtc->InternalTamper4EventCallback = pCallback;
626 break;
627 #endif /* TAMP_CR1_ITAMP4E */
629 #if defined(TAMP_CR1_ITAMP5E)
630 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
631 hrtc->InternalTamper5EventCallback = pCallback;
632 break;
633 #endif /* TAMP_CR1_ITAMP5E */
635 #if defined(TAMP_CR1_ITAMP6E)
636 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
637 hrtc->InternalTamper6EventCallback = pCallback;
638 break;
639 #endif /* TAMP_CR1_ITAMP6E */
641 #if defined(TAMP_CR1_ITAMP8E)
642 case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
643 hrtc->InternalTamper8EventCallback = pCallback;
644 break;
645 #endif /* TAMP_CR1_ITAMP8E */
647 case HAL_RTC_MSPINIT_CB_ID :
648 hrtc->MspInitCallback = pCallback;
649 break;
651 case HAL_RTC_MSPDEINIT_CB_ID :
652 hrtc->MspDeInitCallback = pCallback;
653 break;
655 default :
656 /* Return error status */
657 status = HAL_ERROR;
658 break;
661 else if(HAL_RTC_STATE_RESET == hrtc->State)
663 switch (CallbackID)
665 case HAL_RTC_MSPINIT_CB_ID :
666 hrtc->MspInitCallback = pCallback;
667 break;
669 case HAL_RTC_MSPDEINIT_CB_ID :
670 hrtc->MspDeInitCallback = pCallback;
671 break;
673 default :
674 /* Return error status */
675 status = HAL_ERROR;
676 break;
679 else
681 /* Return error status */
682 status = HAL_ERROR;
685 /* Release Lock */
686 __HAL_UNLOCK(hrtc);
688 return status;
692 * @brief Unregister an RTC Callback
693 * RTC callabck is redirected to the weak predefined callback
694 * @param hrtc RTC handle
695 * @param CallbackID ID of the callback to be unregistered
696 * This parameter can be one of the following values:
697 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
698 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
699 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
700 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
701 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
702 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
703 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
704 * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
705 * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
706 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
707 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
708 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
709 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
710 * @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID
711 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
712 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
713 * @retval HAL status
715 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
717 HAL_StatusTypeDef status = HAL_OK;
719 /* Process locked */
720 __HAL_LOCK(hrtc);
722 if(HAL_RTC_STATE_READY == hrtc->State)
724 switch (CallbackID)
726 case HAL_RTC_ALARM_A_EVENT_CB_ID :
727 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
728 break;
730 case HAL_RTC_ALARM_B_EVENT_CB_ID :
731 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
732 break;
734 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
735 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
736 break;
738 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
739 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
740 break;
742 case HAL_RTC_TAMPER1_EVENT_CB_ID :
743 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
744 break;
746 case HAL_RTC_TAMPER2_EVENT_CB_ID :
747 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
748 break;
750 case HAL_RTC_TAMPER3_EVENT_CB_ID :
751 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
752 break;
754 #if defined(TAMP_CR1_ITAMP1E)
755 case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
756 hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback;
757 break;
758 #endif /* TAMP_CR1_ITAMP1E */
760 #if defined(TAMP_CR1_ITAMP2E)
761 case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
762 hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback;
763 break;
764 #endif /* TAMP_CR1_ITAMP2E */
766 #if defined(TAMP_CR1_ITAMP3E)
767 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
768 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback;
769 break;
770 #endif /* TAMP_CR1_ITAMP3E */
772 #if defined(TAMP_CR1_ITAMP4E)
773 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
774 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback;
775 break;
776 #endif /* TAMP_CR1_ITAMP4E */
778 #if defined(TAMP_CR1_ITAMP5E)
779 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
780 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback;
781 break;
782 #endif /* TAMP_CR1_ITAMP5E */
784 #if defined(TAMP_CR1_ITAMP6E)
785 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
786 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback;
787 break;
788 #endif /* TAMP_CR1_ITAMP6E */
790 #if defined(TAMP_CR1_ITAMP8E)
791 case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
792 hrtc->InternalTamper8EventCallback = HAL_RTCEx_InternalTamper8EventCallback;
793 break;
794 #endif /* TAMP_CR1_ITAMP8E */
796 case HAL_RTC_MSPINIT_CB_ID :
797 hrtc->MspInitCallback = HAL_RTC_MspInit;
798 break;
800 case HAL_RTC_MSPDEINIT_CB_ID :
801 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
802 break;
804 default :
805 /* Return error status */
806 status = HAL_ERROR;
807 break;
810 else if(HAL_RTC_STATE_RESET == hrtc->State)
812 switch (CallbackID)
814 case HAL_RTC_MSPINIT_CB_ID :
815 hrtc->MspInitCallback = HAL_RTC_MspInit;
816 break;
818 case HAL_RTC_MSPDEINIT_CB_ID :
819 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
820 break;
822 default :
823 /* Return error status */
824 status = HAL_ERROR;
825 break;
828 else
830 /* Return error status */
831 status = HAL_ERROR;
834 /* Release Lock */
835 __HAL_UNLOCK(hrtc);
837 return status;
839 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
842 * @brief Initialize the RTC MSP.
843 * @param hrtc RTC handle
844 * @retval None
846 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
848 /* Prevent unused argument(s) compilation warning */
849 UNUSED(hrtc);
851 /* NOTE : This function should not be modified, when the callback is needed,
852 the HAL_RTC_MspInit could be implemented in the user file
857 * @brief DeInitialize the RTC MSP.
858 * @param hrtc RTC handle
859 * @retval None
861 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
863 /* Prevent unused argument(s) compilation warning */
864 UNUSED(hrtc);
866 /* NOTE : This function should not be modified, when the callback is needed,
867 the HAL_RTC_MspDeInit could be implemented in the user file
872 * @}
875 /** @addtogroup RTC_Exported_Functions_Group2
876 * @brief RTC Time and Date functions
878 @verbatim
879 ===============================================================================
880 ##### RTC Time and Date functions #####
881 ===============================================================================
883 [..] This section provides functions allowing to configure Time and Date features
885 @endverbatim
886 * @{
890 * @brief Set RTC current time.
891 * @param hrtc RTC handle
892 * @param sTime Pointer to Time structure
893 * @param Format Specifies the format of the entered parameters.
894 * This parameter can be one of the following values:
895 * @arg RTC_FORMAT_BIN: Binary data format
896 * @arg RTC_FORMAT_BCD: BCD data format
897 * @retval HAL status
899 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
901 uint32_t tmpreg;
903 /* Check the parameters */
904 assert_param(IS_RTC_FORMAT(Format));
905 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
906 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
908 /* Process Locked */
909 __HAL_LOCK(hrtc);
911 hrtc->State = HAL_RTC_STATE_BUSY;
913 /* Disable the write protection for RTC registers */
914 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
916 /* Set Initialization mode */
917 if(RTC_EnterInitMode(hrtc) != HAL_OK)
919 /* Enable the write protection for RTC registers */
920 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
922 /* Set RTC state */
923 hrtc->State = HAL_RTC_STATE_ERROR;
925 /* Process Unlocked */
926 __HAL_UNLOCK(hrtc);
928 return HAL_ERROR;
930 else
932 if(Format == RTC_FORMAT_BIN)
934 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
936 assert_param(IS_RTC_HOUR12(sTime->Hours));
937 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
939 else
941 sTime->TimeFormat = 0x00U;
942 assert_param(IS_RTC_HOUR24(sTime->Hours));
944 assert_param(IS_RTC_MINUTES(sTime->Minutes));
945 assert_param(IS_RTC_SECONDS(sTime->Seconds));
947 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
948 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
949 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
950 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
952 else
954 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
956 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
957 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
959 else
961 sTime->TimeFormat = 0x00U;
962 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
964 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
965 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
966 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
967 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
968 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
969 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
972 /* Set the RTC_TR register */
973 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
975 /* Clear the bits to be configured */
976 hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
978 /* Configure the RTC_CR register */
979 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
981 /* Exit Initialization mode */
982 #if defined(RTC_ICSR_INIT)
983 CLEAR_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
984 #endif /* RTC_ICSR_INIT */
985 #if defined(RTC_ISR_INIT)
986 CLEAR_BIT(hrtc->Instance->ISR, RTC_ISR_INIT);
987 #endif /* RTC_ISR_INIT */
989 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
990 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
992 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
994 /* Enable the write protection for RTC registers */
995 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
997 hrtc->State = HAL_RTC_STATE_ERROR;
999 /* Process Unlocked */
1000 __HAL_UNLOCK(hrtc);
1002 return HAL_ERROR;
1006 /* Enable the write protection for RTC registers */
1007 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1009 hrtc->State = HAL_RTC_STATE_READY;
1011 __HAL_UNLOCK(hrtc);
1013 return HAL_OK;
1018 * @brief Get RTC current time.
1019 * @param hrtc RTC handle
1020 * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
1021 * with input format (BIN or BCD), also SubSeconds field returning the
1022 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
1023 * factor to be used for second fraction ratio computation.
1024 * @param Format Specifies the format of the entered parameters.
1025 * This parameter can be one of the following values:
1026 * @arg RTC_FORMAT_BIN: Binary data format
1027 * @arg RTC_FORMAT_BCD: BCD data format
1028 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
1029 * value in second fraction ratio with time unit following generic formula:
1030 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
1031 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
1032 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1033 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1034 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
1035 * to ensure consistency between the time and date values.
1036 * @retval HAL status
1038 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
1040 uint32_t tmpreg;
1042 /* Check the parameters */
1043 assert_param(IS_RTC_FORMAT(Format));
1045 /* Get subseconds structure field from the corresponding register*/
1046 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
1048 /* Get SecondFraction structure field from the corresponding register field*/
1049 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
1051 /* Get the TR register */
1052 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
1054 /* Fill the structure fields with the read parameters */
1055 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
1056 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
1057 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
1058 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
1060 /* Check the input parameters format */
1061 if(Format == RTC_FORMAT_BIN)
1063 /* Convert the time structure parameters to Binary format */
1064 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
1065 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
1066 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
1069 return HAL_OK;
1073 * @brief Set RTC current date.
1074 * @param hrtc RTC handle
1075 * @param sDate Pointer to date structure
1076 * @param Format specifies the format of the entered parameters.
1077 * This parameter can be one of the following values:
1078 * @arg RTC_FORMAT_BIN: Binary data format
1079 * @arg RTC_FORMAT_BCD: BCD data format
1080 * @retval HAL status
1082 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1084 uint32_t datetmpreg;
1086 /* Check the parameters */
1087 assert_param(IS_RTC_FORMAT(Format));
1089 /* Process Locked */
1090 __HAL_LOCK(hrtc);
1092 hrtc->State = HAL_RTC_STATE_BUSY;
1094 if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
1096 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
1099 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
1101 if(Format == RTC_FORMAT_BIN)
1103 assert_param(IS_RTC_YEAR(sDate->Year));
1104 assert_param(IS_RTC_MONTH(sDate->Month));
1105 assert_param(IS_RTC_DATE(sDate->Date));
1107 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
1108 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
1109 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
1110 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
1112 else
1114 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
1115 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
1116 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
1118 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
1119 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
1120 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
1121 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
1124 /* Disable the write protection for RTC registers */
1125 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1127 /* Set Initialization mode */
1128 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1130 /* Enable the write protection for RTC registers */
1131 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1133 /* Set RTC state*/
1134 hrtc->State = HAL_RTC_STATE_ERROR;
1136 /* Process Unlocked */
1137 __HAL_UNLOCK(hrtc);
1139 return HAL_ERROR;
1141 else
1143 /* Set the RTC_DR register */
1144 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
1146 /* Exit Initialization mode */
1147 #if defined(RTC_ICSR_INIT)
1148 CLEAR_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
1149 #endif /* RTC_ISR_INIT */
1150 #if defined(RTC_ISR_INIT)
1151 CLEAR_BIT(hrtc->Instance->ISR, RTC_ISR_INIT);
1152 #endif /* RTC_ISR_INIT */
1154 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1155 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
1157 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1159 /* Enable the write protection for RTC registers */
1160 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1162 hrtc->State = HAL_RTC_STATE_ERROR;
1164 /* Process Unlocked */
1165 __HAL_UNLOCK(hrtc);
1167 return HAL_ERROR;
1171 /* Enable the write protection for RTC registers */
1172 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1174 hrtc->State = HAL_RTC_STATE_READY ;
1176 /* Process Unlocked */
1177 __HAL_UNLOCK(hrtc);
1179 return HAL_OK;
1184 * @brief Get RTC current date.
1185 * @param hrtc RTC handle
1186 * @param sDate Pointer to Date structure
1187 * @param Format Specifies the format of the entered parameters.
1188 * This parameter can be one of the following values:
1189 * @arg RTC_FORMAT_BIN: Binary data format
1190 * @arg RTC_FORMAT_BCD: BCD data format
1191 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1192 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1193 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1194 * @retval HAL status
1196 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1198 uint32_t datetmpreg;
1200 /* Check the parameters */
1201 assert_param(IS_RTC_FORMAT(Format));
1203 /* Get the DR register */
1204 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
1206 /* Fill the structure fields with the read parameters */
1207 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1208 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1209 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1210 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
1212 /* Check the input parameters format */
1213 if(Format == RTC_FORMAT_BIN)
1215 /* Convert the date structure parameters to Binary format */
1216 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1217 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1218 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1220 return HAL_OK;
1224 * @}
1227 /** @addtogroup RTC_Exported_Functions_Group3
1228 * @brief RTC Alarm functions
1230 @verbatim
1231 ===============================================================================
1232 ##### RTC Alarm functions #####
1233 ===============================================================================
1235 [..] This section provides functions allowing to configure Alarm feature
1237 @endverbatim
1238 * @{
1241 * @brief Set the specified RTC Alarm.
1242 * @param hrtc RTC handle
1243 * @param sAlarm Pointer to Alarm structure
1244 * @param Format Specifies the format of the entered parameters.
1245 * This parameter can be one of the following values:
1246 * @arg RTC_FORMAT_BIN: Binary data format
1247 * @arg RTC_FORMAT_BCD: BCD data format
1248 * @retval HAL status
1250 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1252 uint32_t tickstart;
1253 uint32_t tmpreg;
1254 uint32_t subsecondtmpreg;
1256 /* Check the parameters */
1257 assert_param(IS_RTC_FORMAT(Format));
1258 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1259 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1260 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1261 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1262 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1264 /* Process Locked */
1265 __HAL_LOCK(hrtc);
1267 hrtc->State = HAL_RTC_STATE_BUSY;
1269 if(Format == RTC_FORMAT_BIN)
1271 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1273 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1274 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1276 else
1278 sAlarm->AlarmTime.TimeFormat = 0x00U;
1279 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1281 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1282 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1284 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1286 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1288 else
1290 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1293 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1294 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1295 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1296 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1297 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1298 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1299 ((uint32_t)sAlarm->AlarmMask));
1301 else
1303 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1305 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1306 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1308 else
1310 sAlarm->AlarmTime.TimeFormat = 0x00U;
1311 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1314 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1315 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1317 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1319 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1321 else
1323 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1326 tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours << RTC_ALRMAR_HU_Pos) | \
1327 ((uint32_t)sAlarm->AlarmTime.Minutes << RTC_ALRMAR_MNU_Pos) | \
1328 ((uint32_t)sAlarm->AlarmTime.Seconds << RTC_ALRMAR_SU_Pos) | \
1329 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1330 ((uint32_t)sAlarm->AlarmDateWeekDay << RTC_ALRMAR_DU_Pos) | \
1331 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1332 ((uint32_t)sAlarm->AlarmMask));
1335 /* Configure the Alarm A or Alarm B Sub Second registers */
1336 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1338 /* Disable the write protection for RTC registers */
1339 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1341 /* Configure the Alarm register */
1342 if(sAlarm->Alarm == RTC_ALARM_A)
1344 /* Disable the Alarm A interrupt */
1345 __HAL_RTC_ALARMA_DISABLE(hrtc);
1346 /* Clear flag alarm A */
1347 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1348 /* In case of interrupt mode is used, the interrupt source must disabled */
1349 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1351 tickstart = HAL_GetTick();
1352 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1353 #if defined(RTC_ICSR_ALRAWF)
1354 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1355 #endif /* RTC_ICSR_ALRAWF */
1356 #if defined(RTC_ISR_ALRAWF)
1357 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1358 #endif /* RTC_ISR_ALRAWF */
1360 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1362 /* Enable the write protection for RTC registers */
1363 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1365 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1367 /* Process Unlocked */
1368 __HAL_UNLOCK(hrtc);
1370 return HAL_TIMEOUT;
1374 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1375 /* Configure the Alarm A Sub Second register */
1376 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1377 /* Configure the Alarm state: Enable Alarm */
1378 __HAL_RTC_ALARMA_ENABLE(hrtc);
1380 else
1382 /* Disable the Alarm B interrupt */
1383 __HAL_RTC_ALARMB_DISABLE(hrtc);
1384 /* Clear flag alarm B */
1385 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1386 /* In case of interrupt mode is used, the interrupt source must disabled */
1387 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1389 tickstart = HAL_GetTick();
1390 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1391 #if defined(RTC_ICSR_ALRBWF)
1392 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1393 #endif /* RTC_ICSR_ALRBWF */
1394 #if defined(RTC_ISR_ALRBWF)
1395 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1396 #endif /* RTC_ISR_ALRBWF */
1398 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1400 /* Enable the write protection for RTC registers */
1401 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1403 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1405 /* Process Unlocked */
1406 __HAL_UNLOCK(hrtc);
1408 return HAL_TIMEOUT;
1412 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1413 /* Configure the Alarm B Sub Second register */
1414 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1415 /* Configure the Alarm state: Enable Alarm */
1416 __HAL_RTC_ALARMB_ENABLE(hrtc);
1419 /* Enable the write protection for RTC registers */
1420 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1422 /* Change RTC state */
1423 hrtc->State = HAL_RTC_STATE_READY;
1425 /* Process Unlocked */
1426 __HAL_UNLOCK(hrtc);
1428 return HAL_OK;
1432 * @brief Set the specified RTC Alarm with Interrupt.
1433 * @param hrtc RTC handle
1434 * @param sAlarm Pointer to Alarm structure
1435 * @param Format Specifies the format of the entered parameters.
1436 * This parameter can be one of the following values:
1437 * @arg RTC_FORMAT_BIN: Binary data format
1438 * @arg RTC_FORMAT_BCD: BCD data format
1439 * @note The Alarm register can only be written when the corresponding Alarm
1440 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
1441 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1442 * @retval HAL status
1444 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1446 uint32_t tickstart;
1447 uint32_t tmpreg;
1448 uint32_t subsecondtmpreg;
1450 /* Check the parameters */
1451 assert_param(IS_RTC_FORMAT(Format));
1452 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1453 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1454 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1455 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1456 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1458 /* Process Locked */
1459 __HAL_LOCK(hrtc);
1461 hrtc->State = HAL_RTC_STATE_BUSY;
1463 if(Format == RTC_FORMAT_BIN)
1465 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1467 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1468 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1470 else
1472 sAlarm->AlarmTime.TimeFormat = 0x00U;
1473 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1475 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1476 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1478 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1480 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1482 else
1484 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1487 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1488 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1489 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1490 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1491 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1492 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1493 ((uint32_t)sAlarm->AlarmMask));
1495 else
1497 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1499 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1500 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1502 else
1504 sAlarm->AlarmTime.TimeFormat = 0x00U;
1505 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1508 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1509 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1511 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1513 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1515 else
1517 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1520 tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours << RTC_ALRMAR_HU_Pos) | \
1521 ((uint32_t)sAlarm->AlarmTime.Minutes << RTC_ALRMAR_MNU_Pos) | \
1522 ((uint32_t)sAlarm->AlarmTime.Seconds << RTC_ALRMAR_SU_Pos) | \
1523 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1524 ((uint32_t)sAlarm->AlarmDateWeekDay << RTC_ALRMAR_DU_Pos) | \
1525 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1526 ((uint32_t)sAlarm->AlarmMask));
1528 /* Configure the Alarm A or Alarm B Sub Second registers */
1529 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1531 /* Disable the write protection for RTC registers */
1532 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1534 /* Configure the Alarm register */
1535 if(sAlarm->Alarm == RTC_ALARM_A)
1537 /* Disable the Alarm A interrupt */
1538 __HAL_RTC_ALARMA_DISABLE(hrtc);
1540 /* Clear flag alarm A */
1541 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1543 tickstart = HAL_GetTick();
1544 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1545 #if defined(RTC_ICSR_ALRAWF)
1546 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1547 #endif /* RTC_ICSR_ALRAWF */
1548 #if defined(RTC_ISR_ALRAWF)
1549 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1550 #endif /* RTC_ISR_ALRAWF */
1552 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1554 /* Enable the write protection for RTC registers */
1555 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1557 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1559 /* Process Unlocked */
1560 __HAL_UNLOCK(hrtc);
1562 return HAL_TIMEOUT;
1566 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1567 /* Configure the Alarm A Sub Second register */
1568 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1569 /* Configure the Alarm state: Enable Alarm */
1570 __HAL_RTC_ALARMA_ENABLE(hrtc);
1571 /* Configure the Alarm interrupt */
1572 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
1574 else
1576 /* Disable the Alarm B interrupt */
1577 __HAL_RTC_ALARMB_DISABLE(hrtc);
1579 /* Clear flag alarm B */
1580 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1582 tickstart = HAL_GetTick();
1583 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1584 #if defined(RTC_ICSR_ALRBWF)
1585 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1586 #endif /* RTC_ICSR_ALRBWF */
1587 #if defined(RTC_ISR_ALRBWF)
1588 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1589 #endif /* RTC_ISR_ALRBWF */
1591 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1593 /* Enable the write protection for RTC registers */
1594 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1596 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1598 /* Process Unlocked */
1599 __HAL_UNLOCK(hrtc);
1601 return HAL_TIMEOUT;
1605 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1606 /* Configure the Alarm B Sub Second register */
1607 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1608 /* Configure the Alarm state: Enable Alarm */
1609 __HAL_RTC_ALARMB_ENABLE(hrtc);
1610 /* Configure the Alarm interrupt */
1611 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1614 #if !defined(DUAL_CORE)
1615 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1616 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1617 #endif
1619 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1621 /* Enable the write protection for RTC registers */
1622 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1624 hrtc->State = HAL_RTC_STATE_READY;
1626 /* Process Unlocked */
1627 __HAL_UNLOCK(hrtc);
1629 return HAL_OK;
1633 * @brief Deactivate the specified RTC Alarm.
1634 * @param hrtc RTC handle
1635 * @param Alarm Specifies the Alarm.
1636 * This parameter can be one of the following values:
1637 * @arg RTC_ALARM_A: AlarmA
1638 * @arg RTC_ALARM_B: AlarmB
1639 * @retval HAL status
1641 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1643 uint32_t tickstart;
1645 /* Check the parameters */
1646 assert_param(IS_RTC_ALARM(Alarm));
1648 /* Process Locked */
1649 __HAL_LOCK(hrtc);
1651 hrtc->State = HAL_RTC_STATE_BUSY;
1653 /* Disable the write protection for RTC registers */
1654 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1656 if(Alarm == RTC_ALARM_A)
1658 /* AlarmA */
1659 __HAL_RTC_ALARMA_DISABLE(hrtc);
1661 /* In case of interrupt mode is used, the interrupt source must disabled */
1662 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1664 tickstart = HAL_GetTick();
1666 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1667 #if defined(RTC_ICSR_ALRAWF)
1668 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1669 #endif /* RTC_ICSR_ALRAWF */
1670 #if defined(RTC_ISR_ALRAWF)
1671 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1672 #endif /* RTC_ISR_ALRAWF */
1674 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1676 /* Enable the write protection for RTC registers */
1677 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1679 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1681 /* Process Unlocked */
1682 __HAL_UNLOCK(hrtc);
1684 return HAL_TIMEOUT;
1688 else
1690 /* AlarmB */
1691 __HAL_RTC_ALARMB_DISABLE(hrtc);
1693 /* In case of interrupt mode is used, the interrupt source must disabled */
1694 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1696 tickstart = HAL_GetTick();
1698 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1699 #if defined(RTC_ICSR_ALRBWF)
1700 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1701 #endif /* RTC_ICSR_ALRBWF */
1702 #if defined(RTC_ISR_ALRBWF)
1703 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1704 #endif /* RTC_ISR_ALRBWF */
1706 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1708 /* Enable the write protection for RTC registers */
1709 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1711 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1713 /* Process Unlocked */
1714 __HAL_UNLOCK(hrtc);
1716 return HAL_TIMEOUT;
1720 /* Enable the write protection for RTC registers */
1721 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1723 hrtc->State = HAL_RTC_STATE_READY;
1725 /* Process Unlocked */
1726 __HAL_UNLOCK(hrtc);
1728 return HAL_OK;
1732 * @brief Get the RTC Alarm value and masks.
1733 * @param hrtc RTC handle
1734 * @param sAlarm Pointer to Date structure
1735 * @param Alarm Specifies the Alarm.
1736 * This parameter can be one of the following values:
1737 * @arg RTC_ALARM_A: AlarmA
1738 * @arg RTC_ALARM_B: AlarmB
1739 * @param Format Specifies the format of the entered parameters.
1740 * This parameter can be one of the following values:
1741 * @arg RTC_FORMAT_BIN: Binary data format
1742 * @arg RTC_FORMAT_BCD: BCD data format
1743 * @retval HAL status
1745 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1747 uint32_t tmpreg;
1748 uint32_t subsecondtmpreg;
1750 /* Check the parameters */
1751 assert_param(IS_RTC_FORMAT(Format));
1752 assert_param(IS_RTC_ALARM(Alarm));
1754 if(Alarm == RTC_ALARM_A)
1756 /* AlarmA */
1757 sAlarm->Alarm = RTC_ALARM_A;
1759 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1760 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
1762 /* Fill the structure with the read parameters */
1763 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1764 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1765 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1766 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
1767 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1768 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1769 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1770 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1772 else
1774 sAlarm->Alarm = RTC_ALARM_B;
1776 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1777 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1779 /* Fill the structure with the read parameters */
1780 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1781 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1782 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1783 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1784 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1785 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1786 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1787 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1790 if(Format == RTC_FORMAT_BIN)
1792 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1793 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1794 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1795 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1798 return HAL_OK;
1802 * @brief Handle Alarm interrupt request.
1803 * @param hrtc RTC handle
1804 * @retval None
1806 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1808 /* Clear the EXTI's line Flag for RTC Alarm */
1809 #if defined(DUAL_CORE)
1810 if(HAL_GetCurrentCPUID() == CM7_CPUID)
1812 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1814 else
1816 __HAL_RTC_ALARM_EXTID2_CLEAR_FLAG();
1818 #else /* SINGLE_CORE */
1819 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1820 #endif /* DUAL_CORE */
1822 #if defined(RTC_MISR_ALRAMF)
1823 /* Get interrupt status */
1824 uint32_t tmp = hrtc->Instance->MISR;
1826 if((tmp & RTC_MISR_ALRAMF) != 0u)
1828 /* Clear the AlarmA interrupt pending bit */
1829 hrtc->Instance->SCR = RTC_SCR_CALRAF;
1831 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1832 /* Call Compare Match registered Callback */
1833 hrtc->AlarmAEventCallback(hrtc);
1834 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1835 HAL_RTC_AlarmAEventCallback(hrtc);
1836 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1838 #endif /* RTC_MISR_ALRAMF */
1840 #if defined(RTC_MISR_ALRBMF)
1841 if((tmp & RTC_MISR_ALRBMF) != 0u)
1843 /* Clear the AlarmB interrupt pending bit */
1844 hrtc->Instance->SCR = RTC_SCR_CALRBF;
1846 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1847 /* Call Compare Match registered Callback */
1848 hrtc->AlarmBEventCallback(hrtc);
1849 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1850 HAL_RTCEx_AlarmBEventCallback(hrtc);
1851 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1853 #endif /* RTC_MISR_ALRBMF */
1855 #if defined(RTC_ISR_ALRAF)
1856 /* Get the AlarmA interrupt source enable status */
1857 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
1859 /* Get the pending status of the AlarmA Interrupt */
1860 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
1862 /* Clear the AlarmA interrupt pending bit */
1863 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1865 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1866 hrtc->AlarmAEventCallback(hrtc);
1867 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1868 HAL_RTC_AlarmAEventCallback(hrtc);
1869 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1872 #endif /* RTC_ISR_ALRAF */
1874 #if defined(RTC_ISR_ALRBF)
1875 /* Get the AlarmB interrupt source enable status */
1876 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
1878 /* Get the pending status of the AlarmB Interrupt */
1879 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
1881 /* Clear the AlarmB interrupt pending bit */
1882 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1884 /* AlarmB callback */
1885 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1886 hrtc->AlarmBEventCallback(hrtc);
1887 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1888 HAL_RTCEx_AlarmBEventCallback(hrtc);
1889 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1892 #endif /* RTC_ISR_ALRBF */
1894 /* Change RTC state */
1895 hrtc->State = HAL_RTC_STATE_READY;
1899 * @brief Alarm A callback.
1900 * @param hrtc RTC handle
1901 * @retval None
1903 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1905 /* Prevent unused argument(s) compilation warning */
1906 UNUSED(hrtc);
1908 /* NOTE : This function should not be modified, when the callback is needed,
1909 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1914 * @brief Handle AlarmA Polling request.
1915 * @param hrtc RTC handle
1916 * @param Timeout Timeout duration
1917 * @retval HAL status
1919 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1922 uint32_t tickstart = HAL_GetTick();
1924 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
1926 if(Timeout != HAL_MAX_DELAY)
1928 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1930 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1931 return HAL_TIMEOUT;
1936 /* Clear the Alarm interrupt pending bit */
1937 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1939 /* Change RTC state */
1940 hrtc->State = HAL_RTC_STATE_READY;
1942 return HAL_OK;
1946 * @}
1949 /** @addtogroup RTC_Exported_Functions_Group4
1950 * @brief Peripheral Control functions
1952 @verbatim
1953 ===============================================================================
1954 ##### Peripheral Control functions #####
1955 ===============================================================================
1956 [..]
1957 This subsection provides functions allowing to
1958 (+) Wait for RTC Time and Date Synchronization
1960 @endverbatim
1961 * @{
1965 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1966 * synchronized with RTC APB clock.
1967 * @note The RTC Resynchronization mode is write protected, use the
1968 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1969 * @note To read the calendar through the shadow registers after Calendar
1970 * initialization, calendar update or after wakeup from low power modes
1971 * the software must first clear the RSF flag.
1972 * The software must then wait until it is set again before reading
1973 * the calendar, which means that the calendar registers have been
1974 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1975 * @param hrtc RTC handle
1976 * @retval HAL status
1978 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
1980 uint32_t tickstart;
1982 /* Clear RSF flag */
1983 #if defined(RTC_ICSR_RSF)
1984 hrtc->Instance->ICSR &= (uint32_t)RTC_RSF_MASK;
1985 #endif /* RTC_ICSR_RSF */
1986 #if defined(RTC_ISR_RSF)
1987 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1988 #endif /* RTC_ISR_RSF */
1990 tickstart = HAL_GetTick();
1992 /* Wait the registers to be synchronised */
1993 #if defined(RTC_ICSR_RSF)
1994 while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
1995 #endif /* RTC_ICSR_RSF */
1996 #if defined(RTC_ISR_RSF)
1997 while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
1998 #endif /* RTC_ISR_RSF */
2000 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2002 return HAL_TIMEOUT;
2006 return HAL_OK;
2010 * @}
2013 /** @addtogroup RTC_Exported_Functions_Group5
2014 * @brief Peripheral State functions
2016 @verbatim
2017 ===============================================================================
2018 ##### Peripheral State functions #####
2019 ===============================================================================
2020 [..]
2021 This subsection provides functions allowing to
2022 (+) Get RTC state
2024 @endverbatim
2025 * @{
2028 * @brief Return the RTC handle state.
2029 * @param hrtc RTC handle
2030 * @retval HAL state
2032 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
2034 /* Return RTC handle state */
2035 return hrtc->State;
2039 * @}
2043 * @}
2046 /** @addtogroup RTC_Private_Functions
2047 * @{
2050 * @brief Enter the RTC Initialization mode.
2051 * @note The RTC Initialization mode is write protected, use the
2052 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2053 * @param hrtc RTC handle
2054 * @retval HAL status
2056 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
2058 uint32_t tickstart;
2060 /* Check if the Initialization mode is set */
2061 #if defined(RTC_ICSR_INITF)
2062 if((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
2064 /* Set the Initialization mode */
2065 SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
2067 tickstart = HAL_GetTick();
2068 /* Wait till RTC is in INIT state and if Time out is reached exit */
2069 while ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
2071 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2073 return HAL_TIMEOUT;
2077 #endif /* RTC_ICSR_INITF */
2078 #if defined(RTC_ISR_INITF)
2079 if((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
2081 /* Set the Initialization mode */
2082 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
2084 tickstart = HAL_GetTick();
2085 /* Wait till RTC is in INIT state and if Time out is reached exit */
2086 while ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
2088 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2090 return HAL_TIMEOUT;
2094 #endif /* RTC_ISR_INITF */
2096 return HAL_OK;
2101 * @brief Convert a 2 digit decimal to BCD format.
2102 * @param Value Byte to be converted
2103 * @retval Converted byte
2105 uint8_t RTC_ByteToBcd2(uint8_t Value)
2107 uint32_t bcdhigh = 0U;
2108 uint8_t bcdlow = Value;
2110 while (bcdlow >= 10U)
2112 bcdhigh++;
2113 bcdlow -= 10U;
2116 return ((uint8_t)(bcdhigh << 4U) | bcdlow);
2120 * @brief Convert from 2 digit BCD to Binary.
2121 * @param Value BCD value to be converted
2122 * @retval Converted word
2124 uint8_t RTC_Bcd2ToByte(uint8_t Value)
2126 uint8_t tmp;
2127 tmp = ((Value & 0xF0U) >> 4U) * 10U;
2128 return (tmp + (Value & 0x0FU));
2132 * @}
2135 #endif /* HAL_RTC_MODULE_ENABLED */
2137 * @}
2141 * @}
2144 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/