ArduinoLibs
PowerSave.cpp
00001 /*
00002  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020  * DEALINGS IN THE SOFTWARE.
00021  */
00022 
00023 #include "PowerSave.h"
00024 #include <avr/wdt.h>
00025 #include <avr/sleep.h>
00026 #include <avr/power.h>
00027 #include <avr/interrupt.h>
00028 
00036 /*\@{*/
00037 
00048 ISR(WDT_vect)
00049 {
00050     wdt_disable();
00051 }
00132 void sleepFor(SleepDuration duration, uint8_t mode)
00133 {
00134     // Turn off the analog to digital converter.
00135     ADCSRA &= ~(1 << ADEN);
00136     power_adc_disable();
00137 
00138     // Turn on the watchdog timer for the desired duration.
00139     wdt_enable(duration);
00140     WDTCSR |= (1 << WDIE);
00141 
00142     // Put the device to sleep, including turning off the Brown Out Detector.
00143     set_sleep_mode(mode);
00144     cli();
00145     sleep_enable();
00146 #if defined(sleep_bod_disable)
00147     sleep_bod_disable();
00148 #endif
00149     sei();
00150     sleep_cpu();
00151     sleep_disable();
00152     sei();
00153 
00154     // Turn the analog to digital converter back on.
00155     power_adc_enable();
00156     ADCSRA |= (1 << ADEN);
00157 }
00158 
00159 /*\@}*/
 All Classes Files Functions Variables Typedefs Enumerations Enumerator