stm32f10x_rtc.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f10x_rtc.c
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   This file provides all the RTC firmware functions.
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x_rtc.h"
00023 
00024 /** @addtogroup STM32F10x_StdPeriph_Driver
00025   * @{
00026   */
00027 
00028 /** @defgroup RTC 
00029   * @brief RTC driver modules
00030   * @{
00031   */
00032 
00033 /** @defgroup RTC_Private_TypesDefinitions
00034   * @{
00035   */ 
00036 /**
00037   * @}
00038   */
00039 
00040 /** @defgroup RTC_Private_Defines
00041   * @{
00042   */
00043 #define RTC_LSB_MASK     ((uint32_t)0x0000FFFF)  /*!< RTC LSB Mask */
00044 #define PRLH_MSB_MASK    ((uint32_t)0x000F0000)  /*!< RTC Prescaler MSB Mask */
00045 
00046 /**
00047   * @}
00048   */
00049 
00050 /** @defgroup RTC_Private_Macros
00051   * @{
00052   */
00053 
00054 /**
00055   * @}
00056   */
00057 
00058 /** @defgroup RTC_Private_Variables
00059   * @{
00060   */
00061 
00062 /**
00063   * @}
00064   */
00065 
00066 /** @defgroup RTC_Private_FunctionPrototypes
00067   * @{
00068   */
00069 
00070 /**
00071   * @}
00072   */
00073 
00074 /** @defgroup RTC_Private_Functions
00075   * @{
00076   */
00077 
00078 /**
00079   * @brief  Enables or disables the specified RTC interrupts.
00080   * @param  RTC_IT: specifies the RTC interrupts sources to be enabled or disabled.
00081   *   This parameter can be any combination of the following values:
00082   *     @arg RTC_IT_OW: Overflow interrupt
00083   *     @arg RTC_IT_ALR: Alarm interrupt
00084   *     @arg RTC_IT_SEC: Second interrupt
00085   * @param  NewState: new state of the specified RTC interrupts.
00086   *   This parameter can be: ENABLE or DISABLE.
00087   * @retval None
00088   */
00089 void RTC_ITConfig(uint16_t RTC_IT, FunctionalState NewState)
00090 {
00091   /* Check the parameters */
00092   assert_param(IS_RTC_IT(RTC_IT));  
00093   assert_param(IS_FUNCTIONAL_STATE(NewState));
00094   
00095   if (NewState != DISABLE)
00096   {
00097     RTC->CRH |= RTC_IT;
00098   }
00099   else
00100   {
00101     RTC->CRH &= (uint16_t)~RTC_IT;
00102   }
00103 }
00104 
00105 /**
00106   * @brief  Enters the RTC configuration mode.
00107   * @param  None
00108   * @retval None
00109   */
00110 void RTC_EnterConfigMode(void)
00111 {
00112   /* Set the CNF flag to enter in the Configuration Mode */
00113   RTC->CRL |= RTC_CRL_CNF;
00114 }
00115 
00116 /**
00117   * @brief  Exits from the RTC configuration mode.
00118   * @param  None
00119   * @retval None
00120   */
00121 void RTC_ExitConfigMode(void)
00122 {
00123   /* Reset the CNF flag to exit from the Configuration Mode */
00124   RTC->CRL &= (uint16_t)~((uint16_t)RTC_CRL_CNF); 
00125 }
00126 
00127 /**
00128   * @brief  Gets the RTC counter value.
00129   * @param  None
00130   * @retval RTC counter value.
00131   */
00132 uint32_t RTC_GetCounter(void)
00133 {
00134   uint16_t tmp = 0;
00135   tmp = RTC->CNTL;
00136   return (((uint32_t)RTC->CNTH << 16 ) | tmp) ;
00137 }
00138 
00139 /**
00140   * @brief  Sets the RTC counter value.
00141   * @param  CounterValue: RTC counter new value.
00142   * @retval None
00143   */
00144 void RTC_SetCounter(uint32_t CounterValue)
00145 { 
00146   RTC_EnterConfigMode();
00147   /* Set RTC COUNTER MSB word */
00148   RTC->CNTH = CounterValue >> 16;
00149   /* Set RTC COUNTER LSB word */
00150   RTC->CNTL = (CounterValue & RTC_LSB_MASK);
00151   RTC_ExitConfigMode();
00152 }
00153 
00154 /**
00155   * @brief  Sets the RTC prescaler value.
00156   * @param  PrescalerValue: RTC prescaler new value.
00157   * @retval None
00158   */
00159 void RTC_SetPrescaler(uint32_t PrescalerValue)
00160 {
00161   /* Check the parameters */
00162   assert_param(IS_RTC_PRESCALER(PrescalerValue));
00163   
00164   RTC_EnterConfigMode();
00165   /* Set RTC PRESCALER MSB word */
00166   RTC->PRLH = (PrescalerValue & PRLH_MSB_MASK) >> 16;
00167   /* Set RTC PRESCALER LSB word */
00168   RTC->PRLL = (PrescalerValue & RTC_LSB_MASK);
00169   RTC_ExitConfigMode();
00170 }
00171 
00172 /**
00173   * @brief  Sets the RTC alarm value.
00174   * @param  AlarmValue: RTC alarm new value.
00175   * @retval None
00176   */
00177 void RTC_SetAlarm(uint32_t AlarmValue)
00178 {  
00179   RTC_EnterConfigMode();
00180   /* Set the ALARM MSB word */
00181   RTC->ALRH = AlarmValue >> 16;
00182   /* Set the ALARM LSB word */
00183   RTC->ALRL = (AlarmValue & RTC_LSB_MASK);
00184   RTC_ExitConfigMode();
00185 }
00186 
00187 /**
00188   * @brief  Gets the RTC divider value.
00189   * @param  None
00190   * @retval RTC Divider value.
00191   */
00192 uint32_t RTC_GetDivider(void)
00193 {
00194   uint32_t tmp = 0x00;
00195   tmp = ((uint32_t)RTC->DIVH & (uint32_t)0x000F) << 16;
00196   tmp |= RTC->DIVL;
00197   return tmp;
00198 }
00199 
00200 /**
00201   * @brief  Waits until last write operation on RTC registers has finished.
00202   * @note   This function must be called before any write to RTC registers.
00203   * @param  None
00204   * @retval None
00205   */
00206 void RTC_WaitForLastTask(void)
00207 {
00208   /* Loop until RTOFF flag is set */
00209   while ((RTC->CRL & RTC_FLAG_RTOFF) == (uint16_t)RESET)
00210   {
00211   }
00212 }
00213 
00214 /**
00215   * @brief  Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL)
00216   *   are synchronized with RTC APB clock.
00217   * @note   This function must be called before any read operation after an APB reset
00218   *   or an APB clock stop.
00219   * @param  None
00220   * @retval None
00221   */
00222 void RTC_WaitForSynchro(void)
00223 {
00224   /* Clear RSF flag */
00225   RTC->CRL &= (uint16_t)~RTC_FLAG_RSF;
00226   /* Loop until RSF flag is set */
00227   while ((RTC->CRL & RTC_FLAG_RSF) == (uint16_t)RESET)
00228   {
00229   }
00230 }
00231 
00232 /**
00233   * @brief  Checks whether the specified RTC flag is set or not.
00234   * @param  RTC_FLAG: specifies the flag to check.
00235   *   This parameter can be one the following values:
00236   *     @arg RTC_FLAG_RTOFF: RTC Operation OFF flag
00237   *     @arg RTC_FLAG_RSF: Registers Synchronized flag
00238   *     @arg RTC_FLAG_OW: Overflow flag
00239   *     @arg RTC_FLAG_ALR: Alarm flag
00240   *     @arg RTC_FLAG_SEC: Second flag
00241   * @retval The new state of RTC_FLAG (SET or RESET).
00242   */
00243 FlagStatus RTC_GetFlagStatus(uint16_t RTC_FLAG)
00244 {
00245   FlagStatus bitstatus = RESET;
00246   
00247   /* Check the parameters */
00248   assert_param(IS_RTC_GET_FLAG(RTC_FLAG)); 
00249   
00250   if ((RTC->CRL & RTC_FLAG) != (uint16_t)RESET)
00251   {
00252     bitstatus = SET;
00253   }
00254   else
00255   {
00256     bitstatus = RESET;
00257   }
00258   return bitstatus;
00259 }
00260 
00261 /**
00262   * @brief  Clears the RTC’s pending flags.
00263   * @param  RTC_FLAG: specifies the flag to clear.
00264   *   This parameter can be any combination of the following values:
00265   *     @arg RTC_FLAG_RSF: Registers Synchronized flag. This flag is cleared only after
00266   *                        an APB reset or an APB Clock stop.
00267   *     @arg RTC_FLAG_OW: Overflow flag
00268   *     @arg RTC_FLAG_ALR: Alarm flag
00269   *     @arg RTC_FLAG_SEC: Second flag
00270   * @retval None
00271   */
00272 void RTC_ClearFlag(uint16_t RTC_FLAG)
00273 {
00274   /* Check the parameters */
00275   assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG)); 
00276     
00277   /* Clear the coressponding RTC flag */
00278   RTC->CRL &= (uint16_t)~RTC_FLAG;
00279 }
00280 
00281 /**
00282   * @brief  Checks whether the specified RTC interrupt has occured or not.
00283   * @param  RTC_IT: specifies the RTC interrupts sources to check.
00284   *   This parameter can be one of the following values:
00285   *     @arg RTC_IT_OW: Overflow interrupt
00286   *     @arg RTC_IT_ALR: Alarm interrupt
00287   *     @arg RTC_IT_SEC: Second interrupt
00288   * @retval The new state of the RTC_IT (SET or RESET).
00289   */
00290 ITStatus RTC_GetITStatus(uint16_t RTC_IT)
00291 {
00292   ITStatus bitstatus = RESET;
00293   /* Check the parameters */
00294   assert_param(IS_RTC_GET_IT(RTC_IT)); 
00295   
00296   bitstatus = (ITStatus)(RTC->CRL & RTC_IT);
00297   if (((RTC->CRH & RTC_IT) != (uint16_t)RESET) && (bitstatus != (uint16_t)RESET))
00298   {
00299     bitstatus = SET;
00300   }
00301   else
00302   {
00303     bitstatus = RESET;
00304   }
00305   return bitstatus;
00306 }
00307 
00308 /**
00309   * @brief  Clears the RTC’s interrupt pending bits.
00310   * @param  RTC_IT: specifies the interrupt pending bit to clear.
00311   *   This parameter can be any combination of the following values:
00312   *     @arg RTC_IT_OW: Overflow interrupt
00313   *     @arg RTC_IT_ALR: Alarm interrupt
00314   *     @arg RTC_IT_SEC: Second interrupt
00315   * @retval None
00316   */
00317 void RTC_ClearITPendingBit(uint16_t RTC_IT)
00318 {
00319   /* Check the parameters */
00320   assert_param(IS_RTC_IT(RTC_IT));  
00321   
00322   /* Clear the coressponding RTC pending bit */
00323   RTC->CRL &= (uint16_t)~RTC_IT;
00324 }
00325 
00326 /**
00327   * @}
00328   */
00329 
00330 /**
00331   * @}
00332   */
00333 
00334 /**
00335   * @}
00336   */
00337 
00338 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32