t4.1 detect reboot type

Frankthetech

Active member
I'm looking for help in detecting when the teensy is powered off/on vs a software reset.
This is so I can keep track of the power cycles. I tried to just inc a counter in setup() but I
need to know if the unit was unpluged (hard reset) and keep a count of that too.
 
let me know what you think of this,
as a test setup to start with
I know this only gives me hardware/software or watchdog flag
But since I don't have any software reboots in my code this is a work around... maybe
only question is wdt.expired() reset on a power reboot?

Code:
//test 4.1 for reboot h/w vs s/w

WDT_T4<WDT1> wdt;
WDT_timings_t config;


void setup() {
  // WATCHDOG
  config.timeout = 5;
  wdt.begin(config);
 
  while(!Serial.begin(115200)&& millis()<3000);
 
  if(!wdt.expired()){    //restart was not the result of watchdog timeout
    //must have been a power off/on or software reboot
    //force a wdt timeout here then next restart will be true
    //inc counter & save to eeprom
    delay(6000);
  }
  //continue with setup
 
}

void loop() {
  wdt.feed();

}
 
Frank, if you look at file cores\Teensy4\CrashReport.cpp, you will find the code shown below, which reads and interprets the bits of register SRC_SRSR. Bits 0-8 indicate the cause of reset. You can copy and modify this code for your own use. The SRC (system reset control) is described in section 21 of the IMXRT reference manual.

Code:
  uint32_t SRSR = SRC_SRSR;
  if (SRSR & SRC_SRSR_LOCKUP_SYSRESETREQ) {
    // use SRC_GPR5 to distinguish cases.  See pages 1290 & 1294 in ref manual
    uint32_t gpr5 = SRC_GPR5;
    if (gpr5 == 0x0BAD00F1) {
      p.println("  Reboot was caused by auto reboot after fault or bad interrupt detected");
    } else {
      p.println("  Reboot was caused by software write to SCB_AIRCR or CPU lockup");
    }
  }
  if (SRSR & SRC_SRSR_CSU_RESET_B) {
    p.println("  Reboot was caused by security monitor");
  }
  if (SRSR & SRC_SRSR_IPP_USER_RESET_B) {
    // This case probably can't occur on Teensy 4.x
    // because the bootloader chip monitors 3.3V power
    // and manages DCDC_PSWITCH and RESET, causing the
    // power on event to appear as a normal reset.
    p.println("  Reboot was caused by power on/off button");
  }
  if (SRSR & SRC_SRSR_WDOG_RST_B) {
    p.println("  Reboot was caused by watchdog 1 or 2");
  }
  if (SRSR & SRC_SRSR_JTAG_RST_B) {
    p.println("  Reboot was caused by JTAG boundary scan");
  }
  if (SRSR & SRC_SRSR_JTAG_SW_RST) {
    p.println("  Reboot was caused by JTAG debug");
  }
  if (SRSR & SRC_SRSR_WDOG3_RST_B) {
    p.println("  Reboot was caused by watchdog 3");
  }
  if (SRSR & SRC_SRSR_TEMPSENSE_RST_B) {
    p.println("  Reboot was caused by temperature sensor");
      SRC_SRSR &= ~0x100u; /* Write 0 to clear. */
      p.println("Panic Temp Exceeded Shutting Down");
      p.println("Can be caused by Overclocking w/o Heatsink or other unknown reason");
      IOMUXC_GPR_GPR16 = 0x00000007;
      SNVS_LPCR |= SNVS_LPCR_TOP; //Switch off now
      asm volatile ("dsb":::"memory");
      while (1) asm ("wfi");
  }
 
Thanks for that, tried that code but the SRSR is always 1 on power off\on, won't work in this case with the T4.x

Code:
if (SRSR & SRC_SRSR_IPP_USER_RESET_B) {
    // This case probably can't occur on Teensy 4.x
    // because the bootloader chip monitors 3.3V power
    // and manages DCDC_PSWITCH and RESET, causing the
    // power on event to appear as a normal reset.
    p.println("  Reboot was caused by power on/off button");
  }
 
Back
Top