1 /**
2 ******************************************************************************
3 * @file stm32l4xx_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) 2017 STMicroelectronics.
22 * All rights reserved.
23 *
24 * This software is licensed under terms that can be found in the LICENSE file
25 * in the root directory of this software component.
26 * If no LICENSE file comes with this software, it is provided AS-IS.
27 *
28 ******************************************************************************
29 @verbatim
30 ===============================================================================
31 ##### RTC Operating Condition #####
32 ===============================================================================
33 [..] The real-time clock (RTC) and the RTC backup registers can be powered
34 from the VBAT voltage when the main VDD supply is powered off.
35 To retain the content of the RTC backup registers and supply the RTC
36 when VDD is turned off, VBAT pin can be connected to an optional
37 standby voltage supplied by a battery or by another source.
38
39 ##### Backup Domain Reset #####
40 ===============================================================================
41 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
42 to their reset values.
43 A backup domain reset is generated when one of the following events occurs:
44 (#) Software reset, triggered by setting the BDRST bit in the
45 RCC Backup domain control register (RCC_BDCR).
46 (#) VDD or VBAT power on, if both supplies have previously been powered off.
47 (#) Tamper detection event resets all data backup registers.
48
49 ##### Backup Domain Access #####
50 ==================================================================
51 [..] After reset, the backup domain (RTC registers 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
106 [..]
107 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
108 not defined, the callback registration feature is not available and all callbacks
109 are set to the corresponding weak functions. This is the recommended configuration
110 in order to optimize memory/code consumption footprint/performances.
111
112 [..]
113 The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
114 allows the user to configure dynamically the driver callbacks.
115 Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
116
117 [..]
118 Function HAL_RTC_RegisterCallback() allows to register following callbacks:
119 (+) AlarmAEventCallback : RTC Alarm A Event callback.
120 (+) AlarmBEventCallback : RTC Alarm B Event callback.
121 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
122 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
123 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
124 (+) SSRUEventCallback : RTC SSRU Event callback.
125 #endif
126 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
127 (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
128 (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
129 (+) MspInitCallback : RTC MspInit callback.
130 (+) MspDeInitCallback : RTC MspDeInit callback.
131 This function takes as parameters the HAL peripheral handle, the Callback ID
132 and a pointer to the user callback function.
133
134 [..]
135 Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
136 weak function.
137 HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
138 and the Callback ID.
139 This function allows to reset following callbacks:
140 (+) AlarmAEventCallback : RTC Alarm A Event callback.
141 (+) AlarmBEventCallback : RTC Alarm B Event callback.
142 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
143 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
144 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
145 (+) SSRUEventCallback : RTC SSRU Event callback.
146 #endif
147 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
148 (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
149 (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
150 (+) MspInitCallback : RTC MspInit callback.
151 (+) MspDeInitCallback : RTC MspDeInit callback.
152
153 [..]
154 By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
155 all callbacks are set to the corresponding weak functions :
156 examples AlarmAEventCallback(), TimeStampEventCallback().
157 Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
158 in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
159 (not registered beforehand).
160 If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
161 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
162
163 [..]
164 Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
165 Exception done MspInit/MspDeInit that can be registered/unregistered
166 in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
167 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
168 In that case first register the MspInit/MspDeInit user callbacks
169 using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
170 or HAL_RTC_Init() function.
171
172 [..]
173 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
174 not defined, the callback registration feature is not available and all callbacks
175 are set to the corresponding weak functions.
176
177 @endverbatim
178 ******************************************************************************
179 */
180
181 /* Includes ------------------------------------------------------------------*/
182 #include "stm32l4xx_hal.h"
183
184 /** @addtogroup STM32L4xx_HAL_Driver
185 * @{
186 */
187
188
189 /** @addtogroup RTC
190 * @brief RTC HAL module driver
191 * @{
192 */
193
194 #ifdef HAL_RTC_MODULE_ENABLED
195
196 /* Private typedef -----------------------------------------------------------*/
197 /* Private define ------------------------------------------------------------*/
198 /* Private macro -------------------------------------------------------------*/
199 /* Private variables ---------------------------------------------------------*/
200 /* Private function prototypes -----------------------------------------------*/
201 /* Exported functions --------------------------------------------------------*/
202
203 /** @addtogroup RTC_Exported_Functions
204 * @{
205 */
206
207 /** @addtogroup RTC_Exported_Functions_Group1
208 * @brief Initialization and Configuration functions
209 *
210 @verbatim
211 ===============================================================================
212 ##### Initialization and de-initialization functions #####
213 ===============================================================================
214 [..] This section provides functions allowing to initialize and configure the
215 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
216 RTC registers Write protection, enter and exit the RTC initialization mode,
217 RTC registers synchronization check and reference clock detection enable.
218 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
219 It is split into 2 programmable prescalers to minimize power consumption.
220 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
221 (++) When both prescalers are used, it is recommended to configure the
222 asynchronous prescaler to a high value to minimize power consumption.
223 (#) All RTC registers are Write protected. Writing to the RTC registers
224 is enabled by writing a key into the Write Protection register, RTC_WPR.
225 (#) To configure the RTC Calendar, user application should enter
226 initialization mode. In this mode, the calendar counter is stopped
227 and its value can be updated. When the initialization sequence is
228 complete, the calendar restarts counting after 4 RTCCLK cycles.
229 (#) To read the calendar through the shadow registers after Calendar
230 initialization, calendar update or after wakeup from low power modes
231 the software must first clear the RSF flag. The software must then
232 wait until it is set again before reading the calendar, which means
233 that the calendar registers have been correctly copied into the
234 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
235 implements the above software sequence (RSF clear and RSF check).
236
237 @endverbatim
238 * @{
239 */
240
241 /**
242 * @brief Initialize the RTC peripheral
243 * @param hrtc RTC handle
244 * @retval HAL status
245 */
HAL_RTC_Init(RTC_HandleTypeDef * hrtc)246 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
247 {
248 HAL_StatusTypeDef status = HAL_ERROR;
249
250 /* Check the RTC peripheral state */
251 if (hrtc != NULL)
252 {
253 /* Check the parameters */
254 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
255 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
256 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
257 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
258 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
259 assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
260 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
261 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
262 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
263 assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
264 #endif
265
266 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
267 assert_param(IS_RTC_BINARY_MODE(hrtc->Init.BinMode));
268 assert_param(IS_RTC_BINARY_MIX_BCDU(hrtc->Init.BinMixBcdU));
269 #endif
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 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
277 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
278 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
279 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
280 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
281 hrtc->SSRUEventCallback = HAL_RTCEx_SSRUEventCallback; /* Legacy weak SSRUEventCallback */
282 #endif
283
284 #if defined(RTC_TAMPER1_SUPPORT)
285 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
286 #endif /* RTC_TAMPER1_SUPPORT */
287 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
288 #if defined(RTC_TAMPER3_SUPPORT)
289 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
290 #endif /* RTC_TAMPER3_SUPPORT */
291
292 if (hrtc->MspInitCallback == NULL)
293 {
294 hrtc->MspInitCallback = HAL_RTC_MspInit;
295 }
296 /* Init the low level hardware */
297 hrtc->MspInitCallback(hrtc);
298
299 if (hrtc->MspDeInitCallback == NULL)
300 {
301 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
302 }
303 }
304 #else /* #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
305 if (hrtc->State == HAL_RTC_STATE_RESET)
306 {
307 /* Allocate lock resource and initialize it */
308 hrtc->Lock = HAL_UNLOCKED;
309
310 /* Initialize RTC MSP */
311 HAL_RTC_MspInit(hrtc);
312 }
313 #endif /* #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
314
315 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
316 /* Process TAMP ip offset from RTC one */
317 hrtc->TampOffset = (TAMP_BASE - RTC_BASE);
318 #endif
319 /* Set RTC state */
320 hrtc->State = HAL_RTC_STATE_BUSY;
321
322 /* Disable the write protection for RTC registers */
323 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
324
325 /* Enter Initialization mode */
326 status = RTC_EnterInitMode(hrtc);
327
328 if (status == HAL_OK)
329 {
330 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
331 /* Clear RTC_CR FMT, OSEL, POL and TAMPOE Bits */
332 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE);
333 #else
334 /* Clear RTC_CR FMT, OSEL and POL Bits */
335 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL);
336 #endif
337 /* Set RTC_CR register */
338 hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
339
340 /* Configure the RTC PRER */
341 hrtc->Instance->PRER = (hrtc->Init.SynchPrediv);
342 hrtc->Instance->PRER |= (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos);
343
344 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
345 /* Configure the Binary mode */
346 MODIFY_REG(RTC->ICSR, RTC_ICSR_BIN | RTC_ICSR_BCDU, hrtc->Init.BinMode | hrtc->Init.BinMixBcdU);
347 #endif
348
349 /* Exit Initialization mode */
350 status = RTC_ExitInitMode(hrtc);
351
352 if (status == HAL_OK)
353 {
354 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
355 hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
356 hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
357 #else
358 hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
359 hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
360 #endif
361
362 /* Enable the write protection for RTC registers */
363 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
364
365 hrtc->State = HAL_RTC_STATE_READY;
366 }
367 }
368 }
369
370 return status;
371 }
372
373 /**
374 * @brief DeInitialize the RTC peripheral.
375 * @note This function does not reset the RTC Backup Data registers.
376 * @param hrtc RTC handle
377 * @retval HAL status
378 */
HAL_RTC_DeInit(RTC_HandleTypeDef * hrtc)379 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
380 {
381 HAL_StatusTypeDef status = HAL_ERROR;
382
383 /* Check the RTC peripheral state */
384 if (hrtc != NULL)
385 {
386 /* Check the parameters */
387 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
388
389 /* Set RTC state */
390 hrtc->State = HAL_RTC_STATE_BUSY;
391
392 /* Disable the write protection for RTC registers */
393 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
394
395 /* Enter Initialization mode */
396 status = RTC_EnterInitMode(hrtc);
397
398 if (status == HAL_OK)
399 {
400 /* Reset all RTC CR register bits */
401 hrtc->Instance->TR = 0x00000000U;
402 hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
403 hrtc->Instance->CR &= 0x00000000U;
404
405 hrtc->Instance->WUTR = RTC_WUTR_WUT;
406 hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
407 hrtc->Instance->ALRMAR = 0x00000000U;
408 hrtc->Instance->ALRMBR = 0x00000000U;
409 hrtc->Instance->SHIFTR = 0x00000000U;
410 hrtc->Instance->CALR = 0x00000000U;
411 hrtc->Instance->ALRMASSR = 0x00000000U;
412 hrtc->Instance->ALRMBSSR = 0x00000000U;
413
414 /* Exit initialization mode */
415 status = RTC_ExitInitMode(hrtc);
416
417
418 if (status == HAL_OK)
419 {
420 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
421 /* Reset TAMP registers */
422 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset))->CR1 = 0xFFFF0000U;
423 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset))->CR2 = 0x00000000U;
424 #else
425 /* Reset Tamper configuration register */
426 hrtc->Instance->TAMPCR = 0x00000000U;
427
428 /* Reset Option register */
429 hrtc->Instance->OR = 0x00000000U;
430 #endif
431
432 /* Enable the write protection for RTC registers */
433 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
434
435 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
436 if (hrtc->MspDeInitCallback == NULL)
437 {
438 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
439 }
440
441 /* DeInit the low level hardware: CLOCK, NVIC.*/
442 hrtc->MspDeInitCallback(hrtc);
443 #else
444 /* De-Initialize RTC MSP */
445 HAL_RTC_MspDeInit(hrtc);
446 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
447
448 hrtc->State = HAL_RTC_STATE_RESET;
449
450 /* Release Lock */
451 __HAL_UNLOCK(hrtc);
452 }
453 }
454 }
455
456 return status;
457 }
458
459 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
460 /**
461 * @brief Register a User RTC Callback
462 * To be used instead of the weak predefined callback
463 * @param hrtc RTC handle
464 * @param CallbackID ID of the callback to be registered
465 * This parameter can be one of the following values:
466 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
467 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
468 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
469 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
470 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
471 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
472 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
473 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
474 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
475 * @param pCallback pointer to the Callback function
476 * @retval HAL status
477 */
HAL_RTC_RegisterCallback(RTC_HandleTypeDef * hrtc,HAL_RTC_CallbackIDTypeDef CallbackID,pRTC_CallbackTypeDef pCallback)478 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
479 {
480 HAL_StatusTypeDef status = HAL_OK;
481
482 if (pCallback == NULL)
483 {
484 return HAL_ERROR;
485 }
486
487 /* Process locked */
488 __HAL_LOCK(hrtc);
489
490 if (HAL_RTC_STATE_READY == hrtc->State)
491 {
492 switch (CallbackID)
493 {
494 case HAL_RTC_ALARM_A_EVENT_CB_ID :
495 hrtc->AlarmAEventCallback = pCallback;
496 break;
497
498 case HAL_RTC_ALARM_B_EVENT_CB_ID :
499 hrtc->AlarmBEventCallback = pCallback;
500 break;
501
502 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
503 hrtc->TimeStampEventCallback = pCallback;
504 break;
505
506 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
507 hrtc->WakeUpTimerEventCallback = pCallback;
508 break;
509
510 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
511 case HAL_RTC_SSRU_EVENT_CB_ID :
512 hrtc->SSRUEventCallback = pCallback;
513 break;
514 #endif
515
516 #if defined(RTC_TAMPER1_SUPPORT)
517 case HAL_RTC_TAMPER1_EVENT_CB_ID :
518 hrtc->Tamper1EventCallback = pCallback;
519 break;
520 #endif /* RTC_TAMPER1_SUPPORT */
521
522 case HAL_RTC_TAMPER2_EVENT_CB_ID :
523 hrtc->Tamper2EventCallback = pCallback;
524 break;
525
526 #if defined(RTC_TAMPER3_SUPPORT)
527 case HAL_RTC_TAMPER3_EVENT_CB_ID :
528 hrtc->Tamper3EventCallback = pCallback;
529 break;
530 #endif /* RTC_TAMPER3_SUPPORT */
531
532 case HAL_RTC_MSPINIT_CB_ID :
533 hrtc->MspInitCallback = pCallback;
534 break;
535
536 case HAL_RTC_MSPDEINIT_CB_ID :
537 hrtc->MspDeInitCallback = pCallback;
538 break;
539
540 default :
541 /* Return error status */
542 status = HAL_ERROR;
543 break;
544 }
545 }
546 else if (HAL_RTC_STATE_RESET == hrtc->State)
547 {
548 switch (CallbackID)
549 {
550 case HAL_RTC_MSPINIT_CB_ID :
551 hrtc->MspInitCallback = pCallback;
552 break;
553
554 case HAL_RTC_MSPDEINIT_CB_ID :
555 hrtc->MspDeInitCallback = pCallback;
556 break;
557
558 default :
559 /* Return error status */
560 status = HAL_ERROR;
561 break;
562 }
563 }
564 else
565 {
566 /* Return error status */
567 status = HAL_ERROR;
568 }
569
570 /* Release Lock */
571 __HAL_UNLOCK(hrtc);
572
573 return status;
574 }
575
576 /**
577 * @brief Unregister an RTC Callback
578 * RTC callback is redirected to the weak predefined callback
579 * @param hrtc RTC handle
580 * @param CallbackID ID of the callback to be unregistered
581 * This parameter can be one of the following values:
582 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
583 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
584 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
585 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
586 * @arg @ref HAL_RTC_SSRU_EVENT_CB_ID SSRU Callback ID
587 #endif
588 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
589 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
590 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
591 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
592 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
593 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
594 * @retval HAL status
595 */
HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef * hrtc,HAL_RTC_CallbackIDTypeDef CallbackID)596 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
597 {
598 HAL_StatusTypeDef status = HAL_OK;
599
600 /* Process locked */
601 __HAL_LOCK(hrtc);
602
603 if (HAL_RTC_STATE_READY == hrtc->State)
604 {
605 switch (CallbackID)
606 {
607 case HAL_RTC_ALARM_A_EVENT_CB_ID :
608 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
609 break;
610
611 case HAL_RTC_ALARM_B_EVENT_CB_ID :
612 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
613 break;
614
615 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
616 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
617 break;
618
619 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
620 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
621 break;
622
623 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
624 case HAL_RTC_SSRU_EVENT_CB_ID :
625 hrtc->SSRUEventCallback = HAL_RTCEx_SSRUEventCallback; /* Legacy weak SSRUEventCallback */
626 break;
627 #endif
628
629 #if defined(RTC_TAMPER1_SUPPORT)
630 case HAL_RTC_TAMPER1_EVENT_CB_ID :
631 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
632 break;
633 #endif /* RTC_TAMPER1_SUPPORT */
634
635 case HAL_RTC_TAMPER2_EVENT_CB_ID :
636 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
637 break;
638
639 #if defined(RTC_TAMPER3_SUPPORT)
640 case HAL_RTC_TAMPER3_EVENT_CB_ID :
641 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
642 break;
643 #endif /* RTC_TAMPER3_SUPPORT */
644
645 case HAL_RTC_MSPINIT_CB_ID :
646 hrtc->MspInitCallback = HAL_RTC_MspInit;
647 break;
648
649 case HAL_RTC_MSPDEINIT_CB_ID :
650 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
651 break;
652
653 default :
654 /* Return error status */
655 status = HAL_ERROR;
656 break;
657 }
658 }
659 else if (HAL_RTC_STATE_RESET == hrtc->State)
660 {
661 switch (CallbackID)
662 {
663 case HAL_RTC_MSPINIT_CB_ID :
664 hrtc->MspInitCallback = HAL_RTC_MspInit;
665 break;
666
667 case HAL_RTC_MSPDEINIT_CB_ID :
668 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
669 break;
670
671 default :
672 /* Return error status */
673 status = HAL_ERROR;
674 break;
675 }
676 }
677 else
678 {
679 /* Return error status */
680 status = HAL_ERROR;
681 }
682
683 /* Release Lock */
684 __HAL_UNLOCK(hrtc);
685
686 return status;
687 }
688 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
689
690 /**
691 * @brief Initialize the RTC MSP.
692 * @param hrtc RTC handle
693 * @retval None
694 */
HAL_RTC_MspInit(RTC_HandleTypeDef * hrtc)695 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
696 {
697 /* Prevent unused argument(s) compilation warning */
698 UNUSED(hrtc);
699
700 /* NOTE : This function should not be modified, when the callback is needed,
701 the HAL_RTC_MspInit could be implemented in the user file
702 */
703 }
704
705 /**
706 * @brief DeInitialize the RTC MSP.
707 * @param hrtc RTC handle
708 * @retval None
709 */
HAL_RTC_MspDeInit(RTC_HandleTypeDef * hrtc)710 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
711 {
712 /* Prevent unused argument(s) compilation warning */
713 UNUSED(hrtc);
714
715 /* NOTE : This function should not be modified, when the callback is needed,
716 the HAL_RTC_MspDeInit could be implemented in the user file
717 */
718 }
719
720 /**
721 * @}
722 */
723
724 /** @addtogroup RTC_Exported_Functions_Group2
725 * @brief RTC Time and Date functions
726 *
727 @verbatim
728 ===============================================================================
729 ##### RTC Time and Date functions #####
730 ===============================================================================
731
732 [..] This section provides functions allowing to configure Time and Date features
733
734 @endverbatim
735 * @{
736 */
737
738 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
739 /**
740 * @brief Set RTC current time.
741 * @param hrtc RTC handle
742 * @param sTime Pointer to Time structure
743 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used and RTC_SSR will be automatically reset to 0xFFFFFFFF
744 else sTime->SubSeconds is not used and RTC_SSR will be automatically reset to the A 7-bit async prescaler (RTC_PRER_PREDIV_A)
745 * @param Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds.
746 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used
747 * else this parameter can be one of the following values
748 * @arg RTC_FORMAT_BIN: Binary format
749 * @arg RTC_FORMAT_BCD: BCD 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 #ifdef USE_FULL_ASSERT
758 /* Check the parameters depending of the Binary mode with 32-bit free-running counter configuration. */
759 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
760 {
761 /* Check the parameters */
762 assert_param(IS_RTC_FORMAT(Format));
763 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
764 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
765 }
766 #endif
767
768 /* Process Locked */
769 __HAL_LOCK(hrtc);
770
771 hrtc->State = HAL_RTC_STATE_BUSY;
772
773 /* Disable the write protection for RTC registers */
774 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
775
776 /* Enter Initialization mode */
777 status = RTC_EnterInitMode(hrtc);
778 if (status == HAL_OK)
779 {
780 /* Check Binary mode ((32-bit free-running counter) */
781 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
782 {
783 if (Format == RTC_FORMAT_BIN)
784 {
785 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
786 {
787 assert_param(IS_RTC_HOUR12(sTime->Hours));
788 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
789 }
790 else
791 {
792 sTime->TimeFormat = 0x00U;
793 assert_param(IS_RTC_HOUR24(sTime->Hours));
794 }
795 assert_param(IS_RTC_MINUTES(sTime->Minutes));
796 assert_param(IS_RTC_SECONDS(sTime->Seconds));
797
798 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
799 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
800 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
801 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
802
803 }
804 else
805 {
806 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
807 {
808 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
809 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
810 }
811 else
812 {
813 sTime->TimeFormat = 0x00U;
814 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
815 }
816 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
817 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
818 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
819 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
820 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
821 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
822 }
823
824 /* Set the RTC_TR register */
825 WRITE_REG(RTC->TR, (tmpreg & RTC_TR_RESERVED_MASK));
826
827 /* This interface is deprecated. To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions */
828 CLEAR_BIT(RTC->CR, RTC_CR_BKP);
829
830 /* This interface is deprecated. To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions */
831 SET_BIT(RTC->CR, (sTime->DayLightSaving | sTime->StoreOperation));
832 }
833 }
834
835 /* Exit Initialization mode */
836 status = RTC_ExitInitMode(hrtc);
837
838
839 /* Enable the write protection for RTC registers */
840 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
841
842 if (status == HAL_OK)
843 {
844 hrtc->State = HAL_RTC_STATE_READY;
845 }
846
847 /* Process Unlocked */
848 __HAL_UNLOCK(hrtc);
849
850 return status;
851 }
852
853 /**
854 * @brief Get RTC current time.
855 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
856 * value in second fraction ratio with time unit following generic formula:
857 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
858 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
859 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
860 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
861 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
862 * to ensure consistency between the time and date values.
863 * @param hrtc RTC handle
864 * @param sTime
865 * if Binary mode is RTC_BINARY_ONLY, sTime->SubSeconds only is updated
866 * else
867 * Pointer to Time structure with Hours, Minutes and Seconds fields returned
868 * with input format (BIN or BCD), also SubSeconds field returning the
869 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
870 * factor to be used for second fraction ratio computation.
871 * @param Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds.
872 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used
873 * else this parameter can be one of the following values:
874 * @arg RTC_FORMAT_BIN: Binary format
875 * @arg RTC_FORMAT_BCD: BCD format
876 * @retval HAL status
877 */
HAL_RTC_GetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)878 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
879 {
880 uint32_t tmpreg;
881
882 UNUSED(hrtc);
883 /* Get subseconds structure field from the corresponding register*/
884 sTime->SubSeconds = READ_REG(RTC->SSR);
885
886 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
887 {
888 /* Check the parameters */
889 assert_param(IS_RTC_FORMAT(Format));
890
891 /* Get SecondFraction structure field from the corresponding register field*/
892 sTime->SecondFraction = (uint32_t)(READ_REG(RTC->PRER) & RTC_PRER_PREDIV_S);
893
894 /* Get the TR register */
895 tmpreg = (uint32_t)(READ_REG(RTC->TR) & RTC_TR_RESERVED_MASK);
896
897 /* Fill the structure fields with the read parameters */
898 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
899 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
900 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
901 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
902
903 /* Check the input parameters format */
904 if (Format == RTC_FORMAT_BIN)
905 {
906 /* Convert the time structure parameters to Binary format */
907 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
908 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
909 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
910 }
911 }
912
913 return HAL_OK;
914 }
915
916 /**
917 * @brief Set RTC current date.
918 * @param hrtc RTC handle
919 * @param sDate Pointer to date structure
920 * @param Format Format of sDate->Year, sDate->Month and sDate->Weekday.
921 * This parameter can be one of the following values:
922 * @arg RTC_FORMAT_BIN: Binary format
923 * @arg RTC_FORMAT_BCD: BCD format
924 * @retval HAL status
925 */
HAL_RTC_SetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)926 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
927 {
928 uint32_t datetmpreg;
929 HAL_StatusTypeDef status;
930
931 /* Check the parameters */
932 assert_param(IS_RTC_FORMAT(Format));
933
934 /* Process Locked */
935 __HAL_LOCK(hrtc);
936
937 hrtc->State = HAL_RTC_STATE_BUSY;
938
939 if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
940 {
941 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
942 }
943
944 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
945
946 if (Format == RTC_FORMAT_BIN)
947 {
948 assert_param(IS_RTC_YEAR(sDate->Year));
949 assert_param(IS_RTC_MONTH(sDate->Month));
950 assert_param(IS_RTC_DATE(sDate->Date));
951
952 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
953 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
954 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
955 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
956 }
957 else
958 {
959 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
960 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
961 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
962
963 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
964 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
965 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
966 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
967 }
968
969 /* Disable the write protection for RTC registers */
970 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
971
972 /* Enter Initialization mode */
973 status = RTC_EnterInitMode(hrtc);
974 if (status == HAL_OK)
975 {
976 /* Set the RTC_DR register */
977 WRITE_REG(RTC->DR, (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK));
978
979 /* Exit Initialization mode */
980 status = RTC_ExitInitMode(hrtc);
981 }
982
983 /* Enable the write protection for RTC registers */
984 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
985
986 if (status == HAL_OK)
987 {
988 hrtc->State = HAL_RTC_STATE_READY ;
989 }
990
991 /* Process Unlocked */
992 __HAL_UNLOCK(hrtc);
993
994 return status;
995 }
996
997 /**
998 * @brief Get RTC current date.
999 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1000 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1001 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1002 * @param hrtc RTC handle
1003 * @param sDate Pointer to Date structure
1004 * @param Format Format of sDate->Year, sDate->Month and sDate->Weekday.
1005 * This parameter can be one of the following values:
1006 * @arg RTC_FORMAT_BIN: Binary format
1007 * @arg RTC_FORMAT_BCD: BCD format
1008 * @retval HAL status
1009 */
HAL_RTC_GetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)1010 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1011 {
1012 uint32_t datetmpreg;
1013
1014 UNUSED(hrtc);
1015 /* Check the parameters */
1016 assert_param(IS_RTC_FORMAT(Format));
1017
1018 /* Get the DR register */
1019 datetmpreg = (uint32_t)(READ_REG(RTC->DR) & RTC_DR_RESERVED_MASK);
1020
1021 /* Fill the structure fields with the read parameters */
1022 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1023 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1024 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1025 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
1026
1027 /* Check the input parameters format */
1028 if (Format == RTC_FORMAT_BIN)
1029 {
1030 /* Convert the date structure parameters to Binary format */
1031 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1032 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1033 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1034 }
1035 return HAL_OK;
1036 }
1037
1038 /**
1039 * @}
1040 */
1041
1042 /** @addtogroup RTC_Exported_Functions_Group3
1043 * @brief RTC Alarm functions
1044 *
1045 @verbatim
1046 ===============================================================================
1047 ##### RTC Alarm functions #####
1048 ===============================================================================
1049
1050 [..] This section provides functions allowing to configure Alarm feature
1051
1052 @endverbatim
1053 * @{
1054 */
1055 /**
1056 * @brief Set the specified RTC Alarm.
1057 * @param hrtc RTC handle
1058 * @param sAlarm Pointer to Alarm structure
1059 * if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
1060 * sAlarm->AlarmTime.SubSeconds
1061 * sAlarm->AlarmSubSecondMask
1062 * sAlarm->BinaryAutoClr
1063 * @param Format of the entered parameters.
1064 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1065 * else this parameter can be one of the following values
1066 * @arg RTC_FORMAT_BIN: Binary format
1067 * @arg RTC_FORMAT_BCD: BCD format
1068 * @retval HAL status
1069 */
HAL_RTC_SetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1070 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1071 {
1072 uint32_t tmpreg = 0, binaryMode;
1073
1074 __HAL_LOCK(hrtc);
1075 hrtc->State = HAL_RTC_STATE_BUSY;
1076
1077 #ifdef USE_FULL_ASSERT
1078 /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration). */
1079 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1080 {
1081 assert_param(IS_RTC_FORMAT(Format));
1082 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1083 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1084 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1085 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1086 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1087 }
1088 else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
1089 {
1090 assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
1091 assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
1092 }
1093 else /* RTC_BINARY_MIX */
1094 {
1095 assert_param(IS_RTC_FORMAT(Format));
1096 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1097 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1098 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1099 /* In Binary Mix Mode, the RTC can not generate an alarm on a match involving all calendar items + the upper SSR bits */
1100 assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <= (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
1101 }
1102 #endif
1103
1104 /* Get Binary mode (32-bit free-running counter configuration) */
1105 binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
1106
1107 if (binaryMode != RTC_BINARY_ONLY)
1108 {
1109 if (Format == RTC_FORMAT_BIN)
1110 {
1111 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1112 {
1113 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1114 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1115 }
1116 else
1117 {
1118 sAlarm->AlarmTime.TimeFormat = 0x00U;
1119 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1120 }
1121 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1122 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1123
1124 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1125 {
1126 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1127 }
1128 else
1129 {
1130 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1131 }
1132 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1133 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1134 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1135 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1136 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1137 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1138 ((uint32_t)sAlarm->AlarmMask));
1139 }
1140 else /* format BCD */
1141 {
1142 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1143 {
1144 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1145 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1146 }
1147 else
1148 {
1149 sAlarm->AlarmTime.TimeFormat = 0x00U;
1150 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1151 }
1152
1153 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1154 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1155
1156 #ifdef USE_FULL_ASSERT
1157 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1158 {
1159 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1160 }
1161 else
1162 {
1163 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1164 }
1165
1166 #endif /* USE_FULL_ASSERT */
1167 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1168 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1169 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1170 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1171 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1172 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1173 ((uint32_t)sAlarm->AlarmMask));
1174 }
1175 }
1176
1177 /* Disable the write protection for RTC registers */
1178 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1179
1180 /* Configure the Alarm register */
1181 if (sAlarm->Alarm == RTC_ALARM_A)
1182 {
1183 /* Disable the Alarm A interrupt */
1184 /* In case of interrupt mode is used, the interrupt source must disabled */
1185 CLEAR_BIT(RTC->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
1186
1187 /* Clear flag alarm A */
1188 WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1189
1190 if (binaryMode == RTC_BINARY_ONLY)
1191 {
1192 WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1193 }
1194 else
1195 {
1196 WRITE_REG(RTC->ALRMAR, tmpreg);
1197 WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
1198 }
1199
1200 WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
1201
1202 /* Configure the Alarm state: Enable Alarm */
1203 SET_BIT(RTC->CR, RTC_CR_ALRAE);
1204 }
1205 else
1206 {
1207 /* Disable the Alarm B interrupt */
1208 /* In case of interrupt mode is used, the interrupt source must disabled */
1209 CLEAR_BIT(RTC->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
1210
1211 /* Clear flag alarm B */
1212 WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1213
1214 if (binaryMode == RTC_BINARY_ONLY)
1215 {
1216 WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1217 }
1218 else
1219 {
1220 WRITE_REG(RTC->ALRMBR, tmpreg);
1221 WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
1222 }
1223
1224 WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
1225
1226 /* Configure the Alarm state: Enable Alarm */
1227 SET_BIT(RTC->CR, RTC_CR_ALRBE);
1228 }
1229
1230 /* Enable the write protection for RTC registers */
1231 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1232
1233 /* Change RTC state */
1234 hrtc->State = HAL_RTC_STATE_READY;
1235
1236 /* Process Unlocked */
1237 __HAL_UNLOCK(hrtc);
1238
1239 return HAL_OK;
1240 }
1241
1242 /**
1243 * @brief Set the specified RTC Alarm with Interrupt.
1244 * @param hrtc RTC handle
1245 * @param sAlarm Pointer to Alarm structure
1246 * if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
1247 * sAlarm->AlarmTime.SubSeconds
1248 * sAlarm->AlarmSubSecondMask
1249 * sAlarm->BinaryAutoClr
1250 * @param Format Specifies the format of the entered parameters.
1251 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1252 * else this parameter can be one of the following values
1253 * @arg RTC_FORMAT_BIN: Binary format
1254 * @arg RTC_FORMAT_BCD: BCD format
1255 * @retval HAL status
1256 */
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1257 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1258 {
1259 uint32_t tmpreg = 0, binaryMode;
1260
1261 /* Process Locked */
1262 __HAL_LOCK(hrtc);
1263 hrtc->State = HAL_RTC_STATE_BUSY;
1264
1265 #ifdef USE_FULL_ASSERT
1266 /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration). */
1267 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1268 {
1269 assert_param(IS_RTC_FORMAT(Format));
1270 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1271 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1272 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1273 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1274 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1275 }
1276 else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
1277 {
1278 assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
1279 assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
1280 }
1281 else /* RTC_BINARY_MIX */
1282 {
1283 assert_param(IS_RTC_FORMAT(Format));
1284 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1285 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1286 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1287 /* In Binary Mix Mode, the RTC can not generate an alarm on a match involving all calendar items + the upper SSR bits */
1288 assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <= (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
1289 }
1290 #endif
1291
1292 /* Get Binary mode (32-bit free-running counter configuration) */
1293 binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
1294
1295 if (binaryMode != RTC_BINARY_ONLY)
1296 {
1297 if (Format == RTC_FORMAT_BIN)
1298 {
1299 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1300 {
1301 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1302 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1303 }
1304 else
1305 {
1306 sAlarm->AlarmTime.TimeFormat = 0x00U;
1307 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1308 }
1309 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1310 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1311
1312 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1313 {
1314 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1315 }
1316 else
1317 {
1318 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1319 }
1320 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1321 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1322 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1323 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1324 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1325 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1326 ((uint32_t)sAlarm->AlarmMask));
1327 }
1328 else /* Format BCD */
1329 {
1330 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1331 {
1332 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1333 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1334 }
1335 else
1336 {
1337 sAlarm->AlarmTime.TimeFormat = 0x00U;
1338 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1339 }
1340
1341 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1342 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1343
1344 #ifdef USE_FULL_ASSERT
1345 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1346 {
1347 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1348 }
1349 else
1350 {
1351 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1352 }
1353
1354 #endif /* USE_FULL_ASSERT */
1355 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1356 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1357 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1358 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1359 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1360 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1361 ((uint32_t)sAlarm->AlarmMask));
1362
1363 }
1364 }
1365
1366 /* Disable the write protection for RTC registers */
1367 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1368
1369 /* Configure the Alarm register */
1370 if (sAlarm->Alarm == RTC_ALARM_A)
1371 {
1372 /* Disable the Alarm A interrupt */
1373 CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1374 /* Clear flag alarm A */
1375 WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1376
1377 if (binaryMode == RTC_BINARY_ONLY)
1378 {
1379 RTC->ALRMASSR = sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr;
1380 }
1381 else
1382 {
1383 WRITE_REG(RTC->ALRMAR, tmpreg);
1384 WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
1385 }
1386
1387 WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
1388
1389 /* Configure the Alarm interrupt */
1390 SET_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1391 }
1392 else
1393 {
1394 /* Disable the Alarm B interrupt */
1395 CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1396 /* Clear flag alarm B */
1397 WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1398
1399 if (binaryMode == RTC_BINARY_ONLY)
1400 {
1401 WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1402 }
1403 else
1404 {
1405 WRITE_REG(RTC->ALRMBR, tmpreg);
1406 WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
1407 }
1408
1409 WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
1410
1411 /* Configure the Alarm interrupt */
1412 SET_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1413 }
1414
1415 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1416 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1417 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1418
1419 /* Enable the write protection for RTC registers */
1420 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1421
1422 hrtc->State = HAL_RTC_STATE_READY;
1423
1424 /* Process Unlocked */
1425 __HAL_UNLOCK(hrtc);
1426
1427 return HAL_OK;
1428 }
1429
1430 #else /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
1431
1432 /**
1433 * @brief Set RTC current time.
1434 * @param hrtc RTC handle
1435 * @param sTime Pointer to Time structure
1436 * @param Format Specifies the format of the entered parameters.
1437 * This parameter can be one of the following values:
1438 * @arg RTC_FORMAT_BIN: Binary data format
1439 * @arg RTC_FORMAT_BCD: BCD data format
1440 * @retval HAL status
1441 */
HAL_RTC_SetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)1442 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
1443 {
1444 uint32_t tmpreg;
1445 HAL_StatusTypeDef status;
1446
1447 /* Check the parameters */
1448 assert_param(IS_RTC_FORMAT(Format));
1449 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
1450 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
1451
1452 /* Process Locked */
1453 __HAL_LOCK(hrtc);
1454
1455 hrtc->State = HAL_RTC_STATE_BUSY;
1456
1457 /* Disable the write protection for RTC registers */
1458 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1459
1460 /* Enter Initialization mode */
1461 status = RTC_EnterInitMode(hrtc);
1462 if (status == HAL_OK)
1463 {
1464 if (Format == RTC_FORMAT_BIN)
1465 {
1466 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1467 {
1468 assert_param(IS_RTC_HOUR12(sTime->Hours));
1469 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
1470 }
1471 else
1472 {
1473 sTime->TimeFormat = 0x00U;
1474 assert_param(IS_RTC_HOUR24(sTime->Hours));
1475 }
1476 assert_param(IS_RTC_MINUTES(sTime->Minutes));
1477 assert_param(IS_RTC_SECONDS(sTime->Seconds));
1478
1479 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
1480 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
1481 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
1482 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
1483 }
1484 else
1485 {
1486 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1487 {
1488 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
1489 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
1490 }
1491 else
1492 {
1493 sTime->TimeFormat = 0x00U;
1494 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
1495 }
1496 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
1497 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
1498 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
1499 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
1500 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
1501 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
1502 }
1503
1504 /* Set the RTC_TR register */
1505 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
1506
1507 /* Clear the bits to be configured */
1508 hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
1509
1510 /* Configure the RTC_CR register */
1511 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
1512
1513 /* Exit Initialization mode */
1514 status = RTC_ExitInitMode(hrtc);
1515 }
1516
1517 /* Enable the write protection for RTC registers */
1518 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1519
1520 if (status == HAL_OK)
1521 {
1522 hrtc->State = HAL_RTC_STATE_READY;
1523 }
1524
1525 /* Process Unlocked */
1526 __HAL_UNLOCK(hrtc);
1527
1528 return status;
1529 }
1530
1531 /**
1532 * @brief Get RTC current time.
1533 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
1534 * value in second fraction ratio with time unit following generic formula:
1535 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
1536 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
1537 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1538 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1539 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
1540 * to ensure consistency between the time and date values.
1541 * @param hrtc RTC handle
1542 * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
1543 * with input format (BIN or BCD), also SubSeconds field returning the
1544 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
1545 * factor to be used for second fraction ratio computation.
1546 * @param Format Specifies the format of the entered parameters.
1547 * This parameter can be one of the following values:
1548 * @arg RTC_FORMAT_BIN: Binary data format
1549 * @arg RTC_FORMAT_BCD: BCD data format
1550 * @retval HAL status
1551 */
HAL_RTC_GetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)1552 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
1553 {
1554 uint32_t tmpreg;
1555
1556 /* Check the parameters */
1557 assert_param(IS_RTC_FORMAT(Format));
1558
1559 /* Get subseconds structure field from the corresponding register*/
1560 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
1561
1562 /* Get SecondFraction structure field from the corresponding register field*/
1563 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
1564
1565 /* Get the TR register */
1566 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
1567
1568 /* Fill the structure fields with the read parameters */
1569 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
1570 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
1571 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
1572 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
1573
1574 /* Check the input parameters format */
1575 if (Format == RTC_FORMAT_BIN)
1576 {
1577 /* Convert the time structure parameters to Binary format */
1578 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
1579 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
1580 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
1581 }
1582
1583 return HAL_OK;
1584 }
1585
1586 /**
1587 * @brief Set RTC current date.
1588 * @param hrtc RTC handle
1589 * @param sDate Pointer to date structure
1590 * @param Format specifies the format of the entered parameters.
1591 * This parameter can be one of the following values:
1592 * @arg RTC_FORMAT_BIN: Binary data format
1593 * @arg RTC_FORMAT_BCD: BCD data format
1594 * @retval HAL status
1595 */
HAL_RTC_SetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)1596 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1597 {
1598 uint32_t datetmpreg;
1599 HAL_StatusTypeDef status;
1600
1601 /* Check the parameters */
1602 assert_param(IS_RTC_FORMAT(Format));
1603
1604 /* Process Locked */
1605 __HAL_LOCK(hrtc);
1606
1607 hrtc->State = HAL_RTC_STATE_BUSY;
1608
1609 if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
1610 {
1611 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
1612 }
1613
1614 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
1615
1616 if (Format == RTC_FORMAT_BIN)
1617 {
1618 assert_param(IS_RTC_YEAR(sDate->Year));
1619 assert_param(IS_RTC_MONTH(sDate->Month));
1620 assert_param(IS_RTC_DATE(sDate->Date));
1621
1622 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
1623 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
1624 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
1625 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
1626 }
1627 else
1628 {
1629 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
1630 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
1631 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
1632
1633 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
1634 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
1635 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
1636 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
1637 }
1638
1639 /* Disable the write protection for RTC registers */
1640 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1641
1642 /* Enter Initialization mode */
1643 status = RTC_EnterInitMode(hrtc);
1644 if (status == HAL_OK)
1645 {
1646 /* Set the RTC_DR register */
1647 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
1648
1649 /* Exit Initialization mode */
1650 status = RTC_ExitInitMode(hrtc);
1651 }
1652
1653 /* Enable the write protection for RTC registers */
1654 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1655
1656 if (status == HAL_OK)
1657 {
1658 hrtc->State = HAL_RTC_STATE_READY ;
1659 }
1660
1661 /* Process Unlocked */
1662 __HAL_UNLOCK(hrtc);
1663
1664 return status;
1665 }
1666
1667 /**
1668 * @brief Get RTC current date.
1669 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1670 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1671 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1672 * @param hrtc RTC handle
1673 * @param sDate Pointer to Date structure
1674 * @param Format Specifies the format of the entered parameters.
1675 * This parameter can be one of the following values:
1676 * @arg RTC_FORMAT_BIN: Binary data format
1677 * @arg RTC_FORMAT_BCD: BCD data format
1678 * @retval HAL status
1679 */
HAL_RTC_GetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)1680 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1681 {
1682 uint32_t datetmpreg;
1683
1684 /* Check the parameters */
1685 assert_param(IS_RTC_FORMAT(Format));
1686
1687 /* Get the DR register */
1688 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
1689
1690 /* Fill the structure fields with the read parameters */
1691 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1692 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1693 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1694 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
1695
1696 /* Check the input parameters format */
1697 if (Format == RTC_FORMAT_BIN)
1698 {
1699 /* Convert the date structure parameters to Binary format */
1700 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1701 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1702 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1703 }
1704 return HAL_OK;
1705 }
1706
1707 /**
1708 * @}
1709 */
1710
1711 /** @addtogroup RTC_Exported_Functions_Group3
1712 * @brief RTC Alarm functions
1713 *
1714 @verbatim
1715 ===============================================================================
1716 ##### RTC Alarm functions #####
1717 ===============================================================================
1718
1719 [..] This section provides functions allowing to configure Alarm feature
1720
1721 @endverbatim
1722 * @{
1723 */
1724 /**
1725 * @brief Set the specified RTC Alarm.
1726 * @param hrtc RTC handle
1727 * @param sAlarm Pointer to Alarm structure
1728 * @param Format Specifies the format of the entered parameters.
1729 * This parameter can be one of the following values:
1730 * @arg RTC_FORMAT_BIN: Binary data format
1731 * @arg RTC_FORMAT_BCD: BCD data format
1732 * @retval HAL status
1733 */
HAL_RTC_SetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1734 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1735 {
1736 uint32_t tmpreg, subsecondtmpreg;
1737
1738 /* Check the parameters */
1739 assert_param(IS_RTC_FORMAT(Format));
1740 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1741 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1742 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1743 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1744 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1745
1746 /* Process Locked */
1747 __HAL_LOCK(hrtc);
1748
1749 hrtc->State = HAL_RTC_STATE_BUSY;
1750
1751 if (Format == RTC_FORMAT_BIN)
1752 {
1753 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1754 {
1755 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1756 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1757 }
1758 else
1759 {
1760 sAlarm->AlarmTime.TimeFormat = 0x00U;
1761 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1762 }
1763 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1764 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1765
1766 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1767 {
1768 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1769 }
1770 else
1771 {
1772 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1773 }
1774 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1775 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1776 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1777 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1778 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1779 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1780 ((uint32_t)sAlarm->AlarmMask));
1781 }
1782 else
1783 {
1784 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1785 {
1786 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1787 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1788 }
1789 else
1790 {
1791 sAlarm->AlarmTime.TimeFormat = 0x00U;
1792 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1793 }
1794
1795 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1796 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1797
1798 #ifdef USE_FULL_ASSERT
1799 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1800 {
1801 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1802 }
1803 else
1804 {
1805 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1806 }
1807
1808 #endif /* USE_FULL_ASSERT */
1809 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1810 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1811 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1812 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1813 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1814 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1815 ((uint32_t)sAlarm->AlarmMask));
1816 }
1817
1818 /* Configure the Alarm A or Alarm B Sub Second registers */
1819 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1820
1821 /* Disable the write protection for RTC registers */
1822 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1823
1824 /* Configure the Alarm register */
1825 if (sAlarm->Alarm == RTC_ALARM_A)
1826 {
1827 /* Disable the Alarm A interrupt */
1828 __HAL_RTC_ALARMA_DISABLE(hrtc);
1829 /* Clear flag alarm A */
1830 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1831 /* In case of interrupt mode is used, the interrupt source must disabled */
1832 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1833
1834 #if defined (RTC_FLAG_ALRAWF)
1835 uint32_t tickstart = HAL_GetTick();
1836 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1837 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1838 {
1839 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1840 {
1841 /* Enable the write protection for RTC registers */
1842 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1843
1844 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1845
1846 /* Process Unlocked */
1847 __HAL_UNLOCK(hrtc);
1848
1849 return HAL_TIMEOUT;
1850 }
1851 }
1852 #endif
1853
1854 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1855 /* Configure the Alarm A Sub Second register */
1856 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1857 /* Configure the Alarm state: Enable Alarm */
1858 __HAL_RTC_ALARMA_ENABLE(hrtc);
1859 }
1860 else
1861 {
1862 /* Disable the Alarm B interrupt */
1863 __HAL_RTC_ALARMB_DISABLE(hrtc);
1864 /* Clear flag alarm B */
1865 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1866 /* In case of interrupt mode is used, the interrupt source must disabled */
1867 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1868
1869 #if defined (RTC_FLAG_ALRBWF)
1870 uint32_t tickstart = HAL_GetTick();
1871 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1872 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1873 {
1874 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1875 {
1876 /* Enable the write protection for RTC registers */
1877 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1878
1879 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1880
1881 /* Process Unlocked */
1882 __HAL_UNLOCK(hrtc);
1883
1884 return HAL_TIMEOUT;
1885 }
1886 }
1887 #endif
1888
1889 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1890 /* Configure the Alarm B Sub Second register */
1891 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1892 /* Configure the Alarm state: Enable Alarm */
1893 __HAL_RTC_ALARMB_ENABLE(hrtc);
1894 }
1895
1896 /* Enable the write protection for RTC registers */
1897 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1898
1899 /* Change RTC state */
1900 hrtc->State = HAL_RTC_STATE_READY;
1901
1902 /* Process Unlocked */
1903 __HAL_UNLOCK(hrtc);
1904
1905 return HAL_OK;
1906 }
1907
1908 /**
1909 * @brief Set the specified RTC Alarm with Interrupt.
1910 * @note The Alarm register can only be written when the corresponding Alarm
1911 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
1912 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1913 * @param hrtc RTC handle
1914 * @param sAlarm Pointer to Alarm structure
1915 * @param Format Specifies the format of the entered parameters.
1916 * This parameter can be one of the following values:
1917 * @arg RTC_FORMAT_BIN: Binary data format
1918 * @arg RTC_FORMAT_BCD: BCD data format
1919 * @retval HAL status
1920 */
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1921 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1922 {
1923 uint32_t tmpreg, subsecondtmpreg;
1924
1925 /* Check the parameters */
1926 assert_param(IS_RTC_FORMAT(Format));
1927 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1928 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1929 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1930 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1931 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1932
1933 /* Process Locked */
1934 __HAL_LOCK(hrtc);
1935
1936 hrtc->State = HAL_RTC_STATE_BUSY;
1937
1938 if (Format == RTC_FORMAT_BIN)
1939 {
1940 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1941 {
1942 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1943 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1944 }
1945 else
1946 {
1947 sAlarm->AlarmTime.TimeFormat = 0x00U;
1948 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1949 }
1950 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1951 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1952
1953 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1954 {
1955 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1956 }
1957 else
1958 {
1959 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1960 }
1961
1962 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1963 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1964 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1965 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1966 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1967 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1968 ((uint32_t)sAlarm->AlarmMask));
1969 }
1970 else
1971 {
1972 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1973 {
1974 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1975 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1976 }
1977 else
1978 {
1979 sAlarm->AlarmTime.TimeFormat = 0x00U;
1980 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1981 }
1982
1983 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1984 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1985
1986 #ifdef USE_FULL_ASSERT
1987 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1988 {
1989 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1990 }
1991 else
1992 {
1993 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1994 }
1995
1996 #endif /* USE_FULL_ASSERT */
1997 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1998 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1999 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
2000 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
2001 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
2002 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
2003 ((uint32_t)sAlarm->AlarmMask));
2004 }
2005 /* Configure the Alarm A or Alarm B Sub Second registers */
2006 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
2007
2008 /* Disable the write protection for RTC registers */
2009 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2010
2011 /* Configure the Alarm register */
2012 if (sAlarm->Alarm == RTC_ALARM_A)
2013 {
2014 /* Disable the Alarm A interrupt */
2015 __HAL_RTC_ALARMA_DISABLE(hrtc);
2016
2017 /* Clear flag alarm A */
2018 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
2019
2020 #if defined (RTC_FLAG_ALRAWF)
2021 uint32_t tickstart = HAL_GetTick();
2022 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
2023 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
2024 {
2025 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2026 {
2027 /* Enable the write protection for RTC registers */
2028 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2029
2030 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2031
2032 /* Process Unlocked */
2033 __HAL_UNLOCK(hrtc);
2034
2035 return HAL_TIMEOUT;
2036 }
2037 }
2038 #endif
2039
2040 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
2041 /* Configure the Alarm A Sub Second register */
2042 hrtc->Instance->ALRMASSR = subsecondtmpreg;
2043 /* Configure the Alarm state: Enable Alarm */
2044 __HAL_RTC_ALARMA_ENABLE(hrtc);
2045 /* Configure the Alarm interrupt */
2046 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
2047 }
2048 else
2049 {
2050 /* Disable the Alarm B interrupt */
2051 __HAL_RTC_ALARMB_DISABLE(hrtc);
2052
2053 /* Clear flag alarm B */
2054 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
2055
2056 #if defined (RTC_FLAG_ALRBWF)
2057 uint32_t tickstart = HAL_GetTick();
2058 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
2059 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
2060 {
2061 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2062 {
2063 /* Enable the write protection for RTC registers */
2064 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2065
2066 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2067
2068 /* Process Unlocked */
2069 __HAL_UNLOCK(hrtc);
2070
2071 return HAL_TIMEOUT;
2072 }
2073 }
2074 #endif
2075
2076 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
2077 /* Configure the Alarm B Sub Second register */
2078 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
2079 /* Configure the Alarm state: Enable Alarm */
2080 __HAL_RTC_ALARMB_ENABLE(hrtc);
2081 /* Configure the Alarm interrupt */
2082 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
2083 }
2084
2085 /* RTC Alarm Interrupt Configuration: EXTI configuration */
2086 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
2087 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
2088
2089 /* Enable the write protection for RTC registers */
2090 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2091
2092 hrtc->State = HAL_RTC_STATE_READY;
2093
2094 /* Process Unlocked */
2095 __HAL_UNLOCK(hrtc);
2096
2097 return HAL_OK;
2098 }
2099
2100 #endif /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
2101
2102 /**
2103 * @brief Deactivate the specified RTC Alarm.
2104 * @param hrtc RTC handle
2105 * @param Alarm Specifies the Alarm.
2106 * This parameter can be one of the following values:
2107 * @arg RTC_ALARM_A: AlarmA
2108 * @arg RTC_ALARM_B: AlarmB
2109 * @retval HAL status
2110 */
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef * hrtc,uint32_t Alarm)2111 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
2112 {
2113 /* Check the parameters */
2114 assert_param(IS_RTC_ALARM(Alarm));
2115
2116 /* Process Locked */
2117 __HAL_LOCK(hrtc);
2118
2119 hrtc->State = HAL_RTC_STATE_BUSY;
2120
2121 /* Disable the write protection for RTC registers */
2122 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2123
2124 if (Alarm == RTC_ALARM_A)
2125 {
2126 /* AlarmA */
2127 #if defined (RTC_ALRMASSR_SSCLR)
2128 CLEAR_BIT(RTC->ALRMASSR, RTC_ALRMASSR_SSCLR);
2129 #endif
2130
2131 __HAL_RTC_ALARMA_DISABLE(hrtc);
2132
2133 /* In case of interrupt mode is used, the interrupt source must disabled */
2134 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
2135
2136 #if defined (RTC_FLAG_ALRAWF)
2137 uint32_t tickstart = HAL_GetTick();
2138 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
2139 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
2140 {
2141 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2142 {
2143 /* Enable the write protection for RTC registers */
2144 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2145
2146 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2147
2148 /* Process Unlocked */
2149 __HAL_UNLOCK(hrtc);
2150
2151 return HAL_TIMEOUT;
2152 }
2153 }
2154 #endif
2155 }
2156 else
2157 {
2158 /* AlarmB */
2159 #if defined (RTC_ALRMBSSR_SSCLR)
2160 CLEAR_BIT(RTC->ALRMBSSR, RTC_ALRMASSR_SSCLR);
2161 #endif
2162
2163 __HAL_RTC_ALARMB_DISABLE(hrtc);
2164
2165 /* In case of interrupt mode is used, the interrupt source must disabled */
2166 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
2167
2168 #if defined (RTC_FLAG_ALRBWF)
2169 uint32_t tickstart = HAL_GetTick();
2170 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
2171 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
2172 {
2173 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2174 {
2175 /* Enable the write protection for RTC registers */
2176 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2177
2178 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2179
2180 /* Process Unlocked */
2181 __HAL_UNLOCK(hrtc);
2182
2183 return HAL_TIMEOUT;
2184 }
2185 }
2186 #endif
2187 }
2188 /* Enable the write protection for RTC registers */
2189 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2190
2191 hrtc->State = HAL_RTC_STATE_READY;
2192
2193 /* Process Unlocked */
2194 __HAL_UNLOCK(hrtc);
2195
2196 return HAL_OK;
2197 }
2198
2199 /**
2200 * @brief Get the RTC Alarm value and masks.
2201 * @param hrtc RTC handle
2202 * @param sAlarm Pointer to Date structure
2203 * @param Alarm Specifies the Alarm.
2204 * This parameter can be one of the following values:
2205 * @arg RTC_ALARM_A: AlarmA
2206 * @arg RTC_ALARM_B: AlarmB
2207 * @param Format Specifies the format of the entered parameters.
2208 * This parameter can be one of the following values:
2209 * @arg RTC_FORMAT_BIN: Binary data format
2210 * @arg RTC_FORMAT_BCD: BCD data format
2211 * @retval HAL status
2212 */
HAL_RTC_GetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Alarm,uint32_t Format)2213 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
2214 {
2215 uint32_t tmpreg, subsecondtmpreg;
2216
2217 /* Check the parameters */
2218 assert_param(IS_RTC_FORMAT(Format));
2219 assert_param(IS_RTC_ALARM(Alarm));
2220
2221 if (Alarm == RTC_ALARM_A)
2222 {
2223 /* AlarmA */
2224 sAlarm->Alarm = RTC_ALARM_A;
2225
2226 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
2227 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
2228
2229 /* Fill the structure with the read parameters */
2230 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
2231 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
2232 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
2233 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
2234 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
2235 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
2236 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
2237 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
2238 }
2239 else
2240 {
2241 sAlarm->Alarm = RTC_ALARM_B;
2242
2243 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
2244 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
2245
2246 /* Fill the structure with the read parameters */
2247 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
2248 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
2249 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
2250 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
2251 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
2252 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
2253 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
2254 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
2255 }
2256
2257 if (Format == RTC_FORMAT_BIN)
2258 {
2259 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
2260 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
2261 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
2262 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
2263 }
2264
2265 return HAL_OK;
2266 }
2267
2268
2269 /**
2270 * @brief Daylight Saving Time, Add one hour to the calendar in one single operation
2271 * without going through the initialization procedure.
2272 * @param hrtc RTC handle
2273 * @retval None
2274 */
HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef * hrtc)2275 void HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef *hrtc)
2276 {
2277 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2278 SET_BIT(hrtc->Instance->CR, RTC_CR_ADD1H);
2279 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2280 }
2281
2282 /**
2283 * @brief Daylight Saving Time, Subtract one hour from the calendar in one
2284 * single operation without going through the initialization procedure.
2285 * @param hrtc RTC handle
2286 * @retval None
2287 */
HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef * hrtc)2288 void HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef *hrtc)
2289 {
2290 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2291 SET_BIT(hrtc->Instance->CR, RTC_CR_SUB1H);
2292 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2293 }
2294
2295 /**
2296 * @brief Daylight Saving Time, Set the store operation bit.
2297 * @note It can be used by the software in order to memorize the DST status.
2298 * @param hrtc RTC handle
2299 * @retval None
2300 */
HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef * hrtc)2301 void HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc)
2302 {
2303 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2304 SET_BIT(hrtc->Instance->CR, RTC_CR_BKP);
2305 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2306 }
2307
2308 /**
2309 * @brief Daylight Saving Time, Clear the store operation bit.
2310 * @param hrtc RTC handle
2311 * @retval None
2312 */
HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef * hrtc)2313 void HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc)
2314 {
2315 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2316 CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BKP);
2317 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2318 }
2319
2320 /**
2321 * @brief Daylight Saving Time, Read the store operation bit.
2322 * @param hrtc RTC handle
2323 * @retval operation see RTC_StoreOperation_Definitions
2324 */
HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef * hrtc)2325 uint32_t HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc)
2326 {
2327 return READ_BIT(hrtc->Instance->CR, RTC_CR_BKP);
2328 }
2329
2330 /**
2331 * @brief Handle Alarm interrupt request.
2332 * @param hrtc RTC handle
2333 * @retval None
2334 */
HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef * hrtc)2335 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
2336 {
2337 /* Clear the EXTI's line Flag for RTC Alarm */
2338 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
2339
2340 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
2341 /* Get interrupt status */
2342 uint32_t tmp = hrtc->Instance->MISR;
2343
2344 if ((tmp & RTC_MISR_ALRAMF) != 0u)
2345 {
2346 /* Clear the AlarmA interrupt pending bit */
2347 hrtc->Instance->SCR = RTC_SCR_CALRAF;
2348
2349 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2350 /* Call Compare Match registered Callback */
2351 hrtc->AlarmAEventCallback(hrtc);
2352 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
2353 HAL_RTC_AlarmAEventCallback(hrtc);
2354 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
2355 }
2356
2357 if ((tmp & RTC_MISR_ALRBMF) != 0u)
2358 {
2359 /* Clear the AlarmB interrupt pending bit */
2360 hrtc->Instance->SCR = RTC_SCR_CALRBF;
2361
2362 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2363 /* Call Compare Match registered Callback */
2364 hrtc->AlarmBEventCallback(hrtc);
2365 #else
2366 HAL_RTCEx_AlarmBEventCallback(hrtc);
2367 #endif
2368 }
2369
2370 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
2371
2372 /* Get the AlarmA interrupt source enable status */
2373 if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
2374 {
2375 /* Get the pending status of the AlarmA Interrupt */
2376 if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
2377 {
2378 /* Clear the AlarmA interrupt pending bit */
2379 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
2380
2381 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2382 hrtc->AlarmAEventCallback(hrtc);
2383 #else
2384 HAL_RTC_AlarmAEventCallback(hrtc);
2385 #endif
2386 }
2387 }
2388
2389 /* Get the AlarmB interrupt source enable status */
2390 if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
2391 {
2392 /* Get the pending status of the AlarmB Interrupt */
2393 if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
2394 {
2395 /* Clear the AlarmB interrupt pending bit */
2396 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
2397
2398 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2399 hrtc->AlarmBEventCallback(hrtc);
2400 #else
2401 HAL_RTCEx_AlarmBEventCallback(hrtc);
2402 #endif
2403 }
2404 }
2405 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
2406
2407 /* Change RTC state */
2408 hrtc->State = HAL_RTC_STATE_READY;
2409 }
2410
2411 /**
2412 * @brief Alarm A callback.
2413 * @param hrtc RTC handle
2414 * @retval None
2415 */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)2416 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
2417 {
2418 /* Prevent unused argument(s) compilation warning */
2419 UNUSED(hrtc);
2420
2421 /* NOTE : This function should not be modified, when the callback is needed,
2422 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
2423 */
2424 }
2425
2426 /**
2427 * @brief Handle AlarmA Polling request.
2428 * @param hrtc RTC handle
2429 * @param Timeout Timeout duration
2430 * @retval HAL status
2431 */
HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)2432 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
2433 {
2434
2435 uint32_t tickstart = HAL_GetTick();
2436
2437 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
2438 {
2439 if (Timeout != HAL_MAX_DELAY)
2440 {
2441 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2442 {
2443 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2444 return HAL_TIMEOUT;
2445 }
2446 }
2447 }
2448
2449 /* Clear the Alarm interrupt pending bit */
2450 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
2451
2452 /* Change RTC state */
2453 hrtc->State = HAL_RTC_STATE_READY;
2454
2455 return HAL_OK;
2456 }
2457
2458 /**
2459 * @}
2460 */
2461
2462 /** @addtogroup RTC_Exported_Functions_Group4
2463 * @brief Peripheral Control functions
2464 *
2465 @verbatim
2466 ===============================================================================
2467 ##### Peripheral Control functions #####
2468 ===============================================================================
2469 [..]
2470 This subsection provides functions allowing to
2471 (+) Wait for RTC Time and Date Synchronization
2472
2473 @endverbatim
2474 * @{
2475 */
2476
2477 /**
2478 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
2479 * synchronized with RTC APB clock.
2480 * @note The RTC Resynchronization mode is write protected, use the
2481 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2482 * @note To read the calendar through the shadow registers after Calendar
2483 * initialization, calendar update or after wakeup from low power modes
2484 * the software must first clear the RSF flag.
2485 * The software must then wait until it is set again before reading
2486 * the calendar, which means that the calendar registers have been
2487 * correctly copied into the RTC_TR and RTC_DR shadow registers.
2488 * @param hrtc RTC handle
2489 * @retval HAL status
2490 */
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef * hrtc)2491 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
2492 {
2493 uint32_t tickstart;
2494
2495 /* Clear RSF flag */
2496 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
2497 hrtc->Instance->ICSR &= (uint32_t)RTC_RSF_MASK;
2498 #else
2499 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
2500 #endif
2501
2502 tickstart = HAL_GetTick();
2503
2504 /* Wait the registers to be synchronised */
2505 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
2506 while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
2507 #else
2508 while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
2509 #endif
2510 {
2511 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2512 {
2513 return HAL_TIMEOUT;
2514 }
2515 }
2516
2517 return HAL_OK;
2518 }
2519
2520 /**
2521 * @}
2522 */
2523
2524 /** @addtogroup RTC_Exported_Functions_Group5
2525 * @brief Peripheral State functions
2526 *
2527 @verbatim
2528 ===============================================================================
2529 ##### Peripheral State functions #####
2530 ===============================================================================
2531 [..]
2532 This subsection provides functions allowing to
2533 (+) Get RTC state
2534
2535 @endverbatim
2536 * @{
2537 */
2538 /**
2539 * @brief Return the RTC handle state.
2540 * @param hrtc RTC handle
2541 * @retval HAL state
2542 */
HAL_RTC_GetState(RTC_HandleTypeDef * hrtc)2543 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
2544 {
2545 /* Return RTC handle state */
2546 return hrtc->State;
2547 }
2548
2549 /**
2550 * @}
2551 */
2552
2553 /**
2554 * @}
2555 */
2556
2557 /** @addtogroup RTC_Private_Functions
2558 * @{
2559 */
2560 /**
2561 * @brief Enter the RTC Initialization mode.
2562 * @note The RTC Initialization mode is write protected, use the
2563 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2564 * @param hrtc RTC handle
2565 * @retval HAL status
2566 */
RTC_EnterInitMode(RTC_HandleTypeDef * hrtc)2567 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
2568 {
2569 uint32_t tickstart;
2570 HAL_StatusTypeDef status = HAL_OK;
2571
2572 /* Check if the Initialization mode is set */
2573 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
2574 if ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
2575 {
2576 /* Set the Initialization mode */
2577 SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
2578
2579 tickstart = HAL_GetTick();
2580 /* Wait till RTC is in INIT state and if Time out is reached exit */
2581 while ((READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
2582 {
2583 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2584 {
2585 status = HAL_TIMEOUT;
2586 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2587 }
2588 }
2589 }
2590 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
2591 if ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
2592 {
2593 /* Set the Initialization mode */
2594 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
2595
2596 tickstart = HAL_GetTick();
2597 /* Wait till RTC is in INIT state and if Time out is reached exit */
2598 while ((READ_BIT(hrtc->Instance->ISR, RTC_ISR_INITF) == 0U) && (status != HAL_TIMEOUT))
2599 {
2600 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2601 {
2602 status = HAL_TIMEOUT;
2603 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2604 }
2605 }
2606 }
2607 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
2608
2609 return status;
2610 }
2611
2612 /**
2613 * @brief Exit the RTC Initialization mode.
2614 * @param hrtc RTC handle
2615 * @retval HAL status
2616 */
RTC_ExitInitMode(RTC_HandleTypeDef * hrtc)2617 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
2618 {
2619 HAL_StatusTypeDef status = HAL_OK;
2620
2621 /* Exit Initialization mode */
2622 #if defined(STM32L412xx) || defined(STM32L422xx) || defined(STM32L4P5xx) || defined(STM32L4Q5xx)
2623 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
2624 #else
2625 /* Exit Initialization mode */
2626 CLEAR_BIT(RTC->ISR, RTC_ISR_INIT);
2627 #endif
2628
2629 /* If CR_BYPSHAD bit = 0, wait for synchro */
2630 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
2631 {
2632 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2633 {
2634 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2635 status = HAL_TIMEOUT;
2636 }
2637 }
2638 else /* WA 2.9.6 Calendar initialization may fail in case of consecutive INIT mode entry */
2639 {
2640 /* Clear BYPSHAD bit */
2641 CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
2642 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2643 {
2644 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2645 status = HAL_TIMEOUT;
2646 }
2647 /* Restore BYPSHAD bit */
2648 SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
2649 }
2650
2651 return status;
2652 }
2653
2654
2655
2656 /**
2657 * @brief Convert a 2 digit decimal to BCD format.
2658 * @param Value Byte to be converted
2659 * @retval Converted byte
2660 */
RTC_ByteToBcd2(uint8_t Value)2661 uint8_t RTC_ByteToBcd2(uint8_t Value)
2662 {
2663 uint32_t bcdhigh = 0U;
2664 uint8_t temp = Value;
2665
2666 while (temp >= 10U)
2667 {
2668 bcdhigh++;
2669 temp -= 10U;
2670 }
2671
2672 return ((uint8_t)(bcdhigh << 4U) | temp);
2673 }
2674
2675 /**
2676 * @brief Convert from 2 digit BCD to Binary.
2677 * @param Value BCD value to be converted
2678 * @retval Converted word
2679 */
RTC_Bcd2ToByte(uint8_t Value)2680 uint8_t RTC_Bcd2ToByte(uint8_t Value)
2681 {
2682 uint8_t tmp;
2683 tmp = ((Value & 0xF0U) >> 4U) * 10U;
2684 return (tmp + (Value & 0x0FU));
2685 }
2686
2687 /**
2688 * @}
2689 */
2690
2691 #endif /* HAL_RTC_MODULE_ENABLED */
2692 /**
2693 * @}
2694 */
2695
2696 /**
2697 * @}
2698 */
2699