2 ******************************************************************************
3 * @file stm32l0xx_hal_rtc_ex.c
4 * @author MCD Application Team
6 * @date 06-February-2015
7 * @brief Extended RTC HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Real Time Clock (RTC) Extended peripheral:
11 * + RTC Time Stamp functions
12 * + RTC Tamper functions
13 * + RTC Wake-up functions
14 * + Extended Control functions
15 * + Extended RTC features functions
18 ==============================================================================
19 ##### Backup Domain Operating Condition #####
20 ==============================================================================
21 [..] As long as the supply voltage remains in the operating range,
22 the RTC never stops, regardless of the device status (Run mode,
23 low power modes or under reset).
25 ##### Backup Domain Reset #####
26 ==================================================================
27 [..] The backup domain reset sets all RTC registers and the RCC_CSR register
28 to their reset values.
29 [..] A backup domain reset is generated when one of the following events occurs:
30 (+) Software reset, triggered by setting the RTCRST bit in the
31 RCC Control Status register (RCC_CSR).
32 (+) Power reset (BOR/POR/PDR).
34 ##### Backup Domain Access #####
35 ==================================================================
36 [..] After reset, the backup domain (RTC registers and RTC backup data registers)
37 is protected against possible unwanted write accesses.
38 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
39 (+) Enable the Power Controller (PWR) APB1 interface clock using the
40 __HAL_RCC_PWR_CLK_ENABLE() function.
41 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
42 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
43 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
46 ##### How to use this driver #####
47 ==============================================================================
49 (+) Enable the RTC domain access.
50 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
51 format using the HAL_RTC_Init() function.
53 *** Time and Date configuration ***
54 ===================================
56 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
57 and HAL_RTC_SetDate() functions.
58 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
60 *** Alarm configuration ***
61 ===========================
63 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
64 You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
65 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
67 *** RTC Wakeup configuration ***
68 ================================
70 (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
71 function. You can also configure the RTC Wakeup timer with interrupt mode
72 using the HAL_RTCEx_SetWakeUpTimer_IT() function.
73 (+) To read the RTC WakeUp Counter register, use the HAL_RTCEx_GetWakeUpTimer()
76 *** Outputs configuration ***
77 =============================
78 [..] The RTC has 2 different outputs:
79 (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
81 To output the selected RTC signal, use the HAL_RTC_Init() function.
82 (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
83 To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
84 (+) Two pins can be used as RTC_ALARM or RTC_CALIB (PC13, PB14) managed on
86 (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
87 automatically configured in output alternate function.
89 *** Smooth digital Calibration configuration ***
90 ================================================
92 (+) Configure the RTC Original Digital Calibration Value and the corresponding
93 calibration cycle period (32s,16s and 8s) using the HAL_RTCEx_SetSmoothCalib()
96 *** TimeStamp configuration ***
97 ===============================
99 (+) Enables the RTC TimeStamp using the HAL_RTC_SetTimeStamp() function.
100 You can also configure the RTC TimeStamp with interrupt mode using the
101 HAL_RTC_SetTimeStamp_IT() function.
102 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
105 *** Tamper configuration ***
106 ============================
108 (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge
109 or Level according to the Tamper filter (if equal to 0 Edge else Level)
110 value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
111 Pull-UP using the HAL_RTC_SetTamper() function. You can configure RTC Tamper
112 with interrupt mode using HAL_RTC_SetTamper_IT() function.
113 (+) The default configuration of the Tamper erases the backup registers. To avoid
114 erase, enable the NoErase field on the RTC_TAMPCR register.
116 *** Backup Data Registers configuration ***
117 ===========================================
119 (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
121 (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
123 (+) The backup registers are reset when a tamper detection event occurs
126 ##### RTC and low power modes #####
127 ==================================================================
128 [..] The MCU can be woken up from a low power mode by an RTC alternate
130 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
131 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
132 These RTC alternate functions can wake up the system from the Stop and
133 Standby low power modes.
134 [..] The system can also wake up from low power modes without depending
135 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
136 or the RTC wakeup events.
137 [..] The RTC provides a programmable time base for waking up from the
138 Stop or Standby mode at regular intervals.
139 Wakeup from STOP and Standby modes is possible only when the RTC clock source
143 ******************************************************************************
146 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
148 * Redistribution and use in source and binary forms, with or without modification,
149 * are permitted provided that the following conditions are met:
150 * 1. Redistributions of source code must retain the above copyright notice,
151 * this list of conditions and the following disclaimer.
152 * 2. Redistributions in binary form must reproduce the above copyright notice,
153 * this list of conditions and the following disclaimer in the documentation
154 * and/or other materials provided with the distribution.
155 * 3. Neither the name of STMicroelectronics nor the names of its contributors
156 * may be used to endorse or promote products derived from this software
157 * without specific prior written permission.
159 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
160 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
162 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
163 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
164 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
165 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
166 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
167 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
168 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
170 ******************************************************************************
173 /* Includes ------------------------------------------------------------------*/
174 #include "stm32l0xx_hal.h"
176 /** @addtogroup STM32L0xx_HAL_Driver
180 /** @addtogroup RTCEx
181 * @brief RTC Extended HAL module driver
185 #ifdef HAL_RTC_MODULE_ENABLED
187 /* Private typedef -----------------------------------------------------------*/
188 /* Private define ------------------------------------------------------------*/
189 /* Private macro -------------------------------------------------------------*/
190 /* Private variables ---------------------------------------------------------*/
191 /* Private function prototypes -----------------------------------------------*/
192 /* Exported functions --------------------------------------------------------*/
194 /** @addtogroup RTCEx_Exported_Functions
199 /** @addtogroup RTCEx_Exported_Functions_Group1
200 * @brief RTC TimeStamp and Tamper functions
203 ===============================================================================
204 ##### RTC TimeStamp and Tamper functions #####
205 ===============================================================================
207 [..] This section provide functions allowing to configure TimeStamp feature
214 * @brief Sets TimeStamp.
215 * @note This API must be called before enabling the TimeStamp feature.
216 * @param hrtc: RTC handle
217 * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
219 * This parameter can be one of the following values:
220 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
221 * rising edge of the related pin.
222 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
223 * falling edge of the related pin.
224 * @param RTC_TimeStampPin: specifies the RTC TimeStamp Pin.
225 * This parameter can be one of the following values:
226 * @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
227 * The RTC TimeStamp Pin is per default PC13, but for reasons of
228 * compatibility, this parameter is used.
231 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
235 /* Check the parameters */
236 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
237 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
242 hrtc->State = HAL_RTC_STATE_BUSY;
244 /* Get the RTC_CR register and clear the bits to be configured */
245 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
247 tmpreg|= TimeStampEdge;
249 /* Disable the write protection for RTC registers */
250 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
252 /* Configure the Time Stamp TSEDGE and Enable bits */
253 hrtc->Instance->CR = (uint32_t)tmpreg;
255 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
257 /* Enable the write protection for RTC registers */
258 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
260 /* Change RTC state */
261 hrtc->State = HAL_RTC_STATE_READY;
263 /* Process Unlocked */
270 * @brief Sets TimeStamp with Interrupt.
271 * @param hrtc: RTC handle
272 * @note This API must be called before enabling the TimeStamp feature.
273 * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
275 * This parameter can be one of the following values:
276 * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
277 * rising edge of the related pin.
278 * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
279 * falling edge of the related pin.
280 * @param RTC_TimeStampPin: Specifies the RTC TimeStamp Pin.
281 * This parameter can be one of the following values:
282 * @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
283 * The RTC TimeStamp Pin is per default PC13, but for reasons of
284 * compatibility, this parameter is used.
287 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
291 /* Check the parameters */
292 assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
293 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
298 hrtc->State = HAL_RTC_STATE_BUSY;
300 /* Get the RTC_CR register and clear the bits to be configured */
301 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
303 tmpreg |= TimeStampEdge;
305 /* Disable the write protection for RTC registers */
306 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
308 /* Configure the Time Stamp TSEDGE and Enable bits */
309 hrtc->Instance->CR = (uint32_t)tmpreg;
311 __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
313 /* Enable IT timestamp */
314 __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
316 /* RTC timestamp Interrupt Configuration: EXTI configuration */
317 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
319 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
321 /* Enable the write protection for RTC registers */
322 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
324 hrtc->State = HAL_RTC_STATE_READY;
326 /* Process Unlocked */
333 * @brief Deactivates TimeStamp.
334 * @param hrtc: RTC handle
337 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
344 hrtc->State = HAL_RTC_STATE_BUSY;
346 /* Disable the write protection for RTC registers */
347 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
349 /* In case of interrupt mode is used, the interrupt source must disabled */
350 __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
352 /* Get the RTC_CR register and clear the bits to be configured */
353 tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
355 /* Configure the Time Stamp TSEDGE and Enable bits */
356 hrtc->Instance->CR = (uint32_t)tmpreg;
358 /* Enable the write protection for RTC registers */
359 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
361 hrtc->State = HAL_RTC_STATE_READY;
363 /* Process Unlocked */
371 * @brief Gets the RTC TimeStamp value.
372 * @param hrtc: RTC handle
374 * @param sTimeStamp: Pointer to Time structure
375 * @param sTimeStampDate: Pointer to Date structure
376 * @param Format: specifies the format of the entered parameters.
377 * This parameter can be one of the following values:
378 * RTC_FORMAT_BIN: Binary data format
383 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
385 uint32_t tmptime = 0, tmpdate = 0;
387 /* Check the parameters */
388 assert_param(IS_RTC_FORMAT(Format));
390 /* Get the TimeStamp time and date registers values */
391 tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
392 tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
394 /* Fill the Time structure fields with the read parameters */
395 sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
396 sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
397 sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
398 sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
399 sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
401 /* Fill the Date structure fields with the read parameters */
402 sTimeStampDate->Year = 0;
403 sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
404 sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
405 sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
407 /* Check the input parameters format */
408 if(Format == RTC_FORMAT_BIN)
410 /* Convert the TimeStamp structure parameters to Binary format */
411 sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
412 sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
413 sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
415 /* Convert the DateTimeStamp structure parameters to Binary format */
416 sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
417 sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
418 sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
421 /* Clear the TIMESTAMP Flag */
422 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
429 * @note By calling this API we disable the tamper interrupt for all tampers.
430 * @param hrtc: RTC handle
431 * @param sTamper: Pointer to Tamper Structure.
434 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
438 /* Check the parameters */
439 assert_param( IS_RTC_TAMPER(sTamper->Tamper));
440 assert_param( IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
441 assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
442 assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
443 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
444 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
445 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
446 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
447 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
452 hrtc->State = HAL_RTC_STATE_BUSY;
454 /* Configure the tamper trigger */
455 if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
457 sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
460 if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
462 sTamper->NoErase = 0;
463 if((sTamper->Tamper & RTC_TAMPER_1) != 0)
465 sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
467 if((sTamper->Tamper & RTC_TAMPER_2) != 0)
469 sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
471 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
472 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
474 if((sTamper->Tamper & RTC_TAMPER_3) != 0)
476 sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
479 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
480 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
484 if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
486 sTamper->MaskFlag = 0;
487 if((sTamper->Tamper & RTC_TAMPER_1) != 0)
489 sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
491 if((sTamper->Tamper & RTC_TAMPER_2) != 0)
493 sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
495 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
496 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
498 if((sTamper->Tamper & RTC_TAMPER_3) != 0)
500 sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
502 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
503 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
507 /* Configure the RTC_TAMPCR register */
508 tmpreg = (uint32_t)((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->NoErase |\
509 (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
510 (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | (uint32_t)sTamper->TimeStampOnTamperDetection);
513 #if defined (STM32L063xx) || defined (STM32L062xx) || defined (STM32L061xx) || \
514 defined (STM32L053xx) || defined (STM32L052xx) || defined (STM32L051xx)
516 hrtc->Instance->TAMPCR &= ((uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | RTC_TAMPCR_TAMPTS |\
517 RTC_TAMPCR_TAMPFREQ | RTC_TAMPCR_TAMPFLT | RTC_TAMPCR_TAMPPRCH |\
518 RTC_TAMPCR_TAMPPUDIS | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP1IE |\
519 RTC_TAMPCR_TAMP2IE | RTC_TAMPCR_TAMP1NOERASE | RTC_TAMPCR_TAMP2NOERASE|\
520 RTC_TAMPCR_TAMP1MF | RTC_TAMPCR_TAMP2MF));
522 #endif /* (STM32L063xx) || (STM32L062xx) || (STM32L061xx) ||
523 * (STM32L053xx) || (STM32L052xx) || (STM32L051xx)
526 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
527 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
529 hrtc->Instance->TAMPCR &= ((uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | RTC_TAMPCR_TAMPTS |\
530 RTC_TAMPCR_TAMPFREQ | RTC_TAMPCR_TAMPFLT | RTC_TAMPCR_TAMPPRCH |\
531 RTC_TAMPCR_TAMPPUDIS | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP1IE |\
532 RTC_TAMPCR_TAMP2IE | RTC_TAMPCR_TAMP3IE | RTC_TAMPCR_TAMP1NOERASE |\
533 RTC_TAMPCR_TAMP2NOERASE | RTC_TAMPCR_TAMP3NOERASE | RTC_TAMPCR_TAMP1MF |\
534 RTC_TAMPCR_TAMP2MF | RTC_TAMPCR_TAMP3MF));
536 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
537 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
540 hrtc->Instance->TAMPCR |= tmpreg;
542 hrtc->State = HAL_RTC_STATE_READY;
544 /* Process Unlocked */
551 * @brief Sets Tamper with interrupt.
552 * @note By calling this API we force the tamper interrupt for all tampers.
553 * @param hrtc: RTC handle
554 * @param sTamper: Pointer to RTC Tamper.
557 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
561 /* Check the parameters */
562 assert_param( IS_RTC_TAMPER(sTamper->Tamper));
563 assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
564 assert_param( IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
565 assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
566 assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
567 assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
568 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
569 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
570 assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
571 assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
576 hrtc->State = HAL_RTC_STATE_BUSY;
578 /* Configure the tamper trigger */
579 if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
581 sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
584 if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
586 sTamper->NoErase = 0;
587 if((sTamper->Tamper & RTC_TAMPER_1) != 0)
589 sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
591 if((sTamper->Tamper & RTC_TAMPER_2) != 0)
593 sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
595 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
596 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
598 if((sTamper->Tamper & RTC_TAMPER_3) != 0)
600 sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
602 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
603 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
607 if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
609 sTamper->MaskFlag = 0;
610 if((sTamper->Tamper & RTC_TAMPER_1) != 0)
612 sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
614 if((sTamper->Tamper & RTC_TAMPER_2) != 0)
616 sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
618 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
619 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
621 if((sTamper->Tamper & RTC_TAMPER_3) != 0)
623 sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
625 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
626 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
630 /* Configure the RTC_TAMPCR register */
631 tmpreg = (uint32_t)((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Interrupt | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->NoErase |\
632 (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
633 (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | (uint32_t)sTamper->TimeStampOnTamperDetection);
636 #if defined (STM32L063xx) || defined (STM32L062xx) || defined (STM32L061xx) || \
637 defined (STM32L053xx) || defined (STM32L052xx) || defined (STM32L051xx)
639 hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | RTC_TAMPCR_TAMPTS |\
640 RTC_TAMPCR_TAMPFREQ | RTC_TAMPCR_TAMPFLT | RTC_TAMPCR_TAMPPRCH |\
641 RTC_TAMPCR_TAMPPUDIS | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP1IE |\
642 RTC_TAMPCR_TAMP2IE | RTC_TAMPCR_TAMP1NOERASE | RTC_TAMPCR_TAMP2NOERASE |\
643 RTC_TAMPCR_TAMP1MF | RTC_TAMPCR_TAMP2MF);
645 #endif /* (STM32L063xx) || (STM32L062xx) || (STM32L061xx) ||
646 * (STM32L053xx) || (STM32L052xx) || (STM32L051xx)
648 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
649 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
651 hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | RTC_TAMPCR_TAMPTS |\
652 RTC_TAMPCR_TAMPFREQ | RTC_TAMPCR_TAMPFLT | RTC_TAMPCR_TAMPPRCH |\
653 RTC_TAMPCR_TAMPPUDIS | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP1IE |\
654 RTC_TAMPCR_TAMP2IE | RTC_TAMPCR_TAMP3IE | RTC_TAMPCR_TAMP1NOERASE |\
655 RTC_TAMPCR_TAMP2NOERASE | RTC_TAMPCR_TAMP3NOERASE | RTC_TAMPCR_TAMP1MF |\
656 RTC_TAMPCR_TAMP2MF | RTC_TAMPCR_TAMP3MF);
658 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
659 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
662 hrtc->Instance->TAMPCR |= tmpreg;
664 /* RTC Tamper Interrupt Configuration: EXTI configuration */
665 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
667 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
669 hrtc->State = HAL_RTC_STATE_READY;
671 /* Process Unlocked */
678 * @brief Deactivates Tamper.
679 * @param hrtc: RTC handle
680 * @param Tamper: Selected tamper pin.
681 * This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2 for NEMO.
682 * This parameter can be any combination of RTC_TAMPER_1, RTC_TAMPER_2 and RTC_TAMPER_3 for BIG NEMO.
685 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
687 assert_param( IS_RTC_TAMPER(Tamper));
692 hrtc->State = HAL_RTC_STATE_BUSY;
694 /* Disable the selected Tamper pin */
695 hrtc->Instance->TAMPCR &= ((uint32_t)~Tamper);
697 if ((Tamper & RTC_TAMPER_1) != 0)
699 /* Disable the Tamper1 interrupt */
700 hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1));
702 if ((Tamper & RTC_TAMPER_2) != 0)
704 /* Disable the Tamper2 interrupt */
705 hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2));
708 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
709 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
711 if ((Tamper & RTC_TAMPER_3) != 0)
713 /* Disable the Tamper2 interrupt */
714 hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3));
717 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
718 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
721 hrtc->State = HAL_RTC_STATE_READY;
723 /* Process Unlocked */
730 * @brief This function handles TimeStamp interrupt request.
731 * @param hrtc: RTC handle
734 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
736 if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
738 /* Get the status of the Interrupt */
739 if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
741 /* TIMESTAMP callback */
742 HAL_RTCEx_TimeStampEventCallback(hrtc);
744 /* Clear the TIMESTAMP interrupt pending bit */
745 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
749 /* Get the status of the Interrupt */
750 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== SET)
752 /* Get the TAMPER Interrupt enable bit and pending bit */
753 if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
754 (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP1IE)) != (uint32_t)RESET))
756 /* Tamper callback */
757 HAL_RTCEx_Tamper1EventCallback(hrtc);
759 /* Clear the Tamper interrupt pending bit */
760 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
764 /* Get the status of the Interrupt */
765 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F)== SET)
767 /* Get the TAMPER Interrupt enable bit and pending bit */
768 if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
769 (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP2IE)) != (uint32_t)RESET))
771 /* Tamper callback */
772 HAL_RTCEx_Tamper2EventCallback(hrtc);
774 /* Clear the Tamper interrupt pending bit */
775 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
779 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
780 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
782 /* Get the status of the Interrupt */
783 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F)== SET)
785 /* Get the TAMPER Interrupt enable bit and pending bit */
786 if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
787 (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP3IE)) != (uint32_t)RESET))
789 /* Tamper callback */
790 HAL_RTCEx_Tamper3EventCallback(hrtc);
792 /* Clear the Tamper interrupt pending bit */
793 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
797 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
798 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
801 /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
802 __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
804 /* Change RTC state */
805 hrtc->State = HAL_RTC_STATE_READY;
809 * @brief TimeStamp callback.
810 * @param hrtc: RTC handle
813 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
815 /* NOTE : This function Should not be modified, when the callback is needed,
816 the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
821 * @brief Tamper 1 callback.
822 * @param hrtc: RTC handle
825 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
827 /* NOTE : This function Should not be modified, when the callback is needed,
828 the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
833 * @brief Tamper 2 callback.
834 * @param hrtc: RTC handle
837 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
839 /* NOTE : This function Should not be modified, when the callback is needed,
840 the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
845 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
846 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
849 * @brief Tamper 3 callback.
850 * @param hrtc: RTC handle
853 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
855 /* NOTE : This function Should not be modified, when the callback is needed,
856 the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
860 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
861 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
866 * @brief This function handles TimeStamp polling request.
867 * @param hrtc: RTC handle
868 * @param Timeout: Timeout duration
871 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
873 uint32_t tickstart = HAL_GetTick();
875 while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
877 if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
879 /* Clear the TIMESTAMP OverRun Flag */
880 __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
882 /* Change TIMESTAMP state */
883 hrtc->State = HAL_RTC_STATE_ERROR;
888 if(Timeout != HAL_MAX_DELAY)
890 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
892 hrtc->State = HAL_RTC_STATE_TIMEOUT;
898 /* Change RTC state */
899 hrtc->State = HAL_RTC_STATE_READY;
905 * @brief This function handles Tamper1 Polling.
906 * @param hrtc: RTC handle
907 * @param Timeout: Timeout duration
910 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
912 uint32_t tickstart = HAL_GetTick();
914 /* Get the status of the Interrupt */
915 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
917 if(Timeout != HAL_MAX_DELAY)
919 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
921 hrtc->State = HAL_RTC_STATE_TIMEOUT;
927 /* Clear the Tamper Flag */
928 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
930 /* Change RTC state */
931 hrtc->State = HAL_RTC_STATE_READY;
937 * @brief This function handles Tamper2 Polling.
938 * @param hrtc: RTC handle
939 * @param Timeout: Timeout duration
942 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
944 uint32_t tickstart = HAL_GetTick();
946 /* Get the status of the Interrupt */
947 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
949 if(Timeout != HAL_MAX_DELAY)
951 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
953 hrtc->State = HAL_RTC_STATE_TIMEOUT;
959 /* Clear the Tamper Flag */
960 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
962 /* Change RTC state */
963 hrtc->State = HAL_RTC_STATE_READY;
969 #if defined (STM32L083xx) || defined (STM32L082xx) || defined (STM32L081xx) || \
970 defined (STM32L073xx) || defined (STM32L072xx) || defined (STM32L071xx)
973 * @brief This function handles Tamper3 Polling.
974 * @param hrtc: RTC handle
975 * @param Timeout: Timeout duration
978 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
980 uint32_t tickstart = HAL_GetTick();
982 /* Get the status of the Interrupt */
983 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP3F) == RESET)
985 if(Timeout != HAL_MAX_DELAY)
987 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
989 hrtc->State = HAL_RTC_STATE_TIMEOUT;
995 /* Clear the Tamper Flag */
996 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP3F);
998 /* Change RTC state */
999 hrtc->State = HAL_RTC_STATE_READY;
1003 #endif /* (STM32L083xx) || (STM32L082xx) || (STM32L081xx) ||
1004 * (STM32L073xx) || (STM32L072xx) || (STM32L071xx)
1011 /** @addtogroup RTCEx_Exported_Functions_Group2
1012 * @brief RTC Wake-up functions
1015 ===============================================================================
1016 ##### RTC Wake-up functions #####
1017 ===============================================================================
1019 [..] This section provide functions allowing to configure Wake-up feature
1026 * @brief Sets wake up timer.
1027 * @param hrtc: RTC handle
1028 * @param WakeUpCounter: Wake up counter
1029 * @param WakeUpClock: Wake up clock
1030 * @retval HAL status
1032 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1034 uint32_t tickstart = 0;
1036 /* Check the parameters */
1037 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1038 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1040 /* Process Locked */
1043 hrtc->State = HAL_RTC_STATE_BUSY;
1045 /* Disable the write protection for RTC registers */
1046 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1048 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1050 tickstart = HAL_GetTick();
1052 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1053 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1055 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1057 /* Enable the write protection for RTC registers */
1058 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1060 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1062 /* Process Unlocked */
1069 /* Clear the Wakeup Timer clock source bits in CR register */
1070 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1072 /* Configure the clock source */
1073 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1075 /* Configure the Wakeup Timer counter */
1076 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1078 /* Enable the Wakeup Timer */
1079 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1081 /* Enable the write protection for RTC registers */
1082 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1084 hrtc->State = HAL_RTC_STATE_READY;
1086 /* Process Unlocked */
1093 * @brief Sets wake up timer with interrupt
1094 * @param hrtc: RTC handle
1095 * @param WakeUpCounter: Wake up counter
1096 * @param WakeUpClock: Wake up clock
1097 * @retval HAL status
1099 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1101 uint32_t tickstart = 0;
1103 /* Check the parameters */
1104 assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1105 assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1107 /* Process Locked */
1110 hrtc->State = HAL_RTC_STATE_BUSY;
1112 /* Disable the write protection for RTC registers */
1113 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1115 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1117 tickstart = HAL_GetTick();
1119 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1120 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1122 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1124 /* Enable the write protection for RTC registers */
1125 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1127 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1129 /* Process Unlocked */
1136 /* Configure the Wakeup Timer counter */
1137 hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1139 /* Clear the Wakeup Timer clock source bits in CR register */
1140 hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1142 /* Configure the clock source */
1143 hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1145 /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
1146 __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1148 __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1150 /* Configure the Interrupt in the RTC_CR register */
1151 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
1153 /* Enable the Wakeup Timer */
1154 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1156 /* Enable the write protection for RTC registers */
1157 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1159 hrtc->State = HAL_RTC_STATE_READY;
1161 /* Process Unlocked */
1168 * @brief Deactivates wake up timer counter.
1169 * @param hrtc: RTC handle
1170 * @retval HAL status
1172 uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1174 uint32_t tickstart = 0;
1176 /* Process Locked */
1179 hrtc->State = HAL_RTC_STATE_BUSY;
1181 /* Disable the write protection for RTC registers */
1182 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1184 /* Disable the Wakeup Timer */
1185 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1187 /* In case of interrupt mode is used, the interrupt source must disabled */
1188 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
1190 tickstart = HAL_GetTick();
1191 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1192 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1194 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1196 /* Enable the write protection for RTC registers */
1197 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1199 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1201 /* Process Unlocked */
1208 /* Enable the write protection for RTC registers */
1209 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1211 hrtc->State = HAL_RTC_STATE_READY;
1213 /* Process Unlocked */
1220 * @brief Gets wake up timer counter.
1221 * @param hrtc: RTC handle
1222 * @retval Counter value
1224 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1226 /* Get the counter value */
1227 return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1231 * @brief This function handles Wake Up Timer interrupt request.
1232 * @param hrtc: RTC handle
1235 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1237 if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
1239 /* Get the status of the Interrupt */
1240 if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
1242 /* WAKEUPTIMER callback */
1243 HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1245 /* Clear the WAKEUPTIMER interrupt pending bit */
1246 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1250 /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1251 __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1253 /* Change RTC state */
1254 hrtc->State = HAL_RTC_STATE_READY;
1258 * @brief Wake Up Timer callback.
1259 * @param hrtc: RTC handle
1262 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1264 /* NOTE : This function Should not be modified, when the callback is needed,
1265 the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1271 * @brief This function handles Wake Up Timer Polling.
1272 * @param hrtc: RTC handle
1273 * @param Timeout: Timeout duration
1274 * @retval HAL status
1276 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1278 uint32_t tickstart = HAL_GetTick();
1280 while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
1282 if(Timeout != HAL_MAX_DELAY)
1284 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1286 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1293 /* Clear the WAKEUPTIMER Flag */
1294 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1296 /* Change RTC state */
1297 hrtc->State = HAL_RTC_STATE_READY;
1307 /** @addtogroup RTCEx_Exported_Functions_Group3
1308 * @brief Extended Peripheral Control functions
1311 ===============================================================================
1312 ##### Extended Peripheral Control functions #####
1313 ===============================================================================
1315 This subsection provides functions allowing to
1316 (+) Writes a data in a specified RTC Backup data register
1317 (+) Read a data in a specified RTC Backup data register
1318 (+) Sets the Coarse calibration parameters.
1319 (+) Deactivates the Coarse calibration parameters
1320 (+) Sets the Smooth calibration parameters.
1321 (+) Configures the Synchronization Shift Control Settings.
1322 (+) Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1323 (+) Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1324 (+) Enables the RTC reference clock detection.
1325 (+) Disable the RTC reference clock detection.
1326 (+) Enables the Bypass Shadow feature.
1327 (+) Disables the Bypass Shadow feature.
1334 * @brief Writes a data in a specified RTC Backup data register.
1335 * @param hrtc: RTC handle
1336 * @param BackupRegister: RTC Backup data Register number.
1337 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1338 * specify the register.
1339 * @param Data: Data to be written in the specified RTC Backup data register.
1342 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1346 /* Check the parameters */
1347 assert_param(IS_RTC_BKP(BackupRegister));
1349 tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1350 tmp += (BackupRegister * 4);
1352 /* Write the specified register */
1353 *(__IO uint32_t *)tmp = (uint32_t)Data;
1357 * @brief Reads data from the specified RTC Backup data Register.
1358 * @param hrtc: RTC handle
1359 * @param BackupRegister: RTC Backup data Register number.
1360 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1361 * specify the register.
1362 * @retval Read value
1364 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1368 /* Check the parameters */
1369 assert_param(IS_RTC_BKP(BackupRegister));
1371 tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1372 tmp += (BackupRegister * 4);
1374 /* Read the specified register */
1375 return (*(__IO uint32_t *)tmp);
1379 * @brief Sets the Smooth calibration parameters.
1380 * @param hrtc: RTC handle
1381 * @param SmoothCalibPeriod: Select the Smooth Calibration Period.
1382 * This parameter can be can be one of the following values :
1383 * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration periode is 32s.
1384 * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration periode is 16s.
1385 * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibartion periode is 8s.
1386 * @param SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
1387 * This parameter can be one of the following values:
1388 * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK puls every 2*11 pulses.
1389 * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1390 * @param SmoothCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
1391 * This parameter can be one any value from 0 to 0x000001FF.
1392 * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
1393 * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
1394 * SmoothCalibMinusPulsesValue mut be equal to 0.
1395 * @retval HAL status
1397 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
1399 uint32_t tickstart = 0;
1401 /* Check the parameters */
1402 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1403 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1404 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
1406 /* Process Locked */
1409 hrtc->State = HAL_RTC_STATE_BUSY;
1411 /* Disable the write protection for RTC registers */
1412 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1414 /* check if a calibration is pending*/
1415 if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
1417 tickstart = HAL_GetTick();
1419 /* check if a calibration is pending*/
1420 while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
1422 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1424 /* Enable the write protection for RTC registers */
1425 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1427 /* Change RTC state */
1428 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1430 /* Process Unlocked */
1438 /* Configure the Smooth calibration settings */
1439 hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmoothCalibMinusPulsesValue);
1441 /* Enable the write protection for RTC registers */
1442 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1444 /* Change RTC state */
1445 hrtc->State = HAL_RTC_STATE_READY;
1447 /* Process Unlocked */
1454 * @brief Configures the Synchronization Shift Control Settings.
1455 * @note When REFCKON is set, firmware must not write to Shift control register.
1456 * @param hrtc: RTC handle
1457 * @param ShiftAdd1S: Select to add or not 1 second to the time calendar.
1458 * This parameter can be one of the following values :
1459 * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1460 * @arg RTC_SHIFTADD1S_RESET: No effect.
1461 * @param ShiftSubFS: Select the number of Second Fractions to substitute.
1462 * This parameter can be one any value from 0 to 0x7FFF.
1463 * @retval HAL status
1465 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1467 uint32_t tickstart = 0;
1469 /* Check the parameters */
1470 assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1471 assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1473 /* Process Locked */
1476 hrtc->State = HAL_RTC_STATE_BUSY;
1478 /* Disable the write protection for RTC registers */
1479 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1481 tickstart = HAL_GetTick();
1483 /* Wait until the shift is completed*/
1484 while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
1486 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1488 /* Enable the write protection for RTC registers */
1489 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1491 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1493 /* Process Unlocked */
1500 /* Check if the reference clock detection is disabled */
1501 if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
1503 /* Configure the Shift settings */
1504 hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1506 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1507 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
1509 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1511 /* Enable the write protection for RTC registers */
1512 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1514 hrtc->State = HAL_RTC_STATE_ERROR;
1516 /* Process Unlocked */
1525 /* Enable the write protection for RTC registers */
1526 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1528 /* Change RTC state */
1529 hrtc->State = HAL_RTC_STATE_ERROR;
1531 /* Process Unlocked */
1537 /* Enable the write protection for RTC registers */
1538 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1540 /* Change RTC state */
1541 hrtc->State = HAL_RTC_STATE_READY;
1543 /* Process Unlocked */
1550 * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1551 * @param hrtc: RTC handle
1552 * @param CalibOutput : Select the Calibration output Selection .
1553 * This parameter can be one of the following values:
1554 * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1555 * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1556 * @retval HAL status
1558 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
1560 /* Check the parameters */
1561 assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1563 /* Process Locked */
1566 hrtc->State = HAL_RTC_STATE_BUSY;
1568 /* Disable the write protection for RTC registers */
1569 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1571 /* Clear flags before config */
1572 hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1574 /* Configure the RTC_CR register */
1575 hrtc->Instance->CR |= (uint32_t)CalibOutput;
1577 __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
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 Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1593 * @param hrtc: RTC handle
1594 * @retval HAL status
1596 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
1598 /* Process Locked */
1601 hrtc->State = HAL_RTC_STATE_BUSY;
1603 /* Disable the write protection for RTC registers */
1604 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1606 __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1608 /* Enable the write protection for RTC registers */
1609 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1611 /* Change RTC state */
1612 hrtc->State = HAL_RTC_STATE_READY;
1614 /* Process Unlocked */
1621 * @brief Enables the RTC reference clock detection.
1622 * @param hrtc: RTC handle
1623 * @retval HAL status
1625 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
1627 /* Process Locked */
1630 hrtc->State = HAL_RTC_STATE_BUSY;
1632 /* Disable the write protection for RTC registers */
1633 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1635 /* Set Initialization mode */
1636 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1638 /* Enable the write protection for RTC registers */
1639 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1642 hrtc->State = HAL_RTC_STATE_ERROR;
1644 /* Process Unlocked */
1651 __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1653 /* Exit Initialization mode */
1654 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1657 /* Enable the write protection for RTC registers */
1658 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1660 /* Change RTC state */
1661 hrtc->State = HAL_RTC_STATE_READY;
1663 /* Process Unlocked */
1670 * @brief Disable the RTC reference clock detection.
1671 * @param hrtc: RTC handle
1672 * @retval HAL status
1674 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
1676 /* Process Locked */
1679 hrtc->State = HAL_RTC_STATE_BUSY;
1681 /* Disable the write protection for RTC registers */
1682 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1684 /* Set Initialization mode */
1685 if(RTC_EnterInitMode(hrtc) != HAL_OK)
1687 /* Enable the write protection for RTC registers */
1688 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1691 hrtc->State = HAL_RTC_STATE_ERROR;
1693 /* Process Unlocked */
1700 __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1702 /* Exit Initialization mode */
1703 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1706 /* Enable the write protection for RTC registers */
1707 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1709 /* Change RTC state */
1710 hrtc->State = HAL_RTC_STATE_READY;
1712 /* Process Unlocked */
1719 * @brief Enables the Bypass Shadow feature.
1720 * @param hrtc: RTC handle
1721 * @note When the Bypass Shadow is enabled the calendar value are taken
1722 * directly from the Calendar counter.
1723 * @retval HAL status
1725 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
1727 /* Process Locked */
1730 hrtc->State = HAL_RTC_STATE_BUSY;
1732 /* Disable the write protection for RTC registers */
1733 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1735 /* Set the BYPSHAD bit */
1736 hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1738 /* Enable the write protection for RTC registers */
1739 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1741 /* Change RTC state */
1742 hrtc->State = HAL_RTC_STATE_READY;
1744 /* Process Unlocked */
1751 * @brief Disables the Bypass Shadow feature.
1752 * @param hrtc: RTC handle
1753 * @note When the Bypass Shadow is enabled the calendar value are taken
1754 * directly from the Calendar counter.
1755 * @retval HAL status
1757 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
1759 /* Process Locked */
1762 hrtc->State = HAL_RTC_STATE_BUSY;
1764 /* Disable the write protection for RTC registers */
1765 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1767 /* Reset the BYPSHAD bit */
1768 hrtc->Instance->CR &= ((uint8_t)~RTC_CR_BYPSHAD);
1770 /* Enable the write protection for RTC registers */
1771 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1773 /* Change RTC state */
1774 hrtc->State = HAL_RTC_STATE_READY;
1776 /* Process Unlocked */
1786 /** @addtogroup RTCEx_Exported_Functions_Group4
1787 * @brief Extended features functions
1790 ===============================================================================
1791 ##### Extended features functions #####
1792 ===============================================================================
1793 [..] This section provides functions allowing to:
1794 (+) RTC Alram B callback
1795 (+) RTC Poll for Alarm B request
1802 * @brief Alarm B callback.
1803 * @param hrtc: RTC handle
1806 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1808 /* NOTE : This function Should not be modified, when the callback is needed,
1809 the HAL_RTC_AlarmBEventCallback could be implemented in the user file
1815 * @brief This function handles AlarmB Polling request.
1816 * @param hrtc: RTC handle
1817 * @param Timeout: Timeout duration
1818 * @retval HAL status
1820 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1822 uint32_t tickstart = HAL_GetTick();
1824 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
1826 if(Timeout != HAL_MAX_DELAY)
1828 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1830 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1836 /* Clear the Alarm Flag */
1837 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1839 /* Change RTC state */
1840 hrtc->State = HAL_RTC_STATE_READY;
1853 #endif /* HAL_RTC_MODULE_ENABLED */
1862 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/