Subversion Repositories canSerial

Rev

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

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