I'm trying to use the MCUSR register to determine whether the MCU came up from PORF or WXTRF, but it's not quite doing what I expect:
volatile byte last_mode;
void setup() {
light_led(MCUSR-1); delay(250); leds_off(); delay(250); // What are the contents of the MCUSR?
if(bit_is_set(MCUSR, PORF)) {
MCUSR = 0;
last_mode = 0; // I just powered on!
light_led(10); delay(250); leds_off(); delay(250);
}
else if(bit_is_set(MCUSR, EXTRF)) {
MCUSR = 0;
last_mode++; // advance mode because I just reset
light_led(11); delay(250); leds_off(); delay(250);
if(last_mode > MAX_MODE) {
last_mode = 0; // reset mode
}
}
(light_led just lights the appropriate LED, in lieu of getting SoftwareSerial running)
In practice, on cold boot, last_mode is 0 and lights up LED 10. I hit the reset switch and last_mode is 1 and it lights up LED 11. And every time after that, last_mode is 1. I was under the impression that memory isn't explicitly reinitialized after EXTRF, but it sure seems that last_mode is getting reinitialized every time, and only the increment in the EXTRF elsif is updating it from 0. Is there a trick to ensuring that variables aren't overwritten? It's my understanding that the HLT "bootloader" just flashes the appropriate fuses, so there isn't actually a bootloader mucking around with MCUSR like happens on the ATmegas. MCUSR seems to contain the right bits after PORF and EXTRF are asserted, so I'm getting into the conditionals that I expect.
(Actually, typing this up gave me the idea to monitor last_mode via blinking the appropriate LED, and that confirms that last_mode is getting reset to 0 after I hit the reset switch. Hrmph.)
Any ideas? Googling around has a few suggestions that imply that this ought to work, but none of them are in the Arduino environment, so I half suspect there's some basic assumption stomping on it at compile time.
Thanks!
(mostly the references are along the lines of
http://www.sparkfun.com/datasheets/Kits/tinyCylon2.c which is close to what I'm aiming to do. Start at mode 0 from power on, advance to additional modes on reset.)