Problem with waking Sodaq Sara from sleep with interrupt

Hi,

So I’m trying to make a Sodaq Sara N211 AFF wake up ( and flash a led) to an internal magnetometer interrupt but I’m not sure this is possible.
I used the low power sketch on the Sodaq site as reference.

This is what my sketch looks like at the moment:

#include <Sodaq_wdt.h>
#include <Sodaq_LSM303AGR.h>

#define DEBUG_STREAM SerialUSB
Sodaq_LSM303AGR accel;

int counter = 0;
volatile bool magInterruptFlag = true;

void setup(){

  // Bootup delay to programm the board.
  delay(5000);

  Wire.begin();// I2C for the accelerometer

  SerialUSB.println("start of setup");
  // Disable the LSM303AGR
  //accel.disableAccelerometer();
  //accel.disableMagnetometer();

  pinMode(LED_GREEN,  OUTPUT);
 pinMode(LED_RED,  OUTPUT);
  pinMode(LED_BUILTIN,  OUTPUT);
  //pinMode(MAG_INT,  OUTPUT);
  pinMode(GPS_ENABLE,  OUTPUT);
  pinMode(SARA_ENABLE,  OUTPUT);

  digitalWrite(LED_GREEN, HIGH);
  digitalWrite(LED_RED, HIGH);
  digitalWrite(LED_BUILTIN, LOW);   // led low=off, high=on
  //digitalWrite(MAG_INT, LOW);       // we need to make this low otherwise this pin on the LSM303AGR starts leaking current
  digitalWrite(GPS_ENABLE, LOW);    // low=poweredoff, high=poweredon
  digitalWrite(SARA_ENABLE, HIGH);  // low=poweredoff, high=poweredon 

// Supported periods on both platforms
  // WDT_PERIOD_1DIV64 = 1/64s
  // WDT_PERIOD_1DIV32 = 1/32s
  // WDT_PERIOD_1DIV16 = 1/16s
  // WDT_PERIOD_1DIV8  = 1/8s
  // WDT_PERIOD_1DIV4  = 1/4s
  // WDT_PERIOD_1DIV2  = 1/2s
  // WDT_PERIOD_1X     = 1s
  // WDT_PERIOD_2X     = 2s
  // WDT_PERIOD_4X     = 4s
  // WDT_PERIOD_8X     = 8s
  // Default parameter is set to WDT_PERIOD_1X = 1s

  //this code is needed to setup watchdogtimer and to make MCU sleep
  sodaq_wdt_enable(WDT_PERIOD_2X);    // watchdog expires in ~8 seconds
  sodaq_wdt_reset();                  // restting the watchdog

  initSleep();
  SerialUSB.println("end of setup");
  
  SerialUSB.flush();
  SerialUSB.end();
  USBDevice.detach();
  USB->DEVICE.CTRLA.reg &= ~USB_CTRLA_ENABLE; // Disable USB
  
}

void loop() {
  sodaq_wdt_reset();                  // restting the watchdog
  
  digitalWrite(LED_BUILTIN, HIGH);
  sodaq_wdt_safe_delay(10);                        
  digitalWrite(LED_BUILTIN, LOW);
  sodaq_wdt_safe_delay(10);
 
  if (magInterruptFlag) {
      //SerialUSB.println("INTERRUPT mag");
      digitalWrite(LED_RED, LOW);
      sodaq_wdt_safe_delay(10);                        
      digitalWrite(LED_RED, HIGH);
      sodaq_wdt_safe_delay(10);
            
      //send_Data();
      magInterruptFlag = false;
  }

  systemSleep();                  
}

/**
 * Initializes the CPU sleep mode.
 */
void initSleep()
{
    // Set the sleep mode
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
}

/**
 * Powers down all devices and puts the system to deep sleep.
 */
void systemSleep()
{
 
  __WFI(); // SAMD sleep

}

void do_flash(int pin){

  for( size_t i = 0 ; i < 2; ++i){
    delay(100);
    digitalWrite(pin, HIGH);
    delay(100);
    digitalWrite(pin, LOW);
    }
}

void magInterrupt() 
{
      
    //magInterruptFlag = true;
    /*
    sodaq_wdt_reset();
    digitalWrite(LED_BUILTIN, HIGH);
    sodaq_wdt_safe_delay(10);                        
    digitalWrite(LED_BUILTIN, LOW);
    sodaq_wdt_safe_delay(10);
    systemSleep();  */
}

void setupMagn(){

    if (accel.checkWhoAmI()) {
        do_flash(LED_GREEN);
    }
    else {
        do_flash(LED_RED);
    }
    
    accel.rebootMagnetometer();
    delay(1000);

    accel.enableMagnetometer(Sodaq_LSM303AGR::MagHighResMode, Sodaq_LSM303AGR::Hz100, Sodaq_LSM303AGR::Continuous);

    uint8_t axes = Sodaq_LSM303AGR::MagZ;
    accel.enableMagnetometerInterrupt(axes, -250);

    pinMode(MAG_INT, INPUT_PULLDOWN);
    attachInterrupt(MAG_INT, magInterrupt, CHANGE);
}

Dear @sheijse,

I believe the following changes should enable the interrupt from sleep.

 void magInterrupt() 
{  
    magInterruptFlag = true;
   // handle the interrupt in the loop 
}

Add the following code after your attachInterrupt

// Configure EIC to use GCLK1 which uses XOSC32K, XOSC32K is already running in standby
    // This has to be done after the first call to attachInterrupt()
    GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(GCM_EIC) |
        GCLK_CLKCTRL_GEN_GCLK1 |
        GCLK_CLKCTRL_CLKEN;

We have a fully functional tracker code, here you can also check how we use the different sleep and wake options.

Let me know if you get it working.

Best regards,
Jan