STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
stm32h7xx_hal_rtc.c
Go to the documentation of this file.
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
17 *
18 ******************************************************************************
19 * @attention
20 *
21 * Copyright (c) 2017 STMicroelectronics.
22 * All rights reserved.
23 *
24 * This software is licensed under terms that can be found in the LICENSE file
25 * in the root directory of this software component.
26 * If no LICENSE file comes with this software, it is provided AS-IS.
27 *
28 ******************************************************************************
29 @verbatim
30 ===============================================================================
31 ##### RTC Operating Condition #####
32 ===============================================================================
33 [..] The real-time clock (RTC) and the RTC backup registers can be powered
34 from the VBAT voltage when the main VDD supply is powered off.
35 To retain the content of the RTC backup registers and supply the RTC
36 when VDD is turned off, VBAT pin can be connected to an optional
37 standby voltage supplied by a battery or by another source.
38
39 ##### Backup Domain Reset #####
40 ===============================================================================
41 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
42 to their reset values.
43 A backup domain reset is generated when one of the following events occurs:
44 (#) Software reset, triggered by setting the BDRST bit in the
45 RCC Backup domain control register (RCC_BDCR).
46 (#) VDD or VBAT power on, if both supplies have previously been powered off.
47 (#) Tamper detection event resets all data backup registers.
48
49 ##### Backup Domain Access #####
50 ===================================================================
51 [..] After reset, the backup domain (RTC registers, RTC backup data
52 registers and backup SRAM) is protected against possible unwanted write
53 accesses.
54
55 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
56 (#) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
57 PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
58 (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro.
59
60 ##### How to use RTC Driver #####
61 ===================================================================
62 [..]
63 (+) Enable the RTC domain access (see description in the section above).
64 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
65 format using the HAL_RTC_Init() function.
66
67 *** Time and Date configuration ***
68 ===================================
69 [..]
70 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
71 and HAL_RTC_SetDate() functions.
72 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
73
74 *** Alarm configuration ***
75 ===========================
76 [..]
77 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
78 You can also configure the RTC Alarm with interrupt mode using the
79 HAL_RTC_SetAlarm_IT() function.
80 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
81
82 ##### RTC and low power modes #####
83 ===================================================================
84 [..] The MCU can be woken up from a low power mode by an RTC alternate
85 function.
86 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
87 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
88 These RTC alternate functions can wake up the system from the Stop and
89 Standby low power modes.
90 [..] The system can also wake up from low power modes without depending
91 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
92 or the RTC wakeup events.
93 [..] The RTC provides a programmable time base for waking up from the
94 Stop or Standby mode at regular intervals.
95 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
96 is LSE or LSI.
97
98 *** Callback registration ***
99 =============================================
100 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
101 not defined, the callback registration feature is not available and all callbacks
102 are set to the corresponding weak functions. This is the recommended configuration
103 in order to optimize memory/code consumption footprint/performances.
104
105 The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
106 allows the user to configure dynamically the driver callbacks.
107 Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
108
109 Function HAL_RTC_RegisterCallback() allows to register following callbacks:
110 (+) AlarmAEventCallback : RTC Alarm A Event callback.
111 (+) AlarmBEventCallback : RTC Alarm B Event callback.
112 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
113 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
114 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
115 (+) Tamper2EventCallback : RTC Tamper 2 Event callback. (*)
116 (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
117 (+) InternalTamper1EventCallback : RTC Internal Tamper 1 Callback ID (*)
118 (+) InternalTamper2EventCallback : RTC Internal Tamper 2 Callback ID (*)
119 (+) InternalTamper3EventCallback : RTC Internal Tamper 3 Callback ID (*)
120 (+) InternalTamper4EventCallback : RTC Internal Tamper 4 Callback ID (*)
121 (+) InternalTamper5EventCallback : RTC Internal Tamper 5 Callback ID (*)
122 (+) InternalTamper6EventCallback : RTC Internal Tamper 6 Callback ID (*)
123 (+) InternalTamper8EventCallback : RTC Internal Tamper 8 Callback ID (*)
124 (+) MspInitCallback : RTC MspInit callback.
125 (+) MspDeInitCallback : RTC MspDeInit callback.
126
127 (*) Not applicable to all devices.
128
129 This function takes as parameters the HAL peripheral handle, the Callback ID
130 and a pointer to the user callback function.
131
132 Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
133 weak function.
134 HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
135 and the Callback ID.
136 This function allows to reset following callbacks:
137 (+) AlarmAEventCallback : RTC Alarm A Event callback.
138 (+) AlarmBEventCallback : RTC Alarm B Event callback.
139 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
140 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
141 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
142 (+) Tamper2EventCallback : RTC Tamper 2 Event callback. (*)
143 (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
144 (+) InternalTamper1EventCallback : RTC Internal Tamper 1 Callback ID (*)
145 (+) InternalTamper2EventCallback : RTC Internal Tamper 2 Callback ID (*)
146 (+) InternalTamper3EventCallback : RTC Internal Tamper 3 Callback ID (*)
147 (+) InternalTamper4EventCallback : RTC Internal Tamper 4 Callback ID (*)
148 (+) InternalTamper5EventCallback : RTC Internal Tamper 5 Callback ID (*)
149 (+) InternalTamper6EventCallback : RTC Internal Tamper 6 Callback ID (*)
150 (+) InternalTamper8EventCallback : RTC Internal Tamper 8 Callback ID (*)
151 (+) MspInitCallback : RTC MspInit callback.
152 (+) MspDeInitCallback : RTC MspDeInit callback.
153
154 (*) Not applicable to all devices.
155
156 By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
157 all callbacks are set to the corresponding weak functions :
158 examples AlarmAEventCallback(), WakeUpTimerEventCallback().
159 Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
160 in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
161 (not registered beforehand).
162 If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
163 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
164
165 Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
166 Exception done MspInit/MspDeInit that can be registered/unregistered
167 in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
168 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
169 In that case first register the MspInit/MspDeInit user callbacks
170 using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
171 or HAL_RTC_Init() function.
172
173 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
174 not defined, the callback registration feature is not available and all callbacks
175 are set to the corresponding weak functions.
176 @endverbatim
177
178 */
179
180/* Includes ------------------------------------------------------------------*/
181#include "stm32h7xx_hal.h"
182
183/** @addtogroup STM32H7xx_HAL_Driver
184 * @{
185 */
186
187
188/** @addtogroup RTC
189 * @brief RTC HAL module driver
190 * @{
191 */
192
193#ifdef HAL_RTC_MODULE_ENABLED
194
195/* Private typedef -----------------------------------------------------------*/
196/* Private define ------------------------------------------------------------*/
197/* Private macro -------------------------------------------------------------*/
198/* Private variables ---------------------------------------------------------*/
199/* Private function prototypes -----------------------------------------------*/
200/* Exported functions --------------------------------------------------------*/
201
202/** @addtogroup RTC_Exported_Functions
203 * @{
204 */
205
206/** @addtogroup RTC_Exported_Functions_Group1
207 * @brief Initialization and Configuration functions
208 *
209@verbatim
210 ===============================================================================
211 ##### Initialization and de-initialization functions #####
212 ===============================================================================
213 [..] This section provides functions allowing to initialize and configure the
214 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
215 RTC registers Write protection, enter and exit the RTC initialization mode,
216 RTC registers synchronization check and reference clock detection enable.
217 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
218 It is split into 2 programmable prescalers to minimize power consumption.
219 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
220 (++) When both prescalers are used, it is recommended to configure the
221 asynchronous prescaler to a high value to minimize power consumption.
222 (#) All RTC registers are Write protected. Writing to the RTC registers
223 is enabled by writing a key into the Write Protection register, RTC_WPR.
224 (#) To configure the RTC Calendar, user application should enter
225 initialization mode. In this mode, the calendar counter is stopped
226 and its value can be updated. When the initialization sequence is
227 complete, the calendar restarts counting after 4 RTCCLK cycles.
228 (#) To read the calendar through the shadow registers after Calendar
229 initialization, calendar update or after wakeup from low power modes
230 the software must first clear the RSF flag. The software must then
231 wait until it is set again before reading the calendar, which means
232 that the calendar registers have been correctly copied into the
233 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
234 implements the above software sequence (RSF clear and RSF check).
235
236@endverbatim
237 * @{
238 */
239
240/**
241 * @brief Initialize the RTC peripheral
242 * @param hrtc RTC handle
243 * @retval HAL status
244 */
246{
248
249 /* Check RTC handler */
250 if(hrtc != NULL)
251 {
252 /* Check the parameters */
254 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
255 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
256 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
257 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
258 assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
259 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
260 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
261#if defined(TAMP)
262 assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
263#endif /* TAMP */
264
265#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
266 if(hrtc->State == HAL_RTC_STATE_RESET)
267 {
268 /* Allocate lock resource and initialize it */
269 hrtc->Lock = HAL_UNLOCKED;
270
271 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
272 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
273 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
274 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
275 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
276#if defined(RTC_TAMPER2_SUPPORT)
277 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
278#endif /* RTC_TAMPER2_SUPPORT */
279 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
280
281#if defined(TAMP)
282 hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback;
283 hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback;
284 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback;
285 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback;
286 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback;
287 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback;
288 hrtc->InternalTamper8EventCallback = HAL_RTCEx_InternalTamper8EventCallback;
289#endif /* TAMP */
290
291
292 if(hrtc->MspInitCallback == NULL)
293 {
294 hrtc->MspInitCallback = HAL_RTC_MspInit;
295 }
296 /* Init the low level hardware */
297 hrtc->MspInitCallback(hrtc);
298
299 if(hrtc->MspDeInitCallback == NULL)
300 {
301 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
302 }
303 }
304#else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
305 if(hrtc->State == HAL_RTC_STATE_RESET)
306 {
307 /* Allocate lock resource and initialize it */
308 hrtc->Lock = HAL_UNLOCKED;
309
310 /* Initialize RTC MSP */
312 }
313#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
314
315 /* Set RTC state */
316 hrtc->State = HAL_RTC_STATE_BUSY;
317
318 /* Check whether the calendar needs to be initialized */
320 {
321 /* Disable the write protection for RTC registers */
323
324 /* Enter Initialization mode */
326 if (status == HAL_OK)
327 {
328#if defined(TAMP)
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
332 /* Clear RTC_CR FMT, OSEL and POL Bits */
333 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL);
334#endif /* TAMP */
335
336 /* Set RTC_CR register */
337 hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
338
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);
341
342 /* Exit Initialization mode */
344 }
345 if(status == HAL_OK)
346 {
347#if defined(TAMP)
348 hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
349 hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
350#else
351 hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
352 hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
353#endif /* TAMP */
354 }
355
356 /* Enable the write protection for RTC registers */
358 }
359 else
360 {
361 /* The calendar is already initialized */
362 status = HAL_OK;
363 }
364
365 if (status == HAL_OK)
366 {
367 /* Set RTC state */
368 hrtc->State = HAL_RTC_STATE_READY;
369 }
370 }
371
372 /* return status */
373 return status;
374}
375
376/**
377 * @brief DeInitialize the RTC peripheral.
378 * @note This function doesn't reset the RTC Backup Data registers.
379 * @param hrtc RTC handle
380 * @retval HAL status
381 */
383{
385 uint32_t tickstart;
386
387 /* Check RTC handler */
388 if(hrtc != NULL)
389 {
390 /* Check the parameters */
392
393 /* Set RTC state */
394 hrtc->State = HAL_RTC_STATE_BUSY;
395
396 /* Disable the write protection for RTC registers */
398
399 /* Enter Initialization mode */
401
402 if (status == HAL_OK)
403 {
404 /* Reset TR, DR and CR registers */
405 hrtc->Instance->TR = 0x00000000U;
406 hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
407
408 /* Reset All CR bits except CR[2:0] (which cannot be written before bit
409 WUTE of CR is cleared) */
410 hrtc->Instance->CR = 0x00000000U;
411
412 /* Wait till WUTWF is set (to be able to reset CR[2:0] and WUTR) and if
413 timeout is reached exit */
414 tickstart = HAL_GetTick();
415
416#if defined(TAMP)
417 while ((((hrtc->Instance->ICSR) & RTC_ICSR_WUTWF) == 0U) && (status != HAL_TIMEOUT))
418#else
419 while ((((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == 0U) && (status != HAL_TIMEOUT))
420#endif /* TAMP */
421 {
422 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
423 {
424 /* Enable the write protection for RTC registers */
426
427 /* Set RTC state */
430
431 }
432 }
433 }
434
435 if (status == HAL_OK)
436 {
437 /* Reset RTC CR register bits [2:0] */
438 hrtc->Instance->CR = 0x00000000U;
439
440 /* Reset other RTC registers */
441 hrtc->Instance->WUTR = RTC_WUTR_WUT;
442 hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
443 hrtc->Instance->ALRMAR = 0x00000000U;
444 hrtc->Instance->ALRMBR = 0x00000000U;
445 hrtc->Instance->SHIFTR = 0x00000000U;
446 hrtc->Instance->CALR = 0x00000000U;
447 hrtc->Instance->ALRMASSR = 0x00000000U;
448 hrtc->Instance->ALRMBSSR = 0x00000000U;
449
450 /* Exit initialization mode */
452 }
453
454 if(status == HAL_OK)
455 {
456#if defined(TAMP)
457 /* Reset TAMP registers */
458 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR1 = 0xFFFF0000U;
459 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR2 = 0x00000000U;
460#else
461 /* Reset Tamper configuration register */
462 hrtc->Instance->TAMPCR = 0x00000000U;
463
464 /* Reset Option register */
465 hrtc->Instance->OR = 0x00000000U;
466#endif /* TAMP */
467
468 /* Enable the write protection for RTC registers */
470
471#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
472 if(hrtc->MspDeInitCallback == NULL)
473 {
474 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
475 }
476
477 /* DeInit the low level hardware: CLOCK, NVIC.*/
478 hrtc->MspDeInitCallback(hrtc);
479#else
480 /* De-Initialize RTC MSP */
482#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
483
484 hrtc->State = HAL_RTC_STATE_RESET;
485
486 /* Release Lock */
488 }
489 }
490
491 /* return status */
492 return status;
493}
494
495#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
496/**
497 * @brief Register a User RTC Callback
498 * To be used instead of the weak predefined callback
499 * @param hrtc RTC handle
500 * @param CallbackID ID of the callback to be registered
501 * This parameter can be one of the following values:
502 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
503 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
504 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
505 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
506 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
507 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID (*)
508 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
509 * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID (*)
510 * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID (*)
511 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID (*)
512 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID (*)
513 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID (*)
514 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID (*)
515 * @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID (*)
516 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
517 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
518 * (*) Not applicable to all devices.
519 * @param pCallback pointer to the Callback function
520 * @retval HAL status
521 */
522HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
523{
525
526 if(pCallback == NULL)
527 {
528 return HAL_ERROR;
529 }
530
531 /* Process locked */
533
534 if(HAL_RTC_STATE_READY == hrtc->State)
535 {
536 switch (CallbackID)
537 {
538 case HAL_RTC_ALARM_A_EVENT_CB_ID :
539 hrtc->AlarmAEventCallback = pCallback;
540 break;
541
542 case HAL_RTC_ALARM_B_EVENT_CB_ID :
543 hrtc->AlarmBEventCallback = pCallback;
544 break;
545
546 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
547 hrtc->TimeStampEventCallback = pCallback;
548 break;
549
550 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
551 hrtc->WakeUpTimerEventCallback = pCallback;
552 break;
553
554 case HAL_RTC_TAMPER1_EVENT_CB_ID :
555 hrtc->Tamper1EventCallback = pCallback;
556 break;
557
558#if defined(RTC_TAMPER2_SUPPORT)
559 case HAL_RTC_TAMPER2_EVENT_CB_ID :
560 hrtc->Tamper2EventCallback = pCallback;
561 break;
562#endif /* RTC_TAMPER2_SUPPORT */
563
564 case HAL_RTC_TAMPER3_EVENT_CB_ID :
565 hrtc->Tamper3EventCallback = pCallback;
566 break;
567
568#if defined(TAMP)
569 case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
570 hrtc->InternalTamper1EventCallback = pCallback;
571 break;
572
573 case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
574 hrtc->InternalTamper2EventCallback = pCallback;
575 break;
576
577 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
578 hrtc->InternalTamper3EventCallback = pCallback;
579 break;
580
581 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
582 hrtc->InternalTamper4EventCallback = pCallback;
583 break;
584
585 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
586 hrtc->InternalTamper5EventCallback = pCallback;
587 break;
588
589 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
590 hrtc->InternalTamper6EventCallback = pCallback;
591 break;
592
593 case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
594 hrtc->InternalTamper8EventCallback = pCallback;
595 break;
596#endif /* TAMP */
597
598 case HAL_RTC_MSPINIT_CB_ID :
599 hrtc->MspInitCallback = pCallback;
600 break;
601
602 case HAL_RTC_MSPDEINIT_CB_ID :
603 hrtc->MspDeInitCallback = pCallback;
604 break;
605
606 default :
607 /* Return error status */
609 break;
610 }
611 }
612 else if(HAL_RTC_STATE_RESET == hrtc->State)
613 {
614 switch (CallbackID)
615 {
616 case HAL_RTC_MSPINIT_CB_ID :
617 hrtc->MspInitCallback = pCallback;
618 break;
619
620 case HAL_RTC_MSPDEINIT_CB_ID :
621 hrtc->MspDeInitCallback = pCallback;
622 break;
623
624 default :
625 /* Return error status */
627 break;
628 }
629 }
630 else
631 {
632 /* Return error status */
634 }
635
636 /* Release Lock */
638
639 return status;
640}
641
642/**
643 * @brief Unregister an RTC Callback
644 * RTC callback is redirected to the weak predefined callback
645 * @param hrtc RTC handle
646 * @param CallbackID ID of the callback to be unregistered
647 * This parameter can be one of the following values:
648 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
649 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
650 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
651 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
652 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
653 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID (*)
654 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
655 * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID (*)
656 * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID (*)
657 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID (*)
658 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID (*)
659 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID (*)
660 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID (*)
661 * @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID (*)
662 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
663 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
664 * (*) Not applicable to all devices.
665 * @retval HAL status
666 */
667HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
668{
670
671 /* Process locked */
673
674 if(HAL_RTC_STATE_READY == hrtc->State)
675 {
676 switch (CallbackID)
677 {
678 case HAL_RTC_ALARM_A_EVENT_CB_ID :
679 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
680 break;
681
682 case HAL_RTC_ALARM_B_EVENT_CB_ID :
683 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
684 break;
685
686 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
687 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
688 break;
689
690 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
691 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
692 break;
693
694 case HAL_RTC_TAMPER1_EVENT_CB_ID :
695 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
696 break;
697
698#if defined(RTC_TAMPER2_SUPPORT)
699 case HAL_RTC_TAMPER2_EVENT_CB_ID :
700 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
701 break;
702#endif /* RTC_TAMPER2_SUPPORT */
703
704 case HAL_RTC_TAMPER3_EVENT_CB_ID :
705 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
706 break;
707
708#if defined(TAMP)
709 case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
710 hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback;
711 break;
712
713 case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
714 hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback;
715 break;
716
717 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
718 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback;
719 break;
720
721 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
722 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback;
723 break;
724
725 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
726 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback;
727 break;
728
729 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
730 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback;
731 break;
732
733 case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
734 hrtc->InternalTamper8EventCallback = HAL_RTCEx_InternalTamper8EventCallback;
735 break;
736#endif /* TAMP */
737
738 case HAL_RTC_MSPINIT_CB_ID :
739 hrtc->MspInitCallback = HAL_RTC_MspInit;
740 break;
741
742 case HAL_RTC_MSPDEINIT_CB_ID :
743 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
744 break;
745
746 default :
747 /* Return error status */
749 break;
750 }
751 }
752 else if(HAL_RTC_STATE_RESET == hrtc->State)
753 {
754 switch (CallbackID)
755 {
756 case HAL_RTC_MSPINIT_CB_ID :
757 hrtc->MspInitCallback = HAL_RTC_MspInit;
758 break;
759
760 case HAL_RTC_MSPDEINIT_CB_ID :
761 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
762 break;
763
764 default :
765 /* Return error status */
767 break;
768 }
769 }
770 else
771 {
772 /* Return error status */
774 }
775
776 /* Release Lock */
778
779 return status;
780}
781#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
782
783/**
784 * @brief Initialize the RTC MSP.
785 * @param hrtc RTC handle
786 * @retval None
787 */
789{
790 /* Prevent unused argument(s) compilation warning */
791 UNUSED(hrtc);
792
793 /* NOTE : This function should not be modified, when the callback is needed,
794 the HAL_RTC_MspInit could be implemented in the user file
795 */
796}
797
798/**
799 * @brief DeInitialize the RTC MSP.
800 * @param hrtc RTC handle
801 * @retval None
802 */
804{
805 /* Prevent unused argument(s) compilation warning */
806 UNUSED(hrtc);
807
808 /* NOTE : This function should not be modified, when the callback is needed,
809 the HAL_RTC_MspDeInit could be implemented in the user file
810 */
811}
812
813/**
814 * @}
815 */
816
817/** @addtogroup RTC_Exported_Functions_Group2
818 * @brief RTC Time and Date functions
819 *
820@verbatim
821 ===============================================================================
822 ##### RTC Time and Date functions #####
823 ===============================================================================
824
825 [..] This section provides functions allowing to configure Time and Date features
826
827@endverbatim
828 * @{
829 */
830
831/**
832 * @brief Set RTC current time.
833 * @param hrtc RTC handle
834 * @param sTime Pointer to Time structure
835 * @param Format Specifies the format of the entered parameters.
836 * This parameter can be one of the following values:
837 * @arg RTC_FORMAT_BIN: Binary data format
838 * @arg RTC_FORMAT_BCD: BCD data format
839 * @retval HAL status
840 */
842{
843 uint32_t tmpreg;
845
846 /* Check the parameters */
850
851 /* Process Locked */
853
854 hrtc->State = HAL_RTC_STATE_BUSY;
855
856 /* Disable the write protection for RTC registers */
858 /* Enter Initialization mode */
860 if (status == HAL_OK)
861 {
862 if(Format == RTC_FORMAT_BIN)
863 {
864 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
865 {
868 }
869 else
870 {
871 sTime->TimeFormat = 0x00U;
873 }
876
877 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
878 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
879 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
880 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
881 }
882 else
883 {
884 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
885 {
888 }
889 else
890 {
891 sTime->TimeFormat = 0x00U;
893 }
896 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
897 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
898 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
899 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
900 }
901
902 /* Set the RTC_TR register */
903 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
904
905 /* Clear the bits to be configured */
906 hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
907
908 /* Configure the RTC_CR register */
909 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
910
911 /* Exit Initialization mode */
913 }
914
915 /* Enable the write protection for RTC registers */
917
918 if (status == HAL_OK)
919 {
920 hrtc->State = HAL_RTC_STATE_READY;
921 }
922
923 /* Process Unlocked */
925
926 return status;
927}
928
929/**
930 * @brief Get RTC current time.
931 * @param hrtc RTC handle
932 * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
933 * with input format (BIN or BCD), also SubSeconds field returning the
934 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
935 * factor to be used for second fraction ratio computation.
936 * @param Format Specifies the format of the entered parameters.
937 * This parameter can be one of the following values:
938 * @arg RTC_FORMAT_BIN: Binary data format
939 * @arg RTC_FORMAT_BCD: BCD data format
940 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
941 * value in second fraction ratio with time unit following generic formula:
942 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
943 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
944 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
945 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
946 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
947 * to ensure consistency between the time and date values.
948 * @retval HAL status
949 */
951{
952 uint32_t tmpreg;
953
954 /* Check the parameters */
956
957 /* Get subseconds structure field from the corresponding register*/
958 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
959
960 /* Get SecondFraction structure field from the corresponding register field*/
961 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
962
963 /* Get the TR register */
964 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
965
966 /* Fill the structure fields with the read parameters */
967 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
968 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
969 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
970 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
971
972 /* Check the input parameters format */
973 if(Format == RTC_FORMAT_BIN)
974 {
975 /* Convert the time structure parameters to Binary format */
976 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
977 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
978 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
979 }
980
981 return HAL_OK;
982}
983
984/**
985 * @brief Set RTC current date.
986 * @param hrtc RTC handle
987 * @param sDate Pointer to date structure
988 * @param Format specifies the format of the entered parameters.
989 * This parameter can be one of the following values:
990 * @arg RTC_FORMAT_BIN: Binary data format
991 * @arg RTC_FORMAT_BCD: BCD data format
992 * @retval HAL status
993 */
995{
996 uint32_t datetmpreg;
998
999 /* Check the parameters */
1000 assert_param(IS_RTC_FORMAT(Format));
1001
1002 /* Process Locked */
1004
1005 hrtc->State = HAL_RTC_STATE_BUSY;
1006
1007 if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
1008 {
1009 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
1010 }
1011
1013
1014 if(Format == RTC_FORMAT_BIN)
1015 {
1016 assert_param(IS_RTC_YEAR(sDate->Year));
1018 assert_param(IS_RTC_DATE(sDate->Date));
1019
1020 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
1021 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
1022 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
1023 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
1024 }
1025 else
1026 {
1030
1031 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
1032 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
1033 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
1034 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
1035 }
1036
1037 /* Disable the write protection for RTC registers */
1039
1040
1041 /* Enter Initialization mode */
1043 if (status == HAL_OK)
1044 {
1045 /* Set the RTC_DR register */
1046 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
1047
1048
1049 /* Exit Initialization mode */
1051 }
1052
1053 /* Enable the write protection for RTC registers */
1055
1056 if (status == HAL_OK)
1057 {
1058 hrtc->State = HAL_RTC_STATE_READY;
1059 }
1060
1061 /* Process Unlocked */
1063
1064 return status;
1065
1066
1067}
1068
1069/**
1070 * @brief Get RTC current date.
1071 * @param hrtc RTC handle
1072 * @param sDate Pointer to Date structure
1073 * @param Format Specifies the format of the entered parameters.
1074 * This parameter can be one of the following values:
1075 * @arg RTC_FORMAT_BIN: Binary data format
1076 * @arg RTC_FORMAT_BCD: BCD data format
1077 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1078 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1079 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1080 * @retval HAL status
1081 */
1083{
1084 uint32_t datetmpreg;
1085
1086 /* Check the parameters */
1087 assert_param(IS_RTC_FORMAT(Format));
1088
1089 /* Get the DR register */
1090 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
1091
1092 /* Fill the structure fields with the read parameters */
1093 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1094 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1095 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1096 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
1097
1098 /* Check the input parameters format */
1099 if(Format == RTC_FORMAT_BIN)
1100 {
1101 /* Convert the date structure parameters to Binary format */
1102 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1103 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1104 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1105 }
1106 return HAL_OK;
1107}
1108
1109/**
1110 * @}
1111 */
1112
1113/** @addtogroup RTC_Exported_Functions_Group3
1114 * @brief RTC Alarm functions
1115 *
1116@verbatim
1117 ===============================================================================
1118 ##### RTC Alarm functions #####
1119 ===============================================================================
1120
1121 [..] This section provides functions allowing to configure Alarm feature
1122
1123@endverbatim
1124 * @{
1125 */
1126/**
1127 * @brief Set the specified RTC Alarm.
1128 * @param hrtc RTC handle
1129 * @param sAlarm Pointer to Alarm structure
1130 * @param Format Specifies the format of the entered parameters.
1131 * This parameter can be one of the following values:
1132 * @arg RTC_FORMAT_BIN: Binary data format
1133 * @arg RTC_FORMAT_BCD: BCD data format
1134 * @retval HAL status
1135 */
1137{
1138 uint32_t tickstart;
1139 uint32_t tmpreg;
1140 uint32_t subsecondtmpreg;
1141
1142 /* Check the parameters */
1143 assert_param(IS_RTC_FORMAT(Format));
1149
1150 /* Process Locked */
1152
1153 hrtc->State = HAL_RTC_STATE_BUSY;
1154
1155 if(Format == RTC_FORMAT_BIN)
1156 {
1157 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1158 {
1161 }
1162 else
1163 {
1164 sAlarm->AlarmTime.TimeFormat = 0x00U;
1166 }
1169
1171 {
1173 }
1174 else
1175 {
1177 }
1178
1179 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1180 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1181 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1182 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1183 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1184 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1185 ((uint32_t)sAlarm->AlarmMask));
1186 }
1187 else
1188 {
1189 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1190 {
1193 }
1194 else
1195 {
1196 sAlarm->AlarmTime.TimeFormat = 0x00U;
1198 }
1199
1202
1204 {
1206 }
1207 else
1208 {
1210 }
1211
1212 tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours << RTC_ALRMAR_HU_Pos) | \
1213 ((uint32_t)sAlarm->AlarmTime.Minutes << RTC_ALRMAR_MNU_Pos) | \
1214 ((uint32_t)sAlarm->AlarmTime.Seconds << RTC_ALRMAR_SU_Pos) | \
1215 ((uint32_t)sAlarm->AlarmTime.TimeFormat << RTC_ALRMAR_PM_Pos) | \
1216 ((uint32_t)sAlarm->AlarmDateWeekDay << RTC_ALRMAR_DU_Pos) | \
1217 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1218 ((uint32_t)sAlarm->AlarmMask));
1219 }
1220
1221 /* Configure the Alarm A or Alarm B Sub Second registers */
1222 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1223
1224 /* Disable the write protection for RTC registers */
1226
1227 /* Configure the Alarm register */
1228 if(sAlarm->Alarm == RTC_ALARM_A)
1229 {
1230 /* Disable the Alarm A interrupt */
1232 /* Clear flag alarm A */
1234 /* In case of interrupt mode is used, the interrupt source must disabled */
1236
1237 tickstart = HAL_GetTick();
1238 /* Wait till RTC ALRAWF flag is set and if timeout is reached exit */
1239#if defined(TAMP)
1240 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1241#else
1243#endif /* TAMP */
1244 {
1245 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1246 {
1247 /* Enable the write protection for RTC registers */
1249
1250 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1251
1252 /* Process Unlocked */
1254
1255 return HAL_TIMEOUT;
1256 }
1257 }
1258
1259 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1260 /* Configure the Alarm A Sub Second register */
1261 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1262 /* Configure the Alarm state: Enable Alarm */
1264 }
1265 else
1266 {
1267 /* Disable the Alarm B interrupt */
1269 /* Clear flag alarm B */
1271 /* In case of interrupt mode is used, the interrupt source must disabled */
1273
1274 tickstart = HAL_GetTick();
1275 /* Wait till RTC ALRBWF flag is set and if timeout is reached exit */
1276#if defined(TAMP)
1277 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1278#else
1280#endif /* TAMP */
1281 {
1282 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1283 {
1284 /* Enable the write protection for RTC registers */
1286
1287 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1288
1289 /* Process Unlocked */
1291
1292 return HAL_TIMEOUT;
1293 }
1294 }
1295
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 */
1301 }
1302
1303 /* Enable the write protection for RTC registers */
1305
1306 /* Change RTC state */
1307 hrtc->State = HAL_RTC_STATE_READY;
1308
1309 /* Process Unlocked */
1311
1312 return HAL_OK;
1313}
1314
1315/**
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
1327 */
1329{
1330 uint32_t tickstart;
1331 uint32_t tmpreg;
1332 uint32_t subsecondtmpreg;
1333
1334 /* Check the parameters */
1335 assert_param(IS_RTC_FORMAT(Format));
1341
1342 /* Process Locked */
1344
1345 hrtc->State = HAL_RTC_STATE_BUSY;
1346
1347 if(Format == RTC_FORMAT_BIN)
1348 {
1349 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1350 {
1353 }
1354 else
1355 {
1356 sAlarm->AlarmTime.TimeFormat = 0x00U;
1358 }
1361
1363 {
1365 }
1366 else
1367 {
1369 }
1370
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));
1378 }
1379 else
1380 {
1381 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1382 {
1385 }
1386 else
1387 {
1388 sAlarm->AlarmTime.TimeFormat = 0x00U;
1390 }
1391
1394
1396 {
1398 }
1399 else
1400 {
1402 }
1403
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));
1411 }
1412 /* Configure the Alarm A or Alarm B Sub Second registers */
1413 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1414
1415 /* Disable the write protection for RTC registers */
1417
1418 /* Configure the Alarm register */
1419 if(sAlarm->Alarm == RTC_ALARM_A)
1420 {
1421 /* Disable the Alarm A interrupt */
1423
1424 /* Clear flag alarm A */
1426
1427 tickstart = HAL_GetTick();
1428 /* Wait till RTC ALRAWF flag is set and if timeout is reached exit */
1429#if defined(TAMP)
1430 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1431#else
1433#endif /* TAMP */
1434 {
1435 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1436 {
1437 /* Enable the write protection for RTC registers */
1439
1440 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1441
1442 /* Process Unlocked */
1444
1445 return HAL_TIMEOUT;
1446 }
1447 }
1448
1449 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1450 /* Configure the Alarm A Sub Second register */
1451 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1452 /* Configure the Alarm state: Enable Alarm */
1454 /* Configure the Alarm interrupt */
1456 }
1457 else
1458 {
1459 /* Disable the Alarm B interrupt */
1461
1462 /* Clear flag alarm B */
1464
1465 tickstart = HAL_GetTick();
1466 /* Wait till RTC ALRBWF flag is set and if timeout is reached exit */
1467#if defined(TAMP)
1468 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1469#else
1471#endif /* TAMP */
1472 {
1473 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1474 {
1475 /* Enable the write protection for RTC registers */
1477
1478 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1479
1480 /* Process Unlocked */
1482
1483 return HAL_TIMEOUT;
1484 }
1485 }
1486
1487 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1488 /* Configure the Alarm B Sub Second register */
1489 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1490 /* Configure the Alarm state: Enable Alarm */
1492 /* Configure the Alarm interrupt */
1494 }
1495
1496 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1497#if defined(DUAL_CORE)
1499 {
1501 }
1502 else
1503 {
1504 __HAL_RTC_ALARM_EXTID2_ENABLE_IT();
1505 }
1506#else /* SINGLE_CORE */
1508#endif
1509
1511
1512 /* Enable the write protection for RTC registers */
1514
1515 hrtc->State = HAL_RTC_STATE_READY;
1516
1517 /* Process Unlocked */
1519
1520 return HAL_OK;
1521}
1522
1523/**
1524 * @brief Deactivate the specified RTC Alarm.
1525 * @param hrtc RTC handle
1526 * @param Alarm Specifies the Alarm.
1527 * This parameter can be one of the following values:
1528 * @arg RTC_ALARM_A: AlarmA
1529 * @arg RTC_ALARM_B: AlarmB
1530 * @retval HAL status
1531 */
1533{
1534 uint32_t tickstart;
1535
1536 /* Check the parameters */
1537 assert_param(IS_RTC_ALARM(Alarm));
1538
1539 /* Process Locked */
1541
1542 hrtc->State = HAL_RTC_STATE_BUSY;
1543
1544 /* Disable the write protection for RTC registers */
1546
1547 if(Alarm == RTC_ALARM_A)
1548 {
1549 /* AlarmA */
1551
1552 /* In case of interrupt mode is used, the interrupt source must disabled */
1554
1555 tickstart = HAL_GetTick();
1556
1557 /* Wait till RTC ALRxWF flag is set and if timeout is reached exit */
1558#if defined(TAMP)
1559 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1560#else
1562#endif /* TAMP */
1563 {
1564 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1565 {
1566 /* Enable the write protection for RTC registers */
1568
1569 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1570
1571 /* Process Unlocked */
1573
1574 return HAL_TIMEOUT;
1575 }
1576 }
1577 }
1578 else
1579 {
1580 /* AlarmB */
1582
1583 /* In case of interrupt mode is used, the interrupt source must disabled */
1585
1586 tickstart = HAL_GetTick();
1587
1588 /* Wait till RTC ALRxWF flag is set and if timeout is reached exit */
1589#if defined(TAMP)
1590 while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1591#else
1593#endif /* TAMP */
1594 {
1595 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1596 {
1597 /* Enable the write protection for RTC registers */
1599
1600 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1601
1602 /* Process Unlocked */
1604
1605 return HAL_TIMEOUT;
1606 }
1607 }
1608 }
1609 /* Enable the write protection for RTC registers */
1611
1612 hrtc->State = HAL_RTC_STATE_READY;
1613
1614 /* Process Unlocked */
1616
1617 return HAL_OK;
1618}
1619
1620/**
1621 * @brief Get the RTC Alarm value and masks.
1622 * @param hrtc RTC handle
1623 * @param sAlarm Pointer to Date structure
1624 * @param Alarm Specifies the Alarm.
1625 * This parameter can be one of the following values:
1626 * @arg RTC_ALARM_A: AlarmA
1627 * @arg RTC_ALARM_B: AlarmB
1628 * @param Format Specifies the format of the entered parameters.
1629 * This parameter can be one of the following values:
1630 * @arg RTC_FORMAT_BIN: Binary data format
1631 * @arg RTC_FORMAT_BCD: BCD data format
1632 * @retval HAL status
1633 */
1634HAL_StatusTypeDef HAL_RTC_GetAlarm(const RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1635{
1636 uint32_t tmpreg;
1637 uint32_t subsecondtmpreg;
1638
1639 /* Check the parameters */
1640 assert_param(IS_RTC_FORMAT(Format));
1641 assert_param(IS_RTC_ALARM(Alarm));
1642
1643 if(Alarm == RTC_ALARM_A)
1644 {
1645 /* AlarmA */
1646 sAlarm->Alarm = RTC_ALARM_A;
1647
1648 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1649 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
1650
1651 /* Fill the structure with the read parameters */
1652 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1653 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1654 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1655 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
1656 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1657 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1658 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1659 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1660 }
1661 else
1662 {
1663 sAlarm->Alarm = RTC_ALARM_B;
1664
1665 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1666 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1667
1668 /* Fill the structure with the read parameters */
1669 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1670 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1671 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1672 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1673 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1674 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1675 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1676 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1677 }
1678
1679 if(Format == RTC_FORMAT_BIN)
1680 {
1681 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1685 }
1686
1687 return HAL_OK;
1688}
1689
1690/**
1691 * @brief Handle Alarm interrupt request.
1692 * @param hrtc RTC handle
1693 * @retval None
1694 */
1696{
1697 /* Clear the EXTI's line Flag for RTC Alarm */
1698#if defined(DUAL_CORE)
1700 {
1702 }
1703 else
1704 {
1705 __HAL_RTC_ALARM_EXTID2_CLEAR_FLAG();
1706 }
1707#else /* SINGLE_CORE */
1709#endif /* DUAL_CORE */
1710
1711#if defined(TAMP)
1712 /* Get interrupt status */
1713 uint32_t tmp = hrtc->Instance->MISR;
1714
1715 if((tmp & RTC_FLAG_ALRAF) != 0u)
1716 {
1717 /* Clear the AlarmA interrupt pending bit */
1719
1720 /* Call Alarm A Callback */
1721#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1722 hrtc->AlarmAEventCallback(hrtc);
1723#else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1725#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1726 }
1727
1728 if((tmp & RTC_MISR_ALRBMF) != 0u)
1729 {
1730 /* Clear the AlarmB interrupt pending bit */
1731 hrtc->Instance->SCR = RTC_SCR_CALRBF;
1732
1733 /* Call Alarm B Callback */
1734#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1735 hrtc->AlarmBEventCallback(hrtc);
1736#else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1738#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1739 }
1740#else
1741 /* Get the AlarmA interrupt source enable status */
1743 {
1744 /* Get the pending status of the AlarmA Interrupt */
1746 {
1747 /* Clear the AlarmA interrupt pending bit */
1749
1750#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1751 hrtc->AlarmAEventCallback(hrtc);
1752#else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1754#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1755 }
1756 }
1757
1758 /* Get the AlarmB interrupt source enable status */
1760 {
1761 /* Get the pending status of the AlarmB Interrupt */
1763 {
1764 /* Clear the AlarmB interrupt pending bit */
1766
1767 /* AlarmB callback */
1768#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1769 hrtc->AlarmBEventCallback(hrtc);
1770#else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1772#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1773 }
1774 }
1775#endif /* TAMP */
1776
1777 /* Change RTC state */
1778 hrtc->State = HAL_RTC_STATE_READY;
1779}
1780
1781/**
1782 * @brief Alarm A callback.
1783 * @param hrtc RTC handle
1784 * @retval None
1785 */
1787{
1788 /* Prevent unused argument(s) compilation warning */
1789 UNUSED(hrtc);
1790
1791 /* NOTE : This function should not be modified, when the callback is needed,
1792 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1793 */
1794}
1795
1796/**
1797 * @brief Handle AlarmA Polling request.
1798 * @param hrtc RTC handle
1799 * @param Timeout Timeout duration
1800 * @retval HAL status
1801 */
1803{
1804
1805 uint32_t tickstart = HAL_GetTick();
1806
1808 {
1809 if(Timeout != HAL_MAX_DELAY)
1810 {
1811 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1812 {
1813 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1814 return HAL_TIMEOUT;
1815 }
1816 }
1817 }
1818
1819 /* Clear the Alarm interrupt pending bit */
1821
1822 /* Change RTC state */
1823 hrtc->State = HAL_RTC_STATE_READY;
1824
1825 return HAL_OK;
1826}
1827
1828/**
1829 * @}
1830 */
1831
1832/** @addtogroup RTC_Exported_Functions_Group4
1833 * @brief Peripheral Control functions
1834 *
1835@verbatim
1836 ===============================================================================
1837 ##### Peripheral Control functions #####
1838 ===============================================================================
1839 [..]
1840 This subsection provides functions allowing to
1841 (+) Wait for RTC Time and Date Synchronization
1842
1843@endverbatim
1844 * @{
1845 */
1846
1847/**
1848 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1849 * synchronized with RTC APB clock.
1850 * @note The RTC Resynchronization mode is write protected, use the
1851 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1852 * @note To read the calendar through the shadow registers after Calendar
1853 * initialization, calendar update or after wakeup from low power modes
1854 * the software must first clear the RSF flag.
1855 * The software must then wait until it is set again before reading
1856 * the calendar, which means that the calendar registers have been
1857 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1858 * @param hrtc RTC handle
1859 * @retval HAL status
1860 */
1862{
1863 uint32_t tickstart;
1864
1865 /* Clear RSF flag, keep reserved bits at reset values (setting other flags has no effect) */
1866#if defined(TAMP)
1867 hrtc->Instance->ICSR = ((uint32_t)(RTC_RSF_MASK & RTC_ICSR_RESERVED_MASK));
1868#else
1869 hrtc->Instance->ISR = ((uint32_t)(RTC_RSF_MASK & RTC_ISR_RESERVED_MASK));
1870#endif /* TAMP */
1871
1872 tickstart = HAL_GetTick();
1873
1874 /* Wait the registers to be synchronised */
1875#if defined(TAMP)
1876 while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
1877#else
1878 while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
1879#endif /* TAMP */
1880 {
1881 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1882 {
1883 return HAL_TIMEOUT;
1884 }
1885 }
1886
1887 return HAL_OK;
1888}
1889
1890/**
1891 * @}
1892 */
1893
1894/** @addtogroup RTC_Exported_Functions_Group5
1895 * @brief Peripheral State functions
1896 *
1897@verbatim
1898 ===============================================================================
1899 ##### Peripheral State functions #####
1900 ===============================================================================
1901 [..]
1902 This subsection provides functions allowing to
1903 (+) Get RTC state
1904
1905@endverbatim
1906 * @{
1907 */
1908/**
1909 * @brief Return the RTC handle state.
1910 * @param hrtc RTC handle
1911 * @retval HAL state
1912 */
1914{
1915 /* Return RTC handle state */
1916 return hrtc->State;
1917}
1918
1919/**
1920 * @}
1921 */
1922
1923/**
1924 * @}
1925 */
1926
1927/** @addtogroup RTC_Private_Functions
1928 * @{
1929 */
1930/**
1931 * @brief Enter the RTC Initialization mode.
1932 * @note The RTC Initialization mode is write protected, use the
1933 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1934 * @param hrtc RTC handle
1935 * @retval HAL status
1936 */
1938{
1939 uint32_t tickstart;
1941 /* Check if the Initialization mode is set */
1942#if defined(TAMP)
1943 if ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
1944 {
1945 /* Set the Initialization mode */
1946 SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
1947
1948 tickstart = HAL_GetTick();
1949
1950 /* Wait till RTC is in INIT state and if timeout is reached exit */
1951 while (((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
1952#else
1953 if ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
1954 {
1955 /* Set the Initialization mode */
1956 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1957
1958 tickstart = HAL_GetTick();
1959
1960 /* Wait till RTC is in INIT state and if timeout is reached exit */
1961 while (((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U) && (status != HAL_TIMEOUT))
1962#endif /* TAMP */
1963 {
1964 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1965 {
1967 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1968 }
1969 }
1970 }
1971
1972 return status;
1973}
1974
1975/**
1976 * @brief Exit the RTC Initialization mode.
1977 * @param hrtc RTC handle
1978 * @retval HAL status
1979 */
1981{
1983
1984 /* Check if the Initialization mode is set */
1985
1986 /* Exit Initialization mode */
1987#if defined(TAMP)
1988 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
1989#else
1990 CLEAR_BIT(RTC->ISR, RTC_ISR_INIT);
1991#endif /* TAMP */
1992
1993 /* If CR_BYPSHAD bit = 0, wait for synchro */
1994 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1995 {
1997 {
1998 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2000 }
2001 }
2002 else
2003 {
2004 /* Clear BYPSHAD bit */
2007 {
2008 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2010 }
2011 /* Restore BYPSHAD bit */
2013 }
2014
2015 return status;
2016}
2017
2018/**
2019 * @brief Convert a 2 digit decimal to BCD format.
2020 * @param Value Byte to be converted
2021 * @retval Converted byte
2022 */
2023uint8_t RTC_ByteToBcd2(uint8_t Value)
2024{
2025 uint32_t bcdhigh = 0U;
2026 uint8_t bcdlow = Value;
2027
2028 while (bcdlow >= 10U)
2029 {
2030 bcdhigh++;
2031 bcdlow -= 10U;
2032 }
2033
2034 return ((uint8_t)(bcdhigh << 4U) | bcdlow);
2035}
2036
2037/**
2038 * @brief Convert from 2 digit BCD to Binary.
2039 * @param Value BCD value to be converted
2040 * @retval Converted word
2041 */
2042uint8_t RTC_Bcd2ToByte(uint8_t Value)
2043{
2044 uint8_t tmp;
2045 tmp = ((Value & 0xF0U) >> 4U) * 10U;
2046 return (tmp + (Value & 0x0FU));
2047}
2048
2049/**
2050 * @}
2051 */
2052
2053#endif /* HAL_RTC_MODULE_ENABLED */
2054/**
2055 * @}
2056 */
2057
2058/**
2059 * @}
2060 */
2061
#define CM7_CPUID
uint32_t HAL_GetCurrentCPUID(void)
#define CLEAR_BIT(REG, BIT)
Definition stm32h7xx.h:209
#define SET_BIT(REG, BIT)
Definition stm32h7xx.h:207
#define READ_BIT(REG, BIT)
Definition stm32h7xx.h:211
#define IS_RTC_ALL_INSTANCE(INSTANCE)
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define RTC_ALRMAR_MNT
#define RTC_ALRMBR_MNT
#define RTC_ALRMAR_ST
#define RTC_ALRMBR_DU
#define RTC_ISR_WUTWF
#define RTC_CR_BKP
#define RTC_DR_DU_0
0x00000001
#define RTC_TR_HU_Pos
#define RTC_DR_YT
#define RTC_PRER_PREDIV_S
#define RTC_ALRMBR_HU
#define RTC_ALRMAR_MNU
#define RTC_PRER_PREDIV_S_Pos
#define RTC_DR_MT
#define RTC_TR_PM_Pos
#define RTC_TR_SU_Pos
#define RTC_WUTR_WUT
#define RTC_DR_WDU_0
0x00002000
#define RTC_ALRMAR_SU_Pos
#define RTC_TR_PM
#define RTC_ALRMBSSR_SS
#define RTC_CR_BYPSHAD
#define RTC_ALRMBR_WDSEL
#define RTC_ALRMBR_MNU
#define RTC_DR_DU_Pos
#define RTC_ALRMAR_HU
#define RTC_ALRMASSR_SS
#define RTC_ALRMBR_PM
#define RTC_DR_DT
#define RTC_CR_POL
#define RTC_DR_MU_0
0x00000100
#define RTC_ALRMBR_HT
#define RTC_ALRMBR_DU_Pos
#define RTC_ALRMAR_HT
#define RTC_DR_MU_Pos
#define RTC_ALRMAR_WDSEL
#define RTC_TR_MNT
#define RTC_DR_WDU
#define RTC_TR_SU
#define RTC_DR_WDU_Pos
#define RTC_ALRMAR_DU
#define RTC_ALRMBR_PM_Pos
#define RTC_TR_MNU
#define RTC_ALRMAR_PM_Pos
#define RTC_CR_OSEL
#define RTC_ALRMAR_HU_Pos
#define RTC_OR_OUT_RMP
#define RTC_ALRMAR_DT
#define RTC_ISR_RSF
#define RTC_ALRMBR_DT
#define RTC_ALRMAR_DU_Pos
#define RTC_ALRMAR_PM
#define RTC_TR_ST
#define RTC_ISR_INITF
#define RTC_CR_FMT
#define RTC_TR_MNU_Pos
#define RTC_ALRMAR_SU
#define RTC_DR_DU
#define RTC_OR_ALARMOUTTYPE
#define RTC_ALRMBR_MNU_Pos
#define RTC_ALRMAR_MNU_Pos
#define RTC_TR_HU
#define RTC_DR_MU
#define RTC_PRER_PREDIV_A
#define RTC_TR_HT
#define RTC_ALRMBR_ST
#define RTC_ISR_INIT
#define RTC_ALRMBR_SU
#define RTC_PRER_PREDIV_A_Pos
#define RTC_ALRMBR_SU_Pos
#define RTC_DR_YU_Pos
#define RTC_ALRMBR_HU_Pos
#define RTC_DR_YU
#define RTC
void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
Definition board_sleep.c:18
void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
#define RTC_ALARMDATEWEEKDAYSEL_DATE
#define RTC_ALARMMASK_ALL
#define RTC_ALARM_B
#define RTC_ALARM_A
HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
RTC MSP De-Initialization This function freeze the hardware resources used in this example.
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
RTC MSP Initialization This function configures the hardware resources used in this example.
HAL_StatusTypeDef HAL_RTC_GetDate(const RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
HAL_StatusTypeDef HAL_RTC_GetTime(const RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
HAL_StatusTypeDef HAL_RTC_GetAlarm(const RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
HAL_RTCStateTypeDef HAL_RTC_GetState(const RTC_HandleTypeDef *hrtc)
#define __HAL_RTC_ALARMB_DISABLE(__HANDLE__)
Disable the RTC ALARMB peripheral.
#define __HAL_RTC_ALARM_EXTI_ENABLE_IT()
Enable interrupt on the RTC Alarm associated Exti line.
#define __HAL_RTC_ALARM_EXTI_CLEAR_FLAG()
Clear the RTC Alarm associated Exti line flag.
#define __HAL_RTC_ALARM_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__)
Check whether the specified RTC Alarm interrupt has been enabled or not.
#define __HAL_RTC_ALARMA_DISABLE(__HANDLE__)
Disable the RTC ALARMA peripheral.
#define __HAL_RTC_ALARMA_ENABLE(__HANDLE__)
Enable the RTC ALARMA peripheral.
#define __HAL_RTC_ALARM_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clear the RTC Alarm's pending flags.
#define __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE()
Enable rising edge trigger on the RTC Alarm associated Exti line.
#define __HAL_RTC_WRITEPROTECTION_ENABLE(__HANDLE__)
Enable the write protection for RTC registers.
#define __HAL_RTC_ALARM_GET_FLAG(__HANDLE__, __FLAG__)
Get the selected RTC Alarm's flag status.
#define __HAL_RTC_ALARM_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the RTC Alarm interrupt.
#define __HAL_RTC_IS_CALENDAR_INITIALIZED(__HANDLE__)
Check whether the RTC Calendar is initialized.
#define __HAL_RTC_ALARMB_ENABLE(__HANDLE__)
Enable the RTC ALARMB peripheral.
#define __HAL_RTC_ALARM_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the RTC Alarm interrupt.
#define __HAL_RTC_WRITEPROTECTION_DISABLE(__HANDLE__)
Disable the write protection for RTC registers.
HAL_RTCStateTypeDef
HAL State structures definition.
@ HAL_RTC_STATE_TIMEOUT
RTC timeout state.
@ HAL_RTC_STATE_READY
RTC initialized and ready for use.
@ HAL_RTC_STATE_BUSY
RTC process is ongoing.
@ HAL_RTC_STATE_RESET
RTC not yet initialized or disabled.
#define RTC_FLAG_ALRBF
#define RTC_FLAG_ALRBWF
#define RTC_FLAG_ALRAWF
#define RTC_FLAG_ALRAF
#define IS_RTC_FORMAT(FORMAT)
#define IS_RTC_ALARM_MASK(MASK)
#define IS_RTC_OUTPUT_POL(POL)
#define IS_RTC_SYNCH_PREDIV(PREDIV)
#define IS_RTC_DATE(DATE)
#define IS_RTC_HOUR24(HOUR)
#define IS_RTC_DAYLIGHT_SAVING(SAVE)
#define IS_RTC_STORE_OPERATION(OPERATION)
#define IS_RTC_HOUR_FORMAT(FORMAT)
#define IS_RTC_OUTPUT_TYPE(TYPE)
#define IS_RTC_HOUR12(HOUR)
#define IS_RTC_MONTH(MONTH)
#define IS_RTC_MINUTES(MINUTES)
#define IS_RTC_ALARM_DATE_WEEKDAY_DATE(DATE)
#define IS_RTC_OUTPUT_REMAP(REMAP)
#define IS_RTC_HOURFORMAT12(PM)
#define IS_RTC_ALARM(ALARM)
#define IS_RTC_OUTPUT(OUTPUT)
#define IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(WEEKDAY)
#define IS_RTC_ASYNCH_PREDIV(PREDIV)
#define IS_RTC_ALARM_DATE_WEEKDAY_SEL(SEL)
#define IS_RTC_SECONDS(SECONDS)
#define IS_RTC_YEAR(YEAR)
#define IS_RTC_ALARM_SUB_SECOND_MASK(MASK)
#define IS_RTC_ALARM_SUB_SECOND_VALUE(VALUE)
#define IS_RTC_WEEKDAY(WEEKDAY)
#define RTC_FORMAT_BIN
#define RTC_IT_ALRA
Enable Alarm A Interrupt.
#define RTC_IT_ALRB
Enable Alarm B Interrupt.
#define RTC_INIT_MASK
#define RTC_DR_RESERVED_MASK
#define RTC_RSF_MASK
#define RTC_ISR_RESERVED_MASK
#define RTC_TIMEOUT_VALUE
#define RTC_TR_RESERVED_MASK
HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
uint8_t RTC_ByteToBcd2(uint8_t Value)
uint8_t RTC_Bcd2ToByte(uint8_t Value)
#define NULL
Definition usbd_def.h:44
RTC_HandleTypeDef hrtc
Definition main.c:52
This file contains all the functions prototypes for the HAL module driver.
#define assert_param(expr)
Uncomment the line below to expanse the "assert_param" macro in the HAL drivers code.
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_TIMEOUT
@ HAL_ERROR
@ HAL_OK
#define __HAL_UNLOCK(__HANDLE__)
#define UNUSED(x)
#define HAL_MAX_DELAY
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)
RTC Alarm structure definition.
uint8_t AlarmDateWeekDay
Specifies the RTC Alarm Date/WeekDay.
RTC_TimeTypeDef AlarmTime
Specifies the RTC Alarm Time members.
uint32_t Alarm
Specifies the alarm .
uint32_t AlarmDateWeekDaySel
Specifies the RTC Alarm is on Date or WeekDay.
uint32_t AlarmMask
Specifies the RTC Alarm Masks.
uint32_t AlarmSubSecondMask
Specifies the RTC Alarm SubSeconds Masks.
RTC Date structure definition.
uint8_t Year
Specifies the RTC Date Year.
uint8_t Month
Specifies the RTC Date Month (in BCD format).
uint8_t Date
Specifies the RTC Date.
uint8_t WeekDay
Specifies the RTC Date WeekDay.
RTC Handle Structure definition.
RTC Time structure definition.
uint8_t Seconds
Specifies the RTC Time Seconds.
uint8_t Minutes
Specifies the RTC Time Minutes.
uint8_t TimeFormat
Specifies the RTC AM/PM Time.
uint32_t StoreOperation
Specifies RTC_StoreOperation value to be written in the BKP bit in CR register to store the operation...
uint32_t SecondFraction
Specifies the range or granularity of Sub Second register content corresponding to Synchronous pre-sc...
uint32_t DayLightSaving
Specifies RTC_DayLightSaveOperation: the value of hour adjustment.
uint32_t SubSeconds
Specifies the RTC_SSR RTC Sub Second register content.
uint8_t Hours
Specifies the RTC Time Hour.