Embedded Freaks..

October 6, 2008

AVR Auto Power Mode Selection

Filed under: avr, embedded-tips — Tags: , — kunilkuda @ 7:04 pm

To optimize the AVR’s power consumption, you will need to modify its power mode according to the peripheral that you need at that time.

This is the part of the TinyOS ATMega128 driver that you can use directly (without TinyOS). It will select the best power mode for the MCU, based on the peripheral(s) that is active.

  uint8_t getPowerState() {
    // Note: we go to sleep even if timer 1, 2, or 3's overflow interrupt
    // is enabled - this allows using these timers as TinyOS "Alarm"s
    // while still having power management.

    // Are external timers running?
    if (TIMSK & ~(1 << OCIE0 | 1 << TOIE0 | 1 << TOIE1 | 1 << TOIE2) ||
    ETIMSK & ~(1 << TOIE3)) {
      return SLEEP_MODE_IDLE;
    }
    // SPI (Radio stack on mica/micaZ
    else if (bit_is_set(SPCR, SPE)) {
      return SLEEP_MODE_IDLE;
    }
    // A UART is active
    else if ((UCSR0B | UCSR1B) & (1 << TXCIE | 1 << RXCIE)) { // UART
      return SLEEP_MODE_IDLE;
    }
    // I2C (Two-wire) is active
    else if (bit_is_set(TWCR, TWEN)){
      return SLEEP_MODE_IDLE;
    }
    // ADC is enabled
    else if (bit_is_set(ADCSR, ADEN)) {
      return SLEEP_MODE_ADC;
    }
    else {
      return SLEEP_MODE_PWR_DOWN;
    }
  }

How to use it ? Simple, just call the function to select the correct power mode, before going to sleep. Like this:

while(1)
{
  // do homework before going to nap

  // Take a nap
  set_sleep_mode(getPowerState());
  sleep_mode();
}

Therefore, you can turn on/off your peripheral while optimizing the power mode selection on the fly. Nice invention from TinyOS’ contributor.

Anyway, the function is not covering the whole AVR’s peripheral (and its sleeping mode), but I guess you got the catch.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: