Read the RCAUSE register

Hi,

I am trying to read in a sketch the processor RESET CAUSE.

The code is bellow:

void setup()
{
SerialUSB.begin(115200);
while ((!SerialUSB) && (millis() < 2000)) { ; }
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
delay(5000);

SerialUSB.println(“Start up”);

int flag1 = PM->RCAUSE.bit.SYST;
//int flag2 = PM->RCAUSE.bit.WDT;
int flag3 = PM->RCAUSE.bit.EXT;
int flag4 = PM->RCAUSE.bit.BOD33;
int flag5 = PM->RCAUSE.bit.BOD12;
int flag6 = PM->RCAUSE.bit.POR;

SerialUSB.println("SYST = " + String(flag1));
//SerialUSB.println("WDT = " + String(flag2));
SerialUSB.println("EXT = " + String(flag3));
SerialUSB.println("BOD33 = " + String(flag4));
SerialUSB.println("BOD12 = " + String(flag5));
SerialUSB.println("POR = " + String(flag6));

}

void loop()
{
}

I can read all the bits, except WDT.
If I uncomment the line: int flag2 = PM->RCAUSE.bit.WDT; the sketch cannot be compiled.
Can you please give any sugestion?

BR,
Ionel

Hi ionel,

This is because the “WDT” define is “overruled” by the WDT define in samd21j18a.h.
Specifically line 532
#define WDT ((Wdt *)0x40001000UL) /**< \brief (WDT) APB Base Address */

To overcome this you have two options, you can either undefine WDT or do some bit operations.
To undefine, add the following line:
#undef WDT
This could have some unintended side effects as you’ve also made it harder to access your WDT peripheral. A nicer alternative would be
(PM->RCAUSE.reg & PM_RCAUSE_WDT)

Regards,
Thom

Hi Thom,
I chose ti use the second way.
I tested this and now I can read reset caused by watchdog.

Thank you very much,
Ionel