Subversion Repositories DashDisplay

Rev

Rev 56 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32l1xx_hal_rtc_ex.c
  4.   * @author  MCD Application Team
  5.   * @brief   Extended RTC HAL module driver.
  6.   *          This file provides firmware functions to manage the following
  7.   *          functionalities of the Real-Time Clock (RTC) Extended peripheral:
  8.   *           + RTC Timestamp functions
  9.   *           + RTC Tamper functions
  10.   *           + RTC Wakeup functions
  11.   *           + Extended Control functions
  12.   *           + Extended RTC features functions
  13.   *
  14.   ******************************************************************************
  15.   * @attention
  16.   *
  17.   * Copyright (c) 2016 STMicroelectronics.
  18.   * All rights reserved.
  19.   *
  20.   * This software is licensed under terms that can be found in the LICENSE file
  21.   * in the root directory of this software component.
  22.   * If no LICENSE file comes with this software, it is provided AS-IS.
  23.   *
  24.   ******************************************************************************
  25.   @verbatim
  26.   ==============================================================================
  27.                         ##### How to use this driver #####
  28.   ==============================================================================
  29.   [..]
  30.     (+) Enable the RTC domain access.
  31.     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  32.         format using the HAL_RTC_Init() function.
  33.  
  34.   *** RTC Wakeup configuration ***
  35.   ================================
  36.   [..]
  37.     (+) To configure the RTC Wakeup Clock source and Counter use the
  38.         HAL_RTCEx_SetWakeUpTimer() function.
  39.         You can also configure the RTC Wakeup timer in interrupt mode using the
  40.         HAL_RTCEx_SetWakeUpTimer_IT() function.
  41.     (+) To read the RTC Wakeup Counter register, use the HAL_RTCEx_GetWakeUpTimer()
  42.         function.
  43.  
  44.   *** Timestamp configuration ***
  45.   ===============================
  46.   [..]
  47.     (+) To configure the RTC Timestamp use the HAL_RTCEx_SetTimeStamp() function.
  48.         You can also configure the RTC Timestamp with interrupt mode using the
  49.         HAL_RTCEx_SetTimeStamp_IT() function.
  50.     (+) To read the RTC Timestamp Time and Date register, use the
  51.         HAL_RTCEx_GetTimeStamp() function.
  52.     (+) The Timestamp alternate function is mapped to RTC_AF1 (PC13).
  53.  
  54.   *** Tamper configuration ***
  55.   ============================
  56.   [..]
  57.     (+) To Enable the RTC Tamper and configure the Tamper filter count, trigger
  58.         Edge or Level according to the Tamper filter value (if equal to 0 Edge
  59.         else Level), sampling frequency, precharge or discharge and Pull-UP use
  60.         the HAL_RTCEx_SetTamper() function.
  61.         You can configure RTC Tamper in interrupt mode using HAL_RTCEx_SetTamper_IT()
  62.         function.
  63.     (+) The TAMPER1 alternate function is mapped to RTC_AF1 (PC13).
  64.     (+) The TAMPER2 alternate function is mapped to RTC_AF2 (PA0).
  65.     (+) The TAMPER3 alternate function is mapped to RTC_AF3 (PE6).
  66.  
  67.   *** Backup Data Registers configuration ***
  68.   ===========================================
  69.   [..]
  70.     (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  71.         function.
  72.     (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  73.         function.
  74.  
  75.   *** Coarse Digital Calibration configuration ***
  76.   ================================================
  77.   [..]
  78.     (+) The Coarse Digital Calibration can be used to compensate crystal inaccuracy
  79.         by setting the DCS bit in RTC_CALIBR register.
  80.     (+) When positive calibration is enabled (DCS = ‘0’), 2 asynchronous prescaler
  81.         clock cycles are added every minute during 2xDC minutes.
  82.         This causes the calendar to be updated sooner, thereby adjusting the
  83.         effective RTC frequency to be a bit higher.
  84.     (+) When negative calibration is enabled (DCS = ‘1’), 1 asynchronous prescaler
  85.         clock cycle is removed every minute during 2xDC minutes.
  86.         This causes the calendar to be updated later, thereby adjusting the
  87.         effective RTC frequency to be a bit lower.
  88.     (+) DC is configured through bits DC[4:0] of RTC_CALIBR register. This number
  89.         ranges from 0 to 31 corresponding to a time interval (2xDC) ranging from
  90.         0 to 62.
  91.     (+) In order to measure the clock deviation, a 512 Hz clock is output for
  92.         calibration.
  93.     (+) The RTC Coarse Digital Calibration value and sign can be calibrated using
  94.         the HAL_RTCEx_SetCoarseCalib() function.
  95.  
  96.   *** Smooth Digital Calibration configuration ***
  97.   ================================================
  98.   [..]
  99.     (+) RTC frequency can be digitally calibrated with a resolution of about
  100.         0.954 ppm with a range from -487.1 ppm to +488.5 ppm.
  101.         The correction of the frequency is performed using a series of small
  102.         adjustments (adding and/or subtracting individual RTCCLK pulses).
  103.     (+) The smooth digital calibration is performed during a cycle of about 2^20
  104.         RTCCLK pulses (or 32 seconds) when the input frequency is 32,768 Hz.
  105.         This cycle is maintained by a 20-bit counter clocked by RTCCLK.
  106.     (+) The smooth calibration register (RTC_CALR) specifies the number of RTCCLK
  107.         clock cycles to be masked during the 32-second cycle.
  108.     (+) The RTC Smooth Digital Calibration value and the corresponding calibration
  109.         cycle period (32s, 16s, or 8s) can be calibrated using the
  110.         HAL_RTCEx_SetSmoothCalib() function.
  111.  
  112.   @endverbatim
  113.   ******************************************************************************
  114.   */
  115.  
  116. /* Includes ------------------------------------------------------------------*/
  117. #include "stm32l1xx_hal.h"
  118.  
  119. /** @addtogroup STM32L1xx_HAL_Driver
  120.   * @{
  121.   */
  122.  
  123. /** @defgroup RTCEx RTCEx
  124.   * @brief    RTC Extended HAL module driver
  125.   * @{
  126.   */
  127.  
  128. #ifdef HAL_RTC_MODULE_ENABLED
  129.  
  130. /* Private typedef -----------------------------------------------------------*/
  131. /* Private define ------------------------------------------------------------*/
  132. /* Private macro -------------------------------------------------------------*/
  133. /* Private variables ---------------------------------------------------------*/
  134. /* Private function prototypes -----------------------------------------------*/
  135. /* Exported functions --------------------------------------------------------*/
  136.  
  137. /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  138.   * @{
  139.   */
  140.  
  141. /** @defgroup RTCEx_Exported_Functions_Group1 RTC Timestamp and Tamper functions
  142.   * @brief    RTC Timestamp and Tamper functions
  143.   *
  144. @verbatim
  145.  ===============================================================================
  146.                  ##### RTC Timestamp and Tamper functions #####
  147.  ===============================================================================
  148.  
  149.  [..] This section provides functions allowing to configure Timestamp feature
  150.  
  151. @endverbatim
  152.   * @{
  153.   */
  154.  
  155. /**
  156.   * @brief  Sets Timestamp.
  157.   * @note   This API must be called before enabling the Timestamp feature.
  158.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  159.   *                the configuration information for RTC.
  160.   * @param  RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
  161.   *         activated.
  162.   *          This parameter can be one of the following values:
  163.   *             @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
  164.   *                                        the rising edge of the related pin.
  165.   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
  166.   *                                        the falling edge of the related pin.
  167.   * @retval HAL status
  168.   */
  169. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge)
  170. {
  171.   uint32_t tmpreg = 0U;
  172.  
  173.   /* Check the parameters */
  174.   assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
  175.  
  176.   /* Process Locked */
  177.   __HAL_LOCK(hrtc);
  178.  
  179.   /* Change RTC state to BUSY */
  180.   hrtc->State = HAL_RTC_STATE_BUSY;
  181.  
  182.   /* Get the RTC_CR register and clear the bits to be configured */
  183.   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  184.  
  185.   /* Configure the Timestamp TSEDGE bit */
  186.   tmpreg |= RTC_TimeStampEdge;
  187.  
  188.   /* Disable the write protection for RTC registers */
  189.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  190.  
  191.   /* Copy the desired configuration into the CR register */
  192.   hrtc->Instance->CR = (uint32_t)tmpreg;
  193.  
  194.   /* Clear RTC Timestamp flag */
  195.   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  196.  
  197.   /* Clear RTC Timestamp overrun Flag */
  198.   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  199.  
  200.   /* Enable the Timestamp saving */
  201.   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  202.  
  203.   /* Enable the write protection for RTC registers */
  204.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  205.  
  206.   /* Change RTC state back to READY */
  207.   hrtc->State = HAL_RTC_STATE_READY;
  208.  
  209.   /* Process Unlocked */
  210.   __HAL_UNLOCK(hrtc);
  211.  
  212.   return HAL_OK;
  213. }
  214.  
  215. /**
  216.   * @brief  Sets Timestamp with Interrupt.
  217.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  218.   *                the configuration information for RTC.
  219.   * @note   This API must be called before enabling the Timestamp feature.
  220.   * @param  RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
  221.   *         activated.
  222.   *          This parameter can be one of the following values:
  223.   *             @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
  224.   *                                        the rising edge of the related pin.
  225.   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
  226.   *                                        the falling edge of the related pin.
  227.   * @retval HAL status
  228.   */
  229. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge)
  230. {
  231.   uint32_t tmpreg = 0U;
  232.  
  233.   /* Check the parameters */
  234.   assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
  235.  
  236.   /* Process Locked */
  237.   __HAL_LOCK(hrtc);
  238.  
  239.   /* Change RTC state to BUSY */
  240.   hrtc->State = HAL_RTC_STATE_BUSY;
  241.  
  242.   /* Get the RTC_CR register and clear the bits to be configured */
  243.   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  244.  
  245.   /* Configure the Timestamp TSEDGE bit */
  246.   tmpreg |= RTC_TimeStampEdge;
  247.  
  248.   /* Disable the write protection for RTC registers */
  249.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  250.  
  251.   /* Copy the desired configuration into the CR register */
  252.   hrtc->Instance->CR = (uint32_t)tmpreg;
  253.  
  254.   /* Clear RTC Timestamp flag */
  255.   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  256.  
  257.   /* Clear RTC Timestamp overrun Flag */
  258.   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  259.  
  260.   /* Enable the Timestamp saving */
  261.   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  262.  
  263.   /* Enable IT Timestamp */
  264.   __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
  265.  
  266.   /* Enable the write protection for RTC registers */
  267.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  268.  
  269.   /* RTC Timestamp Interrupt Configuration: EXTI configuration */
  270.   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  271.   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  272.  
  273.   /* Change RTC state back to READY */
  274.   hrtc->State = HAL_RTC_STATE_READY;
  275.  
  276.   /* Process Unlocked */
  277.   __HAL_UNLOCK(hrtc);
  278.  
  279.   return HAL_OK;
  280. }
  281.  
  282. /**
  283.   * @brief  Deactivates Timestamp.
  284.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  285.   *                the configuration information for RTC.
  286.   * @retval HAL status
  287.   */
  288. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  289. {
  290.   uint32_t tmpreg = 0U;
  291.  
  292.   /* Process Locked */
  293.   __HAL_LOCK(hrtc);
  294.  
  295.   hrtc->State = HAL_RTC_STATE_BUSY;
  296.  
  297.   /* Disable the write protection for RTC registers */
  298.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  299.  
  300.   /* In case of interrupt mode is used, the interrupt source must disabled */
  301.   __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  302.  
  303.   /* Get the RTC_CR register and clear the bits to be configured */
  304.   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  305.  
  306.   /* Configure the Timestamp TSEDGE and Enable bits */
  307.   hrtc->Instance->CR = (uint32_t)tmpreg;
  308.  
  309.   /* Enable the write protection for RTC registers */
  310.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  311.  
  312.   hrtc->State = HAL_RTC_STATE_READY;
  313.  
  314.   /* Process Unlocked */
  315.   __HAL_UNLOCK(hrtc);
  316.  
  317.   return HAL_OK;
  318. }
  319.  
  320. /**
  321.   * @brief  Gets the RTC Timestamp value.
  322.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  323.   *                the configuration information for RTC.
  324.   * @param  sTimeStamp Pointer to Time structure
  325.   * @param  sTimeStampDate Pointer to Date structure
  326.   * @param  Format specifies the format of the entered parameters.
  327.   *          This parameter can be one of the following values:
  328.   *             @arg RTC_FORMAT_BIN: Binary data format
  329.   *             @arg RTC_FORMAT_BCD: BCD data format
  330.   * @retval HAL status
  331.   */
  332. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
  333. {
  334.   uint32_t tmptime = 0U;
  335.   uint32_t tmpdate = 0U;
  336.  
  337.   /* Check the parameters */
  338.   assert_param(IS_RTC_FORMAT(Format));
  339.  
  340.   /* Get the Timestamp time and date registers values */
  341.   tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  342.   tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  343.  
  344.   /* Fill the Time structure fields with the read parameters */
  345.   sTimeStamp->Hours      = (uint8_t)((tmptime & (RTC_TSTR_HT  | RTC_TSTR_HU))  >> RTC_TSTR_HU_Pos);
  346.   sTimeStamp->Minutes    = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
  347.   sTimeStamp->Seconds    = (uint8_t)((tmptime & (RTC_TSTR_ST  | RTC_TSTR_SU))  >> RTC_TSTR_SU_Pos);
  348.   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM))                 >> RTC_TSTR_PM_Pos);
  349. #if defined(RTC_SUBSECOND_SUPPORT)
  350.   sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
  351. #endif /* RTC_SUBSECOND_SUPPORT */
  352.  
  353.   /* Fill the Date structure fields with the read parameters */
  354.   sTimeStampDate->Year    = 0U;
  355.   sTimeStampDate->Month   = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
  356.   sTimeStampDate->Date    = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
  357.   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU))              >> RTC_TSDR_WDU_Pos);
  358.  
  359.   /* Check the input parameters format */
  360.   if (Format == RTC_FORMAT_BIN)
  361.   {
  362.     /* Convert the Timestamp structure parameters to Binary format */
  363.     sTimeStamp->Hours   = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  364.     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  365.     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  366.  
  367.     /* Convert the DateTimeStamp structure parameters to Binary format */
  368.     sTimeStampDate->Month   = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  369.     sTimeStampDate->Date    = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  370.     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  371.   }
  372.  
  373.   /* Clear the Timestamp Flag */
  374.   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  375.  
  376.   return HAL_OK;
  377. }
  378.  
  379. /**
  380.   * @brief  Sets Tamper.
  381.   * @note   By calling this API the tamper global interrupt will be disabled.
  382.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  383.   *                the configuration information for RTC.
  384.   * @param  sTamper Pointer to Tamper Structure.
  385.   * @retval HAL status
  386.   */
  387. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  388. {
  389.   uint32_t tmpreg = 0U;
  390.  
  391.   /* Check the parameters */
  392.   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  393.   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  394. #if defined(RTC_TAFCR_TAMPFLT)
  395.   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  396.   assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
  397.   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  398.   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  399.   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  400. #endif /* RTC_TAFCR_TAMPFLT */
  401. #if defined(RTC_TAFCR_TAMPTS)
  402.   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  403. #endif /* RTC_TAFCR_TAMPTS */
  404.  
  405.   /* Process Locked */
  406.   __HAL_LOCK(hrtc);
  407.  
  408.   hrtc->State = HAL_RTC_STATE_BUSY;
  409.  
  410.   /* Copy control register into temporary variable */
  411.   tmpreg = hrtc->Instance->TAFCR;
  412.  
  413.   /* Enable selected tamper */
  414.   tmpreg |= (sTamper->Tamper);
  415.  
  416.   /* Configure the tamper trigger bit (this bit is just on the right of the
  417.        tamper enable bit, hence the one-time right shift before updating it) */
  418.   if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
  419.   {
  420.     /* Set the tamper trigger bit (case of falling edge or high level) */
  421.     tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
  422.   }
  423.   else
  424.   {
  425.     /* Clear the tamper trigger bit (case of rising edge or low level) */
  426.     tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
  427.   }
  428.  
  429. #if (defined(RTC_TAFCR_TAMPFLT)  && defined(RTC_TAFCR_TAMPTS) )
  430.   /* Clear remaining fields before setting them */
  431.   tmpreg &= ~(RTC_TAMPERFILTER_MASK              | \
  432.               RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
  433.               RTC_TAMPERPRECHARGEDURATION_MASK   | \
  434.               RTC_TAMPER_PULLUP_MASK             | \
  435.               RTC_TIMESTAMPONTAMPERDETECTION_MASK);
  436.  
  437.   /* Set remaining parameters of desired configuration into temporary variable */
  438.   tmpreg |= ((uint32_t)sTamper->Filter            | \
  439.              (uint32_t)sTamper->SamplingFrequency | \
  440.              (uint32_t)sTamper->PrechargeDuration | \
  441.              (uint32_t)sTamper->TamperPullUp      | \
  442.              (uint32_t)sTamper->TimeStampOnTamperDetection);
  443. #endif /* RTC_TAFCR_TAMPFLT && RTC_TAFCR_TAMPTS */
  444.  
  445.   /* Disable tamper global interrupt in case it is enabled */
  446.   tmpreg &= (uint32_t)~RTC_TAFCR_TAMPIE;
  447.  
  448.   /* Copy desired configuration into configuration register */
  449.   hrtc->Instance->TAFCR = tmpreg;
  450.  
  451.   hrtc->State = HAL_RTC_STATE_READY;
  452.  
  453.   /* Process Unlocked */
  454.   __HAL_UNLOCK(hrtc);
  455.  
  456.   return HAL_OK;
  457. }
  458.  
  459. /**
  460.   * @brief  Sets Tamper with interrupt.
  461.   * @note   By calling this API the tamper global interrupt will be enabled.
  462.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  463.   *                the configuration information for RTC.
  464.   * @param  sTamper Pointer to RTC Tamper.
  465.   * @retval HAL status
  466.   */
  467. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  468. {
  469.   uint32_t tmpreg = 0U;
  470.  
  471.   /* Check the parameters */
  472.   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  473.   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  474. #if defined(RTC_TAFCR_TAMPFLT)
  475.   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  476.   assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
  477.   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  478.   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  479.   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  480. #endif /* RTC_TAFCR_TAMPFLT */
  481. #if defined(RTC_TAFCR_TAMPTS)
  482.   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  483. #endif /* RTC_TAFCR_TAMPTS */
  484.  
  485.   /* Process Locked */
  486.   __HAL_LOCK(hrtc);
  487.  
  488.   hrtc->State = HAL_RTC_STATE_BUSY;
  489.  
  490.   /* Copy control register into temporary variable */
  491.   tmpreg = hrtc->Instance->TAFCR;
  492.  
  493.   /* Enable selected tamper */
  494.   tmpreg |= (sTamper->Tamper);
  495.  
  496.   /* Configure the tamper trigger bit (this bit is just on the right of the
  497.        tamper enable bit, hence the one-time right shift before updating it) */
  498.   if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
  499.   {
  500.     /* Set the tamper trigger bit (case of falling edge or high level) */
  501.     tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
  502.   }
  503.   else
  504.   {
  505.     /* Clear the tamper trigger bit (case of rising edge or low level) */
  506.     tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
  507.   }
  508.  
  509. #if (defined(RTC_TAFCR_TAMPFLT)  && defined(RTC_TAFCR_TAMPTS) )
  510.   /* Clear remaining fields before setting them */
  511.   tmpreg &= ~(RTC_TAMPERFILTER_MASK              | \
  512.               RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
  513.               RTC_TAMPERPRECHARGEDURATION_MASK   | \
  514.               RTC_TAMPER_PULLUP_MASK             | \
  515.               RTC_TIMESTAMPONTAMPERDETECTION_MASK);
  516.  
  517.   /* Set remaining parameters of desired configuration into temporary variable */
  518.   tmpreg |= ((uint32_t)sTamper->Filter            | \
  519.              (uint32_t)sTamper->SamplingFrequency | \
  520.              (uint32_t)sTamper->PrechargeDuration | \
  521.              (uint32_t)sTamper->TamperPullUp      | \
  522.              (uint32_t)sTamper->TimeStampOnTamperDetection);
  523. #endif /* RTC_TAFCR_TAMPFLT && RTC_TAFCR_TAMPTS */
  524.  
  525.   /* Enable global tamper interrupt */
  526.   tmpreg |= (uint32_t)RTC_TAFCR_TAMPIE;
  527.  
  528.   /* Copy desired configuration into configuration register */
  529.   hrtc->Instance->TAFCR = tmpreg;
  530.  
  531.   /* RTC Tamper Interrupt Configuration: EXTI configuration */
  532.   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  533.   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  534.  
  535.   hrtc->State = HAL_RTC_STATE_READY;
  536.  
  537.   /* Process Unlocked */
  538.   __HAL_UNLOCK(hrtc);
  539.  
  540.   return HAL_OK;
  541. }
  542.  
  543. /**
  544.   * @brief  Deactivates Tamper.
  545.   * @note   The tamper global interrupt bit will remain unchanged.
  546.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  547.   *                the configuration information for RTC.
  548.   * @param  Tamper Selected tamper pin.
  549.   *          This parameter can be any combination of the following values:
  550.   *            @arg RTC_TAMPER_1:  Tamper 1
  551.   *            @arg RTC_TAMPER_2:  Tamper 2
  552.   *            @arg RTC_TAMPER_3:  Tamper 3
  553.   * @note   RTC_TAMPER_2 is not applicable to all devices.
  554.   * @note   RTC_TAMPER_3 is not applicable to all devices.
  555.   * @retval HAL status
  556.   */
  557. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  558. {
  559.   assert_param(IS_RTC_TAMPER(Tamper));
  560.  
  561.   /* Process Locked */
  562.   __HAL_LOCK(hrtc);
  563.  
  564.   hrtc->State = HAL_RTC_STATE_BUSY;
  565.  
  566.   /* Disable the selected Tamper pin */
  567.   hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
  568.  
  569.   hrtc->State = HAL_RTC_STATE_READY;
  570.  
  571.   /* Process Unlocked */
  572.   __HAL_UNLOCK(hrtc);
  573.  
  574.   return HAL_OK;
  575. }
  576.  
  577. /**
  578.   * @brief  Handles Timestamp and Tamper interrupt request.
  579.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  580.   *                the configuration information for RTC.
  581.   * @retval None
  582.   */
  583. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  584. {
  585.   /* Clear the EXTI's Flag for RTC Timestamp and Tamper */
  586.   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  587.  
  588.   /* Get the Timestamp interrupt source enable status */
  589.   if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
  590.   {
  591.     /* Get the pending status of the Timestamp Interrupt */
  592.     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
  593.     {
  594.       /* Timestamp callback */
  595. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  596.       hrtc->TimeStampEventCallback(hrtc);
  597. #else
  598.       HAL_RTCEx_TimeStampEventCallback(hrtc);
  599. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  600.  
  601.       /* Clear the Timestamp interrupt pending bit after returning from callback
  602.          as RTC_TSTR and RTC_TSDR registers are cleared when TSF bit is reset */
  603.       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  604.     }
  605.   }
  606.  
  607.   /* Get the Tamper 1 interrupt source enable status */
  608.   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
  609.   {
  610.     /* Get the pending status of the Tamper 1 Interrupt */
  611.     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
  612.     {
  613.       /* Clear the Tamper interrupt pending bit */
  614.       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  615.  
  616.       /* Tamper callback */
  617. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  618.       hrtc->Tamper1EventCallback(hrtc);
  619. #else
  620.       HAL_RTCEx_Tamper1EventCallback(hrtc);
  621. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  622.     }
  623.   }
  624.  
  625. #if defined(RTC_TAMPER2_SUPPORT)
  626.   /* Get the Tamper 2 interrupt source enable status */
  627.   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
  628.   {
  629.     /* Get the pending status of the Tamper 2 Interrupt */
  630.     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
  631.     {
  632.       /* Clear the Tamper interrupt pending bit */
  633.       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  634.  
  635.       /* Tamper callback */
  636. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  637.       hrtc->Tamper2EventCallback(hrtc);
  638. #else
  639.       HAL_RTCEx_Tamper2EventCallback(hrtc);
  640. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  641.     }
  642.   }
  643. #endif /* RTC_TAMPER2_SUPPORT */
  644.  
  645. #if defined(RTC_TAMPER3_SUPPORT)
  646.   /* Get the Tamper 3 interrupt source enable status */
  647.   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
  648.   {
  649.     /* Get the pending status of the Tamper 3 Interrupt */
  650.     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
  651.     {
  652.       /* Clear the Tamper interrupt pending bit */
  653.       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  654.  
  655.       /* Tamper callback */
  656. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  657.       hrtc->Tamper3EventCallback(hrtc);
  658. #else
  659.       HAL_RTCEx_Tamper3EventCallback(hrtc);
  660. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  661.     }
  662.   }
  663. #endif /* RTC_TAMPER3_SUPPORT */
  664.  
  665.   /* Change RTC state */
  666.   hrtc->State = HAL_RTC_STATE_READY;
  667. }
  668.  
  669. /**
  670.   * @brief  Timestamp callback.
  671.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  672.   *                the configuration information for RTC.
  673.   * @retval None
  674.   */
  675. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  676. {
  677.   /* Prevent unused argument(s) compilation warning */
  678.   UNUSED(hrtc);
  679.  
  680.   /* NOTE: This function should not be modified, when the callback is needed,
  681.            the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
  682.   */
  683. }
  684.  
  685. /**
  686.   * @brief  Tamper 1 callback.
  687.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  688.   *                the configuration information for RTC.
  689.   * @retval None
  690.   */
  691. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  692. {
  693.   /* Prevent unused argument(s) compilation warning */
  694.   UNUSED(hrtc);
  695.  
  696.   /* NOTE: This function should not be modified, when the callback is needed,
  697.            the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
  698.    */
  699. }
  700.  
  701. #if defined(RTC_TAMPER2_SUPPORT)
  702. /**
  703.   * @brief  Tamper 2 callback.
  704.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  705.   *                the configuration information for RTC.
  706.   * @retval None
  707.   */
  708. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  709. {
  710.   /* Prevent unused argument(s) compilation warning */
  711.   UNUSED(hrtc);
  712.  
  713.   /* NOTE: This function should not be modified, when the callback is needed,
  714.            the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
  715.    */
  716. }
  717. #endif /* RTC_TAMPER2_SUPPORT */
  718.  
  719. #if defined(RTC_TAMPER3_SUPPORT)
  720. /**
  721.   * @brief  Tamper 3 callback.
  722.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  723.   *                the configuration information for RTC.
  724.   * @retval None
  725.   */
  726. __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
  727. {
  728.   /* Prevent unused argument(s) compilation warning */
  729.   UNUSED(hrtc);
  730.  
  731.   /* NOTE: This function should not be modified, when the callback is needed,
  732.            the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
  733.    */
  734. }
  735. #endif /* RTC_TAMPER3_SUPPORT */
  736.  
  737. /**
  738.   * @brief  Handles Timestamp polling request.
  739.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  740.   *                the configuration information for RTC.
  741.   * @param  Timeout Timeout duration
  742.   * @retval HAL status
  743.   */
  744. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  745. {
  746.   uint32_t tickstart = 0U;
  747.  
  748.   /* Get tick */
  749.   tickstart = HAL_GetTick();
  750.  
  751.   while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
  752.   {
  753.     if (Timeout != HAL_MAX_DELAY)
  754.     {
  755.       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  756.       {
  757.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  758.         return HAL_TIMEOUT;
  759.       }
  760.     }
  761.  
  762.     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
  763.     {
  764.       /* Clear the Timestamp Overrun Flag */
  765.       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  766.  
  767.       /* Change Timestamp state */
  768.       hrtc->State = HAL_RTC_STATE_ERROR;
  769.  
  770.       return HAL_ERROR;
  771.     }
  772.   }
  773.  
  774.   /* Change RTC state */
  775.   hrtc->State = HAL_RTC_STATE_READY;
  776.  
  777.   return HAL_OK;
  778. }
  779.  
  780. /**
  781.   * @brief  Handles Tamper 1 Polling.
  782.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  783.   *                the configuration information for RTC.
  784.   * @param  Timeout Timeout duration
  785.   * @retval HAL status
  786.   */
  787. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  788. {
  789.   uint32_t tickstart = 0U;
  790.  
  791.   /* Get tick */
  792.   tickstart = HAL_GetTick();
  793.  
  794.   /* Get the status of the Interrupt */
  795.   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
  796.   {
  797.     if (Timeout != HAL_MAX_DELAY)
  798.     {
  799.       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  800.       {
  801.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  802.         return HAL_TIMEOUT;
  803.       }
  804.     }
  805.   }
  806.  
  807.   /* Clear the Tamper Flag */
  808.   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  809.  
  810.   /* Change RTC state */
  811.   hrtc->State = HAL_RTC_STATE_READY;
  812.  
  813.   return HAL_OK;
  814. }
  815.  
  816. #if defined(RTC_TAMPER2_SUPPORT)
  817. /**
  818.   * @brief  Handles Tamper 2 Polling.
  819.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  820.   *                the configuration information for RTC.
  821.   * @param  Timeout Timeout duration
  822.   * @retval HAL status
  823.   */
  824. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  825. {
  826.   uint32_t tickstart = 0U;
  827.  
  828.   /* Get tick */
  829.   tickstart = HAL_GetTick();
  830.  
  831.   /* Get the status of the Interrupt */
  832.   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
  833.   {
  834.     if (Timeout != HAL_MAX_DELAY)
  835.     {
  836.       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  837.       {
  838.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  839.         return HAL_TIMEOUT;
  840.       }
  841.     }
  842.   }
  843.  
  844.   /* Clear the Tamper Flag */
  845.   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  846.  
  847.   /* Change RTC state */
  848.   hrtc->State = HAL_RTC_STATE_READY;
  849.  
  850.   return HAL_OK;
  851. }
  852. #endif /* RTC_TAMPER2_SUPPORT */
  853.  
  854. #if defined(RTC_TAMPER3_SUPPORT)
  855. /**
  856.   * @brief  Handles Tamper 3 Polling.
  857.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  858.   *                the configuration information for RTC.
  859.   * @param  Timeout Timeout duration
  860.   * @retval HAL status
  861.   */
  862. HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  863. {
  864.   uint32_t tickstart = HAL_GetTick();
  865.  
  866.   /* Get the status of the Interrupt */
  867.   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
  868.   {
  869.     if (Timeout != HAL_MAX_DELAY)
  870.     {
  871.       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  872.       {
  873.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  874.         return HAL_TIMEOUT;
  875.       }
  876.     }
  877.   }
  878.  
  879.   /* Clear the Tamper Flag */
  880.   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  881.  
  882.   /* Change RTC state */
  883.   hrtc->State = HAL_RTC_STATE_READY;
  884.  
  885.   return HAL_OK;
  886. }
  887. #endif /* RTC_TAMPER3_SUPPORT */
  888.  
  889. /**
  890.   * @}
  891.   */
  892.  
  893. /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wakeup functions
  894.   * @brief    RTC Wakeup functions
  895.   *
  896. @verbatim
  897.  ===============================================================================
  898.                         ##### RTC Wakeup functions #####
  899.  ===============================================================================
  900.  
  901.  [..] This section provides functions allowing to configure Wakeup feature
  902.  
  903. @endverbatim
  904.   * @{
  905.   */
  906.  
  907. /**
  908.   * @brief  Sets wakeup timer.
  909.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  910.   *                the configuration information for RTC.
  911.   * @param  WakeUpCounter Wakeup counter
  912.   * @param  WakeUpClock Wakeup clock
  913.   * @retval HAL status
  914.   */
  915. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  916. {
  917.   uint32_t tickstart = 0U;
  918.  
  919.   /* Check the parameters */
  920.   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  921.   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  922.  
  923.   /* Process Locked */
  924.   __HAL_LOCK(hrtc);
  925.  
  926.   hrtc->State = HAL_RTC_STATE_BUSY;
  927.  
  928.   /* Disable the write protection for RTC registers */
  929.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  930.  
  931.   /* Check RTC WUTWF flag is reset only when wakeup timer enabled*/
  932.   if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
  933.   {
  934.     tickstart = HAL_GetTick();
  935.  
  936.     /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
  937.     while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
  938.     {
  939.       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  940.       {
  941.         /* Enable the write protection for RTC registers */
  942.         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  943.  
  944.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  945.  
  946.         /* Process Unlocked */
  947.         __HAL_UNLOCK(hrtc);
  948.  
  949.         return HAL_TIMEOUT;
  950.       }
  951.     }
  952.   }
  953.  
  954.   /* Disable the Wakeup timer */
  955.   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  956.  
  957.   /* Clear the Wakeup flag */
  958.   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  959.  
  960.   /* Get tick */
  961.   tickstart = HAL_GetTick();
  962.  
  963.   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
  964.   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
  965.   {
  966.     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  967.     {
  968.       /* Enable the write protection for RTC registers */
  969.       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  970.  
  971.       hrtc->State = HAL_RTC_STATE_TIMEOUT;
  972.  
  973.       /* Process Unlocked */
  974.       __HAL_UNLOCK(hrtc);
  975.  
  976.       return HAL_TIMEOUT;
  977.     }
  978.   }
  979.  
  980.   /* Clear the Wakeup Timer clock source bits in CR register */
  981.   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  982.  
  983.   /* Configure the clock source */
  984.   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  985.  
  986.   /* Configure the Wakeup Timer counter */
  987.   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  988.  
  989.   /* Enable the Wakeup Timer */
  990.   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  991.  
  992.   /* Enable the write protection for RTC registers */
  993.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  994.  
  995.   hrtc->State = HAL_RTC_STATE_READY;
  996.  
  997.   /* Process Unlocked */
  998.   __HAL_UNLOCK(hrtc);
  999.  
  1000.   return HAL_OK;
  1001. }
  1002.  
  1003. /**
  1004.   * @brief  Sets wakeup timer with interrupt.
  1005.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1006.   *                the configuration information for RTC.
  1007.   * @param  WakeUpCounter Wakeup counter
  1008.   * @param  WakeUpClock Wakeup clock
  1009.   * @retval HAL status
  1010.   */
  1011. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  1012. {
  1013.   __IO uint32_t count  = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
  1014.  
  1015.   /* Check the parameters */
  1016.   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  1017.   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  1018.  
  1019.   /* Process Locked */
  1020.   __HAL_LOCK(hrtc);
  1021.  
  1022.   hrtc->State = HAL_RTC_STATE_BUSY;
  1023.  
  1024.   /* Disable the write protection for RTC registers */
  1025.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1026.  
  1027.   /* Check RTC WUTWF flag is reset only when wakeup timer enabled */
  1028.   if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
  1029.   {
  1030.     /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
  1031.     do
  1032.     {
  1033.       count = count - 1U;
  1034.       if (count == 0U)
  1035.       {
  1036.         /* Enable the write protection for RTC registers */
  1037.         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1038.  
  1039.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1040.  
  1041.         /* Process Unlocked */
  1042.         __HAL_UNLOCK(hrtc);
  1043.  
  1044.         return HAL_TIMEOUT;
  1045.       }
  1046.     } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U);
  1047.   }
  1048.  
  1049.   /* Disable the Wakeup timer */
  1050.   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  1051.  
  1052.   /* Clear the Wakeup flag */
  1053.   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1054.  
  1055.   /* Reload the counter */
  1056.   count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
  1057.  
  1058.   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
  1059.   do
  1060.   {
  1061.     count = count - 1U;
  1062.     if (count == 0U)
  1063.     {
  1064.       /* Enable the write protection for RTC registers */
  1065.       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1066.  
  1067.       hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1068.  
  1069.       /* Process Unlocked */
  1070.       __HAL_UNLOCK(hrtc);
  1071.  
  1072.       return HAL_TIMEOUT;
  1073.     }
  1074.   } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U);
  1075.  
  1076.   /* Clear the Wakeup Timer clock source bits in CR register */
  1077.   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  1078.  
  1079.   /* Configure the clock source */
  1080.   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  1081.  
  1082.   /* Configure the Wakeup Timer counter */
  1083.   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  1084.  
  1085.   /* RTC wakeup timer Interrupt Configuration: EXTI configuration */
  1086.   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  1087.   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  1088.  
  1089.   /* Configure the interrupt in the RTC_CR register */
  1090.   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
  1091.  
  1092.   /* Enable the Wakeup Timer */
  1093.   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  1094.  
  1095.   /* Enable the write protection for RTC registers */
  1096.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1097.  
  1098.   hrtc->State = HAL_RTC_STATE_READY;
  1099.  
  1100.   /* Process Unlocked */
  1101.   __HAL_UNLOCK(hrtc);
  1102.  
  1103.   return HAL_OK;
  1104. }
  1105.  
  1106. /**
  1107.   * @brief  Deactivates wakeup timer counter.
  1108.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1109.   *                the configuration information for RTC.
  1110.   * @retval HAL status
  1111.   */
  1112. HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  1113. {
  1114.   uint32_t tickstart = 0U;
  1115.  
  1116.   /* Process Locked */
  1117.   __HAL_LOCK(hrtc);
  1118.  
  1119.   hrtc->State = HAL_RTC_STATE_BUSY;
  1120.  
  1121.   /* Disable the write protection for RTC registers */
  1122.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1123.  
  1124.   /* Disable the Wakeup Timer */
  1125.   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  1126.  
  1127.   /* In case of interrupt mode is used, the interrupt source must disabled */
  1128.   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
  1129.  
  1130.   /* Get tick */
  1131.   tickstart = HAL_GetTick();
  1132.  
  1133.   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
  1134.   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
  1135.   {
  1136.     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1137.     {
  1138.       /* Enable the write protection for RTC registers */
  1139.       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1140.  
  1141.       hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1142.  
  1143.       /* Process Unlocked */
  1144.       __HAL_UNLOCK(hrtc);
  1145.  
  1146.       return HAL_TIMEOUT;
  1147.     }
  1148.   }
  1149.  
  1150.   /* Enable the write protection for RTC registers */
  1151.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1152.  
  1153.   hrtc->State = HAL_RTC_STATE_READY;
  1154.  
  1155.   /* Process Unlocked */
  1156.   __HAL_UNLOCK(hrtc);
  1157.  
  1158.   return HAL_OK;
  1159. }
  1160.  
  1161. /**
  1162.   * @brief  Gets wakeup timer counter.
  1163.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1164.   *                the configuration information for RTC.
  1165.   * @retval Counter value
  1166.   */
  1167. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  1168. {
  1169.   /* Get the counter value */
  1170.   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  1171. }
  1172.  
  1173. /**
  1174.   * @brief  Handles Wakeup Timer interrupt request.
  1175.   * @note   Unlike alarm interrupt line (shared by Alarms A and B) or tamper
  1176.   *         interrupt line (shared by timestamp and tampers) wakeup timer
  1177.   *         interrupt line is exclusive to the wakeup timer.
  1178.   *         There is no need in this case to check on the interrupt enable
  1179.   *         status via __HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE().
  1180.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1181.   *                the configuration information for RTC.
  1182.   * @retval None
  1183.   */
  1184. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  1185. {
  1186.   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  1187.   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  1188.  
  1189.   /* Get the pending status of the Wakeup timer Interrupt */
  1190.   if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
  1191.   {
  1192.     /* Clear the Wakeup timer interrupt pending bit */
  1193.     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1194.  
  1195.     /* Wakeup timer callback */
  1196. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1197.     hrtc->WakeUpTimerEventCallback(hrtc);
  1198. #else
  1199.     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  1200. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1201.   }
  1202.  
  1203.   /* Change RTC state */
  1204.   hrtc->State = HAL_RTC_STATE_READY;
  1205. }
  1206.  
  1207. /**
  1208.   * @brief  Wakeup Timer callback.
  1209.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1210.   *                the configuration information for RTC.
  1211.   * @retval None
  1212.   */
  1213. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  1214. {
  1215.   /* Prevent unused argument(s) compilation warning */
  1216.   UNUSED(hrtc);
  1217.  
  1218.   /* NOTE: This function should not be modified, when the callback is needed,
  1219.            the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
  1220.    */
  1221. }
  1222.  
  1223. /**
  1224.   * @brief  Handles Wakeup Timer Polling.
  1225.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1226.   *                the configuration information for RTC.
  1227.   * @param  Timeout Timeout duration
  1228.   * @retval HAL status
  1229.   */
  1230. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1231. {
  1232.   uint32_t tickstart = 0U;
  1233.  
  1234.   /* Get tick */
  1235.   tickstart = HAL_GetTick();
  1236.  
  1237.   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
  1238.   {
  1239.     if (Timeout != HAL_MAX_DELAY)
  1240.     {
  1241.       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1242.       {
  1243.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1244.         return HAL_TIMEOUT;
  1245.       }
  1246.     }
  1247.   }
  1248.  
  1249.   /* Clear the Wakeup timer Flag */
  1250.   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1251.  
  1252.   /* Change RTC state */
  1253.   hrtc->State = HAL_RTC_STATE_READY;
  1254.  
  1255.   return HAL_OK;
  1256. }
  1257.  
  1258. /**
  1259.   * @}
  1260.   */
  1261.  
  1262. /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
  1263.   * @brief    Extended Peripheral Control functions
  1264.   *
  1265. @verbatim
  1266.  ===============================================================================
  1267.               ##### Extended Peripheral Control functions #####
  1268.  ===============================================================================
  1269.     [..]
  1270.     This subsection provides functions allowing to
  1271.       (+) Write a data in a specified RTC Backup data register
  1272.       (+) Read a data in a specified RTC Backup data register
  1273.       (+) Set the Coarse calibration parameters.
  1274.       (+) Deactivate the Coarse calibration parameters
  1275.       (+) Set the Smooth calibration parameters.
  1276.       (+) Configure the Synchronization Shift Control Settings.
  1277.       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1278.       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1279.       (+) Enable the RTC reference clock detection.
  1280.       (+) Disable the RTC reference clock detection.
  1281.       (+) Enable the Bypass Shadow feature.
  1282.       (+) Disable the Bypass Shadow feature.
  1283.  
  1284. @endverbatim
  1285.   * @{
  1286.   */
  1287.  
  1288. /**
  1289.   * @brief  Writes a data in a specified RTC Backup data register.
  1290.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1291.   *                the configuration information for RTC.
  1292.   * @param  BackupRegister RTC Backup data Register number.
  1293.   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 31)
  1294.   *                                 to specify the register.
  1295.   * @note   The range of values possible for parameter BackupRegister depends on
  1296.   *         the total number of Backup data Registers. This total number is not
  1297.   *         the same on all devices.
  1298.   * @param  Data Data to be written in the specified RTC Backup data register.
  1299.   * @retval None
  1300.   */
  1301. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  1302. {
  1303.   uint32_t tmp = 0U;
  1304.  
  1305.   /* Check the parameters */
  1306.   assert_param(IS_RTC_BKP(BackupRegister));
  1307.  
  1308.   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
  1309.   tmp += (BackupRegister * 4U);
  1310.  
  1311.   /* Write the specified register */
  1312.   *(__IO uint32_t *)tmp = (uint32_t)Data;
  1313. }
  1314.  
  1315. /**
  1316.   * @brief  Reads data from the specified RTC Backup data Register.
  1317.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1318.   *                the configuration information for RTC.
  1319.   * @param  BackupRegister RTC Backup data Register number.
  1320.   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 31)
  1321.   *                                 to specify the register.
  1322.   * @note   The range of values possible for parameter BackupRegister depends on
  1323.   *         the total number of Backup data Registers. This total number is not
  1324.   *         the same on all devices.
  1325.   * @retval Read value
  1326.   */
  1327. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  1328. {
  1329.   uint32_t tmp = 0U;
  1330.  
  1331.   /* Check the parameters */
  1332.   assert_param(IS_RTC_BKP(BackupRegister));
  1333.  
  1334.   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
  1335.   tmp += (BackupRegister * 4U);
  1336.  
  1337.   /* Read the specified register */
  1338.   return (*(__IO uint32_t *)tmp);
  1339. }
  1340.  
  1341. /**
  1342.   * @brief  Sets the Coarse calibration parameters.
  1343.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1344.   *                the configuration information for RTC.
  1345.   * @param  CalibSign Specifies the sign of the coarse calibration value.
  1346.   *          This parameter can be  one of the following values:
  1347.   *             @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
  1348.   *             @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
  1349.   * @param  Value value of coarse calibration expressed in ppm (coded on 5 bits).
  1350.   *
  1351.   * @note   This Calibration value should be between 0 and 63 when using negative
  1352.   *         sign with a 2-ppm step.
  1353.   *
  1354.   * @note   This Calibration value should be between 0 and 126 when using positive
  1355.   *         sign with a 4-ppm step.
  1356.   * @retval HAL status
  1357.   */
  1358. HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef *hrtc, uint32_t CalibSign, uint32_t Value)
  1359. {
  1360.   HAL_StatusTypeDef status;
  1361.  
  1362.   /* Check the parameters */
  1363.   assert_param(IS_RTC_CALIB_SIGN(CalibSign));
  1364.   assert_param(IS_RTC_CALIB_VALUE(Value));
  1365.  
  1366.   /* Process Locked */
  1367.   __HAL_LOCK(hrtc);
  1368.  
  1369.   hrtc->State = HAL_RTC_STATE_BUSY;
  1370.  
  1371.   /* Disable the write protection for RTC registers */
  1372.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1373.  
  1374.   /* Enter Initialization mode */
  1375.   status = RTC_EnterInitMode(hrtc);
  1376.  
  1377.   if (status == HAL_OK)
  1378.   {
  1379.     /* Enable the Coarse Calibration */
  1380.     __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
  1381.  
  1382.     /* Set the coarse calibration value */
  1383.     hrtc->Instance->CALIBR = (uint32_t)(CalibSign | Value);
  1384.  
  1385.     /* Exit Initialization mode */
  1386.     status = RTC_ExitInitMode(hrtc);
  1387.   }
  1388.  
  1389.   if (status == HAL_OK)
  1390.   {
  1391.     hrtc->State = HAL_RTC_STATE_READY;
  1392.   }
  1393.  
  1394.   /* Enable the write protection for RTC registers */
  1395.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1396.  
  1397.   /* Process Unlocked */
  1398.   __HAL_UNLOCK(hrtc);
  1399.  
  1400.   return status;
  1401. }
  1402.  
  1403. /**
  1404.   * @brief  Deactivates the Coarse calibration parameters.
  1405.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1406.   *                the configuration information for RTC.
  1407.   * @retval HAL status
  1408.   */
  1409. HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef *hrtc)
  1410. {
  1411.   HAL_StatusTypeDef status;
  1412.  
  1413.   /* Process Locked */
  1414.   __HAL_LOCK(hrtc);
  1415.  
  1416.   hrtc->State = HAL_RTC_STATE_BUSY;
  1417.  
  1418.   /* Disable the write protection for RTC registers */
  1419.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1420.  
  1421.   /* Enter Initialization mode */
  1422.   status = RTC_EnterInitMode(hrtc);
  1423.  
  1424.   if (status == HAL_OK)
  1425.   {
  1426.     /* Disable the Coarse Calibration */
  1427.     __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
  1428.  
  1429.     /* Exit Initialization mode */
  1430.     status = RTC_ExitInitMode(hrtc);
  1431.   }
  1432.  
  1433.   if (status == HAL_OK)
  1434.   {
  1435.     hrtc->State = HAL_RTC_STATE_READY;
  1436.   }
  1437.  
  1438.   /* Enable the write protection for RTC registers */
  1439.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1440.  
  1441.   /* Process Unlocked */
  1442.   __HAL_UNLOCK(hrtc);
  1443.  
  1444.   return status;
  1445. }
  1446.  
  1447. #if defined(RTC_SMOOTHCALIB_SUPPORT)
  1448. /**
  1449.   * @brief  Sets the Smooth calibration parameters.
  1450.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1451.   *                the configuration information for RTC.
  1452.   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
  1453.   *          This parameter can be can be one of the following values:
  1454.   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  1455.   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  1456.   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  1457.   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  1458.   *          This parameter can be one of the following values:
  1459.   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  1460.   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  1461.   * @param  SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
  1462.   *          This parameter can be one any value from 0 to 0x000001FF.
  1463.   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  1464.   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  1465.   *         SmoothCalibMinusPulsesValue must be equal to 0.
  1466.   * @retval HAL status
  1467.   */
  1468. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
  1469. {
  1470.   uint32_t tickstart = 0U;
  1471.  
  1472.   /* Check the parameters */
  1473.   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  1474.   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  1475.   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
  1476.  
  1477.   /* Process Locked */
  1478.   __HAL_LOCK(hrtc);
  1479.  
  1480.   hrtc->State = HAL_RTC_STATE_BUSY;
  1481.  
  1482.   /* Disable the write protection for RTC registers */
  1483.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1484.  
  1485.   /* check if a calibration is pending*/
  1486.   if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
  1487.   {
  1488.     /* Get tick */
  1489.     tickstart = HAL_GetTick();
  1490.  
  1491.     /* check if a calibration is pending*/
  1492.     while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
  1493.     {
  1494.       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1495.       {
  1496.         /* Enable the write protection for RTC registers */
  1497.         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1498.  
  1499.         /* Change RTC state */
  1500.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1501.  
  1502.         /* Process Unlocked */
  1503.         __HAL_UNLOCK(hrtc);
  1504.  
  1505.         return HAL_TIMEOUT;
  1506.       }
  1507.     }
  1508.   }
  1509.  
  1510.   /* Configure the Smooth calibration settings */
  1511.   hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod     | \
  1512.                                     (uint32_t)SmoothCalibPlusPulses | \
  1513.                                     (uint32_t)SmoothCalibMinusPulsesValue);
  1514.  
  1515.   /* Enable the write protection for RTC registers */
  1516.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1517.  
  1518.   /* Change RTC state */
  1519.   hrtc->State = HAL_RTC_STATE_READY;
  1520.  
  1521.   /* Process Unlocked */
  1522.   __HAL_UNLOCK(hrtc);
  1523.  
  1524.   return HAL_OK;
  1525. }
  1526. #endif /* RTC_SMOOTHCALIB_SUPPORT */
  1527.  
  1528. #if defined(RTC_SUBSECOND_SUPPORT)
  1529. /**
  1530.   * @brief  Configures the Synchronization Shift Control Settings.
  1531.   * @note   When REFCKON is set, firmware must not write to Shift control register.
  1532.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1533.   *                the configuration information for RTC.
  1534.   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
  1535.   *          This parameter can be one of the following values:
  1536.   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  1537.   *             @arg RTC_SHIFTADD1S_RESET: No effect.
  1538.   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
  1539.   *          This parameter can be one any value from 0 to 0x7FFF.
  1540.   * @retval HAL status
  1541.   */
  1542. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  1543. {
  1544.   uint32_t tickstart = 0U;
  1545.  
  1546.   /* Check the parameters */
  1547.   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  1548.   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  1549.  
  1550.   /* Process Locked */
  1551.   __HAL_LOCK(hrtc);
  1552.  
  1553.   hrtc->State = HAL_RTC_STATE_BUSY;
  1554.  
  1555.   /* Disable the write protection for RTC registers */
  1556.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1557.  
  1558.   /* Get tick */
  1559.   tickstart = HAL_GetTick();
  1560.  
  1561.   /* Wait until the shift is completed */
  1562.   while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
  1563.   {
  1564.     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1565.     {
  1566.       /* Enable the write protection for RTC registers */
  1567.       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1568.  
  1569.       hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1570.  
  1571.       /* Process Unlocked */
  1572.       __HAL_UNLOCK(hrtc);
  1573.  
  1574.       return HAL_TIMEOUT;
  1575.     }
  1576.   }
  1577.  
  1578.   /* Check if the reference clock detection is disabled */
  1579.   if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
  1580.   {
  1581.     /* Configure the Shift settings */
  1582.     hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  1583.  
  1584.     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  1585.     if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
  1586.     {
  1587.       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1588.       {
  1589.         /* Enable the write protection for RTC registers */
  1590.         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1591.  
  1592.         hrtc->State = HAL_RTC_STATE_ERROR;
  1593.  
  1594.         /* Process Unlocked */
  1595.         __HAL_UNLOCK(hrtc);
  1596.  
  1597.         return HAL_ERROR;
  1598.       }
  1599.     }
  1600.   }
  1601.   else
  1602.   {
  1603.     /* Enable the write protection for RTC registers */
  1604.     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1605.  
  1606.     /* Change RTC state */
  1607.     hrtc->State = HAL_RTC_STATE_ERROR;
  1608.  
  1609.     /* Process Unlocked */
  1610.     __HAL_UNLOCK(hrtc);
  1611.  
  1612.     return HAL_ERROR;
  1613.   }
  1614.  
  1615.   /* Enable the write protection for RTC registers */
  1616.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1617.  
  1618.   /* Change RTC state */
  1619.   hrtc->State = HAL_RTC_STATE_READY;
  1620.  
  1621.   /* Process Unlocked */
  1622.   __HAL_UNLOCK(hrtc);
  1623.  
  1624.   return HAL_OK;
  1625. }
  1626. #endif /* RTC_SUBSECOND_SUPPORT */
  1627.  
  1628. /**
  1629.   * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1630.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1631.   *                the configuration information for RTC.
  1632.   * @param  CalibOutput Select the Calibration output Selection.
  1633.   *          This parameter can be one of the following values:
  1634.   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  1635.   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  1636.   * @note   Calibration output Selection is not applicable to all devices.
  1637.   * @retval HAL status
  1638.   */
  1639. #if defined(RTC_CR_COSEL)
  1640. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
  1641. {
  1642.   /* Check the parameters */
  1643.   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  1644.  
  1645. #else /* RTC_CR_COSEL */
  1646. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc)
  1647. {
  1648. #endif /* RTC_CR_COSEL */
  1649.   /* Process Locked */
  1650.   __HAL_LOCK(hrtc);
  1651.  
  1652.   hrtc->State = HAL_RTC_STATE_BUSY;
  1653.  
  1654.   /* Disable the write protection for RTC registers */
  1655.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1656.  
  1657. #if defined(RTC_CR_COSEL)
  1658.   /* Clear flags before config */
  1659.   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  1660.  
  1661.   /* Configure the RTC_CR register */
  1662.   hrtc->Instance->CR |= (uint32_t)CalibOutput;
  1663. #endif /* RTC_CR_COSEL */
  1664.  
  1665.   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  1666.  
  1667.   /* Enable the write protection for RTC registers */
  1668.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1669.  
  1670.   /* Change RTC state */
  1671.   hrtc->State = HAL_RTC_STATE_READY;
  1672.  
  1673.   /* Process Unlocked */
  1674.   __HAL_UNLOCK(hrtc);
  1675.  
  1676.   return HAL_OK;
  1677. }
  1678.  
  1679. /**
  1680.   * @brief  Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1681.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1682.   *                the configuration information for RTC.
  1683.   * @retval HAL status
  1684.   */
  1685. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
  1686. {
  1687.   /* Process Locked */
  1688.   __HAL_LOCK(hrtc);
  1689.  
  1690.   hrtc->State = HAL_RTC_STATE_BUSY;
  1691.  
  1692.   /* Disable the write protection for RTC registers */
  1693.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1694.  
  1695.   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  1696.  
  1697.   /* Enable the write protection for RTC registers */
  1698.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1699.  
  1700.   /* Change RTC state */
  1701.   hrtc->State = HAL_RTC_STATE_READY;
  1702.  
  1703.   /* Process Unlocked */
  1704.   __HAL_UNLOCK(hrtc);
  1705.  
  1706.   return HAL_OK;
  1707. }
  1708.  
  1709. /**
  1710.   * @brief  Enables the RTC reference clock detection.
  1711.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1712.   *                the configuration information for RTC.
  1713.   * @retval HAL status
  1714.   */
  1715. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
  1716. {
  1717.   HAL_StatusTypeDef status;
  1718.  
  1719.   /* Process Locked */
  1720.   __HAL_LOCK(hrtc);
  1721.  
  1722.   hrtc->State = HAL_RTC_STATE_BUSY;
  1723.  
  1724.   /* Disable the write protection for RTC registers */
  1725.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1726.  
  1727.   /* Enter Initialization mode */
  1728.   status = RTC_EnterInitMode(hrtc);
  1729.  
  1730.   if (status == HAL_OK)
  1731.   {
  1732.     /* Enable the reference clock detection */
  1733.     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  1734.  
  1735.     /* Exit Initialization mode */
  1736.     status = RTC_ExitInitMode(hrtc);
  1737.   }
  1738.  
  1739.   if (status == HAL_OK)
  1740.   {
  1741.     hrtc->State = HAL_RTC_STATE_READY;
  1742.   }
  1743.  
  1744.   /* Enable the write protection for RTC registers */
  1745.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1746.  
  1747.   /* Process Unlocked */
  1748.   __HAL_UNLOCK(hrtc);
  1749.  
  1750.   return status;
  1751. }
  1752.  
  1753. /**
  1754.   * @brief  Disable the RTC reference clock detection.
  1755.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1756.   *                the configuration information for RTC.
  1757.   * @retval HAL status
  1758.   */
  1759. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
  1760. {
  1761.   HAL_StatusTypeDef status;
  1762.  
  1763.   /* Process Locked */
  1764.   __HAL_LOCK(hrtc);
  1765.  
  1766.   hrtc->State = HAL_RTC_STATE_BUSY;
  1767.  
  1768.   /* Disable the write protection for RTC registers */
  1769.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1770.  
  1771.   /* Enter Initialization mode */
  1772.   status = RTC_EnterInitMode(hrtc);
  1773.  
  1774.   if (status == HAL_OK)
  1775.   {
  1776.     /* Disable the reference clock detection */
  1777.     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  1778.  
  1779.     /* Exit Initialization mode */
  1780.     status = RTC_ExitInitMode(hrtc);
  1781.   }
  1782.  
  1783.   if (status == HAL_OK)
  1784.   {
  1785.     hrtc->State = HAL_RTC_STATE_READY;
  1786.   }
  1787.  
  1788.   /* Enable the write protection for RTC registers */
  1789.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1790.  
  1791.   /* Process Unlocked */
  1792.   __HAL_UNLOCK(hrtc);
  1793.  
  1794.   return status;
  1795. }
  1796.  
  1797. #if defined(RTC_CR_BYPSHAD)
  1798. /**
  1799.   * @brief  Enables the Bypass Shadow feature.
  1800.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1801.   *                the configuration information for RTC.
  1802.   * @note   When the Bypass Shadow is enabled the calendar value are taken
  1803.   *         directly from the Calendar counter.
  1804.   * @retval HAL status
  1805.   */
  1806. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
  1807. {
  1808.   /* Process Locked */
  1809.   __HAL_LOCK(hrtc);
  1810.  
  1811.   hrtc->State = HAL_RTC_STATE_BUSY;
  1812.  
  1813.   /* Disable the write protection for RTC registers */
  1814.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1815.  
  1816.   /* Set the BYPSHAD bit */
  1817.   hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
  1818.  
  1819.   /* Enable the write protection for RTC registers */
  1820.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1821.  
  1822.   /* Change RTC state */
  1823.   hrtc->State = HAL_RTC_STATE_READY;
  1824.  
  1825.   /* Process Unlocked */
  1826.   __HAL_UNLOCK(hrtc);
  1827.  
  1828.   return HAL_OK;
  1829. }
  1830.  
  1831. /**
  1832.   * @brief  Disables the Bypass Shadow feature.
  1833.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1834.   *                the configuration information for RTC.
  1835.   * @note   When the Bypass Shadow is enabled the calendar value are taken
  1836.   *         directly from the Calendar counter.
  1837.   * @retval HAL status
  1838.   */
  1839. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
  1840. {
  1841.   /* Process Locked */
  1842.   __HAL_LOCK(hrtc);
  1843.  
  1844.   hrtc->State = HAL_RTC_STATE_BUSY;
  1845.  
  1846.   /* Disable the write protection for RTC registers */
  1847.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1848.  
  1849.   /* Reset the BYPSHAD bit */
  1850.   hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
  1851.  
  1852.   /* Enable the write protection for RTC registers */
  1853.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1854.  
  1855.   /* Change RTC state */
  1856.   hrtc->State = HAL_RTC_STATE_READY;
  1857.  
  1858.   /* Process Unlocked */
  1859.   __HAL_UNLOCK(hrtc);
  1860.  
  1861.   return HAL_OK;
  1862. }
  1863. #endif /* RTC_CR_BYPSHAD */
  1864.  
  1865. /**
  1866.   * @}
  1867.   */
  1868.  
  1869. /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
  1870.   * @brief    Extended features functions
  1871.   *
  1872. @verbatim
  1873.  ===============================================================================
  1874.                  ##### Extended features functions #####
  1875.  ===============================================================================
  1876.     [..]  This section provides functions allowing to:
  1877.       (+) RTC Alarm B callback
  1878.       (+) RTC Poll for Alarm B request
  1879.  
  1880. @endverbatim
  1881.   * @{
  1882.   */
  1883.  
  1884. /**
  1885.   * @brief  Alarm B callback.
  1886.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1887.   *                the configuration information for RTC.
  1888.   * @retval None
  1889.   */
  1890. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
  1891. {
  1892.   /* Prevent unused argument(s) compilation warning */
  1893.   UNUSED(hrtc);
  1894.  
  1895.   /* NOTE: This function should not be modified, when the callback is needed,
  1896.            the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
  1897.    */
  1898. }
  1899.  
  1900. /**
  1901.   * @brief  Handles Alarm B Polling request.
  1902.   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  1903.   *                the configuration information for RTC.
  1904.   * @param  Timeout Timeout duration
  1905.   * @retval HAL status
  1906.   */
  1907. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1908. {
  1909.   uint32_t tickstart = 0U;
  1910.  
  1911.   /* Get tick */
  1912.   tickstart = HAL_GetTick();
  1913.  
  1914.   /* Wait till RTC ALRBF flag is set and if timeout is reached exit */
  1915.   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
  1916.   {
  1917.     if (Timeout != HAL_MAX_DELAY)
  1918.     {
  1919.       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1920.       {
  1921.         hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1922.         return HAL_TIMEOUT;
  1923.       }
  1924.     }
  1925.   }
  1926.  
  1927.   /* Clear the Alarm flag */
  1928.   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1929.  
  1930.   /* Change RTC state */
  1931.   hrtc->State = HAL_RTC_STATE_READY;
  1932.  
  1933.   return HAL_OK;
  1934. }
  1935.  
  1936. /**
  1937.   * @}
  1938.   */
  1939.  
  1940. /**
  1941.   * @}
  1942.   */
  1943.  
  1944. #endif /* HAL_RTC_MODULE_ENABLED */
  1945. /**
  1946.   * @}
  1947.   */
  1948.  
  1949. /**
  1950.   * @}
  1951.   */
  1952.