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