STM32F0xx Standard Peripherals Firmware Library: stm32f0xx_lp_modes.c Source File

STM32F0xx Standard Peripherals Library

stm32f0xx_lp_modes.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    PWR/PWR_CurrentConsumption/stm32f0xx_lp_modes.c 
00004   * @author  MCD Application Team
00005   * @version V1.4.0
00006   * @date    24-July-2014
00007   * @brief   This file provides firmware functions to manage the following 
00008   *          functionalities of the STM32F0xx Low Power Modes:
00009   *           - Sleep Mode
00010   *           - STOP mode with RTC
00011   *           - STANDBY mode without RTC
00012   *           - STANDBY mode with RTC
00013   ******************************************************************************
00014   * @attention
00015   *
00016   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00017   *
00018   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00019   * You may not use this file except in compliance with the License.
00020   * You may obtain a copy of the License at:
00021   *
00022   *        http://www.st.com/software_license_agreement_liberty_v2
00023   *
00024   * Unless required by applicable law or agreed to in writing, software 
00025   * distributed under the License is distributed on an "AS IS" BASIS, 
00026   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00027   * See the License for the specific language governing permissions and
00028   * limitations under the License.
00029   *
00030   ******************************************************************************
00031   */ 
00032 
00033 /* Includes ------------------------------------------------------------------*/
00034 #include "stm32f0xx_lp_modes.h"
00035 
00036 /** @addtogroup STM32F0xx_StdPeriph_Examples
00037   * @{
00038   */
00039 
00040 /** @addtogroup PWR_CurrentConsumption
00041   * @{
00042   */ 
00043 
00044 /* Private typedef -----------------------------------------------------------*/
00045 /* Private define ------------------------------------------------------------*/
00046 /* Private macro -------------------------------------------------------------*/
00047 /* Private variables ---------------------------------------------------------*/
00048 /* Private function prototypes -----------------------------------------------*/
00049 /* Private functions ---------------------------------------------------------*/
00050 
00051 /**
00052   * @brief  This function configures the system to enter Sleep mode for
00053   *         current consumption measurement purpose.
00054   *         Sleep Mode
00055   *         ==========  
00056   *            - System Running at PLL (48MHz)
00057   *            - Flash 1 wait state
00058   *            - Prefetch and Cache enabled
00059   *            - Code running from Internal FLASH
00060   *            - All peripherals disabled.
00061   *            - Wakeup using EXTI Line (KEY Button PB.08)
00062   * @param  None
00063   * @retval None
00064   */
00065 void SleepMode_Measure(void)
00066 {
00067   __IO uint32_t index = 0;
00068   GPIO_InitTypeDef GPIO_InitStructure;
00069 
00070   /* Configure all GPIO as analog to reduce current consumption on non used IOs */
00071   /* Enable GPIOs clock */
00072   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC |
00073                         RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOF , ENABLE);
00074 #ifdef USE_STM32072B_EVAL
00075   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE , ENABLE);
00076 #endif /* USE_STM32072B_EVAL */
00077   
00078   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00079   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00080   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00081   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
00082   GPIO_Init(GPIOC, &GPIO_InitStructure);
00083   GPIO_Init(GPIOD, &GPIO_InitStructure);
00084   GPIO_Init(GPIOF, &GPIO_InitStructure);
00085   GPIO_Init(GPIOA, &GPIO_InitStructure); 
00086   GPIO_Init(GPIOB, &GPIO_InitStructure);
00087 #ifdef USE_STM32072B_EVAL
00088   GPIO_Init(GPIOE, &GPIO_InitStructure);
00089 #endif /* USE_STM32072B_EVAL */
00090 
00091   /* Disable GPIOs clock */
00092   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC |
00093                          RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOF, DISABLE);
00094 #ifdef USE_STM32072B_EVAL
00095   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE , DISABLE);
00096 #endif /* USE_STM32072B_EVAL */
00097 
00098   /* Configure Joystick SEL Button */
00099   STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI);
00100 
00101   /* Request to enter SLEEP mode */
00102   __WFI();
00103 
00104   /* Initialize LED4 on EVAL board */
00105   STM_EVAL_LEDInit(LED4);
00106 
00107   /* Infinite loop */
00108   while (1)
00109   {
00110     /* Toggle The LED4 */
00111     STM_EVAL_LEDToggle(LED4);
00112 
00113     /* Inserted Delay */
00114     for(index = 0; index < 0x7FFFF; index++);
00115   }
00116 }
00117 
00118 /**
00119   * @brief  This function configures the system to enter Stop mode with RTC 
00120   *         clocked by LSI for current consumption measurement purpose.
00121   *         STOP Mode with RTC clocked by LSI
00122   *         =====================================   
00123   *           - RTC Clocked by LSI
00124   *           - Regulator in LP mode
00125   *           - HSI, HSE OFF and LSI OFF if not used as RTC Clock source
00126   *           - No IWDG
00127   *           - FLASH in deep power down mode
00128   *           - Automatic Wakeup using RTC clocked by LSI (~5s)
00129   * @param  None
00130   * @retval None
00131   */
00132 void StopMode_Measure(void)
00133 {
00134   __IO uint32_t index = 0;
00135   GPIO_InitTypeDef GPIO_InitStructure;
00136   NVIC_InitTypeDef  NVIC_InitStructure;
00137   EXTI_InitTypeDef  EXTI_InitStructure;
00138   RTC_InitTypeDef   RTC_InitStructure;
00139   RTC_TimeTypeDef   RTC_TimeStructure;
00140   RTC_AlarmTypeDef  RTC_AlarmStructure;
00141   
00142   /* Allow access to RTC */
00143   PWR_BackupAccessCmd(ENABLE);
00144 
00145 /* The RTC Clock may varies due to LSI frequency dispersion. */   
00146   /* Enable the LSI OSC */ 
00147   RCC_LSICmd(ENABLE);
00148 
00149   /* Wait till LSI is ready */  
00150   while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
00151   {
00152   }
00153 
00154   /* Select the RTC Clock Source */
00155   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
00156 
00157   /* Enable the RTC Clock */
00158   RCC_RTCCLKCmd(ENABLE);
00159 
00160   /* Wait for RTC APB registers synchronisation */
00161   RTC_WaitForSynchro();
00162 
00163   /* Configure all GPIO as analog to reduce current consumption on non used IOs */
00164   /* Enable GPIOs clock */
00165   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC |
00166                          RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOF , ENABLE);
00167 #ifdef USE_STM32072B_EVAL
00168   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE , ENABLE);
00169 #endif /* USE_STM32072B_EVAL */
00170 
00171   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00172   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00173   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00174   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
00175   GPIO_Init(GPIOC, &GPIO_InitStructure);
00176   GPIO_Init(GPIOD, &GPIO_InitStructure);
00177   GPIO_Init(GPIOF, &GPIO_InitStructure);
00178   GPIO_Init(GPIOA, &GPIO_InitStructure);
00179   GPIO_Init(GPIOB, &GPIO_InitStructure);
00180 #ifdef USE_STM32072B_EVAL
00181   GPIO_Init(GPIOE, &GPIO_InitStructure);
00182 #endif /* USE_STM32072B_EVAL */
00183 
00184   /* Disable GPIOs clock */
00185   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA |RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC |
00186                          RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOF, DISABLE);
00187 #ifdef USE_STM32072B_EVAL
00188   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE , DISABLE);
00189 #endif /* USE_STM32072B_EVAL */
00190   
00191   RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
00192   RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
00193   RTC_InitStructure.RTC_SynchPrediv = 0x0138;
00194   
00195   if (RTC_Init(&RTC_InitStructure) == ERROR)
00196   {
00197     while(1);
00198   }
00199     
00200   /* EXTI configuration */
00201   EXTI_ClearITPendingBit(EXTI_Line17);
00202   EXTI_InitStructure.EXTI_Line = EXTI_Line17;
00203   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
00204   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00205   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00206   EXTI_Init(&EXTI_InitStructure);
00207   
00208   /* NVIC configuration */
00209   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
00210   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00211   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00212   NVIC_Init(&NVIC_InitStructure);
00213   
00214   /* Set the alarm X+5s */
00215   RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_H12_AM;
00216   RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = 0x01;
00217   RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x00;
00218   RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x05;
00219   RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
00220   RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
00221   RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
00222   RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
00223   
00224   /* Enable the alarm */
00225   RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
00226   
00227   /* Enable the RTC Alarm A interrupt */
00228   RTC_ITConfig(RTC_IT_ALRA, ENABLE);
00229   
00230   /* Set the time to 01h 00mn 00s AM */
00231   RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
00232   RTC_TimeStructure.RTC_Hours   = 0x01;
00233   RTC_TimeStructure.RTC_Minutes = 0x00;
00234   RTC_TimeStructure.RTC_Seconds = 0x00;  
00235   
00236   RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
00237   
00238   /* Clear the Alarm A Pending Bit */
00239   RTC_ClearITPendingBit(RTC_IT_ALRA);  
00240 
00241   /* Enter Stop Mode */
00242   PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
00243 
00244   /* Initialize LED4 on EVAL board */
00245   STM_EVAL_LEDInit(LED4);
00246 
00247   /* Infinite loop */
00248   while (1)
00249   {
00250     /* Toggle The LED4 */
00251     STM_EVAL_LEDToggle(LED4);
00252 
00253     /* Inserted Delay */
00254     for(index = 0; index < 0x5FFFF; index++);
00255   }
00256 }
00257 
00258 /**
00259   * @brief  This function configures the system to enter Standby mode for
00260   *         current consumption measurement purpose.
00261   *         STANDBY Mode
00262   *         ============
00263   *           - RTC OFF
00264   *           - IWDG and LSI OFF
00265   *           - Wakeup using WakeUp Pin2 (PC.13)
00266   * @param  None
00267   * @retval None
00268   */
00269 void StandbyMode_Measure(void)
00270 {
00271   /* Disable wake-up source(Wake up pin) to guarantee free access to WUT level-OR input */ 
00272   PWR_WakeUpPinCmd(PWR_WakeUpPin_1, DISABLE);  
00273   
00274   /* Clear Standby flag */
00275   PWR_ClearFlag(PWR_FLAG_SB);
00276     
00277   /* Clear Power Wake-up (CWUF) flag */
00278   PWR_ClearFlag(PWR_FLAG_WU);
00279 
00280   /* Enable WKUP pin 1 */
00281   PWR_WakeUpPinCmd(PWR_WakeUpPin_1,ENABLE);
00282   
00283   /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
00284   PWR_EnterSTANDBYMode();
00285   
00286   /* Infinite loop */
00287   while (1)
00288   {
00289   }
00290 }
00291 
00292 /**
00293   * @brief  This function configures the system to enter Standby mode with RTC 
00294   *         clocked by LSI for current consumption measurement purpose.
00295   *         STANDBY Mode with RTC clocked by LSI
00296   *         ========================================
00297   *           - RTC Clocked by LSI
00298   *           - IWDG OFF
00299   *           - Automatic Wakeup using RTC 
00300   * @param  None
00301   * @retval None
00302   */
00303 void StandbyRTCMode_Measure(void)
00304 {
00305   RTC_InitTypeDef   RTC_InitStructure;
00306   RTC_AlarmTypeDef  RTC_AlarmStructure;
00307   RTC_TimeTypeDef   RTC_TimeStructure;
00308   
00309   /* Allow access to RTC */
00310   PWR_BackupAccessCmd(ENABLE);
00311   
00312   /* The RTC Clock may varies due to LSI frequency dispersion. */   
00313   /* Enable the LSI OSC */ 
00314   RCC_LSICmd(ENABLE);
00315   
00316   /* Wait till LSI is ready */  
00317   while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
00318   {
00319   }
00320   
00321   /* Select the RTC Clock Source */
00322   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
00323   
00324   /* Enable the RTC Clock */
00325   RCC_RTCCLKCmd(ENABLE);
00326   
00327   /* Wait for RTC APB registers synchronisation */
00328   RTC_WaitForSynchro();
00329   
00330   RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
00331   RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
00332   RTC_InitStructure.RTC_SynchPrediv = 0x0138;
00333   
00334   RTC_Init(&RTC_InitStructure);
00335   
00336   /* Set the alarm X+5s */
00337   RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_H12_AM;
00338   RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = 0x01;
00339   RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x00;
00340   RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x08;
00341   RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
00342   RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
00343   RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
00344   RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
00345     
00346   /* Enable the alarm */
00347   RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
00348   
00349   /* Set the time to 01h 00mn 00s AM */
00350   RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
00351   RTC_TimeStructure.RTC_Hours   = 0x01;
00352   RTC_TimeStructure.RTC_Minutes = 0x00;
00353   RTC_TimeStructure.RTC_Seconds = 0x00;  
00354   
00355   RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
00356   
00357   /* Disable RTC Alarm A Interrupt */
00358   RTC_ITConfig(RTC_IT_ALRA, DISABLE);   
00359   
00360   /* Clear Power Wake-up (CWUF) flag */
00361   PWR_ClearFlag(PWR_FLAG_WU);  
00362   
00363   /* Enable RTC Alarm A Interrupt */
00364   RTC_ITConfig(RTC_IT_ALRA, ENABLE);  
00365   
00366   RTC_ClearFlag(RTC_FLAG_ALRAF);
00367   
00368   /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
00369   PWR_EnterSTANDBYMode();
00370   
00371   /* Infinite loop */
00372   while (1)
00373   {
00374   }
00375 }
00376 
00377 /**
00378   * @}
00379   */ 
00380 
00381 /**
00382   * @}
00383   */ 
00384 
00385 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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