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