Subversion Repositories LedShow

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 9
Line 1... Line 1...
1
/**
1
/**
2
  ******************************************************************************
2
  ******************************************************************************
3
  * @file    stm32f1xx_hal_rtc.c
3
  * @file    stm32f1xx_hal_rtc.c
4
  * @author  MCD Application Team
4
  * @author  MCD Application Team
5
  * @brief   RTC HAL module driver.
5
  * @brief   RTC HAL module driver.
6
  *          This file provides firmware functions to manage the following
6
  *          This file provides firmware functions to manage the following
7
  *          functionalities of the Real Time Clock (RTC) peripheral:
7
  *          functionalities of the Real Time Clock (RTC) peripheral:
8
  *           + Initialization and de-initialization functions
8
  *           + Initialization and de-initialization functions
9
  *           + RTC Time and Date functions
9
  *           + RTC Time and Date functions
10
  *           + RTC Alarm functions
10
  *           + RTC Alarm functions
11
  *           + Peripheral Control functions  
11
  *           + Peripheral Control functions
12
  *           + Peripheral State functions
12
  *           + Peripheral State functions
13
  *        
13
  *
14
  @verbatim
14
  @verbatim
15
  ==============================================================================
15
  ==============================================================================
16
                  ##### How to use this driver #####
16
                  ##### How to use this driver #####
17
  ==================================================================
17
  ==================================================================
18
  [..]
18
  [..]
19
    (+) Enable the RTC domain access (see description in the section above).
19
    (+) Enable the RTC domain access (see description in the section above).
20
    (+) Configure the RTC Prescaler (Asynchronous prescaler to generate RTC 1Hz time base)
20
    (+) Configure the RTC Prescaler (Asynchronous prescaler to generate RTC 1Hz time base)
21
        using the HAL_RTC_Init() function.
21
        using the HAL_RTC_Init() function.
22
 
22
 
23
  *** Time and Date configuration ***
23
  *** Time and Date configuration ***
24
  ===================================
24
  ===================================
25
  [..]
25
  [..]
26
    (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
26
    (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
27
        and HAL_RTC_SetDate() functions.
27
        and HAL_RTC_SetDate() functions.
28
    (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
28
    (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
29
 
29
 
30
  *** Alarm configuration ***
30
  *** Alarm configuration ***
31
  ===========================
31
  ===========================
32
  [..]
32
  [..]
33
    (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
33
    (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
34
        You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
34
        You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
35
    (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
35
    (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
36
 
36
 
37
  *** Tamper configuration ***
37
  *** Tamper configuration ***
38
  ============================
38
  ============================
39
  [..]
39
  [..]
40
    (+) Enable the RTC Tamper and configure the Tamper Level using the
40
    (+) Enable the RTC Tamper and configure the Tamper Level using the
41
        HAL_RTCEx_SetTamper() function. You can configure RTC Tamper with interrupt
41
        HAL_RTCEx_SetTamper() function. You can configure RTC Tamper with interrupt
42
        mode using HAL_RTCEx_SetTamper_IT() function.
42
        mode using HAL_RTCEx_SetTamper_IT() function.
43
    (+) The TAMPER1 alternate function can be mapped to PC13
43
    (+) The TAMPER1 alternate function can be mapped to PC13
44
 
44
 
45
  *** Backup Data Registers configuration ***
45
  *** Backup Data Registers configuration ***
46
  ===========================================
46
  ===========================================
47
  [..]
47
  [..]
48
    (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
48
    (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
49
        function.  
49
        function.
50
    (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
50
    (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
51
        function.
51
        function.
52
     
52
 
53
                  ##### WARNING: Drivers Restrictions  #####
53
                  ##### WARNING: Drivers Restrictions  #####
54
  ==================================================================
54
  ==================================================================
55
  [..] RTC version used on STM32F1 families is version V1. All the features supported by V2
55
  [..] RTC version used on STM32F1 families is version V1. All the features supported by V2
56
       (other families) will be not supported on F1.
56
       (other families) will be not supported on F1.
57
  [..] As on V2, main RTC features are managed by HW. But on F1, date feature is completely
57
  [..] As on V2, main RTC features are managed by HW. But on F1, date feature is completely
58
       managed by SW.
58
       managed by SW.
59
  [..] Then, there are some restrictions compared to other families:
59
  [..] Then, there are some restrictions compared to other families:
60
    (+) Only format 24 hours supported in HAL (format 12 hours not supported)
60
    (+) Only format 24 hours supported in HAL (format 12 hours not supported)
61
    (+) Date is saved in SRAM. Then, when MCU is in STOP or STANDBY mode, date will be lost.
61
    (+) Date is saved in SRAM. Then, when MCU is in STOP or STANDBY mode, date will be lost.
62
        User should implement a way to save date before entering in low power mode (an
62
        User should implement a way to save date before entering in low power mode (an
63
        example is provided with firmware package based on backup registers)
63
        example is provided with firmware package based on backup registers)
64
    (+) Date is automatically updated each time a HAL_RTC_GetTime or HAL_RTC_GetDate is called.
64
    (+) Date is automatically updated each time a HAL_RTC_GetTime or HAL_RTC_GetDate is called.
65
    (+) Alarm detection is limited to 1 day. It will expire only 1 time (no alarm repetition, need
65
    (+) Alarm detection is limited to 1 day. It will expire only 1 time (no alarm repetition, need
66
        to program a new alarm)
66
        to program a new alarm)
67
 
67
 
68
              ##### Backup Domain Operating Condition #####
68
              ##### Backup Domain Operating Condition #####
69
  ==============================================================================
69
  ==============================================================================
70
  [..] The real-time clock (RTC) and the RTC backup registers can be powered
70
  [..] The real-time clock (RTC) and the RTC backup registers can be powered
71
       from the VBAT voltage when the main VDD supply is powered off.
71
       from the VBAT voltage when the main VDD supply is powered off.
72
       To retain the content of the RTC backup registers and supply the RTC
72
       To retain the content of the RTC backup registers and supply the RTC
73
       when VDD is turned off, VBAT pin can be connected to an optional
73
       when VDD is turned off, VBAT pin can be connected to an optional
74
       standby voltage supplied by a battery or by another source.
74
       standby voltage supplied by a battery or by another source.
75
 
75
 
76
  [..] To allow the RTC operating even when the main digital supply (VDD) is turned
76
  [..] To allow the RTC operating even when the main digital supply (VDD) is turned
77
       off, the VBAT pin powers the following blocks:
77
       off, the VBAT pin powers the following blocks:
78
    (+) The RTC
78
    (#) The RTC
79
    (+) The LSE oscillator
79
    (#) The LSE oscillator
-
 
80
    (#) The backup SRAM when the low power backup regulator is enabled
80
    (+) PC13 I/O
81
    (#) PC13 to PC15 I/Os, plus PI8 I/O (when available)
81
 
82
 
82
  [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
83
  [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
83
       the following pins are available:
84
       the following pins are available:
84
    (+) PC13 can be used as a Tamper pin
85
    (+) PC13 can be used as a Tamper pin
85
 
86
 
86
  [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
87
  [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
87
       because VDD is not present), the following pins are available:
88
       because VDD is not present), the following pins are available:
88
    (+) PC13 can be used as the Tamper pin
89
    (+) PC13 can be used as the Tamper pin
89
             
90
 
90
                   ##### Backup Domain Reset #####
91
                   ##### Backup Domain Reset #####
91
  ==================================================================
92
  ==================================================================
92
  [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
93
  [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
93
       to their reset values.
94
       to their reset values.
94
  [..] A backup domain reset is generated when one of the following events occurs:
95
  [..] A backup domain reset is generated when one of the following events occurs:
95
    (#) Software reset, triggered by setting the BDRST bit in the
96
    (#) Software reset, triggered by setting the BDRST bit in the
96
        RCC Backup domain control register (RCC_BDCR).
97
        RCC Backup domain control register (RCC_BDCR).
97
    (#) VDD or VBAT power on, if both supplies have previously been powered off.  
98
    (#) VDD or VBAT power on, if both supplies have previously been powered off.
98
    (#) Tamper detection event resets all data backup registers.
99
    (#) Tamper detection event resets all data backup registers.
99
 
100
 
100
                   ##### Backup Domain Access #####
101
                   ##### Backup Domain Access #####
101
  ==================================================================
102
  ==================================================================
102
  [..] After reset, the backup domain (RTC registers, RTC backup data
103
  [..] After reset, the backup domain (RTC registers, RTC backup data
103
       registers and backup SRAM) is protected against possible unwanted write
104
       registers and backup SRAM) is protected against possible unwanted write
104
       accesses.
105
       accesses.
105
  [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
106
  [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
106
    (+) Call the function HAL_RCCEx_PeriphCLKConfig in using RCC_PERIPHCLK_RTC for
107
    (+) Call the function HAL_RCCEx_PeriphCLKConfig in using RCC_PERIPHCLK_RTC for
107
        PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSE)
108
        PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSE)
108
    (+) Enable the BKP clock in using __HAL_RCC_BKP_CLK_ENABLE()
109
    (+) Enable the BKP clock in using __HAL_RCC_BKP_CLK_ENABLE()
109
 
110
 
110
                  ##### RTC and low power modes #####
111
                  ##### RTC and low power modes #####
111
  ==================================================================
112
  ==================================================================
112
  [..] The MCU can be woken up from a low power mode by an RTC alternate
113
  [..] The MCU can be woken up from a low power mode by an RTC alternate
113
       function.
114
       function.
114
  [..] The RTC alternate functions are the RTC alarms (Alarm A),
115
  [..] The RTC alternate functions are the RTC alarms (Alarm A),
115
       and RTC tamper event detection.
116
       and RTC tamper event detection.
116
       These RTC alternate functions can wake up the system from the Stop and
117
       These RTC alternate functions can wake up the system from the Stop and
117
       Standby low power modes.
118
       Standby low power modes.
118
  [..] The system can also wake up from low power modes without depending
119
  [..] The system can also wake up from low power modes without depending
119
       on an external interrupt (Auto-wakeup mode), by using the RTC alarm.
120
       on an external interrupt (Auto-wakeup mode), by using the RTC alarm.
-
 
121
 
-
 
122
  *** Callback registration ***
-
 
123
  =============================================
-
 
124
  [..]
-
 
125
  The compilation define  USE_HAL_RTC_REGISTER_CALLBACKS when set to 1
-
 
126
  allows the user to configure dynamically the driver callbacks.
-
 
127
  Use Function @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
-
 
128
 
-
 
129
  [..]
-
 
130
  Function @ref HAL_RTC_RegisterCallback() allows to register following callbacks:
-
 
131
    (+) AlarmAEventCallback          : RTC Alarm A Event callback.
-
 
132
    (+) Tamper1EventCallback         : RTC Tamper 1 Event callback.
-
 
133
    (+) MspInitCallback              : RTC MspInit callback.
-
 
134
    (+) MspDeInitCallback            : RTC MspDeInit callback.
-
 
135
  [..]   
-
 
136
  This function takes as parameters the HAL peripheral handle, the Callback ID
-
 
137
  and a pointer to the user callback function.
-
 
138
 
-
 
139
  [..]
-
 
140
  Use function @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
-
 
141
  weak function.
-
 
142
  @ref HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
-
 
143
  and the Callback ID.
-
 
144
  This function allows to reset following callbacks:
-
 
145
    (+) AlarmAEventCallback          : RTC Alarm A Event callback.
-
 
146
    (+) Tamper1EventCallback         : RTC Tamper 1 Event callback.
-
 
147
    (+) MspInitCallback              : RTC MspInit callback.
-
 
148
    (+) MspDeInitCallback            : RTC MspDeInit callback.
-
 
149
  [..]
-
 
150
  By default, after the @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
-
 
151
  all callbacks are set to the corresponding weak functions :
-
 
152
  example @ref AlarmAEventCallback().
-
 
153
  Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
-
 
154
  in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
-
 
155
  (not registered beforehand).
-
 
156
  If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit()
-
 
157
  keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
-
 
158
  [..]
-
 
159
  Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
-
 
160
  Exception done MspInit/MspDeInit that can be registered/unregistered
-
 
161
  in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
-
 
162
  thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
-
 
163
  In that case first register the MspInit/MspDeInit user callbacks
-
 
164
  using @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
-
 
165
  or @ref HAL_RTC_Init() function.
120
     
166
  [..]
-
 
167
  When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
-
 
168
  not defined, the callback registration feature is not available and all callbacks
-
 
169
  are set to the corresponding weak functions.
121
   @endverbatim
170
   @endverbatim
122
  ******************************************************************************
171
  ******************************************************************************
123
  * @attention
172
  * @attention
124
  *
173
  *
125
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
174
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
-
 
175
  * All rights reserved.</center></h2>
126
  *
176
  *
127
  * Redistribution and use in source and binary forms, with or without modification,
177
  * This software component is licensed by ST under BSD 3-Clause license,
128
  * are permitted provided that the following conditions are met:
178
  * the "License"; You may not use this file except in compliance with the
129
  *   1. Redistributions of source code must retain the above copyright notice,
-
 
130
  *      this list of conditions and the following disclaimer.
-
 
131
  *   2. Redistributions in binary form must reproduce the above copyright notice,
-
 
132
  *      this list of conditions and the following disclaimer in the documentation
-
 
133
  *      and/or other materials provided with the distribution.
179
  * License. You may obtain a copy of the License at:
134
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
-
 
135
  *      may be used to endorse or promote products derived from this software
180
  *                        opensource.org/licenses/BSD-3-Clause
136
  *      without specific prior written permission.
-
 
137
  *
-
 
138
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-
 
139
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-
 
140
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-
 
141
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
-
 
142
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-
 
143
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-
 
144
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-
 
145
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-
 
146
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-
 
147
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
 
148
  *
181
  *
149
  ******************************************************************************
182
  ******************************************************************************
150
  */
183
  */
151
 
184
 
152
/* Includes ------------------------------------------------------------------*/
185
/* Includes ------------------------------------------------------------------*/
153
#include "stm32f1xx_hal.h"
186
#include "stm32f1xx_hal.h"
154
 
187
 
155
/** @addtogroup STM32F1xx_HAL_Driver
188
/** @addtogroup STM32F1xx_HAL_Driver
Line 186... Line 219...
186
/* Private variables ---------------------------------------------------------*/
219
/* Private variables ---------------------------------------------------------*/
187
/* Private function prototypes -----------------------------------------------*/
220
/* Private function prototypes -----------------------------------------------*/
188
/** @defgroup RTC_Private_Functions RTC Private Functions
221
/** @defgroup RTC_Private_Functions RTC Private Functions
189
  * @{
222
  * @{
190
  */
223
  */
191
static uint32_t           RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc);
224
static uint32_t           RTC_ReadTimeCounter(RTC_HandleTypeDef *hrtc);
192
static HAL_StatusTypeDef  RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter);
225
static HAL_StatusTypeDef  RTC_WriteTimeCounter(RTC_HandleTypeDef *hrtc, uint32_t TimeCounter);
193
static uint32_t           RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc);
226
static uint32_t           RTC_ReadAlarmCounter(RTC_HandleTypeDef *hrtc);
194
static HAL_StatusTypeDef  RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter);
227
static HAL_StatusTypeDef  RTC_WriteAlarmCounter(RTC_HandleTypeDef *hrtc, uint32_t AlarmCounter);
195
static HAL_StatusTypeDef  RTC_EnterInitMode(RTC_HandleTypeDef* hrtc);
228
static HAL_StatusTypeDef  RTC_EnterInitMode(RTC_HandleTypeDef *hrtc);
196
static HAL_StatusTypeDef  RTC_ExitInitMode(RTC_HandleTypeDef* hrtc);
229
static HAL_StatusTypeDef  RTC_ExitInitMode(RTC_HandleTypeDef *hrtc);
197
static uint8_t            RTC_ByteToBcd2(uint8_t Value);
230
static uint8_t            RTC_ByteToBcd2(uint8_t Value);
198
static uint8_t            RTC_Bcd2ToByte(uint8_t Value);
231
static uint8_t            RTC_Bcd2ToByte(uint8_t Value);
199
static uint8_t            RTC_IsLeapYear(uint16_t nYear);
232
static uint8_t            RTC_IsLeapYear(uint16_t nYear);
200
static void               RTC_DateUpdate(RTC_HandleTypeDef* hrtc, uint32_t DayElapsed);
233
static void               RTC_DateUpdate(RTC_HandleTypeDef *hrtc, uint32_t DayElapsed);
201
static uint8_t            RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay);
234
static uint8_t            RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay);
202
 
235
 
203
/**
236
/**
204
  * @}
237
  * @}
205
  */
238
  */
206
 
239
 
207
/* Private functions ---------------------------------------------------------*/
240
/* Private functions ---------------------------------------------------------*/
208
/** @defgroup RTC_Exported_Functions RTC Exported Functions
241
/** @defgroup RTC_Exported_Functions RTC Exported Functions
209
  * @{
242
  * @{
210
  */
243
  */
211
 
244
 
212
/** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
245
/** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
213
 *  @brief    Initialization and Configuration functions
246
 *  @brief    Initialization and Configuration functions
214
 *
247
 *
215
@verbatim    
248
@verbatim
216
 ===============================================================================
249
 ===============================================================================
217
              ##### Initialization and de-initialization functions #####
250
              ##### Initialization and de-initialization functions #####
218
 ===============================================================================
251
 ===============================================================================
219
   [..] This section provides functions allowing to initialize and configure the
252
   [..] This section provides functions allowing to initialize and configure the
220
         RTC Prescaler (Asynchronous), disable RTC registers Write protection,
253
         RTC Prescaler (Asynchronous), disable RTC registers Write protection,
221
         enter and exit the RTC initialization mode,
254
         enter and exit the RTC initialization mode,
222
         RTC registers synchronization check and reference clock detection enable.
255
         RTC registers synchronization check and reference clock detection enable.
223
         (#) The RTC Prescaler should be programmed to generate the RTC 1Hz time base.
256
         (#) The RTC Prescaler should be programmed to generate the RTC 1Hz time base.
224
         (#) All RTC registers are Write protected. Writing to the RTC registers
257
         (#) All RTC registers are Write protected. Writing to the RTC registers
225
             is enabled by setting the CNF bit in the RTC_CRL register.
258
             is enabled by setting the CNF bit in the RTC_CRL register.
226
         (#) To read the calendar after wakeup from low power modes (Standby or Stop)
259
         (#) To read the calendar after wakeup from low power modes (Standby or Stop)
227
             the software must first wait for the RSF bit (Register Synchronized Flag)
260
             the software must first wait for the RSF bit (Register Synchronized Flag)
228
             in the RTC_CRL register to be set by hardware.
261
             in the RTC_CRL register to be set by hardware.
229
             The HAL_RTC_WaitForSynchro() function implements the above software
262
             The HAL_RTC_WaitForSynchro() function implements the above software
230
             sequence (RSF clear and RSF check).
263
             sequence (RSF clear and RSF check).
231
 
264
 
232
@endverbatim
265
@endverbatim
233
  * @{
266
  * @{
234
  */
267
  */
235
 
268
 
236
/**
269
/**
237
  * @brief  Initializes the RTC peripheral
270
  * @brief  Initializes the RTC peripheral
238
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
271
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
239
  *                the configuration information for RTC.
272
  *                the configuration information for RTC.
240
  * @retval HAL status
273
  * @retval HAL status
241
  */
274
  */
242
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
275
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
243
{
276
{
244
  uint32_t prescaler = 0U;
277
  uint32_t prescaler = 0U;
245
  /* Check input parameters */
278
  /* Check input parameters */
246
  if(hrtc == NULL)
279
  if (hrtc == NULL)
247
  {
280
  {
248
     return HAL_ERROR;
281
    return HAL_ERROR;
249
  }
282
  }
250
 
283
 
251
  /* Check the parameters */
284
  /* Check the parameters */
252
  assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
285
  assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
253
  assert_param(IS_RTC_CALIB_OUTPUT(hrtc->Init.OutPut));
286
  assert_param(IS_RTC_CALIB_OUTPUT(hrtc->Init.OutPut));
254
  assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
287
  assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
255
   
288
 
-
 
289
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
256
  if(hrtc->State == HAL_RTC_STATE_RESET)
290
  if (hrtc->State == HAL_RTC_STATE_RESET)
257
  {
291
  {
258
    /* Allocate lock resource and initialize it */
292
    /* Allocate lock resource and initialize it */
259
    hrtc->Lock = HAL_UNLOCKED;
293
    hrtc->Lock = HAL_UNLOCKED;
-
 
294
 
-
 
295
    hrtc->AlarmAEventCallback          =  HAL_RTC_AlarmAEventCallback;        /* Legacy weak AlarmAEventCallback      */
-
 
296
    hrtc->Tamper1EventCallback         =  HAL_RTCEx_Tamper1EventCallback;     /* Legacy weak Tamper1EventCallback     */
-
 
297
 
-
 
298
    if (hrtc->MspInitCallback == NULL)
-
 
299
    {
-
 
300
      hrtc->MspInitCallback = HAL_RTC_MspInit;
-
 
301
    }
-
 
302
    /* Init the low level hardware */
-
 
303
    hrtc->MspInitCallback(hrtc);
-
 
304
 
-
 
305
    if (hrtc->MspDeInitCallback == NULL)
260
   
306
    {
-
 
307
      hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
-
 
308
    }
-
 
309
  }
-
 
310
#else
-
 
311
  if (hrtc->State == HAL_RTC_STATE_RESET)
-
 
312
  {
-
 
313
    /* Allocate lock resource and initialize it */
-
 
314
    hrtc->Lock = HAL_UNLOCKED;
-
 
315
 
261
    /* Initialize RTC MSP */
316
    /* Initialize RTC MSP */
262
    HAL_RTC_MspInit(hrtc);
317
    HAL_RTC_MspInit(hrtc);
263
  }
318
  }
-
 
319
#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
264
 
320
 
265
  /* Set RTC state */  
321
  /* Set RTC state */
266
  hrtc->State = HAL_RTC_STATE_BUSY;  
322
  hrtc->State = HAL_RTC_STATE_BUSY;
267
       
323
 
268
  /* Waiting for synchro */
324
  /* Waiting for synchro */
269
  if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
325
  if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
270
  {
326
  {
271
    /* Set RTC state */
327
    /* Set RTC state */
272
    hrtc->State = HAL_RTC_STATE_ERROR;
328
    hrtc->State = HAL_RTC_STATE_ERROR;
273
   
329
 
274
    return HAL_ERROR;
330
    return HAL_ERROR;
275
  }
331
  }
276
 
332
 
277
  /* Set Initialization mode */
333
  /* Set Initialization mode */
278
  if(RTC_EnterInitMode(hrtc) != HAL_OK)
334
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
279
  {
335
  {
280
    /* Set RTC state */
336
    /* Set RTC state */
281
    hrtc->State = HAL_RTC_STATE_ERROR;
337
    hrtc->State = HAL_RTC_STATE_ERROR;
282
   
338
 
283
    return HAL_ERROR;
339
    return HAL_ERROR;
284
  }
340
  }
285
  else
341
  else
286
  {
342
  {
287
    /* Clear Flags Bits */
343
    /* Clear Flags Bits */
288
    CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_OW | RTC_FLAG_ALRAF | RTC_FLAG_SEC));
344
    CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_OW | RTC_FLAG_ALRAF | RTC_FLAG_SEC));
289
   
345
 
290
    if(hrtc->Init.OutPut != RTC_OUTPUTSOURCE_NONE)
346
    if (hrtc->Init.OutPut != RTC_OUTPUTSOURCE_NONE)
291
    {
347
    {
292
      /* Disable the selected Tamper pin */
348
      /* Disable the selected Tamper pin */
293
      CLEAR_BIT(BKP->CR, BKP_CR_TPE);
349
      CLEAR_BIT(BKP->CR, BKP_CR_TPE);
294
    }
350
    }
295
   
351
 
296
    /* Set the signal which will be routed to RTC Tamper pin*/
352
    /* Set the signal which will be routed to RTC Tamper pin*/
297
    MODIFY_REG(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE | BKP_RTCCR_ASOS), hrtc->Init.OutPut);
353
    MODIFY_REG(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE | BKP_RTCCR_ASOS), hrtc->Init.OutPut);
298
 
354
 
299
    if (hrtc->Init.AsynchPrediv != RTC_AUTO_1_SECOND)
355
    if (hrtc->Init.AsynchPrediv != RTC_AUTO_1_SECOND)
300
    {
356
    {
Line 318... Line 374...
318
      {
374
      {
319
        /* RTC period = RTCCLK/(RTC_PR + 1) */
375
        /* RTC period = RTCCLK/(RTC_PR + 1) */
320
        prescaler = prescaler - 1U;
376
        prescaler = prescaler - 1U;
321
      }
377
      }
322
    }
378
    }
323
   
379
 
324
    /* Configure the RTC_PRLH / RTC_PRLL */
380
    /* Configure the RTC_PRLH / RTC_PRLL */
325
    MODIFY_REG(hrtc->Instance->PRLH, RTC_PRLH_PRL, (prescaler >> 16U));
381
    MODIFY_REG(hrtc->Instance->PRLH, RTC_PRLH_PRL, (prescaler >> 16U));
326
    MODIFY_REG(hrtc->Instance->PRLL, RTC_PRLL_PRL, (prescaler & RTC_PRLL_PRL));
382
    MODIFY_REG(hrtc->Instance->PRLL, RTC_PRLL_PRL, (prescaler & RTC_PRLL_PRL));
327
     
383
 
328
    /* Wait for synchro */
384
    /* Wait for synchro */
329
    if(RTC_ExitInitMode(hrtc) != HAL_OK)
385
    if (RTC_ExitInitMode(hrtc) != HAL_OK)
330
    {      
386
    {
331
      hrtc->State = HAL_RTC_STATE_ERROR;
387
      hrtc->State = HAL_RTC_STATE_ERROR;
332
     
388
 
333
      return HAL_ERROR;
389
      return HAL_ERROR;
334
    }
390
    }
335
   
391
 
336
    /* Initialize date to 1st of January 2000 */
392
    /* Initialize date to 1st of January 2000 */
337
    hrtc->DateToUpdate.Year = 0x00U;
393
    hrtc->DateToUpdate.Year = 0x00U;
338
    hrtc->DateToUpdate.Month = RTC_MONTH_JANUARY;
394
    hrtc->DateToUpdate.Month = RTC_MONTH_JANUARY;
339
    hrtc->DateToUpdate.Date = 0x01U;
395
    hrtc->DateToUpdate.Date = 0x01U;
340
 
396
 
341
    /* Set RTC state */
397
    /* Set RTC state */
342
    hrtc->State = HAL_RTC_STATE_READY;
398
    hrtc->State = HAL_RTC_STATE_READY;
343
   
399
 
344
    return HAL_OK;
400
    return HAL_OK;
345
  }
401
  }
346
}
402
}
347
 
403
 
348
/**
404
/**
349
  * @brief  DeInitializes the RTC peripheral
405
  * @brief  DeInitializes the RTC peripheral
350
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
406
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
351
  *                the configuration information for RTC.
407
  *                the configuration information for RTC.
352
  * @note   This function does not reset the RTC Backup Data registers.  
408
  * @note   This function does not reset the RTC Backup Data registers.
353
  * @retval HAL status
409
  * @retval HAL status
354
  */
410
  */
355
HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
411
HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
356
{
412
{
357
  /* Check input parameters */
413
  /* Check input parameters */
358
  if(hrtc == NULL)
414
  if (hrtc == NULL)
359
  {
415
  {
360
     return HAL_ERROR;
416
    return HAL_ERROR;
361
  }
417
  }
362
 
418
 
363
  /* Check the parameters */
419
  /* Check the parameters */
364
  assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
420
  assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
365
 
421
 
366
  /* Set RTC state */
422
  /* Set RTC state */
367
  hrtc->State = HAL_RTC_STATE_BUSY;
423
  hrtc->State = HAL_RTC_STATE_BUSY;
368
 
424
 
369
  /* Set Initialization mode */
425
  /* Set Initialization mode */
370
  if(RTC_EnterInitMode(hrtc) != HAL_OK)
426
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
371
  {
427
  {
372
    /* Set RTC state */
428
    /* Set RTC state */
373
    hrtc->State = HAL_RTC_STATE_ERROR;
429
    hrtc->State = HAL_RTC_STATE_ERROR;
374
   
430
 
375
    /* Release Lock */
431
    /* Release Lock */
376
    __HAL_UNLOCK(hrtc);
432
    __HAL_UNLOCK(hrtc);
377
 
433
 
378
    return HAL_ERROR;
434
    return HAL_ERROR;
379
  }  
435
  }
380
  else
436
  else
381
  {
437
  {
382
    CLEAR_REG(hrtc->Instance->CNTL);
438
    CLEAR_REG(hrtc->Instance->CNTL);
383
    CLEAR_REG(hrtc->Instance->CNTH);
439
    CLEAR_REG(hrtc->Instance->CNTH);
384
    WRITE_REG(hrtc->Instance->PRLL, 0x00008000U);
440
    WRITE_REG(hrtc->Instance->PRLL, 0x00008000U);
385
    CLEAR_REG(hrtc->Instance->PRLH);
441
    CLEAR_REG(hrtc->Instance->PRLH);
386
 
442
 
387
    /* Reset All CRH/CRL bits */
443
    /* Reset All CRH/CRL bits */
388
    CLEAR_REG(hrtc->Instance->CRH);
444
    CLEAR_REG(hrtc->Instance->CRH);
389
    CLEAR_REG(hrtc->Instance->CRL);
445
    CLEAR_REG(hrtc->Instance->CRL);
390
   
446
 
391
    if(RTC_ExitInitMode(hrtc) != HAL_OK)
447
    if (RTC_ExitInitMode(hrtc) != HAL_OK)
392
    {      
448
    {
393
      hrtc->State = HAL_RTC_STATE_ERROR;
449
      hrtc->State = HAL_RTC_STATE_ERROR;
394
     
450
 
395
      /* Process Unlocked */
451
      /* Process Unlocked */
396
      __HAL_UNLOCK(hrtc);
452
      __HAL_UNLOCK(hrtc);
397
     
453
 
398
      return HAL_ERROR;
454
      return HAL_ERROR;
399
    }
455
    }
400
  }
456
  }
401
 
457
 
402
  /* Wait for synchro*/
458
  /* Wait for synchro*/
403
  HAL_RTC_WaitForSynchro(hrtc);
459
  HAL_RTC_WaitForSynchro(hrtc);
404
 
460
 
405
  /* Clear RSF flag */
461
  /* Clear RSF flag */
406
  CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
462
  CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
-
 
463
 
-
 
464
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
-
 
465
  if (hrtc->MspDeInitCallback == NULL)
407
   
466
  {
-
 
467
    hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
-
 
468
  }
-
 
469
 
-
 
470
  /* DeInit the low level hardware: CLOCK, NVIC.*/
-
 
471
  hrtc->MspDeInitCallback(hrtc);
-
 
472
 
-
 
473
#else
408
  /* De-Initialize RTC MSP */
474
  /* De-Initialize RTC MSP */
409
  HAL_RTC_MspDeInit(hrtc);
475
  HAL_RTC_MspDeInit(hrtc);
-
 
476
#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
-
 
477
 
-
 
478
  hrtc->State = HAL_RTC_STATE_RESET;
410
 
479
 
411
  hrtc->State = HAL_RTC_STATE_RESET;
-
 
412
 
-
 
413
  /* Release Lock */
480
  /* Release Lock */
414
  __HAL_UNLOCK(hrtc);
481
  __HAL_UNLOCK(hrtc);
415
 
482
 
416
  return HAL_OK;
483
  return HAL_OK;
417
}
484
}
418
 
485
 
-
 
486
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
-
 
487
/**
-
 
488
  * @brief  Register a User RTC Callback
-
 
489
  *         To be used instead of the weak predefined callback
-
 
490
  * @param  hrtc RTC handle
-
 
491
  * @param  CallbackID ID of the callback to be registered
-
 
492
  *         This parameter can be one of the following values:
-
 
493
  *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID          Alarm A Event Callback ID
-
 
494
  *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID          Tamper 1 Callback ID
-
 
495
  *          @arg @ref HAL_RTC_MSPINIT_CB_ID                Msp Init callback ID
-
 
496
  *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID              Msp DeInit callback ID
-
 
497
  * @param  pCallback pointer to the Callback function
-
 
498
  * @retval HAL status
-
 
499
  */
-
 
500
HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
-
 
501
{
-
 
502
  HAL_StatusTypeDef status = HAL_OK;
-
 
503
 
-
 
504
  if (pCallback == NULL)
-
 
505
  {
-
 
506
    return HAL_ERROR;
-
 
507
  }
-
 
508
 
-
 
509
  /* Process locked */
-
 
510
  __HAL_LOCK(hrtc);
-
 
511
 
-
 
512
  if (HAL_RTC_STATE_READY == hrtc->State)
-
 
513
  {
-
 
514
    switch (CallbackID)
-
 
515
    {
-
 
516
      case HAL_RTC_ALARM_A_EVENT_CB_ID :
-
 
517
        hrtc->AlarmAEventCallback = pCallback;
-
 
518
        break;
-
 
519
 
-
 
520
      case HAL_RTC_TAMPER1_EVENT_CB_ID :
-
 
521
        hrtc->Tamper1EventCallback = pCallback;
-
 
522
        break;
-
 
523
 
-
 
524
      case HAL_RTC_MSPINIT_CB_ID :
-
 
525
        hrtc->MspInitCallback = pCallback;
-
 
526
        break;
-
 
527
 
-
 
528
      case HAL_RTC_MSPDEINIT_CB_ID :
-
 
529
        hrtc->MspDeInitCallback = pCallback;
-
 
530
        break;
-
 
531
 
-
 
532
      default :
-
 
533
        /* Return error status */
-
 
534
        status =  HAL_ERROR;
-
 
535
        break;
-
 
536
    }
-
 
537
  }
-
 
538
  else if (HAL_RTC_STATE_RESET == hrtc->State)
-
 
539
  {
-
 
540
    switch (CallbackID)
-
 
541
    {
-
 
542
      case HAL_RTC_MSPINIT_CB_ID :
-
 
543
        hrtc->MspInitCallback = pCallback;
-
 
544
        break;
-
 
545
 
-
 
546
      case HAL_RTC_MSPDEINIT_CB_ID :
-
 
547
        hrtc->MspDeInitCallback = pCallback;
-
 
548
        break;
-
 
549
 
-
 
550
      default :
-
 
551
        /* Return error status */
-
 
552
        status =  HAL_ERROR;
-
 
553
        break;
-
 
554
    }
-
 
555
  }
-
 
556
  else
-
 
557
  {
-
 
558
    /* Return error status */
-
 
559
    status =  HAL_ERROR;
-
 
560
  }
-
 
561
 
-
 
562
  /* Release Lock */
-
 
563
  __HAL_UNLOCK(hrtc);
-
 
564
 
-
 
565
  return status;
-
 
566
}
-
 
567
 
-
 
568
/**
-
 
569
  * @brief  Unregister an RTC Callback
-
 
570
  *         RTC callabck is redirected to the weak predefined callback
-
 
571
  * @param  hrtc RTC handle
-
 
572
  * @param  CallbackID ID of the callback to be unregistered
-
 
573
  *         This parameter can be one of the following values:
-
 
574
  *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID          Alarm A Event Callback ID
-
 
575
  *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID          Tamper 1 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_TAMPER1_EVENT_CB_ID :
-
 
596
        hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback;         /* Legacy weak Tamper1EventCallback   */
-
 
597
        break;
-
 
598
 
-
 
599
      case HAL_RTC_MSPINIT_CB_ID :
-
 
600
        hrtc->MspInitCallback = HAL_RTC_MspInit;
-
 
601
        break;
-
 
602
 
-
 
603
      case HAL_RTC_MSPDEINIT_CB_ID :
-
 
604
        hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
-
 
605
        break;
-
 
606
 
-
 
607
      default :
-
 
608
        /* Return error status */
-
 
609
        status =  HAL_ERROR;
-
 
610
        break;
-
 
611
    }
-
 
612
  }
-
 
613
  else if (HAL_RTC_STATE_RESET == hrtc->State)
-
 
614
  {
-
 
615
    switch (CallbackID)
-
 
616
    {
-
 
617
      case HAL_RTC_MSPINIT_CB_ID :
-
 
618
        hrtc->MspInitCallback = HAL_RTC_MspInit;
-
 
619
        break;
-
 
620
 
-
 
621
      case HAL_RTC_MSPDEINIT_CB_ID :
-
 
622
        hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
-
 
623
        break;
-
 
624
 
-
 
625
      default :
-
 
626
        /* Return error status */
-
 
627
        status =  HAL_ERROR;
-
 
628
        break;
-
 
629
    }
-
 
630
  }
-
 
631
  else
-
 
632
  {
-
 
633
    /* Return error status */
-
 
634
    status =  HAL_ERROR;
-
 
635
  }
-
 
636
 
-
 
637
  /* Release Lock */
-
 
638
  __HAL_UNLOCK(hrtc);
-
 
639
 
-
 
640
  return status;
-
 
641
}
-
 
642
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
-
 
643
 
419
/**
644
/**
420
  * @brief  Initializes the RTC MSP.
645
  * @brief  Initializes the RTC MSP.
421
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
646
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
422
  *                the configuration information for RTC.  
647
  *                the configuration information for RTC.
423
  * @retval None
648
  * @retval None
424
  */
649
  */
425
__weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
650
__weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
426
{
651
{
427
  /* Prevent unused argument(s) compilation warning */
652
  /* Prevent unused argument(s) compilation warning */
428
  UNUSED(hrtc);
653
  UNUSED(hrtc);
429
  /* NOTE : This function Should not be modified, when the callback is needed,
654
  /* NOTE : This function Should not be modified, when the callback is needed,
430
            the HAL_RTC_MspInit could be implemented in the user file
655
            the HAL_RTC_MspInit could be implemented in the user file
431
   */
656
   */
432
}
657
}
433
 
658
 
434
/**
659
/**
435
  * @brief  DeInitializes the RTC MSP.
660
  * @brief  DeInitializes the RTC MSP.
436
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
661
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
437
  *                the configuration information for RTC.
662
  *                the configuration information for RTC.
438
  * @retval None
663
  * @retval None
439
  */
664
  */
440
__weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
665
__weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
441
{
666
{
442
  /* Prevent unused argument(s) compilation warning */
667
  /* Prevent unused argument(s) compilation warning */
443
  UNUSED(hrtc);
668
  UNUSED(hrtc);
444
  /* NOTE : This function Should not be modified, when the callback is needed,
669
  /* NOTE : This function Should not be modified, when the callback is needed,
445
            the HAL_RTC_MspDeInit could be implemented in the user file
670
            the HAL_RTC_MspDeInit could be implemented in the user file
446
   */
671
   */
447
}
672
}
448
 
673
 
449
/**
674
/**
450
  * @}
675
  * @}
451
  */
676
  */
452
 
677
 
453
/** @defgroup RTC_Exported_Functions_Group2 Time and Date functions
678
/** @defgroup RTC_Exported_Functions_Group2 Time and Date functions
454
 *  @brief   RTC Time and Date functions
679
 *  @brief   RTC Time and Date functions
455
 *
680
 *
456
@verbatim  
681
@verbatim
457
 ===============================================================================
682
 ===============================================================================
458
                 ##### RTC Time and Date functions #####
683
                 ##### RTC Time and Date functions #####
459
 ===============================================================================  
684
 ===============================================================================
460
 
685
 
461
 [..] This section provides functions allowing to configure Time and Date features
686
 [..] This section provides functions allowing to configure Time and Date features
462
 
687
 
463
@endverbatim
688
@endverbatim
464
  * @{
689
  * @{
465
  */
690
  */
Line 469... Line 694...
469
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
694
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
470
  *                the configuration information for RTC.
695
  *                the configuration information for RTC.
471
  * @param  sTime: Pointer to Time structure
696
  * @param  sTime: Pointer to Time structure
472
  * @param  Format: Specifies the format of the entered parameters.
697
  * @param  Format: Specifies the format of the entered parameters.
473
  *          This parameter can be one of the following values:
698
  *          This parameter can be one of the following values:
474
  *            @arg RTC_FORMAT_BIN: Binary data format
699
  *            @arg RTC_FORMAT_BIN: Binary data format
475
  *            @arg RTC_FORMAT_BCD: BCD data format
700
  *            @arg RTC_FORMAT_BCD: BCD data format
476
  * @retval HAL status
701
  * @retval HAL status
477
  */
702
  */
478
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
703
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
479
{
704
{
480
  uint32_t counter_time = 0U, counter_alarm = 0U;
705
  uint32_t counter_time = 0U, counter_alarm = 0U;
481
 
706
 
482
  /* Check input parameters */
707
  /* Check input parameters */
483
  if((hrtc == NULL) || (sTime == NULL))
708
  if ((hrtc == NULL) || (sTime == NULL))
484
  {
709
  {
485
     return HAL_ERROR;
710
    return HAL_ERROR;
486
  }
711
  }
487
 
712
 
488
 /* Check the parameters */
713
  /* Check the parameters */
489
  assert_param(IS_RTC_FORMAT(Format));
714
  assert_param(IS_RTC_FORMAT(Format));
490
 
715
 
491
  /* Process Locked */
716
  /* Process Locked */
492
  __HAL_LOCK(hrtc);
717
  __HAL_LOCK(hrtc);
493
 
718
 
494
  hrtc->State = HAL_RTC_STATE_BUSY;
719
  hrtc->State = HAL_RTC_STATE_BUSY;
495
 
720
 
496
  if(Format == RTC_FORMAT_BIN)
721
  if (Format == RTC_FORMAT_BIN)
497
  {
722
  {
498
    assert_param(IS_RTC_HOUR24(sTime->Hours));
723
    assert_param(IS_RTC_HOUR24(sTime->Hours));
499
    assert_param(IS_RTC_MINUTES(sTime->Minutes));
724
    assert_param(IS_RTC_MINUTES(sTime->Minutes));
500
    assert_param(IS_RTC_SECONDS(sTime->Seconds));
725
    assert_param(IS_RTC_SECONDS(sTime->Seconds));
501
 
726
 
502
    counter_time = (uint32_t)(((uint32_t)sTime->Hours * 3600U) + \
727
    counter_time = (uint32_t)(((uint32_t)sTime->Hours * 3600U) + \
503
                        ((uint32_t)sTime->Minutes * 60U) + \
728
                              ((uint32_t)sTime->Minutes * 60U) + \
504
                        ((uint32_t)sTime->Seconds));  
729
                              ((uint32_t)sTime->Seconds));
505
  }
730
  }
506
  else
731
  else
507
  {
732
  {
508
    assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
733
    assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
509
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
734
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
510
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
735
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
511
 
736
 
512
    counter_time = (((uint32_t)(RTC_Bcd2ToByte(sTime->Hours)) * 3600U) + \
737
    counter_time = (((uint32_t)(RTC_Bcd2ToByte(sTime->Hours)) * 3600U) + \
513
              ((uint32_t)(RTC_Bcd2ToByte(sTime->Minutes)) * 60U) + \
738
                    ((uint32_t)(RTC_Bcd2ToByte(sTime->Minutes)) * 60U) + \
514
              ((uint32_t)(RTC_Bcd2ToByte(sTime->Seconds))));  
739
                    ((uint32_t)(RTC_Bcd2ToByte(sTime->Seconds))));
515
  }
740
  }
516
 
741
 
517
  /* Write time counter in RTC registers */
742
  /* Write time counter in RTC registers */
518
  if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
743
  if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
519
  {
744
  {
520
    /* Set RTC state */
745
    /* Set RTC state */
521
    hrtc->State = HAL_RTC_STATE_ERROR;
746
    hrtc->State = HAL_RTC_STATE_ERROR;
522
   
747
 
523
    /* Process Unlocked */
748
    /* Process Unlocked */
524
    __HAL_UNLOCK(hrtc);
749
    __HAL_UNLOCK(hrtc);
525
   
750
 
526
    return HAL_ERROR;
751
    return HAL_ERROR;
527
  }
752
  }
528
  else
753
  else
529
  {
754
  {
530
    /* Clear Second and overflow flags */
755
    /* Clear Second and overflow flags */
531
    CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
756
    CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
532
   
757
 
533
    /* Read current Alarm counter in RTC registers */
758
    /* Read current Alarm counter in RTC registers */
534
    counter_alarm = RTC_ReadAlarmCounter(hrtc);
759
    counter_alarm = RTC_ReadAlarmCounter(hrtc);
535
 
760
 
536
    /* Set again alarm to match with new time if enabled */
761
    /* Set again alarm to match with new time if enabled */
537
    if (counter_alarm != RTC_ALARM_RESETVALUE)
762
    if (counter_alarm != RTC_ALARM_RESETVALUE)
538
    {
763
    {
539
      if(counter_alarm < counter_time)
764
      if (counter_alarm < counter_time)
540
      {
765
      {
541
        /* Add 1 day to alarm counter*/
766
        /* Add 1 day to alarm counter*/
542
        counter_alarm += (uint32_t)(24U * 3600U);
767
        counter_alarm += (uint32_t)(24U * 3600U);
543
       
768
 
544
        /* Write new Alarm counter in RTC registers */
769
        /* Write new Alarm counter in RTC registers */
545
        if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
770
        if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
546
        {
771
        {
547
          /* Set RTC state */
772
          /* Set RTC state */
548
          hrtc->State = HAL_RTC_STATE_ERROR;
773
          hrtc->State = HAL_RTC_STATE_ERROR;
549
         
774
 
550
          /* Process Unlocked */
775
          /* Process Unlocked */
551
          __HAL_UNLOCK(hrtc);
776
          __HAL_UNLOCK(hrtc);
552
         
777
 
553
          return HAL_ERROR;
778
          return HAL_ERROR;
554
        }
779
        }
555
      }
780
      }
556
    }
781
    }
557
   
782
 
558
    hrtc->State = HAL_RTC_STATE_READY;
783
    hrtc->State = HAL_RTC_STATE_READY;
559
 
784
 
560
   __HAL_UNLOCK(hrtc);
785
    __HAL_UNLOCK(hrtc);
561
     
786
 
562
   return HAL_OK;
787
    return HAL_OK;
563
  }
788
  }
564
}
789
}
565
 
790
 
566
/**
791
/**
567
  * @brief  Gets RTC current time.
792
  * @brief  Gets RTC current time.
568
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
793
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
569
  *                the configuration information for RTC.
794
  *                the configuration information for RTC.
570
  * @param  sTime: Pointer to Time structure
795
  * @param  sTime: Pointer to Time structure
571
  * @param  Format: Specifies the format of the entered parameters.
796
  * @param  Format: Specifies the format of the entered parameters.
572
  *          This parameter can be one of the following values:
797
  *          This parameter can be one of the following values:
573
  *            @arg RTC_FORMAT_BIN: Binary data format
798
  *            @arg RTC_FORMAT_BIN: Binary data format
574
  *            @arg RTC_FORMAT_BCD: BCD data format
799
  *            @arg RTC_FORMAT_BCD: BCD data format
575
  * @retval HAL status
800
  * @retval HAL status
576
  */
801
  */
577
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
802
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
578
{
803
{
579
  uint32_t counter_time = 0U, counter_alarm = 0U, days_elapsed = 0U, hours = 0U;
804
  uint32_t counter_time = 0U, counter_alarm = 0U, days_elapsed = 0U, hours = 0U;
580
 
805
 
581
  /* Check input parameters */
806
  /* Check input parameters */
582
  if((hrtc == NULL) || (sTime == NULL))
807
  if ((hrtc == NULL) || (sTime == NULL))
583
  {
808
  {
584
     return HAL_ERROR;
809
    return HAL_ERROR;
585
  }
810
  }
586
 
811
 
587
  /* Check the parameters */
812
  /* Check the parameters */
588
  assert_param(IS_RTC_FORMAT(Format));
813
  assert_param(IS_RTC_FORMAT(Format));
589
 
814
 
590
  /* Check if counter overflow occurred */
815
  /* Check if counter overflow occurred */
591
  if (__HAL_RTC_OVERFLOW_GET_FLAG(hrtc, RTC_FLAG_OW))
816
  if (__HAL_RTC_OVERFLOW_GET_FLAG(hrtc, RTC_FLAG_OW))
592
  {
817
  {
593
      return HAL_ERROR;
818
    return HAL_ERROR;
594
  }
819
  }
595
 
820
 
596
  /* Read the time counter*/
821
  /* Read the time counter*/
597
  counter_time = RTC_ReadTimeCounter(hrtc);
822
  counter_time = RTC_ReadTimeCounter(hrtc);
598
 
823
 
Line 605... Line 830...
605
  {
830
  {
606
    /* Get number of days elapsed from last calculation */
831
    /* Get number of days elapsed from last calculation */
607
    days_elapsed = (hours / 24U);
832
    days_elapsed = (hours / 24U);
608
 
833
 
609
    /* Set Hours in RTC_TimeTypeDef structure*/
834
    /* Set Hours in RTC_TimeTypeDef structure*/
610
    sTime->Hours = (hours % 24U);    
835
    sTime->Hours = (hours % 24U);
611
 
836
 
612
    /* Read Alarm counter in RTC registers */
837
    /* Read Alarm counter in RTC registers */
613
    counter_alarm = RTC_ReadAlarmCounter(hrtc);
838
    counter_alarm = RTC_ReadAlarmCounter(hrtc);
614
 
839
 
615
    /* Calculate remaining time to reach alarm (only if set and not yet expired)*/
840
    /* Calculate remaining time to reach alarm (only if set and not yet expired)*/
616
    if ((counter_alarm != RTC_ALARM_RESETVALUE) && (counter_alarm > counter_time))
841
    if ((counter_alarm != RTC_ALARM_RESETVALUE) && (counter_alarm > counter_time))
617
    {
842
    {
618
      counter_alarm -= counter_time;
843
      counter_alarm -= counter_time;
619
    }
844
    }
620
    else
845
    else
621
    {
846
    {
622
      /* In case of counter_alarm < counter_time */
847
      /* In case of counter_alarm < counter_time */
623
      /* Alarm expiration already occurred but alarm not deactivated */
848
      /* Alarm expiration already occurred but alarm not deactivated */
624
      counter_alarm = RTC_ALARM_RESETVALUE;
849
      counter_alarm = RTC_ALARM_RESETVALUE;
625
    }
850
    }
626
 
851
 
627
    /* Set updated time in decreasing counter by number of days elapsed */
852
    /* Set updated time in decreasing counter by number of days elapsed */
628
    counter_time -= (days_elapsed * 24U * 3600U);
853
    counter_time -= (days_elapsed * 24U * 3600U);
629
   
854
 
630
    /* Write time counter in RTC registers */
855
    /* Write time counter in RTC registers */
631
    if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
856
    if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
632
    {
857
    {
633
      return HAL_ERROR;
858
      return HAL_ERROR;
634
    }
859
    }
635
 
860
 
636
    /* Set updated alarm to be set */
861
    /* Set updated alarm to be set */
637
    if (counter_alarm != RTC_ALARM_RESETVALUE)
862
    if (counter_alarm != RTC_ALARM_RESETVALUE)
638
    {
863
    {
639
      counter_alarm += counter_time;
864
      counter_alarm += counter_time;
640
     
865
 
641
      /* Write time counter in RTC registers */
866
      /* Write time counter in RTC registers */
642
      if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
867
      if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
643
      {
868
      {
644
        return HAL_ERROR;
869
        return HAL_ERROR;
645
      }
870
      }
Line 650... Line 875...
650
      if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
875
      if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
651
      {
876
      {
652
        return HAL_ERROR;
877
        return HAL_ERROR;
653
      }
878
      }
654
    }
879
    }
655
   
880
 
656
    /* Update date */
881
    /* Update date */
657
    RTC_DateUpdate(hrtc, days_elapsed);
882
    RTC_DateUpdate(hrtc, days_elapsed);
658
  }
883
  }
659
  else
884
  else
660
  {
885
  {
661
    sTime->Hours = hours;    
886
    sTime->Hours = hours;
662
  }
887
  }
663
 
888
 
664
  /* Check the input parameters format */
889
  /* Check the input parameters format */
665
  if(Format != RTC_FORMAT_BIN)
890
  if (Format != RTC_FORMAT_BIN)
666
  {
891
  {
667
    /* Convert the time structure parameters to BCD format */
892
    /* Convert the time structure parameters to BCD format */
668
    sTime->Hours    = (uint8_t)RTC_ByteToBcd2(sTime->Hours);
893
    sTime->Hours    = (uint8_t)RTC_ByteToBcd2(sTime->Hours);
669
    sTime->Minutes  = (uint8_t)RTC_ByteToBcd2(sTime->Minutes);
894
    sTime->Minutes  = (uint8_t)RTC_ByteToBcd2(sTime->Minutes);
670
    sTime->Seconds  = (uint8_t)RTC_ByteToBcd2(sTime->Seconds);  
895
    sTime->Seconds  = (uint8_t)RTC_ByteToBcd2(sTime->Seconds);
671
  }
896
  }
672
 
897
 
673
  return HAL_OK;
898
  return HAL_OK;
674
}
899
}
675
 
900
 
676
 
901
 
677
/**
902
/**
Line 679... Line 904...
679
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
904
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
680
  *                the configuration information for RTC.
905
  *                the configuration information for RTC.
681
  * @param  sDate: Pointer to date structure
906
  * @param  sDate: Pointer to date structure
682
  * @param  Format: specifies the format of the entered parameters.
907
  * @param  Format: specifies the format of the entered parameters.
683
  *          This parameter can be one of the following values:
908
  *          This parameter can be one of the following values:
684
  *            @arg RTC_FORMAT_BIN: Binary data format
909
  *            @arg RTC_FORMAT_BIN: Binary data format
685
  *            @arg RTC_FORMAT_BCD: BCD data format
910
  *            @arg RTC_FORMAT_BCD: BCD data format
686
  * @retval HAL status
911
  * @retval HAL status
687
  */
912
  */
688
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
913
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
689
{
914
{
690
  uint32_t counter_time = 0U, counter_alarm = 0U, hours = 0U;
915
  uint32_t counter_time = 0U, counter_alarm = 0U, hours = 0U;
691
 
916
 
692
  /* Check input parameters */
917
  /* Check input parameters */
693
  if((hrtc == NULL) || (sDate == NULL))
918
  if ((hrtc == NULL) || (sDate == NULL))
694
  {
919
  {
695
     return HAL_ERROR;
920
    return HAL_ERROR;
696
  }
921
  }
697
 
922
 
698
  /* Check the parameters */
923
  /* Check the parameters */
699
  assert_param(IS_RTC_FORMAT(Format));
924
  assert_param(IS_RTC_FORMAT(Format));
700
 
925
 
701
 /* Process Locked */
926
  /* Process Locked */
702
 __HAL_LOCK(hrtc);
927
  __HAL_LOCK(hrtc);
703
 
928
 
704
  hrtc->State = HAL_RTC_STATE_BUSY;
929
  hrtc->State = HAL_RTC_STATE_BUSY;
705
 
930
 
706
  if(Format == RTC_FORMAT_BIN)
931
  if (Format == RTC_FORMAT_BIN)
707
  {  
932
  {
708
    assert_param(IS_RTC_YEAR(sDate->Year));
933
    assert_param(IS_RTC_YEAR(sDate->Year));
709
    assert_param(IS_RTC_MONTH(sDate->Month));
934
    assert_param(IS_RTC_MONTH(sDate->Month));
710
    assert_param(IS_RTC_DATE(sDate->Date));
935
    assert_param(IS_RTC_DATE(sDate->Date));
711
 
936
 
712
    /* Change the current date */
937
    /* Change the current date */
713
    hrtc->DateToUpdate.Year  = sDate->Year;
938
    hrtc->DateToUpdate.Year  = sDate->Year;
714
    hrtc->DateToUpdate.Month = sDate->Month;
939
    hrtc->DateToUpdate.Month = sDate->Month;
715
    hrtc->DateToUpdate.Date  = sDate->Date;
940
    hrtc->DateToUpdate.Date  = sDate->Date;
716
  }
941
  }
717
  else
942
  else
718
  {  
943
  {
719
    assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
944
    assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
720
    assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
945
    assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
721
    assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
946
    assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
722
   
947
 
723
    /* Change the current date */
948
    /* Change the current date */
724
    hrtc->DateToUpdate.Year  = RTC_Bcd2ToByte(sDate->Year);
949
    hrtc->DateToUpdate.Year  = RTC_Bcd2ToByte(sDate->Year);
725
    hrtc->DateToUpdate.Month = RTC_Bcd2ToByte(sDate->Month);
950
    hrtc->DateToUpdate.Month = RTC_Bcd2ToByte(sDate->Month);
726
    hrtc->DateToUpdate.Date  = RTC_Bcd2ToByte(sDate->Date);
951
    hrtc->DateToUpdate.Date  = RTC_Bcd2ToByte(sDate->Date);
727
  }
952
  }
Line 743... Line 968...
743
    /* Write time counter in RTC registers */
968
    /* Write time counter in RTC registers */
744
    if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
969
    if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
745
    {
970
    {
746
      /* Set RTC state */
971
      /* Set RTC state */
747
      hrtc->State = HAL_RTC_STATE_ERROR;
972
      hrtc->State = HAL_RTC_STATE_ERROR;
748
     
973
 
749
      /* Process Unlocked */
974
      /* Process Unlocked */
750
      __HAL_UNLOCK(hrtc);
975
      __HAL_UNLOCK(hrtc);
751
     
976
 
752
      return HAL_ERROR;
977
      return HAL_ERROR;
753
    }
978
    }
754
 
979
 
755
    /* Read current Alarm counter in RTC registers */
980
    /* Read current Alarm counter in RTC registers */
756
    counter_alarm = RTC_ReadAlarmCounter(hrtc);
981
    counter_alarm = RTC_ReadAlarmCounter(hrtc);
757
 
982
 
758
    /* Set again alarm to match with new time if enabled */
983
    /* Set again alarm to match with new time if enabled */
759
    if (counter_alarm != RTC_ALARM_RESETVALUE)
984
    if (counter_alarm != RTC_ALARM_RESETVALUE)
760
    {
985
    {
761
      if(counter_alarm < counter_time)
986
      if (counter_alarm < counter_time)
762
      {
987
      {
763
        /* Add 1 day to alarm counter*/
988
        /* Add 1 day to alarm counter*/
764
        counter_alarm += (uint32_t)(24U * 3600U);
989
        counter_alarm += (uint32_t)(24U * 3600U);
765
       
990
 
766
        /* Write new Alarm counter in RTC registers */
991
        /* Write new Alarm counter in RTC registers */
767
        if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
992
        if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
768
        {
993
        {
769
          /* Set RTC state */
994
          /* Set RTC state */
770
          hrtc->State = HAL_RTC_STATE_ERROR;
995
          hrtc->State = HAL_RTC_STATE_ERROR;
771
         
996
 
772
          /* Process Unlocked */
997
          /* Process Unlocked */
773
          __HAL_UNLOCK(hrtc);
998
          __HAL_UNLOCK(hrtc);
774
         
999
 
775
          return HAL_ERROR;
1000
          return HAL_ERROR;
776
        }
1001
        }
777
      }
1002
      }
778
    }
1003
    }
779
   
1004
 
780
 
1005
 
781
  }
1006
  }
782
 
1007
 
783
  hrtc->State = HAL_RTC_STATE_READY ;
1008
  hrtc->State = HAL_RTC_STATE_READY ;
784
 
1009
 
785
  /* Process Unlocked */
1010
  /* Process Unlocked */
786
  __HAL_UNLOCK(hrtc);
1011
  __HAL_UNLOCK(hrtc);
787
 
1012
 
788
  return HAL_OK;    
1013
  return HAL_OK;
789
}
1014
}
790
 
1015
 
791
/**
1016
/**
792
  * @brief  Gets RTC current date.
1017
  * @brief  Gets RTC current date.
793
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1018
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
794
  *                the configuration information for RTC.
1019
  *                the configuration information for RTC.
795
  * @param  sDate: Pointer to Date structure
1020
  * @param  sDate: Pointer to Date structure
796
  * @param  Format: Specifies the format of the entered parameters.
1021
  * @param  Format: Specifies the format of the entered parameters.
797
  *          This parameter can be one of the following values:
1022
  *          This parameter can be one of the following values:
798
  *            @arg RTC_FORMAT_BIN:  Binary data format
1023
  *            @arg RTC_FORMAT_BIN:  Binary data format
799
  *            @arg RTC_FORMAT_BCD:  BCD data format
1024
  *            @arg RTC_FORMAT_BCD:  BCD data format
800
  * @retval HAL status
1025
  * @retval HAL status
801
  */
1026
  */
802
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1027
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
803
{
1028
{
804
  RTC_TimeTypeDef stime = {0U};
1029
  RTC_TimeTypeDef stime = {0U};
805
 
1030
 
806
  /* Check input parameters */
1031
  /* Check input parameters */
807
  if((hrtc == NULL) || (sDate == NULL))
1032
  if ((hrtc == NULL) || (sDate == NULL))
808
  {
1033
  {
809
     return HAL_ERROR;
1034
    return HAL_ERROR;
810
  }
1035
  }
811
 
1036
 
812
  /* Check the parameters */
1037
  /* Check the parameters */
813
  assert_param(IS_RTC_FORMAT(Format));
1038
  assert_param(IS_RTC_FORMAT(Format));
814
 
1039
 
815
  /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
1040
  /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
816
  if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
1041
  if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
817
  {
1042
  {
818
    return HAL_ERROR;
1043
    return HAL_ERROR;
819
  }
1044
  }
Line 823... Line 1048...
823
  sDate->Year     = hrtc->DateToUpdate.Year;
1048
  sDate->Year     = hrtc->DateToUpdate.Year;
824
  sDate->Month    = hrtc->DateToUpdate.Month;
1049
  sDate->Month    = hrtc->DateToUpdate.Month;
825
  sDate->Date     = hrtc->DateToUpdate.Date;
1050
  sDate->Date     = hrtc->DateToUpdate.Date;
826
 
1051
 
827
  /* Check the input parameters format */
1052
  /* Check the input parameters format */
828
  if(Format != RTC_FORMAT_BIN)
1053
  if (Format != RTC_FORMAT_BIN)
829
  {    
1054
  {
830
    /* Convert the date structure parameters to BCD format */
1055
    /* Convert the date structure parameters to BCD format */
831
    sDate->Year   = (uint8_t)RTC_ByteToBcd2(sDate->Year);
1056
    sDate->Year   = (uint8_t)RTC_ByteToBcd2(sDate->Year);
832
    sDate->Month  = (uint8_t)RTC_ByteToBcd2(sDate->Month);
1057
    sDate->Month  = (uint8_t)RTC_ByteToBcd2(sDate->Month);
833
    sDate->Date   = (uint8_t)RTC_ByteToBcd2(sDate->Date);  
1058
    sDate->Date   = (uint8_t)RTC_ByteToBcd2(sDate->Date);
834
  }
1059
  }
835
  return HAL_OK;
1060
  return HAL_OK;
836
}
1061
}
837
 
1062
 
838
/**
1063
/**
Line 840... Line 1065...
840
  */
1065
  */
841
 
1066
 
842
/** @defgroup RTC_Exported_Functions_Group3 Alarm functions
1067
/** @defgroup RTC_Exported_Functions_Group3 Alarm functions
843
 *  @brief   RTC Alarm functions
1068
 *  @brief   RTC Alarm functions
844
 *
1069
 *
845
@verbatim  
1070
@verbatim
846
 ===============================================================================
1071
 ===============================================================================
847
                 ##### RTC Alarm functions #####
1072
                 ##### RTC Alarm functions #####
848
 ===============================================================================  
1073
 ===============================================================================
849
 
1074
 
850
 [..] This section provides functions allowing to configure Alarm feature
1075
 [..] This section provides functions allowing to configure Alarm feature
851
 
1076
 
852
@endverbatim
1077
@endverbatim
853
  * @{
1078
  * @{
854
  */
1079
  */
Line 858... Line 1083...
858
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1083
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
859
  *                the configuration information for RTC.
1084
  *                the configuration information for RTC.
860
  * @param  sAlarm: Pointer to Alarm structure
1085
  * @param  sAlarm: Pointer to Alarm structure
861
  * @param  Format: Specifies the format of the entered parameters.
1086
  * @param  Format: Specifies the format of the entered parameters.
862
  *          This parameter can be one of the following values:
1087
  *          This parameter can be one of the following values:
863
  *             @arg RTC_FORMAT_BIN: Binary data format
1088
  *             @arg RTC_FORMAT_BIN: Binary data format
864
  *             @arg RTC_FORMAT_BCD: BCD data format
1089
  *             @arg RTC_FORMAT_BCD: BCD data format
865
  * @retval HAL status
1090
  * @retval HAL status
866
  */
1091
  */
867
HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1092
HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
868
{
1093
{
869
  uint32_t counter_alarm = 0U, counter_time;
1094
  uint32_t counter_alarm = 0U, counter_time;
870
  RTC_TimeTypeDef stime = {0U};
1095
  RTC_TimeTypeDef stime = {0U};
871
 
1096
 
872
  /* Check input parameters */
1097
  /* Check input parameters */
873
  if((hrtc == NULL) || (sAlarm == NULL))
1098
  if ((hrtc == NULL) || (sAlarm == NULL))
874
  {
1099
  {
875
     return HAL_ERROR;
1100
    return HAL_ERROR;
876
  }
1101
  }
877
 
1102
 
878
  /* Check the parameters */
1103
  /* Check the parameters */
879
  assert_param(IS_RTC_FORMAT(Format));
1104
  assert_param(IS_RTC_FORMAT(Format));
880
  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1105
  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
881
 
1106
 
882
  /* Process Locked */
1107
  /* Process Locked */
883
  __HAL_LOCK(hrtc);
1108
  __HAL_LOCK(hrtc);
884
 
1109
 
885
  hrtc->State = HAL_RTC_STATE_BUSY;
1110
  hrtc->State = HAL_RTC_STATE_BUSY;
886
 
1111
 
887
  /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
1112
  /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
888
  if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
1113
  if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
889
  {
1114
  {
890
    return HAL_ERROR;
1115
    return HAL_ERROR;
891
  }
1116
  }
892
 
1117
 
893
  /* Convert time in seconds */
1118
  /* Convert time in seconds */
894
  counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600U) + \
1119
  counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600U) + \
895
                      ((uint32_t)stime.Minutes * 60U) + \
1120
                            ((uint32_t)stime.Minutes * 60U) + \
896
                      ((uint32_t)stime.Seconds));  
1121
                            ((uint32_t)stime.Seconds));
897
 
1122
 
898
  if(Format == RTC_FORMAT_BIN)
1123
  if (Format == RTC_FORMAT_BIN)
899
  {
1124
  {
900
    assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1125
    assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
901
    assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1126
    assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
902
    assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1127
    assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
903
   
1128
 
904
    counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600U) + \
1129
    counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600U) + \
905
                        ((uint32_t)sAlarm->AlarmTime.Minutes * 60U) + \
1130
                               ((uint32_t)sAlarm->AlarmTime.Minutes * 60U) + \
906
                        ((uint32_t)sAlarm->AlarmTime.Seconds));  
1131
                               ((uint32_t)sAlarm->AlarmTime.Seconds));
907
  }
1132
  }
908
  else
1133
  else
909
  {
1134
  {
910
    assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1135
    assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
911
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1136
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
912
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1137
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
913
   
1138
 
914
    counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600U) + \
1139
    counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600U) + \
915
              ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60U) + \
1140
                     ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60U) + \
916
              ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));  
1141
                     ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
917
  }
1142
  }
918
 
1143
 
919
  /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
1144
  /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
920
  if (counter_alarm < counter_time)
1145
  if (counter_alarm < counter_time)
921
  {
1146
  {
Line 926... Line 1151...
926
  /* Write Alarm counter in RTC registers */
1151
  /* Write Alarm counter in RTC registers */
927
  if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
1152
  if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
928
  {
1153
  {
929
    /* Set RTC state */
1154
    /* Set RTC state */
930
    hrtc->State = HAL_RTC_STATE_ERROR;
1155
    hrtc->State = HAL_RTC_STATE_ERROR;
931
   
1156
 
932
    /* Process Unlocked */
1157
    /* Process Unlocked */
933
    __HAL_UNLOCK(hrtc);
1158
    __HAL_UNLOCK(hrtc);
934
   
1159
 
935
    return HAL_ERROR;
1160
    return HAL_ERROR;
936
  }
1161
  }
937
  else
1162
  else
938
  {
1163
  {
939
    hrtc->State = HAL_RTC_STATE_READY;
1164
    hrtc->State = HAL_RTC_STATE_READY;
940
 
1165
 
941
   __HAL_UNLOCK(hrtc);
1166
    __HAL_UNLOCK(hrtc);
942
     
1167
 
943
   return HAL_OK;
1168
    return HAL_OK;
944
  }
1169
  }
945
}
1170
}
946
 
1171
 
947
/**
1172
/**
948
  * @brief  Sets the specified RTC Alarm with Interrupt
1173
  * @brief  Sets the specified RTC Alarm with Interrupt
949
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1174
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
950
  *                the configuration information for RTC.
1175
  *                the configuration information for RTC.
951
  * @param  sAlarm: Pointer to Alarm structure
1176
  * @param  sAlarm: Pointer to Alarm structure
952
  * @param  Format: Specifies the format of the entered parameters.
1177
  * @param  Format: Specifies the format of the entered parameters.
953
  *          This parameter can be one of the following values:
1178
  *          This parameter can be one of the following values:
954
  *             @arg RTC_FORMAT_BIN: Binary data format
1179
  *             @arg RTC_FORMAT_BIN: Binary data format
955
  *             @arg RTC_FORMAT_BCD: BCD data format
1180
  *             @arg RTC_FORMAT_BCD: BCD data format
956
  * @note   The HAL_RTC_SetTime() must be called before enabling the Alarm feature.  
1181
  * @note   The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
957
  * @retval HAL status
1182
  * @retval HAL status
958
  */
1183
  */
959
HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1184
HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
960
{
1185
{
961
  uint32_t counter_alarm = 0U, counter_time;
1186
  uint32_t counter_alarm = 0U, counter_time;
962
  RTC_TimeTypeDef stime = {0U};
1187
  RTC_TimeTypeDef stime = {0U};
963
 
1188
 
964
  /* Check input parameters */
1189
  /* Check input parameters */
965
  if((hrtc == NULL) || (sAlarm == NULL))
1190
  if ((hrtc == NULL) || (sAlarm == NULL))
966
  {
1191
  {
967
     return HAL_ERROR;
1192
    return HAL_ERROR;
968
  }
1193
  }
969
 
1194
 
970
  /* Check the parameters */
1195
  /* Check the parameters */
971
  assert_param(IS_RTC_FORMAT(Format));
1196
  assert_param(IS_RTC_FORMAT(Format));
972
  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1197
  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
973
 
1198
 
974
  /* Process Locked */
1199
  /* Process Locked */
975
  __HAL_LOCK(hrtc);
1200
  __HAL_LOCK(hrtc);
976
 
1201
 
977
  hrtc->State = HAL_RTC_STATE_BUSY;
1202
  hrtc->State = HAL_RTC_STATE_BUSY;
978
 
1203
 
979
  /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
1204
  /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
980
  if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
1205
  if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
981
  {
1206
  {
982
    return HAL_ERROR;
1207
    return HAL_ERROR;
983
  }
1208
  }
984
 
1209
 
985
  /* Convert time in seconds */
1210
  /* Convert time in seconds */
986
  counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600U) + \
1211
  counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600U) + \
987
                      ((uint32_t)stime.Minutes * 60U) + \
1212
                            ((uint32_t)stime.Minutes * 60U) + \
988
                      ((uint32_t)stime.Seconds));  
1213
                            ((uint32_t)stime.Seconds));
989
 
1214
 
990
  if(Format == RTC_FORMAT_BIN)
1215
  if (Format == RTC_FORMAT_BIN)
991
  {
1216
  {
992
    assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1217
    assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
993
    assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1218
    assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
994
    assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1219
    assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
995
   
1220
 
996
    counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600U) + \
1221
    counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600U) + \
997
      ((uint32_t)sAlarm->AlarmTime.Minutes * 60U) + \
1222
                               ((uint32_t)sAlarm->AlarmTime.Minutes * 60U) + \
998
        ((uint32_t)sAlarm->AlarmTime.Seconds));  
1223
                               ((uint32_t)sAlarm->AlarmTime.Seconds));
999
  }
1224
  }
1000
  else
1225
  else
1001
  {
1226
  {
1002
    assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1227
    assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1003
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1228
    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1004
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1229
    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1005
   
1230
 
1006
    counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600U) + \
1231
    counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600U) + \
1007
                     ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60U) + \
1232
                     ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60U) + \
1008
                     ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));  
1233
                     ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1009
  }
1234
  }
1010
 
1235
 
1011
  /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
1236
  /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
1012
  if (counter_alarm < counter_time)
1237
  if (counter_alarm < counter_time)
1013
  {
1238
  {
1014
    /* Add 1 day to alarm counter*/
1239
    /* Add 1 day to alarm counter*/
1015
    counter_alarm += (uint32_t)(24U * 3600U);
1240
    counter_alarm += (uint32_t)(24U * 3600U);
Line 1018... Line 1243...
1018
  /* Write alarm counter in RTC registers */
1243
  /* Write alarm counter in RTC registers */
1019
  if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
1244
  if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
1020
  {
1245
  {
1021
    /* Set RTC state */
1246
    /* Set RTC state */
1022
    hrtc->State = HAL_RTC_STATE_ERROR;
1247
    hrtc->State = HAL_RTC_STATE_ERROR;
1023
   
1248
 
1024
    /* Process Unlocked */
1249
    /* Process Unlocked */
1025
    __HAL_UNLOCK(hrtc);
1250
    __HAL_UNLOCK(hrtc);
1026
   
1251
 
1027
    return HAL_ERROR;
1252
    return HAL_ERROR;
1028
  }
1253
  }
1029
  else
1254
  else
1030
  {
1255
  {
1031
    /* Clear flag alarm A */
1256
    /* Clear flag alarm A */
1032
    __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1257
    __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1033
   
1258
 
1034
    /* Configure the Alarm interrupt */
1259
    /* Configure the Alarm interrupt */
1035
    __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
1260
    __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
1036
   
1261
 
1037
    /* RTC Alarm Interrupt Configuration: EXTI configuration */
1262
    /* RTC Alarm Interrupt Configuration: EXTI configuration */
1038
    __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1263
    __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1039
   
1264
 
1040
    __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1265
    __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1041
 
1266
 
1042
    hrtc->State = HAL_RTC_STATE_READY;
1267
    hrtc->State = HAL_RTC_STATE_READY;
1043
 
1268
 
1044
   __HAL_UNLOCK(hrtc);
1269
    __HAL_UNLOCK(hrtc);
1045
     
1270
 
1046
   return HAL_OK;
1271
    return HAL_OK;
1047
  }
1272
  }
1048
}
1273
}
1049
 
1274
 
1050
/**
1275
/**
1051
  * @brief  Gets the RTC Alarm value and masks.
1276
  * @brief  Gets the RTC Alarm value and masks.
Line 1055... Line 1280...
1055
  * @param  Alarm: Specifies the Alarm.
1280
  * @param  Alarm: Specifies the Alarm.
1056
  *          This parameter can be one of the following values:
1281
  *          This parameter can be one of the following values:
1057
  *             @arg RTC_ALARM_A: Alarm
1282
  *             @arg RTC_ALARM_A: Alarm
1058
  * @param  Format: Specifies the format of the entered parameters.
1283
  * @param  Format: Specifies the format of the entered parameters.
1059
  *          This parameter can be one of the following values:
1284
  *          This parameter can be one of the following values:
1060
  *             @arg RTC_FORMAT_BIN: Binary data format
1285
  *             @arg RTC_FORMAT_BIN: Binary data format
1061
  *             @arg RTC_FORMAT_BCD: BCD data format
1286
  *             @arg RTC_FORMAT_BCD: BCD data format
1062
  * @retval HAL status
1287
  * @retval HAL status
1063
  */
1288
  */
1064
HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1289
HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1065
{
1290
{
Line 1067... Line 1292...
1067
 
1292
 
1068
  /* Prevent unused argument(s) compilation warning */
1293
  /* Prevent unused argument(s) compilation warning */
1069
  UNUSED(Alarm);
1294
  UNUSED(Alarm);
1070
 
1295
 
1071
  /* Check input parameters */
1296
  /* Check input parameters */
1072
  if((hrtc == NULL) || (sAlarm == NULL))
1297
  if ((hrtc == NULL) || (sAlarm == NULL))
1073
  {
1298
  {
1074
    return HAL_ERROR;
1299
    return HAL_ERROR;
1075
  }
1300
  }
1076
 
1301
 
1077
  /* Check the parameters */
1302
  /* Check the parameters */
1078
  assert_param(IS_RTC_FORMAT(Format));
1303
  assert_param(IS_RTC_FORMAT(Format));
1079
  assert_param(IS_RTC_ALARM(Alarm));
1304
  assert_param(IS_RTC_ALARM(Alarm));
1080
 
1305
 
1081
  /* Read Alarm counter in RTC registers */
1306
  /* Read Alarm counter in RTC registers */
1082
  counter_alarm = RTC_ReadAlarmCounter(hrtc);
1307
  counter_alarm = RTC_ReadAlarmCounter(hrtc);
1083
 
1308
 
1084
  /* Fill the structure with the read parameters */
1309
  /* Fill the structure with the read parameters */
1085
  /* Set hours in a day range (between 0 to 24)*/
1310
  /* Set hours in a day range (between 0 to 24)*/
1086
  sAlarm->AlarmTime.Hours   = (uint32_t)((counter_alarm / 3600U) % 24U);
1311
  sAlarm->AlarmTime.Hours   = (uint32_t)((counter_alarm / 3600U) % 24U);
1087
  sAlarm->AlarmTime.Minutes = (uint32_t)((counter_alarm % 3600U) / 60U);
1312
  sAlarm->AlarmTime.Minutes = (uint32_t)((counter_alarm % 3600U) / 60U);
1088
  sAlarm->AlarmTime.Seconds = (uint32_t)((counter_alarm % 3600U) % 60U);
1313
  sAlarm->AlarmTime.Seconds = (uint32_t)((counter_alarm % 3600U) % 60U);
1089
 
1314
 
1090
  if(Format != RTC_FORMAT_BIN)
1315
  if (Format != RTC_FORMAT_BIN)
1091
  {
1316
  {
1092
    sAlarm->AlarmTime.Hours   = RTC_ByteToBcd2(sAlarm->AlarmTime.Hours);
1317
    sAlarm->AlarmTime.Hours   = RTC_ByteToBcd2(sAlarm->AlarmTime.Hours);
1093
    sAlarm->AlarmTime.Minutes = RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes);
1318
    sAlarm->AlarmTime.Minutes = RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes);
1094
    sAlarm->AlarmTime.Seconds = RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds);
1319
    sAlarm->AlarmTime.Seconds = RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds);
1095
  }  
1320
  }
1096
 
1321
 
1097
  return HAL_OK;
1322
  return HAL_OK;
1098
}
1323
}
1099
 
1324
 
1100
/**
1325
/**
1101
  * @brief  Deactive the specified RTC Alarm
1326
  * @brief  Deactive the specified RTC Alarm
1102
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1327
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1103
  *                the configuration information for RTC.
1328
  *                the configuration information for RTC.
1104
  * @param  Alarm: Specifies the Alarm.
1329
  * @param  Alarm: Specifies the Alarm.
1105
  *          This parameter can be one of the following values:
1330
  *          This parameter can be one of the following values:
1106
  *            @arg RTC_ALARM_A:  AlarmA
1331
  *            @arg RTC_ALARM_A:  AlarmA
Line 1111... Line 1336...
1111
  /* Prevent unused argument(s) compilation warning */
1336
  /* Prevent unused argument(s) compilation warning */
1112
  UNUSED(Alarm);
1337
  UNUSED(Alarm);
1113
 
1338
 
1114
  /* Check the parameters */
1339
  /* Check the parameters */
1115
  assert_param(IS_RTC_ALARM(Alarm));
1340
  assert_param(IS_RTC_ALARM(Alarm));
1116
 
1341
 
1117
  /* Check input parameters */
1342
  /* Check input parameters */
1118
  if(hrtc == NULL)
1343
  if (hrtc == NULL)
1119
  {
1344
  {
1120
     return HAL_ERROR;
1345
    return HAL_ERROR;
1121
  }
1346
  }
1122
 
1347
 
1123
  /* Process Locked */
1348
  /* Process Locked */
1124
  __HAL_LOCK(hrtc);
1349
  __HAL_LOCK(hrtc);
1125
 
1350
 
1126
  hrtc->State = HAL_RTC_STATE_BUSY;
1351
  hrtc->State = HAL_RTC_STATE_BUSY;
1127
 
1352
 
1128
  /* In case of interrupt mode is used, the interrupt source must disabled */
1353
  /* In case of interrupt mode is used, the interrupt source must disabled */
1129
  __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1354
  __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1130
 
1355
 
1131
  /* Set Initialization mode */
1356
  /* Set Initialization mode */
1132
  if(RTC_EnterInitMode(hrtc) != HAL_OK)
1357
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
1133
  {
1358
  {
1134
    /* Set RTC state */
1359
    /* Set RTC state */
1135
    hrtc->State = HAL_RTC_STATE_ERROR;
1360
    hrtc->State = HAL_RTC_STATE_ERROR;
1136
   
1361
 
1137
    /* Process Unlocked */
1362
    /* Process Unlocked */
1138
    __HAL_UNLOCK(hrtc);
1363
    __HAL_UNLOCK(hrtc);
1139
   
1364
 
1140
    return HAL_ERROR;
1365
    return HAL_ERROR;
1141
  }
1366
  }
1142
  else
1367
  else
1143
  {
1368
  {
1144
    /* Clear flag alarm A */
1369
    /* Clear flag alarm A */
1145
    __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1370
    __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1146
   
1371
 
1147
    /* Set to default values ALRH & ALRL registers */
1372
    /* Set to default values ALRH & ALRL registers */
1148
    WRITE_REG(hrtc->Instance->ALRH, RTC_ALARM_RESETVALUE_REGISTER);
1373
    WRITE_REG(hrtc->Instance->ALRH, RTC_ALARM_RESETVALUE_REGISTER);
1149
    WRITE_REG(hrtc->Instance->ALRL, RTC_ALARM_RESETVALUE_REGISTER);
1374
    WRITE_REG(hrtc->Instance->ALRL, RTC_ALARM_RESETVALUE_REGISTER);
1150
 
1375
 
1151
    /* RTC Alarm Interrupt Configuration: Disable EXTI configuration */
1376
    /* RTC Alarm Interrupt Configuration: Disable EXTI configuration */
1152
    __HAL_RTC_ALARM_EXTI_DISABLE_IT();
1377
    __HAL_RTC_ALARM_EXTI_DISABLE_IT();
1153
   
1378
 
1154
    /* Wait for synchro */
1379
    /* Wait for synchro */
1155
    if(RTC_ExitInitMode(hrtc) != HAL_OK)
1380
    if (RTC_ExitInitMode(hrtc) != HAL_OK)
1156
    {      
1381
    {
1157
      hrtc->State = HAL_RTC_STATE_ERROR;
1382
      hrtc->State = HAL_RTC_STATE_ERROR;
1158
     
1383
 
1159
      /* Process Unlocked */
1384
      /* Process Unlocked */
1160
      __HAL_UNLOCK(hrtc);
1385
      __HAL_UNLOCK(hrtc);
1161
     
1386
 
1162
      return HAL_ERROR;
1387
      return HAL_ERROR;
1163
    }
1388
    }
1164
  }
1389
  }
1165
  hrtc->State = HAL_RTC_STATE_READY;
1390
  hrtc->State = HAL_RTC_STATE_READY;
1166
 
1391
 
1167
  /* Process Unlocked */
1392
  /* Process Unlocked */
1168
  __HAL_UNLOCK(hrtc);  
1393
  __HAL_UNLOCK(hrtc);
1169
 
1394
 
1170
  return HAL_OK;
1395
  return HAL_OK;
1171
}
1396
}
1172
 
1397
 
1173
/**
1398
/**
1174
  * @brief  This function handles Alarm interrupt request.
1399
  * @brief  This function handles Alarm interrupt request.
1175
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1400
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1176
  *                the configuration information for RTC.
1401
  *                the configuration information for RTC.
1177
  * @retval None
1402
  * @retval None
1178
  */
1403
  */
1179
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1404
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1180
{  
1405
{
1181
  if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA))
1406
  if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA))
1182
  {
1407
  {
1183
    /* Get the status of the Interrupt */
1408
    /* Get the status of the Interrupt */
1184
    if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != (uint32_t)RESET)
1409
    if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != (uint32_t)RESET)
1185
    {
1410
    {
1186
      /* AlarmA callback */
1411
      /* AlarmA callback */
-
 
1412
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
-
 
1413
      hrtc->AlarmAEventCallback(hrtc);
-
 
1414
#else
1187
      HAL_RTC_AlarmAEventCallback(hrtc);
1415
      HAL_RTC_AlarmAEventCallback(hrtc);
-
 
1416
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1188
     
1417
 
1189
      /* Clear the Alarm interrupt pending bit */
1418
      /* Clear the Alarm interrupt pending bit */
1190
      __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
1419
      __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1191
    }
1420
    }
1192
  }
1421
  }
1193
 
1422
 
1194
  /* Clear the EXTI's line Flag for RTC Alarm */
1423
  /* Clear the EXTI's line Flag for RTC Alarm */
1195
  __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1424
  __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1196
 
1425
 
1197
  /* Change RTC state */
1426
  /* Change RTC state */
1198
  hrtc->State = HAL_RTC_STATE_READY;
1427
  hrtc->State = HAL_RTC_STATE_READY;
1199
}
1428
}
1200
 
1429
 
1201
/**
1430
/**
1202
  * @brief  Alarm A callback.
1431
  * @brief  Alarm A callback.
1203
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1432
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
Line 1219... Line 1448...
1219
  *                the configuration information for RTC.
1448
  *                the configuration information for RTC.
1220
  * @param  Timeout: Timeout duration
1449
  * @param  Timeout: Timeout duration
1221
  * @retval HAL status
1450
  * @retval HAL status
1222
  */
1451
  */
1223
HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1452
HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1224
{  
1453
{
1225
  uint32_t tickstart = HAL_GetTick();  
1454
  uint32_t tickstart = HAL_GetTick();
1226
 
1455
 
1227
  /* Check input parameters */
1456
  /* Check input parameters */
1228
  if(hrtc == NULL)
1457
  if (hrtc == NULL)
1229
  {
1458
  {
1230
     return HAL_ERROR;
1459
    return HAL_ERROR;
1231
  }
1460
  }
1232
 
1461
 
1233
  while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1462
  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1234
  {
1463
  {
1235
    if(Timeout != HAL_MAX_DELAY)
1464
    if (Timeout != HAL_MAX_DELAY)
1236
    {
1465
    {
1237
      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1466
      if ((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
1238
      {
1467
      {
1239
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1468
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1240
        return HAL_TIMEOUT;
1469
        return HAL_TIMEOUT;
1241
      }
1470
      }
1242
    }
1471
    }
1243
  }
1472
  }
1244
 
1473
 
1245
  /* Clear the Alarm interrupt pending bit */
1474
  /* Clear the Alarm interrupt pending bit */
1246
  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1475
  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1247
 
1476
 
1248
  /* Change RTC state */
1477
  /* Change RTC state */
1249
  hrtc->State = HAL_RTC_STATE_READY;
1478
  hrtc->State = HAL_RTC_STATE_READY;
1250
 
1479
 
1251
  return HAL_OK;  
1480
  return HAL_OK;
1252
}
1481
}
1253
 
1482
 
1254
/**
1483
/**
1255
  * @}
1484
  * @}
1256
  */
1485
  */
1257
 
1486
 
1258
/** @defgroup RTC_Exported_Functions_Group4 Peripheral State functions
1487
/** @defgroup RTC_Exported_Functions_Group4 Peripheral State functions
1259
 *  @brief   Peripheral State functions
1488
 *  @brief   Peripheral State functions
1260
 *
1489
 *
1261
@verbatim  
1490
@verbatim
1262
 ===============================================================================
1491
 ===============================================================================
1263
                     ##### Peripheral State functions #####
1492
                     ##### Peripheral State functions #####
1264
 ===============================================================================  
1493
 ===============================================================================
1265
    [..]
1494
    [..]
1266
    This subsection provides functions allowing to
1495
    This subsection provides functions allowing to
1267
      (+) Get RTC state
1496
      (+) Get RTC state
1268
 
1497
 
1269
@endverbatim
1498
@endverbatim
Line 1273... Line 1502...
1273
  * @brief  Returns the RTC state.
1502
  * @brief  Returns the RTC state.
1274
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1503
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1275
  *                the configuration information for RTC.
1504
  *                the configuration information for RTC.
1276
  * @retval HAL state
1505
  * @retval HAL state
1277
  */
1506
  */
1278
HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1507
HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
1279
{
1508
{
1280
  return hrtc->State;
1509
  return hrtc->State;
1281
}
1510
}
1282
 
1511
 
1283
/**
1512
/**
1284
  * @}
1513
  * @}
1285
  */
1514
  */
1286
 
1515
 
1287
/** @defgroup RTC_Exported_Functions_Group5 Peripheral Control functions
1516
/** @defgroup RTC_Exported_Functions_Group5 Peripheral Control functions
1288
 *  @brief   Peripheral Control functions
1517
 *  @brief   Peripheral Control functions
1289
 *
1518
 *
1290
@verbatim  
1519
@verbatim
1291
 ===============================================================================
1520
 ===============================================================================
1292
                     ##### Peripheral Control functions #####
1521
                     ##### Peripheral Control functions #####
1293
 ===============================================================================  
1522
 ===============================================================================
1294
    [..]
1523
    [..]
1295
    This subsection provides functions allowing to
1524
    This subsection provides functions allowing to
1296
      (+) Wait for RTC Time and Date Synchronization
1525
      (+) Wait for RTC Time and Date Synchronization
1297
 
1526
 
1298
@endverbatim
1527
@endverbatim
Line 1306... Line 1535...
1306
  *   or an APB clock stop.
1535
  *   or an APB clock stop.
1307
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1536
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1308
  *                the configuration information for RTC.
1537
  *                the configuration information for RTC.
1309
  * @retval HAL status
1538
  * @retval HAL status
1310
  */
1539
  */
1311
HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1540
HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
1312
{
1541
{
1313
  uint32_t tickstart = 0U;
1542
  uint32_t tickstart = 0U;
1314
 
1543
 
1315
  /* Check input parameters */
1544
  /* Check input parameters */
1316
  if(hrtc == NULL)
1545
  if (hrtc == NULL)
1317
  {
1546
  {
1318
     return HAL_ERROR;
1547
    return HAL_ERROR;
1319
  }
1548
  }
1320
 
1549
 
1321
  /* Clear RSF flag */
1550
  /* Clear RSF flag */
1322
  CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
1551
  CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
1323
 
1552
 
1324
  tickstart = HAL_GetTick();
1553
  tickstart = HAL_GetTick();
1325
 
1554
 
1326
  /* Wait the registers to be synchronised */
1555
  /* Wait the registers to be synchronised */
1327
  while((hrtc->Instance->CRL & RTC_FLAG_RSF) == (uint32_t)RESET)
1556
  while ((hrtc->Instance->CRL & RTC_FLAG_RSF) == (uint32_t)RESET)
1328
  {
1557
  {
1329
    if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
1558
    if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1330
    {      
1559
    {
1331
      return HAL_TIMEOUT;
1560
      return HAL_TIMEOUT;
1332
    }
1561
    }
1333
  }
1562
  }
1334
 
1563
 
1335
  return HAL_OK;
1564
  return HAL_OK;
1336
}
1565
}
1337
 
1566
 
1338
/**
1567
/**
1339
  * @}
1568
  * @}
Line 1345... Line 1574...
1345
  */
1574
  */
1346
 
1575
 
1347
/** @addtogroup RTC_Private_Functions
1576
/** @addtogroup RTC_Private_Functions
1348
  * @{
1577
  * @{
1349
  */
1578
  */
1350
 
1579
 
1351
 
1580
 
1352
/**
1581
/**
1353
  * @brief  Read the time counter available in RTC_CNT registers.
1582
  * @brief  Read the time counter available in RTC_CNT registers.
1354
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1583
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1355
  *                the configuration information for RTC.
1584
  *                the configuration information for RTC.
1356
  * @retval Time counter
1585
  * @retval Time counter
1357
  */
1586
  */
1358
static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
1587
static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef *hrtc)
1359
{
1588
{
1360
  uint16_t high1 = 0U, high2 = 0U, low = 0U;
1589
  uint16_t high1 = 0U, high2 = 0U, low = 0U;
1361
  uint32_t timecounter = 0U;
1590
  uint32_t timecounter = 0U;
1362
 
1591
 
1363
  high1 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
1592
  high1 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
1364
  low   = READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT);
1593
  low   = READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT);
1365
  high2 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
1594
  high2 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
1366
 
1595
 
1367
  if (high1 != high2)
1596
  if (high1 != high2)
-
 
1597
  {
1368
  { /* In this case the counter roll over during reading of CNTL and CNTH registers,
1598
    /* In this case the counter roll over during reading of CNTL and CNTH registers,
1369
       read again CNTL register then return the counter value */
1599
       read again CNTL register then return the counter value */
1370
    timecounter = (((uint32_t) high2 << 16U) | READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT));
1600
    timecounter = (((uint32_t) high2 << 16U) | READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT));
1371
  }
1601
  }
1372
  else
1602
  else
-
 
1603
  {
1373
  { /* No counter roll over during reading of CNTL and CNTH registers, counter
1604
    /* No counter roll over during reading of CNTL and CNTH registers, counter
1374
       value is equal to first value of CNTL and CNTH */
1605
       value is equal to first value of CNTL and CNTH */
1375
    timecounter = (((uint32_t) high1 << 16U) | low);
1606
    timecounter = (((uint32_t) high1 << 16U) | low);
1376
  }
1607
  }
1377
 
1608
 
1378
  return timecounter;
1609
  return timecounter;
Line 1383... Line 1614...
1383
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1614
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1384
  *                the configuration information for RTC.
1615
  *                the configuration information for RTC.
1385
  * @param  TimeCounter: Counter to write in RTC_CNT registers
1616
  * @param  TimeCounter: Counter to write in RTC_CNT registers
1386
  * @retval HAL status
1617
  * @retval HAL status
1387
  */
1618
  */
1388
static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter)
1619
static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef *hrtc, uint32_t TimeCounter)
1389
{
1620
{
1390
  HAL_StatusTypeDef status = HAL_OK;
1621
  HAL_StatusTypeDef status = HAL_OK;
1391
 
1622
 
1392
  /* Set Initialization mode */
1623
  /* Set Initialization mode */
1393
  if(RTC_EnterInitMode(hrtc) != HAL_OK)
1624
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
1394
  {
1625
  {
1395
    status = HAL_ERROR;
1626
    status = HAL_ERROR;
1396
  }
1627
  }
1397
  else
1628
  else
1398
  {
1629
  {
1399
    /* Set RTC COUNTER MSB word */
1630
    /* Set RTC COUNTER MSB word */
1400
    WRITE_REG(hrtc->Instance->CNTH, (TimeCounter >> 16U));
1631
    WRITE_REG(hrtc->Instance->CNTH, (TimeCounter >> 16U));
1401
    /* Set RTC COUNTER LSB word */
1632
    /* Set RTC COUNTER LSB word */
1402
    WRITE_REG(hrtc->Instance->CNTL, (TimeCounter & RTC_CNTL_RTC_CNT));
1633
    WRITE_REG(hrtc->Instance->CNTL, (TimeCounter & RTC_CNTL_RTC_CNT));
1403
   
1634
 
1404
    /* Wait for synchro */
1635
    /* Wait for synchro */
1405
    if(RTC_ExitInitMode(hrtc) != HAL_OK)
1636
    if (RTC_ExitInitMode(hrtc) != HAL_OK)
1406
    {      
1637
    {
1407
      status = HAL_ERROR;
1638
      status = HAL_ERROR;
1408
    }
1639
    }
1409
  }
1640
  }
1410
 
1641
 
1411
  return status;
1642
  return status;
Line 1415... Line 1646...
1415
  * @brief  Read the time counter available in RTC_ALR registers.
1646
  * @brief  Read the time counter available in RTC_ALR registers.
1416
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1647
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1417
  *                the configuration information for RTC.
1648
  *                the configuration information for RTC.
1418
  * @retval Time counter
1649
  * @retval Time counter
1419
  */
1650
  */
1420
static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc)
1651
static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef *hrtc)
1421
{
1652
{
1422
  uint16_t high1 = 0U, low = 0U;
1653
  uint16_t high1 = 0U, low = 0U;
1423
 
1654
 
1424
  high1 = READ_REG(hrtc->Instance->ALRH & RTC_CNTH_RTC_CNT);
1655
  high1 = READ_REG(hrtc->Instance->ALRH & RTC_CNTH_RTC_CNT);
1425
  low   = READ_REG(hrtc->Instance->ALRL & RTC_CNTL_RTC_CNT);
1656
  low   = READ_REG(hrtc->Instance->ALRL & RTC_CNTL_RTC_CNT);
Line 1432... Line 1663...
1432
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1663
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1433
  *                the configuration information for RTC.
1664
  *                the configuration information for RTC.
1434
  * @param  AlarmCounter: Counter to write in RTC_ALR registers
1665
  * @param  AlarmCounter: Counter to write in RTC_ALR registers
1435
  * @retval HAL status
1666
  * @retval HAL status
1436
  */
1667
  */
1437
static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter)
1668
static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef *hrtc, uint32_t AlarmCounter)
1438
{
1669
{
1439
  HAL_StatusTypeDef status = HAL_OK;
1670
  HAL_StatusTypeDef status = HAL_OK;
1440
 
1671
 
1441
  /* Set Initialization mode */
1672
  /* Set Initialization mode */
1442
  if(RTC_EnterInitMode(hrtc) != HAL_OK)
1673
  if (RTC_EnterInitMode(hrtc) != HAL_OK)
1443
  {
1674
  {
1444
    status = HAL_ERROR;
1675
    status = HAL_ERROR;
1445
  }
1676
  }
1446
  else
1677
  else
1447
  {
1678
  {
1448
    /* Set RTC COUNTER MSB word */
1679
    /* Set RTC COUNTER MSB word */
1449
    WRITE_REG(hrtc->Instance->ALRH, (AlarmCounter >> 16U));
1680
    WRITE_REG(hrtc->Instance->ALRH, (AlarmCounter >> 16U));
1450
    /* Set RTC COUNTER LSB word */
1681
    /* Set RTC COUNTER LSB word */
1451
    WRITE_REG(hrtc->Instance->ALRL, (AlarmCounter & RTC_ALRL_RTC_ALR));
1682
    WRITE_REG(hrtc->Instance->ALRL, (AlarmCounter & RTC_ALRL_RTC_ALR));
1452
   
1683
 
1453
    /* Wait for synchro */
1684
    /* Wait for synchro */
1454
    if(RTC_ExitInitMode(hrtc) != HAL_OK)
1685
    if (RTC_ExitInitMode(hrtc) != HAL_OK)
1455
    {      
1686
    {
1456
      status = HAL_ERROR;
1687
      status = HAL_ERROR;
1457
    }
1688
    }
1458
  }
1689
  }
1459
 
1690
 
1460
  return status;
1691
  return status;
Line 1464... Line 1695...
1464
  * @brief  Enters the RTC Initialization mode.
1695
  * @brief  Enters the RTC Initialization mode.
1465
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1696
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1466
  *                the configuration information for RTC.
1697
  *                the configuration information for RTC.
1467
  * @retval HAL status
1698
  * @retval HAL status
1468
  */
1699
  */
1469
static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1700
static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
1470
{
1701
{
1471
  uint32_t tickstart = 0U;
1702
  uint32_t tickstart = 0U;
1472
 
1703
 
1473
  tickstart = HAL_GetTick();
1704
  tickstart = HAL_GetTick();
1474
  /* Wait till RTC is in INIT state and if Time out is reached exit */
1705
  /* Wait till RTC is in INIT state and if Time out is reached exit */
1475
  while((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
1706
  while ((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
1476
  {
1707
  {
1477
    if((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1708
    if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1478
    {      
1709
    {
1479
      return HAL_TIMEOUT;
1710
      return HAL_TIMEOUT;
1480
    }
1711
    }
1481
  }
1712
  }
1482
 
1713
 
1483
  /* Disable the write protection for RTC registers */
1714
  /* Disable the write protection for RTC registers */
1484
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1715
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1485
 
1716
 
1486
 
1717
 
1487
  return HAL_OK;  
1718
  return HAL_OK;
1488
}
1719
}
1489
 
1720
 
1490
/**
1721
/**
1491
  * @brief  Exit the RTC Initialization mode.
1722
  * @brief  Exit the RTC Initialization mode.
1492
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1723
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1493
  *                the configuration information for RTC.
1724
  *                the configuration information for RTC.
1494
  * @retval HAL status
1725
  * @retval HAL status
1495
  */
1726
  */
1496
static HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef* hrtc)
1727
static HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
1497
{
1728
{
1498
  uint32_t tickstart = 0U;
1729
  uint32_t tickstart = 0U;
1499
 
1730
 
1500
  /* Disable the write protection for RTC registers */
1731
  /* Disable the write protection for RTC registers */
1501
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1732
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1502
 
1733
 
1503
  tickstart = HAL_GetTick();
1734
  tickstart = HAL_GetTick();
1504
  /* Wait till RTC is in INIT state and if Time out is reached exit */
1735
  /* Wait till RTC is in INIT state and if Time out is reached exit */
1505
  while((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
1736
  while ((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
1506
  {
1737
  {
1507
    if((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1738
    if ((HAL_GetTick() - tickstart) >  RTC_TIMEOUT_VALUE)
1508
    {      
1739
    {
1509
      return HAL_TIMEOUT;
1740
      return HAL_TIMEOUT;
1510
    }
1741
    }
1511
  }
1742
  }
1512
 
1743
 
1513
  return HAL_OK;  
1744
  return HAL_OK;
1514
}
1745
}
1515
 
1746
 
1516
/**
1747
/**
1517
  * @brief  Converts a 2 digit decimal to BCD format.
1748
  * @brief  Converts a 2 digit decimal to BCD format.
1518
  * @param  Value: Byte to be converted
1749
  * @param  Value: Byte to be converted
1519
  * @retval Converted byte
1750
  * @retval Converted byte
1520
  */
1751
  */
1521
static uint8_t RTC_ByteToBcd2(uint8_t Value)
1752
static uint8_t RTC_ByteToBcd2(uint8_t Value)
1522
{
1753
{
1523
  uint32_t bcdhigh = 0U;
1754
  uint32_t bcdhigh = 0U;
1524
 
1755
 
1525
  while(Value >= 10U)
1756
  while (Value >= 10U)
1526
  {
1757
  {
1527
    bcdhigh++;
1758
    bcdhigh++;
1528
    Value -= 10U;
1759
    Value -= 10U;
1529
  }
1760
  }
1530
 
1761
 
1531
  return  ((uint8_t)(bcdhigh << 4U) | Value);
1762
  return ((uint8_t)(bcdhigh << 4U) | Value);
1532
}
1763
}
1533
 
1764
 
1534
/**
1765
/**
1535
  * @brief  Converts from 2 digit BCD to Binary.
1766
  * @brief  Converts from 2 digit BCD to Binary.
1536
  * @param  Value: BCD value to be converted
1767
  * @param  Value: BCD value to be converted
Line 1548... Line 1779...
1548
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1779
  * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
1549
  *                the configuration information for RTC.
1780
  *                the configuration information for RTC.
1550
  * @param  DayElapsed: Number of days elapsed from last date update
1781
  * @param  DayElapsed: Number of days elapsed from last date update
1551
  * @retval None
1782
  * @retval None
1552
  */
1783
  */
1553
static void RTC_DateUpdate(RTC_HandleTypeDef* hrtc, uint32_t DayElapsed)
1784
static void RTC_DateUpdate(RTC_HandleTypeDef *hrtc, uint32_t DayElapsed)
1554
{
1785
{
1555
  uint32_t year = 0U, month = 0U, day = 0U;
1786
  uint32_t year = 0U, month = 0U, day = 0U;
1556
  uint32_t loop = 0U;
1787
  uint32_t loop = 0U;
1557
 
1788
 
1558
  /* Get the current year*/
1789
  /* Get the current year*/
Line 1562... Line 1793...
1562
  month = hrtc->DateToUpdate.Month;
1793
  month = hrtc->DateToUpdate.Month;
1563
  day = hrtc->DateToUpdate.Date;
1794
  day = hrtc->DateToUpdate.Date;
1564
 
1795
 
1565
  for (loop = 0U; loop < DayElapsed; loop++)
1796
  for (loop = 0U; loop < DayElapsed; loop++)
1566
  {
1797
  {
1567
    if((month == 1U) || (month == 3U) || (month == 5U) || (month == 7U) || \
1798
    if ((month == 1U) || (month == 3U) || (month == 5U) || (month == 7U) || \
1568
       (month == 8U) || (month == 10U) || (month == 12U))
1799
        (month == 8U) || (month == 10U) || (month == 12U))
1569
    {
1800
    {
1570
      if(day < 31U)
1801
      if (day < 31U)
1571
      {
1802
      {
1572
        day++;
1803
        day++;
1573
      }
1804
      }
1574
      /* Date structure member: day = 31 */
1805
      /* Date structure member: day = 31 */
1575
      else
1806
      else
1576
      {
1807
      {
1577
        if(month != 12U)
1808
        if (month != 12U)
1578
        {
1809
        {
1579
          month++;
1810
          month++;
1580
          day = 1U;
1811
          day = 1U;
1581
        }
1812
        }
1582
        /* Date structure member: day = 31 & month =12 */
1813
        /* Date structure member: day = 31 & month =12 */
Line 1586... Line 1817...
1586
          day = 1U;
1817
          day = 1U;
1587
          year++;
1818
          year++;
1588
        }
1819
        }
1589
      }
1820
      }
1590
    }
1821
    }
1591
    else if((month == 4U) || (month == 6U) || (month == 9U) || (month == 11U))
1822
    else if ((month == 4U) || (month == 6U) || (month == 9U) || (month == 11U))
1592
    {
1823
    {
1593
      if(day < 30U)
1824
      if (day < 30U)
1594
      {
1825
      {
1595
        day++;
1826
        day++;
1596
      }
1827
      }
1597
      /* Date structure member: day = 30 */
1828
      /* Date structure member: day = 30 */
1598
      else
1829
      else
1599
      {
1830
      {
1600
        month++;
1831
        month++;
1601
        day = 1U;
1832
        day = 1U;
1602
      }
1833
      }
1603
    }
1834
    }
1604
    else if(month == 2U)
1835
    else if (month == 2U)
1605
    {
1836
    {
1606
      if(day < 28U)
1837
      if (day < 28U)
1607
      {
1838
      {
1608
        day++;
1839
        day++;
1609
      }
1840
      }
1610
      else if(day == 28U)
1841
      else if (day == 28U)
1611
      {
1842
      {
1612
        /* Leap year */
1843
        /* Leap year */
1613
        if(RTC_IsLeapYear(year))
1844
        if (RTC_IsLeapYear(year))
1614
        {
1845
        {
1615
          day++;
1846
          day++;
1616
        }
1847
        }
1617
        else
1848
        else
1618
        {
1849
        {
1619
          month++;
1850
          month++;
1620
          day = 1U;
1851
          day = 1U;
1621
        }
1852
        }
1622
      }
1853
      }
1623
      else if(day == 29U)
1854
      else if (day == 29U)
1624
      {
1855
      {
1625
        month++;
1856
        month++;
1626
        day = 1U;
1857
        day = 1U;
1627
      }
1858
      }
1628
    }
1859
    }
Line 1645... Line 1876...
1645
  * @retval 1: leap year
1876
  * @retval 1: leap year
1646
  *         0: not leap year
1877
  *         0: not leap year
1647
  */
1878
  */
1648
static uint8_t RTC_IsLeapYear(uint16_t nYear)
1879
static uint8_t RTC_IsLeapYear(uint16_t nYear)
1649
{
1880
{
1650
  if((nYear % 4U) != 0U)
1881
  if ((nYear % 4U) != 0U)
1651
  {
1882
  {
1652
    return 0U;
1883
    return 0U;
1653
  }
1884
  }
1654
 
1885
 
1655
  if((nYear % 100U) != 0U)
1886
  if ((nYear % 100U) != 0U)
1656
  {
1887
  {
1657
    return 1U;
1888
    return 1U;
1658
  }
1889
  }
1659
 
1890
 
1660
  if((nYear % 400U) == 0U)
1891
  if ((nYear % 400U) == 0U)
1661
  {
1892
  {
1662
    return 1U;
1893
    return 1U;
1663
  }
1894
  }
1664
  else
1895
  else
1665
  {
1896
  {
Line 1685... Line 1916...
1685
static uint8_t RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay)
1916
static uint8_t RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay)
1686
{
1917
{
1687
  uint32_t year = 0U, weekday = 0U;
1918
  uint32_t year = 0U, weekday = 0U;
1688
 
1919
 
1689
  year = 2000U + nYear;
1920
  year = 2000U + nYear;
1690
 
1921
 
1691
  if(nMonth < 3U)
1922
  if (nMonth < 3U)
1692
  {
1923
  {
1693
    /*D = { [(23 x month)/9] + day + 4 + year + [(year-1)/4] - [(year-1)/100] + [(year-1)/400] } mod 7*/
1924
    /*D = { [(23 x month)/9] + day + 4 + year + [(year-1)/4] - [(year-1)/100] + [(year-1)/400] } mod 7*/
1694
    weekday = (((23U * nMonth)/9U) + nDay + 4U + year + ((year-1U)/4U) - ((year-1U)/100U) + ((year-1U)/400U)) % 7U;
1925
    weekday = (((23U * nMonth) / 9U) + nDay + 4U + year + ((year - 1U) / 4U) - ((year - 1U) / 100U) + ((year - 1U) / 400U)) % 7U;
1695
  }
1926
  }
1696
  else
1927
  else
1697
  {
1928
  {
1698
    /*D = { [(23 x month)/9] + day + 4 + year + [year/4] - [year/100] + [year/400] - 2 } mod 7*/
1929
    /*D = { [(23 x month)/9] + day + 4 + year + [year/4] - [year/100] + [year/400] - 2 } mod 7*/
1699
    weekday = (((23U * nMonth)/9U) + nDay + 4U + year + (year/4U) - (year/100U) + (year/400U) - 2U ) % 7U;
1930
    weekday = (((23U * nMonth) / 9U) + nDay + 4U + year + (year / 4U) - (year / 100U) + (year / 400U) - 2U) % 7U;
1700
  }
1931
  }
1701
 
1932
 
1702
  return (uint8_t)weekday;
1933
  return (uint8_t)weekday;
1703
}
1934
}
1704
 
1935