STM32F10x_StdPeriph_Examples/RTC/LSI_Calib/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    RTC/LSI_Calib/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
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 "main.h"
00023 #include <stdio.h>
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup RTC_LSI_Calib
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 #define RTCClockOutput_Enable  /* RTC Clock/64 is output on tamper pin(PC.13) */
00036 
00037 /* Private macro -------------------------------------------------------------*/
00038 /* Private variables ---------------------------------------------------------*/
00039 
00040 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00041 TIM_ICInitTypeDef  TIM_ICInitStructure;
00042 RCC_ClocksTypeDef RCC_Clocks;
00043 __IO uint32_t PeriodValue = 0,  LsiFreq = 0;
00044 __IO uint32_t OperationComplete = 0;
00045 
00046 /* Private function prototypes -----------------------------------------------*/
00047 void RTC_Configuration(void);
00048 void NVIC_Configuration(void);
00049 
00050 /* Private functions ---------------------------------------------------------*/
00051 
00052 /**
00053   * @brief   Main program.
00054   * @param  None
00055   * @retval None
00056   */
00057 int main(void)
00058 {
00059   /*!< At this stage the microcontroller clock setting is already configured, 
00060        this is done through SystemInit() function which is called from startup
00061        file (startup_stm32f10x_xx.s) before to branch to application main.
00062        To reconfigure the default setting of SystemInit() function, refer to
00063        system_stm32f10x.c file
00064      */     
00065 
00066   /* Initialize LEDs and Key Button mounted on STM3210X-EVAL board */       
00067   STM_EVAL_LEDInit(LED1);
00068   STM_EVAL_LEDInit(LED2);
00069   STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);
00070 
00071   /* RTC Configuration */
00072   RTC_Configuration();
00073 
00074   /* Wait until Key Push button is pressed */
00075   while (STM_EVAL_PBGetState(BUTTON_KEY) != 0)
00076   {
00077   }
00078 
00079   /* Get the Frequency value */
00080   RCC_GetClocksFreq(&RCC_Clocks);
00081 
00082   /* Enable TIM5 APB1 clocks */
00083   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
00084 
00085   /* Connect internally the TM5_CH4 Input Capture to the LSI clock output */
00086   GPIO_PinRemapConfig(GPIO_Remap_TIM5CH4_LSI, ENABLE);
00087 
00088   /* TIM5 Time base configuration */
00089   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00090   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00091   TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
00092   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
00093   TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
00094 
00095   /* TIM5 Channel4 Input capture Mode configuration */
00096   TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
00097   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
00098   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
00099   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
00100   TIM_ICInitStructure.TIM_ICFilter = 0;
00101   TIM_ICInit(TIM5, &TIM_ICInitStructure);
00102 
00103   /* Reinitialize the index for the interrupt */
00104   OperationComplete = 0;
00105 
00106   /* Enable the TIM5 Input Capture counter */
00107   TIM_Cmd(TIM5, ENABLE);
00108   /* Reset all TIM5 flags */
00109   TIM5->SR = 0;
00110   /* Enable the TIM5 channel 4 */
00111   TIM_ITConfig(TIM5, TIM_IT_CC4, ENABLE);
00112 
00113   /* NVIC configuration */
00114   NVIC_Configuration();
00115 
00116   /* Wait the TIM5 measuring operation to be completed */
00117   while (OperationComplete != 2)
00118   {}
00119 
00120   /* Compute the actual frequency of the LSI. (TIM5_CLK = 2 * PCLK1)  */
00121   if (PeriodValue != 0)
00122   {
00123 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00124     LsiFreq = (uint32_t)((uint32_t)(RCC_Clocks.PCLK1_Frequency) / (uint32_t)PeriodValue);
00125 #else
00126     LsiFreq = (uint32_t)((uint32_t)(RCC_Clocks.PCLK1_Frequency * 2) / (uint32_t)PeriodValue);
00127 #endif
00128   }
00129 
00130   /* Adjust the RTC prescaler value */
00131   RTC_SetPrescaler(LsiFreq - 1);
00132 
00133   /* Wait until last write operation on RTC registers has finished */
00134   RTC_WaitForLastTask();
00135 
00136   /* Turn on LED2 */
00137   STM_EVAL_LEDOn(LED2);
00138 
00139   while (1)
00140   {
00141     /* Inifinite loop */
00142   }
00143 
00144 }
00145 
00146 /**
00147   * @brief  Configures the nested vectored interrupt controller.
00148   * @param  None
00149   * @retval None
00150   */
00151 void NVIC_Configuration(void)
00152 {
00153   NVIC_InitTypeDef NVIC_InitStructure;
00154 
00155   /* Configure one bit for preemption priority */
00156   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
00157 
00158   /* Enable the RTC Interrupt */
00159   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
00160   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00161   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00162   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00163   NVIC_Init(&NVIC_InitStructure);
00164 
00165   /* Enable the TIM5 Interrupt */
00166   NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
00167   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
00168   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00169   NVIC_Init(&NVIC_InitStructure);
00170 }
00171 
00172 /**
00173   * @brief  Configures the RTC.
00174   * @param  None
00175   * @retval None
00176   */
00177 void RTC_Configuration(void)
00178 {
00179   /* Enable PWR and BKP clocks */
00180   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
00181 
00182   /* Allow access to BKP Domain */
00183   PWR_BackupAccessCmd(ENABLE);
00184 
00185   /* Reset Backup Domain */
00186   BKP_DeInit();
00187 
00188   /* Enable the LSI OSC */
00189   RCC_LSICmd(ENABLE);
00190   /* Wait till LSI is ready */
00191   while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
00192   {}
00193   /* Select the RTC Clock Source */
00194   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
00195 
00196   /* Enable RTC Clock */
00197   RCC_RTCCLKCmd(ENABLE);
00198 
00199   /* Wait for RTC registers synchronization */
00200   RTC_WaitForSynchro();
00201 
00202   /* Wait until last write operation on RTC registers has finished */
00203   RTC_WaitForLastTask();
00204 
00205   /* Enable the RTC Second */
00206   RTC_ITConfig(RTC_IT_SEC, ENABLE);
00207 
00208   /* Wait until last write operation on RTC registers has finished */
00209   RTC_WaitForLastTask();
00210 
00211   /* Set RTC prescaler: set RTC period to 1sec */
00212   RTC_SetPrescaler(40000);
00213 
00214   /* Wait until last write operation on RTC registers has finished */
00215   RTC_WaitForLastTask();
00216 
00217   /* To output second signal on Tamper pin, the tamper functionality
00218        must be disabled (by default this functionality is disabled) */
00219   BKP_TamperPinCmd(DISABLE);
00220 
00221   /* Enable the RTC Second Output on Tamper Pin */
00222   BKP_RTCOutputConfig(BKP_RTCOutputSource_Second);
00223 }
00224 
00225 /**
00226   * @brief  Increments OperationComplete variable and return its value 
00227   *   before increment operation.
00228   * @param  None
00229   * @retval OperationComplete value befor increment
00230   */
00231 uint32_t IncrementVar_OperationComplete(void)
00232 {
00233   OperationComplete++;
00234   
00235   return (uint32_t)(OperationComplete -1);
00236 }
00237 
00238 /**
00239   * @brief  Returns OperationComplete value.
00240   * @param  None
00241   * @retval OperationComplete value
00242   */
00243 uint32_t GetVar_OperationComplete(void)
00244 {
00245   return (uint32_t)OperationComplete;
00246 }
00247 
00248 /**
00249   * @brief  Sets the PeriodValue variable with input parameter.
00250   * @param  lue: Value of PeriodValue to be set.
00251   * @retval None
00252   */
00253 void SetVar_PeriodValue(uint32_t Value)
00254 {
00255   PeriodValue = (uint32_t)(Value);
00256 }
00257 
00258 #ifdef  USE_FULL_ASSERT
00259 /**
00260   * @brief  Reports the name of the source file and the source line number
00261   *         where the assert_param error has occurred.
00262   * @param  file: pointer to the source file name
00263   * @param  line: assert_param error line source number
00264   * @retval None
00265   */
00266 void assert_failed(uint8_t* file, uint32_t line)
00267 {
00268   /* User can add his own implementation to report the file name and line number,
00269      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00270 
00271   /* Infinite loop */
00272   while (1)
00273   {}
00274 }
00275 #endif
00276 
00277 /**
00278   * @}
00279   */
00280 
00281 /**
00282   * @}
00283   */
00284 
00285 /******************* (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