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