Pages: [1]   Go Down
Author Topic: How long does Atmega take to wake up from Sleep mode?  (Read 2081 times)
0 Members and 1 Guest are viewing this topic.
Portland, OR
Offline Offline
Sr. Member
****
Karma: 7
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How long would the Arduino's Atmega328 chip take in terms of "startup time" to wakeup from sleep mode, for example upon a timer- or pushbutton-interrupt? (assuming the standard external crystal)

Is it on the order of milliseconds, or 100s of milliseconds?

I ask because I am playing with a barebones-Arduino project where I would like the Atmega328 to wake up every 100 ms (based on an interrupt from Timer2), do a quick task, then go back to sleep again so as to conserve power.

Obviously, with this in mind, I would like the chip's wakeup action to be as immediate (as low latency) as possible.
« Last Edit: January 15, 2013, 01:33:02 am by giantsfan3 » Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

milliseconds, but check the datasheet to be sure.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It varies depending on the fuse settings, which are designed to allow crystals to get back into full oscillation. From memory the slowest ones are x clock cycles (a few hundred or so) plus 65 mS.

It also depends on the sleep mode.
Logged

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick is right..
However, It will be hard to wake up the  ATMEGA328P-PU if you gave her sleeping pills.. smiley-mr-green
Logged


Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 518
Posts: 26378
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's pretty quick.
From 10.1 of the '328 datasheet:

"If an enabled interrupt occurs while the MCU is in a sleep mode, the MCU wakes up. The MCU is then halted for
four cycles in addition to the start-up time [see 9.2.2], executes the interrupt routine, and resumes execution from the instruction
following SLEEP. The contents of the Register File and SRAM are unaltered when the device wakes up from
sleep. If a reset occurs during sleep mode, the MCU wakes up and executes from the Reset Vector."'

9.2.2:
"To ensure sufficient VCC, the device issues an internal reset with a time-out delay (tTOUT) after the device reset is
released by all other reset sources. ”System Control and Reset” on page 46 describes the start conditions for the
internal reset. The delay (tTOUT) is timed from the Watchdog Oscillator and the number of cycles in the delay is set
by the SUTx and CKSELx fuse bits. The selectable delays are shown in Table 9-2. The frequency of the Watchdog
Oscillator is voltage dependent as shown in ”Typical Characteristics” on page 318."

Table 9-2 shows:
Start up time is fuse programmable for 512 clock cycles, or 8192 clock cycles, approx. 4.1mS [edit - had mA] and 65mS with 5V supply.

"Main purpose of the delay is to keep the AVR in reset until it is supplied with minimum VCC. The delay will not monitor
the actual voltage and it will be required to select a delay longer than the VCC rise time. If this is not possible, an
internal or external Brown-Out Detection circuit should be used. A BOD circuit will ensure sufficient VCC before it
releases the reset, and the time-out delay can be disabled. Disabling the time-out delay without utilizing a Brown-
Out Detection circuit is not recommended."

However, this is the part that seems to apply when waking from Sleep:

"The oscillator is required to oscillate for a minimum number of cycles before the clock is considered stable. An
internal ripple counter monitors the oscillator output clock, and keeps the internal reset active for a given number of
clock cycles. The reset is then released and the device will start to execute. The recommended oscillator start-up
time is dependent on the clock type, and varies from 6 cycles for an externally applied clock to 32K cycles for a low
frequency crystal.
The start-up sequence for the clock includes both the time-out delay and the start-up time when the device starts
up from reset. When starting up from Power-save or Power-down mode, VCC is assumed to be at a sufficient level
and only the start-up time is included.
"
« Last Edit: January 16, 2013, 07:40:58 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Portland, OR
Offline Offline
Sr. Member
****
Karma: 7
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick and @Crossroads:

It appears, then, that the minimum time is 65 ms (in addition to possibly the number of clock cycles depending on which sleep mode).

But this seems to be very, very long (speaking with respect to typical computer time scales, of course).

For example, suppose I want to accomplish a simple repetitive cycle like this:
--Every 100 ms or so, wake up (via a Timer2 interrupt)
--Do a task that takes about 50 ms
--Go back to sleep
--Repeat

Then, unless I am missing something, the startup time of 65 ms would make the above task impossible (or at least not so efficient in terms of power optimization).

What alternative method could, or is typically, applied for a repetitive, periodic sleep-wakeup-task-then-sleep situation like this? I suppose I'm shooting for a strategy where, after the wakeup-interrupt, the MCU would "startup" in less than 10 ms (as opposed to 65).
« Last Edit: January 16, 2013, 06:53:53 pm by giantsfan3 » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6637
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
--Every 100 ms or so, wake up (via a Timer2 interrupt)
Were you planning on attaching an external crystal to timer2?  If not, you are going to need to use one of the power-saving modes that leaves the crystal oscillator running, and these are not subject to the 65ms oscillator startup time.

Quote
Standby Mode
When the SM2...0 bits are 110 and an external crystal/resonator clock option is selected, the SLEEP instruction makes the MCU enter Standby mode. This mode is identical to Power-down with the exception that the Oscillator is kept running. From Standby mode, the device wakes up in six clock cycles.

Since most Arduino boards use a ceramic resonator for the main clock, you might also consider using a different fuse setting with a faster startup time (resonators start up faster than real crystals.)
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 518
Posts: 26378
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe the fuses be set for an effective start up time of 4.1mS when 5V power is not removed.
See table 9-2 that I referenced earlier.

I made a remote control with a Promini that wakes up from power-down sleep mode after every key press. The key press causes an interrupt, the processor wakes up, scans the 4x4 keypad, and never misses a key press - I think if it was taking 65mS the presses would be missed as they would debounce themselves in that time.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Portland, OR
Offline Offline
Sr. Member
****
Karma: 7
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
--Every 100 ms or so, wake up (via a Timer2 interrupt)
Were you planning on attaching an external crystal to timer2?  If not, you are going to need to use one of the power-saving modes that leaves the crystal oscillator running, and these are not subject to the 65ms oscillator startup time.

westfw, yes, I was planning on connecting a 32 Khz crystal. Would that preclude the startup time? If so, I'm a little unclear on why the 65ms startup time would only apply in case of the main oscillator.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It appears, then, that the minimum time is 65 ms (in addition to possibly the number of clock cycles depending on which sleep mode).

Not at all. See page 33 of the data sheet. For example, if you use the internal oscillator and have BOD enabled, the start-up time from power-down sleep is 6 clock cycles (6 * 125 nS) which is 750 nS, under one millisecond (at 8 MHz).
Logged

Portland, OR
Offline Offline
Sr. Member
****
Karma: 7
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe the fuses be set for an effective start up time of 4.1mS when 5V power is not removed.
See table 9-2 that I referenced earlier.

I made a remote control with a Promini that wakes up from power-down sleep mode after every key press. The key press causes an interrupt, the processor wakes up, scans the 4x4 keypad, and never misses a key press - I think if it was taking 65mS the presses would be missed as they would debounce themselves in that time.

CrossRoads, if I understand you correctly, you're using the sleep mode formally named Power-Down. Why is the 65ms startup time absent in this case? It would seem this is the most-power-saving mode, in which case I'd guess the oscillator startup time WOULD add to making it the laziest wakeup.
Logged

Portland, OR
Offline Offline
Sr. Member
****
Karma: 7
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It appears, then, that the minimum time is 65 ms (in addition to possibly the number of clock cycles depending on which sleep mode).

Not at all. See page 33 of the data sheet. For example, if you use the internal oscillator and have BOD enabled, the start-up time from power-down sleep is 6 clock cycles (6 * 125 nS) which is 750 nS, under one millisecond (at 8 MHz).

Nick, thanks for the page reference. (Actually, going by the table on page 35, even in the case of the external clocl, the number of clock cycles seems to be identical, and there is no 65 ms.)

So, am I correct in my interpretation (again, going by what the table headings say) that the 65 ms is only present in the case of a full-reset, and not in the cases of waking up from Power-down/Power-save modes?

Also, here in the comments by you and @Coding Badly on this page (http://arduino.cc/forum/index.php?topic=87319.0), it is mentioned that the delay-from-wakeup time as long as 65 ms does apply -- I suppose I am asking if you could distinguish exactly in which cases the 65 ms startup does apply and in which it doesn't.
« Last Edit: January 16, 2013, 09:10:06 pm by giantsfan3 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, it looks like that. I was quoting the 65 mS from memory, as I am not at my usual PC. I just got the datasheet onto this laptop and it looks like you are right, the extra time is for reset rather than sleep.
Logged

Pages: [1]   Go Up
Jump to: