XMEGA Application Note


xmega_sleep_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00062 #include "avr_compiler.h"
00063 #include "ccpwrite.h"
00064 #include "rtc_driver.h"
00065 #include "sleepmgr.h"
00066 #include "lowpower.h"
00067 
00068 
00083 #define SLEEP_MODE SLEEPMGR_SAVE
00084 
00085 
00092 #define USE_RTC
00094 #define RTC_PERIOD      5
00095 
00097 // #define RTC_XTAL
00098 
00100 #ifndef F_CPU
00101 #define F_CPU 2000000
00102 #endif // F_CPU
00103 
00104 
00112 int main(void)
00113 {
00114     // Initialize to the least power consuming state.
00115     LOWPOWER_Init();
00116     
00117     // Enable EEPROM and Flash power reduction mode.
00118     CCPWrite(&NVM.CTRLB, NVM_EPRM_bm | NVM_FPRM_bm);
00119 
00120     // Initialize the sleep manager, lock a sleep mode if configured.
00121     SLEEPMGR_Init();
00122 #ifdef SLEEP_MODE
00123     SLEEPMGR_Lock( SLEEP_MODE );
00124 #endif // SLEEP_MODE    
00125         
00126         
00127         // If use of the RTC as interval timer is configured, set it up.
00128 #ifdef USE_RTC
00129     // Clear bit for RTC in PRR (set by LOWPOWER_Init()).
00130     PR.PRGEN &= ~PR_RTC_bm;
00131 
00132 #ifndef RTC_XTAL
00133     // Set internal 32kHz ULP oscillator as clock source for RTC.
00134     CLK.RTCCTRL = CLK_RTCSRC_ULP_gc | CLK_RTCEN_bm;
00135 #else
00136     // Enable external 32 kHz XTAL oscillator in low-power mode.
00137     OSC.XOSCCTRL = OSC_XOSCSEL_32KHz_gc | OSC_X32KLPM_bm;
00138     OSC.CTRL |= OSC_XOSCEN_bm;
00139 
00140     // Wait for oscillator to stabilize.
00141     do { } while (!( OSC.STATUS & OSC_XOSCRDY_bm ));
00142     
00143     // Set the oscillator as clock source for RTC.
00144     CLK.RTCCTRL = CLK_RTCSRC_TOSC_gc | CLK_RTCEN_bm;
00145 #endif // RTC_XTAL
00146 
00147     // Wait until RTC is ready.
00148     do { } while ( RTC_Busy() );
00149 
00150     // Configure RTC wakeup period.
00151     RTC_Initialize( RTC_PERIOD, 0, 0, RTC_PRESCALER_DIV1024_gc );
00152     
00153     // Enable the RTC compare interrupts so that the device can wake up.
00154     RTC_SetCompareIntLevel( RTC_COMPINTLVL_LO_gc );
00155     PMIC.CTRL |= PMIC_LOLVLEN_bm;
00156     sei();
00157 #endif // USE_RTC
00158 
00159     // Main loop.
00160     do {
00161         /* On wake-up, stay in ACTIVE mode for 0.5 s and then go back to sleep.
00162          *
00163          * In an actual application, you would process events/data here.
00164          */
00165         delay_us(500000);
00166         
00167         // Due to errata, disable Flash power reduction before sleep.
00168         CCPWrite(&NVM.CTRLB, NVM_EPRM_bm);
00169 
00170         SLEEPMGR_Sleep();
00171         
00172         // Re-enable EEPROM and Flash power reduction mode after sleep.
00173         CCPWrite(&NVM.CTRLB, NVM_EPRM_bm | NVM_FPRM_bm);
00174     } while (1);
00175 }
00176 
00177 
00178 /* RTC compare ISR
00179  *
00180  * The RTC is only used to wake the device up at intervals, so the ISR
00181  * does not need to do anything.
00182  */
00183 ISR(RTC_COMP_vect)
00184 {
00185 }
@DOC_TITLE@
Generated on Mon Nov 9 13:44:26 2009 for XMEGA power consumption evaluation code by doxygen 1.5.9