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