XMEGA Application Note | |||||
#include "avr_compiler.h"
#include "ccpwrite.h"
#include "sleepmgr.h"
#include "lowpower.h"
#include "rtc_driver.h"
Go to the source code of this file.
Defines | |
#define | F_CPU 2000000 |
Enable the 32 kHz XTAL oscillator for RTC. (Otherwise, use ULP.). | |
#define | RTC_PERIOD 8 |
Configure RTC wakeup period in seconds. (Approximate if ULP is used..). | |
Functions | |
ISR (RTC_COMP_vect) | |
int | main (void) |
The main RTC example. | |
Variables | |
uint8_t | gState = 0 |
Global variable to keep track of which state the program is in. |
This file contains a simple program for measuring the current consumption of XMEGA devices in different sleep modes.
The device cycles through ACTIVE, IDLE, POWER-SAVE and POWER-DOWN modes, spending 8 seconds in each mode.
Note that the device will stay in POWER-DOWN.
The interval timing is done with the RTC and ULP as default clock source, which should result in approximately 1 ľA current consumption.
Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file xmega_power_consumption.c.
#define F_CPU 2000000 |
Enable the 32 kHz XTAL oscillator for RTC. (Otherwise, use ULP.).
Define the CPU frequency, for use with delay_us(). (2 MHz is default clock.)
Definition at line 76 of file xmega_power_consumption.c.
#define RTC_PERIOD 8 |
Configure RTC wakeup period in seconds. (Approximate if ULP is used..).
Definition at line 69 of file xmega_power_consumption.c.
Referenced by main().
ISR | ( | RTC_COMP_vect | ) |
RTC compare ISR
This ISR simply updates gState so that the device cycles through the different sleep modes.
Definition at line 151 of file xmega_power_consumption.c.
References gState, SLEEPMGR_DOWN, SLEEPMGR_IDLE, SLEEPMGR_Init(), SLEEPMGR_Lock(), SLEEPMGR_SAVE, and SLEEPMGR_Unlock().
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 }
int main | ( | void | ) |
The main RTC example.
This function initializes the XMEGA to the least power consuming state, before initializing the sleep manager and RTC.
The function waits for gState to change (due to RTC compare ISR), after which an infinite loop is entered in which the device is put to sleep.
Definition at line 91 of file xmega_power_consumption.c.
References CCPWrite(), gState, LOWPOWER_Init(), RTC_Busy, RTC_Initialize(), RTC_PERIOD, RTC_SetCompareIntLevel(), SLEEPMGR_Init(), and SLEEPMGR_Sleep().
00092 { 00093 // Initialize device to the least power consuming state. 00094 LOWPOWER_Init(); 00095 00096 // Enable EEPROM and Flash power reduction mode. 00097 CCPWrite(&NVM.CTRLB, NVM_EPRM_bm | NVM_FPRM_bm); 00098 00099 // Initialize the sleep manager. 00100 SLEEPMGR_Init(); 00101 00102 // Clear bit for RTC in PRR because it is set by LOWPOWER_Init(). 00103 PR.PRGEN &= ~PR_RTC_bm; 00104 00105 // Use ULP as clock source for RTC if RTC_XTAL is not defined. 00106 #ifndef RTC_XTAL 00107 // Set internal 32kHz ULP oscillator as clock source for RTC. 00108 CLK.RTCCTRL = CLK_RTCSRC_ULP_gc | CLK_RTCEN_bm; 00109 #else 00110 // Enable external 32 kHz XTAL oscillator in low-power mode. 00111 OSC.XOSCCTRL = OSC_XOSCSEL_32KHz_gc | OSC_X32KLPM_bm; 00112 OSC.CTRL |= OSC_XOSCEN_bm; 00113 00114 // Wait for oscillator to stabilize before setting as RTC clock source. 00115 do { } while (!( OSC.STATUS & OSC_XOSCRDY_bm )); 00116 CLK.RTCCTRL = CLK_RTCSRC_TOSC_gc | CLK_RTCEN_bm; 00117 #endif // RTC_XTAL 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 }
uint8_t gState = 0 |
Global variable to keep track of which state the program is in.
Definition at line 81 of file xmega_power_consumption.c.
Generated on Mon Nov 9 13:44:38 2009 for XMEGA power consumption evaluation code by ![]() |