DeepSleepLock
The DeepSleepLock
class provides an RAII object for disabling sleep. In other words, creating a DeepSleepLock object calls its constructor, which increments the deep sleep prevention lock. The DeepSleepLock object automatically releases the deep sleep prevention lock in its destructor when the object goes out of scope. Another way to look at this is when the DeepSleepLock object exists, it prevents deep sleep.
Note: Drivers that don't work in deep sleep mode automatically prevent deep sleep mode, so DeepSleepLock does not need to protect them.
DeepSleepLock class reference
Public Member Functions | |
void | lock () |
Mark the start of a locked deep sleep section. More... | |
void | unlock () |
Mark the end of a locked deep sleep section. More... |
Example
This example shows how you can lock deep sleep to decrease interrupt latency at the expense of increased power consumption.
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
InterruptIn button(BUTTON1);
DigitalOut led(LED1);
void toggle()
{
led = !led;
}
int main()
{
button.rise(&toggle);
button.fall(&toggle);
// Lock deep sleep to decrease interrupt latency
// at the expense of high power consumption
DeepSleepLock lock;
while (1) {
// Wait and let interrupts take care of the rest
ThisThread::sleep_for(1000);
}
}