Subversion Repositories DashDisplay

Rev

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

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