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