XMEGA Application Note


xmega_sleep_example.c File Reference

XMEGA sleep manager example main program file. More...

#include "avr_compiler.h"
#include "ccpwrite.h"
#include "rtc_driver.h"
#include "sleepmgr.h"
#include "lowpower.h"

Include dependency graph for xmega_sleep_example.c:

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   5
 Configure RTC wakeup period in seconds. (Approximate if ULP is used..).
#define SLEEP_MODE   SLEEPMGR_SAVE
#define USE_RTC

Functions

 ISR (RTC_COMP_vect)
int main (void)
 The main RTC example.


Detailed Description

XMEGA sleep manager example main program file.

This file contains an example program which initializes the XMEGA to the least power consuming state, and puts the device to sleep.
By default, the XMEGA is put in POWER-SAVE mode, and wakes up every five seconds. The device stays in ACTIVE mode for 0.5 sec before going back to sleep. This gives a duty cycle of 10%.

The interval timing is done with the RTC and ULP as default clock source, which should result in approximately 1 ľA current consumption.

Note:
The device is initialized with the LOWPOWER_Init(), which enables pull-up on all I/O pins and disables the JTAG-interface (if configured in lowpower.h). This results in the lowest current consumption if nothing is connected to the I/O pins.
Application note:
AVR1010: Minimizing the power consumption of XMEGA devices
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
2940
Date
2009-11-02 09:55:56 +0100 (ma, 02 nov 2009)

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_sleep_example.c.


Define Documentation

#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 101 of file xmega_sleep_example.c.

#define RTC_PERIOD   5

Configure RTC wakeup period in seconds. (Approximate if ULP is used..).

Definition at line 94 of file xmega_sleep_example.c.

#define SLEEP_MODE   SLEEPMGR_SAVE

Specify which sleep mode to use.

The default alternatives are:

 SLEEPMGR_IDLE - Idle
 SLEEPMGR_ESTDBY - Extended standby
 SLEEPMGR_SAVE - Power-save
 SLEEPMGR_STDBY - Standby
 SLEEPMGR_DOWN - Power-down

Note:
If this is not defined, the sleep manager defaults to Power-down.

The device cannot wake itself up from Standby or Power-down because the RTC is disabled in these modes.

Definition at line 83 of file xmega_sleep_example.c.

#define USE_RTC

Configure if RTC should be used. (Comment out otherwise.)

This wakes the device up at the specified number of seconds.

Note:
The RTC is disabled in Standby and Power-down!

Definition at line 92 of file xmega_sleep_example.c.


Function Documentation

ISR ( RTC_COMP_vect   ) 

Definition at line 183 of file xmega_sleep_example.c.

00184 {
00185 }

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 as configured above.
The main loop doesn't do anything but put the device back to the configured sleep mode after a delay of 0.5 second.

Definition at line 112 of file xmega_sleep_example.c.

References CCPWrite(), LOWPOWER_Init(), RTC_Busy, RTC_Initialize(), RTC_PERIOD, RTC_SetCompareIntLevel(), SLEEP_MODE, SLEEPMGR_Init(), SLEEPMGR_Lock(), and SLEEPMGR_Sleep().

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 }

Here is the call graph for this function:

@DOC_TITLE@
Generated on Mon Nov 9 13:44:41 2009 for XMEGA power consumption evaluation code by doxygen 1.5.9