STM32F103xB HAL User Manual
stm32f1xx_hal_rtc_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_hal_rtc_ex.c
00004   * @author  MCD Application Team
00005   * @brief   Extended RTC HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Real Time Clock (RTC) Extension peripheral:
00008   *           + RTC Tamper functions
00009   *           + Extension Control functions
00010   *           + Extension RTC features functions
00011   *
00012   ******************************************************************************
00013   * @attention
00014   *
00015   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00016   * All rights reserved.</center></h2>
00017   *
00018   * This software component is licensed by ST under BSD 3-Clause license,
00019   * the "License"; You may not use this file except in compliance with the
00020   * License. You may obtain a copy of the License at:
00021   *                        opensource.org/licenses/BSD-3-Clause
00022   *
00023   ******************************************************************************
00024   */
00025 
00026 /* Includes ------------------------------------------------------------------*/
00027 #include "stm32f1xx_hal.h"
00028 
00029 /** @addtogroup STM32F1xx_HAL_Driver
00030   * @{
00031   */
00032 
00033 #ifdef HAL_RTC_MODULE_ENABLED
00034 
00035 /** @defgroup RTCEx RTCEx
00036   * @brief RTC Extended HAL module driver
00037   * @{
00038   */
00039 
00040 /* Private typedef -----------------------------------------------------------*/
00041 /* Private define ------------------------------------------------------------*/
00042 /* Private macro -------------------------------------------------------------*/
00043 /** @defgroup RTCEx_Private_Macros RTCEx Private Macros
00044   * @{
00045   */
00046 /**
00047   * @}
00048   */
00049 
00050 /* Private variables ---------------------------------------------------------*/
00051 /* Private function prototypes -----------------------------------------------*/
00052 /* Private functions ---------------------------------------------------------*/
00053 
00054 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
00055   * @{
00056   */
00057 
00058 /** @defgroup RTCEx_Exported_Functions_Group1 RTC Tamper functions
00059   * @brief    RTC Tamper functions
00060   *
00061 @verbatim
00062  ===============================================================================
00063                  ##### RTC Tamper functions #####
00064  ===============================================================================
00065 
00066  [..] This section provides functions allowing to configure Tamper feature
00067 
00068 @endverbatim
00069   * @{
00070   */
00071 
00072 /**
00073   * @brief  Sets Tamper
00074   * @note   By calling this API we disable the tamper interrupt for all tampers.
00075   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00076   *                the configuration information for RTC.
00077   * @param  sTamper: Pointer to Tamper Structure.
00078   * @note   Tamper can be enabled only if ASOE and CCO bit are reset
00079   * @retval HAL status
00080   */
00081 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
00082 {
00083   /* Check input parameters */
00084   if ((hrtc == NULL) || (sTamper == NULL))
00085   {
00086     return HAL_ERROR;
00087   }
00088 
00089   /* Check the parameters */
00090   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
00091   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
00092 
00093   /* Process Locked */
00094   __HAL_LOCK(hrtc);
00095 
00096   hrtc->State = HAL_RTC_STATE_BUSY;
00097 
00098   if (HAL_IS_BIT_SET(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
00099   {
00100     hrtc->State = HAL_RTC_STATE_ERROR;
00101 
00102     /* Process Unlocked */
00103     __HAL_UNLOCK(hrtc);
00104 
00105     return HAL_ERROR;
00106   }
00107 
00108   MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
00109 
00110   hrtc->State = HAL_RTC_STATE_READY;
00111 
00112   /* Process Unlocked */
00113   __HAL_UNLOCK(hrtc);
00114 
00115   return HAL_OK;
00116 }
00117 
00118 /**
00119   * @brief  Sets Tamper with interrupt.
00120   * @note   By calling this API we force the tamper interrupt for all tampers.
00121   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00122   *                the configuration information for RTC.
00123   * @param  sTamper: Pointer to RTC Tamper.
00124   * @note   Tamper can be enabled only if ASOE and CCO bit are reset
00125   * @retval HAL status
00126   */
00127 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
00128 {
00129   /* Check input parameters */
00130   if ((hrtc == NULL) || (sTamper == NULL))
00131   {
00132     return HAL_ERROR;
00133   }
00134 
00135   /* Check the parameters */
00136   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
00137   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
00138 
00139   /* Process Locked */
00140   __HAL_LOCK(hrtc);
00141 
00142   hrtc->State = HAL_RTC_STATE_BUSY;
00143 
00144   if (HAL_IS_BIT_SET(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
00145   {
00146     hrtc->State = HAL_RTC_STATE_ERROR;
00147 
00148     /* Process Unlocked */
00149     __HAL_UNLOCK(hrtc);
00150 
00151     return HAL_ERROR;
00152   }
00153 
00154   MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
00155 
00156   /* Configure the Tamper Interrupt in the BKP->CSR */
00157   __HAL_RTC_TAMPER_ENABLE_IT(hrtc, RTC_IT_TAMP1);
00158 
00159   hrtc->State = HAL_RTC_STATE_READY;
00160 
00161   /* Process Unlocked */
00162   __HAL_UNLOCK(hrtc);
00163 
00164   return HAL_OK;
00165 }
00166 
00167 /**
00168   * @brief  Deactivates Tamper.
00169   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00170   *                the configuration information for RTC.
00171   * @param  Tamper: Selected tamper pin.
00172   *          This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
00173   * @retval HAL status
00174   */
00175 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
00176 {
00177   /* Check input parameters */
00178   if (hrtc == NULL)
00179   {
00180     return HAL_ERROR;
00181   }
00182   /* Prevent unused argument(s) compilation warning */
00183   UNUSED(Tamper);
00184 
00185   assert_param(IS_RTC_TAMPER(Tamper));
00186 
00187   /* Process Locked */
00188   __HAL_LOCK(hrtc);
00189 
00190   hrtc->State = HAL_RTC_STATE_BUSY;
00191 
00192   /* Disable the selected Tamper pin */
00193   CLEAR_BIT(BKP->CR, BKP_CR_TPE);
00194 
00195   /* Disable the Tamper Interrupt in the BKP->CSR */
00196   /* Configure the Tamper Interrupt in the BKP->CSR */
00197   __HAL_RTC_TAMPER_DISABLE_IT(hrtc, RTC_IT_TAMP1);
00198 
00199   /* Clear the Tamper interrupt pending bit */
00200   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
00201   SET_BIT(BKP->CSR, BKP_CSR_CTE);
00202 
00203   hrtc->State = HAL_RTC_STATE_READY;
00204 
00205   /* Process Unlocked */
00206   __HAL_UNLOCK(hrtc);
00207 
00208   return HAL_OK;
00209 }
00210 
00211 /**
00212   * @brief  This function handles Tamper interrupt request.
00213   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00214   *                the configuration information for RTC.
00215   * @retval None
00216   */
00217 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
00218 {
00219   /* Get the status of the Interrupt */
00220   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP1))
00221   {
00222     /* Get the TAMPER Interrupt enable bit and pending bit */
00223     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != (uint32_t)RESET)
00224     {
00225       /* Tamper callback */
00226 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00227       hrtc->Tamper1EventCallback(hrtc);
00228 #else
00229       HAL_RTCEx_Tamper1EventCallback(hrtc);
00230 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
00231 
00232       /* Clear the Tamper interrupt pending bit */
00233       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
00234     }
00235   }
00236 
00237   /* Change RTC state */
00238   hrtc->State = HAL_RTC_STATE_READY;
00239 }
00240 
00241 /**
00242   * @brief  Tamper 1 callback.
00243   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00244   *                the configuration information for RTC.
00245   * @retval None
00246   */
00247 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
00248 {
00249   /* Prevent unused argument(s) compilation warning */
00250   UNUSED(hrtc);
00251   /* NOTE : This function Should not be modified, when the callback is needed,
00252             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
00253    */
00254 }
00255 
00256 /**
00257   * @brief  This function handles Tamper1 Polling.
00258   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00259   *                the configuration information for RTC.
00260   * @param  Timeout: Timeout duration
00261   * @retval HAL status
00262   */
00263 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
00264 {
00265   uint32_t tickstart = HAL_GetTick();
00266 
00267   /* Check input parameters */
00268   if (hrtc == NULL)
00269   {
00270     return HAL_ERROR;
00271   }
00272 
00273   /* Get the status of the Interrupt */
00274   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == RESET)
00275   {
00276     if (Timeout != HAL_MAX_DELAY)
00277     {
00278       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
00279       {
00280         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00281         return HAL_TIMEOUT;
00282       }
00283     }
00284   }
00285 
00286   /* Clear the Tamper Flag */
00287   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
00288 
00289   /* Change RTC state */
00290   hrtc->State = HAL_RTC_STATE_READY;
00291 
00292   return HAL_OK;
00293 }
00294 
00295 /**
00296   * @}
00297   */
00298 
00299 /** @defgroup RTCEx_Exported_Functions_Group2 RTC Second functions
00300   * @brief    RTC Second functions
00301   *
00302 @verbatim
00303  ===============================================================================
00304                  ##### RTC Second functions #####
00305  ===============================================================================
00306 
00307  [..] This section provides functions implementing second interupt handlers
00308 
00309 @endverbatim
00310   * @{
00311   */
00312 
00313 /**
00314   * @brief  Sets Interrupt for second
00315   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00316   *                the configuration information for RTC.
00317   * @retval HAL status
00318   */
00319 HAL_StatusTypeDef HAL_RTCEx_SetSecond_IT(RTC_HandleTypeDef *hrtc)
00320 {
00321   /* Check input parameters */
00322   if (hrtc == NULL)
00323   {
00324     return HAL_ERROR;
00325   }
00326 
00327   /* Process Locked */
00328   __HAL_LOCK(hrtc);
00329 
00330   hrtc->State = HAL_RTC_STATE_BUSY;
00331 
00332   /* Enable Second interuption */
00333   __HAL_RTC_SECOND_ENABLE_IT(hrtc, RTC_IT_SEC);
00334 
00335   hrtc->State = HAL_RTC_STATE_READY;
00336 
00337   /* Process Unlocked */
00338   __HAL_UNLOCK(hrtc);
00339 
00340   return HAL_OK;
00341 }
00342 
00343 /**
00344   * @brief  Deactivates Second.
00345   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00346   *                the configuration information for RTC.
00347   * @retval HAL status
00348   */
00349 HAL_StatusTypeDef HAL_RTCEx_DeactivateSecond(RTC_HandleTypeDef *hrtc)
00350 {
00351   /* Check input parameters */
00352   if (hrtc == NULL)
00353   {
00354     return HAL_ERROR;
00355   }
00356 
00357   /* Process Locked */
00358   __HAL_LOCK(hrtc);
00359 
00360   hrtc->State = HAL_RTC_STATE_BUSY;
00361 
00362   /* Deactivate Second interuption*/
00363   __HAL_RTC_SECOND_DISABLE_IT(hrtc, RTC_IT_SEC);
00364 
00365   hrtc->State = HAL_RTC_STATE_READY;
00366 
00367   /* Process Unlocked */
00368   __HAL_UNLOCK(hrtc);
00369 
00370   return HAL_OK;
00371 }
00372 
00373 /**
00374   * @brief  This function handles second interrupt request.
00375   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00376   *                the configuration information for RTC.
00377   * @retval None
00378   */
00379 void HAL_RTCEx_RTCIRQHandler(RTC_HandleTypeDef *hrtc)
00380 {
00381   if (__HAL_RTC_SECOND_GET_IT_SOURCE(hrtc, RTC_IT_SEC))
00382   {
00383     /* Get the status of the Interrupt */
00384     if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_SEC))
00385     {
00386       /* Check if Overrun occurred */
00387       if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_OW))
00388       {
00389         /* Second error callback */
00390         HAL_RTCEx_RTCEventErrorCallback(hrtc);
00391 
00392         /* Clear flag Second */
00393         __HAL_RTC_OVERFLOW_CLEAR_FLAG(hrtc, RTC_FLAG_OW);
00394 
00395         /* Change RTC state */
00396         hrtc->State = HAL_RTC_STATE_ERROR;
00397       }
00398       else
00399       {
00400         /* Second callback */
00401         HAL_RTCEx_RTCEventCallback(hrtc);
00402 
00403         /* Change RTC state */
00404         hrtc->State = HAL_RTC_STATE_READY;
00405       }
00406 
00407       /* Clear flag Second */
00408       __HAL_RTC_SECOND_CLEAR_FLAG(hrtc, RTC_FLAG_SEC);
00409     }
00410   }
00411 }
00412 
00413 /**
00414   * @brief  Second event callback.
00415   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00416   *                the configuration information for RTC.
00417   * @retval None
00418   */
00419 __weak void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc)
00420 {
00421   /* Prevent unused argument(s) compilation warning */
00422   UNUSED(hrtc);
00423   /* NOTE : This function Should not be modified, when the callback is needed,
00424             the HAL_RTCEx_RTCEventCallback could be implemented in the user file
00425    */
00426 }
00427 
00428 /**
00429   * @brief  Second event error callback.
00430   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00431   *                the configuration information for RTC.
00432   * @retval None
00433   */
00434 __weak void HAL_RTCEx_RTCEventErrorCallback(RTC_HandleTypeDef *hrtc)
00435 {
00436   /* Prevent unused argument(s) compilation warning */
00437   UNUSED(hrtc);
00438   /* NOTE : This function Should not be modified, when the callback is needed,
00439             the HAL_RTCEx_RTCEventErrorCallback could be implemented in the user file
00440    */
00441 }
00442 
00443 /**
00444   * @}
00445   */
00446 
00447 /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
00448   * @brief    Extended Peripheral Control functions
00449   *
00450 @verbatim
00451  ===============================================================================
00452               ##### Extension Peripheral Control functions #####
00453  ===============================================================================
00454     [..]
00455     This subsection provides functions allowing to
00456       (+) Writes a data in a specified RTC Backup data register
00457       (+) Read a data in a specified RTC Backup data register
00458       (+) Sets the Smooth calibration parameters.
00459 
00460 @endverbatim
00461   * @{
00462   */
00463 
00464 /**
00465   * @brief  Writes a data in a specified RTC Backup data register.
00466   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00467   *                the configuration information for RTC.
00468   * @param  BackupRegister: RTC Backup data Register number.
00469   *          This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
00470   *                                 specify the register (depending devices).
00471   * @param  Data: Data to be written in the specified RTC Backup data register.
00472   * @retval None
00473   */
00474 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
00475 {
00476   uint32_t tmp = 0U;
00477 
00478   /* Prevent unused argument(s) compilation warning */
00479   UNUSED(hrtc);
00480 
00481   /* Check the parameters */
00482   assert_param(IS_RTC_BKP(BackupRegister));
00483 
00484   tmp = (uint32_t)BKP_BASE;
00485   tmp += (BackupRegister * 4U);
00486 
00487   *(__IO uint32_t *) tmp = (Data & BKP_DR1_D);
00488 }
00489 
00490 /**
00491   * @brief  Reads data from the specified RTC Backup data Register.
00492   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
00493   *                the configuration information for RTC.
00494   * @param  BackupRegister: RTC Backup data Register number.
00495   *          This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
00496   *                                 specify the register (depending devices).
00497   * @retval Read value
00498   */
00499 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
00500 {
00501   uint32_t backupregister = 0U;
00502   uint32_t pvalue = 0U;
00503 
00504   /* Prevent unused argument(s) compilation warning */
00505   UNUSED(hrtc);
00506 
00507   /* Check the parameters */
00508   assert_param(IS_RTC_BKP(BackupRegister));
00509 
00510   backupregister = (uint32_t)BKP_BASE;
00511   backupregister += (BackupRegister * 4U);
00512 
00513   pvalue = (*(__IO uint32_t *)(backupregister)) & BKP_DR1_D;
00514 
00515   /* Read the specified register */
00516   return pvalue;
00517 }
00518 
00519 
00520 /**
00521   * @brief  Sets the Smooth calibration parameters.
00522   * @param  hrtc: RTC handle
00523   * @param  SmoothCalibPeriod: Not used (only present for compatibility with another families)
00524   * @param  SmoothCalibPlusPulses: Not used (only present for compatibility with another families)
00525   * @param  SmouthCalibMinusPulsesValue: specifies the RTC Clock Calibration value.
00526   *          This parameter must be a number between 0 and 0x7F.
00527   * @retval HAL status
00528   */
00529 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
00530 {
00531   /* Check input parameters */
00532   if (hrtc == NULL)
00533   {
00534     return HAL_ERROR;
00535   }
00536   /* Prevent unused argument(s) compilation warning */
00537   UNUSED(SmoothCalibPeriod);
00538   UNUSED(SmoothCalibPlusPulses);
00539 
00540   /* Check the parameters */
00541   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
00542 
00543   /* Process Locked */
00544   __HAL_LOCK(hrtc);
00545 
00546   hrtc->State = HAL_RTC_STATE_BUSY;
00547 
00548   /* Sets RTC Clock Calibration value.*/
00549   MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CAL, SmouthCalibMinusPulsesValue);
00550 
00551   /* Change RTC state */
00552   hrtc->State = HAL_RTC_STATE_READY;
00553 
00554   /* Process Unlocked */
00555   __HAL_UNLOCK(hrtc);
00556 
00557   return HAL_OK;
00558 }
00559 
00560 /**
00561   * @}
00562   */
00563 
00564 /**
00565   * @}
00566   */
00567 
00568 /**
00569   * @}
00570   */
00571 
00572 #endif /* HAL_RTC_MODULE_ENABLED */
00573 
00574 /**
00575   * @}
00576   */
00577 
00578 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00579