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