STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/RTC/RTC_LSI/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    RTC/RTC_LSI/main.c 
00004   * @author  MCD Application Team
00005   * @version V1.4.0
00006   * @date    24-July-2014
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "main.h"
00030 
00031 /** @addtogroup STM32F0xx_StdPeriph_Examples
00032   * @{
00033   */
00034 
00035 /** @addtogroup RTC_LSI
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 RTC_InitTypeDef   RTC_InitStructure;
00044 __IO uint32_t LsiFreq = 0;
00045 __IO uint32_t CaptureNumber = 0, PeriodValue = 0;
00046 
00047 /* Private function prototypes -----------------------------------------------*/
00048 static void RTC_Config(void);
00049 static uint32_t GetLSIFrequency(void);
00050 
00051 /* Private functions ---------------------------------------------------------*/
00052 
00053 /**
00054   * @brief  Main program.
00055   * @param  None
00056   * @retval None
00057   */
00058 int main(void)
00059 {
00060   /*!< At this stage the microcontroller clock setting is already configured, 
00061        this is done through SystemInit() function which is called from startup
00062        file (startup_stm32f0xx.s) before to branch to application main.
00063        To reconfigure the default setting of SystemInit() function, refer to
00064        system_stm32f0xx.c file
00065      */ 
00066 
00067 /* Initialize LEDs mounted on STM320518-EVAL board --------------------------*/
00068   STM_EVAL_LEDInit(LED1);
00069   STM_EVAL_LEDInit(LED2);
00070   STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_GPIO);
00071   
00072   /* RTC Configuration -------------------------------------------------------*/
00073   RTC_Config();
00074 
00075   /* Wait Until KEY BUTTON is pressed */
00076   while(STM_EVAL_PBGetState(BUTTON_TAMPER) != RESET)
00077   {
00078   }
00079   
00080   /* Get the LSI frequency:  TIM14 is used to measure the LSI frequency */
00081   LsiFreq = GetLSIFrequency();
00082 
00083   /* Turn on LED2 */
00084   STM_EVAL_LEDOn(LED2);
00085   
00086   /* Calendar Configuration */
00087   RTC_InitStructure.RTC_AsynchPrediv = 99;
00088   RTC_InitStructure.RTC_SynchPrediv     =  (LsiFreq/100) - 1;
00089   RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
00090   RTC_Init(&RTC_InitStructure);
00091 
00092   /* Infinite loop */
00093   while (1)
00094   {
00095   }
00096 }
00097 
00098 /**
00099   * @brief  Configure the RTC peripheral by selecting the clock source.
00100   * @param  None
00101   * @retval None
00102   */
00103 static void RTC_Config(void)
00104 {
00105   RTC_TimeTypeDef RTC_TimeStructure;
00106   NVIC_InitTypeDef NVIC_InitStructure; 
00107   EXTI_InitTypeDef EXTI_InitStructure;
00108   RTC_AlarmTypeDef RTC_AlarmStructure;
00109   /* Enable the PWR clock */
00110   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
00111   
00112   /* Allow access to RTC */
00113   PWR_BackupAccessCmd(ENABLE);
00114 
00115 /* LSI used as RTC source clock */
00116 /* The RTC Clock may varies due to LSI frequency dispersion. */   
00117   /* Enable the LSI OSC */ 
00118   RCC_LSICmd(ENABLE);
00119 
00120   /* Wait till LSI is ready */  
00121   while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
00122   {
00123   }
00124 
00125   /* Select the RTC Clock Source */
00126   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
00127    
00128   /* Enable the RTC Clock */
00129   RCC_RTCCLKCmd(ENABLE);
00130 
00131   /* Wait for RTC APB registers synchronisation */
00132   RTC_WaitForSynchro();
00133 
00134   /* Calendar Configuration */
00135   RTC_InitStructure.RTC_AsynchPrediv = 99;
00136   RTC_InitStructure.RTC_SynchPrediv     =  399; /* (40KHz / 100) - 1 = 399*/
00137   RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
00138   RTC_Init(&RTC_InitStructure);  
00139 
00140   /* EXTI configuration *******************************************************/
00141   EXTI_ClearITPendingBit(EXTI_Line17);
00142   EXTI_InitStructure.EXTI_Line = EXTI_Line17;
00143   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
00144   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00145   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00146   EXTI_Init(&EXTI_InitStructure);
00147   
00148   /* Enable the RTC Wakeup Interrupt */
00149   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
00150   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00151   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00152   NVIC_Init(&NVIC_InitStructure);
00153   
00154     /* Set the alarm X+5s */
00155   RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_H12_AM;
00156   RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = 0x00;
00157   RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x00;
00158   RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x01;
00159   RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
00160   RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
00161   RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
00162   RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
00163 
00164   RTC_ITConfig(RTC_IT_ALRA, ENABLE);
00165     
00166   /* Enable the alarm */
00167   RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
00168   
00169   /* Set the time to 00h 00mn 00s AM */
00170   RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
00171   RTC_TimeStructure.RTC_Hours   = 0x00;
00172   RTC_TimeStructure.RTC_Minutes = 0x00;
00173   RTC_TimeStructure.RTC_Seconds = 0x00;  
00174   
00175   RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
00176 }
00177 
00178 /**
00179   * @brief  Configures TIM14 to measure the LSI oscillator frequency. 
00180   * @param  None
00181   * @retval LSI Frequency
00182   */
00183 static uint32_t GetLSIFrequency(void)
00184 {
00185   NVIC_InitTypeDef   NVIC_InitStructure;
00186   TIM_ICInitTypeDef  TIM_ICInitStructure;
00187   RCC_ClocksTypeDef  RCC_ClockFreq;
00188 
00189   /* TIM14 configuration *******************************************************/ 
00190   /* Enable TIM14 clock */
00191   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
00192   
00193   /* Reset TIM14 registers */
00194   TIM_DeInit(TIM14);
00195 
00196   /* Configure TIM14 prescaler */
00197   TIM_PrescalerConfig(TIM14, 0, TIM_PSCReloadMode_Immediate);
00198 
00199   /* Connect internally the TIM14_CH1 to the RTC clock output */
00200   TIM_RemapConfig(TIM14, TIM14_RTC_CLK);
00201 
00202   /* TIM14 configuration: Input Capture mode ---------------------
00203      The reference clock(LSE or external) is connected to TIM14 CH1
00204      The Rising edge is used as active edge,
00205      The TIM14 CCR1 is used to compute the frequency value 
00206   ------------------------------------------------------------ */
00207   TIM_ICInitStructure.TIM_Channel     = TIM_Channel_1;
00208   TIM_ICInitStructure.TIM_ICPolarity  = TIM_ICPolarity_Rising;
00209   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
00210   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8;
00211   TIM_ICInitStructure.TIM_ICFilter = 0x0;
00212   TIM_ICInit(TIM14, &TIM_ICInitStructure);
00213 
00214   /* Enable the TIM14 global Interrupt */
00215   NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn;
00216   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00217   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00218   NVIC_Init(&NVIC_InitStructure);
00219 
00220   /* Enable TIM14 counter */
00221   TIM_Cmd(TIM14, ENABLE);
00222 
00223   /* Reset the flags */
00224   TIM14->SR = 0;
00225     
00226   /* Enable the CC1 Interrupt Request */  
00227   TIM_ITConfig(TIM14, TIM_IT_CC1, ENABLE);
00228 
00229 
00230   /* Wait until the TIM14 get 2 LSI edges (refer to TIM14_IRQHandler() in 
00231     stm32F0xx_it.c file) ******************************************************/
00232   while(CaptureNumber != 2)
00233   {
00234   }
00235   /* Deinitialize the TIM14 peripheral registers to their default reset values */
00236   TIM_DeInit(TIM14);
00237 
00238 
00239   /* Compute the LSI frequency, depending on TIM14 input clock frequency (PCLK1)*/
00240   /* Get SYSCLK, HCLK and PCLKx frequency */
00241   RCC_GetClocksFreq(&RCC_ClockFreq);
00242   
00243   /* PCLK1 prescaler equal to 1 => TIMCLK = PCLK1 */
00244   return ((RCC_ClockFreq.PCLK_Frequency / PeriodValue) * 8);
00245 }
00246 
00247 #ifdef  USE_FULL_ASSERT
00248 
00249 /**
00250   * @brief  Reports the name of the source file and the source line number
00251   *         where the assert_param error has occurred.
00252   * @param  file: pointer to the source file name
00253   * @param  line: assert_param error line source number
00254   * @retval None
00255   */
00256 void assert_failed(uint8_t* file, uint32_t line)
00257 { 
00258   /* User can add his own implementation to report the file name and line number,
00259      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00260 
00261   /* Infinite loop */
00262   while (1)
00263   {
00264   }
00265 }
00266 #endif
00267 
00268 /**
00269   * @}
00270   */
00271 
00272 /**
00273   * @}
00274   */
00275 
00276 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32