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