source: trunk/firmware/Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rtc.c

Last change on this file was 6, checked in by f.jahn, 8 months ago
File size: 64.6 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32g0xx_hal_rtc.c
4 * @author MCD Application Team
5 * @brief RTC HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Real-Time Clock (RTC) peripheral:
8 * + Initialization/de-initialization functions
9 * + Calendar (Time and Date) configuration
10 * + Alarms (Alarm A and Alarm B) configuration
11 * + WakeUp Timer configuration
12 * + TimeStamp configuration
13 * + Tampers configuration
14 * + Backup Data Registers configuration
15 * + RTC Tamper and TimeStamp Pins Selection
16 * + Interrupts and flags management
17 *
18 @verbatim
19 ===============================================================================
20 ##### RTC Operating Condition #####
21 ===============================================================================
22 [..] The real-time clock (RTC) and the RTC backup registers can be powered
23 from the VBAT voltage when the main VDD supply is powered off.
24 To retain the content of the RTC backup registers and supply the RTC
25 when VDD is turned off, VBAT pin can be connected to an optional
26 standby voltage supplied by a battery or by another source.
27
28 ##### Backup Domain Reset #####
29 ===============================================================================
30 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
31 to their reset values.
32 A backup domain reset is generated when one of the following events occurs:
33 (+) Software reset, triggered by setting the BDRST bit in the
34 RCC Backup domain control register (RCC_BDCR).
35 (+) VDD or VBAT power on, if both supplies have previously been powered off.
36 (+) Tamper detection event resets all data backup registers.
37
38 ##### Backup Domain Access #####
39 =============================================================================
40 [..] After reset, the backup domain (RTC registers and RTC backup data registers)
41 is protected against possible unwanted write accesses.
42 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
43 (+) Enable the Power Controller (PWR) APB1 interface clock using the
44 __HAL_RCC_PWR_CLK_ENABLE() function.
45 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
46 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
47 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
48
49 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
50 (+) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
51 PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
52 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro.
53
54 ##### How to use RTC Driver #####
55 ===================================================================
56 [..]
57 (+) Enable the RTC domain access (see description in the section above).
58 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
59 format using the HAL_RTC_Init() function.
60
61 *** Time and Date configuration ***
62 ===================================
63 [..]
64 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
65 and HAL_RTC_SetDate() functions.
66 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
67
68 *** Alarm configuration ***
69 ===========================
70 [..]
71 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
72 You can also configure the RTC Alarm with interrupt mode using the
73 HAL_RTC_SetAlarm_IT() function.
74 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
75
76 ##### RTC and low power modes #####
77 ==================================================================
78 [..] The MCU can be woken up from a low power mode by an RTC alternate
79 function.
80 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
81 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
82 These RTC alternate functions can wake up the system from the Stop and
83 Standby low power modes.
84 [..] The system can also wake up from low power modes without depending
85 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
86 or the RTC wakeup events.
87 [..] The RTC provides a programmable time base for waking up from the
88 Stop or Standby mode at regular intervals.
89 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
90 is LSE or LSI.
91
92 *** Callback registration ***
93 =============================================
94
95 [..]
96 The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
97 allows the user to configure dynamically the driver callbacks.
98 Use Function @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
99
100 [..]
101 Function @ref HAL_RTC_RegisterCallback() allows to register following callbacks:
102 (+) AlarmAEventCallback : RTC Alarm A Event callback.
103 (+) AlarmBEventCallback : RTC Alarm B Event callback.
104 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
105 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
106 (+) Tamper1EventCallback : RTC Tamper1Event callback.
107 (+) Tamper2EventCallback : RTC Tamper2Event callback.
108 (+) InternalTamper1EventCallback : RTC Internal Tamper 1 Event callback.
109 (+) InternalTamper3EventCallback : RTC Internal Tamper 3 Event callback.
110 (+) InternalTamper4EventCallback : RTC Internal Tamper 4 Event callback.
111 (+) InternalTamper5EventCallback : RTC Internal Tamper 5 Event callback.
112 (+) InternalTamper6EventCallback : RTC Internal Tamper 6 Event callback.
113 (+) InternalTamper7EventCallback : RTC Internal Tamper 7 Event callback.
114 (+) MspInitCallback : RTC MspInit.
115 (+) MspDeInitCallback : RTC MspDeInit.
116 This function takes as parameters the HAL peripheral handle, the Callback ID
117 and a pointer to the user callback function.
118
119 Use function @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
120 weak function.
121 @ref HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
122 and the Callback ID.
123 This function allows to reset following callbacks:
124 (+) AlarmAEventCallback : RTC Alarm A Event callback.
125 (+) AlarmBEventCallback : RTC Alarm B Event callback.
126 (+) TimeStampEventCallback : RTC TimeStamp Event callback.
127 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
128 (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
129 (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
130 (+) InternalTamper1EventCallback : RTC Internal Tamper 1 Event callback.
131 (+) InternalTamper3EventCallback : RTC Internal Tamper 3 Event callback.
132 (+) InternalTamper4EventCallback : RTC Internal Tamper 4 Event callback.
133 (+) InternalTamper5EventCallback : RTC Internal Tamper 5 Event callback.
134 (+) InternalTamper6EventCallback : RTC Internal Tamper 6 Event callback.
135 (+) InternalTamper7EventCallback : RTC Internal Tamper 7 Event callback.
136 (+) MspInitCallback : RTC MspInit callback.
137 (+) MspDeInitCallback : RTC MspDeInit callback.
138
139 [..]
140 By default, after the @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
141 all callbacks are set to the corresponding weak functions :
142 examples @ref AlarmAEventCallback(), @ref WakeUpTimerEventCallback().
143 Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
144 in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
145 (not registered beforehand).
146 If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit()
147 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
148
149 [..]
150 Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
151 Exception done MspInit/MspDeInit that can be registered/unregistered
152 in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
153 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
154 In that case first register the MspInit/MspDeInit user callbacks
155 using @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
156 or @ref HAL_RTC_Init() function.
157
158 [..]
159 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
160 not defined, the callback registration feature is not available and all callbacks
161 are set to the corresponding weak functions.
162
163 @endverbatim
164
165 ******************************************************************************
166 * @attention
167 *
168 * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
169 * All rights reserved.</center></h2>
170 *
171 * This software component is licensed by ST under BSD 3-Clause license,
172 * the "License"; You may not use this file except in compliance with the
173 * License. You may obtain a copy of the License at:
174 * opensource.org/licenses/BSD-3-Clause
175 *
176 ******************************************************************************
177 */
178
179/* Includes ------------------------------------------------------------------*/
180#include "stm32g0xx_hal.h"
181
182/** @addtogroup STM32G0xx_HAL_Driver
183 * @{
184 */
185
186
187/** @addtogroup RTC
188 * @brief RTC HAL module driver
189 * @{
190 */
191
192#ifdef HAL_RTC_MODULE_ENABLED
193
194/* Private typedef -----------------------------------------------------------*/
195/* Private define ------------------------------------------------------------*/
196/* Private macro -------------------------------------------------------------*/
197/* Private variables ---------------------------------------------------------*/
198/* Private function prototypes -----------------------------------------------*/
199/* Exported functions --------------------------------------------------------*/
200
201/** @addtogroup RTC_Exported_Functions
202 * @{
203 */
204
205/** @addtogroup RTC_Exported_Functions_Group1
206 * @brief Initialization and Configuration functions
207 *
208@verbatim
209 ===============================================================================
210 ##### Initialization and de-initialization functions #####
211 ===============================================================================
212 [..] This section provides functions allowing to initialize and configure the
213 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
214 RTC registers Write protection, enter and exit the RTC initialization mode,
215 RTC registers synchronization check and reference clock detection enable.
216 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
217 It is split into 2 programmable prescalers to minimize power consumption.
218 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
219 (++) When both prescalers are used, it is recommended to configure the
220 asynchronous prescaler to a high value to minimize power consumption.
221 (#) All RTC registers are Write protected. Writing to the RTC registers
222 is enabled by writing a key into the Write Protection register, RTC_WPR.
223 (#) To configure the RTC Calendar, user application should enter
224 initialization mode. In this mode, the calendar counter is stopped
225 and its value can be updated. When the initialization sequence is
226 complete, the calendar restarts counting after 4 RTCCLK cycles.
227 (#) To read the calendar through the shadow registers after Calendar
228 initialization, calendar update or after wakeup from low power modes
229 the software must first clear the RSF flag. The software must then
230 wait until it is set again before reading the calendar, which means
231 that the calendar registers have been correctly copied into the
232 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
233 implements the above software sequence (RSF clear and RSF check).
234
235@endverbatim
236 * @{
237 */
238
239/**
240 * @brief Initialize the RTC peripheral
241 * @param hrtc RTC handle
242 * @retval HAL status
243 */
244HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
245{
246 HAL_StatusTypeDef status = HAL_ERROR;
247
248 /* Check the RTC peripheral state */
249 if(hrtc != NULL)
250 {
251 /* Check the parameters */
252 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
253 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
254 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
255 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
256 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
257 assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
258 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
259 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
260 assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
261
262 if(hrtc->State == HAL_RTC_STATE_RESET)
263 {
264 /* Allocate lock resource and initialize it */
265 hrtc->Lock = HAL_UNLOCKED;
266
267 /* Process TAMP peripheral offset from RTC one */
268 hrtc->TampOffset = (TAMP_BASE - RTC_BASE);
269
270#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
271 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
272 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
273 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
274 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
275 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
276 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
277 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback; /*!< Legacy weak InternalTamper3EventCallback */
278 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback; /*!< Legacy weak InternalTamper4EventCallback */
279 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback; /*!< Legacy weak InternalTamper5EventCallback */
280 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback; /*!< Legacy weak InternalTamper6EventCallback */
281
282 if(hrtc->MspInitCallback == NULL)
283 {
284 hrtc->MspInitCallback = HAL_RTC_MspInit;
285 }
286 /* Init the low level hardware */
287 hrtc->MspInitCallback(hrtc);
288
289 if(hrtc->MspDeInitCallback == NULL)
290 {
291 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
292 }
293#else
294 /* Initialize RTC MSP */
295 HAL_RTC_MspInit(hrtc);
296#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
297 }
298
299 /* Set RTC state */
300 hrtc->State = HAL_RTC_STATE_BUSY;
301
302 /* Disable the write protection for RTC registers */
303 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
304
305 /* Enter Initialization mode */
306 status = RTC_EnterInitMode(hrtc);
307 if(status == HAL_OK)
308 {
309 /* Clear RTC_CR FMT, OSEL and POL Bits */
310 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE);
311 /* Set RTC_CR register */
312 hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
313
314 /* Configure the RTC PRER */
315 hrtc->Instance->PRER = (hrtc->Init.SynchPrediv);
316 hrtc->Instance->PRER |= (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos);
317
318 /* Exit Initialization mode */
319 status = RTC_ExitInitMode(hrtc);
320 if (status == HAL_OK)
321 {
322 hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU |RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
323 hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
324 }
325 }
326
327 /* Enable the write protection for RTC registers */
328 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
329
330 if (status == HAL_OK)
331 {
332 hrtc->State = HAL_RTC_STATE_READY;
333 }
334 }
335
336 return status;
337}
338
339/**
340 * @brief DeInitialize the RTC peripheral.
341 * @note This function does not reset the RTC Backup Data registers.
342 * @param hrtc RTC handle
343 * @retval HAL status
344 */
345HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
346{
347 HAL_StatusTypeDef status;
348 uint32_t tickstart;
349
350 /* Check the parameters */
351 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
352
353 /* Set RTC state */
354 hrtc->State = HAL_RTC_STATE_BUSY;
355
356 /* Disable the write protection for RTC registers */
357 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
358
359 /* Enter Initialization mode */
360 status = RTC_EnterInitMode(hrtc);
361 if(status == HAL_OK)
362 {
363 /* Reset TR, DR and CR registers */
364 hrtc->Instance->TR = 0x00000000U;
365 hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
366 /* Reset All CR bits except CR[2:0] */
367 hrtc->Instance->CR &= RTC_CR_WUCKSEL;
368
369 tickstart = HAL_GetTick();
370
371 /* Wait till WUTWF flag is set and if Time out is reached exit */
372 while(((hrtc->Instance->ICSR) & RTC_ICSR_WUTWF) == 0U)
373 {
374 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
375 {
376 /* Enable the write protection for RTC registers */
377 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
378
379 /* Set RTC state */
380 hrtc->State = HAL_RTC_STATE_TIMEOUT;
381
382 return HAL_TIMEOUT;
383 }
384 }
385
386 /* Reset all RTC CR register bits */
387 hrtc->Instance->CR &= 0x00000000U;
388 hrtc->Instance->WUTR = RTC_WUTR_WUT;
389 hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
390 hrtc->Instance->ALRMAR = 0x00000000U;
391 hrtc->Instance->ALRMBR = 0x00000000U;
392 hrtc->Instance->SHIFTR = 0x00000000U;
393 hrtc->Instance->CALR = 0x00000000U;
394 hrtc->Instance->ALRMASSR = 0x00000000U;
395 hrtc->Instance->ALRMBSSR = 0x00000000U;
396
397 /* Exit initialization mode */
398 status = RTC_ExitInitMode(hrtc);
399 }
400
401 /* Enable the write protection for RTC registers */
402 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
403
404 if (status == HAL_OK)
405 {
406#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
407 if(hrtc->MspDeInitCallback == NULL)
408 {
409 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
410 }
411
412 /* DeInit the low level hardware: CLOCK, NVIC.*/
413 hrtc->MspDeInitCallback(hrtc);
414
415#else
416 /* De-Initialize RTC MSP */
417 HAL_RTC_MspDeInit(hrtc);
418#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
419
420 hrtc->State = HAL_RTC_STATE_RESET;
421 }
422
423 /* Release Lock */
424 __HAL_UNLOCK(hrtc);
425
426 return status;
427}
428
429#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
430/**
431 * @brief Register a User RTC Callback
432 * To be used instead of the weak predefined callback
433 * @param hrtc RTC handle
434 * @param CallbackID ID of the callback to be registered
435 * This parameter can be one of the following values:
436 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
437 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
438 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
439 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
440 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
441 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
442 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
443 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
444 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
445 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
446 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
447 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
448 * @param pCallback pointer to the Callback function
449 * @retval HAL status
450 */
451HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
452{
453 HAL_StatusTypeDef status = HAL_OK;
454
455 if(pCallback == NULL)
456 {
457 return HAL_ERROR;
458 }
459
460 /* Process locked */
461 __HAL_LOCK(hrtc);
462
463 if(HAL_RTC_STATE_READY == hrtc->State)
464 {
465 switch (CallbackID)
466 {
467 case HAL_RTC_ALARM_A_EVENT_CB_ID :
468 hrtc->AlarmAEventCallback = pCallback;
469 break;
470
471 case HAL_RTC_ALARM_B_EVENT_CB_ID :
472 hrtc->AlarmBEventCallback = pCallback;
473 break;
474
475 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
476 hrtc->TimeStampEventCallback = pCallback;
477 break;
478
479 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
480 hrtc->WakeUpTimerEventCallback = pCallback;
481 break;
482
483 case HAL_RTC_TAMPER1_EVENT_CB_ID :
484 hrtc->Tamper1EventCallback = pCallback;
485 break;
486
487 case HAL_RTC_TAMPER2_EVENT_CB_ID :
488 hrtc->Tamper2EventCallback = pCallback;
489 break;
490
491
492 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
493 hrtc->InternalTamper3EventCallback = pCallback;
494 break;
495
496 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
497 hrtc->InternalTamper4EventCallback = pCallback;
498 break;
499
500 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
501 hrtc->InternalTamper5EventCallback = pCallback;
502 break;
503
504 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
505 hrtc->InternalTamper6EventCallback = pCallback;
506 break;
507
508
509 case HAL_RTC_MSPINIT_CB_ID :
510 hrtc->MspInitCallback = pCallback;
511 break;
512
513 case HAL_RTC_MSPDEINIT_CB_ID :
514 hrtc->MspDeInitCallback = pCallback;
515 break;
516
517 default :
518 /* Return error status */
519 status = HAL_ERROR;
520 break;
521 }
522 }
523 else if(HAL_RTC_STATE_RESET == hrtc->State)
524 {
525 switch (CallbackID)
526 {
527 case HAL_RTC_MSPINIT_CB_ID :
528 hrtc->MspInitCallback = pCallback;
529 break;
530
531 case HAL_RTC_MSPDEINIT_CB_ID :
532 hrtc->MspDeInitCallback = pCallback;
533 break;
534
535 default :
536 /* Return error status */
537 status = HAL_ERROR;
538 break;
539 }
540 }
541 else
542 {
543 /* Return error status */
544 status = HAL_ERROR;
545 }
546
547 /* Release Lock */
548 __HAL_UNLOCK(hrtc);
549
550 return status;
551}
552
553/**
554 * @brief Unregister an RTC Callback
555 * RTC callback is redirected to the weak predefined callback
556 * @param hrtc RTC handle
557 * @param CallbackID ID of the callback to be unregistered
558 * This parameter can be one of the following values:
559 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
560 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
561 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
562 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
563 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
564 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
565 * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
566 * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
567 * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
568 * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
569 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
570 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
571 * @retval HAL status
572 */
573HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
574{
575 HAL_StatusTypeDef status = HAL_OK;
576
577 /* Process locked */
578 __HAL_LOCK(hrtc);
579
580 if(HAL_RTC_STATE_READY == hrtc->State)
581 {
582 switch (CallbackID)
583 {
584 case HAL_RTC_ALARM_A_EVENT_CB_ID :
585 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
586 break;
587
588 case HAL_RTC_ALARM_B_EVENT_CB_ID :
589 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
590 break;
591
592 case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
593 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
594 break;
595
596 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
597 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
598 break;
599
600 case HAL_RTC_TAMPER1_EVENT_CB_ID :
601 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
602 break;
603
604 case HAL_RTC_TAMPER2_EVENT_CB_ID :
605 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
606 break;
607
608
609 case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
610 hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback; /* Legacy weak InternalTamper3EventCallback */
611 break;
612
613 case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
614 hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback; /* Legacy weak InternalTamper4EventCallback */
615 break;
616
617 case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
618 hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback; /* Legacy weak InternalTamper5EventCallback */
619 break;
620
621 case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
622 hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback; /* Legacy weak InternalTamper6EventCallback */
623 break;
624
625
626 case HAL_RTC_MSPINIT_CB_ID :
627 hrtc->MspInitCallback = HAL_RTC_MspInit;
628 break;
629
630 case HAL_RTC_MSPDEINIT_CB_ID :
631 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
632 break;
633
634 default :
635 /* Return error status */
636 status = HAL_ERROR;
637 break;
638 }
639 }
640 else if(HAL_RTC_STATE_RESET == hrtc->State)
641 {
642 switch (CallbackID)
643 {
644 case HAL_RTC_MSPINIT_CB_ID :
645 hrtc->MspInitCallback = HAL_RTC_MspInit;
646 break;
647
648 case HAL_RTC_MSPDEINIT_CB_ID :
649 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
650 break;
651
652 default :
653 /* Return error status */
654 status = HAL_ERROR;
655 break;
656 }
657 }
658 else
659 {
660 /* Return error status */
661 status = HAL_ERROR;
662 }
663
664 /* Release Lock */
665 __HAL_UNLOCK(hrtc);
666
667 return status;
668}
669#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
670
671/**
672 * @brief Initialize the RTC MSP.
673 * @param hrtc RTC handle
674 * @retval None
675 */
676__weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
677{
678 /* Prevent unused argument(s) compilation warning */
679 UNUSED(hrtc);
680
681 /* NOTE : This function should not be modified, when the callback is needed,
682 the HAL_RTC_MspInit could be implemented in the user file
683 */
684}
685
686/**
687 * @brief DeInitialize the RTC MSP.
688 * @param hrtc RTC handle
689 * @retval None
690 */
691__weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
692{
693 /* Prevent unused argument(s) compilation warning */
694 UNUSED(hrtc);
695
696 /* NOTE : This function should not be modified, when the callback is needed,
697 the HAL_RTC_MspDeInit could be implemented in the user file
698 */
699}
700
701/**
702 * @}
703 */
704
705/** @addtogroup RTC_Exported_Functions_Group2
706 * @brief RTC Time and Date functions
707 *
708@verbatim
709 ===============================================================================
710 ##### RTC Time and Date functions #####
711 ===============================================================================
712
713 [..] This section provides functions allowing to configure Time and Date features
714
715@endverbatim
716 * @{
717 */
718
719/**
720 * @brief Set RTC current time.
721 * @param hrtc RTC handle
722 * @param sTime Pointer to Time structure
723 * @param Format Specifies the format of the entered parameters.
724 * This parameter can be one of the following values:
725 * @arg RTC_FORMAT_BIN: Binary data format
726 * @arg RTC_FORMAT_BCD: BCD data format
727 * @retval HAL status
728 */
729HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
730{
731 uint32_t tmpreg;
732 HAL_StatusTypeDef status;
733
734 /* Check the parameters */
735 assert_param(IS_RTC_FORMAT(Format));
736 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
737 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
738
739 /* Process Locked */
740 __HAL_LOCK(hrtc);
741
742 hrtc->State = HAL_RTC_STATE_BUSY;
743
744 /* Disable the write protection for RTC registers */
745 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
746
747 /* Enter Initialization mode */
748 status = RTC_EnterInitMode(hrtc);
749 if(status == HAL_OK)
750 {
751 if(Format == RTC_FORMAT_BIN)
752 {
753 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
754 {
755 assert_param(IS_RTC_HOUR12(sTime->Hours));
756 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
757 }
758 else
759 {
760 sTime->TimeFormat = 0x00U;
761 assert_param(IS_RTC_HOUR24(sTime->Hours));
762 }
763 assert_param(IS_RTC_MINUTES(sTime->Minutes));
764 assert_param(IS_RTC_SECONDS(sTime->Seconds));
765
766 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
767 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
768 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
769 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
770 }
771 else
772 {
773 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
774 {
775 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
776 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
777 }
778 else
779 {
780 sTime->TimeFormat = 0x00U;
781 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
782 }
783 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
784 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
785 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
786 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
787 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
788 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
789 }
790
791 /* Set the RTC_TR register */
792 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
793
794 /* Clear the bits to be configured */
795 hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
796
797 /* Configure the RTC_CR register */
798 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
799
800 /* Exit Initialization mode */
801 status = RTC_ExitInitMode(hrtc);
802 }
803
804 /* Enable the write protection for RTC registers */
805 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
806
807 if (status == HAL_OK)
808 {
809 hrtc->State = HAL_RTC_STATE_READY;
810 }
811
812 /* Process Unlocked */
813 __HAL_UNLOCK(hrtc);
814
815 return status;
816}
817
818/**
819 * @brief Get RTC current time.
820 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
821 * value in second fraction ratio with time unit following generic formula:
822 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
823 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
824 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
825 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
826 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
827 * to ensure consistency between the time and date values.
828 * @param hrtc RTC handle
829 * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
830 * with input format (BIN or BCD), also SubSeconds field returning the
831 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
832 * factor to be used for second fraction ratio computation.
833 * @param Format Specifies the format of the entered parameters.
834 * This parameter can be one of the following values:
835 * @arg RTC_FORMAT_BIN: Binary data format
836 * @arg RTC_FORMAT_BCD: BCD data format
837 * @retval HAL status
838 */
839HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
840{
841 uint32_t tmpreg;
842
843 /* Check the parameters */
844 assert_param(IS_RTC_FORMAT(Format));
845
846 /* Get subseconds structure field from the corresponding register*/
847 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
848
849 /* Get SecondFraction structure field from the corresponding register field*/
850 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
851
852 /* Get the TR register */
853 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
854
855 /* Fill the structure fields with the read parameters */
856 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
857 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
858 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
859 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
860
861 /* Check the input parameters format */
862 if(Format == RTC_FORMAT_BIN)
863 {
864 /* Convert the time structure parameters to Binary format */
865 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
866 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
867 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
868 }
869
870 return HAL_OK;
871}
872
873/**
874 * @brief Set RTC current date.
875 * @param hrtc RTC handle
876 * @param sDate Pointer to date structure
877 * @param Format specifies the format of the entered parameters.
878 * This parameter can be one of the following values:
879 * @arg RTC_FORMAT_BIN: Binary data format
880 * @arg RTC_FORMAT_BCD: BCD data format
881 * @retval HAL status
882 */
883HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
884{
885 uint32_t datetmpreg;
886 HAL_StatusTypeDef status;
887
888 /* Check the parameters */
889 assert_param(IS_RTC_FORMAT(Format));
890
891 /* Process Locked */
892 __HAL_LOCK(hrtc);
893
894 hrtc->State = HAL_RTC_STATE_BUSY;
895
896 if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
897 {
898 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
899 }
900
901 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
902
903 if(Format == RTC_FORMAT_BIN)
904 {
905 assert_param(IS_RTC_YEAR(sDate->Year));
906 assert_param(IS_RTC_MONTH(sDate->Month));
907 assert_param(IS_RTC_DATE(sDate->Date));
908
909 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
910 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
911 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
912 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
913 }
914 else
915 {
916 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
917 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
918 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
919
920 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
921 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
922 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos)| \
923 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
924 }
925
926 /* Disable the write protection for RTC registers */
927 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
928
929 /* Enter Initialization mode */
930 status = RTC_EnterInitMode(hrtc);
931 if(status == HAL_OK)
932 {
933 /* Set the RTC_DR register */
934 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
935
936 /* Exit Initialization mode */
937 status = RTC_ExitInitMode(hrtc);
938 }
939
940 /* Enable the write protection for RTC registers */
941 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
942
943 if (status == HAL_OK)
944 {
945 hrtc->State = HAL_RTC_STATE_READY;
946 }
947
948 /* Process Unlocked */
949 __HAL_UNLOCK(hrtc);
950
951 return status;
952}
953
954/**
955 * @brief Get RTC current date.
956 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
957 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
958 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
959 * @param hrtc RTC handle
960 * @param sDate Pointer to Date structure
961 * @param Format Specifies the format of the entered parameters.
962 * This parameter can be one of the following values:
963 * @arg RTC_FORMAT_BIN: Binary data format
964 * @arg RTC_FORMAT_BCD: BCD data format
965 * @retval HAL status
966 */
967HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
968{
969 uint32_t datetmpreg;
970
971 /* Check the parameters */
972 assert_param(IS_RTC_FORMAT(Format));
973
974 /* Get the DR register */
975 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
976
977 /* Fill the structure fields with the read parameters */
978 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
979 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
980 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
981 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
982
983 /* Check the input parameters format */
984 if(Format == RTC_FORMAT_BIN)
985 {
986 /* Convert the date structure parameters to Binary format */
987 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
988 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
989 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
990 }
991 return HAL_OK;
992}
993
994/**
995 * @}
996 */
997
998/** @addtogroup RTC_Exported_Functions_Group3
999 * @brief RTC Alarm functions
1000 *
1001@verbatim
1002 ===============================================================================
1003 ##### RTC Alarm functions #####
1004 ===============================================================================
1005
1006 [..] This section provides functions allowing to configure Alarm feature
1007
1008@endverbatim
1009 * @{
1010 */
1011/**
1012 * @brief Set the specified RTC Alarm.
1013 * @param hrtc RTC handle
1014 * @param sAlarm Pointer to Alarm structure
1015 * @param Format Specifies the format of the entered parameters.
1016 * This parameter can be one of the following values:
1017 * @arg RTC_FORMAT_BIN: Binary data format
1018 * @arg RTC_FORMAT_BCD: BCD data format
1019 * @retval HAL status
1020 */
1021HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1022{
1023 uint32_t tickstart;
1024 uint32_t tmpreg;
1025 uint32_t subsecondtmpreg;
1026
1027 /* Check the parameters */
1028 assert_param(IS_RTC_FORMAT(Format));
1029 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1030 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1031 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1032 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1033 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1034
1035 /* Process Locked */
1036 __HAL_LOCK(hrtc);
1037
1038 hrtc->State = HAL_RTC_STATE_BUSY;
1039
1040 if(Format == RTC_FORMAT_BIN)
1041 {
1042 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1043 {
1044 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1045 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1046 }
1047 else
1048 {
1049 sAlarm->AlarmTime.TimeFormat = 0x00U;
1050 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1051 }
1052 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1053 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1054
1055 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1056 {
1057 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1058 }
1059 else
1060 {
1061 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1062 }
1063
1064 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1065 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1066 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1067 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1068 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1069 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1070 ((uint32_t)sAlarm->AlarmMask));
1071 }
1072 else
1073 {
1074 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1075 {
1076 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1077 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1078 }
1079 else
1080 {
1081 sAlarm->AlarmTime.TimeFormat = 0x00U;
1082 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1083 }
1084
1085 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1086 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1087
1088 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1089 {
1090 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1091 }
1092 else
1093 {
1094 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1095 }
1096
1097 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1098 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1099 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1100 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1101 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1102 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1103 ((uint32_t)sAlarm->AlarmMask));
1104 }
1105
1106 /* Configure the Alarm A or Alarm B Sub Second registers */
1107 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1108
1109 /* Disable the write protection for RTC registers */
1110 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1111
1112 /* Configure the Alarm register */
1113 if(sAlarm->Alarm == RTC_ALARM_A)
1114 {
1115 /* Disable the Alarm A interrupt */
1116 __HAL_RTC_ALARMA_DISABLE(hrtc);
1117
1118 /* In case of interrupt mode is used, the interrupt source must disabled */
1119 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1120
1121 tickstart = HAL_GetTick();
1122 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1123 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1124 {
1125 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1126 {
1127 /* Enable the write protection for RTC registers */
1128 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1129
1130 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1131
1132 /* Process Unlocked */
1133 __HAL_UNLOCK(hrtc);
1134
1135 return HAL_TIMEOUT;
1136 }
1137 }
1138
1139 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1140 /* Configure the Alarm A Sub Second register */
1141 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1142 /* Configure the Alarm state: Enable Alarm */
1143 __HAL_RTC_ALARMA_ENABLE(hrtc);
1144 }
1145 else
1146 {
1147 /* Disable the Alarm B interrupt */
1148 __HAL_RTC_ALARMB_DISABLE(hrtc);
1149
1150 /* In case of interrupt mode is used, the interrupt source must disabled */
1151 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1152
1153 tickstart = HAL_GetTick();
1154 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1155 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1156 {
1157 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1158 {
1159 /* Enable the write protection for RTC registers */
1160 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1161
1162 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1163
1164 /* Process Unlocked */
1165 __HAL_UNLOCK(hrtc);
1166
1167 return HAL_TIMEOUT;
1168 }
1169 }
1170
1171 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1172 /* Configure the Alarm B Sub Second register */
1173 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1174 /* Configure the Alarm state: Enable Alarm */
1175 __HAL_RTC_ALARMB_ENABLE(hrtc);
1176 }
1177
1178 /* Enable the write protection for RTC registers */
1179 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1180
1181 /* Change RTC state */
1182 hrtc->State = HAL_RTC_STATE_READY;
1183
1184 /* Process Unlocked */
1185 __HAL_UNLOCK(hrtc);
1186
1187 return HAL_OK;
1188}
1189
1190/**
1191 * @brief Set the specified RTC Alarm with Interrupt.
1192 * @note The Alarm register can only be written when the corresponding Alarm
1193 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
1194 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1195 * @param hrtc RTC handle
1196 * @param sAlarm Pointer to Alarm structure
1197 * @param Format Specifies the format of the entered parameters.
1198 * This parameter can be one of the following values:
1199 * @arg RTC_FORMAT_BIN: Binary data format
1200 * @arg RTC_FORMAT_BCD: BCD data format
1201 * @retval HAL status
1202 */
1203HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1204{
1205 uint32_t tickstart;
1206 uint32_t tmpreg;
1207 uint32_t subsecondtmpreg;
1208
1209 /* Check the parameters */
1210 assert_param(IS_RTC_FORMAT(Format));
1211 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1212 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1213 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1214 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1215 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1216
1217 /* Process Locked */
1218 __HAL_LOCK(hrtc);
1219
1220 hrtc->State = HAL_RTC_STATE_BUSY;
1221
1222 if(Format == RTC_FORMAT_BIN)
1223 {
1224 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1225 {
1226 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1227 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1228 }
1229 else
1230 {
1231 sAlarm->AlarmTime.TimeFormat = 0x00U;
1232 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1233 }
1234 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1235 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1236
1237 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1238 {
1239 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1240 }
1241 else
1242 {
1243 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1244 }
1245 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1246 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1247 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1248 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1249 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1250 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1251 ((uint32_t)sAlarm->AlarmMask));
1252 }
1253 else
1254 {
1255 if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1256 {
1257 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1258 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1259 }
1260 else
1261 {
1262 sAlarm->AlarmTime.TimeFormat = 0x00U;
1263 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1264 }
1265
1266 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1267 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1268
1269 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1270 {
1271 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1272 }
1273 else
1274 {
1275 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1276 }
1277 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1278 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1279 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1280 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1281 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1282 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1283 ((uint32_t)sAlarm->AlarmMask));
1284 }
1285 /* Configure the Alarm A or Alarm B Sub Second registers */
1286 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1287
1288 /* Disable the write protection for RTC registers */
1289 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1290
1291 /* Configure the Alarm register */
1292 if(sAlarm->Alarm == RTC_ALARM_A)
1293 {
1294 /* Disable the Alarm A interrupt */
1295 __HAL_RTC_ALARMA_DISABLE(hrtc);
1296
1297 /* Clear flag alarm A */
1298 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1299
1300 tickstart = HAL_GetTick();
1301 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1302 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1303 {
1304 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1305 {
1306 /* Enable the write protection for RTC registers */
1307 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1308
1309 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1310
1311 /* Process Unlocked */
1312 __HAL_UNLOCK(hrtc);
1313
1314 return HAL_TIMEOUT;
1315 }
1316 }
1317
1318 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1319 /* Configure the Alarm A Sub Second register */
1320 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1321 /* Configure the Alarm state: Enable Alarm */
1322 __HAL_RTC_ALARMA_ENABLE(hrtc);
1323 /* Configure the Alarm interrupt */
1324 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
1325 }
1326 else
1327 {
1328 /* Disable the Alarm B interrupt */
1329 __HAL_RTC_ALARMB_DISABLE(hrtc);
1330
1331 /* Clear flag alarm B */
1332 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_CLEAR_ALRBF);
1333
1334 tickstart = HAL_GetTick();
1335 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1336 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1337 {
1338 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1339 {
1340 /* Enable the write protection for RTC registers */
1341 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1342
1343 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1344
1345 /* Process Unlocked */
1346 __HAL_UNLOCK(hrtc);
1347
1348 return HAL_TIMEOUT;
1349 }
1350 }
1351
1352 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1353 /* Configure the Alarm B Sub Second register */
1354 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1355 /* Configure the Alarm state: Enable Alarm */
1356 __HAL_RTC_ALARMB_ENABLE(hrtc);
1357 /* Configure the Alarm interrupt */
1358 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1359 }
1360
1361 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1362 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1363
1364 /* Enable the write protection for RTC registers */
1365 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1366
1367 hrtc->State = HAL_RTC_STATE_READY;
1368
1369 /* Process Unlocked */
1370 __HAL_UNLOCK(hrtc);
1371
1372 return HAL_OK;
1373}
1374
1375/**
1376 * @brief Deactivate the specified RTC Alarm.
1377 * @param hrtc RTC handle
1378 * @param Alarm Specifies the Alarm.
1379 * This parameter can be one of the following values:
1380 * @arg RTC_ALARM_A: AlarmA
1381 * @arg RTC_ALARM_B: AlarmB
1382 * @retval HAL status
1383 */
1384HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1385{
1386 uint32_t tickstart;
1387
1388 /* Check the parameters */
1389 assert_param(IS_RTC_ALARM(Alarm));
1390
1391 /* Process Locked */
1392 __HAL_LOCK(hrtc);
1393
1394 hrtc->State = HAL_RTC_STATE_BUSY;
1395
1396 /* Disable the write protection for RTC registers */
1397 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1398
1399 if(Alarm == RTC_ALARM_A)
1400 {
1401 /* AlarmA */
1402 __HAL_RTC_ALARMA_DISABLE(hrtc);
1403
1404 /* In case of interrupt mode is used, the interrupt source must disabled */
1405 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1406
1407 tickstart = HAL_GetTick();
1408
1409 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1410 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1411 {
1412 if( (HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1413 {
1414 /* Enable the write protection for RTC registers */
1415 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1416
1417 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1418
1419 /* Process Unlocked */
1420 __HAL_UNLOCK(hrtc);
1421
1422 return HAL_TIMEOUT;
1423 }
1424 }
1425 }
1426 else
1427 {
1428 /* AlarmB */
1429 __HAL_RTC_ALARMB_DISABLE(hrtc);
1430
1431 /* In case of interrupt mode is used, the interrupt source must disabled */
1432 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
1433
1434 tickstart = HAL_GetTick();
1435
1436 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1437 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1438 {
1439 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1440 {
1441 /* Enable the write protection for RTC registers */
1442 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1443
1444 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1445
1446 /* Process Unlocked */
1447 __HAL_UNLOCK(hrtc);
1448
1449 return HAL_TIMEOUT;
1450 }
1451 }
1452 }
1453 /* Enable the write protection for RTC registers */
1454 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1455
1456 hrtc->State = HAL_RTC_STATE_READY;
1457
1458 /* Process Unlocked */
1459 __HAL_UNLOCK(hrtc);
1460
1461 return HAL_OK;
1462}
1463
1464/**
1465 * @brief Get the RTC Alarm value and masks.
1466 * @param hrtc RTC handle
1467 * @param sAlarm Pointer to Date structure
1468 * @param Alarm Specifies the Alarm.
1469 * This parameter can be one of the following values:
1470 * @arg RTC_ALARM_A: AlarmA
1471 * @arg RTC_ALARM_B: AlarmB
1472 * @param Format Specifies the format of the entered parameters.
1473 * This parameter can be one of the following values:
1474 * @arg RTC_FORMAT_BIN: Binary data format
1475 * @arg RTC_FORMAT_BCD: BCD data format
1476 * @retval HAL status
1477 */
1478HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1479{
1480 uint32_t tmpreg;
1481 uint32_t subsecondtmpreg;
1482
1483 /* Check the parameters */
1484 assert_param(IS_RTC_FORMAT(Format));
1485 assert_param(IS_RTC_ALARM(Alarm));
1486
1487 if(Alarm == RTC_ALARM_A)
1488 {
1489 /* AlarmA */
1490 sAlarm->Alarm = RTC_ALARM_A;
1491
1492 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1493 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
1494
1495 /* Fill the structure with the read parameters */
1496 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1497 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1498 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1499 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_SU_Pos);
1500 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1501 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1502 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1503 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1504 }
1505 else
1506 {
1507 sAlarm->Alarm = RTC_ALARM_B;
1508
1509 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1510 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1511
1512 /* Fill the structure with the read parameters */
1513 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1514 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1515 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1516 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1517 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1518 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1519 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1520 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1521 }
1522
1523 if(Format == RTC_FORMAT_BIN)
1524 {
1525 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1526 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1527 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1528 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1529 }
1530
1531 return HAL_OK;
1532}
1533
1534/**
1535 * @brief Handle Alarm interrupt request.
1536 * @param hrtc RTC handle
1537 * @retval None
1538 */
1539void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1540{
1541 /* Get the AlarmA interrupt source enable status */
1542 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
1543 {
1544 /* Get the pending status of the AlarmA Interrupt */
1545 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
1546 {
1547 /* Clear the AlarmA interrupt pending bit */
1548 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1549
1550#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1551 /* Call Compare Match registered Callback */
1552 hrtc->AlarmAEventCallback(hrtc);
1553#else
1554 /* AlarmA callback */
1555 HAL_RTC_AlarmAEventCallback(hrtc);
1556#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1557 }
1558 }
1559
1560 /* Get the AlarmB interrupt source enable status */
1561 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
1562 {
1563 /* Get the pending status of the AlarmB Interrupt */
1564 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
1565 {
1566 /* Clear the AlarmB interrupt pending bit */
1567 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_CLEAR_ALRBF);
1568
1569#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1570 /* Call Compare Match registered Callback */
1571 hrtc->AlarmBEventCallback(hrtc);
1572#else
1573 /* AlarmB callback */
1574 HAL_RTCEx_AlarmBEventCallback(hrtc);
1575#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1576 }
1577 }
1578
1579 /* Change RTC state */
1580 hrtc->State = HAL_RTC_STATE_READY;
1581}
1582
1583/**
1584 * @brief Alarm A callback.
1585 * @param hrtc RTC handle
1586 * @retval None
1587 */
1588__weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1589{
1590 /* Prevent unused argument(s) compilation warning */
1591 UNUSED(hrtc);
1592
1593 /* NOTE : This function should not be modified, when the callback is needed,
1594 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1595 */
1596}
1597
1598/**
1599 * @brief Handle AlarmA Polling request.
1600 * @param hrtc RTC handle
1601 * @param Timeout Timeout duration
1602 * @retval HAL status
1603 */
1604HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1605{
1606
1607 uint32_t tickstart = HAL_GetTick();
1608
1609 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
1610 {
1611 if(Timeout != HAL_MAX_DELAY)
1612 {
1613 if(((HAL_GetTick() - tickstart ) > Timeout)||(Timeout == 0U))
1614 {
1615 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1616 return HAL_TIMEOUT;
1617 }
1618 }
1619 }
1620
1621 /* Clear the Alarm interrupt pending bit */
1622 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1623
1624 /* Change RTC state */
1625 hrtc->State = HAL_RTC_STATE_READY;
1626
1627 return HAL_OK;
1628}
1629
1630/**
1631 * @}
1632 */
1633
1634/** @addtogroup RTC_Exported_Functions_Group4
1635 * @brief Peripheral Control functions
1636 *
1637@verbatim
1638 ===============================================================================
1639 ##### Peripheral Control functions #####
1640 ===============================================================================
1641 [..]
1642 This subsection provides functions allowing to
1643 (+) Wait for RTC Time and Date Synchronization
1644
1645@endverbatim
1646 * @{
1647 */
1648
1649/**
1650 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1651 * synchronized with RTC APB clock.
1652 * @note The RTC Resynchronization mode is write protected, use the
1653 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1654 * @note To read the calendar through the shadow registers after Calendar
1655 * initialization, calendar update or after wakeup from low power modes
1656 * the software must first clear the RSF flag.
1657 * The software must then wait until it is set again before reading
1658 * the calendar, which means that the calendar registers have been
1659 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1660 * @param hrtc RTC handle
1661 * @retval HAL status
1662 */
1663HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1664{
1665 uint32_t tickstart;
1666
1667 /* Clear RSF flag */
1668 hrtc->Instance->ICSR &= (uint32_t)RTC_RSF_MASK;
1669
1670 tickstart = HAL_GetTick();
1671
1672 /* Wait the registers to be synchronised */
1673 while((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
1674 {
1675 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1676 {
1677 return HAL_TIMEOUT;
1678 }
1679 }
1680
1681 return HAL_OK;
1682}
1683
1684/**
1685 * @}
1686 */
1687
1688/** @addtogroup RTC_Exported_Functions_Group5
1689 * @brief Peripheral State functions
1690 *
1691@verbatim
1692 ===============================================================================
1693 ##### Peripheral State functions #####
1694 ===============================================================================
1695 [..]
1696 This subsection provides functions allowing to
1697 (+) Get RTC state
1698
1699@endverbatim
1700 * @{
1701 */
1702/**
1703 * @brief Return the RTC handle state.
1704 * @param hrtc RTC handle
1705 * @retval HAL state
1706 */
1707HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1708{
1709 /* Return RTC handle state */
1710 return hrtc->State;
1711}
1712
1713/**
1714 * @}
1715 */
1716/**
1717 * @}
1718 */
1719
1720/** @addtogroup RTC_Private_Functions
1721 * @{
1722 */
1723/**
1724 * @brief Enter the RTC Initialization mode.
1725 * @note The RTC Initialization mode is write protected, use the
1726 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1727 * @param hrtc RTC handle
1728 * @retval HAL status
1729 */
1730HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1731{
1732 uint32_t tickstart;
1733 HAL_StatusTypeDef status = HAL_OK;
1734
1735 /* Check if the Initialization mode is set */
1736 if((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
1737 {
1738 /* Set the Initialization mode */
1739 SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
1740
1741 tickstart = HAL_GetTick();
1742 /* Wait till RTC is in INIT state and if Time out is reached exit */
1743 while(((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
1744 {
1745 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1746 {
1747 status = HAL_TIMEOUT;
1748 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1749 }
1750 }
1751 }
1752
1753 return status;
1754}
1755
1756/**
1757 * @brief Exit the RTC Initialization mode.
1758 * @param hrtc RTC handle
1759 * @retval HAL status
1760 */
1761HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
1762{
1763 HAL_StatusTypeDef status = HAL_OK;
1764
1765 /* Exit Initialization mode */
1766 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
1767
1768 /* If CR_BYPSHAD bit = 0, wait for synchro */
1769 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1770 {
1771 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1772 {
1773 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1774 status = HAL_TIMEOUT;
1775 }
1776 }
1777 else /* WA 2.7.1 Calendar initialization may fail in case of consecutive INIT mode entry.
1778 Please look at STM32G0 Errata sheet on the internet for details. */
1779 {
1780 /* Clear BYPSHAD bit */
1781 CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1782 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1783 {
1784 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1785 status = HAL_TIMEOUT;
1786 }
1787 /* Restore BYPSHAD bit */
1788 SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1789 }
1790
1791 return status;
1792}
1793/**
1794 * @brief Convert a 2 digit decimal to BCD format.
1795 * @param Value Byte to be converted
1796 * @retval Converted byte
1797 */
1798uint8_t RTC_ByteToBcd2(uint8_t Value)
1799{
1800 uint32_t bcdhigh = 0U;
1801 uint8_t Param = Value;
1802
1803 while(Param >= 10U)
1804 {
1805 bcdhigh++;
1806 Param -= 10U;
1807 }
1808
1809 return ((uint8_t)(bcdhigh << 4U) | Param);
1810}
1811
1812/**
1813 * @brief Convert from 2 digit BCD to Binary.
1814 * @param Value BCD value to be converted
1815 * @retval Converted word
1816 */
1817uint8_t RTC_Bcd2ToByte(uint8_t Value)
1818{
1819 uint32_t tmp;
1820 tmp = (((uint32_t)Value & 0xF0U) >> 4U) * 10U;
1821 return (uint8_t)(tmp + ((uint32_t)Value & 0x0FU));
1822}
1823
1824/**
1825 * @}
1826 */
1827
1828#endif /* HAL_RTC_MODULE_ENABLED */
1829/**
1830 * @}
1831 */
1832
1833/**
1834 * @}
1835 */
1836
1837/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.