1 /**
2 ******************************************************************************
3 * @file stm32l0xx_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) Extended peripheral:
8 * + RTC Timestamp functions
9 * + RTC Tamper functions
10 * + RTC Wakeup functions
11 * + Extended Control functions
12 * + Extended RTC features functions
13 *
14 ******************************************************************************
15 * @attention
16 *
17 * Copyright (c) 2016 STMicroelectronics.
18 * All rights reserved.
19 *
20 * This software is licensed under terms that can be found in the LICENSE file
21 * in the root directory of this software component.
22 * If no LICENSE file comes with this software, it is provided AS-IS.
23 *
24 ******************************************************************************
25 @verbatim
26 ==============================================================================
27 ##### How to use this driver #####
28 ==============================================================================
29 [..]
30 (+) Enable the RTC domain access.
31 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
32 format using the HAL_RTC_Init() function.
33
34 *** RTC Wakeup configuration ***
35 ================================
36 [..]
37 (+) To configure the RTC Wakeup Clock source and Counter use the
38 HAL_RTCEx_SetWakeUpTimer() function.
39 You can also configure the RTC Wakeup timer in interrupt mode using the
40 HAL_RTCEx_SetWakeUpTimer_IT() function.
41 (+) To read the RTC Wakeup Counter register, use the HAL_RTCEx_GetWakeUpTimer()
42 function.
43
44 *** Timestamp configuration ***
45 ===============================
46 [..]
47 (+) To configure the RTC Timestamp use the HAL_RTCEx_SetTimeStamp() function.
48 You can also configure the RTC Timestamp with interrupt mode using the
49 HAL_RTCEx_SetTimeStamp_IT() function.
50 (+) To read the RTC Timestamp Time and Date register, use the
51 HAL_RTCEx_GetTimeStamp() function.
52 (+) The Timestamp alternate function is mapped to RTC_AF1 (PC13).
53
54 *** Tamper configuration ***
55 ============================
56 [..]
57 (+) To Enable the RTC Tamper and configure the Tamper filter count, trigger
58 Edge or Level according to the Tamper filter value (if equal to 0 Edge
59 else Level), sampling frequency, NoErase, MaskFlag, precharge or
60 discharge and Pull-UP use the HAL_RTCEx_SetTamper() function.
61 You can configure RTC Tamper in interrupt mode using HAL_RTCEx_SetTamper_IT()
62 function.
63 (+) The default configuration of the Tamper erases the backup registers.
64 To avoid this, enable the NoErase field on the RTC_TAMPCR register.
65 (+) The TAMPER1 alternate function is mapped to RTC_AF1 (PC13).
66 (+) The TAMPER2 alternate function is mapped to RTC_AF2 (PA0).
67 (+) The TAMPER3 alternate function is mapped to RTC_AF3 (PE6).
68
69 *** Backup Data Registers configuration ***
70 ===========================================
71 [..]
72 (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
73 function.
74 (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
75 function.
76
77 *** Smooth Digital Calibration configuration ***
78 ================================================
79 [..]
80 (+) RTC frequency can be digitally calibrated with a resolution of about
81 0.954 ppm with a range from -487.1 ppm to +488.5 ppm.
82 The correction of the frequency is performed using a series of small
83 adjustments (adding and/or subtracting individual RTCCLK pulses).
84 (+) The smooth digital calibration is performed during a cycle of about 2^20
85 RTCCLK pulses (or 32 seconds) when the input frequency is 32,768 Hz.
86 This cycle is maintained by a 20-bit counter clocked by RTCCLK.
87 (+) The smooth calibration register (RTC_CALR) specifies the number of RTCCLK
88 clock cycles to be masked during the 32-second cycle.
89 (+) The RTC Smooth Digital Calibration value and the corresponding calibration
90 cycle period (32s, 16s, or 8s) can be calibrated using the
91 HAL_RTCEx_SetSmoothCalib() function.
92
93 *** Outputs configuration ***
94 =============================
95 [..] The RTC has 2 different outputs:
96 (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
97 and WaKeUp signals.
98 To output the selected RTC signal, use the HAL_RTC_Init() function.
99 (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
100 To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
101 (+) Two pins can be used as RTC_ALARM or RTC_CALIB output, selected through
102 bit OUT_RMP of the RTC_OR register:
103 - (PC13, PB14) for STM32L05x/6x/7x/8x
104 - (PA2, PB14) for STM32L03x/4x
105 (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
106 automatically configured in output alternate function.
107
108 @endverbatim
109 ******************************************************************************
110 */
111
112 /* Includes ------------------------------------------------------------------*/
113 #include "stm32l0xx_hal.h"
114
115 /** @addtogroup STM32L0xx_HAL_Driver
116 * @{
117 */
118
119 /** @defgroup RTCEx RTCEx
120 * @brief RTC Extended HAL module driver
121 * @{
122 */
123
124 #ifdef HAL_RTC_MODULE_ENABLED
125
126 /* Private typedef -----------------------------------------------------------*/
127 /* Private define ------------------------------------------------------------*/
128 /* Private macro -------------------------------------------------------------*/
129 /* Private variables ---------------------------------------------------------*/
130 /* Private function prototypes -----------------------------------------------*/
131 /* Exported functions --------------------------------------------------------*/
132
133 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
134 * @{
135 */
136
137 /** @defgroup RTCEx_Exported_Functions_Group1 RTC Timestamp and Tamper functions
138 * @brief RTC Timestamp and Tamper functions
139 *
140 @verbatim
141 ===============================================================================
142 ##### RTC Timestamp and Tamper functions #####
143 ===============================================================================
144
145 [..] This section provides functions allowing to configure Timestamp feature
146
147 @endverbatim
148 * @{
149 */
150
151 /**
152 * @brief Sets Timestamp.
153 * @note This API must be called before enabling the Timestamp feature.
154 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
155 * the configuration information for RTC.
156 * @param RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
157 * activated.
158 * This parameter can be one of the following values:
159 * @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
160 * the rising edge of the related pin.
161 * @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
162 * the falling edge of the related pin.
163 * @param RTC_TimeStampPin Specifies the RTC Timestamp Pin.
164 * This parameter can be one of the following values:
165 * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC Timestamp Pin.
166 * @note RTC_TIMESTAMPPIN_DEFAULT corresponds to pin:
167 * - PC13 in the case of STM32L05x/6x/7x/8x devices
168 * - PA2 in the case of STM32L01x/2x/3x/4x devices
169 * @note Although unused, parameter RTC_TimeStampPin has been kept for portability
170 * reasons.
171 * @retval HAL status
172 */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t RTC_TimeStampEdge,uint32_t RTC_TimeStampPin)173 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
174 {
175 uint32_t tmpreg = 0U;
176
177 /* Check the parameters */
178 assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
179 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
180
181 /* Prevent compilation warning due to unused argument(s) if assert_param check
182 is disabled */
183 UNUSED(RTC_TimeStampPin);
184
185 /* Process Locked */
186 __HAL_LOCK(hrtc);
187
188 /* Change RTC state to BUSY */
189 hrtc->State = HAL_RTC_STATE_BUSY;
190
191 /* Get the RTC_CR register and clear the bits to be configured */
192 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
193
194 /* Configure the Timestamp TSEDGE bit */
195 tmpreg |= RTC_TimeStampEdge;
196
197 /* Disable the write protection for RTC registers */
198 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
199
200 /* Copy the desired configuration into the CR register */
201 hrtc->Instance->CR = (uint32_t)tmpreg;
202
203 /* Clear RTC Timestamp flag */
204 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
205
206 /* Clear RTC Timestamp overrun Flag */
207 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
208
209 /* Enable the Timestamp saving */
210 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
211
212 /* Enable the write protection for RTC registers */
213 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
214
215 /* Change RTC state back to READY */
216 hrtc->State = HAL_RTC_STATE_READY;
217
218 /* Process Unlocked */
219 __HAL_UNLOCK(hrtc);
220
221 return HAL_OK;
222 }
223
224 /**
225 * @brief Sets Timestamp with Interrupt.
226 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
227 * the configuration information for RTC.
228 * @note This API must be called before enabling the Timestamp feature.
229 * @param RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
230 * activated.
231 * This parameter can be one of the following values:
232 * @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
233 * the rising edge of the related pin.
234 * @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
235 * the falling edge of the related pin.
236 * @param RTC_TimeStampPin Specifies the RTC Timestamp Pin.
237 * This parameter can be one of the following values:
238 * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC Timestamp Pin.
239 * @note RTC_TIMESTAMPPIN_DEFAULT corresponds to pin:
240 * - PC13 in the case of STM32L05x/6x/7x/8x devices
241 * - PA2 in the case of STM32L01x/2x/3x/4x devices
242 * @note Although unused, parameter RTC_TimeStampPin has been kept for portability
243 * reasons.
244 * @retval HAL status
245 */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t RTC_TimeStampEdge,uint32_t RTC_TimeStampPin)246 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
247 {
248 uint32_t tmpreg = 0U;
249
250 /* Check the parameters */
251 assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
252 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
253
254 /* Prevent compilation warning due to unused argument(s) if assert_param check
255 is disabled */
256 UNUSED(RTC_TimeStampPin);
257
258 /* Process Locked */
259 __HAL_LOCK(hrtc);
260
261 /* Change RTC state to BUSY */
262 hrtc->State = HAL_RTC_STATE_BUSY;
263
264 /* Get the RTC_CR register and clear the bits to be configured */
265 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
266
267 /* Configure the Timestamp TSEDGE bit */
268 tmpreg |= RTC_TimeStampEdge;
269
270 /* Disable the write protection for RTC registers */
271 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
272
273 /* Copy the desired configuration into the CR register */
274 hrtc->Instance->CR = (uint32_t)tmpreg;
275
276 /* Clear RTC Timestamp flag */
277 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
278
279 /* Clear RTC Timestamp overrun Flag */
280 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
281
282 /* Enable the Timestamp saving */
283 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
284
285 /* Enable IT Timestamp */
286 __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
287
288 /* Enable the write protection for RTC registers */
289 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
290
291 /* RTC Timestamp Interrupt Configuration: EXTI configuration */
292 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
293 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
294
295 /* Change RTC state back to READY */
296 hrtc->State = HAL_RTC_STATE_READY;
297
298 /* Process Unlocked */
299 __HAL_UNLOCK(hrtc);
300
301 return HAL_OK;
302 }
303
304 /**
305 * @brief Deactivates Timestamp.
306 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
307 * the configuration information for RTC.
308 * @retval HAL status
309 */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)310 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
311 {
312 uint32_t tmpreg = 0U;
313
314 /* Process Locked */
315 __HAL_LOCK(hrtc);
316
317 hrtc->State = HAL_RTC_STATE_BUSY;
318
319 /* Disable the write protection for RTC registers */
320 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
321
322 /* In case of interrupt mode is used, the interrupt source must disabled */
323 __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
324
325 /* Get the RTC_CR register and clear the bits to be configured */
326 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
327
328 /* Configure the Timestamp TSEDGE and Enable bits */
329 hrtc->Instance->CR = (uint32_t)tmpreg;
330
331 /* Enable the write protection for RTC registers */
332 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
333
334 hrtc->State = HAL_RTC_STATE_READY;
335
336 /* Process Unlocked */
337 __HAL_UNLOCK(hrtc);
338
339 return HAL_OK;
340 }
341
342 /**
343 * @brief Gets the RTC Timestamp value.
344 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
345 * the configuration information for RTC.
346 * @param sTimeStamp Pointer to Time structure
347 * @param sTimeStampDate Pointer to Date structure
348 * @param Format specifies the format of the entered parameters.
349 * This parameter can be one of the following values:
350 * @arg RTC_FORMAT_BIN: Binary data format
351 * @arg RTC_FORMAT_BCD: BCD data format
352 * @retval HAL status
353 */
HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)354 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
355 {
356 uint32_t tmptime = 0U;
357 uint32_t tmpdate = 0U;
358
359 /* Check the parameters */
360 assert_param(IS_RTC_FORMAT(Format));
361
362 /* Get the Timestamp time and date registers values */
363 tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
364 tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
365
366 /* Fill the Time structure fields with the read parameters */
367 sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
368 sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
369 sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
370 sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
371 sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
372
373 /* Fill the Date structure fields with the read parameters */
374 sTimeStampDate->Year = 0U;
375 sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
376 sTimeStampDate->Date = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
377 sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
378
379 /* Check the input parameters format */
380 if (Format == RTC_FORMAT_BIN)
381 {
382 /* Convert the Timestamp structure parameters to Binary format */
383 sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
384 sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
385 sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
386
387 /* Convert the DateTimeStamp structure parameters to Binary format */
388 sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
389 sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
390 sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
391 }
392
393 /* Clear the Timestamp Flag */
394 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
395
396 return HAL_OK;
397 }
398
399 /**
400 * @brief Sets Tamper.
401 * @note By calling this API the tamper global interrupt will be disabled and
402 * the selected tamper's interrupt as well.
403 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
404 * the configuration information for RTC.
405 * @param sTamper Pointer to Tamper Structure.
406 * @retval HAL status
407 */
HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)408 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
409 {
410 uint32_t tmpreg = 0U;
411
412 /* Check the parameters */
413 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
414 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
415 assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
416 assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
417 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
418 assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
419 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
420 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
421 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
422 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
423
424 /* Process Locked */
425 __HAL_LOCK(hrtc);
426
427 hrtc->State = HAL_RTC_STATE_BUSY;
428
429 /* Copy control register into temporary variable */
430 tmpreg = hrtc->Instance->TAMPCR;
431
432 /* Enable selected tamper */
433 tmpreg |= (sTamper->Tamper);
434
435 /* Configure the tamper trigger bit (this bit is just on the right of the
436 tamper enable bit, hence the one-time right shift before updating it) */
437 if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
438 {
439 /* Set the tamper trigger bit (case of falling edge or high level) */
440 tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
441 }
442 else
443 {
444 /* Clear the tamper trigger bit (case of rising edge or low level) */
445 tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
446 }
447
448 /* Configure the backup registers erasure enabling bits */
449 if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
450 {
451 #if defined(RTC_TAMPER1_SUPPORT)
452 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
453 {
454 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1NOERASE);
455 }
456 #endif /* RTC_TAMPER1_SUPPORT */
457 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
458 {
459 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2NOERASE);
460 }
461 #if defined(RTC_TAMPER3_SUPPORT)
462 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
463 {
464 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3NOERASE);
465 }
466 #endif /* RTC_TAMPER3_SUPPORT */
467 }
468 else
469 {
470 #if defined(RTC_TAMPER1_SUPPORT)
471 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
472 {
473 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1NOERASE);
474 }
475 #endif /* RTC_TAMPER1_SUPPORT */
476 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
477 {
478 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2NOERASE);
479 }
480 #if defined(RTC_TAMPER3_SUPPORT)
481 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
482 {
483 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3NOERASE);
484 }
485 #endif /* RTC_TAMPER3_SUPPORT */
486 }
487
488 /* Configure the tamper flags masking bits */
489 if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
490 {
491 #if defined(RTC_TAMPER1_SUPPORT)
492 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
493 {
494 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1MF);
495 }
496 #endif /* RTC_TAMPER1_SUPPORT */
497 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
498 {
499 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2MF);
500 }
501 #if defined(RTC_TAMPER3_SUPPORT)
502 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
503 {
504 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3MF);
505 }
506 #endif /* RTC_TAMPER3_SUPPORT */
507 }
508 else
509 {
510 #if defined(RTC_TAMPER1_SUPPORT)
511 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
512 {
513 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1MF);
514 }
515 #endif /* RTC_TAMPER1_SUPPORT */
516 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
517 {
518 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2MF);
519 }
520 #if defined(RTC_TAMPER3_SUPPORT)
521 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
522 {
523 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3MF);
524 }
525 #endif /* RTC_TAMPER3_SUPPORT */
526 }
527
528 /* Clear remaining fields before setting them */
529 tmpreg &= ~(RTC_TAMPERFILTER_MASK | \
530 RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
531 RTC_TAMPERPRECHARGEDURATION_MASK | \
532 RTC_TAMPER_PULLUP_MASK | \
533 RTC_TIMESTAMPONTAMPERDETECTION_MASK);
534
535 /* Set remaining parameters of desired configuration into temporary variable */
536 tmpreg |= ((uint32_t)sTamper->Filter | \
537 (uint32_t)sTamper->SamplingFrequency | \
538 (uint32_t)sTamper->PrechargeDuration | \
539 (uint32_t)sTamper->TamperPullUp | \
540 (uint32_t)sTamper->TimeStampOnTamperDetection);
541
542 /* Disable interrupt on selected tamper in case it is enabled */
543 #if defined(RTC_TAMPER1_SUPPORT)
544 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
545 {
546 tmpreg &= (uint32_t)~RTC_IT_TAMP1;
547 }
548 #endif /* RTC_TAMPER1_SUPPORT */
549 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
550 {
551 tmpreg &= (uint32_t)~RTC_IT_TAMP2;
552 }
553 #if defined(RTC_TAMPER3_SUPPORT)
554 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
555 {
556 tmpreg &= (uint32_t)~RTC_IT_TAMP3;
557 }
558 #endif /* RTC_TAMPER3_SUPPORT */
559
560 /* Disable tamper global interrupt in case it is enabled */
561 tmpreg &= (uint32_t)~RTC_TAMPCR_TAMPIE;
562
563 /* Copy desired configuration into configuration register */
564 hrtc->Instance->TAMPCR = tmpreg;
565
566 hrtc->State = HAL_RTC_STATE_READY;
567
568 /* Process Unlocked */
569 __HAL_UNLOCK(hrtc);
570
571 return HAL_OK;
572 }
573
574 /**
575 * @brief Sets Tamper with interrupt.
576 * @note By setting the tamper global interrupt bit, interrupts will be
577 * enabled for all tampers.
578 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
579 * the configuration information for RTC.
580 * @param sTamper Pointer to RTC Tamper.
581 * @retval HAL status
582 */
HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)583 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
584 {
585 uint32_t tmpreg = 0U;
586
587 /* Check the parameters */
588 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
589 assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
590 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
591 assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
592 assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
593 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
594 assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
595 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
596 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
597 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
598 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
599
600 /* Process Locked */
601 __HAL_LOCK(hrtc);
602
603 hrtc->State = HAL_RTC_STATE_BUSY;
604
605 /* Copy control register into temporary variable */
606 tmpreg = hrtc->Instance->TAMPCR;
607
608 /* Enable selected tamper */
609 tmpreg |= (sTamper->Tamper);
610
611 /* Configure the tamper trigger bit (this bit is just on the right of the
612 tamper enable bit, hence the one-time right shift before updating it) */
613 if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
614 {
615 /* Set the tamper trigger bit (case of falling edge or high level) */
616 tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
617 }
618 else
619 {
620 /* Clear the tamper trigger bit (case of rising edge or low level) */
621 tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
622 }
623
624 /* Configure the backup registers erasure enabling bits */
625 if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
626 {
627 #if defined(RTC_TAMPER1_SUPPORT)
628 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
629 {
630 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1NOERASE);
631 }
632 #endif /* RTC_TAMPER1_SUPPORT */
633 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
634 {
635 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2NOERASE);
636 }
637 #if defined(RTC_TAMPER3_SUPPORT)
638 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
639 {
640 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3NOERASE);
641 }
642 #endif /* RTC_TAMPER3_SUPPORT */
643 }
644 else
645 {
646 #if defined(RTC_TAMPER1_SUPPORT)
647 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
648 {
649 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1NOERASE);
650 }
651 #endif /* RTC_TAMPER1_SUPPORT */
652 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
653 {
654 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2NOERASE);
655 }
656 #if defined(RTC_TAMPER3_SUPPORT)
657 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
658 {
659 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3NOERASE);
660 }
661 #endif /* RTC_TAMPER3_SUPPORT */
662 }
663
664 /* Configure the tamper flags masking bits */
665 if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
666 {
667 #if defined(RTC_TAMPER1_SUPPORT)
668 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
669 {
670 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1MF);
671 }
672 #endif /* RTC_TAMPER1_SUPPORT */
673 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
674 {
675 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2MF);
676 }
677 #if defined(RTC_TAMPER3_SUPPORT)
678 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
679 {
680 tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3MF);
681 }
682 #endif /* RTC_TAMPER3_SUPPORT */
683 }
684 else
685 {
686 #if defined(RTC_TAMPER1_SUPPORT)
687 if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
688 {
689 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1MF);
690 }
691 #endif /* RTC_TAMPER1_SUPPORT */
692 if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
693 {
694 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2MF);
695 }
696 #if defined(RTC_TAMPER3_SUPPORT)
697 if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
698 {
699 tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3MF);
700 }
701 #endif /* RTC_TAMPER3_SUPPORT */
702 }
703
704 /* Clear remaining fields before setting them */
705 tmpreg &= ~(RTC_TAMPERFILTER_MASK | \
706 RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
707 RTC_TAMPERPRECHARGEDURATION_MASK | \
708 RTC_TAMPER_PULLUP_MASK | \
709 RTC_TIMESTAMPONTAMPERDETECTION_MASK);
710
711 /* Set remaining parameters of desired configuration into temporary variable */
712 tmpreg |= ((uint32_t)sTamper->Filter | \
713 (uint32_t)sTamper->SamplingFrequency | \
714 (uint32_t)sTamper->PrechargeDuration | \
715 (uint32_t)sTamper->TamperPullUp | \
716 (uint32_t)sTamper->TimeStampOnTamperDetection);
717
718 /* Enable interrupt on selected tamper */
719 tmpreg |= (uint32_t)sTamper->Interrupt;
720
721 /* Copy desired configuration into configuration register */
722 hrtc->Instance->TAMPCR = tmpreg;
723
724 /* RTC Tamper Interrupt Configuration: EXTI configuration */
725 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
726 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
727
728 hrtc->State = HAL_RTC_STATE_READY;
729
730 /* Process Unlocked */
731 __HAL_UNLOCK(hrtc);
732
733 return HAL_OK;
734 }
735
736 /**
737 * @brief Deactivates Tamper.
738 * @note By calling this API the tamper global interrupt will be disabled.
739 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
740 * the configuration information for RTC.
741 * @param Tamper Selected tamper pin.
742 * This parameter can be any combination of the following values:
743 * @arg RTC_TAMPER_1: Tamper 1
744 * @arg RTC_TAMPER_2: Tamper 2
745 * @arg RTC_TAMPER_3: Tamper 3
746 * @note RTC_TAMPER_1 is not applicable to all devices.
747 * @note RTC_TAMPER_3 is not applicable to all devices.
748 * @retval HAL status
749 */
HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc,uint32_t Tamper)750 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
751 {
752 assert_param(IS_RTC_TAMPER(Tamper));
753
754 /* Process Locked */
755 __HAL_LOCK(hrtc);
756
757 hrtc->State = HAL_RTC_STATE_BUSY;
758
759 /* Disable the selected Tamper pin */
760 hrtc->Instance->TAMPCR &= (uint32_t)~Tamper;
761
762 #if defined(RTC_TAMPER1_SUPPORT)
763 if ((Tamper & RTC_TAMPER_1) != 0U)
764 {
765 /* Disable the Tamper 1 interrupt */
766 hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1);
767 }
768 #endif /* RTC_TAMPER1_SUPPORT */
769 if ((Tamper & RTC_TAMPER_2) != 0U)
770 {
771 /* Disable the Tamper 2 interrupt */
772 hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2);
773 }
774 #if defined(RTC_TAMPER3_SUPPORT)
775 if ((Tamper & RTC_TAMPER_3) != 0U)
776 {
777 /* Disable the Tamper 3 interrupt */
778 hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3);
779 }
780 #endif /* RTC_TAMPER3_SUPPORT */
781
782 hrtc->State = HAL_RTC_STATE_READY;
783
784 /* Process Unlocked */
785 __HAL_UNLOCK(hrtc);
786
787 return HAL_OK;
788 }
789
790 /**
791 * @brief Handles Timestamp and Tamper interrupt request.
792 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
793 * the configuration information for RTC.
794 * @retval None
795 */
HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef * hrtc)796 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
797 {
798 /* Clear the EXTI's Flag for RTC Timestamp and Tamper */
799 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
800
801 /* Get the Timestamp interrupt source enable status */
802 if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
803 {
804 /* Get the pending status of the Timestamp Interrupt */
805 if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
806 {
807 /* Timestamp callback */
808 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
809 hrtc->TimeStampEventCallback(hrtc);
810 #else
811 HAL_RTCEx_TimeStampEventCallback(hrtc);
812 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
813
814 /* Clear the Timestamp interrupt pending bit after returning from callback
815 as RTC_TSTR and RTC_TSDR registers are cleared when TSF bit is reset */
816 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
817 }
818 }
819
820 #if defined(RTC_TAMPER1_SUPPORT)
821 /* Get the Tamper 1 interrupt source enable status */
822 if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != 0U)
823 {
824 /* Get the pending status of the Tamper 1 Interrupt */
825 if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
826 {
827 /* Clear the Tamper interrupt pending bit */
828 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
829
830 /* Tamper callback */
831 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
832 hrtc->Tamper1EventCallback(hrtc);
833 #else
834 HAL_RTCEx_Tamper1EventCallback(hrtc);
835 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
836 }
837 }
838 #endif /* RTC_TAMPER1_SUPPORT */
839
840 /* Get the Tamper 2 interrupt source enable status */
841 if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != 0U)
842 {
843 /* Get the pending status of the Tamper 2 Interrupt */
844 if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
845 {
846 /* Clear the Tamper interrupt pending bit */
847 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
848
849 /* Tamper callback */
850 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
851 hrtc->Tamper2EventCallback(hrtc);
852 #else
853 HAL_RTCEx_Tamper2EventCallback(hrtc);
854 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
855 }
856 }
857
858 #if defined(RTC_TAMPER3_SUPPORT)
859 /* Get the Tamper 3 interrupt source enable status */
860 if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != 0U)
861 {
862 /* Get the pending status of the Tamper 3 Interrupt */
863 if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
864 {
865 /* Clear the Tamper interrupt pending bit */
866 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
867
868 /* Tamper callback */
869 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
870 hrtc->Tamper3EventCallback(hrtc);
871 #else
872 HAL_RTCEx_Tamper3EventCallback(hrtc);
873 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
874 }
875 }
876 #endif /* RTC_TAMPER3_SUPPORT */
877
878 /* Change RTC state */
879 hrtc->State = HAL_RTC_STATE_READY;
880 }
881
882 /**
883 * @brief Timestamp callback.
884 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
885 * the configuration information for RTC.
886 * @retval None
887 */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)888 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
889 {
890 /* Prevent unused argument(s) compilation warning */
891 UNUSED(hrtc);
892
893 /* NOTE: This function should not be modified, when the callback is needed,
894 the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
895 */
896 }
897
898 #if defined(RTC_TAMPER1_SUPPORT)
899 /**
900 * @brief Tamper 1 callback.
901 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
902 * the configuration information for RTC.
903 * @retval None
904 */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)905 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
906 {
907 /* Prevent unused argument(s) compilation warning */
908 UNUSED(hrtc);
909
910 /* NOTE: This function should not be modified, when the callback is needed,
911 the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
912 */
913 }
914 #endif /* RTC_TAMPER1_SUPPORT */
915
916 /**
917 * @brief Tamper 2 callback.
918 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
919 * the configuration information for RTC.
920 * @retval None
921 */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)922 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
923 {
924 /* Prevent unused argument(s) compilation warning */
925 UNUSED(hrtc);
926
927 /* NOTE: This function should not be modified, when the callback is needed,
928 the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
929 */
930 }
931
932 #if defined(RTC_TAMPER3_SUPPORT)
933 /**
934 * @brief Tamper 3 callback.
935 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
936 * the configuration information for RTC.
937 * @retval None
938 */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)939 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
940 {
941 /* Prevent unused argument(s) compilation warning */
942 UNUSED(hrtc);
943
944 /* NOTE: This function should not be modified, when the callback is needed,
945 the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
946 */
947 }
948 #endif /* RTC_TAMPER3_SUPPORT */
949
950 /**
951 * @brief Handles Timestamp polling request.
952 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
953 * the configuration information for RTC.
954 * @param Timeout Timeout duration
955 * @retval HAL status
956 */
HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)957 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
958 {
959 uint32_t tickstart = 0U;
960
961 /* Get tick */
962 tickstart = HAL_GetTick();
963
964 while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
965 {
966 if (Timeout != HAL_MAX_DELAY)
967 {
968 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
969 {
970 hrtc->State = HAL_RTC_STATE_TIMEOUT;
971 return HAL_TIMEOUT;
972 }
973 }
974
975 if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
976 {
977 /* Clear the Timestamp Overrun Flag */
978 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
979
980 /* Change Timestamp state */
981 hrtc->State = HAL_RTC_STATE_ERROR;
982
983 return HAL_ERROR;
984 }
985 }
986
987 /* Change RTC state */
988 hrtc->State = HAL_RTC_STATE_READY;
989
990 return HAL_OK;
991 }
992
993 #if defined(RTC_TAMPER1_SUPPORT)
994 /**
995 * @brief Handles Tamper 1 Polling.
996 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
997 * the configuration information for RTC.
998 * @param Timeout Timeout duration
999 * @retval HAL status
1000 */
HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1001 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1002 {
1003 uint32_t tickstart = 0U;
1004
1005 /* Get tick */
1006 tickstart = HAL_GetTick();
1007
1008 /* Get the status of the Interrupt */
1009 while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
1010 {
1011 if (Timeout != HAL_MAX_DELAY)
1012 {
1013 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1014 {
1015 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1016 return HAL_TIMEOUT;
1017 }
1018 }
1019 }
1020
1021 /* Clear the Tamper Flag */
1022 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
1023
1024 /* Change RTC state */
1025 hrtc->State = HAL_RTC_STATE_READY;
1026
1027 return HAL_OK;
1028 }
1029 #endif /* RTC_TAMPER1_SUPPORT */
1030
1031 /**
1032 * @brief Handles Tamper 2 Polling.
1033 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1034 * the configuration information for RTC.
1035 * @param Timeout Timeout duration
1036 * @retval HAL status
1037 */
HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1038 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1039 {
1040 uint32_t tickstart = 0U;
1041
1042 /* Get tick */
1043 tickstart = HAL_GetTick();
1044
1045 /* Get the status of the Interrupt */
1046 while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
1047 {
1048 if (Timeout != HAL_MAX_DELAY)
1049 {
1050 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1051 {
1052 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1053 return HAL_TIMEOUT;
1054 }
1055 }
1056 }
1057
1058 /* Clear the Tamper Flag */
1059 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
1060
1061 /* Change RTC state */
1062 hrtc->State = HAL_RTC_STATE_READY;
1063
1064 return HAL_OK;
1065 }
1066
1067 #if defined(RTC_TAMPER3_SUPPORT)
1068 /**
1069 * @brief Handles Tamper 3 Polling.
1070 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1071 * the configuration information for RTC.
1072 * @param Timeout Timeout duration
1073 * @retval HAL status
1074 */
HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1075 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1076 {
1077 uint32_t tickstart = HAL_GetTick();
1078
1079 /* Get the status of the Interrupt */
1080 while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
1081 {
1082 if (Timeout != HAL_MAX_DELAY)
1083 {
1084 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1085 {
1086 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1087 return HAL_TIMEOUT;
1088 }
1089 }
1090 }
1091
1092 /* Clear the Tamper Flag */
1093 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
1094
1095 /* Change RTC state */
1096 hrtc->State = HAL_RTC_STATE_READY;
1097
1098 return HAL_OK;
1099 }
1100 #endif /* RTC_TAMPER3_SUPPORT */
1101
1102 /**
1103 * @}
1104 */
1105
1106 /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wakeup functions
1107 * @brief RTC Wakeup functions
1108 *
1109 @verbatim
1110 ===============================================================================
1111 ##### RTC Wakeup functions #####
1112 ===============================================================================
1113
1114 [..] This section provides functions allowing to configure Wakeup feature
1115
1116 @endverbatim
1117 * @{
1118 */
1119
1120 /**
1121 * @brief Sets wakeup timer.
1122 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1123 * the configuration information for RTC.
1124 * @param WakeUpCounter Wakeup counter
1125 * @param WakeUpClock Wakeup clock
1126 * @retval HAL status
1127 */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)1128 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1129 {
1130 uint32_t tickstart = 0U;
1131
1132 /* Check the parameters */
1133 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1134 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1135
1136 /* Process Locked */
1137 __HAL_LOCK(hrtc);
1138
1139 hrtc->State = HAL_RTC_STATE_BUSY;
1140
1141 /* Disable the write protection for RTC registers */
1142 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1143
1144 /* Check RTC WUTWF flag is reset only when wakeup timer enabled*/
1145 if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
1146 {
1147 tickstart = HAL_GetTick();
1148
1149 /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
1150 while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
1151 {
1152 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1153 {
1154 /* Enable the write protection for RTC registers */
1155 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1156
1157 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1158
1159 /* Process Unlocked */
1160 __HAL_UNLOCK(hrtc);
1161
1162 return HAL_TIMEOUT;
1163 }
1164 }
1165 }
1166
1167 /* Disable the Wakeup timer */
1168 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1169
1170 /* Clear the Wakeup flag */
1171 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1172
1173 /* Get tick */
1174 tickstart = HAL_GetTick();
1175
1176 /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1177 while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
1178 {
1179 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1180 {
1181 /* Enable the write protection for RTC registers */
1182 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1183
1184 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1185
1186 /* Process Unlocked */
1187 __HAL_UNLOCK(hrtc);
1188
1189 return HAL_TIMEOUT;
1190 }
1191 }
1192
1193 /* Clear the Wakeup Timer clock source bits in CR register */
1194 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1195
1196 /* Configure the clock source */
1197 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1198
1199 /* Configure the Wakeup Timer counter */
1200 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1201
1202 /* Enable the Wakeup Timer */
1203 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1204
1205 /* Enable the write protection for RTC registers */
1206 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1207
1208 hrtc->State = HAL_RTC_STATE_READY;
1209
1210 /* Process Unlocked */
1211 __HAL_UNLOCK(hrtc);
1212
1213 return HAL_OK;
1214 }
1215
1216 /**
1217 * @brief Sets wakeup timer with interrupt.
1218 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1219 * the configuration information for RTC.
1220 * @param WakeUpCounter Wakeup counter
1221 * @param WakeUpClock Wakeup clock
1222 * @retval HAL status
1223 */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)1224 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1225 {
1226 __IO uint32_t count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
1227
1228 /* Check the parameters */
1229 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1230 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1231
1232 /* Process Locked */
1233 __HAL_LOCK(hrtc);
1234
1235 hrtc->State = HAL_RTC_STATE_BUSY;
1236
1237 /* Disable the write protection for RTC registers */
1238 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1239
1240 /* Check RTC WUTWF flag is reset only when wakeup timer enabled */
1241 if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
1242 {
1243 /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
1244 do
1245 {
1246 count = count - 1U;
1247 if (count == 0U)
1248 {
1249 /* Enable the write protection for RTC registers */
1250 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1251
1252 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1253
1254 /* Process Unlocked */
1255 __HAL_UNLOCK(hrtc);
1256
1257 return HAL_TIMEOUT;
1258 }
1259 } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U);
1260 }
1261
1262 /* Disable the Wakeup timer */
1263 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1264
1265 /* Clear the Wakeup flag */
1266 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1267
1268 /* Reload the counter */
1269 count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
1270
1271 /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1272 do
1273 {
1274 count = count - 1U;
1275 if (count == 0U)
1276 {
1277 /* Enable the write protection for RTC registers */
1278 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1279
1280 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1281
1282 /* Process Unlocked */
1283 __HAL_UNLOCK(hrtc);
1284
1285 return HAL_TIMEOUT;
1286 }
1287 } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U);
1288
1289 /* Clear the Wakeup Timer clock source bits in CR register */
1290 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1291
1292 /* Configure the clock source */
1293 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1294
1295 /* Configure the Wakeup Timer counter */
1296 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1297
1298 /* RTC wakeup timer Interrupt Configuration: EXTI configuration */
1299 __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1300 __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1301
1302 /* Configure the interrupt in the RTC_CR register */
1303 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
1304
1305 /* Enable the Wakeup Timer */
1306 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1307
1308 /* Enable the write protection for RTC registers */
1309 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1310
1311 hrtc->State = HAL_RTC_STATE_READY;
1312
1313 /* Process Unlocked */
1314 __HAL_UNLOCK(hrtc);
1315
1316 return HAL_OK;
1317 }
1318
1319 /**
1320 * @brief Deactivates wakeup timer counter.
1321 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1322 * the configuration information for RTC.
1323 * @retval HAL status
1324 */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)1325 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1326 {
1327 uint32_t tickstart = 0U;
1328
1329 /* Process Locked */
1330 __HAL_LOCK(hrtc);
1331
1332 hrtc->State = HAL_RTC_STATE_BUSY;
1333
1334 /* Disable the write protection for RTC registers */
1335 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1336
1337 /* Disable the Wakeup Timer */
1338 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1339
1340 /* In case of interrupt mode is used, the interrupt source must disabled */
1341 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
1342
1343 /* Get tick */
1344 tickstart = HAL_GetTick();
1345
1346 /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1347 while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
1348 {
1349 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1350 {
1351 /* Enable the write protection for RTC registers */
1352 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1353
1354 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1355
1356 /* Process Unlocked */
1357 __HAL_UNLOCK(hrtc);
1358
1359 return HAL_TIMEOUT;
1360 }
1361 }
1362
1363 /* Enable the write protection for RTC registers */
1364 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1365
1366 hrtc->State = HAL_RTC_STATE_READY;
1367
1368 /* Process Unlocked */
1369 __HAL_UNLOCK(hrtc);
1370
1371 return HAL_OK;
1372 }
1373
1374 /**
1375 * @brief Gets wakeup timer counter.
1376 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1377 * the configuration information for RTC.
1378 * @retval Counter value
1379 */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)1380 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1381 {
1382 /* Get the counter value */
1383 return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1384 }
1385
1386 /**
1387 * @brief Handles Wakeup Timer interrupt request.
1388 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1389 * the configuration information for RTC.
1390 * @retval None
1391 */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)1392 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1393 {
1394 /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1395 __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1396
1397 /* Get the Wakeup timer interrupt source enable status */
1398 if (__HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE(hrtc, RTC_IT_WUT) != RESET)
1399 {
1400 /* Get the pending status of the Wakeup timer Interrupt */
1401 if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
1402 {
1403 /* Clear the Wakeup timer interrupt pending bit */
1404 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1405
1406 /* Wakeup timer callback */
1407 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1408 hrtc->WakeUpTimerEventCallback(hrtc);
1409 #else
1410 HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1411 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1412 }
1413 }
1414
1415 /* Change RTC state */
1416 hrtc->State = HAL_RTC_STATE_READY;
1417 }
1418
1419 /**
1420 * @brief Wakeup Timer callback.
1421 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1422 * the configuration information for RTC.
1423 * @retval None
1424 */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)1425 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1426 {
1427 /* Prevent unused argument(s) compilation warning */
1428 UNUSED(hrtc);
1429
1430 /* NOTE: This function should not be modified, when the callback is needed,
1431 the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1432 */
1433 }
1434
1435 /**
1436 * @brief Handles Wakeup Timer Polling.
1437 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1438 * the configuration information for RTC.
1439 * @param Timeout Timeout duration
1440 * @retval HAL status
1441 */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1442 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1443 {
1444 uint32_t tickstart = 0U;
1445
1446 /* Get tick */
1447 tickstart = HAL_GetTick();
1448
1449 while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
1450 {
1451 if (Timeout != HAL_MAX_DELAY)
1452 {
1453 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1454 {
1455 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1456 return HAL_TIMEOUT;
1457 }
1458 }
1459 }
1460
1461 /* Clear the Wakeup timer Flag */
1462 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1463
1464 /* Change RTC state */
1465 hrtc->State = HAL_RTC_STATE_READY;
1466
1467 return HAL_OK;
1468 }
1469
1470 /**
1471 * @}
1472 */
1473
1474 /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
1475 * @brief Extended Peripheral Control functions
1476 *
1477 @verbatim
1478 ===============================================================================
1479 ##### Extended Peripheral Control functions #####
1480 ===============================================================================
1481 [..]
1482 This subsection provides functions allowing to
1483 (+) Write a data in a specified RTC Backup data register
1484 (+) Read a data in a specified RTC Backup data register
1485 (+) Set the Smooth calibration parameters.
1486 (+) Configure the Synchronization Shift Control Settings.
1487 (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1488 (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1489 (+) Enable the RTC reference clock detection.
1490 (+) Disable the RTC reference clock detection.
1491 (+) Enable the Bypass Shadow feature.
1492 (+) Disable the Bypass Shadow feature.
1493
1494 @endverbatim
1495 * @{
1496 */
1497
1498 /**
1499 * @brief Writes a data in a specified RTC Backup data register.
1500 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1501 * the configuration information for RTC.
1502 * @param BackupRegister RTC Backup data Register number.
1503 * This parameter can be: RTC_BKP_DRx (where x can be from 0 to 4)
1504 * to specify the register.
1505 * @param Data Data to be written in the specified RTC Backup data register.
1506 * @retval None
1507 */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)1508 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1509 {
1510 uint32_t tmp = 0U;
1511
1512 /* Check the parameters */
1513 assert_param(IS_RTC_BKP(BackupRegister));
1514
1515 tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1516 tmp += (BackupRegister * 4U);
1517
1518 /* Write the specified register */
1519 *(__IO uint32_t *)tmp = (uint32_t)Data;
1520 }
1521
1522 /**
1523 * @brief Reads data from the specified RTC Backup data Register.
1524 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1525 * the configuration information for RTC.
1526 * @param BackupRegister RTC Backup data Register number.
1527 * This parameter can be: RTC_BKP_DRx (where x can be from 0 to 4)
1528 * to specify the register.
1529 * @retval Read value
1530 */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)1531 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1532 {
1533 uint32_t tmp = 0U;
1534
1535 /* Check the parameters */
1536 assert_param(IS_RTC_BKP(BackupRegister));
1537
1538 tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1539 tmp += (BackupRegister * 4U);
1540
1541 /* Read the specified register */
1542 return (*(__IO uint32_t *)tmp);
1543 }
1544
1545 /**
1546 * @brief Sets the Smooth calibration parameters.
1547 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1548 * the configuration information for RTC.
1549 * @param SmoothCalibPeriod Select the Smooth Calibration Period.
1550 * This parameter can be can be one of the following values:
1551 * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
1552 * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
1553 * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
1554 * @param SmoothCalibPlusPulses Select to Set or reset the CALP bit.
1555 * This parameter can be one of the following values:
1556 * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
1557 * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1558 * @param SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
1559 * This parameter can be one any value from 0 to 0x000001FF.
1560 * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
1561 * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
1562 * SmoothCalibMinusPulsesValue must be equal to 0.
1563 * @retval HAL status
1564 */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)1565 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
1566 {
1567 uint32_t tickstart = 0U;
1568
1569 /* Check the parameters */
1570 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1571 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1572 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
1573
1574 /* Process Locked */
1575 __HAL_LOCK(hrtc);
1576
1577 hrtc->State = HAL_RTC_STATE_BUSY;
1578
1579 /* Disable the write protection for RTC registers */
1580 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1581
1582 /* check if a calibration is pending*/
1583 if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1584 {
1585 /* Get tick */
1586 tickstart = HAL_GetTick();
1587
1588 /* check if a calibration is pending*/
1589 while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1590 {
1591 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1592 {
1593 /* Enable the write protection for RTC registers */
1594 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1595
1596 /* Change RTC state */
1597 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1598
1599 /* Process Unlocked */
1600 __HAL_UNLOCK(hrtc);
1601
1602 return HAL_TIMEOUT;
1603 }
1604 }
1605 }
1606
1607 /* Configure the Smooth calibration settings */
1608 hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | \
1609 (uint32_t)SmoothCalibPlusPulses | \
1610 (uint32_t)SmoothCalibMinusPulsesValue);
1611
1612 /* Enable the write protection for RTC registers */
1613 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1614
1615 /* Change RTC state */
1616 hrtc->State = HAL_RTC_STATE_READY;
1617
1618 /* Process Unlocked */
1619 __HAL_UNLOCK(hrtc);
1620
1621 return HAL_OK;
1622 }
1623
1624 /**
1625 * @brief Configures the Synchronization Shift Control Settings.
1626 * @note When REFCKON is set, firmware must not write to Shift control register.
1627 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1628 * the configuration information for RTC.
1629 * @param ShiftAdd1S Select to add or not 1 second to the time calendar.
1630 * This parameter can be one of the following values:
1631 * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1632 * @arg RTC_SHIFTADD1S_RESET: No effect.
1633 * @param ShiftSubFS Select the number of Second Fractions to substitute.
1634 * This parameter can be one any value from 0 to 0x7FFF.
1635 * @retval HAL status
1636 */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)1637 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1638 {
1639 uint32_t tickstart = 0U;
1640
1641 /* Check the parameters */
1642 assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1643 assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1644
1645 /* Process Locked */
1646 __HAL_LOCK(hrtc);
1647
1648 hrtc->State = HAL_RTC_STATE_BUSY;
1649
1650 /* Disable the write protection for RTC registers */
1651 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1652
1653 /* Get tick */
1654 tickstart = HAL_GetTick();
1655
1656 /* Wait until the shift is completed */
1657 while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
1658 {
1659 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1660 {
1661 /* Enable the write protection for RTC registers */
1662 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1663
1664 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1665
1666 /* Process Unlocked */
1667 __HAL_UNLOCK(hrtc);
1668
1669 return HAL_TIMEOUT;
1670 }
1671 }
1672
1673 /* Check if the reference clock detection is disabled */
1674 if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
1675 {
1676 /* Configure the Shift settings */
1677 hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1678
1679 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1680 if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
1681 {
1682 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1683 {
1684 /* Enable the write protection for RTC registers */
1685 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1686
1687 hrtc->State = HAL_RTC_STATE_ERROR;
1688
1689 /* Process Unlocked */
1690 __HAL_UNLOCK(hrtc);
1691
1692 return HAL_ERROR;
1693 }
1694 }
1695 }
1696 else
1697 {
1698 /* Enable the write protection for RTC registers */
1699 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1700
1701 /* Change RTC state */
1702 hrtc->State = HAL_RTC_STATE_ERROR;
1703
1704 /* Process Unlocked */
1705 __HAL_UNLOCK(hrtc);
1706
1707 return HAL_ERROR;
1708 }
1709
1710 /* Enable the write protection for RTC registers */
1711 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1712
1713 /* Change RTC state */
1714 hrtc->State = HAL_RTC_STATE_READY;
1715
1716 /* Process Unlocked */
1717 __HAL_UNLOCK(hrtc);
1718
1719 return HAL_OK;
1720 }
1721
1722 /**
1723 * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1724 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1725 * the configuration information for RTC.
1726 * @param CalibOutput Select the Calibration output Selection.
1727 * This parameter can be one of the following values:
1728 * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1729 * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1730 * @retval HAL status
1731 */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1732 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1733 {
1734 /* Check the parameters */
1735 assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1736
1737 /* Process Locked */
1738 __HAL_LOCK(hrtc);
1739
1740 hrtc->State = HAL_RTC_STATE_BUSY;
1741
1742 /* Disable the write protection for RTC registers */
1743 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1744
1745 /* Clear flags before config */
1746 hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1747
1748 /* Configure the RTC_CR register */
1749 hrtc->Instance->CR |= (uint32_t)CalibOutput;
1750
1751 __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1752
1753 /* Enable the write protection for RTC registers */
1754 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1755
1756 /* Change RTC state */
1757 hrtc->State = HAL_RTC_STATE_READY;
1758
1759 /* Process Unlocked */
1760 __HAL_UNLOCK(hrtc);
1761
1762 return HAL_OK;
1763 }
1764
1765 /**
1766 * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1767 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1768 * the configuration information for RTC.
1769 * @retval HAL status
1770 */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1771 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1772 {
1773 /* Process Locked */
1774 __HAL_LOCK(hrtc);
1775
1776 hrtc->State = HAL_RTC_STATE_BUSY;
1777
1778 /* Disable the write protection for RTC registers */
1779 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1780
1781 __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1782
1783 /* Enable the write protection for RTC registers */
1784 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1785
1786 /* Change RTC state */
1787 hrtc->State = HAL_RTC_STATE_READY;
1788
1789 /* Process Unlocked */
1790 __HAL_UNLOCK(hrtc);
1791
1792 return HAL_OK;
1793 }
1794
1795 /**
1796 * @brief Enables the RTC reference clock detection.
1797 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1798 * the configuration information for RTC.
1799 * @retval HAL status
1800 */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1801 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1802 {
1803 HAL_StatusTypeDef status;
1804
1805 /* Process Locked */
1806 __HAL_LOCK(hrtc);
1807
1808 hrtc->State = HAL_RTC_STATE_BUSY;
1809
1810 /* Disable the write protection for RTC registers */
1811 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1812
1813 /* Enter Initialization mode */
1814 status = RTC_EnterInitMode(hrtc);
1815
1816 if (status == HAL_OK)
1817 {
1818 /* Enable the reference clock detection */
1819 __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1820
1821 /* Exit Initialization mode */
1822 status = RTC_ExitInitMode(hrtc);
1823 }
1824
1825 if (status == HAL_OK)
1826 {
1827 hrtc->State = HAL_RTC_STATE_READY;
1828 }
1829
1830 /* Enable the write protection for RTC registers */
1831 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1832
1833 /* Process Unlocked */
1834 __HAL_UNLOCK(hrtc);
1835
1836 return status;
1837 }
1838
1839 /**
1840 * @brief Disable the RTC reference clock detection.
1841 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1842 * the configuration information for RTC.
1843 * @retval HAL status
1844 */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1845 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1846 {
1847 HAL_StatusTypeDef status;
1848
1849 /* Process Locked */
1850 __HAL_LOCK(hrtc);
1851
1852 hrtc->State = HAL_RTC_STATE_BUSY;
1853
1854 /* Disable the write protection for RTC registers */
1855 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1856
1857 /* Enter Initialization mode */
1858 status = RTC_EnterInitMode(hrtc);
1859
1860 if (status == HAL_OK)
1861 {
1862 /* Disable the reference clock detection */
1863 __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1864
1865 /* Exit Initialization mode */
1866 status = RTC_ExitInitMode(hrtc);
1867 }
1868
1869 if (status == HAL_OK)
1870 {
1871 hrtc->State = HAL_RTC_STATE_READY;
1872 }
1873
1874 /* Enable the write protection for RTC registers */
1875 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1876
1877 /* Process Unlocked */
1878 __HAL_UNLOCK(hrtc);
1879
1880 return status;
1881 }
1882
1883 /**
1884 * @brief Enables the Bypass Shadow feature.
1885 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1886 * the configuration information for RTC.
1887 * @note When the Bypass Shadow is enabled the calendar value are taken
1888 * directly from the Calendar counter.
1889 * @retval HAL status
1890 */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1891 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1892 {
1893 /* Process Locked */
1894 __HAL_LOCK(hrtc);
1895
1896 hrtc->State = HAL_RTC_STATE_BUSY;
1897
1898 /* Disable the write protection for RTC registers */
1899 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1900
1901 /* Set the BYPSHAD bit */
1902 hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1903
1904 /* Enable the write protection for RTC registers */
1905 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1906
1907 /* Change RTC state */
1908 hrtc->State = HAL_RTC_STATE_READY;
1909
1910 /* Process Unlocked */
1911 __HAL_UNLOCK(hrtc);
1912
1913 return HAL_OK;
1914 }
1915
1916 /**
1917 * @brief Disables the Bypass Shadow feature.
1918 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1919 * the configuration information for RTC.
1920 * @note When the Bypass Shadow is enabled the calendar value are taken
1921 * directly from the Calendar counter.
1922 * @retval HAL status
1923 */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1924 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1925 {
1926 /* Process Locked */
1927 __HAL_LOCK(hrtc);
1928
1929 hrtc->State = HAL_RTC_STATE_BUSY;
1930
1931 /* Disable the write protection for RTC registers */
1932 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1933
1934 /* Reset the BYPSHAD bit */
1935 hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
1936
1937 /* Enable the write protection for RTC registers */
1938 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1939
1940 /* Change RTC state */
1941 hrtc->State = HAL_RTC_STATE_READY;
1942
1943 /* Process Unlocked */
1944 __HAL_UNLOCK(hrtc);
1945
1946 return HAL_OK;
1947 }
1948
1949 /**
1950 * @}
1951 */
1952
1953 /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
1954 * @brief Extended features functions
1955 *
1956 @verbatim
1957 ===============================================================================
1958 ##### Extended features functions #####
1959 ===============================================================================
1960 [..] This section provides functions allowing to:
1961 (+) RTC Alarm B callback
1962 (+) RTC Poll for Alarm B request
1963
1964 @endverbatim
1965 * @{
1966 */
1967
1968 /**
1969 * @brief Alarm B callback.
1970 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1971 * the configuration information for RTC.
1972 * @retval None
1973 */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1974 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1975 {
1976 /* Prevent unused argument(s) compilation warning */
1977 UNUSED(hrtc);
1978
1979 /* NOTE: This function should not be modified, when the callback is needed,
1980 the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1981 */
1982 }
1983
1984 /**
1985 * @brief Handles Alarm B Polling request.
1986 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1987 * the configuration information for RTC.
1988 * @param Timeout Timeout duration
1989 * @retval HAL status
1990 */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1991 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1992 {
1993 uint32_t tickstart = 0U;
1994
1995 /* Get tick */
1996 tickstart = HAL_GetTick();
1997
1998 /* Wait till RTC ALRBF flag is set and if timeout is reached exit */
1999 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
2000 {
2001 if (Timeout != HAL_MAX_DELAY)
2002 {
2003 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
2004 {
2005 hrtc->State = HAL_RTC_STATE_TIMEOUT;
2006 return HAL_TIMEOUT;
2007 }
2008 }
2009 }
2010
2011 /* Clear the Alarm flag */
2012 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
2013
2014 /* Change RTC state */
2015 hrtc->State = HAL_RTC_STATE_READY;
2016
2017 return HAL_OK;
2018 }
2019
2020 /**
2021 * @}
2022 */
2023
2024 /**
2025 * @}
2026 */
2027
2028 #endif /* HAL_RTC_MODULE_ENABLED */
2029 /**
2030 * @}
2031 */
2032
2033 /**
2034 * @}
2035 */
2036