]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_rtc_ex.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_rtc_ex.c
diff --git a/FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_rtc_ex.c b/FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_rtc_ex.c
new file mode 100644 (file)
index 0000000..d985e0a
--- /dev/null
@@ -0,0 +1,1805 @@
+/**\r
+  ******************************************************************************\r
+  * @file    stm32f7xx_hal_rtc_ex.c\r
+  * @author  MCD Application Team\r
+  * @version V1.0.0RC1\r
+  * @date    24-March-2015\r
+  * @brief   RTC HAL module driver.\r
+  *          This file provides firmware functions to manage the following \r
+  *          functionalities of the Real Time Clock (RTC) Extension peripheral:\r
+  *           + RTC Time Stamp functions\r
+  *           + RTC Tamper functions \r
+  *           + RTC Wake-up functions\r
+  *           + Extension Control functions\r
+  *           + Extension RTC features functions    \r
+  *         \r
+  @verbatim\r
+  ==============================================================================\r
+                  ##### How to use this driver #####\r
+  ==============================================================================\r
+  [..] \r
+    (+) Enable the RTC domain access.\r
+    (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour \r
+        format using the HAL_RTC_Init() function.\r
+  \r
+  *** RTC Wakeup configuration ***\r
+  ================================\r
+  [..] \r
+    (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTC_SetWakeUpTimer()\r
+        function. You can also configure the RTC Wakeup timer in interrupt mode \r
+        using the HAL_RTC_SetWakeUpTimer_IT() function.\r
+    (+) To read the RTC WakeUp Counter register, use the HAL_RTC_GetWakeUpTimer() \r
+        function.\r
+  \r
+  *** TimeStamp configuration ***\r
+  ===============================\r
+  [..]\r
+    (+) Enables the RTC TimeStamp using the HAL_RTC_SetTimeStamp() function.\r
+        You can also configure the RTC TimeStamp with interrupt mode using the\r
+        HAL_RTC_SetTimeStamp_IT() function.\r
+    (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()\r
+        function.\r
+\r
+  *** Internal TimeStamp configuration ***\r
+  ===============================\r
+  [..]\r
+    (+) Enables the RTC internal TimeStamp using the HAL_RTC_SetInternalTimeStamp() function.\r
+    (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()\r
+        function.\r
+  \r
+  *** Tamper configuration ***\r
+  ============================\r
+  [..]\r
+    (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge \r
+        or Level according to the Tamper filter (if equal to 0 Edge else Level) \r
+        value, sampling frequency, NoErase, MaskFlag,  precharge or discharge and\r
+        Pull-UP using the HAL_RTC_SetTamper() function. You can configure RTC Tamper\r
+        with interrupt mode using HAL_RTC_SetTamper_IT() function.\r
+    (+) The default configuration of the Tamper erases the backup registers. To avoid\r
+        erase, enable the NoErase field on the RTC_TAMPCR register.\r
+  \r
+  *** Backup Data Registers configuration ***\r
+  ===========================================\r
+  [..]\r
+    (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()\r
+        function.  \r
+    (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()\r
+        function.\r
+     \r
+   @endverbatim\r
+  ******************************************************************************\r
+  * @attention\r
+  *\r
+  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
+  *\r
+  * Redistribution and use in source and binary forms, with or without modification,\r
+  * are permitted provided that the following conditions are met:\r
+  *   1. Redistributions of source code must retain the above copyright notice,\r
+  *      this list of conditions and the following disclaimer.\r
+  *   2. Redistributions in binary form must reproduce the above copyright notice,\r
+  *      this list of conditions and the following disclaimer in the documentation\r
+  *      and/or other materials provided with the distribution.\r
+  *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
+  *      may be used to endorse or promote products derived from this software\r
+  *      without specific prior written permission.\r
+  *\r
+  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
+  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
+  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
+  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
+  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
+  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
+  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
+  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
+  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+  *\r
+  ******************************************************************************\r
+  */ \r
+\r
+/* Includes ------------------------------------------------------------------*/\r
+#include "stm32f7xx_hal.h"\r
+\r
+/** @addtogroup STM32F7xx_HAL_Driver\r
+  * @{\r
+  */\r
+\r
+/** @defgroup RTCEx RTCEx \r
+  * @brief RTC Extended HAL module driver\r
+  * @{\r
+  */\r
+\r
+#ifdef HAL_RTC_MODULE_ENABLED\r
+\r
+/* Private typedef -----------------------------------------------------------*/\r
+/* Private define ------------------------------------------------------------*/\r
+/* Private macro -------------------------------------------------------------*/\r
+/* Private variables ---------------------------------------------------------*/\r
+/* Private function prototypes -----------------------------------------------*/\r
+/* Private functions ---------------------------------------------------------*/\r
+\r
+/** @defgroup RTCEx_Exported_Functions RTC Extended Exported Functions\r
+  * @{\r
+  */\r
+  \r
+\r
+/** @defgroup RTCEx_Group1 RTC TimeStamp and Tamper functions\r
+ *  @brief   RTC TimeStamp and Tamper functions\r
+ *\r
+@verbatim   \r
+ ===============================================================================\r
+                 ##### RTC TimeStamp and Tamper functions #####\r
+ ===============================================================================  \r
\r
+ [..] This section provides functions allowing to configure TimeStamp feature\r
+\r
+@endverbatim\r
+  * @{\r
+  */\r
+\r
+/**\r
+  * @brief  Sets TimeStamp.\r
+  * @note   This API must be called before enabling the TimeStamp feature. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  TimeStampEdge: Specifies the pin edge on which the TimeStamp is \r
+  *         activated.\r
+  *          This parameter can be one of the following values:\r
+  *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the  \r
+  *                                        rising edge of the related pin.\r
+  *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the \r
+  *                                         falling edge of the related pin.\r
+  * @param  RTC_TimeStampPin: specifies the RTC TimeStamp Pin.\r
+  *          This parameter can be one of the following values:\r
+  *             @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.\r
+  *             @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin.  \r
+  *             @arg RTC_TIMESTAMPPIN_PC1: PC1 is selected as RTC TimeStamp Pin.   \r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)\r
+{\r
+  uint32_t tmpreg = 0;\r
+  \r
+  /* Check the parameters */\r
+  assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));\r
+  assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));\r
+  \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Get the RTC_CR register and clear the bits to be configured */\r
+  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));\r
+    \r
+  tmpreg|= TimeStampEdge;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  hrtc->Instance->OR &= (uint32_t)~RTC_OR_TSINSEL;\r
+  hrtc->Instance->OR |= (uint32_t)(RTC_TimeStampPin); \r
+  \r
+  /* Configure the Time Stamp TSEDGE and Enable bits */\r
+  hrtc->Instance->CR = (uint32_t)tmpreg;\r
+  \r
+  __HAL_RTC_TIMESTAMP_ENABLE(hrtc);\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);    \r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Sets TimeStamp with Interrupt. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @note   This API must be called before enabling the TimeStamp feature.\r
+  * @param  TimeStampEdge: Specifies the pin edge on which the TimeStamp is \r
+  *         activated.\r
+  *          This parameter can be one of the following values:\r
+  *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the  \r
+  *                                        rising edge of the related pin.\r
+  *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the \r
+  *                                         falling edge of the related pin.\r
+  * @param  RTC_TimeStampPin: Specifies the RTC TimeStamp Pin.\r
+  *          This parameter can be one of the following values:\r
+  *             @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.\r
+  *             @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin.  \r
+  *             @arg RTC_TIMESTAMPPIN_PC1: PC1 is selected as RTC TimeStamp Pin.   \r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)\r
+{\r
+  uint32_t tmpreg = 0;\r
+  \r
+  /* Check the parameters */\r
+  assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));\r
+  assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));\r
+  \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Get the RTC_CR register and clear the bits to be configured */\r
+  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));\r
+  \r
+  tmpreg |= TimeStampEdge;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* Configure the Time Stamp TSEDGE and Enable bits */\r
+  hrtc->Instance->CR = (uint32_t)tmpreg;\r
+  \r
+  hrtc->Instance->OR &= (uint32_t)~RTC_OR_TSINSEL;\r
+  hrtc->Instance->OR |= (uint32_t)(RTC_TimeStampPin); \r
+  \r
+  __HAL_RTC_TIMESTAMP_ENABLE(hrtc);\r
+  \r
+  /* Enable IT timestamp */ \r
+  __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);\r
+  \r
+  /* RTC timestamp Interrupt Configuration: EXTI configuration */\r
+  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();\r
+  \r
+  EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);  \r
+  \r
+  hrtc->State = HAL_RTC_STATE_READY;  \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Deactivates TimeStamp. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)\r
+{\r
+  uint32_t tmpreg = 0;\r
+  \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* In case of interrupt mode is used, the interrupt source must disabled */ \r
+  __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);\r
+  \r
+  /* Get the RTC_CR register and clear the bits to be configured */\r
+  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));\r
+  \r
+  /* Configure the Time Stamp TSEDGE and Enable bits */\r
+  hrtc->Instance->CR = (uint32_t)tmpreg;\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
\r
+  hrtc->State = HAL_RTC_STATE_READY;  \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Sets Internal TimeStamp.\r
+  * @note   This API must be called before enabling the internal TimeStamp feature.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* Process Locked */\r
+  __HAL_LOCK(hrtc);\r
+\r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+\r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+\r
+  /* Configure the internal Time Stamp Enable bits */\r
+  __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);\r
+\r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+\r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY;\r
+\r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+\r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Deactivates internal TimeStamp.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* Process Locked */\r
+  __HAL_LOCK(hrtc);\r
+\r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+\r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+\r
+  /* Configure the internal Time Stamp Enable bits */\r
+  __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);\r
+\r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+\r
+  hrtc->State = HAL_RTC_STATE_READY;\r
+\r
+  /* Process Unlocked */\r
+  __HAL_UNLOCK(hrtc);\r
+\r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Gets the RTC TimeStamp value.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  sTimeStamp: Pointer to Time structure\r
+  * @param  sTimeStampDate: Pointer to Date structure  \r
+  * @param  Format: specifies the format of the entered parameters.\r
+  *          This parameter can be one of the following values:\r
+  *             FORMAT_BIN: Binary data format \r
+  *             FORMAT_BCD: BCD data format\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)\r
+{\r
+  uint32_t tmptime = 0, tmpdate = 0;\r
+\r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_FORMAT(Format));\r
+\r
+  /* Get the TimeStamp time and date registers values */\r
+  tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);\r
+  tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);\r
+\r
+  /* Fill the Time structure fields with the read parameters */\r
+  sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);\r
+  sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);\r
+  sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));\r
+  sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);  \r
+  sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;\r
+  \r
+  /* Fill the Date structure fields with the read parameters */\r
+  sTimeStampDate->Year = 0;\r
+  sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);\r
+  sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));\r
+  sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);\r
+\r
+  /* Check the input parameters format */\r
+  if(Format == RTC_FORMAT_BIN)\r
+  {\r
+    /* Convert the TimeStamp structure parameters to Binary format */\r
+    sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);\r
+    sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);\r
+    sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);\r
+    \r
+    /* Convert the DateTimeStamp structure parameters to Binary format */\r
+    sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);\r
+    sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);\r
+    sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);\r
+  }\r
+  \r
+  /* Clear the TIMESTAMP Flag */\r
+  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);\r
+    \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Sets Tamper\r
+  * @note   By calling this API we disable the tamper interrupt for all tampers. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  sTamper: Pointer to Tamper Structure.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)\r
+{\r
+  uint32_t tmpreg = 0;\r
+  \r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_TAMPER(sTamper->Tamper)); \r
+  assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));\r
+  assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));\r
+  assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));\r
+  assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));\r
+  assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));         \r
+  assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));\r
+  assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));\r
+  assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));\r
\r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+    \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+\r
+  if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)\r
+  { \r
+    sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1); \r
+  } \r
+  \r
+  if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)\r
+  { \r
+    sTamper->NoErase = 0;\r
+    if((sTamper->Tamper & RTC_TAMPER_1) != 0)\r
+    {\r
+      sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;\r
+    }\r
+    if((sTamper->Tamper & RTC_TAMPER_2) != 0)\r
+    {\r
+      sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;\r
+    }\r
+    if((sTamper->Tamper & RTC_TAMPER_3) != 0)\r
+    {\r
+      sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;\r
+    }\r
+  }\r
+\r
+  if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)\r
+  {\r
+    sTamper->MaskFlag = 0;\r
+    if((sTamper->Tamper & RTC_TAMPER_1) != 0)\r
+    {\r
+      sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;\r
+    }\r
+    if((sTamper->Tamper & RTC_TAMPER_2) != 0)\r
+    {\r
+      sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;\r
+    }\r
+    if((sTamper->Tamper & RTC_TAMPER_3) != 0)\r
+    {\r
+      sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;\r
+    }\r
+  }\r
+  \r
+  tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger  | (uint32_t)sTamper->NoErase |\\r
+            (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\\r
+            (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);\r
+\r
+  hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAMPCR_TAMPTS |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMPFREQ | (uint32_t)RTC_TAMPCR_TAMPFLT | (uint32_t)RTC_TAMPCR_TAMPPRCH |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE | (uint32_t)RTC_TAMPCR_TAMP1IE |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMP2IE | (uint32_t)RTC_TAMPCR_TAMP3IE | (uint32_t)RTC_TAMPCR_TAMP1NOERASE |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMP2MF | (uint32_t)RTC_TAMPCR_TAMP3MF);\r
+\r
+  hrtc->Instance->TAMPCR |= tmpreg;      \r
+      \r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+\r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+    \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Sets Tamper with interrupt.\r
+  * @note   By calling this API we force the tamper interrupt for all tampers.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  sTamper: Pointer to RTC Tamper.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)\r
+{\r
+  uint32_t tmpreg = 0;\r
+  \r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_TAMPER(sTamper->Tamper)); \r
+  assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));\r
+  assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));\r
+  assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));\r
+  assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));\r
+  assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));\r
+  assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));         \r
+  assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));\r
+  assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));\r
+  assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));\r
\r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+      \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Configure the tamper trigger */\r
+  if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)\r
+  { \r
+    sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1); \r
+  } \r
+  \r
+  if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)\r
+  { \r
+    sTamper->NoErase = 0;\r
+    if((sTamper->Tamper & RTC_TAMPER_1) != 0)\r
+    {\r
+      sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;\r
+    }\r
+    if((sTamper->Tamper & RTC_TAMPER_2) != 0)\r
+    {\r
+      sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;\r
+    }\r
+    if((sTamper->Tamper & RTC_TAMPER_3) != 0)\r
+    {\r
+      sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;\r
+    }\r
+  }\r
+\r
+  if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)\r
+  {\r
+    sTamper->MaskFlag = 0;\r
+    if((sTamper->Tamper & RTC_TAMPER_1) != 0)\r
+    {\r
+      sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;\r
+    }\r
+    if((sTamper->Tamper & RTC_TAMPER_2) != 0)\r
+    {\r
+      sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;\r
+    }\r
+    if((sTamper->Tamper & RTC_TAMPER_3) != 0)\r
+    {\r
+      sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;\r
+    }\r
+  }\r
+  \r
+  tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Interrupt | (uint32_t)sTamper->Trigger  | (uint32_t)sTamper->NoErase |\\r
+            (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\\r
+            (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);\r
+  \r
+  hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAMPCR_TAMPTS |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMPFREQ | (uint32_t)RTC_TAMPCR_TAMPFLT | (uint32_t)RTC_TAMPCR_TAMPPRCH |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE | (uint32_t)RTC_TAMPCR_TAMP1IE |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMP2IE | (uint32_t)RTC_TAMPCR_TAMP3IE | (uint32_t)RTC_TAMPCR_TAMP1NOERASE |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF |\\r
+                                       (uint32_t)RTC_TAMPCR_TAMP2MF | (uint32_t)RTC_TAMPCR_TAMP3MF);\r
+\r
+  hrtc->Instance->TAMPCR |= tmpreg;\r
+\r
+  /* RTC Tamper Interrupt Configuration: EXTI configuration */\r
+  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();\r
+\r
+  EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;\r
+  \r
+  hrtc->State = HAL_RTC_STATE_READY;   \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Deactivates Tamper.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  Tamper: Selected tamper pin.\r
+  *          This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)\r
+{\r
+  assert_param(IS_RTC_TAMPER(Tamper)); \r
+  \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+      \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+/* Disable the selected Tamper pin */\r
+  hrtc->Instance->TAMPCR &= (uint32_t)~Tamper;\r
+\r
+  if ((Tamper & RTC_TAMPER_1) != 0)\r
+  {\r
+    /* Disable the Tamper1 interrupt */\r
+    hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1);\r
+  }\r
+  if ((Tamper & RTC_TAMPER_2) != 0)\r
+  {\r
+    /* Disable the Tamper2 interrupt */\r
+    hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2);\r
+  }\r
+  if ((Tamper & RTC_TAMPER_3) != 0)\r
+  {\r
+    /* Disable the Tamper2 interrupt */\r
+    hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3);\r
+  } \r
+  \r
+  hrtc->State = HAL_RTC_STATE_READY;   \r
+  \r
+  /* Process Unlocked */  \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK; \r
+}\r
+\r
+/**\r
+  * @brief  This function handles TimeStamp interrupt request.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval None\r
+  */\r
+void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)\r
+{  \r
+  if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))\r
+  {\r
+    /* Get the status of the Interrupt */\r
+    if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)\r
+    {\r
+       /* TIMESTAMP callback */ \r
+        HAL_RTCEx_TimeStampEventCallback(hrtc);\r
+\r
+      /* Clear the TIMESTAMP interrupt pending bit */\r
+      __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);\r
+    }\r
+  }\r
+\r
+  /* Get the status of the Interrupt */\r
+  if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== SET)\r
+  {\r
+    /* Get the TAMPER Interrupt enable bit and pending bit */\r
+    if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \\r
+       (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP1IE)) != (uint32_t)RESET))\r
+    {\r
+      /* Tamper callback */\r
+      HAL_RTCEx_Tamper1EventCallback(hrtc);\r
+\r
+      /* Clear the Tamper interrupt pending bit */\r
+      __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);\r
+    }\r
+  }\r
+\r
+  /* Get the status of the Interrupt */\r
+  if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F)== SET)\r
+  {\r
+    /* Get the TAMPER Interrupt enable bit and pending bit */\r
+    if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \\r
+       (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP2IE)) != (uint32_t)RESET))\r
+    {\r
+      /* Tamper callback */\r
+      HAL_RTCEx_Tamper2EventCallback(hrtc);\r
+\r
+      /* Clear the Tamper interrupt pending bit */\r
+      __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);\r
+    }\r
+  }\r
+\r
+  /* Get the status of the Interrupt */\r
+  if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F)== SET)\r
+  {\r
+    /* Get the TAMPER Interrupt enable bit and pending bit */\r
+    if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \\r
+       (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP3IE)) != (uint32_t)RESET))\r
+    {\r
+      /* Tamper callback */\r
+      HAL_RTCEx_Tamper3EventCallback(hrtc);\r
+\r
+      /* Clear the Tamper interrupt pending bit */\r
+      __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);\r
+    }\r
+  }\r
+  \r
+  /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */\r
+  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();\r
+\r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY;\r
+}\r
+\r
+/**\r
+  * @brief  TimeStamp callback. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval None\r
+  */\r
+__weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* NOTE : This function Should not be modified, when the callback is needed,\r
+            the HAL_RTC_TimeStampEventCallback could be implemented in the user file\r
+  */\r
+}\r
+\r
+/**\r
+  * @brief  Tamper 1 callback. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval None\r
+  */\r
+__weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* NOTE : This function Should not be modified, when the callback is needed,\r
+            the HAL_RTC_Tamper1EventCallback could be implemented in the user file\r
+   */\r
+}\r
+\r
+/**\r
+  * @brief  Tamper 2 callback. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval None\r
+  */\r
+__weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* NOTE : This function Should not be modified, when the callback is needed,\r
+            the HAL_RTC_Tamper2EventCallback could be implemented in the user file\r
+   */\r
+}\r
+\r
+/**\r
+  * @brief  Tamper 3 callback. \r
+  * @param  hrtc: RTC handle\r
+  * @retval None\r
+  */\r
+__weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* NOTE : This function Should not be modified, when the callback is needed,\r
+            the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file\r
+   */\r
+}\r
+\r
+/**\r
+  * @brief  This function handles TimeStamp polling request.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  Timeout: Timeout duration\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)\r
+{ \r
+  uint32_t tickstart = 0; \r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+\r
+  while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)\r
+  {            \r
+    if(Timeout != HAL_MAX_DELAY)\r
+    {\r
+      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
+      {\r
+        hrtc->State = HAL_RTC_STATE_TIMEOUT;\r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  }\r
+       \r
+  if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)\r
+  {\r
+    /* Clear the TIMESTAMP OverRun Flag */\r
+    __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);\r
+      \r
+    /* Change TIMESTAMP state */\r
+    hrtc->State = HAL_RTC_STATE_ERROR; \r
+      \r
+    return HAL_ERROR; \r
+   }\r
+       \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  return HAL_OK; \r
+}\r
+  \r
+/**\r
+  * @brief  This function handles Tamper1 Polling.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  Timeout: Timeout duration\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)\r
+{  \r
+  uint32_t tickstart = 0; \r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+  \r
+  /* Get the status of the Interrupt */\r
+  while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)\r
+  {\r
+    if(Timeout != HAL_MAX_DELAY)\r
+    {\r
+      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
+      {\r
+        hrtc->State = HAL_RTC_STATE_TIMEOUT;\r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  }\r
+  \r
+  /* Clear the Tamper Flag */\r
+  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY;\r
+  \r
+  return HAL_OK; \r
+}\r
+\r
+/**\r
+  * @brief  This function handles Tamper2 Polling.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  Timeout: Timeout duration\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)\r
+{  \r
+  uint32_t tickstart = 0; \r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+  \r
+  /* Get the status of the Interrupt */\r
+  while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)\r
+  {\r
+    if(Timeout != HAL_MAX_DELAY)\r
+    {\r
+      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
+      {\r
+        hrtc->State = HAL_RTC_STATE_TIMEOUT;\r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  }\r
+  \r
+  /* Clear the Tamper Flag */\r
+  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY;\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  This function handles Tamper3 Polling.\r
+  * @param  hrtc: RTC handle\r
+  * @param  Timeout: Timeout duration\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)\r
+{\r
+  uint32_t tickstart = HAL_GetTick();\r
+\r
+  /* Get the status of the Interrupt */\r
+  while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == RESET)\r
+  {\r
+    if(Timeout != HAL_MAX_DELAY)\r
+    {\r
+      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
+      {\r
+        hrtc->State = HAL_RTC_STATE_TIMEOUT;\r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  }\r
+\r
+  /* Clear the Tamper Flag */\r
+  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP3F);\r
+\r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY;\r
+\r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @}\r
+  */\r
+  \r
+/** @defgroup RTCEx_Group2 RTC Wake-up functions\r
+ *  @brief   RTC Wake-up functions\r
+ *\r
+@verbatim   \r
+ ===============================================================================\r
+                        ##### RTC Wake-up functions #####\r
+ ===============================================================================  \r
\r
+ [..] This section provides functions allowing to configure Wake-up feature\r
+\r
+@endverbatim\r
+  * @{\r
+  */\r
+\r
+/**\r
+  * @brief  Sets wake up timer. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  WakeUpCounter: Wake up counter\r
+  * @param  WakeUpClock: Wake up clock  \r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)\r
+{\r
+  uint32_t tickstart = 0;\r
+\r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));\r
+  assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));\r
\r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+    \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);\r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+\r
+  /* Wait till RTC WUTWF flag is set and if Time out is reached exit */\r
+  while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)\r
+  {\r
+    if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)\r
+    {\r
+      /* Enable the write protection for RTC registers */\r
+      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+      \r
+      hrtc->State = HAL_RTC_STATE_TIMEOUT; \r
+      \r
+      /* Process Unlocked */ \r
+      __HAL_UNLOCK(hrtc);\r
+      \r
+      return HAL_TIMEOUT;\r
+    }  \r
+  }\r
+  \r
+  /* Clear the Wakeup Timer clock source bits in CR register */\r
+  hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;\r
+  \r
+  /* Configure the clock source */\r
+  hrtc->Instance->CR |= (uint32_t)WakeUpClock;\r
+  \r
+  /* Configure the Wakeup Timer counter */\r
+  hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;\r
+  \r
+   /* Enable the Wakeup Timer */\r
+  __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);   \r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); \r
+  \r
+  hrtc->State = HAL_RTC_STATE_READY;   \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Sets wake up timer with interrupt\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  WakeUpCounter: Wake up counter\r
+  * @param  WakeUpClock: Wake up clock  \r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)\r
+{\r
+  uint32_t tickstart = 0;\r
+  \r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));\r
+  assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));\r
+  \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);\r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+\r
+  /* Wait till RTC WUTWF flag is set and if Time out is reached exit */\r
+  while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)\r
+  {\r
+    if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)\r
+    {\r
+      /* Enable the write protection for RTC registers */\r
+      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+      \r
+      hrtc->State = HAL_RTC_STATE_TIMEOUT; \r
+      \r
+      /* Process Unlocked */ \r
+      __HAL_UNLOCK(hrtc);\r
+      \r
+      return HAL_TIMEOUT;\r
+    }  \r
+  }\r
+  \r
+  /* Configure the Wakeup Timer counter */\r
+  hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;\r
+\r
+  /* Clear the Wakeup Timer clock source bits in CR register */\r
+  hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;\r
+\r
+  /* Configure the clock source */\r
+  hrtc->Instance->CR |= (uint32_t)WakeUpClock;\r
+  \r
+  /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */\r
+  __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();\r
+  \r
+  EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;\r
+  \r
+  /* Configure the Interrupt in the RTC_CR register */\r
+  __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);\r
+  \r
+  /* Enable the Wakeup Timer */\r
+  __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);\r
+    \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); \r
+  \r
+  hrtc->State = HAL_RTC_STATE_READY;   \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
\r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Deactivates wake up timer counter.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC. \r
+  * @retval HAL status\r
+  */\r
+uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)\r
+{\r
+  uint32_t tickstart = 0;\r
+  \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* Disable the Wakeup Timer */\r
+  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);\r
+  \r
+  /* In case of interrupt mode is used, the interrupt source must disabled */ \r
+  __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);\r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+\r
+  /* Wait till RTC WUTWF flag is set and if Time out is reached exit */\r
+  while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)\r
+  {\r
+    if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)\r
+    {\r
+      /* Enable the write protection for RTC registers */\r
+      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+      \r
+      hrtc->State = HAL_RTC_STATE_TIMEOUT; \r
+      \r
+      /* Process Unlocked */ \r
+      __HAL_UNLOCK(hrtc);\r
+      \r
+      return HAL_TIMEOUT;\r
+    }   \r
+  }\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_READY;   \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Gets wake up timer counter.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC. \r
+  * @retval Counter value\r
+  */\r
+uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* Get the counter value */\r
+  return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT)); \r
+}\r
+\r
+/**\r
+  * @brief  This function handles Wake Up Timer interrupt request.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval None\r
+  */\r
+void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)\r
+{  \r
+  if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))\r
+  {\r
+    /* Get the status of the Interrupt */\r
+    if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)\r
+    {\r
+      /* WAKEUPTIMER callback */ \r
+      HAL_RTCEx_WakeUpTimerEventCallback(hrtc);\r
+      \r
+      /* Clear the WAKEUPTIMER interrupt pending bit */\r
+      __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);\r
+    }\r
+  }\r
+  \r
+  /* Clear the EXTI's line Flag for RTC WakeUpTimer */\r
+  __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY;\r
+}\r
+\r
+/**\r
+  * @brief  Wake Up Timer callback.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval None\r
+  */\r
+__weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* NOTE : This function Should not be modified, when the callback is needed,\r
+            the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file\r
+   */\r
+}\r
+\r
+/**\r
+  * @brief  This function handles Wake Up Timer Polling.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  Timeout: Timeout duration\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)\r
+{  \r
+  uint32_t tickstart = 0; \r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+\r
+  while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)\r
+  {\r
+    if(Timeout != HAL_MAX_DELAY)\r
+    {\r
+      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
+      {\r
+        hrtc->State = HAL_RTC_STATE_TIMEOUT;\r
+      \r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  }\r
+  \r
+  /* Clear the WAKEUPTIMER Flag */\r
+  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY;\r
+  \r
+  return HAL_OK; \r
+}\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+\r
+/** @defgroup RTCEx_Group3 Extension Peripheral Control functions \r
+ *  @brief   Extension Peripheral Control functions \r
+ *\r
+@verbatim   \r
+ ===============================================================================\r
+              ##### Extension Peripheral Control functions #####\r
+ ===============================================================================  \r
+    [..]\r
+    This subsection provides functions allowing to\r
+      (+) Write a data in a specified RTC Backup data register\r
+      (+) Read a data in a specified RTC Backup data register\r
+      (+) Set the Coarse calibration parameters.\r
+      (+) Deactivate the Coarse calibration parameters\r
+      (+) Set the Smooth calibration parameters.\r
+      (+) Configure the Synchronization Shift Control Settings.\r
+      (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).\r
+      (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).\r
+      (+) Enable the RTC reference clock detection.\r
+      (+) Disable the RTC reference clock detection.\r
+      (+) Enable the Bypass Shadow feature.\r
+      (+) Disable the Bypass Shadow feature.\r
+\r
+@endverbatim\r
+  * @{\r
+  */\r
+\r
+/**\r
+  * @brief  Writes a data in a specified RTC Backup data register.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC. \r
+  * @param  BackupRegister: RTC Backup data Register number.\r
+  *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to \r
+  *                                 specify the register.\r
+  * @param  Data: Data to be written in the specified RTC Backup data register.                     \r
+  * @retval None\r
+  */\r
+void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)\r
+{\r
+  uint32_t tmp = 0;\r
+  \r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_BKP(BackupRegister));\r
+  \r
+  tmp = (uint32_t)&(hrtc->Instance->BKP0R);\r
+  tmp += (BackupRegister * 4);\r
+  \r
+  /* Write the specified register */\r
+  *(__IO uint32_t *)tmp = (uint32_t)Data;\r
+}\r
+\r
+/**\r
+  * @brief  Reads data from the specified RTC Backup data Register.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC. \r
+  * @param  BackupRegister: RTC Backup data Register number.\r
+  *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to \r
+  *                                 specify the register.                   \r
+  * @retval Read value\r
+  */\r
+uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)\r
+{\r
+  uint32_t tmp = 0;\r
+  \r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_BKP(BackupRegister));\r
+\r
+  tmp = (uint32_t)&(hrtc->Instance->BKP0R);\r
+  tmp += (BackupRegister * 4);\r
+  \r
+  /* Read the specified register */\r
+  return (*(__IO uint32_t *)tmp);\r
+}\r
+\r
+/**\r
+  * @brief  Sets the Smooth calibration parameters.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.  \r
+  * @param  SmoothCalibPeriod: Select the Smooth Calibration Period.\r
+  *          This parameter can be can be one of the following values :\r
+  *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.\r
+  *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.\r
+  *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.\r
+  * @param  SmoothCalibPlusPulses: Select to Set or reset the CALP bit.\r
+  *          This parameter can be one of the following values:\r
+  *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulses every 2*11 pulses.\r
+  *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.\r
+  * @param  SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.\r
+  *          This parameter can be one any value from 0 to 0x000001FF.\r
+  * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses \r
+  *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field \r
+  *         SmouthCalibMinusPulsesValue must be equal to 0.  \r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)\r
+{\r
+  uint32_t tickstart = 0;\r
+  \r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));\r
+  assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));\r
+  assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));\r
+  \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* check if a calibration is pending*/\r
+  if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)\r
+  {\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+\r
+    /* check if a calibration is pending*/\r
+    while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)\r
+    {\r
+      if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)\r
+      {\r
+        /* Enable the write protection for RTC registers */\r
+        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+        \r
+        /* Change RTC state */\r
+        hrtc->State = HAL_RTC_STATE_TIMEOUT; \r
+        \r
+        /* Process Unlocked */ \r
+        __HAL_UNLOCK(hrtc);\r
+        \r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  }\r
+  \r
+  /* Configure the Smooth calibration settings */\r
+  hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Configures the Synchronization Shift Control Settings.\r
+  * @note   When REFCKON is set, firmware must not write to Shift control register. \r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.    \r
+  * @param  ShiftAdd1S: Select to add or not 1 second to the time calendar.\r
+  *          This parameter can be one of the following values :\r
+  *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar. \r
+  *             @arg RTC_SHIFTADD1S_RESET: No effect.\r
+  * @param  ShiftSubFS: Select the number of Second Fractions to substitute.\r
+  *          This parameter can be one any value from 0 to 0x7FFF.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)\r
+{\r
+  uint32_t tickstart = 0;\r
+\r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));\r
+  assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));\r
+\r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+\r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+\r
+    /* Wait until the shift is completed*/\r
+    while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)\r
+    {\r
+      if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)\r
+      {  \r
+        /* Enable the write protection for RTC registers */\r
+        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);  \r
+        \r
+        hrtc->State = HAL_RTC_STATE_TIMEOUT;\r
+        \r
+        /* Process Unlocked */ \r
+        __HAL_UNLOCK(hrtc);\r
+        \r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  \r
+    /* Check if the reference clock detection is disabled */\r
+    if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)\r
+    {\r
+      /* Configure the Shift settings */\r
+      hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);\r
+      \r
+      /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */\r
+      if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)\r
+      {\r
+        if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)\r
+        {\r
+          /* Enable the write protection for RTC registers */\r
+          __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);  \r
+          \r
+          hrtc->State = HAL_RTC_STATE_ERROR;\r
+          \r
+          /* Process Unlocked */ \r
+          __HAL_UNLOCK(hrtc);\r
+          \r
+          return HAL_ERROR;\r
+        }\r
+      }\r
+    }\r
+    else\r
+    {\r
+      /* Enable the write protection for RTC registers */\r
+      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+      \r
+      /* Change RTC state */\r
+      hrtc->State = HAL_RTC_STATE_ERROR; \r
+      \r
+      /* Process Unlocked */ \r
+      __HAL_UNLOCK(hrtc);\r
+      \r
+      return HAL_ERROR;\r
+    }\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.    \r
+  * @param  CalibOutput: Select the Calibration output Selection .\r
+  *          This parameter can be one of the following values:\r
+  *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz. \r
+  *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)\r
+{\r
+  /* Check the parameters */\r
+  assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));\r
+  \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+\r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* Clear flags before config */\r
+  hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;\r
+  \r
+  /* Configure the RTC_CR register */\r
+  hrtc->Instance->CR |= (uint32_t)CalibOutput;\r
+  \r
+  __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.    \r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)\r
+{\r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);\r
+    \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Enables the RTC reference clock detection.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.    \r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)\r
+{\r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* Set Initialization mode */\r
+  if(RTC_EnterInitMode(hrtc) != HAL_OK)\r
+  {\r
+    /* Enable the write protection for RTC registers */\r
+    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); \r
+    \r
+    /* Set RTC state*/\r
+    hrtc->State = HAL_RTC_STATE_ERROR;\r
+    \r
+    /* Process Unlocked */ \r
+    __HAL_UNLOCK(hrtc);\r
+    \r
+    return HAL_ERROR;\r
+  } \r
+  else\r
+  {\r
+    __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);\r
+\r
+    /* Exit Initialization mode */\r
+    hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; \r
+  }\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+   /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Disable the RTC reference clock detection.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.    \r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)\r
+{ \r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* Set Initialization mode */\r
+  if(RTC_EnterInitMode(hrtc) != HAL_OK)\r
+  {\r
+    /* Enable the write protection for RTC registers */\r
+    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); \r
+    \r
+    /* Set RTC state*/\r
+    hrtc->State = HAL_RTC_STATE_ERROR;\r
+    \r
+    /* Process Unlocked */ \r
+    __HAL_UNLOCK(hrtc);\r
+    \r
+    return HAL_ERROR;\r
+  } \r
+  else\r
+  {\r
+    __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);\r
+    \r
+    /* Exit Initialization mode */\r
+    hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; \r
+  }\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Enables the Bypass Shadow feature.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.  \r
+  * @note   When the Bypass Shadow is enabled the calendar value are taken \r
+  *         directly from the Calendar counter.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)\r
+{\r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* Set the BYPSHAD bit */\r
+  hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Disables the Bypass Shadow feature.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.  \r
+  * @note   When the Bypass Shadow is enabled the calendar value are taken \r
+  *         directly from the Calendar counter.\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)\r
+{\r
+  /* Process Locked */ \r
+  __HAL_LOCK(hrtc);\r
+  \r
+  hrtc->State = HAL_RTC_STATE_BUSY;\r
+  \r
+  /* Disable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);\r
+  \r
+  /* Reset the BYPSHAD bit */\r
+  hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;\r
+  \r
+  /* Enable the write protection for RTC registers */\r
+  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  /* Process Unlocked */ \r
+  __HAL_UNLOCK(hrtc);\r
+  \r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+  /** @defgroup RTCEx_Group4 Extended features functions \r
+ *  @brief    Extended features functions  \r
+ *\r
+@verbatim   \r
+ ===============================================================================\r
+                 ##### Extended features functions #####\r
+ ===============================================================================  \r
+    [..]  This section provides functions allowing to:\r
+      (+) RTC Alram B callback\r
+      (+) RTC Poll for Alarm B request\r
+               \r
+@endverbatim\r
+  * @{\r
+  */\r
+\r
+/**\r
+  * @brief  Alarm B callback.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @retval None\r
+  */\r
+__weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)\r
+{\r
+  /* NOTE : This function Should not be modified, when the callback is needed,\r
+            the HAL_RTC_AlarmBEventCallback could be implemented in the user file\r
+   */\r
+}\r
+\r
+/**\r
+  * @brief  This function handles AlarmB Polling request.\r
+  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains\r
+  *                the configuration information for RTC.\r
+  * @param  Timeout: Timeout duration\r
+  * @retval HAL status\r
+  */\r
+HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)\r
+{  \r
+  uint32_t tickstart = 0; \r
+\r
+  /* Get tick */\r
+  tickstart = HAL_GetTick();\r
+\r
+  while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)\r
+  {\r
+    if(Timeout != HAL_MAX_DELAY)\r
+    {\r
+      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
+      {\r
+        hrtc->State = HAL_RTC_STATE_TIMEOUT;\r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  }\r
+  \r
+  /* Clear the Alarm Flag */\r
+  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);\r
+  \r
+  /* Change RTC state */\r
+  hrtc->State = HAL_RTC_STATE_READY; \r
+  \r
+  return HAL_OK; \r
+}\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+#endif /* HAL_RTC_MODULE_ENABLED */\r
+/**\r
+  * @}\r
+  */\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r