1 /**
2 ******************************************************************************
3 * @file stm32h5xx_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) 2023 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 outputs pins (RTC_OUT1 & RTC_OUT2).
46 To configure the outputs, use the HAL_RTC_Init() function.
47 (+) RTC_OUT1 and RTC_OUT2 which select one of the following two outputs:
48 (+) CALIB: 512Hz or 1Hz clock output (with an LSE frequency of 32.768kHz).
49 To enable the CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
50 (+) TAMPALRM: This output is the OR between rtc_tamp_evt and ALARM signals.
51 ALARM is enabled by configuring the OSEL[1:0] bits in the RTC_CR register
52 which select the alarm A, alarm B or wakeup outputs.
53 rtc_tamp_evt is enabled by setting the TAMPOE bit
54 in the RTC_CR register which selects the tamper event outputs.
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 *** RTC synchronization ***
64 ================================================
65 [..]
66 (+) The RTC can be finely adjusted using HAL_RTCEx_SetSynchroShift() function.
67 Writing to RTC_SHIFTR can shift (either delay or advance) the clock with
68 a resolution of 1 ck_apre period.
69 The shift operation consists in adding the SUBFS[14:0] value to the synchronous
70 prescaler counter SS[15:0]: this delays the clock.
71
72 *** Bypass shadow registers ***
73 ================================================
74 [..]
75 (+) Enable bypass shadow registers using the HAL_RTCEx_EnableBypassShadow().
76 When the Bypass Shadow is enabled the calendar value are taken directly
77 from the Calendar counter.
78 Thus eliminating the need to wait for the RSF bit to be set.
79 This is especially useful after exiting from low-power modes (Stop or Standby),
80 since the shadow registers are not updated during these modes.
81
82 *** RTC ultra-low-power mode ***
83 ================================================
84 [..]
85 (+) Configure the RTC ultra-low-power mode using HAL_RTCEx_SetLowPowerCalib() function.
86 In this case, the calibration mechanism is applied on ck_apre instead of RTCCLK.
87 The resulting accuracy is the same, but the calibration is performed during a
88 calibration cycle of about 220 x PREDIV_A x RTCCLK pulses instead of 220 RTCCLK pulses.
89
90 *** RTC subsecond register underflow interruption ***
91 ================================================
92 [..]
93 (+) Enable the RTC SSRU interruption mode using HAL_RTCEx_SetSSRU_IT() function.
94 In this case, when the SSR rolls under 0, an SSRU interruption is triggered.
95 Disable the RTC SSRU interruption mode using HAL_RTCEx_DeactivateSSRU() function.
96
97 *** TimeStamp configuration ***
98 ===============================
99 [..]
100 (+) Enable the RTC TimeStamp using the HAL_RTCEx_SetTimeStamp() function.
101 You can also configure the RTC TimeStamp with interrupt mode using the
102 HAL_RTCEx_SetTimeStamp_IT() function.
103 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
104 function.
105
106 *** Internal TimeStamp configuration ***
107 ===============================
108 [..]
109 (+) Enable the RTC internal TimeStamp using the HAL_RTCEx_SetInternalTimeStamp() function.
110 User has to check internal timestamp occurrence using __HAL_RTC_INTERNAL_TIMESTAMP_GET_FLAG.
111 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
112 function.
113
114 *** Tamper configuration ***
115 ============================
116 [..]
117 (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
118 or Level according to the Tamper filter (if equal to 0 Edge else Level)
119 value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
120 Pull-UP using the HAL_RTCEx_SetTamper() function. You can configure RTC Tamper
121 with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
122 (+) The default configuration of the Tamper erases the backup registers. To avoid
123 erase, enable the NoErase field on the TAMP_CR2 register.
124 (+) With new RTC tamper configuration, you have to call HAL_RTC_Init() in order to
125 perform TAMP base address offset calculation.
126 (+) Enable Internal tamper using HAL_RTCEx_SetInternalTamper. IT mode can be chosen using
127 setting Interrupt field.
128
129 *** Backup Data Registers and Device Secrets configuration ***
130 ===========================================
131 [..]
132 (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
133 function.
134 (+) To read the RTC Backup registers, use the HAL_RTCEx_BKUPRead()
135 function.
136 (+) To reset the RTC Backup registers and erase the device secrets,
137 use HAL_RTCEx_BKUPErase() function.
138 (+) Enable the lock of the Boot hardware Key using the HAL_RTCEx_LockBootHardwareKey()
139 function.
140 The backup registers from TAMP_BKP0R to TAMP_BKP7R cannot be accessed neither in
141 read nor in write (they are read as 0 and write ignore).
142 (+) Configure the erase of the Device Secrets using HAL_RTCEx_ConfigEraseDeviceSecrets()
143 function.
144 (+) Block the access to the RTC Backup registers and all the device secrets
145 using HAL_RTCEx_BKUPBlock() function.
146
147 *** Monotonic counter ***
148 ================================================
149 [..]
150 (+) To increment the Monotonic counter, use the HAL_RTCEx_MonotonicCounterIncrement()
151 function.
152 (+) To get the current value of the Monotonic counter, use the HAL_RTCEx_MonotonicCounterGet()
153 function.
154
155 *** RTC & TAMP secure protection modes ***
156 ================================================
157 [..]
158 (+) Set the security level of the RTC/TAMP/Backup registers using HAL_RTCEx_SecureModeSet()
159 function.
160 +) Get the security level of the RTC/TAMP/Backup registers using HAL_RTCEx_SecureModeGet()
161 function.
162
163 *** RTC & TAMP privilege protection modes ***
164 ================================================
165 [..]
166 (+) Set the privilege level of the RTC/TAMP/Backup registers using HAL_RTCEx_PrivilegeModeSet()
167 function.
168 +) Get the privilege level of the RTC/TAMP/Backup registers using HAL_RTCEx_PrivilegeModeGet()
169 function.
170
171 @endverbatim
172 */
173
174 /* Includes ------------------------------------------------------------------*/
175 #include "stm32h5xx_hal.h"
176
177 /** @addtogroup STM32H5xx_HAL_Driver
178 * @{
179 */
180
181 /** @addtogroup RTCEx
182 * @brief RTC Extended HAL module driver
183 * @{
184 */
185
186 #ifdef HAL_RTC_MODULE_ENABLED
187
188 /* Private typedef -----------------------------------------------------------*/
189 /* Private define ------------------------------------------------------------*/
190 #define TAMP_ALL RTC_TAMPER_ALL
191
192 /* Private macro -------------------------------------------------------------*/
193 /* Private variables ---------------------------------------------------------*/
194 /* Private function prototypes -----------------------------------------------*/
195 /* Exported functions --------------------------------------------------------*/
196
197 /** @addtogroup RTCEx_Exported_Functions
198 * @{
199 */
200
201
202 /** @addtogroup RTCEx_Exported_Functions_Group1
203 * @brief RTC TimeStamp and Tamper functions
204 *
205 @verbatim
206 ===============================================================================
207 ##### RTC TimeStamp and Tamper functions #####
208 ===============================================================================
209
210 [..] This section provides functions allowing to configure TimeStamp feature
211
212 @endverbatim
213 * @{
214 */
215
216 #ifdef RTC_CR_TSE
217 /**
218 * @brief Set TimeStamp.
219 * @note This API must be called before enabling the TimeStamp feature.
220 * @param hrtc RTC handle
221 * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
222 * activated.
223 * This parameter can be one of the following values:
224 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
225 * rising edge of the related pin.
226 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
227 * falling edge of the related pin.
228 * @param RTC_TimeStampPin specifies the RTC TimeStamp Pin.
229 * This parameter can be one of the following values:
230 * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
231 * The RTC TimeStamp Pin is per default PC13, but for reasons of
232 * compatibility, this parameter is required.
233 * @retval HAL status
234 */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)235 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
236 {
237 /* Check the parameters */
238 #if defined(RTC_CR_TSEDGE)
239 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
240 #endif /* RTC_CR_TSEDGE */
241 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
242 UNUSED(RTC_TimeStampPin);
243
244 /* Process Locked */
245 __HAL_LOCK(hrtc);
246
247 /* Change RTC state */
248 hrtc->State = HAL_RTC_STATE_BUSY;
249
250 /* Get the RTC_CR register and clear the bits to be configured */
251 #if defined(RTC_CR_TSEDGE)
252 CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
253 #else
254 CLEAR_BIT(RTC->CR, RTC_CR_TSE);
255 #endif /* RTC_CR_TSEDGE */
256
257 /* Configure the Time Stamp TSEDGE and Enable bits */
258 SET_BIT(RTC->CR, (uint32_t)TimeStampEdge | RTC_CR_TSE);
259
260 /* Change RTC state */
261 hrtc->State = HAL_RTC_STATE_READY;
262
263 /* Process Unlocked */
264 __HAL_UNLOCK(hrtc);
265
266 return HAL_OK;
267 }
268
269 /**
270 * @brief Set TimeStamp with Interrupt.
271 * @note This API must be called before enabling the TimeStamp feature.
272 * @note The application must ensure that the EXTI RTC interrupt line is enabled.
273 * @param hrtc RTC handle
274 * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
275 * activated.
276 * This parameter can be one of the following values:
277 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
278 * rising edge of the related pin.
279 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
280 * falling edge of the related pin.
281 * @param RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
282 * This parameter can be one of the following values:
283 * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
284 * The RTC TimeStamp Pin is per default PC13, but for reasons of
285 * compatibility, this parameter is required.
286 * @retval HAL status
287 */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)288 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
289 {
290 /* Check the parameters */
291 #if defined(RTC_CR_TSEDGE)
292 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
293 #endif /* RTC_CR_TSEDGE */
294 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
295 UNUSED(RTC_TimeStampPin);
296
297 /* Process Locked */
298 __HAL_LOCK(hrtc);
299
300 /* Change RTC state */
301 hrtc->State = HAL_RTC_STATE_BUSY;
302
303 /* Get the RTC_CR register and clear the bits to be configured */
304 #if defined(RTC_CR_TSEDGE)
305 CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
306 #else
307 CLEAR_BIT(RTC->CR, (RTC_CR_TSE | RTC_CR_TSIE));
308 #endif /* RTC_CR_TSEDGE */
309
310 /* Configure the Time Stamp TSEDGE before Enable bit to avoid unwanted TSF setting. */
311 SET_BIT(RTC->CR, (uint32_t)TimeStampEdge);
312
313 /* Enable timestamp and IT */
314 SET_BIT(RTC->CR, RTC_CR_TSE | RTC_CR_TSIE);
315
316 /* Change RTC state */
317 hrtc->State = HAL_RTC_STATE_READY;
318
319 /* Process Unlocked */
320 __HAL_UNLOCK(hrtc);
321
322 return HAL_OK;
323 }
324
325 /**
326 * @brief Deactivate TimeStamp.
327 * @param hrtc RTC handle
328 * @retval HAL status
329 */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)330 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
331 {
332 /* Process Locked */
333 __HAL_LOCK(hrtc);
334
335 /* Change RTC state */
336 hrtc->State = HAL_RTC_STATE_BUSY;
337
338 /* In case of interrupt mode is used, the interrupt source must disabled */
339 #if defined(RTC_CR_TSEDGE)
340 CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
341 #else
342 CLEAR_BIT(RTC->CR, (RTC_CR_TSE | RTC_CR_TSIE));
343 #endif /* RTC_CR_TSEDGE */
344
345 /* Change RTC state */
346 hrtc->State = HAL_RTC_STATE_READY;
347
348 /* Process Unlocked */
349 __HAL_UNLOCK(hrtc);
350
351 return HAL_OK;
352 }
353 #endif /* RTC_CR_TSE */
354
355 /**
356 * @brief Set Internal TimeStamp.
357 * @note This API must be called before enabling the internal TimeStamp feature.
358 * @param hrtc RTC handle
359 * @retval HAL status
360 */
HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef * hrtc)361 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
362 {
363 /* Process Locked */
364 __HAL_LOCK(hrtc);
365
366 /* Change RTC state */
367 hrtc->State = HAL_RTC_STATE_BUSY;
368
369 /* Configure the internal Time Stamp Enable bits */
370 SET_BIT(RTC->CR, RTC_CR_ITSE);
371
372 /* Change RTC state */
373 hrtc->State = HAL_RTC_STATE_READY;
374
375 /* Process Unlocked */
376 __HAL_UNLOCK(hrtc);
377
378 return HAL_OK;
379 }
380
381 /**
382 * @brief Deactivate Internal TimeStamp.
383 * @param hrtc RTC handle
384 * @retval HAL status
385 */
HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef * hrtc)386 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
387 {
388 /* Process Locked */
389 __HAL_LOCK(hrtc);
390
391 /* Change RTC state */
392 hrtc->State = HAL_RTC_STATE_BUSY;
393
394 /* Configure the internal Time Stamp Enable bits */
395 CLEAR_BIT(RTC->CR, RTC_CR_ITSE);
396
397 hrtc->State = HAL_RTC_STATE_READY;
398
399 /* Process Unlocked */
400 __HAL_UNLOCK(hrtc);
401
402 return HAL_OK;
403 }
404
405 /**
406 * @brief Get the RTC TimeStamp value.
407 * @param hrtc RTC handle
408 * @param sTimeStamp Pointer to Time structure
409 * @param sTimeStampDate Pointer to Date structure
410 * @param Format specifies the format of the entered parameters.
411 * This parameter can be one of the following values:
412 * @arg RTC_FORMAT_BIN: Binary data format
413 * @arg RTC_FORMAT_BCD: BCD data format
414 * @retval HAL status
415 */
HAL_RTCEx_GetTimeStamp(const RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)416 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(const RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp,
417 RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
418 {
419 uint32_t tmptime;
420 uint32_t tmpdate;
421
422 /* Prevent unused argument(s) compilation warning */
423 UNUSED(hrtc);
424
425 /* Check the parameters */
426 assert_param(IS_RTC_FORMAT(Format));
427
428 /* Get the TimeStamp time and date registers values */
429 tmptime = READ_BIT(RTC->TSTR, RTC_TR_RESERVED_MASK);
430 tmpdate = READ_BIT(RTC->TSDR, RTC_DR_RESERVED_MASK);
431
432 /* Fill the Time structure fields with the read parameters */
433 sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
434 sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
435 sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
436 sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
437 sTimeStamp->SubSeconds = READ_BIT(RTC->TSSSR, RTC_TSSSR_SS);
438
439 /* Fill the Date structure fields with the read parameters */
440 sTimeStampDate->Year = 0U;
441 sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
442 sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU));
443 sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
444
445 /* Check the input parameters format */
446 if (Format == RTC_FORMAT_BIN)
447 {
448 /* Convert the TimeStamp structure parameters to Binary format */
449 sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
450 sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
451 sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
452
453 /* Convert the DateTimeStamp structure parameters to Binary format */
454 sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
455 sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
456 sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
457 }
458
459 /* Clear the TIMESTAMP Flags */
460 WRITE_REG(RTC->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
461
462 return HAL_OK;
463 }
464
465 /**
466 * @brief Handle TimeStamp interrupt request.
467 * @param hrtc RTC handle
468 * @retval None
469 */
HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef * hrtc)470 void HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
471 {
472 /* Get the pending status of the TimeStamp Interrupt */
473 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
474 if (READ_BIT(RTC->SMISR, RTC_SMISR_TSMF) != 0U)
475 #else
476 if (READ_BIT(RTC->MISR, RTC_MISR_TSMF) != 0U)
477 #endif /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
478 {
479 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
480 /* Call TimeStampEvent registered Callback */
481 hrtc->TimeStampEventCallback(hrtc);
482 #else
483 HAL_RTCEx_TimeStampEventCallback(hrtc);
484 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
485
486 /* Clearing flags after the Callback because the content of RTC_TSTR and RTC_TSDR are cleared when
487 TSF bit is reset.*/
488 WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSF);
489 }
490
491 /* Change RTC state */
492 hrtc->State = HAL_RTC_STATE_READY;
493 }
494
495 /**
496 * @brief TimeStamp callback.
497 * @param hrtc RTC handle
498 * @retval None
499 */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)500 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
501 {
502 /* Prevent unused argument(s) compilation warning */
503 UNUSED(hrtc);
504
505 /* NOTE : This function should not be modified, when the callback is needed,
506 the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
507 */
508 }
509
510 /**
511 * @brief Handle TimeStamp polling request.
512 * @param hrtc RTC handle
513 * @param Timeout Timeout duration
514 * @retval HAL status
515 */
HAL_RTCEx_PollForTimeStampEvent(const RTC_HandleTypeDef * hrtc,uint32_t Timeout)516 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(const RTC_HandleTypeDef *hrtc, uint32_t Timeout)
517 {
518 /* Prevent unused argument(s) compilation warning */
519 UNUSED(hrtc);
520
521 uint32_t tickstart = HAL_GetTick();
522
523 while (READ_BIT(RTC->SR, RTC_SR_TSF) == 0U)
524 {
525 if (READ_BIT(RTC->SR, RTC_SR_TSOVF) != 0U)
526 {
527 /* Clear the TIMESTAMP OverRun Flag */
528 WRITE_REG(RTC->SCR, RTC_SCR_CTSOVF);
529
530 return HAL_ERROR;
531 }
532
533 if (Timeout != HAL_MAX_DELAY)
534 {
535 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
536 {
537 /* New check to avoid false timeout detection in case of preemption */
538 if (READ_BIT(RTC->SR, RTC_SR_TSF) == 0U)
539 {
540 return HAL_TIMEOUT;
541 }
542 else
543 {
544 break;
545 }
546 }
547 }
548 }
549
550 return HAL_OK;
551 }
552
553 /**
554 * @}
555 */
556
557 /** @addtogroup RTCEx_Exported_Functions_Group2
558 * @brief RTC Wake-up functions
559 *
560 @verbatim
561 ===============================================================================
562 ##### RTC Wake-up functions #####
563 ===============================================================================
564
565 [..] This section provides functions allowing to configure Wake-up feature
566
567 @endverbatim
568 * @{
569 */
570
571 /**
572 * @brief Set wake up timer.
573 * @param hrtc RTC handle
574 * @param WakeUpCounter Wake up counter
575 * @param WakeUpClock Wake up clock
576 * @retval HAL status
577 */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)578 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
579 {
580 uint32_t tickstart;
581
582 /* Check the parameters */
583 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
584 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
585
586 /* Process Locked */
587 __HAL_LOCK(hrtc);
588
589 /* Change RTC state */
590 hrtc->State = HAL_RTC_STATE_BUSY;
591
592 /* Clear WUTE in RTC_CR to disable the wakeup timer */
593 CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
594
595 /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
596 counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
597 calendar initialization mode. */
598 if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
599 {
600 tickstart = HAL_GetTick();
601
602 while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
603 {
604 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
605 {
606 /* New check to avoid false timeout detection in case of preemption */
607 if (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
608 {
609 /* Change RTC state */
610 hrtc->State = HAL_RTC_STATE_TIMEOUT;
611
612 /* Process Unlocked */
613 __HAL_UNLOCK(hrtc);
614
615 return HAL_TIMEOUT;
616 }
617 else
618 {
619 break;
620 }
621 }
622 }
623 }
624
625 /* Configure the clock source */
626 MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
627
628 /* Configure the Wakeup Timer counter */
629 WRITE_REG(RTC->WUTR, (uint32_t)WakeUpCounter);
630
631 /* Enable the Wakeup Timer */
632 SET_BIT(RTC->CR, RTC_CR_WUTE);
633
634 /* Change RTC state */
635 hrtc->State = HAL_RTC_STATE_READY;
636
637 /* Process Unlocked */
638 __HAL_UNLOCK(hrtc);
639
640 return HAL_OK;
641 }
642
643 /**
644 * @brief Set wake up timer with interrupt.
645 * @note The application must ensure that the EXTI RTC interrupt line is enabled.
646 * @param hrtc RTC handle
647 * @param WakeUpCounter Wake up counter
648 * @param WakeUpClock Wake up clock
649 * @param WakeUpAutoClr Wake up auto clear value (look at WUTOCLR in reference manual)
650 * - No effect if WakeUpAutoClr is set to zero
651 * - This feature is meaningful in case of Low power mode to avoid any RTC software execution
652 * after Wake Up.
653 * @retval HAL status
654 */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock,uint32_t WakeUpAutoClr)655 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock,
656 uint32_t WakeUpAutoClr)
657 {
658 uint32_t tickstart;
659
660 /* Check the parameters */
661 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
662 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
663 /* (0x0000<=WUTOCLR<=WUT) */
664 assert_param(WakeUpAutoClr <= WakeUpCounter);
665
666 /* Process Locked */
667 __HAL_LOCK(hrtc);
668
669 /* Change RTC state */
670 hrtc->State = HAL_RTC_STATE_BUSY;
671
672 /* Clear WUTE in RTC_CR to disable the wakeup timer */
673 CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
674
675 /* Clear flag Wake-Up */
676 WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
677
678 /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
679 counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
680 calendar initialization mode. */
681 if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
682 {
683 tickstart = HAL_GetTick();
684 while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
685 {
686 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
687 {
688 /* New check to avoid false timeout detection in case of preemption */
689 if (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
690 {
691 /* Change RTC state */
692 hrtc->State = HAL_RTC_STATE_TIMEOUT;
693
694 /* Process Unlocked */
695 __HAL_UNLOCK(hrtc);
696
697 return HAL_TIMEOUT;
698 }
699 else
700 {
701 break;
702 }
703 }
704 }
705 }
706
707 /* Configure the Wakeup Timer counter and auto clear value */
708 WRITE_REG(RTC->WUTR, (uint32_t)(WakeUpCounter | (WakeUpAutoClr << RTC_WUTR_WUTOCLR_Pos)));
709
710 /* Configure the clock source */
711 MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
712
713 /* Configure the Interrupt in the RTC_CR register and Enable the Wakeup Timer*/
714 SET_BIT(RTC->CR, (RTC_CR_WUTIE | RTC_CR_WUTE));
715
716 /* Change RTC state */
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 Deactivate wake up timer counter.
727 * @param hrtc RTC handle
728 * @retval HAL status
729 */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)730 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
731 {
732 /* Process Locked */
733 __HAL_LOCK(hrtc);
734
735 /* Change RTC state */
736 hrtc->State = HAL_RTC_STATE_BUSY;
737
738 /* Disable the Wakeup Timer */
739 /* In case of interrupt mode is used, the interrupt source must disabled */
740 CLEAR_BIT(RTC->CR, (RTC_CR_WUTE | RTC_CR_WUTIE));
741
742 /* Change RTC state */
743 hrtc->State = HAL_RTC_STATE_READY;
744
745 /* Process Unlocked */
746 __HAL_UNLOCK(hrtc);
747
748 return HAL_OK;
749 }
750
751 /**
752 * @brief Get wake up timer counter.
753 * @param hrtc RTC handle
754 * @retval Counter value
755 */
HAL_RTCEx_GetWakeUpTimer(const RTC_HandleTypeDef * hrtc)756 uint32_t HAL_RTCEx_GetWakeUpTimer(const RTC_HandleTypeDef *hrtc)
757 {
758 /* Prevent unused argument(s) compilation warning */
759 UNUSED(hrtc);
760
761 /* Get the counter value */
762 return (uint32_t)(READ_BIT(RTC->WUTR, RTC_WUTR_WUT));
763 }
764
765 /**
766 * @brief Handle Wake Up Timer interrupt request.
767 * @param hrtc RTC handle
768 * @retval None
769 */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)770 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
771 {
772 /* Get the pending status of the Wake-Up Timer Interrupt */
773 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
774 if (READ_BIT(RTC->SMISR, RTC_SMISR_WUTMF) != 0U)
775 #else
776 if (READ_BIT(RTC->MISR, RTC_MISR_WUTMF) != 0U)
777 #endif /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
778 {
779 /* Immediately clear flags */
780 WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
781 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
782 /* Call wake up timer registered Callback */
783 hrtc->WakeUpTimerEventCallback(hrtc);
784 #else
785 HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
786 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
787 }
788
789 /* Change RTC state */
790 hrtc->State = HAL_RTC_STATE_READY;
791 }
792
793 /**
794 * @brief Wake Up Timer callback.
795 * @param hrtc RTC handle
796 * @retval None
797 */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)798 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
799 {
800 /* Prevent unused argument(s) compilation warning */
801 UNUSED(hrtc);
802
803 /* NOTE : This function should not be modified, when the callback is needed,
804 the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
805 */
806 }
807
808 /**
809 * @brief Handle Wake Up Timer Polling.
810 * @param hrtc RTC handle
811 * @param Timeout Timeout duration
812 * @retval HAL status
813 */
HAL_RTCEx_PollForWakeUpTimerEvent(const RTC_HandleTypeDef * hrtc,uint32_t Timeout)814 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(const RTC_HandleTypeDef *hrtc, uint32_t Timeout)
815 {
816 /* Prevent unused argument(s) compilation warning */
817 UNUSED(hrtc);
818
819 uint32_t tickstart = HAL_GetTick();
820
821 while (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
822 {
823 if (Timeout != HAL_MAX_DELAY)
824 {
825 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
826 {
827 /* New check to avoid false timeout detection in case of preemption */
828 if (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
829 {
830 return HAL_TIMEOUT;
831 }
832 else
833 {
834 break;
835 }
836 }
837 }
838 }
839
840 /* Clear the WAKEUPTIMER Flag */
841 WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
842
843 return HAL_OK;
844 }
845
846 /**
847 * @}
848 */
849
850 /** @addtogroup RTCEx_Exported_Functions_Group3
851 * @brief Extended Peripheral Control functions
852 *
853 @verbatim
854 ===============================================================================
855 ##### Extended Peripheral Control functions #####
856 ===============================================================================
857 [..]
858 This subsection provides functions allowing to
859 (+) Write a data in a specified RTC Backup data register
860 (+) Read a data in a specified RTC Backup data register
861 (+) Set the Coarse calibration parameters.
862 (+) Deactivate the Coarse calibration parameters
863 (+) Set the Smooth calibration parameters.
864 (+) Set Low Power calibration parameter.
865 (+) Configure the Synchronization Shift Control Settings.
866 (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
867 (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
868 (+) Enable the RTC reference clock detection.
869 (+) Disable the RTC reference clock detection.
870 (+) Enable the Bypass Shadow feature.
871 (+) Disable the Bypass Shadow feature.
872
873 @endverbatim
874 * @{
875 */
876
877 /**
878 * @brief Set the Smooth calibration parameters.
879 * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
880 * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
881 * SmoothCalibMinusPulsesValue must be equal to 0.
882 * @param hrtc RTC handle
883 * @param SmoothCalibPeriod Select the Smooth Calibration Period.
884 * This parameter can be one of the following values :
885 * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
886 * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
887 * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
888 * @param SmoothCalibPlusPulses Select to Set or reset the CALP bit.
889 * This parameter can be one of the following values:
890 * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
891 * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
892 * @param SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
893 * This parameter can be one any value from 0 to 0x000001FF.
894 * @retval HAL status
895 */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)896 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod,
897 uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
898 {
899 uint32_t tickstart;
900
901 /* Check the parameters */
902 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
903 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
904 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
905
906 /* Process Locked */
907 __HAL_LOCK(hrtc);
908
909 /* Change RTC state */
910 hrtc->State = HAL_RTC_STATE_BUSY;
911
912 tickstart = HAL_GetTick();
913
914 /* check if a calibration is pending */
915 while (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
916 {
917 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
918 {
919 /* New check to avoid false timeout detection in case of preemption */
920 if (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
921 {
922
923 /* Change RTC state */
924 hrtc->State = HAL_RTC_STATE_TIMEOUT;
925
926 /* Process Unlocked */
927 __HAL_UNLOCK(hrtc);
928
929 return HAL_TIMEOUT;
930 }
931 else
932 {
933 break;
934 }
935 }
936 }
937
938 /* Disable the write protection for RTC registers */
939 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
940
941 /* Configure the Smooth calibration settings */
942 MODIFY_REG(RTC->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM),
943 (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
944
945 /* Enable the write protection for RTC registers */
946 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
947
948 /* Change RTC state */
949 hrtc->State = HAL_RTC_STATE_READY;
950
951 /* Process Unlocked */
952 __HAL_UNLOCK(hrtc);
953
954 return HAL_OK;
955 }
956
957 /**
958 * @brief Select the low power Calibration mode.
959 * @param hrtc: RTC handle
960 * @param LowPowerCalib: Low power Calibration mode.
961 * This parameter can be one of the following values :
962 * @arg RTC_LPCAL_SET: Low power mode.
963 * @arg RTC_LPCAL_RESET: High consumption mode.
964 * @retval HAL status
965 */
HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef * hrtc,uint32_t LowPowerCalib)966 HAL_StatusTypeDef HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef *hrtc, uint32_t LowPowerCalib)
967 {
968 /* Check the parameters */
969 assert_param(IS_RTC_LOW_POWER_CALIB(LowPowerCalib));
970
971 /* Process Locked */
972 __HAL_LOCK(hrtc);
973
974 /* Change RTC state */
975 hrtc->State = HAL_RTC_STATE_BUSY;
976
977 /* Disable the write protection for RTC registers */
978 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
979
980 /* Configure the Smooth calibration settings */
981 MODIFY_REG(RTC->CALR, RTC_CALR_LPCAL, LowPowerCalib);
982
983 /* Enable the write protection for RTC registers */
984 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
985
986 /* Change RTC state */
987 hrtc->State = HAL_RTC_STATE_READY;
988
989 /* Process Unlocked */
990 __HAL_UNLOCK(hrtc);
991
992 return HAL_OK;
993 }
994
995 /**
996 * @brief Configure the Synchronization Shift Control Settings.
997 * @note When REFCKON is set, firmware must not write to Shift control register.
998 * @param hrtc RTC handle
999 * @param ShiftAdd1S Select to add or not 1 second to the time calendar.
1000 * This parameter can be one of the following values:
1001 * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1002 * @arg RTC_SHIFTADD1S_RESET: No effect.
1003 * @param ShiftSubFS Select the number of Second Fractions to substitute.
1004 * This parameter can be one any value from 0 to 0x7FFF.
1005 * @retval HAL status
1006 */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)1007 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1008 {
1009 uint32_t tickstart;
1010
1011 /* Check the parameters */
1012 assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1013 assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1014
1015 /* Process Locked */
1016 __HAL_LOCK(hrtc);
1017
1018 /* Change RTC state */
1019 hrtc->State = HAL_RTC_STATE_BUSY;
1020
1021 tickstart = HAL_GetTick();
1022
1023 /* Wait until the shift is completed */
1024 while (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
1025 {
1026 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1027 {
1028 /* New check to avoid false timeout detection in case of preemption */
1029 if (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
1030 {
1031 /* Change RTC state */
1032 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1033
1034 /* Process Unlocked */
1035 __HAL_UNLOCK(hrtc);
1036
1037 return HAL_TIMEOUT;
1038 }
1039 else
1040 {
1041 break;
1042 }
1043 }
1044 }
1045
1046 /* Disable the write protection for RTC registers */
1047 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1048
1049 #if defined(RTC_CR_REFCKON)
1050 /* Check if the reference clock detection is disabled */
1051 if (READ_BIT(RTC->CR, RTC_CR_REFCKON) == 0U)
1052 {
1053 #endif /* RTC_CR_REFCKON */
1054 /* Configure the Shift settings */
1055 MODIFY_REG(RTC->SHIFTR, RTC_SHIFTR_SUBFS, (uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S));
1056
1057 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1058 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1059 {
1060 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1061 {
1062 /* Enable the write protection for RTC registers */
1063 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1064
1065 /* Change RTC state */
1066 hrtc->State = HAL_RTC_STATE_ERROR;
1067
1068 /* Process Unlocked */
1069 __HAL_UNLOCK(hrtc);
1070
1071 return HAL_ERROR;
1072 }
1073 }
1074 #if defined(RTC_CR_REFCKON)
1075 }
1076 else
1077 {
1078 /* Enable the write protection for RTC registers */
1079 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1080
1081 /* Change RTC state */
1082 hrtc->State = HAL_RTC_STATE_ERROR;
1083
1084 /* Process Unlocked */
1085 __HAL_UNLOCK(hrtc);
1086
1087 return HAL_ERROR;
1088 }
1089 #endif /* RTC_CR_REFCKON */
1090
1091 /* Enable the write protection for RTC registers */
1092 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1093
1094 /* Change RTC state */
1095 hrtc->State = HAL_RTC_STATE_READY;
1096
1097 /* Process Unlocked */
1098 __HAL_UNLOCK(hrtc);
1099
1100 return HAL_OK;
1101 }
1102
1103 #if defined(RTC_CR_COSEL)
1104 /**
1105 * @brief Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1106 * @param hrtc RTC handle
1107 * @param CalibOutput Select the Calibration output Selection .
1108 * This parameter can be one of the following values:
1109 * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1110 * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1111 * @retval HAL status
1112 */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1113 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1114 {
1115 /* Check the parameters */
1116 assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1117
1118 /* Process Locked */
1119 __HAL_LOCK(hrtc);
1120
1121 /* Change RTC state */
1122 hrtc->State = HAL_RTC_STATE_BUSY;
1123
1124 /* Configure the RTC_CR register */
1125 MODIFY_REG(RTC->CR, RTC_CR_COSEL, CalibOutput);
1126
1127 /* Enable calibration output */
1128 SET_BIT(RTC->CR, RTC_CR_COE);
1129
1130 /* Change RTC state */
1131 hrtc->State = HAL_RTC_STATE_READY;
1132
1133 /* Process Unlocked */
1134 __HAL_UNLOCK(hrtc);
1135
1136 return HAL_OK;
1137 }
1138
1139 /**
1140 * @brief Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1141 * @param hrtc RTC handle
1142 * @retval HAL status
1143 */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1144 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1145 {
1146 /* Process Locked */
1147 __HAL_LOCK(hrtc);
1148
1149 /* Change RTC state */
1150 hrtc->State = HAL_RTC_STATE_BUSY;
1151
1152 /* Disable calibration output */
1153 CLEAR_BIT(RTC->CR, RTC_CR_COE);
1154
1155 /* Change RTC state */
1156 hrtc->State = HAL_RTC_STATE_READY;
1157
1158 /* Process Unlocked */
1159 __HAL_UNLOCK(hrtc);
1160
1161 return HAL_OK;
1162 }
1163 #endif /* RTC_CR_COSEL */
1164
1165 #if defined(RTC_CR_REFCKON)
1166 /**
1167 * @brief Enable the RTC reference clock detection.
1168 * @param hrtc RTC handle
1169 * @retval HAL status
1170 */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1171 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1172 {
1173 HAL_StatusTypeDef status;
1174
1175 /* Process Locked */
1176 __HAL_LOCK(hrtc);
1177
1178 /* Change RTC state */
1179 hrtc->State = HAL_RTC_STATE_BUSY;
1180
1181 /* Disable the write protection for RTC registers */
1182 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1183
1184 /* Enter Initialization mode */
1185 status = RTC_EnterInitMode(hrtc);
1186 if (status == HAL_OK)
1187 {
1188 /* Enable clockref detection */
1189 SET_BIT(RTC->CR, RTC_CR_REFCKON);
1190
1191 /* Exit Initialization mode */
1192 status = RTC_ExitInitMode(hrtc);
1193 }
1194
1195 /* Enable the write protection for RTC registers */
1196 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1197
1198 if (status == HAL_OK)
1199 {
1200 /* Change RTC state */
1201 hrtc->State = HAL_RTC_STATE_READY;
1202 }
1203
1204 /* Process Unlocked */
1205 __HAL_UNLOCK(hrtc);
1206
1207 return status;
1208 }
1209
1210 /**
1211 * @brief Disable the RTC reference clock detection.
1212 * @param hrtc RTC handle
1213 * @retval HAL status
1214 */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1215 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1216 {
1217 HAL_StatusTypeDef status;
1218
1219 /* Process Locked */
1220 __HAL_LOCK(hrtc);
1221
1222 /* Change RTC state */
1223 hrtc->State = HAL_RTC_STATE_BUSY;
1224
1225 /* Disable the write protection for RTC registers */
1226 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1227
1228 /* Enter Initialization mode */
1229 status = RTC_EnterInitMode(hrtc);
1230 if (status == HAL_OK)
1231 {
1232 /* Disable clockref detection */
1233 CLEAR_BIT(RTC->CR, RTC_CR_REFCKON);
1234
1235 /* Exit Initialization mode */
1236 status = RTC_ExitInitMode(hrtc);
1237 }
1238
1239 /* Enable the write protection for RTC registers */
1240 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1241
1242 if (status == HAL_OK)
1243 {
1244 /* Change RTC state */
1245 hrtc->State = HAL_RTC_STATE_READY;
1246 }
1247
1248 /* Process Unlocked */
1249 __HAL_UNLOCK(hrtc);
1250
1251 return status;
1252 }
1253 #endif /* RTC_CR_REFCKON */
1254
1255 /**
1256 * @brief Enable the Bypass Shadow feature.
1257 * @note When the Bypass Shadow is enabled the calendar value are taken
1258 * directly from the Calendar counter.
1259 * @param hrtc RTC handle
1260 * @retval HAL status
1261 */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1262 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1263 {
1264 /* Process Locked */
1265 __HAL_LOCK(hrtc);
1266
1267 /* Change RTC state */
1268 hrtc->State = HAL_RTC_STATE_BUSY;
1269
1270 /* Set the BYPSHAD bit */
1271 SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1272
1273 /* Change RTC state */
1274 hrtc->State = HAL_RTC_STATE_READY;
1275
1276 /* Process Unlocked */
1277 __HAL_UNLOCK(hrtc);
1278
1279 return HAL_OK;
1280 }
1281
1282 /**
1283 * @brief Disable the Bypass Shadow feature.
1284 * @note When the Bypass Shadow is enabled the calendar value are taken
1285 * directly from the Calendar counter.
1286 * @param hrtc RTC handle
1287 * @retval HAL status
1288 */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1289 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1290 {
1291 /* Process Locked */
1292 __HAL_LOCK(hrtc);
1293
1294 /* Change RTC state */
1295 hrtc->State = HAL_RTC_STATE_BUSY;
1296
1297 /* Reset the BYPSHAD bit */
1298 CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1299
1300 /* Change RTC state */
1301 hrtc->State = HAL_RTC_STATE_READY;
1302
1303 /* Process Unlocked */
1304 __HAL_UNLOCK(hrtc);
1305
1306 return HAL_OK;
1307 }
1308
1309 /**
1310 * @brief Increment Monotonic counter.
1311 * @param hrtc RTC handle
1312 * @param Instance Monotonic counter Instance
1313 * This parameter can be one of the following values :
1314 * @arg RTC_MONOTONIC_COUNTER_1
1315 * @retval HAL status
1316 */
HAL_RTCEx_MonotonicCounterIncrement(const RTC_HandleTypeDef * hrtc,uint32_t Instance)1317 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterIncrement(const RTC_HandleTypeDef *hrtc, uint32_t Instance)
1318 {
1319 /* Prevent unused argument(s) compilation warning */
1320 UNUSED(hrtc);
1321 UNUSED(Instance);
1322
1323 /* This register is read-only only and is incremented by one when a write access is done to this
1324 register. This register cannot roll-over and is frozen when reaching the maximum value. */
1325 CLEAR_REG(TAMP->COUNT1R);
1326
1327 return HAL_OK;
1328 }
1329
1330 /**
1331 * @brief Monotonic counter.
1332 * @param hrtc RTC handle
1333 * @param Instance Monotonic counter Instance
1334 * This parameter can be one of the following values :
1335 * @arg RTC_MONOTONIC_COUNTER_1
1336 * @param pValue Pointer to the counter monotonic counter value
1337 * @retval HAL status
1338 */
HAL_RTCEx_MonotonicCounterGet(const RTC_HandleTypeDef * hrtc,uint32_t Instance,uint32_t * pValue)1339 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterGet(const RTC_HandleTypeDef *hrtc, uint32_t Instance, uint32_t *pValue)
1340 {
1341 /* Prevent unused argument(s) compilation warning */
1342 UNUSED(hrtc);
1343 UNUSED(Instance);
1344
1345 /* This register is read-only only and is incremented by one when a write access is done to this
1346 register. This register cannot roll-over and is frozen when reaching the maximum value. */
1347 *pValue = READ_REG(TAMP->COUNT1R);
1348
1349 return HAL_OK;
1350 }
1351
1352 /**
1353 * @brief Set SSR Underflow detection with Interrupt.
1354 * @note The application must ensure that the EXTI RTC interrupt line is enabled.
1355 * @param hrtc RTC handle
1356 * @retval HAL status
1357 */
HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef * hrtc)1358 HAL_StatusTypeDef HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef *hrtc)
1359 {
1360 /* Process Locked */
1361 __HAL_LOCK(hrtc);
1362
1363 /* Change RTC state */
1364 hrtc->State = HAL_RTC_STATE_BUSY;
1365
1366 /* Enable IT SSRU */
1367 __HAL_RTC_SSRU_ENABLE_IT(hrtc, RTC_IT_SSRU);
1368
1369 /* Change RTC state */
1370 hrtc->State = HAL_RTC_STATE_READY;
1371
1372 /* Process Unlocked */
1373 __HAL_UNLOCK(hrtc);
1374
1375 return HAL_OK;
1376 }
1377
1378 /**
1379 * @brief Deactivate SSR Underflow.
1380 * @param hrtc RTC handle
1381 * @retval HAL status
1382 */
HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef * hrtc)1383 HAL_StatusTypeDef HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef *hrtc)
1384 {
1385 /* Process Locked */
1386 __HAL_LOCK(hrtc);
1387
1388 /* Change RTC state */
1389 hrtc->State = HAL_RTC_STATE_BUSY;
1390
1391 /* In case of interrupt mode is used, the interrupt source must disabled */
1392 __HAL_RTC_SSRU_DISABLE_IT(hrtc, RTC_IT_SSRU);
1393
1394 /* Change RTC state */
1395 hrtc->State = HAL_RTC_STATE_READY;
1396
1397 /* Process Unlocked */
1398 __HAL_UNLOCK(hrtc);
1399
1400 return HAL_OK;
1401 }
1402
1403 /**
1404 * @brief Handle SSR underflow interrupt request.
1405 * @param hrtc RTC handle
1406 * @retval None
1407 */
HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef * hrtc)1408 void HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef *hrtc)
1409 {
1410 /* Get the pending status of the SSR Underflow Interrupt */
1411 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
1412 if (READ_BIT(RTC->SMISR, RTC_SMISR_SSRUMF) != 0U)
1413 #else
1414 if (READ_BIT(RTC->MISR, RTC_MISR_SSRUMF) != 0U)
1415 #endif /* (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
1416 {
1417 /* Immediately clear SSR underflow flag */
1418 WRITE_REG(RTC->SCR, RTC_SCR_CSSRUF);
1419
1420 /* SSRU callback */
1421 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1422 /* Call SSRUEvent registered Callback */
1423 hrtc->SSRUEventCallback(hrtc);
1424 #else
1425 HAL_RTCEx_SSRUEventCallback(hrtc);
1426 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1427
1428 }
1429
1430 /* Change RTC state */
1431 hrtc->State = HAL_RTC_STATE_READY;
1432 }
1433
1434 /**
1435 * @brief SSR underflow callback.
1436 * @param hrtc RTC handle
1437 * @retval None
1438 */
HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef * hrtc)1439 __weak void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc)
1440 {
1441 /* Prevent unused argument(s) compilation warning */
1442 UNUSED(hrtc);
1443
1444 /* NOTE : This function should not be modified, when the callback is needed,
1445 the HAL_RTCEx_SSRUEventCallback could be implemented in the user file
1446 */
1447 }
1448
1449 /**
1450 * @}
1451 */
1452
1453 /** @addtogroup RTCEx_Exported_Functions_Group4
1454 * @brief Extended features functions
1455 *
1456 @verbatim
1457 ===============================================================================
1458 ##### Extended features functions #####
1459 ===============================================================================
1460 [..] This section provides functions allowing to:
1461 (+) RTC Alarm B callback
1462 (+) RTC Poll for Alarm B request
1463
1464 @endverbatim
1465 * @{
1466 */
1467
1468 /**
1469 * @brief Alarm B callback.
1470 * @param hrtc RTC handle
1471 * @retval None
1472 */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1473 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1474 {
1475 /* Prevent unused argument(s) compilation warning */
1476 UNUSED(hrtc);
1477
1478 /* NOTE : This function should not be modified, when the callback is needed,
1479 the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1480 */
1481 }
1482
1483 /**
1484 * @brief Handle Alarm B Polling request.
1485 * @param hrtc RTC handle
1486 * @param Timeout Timeout duration
1487 * @retval HAL status
1488 */
HAL_RTCEx_PollForAlarmBEvent(const RTC_HandleTypeDef * hrtc,uint32_t Timeout)1489 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(const RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1490 {
1491 /* Prevent unused argument(s) compilation warning */
1492 UNUSED(hrtc);
1493
1494 uint32_t tickstart = HAL_GetTick();
1495
1496 while (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1497 {
1498 if (Timeout != HAL_MAX_DELAY)
1499 {
1500 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1501 {
1502 /* New check to avoid false timeout detection in case of preemption */
1503 if (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1504 {
1505 return HAL_TIMEOUT;
1506 }
1507 else
1508 {
1509 break;
1510 }
1511 }
1512 }
1513 }
1514
1515 /* Clear the Alarm Flag */
1516 WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1517
1518 return HAL_OK;
1519 }
1520
1521 /**
1522 * @}
1523 */
1524
1525 /** @addtogroup RTCEx_Exported_Functions_Group5
1526 * @brief Extended RTC Tamper functions
1527 *
1528 @verbatim
1529 ==============================================================================
1530 ##### Tamper functions #####
1531 ==============================================================================
1532 [..]
1533 (+) Before calling any tamper or internal tamper function, you have to call first
1534 HAL_RTC_Init() function.
1535 (+) In that one you can select to output tamper event on RTC pin.
1536 [..]
1537 (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
1538 or Level according to the Tamper filter (if equal to 0 Edge else Level)
1539 value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
1540 Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
1541 You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
1542 (+) The default configuration of the Tamper erases the backup registers. To avoid
1543 erase, enable the NoErase field on the TAMP_TAMPCR register.
1544 [..]
1545 (+) Enable Internal Tamper and configure it with interrupt, timestamp using
1546 the HAL_RTCEx_SetInternalTamper() function.
1547
1548 @endverbatim
1549 * @{
1550 */
1551
1552
1553 /**
1554 * @brief Set Tamper
1555 * @param hrtc RTC handle
1556 * @param sTamper Pointer to Tamper Structure.
1557 * @retval HAL status
1558 */
HAL_RTCEx_SetTamper(const RTC_HandleTypeDef * hrtc,const RTC_TamperTypeDef * sTamper)1559 HAL_StatusTypeDef HAL_RTCEx_SetTamper(const RTC_HandleTypeDef *hrtc, const RTC_TamperTypeDef *sTamper)
1560 {
1561 uint32_t tmpreg;
1562
1563 /* Prevent unused argument(s) compilation warning */
1564 UNUSED(hrtc);
1565
1566 /* Check the parameters */
1567 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1568 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1569 assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1570 assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1571 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1572 #if (RTC_TAMP_NB > 2U)
1573 /* Mask flag only supported by TAMPER 1, 2 and 3 */
1574 assert_param(!((sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (sTamper->Tamper > RTC_TAMPER_3)));
1575 #else
1576 assert_param(!((sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (sTamper->Tamper > RTC_TAMPER_2)));
1577 #endif /* (RTC_TAMP_NB > 2U) */
1578 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1579 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1580 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1581 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1582 /* Trigger and Filter have exclusive configurations */
1583 assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) &&
1584 ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) ||
1585 (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL))) ||
1586 ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) &&
1587 ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) ||
1588 (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1589
1590 /* Configuration register 2 */
1591 tmpreg = READ_REG(TAMP->CR2);
1592 tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) |
1593 (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1594
1595 if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1596 {
1597 tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1598 }
1599
1600 if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1601 {
1602 tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1603 }
1604
1605 if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1606 {
1607 tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1608 }
1609 WRITE_REG(TAMP->CR2, tmpreg);
1610
1611 /* Filter control register */
1612 WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration |
1613 sTamper->TamperPullUp);
1614
1615 /* Timestamp on tamper */
1616 if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1617 {
1618 MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1619 }
1620
1621 /* Control register 1 */
1622 SET_BIT(TAMP->CR1, sTamper->Tamper);
1623
1624 return HAL_OK;
1625 }
1626
1627
1628 /**
1629 * @brief Set Tamper in IT mode
1630 * @note The application must ensure that the EXTI TAMP interrupt line is enabled.
1631 * @param hrtc RTC handle
1632 * @param sTamper Pointer to Tamper Structure.
1633 * @retval HAL status
1634 */
HAL_RTCEx_SetTamper_IT(const RTC_HandleTypeDef * hrtc,const RTC_TamperTypeDef * sTamper)1635 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(const RTC_HandleTypeDef *hrtc, const RTC_TamperTypeDef *sTamper)
1636 {
1637 uint32_t tmpreg;
1638
1639 /* Prevent unused argument(s) compilation warning */
1640 UNUSED(hrtc);
1641
1642 /* Check the parameters */
1643 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1644 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1645 assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1646 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1647 /* The interrupt must not be enabled when TAMPxMSK is set. */
1648 assert_param(sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_DISABLE);
1649 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1650 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1651 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1652 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1653 /* Trigger and Filter have exclusive configurations */
1654 assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) &&
1655 ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) ||
1656 (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL))) ||
1657 ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) &&
1658 ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) ||
1659 (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1660
1661 /* Configuration register 2 */
1662 tmpreg = READ_REG(TAMP->CR2);
1663 tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) |
1664 (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1665
1666 if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1667 {
1668 tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1669 }
1670
1671 if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1672 {
1673 tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1674 }
1675 WRITE_REG(TAMP->CR2, tmpreg);
1676
1677 /* Filter control register */
1678 WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration |
1679 sTamper->TamperPullUp);
1680
1681 /* Timestamp on tamper */
1682 if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1683 {
1684 MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1685 }
1686
1687 /* Interrupt enable register */
1688 SET_BIT(TAMP->IER, sTamper->Tamper);
1689
1690 /* Control register 1 */
1691 SET_BIT(TAMP->CR1, sTamper->Tamper);
1692
1693 return HAL_OK;
1694 }
1695
1696 /**
1697 * @brief Deactivate Tamper.
1698 * @param hrtc RTC handle
1699 * @param Tamper Selected tamper pin.
1700 * This parameter can be a combination of the following values:
1701 * @arg RTC_TAMPER_1
1702 * @arg RTC_TAMPER_2
1703 * @arg RTC_TAMPER_3
1704 * @arg RTC_TAMPER_4
1705 * @arg RTC_TAMPER_5
1706 * @arg RTC_TAMPER_6
1707 * @arg RTC_TAMPER_7
1708 * @arg RTC_TAMPER_8
1709 * @retval HAL status
1710 */
HAL_RTCEx_DeactivateTamper(const RTC_HandleTypeDef * hrtc,uint32_t Tamper)1711 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(const RTC_HandleTypeDef *hrtc, uint32_t Tamper)
1712 {
1713 /* Prevent unused argument(s) compilation warning */
1714 UNUSED(hrtc);
1715
1716 assert_param(IS_RTC_TAMPER(Tamper));
1717
1718 /* Disable the selected Tamper pin */
1719 CLEAR_BIT(TAMP->CR1, Tamper);
1720
1721 /* Clear tamper interrupt and event flags (WO register) */
1722 WRITE_REG(TAMP->SCR, Tamper);
1723
1724 /* Clear tamper mask/noerase/trigger configuration */
1725 CLEAR_BIT(TAMP->CR2, (Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | \
1726 (Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1727
1728 /* Clear tamper interrupt mode configuration */
1729 CLEAR_BIT(TAMP->IER, Tamper);
1730
1731 return HAL_OK;
1732 }
1733
1734 /**
1735 * @brief Set all active Tampers at the same time.
1736 * @note For interrupt mode, the application must ensure that the EXTI TAMP interrupt line is enabled.
1737 * @param hrtc RTC handle
1738 * @param sAllTamper Pointer to active Tamper Structure.
1739 * @retval HAL status
1740 */
HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef * hrtc,const RTC_ActiveTampersTypeDef * sAllTamper)1741 HAL_StatusTypeDef HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef *hrtc, const RTC_ActiveTampersTypeDef *sAllTamper)
1742 {
1743 uint32_t tmp_ier;
1744 uint32_t tmp_cr1;
1745 uint32_t tmp_cr2;
1746 uint32_t tmp_atcr1;
1747 uint32_t tmp_atcr2;
1748 uint32_t tmp_cr;
1749 uint32_t i;
1750 uint32_t tickstart;
1751
1752 #ifdef USE_FULL_ASSERT
1753 for (i = 0; i < RTC_TAMP_NB; i++)
1754 {
1755 assert_param(IS_RTC_TAMPER_ERASE_MODE(sAllTamper->TampInput[i].NoErase));
1756 assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sAllTamper->TampInput[i].MaskFlag));
1757 /* Mask flag only supported by TAMPER 1, 2 and 3 */
1758 assert_param(!((sAllTamper->TampInput[i].MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE) &&
1759 (i >= RTC_TAMPER_MASKABLE_NB)));
1760 }
1761 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sAllTamper->TimeStampOnTamperDetection));
1762 assert_param(IS_RTC_ATAMPER_FILTER(sAllTamper->ActiveFilter));
1763 assert_param(IS_RTC_ATAMPER_OUTPUT_CHANGE_PERIOD(sAllTamper->ActiveOutputChangePeriod));
1764 assert_param(IS_RTC_ATAMPER_ASYNCPRES_RTCCLK(sAllTamper->ActiveAsyncPrescaler));
1765 #endif /* USE_FULL_ASSERT */
1766
1767 /* Active Tampers must not be already enabled */
1768 if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) != 0U)
1769 {
1770 /* Disable all actives tampers with HAL_RTCEx_DeactivateActiveTampers.
1771 No need to check return value because it returns always HAL_OK */
1772 (void) HAL_RTCEx_DeactivateActiveTampers(hrtc);
1773 }
1774
1775 /* Set TimeStamp on tamper detection */
1776 tmp_cr = READ_REG(RTC->CR);
1777 if ((tmp_cr & RTC_CR_TAMPTS) != (sAllTamper->TimeStampOnTamperDetection))
1778 {
1779 MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sAllTamper->TimeStampOnTamperDetection);
1780 }
1781
1782 tmp_cr1 = READ_REG(TAMP->CR1);
1783 tmp_cr2 = READ_REG(TAMP->CR2);
1784 tmp_atcr2 = 0U;
1785 tmp_ier = READ_REG(TAMP->IER);
1786
1787 /* Set common parameters */
1788 tmp_atcr1 = (sAllTamper->ActiveFilter | (sAllTamper->ActiveOutputChangePeriod << TAMP_ATCR1_ATPER_Pos) |
1789 sAllTamper->ActiveAsyncPrescaler);
1790
1791 /* Set specific parameters for each active tamper inputs if enable */
1792 for (i = 0; i < RTC_TAMP_NB; i++)
1793 {
1794 if (sAllTamper->TampInput[i].Enable != RTC_ATAMP_DISABLE)
1795 {
1796 tmp_cr1 |= (TAMP_CR1_TAMP1E << i);
1797 tmp_atcr1 |= (TAMP_ATCR1_TAMP1AM << i);
1798
1799 if (sAllTamper->TampInput[i].Interrupt != RTC_ATAMP_INTERRUPT_DISABLE)
1800 {
1801 /* Interrupt enable register */
1802 tmp_ier |= (TAMP_IER_TAMP1IE << i);
1803 }
1804
1805 if (sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1806 {
1807 tmp_cr2 |= (TAMP_CR2_TAMP1MSK << i);
1808 }
1809
1810 if (sAllTamper->TampInput[i].NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1811 {
1812 tmp_cr2 |= (TAMP_CR2_TAMP1NOERASE << i);
1813 }
1814
1815 /* Configure ATOSELx[] in case of output sharing */
1816 tmp_atcr2 |= sAllTamper->TampInput[i].Output << ((3U * i) + TAMP_ATCR2_ATOSEL1_Pos);
1817
1818 if (i != sAllTamper->TampInput[i].Output)
1819 {
1820 tmp_atcr1 |= TAMP_ATCR1_ATOSHARE;
1821 }
1822 }
1823 }
1824
1825 WRITE_REG(TAMP->IER, tmp_ier);
1826 WRITE_REG(TAMP->ATCR1, tmp_atcr1);
1827 WRITE_REG(TAMP->ATCR2, tmp_atcr2);
1828 WRITE_REG(TAMP->CR2, tmp_cr2);
1829 WRITE_REG(TAMP->CR1, tmp_cr1);
1830
1831 /* Write seed */
1832 for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1833 {
1834 WRITE_REG(TAMP->ATSEEDR, sAllTamper->Seed[i]);
1835 }
1836
1837 /* Wait till RTC SEEDF flag is cleared and if Time out is reached exit */
1838 tickstart = HAL_GetTick();
1839 while (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
1840 {
1841 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1842 {
1843 /* New check to avoid false timeout detection in case of preemption */
1844 if (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
1845 {
1846 /* Change RTC state */
1847 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1848
1849 return HAL_TIMEOUT;
1850 }
1851 else
1852 {
1853 break;
1854 }
1855 }
1856 }
1857
1858 return HAL_OK;
1859 }
1860
1861 /**
1862 * @brief Write a new seed. Active tamper must be enabled.
1863 * @param hrtc RTC handle
1864 * @param pSeed Pointer to active tamper seed values.
1865 * @retval HAL status
1866 */
HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef * hrtc,const uint32_t * pSeed)1867 HAL_StatusTypeDef HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef *hrtc, const uint32_t *pSeed)
1868 {
1869 uint32_t i;
1870 uint32_t tickstart;
1871
1872 /* Active Tampers must be enabled */
1873 if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) == 0U)
1874 {
1875 return HAL_ERROR;
1876 }
1877
1878 for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1879 {
1880 WRITE_REG(TAMP->ATSEEDR, pSeed[i]);
1881 }
1882
1883 /* Wait till RTC SEEDF flag is cleared and if Time out is reached exit */
1884 tickstart = HAL_GetTick();
1885 while (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
1886 {
1887 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1888 {
1889 /* New check to avoid false timeout detection in case of preemption */
1890 if (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
1891 {
1892 /* Change RTC state */
1893 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1894
1895 return HAL_TIMEOUT;
1896 }
1897 else
1898 {
1899 break;
1900 }
1901 }
1902 }
1903
1904 return HAL_OK;
1905 }
1906
1907 #if defined(TAMP_SECCFGR_BHKLOCK)
1908 /**
1909 * @brief Lock the Boot hardware Key
1910 * @param hrtc RTC handle
1911 * @note The backup registers from TAMP_BKP0R to TAMP_BKP7R cannot be accessed neither in
1912 * read nor in write (they are read as 0 and write ignore).
1913 * @retval HAL status
1914 */
HAL_RTCEx_LockBootHardwareKey(const RTC_HandleTypeDef * hrtc)1915 HAL_StatusTypeDef HAL_RTCEx_LockBootHardwareKey(const RTC_HandleTypeDef *hrtc)
1916 {
1917 /* Prevent unused argument(s) compilation warning */
1918 UNUSED(hrtc);
1919
1920 WRITE_REG(TAMP->SECCFGR, TAMP_SECCFGR_BHKLOCK);
1921
1922 return HAL_OK;
1923 }
1924 #endif /* TAMP_SECCFGR_BHKLOCK */
1925
1926 /**
1927 * @brief Deactivate all Active Tampers at the same time.
1928 * @param hrtc RTC handle
1929 * @retval HAL status
1930 */
HAL_RTCEx_DeactivateActiveTampers(const RTC_HandleTypeDef * hrtc)1931 HAL_StatusTypeDef HAL_RTCEx_DeactivateActiveTampers(const RTC_HandleTypeDef *hrtc)
1932 {
1933 /* Prevent unused argument(s) compilation warning */
1934 UNUSED(hrtc);
1935
1936 /* Get Active tampers */
1937 uint32_t atamp_mask = READ_BIT(TAMP->ATCR1, TAMP_ALL);
1938
1939 /* Disable all actives tampers but not passives tampers */
1940 CLEAR_BIT(TAMP->CR1, atamp_mask);
1941
1942 /* Clear tamper interrupt and event flags (WO register) of all actives tampers but not passives tampers */
1943 WRITE_REG(TAMP->SCR, atamp_mask);
1944
1945 /* Disable no erase and mask */
1946 #if (RTC_TAMPER_MASKABLE_NB == 2)
1947 CLEAR_BIT(TAMP->CR2, (atamp_mask | ((atamp_mask & (TAMP_ATCR1_TAMP1AM | TAMP_ATCR1_TAMP2AM)) <<
1948 TAMP_CR2_TAMP1MSK_Pos)));
1949 #else
1950 CLEAR_BIT(TAMP->CR2, (atamp_mask | ((atamp_mask & (TAMP_ATCR1_TAMP1AM | TAMP_ATCR1_TAMP2AM | TAMP_ATCR1_TAMP3AM)) <<
1951 TAMP_CR2_TAMP1MSK_Pos)));
1952 #endif /* RTC_TAMPER_MASKABLE_NB */
1953
1954 /* Clear all active tampers interrupt mode configuration but not passives tampers */
1955 CLEAR_BIT(TAMP->IER, atamp_mask);
1956
1957 /* Set reset value for active tamper control register 1 */
1958 WRITE_REG(TAMP->ATCR1, TAMP_ATCR1_ATCKSEL);
1959
1960 /* Set reset value for active tamper control register 2 */
1961 CLEAR_REG(TAMP->ATCR2);
1962
1963 return HAL_OK;
1964 }
1965
1966
1967 /**
1968 * @brief Tamper event polling.
1969 * @param hrtc RTC handle
1970 * @param Tamper Selected tamper pin.
1971 * This parameter can be a combination of the following values:
1972 * @arg RTC_TAMPER_1
1973 * @arg RTC_TAMPER_2
1974 * @arg RTC_TAMPER_3
1975 * @arg RTC_TAMPER_4
1976 * @arg RTC_TAMPER_5
1977 * @arg RTC_TAMPER_6
1978 * @arg RTC_TAMPER_7
1979 * @arg RTC_TAMPER_8
1980 * @param Timeout Timeout duration
1981 * @retval HAL status
1982 */
HAL_RTCEx_PollForTamperEvent(const RTC_HandleTypeDef * hrtc,uint32_t Tamper,uint32_t Timeout)1983 HAL_StatusTypeDef HAL_RTCEx_PollForTamperEvent(const RTC_HandleTypeDef *hrtc, uint32_t Tamper, uint32_t Timeout)
1984 {
1985 /* Prevent unused argument(s) compilation warning */
1986 UNUSED(hrtc);
1987
1988 assert_param(IS_RTC_TAMPER(Tamper));
1989
1990 uint32_t tickstart = HAL_GetTick();
1991
1992 /* Get the status of the Interrupt */
1993 while (READ_BIT(TAMP->SR, Tamper) != Tamper)
1994 {
1995 if (Timeout != HAL_MAX_DELAY)
1996 {
1997 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1998 {
1999 /* New check to avoid false timeout detection in case of preemption */
2000 if (READ_BIT(TAMP->SR, Tamper) != Tamper)
2001 {
2002 return HAL_TIMEOUT;
2003 }
2004 else
2005 {
2006 break;
2007 }
2008 }
2009 }
2010 }
2011
2012 /* Clear the Tamper Flag */
2013 WRITE_REG(TAMP->SCR, Tamper);
2014
2015 return HAL_OK;
2016 }
2017
2018
2019 /**
2020 * @brief Set Internal Tamper
2021 * @param hrtc RTC handle
2022 * @param sIntTamper Pointer to Internal Tamper Structure.
2023 * @retval HAL status
2024 */
HAL_RTCEx_SetInternalTamper(const RTC_HandleTypeDef * hrtc,const RTC_InternalTamperTypeDef * sIntTamper)2025 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(const RTC_HandleTypeDef *hrtc,
2026 const RTC_InternalTamperTypeDef *sIntTamper)
2027 {
2028 /* Prevent unused argument(s) compilation warning */
2029 UNUSED(hrtc);
2030
2031 /* Check the parameters */
2032 assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
2033 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
2034 assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
2035
2036 /* Timestamp enable on internal tamper */
2037 if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
2038 {
2039 MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
2040 }
2041
2042 /* No Erase Backup register enable for Internal Tamper */
2043 if (sIntTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
2044 {
2045 SET_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos)));
2046 }
2047 else
2048 {
2049 CLEAR_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos)));
2050 }
2051
2052 /* Enable Internal Tamper */
2053 SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
2054
2055 return HAL_OK;
2056 }
2057
2058
2059 /**
2060 * @brief Set Internal Tamper in interrupt mode
2061 * @note The application must ensure that the EXTI TAMP interrupt line is enabled.
2062 * @param hrtc RTC handle
2063 * @param sIntTamper Pointer to Internal Tamper Structure.
2064 * @retval HAL status
2065 */
HAL_RTCEx_SetInternalTamper_IT(const RTC_HandleTypeDef * hrtc,const RTC_InternalTamperTypeDef * sIntTamper)2066 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(const RTC_HandleTypeDef *hrtc,
2067 const RTC_InternalTamperTypeDef *sIntTamper)
2068 {
2069 /* Prevent unused argument(s) compilation warning */
2070 UNUSED(hrtc);
2071
2072 /* Check the parameters */
2073 assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
2074 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
2075 assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
2076
2077 /* Timestamp enable on internal tamper */
2078 if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
2079 {
2080 MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
2081 }
2082
2083 /* Interrupt enable register */
2084 SET_BIT(TAMP->IER, sIntTamper->IntTamper);
2085
2086 /* No Erase Backup register enable for Internal Tamper */
2087 if (sIntTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
2088 {
2089 SET_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos)));
2090 }
2091 else
2092 {
2093 CLEAR_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos)));
2094 }
2095
2096 /* Enable Internal Tamper */
2097 SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
2098
2099 return HAL_OK;
2100 }
2101
2102 /**
2103 * @brief Deactivate Internal Tamper.
2104 * @param hrtc RTC handle
2105 * @param IntTamper Selected internal tamper event.
2106 * This parameter can be any combination of existing internal tampers.
2107 * @retval HAL status
2108 */
HAL_RTCEx_DeactivateInternalTamper(const RTC_HandleTypeDef * hrtc,uint32_t IntTamper)2109 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(const RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
2110 {
2111 /* Prevent unused argument(s) compilation warning */
2112 UNUSED(hrtc);
2113
2114 assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
2115
2116 /* Disable the selected Tamper pin */
2117 CLEAR_BIT(TAMP->CR1, IntTamper);
2118
2119 /* Clear internal tamper interrupt mode configuration */
2120 CLEAR_BIT(TAMP->IER, IntTamper);
2121
2122 /* Clear internal tamper interrupt */
2123 WRITE_REG(TAMP->SCR, IntTamper);
2124
2125 return HAL_OK;
2126 }
2127
2128 /**
2129 * @brief Internal Tamper event polling.
2130 * @param hrtc RTC handle
2131 * @param IntTamper selected tamper.
2132 * This parameter can be any combination of existing internal tampers.
2133 * @param Timeout Timeout duration
2134 * @retval HAL status
2135 */
HAL_RTCEx_PollForInternalTamperEvent(const RTC_HandleTypeDef * hrtc,uint32_t IntTamper,uint32_t Timeout)2136 HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(const RTC_HandleTypeDef *hrtc, uint32_t IntTamper,
2137 uint32_t Timeout)
2138 {
2139 /* Prevent unused argument(s) compilation warning */
2140 UNUSED(hrtc);
2141
2142 assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
2143
2144 uint32_t tickstart = HAL_GetTick();
2145
2146 /* Get the status of the Interrupt */
2147 while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
2148 {
2149 if (Timeout != HAL_MAX_DELAY)
2150 {
2151 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2152 {
2153 /* New check to avoid false timeout detection in case of preemption */
2154 if (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
2155 {
2156 return HAL_TIMEOUT;
2157 }
2158 else
2159 {
2160 break;
2161 }
2162 }
2163 }
2164 }
2165
2166 /* Clear the Tamper Flag */
2167 WRITE_REG(TAMP->SCR, IntTamper);
2168
2169 return HAL_OK;
2170 }
2171
2172 /**
2173 * @brief Handle Tamper interrupt request.
2174 * @param hrtc RTC handle
2175 * @retval None
2176 */
HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef * hrtc)2177 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
2178 {
2179 /* Get the pending status of the Tampers Interrupt */
2180 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
2181 uint32_t tmp = READ_REG(TAMP->SMISR);
2182 #else
2183 uint32_t tmp = READ_REG(TAMP->MISR);
2184 #endif /* defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
2185
2186 /* Check Tamper1 status */
2187 if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
2188 {
2189 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2190 /* Call Tamper 1 Event registered secure Callback */
2191 hrtc->Tamper1EventCallback(hrtc);
2192 #else
2193 /* Tamper1 secure callback */
2194 HAL_RTCEx_Tamper1EventCallback(hrtc);
2195 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2196 }
2197
2198 /* Check Tamper2 status */
2199 if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
2200 {
2201 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2202 /* Call Tamper 2 Event registered secure Callback */
2203 hrtc->Tamper2EventCallback(hrtc);
2204 #else
2205 /* Tamper2 secure callback */
2206 HAL_RTCEx_Tamper2EventCallback(hrtc);
2207 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2208 }
2209
2210 #if (RTC_TAMP_NB > 2U)
2211 /* Check Tamper3 status */
2212 if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
2213 {
2214 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2215 /* Call Tamper 3 Event registered secure Callback */
2216 hrtc->Tamper3EventCallback(hrtc);
2217 #else
2218 /* Tamper3 secure callback */
2219 HAL_RTCEx_Tamper3EventCallback(hrtc);
2220 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2221 }
2222
2223 /* Check Tamper4 status */
2224 if ((tmp & RTC_TAMPER_4) == RTC_TAMPER_4)
2225 {
2226 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2227 /* Call Tamper 4 Event registered secure Callback */
2228 hrtc->Tamper4EventCallback(hrtc);
2229 #else
2230 /* Tamper4 secure callback */
2231 HAL_RTCEx_Tamper4EventCallback(hrtc);
2232 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2233 }
2234
2235 /* Check Tamper5 status */
2236 if ((tmp & RTC_TAMPER_5) == RTC_TAMPER_5)
2237 {
2238 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2239 /* Call Tamper 5 Event registered secure Callback */
2240 hrtc->Tamper5EventCallback(hrtc);
2241 #else
2242 /* Tamper5 secure callback */
2243 HAL_RTCEx_Tamper5EventCallback(hrtc);
2244 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2245 }
2246
2247 /* Check Tamper6 status */
2248 if ((tmp & RTC_TAMPER_6) == RTC_TAMPER_6)
2249 {
2250 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2251 /* Call Tamper 6 Event registered secure Callback */
2252 hrtc->Tamper6EventCallback(hrtc);
2253 #else
2254 /* Tamper6 secure callback */
2255 HAL_RTCEx_Tamper6EventCallback(hrtc);
2256 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2257 }
2258
2259 /* Check Tamper7 status */
2260 if ((tmp & RTC_TAMPER_7) == RTC_TAMPER_7)
2261 {
2262 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2263 /* Call Tamper 7 Event registered secure Callback */
2264 hrtc->Tamper7EventCallback(hrtc);
2265 #else
2266 /* Tamper7 secure callback */
2267 HAL_RTCEx_Tamper7EventCallback(hrtc);
2268 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2269 }
2270
2271 /* Check Tamper8 status */
2272 if ((tmp & RTC_TAMPER_8) == RTC_TAMPER_8)
2273 {
2274 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2275 /* Call Tamper 8 Event registered secure Callback */
2276 hrtc->Tamper8EventCallback(hrtc);
2277 #else
2278 /* Tamper8 secure callback */
2279 HAL_RTCEx_Tamper8EventCallback(hrtc);
2280 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2281 }
2282 #endif /* (RTC_TAMP_NB > 2U) */
2283
2284 /* Check Internal Tamper1 status */
2285 if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
2286 {
2287 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2288 /* Call Internal Tamper 1 Event registered secure Callback */
2289 hrtc->InternalTamper1EventCallback(hrtc);
2290 #else
2291 /* Internal Tamper1 secure callback */
2292 HAL_RTCEx_InternalTamper1EventCallback(hrtc);
2293 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2294 }
2295
2296 /* Check Internal Tamper2 status */
2297 if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
2298 {
2299 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2300 /* Call Internal Tamper 2 Event registered secure Callback */
2301 hrtc->InternalTamper2EventCallback(hrtc);
2302 #else
2303 /* Internal Tamper2 secure callback */
2304 HAL_RTCEx_InternalTamper2EventCallback(hrtc);
2305 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2306 }
2307
2308 /* Check Internal Tamper3 status */
2309 if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
2310 {
2311 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2312 /* Call Internal Tamper 3 Event registered secure Callback */
2313 hrtc->InternalTamper3EventCallback(hrtc);
2314 #else
2315 /* Internal Tamper3 secure callback */
2316 HAL_RTCEx_InternalTamper3EventCallback(hrtc);
2317 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2318 }
2319
2320 /* Check Internal Tamper4 status */
2321 if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
2322 {
2323 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2324 /* Call Internal Tamper 4 Event registered secure Callback */
2325 hrtc->InternalTamper4EventCallback(hrtc);
2326 #else
2327 /* Internal Tamper4 secure callback */
2328 HAL_RTCEx_InternalTamper4EventCallback(hrtc);
2329 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2330 }
2331
2332 /* Check Internal Tamper5 status */
2333 if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
2334 {
2335 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2336 /* Call Internal Tamper 5 Event registered secure Callback */
2337 hrtc->InternalTamper5EventCallback(hrtc);
2338 #else
2339 /* Internal Tamper5 secure callback */
2340 HAL_RTCEx_InternalTamper5EventCallback(hrtc);
2341 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2342 }
2343
2344 /* Check Internal Tamper6 status */
2345 if ((tmp & RTC_INT_TAMPER_6) == RTC_INT_TAMPER_6)
2346 {
2347 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2348 /* Call Internal Tamper 6 Event registered secure Callback */
2349 hrtc->InternalTamper6EventCallback(hrtc);
2350 #else
2351 /* Internal Tamper6 secure callback */
2352 HAL_RTCEx_InternalTamper6EventCallback(hrtc);
2353 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2354 }
2355
2356 /* Check Internal Tamper7 status */
2357 if ((tmp & RTC_INT_TAMPER_7) == RTC_INT_TAMPER_7)
2358 {
2359 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2360 /* Call Internal Tamper 7 Event registered secure Callback */
2361 hrtc->InternalTamper7EventCallback(hrtc);
2362 #else
2363 /* Internal Tamper7 secure callback */
2364 HAL_RTCEx_InternalTamper7EventCallback(hrtc);
2365 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2366 }
2367
2368 /* Check Internal Tamper8 status */
2369 if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
2370 {
2371 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2372 /* Call Internal Tamper 8 Event registered secure Callback */
2373 hrtc->InternalTamper8EventCallback(hrtc);
2374 #else
2375 /* Internal Tamper8 secure callback */
2376 HAL_RTCEx_InternalTamper8EventCallback(hrtc);
2377 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2378 }
2379
2380 /* Check Internal Tamper9 status */
2381 if ((tmp & RTC_INT_TAMPER_9) == RTC_INT_TAMPER_9)
2382 {
2383 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2384 /* Call Internal Tamper 9 Event registered secure Callback */
2385 hrtc->InternalTamper9EventCallback(hrtc);
2386 #else
2387 /* Internal Tamper9 secure callback */
2388 HAL_RTCEx_InternalTamper9EventCallback(hrtc);
2389 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2390 }
2391
2392 /* Check Internal Tamper11 status */
2393 if ((tmp & RTC_INT_TAMPER_11) == RTC_INT_TAMPER_11)
2394 {
2395 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2396 /* Call Internal Tamper 11 Event registered secure Callback */
2397 hrtc->InternalTamper11EventCallback(hrtc);
2398 #else
2399 /* Internal Tamper11 secure callback */
2400 HAL_RTCEx_InternalTamper11EventCallback(hrtc);
2401 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2402 }
2403
2404 /* Check Internal Tamper12 status */
2405 if ((tmp & RTC_INT_TAMPER_12) == RTC_INT_TAMPER_12)
2406 {
2407 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2408 /* Call Internal Tamper 12 Event registered secure Callback */
2409 hrtc->InternalTamper12EventCallback(hrtc);
2410 #else
2411 /* Internal Tamper12 secure callback */
2412 HAL_RTCEx_InternalTamper12EventCallback(hrtc);
2413 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2414 }
2415
2416 /* Check Internal Tamper13 status */
2417 if ((tmp & RTC_INT_TAMPER_13) == RTC_INT_TAMPER_13)
2418 {
2419 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2420 /* Call Internal Tamper 13 Event registered secure Callback */
2421 hrtc->InternalTamper13EventCallback(hrtc);
2422 #else
2423 /* Internal Tamper13 secure callback */
2424 HAL_RTCEx_InternalTamper13EventCallback(hrtc);
2425 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2426 }
2427
2428 /* Check Internal Tamper15 status */
2429 if ((tmp & RTC_INT_TAMPER_15) == RTC_INT_TAMPER_15)
2430 {
2431 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2432 /* Call Internal Tamper 15 Event registered secure Callback */
2433 hrtc->InternalTamper15EventCallback(hrtc);
2434 #else
2435 /* Internal Tamper15 secure callback */
2436 HAL_RTCEx_InternalTamper15EventCallback(hrtc);
2437 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2438 }
2439
2440 /* Clear flags after treatment to allow the potential tamper feature */
2441 WRITE_REG(TAMP->SCR, tmp);
2442 }
2443
2444 /**
2445 * @brief Tamper 1 callback.
2446 * @param hrtc RTC handle
2447 * @retval None
2448 */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)2449 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
2450 {
2451 /* Prevent unused argument(s) compilation warning */
2452 UNUSED(hrtc);
2453
2454 /* NOTE : This function should not be modified, when the callback is needed,
2455 the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
2456 */
2457 }
2458
2459 /**
2460 * @brief Tamper 2 callback.
2461 * @param hrtc RTC handle
2462 * @retval None
2463 */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)2464 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
2465 {
2466 /* Prevent unused argument(s) compilation warning */
2467 UNUSED(hrtc);
2468
2469 /* NOTE : This function should not be modified, when the callback is needed,
2470 the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
2471 */
2472 }
2473
2474 /**
2475 * @brief Tamper 3 callback.
2476 * @param hrtc RTC handle
2477 * @retval None
2478 */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)2479 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
2480 {
2481 /* Prevent unused argument(s) compilation warning */
2482 UNUSED(hrtc);
2483
2484 /* NOTE : This function should not be modified, when the callback is needed,
2485 the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
2486 */
2487 }
2488
2489 /**
2490 * @brief Tamper 4 callback.
2491 * @param hrtc RTC handle
2492 * @retval None
2493 */
HAL_RTCEx_Tamper4EventCallback(RTC_HandleTypeDef * hrtc)2494 __weak void HAL_RTCEx_Tamper4EventCallback(RTC_HandleTypeDef *hrtc)
2495 {
2496 /* Prevent unused argument(s) compilation warning */
2497 UNUSED(hrtc);
2498
2499 /* NOTE : This function should not be modified, when the callback is needed,
2500 the HAL_RTCEx_Tamper4EventCallback could be implemented in the user file
2501 */
2502 }
2503
2504 /**
2505 * @brief Tamper 5 callback.
2506 * @param hrtc RTC handle
2507 * @retval None
2508 */
HAL_RTCEx_Tamper5EventCallback(RTC_HandleTypeDef * hrtc)2509 __weak void HAL_RTCEx_Tamper5EventCallback(RTC_HandleTypeDef *hrtc)
2510 {
2511 /* Prevent unused argument(s) compilation warning */
2512 UNUSED(hrtc);
2513
2514 /* NOTE : This function should not be modified, when the callback is needed,
2515 the HAL_RTCEx_Tamper5EventCallback could be implemented in the user file
2516 */
2517 }
2518
2519 /**
2520 * @brief Tamper 6 callback.
2521 * @param hrtc RTC handle
2522 * @retval None
2523 */
HAL_RTCEx_Tamper6EventCallback(RTC_HandleTypeDef * hrtc)2524 __weak void HAL_RTCEx_Tamper6EventCallback(RTC_HandleTypeDef *hrtc)
2525 {
2526 /* Prevent unused argument(s) compilation warning */
2527 UNUSED(hrtc);
2528
2529 /* NOTE : This function should not be modified, when the callback is needed,
2530 the HAL_RTCEx_Tamper6EventCallback could be implemented in the user file
2531 */
2532 }
2533
2534 /**
2535 * @brief Tamper 7 callback.
2536 * @param hrtc RTC handle
2537 * @retval None
2538 */
HAL_RTCEx_Tamper7EventCallback(RTC_HandleTypeDef * hrtc)2539 __weak void HAL_RTCEx_Tamper7EventCallback(RTC_HandleTypeDef *hrtc)
2540 {
2541 /* Prevent unused argument(s) compilation warning */
2542 UNUSED(hrtc);
2543
2544 /* NOTE : This function should not be modified, when the callback is needed,
2545 the HAL_RTCEx_Tamper7EventCallback could be implemented in the user file
2546 */
2547 }
2548
2549 /**
2550 * @brief Tamper 8 callback.
2551 * @param hrtc RTC handle
2552 * @retval None
2553 */
HAL_RTCEx_Tamper8EventCallback(RTC_HandleTypeDef * hrtc)2554 __weak void HAL_RTCEx_Tamper8EventCallback(RTC_HandleTypeDef *hrtc)
2555 {
2556 /* Prevent unused argument(s) compilation warning */
2557 UNUSED(hrtc);
2558
2559 /* NOTE : This function should not be modified, when the callback is needed,
2560 the HAL_RTCEx_Tamper8EventCallback could be implemented in the user file
2561 */
2562 }
2563
2564 /**
2565 * @brief Internal Tamper 1 callback.
2566 * @param hrtc RTC handle
2567 * @retval None
2568 */
HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef * hrtc)2569 __weak void HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef *hrtc)
2570 {
2571 /* Prevent unused argument(s) compilation warning */
2572 UNUSED(hrtc);
2573
2574 /* NOTE : This function should not be modified, when the callback is needed,
2575 the HAL_RTCEx_InternalTamper1EventCallback could be implemented in the user file
2576 */
2577 }
2578
2579 /**
2580 * @brief Internal Tamper 2 callback.
2581 * @param hrtc RTC handle
2582 * @retval None
2583 */
HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef * hrtc)2584 __weak void HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef *hrtc)
2585 {
2586 /* Prevent unused argument(s) compilation warning */
2587 UNUSED(hrtc);
2588
2589 /* NOTE : This function should not be modified, when the callback is needed,
2590 the HAL_RTCEx_InternalTamper2EventCallback could be implemented in the user file
2591 */
2592 }
2593
2594 /**
2595 * @brief Internal Tamper 3 callback.
2596 * @param hrtc RTC handle
2597 * @retval None
2598 */
HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef * hrtc)2599 __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
2600 {
2601 /* Prevent unused argument(s) compilation warning */
2602 UNUSED(hrtc);
2603
2604 /* NOTE : This function should not be modified, when the callback is needed,
2605 the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
2606 */
2607 }
2608
2609 /**
2610 * @brief Internal Tamper 4 callback.
2611 * @param hrtc RTC handle
2612 * @retval None
2613 */
HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef * hrtc)2614 __weak void HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef *hrtc)
2615 {
2616 /* Prevent unused argument(s) compilation warning */
2617 UNUSED(hrtc);
2618
2619 /* NOTE : This function should not be modified, when the callback is needed,
2620 the HAL_RTCEx_InternalTamper4EventCallback could be implemented in the user file
2621 */
2622 }
2623
2624 /**
2625 * @brief Internal Tamper 5 callback.
2626 * @param hrtc RTC handle
2627 * @retval None
2628 */
HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef * hrtc)2629 __weak void HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef *hrtc)
2630 {
2631 /* Prevent unused argument(s) compilation warning */
2632 UNUSED(hrtc);
2633
2634 /* NOTE : This function should not be modified, when the callback is needed,
2635 the HAL_RTCEx_InternalTamper5EventCallback could be implemented in the user file
2636 */
2637 }
2638
2639 /**
2640 * @brief Internal Tamper 6 callback.
2641 * @param hrtc RTC handle
2642 * @retval None
2643 */
HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef * hrtc)2644 __weak void HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef *hrtc)
2645 {
2646 /* Prevent unused argument(s) compilation warning */
2647 UNUSED(hrtc);
2648
2649 /* NOTE : This function should not be modified, when the callback is needed,
2650 the HAL_RTCEx_InternalTamper6EventCallback could be implemented in the user file
2651 */
2652 }
2653
2654 /**
2655 * @brief Internal Tamper 7 callback.
2656 * @param hrtc RTC handle
2657 * @retval None
2658 */
HAL_RTCEx_InternalTamper7EventCallback(RTC_HandleTypeDef * hrtc)2659 __weak void HAL_RTCEx_InternalTamper7EventCallback(RTC_HandleTypeDef *hrtc)
2660 {
2661 /* Prevent unused argument(s) compilation warning */
2662 UNUSED(hrtc);
2663
2664 /* NOTE : This function should not be modified, when the callback is needed,
2665 the HAL_RTCEx_InternalTamper7EventCallback could be implemented in the user file
2666 */
2667 }
2668
2669 /**
2670 * @brief Internal Tamper 8 callback.
2671 * @param hrtc RTC handle
2672 * @retval None
2673 */
HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef * hrtc)2674 __weak void HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef *hrtc)
2675 {
2676 /* Prevent unused argument(s) compilation warning */
2677 UNUSED(hrtc);
2678
2679 /* NOTE : This function should not be modified, when the callback is needed,
2680 the HAL_RTCEx_InternalTamper8EventCallback could be implemented in the user file
2681 */
2682 }
2683
2684 /**
2685 * @brief Internal Tamper 9 callback.
2686 * @param hrtc RTC handle
2687 * @retval None
2688 */
HAL_RTCEx_InternalTamper9EventCallback(RTC_HandleTypeDef * hrtc)2689 __weak void HAL_RTCEx_InternalTamper9EventCallback(RTC_HandleTypeDef *hrtc)
2690 {
2691 /* Prevent unused argument(s) compilation warning */
2692 UNUSED(hrtc);
2693
2694 /* NOTE : This function should not be modified, when the callback is needed,
2695 the HAL_RTCEx_InternalTamper9EventCallback could be implemented in the user file
2696 */
2697 }
2698
2699 /**
2700 * @brief Internal Tamper 11 callback.
2701 * @param hrtc RTC handle
2702 * @retval None
2703 */
HAL_RTCEx_InternalTamper11EventCallback(RTC_HandleTypeDef * hrtc)2704 __weak void HAL_RTCEx_InternalTamper11EventCallback(RTC_HandleTypeDef *hrtc)
2705 {
2706 /* Prevent unused argument(s) compilation warning */
2707 UNUSED(hrtc);
2708
2709 /* NOTE : This function should not be modified, when the callback is needed,
2710 the HAL_RTCEx_InternalTamper11EventCallback could be implemented in the user file
2711 */
2712 }
2713
2714 /**
2715 * @brief Internal Tamper 12 callback.
2716 * @param hrtc RTC handle
2717 * @retval None
2718 */
HAL_RTCEx_InternalTamper12EventCallback(RTC_HandleTypeDef * hrtc)2719 __weak void HAL_RTCEx_InternalTamper12EventCallback(RTC_HandleTypeDef *hrtc)
2720 {
2721 /* Prevent unused argument(s) compilation warning */
2722 UNUSED(hrtc);
2723
2724 /* NOTE : This function should not be modified, when the callback is needed,
2725 the HAL_RTCEx_InternalTamper12EventCallback could be implemented in the user file
2726 */
2727 }
2728
2729 /**
2730 * @brief Internal Tamper 13 callback.
2731 * @param hrtc RTC handle
2732 * @retval None
2733 */
HAL_RTCEx_InternalTamper13EventCallback(RTC_HandleTypeDef * hrtc)2734 __weak void HAL_RTCEx_InternalTamper13EventCallback(RTC_HandleTypeDef *hrtc)
2735 {
2736 /* Prevent unused argument(s) compilation warning */
2737 UNUSED(hrtc);
2738
2739 /* NOTE : This function should not be modified, when the callback is needed,
2740 the HAL_RTCEx_InternalTamper13EventCallback could be implemented in the user file
2741 */
2742 }
2743
2744 /**
2745 * @brief Internal Tamper 15 callback.
2746 * @param hrtc RTC handle
2747 * @retval None
2748 */
HAL_RTCEx_InternalTamper15EventCallback(RTC_HandleTypeDef * hrtc)2749 __weak void HAL_RTCEx_InternalTamper15EventCallback(RTC_HandleTypeDef *hrtc)
2750 {
2751 /* Prevent unused argument(s) compilation warning */
2752 UNUSED(hrtc);
2753
2754 /* NOTE : This function should not be modified, when the callback is needed,
2755 the HAL_RTCEx_InternalTamper15EventCallback could be implemented in the user file
2756 */
2757 }
2758 /**
2759 * @}
2760 */
2761
2762
2763 /** @addtogroup RTCEx_Exported_Functions_Group6
2764 * @brief Extended RTC Backup register functions
2765 *
2766 @verbatim
2767 ===============================================================================
2768 ##### Extended RTC Backup register functions #####
2769 ===============================================================================
2770 [..]
2771 (+) Before calling any tamper or internal tamper function, you have to call first
2772 HAL_RTC_Init() function.
2773 (+) In that one you can select to output tamper event on RTC pin.
2774 [..]
2775 This subsection provides functions allowing to
2776 (+) Write a data in a specified RTC Backup data register
2777 (+) Read a data in a specified RTC Backup data register
2778 @endverbatim
2779 * @{
2780 */
2781
2782
2783 /**
2784 * @brief Write a data in a specified RTC Backup data register.
2785 * @param hrtc RTC handle
2786 * @param BackupRegister RTC Backup data Register number.
2787 * This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2788 * @param Data Data to be written in the specified Backup data register.
2789 * @retval None
2790 */
HAL_RTCEx_BKUPWrite(const RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)2791 void HAL_RTCEx_BKUPWrite(const RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
2792 {
2793 uint32_t tmp;
2794
2795 /* Prevent unused argument(s) compilation warning */
2796 UNUSED(hrtc);
2797
2798 /* Check the parameters */
2799 assert_param(IS_RTC_BKP(BackupRegister));
2800
2801 /* Determine address of the specified Backup register */
2802 tmp = (uint32_t)(&(TAMP->BKP0R));
2803 tmp += (BackupRegister * 4U);
2804
2805 /* Write data in the specified register Backup register */
2806 *(__IO uint32_t *)tmp = (uint32_t)Data;
2807 }
2808
2809
2810 /**
2811 * @brief Reads data from the specified RTC Backup data Register.
2812 * @param hrtc RTC handle
2813 * @param BackupRegister RTC Backup data Register number.
2814 * This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2815 * @retval Read value
2816 */
HAL_RTCEx_BKUPRead(const RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)2817 uint32_t HAL_RTCEx_BKUPRead(const RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
2818 {
2819 uint32_t tmp;
2820
2821 /* Prevent unused argument(s) compilation warning */
2822 UNUSED(hrtc);
2823
2824 /* Check the parameters */
2825 assert_param(IS_RTC_BKP(BackupRegister));
2826
2827 /* Determine address of the specified Backup register */
2828 tmp = (uint32_t)(&(TAMP->BKP0R));
2829 tmp += (BackupRegister * 4U);
2830
2831 /* Read the data from the specified register */
2832 return (*(__IO uint32_t *)tmp);
2833 }
2834
2835 /**
2836 * @brief Reset the RTC Backup data Registers and the device secrets.
2837 * @param hrtc RTC handle
2838 * @retval None
2839 */
HAL_RTCEx_BKUPErase(const RTC_HandleTypeDef * hrtc)2840 void HAL_RTCEx_BKUPErase(const RTC_HandleTypeDef *hrtc)
2841 {
2842 /* Prevent unused argument(s) compilation warning */
2843 UNUSED(hrtc);
2844
2845 SET_BIT(TAMP->CR2, TAMP_CR2_BKERASE);
2846 }
2847
2848 /**
2849 * @brief Block the access to the RTC Backup data Register and all the device secrets.
2850 * @param hrtc RTC handle
2851 * @retval None
2852 */
HAL_RTCEx_BKUPBlock(const RTC_HandleTypeDef * hrtc)2853 void HAL_RTCEx_BKUPBlock(const RTC_HandleTypeDef *hrtc)
2854 {
2855 /* Prevent unused argument(s) compilation warning */
2856 UNUSED(hrtc);
2857
2858 WRITE_REG(TAMP->CR2, TAMP_CR2_BKBLOCK);
2859 }
2860
2861 /**
2862 * @brief Disable the Block to the access to the RTC Backup data Register and the device secrets.
2863 * @param hrtc RTC handle
2864 * @retval None
2865 */
HAL_RTCEx_BKUPUnblock(const RTC_HandleTypeDef * hrtc)2866 void HAL_RTCEx_BKUPUnblock(const RTC_HandleTypeDef *hrtc)
2867 {
2868 /* Prevent unused argument(s) compilation warning */
2869 UNUSED(hrtc);
2870
2871 CLEAR_BIT(TAMP->CR2, TAMP_CR2_BKBLOCK);
2872 }
2873
2874 #ifdef TAMP_ERCFGR_ERCFG0
2875 /**
2876 * @brief Enable and Disable the erase of the configurable Device Secrets
2877 * @note This API must be called before enabling the Tamper.
2878 * @param hrtc RTC handle
2879 * @param DeviceSecretConf Specifies the configuration of the Device Secrets
2880 * This parameter can be a combination of the following values:
2881 * @arg TAMP_DEVICESECRETS_ERASE_NONE
2882 * @arg TAMP_DEVICESECRETS_ERASE_BKPSRAM
2883 *
2884 * @retval None
2885 */
HAL_RTCEx_ConfigEraseDeviceSecrets(const RTC_HandleTypeDef * hrtc,uint32_t DeviceSecretConf)2886 void HAL_RTCEx_ConfigEraseDeviceSecrets(const RTC_HandleTypeDef *hrtc, uint32_t DeviceSecretConf)
2887 {
2888 /* Prevent unused argument(s) compilation warning */
2889 UNUSED(hrtc);
2890
2891 MODIFY_REG(TAMP->ERCFGR, TAMP_ERCFGR_ERCFG0, DeviceSecretConf);
2892 }
2893 #endif /* TAMP_ERCFGR_ERCFG0 */
2894
2895 /**
2896 * @}
2897 */
2898
2899 #if defined(RTC_SECCFGR_SEC)
2900 /** @addtogroup RTCEx_Exported_Functions_Group7
2901 * @brief Extended RTC security functions
2902 *
2903 @verbatim
2904 ===============================================================================
2905 ##### Extended RTC security functions #####
2906 ===============================================================================
2907 [..]
2908 (+) Before calling security function, you have to call first
2909 HAL_RTC_Init() function.
2910 @endverbatim
2911 * @{
2912 */
2913
2914 /**
2915 * @brief Get the security level of the RTC/TAMP/Backup registers.
2916 * To set the secure level please call HAL_RTCEx_SecureModeSet.
2917 * @param hrtc RTC handle
2918 * @param secureState Secure state
2919 * @retval HAL_StatusTypeDef
2920 */
HAL_RTCEx_SecureModeGet(const RTC_HandleTypeDef * hrtc,RTC_SecureStateTypeDef * secureState)2921 HAL_StatusTypeDef HAL_RTCEx_SecureModeGet(const RTC_HandleTypeDef *hrtc, RTC_SecureStateTypeDef *secureState)
2922 {
2923 /* Prevent unused argument(s) compilation warning */
2924 UNUSED(hrtc);
2925
2926 /* Read registers */
2927 uint32_t rtc_seccfgr = READ_REG(RTC->SECCFGR);
2928 uint32_t tamp_seccfgr = READ_REG(TAMP->SECCFGR);
2929
2930 /* RTC */
2931 secureState->rtcSecureFull = READ_BIT(rtc_seccfgr, RTC_SECCFGR_SEC);
2932
2933 /* Warning, rtcNonSecureFeatures is only relevant if secureState->rtcSecureFull == RTC_SECURE_FULL_NO */
2934 secureState->rtcNonSecureFeatures = ~(READ_BIT(rtc_seccfgr, RTC_NONSECURE_FEATURE_ALL)) & RTC_NONSECURE_FEATURE_ALL;
2935
2936 /* TAMP */
2937 secureState->tampSecureFull = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_TAMPSEC);
2938
2939 /* Monotonic Counter */
2940 secureState->MonotonicCounterSecure = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_CNT1SEC);
2941
2942 /* Backup register start zones
2943 Warning : Backup register start zones are shared with privilege configuration */
2944 secureState->backupRegisterStartZone2 = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_BKPRWSEC) >> TAMP_SECCFGR_BKPRWSEC_Pos;
2945 secureState->backupRegisterStartZone3 = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_BKPWSEC) >> TAMP_SECCFGR_BKPWSEC_Pos;
2946
2947 return HAL_OK;
2948 }
2949
2950
2951 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
2952 /**
2953 * @brief Set the security level of the RTC/TAMP/Backup registers.
2954 * To get the current security level call HAL_RTCEx_SecureModeGet.
2955 * @param hrtc RTC handle
2956 * @param secureState Secure state
2957 * @retval HAL_StatusTypeDef
2958 */
HAL_RTCEx_SecureModeSet(const RTC_HandleTypeDef * hrtc,const RTC_SecureStateTypeDef * secureState)2959 HAL_StatusTypeDef HAL_RTCEx_SecureModeSet(const RTC_HandleTypeDef *hrtc, const RTC_SecureStateTypeDef *secureState)
2960 {
2961 /* Prevent unused argument(s) compilation warning */
2962 UNUSED(hrtc);
2963
2964 assert_param(IS_RTC_SECURE_FULL(secureState->rtcSecureFull));
2965 assert_param(IS_RTC_NONSECURE_FEATURES(secureState->rtcNonSecureFeatures));
2966 assert_param(IS_TAMP_SECURE_FULL(secureState->tampSecureFull));
2967 assert_param(IS_RTC_BKP(secureState->backupRegisterStartZone2));
2968 assert_param(IS_RTC_BKP(secureState->backupRegisterStartZone3));
2969 assert_param(IS_TAMP_MONOTONIC_CNT_SECURE(secureState->MonotonicCounterSecure));
2970
2971 /* RTC, rtcNonSecureFeatures is only relevant if secureState->rtcSecureFull == RTC_SECURE_FULL_NO */
2972 WRITE_REG(RTC->SECCFGR, secureState->rtcSecureFull | (~(secureState->rtcNonSecureFeatures) &
2973 RTC_NONSECURE_FEATURE_ALL));
2974
2975 /* Tamper + Backup register + Monotonic counter
2976 Warning : Backup register start zone are Shared with privilege configuration */
2977 WRITE_REG(TAMP->SECCFGR,
2978 secureState->tampSecureFull | secureState->MonotonicCounterSecure |
2979 (TAMP_SECCFGR_BKPRWSEC & (secureState->backupRegisterStartZone2 << TAMP_SECCFGR_BKPRWSEC_Pos)) |
2980 (TAMP_SECCFGR_BKPWSEC & (secureState->backupRegisterStartZone3 << TAMP_SECCFGR_BKPWSEC_Pos)));
2981
2982 return HAL_OK;
2983 }
2984
2985
2986 #endif /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
2987
2988 /**
2989 * @}
2990 */
2991 #endif /* RTC_SECCFGR_SEC */
2992
2993 #if defined(TAMP_PRIVCFGR_TAMPPRIV)
2994 /** @addtogroup RTCEx_Exported_Functions_Group8
2995 * @brief Extended RTC privilege functions
2996 *
2997 @verbatim
2998 ===============================================================================
2999 ##### Extended RTC privilege functions #####
3000 ===============================================================================
3001 [..]
3002 (+) Before calling privilege function, you have to call first
3003 HAL_RTC_Init() function.
3004 @endverbatim
3005 * @{
3006 */
3007
3008 /**
3009 * @brief Set the privilege level of the RTC/TAMP/Backup registers.
3010 * To get the current privilege level call HAL_RTCEx_PrivilegeModeGet.
3011 * @param hrtc RTC handle
3012 * @param privilegeState Privilege state
3013 * @retval HAL_StatusTypeDef
3014 */
HAL_RTCEx_PrivilegeModeSet(const RTC_HandleTypeDef * hrtc,const RTC_PrivilegeStateTypeDef * privilegeState)3015 HAL_StatusTypeDef HAL_RTCEx_PrivilegeModeSet(const RTC_HandleTypeDef *hrtc,
3016 const RTC_PrivilegeStateTypeDef *privilegeState)
3017 {
3018 /* Prevent unused argument(s) compilation warning */
3019 UNUSED(hrtc);
3020
3021 assert_param(IS_RTC_PRIVILEGE_FULL(privilegeState->rtcPrivilegeFull));
3022 assert_param(IS_RTC_PRIVILEGE_FEATURES(privilegeState->rtcPrivilegeFeatures));
3023 assert_param(IS_TAMP_PRIVILEGE_FULL(privilegeState->tampPrivilegeFull));
3024 assert_param(IS_TAMP_MONOTONIC_CNT_PRIVILEGE(privilegeState->MonotonicCounterPrivilege));
3025 assert_param(IS_RTC_PRIVILEGE_BKUP_ZONE(privilegeState->backupRegisterPrivZone));
3026 assert_param(IS_RTC_BKP(privilegeState->backupRegisterStartZone2));
3027 assert_param(IS_RTC_BKP(privilegeState->backupRegisterStartZone3));
3028
3029 /* RTC privilege configuration */
3030 WRITE_REG(RTC->PRIVCFGR, privilegeState->rtcPrivilegeFull | privilegeState->rtcPrivilegeFeatures);
3031
3032 /* TAMP, Monotonic counter and Backup registers privilege configuration
3033 Warning : privilegeState->backupRegisterPrivZone is only writable in secure mode or if trustzone is disabled.
3034 In non secure mode, a notification is generated through a flag/interrupt in the TZIC
3035 (TrustZone interrupt controller). The bits are not written. */
3036 WRITE_REG(TAMP->PRIVCFGR, privilegeState->tampPrivilegeFull | privilegeState->backupRegisterPrivZone | \
3037 privilegeState->MonotonicCounterPrivilege);
3038
3039 /* Backup register start zone
3040 Warning : This parameter is only writable in secure mode or if trustzone is disabled.
3041 In non secure mode, a notification is generated through a flag/interrupt in the TZIC
3042 (TrustZone interrupt controller). The bits are not written.
3043 Warning : Backup register start zones are shared with secure configuration */
3044 #if defined(TAMP_SECCFGR_BKPWSEC)
3045 MODIFY_REG(TAMP->SECCFGR,
3046 (TAMP_SECCFGR_BKPRWSEC | TAMP_SECCFGR_BKPWSEC),
3047 ((privilegeState->backupRegisterStartZone2 << TAMP_SECCFGR_BKPRWSEC_Pos) | \
3048 (privilegeState->backupRegisterStartZone3 << TAMP_SECCFGR_BKPWSEC_Pos)));
3049 #endif /* TAMP_SECCFGR_BKPWSEC */
3050
3051 return HAL_OK;
3052 }
3053
3054 /**
3055 * @brief Get the privilege level of the RTC/TAMP/Backup registers.
3056 * To set the privilege level please call HAL_RTCEx_PrivilegeModeSet.
3057 * @param hrtc RTC handle
3058 * @param privilegeState Privilege state
3059 * @retval HAL_StatusTypeDef
3060 */
HAL_RTCEx_PrivilegeModeGet(const RTC_HandleTypeDef * hrtc,RTC_PrivilegeStateTypeDef * privilegeState)3061 HAL_StatusTypeDef HAL_RTCEx_PrivilegeModeGet(const RTC_HandleTypeDef *hrtc, RTC_PrivilegeStateTypeDef *privilegeState)
3062 {
3063 /* Prevent unused argument(s) compilation warning */
3064 UNUSED(hrtc);
3065
3066 /* Read registers */
3067 uint32_t rtc_privcfgr = READ_REG(RTC->PRIVCFGR);
3068 uint32_t tamp_privcfgr = READ_REG(TAMP->PRIVCFGR);
3069 uint32_t tamp_seccfgr = READ_REG(TAMP->SECCFGR);
3070
3071 /* RTC privilege configuration */
3072 privilegeState->rtcPrivilegeFull = READ_BIT(rtc_privcfgr, RTC_PRIVCFGR_PRIV);
3073
3074 /* Warning, rtcPrivilegeFeatures is only relevant if privilegeState->rtcPrivilegeFull == RTC_PRIVILEGE_FULL_NO */
3075 privilegeState->rtcPrivilegeFeatures = READ_BIT(rtc_privcfgr, RTC_PRIVILEGE_FEATURE_ALL);
3076
3077 /* TAMP and Backup registers privilege configuration */
3078 privilegeState->tampPrivilegeFull = READ_BIT(tamp_privcfgr, TAMP_PRIVCFGR_TAMPPRIV);
3079
3080 /* Monotonic registers privilege configuration */
3081 privilegeState->MonotonicCounterPrivilege = READ_BIT(tamp_privcfgr, TAMP_PRIVCFGR_CNT1PRIV);
3082
3083 /* Backup registers Zones */
3084 privilegeState->backupRegisterPrivZone = READ_BIT(tamp_privcfgr, (TAMP_PRIVCFGR_BKPWPRIV | TAMP_PRIVCFGR_BKPRWPRIV));
3085
3086 /* Backup register start zones
3087 Warning : Shared with secure configuration */
3088 privilegeState->backupRegisterStartZone2 = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_BKPRWSEC) >>
3089 TAMP_SECCFGR_BKPRWSEC_Pos;
3090
3091 privilegeState->backupRegisterStartZone3 = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_BKPWSEC) >>
3092 TAMP_SECCFGR_BKPWSEC_Pos;
3093
3094 return HAL_OK;
3095 }
3096
3097 /**
3098 * @}
3099 */
3100 #endif /* TAMP_PRIVCFGR_TAMPPRIV */
3101
3102 #if defined (RTC_OR_OUT2_RMP)
3103 /**
3104 * @brief Enable RTC OUT2 remap feature.
3105 * @note When enable RTC_OUT2 is mapped on PB2.
3106 * @param hrtc RTC handle
3107 * @retval None
3108 */
HAL_RTCEx_EnableRemapRtcOut2(RTC_HandleTypeDef * hrtc)3109 void HAL_RTCEx_EnableRemapRtcOut2(RTC_HandleTypeDef *hrtc)
3110 {
3111 /* Prevent unused argument(s) compilation warning */
3112 UNUSED(hrtc);
3113
3114 /* Enable RTC OUT2 remap */
3115 SET_BIT(RTC->OR, RTC_OR_OUT2_RMP);
3116 }
3117
3118 /**
3119 * @brief Disable RTC OUT2 remap feature.
3120 * @note When disable RTC_OUT2 is mapped on PI8.
3121 * @param hrtc RTC handle
3122 * @retval None
3123 */
HAL_RTCEx_DisableRemapRtcOut2(RTC_HandleTypeDef * hrtc)3124 void HAL_RTCEx_DisableRemapRtcOut2(RTC_HandleTypeDef *hrtc)
3125 {
3126 /* Prevent unused argument(s) compilation warning */
3127 UNUSED(hrtc);
3128
3129 /* Disable RTC OUT2 remap */
3130 CLEAR_BIT(RTC->OR, RTC_OR_OUT2_RMP);
3131 }
3132 #endif /* defined (RTC_OR_OUT2_RMP) */
3133
3134 /**
3135 * @}
3136 */
3137
3138 #endif /* HAL_RTC_MODULE_ENABLED */
3139
3140 /**
3141 * @}
3142 */
3143
3144
3145 /**
3146 * @}
3147 */
3148