2 ******************************************************************************
3 * @file stm32f4xx_hal_rtc_ex.c
4 * @author MCD Application Team
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
17 ==============================================================================
18 ##### How to use this driver #####
19 ==============================================================================
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.
25 *** RTC Wakeup configuration ***
26 ================================
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()
34 *** TimeStamp configuration ***
35 ===============================
37 (+) Configure the RTC_AFx trigger and enable the RTC TimeStamp using the
38 HAL_RTC_SetTimeStamp() function. You can also configure the RTC TimeStamp with
39 interrupt mode using the HAL_RTC_SetTimeStamp_IT() function.
40 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
42 (+) The TIMESTAMP alternate function can be mapped either to RTC_AF1 (PC13)
43 or RTC_AF2 (PI8) depending on the value of TSINSEL bit in
44 RTC_TAFCR register. The corresponding pin is also selected by HAL_RTC_SetTimeStamp()
45 or HAL_RTC_SetTimeStamp_IT() function.
47 *** Tamper configuration ***
48 ============================
50 (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
51 or Level according to the Tamper filter (if equal to 0 Edge else Level)
52 value, sampling frequency, precharge or discharge and Pull-UP using the
53 HAL_RTC_SetTamper() function. You can configure RTC Tamper in interrupt
54 mode using HAL_RTC_SetTamper_IT() function.
55 (+) The TAMPER1 alternate function can be mapped either to RTC_AF1 (PC13)
56 or RTC_AF2 (PI8) depending on the value of TAMP1INSEL bit in
57 RTC_TAFCR register. The corresponding pin is also selected by HAL_RTC_SetTamper()
58 or HAL_RTC_SetTamper_IT() function.
60 *** Backup Data Registers configuration ***
61 ===========================================
63 (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
65 (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
69 ******************************************************************************
72 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
74 * Redistribution and use in source and binary forms, with or without modification,
75 * are permitted provided that the following conditions are met:
76 * 1. Redistributions of source code must retain the above copyright notice,
77 * this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright notice,
79 * this list of conditions and the following disclaimer in the documentation
80 * and/or other materials provided with the distribution.
81 * 3. Neither the name of STMicroelectronics nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
86 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
88 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
91 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
92 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
93 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
94 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96 ******************************************************************************
99 /* Includes ------------------------------------------------------------------*/
100 #include "stm32f4xx_hal.h"
102 /** @addtogroup STM32F4xx_HAL_Driver
107 * @brief RTC HAL module driver
111 #ifdef HAL_RTC_MODULE_ENABLED
113 /* Private typedef -----------------------------------------------------------*/
114 /* Private define ------------------------------------------------------------*/
115 /* Private macro -------------------------------------------------------------*/
116 /* Private variables ---------------------------------------------------------*/
117 /* Private function prototypes -----------------------------------------------*/
118 /* Private functions ---------------------------------------------------------*/
120 /** @defgroup RTCEx_Private_Functions
125 /** @defgroup RTCEx_Group1 RTC TimeStamp and Tamper functions
126 * @brief RTC TimeStamp and Tamper functions
129 ===============================================================================
130 ##### RTC TimeStamp and Tamper functions #####
131 ===============================================================================
133 [..] This section provides functions allowing to configure TimeStamp feature
140 * @brief Sets TimeStamp.
141 * @note This API must be called before enabling the TimeStamp feature.
142 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
143 * the configuration information for RTC.
144 * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
146 * This parameter can be one of the following values:
147 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
148 * rising edge of the related pin.
149 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
150 * falling edge of the related pin.
151 * @param RTC_TimeStampPin: specifies the RTC TimeStamp Pin.
152 * This parameter can be one of the following values:
153 * @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
154 * @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin.
157 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
161 /* Check the parameters */
162 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
163 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
168 hrtc->State = HAL_RTC_STATE_BUSY;
170 /* Get the RTC_CR register and clear the bits to be configured */
171 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
173 tmpreg|= TimeStampEdge;
175 /* Disable the write protection for RTC registers */
176 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
178 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
179 hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
181 /* Configure the Time Stamp TSEDGE and Enable bits */
182 hrtc->Instance->CR = (uint32_t)tmpreg;
184 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
186 /* Enable the write protection for RTC registers */
187 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
189 /* Change RTC state */
190 hrtc->State = HAL_RTC_STATE_READY;
192 /* Process Unlocked */
199 * @brief Sets TimeStamp with Interrupt.
200 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
201 * the configuration information for RTC.
202 * @note This API must be called before enabling the TimeStamp feature.
203 * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
205 * This parameter can be one of the following values:
206 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
207 * rising edge of the related pin.
208 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
209 * falling edge of the related pin.
210 * @param RTC_TimeStampPin: Specifies the RTC TimeStamp Pin.
211 * This parameter can be one of the following values:
212 * @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
213 * @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin.
216 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
220 /* Check the parameters */
221 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
222 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
227 hrtc->State = HAL_RTC_STATE_BUSY;
229 /* Get the RTC_CR register and clear the bits to be configured */
230 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
232 tmpreg |= TimeStampEdge;
234 /* Disable the write protection for RTC registers */
235 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
237 /* Configure the Time Stamp TSEDGE and Enable bits */
238 hrtc->Instance->CR = (uint32_t)tmpreg;
240 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
241 hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
243 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
245 /* Enable IT timestamp */
246 __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
248 /* RTC timestamp Interrupt Configuration: EXTI configuration */
249 __HAL_RTC_EXTI_ENABLE_IT(RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT);
251 EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
253 /* Enable the write protection for RTC registers */
254 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
256 hrtc->State = HAL_RTC_STATE_READY;
258 /* Process Unlocked */
265 * @brief Deactivates TimeStamp.
266 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
267 * the configuration information for RTC.
270 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
277 hrtc->State = HAL_RTC_STATE_BUSY;
279 /* Disable the write protection for RTC registers */
280 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
282 /* In case of interrupt mode is used, the interrupt source must disabled */
283 __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
285 /* Get the RTC_CR register and clear the bits to be configured */
286 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
288 /* Configure the Time Stamp TSEDGE and Enable bits */
289 hrtc->Instance->CR = (uint32_t)tmpreg;
291 /* Enable the write protection for RTC registers */
292 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
294 hrtc->State = HAL_RTC_STATE_READY;
296 /* Process Unlocked */
303 * @brief Gets the RTC TimeStamp value.
304 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
305 * the configuration information for RTC.
306 * @param sTimeStamp: Pointer to Time structure
307 * @param sTimeStampDate: Pointer to Date structure
308 * @param Format: specifies the format of the entered parameters.
309 * This parameter can be one of the following values:
310 * FORMAT_BIN: Binary data format
311 * FORMAT_BCD: BCD data format
314 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
316 uint32_t tmptime = 0, tmpdate = 0;
318 /* Check the parameters */
319 assert_param(IS_RTC_FORMAT(Format));
321 /* Get the TimeStamp time and date registers values */
322 tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
323 tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
325 /* Fill the Time structure fields with the read parameters */
326 sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
327 sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
328 sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
329 sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
330 sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
332 /* Fill the Date structure fields with the read parameters */
333 sTimeStampDate->Year = 0;
334 sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
335 sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
336 sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
338 /* Check the input parameters format */
339 if(Format == FORMAT_BIN)
341 /* Convert the TimeStamp structure parameters to Binary format */
342 sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
343 sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
344 sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
346 /* Convert the DateTimeStamp structure parameters to Binary format */
347 sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
348 sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
349 sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
352 /* Clear the TIMESTAMP Flag */
353 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
360 * @note By calling this API we disable the tamper interrupt for all tampers.
361 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
362 * the configuration information for RTC.
363 * @param sTamper: Pointer to Tamper Structure.
366 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
370 /* Check the parameters */
371 assert_param(IS_TAMPER(sTamper->Tamper));
372 assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
373 assert_param(IS_TAMPER_TRIGGER(sTamper->Trigger));
374 assert_param(IS_TAMPER_FILTER(sTamper->Filter));
375 assert_param(IS_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
376 assert_param(IS_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
377 assert_param(IS_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
378 assert_param(IS_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
383 hrtc->State = HAL_RTC_STATE_BUSY;
385 if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
387 sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
390 tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger |\
391 (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
392 (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
394 hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
395 (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
396 (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL | (uint32_t)RTC_TAFCR_TAMPIE);
398 hrtc->Instance->TAFCR |= tmpreg;
400 hrtc->State = HAL_RTC_STATE_READY;
402 /* Process Unlocked */
409 * @brief Sets Tamper with interrupt.
410 * @note By calling this API we force the tamper interrupt for all tampers.
411 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
412 * the configuration information for RTC.
413 * @param sTamper: Pointer to RTC Tamper.
416 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
420 /* Check the parameters */
421 assert_param(IS_TAMPER(sTamper->Tamper));
422 assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
423 assert_param(IS_TAMPER_TRIGGER(sTamper->Trigger));
424 assert_param(IS_TAMPER_FILTER(sTamper->Filter));
425 assert_param(IS_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
426 assert_param(IS_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
427 assert_param(IS_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
428 assert_param(IS_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
433 hrtc->State = HAL_RTC_STATE_BUSY;
435 /* Configure the tamper trigger */
436 if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
438 sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
441 tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger |\
442 (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
443 (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
445 hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
446 (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
447 (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL);
449 hrtc->Instance->TAFCR |= tmpreg;
451 /* Configure the Tamper Interrupt in the RTC_TAFCR */
452 hrtc->Instance->TAFCR |= (uint32_t)RTC_TAFCR_TAMPIE;
454 /* RTC Tamper Interrupt Configuration: EXTI configuration */
455 __HAL_RTC_EXTI_ENABLE_IT(RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT);
457 EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
459 hrtc->State = HAL_RTC_STATE_READY;
461 /* Process Unlocked */
468 * @brief Deactivates Tamper.
469 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
470 * the configuration information for RTC.
471 * @param Tamper: Selected tamper pin.
472 * This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.
475 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
477 assert_param(IS_TAMPER(Tamper));
482 hrtc->State = HAL_RTC_STATE_BUSY;
484 /* Disable the selected Tamper pin */
485 hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
487 hrtc->State = HAL_RTC_STATE_READY;
489 /* Process Unlocked */
496 * @brief This function handles TimeStamp interrupt request.
497 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
498 * the configuration information for RTC.
501 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
503 if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
505 /* Get the status of the Interrupt */
506 if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
508 /* TIMESTAMP callback */
509 HAL_RTCEx_TimeStampEventCallback(hrtc);
511 /* Clear the TIMESTAMP interrupt pending bit */
512 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
516 /* Get the status of the Interrupt */
517 if(__HAL_RTC_TAMPER_GET_IT(hrtc,RTC_IT_TAMP1))
519 /* Get the TAMPER Interrupt enable bit and pending bit */
520 if(((hrtc->Instance->TAFCR & (RTC_TAFCR_TAMPIE))) != (uint32_t)RESET)
522 /* Tamper callback */
523 HAL_RTCEx_Tamper1EventCallback(hrtc);
525 /* Clear the Tamper interrupt pending bit */
526 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
530 /* Get the status of the Interrupt */
531 if(__HAL_RTC_TAMPER_GET_IT(hrtc, RTC_IT_TAMP2))
533 /* Get the TAMPER Interrupt enable bit and pending bit */
534 if(((hrtc->Instance->TAFCR & RTC_TAFCR_TAMPIE)) != (uint32_t)RESET)
536 /* Tamper callback */
537 HAL_RTCEx_Tamper2EventCallback(hrtc);
539 /* Clear the Tamper interrupt pending bit */
540 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
543 /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
544 __HAL_RTC_EXTI_CLEAR_FLAG(RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT);
546 /* Change RTC state */
547 hrtc->State = HAL_RTC_STATE_READY;
551 * @brief TimeStamp callback.
552 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
553 * the configuration information for RTC.
556 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
558 /* NOTE : This function Should not be modified, when the callback is needed,
559 the HAL_RTC_TimeStampEventCallback could be implemented in the user file
564 * @brief Tamper 1 callback.
565 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
566 * the configuration information for RTC.
569 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
571 /* NOTE : This function Should not be modified, when the callback is needed,
572 the HAL_RTC_Tamper1EventCallback could be implemented in the user file
577 * @brief Tamper 2 callback.
578 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
579 * the configuration information for RTC.
582 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
584 /* NOTE : This function Should not be modified, when the callback is needed,
585 the HAL_RTC_Tamper2EventCallback could be implemented in the user file
590 * @brief This function handles TimeStamp polling request.
591 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
592 * the configuration information for RTC.
593 * @param Timeout: Timeout duration
596 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
598 uint32_t tickstart = 0;
601 tickstart = HAL_GetTick();
603 while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
605 if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
607 /* Clear the TIMESTAMP OverRun Flag */
608 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
610 /* Change TIMESTAMP state */
611 hrtc->State = HAL_RTC_STATE_ERROR;
616 if(Timeout != HAL_MAX_DELAY)
618 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
620 hrtc->State = HAL_RTC_STATE_TIMEOUT;
626 /* Change RTC state */
627 hrtc->State = HAL_RTC_STATE_READY;
633 * @brief This function handles Tamper1 Polling.
634 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
635 * the configuration information for RTC.
636 * @param Timeout: Timeout duration
639 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
641 uint32_t tickstart = 0;
644 tickstart = HAL_GetTick();
646 /* Get the status of the Interrupt */
647 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
649 if(Timeout != HAL_MAX_DELAY)
651 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
653 hrtc->State = HAL_RTC_STATE_TIMEOUT;
659 /* Clear the Tamper Flag */
660 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
662 /* Change RTC state */
663 hrtc->State = HAL_RTC_STATE_READY;
669 * @brief This function handles Tamper2 Polling.
670 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
671 * the configuration information for RTC.
672 * @param Timeout: Timeout duration
675 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
677 uint32_t tickstart = 0;
680 tickstart = HAL_GetTick();
682 /* Get the status of the Interrupt */
683 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
685 if(Timeout != HAL_MAX_DELAY)
687 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
689 hrtc->State = HAL_RTC_STATE_TIMEOUT;
695 /* Clear the Tamper Flag */
696 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
698 /* Change RTC state */
699 hrtc->State = HAL_RTC_STATE_READY;
708 /** @defgroup RTCEx_Group2 RTC Wake-up functions
709 * @brief RTC Wake-up functions
712 ===============================================================================
713 ##### RTC Wake-up functions #####
714 ===============================================================================
716 [..] This section provides functions allowing to configure Wake-up feature
723 * @brief Sets wake up timer.
724 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
725 * the configuration information for RTC.
726 * @param WakeUpCounter: Wake up counter
727 * @param WakeUpClock: Wake up clock
730 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
732 uint32_t tickstart = 0;
734 /* Check the parameters */
735 assert_param(IS_WAKEUP_CLOCK(WakeUpClock));
736 assert_param(IS_WAKEUP_COUNTER(WakeUpCounter));
741 hrtc->State = HAL_RTC_STATE_BUSY;
743 /* Disable the write protection for RTC registers */
744 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
746 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
749 tickstart = HAL_GetTick();
751 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
752 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
754 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
756 /* Enable the write protection for RTC registers */
757 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
759 hrtc->State = HAL_RTC_STATE_TIMEOUT;
761 /* Process Unlocked */
768 /* Clear the Wakeup Timer clock source bits in CR register */
769 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
771 /* Configure the clock source */
772 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
774 /* Configure the Wakeup Timer counter */
775 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
777 /* Enable the Wakeup Timer */
778 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
780 /* Enable the write protection for RTC registers */
781 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
783 hrtc->State = HAL_RTC_STATE_READY;
785 /* Process Unlocked */
792 * @brief Sets wake up timer with interrupt
793 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
794 * the configuration information for RTC.
795 * @param WakeUpCounter: Wake up counter
796 * @param WakeUpClock: Wake up clock
799 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
801 uint32_t tickstart = 0;
803 /* Check the parameters */
804 assert_param(IS_WAKEUP_CLOCK(WakeUpClock));
805 assert_param(IS_WAKEUP_COUNTER(WakeUpCounter));
810 hrtc->State = HAL_RTC_STATE_BUSY;
812 /* Disable the write protection for RTC registers */
813 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
815 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
818 tickstart = HAL_GetTick();
820 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
821 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
823 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
825 /* Enable the write protection for RTC registers */
826 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
828 hrtc->State = HAL_RTC_STATE_TIMEOUT;
830 /* Process Unlocked */
837 /* Configure the Wakeup Timer counter */
838 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
840 /* Clear the Wakeup Timer clock source bits in CR register */
841 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
843 /* Configure the clock source */
844 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
846 /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
847 __HAL_RTC_EXTI_ENABLE_IT(RTC_EXTI_LINE_WAKEUPTIMER_EVENT);
849 EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
851 /* Configure the Interrupt in the RTC_CR register */
852 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
854 /* Enable the Wakeup Timer */
855 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
857 /* Enable the write protection for RTC registers */
858 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
860 hrtc->State = HAL_RTC_STATE_READY;
862 /* Process Unlocked */
869 * @brief Deactivates wake up timer counter.
870 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
871 * the configuration information for RTC.
874 uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
876 uint32_t tickstart = 0;
881 hrtc->State = HAL_RTC_STATE_BUSY;
883 /* Disable the write protection for RTC registers */
884 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
886 /* Disable the Wakeup Timer */
887 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
889 /* In case of interrupt mode is used, the interrupt source must disabled */
890 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
893 tickstart = HAL_GetTick();
895 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
896 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
898 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
900 /* Enable the write protection for RTC registers */
901 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
903 hrtc->State = HAL_RTC_STATE_TIMEOUT;
905 /* Process Unlocked */
912 /* Enable the write protection for RTC registers */
913 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
915 hrtc->State = HAL_RTC_STATE_READY;
917 /* Process Unlocked */
924 * @brief Gets wake up timer counter.
925 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
926 * the configuration information for RTC.
927 * @retval Counter value
929 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
931 /* Get the counter value */
932 return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
936 * @brief This function handles Wake Up Timer interrupt request.
937 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
938 * the configuration information for RTC.
941 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
943 if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
945 /* Get the status of the Interrupt */
946 if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
948 /* WAKEUPTIMER callback */
949 HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
951 /* Clear the WAKEUPTIMER interrupt pending bit */
952 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
956 /* Clear the EXTI's line Flag for RTC WakeUpTimer */
957 __HAL_RTC_EXTI_CLEAR_FLAG(RTC_EXTI_LINE_WAKEUPTIMER_EVENT);
959 /* Change RTC state */
960 hrtc->State = HAL_RTC_STATE_READY;
964 * @brief Wake Up Timer callback.
965 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
966 * the configuration information for RTC.
969 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
971 /* NOTE : This function Should not be modified, when the callback is needed,
972 the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
977 * @brief This function handles Wake Up Timer Polling.
978 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
979 * the configuration information for RTC.
980 * @param Timeout: Timeout duration
983 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
985 uint32_t tickstart = 0;
988 tickstart = HAL_GetTick();
990 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
992 if(Timeout != HAL_MAX_DELAY)
994 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
996 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1003 /* Clear the WAKEUPTIMER Flag */
1004 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1006 /* Change RTC state */
1007 hrtc->State = HAL_RTC_STATE_READY;
1017 /** @defgroup RTCEx_Group3 Extension Peripheral Control functions
1018 * @brief Extension Peripheral Control functions
1021 ===============================================================================
1022 ##### Extension Peripheral Control functions #####
1023 ===============================================================================
1025 This subsection provides functions allowing to
1026 (+) Write a data in a specified RTC Backup data register
1027 (+) Read a data in a specified RTC Backup data register
1028 (+) Set the Coarse calibration parameters.
1029 (+) Deactivate the Coarse calibration parameters
1030 (+) Set the Smooth calibration parameters.
1031 (+) Configure the Synchronization Shift Control Settings.
1032 (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1033 (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1034 (+) Enable the RTC reference clock detection.
1035 (+) Disable the RTC reference clock detection.
1036 (+) Enable the Bypass Shadow feature.
1037 (+) Disable the Bypass Shadow feature.
1044 * @brief Writes a data in a specified RTC Backup data register.
1045 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1046 * the configuration information for RTC.
1047 * @param BackupRegister: RTC Backup data Register number.
1048 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1049 * specify the register.
1050 * @param Data: Data to be written in the specified RTC Backup data register.
1053 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1057 /* Check the parameters */
1058 assert_param(IS_RTC_BKP(BackupRegister));
1060 tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1061 tmp += (BackupRegister * 4);
1063 /* Write the specified register */
1064 *(__IO uint32_t *)tmp = (uint32_t)Data;
1068 * @brief Reads data from the specified RTC Backup data Register.
1069 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1070 * the configuration information for RTC.
1071 * @param BackupRegister: RTC Backup data Register number.
1072 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1073 * specify the register.
1074 * @retval Read value
1076 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1080 /* Check the parameters */
1081 assert_param(IS_RTC_BKP(BackupRegister));
1083 tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1084 tmp += (BackupRegister * 4);
1086 /* Read the specified register */
1087 return (*(__IO uint32_t *)tmp);
1091 * @brief Sets the Coarse calibration parameters.
1092 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1093 * the configuration information for RTC.
1094 * @param CalibSign: Specifies the sign of the coarse calibration value.
1095 * This parameter can be one of the following values :
1096 * @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
1097 * @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
1098 * @param Value: value of coarse calibration expressed in ppm (coded on 5 bits).
1100 * @note This Calibration value should be between 0 and 63 when using negative
1101 * sign with a 2-ppm step.
1103 * @note This Calibration value should be between 0 and 126 when using positive
1104 * sign with a 4-ppm step.
1105 * @retval HAL status
1107 HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef* hrtc, uint32_t CalibSign, uint32_t Value)
1109 /* Check the parameters */
1110 assert_param(IS_RTC_CALIB_SIGN(CalibSign));
1111 assert_param(IS_RTC_CALIB_VALUE(Value));
1113 /* Process Locked */
1116 hrtc->State = HAL_RTC_STATE_BUSY;
1118 /* Disable the write protection for RTC registers */
1119 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1121 /* Set Initialization mode */
1122 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1124 /* Enable the write protection for RTC registers */
1125 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1128 hrtc->State = HAL_RTC_STATE_ERROR;
1130 /* Process Unlocked */
1137 /* Enable the Coarse Calibration */
1138 __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
1140 /* Set the coarse calibration value */
1141 hrtc->Instance->CALIBR = (uint32_t)(CalibSign|Value);
1143 /* Exit Initialization mode */
1144 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1147 /* Enable the write protection for RTC registers */
1148 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1151 hrtc->State = HAL_RTC_STATE_READY;
1153 /* Process Unlocked */
1160 * @brief Deactivates the Coarse calibration parameters.
1161 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1162 * the configuration information for RTC.
1163 * @retval HAL status
1165 HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef* hrtc)
1167 /* Process Locked */
1170 hrtc->State = HAL_RTC_STATE_BUSY;
1172 /* Disable the write protection for RTC registers */
1173 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1175 /* Set Initialization mode */
1176 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1178 /* Enable the write protection for RTC registers */
1179 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1182 hrtc->State = HAL_RTC_STATE_ERROR;
1184 /* Process Unlocked */
1191 /* Enable the Coarse Calibration */
1192 __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
1194 /* Exit Initialization mode */
1195 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1198 /* Enable the write protection for RTC registers */
1199 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1202 hrtc->State = HAL_RTC_STATE_READY;
1204 /* Process Unlocked */
1211 * @brief Sets the Smooth calibration parameters.
1212 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1213 * the configuration information for RTC.
1214 * @param SmoothCalibPeriod: Select the Smooth Calibration Period.
1215 * This parameter can be can be one of the following values :
1216 * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration periode is 32s.
1217 * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration periode is 16s.
1218 * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibartion periode is 8s.
1219 * @param SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
1220 * This parameter can be one of the following values:
1221 * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK puls every 2*11 pulses.
1222 * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1223 * @param SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
1224 * This parameter can be one any value from 0 to 0x000001FF.
1225 * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
1226 * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
1227 * SmouthCalibMinusPulsesValue must be equal to 0.
1228 * @retval HAL status
1230 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
1232 uint32_t tickstart = 0;
1234 /* Check the parameters */
1235 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1236 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1237 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
1239 /* Process Locked */
1242 hrtc->State = HAL_RTC_STATE_BUSY;
1244 /* Disable the write protection for RTC registers */
1245 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1247 /* check if a calibration is pending*/
1248 if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
1251 tickstart = HAL_GetTick();
1253 /* check if a calibration is pending*/
1254 while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
1256 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1258 /* Enable the write protection for RTC registers */
1259 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1261 /* Change RTC state */
1262 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1264 /* Process Unlocked */
1272 /* Configure the Smooth calibration settings */
1273 hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
1275 /* Enable the write protection for RTC registers */
1276 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1278 /* Change RTC state */
1279 hrtc->State = HAL_RTC_STATE_READY;
1281 /* Process Unlocked */
1288 * @brief Configures the Synchronization Shift Control Settings.
1289 * @note When REFCKON is set, firmware must not write to Shift control register.
1290 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1291 * the configuration information for RTC.
1292 * @param ShiftAdd1S: Select to add or not 1 second to the time calendar.
1293 * This parameter can be one of the following values :
1294 * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1295 * @arg RTC_SHIFTADD1S_RESET: No effect.
1296 * @param ShiftSubFS: Select the number of Second Fractions to substitute.
1297 * This parameter can be one any value from 0 to 0x7FFF.
1298 * @retval HAL status
1300 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1302 uint32_t tickstart = 0;
1304 /* Check the parameters */
1305 assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1306 assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1308 /* Process Locked */
1311 hrtc->State = HAL_RTC_STATE_BUSY;
1313 /* Disable the write protection for RTC registers */
1314 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1317 tickstart = HAL_GetTick();
1319 /* Wait until the shift is completed*/
1320 while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
1322 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1324 /* Enable the write protection for RTC registers */
1325 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1327 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1329 /* Process Unlocked */
1336 /* Check if the reference clock detection is disabled */
1337 if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
1339 /* Configure the Shift settings */
1340 hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1342 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1343 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
1345 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1347 /* Enable the write protection for RTC registers */
1348 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1350 hrtc->State = HAL_RTC_STATE_ERROR;
1352 /* Process Unlocked */
1361 /* Enable the write protection for RTC registers */
1362 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1364 /* Change RTC state */
1365 hrtc->State = HAL_RTC_STATE_ERROR;
1367 /* Process Unlocked */
1373 /* Enable the write protection for RTC registers */
1374 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1376 /* Change RTC state */
1377 hrtc->State = HAL_RTC_STATE_READY;
1379 /* Process Unlocked */
1386 * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1387 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1388 * the configuration information for RTC.
1389 * @param CalibOutput: Select the Calibration output Selection .
1390 * This parameter can be one of the following values:
1391 * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1392 * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1393 * @retval HAL status
1395 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
1397 /* Check the parameters */
1398 assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1400 /* Process Locked */
1403 hrtc->State = HAL_RTC_STATE_BUSY;
1405 /* Disable the write protection for RTC registers */
1406 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1408 /* Clear flags before config */
1409 hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1411 /* Configure the RTC_CR register */
1412 hrtc->Instance->CR |= (uint32_t)CalibOutput;
1414 __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1416 /* Enable the write protection for RTC registers */
1417 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1419 /* Change RTC state */
1420 hrtc->State = HAL_RTC_STATE_READY;
1422 /* Process Unlocked */
1429 * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1430 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1431 * the configuration information for RTC.
1432 * @retval HAL status
1434 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
1436 /* Process Locked */
1439 hrtc->State = HAL_RTC_STATE_BUSY;
1441 /* Disable the write protection for RTC registers */
1442 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1444 __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1446 /* Enable the write protection for RTC registers */
1447 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1449 /* Change RTC state */
1450 hrtc->State = HAL_RTC_STATE_READY;
1452 /* Process Unlocked */
1459 * @brief Enables the RTC reference clock detection.
1460 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1461 * the configuration information for RTC.
1462 * @retval HAL status
1464 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
1466 /* Process Locked */
1469 hrtc->State = HAL_RTC_STATE_BUSY;
1471 /* Disable the write protection for RTC registers */
1472 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1474 /* Set Initialization mode */
1475 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1477 /* Enable the write protection for RTC registers */
1478 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1481 hrtc->State = HAL_RTC_STATE_ERROR;
1483 /* Process Unlocked */
1490 __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1492 /* Exit Initialization mode */
1493 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1496 /* Enable the write protection for RTC registers */
1497 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1499 /* Change RTC state */
1500 hrtc->State = HAL_RTC_STATE_READY;
1502 /* Process Unlocked */
1509 * @brief Disable the RTC reference clock detection.
1510 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1511 * the configuration information for RTC.
1512 * @retval HAL status
1514 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
1516 /* Process Locked */
1519 hrtc->State = HAL_RTC_STATE_BUSY;
1521 /* Disable the write protection for RTC registers */
1522 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1524 /* Set Initialization mode */
1525 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1527 /* Enable the write protection for RTC registers */
1528 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1531 hrtc->State = HAL_RTC_STATE_ERROR;
1533 /* Process Unlocked */
1540 __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1542 /* Exit Initialization mode */
1543 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1546 /* Enable the write protection for RTC registers */
1547 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1549 /* Change RTC state */
1550 hrtc->State = HAL_RTC_STATE_READY;
1552 /* Process Unlocked */
1559 * @brief Enables the Bypass Shadow feature.
1560 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1561 * the configuration information for RTC.
1562 * @note When the Bypass Shadow is enabled the calendar value are taken
1563 * directly from the Calendar counter.
1564 * @retval HAL status
1566 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
1568 /* Process Locked */
1571 hrtc->State = HAL_RTC_STATE_BUSY;
1573 /* Disable the write protection for RTC registers */
1574 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1576 /* Set the BYPSHAD bit */
1577 hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1579 /* Enable the write protection for RTC registers */
1580 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1582 /* Change RTC state */
1583 hrtc->State = HAL_RTC_STATE_READY;
1585 /* Process Unlocked */
1592 * @brief Disables the Bypass Shadow feature.
1593 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1594 * the configuration information for RTC.
1595 * @note When the Bypass Shadow is enabled the calendar value are taken
1596 * directly from the Calendar counter.
1597 * @retval HAL status
1599 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
1601 /* Process Locked */
1604 hrtc->State = HAL_RTC_STATE_BUSY;
1606 /* Disable the write protection for RTC registers */
1607 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1609 /* Reset the BYPSHAD bit */
1610 hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
1612 /* Enable the write protection for RTC registers */
1613 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1615 /* Change RTC state */
1616 hrtc->State = HAL_RTC_STATE_READY;
1618 /* Process Unlocked */
1628 /** @defgroup RTCEx_Group4 Extended features functions
1629 * @brief Extended features functions
1632 ===============================================================================
1633 ##### Extended features functions #####
1634 ===============================================================================
1635 [..] This section provides functions allowing to:
1636 (+) RTC Alram B callback
1637 (+) RTC Poll for Alarm B request
1644 * @brief Alarm B callback.
1645 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1646 * the configuration information for RTC.
1649 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1651 /* NOTE : This function Should not be modified, when the callback is needed,
1652 the HAL_RTC_AlarmBEventCallback could be implemented in the user file
1657 * @brief This function handles AlarmB Polling request.
1658 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1659 * the configuration information for RTC.
1660 * @param Timeout: Timeout duration
1661 * @retval HAL status
1663 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1665 uint32_t tickstart = 0;
1668 tickstart = HAL_GetTick();
1670 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
1672 if(Timeout != HAL_MAX_DELAY)
1674 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1676 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1682 /* Clear the Alarm Flag */
1683 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1685 /* Change RTC state */
1686 hrtc->State = HAL_RTC_STATE_READY;
1699 #endif /* HAL_RTC_MODULE_ENABLED */
1708 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/