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