6 years, 11 months ago.

Why DeepSleep doesn't halt the program on my nucleo F401RE

Dear all, the problem I encounter is pretty simple. I inserted a "deepsleep" command in my code in which I setup an interrupt for an external rtc and a function to start when interrupt activated. The compiler is OK, the program runs, but it doesn't go to sleep after the instruction "deepsleep". I traced with some "printf" the course of the program, here are below the program and the traces. In the traces, one can see that the program goes before and after "deepsleep" without stopping, and re bounces at the beginning of the "while" until the question asked to the user... Can someone help me to explain why it doesn't stop ? thanks a lot to you experts...

#include "mbed.h"
#include "Ds3231Ext.h"
#include "userEntry.h"

#define TERM_HOME ("")
#define TERM_CLEAR_FROM_CURSOR ("")
#define TERM_CLEAR_EOL ("")

#define DS3231_WRITE_ADRS (0x68 << 1)
#define DS3231_READ_ADRS ((0x68 << 1) | 1)
#define ALARM_1_BIT (1)
#define ALARM_2_BIT (2)
#define STATUS_REG_ADRS (0x0F)

int nbInt = 0;      // interrupt counter
int nbMain = 0;     // main counter
int nbWhile = 0;    // while counter
int beforeSleep = 0;
int afterSleep = 0;
//I2C Bus
I2C i2c(D5, D7);

//DS3231 INT pin connected to A1 with pull-up
InterruptIn rtc_int(A1);

void DS3231Int()
{
    printf("\nEnter   in  DS3231Int \n");   
    //todo
    printf("Exit    of  DS3231Int \n");   
}

int main()
{
    printf("nbMain = %i\n",nbMain++);
    Ds3231Ext rtc(i2c,A1);
    rtc_int.fall(&DS3231Int);
    uint32_t userAnswerNum;
    while(1)
    {
        printf("nbWhile = %i\n",nbWhile++);
        printTime(&rtc);
     
        //get answer from user
        get_user_input("\nDo you want to update time & calendar ? (0->no, 1->yes): ", 0,
            1, &userAnswerNum);   
            
        printf("userAnswerNum = %d\n",userAnswerNum);
        
        if (userAnswerNum == 1)
        {
            // enter time and date on the DS3231
            enterTime(&rtc);
        }
        
        // enter alarm on the DS3231  
        enterAlarm(&rtc); //Alarm1 only
        
         // entering sleep mode
         printf("beforeSleep = %i\n",beforeSleep++);
        deepsleep();
        wait(5);
        printf("afterSleep = %i\n",afterSleep++);
    }    
}

1 Answer

6 years, 11 months ago.

I also had similar problem some weeks ago. I was sure that code for entry or out of sleep should be OK. It should be caused by some unwanted interrupt which wakes the MCU. Because this was commonly seen when I was implementing seeping code with ST's cube demo code. So I was not supprised by this on ST's mbed platform. But I did not spending too much time trying to pinpoint which source was keeping waking my MCU up.

Accepted Answer