0

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;
  ...
  • 3
    What does not work? What do you expect to happen? How are you using the alarms? –  Jan 03 '13 at 22:22
  • Please have a look at alarm startup, which I just added for completeness. Whenever I force a restart by triggering a watchdog I always see the startup alarm, but not the watchdog alarm. It looks like wdinit() is either never called or does not do what it should. – user1728219 Jan 03 '13 at 23:11
  • Thanks - but what exactly is it supposed to do? –  Jan 03 '13 at 23:11
  • I am recording a number of alarm conditions in my atmega16. Those are periodically polled by a supervisory system and stored in an error log. – user1728219 Jan 03 '13 at 23:15
  • @userXXX And isn't the polling working in this case? Doesn't the log ever appear? –  Jan 03 '13 at 23:34
  • alarms.b.watchdog never becomes 1, even if I force a watchdog. Everything else works. – user1728219 Jan 04 '13 at 11:23

0 Answers0