Subversion Repositories DashDisplay

Rev

Rev 56 | Go to most recent revision | Details | Compare with Previous | 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;
61 mjames 329
#if defined (RTC_CR_BYPSHAD)
330
    /* If  CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
331
    if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
332
#endif /* RTC_CR_BYPSHAD */
333
    {
334
      if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
335
      {
336
        /* Enable the write protection for RTC registers */
337
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
56 mjames 338
 
61 mjames 339
        hrtc->State = HAL_RTC_STATE_ERROR;
340
 
341
        return HAL_ERROR;
342
      }
343
    }
56 mjames 344
    hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
345
    hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
346
 
347
    /* Enable the write protection for RTC registers */
348
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
349
 
350
    /* Set RTC state */
351
    hrtc->State = HAL_RTC_STATE_READY;
352
 
353
    return HAL_OK;
354
  }
355
}
356
 
357
/**
358
  * @brief  DeInitialize the RTC peripheral.
359
  * @param  hrtc RTC handle
360
  * @note   This function does not reset the RTC Backup Data registers.
361
  * @retval HAL status
362
  */
363
HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
364
{
365
  uint32_t tickstart;
366
 
367
  /* Check the parameters */
368
  assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
369
 
370
  /* Set RTC state */
371
  hrtc->State = HAL_RTC_STATE_BUSY;
372
 
373
  /* Disable the write protection for RTC registers */
374
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
375
 
376
  /* Set Initialization mode */
377
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
378
  {
379
    /* Enable the write protection for RTC registers */
380
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
381
 
382
    /* Set RTC state */
383
    hrtc->State = HAL_RTC_STATE_ERROR;
384
 
385
    return HAL_ERROR;
386
  }
387
  else
388
  {
389
    /* Reset TR, DR and CR registers */
390
    hrtc->Instance->TR = 0x00000000U;
391
    hrtc->Instance->DR = 0x00002101U;
392
    /* Reset All CR bits except CR[2:0] */
393
    hrtc->Instance->CR &= 0x00000007U;
394
 
395
    tickstart = HAL_GetTick();
396
 
397
    /* Wait till WUTWF flag is set and if Time out is reached exit */
398
    while (((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == 0U)
399
    {
400
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
401
      {
402
        /* Enable the write protection for RTC registers */
403
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
404
 
405
        /* Set RTC state */
406
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
407
 
408
        return HAL_TIMEOUT;
409
      }
410
    }
411
 
412
    /* Reset all RTC CR register bits */
413
    hrtc->Instance->CR &= 0x00000000U;
414
    hrtc->Instance->WUTR = 0x0000FFFFU;
415
    hrtc->Instance->PRER = 0x007F00FFU;
416
    hrtc->Instance->CALIBR = 0x00000000U;
417
    hrtc->Instance->ALRMAR = 0x00000000U;
418
    hrtc->Instance->ALRMBR = 0x00000000U;
419
#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)
420
    hrtc->Instance->SHIFTR = 0x00000000U;
421
    hrtc->Instance->CALR = 0x00000000U;
422
    hrtc->Instance->ALRMASSR = 0x00000000U;
423
    hrtc->Instance->ALRMBSSR = 0x00000000U;
424
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
425
    /* Reset ISR register and exit initialization mode */
426
    hrtc->Instance->ISR = 0x00000000U;
427
 
428
    /* Reset Tamper and alternate functions configuration register */
429
    hrtc->Instance->TAFCR = 0x00000000U;
430
 
431
    /* Wait for synchro */
432
    if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
433
    {
434
      /* Enable the write protection for RTC registers */
435
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
436
 
437
      hrtc->State = HAL_RTC_STATE_ERROR;
438
 
439
      return HAL_ERROR;
440
    }
441
  }
442
 
443
  /* Enable the write protection for RTC registers */
444
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
445
 
446
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
447
  if (hrtc->MspDeInitCallback == NULL)
448
  {
449
    hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
450
  }
451
 
452
  /* DeInit the low level hardware: CLOCK, NVIC.*/
453
  hrtc->MspDeInitCallback(hrtc);
454
 
455
#else
456
  /* De-Initialize RTC MSP */
457
  HAL_RTC_MspDeInit(hrtc);
458
#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
459
 
460
  hrtc->State = HAL_RTC_STATE_RESET;
461
 
462
  /* Release Lock */
463
  __HAL_UNLOCK(hrtc);
464
 
465
  return HAL_OK;
466
}
467
 
468
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
469
/**
470
  * @brief  Register a User RTC Callback
471
  *         To be used instead of the weak predefined callback
472
  * @param  hrtc RTC handle
473
  * @param  CallbackID ID of the callback to be registered
474
  *         This parameter can be one of the following values:
475
  *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID          Alarm A Event Callback ID
476
  *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID          Alarm B Event Callback ID
477
  *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID        TimeStamp Event Callback ID
478
  *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID      WakeUp Timer Event Callback ID
479
  *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID          Tamper 1 Callback ID
480
  *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID          Tamper 2 Callback ID
481
  *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID          Tamper 3 Callback ID
482
  *          @arg @ref HAL_RTC_MSPINIT_CB_ID                Msp Init callback ID
483
  *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID              Msp DeInit callback ID
484
  * @param  pCallback pointer to the Callback function
485
  * @retval HAL status
486
  */
487
HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
488
{
489
  HAL_StatusTypeDef status = HAL_OK;
490
 
491
  if (pCallback == NULL)
492
  {
493
    return HAL_ERROR;
494
  }
495
 
496
  /* Process locked */
497
  __HAL_LOCK(hrtc);
498
 
499
  if (HAL_RTC_STATE_READY == hrtc->State)
500
  {
501
    switch (CallbackID)
502
    {
503
      case HAL_RTC_ALARM_A_EVENT_CB_ID :
504
        hrtc->AlarmAEventCallback = pCallback;
505
        break;
506
 
507
      case HAL_RTC_ALARM_B_EVENT_CB_ID :
508
        hrtc->AlarmBEventCallback = pCallback;
509
        break;
510
 
511
      case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
512
        hrtc->TimeStampEventCallback = pCallback;
513
        break;
514
 
515
      case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
516
        hrtc->WakeUpTimerEventCallback = pCallback;
517
        break;
518
 
519
      case HAL_RTC_TAMPER1_EVENT_CB_ID :
520
        hrtc->Tamper1EventCallback = pCallback;
521
        break;
522
 
523
#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)
524
      case HAL_RTC_TAMPER2_EVENT_CB_ID :
525
        hrtc->Tamper2EventCallback = pCallback;
526
        break;
527
 
528
      case HAL_RTC_TAMPER3_EVENT_CB_ID :
529
        hrtc->Tamper3EventCallback = pCallback;
530
        break;
531
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
532
 
533
      case HAL_RTC_MSPINIT_CB_ID :
534
        hrtc->MspInitCallback = pCallback;
535
        break;
536
 
537
      case HAL_RTC_MSPDEINIT_CB_ID :
538
        hrtc->MspDeInitCallback = pCallback;
539
        break;
540
 
541
      default :
542
        /* Return error status */
543
        status =  HAL_ERROR;
544
        break;
545
    }
546
  }
547
  else if (HAL_RTC_STATE_RESET == hrtc->State)
548
  {
549
    switch (CallbackID)
550
    {
551
      case HAL_RTC_MSPINIT_CB_ID :
552
        hrtc->MspInitCallback = pCallback;
553
        break;
554
 
555
      case HAL_RTC_MSPDEINIT_CB_ID :
556
        hrtc->MspDeInitCallback = pCallback;
557
        break;
558
 
559
      default :
560
        /* Return error status */
561
        status =  HAL_ERROR;
562
        break;
563
    }
564
  }
565
  else
566
  {
567
    /* Return error status */
568
    status =  HAL_ERROR;
569
  }
570
 
571
  /* Release Lock */
572
  __HAL_UNLOCK(hrtc);
573
 
574
  return status;
575
}
576
 
577
/**
578
  * @brief  Unregister an RTC Callback
579
  *         RTC callabck is redirected to the weak predefined callback
580
  * @param  hrtc RTC handle
581
  * @param  CallbackID ID of the callback to be unregistered
582
  *         This parameter can be one of the following values:
583
  *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID          Alarm A Event Callback ID
584
  *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID          Alarm B Event Callback ID
585
  *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID        TimeStamp Event Callback ID
586
  *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID      WakeUp Timer Event Callback ID
587
  *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID          Tamper 1 Callback ID
588
  *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID          Tamper 2 Callback ID
589
  *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID          Tamper 3 Callback ID
590
  *          @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
591
  *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
592
  * @retval HAL status
593
  */
594
HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
595
{
596
  HAL_StatusTypeDef status = HAL_OK;
597
 
598
  /* Process locked */
599
  __HAL_LOCK(hrtc);
600
 
601
  if (HAL_RTC_STATE_READY == hrtc->State)
602
  {
603
    switch (CallbackID)
604
    {
605
      case HAL_RTC_ALARM_A_EVENT_CB_ID :
606
        hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback;         /* Legacy weak AlarmAEventCallback    */
607
        break;
608
 
609
      case HAL_RTC_ALARM_B_EVENT_CB_ID :
610
        hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback;          /* Legacy weak AlarmBEventCallback */
611
        break;
612
 
613
      case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
614
        hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback;    /* Legacy weak TimeStampEventCallback    */
615
        break;
616
 
617
      case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
618
        hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
619
        break;
620
 
621
      case HAL_RTC_TAMPER1_EVENT_CB_ID :
622
        hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback;         /* Legacy weak Tamper1EventCallback   */
623
        break;
624
 
625
#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)
626
      case HAL_RTC_TAMPER2_EVENT_CB_ID :
627
        hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback;         /* Legacy weak Tamper2EventCallback         */
628
        break;
629
 
630
      case HAL_RTC_TAMPER3_EVENT_CB_ID :
631
        hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback;         /* Legacy weak Tamper3EventCallback         */
632
        break;
633
#endif
634
      case HAL_RTC_MSPINIT_CB_ID :
635
        hrtc->MspInitCallback = HAL_RTC_MspInit;
636
        break;
637
 
638
      case HAL_RTC_MSPDEINIT_CB_ID :
639
        hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
640
        break;
641
 
642
      default :
643
        /* Return error status */
644
        status =  HAL_ERROR;
645
        break;
646
    }
647
  }
648
  else if (HAL_RTC_STATE_RESET == hrtc->State)
649
  {
650
    switch (CallbackID)
651
    {
652
      case HAL_RTC_MSPINIT_CB_ID :
653
        hrtc->MspInitCallback = HAL_RTC_MspInit;
654
        break;
655
 
656
      case HAL_RTC_MSPDEINIT_CB_ID :
657
        hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
658
        break;
659
 
660
      default :
661
        /* Return error status */
662
        status =  HAL_ERROR;
663
        break;
664
    }
665
  }
666
  else
667
  {
668
    /* Return error status */
669
    status =  HAL_ERROR;
670
  }
671
 
672
  /* Release Lock */
673
  __HAL_UNLOCK(hrtc);
674
 
675
  return status;
676
}
677
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
678
 
679
/**
680
  * @brief  Initialize the RTC MSP.
681
  * @param  hrtc RTC handle
682
  * @retval None
683
  */
684
__weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
685
{
686
  /* Prevent unused argument(s) compilation warning */
687
  UNUSED(hrtc);
688
 
689
  /* NOTE : This function Should not be modified, when the callback is needed,
690
            the HAL_RTC_MspInit could be implemented in the user file
691
   */
692
}
693
 
694
/**
695
  * @brief  DeInitialize the RTC MSP.
696
  * @param  hrtc RTC handle
697
  * @retval None
698
  */
699
__weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
700
{
701
  /* Prevent unused argument(s) compilation warning */
702
  UNUSED(hrtc);
703
 
704
  /* NOTE : This function Should not be modified, when the callback is needed,
705
            the HAL_RTC_MspDeInit could be implemented in the user file
706
   */
707
}
708
 
709
/**
710
  * @}
711
  */
712
 
713
/** @addtogroup RTC_Exported_Functions_Group2
714
 *  @brief   RTC Time and Date functions
715
 *
716
@verbatim
717
 ===============================================================================
718
                 ##### RTC Time and Date functions #####
719
 ===============================================================================
720
 
721
 [..] This section provides functions allowing to configure Time and Date features
722
 
723
@endverbatim
724
  * @{
725
  */
726
 
727
/**
728
  * @brief  Set RTC current time.
729
  * @param  hrtc RTC handle
730
  * @param  sTime Pointer to Time structure
731
  * @param  Format Specifies the format of the entered parameters.
732
  *          This parameter can be one of the following values:
733
  *            @arg RTC_FORMAT_BIN: Binary data format
734
  *            @arg RTC_FORMAT_BCD: BCD data format
735
  * @retval HAL status
736
  */
737
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
738
{
739
  uint32_t tmpreg;
740
 
741
  /* Check the parameters */
742
  assert_param(IS_RTC_FORMAT(Format));
743
  assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
744
  assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
745
 
746
  /* Process Locked */
747
  __HAL_LOCK(hrtc);
748
 
749
  hrtc->State = HAL_RTC_STATE_BUSY;
750
 
751
  if (Format == RTC_FORMAT_BIN)
752
  {
753
    if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
754
    {
755
      assert_param(IS_RTC_HOUR12(sTime->Hours));
756
      assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
757
    }
758
    else
759
    {
760
      sTime->TimeFormat = 0x00U;
761
      assert_param(IS_RTC_HOUR24(sTime->Hours));
762
    }
763
    assert_param(IS_RTC_MINUTES(sTime->Minutes));
764
    assert_param(IS_RTC_SECONDS(sTime->Seconds));
765
 
766
    tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16U) | \
767
                        ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8U) | \
768
                        ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
769
                        (((uint32_t)sTime->TimeFormat) << 16U));
770
  }
771
  else
772
  {
773
    if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
774
    {
775
      assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
776
      assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
777
    }
778
    else
779
    {
780
      sTime->TimeFormat = 0x00U;
781
      assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
782
    }
783
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
784
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
785
    tmpreg = (((uint32_t)(sTime->Hours) << 16U) | \
786
              ((uint32_t)(sTime->Minutes) << 8U) | \
787
              ((uint32_t)sTime->Seconds) | \
788
              ((uint32_t)(sTime->TimeFormat) << 16U));
789
  }
790
  UNUSED(tmpreg);
791
  /* Disable the write protection for RTC registers */
792
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
793
 
794
  /* Set Initialization mode */
795
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
796
  {
797
    /* Enable the write protection for RTC registers */
798
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
799
 
800
    /* Set RTC state */
801
    hrtc->State = HAL_RTC_STATE_ERROR;
802
 
803
    /* Process Unlocked */
804
    __HAL_UNLOCK(hrtc);
805
 
806
    return HAL_ERROR;
807
  }
808
  else
809
  {
810
    /* Set the RTC_TR register */
811
    hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
812
 
61 mjames 813
    /* Clear the bits to be configured (Deprecated. Use HAL_RTC_DST_xxx functions instead) */
56 mjames 814
    hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
815
 
61 mjames 816
    /* Configure the RTC_CR register (Deprecated. Use HAL_RTC_DST_xxx functions instead) */
56 mjames 817
    hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
818
 
819
    /* Exit Initialization mode */
820
    hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
821
 
822
    /* Wait for synchro */
823
    if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
824
    {
825
      /* Enable the write protection for RTC registers */
826
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
827
 
828
      hrtc->State = HAL_RTC_STATE_ERROR;
829
 
830
      /* Process Unlocked */
831
      __HAL_UNLOCK(hrtc);
832
 
833
      return HAL_ERROR;
834
    }
835
 
836
    /* Enable the write protection for RTC registers */
837
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
838
 
839
    hrtc->State = HAL_RTC_STATE_READY;
840
 
841
    __HAL_UNLOCK(hrtc);
842
 
843
    return HAL_OK;
844
  }
845
}
846
 
847
/**
848
  * @brief  Get RTC current time.
849
  * @param  hrtc RTC handle
850
  * @param  sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
851
  *                with input format (BIN or BCD), also SubSeconds field (if availabale) returning the
852
  *                RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
853
  *                factor to be used for second fraction ratio computation.
854
  * @param  Format Specifies the format of the entered parameters.
855
  *          This parameter can be one of the following values:
856
  *            @arg RTC_FORMAT_BIN: Binary data format
857
  *            @arg RTC_FORMAT_BCD: BCD data format
858
  * @note  If available, you can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
859
  *        value in second fraction ratio with time unit following generic formula:
860
  *        Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
861
  *        This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
862
  * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
863
  * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
864
  *        Reading RTC current time locks the values in calendar shadow registers until Current date is read
865
  *        to ensure consistency between the time and date values.
866
  * @retval HAL status
867
  */
868
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
869
{
870
  uint32_t tmpreg;
871
 
872
  /* Check the parameters */
873
  assert_param(IS_RTC_FORMAT(Format));
874
 
875
#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)
876
  /* Get subseconds structure field from the corresponding register*/
877
  sTime->SubSeconds = (uint32_t)((hrtc->Instance->SSR) & RTC_SSR_SS);
878
 
879
  /* Get SecondFraction structure field from the corresponding register field*/
880
  sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
881
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
882
 
883
  /* Get the TR register */
884
  tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
885
 
886
  /* Fill the structure fields with the read parameters */
887
  sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
888
  sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
889
  sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
890
  sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U);
891
 
892
  /* Check the input parameters format */
893
  if (Format == RTC_FORMAT_BIN)
894
  {
895
    /* Convert the time structure parameters to Binary format */
896
    sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
897
    sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
898
    sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
899
  }
900
 
901
  return HAL_OK;
902
}
903
 
904
/**
905
  * @brief  Set RTC current date.
906
  * @param  hrtc RTC handle
907
  * @param  sDate Pointer to date structure
908
  * @param  Format specifies the format of the entered parameters.
909
  *          This parameter can be one of the following values:
910
  *            @arg RTC_FORMAT_BIN: Binary data format
911
  *            @arg RTC_FORMAT_BCD: BCD data format
912
  * @retval HAL status
913
  */
914
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
915
{
916
  uint32_t datetmpreg;
917
 
918
  /* Check the parameters */
919
  assert_param(IS_RTC_FORMAT(Format));
920
 
921
  /* Process Locked */
922
  __HAL_LOCK(hrtc);
923
 
924
  hrtc->State = HAL_RTC_STATE_BUSY;
925
 
926
  if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
927
  {
928
    sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
929
  }
930
 
931
  assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
932
 
933
  if (Format == RTC_FORMAT_BIN)
934
  {
935
    assert_param(IS_RTC_YEAR(sDate->Year));
936
    assert_param(IS_RTC_MONTH(sDate->Month));
937
    assert_param(IS_RTC_DATE(sDate->Date));
938
 
939
    datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16U) | \
940
                  ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8U) | \
941
                  ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
942
                  ((uint32_t)sDate->WeekDay << 13U));
943
  }
944
  else
945
  {
946
    assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
947
    assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
948
    assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
949
 
950
    datetmpreg = ((((uint32_t)sDate->Year) << 16U) | \
951
                  (((uint32_t)sDate->Month) << 8U) | \
952
                  ((uint32_t)sDate->Date) | \
953
                  (((uint32_t)sDate->WeekDay) << 13U));
954
  }
955
 
956
  /* Disable the write protection for RTC registers */
957
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
958
 
959
  /* Set Initialization mode */
960
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
961
  {
962
    /* Enable the write protection for RTC registers */
963
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
964
 
965
    /* Set RTC state*/
966
    hrtc->State = HAL_RTC_STATE_ERROR;
967
 
968
    /* Process Unlocked */
969
    __HAL_UNLOCK(hrtc);
970
 
971
    return HAL_ERROR;
972
  }
973
  else
974
  {
975
    /* Set the RTC_DR register */
976
    hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
977
 
978
    /* Exit Initialization mode */
979
    hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
980
 
981
    /* Wait for synchro */
982
    if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
983
    {
984
      /* Enable the write protection for RTC registers */
985
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
986
 
987
      hrtc->State = HAL_RTC_STATE_ERROR;
988
 
989
      /* Process Unlocked */
990
      __HAL_UNLOCK(hrtc);
991
 
992
      return HAL_ERROR;
993
    }
994
 
995
    /* Enable the write protection for RTC registers */
996
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
997
 
998
    hrtc->State = HAL_RTC_STATE_READY ;
999
 
1000
    /* Process Unlocked */
1001
    __HAL_UNLOCK(hrtc);
1002
 
1003
    return HAL_OK;
1004
  }
1005
}
1006
 
1007
/**
1008
  * @brief  Get RTC current date.
1009
  * @param  hrtc RTC handle
1010
  * @param  sDate Pointer to Date structure
1011
  * @param  Format Specifies the format of the entered parameters.
1012
  *          This parameter can be one of the following values:
1013
  *            @arg RTC_FORMAT_BIN:  Binary data format
1014
  *            @arg RTC_FORMAT_BCD:  BCD data format
1015
  * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1016
  * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1017
  * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1018
  * @retval HAL status
1019
  */
1020
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1021
{
1022
  uint32_t datetmpreg;
1023
 
1024
  /* Check the parameters */
1025
  assert_param(IS_RTC_FORMAT(Format));
1026
 
1027
  /* Get the DR register */
1028
  datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
1029
 
1030
  /* Fill the structure fields with the read parameters */
1031
  sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16U);
1032
  sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
1033
  sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
1034
  sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13U);
1035
 
1036
  /* Check the input parameters format */
1037
  if (Format == RTC_FORMAT_BIN)
1038
  {
1039
    /* Convert the date structure parameters to Binary format */
1040
    sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1041
    sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1042
    sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1043
  }
1044
  return HAL_OK;
1045
}
1046
 
1047
/**
61 mjames 1048
  * @brief  Daylight Saving Time, adda one hour to the calendar in one
1049
  *         single operation without going through the initialization procedure.
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_Add1Hour(RTC_HandleTypeDef *hrtc)
1055
{
1056
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1057
  SET_BIT(hrtc->Instance->CR, RTC_CR_ADD1H);
1058
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1059
}
1060
 
1061
/**
1062
  * @brief  Daylight Saving Time, subtracts one hour from the calendar in one
1063
  *         single operation without going through the initialization procedure.
1064
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1065
  *                the configuration information for RTC.
1066
  * @retval None
1067
  */
1068
void HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef *hrtc)
1069
{
1070
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1071
  SET_BIT(hrtc->Instance->CR, RTC_CR_SUB1H);
1072
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1073
}
1074
 
1075
/**
1076
  * @brief  Daylight Saving Time, sets the store operation bit.
1077
  * @note   It can be used by the software in order to memorize the DST status.
1078
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1079
  *                the configuration information for RTC.
1080
  * @retval None
1081
  */
1082
void HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc)
1083
{
1084
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1085
  SET_BIT(hrtc->Instance->CR, RTC_CR_BKP);
1086
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1087
}
1088
 
1089
/**
1090
  * @brief  Daylight Saving Time, clears the store operation bit.
1091
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1092
  *                the configuration information for RTC.
1093
  * @retval None
1094
  */
1095
void HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc)
1096
{
1097
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1098
  CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BKP);
1099
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1100
}
1101
 
1102
/**
1103
  * @brief  Daylight Saving Time, reads the store operation bit.
1104
  * @param  hrtc RTC handle
1105
  * @retval operation see RTC_StoreOperation_Definitions
1106
  */
1107
uint32_t HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc)
1108
{
1109
  return READ_BIT(hrtc->Instance->CR, RTC_CR_BKP);
1110
}
1111
 
1112
/**
56 mjames 1113
  * @}
1114
  */
1115
 
1116
/** @addtogroup RTC_Exported_Functions_Group3
1117
 *  @brief   RTC Alarm functions
1118
 *
1119
@verbatim
1120
 ===============================================================================
1121
                 ##### RTC Alarm functions #####
1122
 ===============================================================================
1123
 
1124
 [..] This section provides functions allowing to configure Alarm feature
1125
 
1126
@endverbatim
1127
  * @{
1128
  */
1129
/**
1130
  * @brief  Set the specified RTC Alarm.
1131
  * @param  hrtc RTC handle
1132
  * @param  sAlarm Pointer to Alarm structure
1133
  * @param  Format Specifies the format of the entered parameters.
1134
  *          This parameter can be one of the following values:
1135
  *             @arg RTC_FORMAT_BIN: Binary data format
1136
  *             @arg RTC_FORMAT_BCD: BCD data format
1137
  * @retval HAL status
1138
  */
1139
HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1140
{
1141
  uint32_t tickstart;
1142
  uint32_t tmpreg;
1143
 
1144
#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)
1145
  uint32_t subsecondtmpreg = 0;
1146
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1147
 
1148
  /* Check the parameters */
1149
  assert_param(IS_RTC_FORMAT(Format));
1150
  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1151
  assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1152
  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1153
#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)
1154
  assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1155
  assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1156
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1157
 
1158
  /* Process Locked */
1159
  __HAL_LOCK(hrtc);
1160
 
1161
  hrtc->State = HAL_RTC_STATE_BUSY;
1162
 
1163
  if (Format == RTC_FORMAT_BIN)
1164
  {
1165
    if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1166
    {
1167
      assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1168
      assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1169
    }
1170
    else
1171
    {
1172
      sAlarm->AlarmTime.TimeFormat = 0x00U;
1173
      assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1174
    }
1175
    assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1176
    assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1177
 
1178
    if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1179
    {
1180
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1181
    }
1182
    else
1183
    {
1184
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1185
    }
1186
 
1187
    tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
1188
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
1189
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
1190
              ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
1191
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
1192
              ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1193
              ((uint32_t)sAlarm->AlarmMask));
1194
  }
1195
  else
1196
  {
1197
    if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1198
    {
1199
      assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1200
      assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1201
    }
1202
    else
1203
    {
1204
      sAlarm->AlarmTime.TimeFormat = 0x00U;
1205
      assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1206
    }
1207
 
1208
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1209
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1210
 
1211
    if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1212
    {
1213
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1214
    }
1215
    else
1216
    {
1217
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1218
    }
1219
 
1220
    tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
1221
              ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
1222
              ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1223
              ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
1224
              ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
1225
              ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1226
              ((uint32_t)sAlarm->AlarmMask));
1227
  }
1228
 
1229
#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)
1230
  /* Configure the Alarm A or Alarm B Sub Second registers */
1231
  subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1232
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1233
 
1234
  /* Disable the write protection for RTC registers */
1235
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1236
 
1237
  /* Configure the Alarm register */
1238
  if (sAlarm->Alarm == RTC_ALARM_A)
1239
  {
1240
    /* Disable the Alarm A interrupt */
1241
    __HAL_RTC_ALARMA_DISABLE(hrtc);
1242
 
1243
    /* In case of interrupt mode is used, the interrupt source must disabled */
1244
    __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1245
 
1246
    tickstart = HAL_GetTick();
1247
    /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1248
    while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1249
    {
1250
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1251
      {
1252
        /* Enable the write protection for RTC registers */
1253
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1254
 
1255
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1256
 
1257
        /* Process Unlocked */
1258
        __HAL_UNLOCK(hrtc);
1259
 
1260
        return HAL_TIMEOUT;
1261
      }
1262
    }
1263
 
1264
    hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1265
#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)
1266
    /* Configure the Alarm A Sub Second register */
1267
    hrtc->Instance->ALRMASSR = subsecondtmpreg;
1268
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1269
    /* Configure the Alarm state: Enable Alarm */
1270
    __HAL_RTC_ALARMA_ENABLE(hrtc);
1271
  }
1272
  else
1273
  {
1274
    /* Disable the Alarm B interrupt */
1275
    __HAL_RTC_ALARMB_DISABLE(hrtc);
1276
 
1277
    /* In case of interrupt mode is used, the interrupt source must disabled */
1278
    __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1279
 
1280
    tickstart = HAL_GetTick();
1281
    /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1282
    while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1283
    {
1284
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1285
      {
1286
        /* Enable the write protection for RTC registers */
1287
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1288
 
1289
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1290
 
1291
        /* Process Unlocked */
1292
        __HAL_UNLOCK(hrtc);
1293
 
1294
        return HAL_TIMEOUT;
1295
      }
1296
    }
1297
 
1298
    hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1299
#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)
1300
    /* Configure the Alarm B Sub Second register */
1301
    hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1302
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1303
    /* Configure the Alarm state: Enable Alarm */
1304
    __HAL_RTC_ALARMB_ENABLE(hrtc);
1305
  }
1306
 
1307
  /* Enable the write protection for RTC registers */
1308
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1309
 
1310
  /* Change RTC state */
1311
  hrtc->State = HAL_RTC_STATE_READY;
1312
 
1313
  /* Process Unlocked */
1314
  __HAL_UNLOCK(hrtc);
1315
 
1316
  return HAL_OK;
1317
}
1318
 
1319
/**
1320
  * @brief  Set the specified RTC Alarm with Interrupt.
1321
  * @param  hrtc RTC handle
1322
  * @param  sAlarm Pointer to Alarm structure
1323
  * @param  Format Specifies the format of the entered parameters.
1324
  *          This parameter can be one of the following values:
1325
  *             @arg RTC_FORMAT_BIN: Binary data format
1326
  *             @arg RTC_FORMAT_BCD: BCD data format
1327
  * @note   The Alarm register can only be written when the corresponding Alarm
1328
  *         is disabled (Use the HAL_RTC_DeactivateAlarm()).
1329
  * @note   The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1330
  * @retval HAL status
1331
  */
1332
HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1333
{
1334
  uint32_t tickstart = 0;
1335
  uint32_t tmpreg = 0;
1336
#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)
1337
  uint32_t subsecondtmpreg = 0;
1338
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1339
 
1340
  /* Check the parameters */
1341
  assert_param(IS_RTC_FORMAT(Format));
1342
  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1343
  assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1344
  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1345
#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)
1346
  assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1347
  assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1348
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1349
 
1350
  /* Process Locked */
1351
  __HAL_LOCK(hrtc);
1352
 
1353
  hrtc->State = HAL_RTC_STATE_BUSY;
1354
 
1355
  if (Format == RTC_FORMAT_BIN)
1356
  {
1357
    if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1358
    {
1359
      assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1360
      assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1361
    }
1362
    else
1363
    {
1364
      sAlarm->AlarmTime.TimeFormat = 0x00U;
1365
      assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1366
    }
1367
    assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1368
    assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1369
 
1370
    if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1371
    {
1372
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1373
    }
1374
    else
1375
    {
1376
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1377
    }
1378
    tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
1379
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
1380
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
1381
              ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
1382
              ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
1383
              ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1384
              ((uint32_t)sAlarm->AlarmMask));
1385
  }
1386
  else
1387
  {
1388
    if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1389
    {
1390
      assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1391
      assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1392
    }
1393
    else
1394
    {
1395
      sAlarm->AlarmTime.TimeFormat = 0x00U;
1396
      assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1397
    }
1398
 
1399
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1400
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1401
 
1402
    if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1403
    {
1404
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1405
    }
1406
    else
1407
    {
1408
      assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1409
    }
1410
    tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
1411
              ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
1412
              ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1413
              ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
1414
              ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
1415
              ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1416
              ((uint32_t)sAlarm->AlarmMask));
1417
  }
1418
#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)
1419
  /* Configure the Alarm A or Alarm B Sub Second registers */
1420
  subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1421
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1422
 
1423
  /* Disable the write protection for RTC registers */
1424
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1425
 
1426
  /* Configure the Alarm register */
1427
  if (sAlarm->Alarm == RTC_ALARM_A)
1428
  {
1429
    /* Disable the Alarm A interrupt */
1430
    __HAL_RTC_ALARMA_DISABLE(hrtc);
1431
 
1432
    /* Clear flag alarm A */
1433
    __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1434
 
1435
    tickstart = HAL_GetTick();
1436
    /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1437
    while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1438
    {
1439
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1440
      {
1441
        /* Enable the write protection for RTC registers */
1442
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1443
 
1444
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1445
 
1446
        /* Process Unlocked */
1447
        __HAL_UNLOCK(hrtc);
1448
 
1449
        return HAL_TIMEOUT;
1450
      }
1451
    }
1452
 
1453
    hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1454
#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)
1455
    /* Configure the Alarm A Sub Second register */
1456
    hrtc->Instance->ALRMASSR = subsecondtmpreg;
1457
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1458
    /* Configure the Alarm state: Enable Alarm */
1459
    __HAL_RTC_ALARMA_ENABLE(hrtc);
1460
    /* Configure the Alarm interrupt */
1461
    __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
1462
  }
1463
  else
1464
  {
1465
    /* Disable the Alarm B interrupt */
1466
    __HAL_RTC_ALARMB_DISABLE(hrtc);
1467
 
1468
    /* Clear flag alarm B */
1469
    __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1470
 
1471
    tickstart = HAL_GetTick();
1472
    /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1473
    while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1474
    {
1475
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1476
      {
1477
        /* Enable the write protection for RTC registers */
1478
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1479
 
1480
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1481
 
1482
        /* Process Unlocked */
1483
        __HAL_UNLOCK(hrtc);
1484
 
1485
        return HAL_TIMEOUT;
1486
      }
1487
    }
1488
 
1489
    hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1490
#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)
1491
    /* Configure the Alarm B Sub Second register */
1492
    hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1493
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1494
    /* Configure the Alarm state: Enable Alarm */
1495
    __HAL_RTC_ALARMB_ENABLE(hrtc);
1496
    /* Configure the Alarm interrupt */
1497
    __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1498
  }
1499
 
1500
  /* RTC Alarm Interrupt Configuration: EXTI configuration */
1501
  __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1502
 
1503
  __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1504
 
1505
  /* Enable the write protection for RTC registers */
1506
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1507
 
1508
  hrtc->State = HAL_RTC_STATE_READY;
1509
 
1510
  /* Process Unlocked */
1511
  __HAL_UNLOCK(hrtc);
1512
 
1513
  return HAL_OK;
1514
}
1515
 
1516
/**
1517
  * @brief  Deactivate the specified RTC Alarm.
1518
  * @param  hrtc RTC handle
1519
  * @param  Alarm Specifies the Alarm.
1520
  *          This parameter can be one of the following values:
1521
  *            @arg RTC_ALARM_A:  AlarmA
1522
  *            @arg RTC_ALARM_B:  AlarmB
1523
  * @retval HAL status
1524
  */
1525
HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1526
{
1527
  uint32_t tickstart;
1528
 
1529
  /* Check the parameters */
1530
  assert_param(IS_RTC_ALARM(Alarm));
1531
 
1532
  /* Process Locked */
1533
  __HAL_LOCK(hrtc);
1534
 
1535
  hrtc->State = HAL_RTC_STATE_BUSY;
1536
 
1537
  /* Disable the write protection for RTC registers */
1538
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1539
 
1540
  if (Alarm == RTC_ALARM_A)
1541
  {
1542
    /* AlarmA */
1543
    __HAL_RTC_ALARMA_DISABLE(hrtc);
1544
 
1545
    /* In case of interrupt mode is used, the interrupt source must disabled */
1546
    __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1547
 
1548
    tickstart = HAL_GetTick();
1549
 
1550
    /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1551
    while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1552
    {
1553
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1554
      {
1555
        /* Enable the write protection for RTC registers */
1556
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1557
 
1558
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1559
 
1560
        /* Process Unlocked */
1561
        __HAL_UNLOCK(hrtc);
1562
 
1563
        return HAL_TIMEOUT;
1564
      }
1565
    }
1566
  }
1567
  else
1568
  {
1569
    /* AlarmB */
1570
    __HAL_RTC_ALARMB_DISABLE(hrtc);
1571
 
1572
    /* In case of interrupt mode is used, the interrupt source must disabled */
1573
    __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1574
 
1575
    tickstart = HAL_GetTick();
1576
 
1577
    /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1578
    while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1579
    {
1580
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1581
      {
1582
        /* Enable the write protection for RTC registers */
1583
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1584
 
1585
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1586
 
1587
        /* Process Unlocked */
1588
        __HAL_UNLOCK(hrtc);
1589
 
1590
        return HAL_TIMEOUT;
1591
      }
1592
    }
1593
  }
1594
  /* Enable the write protection for RTC registers */
1595
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1596
 
1597
  hrtc->State = HAL_RTC_STATE_READY;
1598
 
1599
  /* Process Unlocked */
1600
  __HAL_UNLOCK(hrtc);
1601
 
1602
  return HAL_OK;
1603
}
1604
 
1605
/**
1606
  * @brief  Get the RTC Alarm value and masks.
1607
  * @param  hrtc RTC handle
1608
  * @param  sAlarm Pointer to Date structure
1609
  * @param  Alarm Specifies the Alarm.
1610
  *          This parameter can be one of the following values:
1611
  *             @arg RTC_ALARM_A: AlarmA
1612
  *             @arg RTC_ALARM_B: AlarmB
1613
  * @param  Format Specifies the format of the entered parameters.
1614
  *          This parameter can be one of the following values:
1615
  *             @arg RTC_FORMAT_BIN: Binary data format
1616
  *             @arg RTC_FORMAT_BCD: BCD data format
1617
  * @retval HAL status
1618
  */
1619
HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1620
{
1621
  uint32_t tmpreg;
1622
#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)
1623
  uint32_t subsecondtmpreg;
1624
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1625
 
1626
  /* Check the parameters */
1627
  assert_param(IS_RTC_FORMAT(Format));
1628
  assert_param(IS_RTC_ALARM(Alarm));
1629
 
1630
  if (Alarm == RTC_ALARM_A)
1631
  {
1632
    /* AlarmA */
1633
    sAlarm->Alarm = RTC_ALARM_A;
1634
 
1635
    tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1636
#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)
1637
    subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
1638
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1639
  }
1640
  else
1641
  {
1642
    sAlarm->Alarm = RTC_ALARM_B;
1643
 
1644
    tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1645
#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)
1646
    subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1647
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1648
  }
1649
 
1650
  /* Fill the structure with the read parameters */
1651
  sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16U);
1652
  sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8U);
1653
  sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
1654
  sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16U);
1655
#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)
1656
  sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1657
#endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1658
  sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1659
  sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1660
  sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1661
 
1662
  if (Format == RTC_FORMAT_BIN)
1663
  {
1664
    sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1665
    sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1666
    sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1667
    sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1668
  }
1669
 
1670
  return HAL_OK;
1671
}
1672
 
1673
/**
1674
  * @brief  Handle Alarm interrupt request.
1675
  * @param  hrtc RTC handle
1676
  * @retval None
1677
  */
1678
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1679
{
1680
  /* Get the AlarmA interrupt source enable status */
1681
  if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
1682
  {
1683
    /* Get the pending status of the AlarmA Interrupt */
1684
    if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
1685
    {
1686
      /* AlarmA callback */
1687
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1688
      hrtc->AlarmAEventCallback(hrtc);
1689
#else
1690
      HAL_RTC_AlarmAEventCallback(hrtc);
1691
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1692
 
1693
      /* Clear the AlarmA interrupt pending bit */
1694
      __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1695
    }
1696
  }
1697
 
1698
  /* Get the AlarmB interrupt source enable status */
1699
  if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
1700
  {
1701
    /* Get the pending status of the AlarmB Interrupt */
1702
    if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
1703
    {
1704
      /* AlarmB callback */
1705
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1706
      hrtc->AlarmBEventCallback(hrtc);
1707
#else
1708
      HAL_RTCEx_AlarmBEventCallback(hrtc);
1709
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1710
 
1711
      /* Clear the AlarmB interrupt pending bit */
1712
      __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1713
    }
1714
  }
1715
  /* Clear the EXTI's line Flag for RTC Alarm */
1716
  __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1717
 
1718
  /* Change RTC state */
1719
  hrtc->State = HAL_RTC_STATE_READY;
1720
}
1721
 
1722
/**
1723
  * @brief  Alarm A callback.
1724
  * @param  hrtc RTC handle
1725
  * @retval None
1726
  */
1727
__weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1728
{
1729
  /* Prevent unused argument(s) compilation warning */
1730
  UNUSED(hrtc);
1731
 
1732
  /* NOTE : This function Should not be modified, when the callback is needed,
1733
            the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1734
   */
1735
}
1736
 
1737
/**
1738
  * @brief  Handle AlarmA Polling request.
1739
  * @param  hrtc RTC handle
1740
  * @param  Timeout Timeout duration
1741
  * @retval HAL status
1742
  */
1743
HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1744
{
1745
 
1746
  uint32_t tickstart = HAL_GetTick();
1747
 
1748
  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
1749
  {
1750
    if (Timeout != HAL_MAX_DELAY)
1751
    {
1752
      if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1753
      {
1754
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1755
        return HAL_TIMEOUT;
1756
      }
1757
    }
1758
  }
1759
 
1760
  /* Clear the Alarm interrupt pending bit */
1761
  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1762
 
1763
  /* Change RTC state */
1764
  hrtc->State = HAL_RTC_STATE_READY;
1765
 
1766
  return HAL_OK;
1767
}
1768
 
1769
/**
1770
  * @}
1771
  */
1772
 
1773
/** @addtogroup RTC_Exported_Functions_Group4
1774
 *  @brief   Peripheral Control functions
1775
 *
1776
@verbatim
1777
 ===============================================================================
1778
                     ##### Peripheral Control functions #####
1779
 ===============================================================================
1780
    [..]
1781
    This subsection provides functions allowing to
1782
      (+) Wait for RTC Time and Date Synchronization
1783
 
1784
@endverbatim
1785
  * @{
1786
  */
1787
 
1788
/**
1789
  * @brief  Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1790
  *         synchronized with RTC APB clock.
1791
  * @note   The RTC Resynchronization mode is write protected, use the
1792
  *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1793
  * @note   To read the calendar through the shadow registers after Calendar
1794
  *         initialization, calendar update or after wakeup from low power modes
1795
  *         the software must first clear the RSF flag.
1796
  *         The software must then wait until it is set again before reading
1797
  *         the calendar, which means that the calendar registers have been
1798
  *         correctly copied into the RTC_TR and RTC_DR shadow registers.
1799
  * @param  hrtc RTC handle
1800
  * @retval HAL status
1801
  */
1802
HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
1803
{
1804
  uint32_t tickstart;
1805
 
61 mjames 1806
#if defined (RTC_CR_BYPSHAD)
56 mjames 1807
  /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1808
  if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
61 mjames 1809
#endif /* RTC_CR_BYPSHAD */
56 mjames 1810
  {
1811
    /* Clear RSF flag */
1812
    hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1813
 
1814
    tickstart = HAL_GetTick();
1815
 
1816
    /* Wait the registers to be synchronised */
1817
    while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
1818
    {
1819
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1820
      {
1821
        return HAL_TIMEOUT;
1822
      }
1823
    }
1824
  }
1825
 
1826
  return HAL_OK;
1827
}
1828
 
1829
/**
1830
  * @}
1831
  */
1832
 
1833
/** @addtogroup RTC_Exported_Functions_Group5
1834
 *  @brief   Peripheral State functions
1835
 *
1836
@verbatim
1837
 ===============================================================================
1838
                     ##### Peripheral State functions #####
1839
 ===============================================================================
1840
    [..]
1841
    This subsection provides functions allowing to
1842
      (+) Get RTC state
1843
 
1844
@endverbatim
1845
  * @{
1846
  */
1847
/**
1848
  * @brief  Return the RTC handle state.
1849
  * @param  hrtc RTC handle
1850
  * @retval HAL state
1851
  */
1852
HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
1853
{
1854
  /* Return RTC handle state */
1855
  return hrtc->State;
1856
}
1857
 
1858
/**
1859
  * @}
1860
  */
1861
/**
1862
  * @}
1863
  */
1864
 
1865
/** @addtogroup RTC_Private_Functions
1866
  * @{
1867
  */
1868
/**
1869
  * @brief  Enter the RTC Initialization mode.
1870
  * @note   The RTC Initialization mode is write protected, use the
1871
  *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1872
  * @param  hrtc RTC handle
1873
  * @retval HAL status
1874
  */
1875
HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
1876
{
1877
  uint32_t tickstart;
1878
 
1879
  /* Check if the Initialization mode is set */
1880
  if ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
1881
  {
1882
    /* Set the Initialization mode */
1883
    hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1884
 
1885
    tickstart = HAL_GetTick();
1886
    /* Wait till RTC is in INIT state and if Time out is reached exit */
1887
    while ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
1888
    {
1889
      if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1890
      {
1891
        return HAL_TIMEOUT;
1892
      }
1893
    }
1894
  }
1895
 
1896
  return HAL_OK;
1897
}
1898
 
1899
 
1900
/**
1901
  * @brief  Convert a 2 digit decimal to BCD format.
1902
  * @param  Value Byte to be converted
1903
  * @retval Converted byte
1904
  */
1905
uint8_t RTC_ByteToBcd2(uint8_t Value)
1906
{
1907
  uint32_t bcdhigh = 0U;
1908
  uint8_t Param = Value;
1909
 
1910
  while (Param >= 10U)
1911
  {
1912
    bcdhigh++;
1913
    Param -= 10U;
1914
  }
1915
 
1916
  return ((uint8_t)(bcdhigh << 4U) | Param);
1917
}
1918
 
1919
/**
1920
  * @brief  Convert from 2 digit BCD to Binary.
1921
  * @param  Value BCD value to be converted
1922
  * @retval Converted word
1923
  */
1924
uint8_t RTC_Bcd2ToByte(uint8_t Value)
1925
{
1926
  uint32_t tmp;
1927
  tmp = (((uint32_t)Value & 0xF0U) >> 4U) * 10U;
1928
  return (uint8_t)(tmp + ((uint32_t)Value & 0x0FU));
1929
}
1930
 
1931
/**
1932
  * @}
1933
  */
1934
 
1935
#endif /* HAL_RTC_MODULE_ENABLED */
1936
/**
1937
  * @}
1938
  */
1939
 
1940
/**
1941
  * @}
1942
  */
1943
 
1944
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/