Move master branch from from mios git repo
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
Name : Sleep_n0m1 Library
|
||||
Author : Noah Shibley, Michael Grant, NoMi Design Ltd. http://n0m1.com
|
||||
Date : July 10th 2011
|
||||
Version : 0.1
|
||||
Notes : Arduino Library to place the arduino into sleep mode for
|
||||
a specific length of time, or a specific number of sleep cycles
|
||||
Some of this code comes from "Cloudy" on the arduino forum
|
||||
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292898715
|
||||
|
||||
Refer to the examples for how to use this library.
|
||||
|
||||
Dependencies:
|
||||
none
|
||||
|
||||
|
||||
|
||||
List of Functions:
|
||||
|
||||
Function: idleMode
|
||||
Description: sets the Arduino into idle Mode sleep,
|
||||
the least power saving, The idle mode stops the MCU
|
||||
but leaves peripherals and timers running.
|
||||
------------------------------------------------------
|
||||
Function: adcMode
|
||||
Description: sets the Arduino into adc Mode sleep,
|
||||
This mode makes the MCU enter ADC Noise Reduction mode,
|
||||
stopping the CPU but allowing the ADC, the external interrupts,
|
||||
the 2-wire Serial Interface address watch, Timer/Counter2
|
||||
and the Watchdog to continue operating
|
||||
------------------------------------------------------
|
||||
Function: pwrSaveMode
|
||||
Description: sets the Arduino into power Save Mode sleep,
|
||||
The timer crystal will continue to operate in this mode,
|
||||
Timer2 still active.
|
||||
------------------------------------------------------
|
||||
Function: extStandbyMode
|
||||
Description: sets the Arduino into extStandby Mode sleep,
|
||||
This mode is identical to Power-save with the exception
|
||||
that the Oscillator is kept running for fast wake up
|
||||
------------------------------------------------------
|
||||
Function: standbyMode
|
||||
Description: sets the Arduino into standby Mode sleep,
|
||||
This mode is identical to Power-down with the exception
|
||||
that the Oscillator is kept running for fast wake up
|
||||
------------------------------------------------------
|
||||
Function: pwrDownMode
|
||||
Description: sets the Arduino into power Down Mode sleep,
|
||||
The most power saving, all systems are powered down
|
||||
except the watch dog timer and external reset
|
||||
------------------------------------------------------
|
||||
Function: sleepDelay
|
||||
Description: Works like the Arduino delay function, sets the
|
||||
Arduino into sleep mode for a specified time.
|
||||
Parameters: (unsigned long) time in ms of the sleep cycle
|
||||
------------------------------------------------------
|
||||
Function: sleepDelay
|
||||
Description: Works like the Arduino delay function, sets the
|
||||
Arduino into sleep mode for a specified time.
|
||||
Parameters: (unsigned long) time in ms of the sleep cycle
|
||||
(boolean) prevents the Arduino from entering sleep
|
||||
------------------------------------------------------
|
||||
Function: setCalibrationInterval
|
||||
Description: the WDT needs to be calibrated against timer 0
|
||||
periodically to keep the sleep time accurate. Default calibration
|
||||
occurs every 100 wake/sleep cycles. recalibrate too often will
|
||||
waste power and too rarely will make the sleep time inaccurate.
|
||||
Parameters: (int) set the # of wake/sleep cycles between calibrations
|
||||
------------------------------------------------------
|
||||
Function: sleepInterrupt
|
||||
Description: set the Arduino into sleep mode until an interrupt is
|
||||
triggered. The interrupts are passed in as parameters
|
||||
Parameters: (int) interrupt value, 0, 1, etc, see attachinterrupt()
|
||||
(int) mode of trigger, LOW,RISING,FALLING,CHANGE
|
||||
@@ -0,0 +1,243 @@
|
||||
/************************************************************************************
|
||||
*
|
||||
* Name : Sleep_n0m1.cpp
|
||||
* Author : Noah Shibley / NoMi Design
|
||||
* Date : July 10th 2011
|
||||
* Version : 0.1
|
||||
* Notes : Some of this code comes from "Cloudy" on the arduino forum
|
||||
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292898715
|
||||
*
|
||||
* Copyright (c) 2012 NoMi Design (http://n0m1.com) All right reserved.
|
||||
*
|
||||
* This file is part of Triggertrap. See Triggertrap.com for more information.
|
||||
*
|
||||
* Triggertrap is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Triggertrap is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Triggertrap. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
***********************************************************************************/
|
||||
|
||||
#include "Sleep_n0m1.h"
|
||||
|
||||
Sleep* Sleep::pSleep = 0;
|
||||
|
||||
Sleep::Sleep()
|
||||
{
|
||||
pSleep = this; //the ptr points to this object
|
||||
timeSleep = 0; // total time due to sleep
|
||||
calibv = 1.0; // ratio of real clock with WDT clock
|
||||
byte isrcalled = 0; // WDT vector flag
|
||||
sleepCycleCount = 0;
|
||||
sleepCycleInterval = 100;
|
||||
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* setSleepMode
|
||||
*
|
||||
********************************************************************/
|
||||
void Sleep::setSleepMode(int mode)
|
||||
{
|
||||
sleepMode_ = mode;
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* calibrateTime
|
||||
*
|
||||
********************************************************************/
|
||||
void Sleep::calibrateTime(unsigned long sleepTime, boolean &abortCycle) {
|
||||
// timer0 continues to run in idle sleep mode
|
||||
set_sleep_mode(SLEEP_MODE_IDLE);
|
||||
long tt1=millis();
|
||||
sleepWDT(sleepTime,abortCycle);
|
||||
long tt2=millis();
|
||||
|
||||
calibv = (float) sleepTime/(tt2-tt1);
|
||||
|
||||
//Serial.println(calibv);
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* WDTMillis
|
||||
*
|
||||
********************************************************************/
|
||||
unsigned long Sleep::WDTMillis() {
|
||||
return millis()+timeSleep;
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* sleepNow
|
||||
*
|
||||
********************************************************************/
|
||||
void Sleep::sleepInterrupt(int interrupt,int mode) {
|
||||
|
||||
if(mode == FALLING || mode == LOW)
|
||||
{
|
||||
int pin = interrupt + 2; //will fail on the mega
|
||||
pinMode (pin, INPUT);
|
||||
digitalWrite (pin, HIGH);
|
||||
}
|
||||
|
||||
set_sleep_mode(sleepMode_);
|
||||
sleep_enable();
|
||||
attachInterrupt(interrupt,sleepHandler,mode);
|
||||
sei(); //make sure interrupts are on!
|
||||
sleep_mode();
|
||||
//----------------------------- ZZZZZZ sleeping here----------------------
|
||||
sleep_disable(); //disable sleep, awake now
|
||||
detachInterrupt(interrupt);
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* sleepDelay
|
||||
*
|
||||
********************************************************************/
|
||||
void Sleep::sleepDelay(unsigned long sleepTime){
|
||||
|
||||
boolean abortCycle = false;
|
||||
|
||||
sleepDelay(sleepTime,abortCycle);
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* sleepDelay
|
||||
*
|
||||
********************************************************************/
|
||||
void Sleep::sleepDelay(unsigned long sleepTime, boolean &abortCycle) {
|
||||
ADCSRA &= ~(1<<ADEN); // adc off
|
||||
// PRR = 0xEF; // modules off
|
||||
|
||||
++sleepCycleCount;
|
||||
sleepCycleCount = sleepCycleCount % sleepCycleInterval; //recalibrate every interval cycles
|
||||
if(sleepCycleCount == 1)
|
||||
{
|
||||
calibrateTime(sleepTime,abortCycle);
|
||||
}
|
||||
else
|
||||
{
|
||||
set_sleep_mode(sleepMode_);
|
||||
int trem = sleepWDT(sleepTime*calibv,abortCycle);
|
||||
timeSleep += (sleepTime-trem);
|
||||
}
|
||||
// PRR = 0x00; //modules on
|
||||
ADCSRA |= (1<<ADEN); // adc on
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* sleepWDT
|
||||
*
|
||||
********************************************************************/
|
||||
int Sleep::sleepWDT(unsigned long remainTime, boolean &abortCycle) {
|
||||
|
||||
#if defined(WDP3)
|
||||
byte WDTps = 9; // WDT Prescaler value, 9 = 8192ms
|
||||
#else
|
||||
byte WDTps = 7; // WDT Prescaler value, 7 = 2048ms
|
||||
#endif
|
||||
|
||||
isrcalled = 0;
|
||||
sleep_enable();
|
||||
while(remainTime > 0) {
|
||||
//work out next prescale unit to use
|
||||
while ((0x10<<WDTps) > remainTime && WDTps > 0) {
|
||||
WDTps--;
|
||||
}
|
||||
// send prescaler mask to WDT_On
|
||||
WDT_On((WDTps & 0x08 ? (1<<WDP3) : 0x00) | (WDTps & 0x07));
|
||||
isrcalled=0;
|
||||
while (isrcalled==0 && abortCycle == false) {
|
||||
|
||||
#if defined(__AVR_ATmega328P__)
|
||||
// turn bod off
|
||||
MCUCR |= (1<<BODS) | (1<<BODSE);
|
||||
MCUCR &= ~(1<<BODSE); // must be done right before sleep
|
||||
#endif
|
||||
sleep_cpu(); // sleep here
|
||||
}
|
||||
// calculate remaining time
|
||||
remainTime -= (0x10<<WDTps);
|
||||
if ((long) remainTime < 0 ) {remainTime = 0;} //check for unsigned underflow, by converting to signed
|
||||
|
||||
}
|
||||
sleep_disable();
|
||||
return remainTime;
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* WDT_On
|
||||
*
|
||||
********************************************************************/
|
||||
void Sleep::WDT_On(byte psMask)
|
||||
{
|
||||
// prepare timed sequence first
|
||||
byte ps = (psMask | (1<<WDIE)) & ~(1<<WDE);
|
||||
cli();
|
||||
wdt_reset();
|
||||
/* Clear WDRF in MCUSR */
|
||||
MCUSR &= ~(1<<WDRF);
|
||||
// start timed sequence
|
||||
WDTCSR |= (1<<WDCE) | (1<<WDE);
|
||||
// set new watchdog timeout value
|
||||
WDTCSR = ps;
|
||||
sei();
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* WDT_Off
|
||||
*
|
||||
********************************************************************/
|
||||
void Sleep::WDT_Off() {
|
||||
cli();
|
||||
wdt_reset();
|
||||
/* Clear WDRF in MCUSR */
|
||||
MCUSR &= ~(1<<WDRF);
|
||||
/* Write logical one to WDCE and WDE */
|
||||
/* Keep old prescaler setting to prevent unintentional time-out */
|
||||
WDTCSR |= (1<<WDCE) | (1<<WDE);
|
||||
/* Turn off WDT */
|
||||
WDTCSR = 0x00;
|
||||
sei();
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* sleepHandler ISR
|
||||
*
|
||||
********************************************************************/
|
||||
void sleepHandler(void)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
*
|
||||
* WDT ISR
|
||||
*
|
||||
********************************************************************/
|
||||
ISR(WDT_vect) {
|
||||
Sleep::pSleep->WDT_Off();
|
||||
Sleep::pSleep->isrcalled=1;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/************************************************************************************
|
||||
*
|
||||
* Name : Sleep_n0m1.h
|
||||
* Author : Noah Shibley / NoMi Design
|
||||
* Date : July 10th 2011
|
||||
* Version : 0.1
|
||||
* Notes : Some of this code comes from "Cloudy" on the arduino forum
|
||||
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292898715
|
||||
*
|
||||
* Copyright (c) 2012 NoMi Design (http://n0m1.com) All right reserved.
|
||||
*
|
||||
* This file is part of Triggertrap. See Triggertrap.com for more information.
|
||||
*
|
||||
* Triggertrap is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Triggertrap is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Triggertrap. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
***********************************************************************************/
|
||||
|
||||
#ifndef SLEEP_H
|
||||
#define SLEEP_H
|
||||
|
||||
|
||||
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/wdt.h>
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
extern "C" void WDT_vect(void) __attribute__ ((signal));
|
||||
extern "C" void sleepHandler(void) __attribute__ ((signal));
|
||||
|
||||
class Sleep {
|
||||
|
||||
public:
|
||||
|
||||
friend void WDT_vect(void);
|
||||
friend void sleepHandler(void);
|
||||
|
||||
Sleep();
|
||||
|
||||
/* modes of sleep
|
||||
SLEEP_MODE_IDLE
|
||||
SLEEP_MODE_ADC
|
||||
SLEEP_MODE_PWR_SAVE
|
||||
SLEEP_MODE_EXT_STANDBY
|
||||
SLEEP_MODE_STANDBY
|
||||
SLEEP_MODE_PWR_DOWN
|
||||
*/
|
||||
|
||||
void idleMode() { setSleepMode(SLEEP_MODE_IDLE);}
|
||||
void adcMode() {setSleepMode(SLEEP_MODE_ADC);}
|
||||
void pwrSaveMode() {setSleepMode(SLEEP_MODE_PWR_SAVE);}
|
||||
void extStandbyMode(){setSleepMode(SLEEP_MODE_EXT_STANDBY);}
|
||||
void standbyMode(){setSleepMode(SLEEP_MODE_STANDBY);}
|
||||
void pwrDownMode(){setSleepMode(SLEEP_MODE_PWR_DOWN);}
|
||||
|
||||
//WatchDog Sleep Functions: Sleep for a specfic length of time
|
||||
void sleepDelay(unsigned long sleepTime);
|
||||
void sleepDelay(unsigned long sleepTime,boolean &abortCycle);
|
||||
void setCalibrationInterval(int interval){ sleepCycleInterval = interval; }
|
||||
|
||||
//Interrupt Sleep Function: Sleep till something interrupts sleep
|
||||
void sleepInterrupt(int interrupt,int mode);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
int sleepMode_;
|
||||
unsigned long timeSleep;
|
||||
float calibv;
|
||||
volatile byte isrcalled;
|
||||
static Sleep* pSleep; //static ptr to Sleep class for the ISR
|
||||
int sleepCycleCount;
|
||||
int sleepCycleInterval;
|
||||
|
||||
void setSleepMode(int mode);
|
||||
void WDT_Off();
|
||||
void WDT_On(byte psMask);
|
||||
int sleepWDT(unsigned long remainTime,boolean &abortCycle);
|
||||
void calibrateTime(unsigned long sleepTime,boolean &abortCycle); //calibrate the time keeping difference between WDT and Timer0
|
||||
unsigned long WDTMillis(); // Estimated millis is real clock + calibrated sleep time
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <Sleep_n0m1.h>
|
||||
|
||||
Sleep sleep;
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
delay(100); ////delays are just for serial print, without serial they can be removed
|
||||
Serial.println("execute your code here");
|
||||
|
||||
Serial.print("Sleeping Till Interrupt");
|
||||
|
||||
delay(100); //delay to allow serial to fully print before sleep
|
||||
|
||||
sleep.pwrDownMode(); //set sleep mode
|
||||
sleep.sleepInterrupt(0,FALLING); //sleep for: sleepTime
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <Sleep_n0m1.h>
|
||||
|
||||
Sleep sleep;
|
||||
boolean abortSleep; //cancel sleep cycle
|
||||
int sleepCycleCount; //the number of times awake then asleep
|
||||
unsigned long sleepTime; //how long you want the arduino to sleep
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(9600);
|
||||
abortSleep = false; //can be used to cancel the sleep cycle
|
||||
sleepTime = 5000; //set sleep time in ms, max sleep time is 49.7 days
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(abortSleep)
|
||||
{
|
||||
Serial.println("sleep no more!");
|
||||
}
|
||||
else
|
||||
{
|
||||
delay(100);
|
||||
Serial.print("sleeping for ");
|
||||
Serial.println(sleepTime);
|
||||
delay(100); //delay to allow serial to fully print before sleep
|
||||
}
|
||||
|
||||
sleepCycleCount++;
|
||||
if(sleepCycleCount > 5)
|
||||
{
|
||||
abortSleep = true;
|
||||
}
|
||||
|
||||
sleep.pwrDownMode(); //set sleep mode
|
||||
sleep.sleepDelay(sleepTime,abortSleep); //sleep for: sleepTime
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <Sleep_n0m1.h>
|
||||
|
||||
int count = 0;
|
||||
Sleep sleep;
|
||||
unsigned long sleepTime; //how long you want the arduino to sleep
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(9600);
|
||||
sleepTime = 50000; //set sleep time in ms, max sleep time is 49.7 days
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(100); //delay to allow serial output to be ready after wake up
|
||||
Serial.print("Awake for ");
|
||||
Serial.print(count);
|
||||
Serial.println("sec");
|
||||
|
||||
count++;
|
||||
delay(1000); // waits for a second
|
||||
|
||||
if(count >= 3)
|
||||
{
|
||||
count = 0;
|
||||
Serial.print("sleeping ");
|
||||
Serial.println(sleepTime);
|
||||
delay(100); //delay to allow serial to fully print before sleep
|
||||
|
||||
sleep.pwrDownMode(); //set sleep mode
|
||||
sleep.sleepDelay(sleepTime); //sleep for: sleepTime
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <Sleep_n0m1.h>
|
||||
|
||||
Sleep sleep;
|
||||
unsigned long sleepTime; //how long you want the arduino to sleep
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(9600);
|
||||
sleepTime = 50000; //set sleep time in ms, max sleep time is 49.7 days
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
delay(100); ////delays are just for serial print, without serial they can be removed
|
||||
Serial.println("execute your code here");
|
||||
|
||||
Serial.print("sleeping for ");
|
||||
Serial.println(sleepTime);
|
||||
delay(100); //delay to allow serial to fully print before sleep
|
||||
|
||||
sleep.pwrDownMode(); //set sleep mode
|
||||
sleep.sleepDelay(sleepTime); //sleep for: sleepTime
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user