mirror of https://github.com/ARMmbed/mbed-os.git
Sleep HAL: add sleep manager API
Sleep manager provides API to lock/unlock deepsleep. This API allows a user to control deep sleep. This API should be done via atomic operations (to be IRQ/thread safe).pull/4912/head
parent
5bddd881e9
commit
cb4e9b32a2
|
@ -0,0 +1,71 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "mbed_sleep.h"
|
||||
#include "mbed_critical.h"
|
||||
#include "sleep_api.h"
|
||||
#include "mbed_error.h"
|
||||
#include <limits.h>
|
||||
|
||||
#if DEVICE_SLEEP
|
||||
|
||||
// deep sleep locking counter. A target is allowed to deep sleep if counter == 0
|
||||
static uint16_t deep_sleep_lock = 0U;
|
||||
|
||||
void sleep_manager_lock_deep_sleep(void)
|
||||
{
|
||||
core_util_critical_section_enter();
|
||||
if (deep_sleep_lock == USHRT_MAX) {
|
||||
core_util_critical_section_exit();
|
||||
error("Deep sleep lock would overflow (> USHRT_MAX)");
|
||||
}
|
||||
core_util_atomic_incr_u16(&deep_sleep_lock, 1);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
void sleep_manager_unlock_deep_sleep(void)
|
||||
{
|
||||
core_util_critical_section_enter();
|
||||
if (deep_sleep_lock == 0) {
|
||||
core_util_critical_section_exit();
|
||||
error("Deep sleep lock would underflow (< 0)");
|
||||
}
|
||||
core_util_atomic_decr_u16(&deep_sleep_lock, 1);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
bool sleep_manager_can_deep_sleep(void)
|
||||
{
|
||||
return deep_sleep_lock == 0 ? true : false;
|
||||
}
|
||||
|
||||
void sleep_manager_sleep_auto(void)
|
||||
{
|
||||
core_util_critical_section_enter();
|
||||
// debug profile should keep debuggers attached, no deep sleep allowed
|
||||
#ifdef MBED_DEBUG
|
||||
hal_sleep();
|
||||
#else
|
||||
if (sleep_manager_can_deep_sleep()) {
|
||||
hal_deepsleep();
|
||||
} else {
|
||||
hal_sleep();
|
||||
}
|
||||
#endif
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
#endif
|
|
@ -20,11 +20,87 @@
|
|||
#define MBED_SLEEP_H
|
||||
|
||||
#include "sleep_api.h"
|
||||
#include "mbed_toolchain.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Sleep manager API
|
||||
* The sleep manager provides API to automatically select sleep mode.
|
||||
*
|
||||
* There are two sleep modes:
|
||||
* - sleep
|
||||
* - deepsleep
|
||||
*
|
||||
* Use locking/unlocking deepsleep for drivers that depend on features that
|
||||
* are not allowed (=disabled) during the deepsleep. For instance, high frequency
|
||||
* clocks.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
*
|
||||
* void driver::handler()
|
||||
* {
|
||||
* if (_sensor.get_event()) {
|
||||
* // any event - we are finished, unlock the deepsleep
|
||||
* sleep_manager_unlock_deep_sleep();
|
||||
* _callback();
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* int driver::measure(event_t event, callback_t& callback)
|
||||
* {
|
||||
* _callback = callback;
|
||||
* sleep_manager_lock_deep_sleep();
|
||||
* // start async transaction, we are waiting for an event
|
||||
* return _sensor.start(event, callback);
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
/** Lock the deep sleep mode
|
||||
*
|
||||
* This locks the automatic deep mode selection.
|
||||
* sleep_manager_sleep_auto() will ignore deepsleep mode if
|
||||
* this function is invoked at least once (the internal counter is non-zero)
|
||||
*
|
||||
* Use this locking mechanism for interrupt driven API that are
|
||||
* running in the background and deepsleep could affect their functionality
|
||||
*
|
||||
* The lock is a counter, can be locked up to USHRT_MAX
|
||||
* This function is IRQ and thread safe
|
||||
*/
|
||||
void sleep_manager_lock_deep_sleep(void);
|
||||
|
||||
/** Unlock the deep sleep mode
|
||||
*
|
||||
* Use unlocking in pair with sleep_manager_lock_deep_sleep().
|
||||
*
|
||||
* The lock is a counter, should be equally unlocked as locked
|
||||
* This function is IRQ and thread safe
|
||||
*/
|
||||
void sleep_manager_unlock_deep_sleep(void);
|
||||
|
||||
/** Get the status of deep sleep allowance for a target
|
||||
*
|
||||
* @return true if a target can go to deepsleep, false otherwise
|
||||
*/
|
||||
bool sleep_manager_can_deep_sleep(void);
|
||||
|
||||
/** Enter auto selected sleep mode. It chooses the sleep or deeepsleep modes based
|
||||
* on the deepsleep locking counter
|
||||
*
|
||||
* This function is IRQ and thread safe
|
||||
*
|
||||
* @note
|
||||
* If MBED_DEBUG is defined, only hal_sleep is allowed. This ensures the debugger
|
||||
* to be active for debug modes.
|
||||
*
|
||||
*/
|
||||
void sleep_manager_sleep_auto(void);
|
||||
|
||||
/** Send the microcontroller to sleep
|
||||
*
|
||||
* @note This function can be a noop if not implemented by the platform.
|
||||
|
@ -46,11 +122,9 @@ extern "C" {
|
|||
__INLINE static void sleep(void)
|
||||
{
|
||||
#if !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED))
|
||||
#ifndef MBED_DEBUG
|
||||
#if DEVICE_SLEEP
|
||||
hal_sleep();
|
||||
sleep_manager_sleep_auto();
|
||||
#endif /* DEVICE_SLEEP */
|
||||
#endif /* MBED_DEBUG */
|
||||
#endif /* !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) */
|
||||
}
|
||||
|
||||
|
@ -60,7 +134,7 @@ __INLINE static void sleep(void)
|
|||
* @note This function will be a noop in debug mode (debug build profile when MBED_DEBUG is defined)
|
||||
* @note This function will be a noop while uVisor is in use.
|
||||
*
|
||||
* This processor is setup ready for deep sleep, and sent to sleep using __WFI(). This mode
|
||||
* This processor is setup ready for deep sleep, and sent to sleep. This mode
|
||||
* has the same sleep features as sleep plus it powers down peripherals and clocks. All state
|
||||
* is still maintained.
|
||||
*
|
||||
|
@ -71,14 +145,14 @@ __INLINE static void sleep(void)
|
|||
* Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
|
||||
* able to access the LocalFileSystem
|
||||
*/
|
||||
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.6", "One entry point for an application, use sleep()")
|
||||
__INLINE static void deepsleep(void)
|
||||
{
|
||||
#if !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED))
|
||||
#ifndef MBED_DEBUG
|
||||
#if DEVICE_SLEEP
|
||||
hal_deepsleep();
|
||||
sleep_manager_sleep_auto();
|
||||
#endif /* DEVICE_SLEEP */
|
||||
#endif /* MBED_DEBUG */
|
||||
#endif /* !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) */
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue