0

I'am working on a personnal board build with an atsamd21e18a. I'am actually working on sleep mode. I make a function to put samd21 in sleep mode like that. I use RTCZero library.

So in my setup function I've something like this

RTCZero rtc;

void setup(){
    // Set the XOSC32K to run in standby
    SYSCTRL->XOSC32K.bit.RUNSTDBY = 1;

    rtc.begin();   
    .... other line code ....
    attachInterrupt(digitalPinToInterrupt(PIN_USER_BUTTON), wakeUpButtonUser, CHANGE);
    ...other line....
}

So in my setup I intialise rtc and I attach an interrupt in my user button. This button is used to power on or power off my board.

My function goTosleep() :

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

In my function wakeUpButtonUser I have a simple if/else statement. If the user pressed the button more than 3 seconds samd21 go to sleep with goToSleep() function, else if it's less than 3 seconds I want wake up the board and light up my led but it's doesn't work.

Here my wakeUpButtonUser function, user_button is object create from C++ for my button.

void wakeUpButtonUser(){
  if (userButton.isPressed())
  {
    userButton.setTimeStartPressed(millis());
    PRINT_DEBUG_LN("Pressed");
  }
  else
  {
    userButton.setTimeEndPressed(millis());
    PRINT_DEBUG_LN("Released");
    uint32_t time_pressed = userButton.getTimePressed();
    if (time_pressed >= temps_on && time_pressed < temps_off)
    {                  
        PRINT_DEBUG_LN("ON");
         //here I have code to light up a led
    }
    else
    {
        PRINT_DEBUG_LN("STOP");
        goSleepMode();
    }
  }
}

This fuction works because if I comment the line goToSleep(), I can read on my serial ON or OFF depending the time I pressed my button and the led works also because I can light up led before going to sleep. But when my board go to sleep, she's never wake up I don't understand why, I missed something?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
simon
  • 1,180
  • 3
  • 12
  • 33
  • I think you need to use `__WFE();` instead of `_WFI();`. See https://stackoverflow.com/a/18828964/4902099 – hcheung Jun 10 '21 at 03:30
  • Also this one https://www.embedded.com/the-basics-of-low-power-programming-on-the-cortex-m0/ – hcheung Jun 10 '21 at 03:31
  • @hcheung I think the problem was beceause I entered in sleep mode in my function interrupt, if I put `goSleepMode()` in my `loop` I can wake up my board. But thank you for your links it's very interresting also – simon Jun 10 '21 at 07:14

0 Answers0