STM32F10x_StdPeriph_Examples/PWR/STOP/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    PWR/STOP/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_STOP
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 /* Private macro -------------------------------------------------------------*/
00036 /* Private variables ---------------------------------------------------------*/
00037 extern __IO uint32_t TimingDelay;
00038 ErrorStatus HSEStartUpStatus;
00039 
00040 /* Private function prototypes -----------------------------------------------*/
00041 void SYSCLKConfig_STOP(void);
00042 void EXTI_Configuration(void);
00043 void RTC_Configuration(void);
00044 void NVIC_Configuration(void);
00045 void SysTick_Configuration(void);
00046 void Delay(__IO uint32_t nTime);
00047 
00048 /* Private functions ---------------------------------------------------------*/
00049 
00050 /**
00051   * @brief   Main program.
00052   * @param  None
00053   * @retval None
00054   */
00055 int main(void)
00056 {
00057   /*!< At this stage the microcontroller clock setting is already configured, 
00058        this is done through SystemInit() function which is called from startup
00059        file (startup_stm32f10x_xx.s) before to branch to application main.
00060        To reconfigure the default setting of SystemInit() function, refer to
00061        system_stm32f10x.c file
00062      */     
00063 
00064   /* Initialize LEDs and Key Button mounted on STM3210X-EVAL board */       
00065   STM_EVAL_LEDInit(LED1);
00066   STM_EVAL_LEDInit(LED2);
00067   STM_EVAL_LEDInit(LED3);
00068   STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);
00069 
00070   /* Enable PWR and BKP clock */
00071   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
00072 
00073   /* Configure EXTI Line to generate an interrupt on falling edge */
00074   EXTI_Configuration();
00075 
00076   /* Configure RTC clock source and prescaler */
00077   RTC_Configuration();
00078 
00079   /* NVIC configuration */
00080   NVIC_Configuration();
00081 
00082   /* Configure the SysTick to generate an interrupt each 1 millisecond */
00083   SysTick_Configuration();
00084 
00085   /* Turn on LED1 */
00086   STM_EVAL_LEDOn(LED1);
00087   
00088   while (1)
00089   {
00090     /* Insert 1.5 second delay */
00091     Delay(1500);
00092 
00093     /* Wait till RTC Second event occurs */
00094     RTC_ClearFlag(RTC_FLAG_SEC);
00095     while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET);
00096 
00097     /* Alarm in 3 second */
00098     RTC_SetAlarm(RTC_GetCounter()+ 3);
00099     /* Wait until last write operation on RTC registers has finished */
00100     RTC_WaitForLastTask();
00101 
00102     /* Turn off LED1 */
00103     STM_EVAL_LEDOff(LED1);
00104 
00105     /* Request to enter STOP mode with regulator in low power mode*/
00106     PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
00107     
00108     /* At this stage the system has resumed from STOP mode -------------------*/
00109     /* Turn on LED1 */
00110     STM_EVAL_LEDOn(LED1);
00111 
00112     /* Configures system clock after wake-up from STOP: enable HSE, PLL and select 
00113        PLL as system clock source (HSE and PLL are disabled in STOP mode) */
00114     SYSCLKConfig_STOP();
00115   }
00116 }
00117 
00118 /**
00119   * @brief  Configures system clock after wake-up from STOP: enable HSE, PLL
00120   *   and select PLL as system clock source.
00121   * @param  None
00122   * @retval None
00123   */
00124 void SYSCLKConfig_STOP(void)
00125 {
00126   /* Enable HSE */
00127   RCC_HSEConfig(RCC_HSE_ON);
00128 
00129   /* Wait till HSE is ready */
00130   HSEStartUpStatus = RCC_WaitForHSEStartUp();
00131 
00132   if(HSEStartUpStatus == SUCCESS)
00133   {
00134 
00135 #ifdef STM32F10X_CL
00136     /* Enable PLL2 */ 
00137     RCC_PLL2Cmd(ENABLE);
00138 
00139     /* Wait till PLL2 is ready */
00140     while(RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET)
00141     {
00142     }
00143 #endif
00144 
00145     /* Enable PLL */ 
00146     RCC_PLLCmd(ENABLE);
00147 
00148     /* Wait till PLL is ready */
00149     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
00150     {
00151     }
00152 
00153     /* Select PLL as system clock source */
00154     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
00155 
00156     /* Wait till PLL is used as system clock source */
00157     while(RCC_GetSYSCLKSource() != 0x08)
00158     {
00159     }
00160   }
00161 }
00162 
00163 /**
00164   * @brief  Configures EXTI Lines.
00165   * @param  None
00166   * @retval None
00167   */
00168 void EXTI_Configuration(void)
00169 {
00170   EXTI_InitTypeDef EXTI_InitStructure;
00171 
00172   /* Configure EXTI Line17(RTC Alarm) to generate an interrupt on rising edge */
00173   EXTI_ClearITPendingBit(EXTI_Line17);
00174   EXTI_InitStructure.EXTI_Line = EXTI_Line17;
00175   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
00176   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00177   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00178   EXTI_Init(&EXTI_InitStructure);
00179 }
00180 
00181 /**
00182   * @brief  Configures RTC clock source and prescaler.
00183   * @param  None
00184   * @retval None
00185   */
00186 void RTC_Configuration(void)
00187 {
00188   /* RTC clock source configuration ------------------------------------------*/
00189   /* Allow access to BKP Domain */
00190   PWR_BackupAccessCmd(ENABLE);
00191 
00192   /* Reset Backup Domain */
00193   BKP_DeInit();
00194   
00195   /* Enable the LSE OSC */
00196   RCC_LSEConfig(RCC_LSE_ON);
00197   /* Wait till LSE is ready */
00198   while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
00199   {
00200   }
00201 
00202   /* Select the RTC Clock Source */
00203   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
00204 
00205   /* Enable the RTC Clock */
00206   RCC_RTCCLKCmd(ENABLE);
00207 
00208   /* RTC configuration -------------------------------------------------------*/
00209   /* Wait for RTC APB registers synchronisation */
00210   RTC_WaitForSynchro();
00211 
00212   /* Set the RTC time base to 1s */
00213   RTC_SetPrescaler(32767);  
00214   /* Wait until last write operation on RTC registers has finished */
00215   RTC_WaitForLastTask();
00216 
00217   /* Enable the RTC Alarm interrupt */
00218   RTC_ITConfig(RTC_IT_ALR, ENABLE);
00219   /* Wait until last write operation on RTC registers has finished */
00220   RTC_WaitForLastTask();
00221 }
00222 
00223 /**
00224   * @brief  Configures NVIC and Vector Table base location.
00225   * @param  None
00226   * @retval None
00227   */
00228 void NVIC_Configuration(void)
00229 {
00230   NVIC_InitTypeDef NVIC_InitStructure;
00231 
00232   /* 2 bits for Preemption Priority and 2 bits for Sub Priority */
00233   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
00234 
00235   NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
00236   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00237   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00238   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00239   NVIC_Init(&NVIC_InitStructure);
00240 }
00241 
00242 /**
00243   * @brief  Configures the SysTick to generate an interrupt each 1 millisecond.
00244   * @param  None
00245   * @retval None
00246   */
00247 void SysTick_Configuration(void)
00248 {
00249   /* Setup SysTick Timer for 1 msec interrupts  */
00250   if (SysTick_Config(SystemCoreClock / 1000))
00251   { 
00252     /* Capture error */ 
00253     while (1);
00254   }
00255   /* Set SysTick Priority to 3 */
00256   NVIC_SetPriority(SysTick_IRQn, 0x0C);
00257 }
00258 
00259 /**
00260   * @brief  Inserts a delay time.
00261   * @param  nTime: specifies the delay time length, in milliseconds.
00262   * @retval None
00263   */
00264 void Delay(__IO uint32_t nTime)
00265 {
00266   TimingDelay = nTime;
00267 
00268   while(TimingDelay != 0);
00269 
00270 }
00271 
00272 #ifdef  USE_FULL_ASSERT
00273 /**
00274   * @brief  Reports the name of the source file and the source line number
00275   *         where the assert_param error has occurred.
00276   * @param  file: pointer to the source file name
00277   * @param  line: assert_param error line source number
00278   * @retval None
00279   */
00280 void assert_failed(uint8_t* file, uint32_t line)
00281 { 
00282   /* User can add his own implementation to report the file name and line number,
00283      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00284 
00285   /* Infinite loop */
00286   while (1)
00287   {
00288   }
00289 }
00290 #endif
00291 
00292 /**
00293   * @}
00294   */
00295 
00296 /**
00297   * @}
00298   */
00299 
00300 /******************* (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