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