1 /**
2 ******************************************************************************
3 * @file stm32l1xx_hal_rtc_ex.c
4 * @author MCD Application Team
5 * @brief Extended RTC HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Real Time Clock (RTC) Extension peripheral:
8 * + RTC Time Stamp functions
9 * + RTC Tamper functions
10 * + RTC Wake-up functions
11 * + Extension Control functions
12 * + Extension RTC features functions
13 *
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
19 (+) Enable the RTC domain access.
20 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
21 format using the HAL_RTC_Init() function.
22
23 *** RTC Wakeup configuration ***
24 ================================
25 [..]
26 (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
27 function. You can also configure the RTC Wakeup timer with interrupt mode
28 using the HAL_RTCEx_SetWakeUpTimer_IT() function.
29 (+) To read the RTC WakeUp Counter register, use the HAL_RTCEx_GetWakeUpTimer()
30 function.
31
32 *** TimeStamp configuration ***
33 ===============================
34 [..]
35 (+) Configure the RTC_AFx trigger and enable the RTC TimeStamp using the
36 HAL_RTCEx_SetTimeStamp() function. You can also configure the RTC TimeStamp with
37 interrupt mode using the HAL_RTCEx_SetTimeStamp_IT() function.
38 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
39 function.
40 (+) The TIMESTAMP alternate function can be mapped to RTC_AF1 (PC13).
41
42 *** Tamper configuration ***
43 ============================
44 [..]
45 (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
46 or Level according to the Tamper filter (if equal to 0 Edge else Level)
47 value, sampling frequency, precharge or discharge and Pull-UP using the
48 HAL_RTCEx_SetTamper() function. You can configure RTC Tamper with interrupt
49 mode using HAL_RTCEx_SetTamper_IT() function.
50 (+) The TAMPER1 alternate function can be mapped to RTC_AF1 (PC13).
51
52 *** Backup Data Registers configuration ***
53 ===========================================
54 [..]
55 (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
56 function.
57 (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
58 function.
59
60 @endverbatim
61 ******************************************************************************
62 * @attention
63 *
64 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
65 *
66 * Redistribution and use in source and binary forms, with or without modification,
67 * are permitted provided that the following conditions are met:
68 * 1. Redistributions of source code must retain the above copyright notice,
69 * this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright notice,
71 * this list of conditions and the following disclaimer in the documentation
72 * and/or other materials provided with the distribution.
73 * 3. Neither the name of STMicroelectronics nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
78 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
80 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
83 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
84 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
85 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
86 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87 *
88 ******************************************************************************
89 */
90
91 /* Includes ------------------------------------------------------------------*/
92 #include "stm32l1xx_hal.h"
93
94 /** @addtogroup STM32L1xx_HAL_Driver
95 * @{
96 */
97
98 /** @addtogroup RTC
99 * @{
100 */
101
102 #ifdef HAL_RTC_MODULE_ENABLED
103
104 /* Private typedef -----------------------------------------------------------*/
105 /* Private define ------------------------------------------------------------*/
106 /* Private macro -------------------------------------------------------------*/
107 /* Private variables ---------------------------------------------------------*/
108 /* Private function prototypes -----------------------------------------------*/
109 /* Private functions ---------------------------------------------------------*/
110
111 /** @addtogroup RTC_Exported_Functions
112 * @{
113 */
114
115
116 /** @addtogroup RTC_Exported_Functions_Group1
117 * @{
118 */
119
120 /**
121 * @brief DeInitializes the RTC peripheral
122 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
123 * the configuration information for RTC.
124 * @note This function does not reset the RTC Backup Data registers.
125 * @retval HAL status
126 */
HAL_RTC_DeInit(RTC_HandleTypeDef * hrtc)127 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
128 {
129 uint32_t tickstart = 0;
130
131 /* Check the parameters */
132 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
133
134 /* Set RTC state */
135 hrtc->State = HAL_RTC_STATE_BUSY;
136
137 /* Disable the write protection for RTC registers */
138 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
139
140 /* Set Initialization mode */
141 if(RTC_EnterInitMode(hrtc) != HAL_OK)
142 {
143 /* Enable the write protection for RTC registers */
144 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
145
146 /* Set RTC state */
147 hrtc->State = HAL_RTC_STATE_ERROR;
148
149 return HAL_ERROR;
150 }
151 else
152 {
153 /* Reset TR, DR and CR registers */
154 hrtc->Instance->TR = 0x00000000U;
155 hrtc->Instance->DR = 0x00002101U;
156 /* Reset All CR bits except CR[2:0] */
157 hrtc->Instance->CR &= 0x00000007U;
158
159 tickstart = HAL_GetTick();
160
161 /* Wait till WUTWF flag is set and if Time out is reached exit */
162 while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
163 {
164 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
165 {
166 /* Enable the write protection for RTC registers */
167 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
168
169 /* Set RTC state */
170 hrtc->State = HAL_RTC_STATE_TIMEOUT;
171
172 return HAL_TIMEOUT;
173 }
174 }
175
176 /* Reset all RTC CR register bits */
177 hrtc->Instance->CR &= 0x00000000U;
178 hrtc->Instance->WUTR = 0x0000FFFFU;
179 hrtc->Instance->PRER = 0x007F00FFU;
180 hrtc->Instance->CALIBR = 0x00000000U;
181 hrtc->Instance->ALRMAR = 0x00000000U;
182 hrtc->Instance->ALRMBR = 0x00000000U;
183 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
184 hrtc->Instance->SHIFTR = 0x00000000U;
185 hrtc->Instance->CALR = 0x00000000U;
186 hrtc->Instance->ALRMASSR = 0x00000000U;
187 hrtc->Instance->ALRMBSSR = 0x00000000U;
188 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
189 /* Reset ISR register and exit initialization mode */
190 hrtc->Instance->ISR = 0x00000000U;
191
192 /* Reset Tamper and alternate functions configuration register */
193 hrtc->Instance->TAFCR = 0x00000000U;
194
195 /* Wait for synchro */
196 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
197 {
198 /* Enable the write protection for RTC registers */
199 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
200
201 hrtc->State = HAL_RTC_STATE_ERROR;
202
203 return HAL_ERROR;
204 }
205 }
206
207 /* Enable the write protection for RTC registers */
208 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
209
210 /* De-Initialize RTC MSP */
211 HAL_RTC_MspDeInit(hrtc);
212
213 hrtc->State = HAL_RTC_STATE_RESET;
214
215 /* Release Lock */
216 __HAL_UNLOCK(hrtc);
217
218 return HAL_OK;
219 }
220
221 /**
222 * @}
223 */
224
225 /** @addtogroup RTC_Exported_Functions_Group2
226 * @{
227 */
228
229 /**
230 * @brief Get RTC current time.
231 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
232 * the configuration information for RTC.
233 * @param sTime: Pointer to Time structure with Hours, Minutes and Seconds fields returned
234 * with input format (BIN or BCD), also SubSeconds field (if availabale) returning the
235 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
236 * factor to be used for second fraction ratio computation.
237 * @param Format: Specifies the format of the entered parameters.
238 * This parameter can be one of the following values:
239 * @arg RTC_FORMAT_BIN: Binary data format
240 * @arg RTC_FORMAT_BCD: BCD data format
241 * @note If available, you can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
242 * value in second fraction ratio with time unit following generic formula:
243 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
244 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
245 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
246 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
247 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
248 * to ensure consistency between the time and date values.
249 * @retval HAL status
250 */
HAL_RTC_GetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)251 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
252 {
253 uint32_t tmpreg = 0;
254
255 /* Check the parameters */
256 assert_param(IS_RTC_FORMAT(Format));
257
258 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
259 /* Get subseconds structure field from the corresponding register*/
260 sTime->SubSeconds = (uint32_t)((hrtc->Instance->SSR) & RTC_SSR_SS);
261
262 /* Get SecondFraction structure field from the corresponding register field*/
263 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
264 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
265
266 /* Get the TR register */
267 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
268
269 /* Fill the structure fields with the read parameters */
270 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
271 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
272 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
273 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
274
275 /* Check the input parameters format */
276 if(Format == RTC_FORMAT_BIN)
277 {
278 /* Convert the time structure parameters to Binary format */
279 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
280 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
281 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
282 }
283
284 return HAL_OK;
285 }
286
287 /**
288 * @}
289 */
290
291 /** @addtogroup RTC_Exported_Functions_Group3
292 * @{
293 */
294
295 /**
296 * @brief Sets the specified RTC Alarm.
297 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
298 * the configuration information for RTC.
299 * @param sAlarm: Pointer to Alarm structure
300 * @param Format: Specifies the format of the entered parameters.
301 * This parameter can be one of the following values:
302 * @arg RTC_FORMAT_BIN: Binary data format
303 * @arg RTC_FORMAT_BCD: BCD data format
304 * @retval HAL status
305 */
HAL_RTC_SetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)306 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
307 {
308 uint32_t tickstart = 0;
309 uint32_t tmpreg = 0;
310
311 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
312 uint32_t subsecondtmpreg = 0;
313 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
314
315 /* Check the parameters */
316 assert_param(IS_RTC_FORMAT(Format));
317 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
318 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
319 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
320 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
321 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
322 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
323 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
324
325 /* Process Locked */
326 __HAL_LOCK(hrtc);
327
328 hrtc->State = HAL_RTC_STATE_BUSY;
329
330 if(Format == RTC_FORMAT_BIN)
331 {
332 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
333 {
334 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
335 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
336 }
337 else
338 {
339 sAlarm->AlarmTime.TimeFormat = 0x00;
340 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
341 }
342 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
343 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
344
345 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
346 {
347 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
348 }
349 else
350 {
351 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
352 }
353
354 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
355 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
356 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
357 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
358 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
359 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
360 ((uint32_t)sAlarm->AlarmMask));
361 }
362 else
363 {
364 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
365 {
366 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
367 assert_param(IS_RTC_HOUR12(tmpreg));
368 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
369 }
370 else
371 {
372 sAlarm->AlarmTime.TimeFormat = 0x00;
373 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
374 }
375
376 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
377 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
378
379 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
380 {
381 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
382 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
383 }
384 else
385 {
386 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
387 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
388 }
389
390 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
391 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
392 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
393 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
394 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
395 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
396 ((uint32_t)sAlarm->AlarmMask));
397 }
398
399 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
400 /* Configure the Alarm A or Alarm B Sub Second registers */
401 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
402 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
403
404 /* Disable the write protection for RTC registers */
405 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
406
407 /* Configure the Alarm register */
408 if(sAlarm->Alarm == RTC_ALARM_A)
409 {
410 /* Disable the Alarm A interrupt */
411 __HAL_RTC_ALARMA_DISABLE(hrtc);
412
413 /* In case of interrupt mode is used, the interrupt source must disabled */
414 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
415
416 tickstart = HAL_GetTick();
417 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
418 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
419 {
420 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
421 {
422 /* Enable the write protection for RTC registers */
423 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
424
425 hrtc->State = HAL_RTC_STATE_TIMEOUT;
426
427 /* Process Unlocked */
428 __HAL_UNLOCK(hrtc);
429
430 return HAL_TIMEOUT;
431 }
432 }
433
434 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
435 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
436 /* Configure the Alarm A Sub Second register */
437 hrtc->Instance->ALRMASSR = subsecondtmpreg;
438 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
439 /* Configure the Alarm state: Enable Alarm */
440 __HAL_RTC_ALARMA_ENABLE(hrtc);
441 }
442 else
443 {
444 /* Disable the Alarm B interrupt */
445 __HAL_RTC_ALARMB_DISABLE(hrtc);
446
447 /* In case of interrupt mode is used, the interrupt source must disabled */
448 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
449
450 tickstart = HAL_GetTick();
451 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
452 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
453 {
454 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
455 {
456 /* Enable the write protection for RTC registers */
457 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
458
459 hrtc->State = HAL_RTC_STATE_TIMEOUT;
460
461 /* Process Unlocked */
462 __HAL_UNLOCK(hrtc);
463
464 return HAL_TIMEOUT;
465 }
466 }
467
468 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
469 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
470 /* Configure the Alarm B Sub Second register */
471 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
472 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
473 /* Configure the Alarm state: Enable Alarm */
474 __HAL_RTC_ALARMB_ENABLE(hrtc);
475 }
476
477 /* Enable the write protection for RTC registers */
478 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
479
480 /* Change RTC state */
481 hrtc->State = HAL_RTC_STATE_READY;
482
483 /* Process Unlocked */
484 __HAL_UNLOCK(hrtc);
485
486 return HAL_OK;
487 }
488
489 /**
490 * @brief Sets the specified RTC Alarm with Interrupt
491 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
492 * the configuration information for RTC.
493 * @param sAlarm: Pointer to Alarm structure
494 * @param Format: Specifies the format of the entered parameters.
495 * This parameter can be one of the following values:
496 * @arg RTC_FORMAT_BIN: Binary data format
497 * @arg RTC_FORMAT_BCD: BCD data format
498 * @note The Alarm register can only be written when the corresponding Alarm
499 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
500 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
501 * @retval HAL status
502 */
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)503 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
504 {
505 uint32_t tickstart = 0;
506 uint32_t tmpreg = 0;
507 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
508 uint32_t subsecondtmpreg = 0;
509 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
510
511 /* Check the parameters */
512 assert_param(IS_RTC_FORMAT(Format));
513 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
514 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
515 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
516 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
517 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
518 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
519 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
520
521 /* Process Locked */
522 __HAL_LOCK(hrtc);
523
524 hrtc->State = HAL_RTC_STATE_BUSY;
525
526 if(Format == RTC_FORMAT_BIN)
527 {
528 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
529 {
530 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
531 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
532 }
533 else
534 {
535 sAlarm->AlarmTime.TimeFormat = 0x00;
536 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
537 }
538 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
539 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
540
541 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
542 {
543 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
544 }
545 else
546 {
547 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
548 }
549 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
550 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
551 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
552 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
553 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
554 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
555 ((uint32_t)sAlarm->AlarmMask));
556 }
557 else
558 {
559 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
560 {
561 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
562 assert_param(IS_RTC_HOUR12(tmpreg));
563 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
564 }
565 else
566 {
567 sAlarm->AlarmTime.TimeFormat = 0x00;
568 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
569 }
570
571 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
572 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
573
574 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
575 {
576 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
577 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
578 }
579 else
580 {
581 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
582 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
583 }
584 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
585 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
586 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
587 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
588 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
589 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
590 ((uint32_t)sAlarm->AlarmMask));
591 }
592 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
593 /* Configure the Alarm A or Alarm B Sub Second registers */
594 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
595 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
596
597 /* Disable the write protection for RTC registers */
598 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
599
600 /* Configure the Alarm register */
601 if(sAlarm->Alarm == RTC_ALARM_A)
602 {
603 /* Disable the Alarm A interrupt */
604 __HAL_RTC_ALARMA_DISABLE(hrtc);
605
606 /* Clear flag alarm A */
607 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
608
609 tickstart = HAL_GetTick();
610 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
611 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
612 {
613 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
614 {
615 /* Enable the write protection for RTC registers */
616 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
617
618 hrtc->State = HAL_RTC_STATE_TIMEOUT;
619
620 /* Process Unlocked */
621 __HAL_UNLOCK(hrtc);
622
623 return HAL_TIMEOUT;
624 }
625 }
626
627 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
628 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
629 /* Configure the Alarm A Sub Second register */
630 hrtc->Instance->ALRMASSR = subsecondtmpreg;
631 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
632 /* Configure the Alarm state: Enable Alarm */
633 __HAL_RTC_ALARMA_ENABLE(hrtc);
634 /* Configure the Alarm interrupt */
635 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
636 }
637 else
638 {
639 /* Disable the Alarm B interrupt */
640 __HAL_RTC_ALARMB_DISABLE(hrtc);
641
642 /* Clear flag alarm B */
643 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
644
645 tickstart = HAL_GetTick();
646 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
647 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
648 {
649 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
650 {
651 /* Enable the write protection for RTC registers */
652 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
653
654 hrtc->State = HAL_RTC_STATE_TIMEOUT;
655
656 /* Process Unlocked */
657 __HAL_UNLOCK(hrtc);
658
659 return HAL_TIMEOUT;
660 }
661 }
662
663 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
664 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
665 /* Configure the Alarm B Sub Second register */
666 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
667 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
668 /* Configure the Alarm state: Enable Alarm */
669 __HAL_RTC_ALARMB_ENABLE(hrtc);
670 /* Configure the Alarm interrupt */
671 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
672 }
673
674 /* RTC Alarm Interrupt Configuration: EXTI configuration */
675 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
676
677 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
678
679 /* Enable the write protection for RTC registers */
680 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
681
682 hrtc->State = HAL_RTC_STATE_READY;
683
684 /* Process Unlocked */
685 __HAL_UNLOCK(hrtc);
686
687 return HAL_OK;
688 }
689
690 /**
691 * @brief Gets the RTC Alarm value and masks.
692 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
693 * the configuration information for RTC.
694 * @param sAlarm: Pointer to Date structure
695 * @param Alarm: Specifies the Alarm.
696 * This parameter can be one of the following values:
697 * @arg RTC_ALARM_A: AlarmA
698 * @arg RTC_ALARM_B: AlarmB
699 * @param Format: Specifies the format of the entered parameters.
700 * This parameter can be one of the following values:
701 * @arg RTC_FORMAT_BIN: Binary data format
702 * @arg RTC_FORMAT_BCD: BCD data format
703 * @retval HAL status
704 */
HAL_RTC_GetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Alarm,uint32_t Format)705 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
706 {
707 uint32_t tmpreg = 0;
708 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
709 uint32_t subsecondtmpreg = 0;
710 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
711
712 /* Check the parameters */
713 assert_param(IS_RTC_FORMAT(Format));
714 assert_param(IS_RTC_ALARM(Alarm));
715
716 if(Alarm == RTC_ALARM_A)
717 {
718 /* AlarmA */
719 sAlarm->Alarm = RTC_ALARM_A;
720
721 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
722 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
723 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
724 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
725 }
726 else
727 {
728 sAlarm->Alarm = RTC_ALARM_B;
729
730 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
731 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
732 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
733 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
734 }
735
736 /* Fill the structure with the read parameters */
737 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
738 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
739 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
740 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
741 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
742 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
743 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
744 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
745 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
746 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
747
748 if(Format == RTC_FORMAT_BIN)
749 {
750 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
751 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
752 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
753 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
754 }
755
756 return HAL_OK;
757 }
758
759 /**
760 * @}
761 */
762
763
764 /** @defgroup RTC_Exported_Functions_Group6 Peripheral Control functions
765 * @brief Peripheral Control functions
766 *
767 @verbatim
768 ===============================================================================
769 ##### Peripheral Control functions #####
770 ===============================================================================
771 [..]
772 This subsection provides functions allowing to
773 (+) Wait for RTC Time and Date Synchronization
774
775 @endverbatim
776 * @{
777 */
778
779 /**
780 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
781 * synchronized with RTC APB clock.
782 * @note The RTC Resynchronization mode is write protected, use the
783 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
784 * @note To read the calendar through the shadow registers after Calendar
785 * initialization, calendar update or after wakeup from low power modes
786 * the software must first clear the RSF flag.
787 * The software must then wait until it is set again before reading
788 * the calendar, which means that the calendar registers have been
789 * correctly copied into the RTC_TR and RTC_DR shadow registers.
790 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
791 * the configuration information for RTC.
792 * @retval HAL status
793 */
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef * hrtc)794 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
795 {
796 uint32_t tickstart = 0;
797
798 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
799 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
800 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
801 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
802 {
803 /* Clear RSF flag */
804 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
805
806 tickstart = HAL_GetTick();
807
808 /* Wait the registers to be synchronised */
809 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
810 {
811 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
812 {
813 return HAL_TIMEOUT;
814 }
815 }
816 }
817
818 return HAL_OK;
819 }
820
821 /**
822 * @}
823 */
824
825 /**
826 * @}
827 */
828
829 /**
830 * @}
831 */
832
833 /** @defgroup RTCEx RTCEx
834 * @brief RTC Extended HAL module driver
835 * @{
836 */
837
838 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
839 * @{
840 */
841
842 /** @defgroup RTCEx_Exported_Functions_Group4 RTC TimeStamp and Tamper functions
843 * @brief RTC TimeStamp and Tamper functions
844 *
845 @verbatim
846 ===============================================================================
847 ##### RTC TimeStamp and Tamper functions #####
848 ===============================================================================
849
850 [..] This section provides functions allowing to configure TimeStamp feature
851
852 @endverbatim
853 * @{
854 */
855
856 /**
857 * @brief Sets TimeStamp.
858 * @note This API must be called before enabling the TimeStamp feature.
859 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
860 * the configuration information for RTC.
861 * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
862 * activated.
863 * This parameter can be one of the following values:
864 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
865 * rising edge of the related pin.
866 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
867 * falling edge of the related pin.
868 * @retval HAL status
869 */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge)870 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge)
871 {
872 uint32_t tmpreg = 0;
873
874 /* Check the parameters */
875 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
876
877 /* Process Locked */
878 __HAL_LOCK(hrtc);
879
880 hrtc->State = HAL_RTC_STATE_BUSY;
881
882 /* Get the RTC_CR register and clear the bits to be configured */
883 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
884
885 tmpreg|= TimeStampEdge;
886
887 /* Disable the write protection for RTC registers */
888 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
889
890 /* Configure the Time Stamp TSEDGE and Enable bits */
891 hrtc->Instance->CR = (uint32_t)tmpreg;
892
893 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
894
895 /* Enable the write protection for RTC registers */
896 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
897
898 /* Change RTC state */
899 hrtc->State = HAL_RTC_STATE_READY;
900
901 /* Process Unlocked */
902 __HAL_UNLOCK(hrtc);
903
904 return HAL_OK;
905 }
906
907 /**
908 * @brief Sets TimeStamp with Interrupt.
909 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
910 * the configuration information for RTC.
911 * @note This API must be called before enabling the TimeStamp feature.
912 * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
913 * activated.
914 * This parameter can be one of the following values:
915 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
916 * rising edge of the related pin.
917 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
918 * falling edge of the related pin.
919 * @retval HAL status
920 */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge)921 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge)
922 {
923 uint32_t tmpreg = 0;
924
925 /* Check the parameters */
926 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
927
928 /* Process Locked */
929 __HAL_LOCK(hrtc);
930
931 hrtc->State = HAL_RTC_STATE_BUSY;
932
933 /* Get the RTC_CR register and clear the bits to be configured */
934 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
935
936 tmpreg |= TimeStampEdge;
937
938 /* Disable the write protection for RTC registers */
939 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
940
941 /* Configure the Time Stamp TSEDGE and Enable bits */
942 hrtc->Instance->CR = (uint32_t)tmpreg;
943
944 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
945
946 /* Enable IT timestamp */
947 __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
948
949 /* RTC timestamp Interrupt Configuration: EXTI configuration */
950 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
951
952 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
953
954 /* Enable the write protection for RTC registers */
955 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
956
957 hrtc->State = HAL_RTC_STATE_READY;
958
959 /* Process Unlocked */
960 __HAL_UNLOCK(hrtc);
961
962 return HAL_OK;
963 }
964
965 /**
966 * @brief Deactivates TimeStamp.
967 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
968 * the configuration information for RTC.
969 * @retval HAL status
970 */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)971 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
972 {
973 uint32_t tmpreg = 0;
974
975 /* Process Locked */
976 __HAL_LOCK(hrtc);
977
978 hrtc->State = HAL_RTC_STATE_BUSY;
979
980 /* Disable the write protection for RTC registers */
981 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
982
983 /* In case of interrupt mode is used, the interrupt source must disabled */
984 __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
985
986 /* Get the RTC_CR register and clear the bits to be configured */
987 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
988
989 /* Configure the Time Stamp TSEDGE and Enable bits */
990 hrtc->Instance->CR = (uint32_t)tmpreg;
991
992 /* Enable the write protection for RTC registers */
993 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
994
995 hrtc->State = HAL_RTC_STATE_READY;
996
997 /* Process Unlocked */
998 __HAL_UNLOCK(hrtc);
999
1000 return HAL_OK;
1001 }
1002
1003 /**
1004 * @brief Gets the RTC TimeStamp value.
1005 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1006 * the configuration information for RTC.
1007 * @param sTimeStamp: Pointer to Time structure
1008 * @param sTimeStampDate: Pointer to Date structure
1009 * @param Format: specifies the format of the entered parameters.
1010 * This parameter can be one of the following values:
1011 * RTC_FORMAT_BIN: Binary data format
1012 * RTC_FORMAT_BCD: BCD data format
1013 * @retval HAL status
1014 */
HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)1015 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
1016 {
1017 uint32_t tmptime = 0, tmpdate = 0;
1018
1019 /* Check the parameters */
1020 assert_param(IS_RTC_FORMAT(Format));
1021
1022 /* Get the TimeStamp time and date registers values */
1023 tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
1024 tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
1025
1026 /* Fill the Time structure fields with the read parameters */
1027 sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
1028 sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
1029 sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
1030 sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
1031 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
1032 sTimeStamp->SubSeconds = (uint32_t)((hrtc->Instance->TSSSR) & RTC_TSSSR_SS);
1033 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1034
1035 /* Fill the Date structure fields with the read parameters */
1036 sTimeStampDate->Year = 0;
1037 sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
1038 sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
1039 sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
1040
1041 /* Check the input parameters format */
1042 if(Format == RTC_FORMAT_BIN)
1043 {
1044 /* Convert the TimeStamp structure parameters to Binary format */
1045 sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
1046 sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
1047 sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
1048
1049 /* Convert the DateTimeStamp structure parameters to Binary format */
1050 sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
1051 sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
1052 sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
1053 }
1054
1055 /* Clear the TIMESTAMP Flag */
1056 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
1057
1058 return HAL_OK;
1059 }
1060
1061 /**
1062 * @brief Sets Tamper
1063 * @note By calling this API we disable the tamper interrupt for all tampers.
1064 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1065 * the configuration information for RTC.
1066 * @param sTamper: Pointer to Tamper Structure.
1067 * @retval HAL status
1068 */
HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)1069 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
1070 {
1071 uint32_t tmpreg = 0;
1072
1073 /* Check the parameters */
1074 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1075 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1076 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
1077 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1078 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1079 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1080 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1081 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1082 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1083
1084 /* Process Locked */
1085 __HAL_LOCK(hrtc);
1086
1087 hrtc->State = HAL_RTC_STATE_BUSY;
1088
1089 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
1090 if((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE))
1091 {
1092 /* Configure the RTC_TAFCR register */
1093 sTamper->Trigger = RTC_TAMPERTRIGGER_RISINGEDGE;
1094 }
1095 else
1096 {
1097 sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
1098 }
1099
1100 tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->Filter |\
1101 (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
1102 (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
1103
1104 hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
1105 (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
1106 (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPIE);
1107 #else
1108 tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Trigger));
1109
1110 hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)RTC_TAFCR_TAMP1E | (uint32_t)RTC_TAFCR_TAMP1TRG);
1111
1112 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1113 hrtc->Instance->TAFCR |= tmpreg;
1114
1115 hrtc->State = HAL_RTC_STATE_READY;
1116
1117 /* Process Unlocked */
1118 __HAL_UNLOCK(hrtc);
1119
1120 return HAL_OK;
1121 }
1122
1123 /**
1124 * @brief Sets Tamper with interrupt.
1125 * @note By calling this API we force the tamper interrupt for all tampers.
1126 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1127 * the configuration information for RTC.
1128 * @param sTamper: Pointer to RTC Tamper.
1129 * @retval HAL status
1130 */
HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)1131 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
1132 {
1133 uint32_t tmpreg = 0;
1134
1135 /* Check the parameters */
1136 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1137 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1138 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
1139 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1140 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1141 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1142 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1143 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1144 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1145
1146 /* Process Locked */
1147 __HAL_LOCK(hrtc);
1148
1149 hrtc->State = HAL_RTC_STATE_BUSY;
1150
1151 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
1152 /* Configure the tamper trigger */
1153 if((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE))
1154 {
1155 sTamper->Trigger = RTC_TAMPERTRIGGER_RISINGEDGE;
1156 }
1157 else
1158 {
1159 sTamper->Trigger = (uint32_t) (sTamper->Tamper<<1);
1160 }
1161
1162 tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->Filter |\
1163 (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
1164 (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
1165
1166 hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
1167 (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
1168 (uint32_t)RTC_TAFCR_TAMPPUDIS);
1169 #else
1170 tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger);
1171
1172 hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)RTC_TAFCR_TAMP1E | (uint32_t)RTC_TAFCR_TAMP1TRG | (uint32_t)RTC_TAFCR_TAMPIE);
1173 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1174 hrtc->Instance->TAFCR |= tmpreg;
1175
1176 /* Configure the Tamper Interrupt in the RTC_TAFCR */
1177 hrtc->Instance->TAFCR |= (uint32_t)RTC_TAFCR_TAMPIE;
1178
1179 /* RTC Tamper Interrupt Configuration: EXTI configuration */
1180 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
1181
1182 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
1183
1184 hrtc->State = HAL_RTC_STATE_READY;
1185
1186 /* Process Unlocked */
1187 __HAL_UNLOCK(hrtc);
1188
1189 return HAL_OK;
1190 }
1191
1192 /**
1193 * @brief Deactivates Tamper.
1194 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1195 * the configuration information for RTC.
1196 * @param Tamper: Selected tamper pin.
1197 * This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
1198 * @retval HAL status
1199 */
HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc,uint32_t Tamper)1200 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
1201 {
1202 assert_param(IS_RTC_TAMPER(Tamper));
1203
1204 /* Process Locked */
1205 __HAL_LOCK(hrtc);
1206
1207 hrtc->State = HAL_RTC_STATE_BUSY;
1208
1209 /* Disable the selected Tamper pin */
1210 hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
1211
1212 hrtc->State = HAL_RTC_STATE_READY;
1213
1214 /* Process Unlocked */
1215 __HAL_UNLOCK(hrtc);
1216
1217 return HAL_OK;
1218 }
1219
1220 /**
1221 * @brief This function handles TimeStamp interrupt request.
1222 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1223 * the configuration information for RTC.
1224 * @retval None
1225 */
HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef * hrtc)1226 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
1227 {
1228 /* Get the TimeStamp interrupt source enable status */
1229 if(__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != RESET)
1230 {
1231 /* Get the pending status of the TIMESTAMP Interrupt */
1232 if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != RESET)
1233 {
1234 /* TIMESTAMP callback */
1235 HAL_RTCEx_TimeStampEventCallback(hrtc);
1236
1237 /* Clear the TIMESTAMP interrupt pending bit */
1238 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
1239 }
1240 }
1241
1242 /* Get the Tamper1 interrupts source enable status */
1243 if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != RESET)
1244 {
1245 /* Get the pending status of the Tamper1 Interrupt */
1246 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != RESET)
1247 {
1248 /* Tamper1 callback */
1249 HAL_RTCEx_Tamper1EventCallback(hrtc);
1250
1251 /* Clear the Tamper1 interrupt pending bit */
1252 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
1253 }
1254 }
1255
1256 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
1257 /* Get the Tamper2 interrupts source enable status */
1258 if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != RESET)
1259 {
1260 /* Get the pending status of the Tamper2 Interrupt */
1261 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != RESET)
1262 {
1263 /* Tamper2 callback */
1264 HAL_RTCEx_Tamper2EventCallback(hrtc);
1265
1266 /* Clear the Tamper2 interrupt pending bit */
1267 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
1268 }
1269 }
1270
1271 /* Get the Tamper3 interrupts source enable status */
1272 if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != RESET)
1273 {
1274 /* Get the pending status of the Tamper3 Interrupt */
1275 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != RESET)
1276 {
1277 /* Tamper3 callback */
1278 HAL_RTCEx_Tamper3EventCallback(hrtc);
1279
1280 /* Clear the Tamper3 interrupt pending bit */
1281 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
1282 }
1283 }
1284 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1285
1286 /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
1287 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
1288
1289 /* Change RTC state */
1290 hrtc->State = HAL_RTC_STATE_READY;
1291 }
1292
1293 /**
1294 * @brief TimeStamp callback.
1295 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1296 * the configuration information for RTC.
1297 * @retval None
1298 */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)1299 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
1300 {
1301 /* Prevent unused argument(s) compilation warning */
1302 UNUSED(hrtc);
1303
1304 /* NOTE : This function Should not be modified, when the callback is needed,
1305 the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
1306 */
1307 }
1308
1309 /**
1310 * @brief Tamper 1 callback.
1311 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1312 * the configuration information for RTC.
1313 * @retval None
1314 */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)1315 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
1316 {
1317 /* Prevent unused argument(s) compilation warning */
1318 UNUSED(hrtc);
1319
1320 /* NOTE : This function Should not be modified, when the callback is needed,
1321 the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
1322 */
1323 }
1324
1325 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
1326 /**
1327 * @brief Tamper 2 callback.
1328 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1329 * the configuration information for RTC.
1330 * @retval None
1331 */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)1332 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
1333 {
1334 /* Prevent unused argument(s) compilation warning */
1335 UNUSED(hrtc);
1336
1337 /* NOTE : This function should not be modified, when the callback is needed,
1338 the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
1339 */
1340 }
1341
1342 /**
1343 * @brief Tamper 3 callback.
1344 * @param hrtc: RTC handle
1345 * @retval None
1346 */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)1347 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
1348 {
1349 /* Prevent unused argument(s) compilation warning */
1350 UNUSED(hrtc);
1351
1352 /* NOTE : This function Should not be modified, when the callback is needed,
1353 the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
1354 */
1355 }
1356 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1357
1358 /**
1359 * @brief This function handles TimeStamp polling request.
1360 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1361 * the configuration information for RTC.
1362 * @param Timeout: Timeout duration
1363 * @retval HAL status
1364 */
HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1365 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1366 {
1367 uint32_t tickstart = HAL_GetTick();
1368
1369 while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
1370 {
1371 if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
1372 {
1373 /* Clear the TIMESTAMP OverRun Flag */
1374 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
1375
1376 /* Change TIMESTAMP state */
1377 hrtc->State = HAL_RTC_STATE_ERROR;
1378
1379 return HAL_ERROR;
1380 }
1381
1382 if(Timeout != HAL_MAX_DELAY)
1383 {
1384 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1385 {
1386 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1387 return HAL_TIMEOUT;
1388 }
1389 }
1390 }
1391
1392 /* Change RTC state */
1393 hrtc->State = HAL_RTC_STATE_READY;
1394
1395 return HAL_OK;
1396 }
1397
1398 /**
1399 * @brief This function handles Tamper1 Polling.
1400 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1401 * the configuration information for RTC.
1402 * @param Timeout: Timeout duration
1403 * @retval HAL status
1404 */
HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1405 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1406 {
1407 uint32_t tickstart = HAL_GetTick();
1408
1409 /* Get the status of the Interrupt */
1410 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP1F)== RESET)
1411 {
1412 if(Timeout != HAL_MAX_DELAY)
1413 {
1414 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1415 {
1416 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1417 return HAL_TIMEOUT;
1418 }
1419 }
1420 }
1421
1422 /* Clear the Tamper Flag */
1423 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
1424
1425 /* Change RTC state */
1426 hrtc->State = HAL_RTC_STATE_READY;
1427
1428 return HAL_OK;
1429 }
1430
1431 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
1432 /**
1433 * @brief This function handles Tamper2 Polling.
1434 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1435 * the configuration information for RTC.
1436 * @param Timeout: Timeout duration
1437 * @retval HAL status
1438 */
HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1439 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1440 {
1441 uint32_t tickstart = HAL_GetTick();
1442
1443 /* Get the status of the Interrupt */
1444 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP2F) == RESET)
1445 {
1446 if(Timeout != HAL_MAX_DELAY)
1447 {
1448 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1449 {
1450 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1451 return HAL_TIMEOUT;
1452 }
1453 }
1454 }
1455
1456 /* Clear the Tamper Flag */
1457 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
1458
1459 /* Change RTC state */
1460 hrtc->State = HAL_RTC_STATE_READY;
1461
1462 return HAL_OK;
1463 }
1464
1465 /**
1466 * @brief This function handles Tamper3 Polling.
1467 * @param hrtc: RTC handle
1468 * @param Timeout: Timeout duration
1469 * @retval HAL status
1470 */
HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1471 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1472 {
1473 uint32_t tickstart = HAL_GetTick();
1474
1475 /* Get the status of the Interrupt */
1476 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP3F) == RESET)
1477 {
1478 if(Timeout != HAL_MAX_DELAY)
1479 {
1480 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1481 {
1482 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1483 return HAL_TIMEOUT;
1484 }
1485 }
1486 }
1487
1488 /* Clear the Tamper Flag */
1489 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP3F);
1490
1491 /* Change RTC state */
1492 hrtc->State = HAL_RTC_STATE_READY;
1493
1494 return HAL_OK;
1495 }
1496 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
1497
1498 /**
1499 * @}
1500 */
1501
1502 /** @defgroup RTCEx_Exported_Functions_Group5 RTC Wake-up functions
1503 * @brief RTC Wake-up functions
1504 *
1505 @verbatim
1506 ===============================================================================
1507 ##### RTC Wake-up functions #####
1508 ===============================================================================
1509
1510 [..] This section provides functions allowing to configure Wake-up feature
1511
1512 @endverbatim
1513 * @{
1514 */
1515
1516 /**
1517 * @brief Set wake up timer.
1518 * @param hrtc: RTC handle
1519 * @param WakeUpCounter: Wake up counter
1520 * @param WakeUpClock: Wake up clock
1521 * @retval HAL status
1522 */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)1523 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1524 {
1525 uint32_t tickstart = 0;
1526
1527 /* Check the parameters */
1528 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1529 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1530
1531 /* Process Locked */
1532 __HAL_LOCK(hrtc);
1533
1534 hrtc->State = HAL_RTC_STATE_BUSY;
1535
1536 /* Disable the write protection for RTC registers */
1537 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1538
1539 /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
1540 if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET){
1541 tickstart = HAL_GetTick();
1542
1543 /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
1544 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET)
1545 {
1546 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1547 {
1548 /* Enable the write protection for RTC registers */
1549 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1550
1551 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1552
1553 /* Process Unlocked */
1554 __HAL_UNLOCK(hrtc);
1555
1556 return HAL_TIMEOUT;
1557 }
1558 }
1559 }
1560
1561 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1562
1563 tickstart = HAL_GetTick();
1564
1565 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1566 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1567 {
1568 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1569 {
1570 /* Enable the write protection for RTC registers */
1571 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1572
1573 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1574
1575 /* Process Unlocked */
1576 __HAL_UNLOCK(hrtc);
1577
1578 return HAL_TIMEOUT;
1579 }
1580 }
1581
1582 /* Clear the Wakeup Timer clock source bits in CR register */
1583 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1584
1585 /* Configure the clock source */
1586 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1587
1588 /* Configure the Wakeup Timer counter */
1589 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1590
1591 /* Enable the Wakeup Timer */
1592 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1593
1594 /* Enable the write protection for RTC registers */
1595 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1596
1597 hrtc->State = HAL_RTC_STATE_READY;
1598
1599 /* Process Unlocked */
1600 __HAL_UNLOCK(hrtc);
1601
1602 return HAL_OK;
1603 }
1604
1605 /**
1606 * @brief Set wake up timer with interrupt.
1607 * @param hrtc: RTC handle
1608 * @param WakeUpCounter: Wake up counter
1609 * @param WakeUpClock: Wake up clock
1610 * @retval HAL status
1611 */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)1612 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1613 {
1614 uint32_t tickstart = 0;
1615
1616 /* Check the parameters */
1617 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1618 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1619
1620 /* Process Locked */
1621 __HAL_LOCK(hrtc);
1622
1623 hrtc->State = HAL_RTC_STATE_BUSY;
1624
1625 /* Disable the write protection for RTC registers */
1626 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1627
1628 /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
1629 if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET){
1630 tickstart = HAL_GetTick();
1631
1632 /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
1633 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET)
1634 {
1635 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1636 {
1637 /* Enable the write protection for RTC registers */
1638 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1639
1640 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1641
1642 /* Process Unlocked */
1643 __HAL_UNLOCK(hrtc);
1644
1645 return HAL_TIMEOUT;
1646 }
1647 }
1648 }
1649
1650 /* Disable the Wake-Up timer */
1651 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1652
1653 /* Clear flag Wake-Up */
1654 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1655
1656 tickstart = HAL_GetTick();
1657
1658 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1659 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1660 {
1661 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1662 {
1663 /* Enable the write protection for RTC registers */
1664 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1665
1666 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1667
1668 /* Process Unlocked */
1669 __HAL_UNLOCK(hrtc);
1670
1671 return HAL_TIMEOUT;
1672 }
1673 }
1674
1675 /* Configure the Wakeup Timer counter */
1676 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1677
1678 /* Clear the Wakeup Timer clock source bits in CR register */
1679 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1680
1681 /* Configure the clock source */
1682 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1683
1684 /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
1685 __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1686
1687 __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1688
1689 /* Configure the Interrupt in the RTC_CR register */
1690 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
1691
1692 /* Enable the Wakeup Timer */
1693 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1694
1695 /* Enable the write protection for RTC registers */
1696 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1697
1698 hrtc->State = HAL_RTC_STATE_READY;
1699
1700 /* Process Unlocked */
1701 __HAL_UNLOCK(hrtc);
1702
1703 return HAL_OK;
1704 }
1705
1706 /**
1707 * @brief Deactivates wake up timer counter.
1708 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1709 * the configuration information for RTC.
1710 * @retval HAL status
1711 */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)1712 uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1713 {
1714 uint32_t tickstart = 0;
1715
1716 /* Process Locked */
1717 __HAL_LOCK(hrtc);
1718
1719 hrtc->State = HAL_RTC_STATE_BUSY;
1720
1721 /* Disable the write protection for RTC registers */
1722 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1723
1724 /* Disable the Wakeup Timer */
1725 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1726
1727 /* In case of interrupt mode is used, the interrupt source must disabled */
1728 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
1729
1730 tickstart = HAL_GetTick();
1731 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1732 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1733 {
1734 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1735 {
1736 /* Enable the write protection for RTC registers */
1737 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1738
1739 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1740
1741 /* Process Unlocked */
1742 __HAL_UNLOCK(hrtc);
1743
1744 return HAL_TIMEOUT;
1745 }
1746 }
1747
1748 /* Enable the write protection for RTC registers */
1749 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1750
1751 hrtc->State = HAL_RTC_STATE_READY;
1752
1753 /* Process Unlocked */
1754 __HAL_UNLOCK(hrtc);
1755
1756 return HAL_OK;
1757 }
1758
1759 /**
1760 * @brief Gets wake up timer counter.
1761 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1762 * the configuration information for RTC.
1763 * @retval Counter value
1764 */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)1765 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1766 {
1767 /* Get the counter value */
1768 return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1769 }
1770
1771 /**
1772 * @brief This function handles Wake Up Timer interrupt request.
1773 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1774 * the configuration information for RTC.
1775 * @retval None
1776 */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)1777 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1778 {
1779 /* Get the pending status of the WAKEUPTIMER Interrupt */
1780 if(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != RESET)
1781 {
1782 /* WAKEUPTIMER callback */
1783 HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1784
1785 /* Clear the WAKEUPTIMER interrupt pending bit */
1786 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1787 }
1788
1789
1790 /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1791 __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1792
1793 /* Change RTC state */
1794 hrtc->State = HAL_RTC_STATE_READY;
1795 }
1796
1797 /**
1798 * @brief Wake Up Timer callback.
1799 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1800 * the configuration information for RTC.
1801 * @retval None
1802 */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)1803 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1804 {
1805 /* Prevent unused argument(s) compilation warning */
1806 UNUSED(hrtc);
1807
1808 /* NOTE : This function Should not be modified, when the callback is needed,
1809 the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1810 */
1811 }
1812
1813 /**
1814 * @brief This function handles Wake Up Timer Polling.
1815 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1816 * the configuration information for RTC.
1817 * @param Timeout: Timeout duration
1818 * @retval HAL status
1819 */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1820 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1821 {
1822 uint32_t tickstart = HAL_GetTick();
1823
1824 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
1825 {
1826 if(Timeout != HAL_MAX_DELAY)
1827 {
1828 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1829 {
1830 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1831
1832 return HAL_TIMEOUT;
1833 }
1834 }
1835 }
1836
1837 /* Clear the WAKEUPTIMER Flag */
1838 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1839
1840 /* Change RTC state */
1841 hrtc->State = HAL_RTC_STATE_READY;
1842
1843 return HAL_OK;
1844 }
1845
1846 /**
1847 * @}
1848 */
1849
1850 /** @defgroup RTCEx_Exported_Functions_Group7 Extended Peripheral Control functions
1851 * @brief Extended Peripheral Control functions
1852 *
1853 @verbatim
1854 ===============================================================================
1855 ##### Extension Peripheral Control functions #####
1856 ===============================================================================
1857 [..]
1858 This subsection provides functions allowing to
1859 (+) Writes a data in a specified RTC Backup data register
1860 (+) Read a data in a specified RTC Backup data register
1861 (+) Sets the Coarse calibration parameters.
1862 (+) Deactivates the Coarse calibration parameters
1863 (+) Sets the Smooth calibration parameters.
1864 (+) Configures the Synchronization Shift Control Settings.
1865 (+) Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1866 (+) Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1867 (+) Enables the RTC reference clock detection.
1868 (+) Disable the RTC reference clock detection.
1869 (+) Enables the Bypass Shadow feature.
1870 (+) Disables the Bypass Shadow feature.
1871
1872 @endverbatim
1873 * @{
1874 */
1875
1876 /**
1877 * @brief Writes a data in a specified RTC Backup data register.
1878 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1879 * the configuration information for RTC.
1880 * @param BackupRegister: RTC Backup data Register number.
1881 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1882 * specify the register.
1883 * @param Data: Data to be written in the specified RTC Backup data register.
1884 * @retval None
1885 */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)1886 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1887 {
1888 uint32_t tmp = 0;
1889
1890 /* Check the parameters */
1891 assert_param(IS_RTC_BKP(BackupRegister));
1892
1893 tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1894 tmp += (BackupRegister * 4);
1895
1896 /* Write the specified register */
1897 *(__IO uint32_t *)tmp = (uint32_t)Data;
1898 }
1899
1900 /**
1901 * @brief Reads data from the specified RTC Backup data Register.
1902 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1903 * the configuration information for RTC.
1904 * @param BackupRegister: RTC Backup data Register number.
1905 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1906 * specify the register.
1907 * @retval Read value
1908 */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)1909 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1910 {
1911 uint32_t tmp = 0;
1912
1913 /* Check the parameters */
1914 assert_param(IS_RTC_BKP(BackupRegister));
1915
1916 tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1917 tmp += (BackupRegister * 4);
1918
1919 /* Read the specified register */
1920 return (*(__IO uint32_t *)tmp);
1921 }
1922
1923 /**
1924 * @brief Sets the Coarse calibration parameters.
1925 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1926 * the configuration information for RTC.
1927 * @param CalibSign: Specifies the sign of the coarse calibration value.
1928 * This parameter can be one of the following values :
1929 * @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
1930 * @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
1931 * @param Value: value of coarse calibration expressed in ppm (coded on 5 bits).
1932 *
1933 * @note This Calibration value should be between 0 and 63 when using negative
1934 * sign with a 2-ppm step.
1935 *
1936 * @note This Calibration value should be between 0 and 126 when using positive
1937 * sign with a 4-ppm step.
1938 * @retval HAL status
1939 */
HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef * hrtc,uint32_t CalibSign,uint32_t Value)1940 HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef* hrtc, uint32_t CalibSign, uint32_t Value)
1941 {
1942 /* Check the parameters */
1943 assert_param(IS_RTC_CALIB_SIGN(CalibSign));
1944 assert_param(IS_RTC_CALIB_VALUE(Value));
1945
1946 /* Process Locked */
1947 __HAL_LOCK(hrtc);
1948
1949 hrtc->State = HAL_RTC_STATE_BUSY;
1950
1951 /* Disable the write protection for RTC registers */
1952 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1953
1954 /* Set Initialization mode */
1955 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1956 {
1957 /* Enable the write protection for RTC registers */
1958 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1959
1960 /* Set RTC state*/
1961 hrtc->State = HAL_RTC_STATE_ERROR;
1962
1963 /* Process Unlocked */
1964 __HAL_UNLOCK(hrtc);
1965
1966 return HAL_ERROR;
1967 }
1968 else
1969 {
1970 /* Enable the Coarse Calibration */
1971 __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
1972
1973 /* Set the coarse calibration value */
1974 hrtc->Instance->CALIBR = (uint32_t)(CalibSign|Value);
1975
1976 /* Exit Initialization mode */
1977 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1978 }
1979
1980 /* Enable the write protection for RTC registers */
1981 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1982
1983 /* Change state */
1984 hrtc->State = HAL_RTC_STATE_READY;
1985
1986 /* Process Unlocked */
1987 __HAL_UNLOCK(hrtc);
1988
1989 return HAL_OK;
1990 }
1991
1992 /**
1993 * @brief Deactivates the Coarse calibration parameters.
1994 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1995 * the configuration information for RTC.
1996 * @retval HAL status
1997 */
HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef * hrtc)1998 HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef* hrtc)
1999 {
2000 /* Process Locked */
2001 __HAL_LOCK(hrtc);
2002
2003 hrtc->State = HAL_RTC_STATE_BUSY;
2004
2005 /* Disable the write protection for RTC registers */
2006 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2007
2008 /* Set Initialization mode */
2009 if(RTC_EnterInitMode(hrtc) != HAL_OK)
2010 {
2011 /* Enable the write protection for RTC registers */
2012 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2013
2014 /* Set RTC state*/
2015 hrtc->State = HAL_RTC_STATE_ERROR;
2016
2017 /* Process Unlocked */
2018 __HAL_UNLOCK(hrtc);
2019
2020 return HAL_ERROR;
2021 }
2022 else
2023 {
2024 /* Enable the Coarse Calibration */
2025 __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
2026
2027 /* Exit Initialization mode */
2028 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
2029 }
2030
2031 /* Enable the write protection for RTC registers */
2032 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2033
2034 /* Change state */
2035 hrtc->State = HAL_RTC_STATE_READY;
2036
2037 /* Process Unlocked */
2038 __HAL_UNLOCK(hrtc);
2039
2040 return HAL_OK;
2041 }
2042
2043 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
2044 /**
2045 * @brief Sets the Smooth calibration parameters.
2046 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2047 * the configuration information for RTC.
2048 * @param SmoothCalibPeriod: Select the Smooth Calibration Period.
2049 * This parameter can be can be one of the following values :
2050 * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration periode is 32s.
2051 * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration periode is 16s.
2052 * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibartion periode is 8s.
2053 * @param SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
2054 * This parameter can be one of the following values:
2055 * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK puls every 2*11 pulses.
2056 * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
2057 * @param SmoothCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
2058 * This parameter can be one any value from 0 to 0x000001FF.
2059 * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
2060 * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
2061 * SmoothCalibMinusPulsesValue mut be equal to 0.
2062 * @retval HAL status
2063 */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)2064 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
2065 {
2066 uint32_t tickstart = 0;
2067
2068 /* Check the parameters */
2069 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
2070 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
2071 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
2072
2073 /* Process Locked */
2074 __HAL_LOCK(hrtc);
2075
2076 hrtc->State = HAL_RTC_STATE_BUSY;
2077
2078 /* Disable the write protection for RTC registers */
2079 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2080
2081 /* check if a calibration is pending*/
2082 if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
2083 {
2084 tickstart = HAL_GetTick();
2085
2086 /* check if a calibration is pending*/
2087 while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
2088 {
2089 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
2090 {
2091 /* Enable the write protection for RTC registers */
2092 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2093
2094 /* Change RTC state */
2095 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2096
2097 /* Process Unlocked */
2098 __HAL_UNLOCK(hrtc);
2099
2100 return HAL_TIMEOUT;
2101 }
2102 }
2103 }
2104
2105 /* Configure the Smooth calibration settings */
2106 hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmoothCalibMinusPulsesValue);
2107
2108 /* Enable the write protection for RTC registers */
2109 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2110
2111 /* Change RTC state */
2112 hrtc->State = HAL_RTC_STATE_READY;
2113
2114 /* Process Unlocked */
2115 __HAL_UNLOCK(hrtc);
2116
2117 return HAL_OK;
2118 }
2119
2120 /**
2121 * @brief Configures the Synchronization Shift Control Settings.
2122 * @note When REFCKON is set, firmware must not write to Shift control register.
2123 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2124 * the configuration information for RTC.
2125 * @param ShiftAdd1S: Select to add or not 1 second to the time calendar.
2126 * This parameter can be one of the following values :
2127 * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
2128 * @arg RTC_SHIFTADD1S_RESET: No effect.
2129 * @param ShiftSubFS: Select the number of Second Fractions to substitute.
2130 * This parameter can be one any value from 0 to 0x7FFF.
2131 * @retval HAL status
2132 */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)2133 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
2134 {
2135 uint32_t tickstart = 0;
2136
2137 /* Check the parameters */
2138 assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
2139 assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
2140
2141 /* Process Locked */
2142 __HAL_LOCK(hrtc);
2143
2144 hrtc->State = HAL_RTC_STATE_BUSY;
2145
2146 /* Disable the write protection for RTC registers */
2147 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2148
2149 tickstart = HAL_GetTick();
2150
2151 /* Wait until the shift is completed*/
2152 while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
2153 {
2154 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
2155 {
2156 /* Enable the write protection for RTC registers */
2157 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2158
2159 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2160
2161 /* Process Unlocked */
2162 __HAL_UNLOCK(hrtc);
2163
2164 return HAL_TIMEOUT;
2165 }
2166 }
2167
2168 /* Check if the reference clock detection is disabled */
2169 if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
2170 {
2171 /* Configure the Shift settings */
2172 hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
2173
2174 /* Wait for synchro */
2175 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2176 {
2177 /* Enable the write protection for RTC registers */
2178 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2179
2180 hrtc->State = HAL_RTC_STATE_ERROR;
2181
2182 /* Process Unlocked */
2183 __HAL_UNLOCK(hrtc);
2184
2185 return HAL_ERROR;
2186 }
2187 }
2188 else
2189 {
2190 /* Enable the write protection for RTC registers */
2191 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2192
2193 /* Change RTC state */
2194 hrtc->State = HAL_RTC_STATE_ERROR;
2195
2196 /* Process Unlocked */
2197 __HAL_UNLOCK(hrtc);
2198
2199 return HAL_ERROR;
2200 }
2201
2202 /* Enable the write protection for RTC registers */
2203 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2204
2205 /* Change RTC state */
2206 hrtc->State = HAL_RTC_STATE_READY;
2207
2208 /* Process Unlocked */
2209 __HAL_UNLOCK(hrtc);
2210
2211 return HAL_OK;
2212 }
2213 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
2214
2215
2216 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
2217 /**
2218 * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
2219 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2220 * the configuration information for RTC.
2221 * @param CalibOutput : Select the Calibration output Selection .
2222 * This parameter can be one of the following values:
2223 * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
2224 * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
2225 * @retval HAL status
2226 */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)2227 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
2228 #else
2229 /**
2230 * @brief Configure the Calibration Pinout (RTC_CALIB).
2231 * @param hrtc : RTC handle
2232 * @retval HAL status
2233 */
2234 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc)
2235 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
2236 {
2237 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
2238 /* Check the parameters */
2239 assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
2240 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
2241
2242 /* Process Locked */
2243 __HAL_LOCK(hrtc);
2244
2245 hrtc->State = HAL_RTC_STATE_BUSY;
2246
2247 /* Disable the write protection for RTC registers */
2248 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2249
2250 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
2251 /* Clear flags before config */
2252 hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
2253
2254 /* Configure the RTC_CR register */
2255 hrtc->Instance->CR |= (uint32_t)CalibOutput;
2256 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
2257
2258 __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
2259
2260 /* Enable the write protection for RTC registers */
2261 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2262
2263 /* Change RTC state */
2264 hrtc->State = HAL_RTC_STATE_READY;
2265
2266 /* Process Unlocked */
2267 __HAL_UNLOCK(hrtc);
2268
2269 return HAL_OK;
2270 }
2271
2272 /**
2273 * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
2274 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2275 * the configuration information for RTC.
2276 * @retval HAL status
2277 */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)2278 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
2279 {
2280 /* Process Locked */
2281 __HAL_LOCK(hrtc);
2282
2283 hrtc->State = HAL_RTC_STATE_BUSY;
2284
2285 /* Disable the write protection for RTC registers */
2286 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2287
2288 __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
2289
2290 /* Enable the write protection for RTC registers */
2291 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2292
2293 /* Change RTC state */
2294 hrtc->State = HAL_RTC_STATE_READY;
2295
2296 /* Process Unlocked */
2297 __HAL_UNLOCK(hrtc);
2298
2299 return HAL_OK;
2300 }
2301
2302 /**
2303 * @brief Enables the RTC reference clock detection.
2304 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2305 * the configuration information for RTC.
2306 * @retval HAL status
2307 */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)2308 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
2309 {
2310 /* Process Locked */
2311 __HAL_LOCK(hrtc);
2312
2313 hrtc->State = HAL_RTC_STATE_BUSY;
2314
2315 /* Disable the write protection for RTC registers */
2316 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2317
2318 /* Set Initialization mode */
2319 if(RTC_EnterInitMode(hrtc) != HAL_OK)
2320 {
2321 /* Enable the write protection for RTC registers */
2322 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2323
2324 /* Set RTC state*/
2325 hrtc->State = HAL_RTC_STATE_ERROR;
2326
2327 /* Process Unlocked */
2328 __HAL_UNLOCK(hrtc);
2329
2330 return HAL_ERROR;
2331 }
2332 else
2333 {
2334 __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
2335
2336 /* Exit Initialization mode */
2337 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
2338 }
2339
2340 /* Enable the write protection for RTC registers */
2341 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2342
2343 /* Change RTC state */
2344 hrtc->State = HAL_RTC_STATE_READY;
2345
2346 /* Process Unlocked */
2347 __HAL_UNLOCK(hrtc);
2348
2349 return HAL_OK;
2350 }
2351
2352 /**
2353 * @brief Disable the RTC reference clock detection.
2354 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2355 * the configuration information for RTC.
2356 * @retval HAL status
2357 */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)2358 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
2359 {
2360 /* Process Locked */
2361 __HAL_LOCK(hrtc);
2362
2363 hrtc->State = HAL_RTC_STATE_BUSY;
2364
2365 /* Disable the write protection for RTC registers */
2366 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2367
2368 /* Set Initialization mode */
2369 if(RTC_EnterInitMode(hrtc) != HAL_OK)
2370 {
2371 /* Enable the write protection for RTC registers */
2372 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2373
2374 /* Set RTC state*/
2375 hrtc->State = HAL_RTC_STATE_ERROR;
2376
2377 /* Process Unlocked */
2378 __HAL_UNLOCK(hrtc);
2379
2380 return HAL_ERROR;
2381 }
2382 else
2383 {
2384 __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
2385
2386 /* Exit Initialization mode */
2387 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
2388 }
2389
2390 /* Enable the write protection for RTC registers */
2391 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2392
2393 /* Change RTC state */
2394 hrtc->State = HAL_RTC_STATE_READY;
2395
2396 /* Process Unlocked */
2397 __HAL_UNLOCK(hrtc);
2398
2399 return HAL_OK;
2400 }
2401
2402 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
2403 /**
2404 * @brief Enables the Bypass Shadow feature.
2405 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2406 * the configuration information for RTC.
2407 * @note When the Bypass Shadow is enabled the calendar value are taken
2408 * directly from the Calendar counter.
2409 * @retval HAL status
2410 */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)2411 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
2412 {
2413 /* Process Locked */
2414 __HAL_LOCK(hrtc);
2415
2416 hrtc->State = HAL_RTC_STATE_BUSY;
2417
2418 /* Disable the write protection for RTC registers */
2419 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2420
2421 /* Set the BYPSHAD bit */
2422 hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
2423
2424 /* Enable the write protection for RTC registers */
2425 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2426
2427 /* Change RTC state */
2428 hrtc->State = HAL_RTC_STATE_READY;
2429
2430 /* Process Unlocked */
2431 __HAL_UNLOCK(hrtc);
2432
2433 return HAL_OK;
2434 }
2435
2436 /**
2437 * @brief Disables the Bypass Shadow feature.
2438 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2439 * the configuration information for RTC.
2440 * @note When the Bypass Shadow is enabled the calendar value are taken
2441 * directly from the Calendar counter.
2442 * @retval HAL status
2443 */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)2444 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
2445 {
2446 /* Process Locked */
2447 __HAL_LOCK(hrtc);
2448
2449 hrtc->State = HAL_RTC_STATE_BUSY;
2450
2451 /* Disable the write protection for RTC registers */
2452 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2453
2454 /* Reset the BYPSHAD bit */
2455 hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
2456
2457 /* Enable the write protection for RTC registers */
2458 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2459
2460 /* Change RTC state */
2461 hrtc->State = HAL_RTC_STATE_READY;
2462
2463 /* Process Unlocked */
2464 __HAL_UNLOCK(hrtc);
2465
2466 return HAL_OK;
2467 }
2468 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
2469
2470 /**
2471 * @}
2472 */
2473
2474 /** @defgroup RTCEx_Exported_Functions_Group8 Extended features functions
2475 * @brief Extended features functions
2476 *
2477 @verbatim
2478 ===============================================================================
2479 ##### Extended features functions #####
2480 ===============================================================================
2481 [..] This section provides functions allowing to:
2482 (+) RTC Alram B callback
2483 (+) RTC Poll for Alarm B request
2484
2485 @endverbatim
2486 * @{
2487 */
2488
2489 /**
2490 * @brief Alarm B callback.
2491 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2492 * the configuration information for RTC.
2493 * @retval None
2494 */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)2495 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
2496 {
2497 /* Prevent unused argument(s) compilation warning */
2498 UNUSED(hrtc);
2499
2500 /* NOTE : This function Should not be modified, when the callback is needed,
2501 the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
2502 */
2503 }
2504
2505 /**
2506 * @brief This function handles AlarmB Polling request.
2507 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
2508 * the configuration information for RTC.
2509 * @param Timeout: Timeout duration
2510 * @retval HAL status
2511 */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)2512 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
2513 {
2514 uint32_t tickstart = HAL_GetTick();
2515
2516 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
2517 {
2518 if(Timeout != HAL_MAX_DELAY)
2519 {
2520 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
2521 {
2522 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2523 return HAL_TIMEOUT;
2524 }
2525 }
2526 }
2527
2528 /* Clear the Alarm Flag */
2529 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
2530
2531 /* Change RTC state */
2532 hrtc->State = HAL_RTC_STATE_READY;
2533
2534 return HAL_OK;
2535 }
2536
2537 /**
2538 * @}
2539 */
2540
2541 /**
2542 * @}
2543 */
2544
2545 /**
2546 * @}
2547 */
2548
2549 #endif /* HAL_RTC_MODULE_ENABLED */
2550
2551 /**
2552 * @}
2553 */
2554
2555 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2556