Here is my code for reading the MCUCSR register and making alarms from the 3 bits PORF, EXTRF, WDRF. I expected this to be trivial, but it does not work.
Any idea?
static uint8_t mcucsr __attribute__ ((section (".noinit")));
static volatile union
{
struct
{
uint32_t watchdog:1;
uint32_t powerfail:1;
uint32_t reset:1;
uint32_t startup:1;
} b;
uint32_t w;
} __attribute__ ((packed)) alarms;
static void wdinit ()
{
mcucsr = MCUCSR;
MCUCSR &= ~(1 << PORF);
MCUCSR &= ~(1 << EXTRF);
MCUCSR &= ~(1 << WDRF);
} __attribute__((naked)) __attribute__ ((section (".init3")))
int main ()
{
alarms.b.startup = true;
alarms.b.powerfail |= !!(mcucsr & (1 << PORF));
alarms.b.reset |= !!(mcucsr & (1 << EXTRF));
alarms.b.watchdog |= !!(mcucsr & (1 << WDRF));
mcucsr = 0;
...