diff --git a/LowPower.cpp b/LowPower.cpp index 695429d..d6039ee 100644 --- a/LowPower.cpp +++ b/LowPower.cpp @@ -35,6 +35,8 @@ #include #include "LowPower.h" +static volatile int tookInterrupt; + // Only Pico Power devices can change BOD settings through software #if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) #ifndef sleep_bod_disable @@ -97,8 +99,19 @@ do { \ #endif #endif +// Some macros is still missing from AVR GCC distribution for ATmega2560 +#if defined __AVR_ATmega2560__ + #ifndef power_usart3_enable + #define power_usart3_enable() (PRR1 &= (uint8_t)~(1 << PRUSART3)) + #endif + + #ifndef power_usart3_disable + #define power_usart3_disable() (PRR1 |= (uint8_t)(1 << PRUSART3)) + #endif +#endif + /******************************************************************************* -* Name: idle +* Name: idleWait * Description: Putting ATmega328P/168 into idle state. Please make sure you * understand the implication and result of disabling module. * @@ -146,11 +159,15 @@ do { \ * (a) TWI_OFF - Turn off TWI module * (b) TWI_ON - Leave TWI module in its default state * +* 9. wait If true, stay asleep until watchdog goes off; if false, wake up +* on any enabled interrupt. +* *******************************************************************************/ #if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega1284P__) -void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2, +void LowPowerClass::idleWait(period_t period, adc_t adc, timer2_t timer2, timer1_t timer1, timer0_t timer0, - spi_t spi, usart0_t usart0, twi_t twi) + spi_t spi, usart0_t usart0, twi_t twi, + bool wait) { // Temporary clock source variable unsigned char clockSource = 0; @@ -187,7 +204,10 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2, WDTCSR |= (1 << WDIE); } - lowPowerBodOn(SLEEP_MODE_IDLE); + tookInterrupt = 0; + do { + lowPowerBodOn(SLEEP_MODE_IDLE); + } while(wait && !tookInterrupt); if (adc == ADC_OFF) { @@ -214,6 +234,65 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2, /******************************************************************************* * Name: idle +* Description: Putting ATmega328P/168 into idle state. Please make sure you +* understand the implication and result of disabling module. +* +* Argument Description +* ========= =========== +* 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake +* up resource: +* (a) SLEEP_15MS - 15 ms sleep +* (b) SLEEP_30MS - 30 ms sleep +* (c) SLEEP_60MS - 60 ms sleep +* (d) SLEEP_120MS - 120 ms sleep +* (e) SLEEP_250MS - 250 ms sleep +* (f) SLEEP_500MS - 500 ms sleep +* (g) SLEEP_1S - 1 s sleep +* (h) SLEEP_2S - 2 s sleep +* (i) SLEEP_4S - 4 s sleep +* (j) SLEEP_8S - 8 s sleep +* (k) SLEEP_FOREVER - Sleep without waking up through WDT +* +* 2. adc ADC module disable control: +* (a) ADC_OFF - Turn off ADC module +* (b) ADC_ON - Leave ADC module in its default state +* +* 3. timer2 Timer 2 module disable control: +* (a) TIMER2_OFF - Turn off Timer 2 module +* (b) TIMER2_ON - Leave Timer 2 module in its default state +* +* 4. timer1 Timer 1 module disable control: +* (a) TIMER1_OFF - Turn off Timer 1 module +* (b) TIMER1_ON - Leave Timer 1 module in its default state +* +* 5. timer0 Timer 0 module disable control: +* (a) TIMER0_OFF - Turn off Timer 0 module +* (b) TIMER0_ON - Leave Timer 0 module in its default state +* +* 6. spi SPI module disable control: +* (a) SPI_OFF - Turn off SPI module +* (b) SPI_ON - Leave SPI module in its default state +* +* 7. usart0 USART0 module disable control: +* (a) USART0_OFF - Turn off USART0 module +* (b) USART0_ON - Leave USART0 module in its default state +* +* 8. twi TWI module disable control: +* (a) TWI_OFF - Turn off TWI module +* (b) TWI_ON - Leave TWI module in its default state +* +*******************************************************************************/ +#if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega1284P__) +void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2, + timer1_t timer1, timer0_t timer0, + spi_t spi, usart0_t usart0, twi_t twi) +{ + idleWait(period, adc, timer2, timer1, timer0, spi, usart0, twi, 0); +} +#endif + +/******************************************************************************* +* Name: idleWait * Description: Putting ATmega32U4 into idle state. Please make sure you * understand the implication and result of disabling module. * Take note that Timer 2 is not available and USART0 @@ -270,12 +349,17 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2, * 10.usb USB module disable control: * (a) USB_OFF - Turn off USB module * (b) USB_ON - Leave USB module in its default state +* +* 11. wait If true, stay asleep until watchdog goes off; if false, wake up +* on any enabled interrupt. +* *******************************************************************************/ #if defined __AVR_ATmega32U4__ -void LowPowerClass::idle(period_t period, adc_t adc, +void LowPowerClass::idleWait(period_t period, adc_t adc, timer4_t timer4, timer3_t timer3, timer1_t timer1, timer0_t timer0, - spi_t spi, usart1_t usart1, twi_t twi, usb_t usb) + spi_t spi, usart1_t usart1, twi_t twi, usb_t usb + bool wait) { if (adc == ADC_OFF) { @@ -298,7 +382,10 @@ void LowPowerClass::idle(period_t period, adc_t adc, WDTCSR |= (1 << WDIE); } - lowPowerBodOn(SLEEP_MODE_IDLE); + tookInterrupt = 0; + do { + lowPowerBodOn(SLEEP_MODE_IDLE); + } while(wait && !tookInterrupt); if (adc == ADC_OFF) { @@ -319,6 +406,75 @@ void LowPowerClass::idle(period_t period, adc_t adc, /******************************************************************************* * Name: idle +* Description: Putting ATmega32U4 into idle state. Please make sure you +* understand the implication and result of disabling module. +* Take note that Timer 2 is not available and USART0 +* is replaced with USART1 on ATmega32U4. +* +* Argument Description +* ========= =========== +* 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake +* up resource: +* (a) SLEEP_15MS - 15 ms sleep +* (b) SLEEP_30MS - 30 ms sleep +* (c) SLEEP_60MS - 60 ms sleep +* (d) SLEEP_120MS - 120 ms sleep +* (e) SLEEP_250MS - 250 ms sleep +* (f) SLEEP_500MS - 500 ms sleep +* (g) SLEEP_1S - 1 s sleep +* (h) SLEEP_2S - 2 s sleep +* (i) SLEEP_4S - 4 s sleep +* (j) SLEEP_8S - 8 s sleep +* (k) SLEEP_FOREVER - Sleep without waking up through WDT +* +* 2. adc ADC module disable control: +* (a) ADC_OFF - Turn off ADC module +* (b) ADC_ON - Leave ADC module in its default state +* +* 3. timer4 Timer 4 module disable control: +* (a) TIMER4_OFF - Turn off Timer 4 module +* (b) TIMER4_ON - Leave Timer 4 module in its default state +* +* 4. timer3 Timer 3 module disable control: +* (a) TIMER3_OFF - Turn off Timer 3 module +* (b) TIMER3_ON - Leave Timer 3 module in its default state +* +* 5. timer1 Timer 1 module disable control: +* (a) TIMER1_OFF - Turn off Timer 1 module +* (b) TIMER1_ON - Leave Timer 1 module in its default state +* +* 6. timer0 Timer 0 module disable control: +* (a) TIMER0_OFF - Turn off Timer 0 module +* (b) TIMER0_ON - Leave Timer 0 module in its default state +* +* 7. spi SPI module disable control: +* (a) SPI_OFF - Turn off SPI module +* (b) SPI_ON - Leave SPI module in its default state +* +* 8. usart1 USART1 module disable control: +* (a) USART1_OFF - Turn off USART1 module +* (b) USART1_ON - Leave USART1 module in its default state +* +* 9. twi TWI module disable control: +* (a) TWI_OFF - Turn off TWI module +* (b) TWI_ON - Leave TWI module in its default state +* +* 10.usb USB module disable control: +* (a) USB_OFF - Turn off USB module +* (b) USB_ON - Leave USB module in its default state +*******************************************************************************/ +#if defined __AVR_ATmega32U4__ +void LowPowerClass::idle(period_t period, adc_t adc, + timer4_t timer4, timer3_t timer3, + timer1_t timer1, timer0_t timer0, + spi_t spi, usart1_t usart1, twi_t twi, usb_t usb) +{ + idleWait(period, adc, timer4, timer3, timer1, timer0, spi, usart1, twi, usb, 0); +} +#endif + +/******************************************************************************* +* Name: idleWait * Description: Putting ATmega2560 & ATmega1280 into idle state. Please make sure * you understand the implication and result of disabling module. * Take note that extra Timer 5, 4, 3 compared to an ATmega328P/168. @@ -393,13 +549,16 @@ void LowPowerClass::idle(period_t period, adc_t adc, * (a) TWI_OFF - Turn off TWI module * (b) TWI_ON - Leave TWI module in its default state * +* 15. wait If true, stay asleep until watchdog goes off; if false, wake up +* on any enabled interrupt. +* *******************************************************************************/ #if defined (__AVR_ATmega2560__) || defined (__AVR_ATmega1280__) -void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5, +void LowPowerClass::idleWait(period_t period, adc_t adc, timer5_t timer5, timer4_t timer4, timer3_t timer3, timer2_t timer2, timer1_t timer1, timer0_t timer0, spi_t spi, usart3_t usart3, usart2_t usart2, usart1_t usart1, - usart0_t usart0, twi_t twi) + usart0_t usart0, twi_t twi, bool wait) { // Temporary clock source variable unsigned char clockSource = 0; @@ -442,8 +601,11 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5, WDTCSR |= (1 << WDIE); } - lowPowerBodOn(SLEEP_MODE_IDLE); - + tookInterrupt = 0; + do { + lowPowerBodOn(SLEEP_MODE_IDLE); + } while(wait && !tookInterrupt); + if (adc == ADC_OFF) { power_adc_enable(); @@ -474,7 +636,96 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5, #endif /******************************************************************************* -* Name: adcNoiseReduction +* Name: idle +* Description: Putting ATmega2560 & ATmega1280 into idle state. Please make sure +* you understand the implication and result of disabling module. +* Take note that extra Timer 5, 4, 3 compared to an ATmega328P/168. +* Also take note that extra USART 3, 2, 1 compared to an +* ATmega328P/168. +* +* Argument Description +* ========= =========== +* 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake +* up resource: +* (a) SLEEP_15MS - 15 ms sleep +* (b) SLEEP_30MS - 30 ms sleep +* (c) SLEEP_60MS - 60 ms sleep +* (d) SLEEP_120MS - 120 ms sleep +* (e) SLEEP_250MS - 250 ms sleep +* (f) SLEEP_500MS - 500 ms sleep +* (g) SLEEP_1S - 1 s sleep +* (h) SLEEP_2S - 2 s sleep +* (i) SLEEP_4S - 4 s sleep +* (j) SLEEP_8S - 8 s sleep +* (k) SLEEP_FOREVER - Sleep without waking up through WDT +* +* 2. adc ADC module disable control: +* (a) ADC_OFF - Turn off ADC module +* (b) ADC_ON - Leave ADC module in its default state +* +* 3. timer5 Timer 5 module disable control: +* (a) TIMER5_OFF - Turn off Timer 5 module +* (b) TIMER5_ON - Leave Timer 5 module in its default state +* +* 4. timer4 Timer 4 module disable control: +* (a) TIMER4_OFF - Turn off Timer 4 module +* (b) TIMER4_ON - Leave Timer 4 module in its default state +* +* 5. timer3 Timer 3 module disable control: +* (a) TIMER3_OFF - Turn off Timer 3 module +* (b) TIMER3_ON - Leave Timer 3 module in its default state +* +* 6. timer2 Timer 2 module disable control: +* (a) TIMER2_OFF - Turn off Timer 2 module +* (b) TIMER2_ON - Leave Timer 2 module in its default state +* +* 7. timer1 Timer 1 module disable control: +* (a) TIMER1_OFF - Turn off Timer 1 module +* (b) TIMER1_ON - Leave Timer 1 module in its default state +* +* 8. timer0 Timer 0 module disable control: +* (a) TIMER0_OFF - Turn off Timer 0 module +* (b) TIMER0_ON - Leave Timer 0 module in its default state +* +* 9. spi SPI module disable control: +* (a) SPI_OFF - Turn off SPI module +* (b) SPI_ON - Leave SPI module in its default state +* +* 10.usart3 USART3 module disable control: +* (a) USART3_OFF - Turn off USART3 module +* (b) USART3_ON - Leave USART3 module in its default state +* +* 11.usart2 USART2 module disable control: +* (a) USART2_OFF - Turn off USART2 module +* (b) USART2_ON - Leave USART2 module in its default state +* +* 12.usart1 USART1 module disable control: +* (a) USART1_OFF - Turn off USART1 module +* (b) USART1_ON - Leave USART1 module in its default state +* +* 13.usart0 USART0 module disable control: +* (a) USART0_OFF - Turn off USART0 module +* (b) USART0_ON - Leave USART0 module in its default state +* +* 14.twi TWI module disable control: +* (a) TWI_OFF - Turn off TWI module +* (b) TWI_ON - Leave TWI module in its default state +* +*******************************************************************************/ +#if defined (__AVR_ATmega2560__) || defined (__AVR_ATmega1280__) +void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5, + timer4_t timer4, timer3_t timer3, timer2_t timer2, + timer1_t timer1, timer0_t timer0, spi_t spi, + usart3_t usart3, usart2_t usart2, usart1_t usart1, + usart0_t usart0, twi_t twi) +{ + idleWait(period, adc, timer5, timer4, timer3, timer2, timer1, timer0, + spi, usart3, usart2, usart1, usart0, twi, 0); +} +#endif + +/******************************************************************************* +* Name: adcNoiseReductionWait * Description: Putting microcontroller into ADC noise reduction state. This is * a very useful state when using the ADC to achieve best and low * noise signal. @@ -504,9 +755,12 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5, * (a) TIMER2_OFF - Turn off Timer 2 module * (b) TIMER2_ON - Leave Timer 2 module in its default state * +* 4. wait If true, stay asleep until watchdog goes off; if false, wake up +* on any enabled interrupt. +* *******************************************************************************/ -void LowPowerClass::adcNoiseReduction(period_t period, adc_t adc, - timer2_t timer2) +void LowPowerClass::adcNoiseReductionWait(period_t period, adc_t adc, + timer2_t timer2, bool wait) { // Temporary clock source variable unsigned char clockSource = 0; @@ -533,8 +787,11 @@ void LowPowerClass::adcNoiseReduction(period_t period, adc_t adc, WDTCSR |= (1 << WDIE); } - lowPowerBodOn(SLEEP_MODE_ADC); - + tookInterrupt = 0; + do { + lowPowerBodOn(SLEEP_MODE_ADC); + } while(wait && !tookInterrupt); + if (adc == ADC_OFF) ADCSRA |= (1 << ADEN); #if !defined(__AVR_ATmega32U4__) @@ -549,7 +806,45 @@ void LowPowerClass::adcNoiseReduction(period_t period, adc_t adc, } /******************************************************************************* -* Name: powerDown +* Name: adcNoiseReduction +* Description: Putting microcontroller into ADC noise reduction state. This is +* a very useful state when using the ADC to achieve best and low +* noise signal. +* +* Argument Description +* ========= =========== +* 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake +* up resource: +* (a) SLEEP_15MS - 15 ms sleep +* (b) SLEEP_30MS - 30 ms sleep +* (c) SLEEP_60MS - 60 ms sleep +* (d) SLEEP_120MS - 120 ms sleep +* (e) SLEEP_250MS - 250 ms sleep +* (f) SLEEP_500MS - 500 ms sleep +* (g) SLEEP_1S - 1 s sleep +* (h) SLEEP_2S - 2 s sleep +* (i) SLEEP_4S - 4 s sleep +* (j) SLEEP_8S - 8 s sleep +* (k) SLEEP_FOREVER - Sleep without waking up through WDT +* +* 2. adc ADC module disable control. Turning off the ADC module is +* basically removing the purpose of this low power mode. +* (a) ADC_OFF - Turn off ADC module +* (b) ADC_ON - Leave ADC module in its default state +* +* 3. timer2 Timer 2 module disable control: +* (a) TIMER2_OFF - Turn off Timer 2 module +* (b) TIMER2_ON - Leave Timer 2 module in its default state +* +*******************************************************************************/ +void LowPowerClass::adcNoiseReduction(period_t period, adc_t adc, + timer2_t timer2) +{ + adcNoiseReductionWait(period, adc, timer2, 0); +} + +/******************************************************************************* +* Name: powerDownWait * Description: Putting microcontroller into power down state. This is * the lowest current consumption state. Use this together with * external pin interrupt to wake up through external event @@ -580,8 +875,11 @@ void LowPowerClass::adcNoiseReduction(period_t period, adc_t adc, * (a) BOD_OFF - Turn off BOD module * (b) BOD_ON - Leave BOD module in its default state * +* 4. wait If true, stay asleep until watchdog goes off; if false, wake up +* on any enabled interrupt. +* *******************************************************************************/ -void LowPowerClass::powerDown(period_t period, adc_t adc, bod_t bod) +void LowPowerClass::powerDownWait(period_t period, adc_t adc, bod_t bod, bool wait) { if (adc == ADC_OFF) ADCSRA &= ~(1 << ADEN); @@ -590,24 +888,66 @@ void LowPowerClass::powerDown(period_t period, adc_t adc, bod_t bod) wdt_enable(period); WDTCSR |= (1 << WDIE); } - if (bod == BOD_OFF) - { - #if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) + + tookInterrupt = 0; + do { + if (bod == BOD_OFF) + { +#if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) lowPowerBodOff(SLEEP_MODE_PWR_DOWN); - #else +#else lowPowerBodOn(SLEEP_MODE_PWR_DOWN); - #endif - } - else - { - lowPowerBodOn(SLEEP_MODE_PWR_DOWN); - } - +#endif + } + else + { + lowPowerBodOn(SLEEP_MODE_PWR_DOWN); + } + } while(wait && !tookInterrupt); + if (adc == ADC_OFF) ADCSRA |= (1 << ADEN); } /******************************************************************************* -* Name: powerSave +* Name: powerDown +* Description: Putting microcontroller into power down state. This is +* the lowest current consumption state. Use this together with +* external pin interrupt to wake up through external event +* triggering (example: RTC clockout pin, SD card detect pin). +* +* Argument Description +* ========= =========== +* 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake +* up resource: +* (a) SLEEP_15MS - 15 ms sleep +* (b) SLEEP_30MS - 30 ms sleep +* (c) SLEEP_60MS - 60 ms sleep +* (d) SLEEP_120MS - 120 ms sleep +* (e) SLEEP_250MS - 250 ms sleep +* (f) SLEEP_500MS - 500 ms sleep +* (g) SLEEP_1S - 1 s sleep +* (h) SLEEP_2S - 2 s sleep +* (i) SLEEP_4S - 4 s sleep +* (j) SLEEP_8S - 8 s sleep +* (k) SLEEP_FOREVER - Sleep without waking up through WDT +* +* 2. adc ADC module disable control. Turning off the ADC module is +* basically removing the purpose of this low power mode. +* (a) ADC_OFF - Turn off ADC module +* (b) ADC_ON - Leave ADC module in its default state +* +* 3. bod Brown Out Detector (BOD) module disable control: +* (a) BOD_OFF - Turn off BOD module +* (b) BOD_ON - Leave BOD module in its default state +* +*******************************************************************************/ +void LowPowerClass::powerDown(period_t period, adc_t adc, bod_t bod) +{ + powerDownWait(period, adc, bod, 0); +} + +/******************************************************************************* +* Name: powerSaveWait * Description: Putting microcontroller into power save state. This is * the lowest current consumption state after power down. * Use this state together with an external 32.768 kHz crystal (but @@ -648,9 +988,12 @@ void LowPowerClass::powerDown(period_t period, adc_t adc, bod_t bod) * (a) TIMER2_OFF - Turn off Timer 2 module * (b) TIMER2_ON - Leave Timer 2 module in its default state * +* 5. wait If true, stay asleep until watchdog goes off; if false, wake up +* on any enabled interrupt. +* *******************************************************************************/ -void LowPowerClass::powerSave(period_t period, adc_t adc, bod_t bod, - timer2_t timer2) +void LowPowerClass::powerSaveWait(period_t period, adc_t adc, bod_t bod, + timer2_t timer2, bool wait) { // Temporary clock source variable unsigned char clockSource = 0; @@ -677,19 +1020,22 @@ void LowPowerClass::powerSave(period_t period, adc_t adc, bod_t bod, WDTCSR |= (1 << WDIE); } - if (bod == BOD_OFF) - { - #if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) + tookInterrupt = 0; + do { + if (bod == BOD_OFF) + { +#if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) lowPowerBodOff(SLEEP_MODE_PWR_SAVE); - #else +#else lowPowerBodOn(SLEEP_MODE_PWR_SAVE); - #endif - } - else - { - lowPowerBodOn(SLEEP_MODE_PWR_SAVE); - } - +#endif + } + else + { + lowPowerBodOn(SLEEP_MODE_PWR_SAVE); + } + } while(wait && !tookInterrupt); + if (adc == ADC_OFF) ADCSRA |= (1 << ADEN); #if !defined(__AVR_ATmega32U4__) @@ -703,7 +1049,56 @@ void LowPowerClass::powerSave(period_t period, adc_t adc, bod_t bod, } /******************************************************************************* -* Name: powerStandby +* Name: powerSave +* Description: Putting microcontroller into power save state. This is +* the lowest current consumption state after power down. +* Use this state together with an external 32.768 kHz crystal (but +* 8/16 MHz crystal/resonator need to be removed) to provide an +* asynchronous clock source to Timer 2. Please take note that +* Timer 2 is also used by the Arduino core for PWM operation. +* Please refer to wiring.c for explanation. Removal of the external +* 8/16 MHz crystal/resonator requires the microcontroller to run +* on its internal RC oscillator which is not so accurate for time +* critical operation. +* +* Argument Description +* ========= =========== +* 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake +* up resource: +* (a) SLEEP_15MS - 15 ms sleep +* (b) SLEEP_30MS - 30 ms sleep +* (c) SLEEP_60MS - 60 ms sleep +* (d) SLEEP_120MS - 120 ms sleep +* (e) SLEEP_250MS - 250 ms sleep +* (f) SLEEP_500MS - 500 ms sleep +* (g) SLEEP_1S - 1 s sleep +* (h) SLEEP_2S - 2 s sleep +* (i) SLEEP_4S - 4 s sleep +* (j) SLEEP_8S - 8 s sleep +* (k) SLEEP_FOREVER - Sleep without waking up through WDT +* +* 2. adc ADC module disable control. Turning off the ADC module is +* basically removing the purpose of this low power mode. +* (a) ADC_OFF - Turn off ADC module +* (b) ADC_ON - Leave ADC module in its default state +* +* 3. bod Brown Out Detector (BOD) module disable control: +* (a) BOD_OFF - Turn off BOD module +* (b) BOD_ON - Leave BOD module in its default state +* +* 4. timer2 Timer 2 module disable control: +* (a) TIMER2_OFF - Turn off Timer 2 module +* (b) TIMER2_ON - Leave Timer 2 module in its default state +* +*******************************************************************************/ +void LowPowerClass::powerSave(period_t period, adc_t adc, bod_t bod, + timer2_t timer2) +{ + powerSaveWait(period, adc, bod, timer2, 0); +} + +/******************************************************************************* +* Name: powerStandbyWait * Description: Putting microcontroller into power standby state. * * Argument Description @@ -731,8 +1126,11 @@ void LowPowerClass::powerSave(period_t period, adc_t adc, bod_t bod, * (a) BOD_OFF - Turn off BOD module * (b) BOD_ON - Leave BOD module in its default state * +* 4. wait If true, stay asleep until watchdog goes off; if false, wake up +* on any enabled interrupt. +* *******************************************************************************/ -void LowPowerClass::powerStandby(period_t period, adc_t adc, bod_t bod) +void LowPowerClass::powerStandbyWait(period_t period, adc_t adc, bod_t bod, bool wait) { if (adc == ADC_OFF) ADCSRA &= ~(1 << ADEN); @@ -742,24 +1140,62 @@ void LowPowerClass::powerStandby(period_t period, adc_t adc, bod_t bod) WDTCSR |= (1 << WDIE); } - if (bod == BOD_OFF) - { - #if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) + tookInterrupt = 0; + do { + if (bod == BOD_OFF) + { +#if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) lowPowerBodOff(SLEEP_MODE_STANDBY); - #else +#else lowPowerBodOn(SLEEP_MODE_STANDBY); - #endif - } - else - { - lowPowerBodOn(SLEEP_MODE_STANDBY); - } - +#endif + } + else + { + lowPowerBodOn(SLEEP_MODE_STANDBY); + } + } while(wait && !tookInterrupt); + if (adc == ADC_OFF) ADCSRA |= (1 << ADEN); } /******************************************************************************* -* Name: powerExtStandby +* Name: powerStandby +* Description: Putting microcontroller into power standby state. +* +* Argument Description +* ========= =========== +* 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake +* up resource: +* (a) SLEEP_15MS - 15 ms sleep +* (b) SLEEP_30MS - 30 ms sleep +* (c) SLEEP_60MS - 60 ms sleep +* (d) SLEEP_120MS - 120 ms sleep +* (e) SLEEP_250MS - 250 ms sleep +* (f) SLEEP_500MS - 500 ms sleep +* (g) SLEEP_1S - 1 s sleep +* (h) SLEEP_2S - 2 s sleep +* (i) SLEEP_4S - 4 s sleep +* (j) SLEEP_8S - 8 s sleep +* (k) SLEEP_FOREVER - Sleep without waking up through WDT +* +* 2. adc ADC module disable control. Turning off the ADC module is +* basically removing the purpose of this low power mode. +* (a) ADC_OFF - Turn off ADC module +* (b) ADC_ON - Leave ADC module in its default state +* +* 3. bod Brown Out Detector (BOD) module disable control: +* (a) BOD_OFF - Turn off BOD module +* (b) BOD_ON - Leave BOD module in its default state +* +*******************************************************************************/ +void LowPowerClass::powerStandby(period_t period, adc_t adc, bod_t bod) +{ + powerStandbyWait(period, adc, bod, 0); +} + +/******************************************************************************* +* Name: powerExtStandbyWait * Description: Putting microcontroller into power extended standby state. This * is different from the power standby state as it has the * capability to run Timer 2 asynchronously. @@ -792,9 +1228,12 @@ void LowPowerClass::powerStandby(period_t period, adc_t adc, bod_t bod) * (a) TIMER2_OFF - Turn off Timer 2 module * (b) TIMER2_ON - Leave Timer 2 module in its default state * +* 5. wait If true, stay asleep until watchdog goes off; if false, wake up +* on any enabled interrupt. +* *******************************************************************************/ -void LowPowerClass::powerExtStandby(period_t period, adc_t adc, bod_t bod, - timer2_t timer2) +void LowPowerClass::powerExtStandbyWait(period_t period, adc_t adc, bod_t bod, + timer2_t timer2, bool wait) { // Temporary clock source variable unsigned char clockSource = 0; @@ -820,18 +1259,22 @@ void LowPowerClass::powerExtStandby(period_t period, adc_t adc, bod_t bod, wdt_enable(period); WDTCSR |= (1 << WDIE); } - if (bod == BOD_OFF) - { - #if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) + + tookInterrupt = 0; + do { + if (bod == BOD_OFF) + { +#if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1284P__) lowPowerBodOff(SLEEP_MODE_EXT_STANDBY); - #else +#else lowPowerBodOn(SLEEP_MODE_EXT_STANDBY); - #endif - } - else - { - lowPowerBodOn(SLEEP_MODE_EXT_STANDBY); - } +#endif + } + else + { + lowPowerBodOn(SLEEP_MODE_EXT_STANDBY); + } + } while(wait && !tookInterrupt); if (adc == ADC_OFF) ADCSRA |= (1 << ADEN); @@ -846,6 +1289,47 @@ void LowPowerClass::powerExtStandby(period_t period, adc_t adc, bod_t bod, } /******************************************************************************* +* Name: powerExtStandby +* Description: Putting microcontroller into power extended standby state. This +* is different from the power standby state as it has the +* capability to run Timer 2 asynchronously. +* +* Argument Description +* ========= =========== +* 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake +* up resource: +* (a) SLEEP_15MS - 15 ms sleep +* (b) SLEEP_30MS - 30 ms sleep +* (c) SLEEP_60MS - 60 ms sleep +* (d) SLEEP_120MS - 120 ms sleep +* (e) SLEEP_250MS - 250 ms sleep +* (f) SLEEP_500MS - 500 ms sleep +* (g) SLEEP_1S - 1 s sleep +* (h) SLEEP_2S - 2 s sleep +* (i) SLEEP_4S - 4 s sleep +* (j) SLEEP_8S - 8 s sleep +* (k) SLEEP_FOREVER - Sleep without waking up through WDT +* +* 2. adc ADC module disable control. +* (a) ADC_OFF - Turn off ADC module +* (b) ADC_ON - Leave ADC module in its default state +* +* 3. bod Brown Out Detector (BOD) module disable control: +* (a) BOD_OFF - Turn off BOD module +* (b) BOD_ON - Leave BOD module in its default state +* +* 4. timer2 Timer 2 module disable control: +* (a) TIMER2_OFF - Turn off Timer 2 module +* (b) TIMER2_ON - Leave Timer 2 module in its default state +* +*******************************************************************************/ +void LowPowerClass::powerExtStandby(period_t period, adc_t adc, bod_t bod, + timer2_t timer2) +{ + powerExtStandbyWait(period, adc, bod, timer2, 0); +} + +/******************************************************************************* * Name: ISR (WDT_vect) * Description: Watchdog Timer interrupt service routine. This routine is * required to allow automatic WDIF and WDIE bit clearance in @@ -854,8 +1338,10 @@ void LowPowerClass::powerExtStandby(period_t period, adc_t adc, bod_t bod, *******************************************************************************/ ISR (WDT_vect) { + tookInterrupt++; + // WDIE & WDIF is cleared in hardware upon entering this ISR wdt_disable(); } -LowPowerClass LowPower; \ No newline at end of file +LowPowerClass LowPower; diff --git a/LowPower.h b/LowPower.h index d8af309..673fd15 100644 --- a/LowPower.h +++ b/LowPower.h @@ -117,24 +113,40 @@ class LowPowerClass void idle(period_t period, adc_t adc, timer2_t timer2, timer1_t timer1, timer0_t timer0, spi_t spi, usart0_t usart0, twi_t twi); + void idleWait(period_t period, adc_t adc, timer2_t timer2, + timer1_t timer1, timer0_t timer0, spi_t spi, + usart0_t usart0, twi_t twi, bool wait); #elif defined __AVR_ATmega2560__ void idle(period_t period, adc_t adc, timer5_t timer5, timer4_t timer4, timer3_t timer3, timer2_t timer2, timer1_t timer1, timer0_t timer0, spi_t spi, usart3_t usart3, usart2_t usart2, usart1_t usart1, usart0_t usart0, twi_t twi); + void idleWait(period_t period, adc_t adc, timer5_t timer5, + timer4_t timer4, timer3_t timer3, timer2_t timer2, + timer1_t timer1, timer0_t timer0, spi_t spi, + usart3_t usart3, usart2_t usart2, usart1_t usart1, + usart0_t usart0, twi_t twi, bool wait); #elif defined __AVR_ATmega32U4__ void idle(period_t period, adc_t adc, timer4_t timer4, timer3_t timer3, timer1_t timer1, timer0_t timer0, spi_t spi, usart1_t usart1, twi_t twi, usb_t usb); + void idleWait(period_t period, adc_t adc, timer4_t timer4, timer3_t timer3, + timer1_t timer1, timer0_t timer0, spi_t spi, + usart1_t usart1, twi_t twi, usb_t usb, bool wait); #else #error "Please ensure chosen MCU is either 168, 328P, 32U4, 1284P, 1280 or 2560." #endif void adcNoiseReduction(period_t period, adc_t adc, timer2_t timer2) __attribute__((optimize("-O1"))); + void adcNoiseReductionWait(period_t period, adc_t adc, timer2_t timer2, bool wait) __attribute__((optimize("-O1"))); void powerDown(period_t period, adc_t adc, bod_t bod) __attribute__((optimize("-O1"))); + void powerDownWait(period_t period, adc_t adc, bod_t bod, bool wait) __attribute__((optimize("-O1"))); void powerSave(period_t period, adc_t adc, bod_t bod, timer2_t timer2) __attribute__((optimize("-O1"))); + void powerSaveWait(period_t period, adc_t adc, bod_t bod, timer2_t timer2, bool wait) __attribute__((optimize("-O1"))); void powerStandby(period_t period, adc_t adc, bod_t bod) __attribute__((optimize("-O1"))); + void powerStandbyWait(period_t period, adc_t adc, bod_t bod, bool wait) __attribute__((optimize("-O1"))); void powerExtStandby(period_t period, adc_t adc, bod_t bod, timer2_t timer2) __attribute__((optimize("-O1"))); + void powerExtStandbyWait(period_t period, adc_t adc, bod_t bod, timer2_t timer2, bool wait) __attribute__((optimize("-O1"))); }; extern LowPowerClass LowPower;