XMEGA Application Note


xplain_power_consumption.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00064 #include "avr_compiler.h"
00065 #include "ccpwrite.h"
00066 #include "sleepmgr.h"
00067 #include "lowpower.h"
00068 #include "rtc_driver.h"
00069 
00071 #define RTC_PERIOD      8
00072 
00074 #ifndef F_CPU
00075 #define F_CPU 2000000
00076 #endif // F_CPU
00077 
00078 
00080 uint8_t gState = 0;
00081 
00082 
00090 int main(void)
00091 {
00092     // Initialize XMEGA to the least power consuming state on the Xplain board.
00093     LOWPOWER_Init();
00094     
00095     // Enable EEPROM and Flash power reduction mode.
00096     CCPWrite(&NVM.CTRLB, NVM_EPRM_bm | NVM_FPRM_bm);
00097     
00098     // Enable pull-down on PORTQ3.
00099     PORTQ.PIN3CTRL = PORT_OPC_PULLDOWN_gc;
00100     // Set PORTB1 as input w/ buffer disabled.
00101     PORTB.PIN1CTRL = PORT_ISC_INPUT_DISABLE_gc;
00102 
00103     // Initialize the sleep manager.
00104     SLEEPMGR_Init();
00105 
00106     // Clear bit for RTC in PRR (it is set by LOWPOWER_Init()).
00107     PR.PRGEN &= ~PR_RTC_bm;
00108 
00109     // Enable external 32 kHz XTAL oscillator in low-power mode for RTC.
00110     OSC.XOSCCTRL = OSC_XOSCSEL_32KHz_gc | OSC_X32KLPM_bm;
00111     OSC.CTRL |= OSC_XOSCEN_bm;
00112 
00113     // Wait for oscillator to stabilize.
00114     do { } while (!( OSC.STATUS & OSC_XOSCRDY_bm ));
00115     
00116     // Set the oscillator as clock source for RTC.
00117     CLK.RTCCTRL = CLK_RTCSRC_TOSC_gc | CLK_RTCEN_bm;
00118 
00119     // Wait until RTC is ready.
00120     do { } while ( RTC_Busy() );
00121 
00122     // Configure RTC wakeup period.
00123     RTC_Initialize( RTC_PERIOD, 0, RTC_PERIOD-1, RTC_PRESCALER_DIV1024_gc );
00124     
00125     // Enable RTC compare interrupts.
00126     RTC_SetCompareIntLevel( RTC_COMPINTLVL_LO_gc );
00127     PMIC.CTRL |= PMIC_LOLVLEN_bm;
00128     sei();
00129 
00130     // The device should first spend time in ACTIVE, so wait for
00131     // the RTC compare ISR to change the state.
00132     do { } while(gState == 0);
00133     
00134     // Disable Flash power reduction mode due to errata.
00135     // (The device will spend most of its time in sleep from now on, so we
00136     // won't bother clearing/setting FPRM before and after sleep.)
00137     CCPWrite(&NVM.CTRLB, NVM_EPRM_bm);
00138     
00139     // Go to sleep. The RTC compare ISR configures the sleep modes.
00140     do {
00141         SLEEPMGR_Sleep();
00142     } while (1);
00143 }
00144 
00145 
00151 ISR(RTC_COMP_vect)
00152 {
00153     switch(gState) {
00154         // The device starts out in active mode. Go to Idle.
00155         case 0:
00156             SLEEPMGR_Lock( SLEEPMGR_IDLE );
00157             ++gState;
00158             break;
00159                 
00160         // Power-save follows after Idle.
00161         case 1:
00162             SLEEPMGR_Unlock( SLEEPMGR_IDLE );
00163             SLEEPMGR_Lock( SLEEPMGR_SAVE );
00164             ++gState;
00165             break;
00166                 
00167         // Power-down follows after Power-save. The device won't wake up again.
00168         case 2:
00169             SLEEPMGR_Unlock( SLEEPMGR_SAVE );
00170             SLEEPMGR_Lock( SLEEPMGR_DOWN );
00171             ++gState;
00172             break;
00173         
00174         // Shouldn't end up here.. Go to Idle.
00175         default:
00176             SLEEPMGR_Init();
00177             SLEEPMGR_Lock( SLEEPMGR_IDLE );
00178             gState = 1;
00179     }
00180 }
@DOC_TITLE@
Generated on Mon Nov 9 13:44:26 2009 for XMEGA power consumption evaluation code by doxygen 1.5.9