STM32F10x_StdPeriph_Examples/PWR/STANDBY/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    PWR/STANDBY/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 "stm32f10x.h"
00023 #include "stm32_eval.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup PWR_STANDBY
00030   * @{
00031   */
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 /* Private macro -------------------------------------------------------------*/
00036 /* Private variables ---------------------------------------------------------*/
00037 
00038 /* Private function prototypes -----------------------------------------------*/
00039 void RTC_Configuration(void);
00040 void SysTick_Configuration(void);
00041 
00042 /* Private functions ---------------------------------------------------------*/
00043 
00044 /**
00045   * @brief  Main program.
00046   * @param  None
00047   * @retval None
00048   */
00049 int main(void)
00050 {
00051   /*!< At this stage the microcontroller clock setting is already configured, 
00052        this is done through SystemInit() function which is called from startup
00053        file (startup_stm32f10x_xx.s) before to branch to application main.
00054        To reconfigure the default setting of SystemInit() function, refer to
00055        system_stm32f10x.c file
00056      */     
00057 
00058   /* Initialize LEDs and Key Button mounted on STM3210X-EVAL board */       
00059   STM_EVAL_LEDInit(LED1);
00060   STM_EVAL_LEDInit(LED2);
00061   STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);
00062 
00063   /* Turn on LED1 */
00064   STM_EVAL_LEDOn(LED1);
00065 
00066   /* Enable PWR and BKP clock */
00067   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
00068 
00069   /* Enable WKUP pin */
00070   PWR_WakeUpPinCmd(ENABLE);
00071 
00072   /* Allow access to BKP Domain */
00073   PWR_BackupAccessCmd(ENABLE);
00074 
00075   /* Configure RTC clock source and prescaler */
00076   RTC_Configuration();
00077 
00078   /* Configure the SysTick to generate an interrupt each 250 ms */
00079   SysTick_Configuration();
00080  
00081   while (1)
00082   {
00083   }
00084 }
00085 
00086 /**
00087   * @brief  Configures RTC clock source and prescaler.
00088   * @param  None
00089   * @retval None
00090   */
00091 void RTC_Configuration(void)
00092 {
00093   /* Check if the StandBy flag is set */
00094   if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET)
00095   {/* System resumed from STANDBY mode */
00096 
00097     /* Turn on LED2 */
00098     STM_EVAL_LEDOn(LED2);
00099 
00100     /* Clear StandBy flag */
00101     PWR_ClearFlag(PWR_FLAG_SB);
00102 
00103     /* Wait for RTC APB registers synchronisation */
00104     RTC_WaitForSynchro();
00105     /* No need to configure the RTC as the RTC configuration(clock source, enable,
00106        prescaler,...) is kept after wake-up from STANDBY */
00107   }
00108   else
00109   {/* StandBy flag is not set */
00110 
00111     /* RTC clock source configuration ----------------------------------------*/
00112     /* Reset Backup Domain */
00113     BKP_DeInit();
00114   
00115     /* Enable LSE OSC */
00116     RCC_LSEConfig(RCC_LSE_ON);
00117     /* Wait till LSE is ready */
00118     while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
00119     {
00120     }
00121 
00122     /* Select the RTC Clock Source */
00123     RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
00124 
00125     /* Enable the RTC Clock */
00126     RCC_RTCCLKCmd(ENABLE);
00127 
00128     /* RTC configuration -----------------------------------------------------*/
00129     /* Wait for RTC APB registers synchronisation */
00130     RTC_WaitForSynchro();
00131 
00132     /* Set the RTC time base to 1s */
00133     RTC_SetPrescaler(32767);  
00134     /* Wait until last write operation on RTC registers has finished */
00135     RTC_WaitForLastTask();
00136   }
00137 }
00138 
00139 /**
00140   * @brief  Configures the SysTick to generate an interrupt each 250 ms.
00141   * @param  None
00142   * @retval None
00143   */
00144 void SysTick_Configuration(void)
00145 {
00146   /* SysTick interrupt each 250 ms with SysTick Clock equal to (HCLK/8) */
00147   if (SysTick_Config((SystemCoreClock/8) / 4))
00148   { 
00149     /* Capture error */ 
00150     while (1);
00151   }
00152 
00153   /* Select AHB clock(HCLK) divided by 8 as SysTick clock source */
00154   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
00155 
00156   /* Set SysTick Preemption Priority to 1 */
00157   NVIC_SetPriority(SysTick_IRQn, 0x04);
00158 }
00159 
00160 #ifdef  USE_FULL_ASSERT
00161 /**
00162   * @brief  Reports the name of the source file and the source line number
00163   *         where the assert_param error has occurred.
00164   * @param  file: pointer to the source file name
00165   * @param  line: assert_param error line source number
00166   * @retval None
00167   */
00168 void assert_failed(uint8_t* file, uint32_t line)
00169 { 
00170   /* User can add his own implementation to report the file name and line number,
00171      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00172 
00173   /* Infinite loop */
00174   while (1)
00175   {
00176   }
00177 }
00178 #endif
00179 
00180 /**
00181   * @}
00182   */
00183 
00184 /**
00185   * @}
00186   */ 
00187 
00188 /******************* (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