STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/PWR/PWR_Stop/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file PWR/PWR_Stop/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>© 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 PWR_Stop 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 extern __IO uint32_t TimingDelay; 00044 00045 /* Private function prototypes -----------------------------------------------*/ 00046 static void RTC_Config(void); 00047 static void SYSCLKConfig_STOP(void); 00048 static void RTC_AlarmConfig(void); 00049 void Delay(__IO uint32_t nTime); 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 /* Configures the TAMPER button */ 00068 STM_EVAL_PBInit(BUTTON_TAMPER,BUTTON_MODE_EXTI); 00069 00070 /* Configure LEDs */ 00071 STM_EVAL_LEDInit(LED1); 00072 STM_EVAL_LEDInit(LED2); 00073 STM_EVAL_LEDInit(LED4); 00074 00075 /* SysTick interrupt each 10 ms */ 00076 if (SysTick_Config(SystemCoreClock / 100)) 00077 { 00078 /* Capture error */ 00079 while (1); 00080 } 00081 00082 /* RTC Configuration */ 00083 RTC_Config(); 00084 00085 /* LED1 On */ 00086 STM_EVAL_LEDOn(LED1); 00087 00088 while(1) 00089 { 00090 /* Insert 5 second delay */ 00091 Delay(500); 00092 00093 /* Set alarm in 5s */ 00094 RTC_AlarmConfig(); 00095 00096 /* LEDs Off */ 00097 STM_EVAL_LEDOff(LED1); 00098 STM_EVAL_LEDOff(LED2); 00099 STM_EVAL_LEDOff(LED4); 00100 00101 /* Request to enter STOP mode with regulator in low power mode */ 00102 PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); 00103 00104 /* LED1 On */ 00105 STM_EVAL_LEDOn(LED1); 00106 00107 /* Disable the RTC Alarm interrupt */ 00108 RTC_ITConfig(RTC_IT_ALRA, DISABLE); 00109 RTC_AlarmCmd(RTC_Alarm_A, DISABLE); 00110 00111 /* Configures system clock after wake-up from STOP */ 00112 SYSCLKConfig_STOP(); 00113 } 00114 } 00115 00116 /** 00117 * @brief Configures the RTC clock source. 00118 * @param None 00119 * @retval None 00120 */ 00121 static void RTC_Config(void) 00122 { 00123 RTC_TimeTypeDef RTC_TimeStructure; 00124 RTC_InitTypeDef RTC_InitStructure; 00125 EXTI_InitTypeDef EXTI_InitStructure; 00126 NVIC_InitTypeDef NVIC_InitStructure; 00127 00128 /* RTC Configuration **********************************************************/ 00129 /* Enable the PWR clock */ 00130 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); 00131 00132 /* Allow access to RTC */ 00133 PWR_BackupAccessCmd(ENABLE); 00134 00135 /* Reset back up registers */ 00136 RCC_BackupResetCmd(ENABLE); 00137 RCC_BackupResetCmd(DISABLE); 00138 00139 /* Enable the LSE */ 00140 RCC_LSEConfig(RCC_LSE_ON); 00141 00142 /* Wait till LSE is ready */ 00143 while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) 00144 {} 00145 00146 /* Select the RTC Clock Source */ 00147 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); 00148 00149 /* Enable the RTC Clock */ 00150 RCC_RTCCLKCmd(ENABLE); 00151 00152 RTC_DeInit(); 00153 /* Wait for RTC APB registers synchronisation */ 00154 RTC_WaitForSynchro(); 00155 00156 /* Set RTC calendar clock to 1 HZ (1 second) */ 00157 RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; 00158 RTC_InitStructure.RTC_AsynchPrediv = 0x7F; 00159 RTC_InitStructure.RTC_SynchPrediv = 0x0FF; 00160 00161 if (RTC_Init(&RTC_InitStructure) == ERROR) 00162 { 00163 while(1); 00164 } 00165 00166 /* Set the time to 01h 00mn 00s AM */ 00167 RTC_TimeStructure.RTC_H12 = RTC_H12_AM; 00168 RTC_TimeStructure.RTC_Hours = 0x01; 00169 RTC_TimeStructure.RTC_Minutes = 0x00; 00170 RTC_TimeStructure.RTC_Seconds = 0x00; 00171 00172 RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); 00173 00174 /* Configure EXTI line 17 (connected to the RTC Alarm event) */ 00175 EXTI_ClearITPendingBit(EXTI_Line17); 00176 EXTI_InitStructure.EXTI_Line = EXTI_Line17; 00177 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 00178 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 00179 EXTI_InitStructure.EXTI_LineCmd = ENABLE; 00180 EXTI_Init(&EXTI_InitStructure); 00181 00182 /* NVIC configuration */ 00183 NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; 00184 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00185 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00186 NVIC_Init(&NVIC_InitStructure); 00187 } 00188 00189 /** 00190 * @brief Configures the RTC clock source. 00191 * @param None 00192 * @retval None 00193 */ 00194 static void RTC_AlarmConfig(void) 00195 { 00196 RTC_TimeTypeDef RTC_TimeStructure; 00197 RTC_AlarmTypeDef RTC_AlarmStructure; 00198 00199 /* Get current time */ 00200 RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); 00201 00202 /* Set the alarm to current time + 5s */ 00203 RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM; 00204 RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = RTC_TimeStructure.RTC_Hours; 00205 RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = RTC_TimeStructure.RTC_Minutes; 00206 RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = RTC_TimeStructure.RTC_Seconds + 5; 00207 RTC_AlarmStructure.RTC_AlarmDateWeekDay = 31; 00208 RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; 00209 RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay | RTC_AlarmMask_Minutes | 00210 RTC_AlarmMask_Hours; 00211 RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); 00212 00213 /* Enable the RTC Alarm A interrupt */ 00214 RTC_ITConfig(RTC_IT_ALRA, ENABLE); 00215 00216 /* Enable the alarm */ 00217 RTC_AlarmCmd(RTC_Alarm_A, ENABLE); 00218 00219 /* Clear the Alarm A Pending Bit */ 00220 RTC_ClearITPendingBit(RTC_IT_ALRA); 00221 } 00222 00223 /** 00224 * @brief Configures system clock after wake-up from STOP: enable HSE, PLL 00225 * and select PLL as system clock source. 00226 * @param None 00227 * @retval None 00228 */ 00229 static void SYSCLKConfig_STOP(void) 00230 { 00231 /* After wake-up from STOP reconfigure the system clock */ 00232 /* Enable HSE */ 00233 RCC_HSEConfig(RCC_HSE_ON); 00234 00235 /* Wait till HSE is ready */ 00236 while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) 00237 {} 00238 00239 /* Enable PLL */ 00240 RCC_PLLCmd(ENABLE); 00241 00242 /* Wait till PLL is ready */ 00243 while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) 00244 {} 00245 00246 /* Select PLL as system clock source */ 00247 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 00248 00249 /* Wait till PLL is used as system clock source */ 00250 while (RCC_GetSYSCLKSource() != 0x08) 00251 {} 00252 } 00253 00254 /** 00255 * @brief Inserts a delay time. 00256 * @param nTime: specifies the delay time length, with a base of 250 milliseconds. 00257 * @retval None 00258 */ 00259 void Delay(__IO uint32_t nTime) 00260 { 00261 TimingDelay = nTime; 00262 00263 while(TimingDelay != 0); 00264 00265 } 00266 00267 00268 #ifdef USE_FULL_ASSERT 00269 00270 /** 00271 * @brief Reports the name of the source file and the source line number 00272 * where the assert_param error has occurred. 00273 * @param file: pointer to the source file name 00274 * @param line: assert_param error line source number 00275 * @retval None 00276 */ 00277 void assert_failed(uint8_t* file, uint32_t line) 00278 { 00279 /* KEY can add his own implementation to report the file name and line number, 00280 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00281 00282 /* Infinite loop */ 00283 while (1) 00284 { 00285 } 00286 } 00287 #endif 00288 00289 /** 00290 * @} 00291 */ 00292 00293 /** 00294 * @} 00295 */ 00296 00297 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/