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