System Reset

From Nutwiki
Revision as of 17:03, 27 October 2016 by Harald (Talk | contribs) (1 revision imported)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Test Environments

Hardware Comments Nut/OS
4.8.0
Ethernut 1.3 H Unknown bug:
doesn't recognize reset cause
Ethernut 2.1 B Unknown bug:
doesn't recognize reset cause
Ethernut 3.0 E Compiling Error
(see discussion)
EIR 1.0 C Set jumper JP1 to DBGU mode. OK
Binaries
Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <dev/reset.h>
  3. include <sys/timer.h>
  1. include <stdio.h>
  2. include <io.h>

int main(void) {

   int rc = NutResetCause();
   uint32_t baud = 115200;
   char *cause;
   int i;
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   switch (rc) {
   case NUT_RSTTYP_POWERUP:
       cause = "Powerup";
       break;
   case NUT_RSTTYP_WATCHDOG:
       cause = "Watchdog";
       break;
   case NUT_RSTTYP_EXTERNAL:
       cause = "External";
       break;
   case NUT_RSTTYP_SOFTWARE:
       cause = "Software";
       break;
   case NUT_RSTTYP_BROWNOUT:
       cause = "Brownout";
       break;
   default:
       cause = "Unknown";
       break;
   }
   printf("\nLast reset caused by %s (rc = %d)\n", cause, rc);
   for(i = 32; --i > 0 ;) {
       printf("\rNext reset in %d seconds", i);
       NutSleep(1000);
   }
   printf("\nReset\n");
   NutReset();
   printf("Reset failed\n");
   for (;;);
   return 0;

} </source>

Details

You can invoke a system reset by calling

<source lang="c"> NutReset(); </source>

This function will only return in case of a failure.

Use NutResetCause to determine the cause of the last reset.

<source lang="c"> rc = NutResetCause(); </source>

This function should be called at the very first beginning of your main program. It may return any of the following values:

  • NUT_RSTTYP_POWERUP, if the reset was caused by powering up the system.
  • NUT_RSTTYP_WATCHDOG, if the watchdog triggered the last reset.
  • NUT_RSTTYP_EXTERNAL, if an external reset signal appeared like pressing a reset button.
  • NUT_RSTTYP_SOFTWARE, if the reset was triggered by writing to a reset controller register.
  • NUT_RSTTYP_BROWNOUT, if the reset was cause by low voltage detection.

Which code is actually returned depends on the capabilities of the CPU and the board design.

See also