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