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