rocketscream / Low-Power

Low Power Library for Arduino
www.rocketscream.com
1.26k stars 345 forks source link

Calling with SLEEP_FOREVER does not disable watchdog #121

Open hallard opened 2 years ago

hallard commented 2 years ago

If for any reason (for example the application start/stop playing with watchdog while not in sleep mode) then a call to

LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

can have unexpected wake (depending on how app is coded), so in case of using SLEEP_FOREVER parameter calling powerDown() the function should disable the watchdog (just in case, and it does not hurt because this is exactly what we want to do)

    if (period != SLEEP_FOREVER)
    {
        wdt_enable(period);
        WDTCSR |= (1 << WDIE);
    }

should becomes something like that

    if (period == SLEEP_FOREVER)
    {
                // we don't want to be waked by the watchdog, so be 
                // sure to disable it by changing the config
                // This has been tried and works
                WDTCSR = _BV(WDCE) | _BV(WDE);
                WDTCSR = 0; 
                // but may be just following also
                // wdt_disable();
    }
        else 
        {

        wdt_enable(period);
        WDTCSR |= (1 << WDIE);
    }