lancaster-university / codal-microbit-v2

CODAL target for the micro:bit v2.x series of devices
MIT License
41 stars 50 forks source link

In a tight loop, mostly deep sleeping, takes 600 milliseconds to go back to sleep #405

Open microbit-carlos opened 4 months ago

microbit-carlos commented 4 months ago

With a bare minimum programme like this one:

#include "MicroBit.h"

MicroBit uBit;

int main() {
    uBit.init();

    while (true) {
        uBit.power.deepSleep(1000);
    }
}
image

It takes about 600 msec to fully go to deep sleep:

image
microbit-carlos commented 4 months ago

Okay, if in the infinite loop the programme yields for a second we see a lower time to go to sleep:

    while (true) {
        uBit.power.deepSleep(1000);
        uBit.sleep(1000);
    }

So top level view:

image

If we zoom in, we can see the first half (one second) with uBit.sleep() and the half (one second) with uBit.power.deepSleep():

image image

And if we zoom into at the 2nd half, when it is going into uBit.power.deepSleep(), we can see that it takes about 95 ms to go to sleep:

image

So it must be the combination of going to sleep and waking up in short succession that causes the 600 ms delay.

We can also see that the first 100ms of being awake are spending more power, so that's likely how long it takes for CODAL to try to wake and restore everything.

image