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