chipKIT® Development Platform

Inspired by Arduino™

Chipkit MAX32 watchdog

Created Sun, 17 Apr 2016 14:53:16 +0000 by omega


omega

Sun, 17 Apr 2016 14:53:16 +0000

I am working in a battery operated temperature datalogger, the consumption is important when not running. I want to use watchdog and sleep mode. I am starting with this code:

// the setup function runs once when you press reset or power the board
void setup() {
    Serial.begin(9600);
  Serial.println("Initializing...");
  if ( RCON & 0x18 )        // The WDT caused a wake from Sleep
{
  Serial.println("Wake up ...");
}
  // initialize digital pin 13 as an output.
  pinMode(9, OUTPUT);
    // Standard unlock sequence

SYSKEY = 0x0;
SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA;
WDTCONCLR = 0x0002;       // Disable WDT window mode
//OSCCONSET = 0x10; // Enable sleep mode
SYSKEY = 0x0;

// WDT time-out period is set in the device 
// configuration
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(9, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);  
WDTCONSET = 0x01; // Kick the dog!

  // wait for a second
  digitalWrite(9, LOW);    // turn the LED off by making the voltage LOW
  delay(500);              // wait for a second
//Serial.println("Acostandose...");

uint32_t status = disableInterrupts();
// Run some sensitive code

WDTCONSET = 1<<15; // Turn on
WDTCONSET = 0x01; // Kick the dog!

 asm volatile("wait");
 asm volatile("eret");
  
WDTCONCLR = 1<<15; // Turn off
restoreInterrupts(status);
  
 //Serial.println("Despertando...");
}

But it is nor working, after wake up, the MAX 32 restart from reset. What is wrong?

Thank you


majenko

Sun, 17 Apr 2016 15:12:54 +0000

The problem is that the bootloader is getting in the way. It reads and clears RCON, so you can't see why it rebooted.

You should be able to get at the RCON via:

_image_header_info.pRamHeader->rcon

But it's down to the bootloader to ensure that's filled in, so no guarantees ;)


omega

Sun, 17 Apr 2016 15:30:38 +0000

I have cleaned my code:

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  Serial.println("Initializing...");

  // initialize digital pin 9 as an output.
  pinMode(9, OUTPUT);

  // Standard unlock sequence

  SYSKEY = 0x0;
  SYSKEY = 0xAA996655;
  SYSKEY = 0x556699AA;
  WDTCONCLR = 0x0002;       // Disable WDT window mode
  OSCCONSET = 0x10; // Enable sleep mode
  SYSKEY = 0x0;

  // WDT time-out period is set in the device
  // configuration
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(9, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a half second
  digitalWrite(9, LOW);    // turn the LED off by making the voltage LOW
  delay(500);              // wait for a half second

  Serial.println("Wake down...");

  delay(1000);
  WDTCONSET = 0x01; // Kick the dog!
  WDTCONSET = 1 << 15; // Turn on
  asm volatile("wait"); //Sleep
  WDTCONCLR = 1 << 15; // Turn off
  WDTCONSET = 0x01; // Kick the dog!
  Serial.println("Wake up ...");
}

Serial port is showing:

Initializing... Wake down... Initializing... Wake down... Initializing... Wake down... Initializing... Wake down... Initializing... And the led conected to pin 9 is flashing only once, then MAX32 is starting again from reset showing Initializing...

When I expect see: Initializing... Wake down... Wake up... Wake down... Wake up... Wake down...

And the led flashing.

I want use power save mode (sleep) and the watchdog to wake up MAX32.

Thank you


majenko

Sun, 17 Apr 2016 17:50:27 +0000

I think also there may be a problem in that the bootloader doesn't know what a watchdog wakeup is.

I'm sure I added code to the bootloader to handle the Watchdog's NMI but it seems not to be there. No idea what happened to it. Maybe I never made it public. It was ages back when I was working on my little Watchdog library (https://github.com/MajenkoLibraries/Watchdog). In the readme on there I note the NMI support is needed and that it is in the main bootloader repo - but I can't see it at all. Strange. No sign of it.

I shall have to investigate that further...


omega

Wed, 20 Apr 2016 11:30:15 +0000

What is a bootloader with NMI support?

Best regards,


majenko

Wed, 20 Apr 2016 12:10:32 +0000

A bootloader with NMI support is a bootloader which supports the handling of the Non-Maskable Interrupt that the watchdog timer raises as it resets the CPU from sleep mode. The bootloader has to intercept that and instead of executing the sketch as it would with a reset it has to do an "ERET" instruction to return from an exception and resume the sketch from the point the watchdog fell asleep.

The default bootloader code has the NMI handling commented out:

_startup:
        ##################################################################
        # If entered because of an NMI, jump to the NMI handler.
        ##################################################################
#        mfc0    k0,_CP0_STATUS
#        ext     k0,k0,19,1              # Extract NMI bit
#        beqz    k0,_no_nmi
#        nop
#        la      k0,_nmi_handler
#        jr      k0
#        nop
_no_nmi:

I need to get around to re-writing the _nmi_handler routine I had before (or trying to find my old copy) and feeding it back in to the bootloader code.


omega

Thu, 21 Apr 2016 07:06:02 +0000

So, right now it is not possible use watchdog ans sleep mode with Max32 ?


majenko

Thu, 21 Apr 2016 08:43:18 +0000

I have an experimental bootloader if you have any way of programming it into the board (PICkit3, chipKIT PGM, etc). I could do with overhauling the bootloader update sketches to make them easier to work with...


omega

Mon, 04 Jul 2016 09:00:09 +0000

I am still trying to use watchdog and sleep in MAX32 board. :(

Does anybody have an example program?

Thank you