491ee9d357dbfb7a5525f56b4546bde087049fa2
[mTask.git] / int / com / lib / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_rtc_ex.c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_rtc_ex.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 22-April-2016
7 * @brief RTC HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Real Time Clock (RTC) Extension peripheral:
10 * + RTC Time Stamp functions
11 * + RTC Tamper functions
12 * + RTC Wake-up functions
13 * + Extension Control functions
14 * + Extension RTC features functions
15 *
16 @verbatim
17 ==============================================================================
18 ##### How to use this driver #####
19 ==============================================================================
20 [..]
21 (+) Enable the RTC domain access.
22 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
23 format using the HAL_RTC_Init() function.
24
25 *** RTC Wakeup configuration ***
26 ================================
27 [..]
28 (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
29 function. You can also configure the RTC Wakeup timer in interrupt mode
30 using the HAL_RTC_SetWakeUpTimer_IT() function.
31 (+) To read the RTC WakeUp Counter register, use the HAL_RTC_GetWakeUpTimer()
32 function.
33
34 *** TimeStamp configuration ***
35 ===============================
36 [..]
37 (+) Enables the RTC TimeStamp using the HAL_RTC_SetTimeStamp() function.
38 You can also configure the RTC TimeStamp with interrupt mode using the
39 HAL_RTC_SetTimeStamp_IT() function.
40 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
41 function.
42
43 *** Internal TimeStamp configuration ***
44 ===============================
45 [..]
46 (+) Enables the RTC internal TimeStamp using the HAL_RTC_SetInternalTimeStamp() function.
47 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
48 function.
49
50 *** Tamper configuration ***
51 ============================
52 [..]
53 (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge
54 or Level according to the Tamper filter (if equal to 0 Edge else Level)
55 value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
56 Pull-UP using the HAL_RTC_SetTamper() function. You can configure RTC Tamper
57 with interrupt mode using HAL_RTC_SetTamper_IT() function.
58 (+) The default configuration of the Tamper erases the backup registers. To avoid
59 erase, enable the NoErase field on the RTC_TAMPCR register.
60
61 *** Backup Data Registers configuration ***
62 ===========================================
63 [..]
64 (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
65 function.
66 (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
67 function.
68
69 @endverbatim
70 ******************************************************************************
71 * @attention
72 *
73 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
74 *
75 * Redistribution and use in source and binary forms, with or without modification,
76 * are permitted provided that the following conditions are met:
77 * 1. Redistributions of source code must retain the above copyright notice,
78 * this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright notice,
80 * this list of conditions and the following disclaimer in the documentation
81 * and/or other materials provided with the distribution.
82 * 3. Neither the name of STMicroelectronics nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
85 *
86 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
92 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
93 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
94 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
95 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96 *
97 ******************************************************************************
98 */
99
100 /* Includes ------------------------------------------------------------------*/
101 #include "stm32f7xx_hal.h"
102
103 /** @addtogroup STM32F7xx_HAL_Driver
104 * @{
105 */
106
107 /** @defgroup RTCEx RTCEx
108 * @brief RTC Extended HAL module driver
109 * @{
110 */
111
112 #ifdef HAL_RTC_MODULE_ENABLED
113
114 /* Private typedef -----------------------------------------------------------*/
115 /* Private define ------------------------------------------------------------*/
116 /* Private macro -------------------------------------------------------------*/
117 /* Private variables ---------------------------------------------------------*/
118 /* Private function prototypes -----------------------------------------------*/
119 /* Private functions ---------------------------------------------------------*/
120
121 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
122 * @{
123 */
124
125
126 /** @defgroup RTCEx_Group1 RTC TimeStamp and Tamper functions
127 * @brief RTC TimeStamp and Tamper functions
128 *
129 @verbatim
130 ===============================================================================
131 ##### RTC TimeStamp and Tamper functions #####
132 ===============================================================================
133
134 [..] This section provides functions allowing to configure TimeStamp feature
135
136 @endverbatim
137 * @{
138 */
139
140 /**
141 * @brief Sets TimeStamp.
142 * @note This API must be called before enabling the TimeStamp feature.
143 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
144 * the configuration information for RTC.
145 * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
146 * activated.
147 * This parameter can be one of the following values:
148 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
149 * rising edge of the related pin.
150 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
151 * falling edge of the related pin.
152 * @param RTC_TimeStampPin: specifies the RTC TimeStamp Pin.
153 * This parameter can be one of the following values:
154 * @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
155 * @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin.
156 * @arg RTC_TIMESTAMPPIN_PC1: PC1 is selected as RTC TimeStamp Pin.
157 * @retval HAL status
158 */
159 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
160 {
161 uint32_t tmpreg = 0;
162
163 /* Check the parameters */
164 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
165 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
166
167 /* Process Locked */
168 __HAL_LOCK(hrtc);
169
170 hrtc->State = HAL_RTC_STATE_BUSY;
171
172 /* Get the RTC_CR register and clear the bits to be configured */
173 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
174
175 tmpreg|= TimeStampEdge;
176
177 /* Disable the write protection for RTC registers */
178 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
179
180 hrtc->Instance->OR &= (uint32_t)~RTC_OR_TSINSEL;
181 hrtc->Instance->OR |= (uint32_t)(RTC_TimeStampPin);
182
183 /* Configure the Time Stamp TSEDGE and Enable bits */
184 hrtc->Instance->CR = (uint32_t)tmpreg;
185
186 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
187
188 /* Enable the write protection for RTC registers */
189 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
190
191 /* Change RTC state */
192 hrtc->State = HAL_RTC_STATE_READY;
193
194 /* Process Unlocked */
195 __HAL_UNLOCK(hrtc);
196
197 return HAL_OK;
198 }
199
200 /**
201 * @brief Sets TimeStamp with Interrupt.
202 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
203 * the configuration information for RTC.
204 * @note This API must be called before enabling the TimeStamp feature.
205 * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
206 * activated.
207 * This parameter can be one of the following values:
208 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
209 * rising edge of the related pin.
210 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
211 * falling edge of the related pin.
212 * @param RTC_TimeStampPin: Specifies the RTC TimeStamp Pin.
213 * This parameter can be one of the following values:
214 * @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
215 * @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin.
216 * @arg RTC_TIMESTAMPPIN_PC1: PC1 is selected as RTC TimeStamp Pin.
217 * @retval HAL status
218 */
219 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
220 {
221 uint32_t tmpreg = 0;
222
223 /* Check the parameters */
224 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
225 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
226
227 /* Process Locked */
228 __HAL_LOCK(hrtc);
229
230 hrtc->State = HAL_RTC_STATE_BUSY;
231
232 /* Get the RTC_CR register and clear the bits to be configured */
233 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
234
235 tmpreg |= TimeStampEdge;
236
237 /* Disable the write protection for RTC registers */
238 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
239
240 /* Configure the Time Stamp TSEDGE and Enable bits */
241 hrtc->Instance->CR = (uint32_t)tmpreg;
242
243 hrtc->Instance->OR &= (uint32_t)~RTC_OR_TSINSEL;
244 hrtc->Instance->OR |= (uint32_t)(RTC_TimeStampPin);
245
246 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
247
248 /* Enable IT timestamp */
249 __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
250
251 /* RTC timestamp Interrupt Configuration: EXTI configuration */
252 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
253
254 EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
255
256 /* Enable the write protection for RTC registers */
257 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
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 Deactivates TimeStamp.
269 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
270 * the configuration information for RTC.
271 * @retval HAL status
272 */
273 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
274 {
275 uint32_t tmpreg = 0;
276
277 /* Process Locked */
278 __HAL_LOCK(hrtc);
279
280 hrtc->State = HAL_RTC_STATE_BUSY;
281
282 /* Disable the write protection for RTC registers */
283 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
284
285 /* In case of interrupt mode is used, the interrupt source must disabled */
286 __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
287
288 /* Get the RTC_CR register and clear the bits to be configured */
289 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
290
291 /* Configure the Time Stamp TSEDGE and Enable bits */
292 hrtc->Instance->CR = (uint32_t)tmpreg;
293
294 /* Enable the write protection for RTC registers */
295 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
296
297 hrtc->State = HAL_RTC_STATE_READY;
298
299 /* Process Unlocked */
300 __HAL_UNLOCK(hrtc);
301
302 return HAL_OK;
303 }
304
305 /**
306 * @brief Sets Internal TimeStamp.
307 * @note This API must be called before enabling the internal TimeStamp feature.
308 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
309 * the configuration information for RTC.
310 * @retval HAL status
311 */
312 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
313 {
314 /* Process Locked */
315 __HAL_LOCK(hrtc);
316
317 hrtc->State = HAL_RTC_STATE_BUSY;
318
319 /* Disable the write protection for RTC registers */
320 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
321
322 /* Configure the internal Time Stamp Enable bits */
323 __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);
324
325 /* Enable the write protection for RTC registers */
326 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
327
328 /* Change RTC state */
329 hrtc->State = HAL_RTC_STATE_READY;
330
331 /* Process Unlocked */
332 __HAL_UNLOCK(hrtc);
333
334 return HAL_OK;
335 }
336
337 /**
338 * @brief Deactivates internal TimeStamp.
339 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
340 * the configuration information for RTC.
341 * @retval HAL status
342 */
343 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
344 {
345 /* Process Locked */
346 __HAL_LOCK(hrtc);
347
348 hrtc->State = HAL_RTC_STATE_BUSY;
349
350 /* Disable the write protection for RTC registers */
351 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
352
353 /* Configure the internal Time Stamp Enable bits */
354 __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);
355
356 /* Enable the write protection for RTC registers */
357 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
358
359 hrtc->State = HAL_RTC_STATE_READY;
360
361 /* Process Unlocked */
362 __HAL_UNLOCK(hrtc);
363
364 return HAL_OK;
365 }
366
367 /**
368 * @brief Gets the RTC TimeStamp value.
369 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
370 * the configuration information for RTC.
371 * @param sTimeStamp: Pointer to Time structure
372 * @param sTimeStampDate: Pointer to Date structure
373 * @param Format: specifies the format of the entered parameters.
374 * This parameter can be one of the following values:
375 * FORMAT_BIN: Binary data format
376 * FORMAT_BCD: BCD data format
377 * @retval HAL status
378 */
379 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
380 {
381 uint32_t tmptime = 0, tmpdate = 0;
382
383 /* Check the parameters */
384 assert_param(IS_RTC_FORMAT(Format));
385
386 /* Get the TimeStamp time and date registers values */
387 tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
388 tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
389
390 /* Fill the Time structure fields with the read parameters */
391 sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
392 sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
393 sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
394 sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
395 sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
396
397 /* Fill the Date structure fields with the read parameters */
398 sTimeStampDate->Year = 0;
399 sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
400 sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
401 sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
402
403 /* Check the input parameters format */
404 if(Format == RTC_FORMAT_BIN)
405 {
406 /* Convert the TimeStamp structure parameters to Binary format */
407 sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
408 sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
409 sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
410
411 /* Convert the DateTimeStamp structure parameters to Binary format */
412 sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
413 sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
414 sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
415 }
416
417 /* Clear the TIMESTAMP Flag */
418 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
419
420 return HAL_OK;
421 }
422
423 /**
424 * @brief Sets Tamper
425 * @note By calling this API we disable the tamper interrupt for all tampers.
426 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
427 * the configuration information for RTC.
428 * @param sTamper: Pointer to Tamper Structure.
429 * @retval HAL status
430 */
431 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
432 {
433 uint32_t tmpreg = 0;
434
435 /* Check the parameters */
436 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
437 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
438 assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
439 assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
440 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
441 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
442 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
443 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
444 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
445
446 /* Process Locked */
447 __HAL_LOCK(hrtc);
448
449 hrtc->State = HAL_RTC_STATE_BUSY;
450
451 if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
452 {
453 sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
454 }
455
456 if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
457 {
458 sTamper->NoErase = 0;
459 if((sTamper->Tamper & RTC_TAMPER_1) != 0)
460 {
461 sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
462 }
463 if((sTamper->Tamper & RTC_TAMPER_2) != 0)
464 {
465 sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
466 }
467 if((sTamper->Tamper & RTC_TAMPER_3) != 0)
468 {
469 sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
470 }
471 }
472
473 if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
474 {
475 sTamper->MaskFlag = 0;
476 if((sTamper->Tamper & RTC_TAMPER_1) != 0)
477 {
478 sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
479 }
480 if((sTamper->Tamper & RTC_TAMPER_2) != 0)
481 {
482 sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
483 }
484 if((sTamper->Tamper & RTC_TAMPER_3) != 0)
485 {
486 sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
487 }
488 }
489
490 tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->NoErase |\
491 (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
492 (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
493
494 hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAMPCR_TAMPTS |\
495 (uint32_t)RTC_TAMPCR_TAMPFREQ | (uint32_t)RTC_TAMPCR_TAMPFLT | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
496 (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE | (uint32_t)RTC_TAMPCR_TAMP1IE |\
497 (uint32_t)RTC_TAMPCR_TAMP2IE | (uint32_t)RTC_TAMPCR_TAMP3IE | (uint32_t)RTC_TAMPCR_TAMP1NOERASE |\
498 (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF |\
499 (uint32_t)RTC_TAMPCR_TAMP2MF | (uint32_t)RTC_TAMPCR_TAMP3MF);
500
501 hrtc->Instance->TAMPCR |= tmpreg;
502
503 hrtc->State = HAL_RTC_STATE_READY;
504
505 /* Process Unlocked */
506 __HAL_UNLOCK(hrtc);
507
508 return HAL_OK;
509 }
510
511 /**
512 * @brief Sets Tamper with interrupt.
513 * @note By calling this API we force the tamper interrupt for all tampers.
514 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
515 * the configuration information for RTC.
516 * @param sTamper: Pointer to RTC Tamper.
517 * @retval HAL status
518 */
519 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
520 {
521 uint32_t tmpreg = 0;
522
523 /* Check the parameters */
524 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
525 assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
526 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
527 assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
528 assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
529 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
530 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
531 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
532 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
533 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
534
535 /* Process Locked */
536 __HAL_LOCK(hrtc);
537
538 hrtc->State = HAL_RTC_STATE_BUSY;
539
540 /* Configure the tamper trigger */
541 if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
542 {
543 sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
544 }
545
546 if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
547 {
548 sTamper->NoErase = 0;
549 if((sTamper->Tamper & RTC_TAMPER_1) != 0)
550 {
551 sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
552 }
553 if((sTamper->Tamper & RTC_TAMPER_2) != 0)
554 {
555 sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
556 }
557 if((sTamper->Tamper & RTC_TAMPER_3) != 0)
558 {
559 sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
560 }
561 }
562
563 if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
564 {
565 sTamper->MaskFlag = 0;
566 if((sTamper->Tamper & RTC_TAMPER_1) != 0)
567 {
568 sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
569 }
570 if((sTamper->Tamper & RTC_TAMPER_2) != 0)
571 {
572 sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
573 }
574 if((sTamper->Tamper & RTC_TAMPER_3) != 0)
575 {
576 sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
577 }
578 }
579
580 tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Interrupt | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->NoErase |\
581 (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
582 (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
583
584 hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAMPCR_TAMPTS |\
585 (uint32_t)RTC_TAMPCR_TAMPFREQ | (uint32_t)RTC_TAMPCR_TAMPFLT | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
586 (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE | (uint32_t)RTC_TAMPCR_TAMP1IE |\
587 (uint32_t)RTC_TAMPCR_TAMP2IE | (uint32_t)RTC_TAMPCR_TAMP3IE | (uint32_t)RTC_TAMPCR_TAMP1NOERASE |\
588 (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF |\
589 (uint32_t)RTC_TAMPCR_TAMP2MF | (uint32_t)RTC_TAMPCR_TAMP3MF);
590
591 hrtc->Instance->TAMPCR |= tmpreg;
592
593 /* RTC Tamper Interrupt Configuration: EXTI configuration */
594 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
595
596 EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
597
598 hrtc->State = HAL_RTC_STATE_READY;
599
600 /* Process Unlocked */
601 __HAL_UNLOCK(hrtc);
602
603 return HAL_OK;
604 }
605
606 /**
607 * @brief Deactivates Tamper.
608 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
609 * the configuration information for RTC.
610 * @param Tamper: Selected tamper pin.
611 * This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.
612 * @retval HAL status
613 */
614 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
615 {
616 assert_param(IS_RTC_TAMPER(Tamper));
617
618 /* Process Locked */
619 __HAL_LOCK(hrtc);
620
621 hrtc->State = HAL_RTC_STATE_BUSY;
622
623 /* Disable the selected Tamper pin */
624 hrtc->Instance->TAMPCR &= (uint32_t)~Tamper;
625
626 if ((Tamper & RTC_TAMPER_1) != 0)
627 {
628 /* Disable the Tamper1 interrupt */
629 hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1);
630 }
631 if ((Tamper & RTC_TAMPER_2) != 0)
632 {
633 /* Disable the Tamper2 interrupt */
634 hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2);
635 }
636 if ((Tamper & RTC_TAMPER_3) != 0)
637 {
638 /* Disable the Tamper2 interrupt */
639 hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3);
640 }
641
642 hrtc->State = HAL_RTC_STATE_READY;
643
644 /* Process Unlocked */
645 __HAL_UNLOCK(hrtc);
646
647 return HAL_OK;
648 }
649
650 /**
651 * @brief This function handles TimeStamp interrupt request.
652 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
653 * the configuration information for RTC.
654 * @retval None
655 */
656 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
657 {
658 if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
659 {
660 /* Get the status of the Interrupt */
661 if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
662 {
663 /* TIMESTAMP callback */
664 HAL_RTCEx_TimeStampEventCallback(hrtc);
665
666 /* Clear the TIMESTAMP interrupt pending bit */
667 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
668 }
669 }
670
671 /* Get the status of the Interrupt */
672 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== SET)
673 {
674 /* Get the TAMPER Interrupt enable bit and pending bit */
675 if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
676 (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP1IE)) != (uint32_t)RESET))
677 {
678 /* Tamper callback */
679 HAL_RTCEx_Tamper1EventCallback(hrtc);
680
681 /* Clear the Tamper interrupt pending bit */
682 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
683 }
684 }
685
686 /* Get the status of the Interrupt */
687 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F)== SET)
688 {
689 /* Get the TAMPER Interrupt enable bit and pending bit */
690 if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
691 (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP2IE)) != (uint32_t)RESET))
692 {
693 /* Tamper callback */
694 HAL_RTCEx_Tamper2EventCallback(hrtc);
695
696 /* Clear the Tamper interrupt pending bit */
697 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
698 }
699 }
700
701 /* Get the status of the Interrupt */
702 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F)== SET)
703 {
704 /* Get the TAMPER Interrupt enable bit and pending bit */
705 if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
706 (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP3IE)) != (uint32_t)RESET))
707 {
708 /* Tamper callback */
709 HAL_RTCEx_Tamper3EventCallback(hrtc);
710
711 /* Clear the Tamper interrupt pending bit */
712 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
713 }
714 }
715
716 /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
717 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
718
719 /* Change RTC state */
720 hrtc->State = HAL_RTC_STATE_READY;
721 }
722
723 /**
724 * @brief TimeStamp callback.
725 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
726 * the configuration information for RTC.
727 * @retval None
728 */
729 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
730 {
731 /* Prevent unused argument(s) compilation warning */
732 UNUSED(hrtc);
733
734 /* NOTE : This function Should not be modified, when the callback is needed,
735 the HAL_RTC_TimeStampEventCallback could be implemented in the user file
736 */
737 }
738
739 /**
740 * @brief Tamper 1 callback.
741 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
742 * the configuration information for RTC.
743 * @retval None
744 */
745 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
746 {
747 /* Prevent unused argument(s) compilation warning */
748 UNUSED(hrtc);
749
750 /* NOTE : This function Should not be modified, when the callback is needed,
751 the HAL_RTC_Tamper1EventCallback could be implemented in the user file
752 */
753 }
754
755 /**
756 * @brief Tamper 2 callback.
757 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
758 * the configuration information for RTC.
759 * @retval None
760 */
761 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
762 {
763 /* Prevent unused argument(s) compilation warning */
764 UNUSED(hrtc);
765
766 /* NOTE : This function Should not be modified, when the callback is needed,
767 the HAL_RTC_Tamper2EventCallback could be implemented in the user file
768 */
769 }
770
771 /**
772 * @brief Tamper 3 callback.
773 * @param hrtc: RTC handle
774 * @retval None
775 */
776 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
777 {
778 /* Prevent unused argument(s) compilation warning */
779 UNUSED(hrtc);
780
781 /* NOTE : This function Should not be modified, when the callback is needed,
782 the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
783 */
784 }
785
786 /**
787 * @brief This function handles TimeStamp polling request.
788 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
789 * the configuration information for RTC.
790 * @param Timeout: Timeout duration
791 * @retval HAL status
792 */
793 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
794 {
795 uint32_t tickstart = 0;
796
797 /* Get tick */
798 tickstart = HAL_GetTick();
799
800 while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
801 {
802 if(Timeout != HAL_MAX_DELAY)
803 {
804 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
805 {
806 hrtc->State = HAL_RTC_STATE_TIMEOUT;
807 return HAL_TIMEOUT;
808 }
809 }
810 }
811
812 if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
813 {
814 /* Clear the TIMESTAMP OverRun Flag */
815 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
816
817 /* Change TIMESTAMP state */
818 hrtc->State = HAL_RTC_STATE_ERROR;
819
820 return HAL_ERROR;
821 }
822
823 /* Change RTC state */
824 hrtc->State = HAL_RTC_STATE_READY;
825
826 return HAL_OK;
827 }
828
829 /**
830 * @brief This function handles Tamper1 Polling.
831 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
832 * the configuration information for RTC.
833 * @param Timeout: Timeout duration
834 * @retval HAL status
835 */
836 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
837 {
838 uint32_t tickstart = 0;
839
840 /* Get tick */
841 tickstart = HAL_GetTick();
842
843 /* Get the status of the Interrupt */
844 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
845 {
846 if(Timeout != HAL_MAX_DELAY)
847 {
848 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
849 {
850 hrtc->State = HAL_RTC_STATE_TIMEOUT;
851 return HAL_TIMEOUT;
852 }
853 }
854 }
855
856 /* Clear the Tamper Flag */
857 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
858
859 /* Change RTC state */
860 hrtc->State = HAL_RTC_STATE_READY;
861
862 return HAL_OK;
863 }
864
865 /**
866 * @brief This function handles Tamper2 Polling.
867 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
868 * the configuration information for RTC.
869 * @param Timeout: Timeout duration
870 * @retval HAL status
871 */
872 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
873 {
874 uint32_t tickstart = 0;
875
876 /* Get tick */
877 tickstart = HAL_GetTick();
878
879 /* Get the status of the Interrupt */
880 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
881 {
882 if(Timeout != HAL_MAX_DELAY)
883 {
884 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
885 {
886 hrtc->State = HAL_RTC_STATE_TIMEOUT;
887 return HAL_TIMEOUT;
888 }
889 }
890 }
891
892 /* Clear the Tamper Flag */
893 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
894
895 /* Change RTC state */
896 hrtc->State = HAL_RTC_STATE_READY;
897
898 return HAL_OK;
899 }
900
901 /**
902 * @brief This function handles Tamper3 Polling.
903 * @param hrtc: RTC handle
904 * @param Timeout: Timeout duration
905 * @retval HAL status
906 */
907 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
908 {
909 uint32_t tickstart = HAL_GetTick();
910
911 /* Get the status of the Interrupt */
912 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == RESET)
913 {
914 if(Timeout != HAL_MAX_DELAY)
915 {
916 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
917 {
918 hrtc->State = HAL_RTC_STATE_TIMEOUT;
919 return HAL_TIMEOUT;
920 }
921 }
922 }
923
924 /* Clear the Tamper Flag */
925 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP3F);
926
927 /* Change RTC state */
928 hrtc->State = HAL_RTC_STATE_READY;
929
930 return HAL_OK;
931 }
932
933 /**
934 * @}
935 */
936
937 /** @defgroup RTCEx_Group2 RTC Wake-up functions
938 * @brief RTC Wake-up functions
939 *
940 @verbatim
941 ===============================================================================
942 ##### RTC Wake-up functions #####
943 ===============================================================================
944
945 [..] This section provides functions allowing to configure Wake-up feature
946
947 @endverbatim
948 * @{
949 */
950
951 /**
952 * @brief Sets wake up timer.
953 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
954 * the configuration information for RTC.
955 * @param WakeUpCounter: Wake up counter
956 * @param WakeUpClock: Wake up clock
957 * @retval HAL status
958 */
959 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
960 {
961 uint32_t tickstart = 0;
962
963 /* Check the parameters */
964 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
965 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
966
967 /* Process Locked */
968 __HAL_LOCK(hrtc);
969
970 hrtc->State = HAL_RTC_STATE_BUSY;
971
972 /* Disable the write protection for RTC registers */
973 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
974
975 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
976
977 /* Get tick */
978 tickstart = HAL_GetTick();
979
980 /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
981 if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
982 {
983 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
984 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
985 {
986 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
987 {
988 /* Enable the write protection for RTC registers */
989 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
990
991 hrtc->State = HAL_RTC_STATE_TIMEOUT;
992
993 /* Process Unlocked */
994 __HAL_UNLOCK(hrtc);
995
996 return HAL_TIMEOUT;
997 }
998 }
999 }
1000
1001 /* Clear the Wakeup Timer clock source bits in CR register */
1002 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1003
1004 /* Configure the clock source */
1005 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1006
1007 /* Configure the Wakeup Timer counter */
1008 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1009
1010 /* Enable the Wakeup Timer */
1011 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1012
1013 /* Enable the write protection for RTC registers */
1014 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1015
1016 hrtc->State = HAL_RTC_STATE_READY;
1017
1018 /* Process Unlocked */
1019 __HAL_UNLOCK(hrtc);
1020
1021 return HAL_OK;
1022 }
1023
1024 /**
1025 * @brief Sets wake up timer with interrupt
1026 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1027 * the configuration information for RTC.
1028 * @param WakeUpCounter: Wake up counter
1029 * @param WakeUpClock: Wake up clock
1030 * @retval HAL status
1031 */
1032 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1033 {
1034 uint32_t tickstart = 0;
1035
1036 /* Check the parameters */
1037 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1038 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1039
1040 /* Process Locked */
1041 __HAL_LOCK(hrtc);
1042
1043 hrtc->State = HAL_RTC_STATE_BUSY;
1044
1045 /* Disable the write protection for RTC registers */
1046 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1047
1048 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1049
1050 /* Get tick */
1051 tickstart = HAL_GetTick();
1052
1053 /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
1054 if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
1055 {
1056 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1057 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1058 {
1059 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1060 {
1061 /* Enable the write protection for RTC registers */
1062 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1063
1064 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1065
1066 /* Process Unlocked */
1067 __HAL_UNLOCK(hrtc);
1068
1069 return HAL_TIMEOUT;
1070 }
1071 }
1072 }
1073
1074 /* Configure the Wakeup Timer counter */
1075 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1076
1077 /* Clear the Wakeup Timer clock source bits in CR register */
1078 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1079
1080 /* Configure the clock source */
1081 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1082
1083 /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
1084 __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1085
1086 EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
1087
1088 /* Configure the Interrupt in the RTC_CR register */
1089 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
1090
1091 /* Enable the Wakeup Timer */
1092 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1093
1094 /* Enable the write protection for RTC registers */
1095 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1096
1097 hrtc->State = HAL_RTC_STATE_READY;
1098
1099 /* Process Unlocked */
1100 __HAL_UNLOCK(hrtc);
1101
1102 return HAL_OK;
1103 }
1104
1105 /**
1106 * @brief Deactivates wake up timer counter.
1107 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1108 * the configuration information for RTC.
1109 * @retval HAL status
1110 */
1111 uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1112 {
1113 uint32_t tickstart = 0;
1114
1115 /* Process Locked */
1116 __HAL_LOCK(hrtc);
1117
1118 hrtc->State = HAL_RTC_STATE_BUSY;
1119
1120 /* Disable the write protection for RTC registers */
1121 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1122
1123 /* Disable the Wakeup Timer */
1124 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1125
1126 /* In case of interrupt mode is used, the interrupt source must disabled */
1127 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
1128
1129 /* Get tick */
1130 tickstart = HAL_GetTick();
1131
1132 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1133 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1134 {
1135 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1136 {
1137 /* Enable the write protection for RTC registers */
1138 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1139
1140 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1141
1142 /* Process Unlocked */
1143 __HAL_UNLOCK(hrtc);
1144
1145 return HAL_TIMEOUT;
1146 }
1147 }
1148
1149 /* Enable the write protection for RTC registers */
1150 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1151
1152 hrtc->State = HAL_RTC_STATE_READY;
1153
1154 /* Process Unlocked */
1155 __HAL_UNLOCK(hrtc);
1156
1157 return HAL_OK;
1158 }
1159
1160 /**
1161 * @brief Gets wake up timer counter.
1162 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1163 * the configuration information for RTC.
1164 * @retval Counter value
1165 */
1166 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1167 {
1168 /* Get the counter value */
1169 return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1170 }
1171
1172 /**
1173 * @brief This function handles Wake Up Timer interrupt request.
1174 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1175 * the configuration information for RTC.
1176 * @retval None
1177 */
1178 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1179 {
1180 if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
1181 {
1182 /* Get the status of the Interrupt */
1183 if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
1184 {
1185 /* WAKEUPTIMER callback */
1186 HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1187
1188 /* Clear the WAKEUPTIMER interrupt pending bit */
1189 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1190 }
1191 }
1192
1193 /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1194 __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1195
1196 /* Change RTC state */
1197 hrtc->State = HAL_RTC_STATE_READY;
1198 }
1199
1200 /**
1201 * @brief Wake Up Timer callback.
1202 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1203 * the configuration information for RTC.
1204 * @retval None
1205 */
1206 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1207 {
1208 /* Prevent unused argument(s) compilation warning */
1209 UNUSED(hrtc);
1210
1211 /* NOTE : This function Should not be modified, when the callback is needed,
1212 the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
1213 */
1214 }
1215
1216 /**
1217 * @brief This function handles Wake Up Timer Polling.
1218 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1219 * the configuration information for RTC.
1220 * @param Timeout: Timeout duration
1221 * @retval HAL status
1222 */
1223 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1224 {
1225 uint32_t tickstart = 0;
1226
1227 /* Get tick */
1228 tickstart = HAL_GetTick();
1229
1230 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
1231 {
1232 if(Timeout != HAL_MAX_DELAY)
1233 {
1234 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1235 {
1236 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1237
1238 return HAL_TIMEOUT;
1239 }
1240 }
1241 }
1242
1243 /* Clear the WAKEUPTIMER Flag */
1244 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1245
1246 /* Change RTC state */
1247 hrtc->State = HAL_RTC_STATE_READY;
1248
1249 return HAL_OK;
1250 }
1251
1252 /**
1253 * @}
1254 */
1255
1256
1257 /** @defgroup RTCEx_Group3 Extension Peripheral Control functions
1258 * @brief Extension Peripheral Control functions
1259 *
1260 @verbatim
1261 ===============================================================================
1262 ##### Extension Peripheral Control functions #####
1263 ===============================================================================
1264 [..]
1265 This subsection provides functions allowing to
1266 (+) Write a data in a specified RTC Backup data register
1267 (+) Read a data in a specified RTC Backup data register
1268 (+) Set the Coarse calibration parameters.
1269 (+) Deactivate the Coarse calibration parameters
1270 (+) Set the Smooth calibration parameters.
1271 (+) Configure the Synchronization Shift Control Settings.
1272 (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1273 (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1274 (+) Enable the RTC reference clock detection.
1275 (+) Disable the RTC reference clock detection.
1276 (+) Enable the Bypass Shadow feature.
1277 (+) Disable the Bypass Shadow feature.
1278
1279 @endverbatim
1280 * @{
1281 */
1282
1283 /**
1284 * @brief Writes a data in a specified RTC Backup data register.
1285 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1286 * the configuration information for RTC.
1287 * @param BackupRegister: RTC Backup data Register number.
1288 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1289 * specify the register.
1290 * @param Data: Data to be written in the specified RTC Backup data register.
1291 * @retval None
1292 */
1293 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1294 {
1295 uint32_t tmp = 0;
1296
1297 /* Check the parameters */
1298 assert_param(IS_RTC_BKP(BackupRegister));
1299
1300 tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1301 tmp += (BackupRegister * 4);
1302
1303 /* Write the specified register */
1304 *(__IO uint32_t *)tmp = (uint32_t)Data;
1305 }
1306
1307 /**
1308 * @brief Reads data from the specified RTC Backup data Register.
1309 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1310 * the configuration information for RTC.
1311 * @param BackupRegister: RTC Backup data Register number.
1312 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1313 * specify the register.
1314 * @retval Read value
1315 */
1316 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1317 {
1318 uint32_t tmp = 0;
1319
1320 /* Check the parameters */
1321 assert_param(IS_RTC_BKP(BackupRegister));
1322
1323 tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1324 tmp += (BackupRegister * 4);
1325
1326 /* Read the specified register */
1327 return (*(__IO uint32_t *)tmp);
1328 }
1329
1330 /**
1331 * @brief Sets the Smooth calibration parameters.
1332 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1333 * the configuration information for RTC.
1334 * @param SmoothCalibPeriod: Select the Smooth Calibration Period.
1335 * This parameter can be can be one of the following values :
1336 * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
1337 * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
1338 * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
1339 * @param SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
1340 * This parameter can be one of the following values:
1341 * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulses every 2*11 pulses.
1342 * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1343 * @param SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
1344 * This parameter can be one any value from 0 to 0x000001FF.
1345 * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
1346 * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
1347 * SmouthCalibMinusPulsesValue must be equal to 0.
1348 * @retval HAL status
1349 */
1350 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
1351 {
1352 uint32_t tickstart = 0;
1353
1354 /* Check the parameters */
1355 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1356 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1357 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
1358
1359 /* Process Locked */
1360 __HAL_LOCK(hrtc);
1361
1362 hrtc->State = HAL_RTC_STATE_BUSY;
1363
1364 /* Disable the write protection for RTC registers */
1365 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1366
1367 /* check if a calibration is pending*/
1368 if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
1369 {
1370 /* Get tick */
1371 tickstart = HAL_GetTick();
1372
1373 /* check if a calibration is pending*/
1374 while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
1375 {
1376 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1377 {
1378 /* Enable the write protection for RTC registers */
1379 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1380
1381 /* Change RTC state */
1382 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1383
1384 /* Process Unlocked */
1385 __HAL_UNLOCK(hrtc);
1386
1387 return HAL_TIMEOUT;
1388 }
1389 }
1390 }
1391
1392 /* Configure the Smooth calibration settings */
1393 hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
1394
1395 /* Enable the write protection for RTC registers */
1396 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1397
1398 /* Change RTC state */
1399 hrtc->State = HAL_RTC_STATE_READY;
1400
1401 /* Process Unlocked */
1402 __HAL_UNLOCK(hrtc);
1403
1404 return HAL_OK;
1405 }
1406
1407 /**
1408 * @brief Configures the Synchronization Shift Control Settings.
1409 * @note When REFCKON is set, firmware must not write to Shift control register.
1410 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1411 * the configuration information for RTC.
1412 * @param ShiftAdd1S: Select to add or not 1 second to the time calendar.
1413 * This parameter can be one of the following values :
1414 * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1415 * @arg RTC_SHIFTADD1S_RESET: No effect.
1416 * @param ShiftSubFS: Select the number of Second Fractions to substitute.
1417 * This parameter can be one any value from 0 to 0x7FFF.
1418 * @retval HAL status
1419 */
1420 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1421 {
1422 uint32_t tickstart = 0;
1423
1424 /* Check the parameters */
1425 assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1426 assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1427
1428 /* Process Locked */
1429 __HAL_LOCK(hrtc);
1430
1431 hrtc->State = HAL_RTC_STATE_BUSY;
1432
1433 /* Disable the write protection for RTC registers */
1434 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1435
1436 /* Get tick */
1437 tickstart = HAL_GetTick();
1438
1439 /* Wait until the shift is completed*/
1440 while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
1441 {
1442 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1443 {
1444 /* Enable the write protection for RTC registers */
1445 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1446
1447 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1448
1449 /* Process Unlocked */
1450 __HAL_UNLOCK(hrtc);
1451
1452 return HAL_TIMEOUT;
1453 }
1454 }
1455
1456 /* Check if the reference clock detection is disabled */
1457 if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
1458 {
1459 /* Configure the Shift settings */
1460 hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1461
1462 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1463 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
1464 {
1465 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1466 {
1467 /* Enable the write protection for RTC registers */
1468 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1469
1470 hrtc->State = HAL_RTC_STATE_ERROR;
1471
1472 /* Process Unlocked */
1473 __HAL_UNLOCK(hrtc);
1474
1475 return HAL_ERROR;
1476 }
1477 }
1478 }
1479 else
1480 {
1481 /* Enable the write protection for RTC registers */
1482 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1483
1484 /* Change RTC state */
1485 hrtc->State = HAL_RTC_STATE_ERROR;
1486
1487 /* Process Unlocked */
1488 __HAL_UNLOCK(hrtc);
1489
1490 return HAL_ERROR;
1491 }
1492
1493 /* Enable the write protection for RTC registers */
1494 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1495
1496 /* Change RTC state */
1497 hrtc->State = HAL_RTC_STATE_READY;
1498
1499 /* Process Unlocked */
1500 __HAL_UNLOCK(hrtc);
1501
1502 return HAL_OK;
1503 }
1504
1505 /**
1506 * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1507 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1508 * the configuration information for RTC.
1509 * @param CalibOutput: Select the Calibration output Selection .
1510 * This parameter can be one of the following values:
1511 * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1512 * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1513 * @retval HAL status
1514 */
1515 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
1516 {
1517 /* Check the parameters */
1518 assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1519
1520 /* Process Locked */
1521 __HAL_LOCK(hrtc);
1522
1523 hrtc->State = HAL_RTC_STATE_BUSY;
1524
1525 /* Disable the write protection for RTC registers */
1526 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1527
1528 /* Clear flags before config */
1529 hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1530
1531 /* Configure the RTC_CR register */
1532 hrtc->Instance->CR |= (uint32_t)CalibOutput;
1533
1534 __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1535
1536 /* Enable the write protection for RTC registers */
1537 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1538
1539 /* Change RTC state */
1540 hrtc->State = HAL_RTC_STATE_READY;
1541
1542 /* Process Unlocked */
1543 __HAL_UNLOCK(hrtc);
1544
1545 return HAL_OK;
1546 }
1547
1548 /**
1549 * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1550 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1551 * the configuration information for RTC.
1552 * @retval HAL status
1553 */
1554 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
1555 {
1556 /* Process Locked */
1557 __HAL_LOCK(hrtc);
1558
1559 hrtc->State = HAL_RTC_STATE_BUSY;
1560
1561 /* Disable the write protection for RTC registers */
1562 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1563
1564 __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1565
1566 /* Enable the write protection for RTC registers */
1567 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1568
1569 /* Change RTC state */
1570 hrtc->State = HAL_RTC_STATE_READY;
1571
1572 /* Process Unlocked */
1573 __HAL_UNLOCK(hrtc);
1574
1575 return HAL_OK;
1576 }
1577
1578 /**
1579 * @brief Enables the RTC reference clock detection.
1580 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1581 * the configuration information for RTC.
1582 * @retval HAL status
1583 */
1584 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
1585 {
1586 /* Process Locked */
1587 __HAL_LOCK(hrtc);
1588
1589 hrtc->State = HAL_RTC_STATE_BUSY;
1590
1591 /* Disable the write protection for RTC registers */
1592 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1593
1594 /* Set Initialization mode */
1595 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1596 {
1597 /* Enable the write protection for RTC registers */
1598 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1599
1600 /* Set RTC state*/
1601 hrtc->State = HAL_RTC_STATE_ERROR;
1602
1603 /* Process Unlocked */
1604 __HAL_UNLOCK(hrtc);
1605
1606 return HAL_ERROR;
1607 }
1608 else
1609 {
1610 __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1611
1612 /* Exit Initialization mode */
1613 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1614 }
1615
1616 /* Enable the write protection for RTC registers */
1617 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1618
1619 /* Change RTC state */
1620 hrtc->State = HAL_RTC_STATE_READY;
1621
1622 /* Process Unlocked */
1623 __HAL_UNLOCK(hrtc);
1624
1625 return HAL_OK;
1626 }
1627
1628 /**
1629 * @brief Disable the RTC reference clock detection.
1630 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1631 * the configuration information for RTC.
1632 * @retval HAL status
1633 */
1634 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
1635 {
1636 /* Process Locked */
1637 __HAL_LOCK(hrtc);
1638
1639 hrtc->State = HAL_RTC_STATE_BUSY;
1640
1641 /* Disable the write protection for RTC registers */
1642 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1643
1644 /* Set Initialization mode */
1645 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1646 {
1647 /* Enable the write protection for RTC registers */
1648 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1649
1650 /* Set RTC state*/
1651 hrtc->State = HAL_RTC_STATE_ERROR;
1652
1653 /* Process Unlocked */
1654 __HAL_UNLOCK(hrtc);
1655
1656 return HAL_ERROR;
1657 }
1658 else
1659 {
1660 __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1661
1662 /* Exit Initialization mode */
1663 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1664 }
1665
1666 /* Enable the write protection for RTC registers */
1667 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1668
1669 /* Change RTC state */
1670 hrtc->State = HAL_RTC_STATE_READY;
1671
1672 /* Process Unlocked */
1673 __HAL_UNLOCK(hrtc);
1674
1675 return HAL_OK;
1676 }
1677
1678 /**
1679 * @brief Enables the Bypass Shadow feature.
1680 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1681 * the configuration information for RTC.
1682 * @note When the Bypass Shadow is enabled the calendar value are taken
1683 * directly from the Calendar counter.
1684 * @retval HAL status
1685 */
1686 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
1687 {
1688 /* Process Locked */
1689 __HAL_LOCK(hrtc);
1690
1691 hrtc->State = HAL_RTC_STATE_BUSY;
1692
1693 /* Disable the write protection for RTC registers */
1694 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1695
1696 /* Set the BYPSHAD bit */
1697 hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1698
1699 /* Enable the write protection for RTC registers */
1700 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1701
1702 /* Change RTC state */
1703 hrtc->State = HAL_RTC_STATE_READY;
1704
1705 /* Process Unlocked */
1706 __HAL_UNLOCK(hrtc);
1707
1708 return HAL_OK;
1709 }
1710
1711 /**
1712 * @brief Disables the Bypass Shadow feature.
1713 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1714 * the configuration information for RTC.
1715 * @note When the Bypass Shadow is enabled the calendar value are taken
1716 * directly from the Calendar counter.
1717 * @retval HAL status
1718 */
1719 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
1720 {
1721 /* Process Locked */
1722 __HAL_LOCK(hrtc);
1723
1724 hrtc->State = HAL_RTC_STATE_BUSY;
1725
1726 /* Disable the write protection for RTC registers */
1727 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1728
1729 /* Reset the BYPSHAD bit */
1730 hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
1731
1732 /* Enable the write protection for RTC registers */
1733 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1734
1735 /* Change RTC state */
1736 hrtc->State = HAL_RTC_STATE_READY;
1737
1738 /* Process Unlocked */
1739 __HAL_UNLOCK(hrtc);
1740
1741 return HAL_OK;
1742 }
1743
1744 /**
1745 * @}
1746 */
1747
1748 /** @defgroup RTCEx_Group4 Extended features functions
1749 * @brief Extended features functions
1750 *
1751 @verbatim
1752 ===============================================================================
1753 ##### Extended features functions #####
1754 ===============================================================================
1755 [..] This section provides functions allowing to:
1756 (+) RTC Alram B callback
1757 (+) RTC Poll for Alarm B request
1758
1759 @endverbatim
1760 * @{
1761 */
1762
1763 /**
1764 * @brief Alarm B callback.
1765 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1766 * the configuration information for RTC.
1767 * @retval None
1768 */
1769 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1770 {
1771 /* Prevent unused argument(s) compilation warning */
1772 UNUSED(hrtc);
1773
1774 /* NOTE : This function Should not be modified, when the callback is needed,
1775 the HAL_RTC_AlarmBEventCallback could be implemented in the user file
1776 */
1777 }
1778
1779 /**
1780 * @brief This function handles AlarmB Polling request.
1781 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1782 * the configuration information for RTC.
1783 * @param Timeout: Timeout duration
1784 * @retval HAL status
1785 */
1786 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1787 {
1788 uint32_t tickstart = 0;
1789
1790 /* Get tick */
1791 tickstart = HAL_GetTick();
1792
1793 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
1794 {
1795 if(Timeout != HAL_MAX_DELAY)
1796 {
1797 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1798 {
1799 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1800 return HAL_TIMEOUT;
1801 }
1802 }
1803 }
1804
1805 /* Clear the Alarm Flag */
1806 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1807
1808 /* Change RTC state */
1809 hrtc->State = HAL_RTC_STATE_READY;
1810
1811 return HAL_OK;
1812 }
1813
1814 /**
1815 * @}
1816 */
1817
1818 /**
1819 * @}
1820 */
1821
1822 #endif /* HAL_RTC_MODULE_ENABLED */
1823 /**
1824 * @}
1825 */
1826
1827 /**
1828 * @}
1829 */
1830
1831 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/