Subversion Repositories FuelGauge

Rev

Rev 2 | Details | Compare with Previous | 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.
6 mjames 59
  Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
2 mjames 60
 
6 mjames 61
  Function HAL_RTC_RegisterCallback() allows to register following callbacks:
2 mjames 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
 
6 mjames 73
  Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
2 mjames 74
  weak function.
6 mjames 75
  HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
2 mjames 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
 
6 mjames 87
  By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
2 mjames 88
  all callbacks are set to the corresponding weak functions :
6 mjames 89
  examples AlarmAEventCallback(), WakeUpTimerEventCallback().
2 mjames 90
  Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
6 mjames 91
  in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
2 mjames 92
  (not registered beforehand).
6 mjames 93
  If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
2 mjames 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
6 mjames 101
  using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
102
  or HAL_RTC_Init() function.
2 mjames 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
6 mjames 684
  * @note   DayLightSaving and StoreOperation interfaces are deprecated.
685
  *         To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions.
2 mjames 686
  * @param  Format Specifies the format of the entered parameters.
687
  *          This parameter can be one of the following values:
688
  *            @arg RTC_FORMAT_BIN: Binary data format
689
  *            @arg RTC_FORMAT_BCD: BCD data format
690
  * @retval HAL status
691
  */
692
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
693
{
694
  uint32_t tmpreg = 0U;
695
 
696
  /* Check the parameters */
697
  assert_param(IS_RTC_FORMAT(Format));
698
  assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
699
  assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
700
 
701
  /* Process Locked */
702
  __HAL_LOCK(hrtc);
703
 
704
  hrtc->State = HAL_RTC_STATE_BUSY;
705
 
706
  if (Format == RTC_FORMAT_BIN)
707
  {
708
    if ((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
709
    {
710
      assert_param(IS_RTC_HOUR12(sTime->Hours));
711
      assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
712
    }
713
    else
714
    {
715
      sTime->TimeFormat = 0x00U;
716
      assert_param(IS_RTC_HOUR24(sTime->Hours));
717
    }
718
    assert_param(IS_RTC_MINUTES(sTime->Minutes));
719
    assert_param(IS_RTC_SECONDS(sTime->Seconds));
720
 
721
    tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16U) | \
722
                        ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8U) | \
723
                        ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
724
                        (((uint32_t)sTime->TimeFormat) << 16U));
725
  }
726
  else
727
  {
728
    if ((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
729
    {
730
      assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
731
      assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
732
    }
733
    else
734
    {
735
      sTime->TimeFormat = 0x00U;
736
      assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
737
    }
738
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
739
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
740
    tmpreg = (((uint32_t)(sTime->Hours) << 16U) | \
741
              ((uint32_t)(sTime->Minutes) << 8U) | \
742
              ((uint32_t)sTime->Seconds) | \
743
              ((uint32_t)(sTime->TimeFormat) << 16U));
744
  }
745
 
746
  /* Disable the write protection for RTC registers */
747
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
748
 
749
  /* Set Initialization mode */
750
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
751
  {
752
    /* Enable the write protection for RTC registers */
753
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
754
 
755
    /* Set RTC state */
756
    hrtc->State = HAL_RTC_STATE_ERROR;
757
 
758
    /* Process Unlocked */
759
    __HAL_UNLOCK(hrtc);
760
 
761
    return HAL_ERROR;
762
  }
763
  else
764
  {
765
    /* Set the RTC_TR register */
766
    hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
767
 
6 mjames 768
    /* Clear the bits to be configured (Deprecated. Use HAL_RTC_DST_xxx functions instead) */
2 mjames 769
    hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
770
 
6 mjames 771
    /* Configure the RTC_CR register (Deprecated. Use HAL_RTC_DST_xxx functions instead) */
2 mjames 772
    hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
773
 
774
    /* Exit Initialization mode */
775
    hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
776
 
777
    /* If  CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
778
    if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
779
    {
780
      if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
781
      {
782
        /* Enable the write protection for RTC registers */
783
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
784
 
785
        hrtc->State = HAL_RTC_STATE_ERROR;
786
 
787
        /* Process Unlocked */
788
        __HAL_UNLOCK(hrtc);
789
 
790
        return HAL_ERROR;
791
      }
792
    }
793
 
794
    /* Enable the write protection for RTC registers */
795
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
796
 
797
    hrtc->State = HAL_RTC_STATE_READY;
798
 
799
    __HAL_UNLOCK(hrtc);
800
 
801
    return HAL_OK;
802
  }
803
}
804
 
805
/**
806
  * @brief  Get RTC current time.
807
  * @param  hrtc RTC handle
808
  * @param  sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
809
  *                with input format (BIN or BCD), also SubSeconds field returning the
810
  *                RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
811
  *                factor to be used for second fraction ratio computation.
812
  * @param  Format Specifies the format of the entered parameters.
813
  *          This parameter can be one of the following values:
814
  *            @arg RTC_FORMAT_BIN: Binary data format
815
  *            @arg RTC_FORMAT_BCD: BCD data format
816
  * @note  You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
817
  *        value in second fraction ratio with time unit following generic formula:
818
  *        Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
819
  *        This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
820
  * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
821
  * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
822
  * Reading RTC current time locks the values in calendar shadow registers until Current date is read
823
  * to ensure consistency between the time and date values.
824
  * @retval HAL status
825
  */
826
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
827
{
828
  uint32_t tmpreg = 0;
829
 
830
  /* Check the parameters */
831
  assert_param(IS_RTC_FORMAT(Format));
832
 
833
  /* Get subseconds structure field from the corresponding register*/
834
  sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
835
 
836
  /* Get SecondFraction structure field from the corresponding register field*/
837
  sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
838
 
839
  /* Get the TR register */
840
  tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
841
 
842
  /* Fill the structure fields with the read parameters */
843
  sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
844
  sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
845
  sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
846
  sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U);
847
 
848
  /* Check the input parameters format */
849
  if (Format == RTC_FORMAT_BIN)
850
  {
851
    /* Convert the time structure parameters to Binary format */
852
    sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
853
    sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
854
    sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
855
  }
856
 
857
  return HAL_OK;
858
}
859
 
860
/**
861
  * @brief  Set RTC current date.
862
  * @param  hrtc RTC handle
863
  * @param  sDate Pointer to date structure
864
  * @param  Format specifies the format of the entered parameters.
865
  *          This parameter can be one of the following values:
866
  *            @arg RTC_FORMAT_BIN: Binary data format
867
  *            @arg RTC_FORMAT_BCD: BCD data format
868
  * @retval HAL status
869
  */
870
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
871
{
872
  uint32_t datetmpreg = 0U;
873
 
874
  /* Check the parameters */
875
  assert_param(IS_RTC_FORMAT(Format));
876
 
877
  /* Process Locked */
878
  __HAL_LOCK(hrtc);
879
 
880
  hrtc->State = HAL_RTC_STATE_BUSY;
881
 
882
  if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
883
  {
884
    sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
885
  }
886
 
887
  assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
888
 
889
  if (Format == RTC_FORMAT_BIN)
890
  {
891
    assert_param(IS_RTC_YEAR(sDate->Year));
892
    assert_param(IS_RTC_MONTH(sDate->Month));
893
    assert_param(IS_RTC_DATE(sDate->Date));
894
 
895
    datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16U) | \
896
                  ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8U) | \
897
                  ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
898
                  ((uint32_t)sDate->WeekDay << 13U));
899
  }
900
  else
901
  {
902
    assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
903
    assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
904
    assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
905
 
906
    datetmpreg = ((((uint32_t)sDate->Year) << 16U) | \
907
                  (((uint32_t)sDate->Month) << 8U) | \
908
                  ((uint32_t)sDate->Date) | \
909
                  (((uint32_t)sDate->WeekDay) << 13U));
910
  }
911
 
912
  /* Disable the write protection for RTC registers */
913
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
914
 
915
  /* Set Initialization mode */
916
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
917
  {
918
    /* Enable the write protection for RTC registers */
919
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
920
 
921
    /* Set RTC state*/
922
    hrtc->State = HAL_RTC_STATE_ERROR;
923
 
924
    /* Process Unlocked */
925
    __HAL_UNLOCK(hrtc);
926
 
927
    return HAL_ERROR;
928
  }
929
  else
930
  {
931
    /* Set the RTC_DR register */
932
    hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
933
 
934
    /* Exit Initialization mode */
935
    hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
936
 
937
    /* If  CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
938
    if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
939
    {
940
      if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
941
      {
942
        /* Enable the write protection for RTC registers */
943
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
944
 
945
        hrtc->State = HAL_RTC_STATE_ERROR;
946
 
947
        /* Process Unlocked */
948
        __HAL_UNLOCK(hrtc);
949
 
950
        return HAL_ERROR;
951
      }
952
    }
953
 
954
    /* Enable the write protection for RTC registers */
955
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
956
 
957
    hrtc->State = HAL_RTC_STATE_READY ;
958
 
959
    /* Process Unlocked */
960
    __HAL_UNLOCK(hrtc);
961
 
962
    return HAL_OK;
963
  }
964
}
965
 
966
/**
967
  * @brief  Get RTC current date.
968
  * @param  hrtc RTC handle
969
  * @param  sDate Pointer to Date structure
970
  * @param  Format Specifies the format of the entered parameters.
971
  *          This parameter can be one of the following values:
972
  *            @arg RTC_FORMAT_BIN :  Binary data format
973
  *            @arg RTC_FORMAT_BCD :  BCD data format
974
  * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
975
  * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
976
  * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
977
  * @retval HAL status
978
  */
979
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
980
{
981
  uint32_t datetmpreg = 0U;
982
 
983
  /* Check the parameters */
984
  assert_param(IS_RTC_FORMAT(Format));
985
 
986
  /* Get the DR register */
987
  datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
988
 
989
  /* Fill the structure fields with the read parameters */
990
  sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16U);
991
  sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
992
  sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
993
  sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13U);
994
 
995
  /* Check the input parameters format */
996
  if (Format == RTC_FORMAT_BIN)
997
  {
998
    /* Convert the date structure parameters to Binary format */
999
    sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1000
    sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1001
    sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1002
  }
1003
  return HAL_OK;
1004
}
1005
 
1006
/**
6 mjames 1007
  * @brief  Daylight Saving Time, adda one hour to the calendar in one
1008
  *         single operation without going through the initialization procedure.
1009
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1010
  *                the configuration information for RTC.
1011
  * @retval None
1012
  */
1013
void HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef *hrtc)
1014
{
1015
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1016
  SET_BIT(hrtc->Instance->CR, RTC_CR_ADD1H);
1017
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1018
}
1019
 
1020
/**
1021
  * @brief  Daylight Saving Time, subtracts one hour from the calendar in one
1022
  *         single operation without going through the initialization procedure.
1023
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1024
  *                the configuration information for RTC.
1025
  * @retval None
1026
  */
1027
void HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef *hrtc)
1028
{
1029
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1030
  SET_BIT(hrtc->Instance->CR, RTC_CR_SUB1H);
1031
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1032
}
1033
 
1034
/**
1035
  * @brief  Daylight Saving Time, sets the store operation bit.
1036
  * @note   It can be used by the software in order to memorize the DST status.
1037
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1038
  *                the configuration information for RTC.
1039
  * @retval None
1040
  */
1041
void HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc)
1042
{
1043
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1044
  SET_BIT(hrtc->Instance->CR, RTC_CR_BKP);
1045
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1046
}
1047
 
1048
/**
1049
  * @brief  Daylight Saving Time, clears the store operation bit.
1050
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1051
  *                the configuration information for RTC.
1052
  * @retval None
1053
  */
1054
void HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc)
1055
{
1056
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1057
  CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BKP);
1058
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1059
}
1060
 
1061
/**
1062
  * @brief  Daylight Saving Time, reads the store operation bit.
1063
  * @param  hrtc RTC handle
1064
  * @retval operation see RTC_StoreOperation_Definitions
1065
  */
1066
uint32_t HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc)
1067
{
1068
  return READ_BIT(hrtc->Instance->CR, RTC_CR_BKP);
1069
}
1070
 
1071
/**
2 mjames 1072
  * @}
1073
  */
1074
 
1075
/** @addtogroup RTC_Exported_Functions_Group3
1076
 *  @brief   RTC Alarm functions
1077
 *
1078
@verbatim
1079
 ===============================================================================
1080
                 ##### RTC Alarm functions #####
1081
 ===============================================================================
1082
 
1083
 [..] This section provides functions allowing to configure Alarm feature
1084
 
1085
@endverbatim
1086
  * @{
1087
  */
1088
/**
1089
  * @brief  Set the specified RTC Alarm.
1090
  * @param  hrtc RTC handle
1091
  * @param  sAlarm Pointer to Alarm structure
1092
  * @param  Format Specifies the format of the entered parameters.
1093
  *          This parameter can be one of the following values:
1094
  *             @arg RTC_FORMAT_BIN: Binary data format
1095
  *             @arg RTC_FORMAT_BCD: BCD data format
1096
  * @retval HAL status
1097
  */
1098
HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1099
{
1100
  uint32_t tickstart = 0U;
1101
  uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
1102
 
1103
  /* Check the parameters */
1104
  assert_param(IS_RTC_FORMAT(Format));
1105
  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1106
  assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1107
  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1108
  assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1109
  assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1110
 
1111
  /* Process Locked */
1112
  __HAL_LOCK(hrtc);
1113
 
1114
  hrtc->State = HAL_RTC_STATE_BUSY;
1115
 
1116
  if (Format == RTC_FORMAT_BIN)
1117
  {
1118
    if ((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1119
    {
1120
      assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1121
      assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1122
    }
1123
    else
1124
    {
1125
      sAlarm->AlarmTime.TimeFormat = 0x00U;
1126
      assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1127
    }
1128
    assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1129
    assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1130
 
1131
    if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1132
    {
1133
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1134
    }
1135
    else
1136
    {
1137
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1138
    }
1139
 
1140
    tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
1141
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
1142
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
1143
              ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
1144
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
1145
              ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1146
              ((uint32_t)sAlarm->AlarmMask));
1147
  }
1148
  else
1149
  {
1150
    if ((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1151
    {
1152
      assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1153
      assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1154
    }
1155
    else
1156
    {
1157
      sAlarm->AlarmTime.TimeFormat = 0x00U;
1158
      assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1159
    }
1160
 
1161
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1162
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1163
 
1164
    if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1165
    {
1166
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1167
    }
1168
    else
1169
    {
1170
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1171
    }
1172
 
1173
    tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
1174
              ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
1175
              ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1176
              ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
1177
              ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
1178
              ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1179
              ((uint32_t)sAlarm->AlarmMask));
1180
  }
1181
 
1182
  /* Configure the Alarm A Sub Second registers */
1183
  subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1184
 
1185
  /* Disable the write protection for RTC registers */
1186
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1187
 
1188
  /* Disable the Alarm A interrupt */
1189
  __HAL_RTC_ALARMA_DISABLE(hrtc);
1190
 
1191
  /* In case of interrupt mode is used, the interrupt source must disabled */
1192
  __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1193
 
1194
  tickstart = HAL_GetTick();
1195
  /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1196
  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1197
  {
1198
    if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1199
    {
1200
      /* Enable the write protection for RTC registers */
1201
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1202
 
1203
      hrtc->State = HAL_RTC_STATE_TIMEOUT;
1204
 
1205
      /* Process Unlocked */
1206
      __HAL_UNLOCK(hrtc);
1207
 
1208
      return HAL_TIMEOUT;
1209
    }
1210
  }
1211
 
1212
  hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1213
  /* Configure the Alarm A Sub Second register */
1214
  hrtc->Instance->ALRMASSR = subsecondtmpreg;
1215
  /* Configure the Alarm state: Enable Alarm */
1216
  __HAL_RTC_ALARMA_ENABLE(hrtc);
1217
 
1218
  /* Enable the write protection for RTC registers */
1219
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1220
 
1221
  /* Change RTC state */
1222
  hrtc->State = HAL_RTC_STATE_READY;
1223
 
1224
  /* Process Unlocked */
1225
  __HAL_UNLOCK(hrtc);
1226
 
1227
  return HAL_OK;
1228
}
1229
 
1230
/**
1231
  * @brief  Set the specified RTC Alarm with Interrupt.
1232
  * @param  hrtc RTC handle
1233
  * @param  sAlarm Pointer to Alarm structure
1234
  * @param  Format Specifies the format of the entered parameters.
1235
  *          This parameter can be one of the following values:
1236
  *             @arg RTC_FORMAT_BIN: Binary data format
1237
  *             @arg RTC_FORMAT_BCD: BCD data format
1238
  * @note   The Alarm register can only be written when the corresponding Alarm
1239
  *         is disabled (Use the HAL_RTC_DeactivateAlarm()).
1240
  * @note   The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1241
  * @retval HAL status
1242
  */
1243
HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1244
{
1245
  uint32_t tickstart = 0U;
1246
  uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
1247
 
1248
  /* Check the parameters */
1249
  assert_param(IS_RTC_FORMAT(Format));
1250
  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1251
  assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1252
  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1253
  assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1254
  assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1255
 
1256
  /* Process Locked */
1257
  __HAL_LOCK(hrtc);
1258
 
1259
  hrtc->State = HAL_RTC_STATE_BUSY;
1260
 
1261
  if (Format == RTC_FORMAT_BIN)
1262
  {
1263
    if ((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1264
    {
1265
      assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1266
      assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1267
    }
1268
    else
1269
    {
1270
      sAlarm->AlarmTime.TimeFormat = 0x00U;
1271
      assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1272
    }
1273
    assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1274
    assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1275
 
1276
    if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1277
    {
1278
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1279
    }
1280
    else
1281
    {
1282
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1283
    }
1284
    tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
1285
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
1286
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
1287
              ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
1288
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
1289
              ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1290
              ((uint32_t)sAlarm->AlarmMask));
1291
  }
1292
  else
1293
  {
1294
    if ((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1295
    {
1296
      assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1297
      assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1298
    }
1299
    else
1300
    {
1301
      sAlarm->AlarmTime.TimeFormat = 0x00U;
1302
      assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1303
    }
1304
 
1305
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1306
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1307
 
1308
    if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1309
    {
1310
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1311
    }
1312
    else
1313
    {
1314
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1315
    }
1316
    tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
1317
              ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
1318
              ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1319
              ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
1320
              ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
1321
              ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1322
              ((uint32_t)sAlarm->AlarmMask));
1323
  }
1324
  /* Configure the Alarm A Sub Second registers */
1325
  subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1326
 
1327
  /* Disable the write protection for RTC registers */
1328
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1329
 
1330
  /* Disable the Alarm A interrupt */
1331
  __HAL_RTC_ALARMA_DISABLE(hrtc);
1332
 
1333
  /* Clear flag alarm A */
1334
  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1335
 
1336
  tickstart = HAL_GetTick();
1337
 
1338
  /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1339
  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1340
  {
1341
    if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1342
    {
1343
      /* Enable the write protection for RTC registers */
1344
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1345
 
1346
      hrtc->State = HAL_RTC_STATE_TIMEOUT;
1347
 
1348
      /* Process Unlocked */
1349
      __HAL_UNLOCK(hrtc);
1350
 
1351
      return HAL_TIMEOUT;
1352
    }
1353
  }
1354
 
1355
  hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1356
  /* Configure the Alarm A Sub Second register */
1357
  hrtc->Instance->ALRMASSR = subsecondtmpreg;
1358
  /* Configure the Alarm state: Enable Alarm */
1359
  __HAL_RTC_ALARMA_ENABLE(hrtc);
1360
  /* Configure the Alarm interrupt */
1361
  __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
1362
 
1363
  /* RTC Alarm Interrupt Configuration: EXTI configuration */
1364
  __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1365
 
1366
  __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1367
 
1368
  /* Enable the write protection for RTC registers */
1369
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1370
 
1371
  hrtc->State = HAL_RTC_STATE_READY;
1372
 
1373
  /* Process Unlocked */
1374
  __HAL_UNLOCK(hrtc);
1375
 
1376
  return HAL_OK;
1377
}
1378
 
1379
/**
1380
  * @brief  Deactivate the specified RTC Alarm.
1381
  * @param  hrtc RTC handle
1382
  * @param  Alarm Specifies the Alarm.
1383
  *          This parameter can be one of the following values:
1384
  *            @arg RTC_ALARM_A:  AlarmA
1385
  * @retval HAL status
1386
  */
1387
HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1388
{
1389
  uint32_t tickstart = 0U;
1390
 
1391
  /* Check the parameters */
1392
  assert_param(IS_RTC_ALARM(Alarm));
1393
 
1394
  /* Process Locked */
1395
  __HAL_LOCK(hrtc);
1396
 
1397
  hrtc->State = HAL_RTC_STATE_BUSY;
1398
 
1399
  /* Disable the write protection for RTC registers */
1400
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1401
 
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) == RESET)
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
  /* Enable the write protection for RTC registers */
1426
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1427
 
1428
  hrtc->State = HAL_RTC_STATE_READY;
1429
 
1430
  /* Process Unlocked */
1431
  __HAL_UNLOCK(hrtc);
1432
 
1433
  return HAL_OK;
1434
}
1435
 
1436
/**
1437
  * @brief  Get the RTC Alarm value and masks.
1438
  * @param  hrtc RTC handle
1439
  * @param  sAlarm Pointer to Date structure
1440
  * @param  Alarm Specifies the Alarm.
1441
  *          This parameter can be one of the following values:
1442
  *             @arg RTC_ALARM_A: AlarmA
1443
  * @param  Format Specifies the format of the entered parameters.
1444
  *          This parameter can be one of the following values:
1445
  *             @arg RTC_FORMAT_BIN: Binary data format
1446
  *             @arg RTC_FORMAT_BCD: BCD data format
1447
  * @retval HAL status
1448
  */
1449
HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1450
{
1451
  uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
1452
 
1453
  /* Check the parameters */
1454
  assert_param(IS_RTC_FORMAT(Format));
1455
  assert_param(IS_RTC_ALARM(Alarm));
1456
 
1457
  sAlarm->Alarm = RTC_ALARM_A;
1458
 
1459
  tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1460
  subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
1461
 
1462
  /* Fill the structure with the read parameters */
1463
  sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16U);
1464
  sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8U);
1465
  sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
1466
  sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16U);
1467
  sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1468
  sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24U);
1469
  sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1470
  sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1471
 
1472
  if (Format == RTC_FORMAT_BIN)
1473
  {
1474
    sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1475
    sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1476
    sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1477
    sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1478
  }
1479
 
1480
  return HAL_OK;
1481
}
1482
 
1483
/**
1484
  * @brief  Handle Alarm interrupt request.
1485
  * @param  hrtc RTC handle
1486
  * @retval None
1487
  */
1488
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1489
{
1490
  /* Get the AlarmA interrupt source enable status */
1491
  if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != RESET)
1492
  {
1493
    /* Get the pending status of the AlarmA Interrupt */
1494
    if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != RESET)
1495
    {
1496
      /* AlarmA callback */
1497
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1498
      hrtc->AlarmAEventCallback(hrtc);
1499
#else
1500
      HAL_RTC_AlarmAEventCallback(hrtc);
1501
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1502
 
1503
      /* Clear the AlarmA interrupt pending bit */
1504
      __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1505
    }
1506
  }
1507
 
1508
  /* Clear the EXTI's line Flag for RTC Alarm */
1509
  __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1510
 
1511
  /* Change RTC state */
1512
  hrtc->State = HAL_RTC_STATE_READY;
1513
}
1514
 
1515
/**
1516
  * @brief  Alarm A callback.
1517
  * @param  hrtc RTC handle
1518
  * @retval None
1519
  */
1520
__weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1521
{
1522
  /* Prevent unused argument(s) compilation warning */
1523
  UNUSED(hrtc);
1524
 
1525
  /* NOTE : This function should not be modified, when the callback is needed,
1526
            the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1527
   */
1528
}
1529
 
1530
/**
1531
  * @brief  Handle AlarmA Polling request.
1532
  * @param  hrtc RTC handle
1533
  * @param  Timeout Timeout duration
1534
  * @retval HAL status
1535
  */
1536
HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1537
{
1538
 
1539
  uint32_t tickstart = HAL_GetTick();
1540
 
1541
  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1542
  {
1543
    if (Timeout != HAL_MAX_DELAY)
1544
    {
1545
      if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1546
      {
1547
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1548
        return HAL_TIMEOUT;
1549
      }
1550
    }
1551
  }
1552
 
1553
  /* Clear the Alarm interrupt pending bit */
1554
  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1555
 
1556
  /* Change RTC state */
1557
  hrtc->State = HAL_RTC_STATE_READY;
1558
 
1559
  return HAL_OK;
1560
}
1561
 
1562
/**
1563
  * @}
1564
  */
1565
 
1566
/** @addtogroup RTC_Exported_Functions_Group4
1567
 *  @brief   Peripheral Control functions
1568
 *
1569
@verbatim
1570
 ===============================================================================
1571
                     ##### Peripheral Control functions #####
1572
 ===============================================================================
1573
    [..]
1574
    This subsection provides functions allowing to
1575
      (+) Wait for RTC Time and Date Synchronization
1576
 
1577
@endverbatim
1578
  * @{
1579
  */
1580
 
1581
/**
1582
  * @brief  Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1583
  *         synchronized with RTC APB clock.
1584
  * @note   The RTC Resynchronization mode is write protected, use the
1585
  *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1586
  * @note   To read the calendar through the shadow registers after Calendar
1587
  *         initialization, calendar update or after wakeup from low power modes
1588
  *         the software must first clear the RSF flag.
1589
  *         The software must then wait until it is set again before reading
1590
  *         the calendar, which means that the calendar registers have been
1591
  *         correctly copied into the RTC_TR and RTC_DR shadow registers.
1592
  * @param  hrtc RTC handle
1593
  * @retval HAL status
1594
  */
1595
HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
1596
{
1597
  uint32_t tickstart = 0U;
1598
 
1599
  /* Clear RSF flag */
1600
  hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1601
 
1602
  tickstart = HAL_GetTick();
1603
 
1604
  /* Wait the registers to be synchronised */
1605
  while ((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
1606
  {
1607
    if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1608
    {
1609
      return HAL_TIMEOUT;
1610
    }
1611
  }
1612
 
1613
  return HAL_OK;
1614
}
1615
 
1616
/**
1617
  * @}
1618
  */
1619
 
1620
/** @addtogroup RTC_Exported_Functions_Group5
1621
 *  @brief   Peripheral State functions
1622
 *
1623
@verbatim
1624
 ===============================================================================
1625
                     ##### Peripheral State functions #####
1626
 ===============================================================================
1627
    [..]
1628
    This subsection provides functions allowing to
1629
      (+) Get RTC state
1630
 
1631
@endverbatim
1632
  * @{
1633
  */
1634
/**
1635
  * @brief  Return the RTC handle state.
1636
  * @param  hrtc RTC handle
1637
  * @retval HAL state
1638
  */
1639
HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
1640
{
1641
  /* Return RTC handle state */
1642
  return hrtc->State;
1643
}
1644
 
1645
/**
1646
  * @}
1647
  */
1648
 
1649
/**
1650
  * @}
1651
  */
1652
 
1653
/** @addtogroup RTC_Private_Functions
1654
  * @{
1655
  */
1656
/**
1657
  * @brief  Enter the RTC Initialization mode.
1658
  * @note   The RTC Initialization mode is write protected, use the
1659
  *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1660
  * @param  hrtc RTC handle
1661
  * @retval HAL status
1662
  */
1663
HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
1664
{
1665
  uint32_t tickstart = 0U;
1666
 
1667
  /* Check if the Initialization mode is set */
1668
  if ((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1669
  {
1670
    /* Set the Initialization mode */
1671
    hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1672
 
1673
    tickstart = HAL_GetTick();
1674
 
1675
    /* Wait till RTC is in INIT state and if Time out is reached exit */
1676
    while ((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1677
    {
1678
      if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1679
      {
1680
        return HAL_TIMEOUT;
1681
      }
1682
    }
1683
  }
1684
 
1685
  return HAL_OK;
1686
}
1687
 
1688
 
1689
/**
1690
  * @brief  Convert a 2 digit decimal to BCD format.
1691
  * @param  Value Byte to be converted
1692
  * @retval Converted byte
1693
  */
1694
uint8_t RTC_ByteToBcd2(uint8_t Value)
1695
{
1696
  uint32_t bcdhigh = 0U;
1697
 
1698
  while (Value >= 10U)
1699
  {
1700
    bcdhigh++;
1701
    Value -= 10U;
1702
  }
1703
 
1704
  return ((uint8_t)(bcdhigh << 4U) | Value);
1705
}
1706
 
1707
/**
1708
  * @brief  Convert from 2 digit BCD to Binary.
1709
  * @param  Value BCD value to be converted
1710
  * @retval Converted word
1711
  */
1712
uint8_t RTC_Bcd2ToByte(uint8_t Value)
1713
{
1714
  uint32_t tmp = 0U;
1715
  tmp = ((uint8_t)(Value & (uint8_t)0xF0U) >> (uint8_t)0x4U) * 10U;
1716
  return (tmp + (Value & (uint8_t)0x0FU));
1717
}
1718
/**
1719
  * @}
1720
  */
1721
 
1722
#endif /* HAL_RTC_MODULE_ENABLED */
1723
 
1724
/**
1725
  * @}
1726
  */
1727
 
1728
 
1729
/**
1730
  * @}
1731
  */
1732
 
1733
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/