Subversion Repositories FuelGauge

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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