From 50ad43feb949efb718b996c214e59a25cacc4faa Mon Sep 17 00:00:00 2001 From: WestfW Date: Mon, 25 May 2015 22:54:45 -0700 Subject: [PATCH] Add test_reset example demonstrating/testing reset cause determination --- optiboot/examples/test_reset/test_reset.ino | 82 +++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 optiboot/examples/test_reset/test_reset.ino diff --git a/optiboot/examples/test_reset/test_reset.ino b/optiboot/examples/test_reset/test_reset.ino new file mode 100644 index 0000000..9f1af05 --- /dev/null +++ b/optiboot/examples/test_reset/test_reset.ino @@ -0,0 +1,82 @@ +/* + * test_reset + * May 2015 by Bill Westfield (WestfW) + * Released to the public domain. + * + * This sketch demonstrates retrival of the Reset Cause register (MCUSR) of the AVR. + * Normally, MCUSR itself is destroyed by the use of a bootloader, but Optiboot v4.6 + * and later save the contents in register r2, where it can be accessed by an + * application. + */ +#include + +/* + * First, we need a variable to hold the reset cause that can be written before + * early sketch initialization (that might change r2), and won't be reset by the + * various initialization code. + * avr-gcc provides for this via the ".noinit" section. + */ +uint8_t resetFlags __attribute__ ((section(".noinit"))); + +/* + * Next, we need to put some code to save reset cause from the bootload (in r2) + * to the variable. Again, avr-gcc provides special code sections for this. + */ +void resetFlagsInit(void) __attribute__ ((naked)) + __attribute__ ((section (".init0"))); +void resetFlagsInit(void) +{ + /* + * save the reset flags passed from the bootloader + * This is a "simple" matter of storing (STS) r2 in the special variable + * that we have created. We use assembler to access the right variable. + */ + __asm__ __volatile__ ("sts %0, r2\n" : "=m" (resetFlags) :); +} + +void setup() { + Serial.begin(9600); // Initialize serial port +} + +void loop() { + Serial.println("Reset flag test"); + while (Serial.read() < 0) + ; // wait for some type-in + + Serial.print("Have reset flag value 0x"); + Serial.print(resetFlags, HEX); + /* + * check for the usual bits. Note that the symnbols defined in wdt.h are + * bit numbers, so they have to be shifted before comparison. + */ + switch (resetFlags) { + case 1<