Sodaq SARA accelerometer wakeup

I am using a SODAQ SARA 412 AFF

I have the following code in setup :

Wire.begin();

accelerometer.rebootAccelerometer();
delay(1000);

accelerometer.enableAccelerometer();	
// Enable the Accelerometer, in 2 G (default)
pinMode(ACC_INT_PIN, INPUT);

I have the following code in loop :

if (condition) gotosleep();

In function gotosleep i have this code :

    pinMode(ACC_INT_PIN, INPUT);
    attachInterrupt(ACC_INT_PIN, interrupt_event, RISING);
	
	
	GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(GCM_EIC) |
		GCLK_CLKCTRL_GEN_GCLK1 |
		GCLK_CLKCTRL_CLKEN;

	accelerometer.enableInterrupt1(accelerometer.XHigh | accelerometer.YHigh, ACC_threshold, 0, accelerometer.MovementRecognition);			debug2("interrupt ready");


	
	pinMode(LED_BUILTIN,  OUTPUT);
	pinMode(MAG_INT,  OUTPUT);
	pinMode(GPS_ENABLE,  OUTPUT);
	pinMode(SARA_ENABLE,  OUTPUT);


	//modemGSM.powerOff();
	digitalWrite(GPS_ENABLE, LOW);    // low=poweredoff, high=poweredon
	digitalWrite(SARA_ENABLE, LOW);  // low=poweredoff, high=poweredon
	digitalWrite(MAG_INT, LOW);   																											debug2("deep sleep");

	SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
	 __WFI();

In function interrupt_event, i have:

void interrupt_event() {

setLedColor(WHITE);

}

The code works, up to the point that the goto_sleep() function is being called. But I cant wake the device up from sleep no matter how hard i shake it - even the LED light wont glow to white (still steady blue as set in the loop function).

What can be done? The datasheet of SAMD21 says

The processor wakes the system up when it detects the occurrence of any interrupt that is not masked in the NVIC Controller with sufficient priority to cause exception entry. The system goes back to the ACTIVE mode. The CPU and affected modules are restarted.

How can I make my system wake up? It is connected to a computer with USB. Taking away the USB also does not change anything.

Hi Sean,

You’re basing your EIC clock off GCLK1, this clock is disabled while the device is asleep. No clock while asleep = no functioning EIC = no wake on shake.

As a first recommendation, you want to make the accelerometer a pulldown.

Secondly, as running GCLK1 in sleep mode is not a good idea, I recommend you use the snippet below.

// enable EIC clock
GCLK->CLKCTRL.bit.CLKEN = 0; //disable GCLK module
while (GCLK->STATUS.bit.SYNCBUSY)
    ;

GCLK->CLKCTRL.reg = (uint16_t)(GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK6 | GCLK_CLKCTRL_ID(GCM_EIC)); //EIC clock switched on GCLK6
while (GCLK->STATUS.bit.SYNCBUSY)
    ;

GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(6)); //source for GCLK6 is OSCULP32K
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
    ;

GCLK->GENCTRL.bit.RUNSTDBY = 1; //GCLK6 run standby
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
    ;

Regards,
Thom

Hi,

I am having the same problem waking SODAQ SARA 412 AFF from sleep using an io pin interrupt. (pin 9 connected to a push button)

pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(BUTTON_PIN, buttonISR, CHANGE);

In this post Problem with waking Sodaq Sara from sleep with interrupt you are recommending to configure EIC to use GCLK1 with a reference to the Sodaq_Universal_Tracker code.

I am a bit confused here.

Hi angeliki,

Although that snippet would work it’s a bit dangerous. Using GCLK1 without configuring it makes you very dependant on what libraries and the arduino core does for you.
By default the arduino core connects the XOSC32K to this clock but does not make it run in sleep.
If you use our RTC code (or most libraries) it will be enabled in sleep mode and connected to the RTC. If you do not, it will not work.

As you can tell this introduces a lot of uncertainties.

If you just want the EIC to work in sleep mode I recommend just using the internal oscillator (this one is designed to be low power) that runs all the time instead.

Regards,
Thom

Hi Thom,

thank you for your answer. Not sure if iI understood your point about the RTC code.

Can you add a reference to sodaq rtc code? or reference to a sodaq library that configures EIC to work in sleepmode ?

I am using RTCZero for timer interrupts without any issues. I believe its using GCLK2

Arduino core lib used is “framework-arduino-samd-sodaq” lib. clocks 0,1,3 are configured in startup.c and indeed are not set to run in standby code.

If I understand correctly for EIC to work you propose to configure GCLK6 referenced by the internal oscillator running in standby mode?

Hi angeliki,

I went a bit on a tangent there, but if you “just” want the EIC to work in sleep mode you can just copy the snippet I posted and it should work.

Regards,
Thom