Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_sleep.h Source File

mbed_sleep.h

00001 
00002 /** \addtogroup platform */
00003 /** @{*/
00004 /**
00005  * \defgroup platform_sleep Sleep functions
00006  * @{
00007  */
00008  
00009 /* mbed Microcontroller Library
00010  * Copyright (c) 2006-2017 ARM Limited
00011  *
00012  * Licensed under the Apache License, Version 2.0 (the "License");
00013  * you may not use this file except in compliance with the License.
00014  * You may obtain a copy of the License at
00015  *
00016  *     http://www.apache.org/licenses/LICENSE-2.0
00017  *
00018  * Unless required by applicable law or agreed to in writing, software
00019  * distributed under the License is distributed on an "AS IS" BASIS,
00020  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  * See the License for the specific language governing permissions and
00022  * limitations under the License.
00023  */
00024 #ifndef MBED_SLEEP_H
00025 #define MBED_SLEEP_H
00026 
00027 #include "sleep_api.h"
00028 #include "mbed_toolchain.h"
00029 #include <stdbool.h>
00030 
00031 #ifdef __cplusplus
00032 extern "C" {
00033 #endif
00034 
00035 /** Sleep manager API
00036  * The sleep manager provides API to automatically select sleep mode.
00037  *
00038  * There are two sleep modes:
00039  * - sleep
00040  * - deepsleep
00041  *
00042  * Use locking/unlocking deepsleep for drivers that depend on features that
00043  * are not allowed (=disabled) during the deepsleep. For instance, high frequency
00044  * clocks.
00045  *
00046  * Example:
00047  * @code
00048  *
00049  * void driver::handler()
00050  * {
00051  *     if (_sensor.get_event()) {
00052  *         // any event - we are finished, unlock the deepsleep
00053  *         sleep_manager_unlock_deep_sleep();
00054  *         _callback();
00055  *     }
00056  * }
00057  *
00058  * int driver::measure(event_t event, callback_t& callback)
00059  * {
00060  *      _callback = callback;
00061  *      sleep_manager_lock_deep_sleep();
00062  *      // start async transaction, we are waiting for an event
00063  *      return _sensor.start(event, callback);
00064  * }
00065  * @endcode
00066  */
00067 
00068 /** Lock the deep sleep mode
00069  *
00070  * This locks the automatic deep mode selection.
00071  * sleep_manager_sleep_auto() will ignore deepsleep mode if
00072  * this function is invoked at least once (the internal counter is non-zero)
00073  *
00074  * Use this locking mechanism for interrupt driven API that are
00075  * running in the background and deepsleep could affect their functionality
00076  *
00077  * The lock is a counter, can be locked up to USHRT_MAX
00078  * This function is IRQ and thread safe
00079  */
00080 void sleep_manager_lock_deep_sleep(void);
00081 
00082 /** Unlock the deep sleep mode
00083  *
00084  * Use unlocking in pair with sleep_manager_lock_deep_sleep().
00085  *
00086  * The lock is a counter, should be equally unlocked as locked
00087  * This function is IRQ and thread safe
00088  */
00089 void sleep_manager_unlock_deep_sleep(void);
00090 
00091 /** Get the status of deep sleep allowance for a target
00092  *
00093  * @return true if a target can go to deepsleep, false otherwise
00094  */
00095 bool sleep_manager_can_deep_sleep(void);
00096 
00097 /** Enter auto selected sleep mode. It chooses the sleep or deeepsleep modes based
00098  *  on the deepsleep locking counter
00099  *
00100  * This function is IRQ and thread safe
00101  *
00102  * @note
00103  * If MBED_DEBUG is defined, only hal_sleep is allowed. This ensures the debugger
00104  * to be active for debug modes.
00105  *
00106  */
00107 void sleep_manager_sleep_auto(void);
00108 
00109 /** Send the microcontroller to sleep
00110  *
00111  * @note This function can be a noop if not implemented by the platform.
00112  * @note This function will be a noop in debug mode (debug build profile when MBED_DEBUG is defined).
00113  * @note This function will be a noop while uVisor is in use.
00114  * @note This function will be a noop if the following conditions are met:
00115  *   - The RTOS is present
00116  *   - The processor turn off the Systick clock during sleep
00117  *   - The target does not implement tickless mode
00118  *
00119  * The processor is setup ready for sleep, and sent to sleep using __WFI(). In this mode, the
00120  * system clock to the core is stopped until a reset or an interrupt occurs. This eliminates
00121  * dynamic power used by the processor, memory systems and buses. The processor, peripheral and
00122  * memory state are maintained, and the peripherals continue to work and can generate interrupts.
00123  *
00124  * The processor can be woken up by any internal peripheral interrupt or external pin interrupt.
00125  *
00126  * @note
00127  *  The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
00128  * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
00129  * able to access the LocalFileSystem
00130  */
00131 __INLINE static void sleep(void)
00132 {
00133 #if !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED))
00134 #if DEVICE_SLEEP
00135 #if (MBED_CONF_RTOS_PRESENT == 0) || (DEVICE_STCLK_OFF_DURING_SLEEP == 0) || defined(MBED_TICKLESS)
00136     sleep_manager_sleep_auto();
00137 #endif /* (MBED_CONF_RTOS_PRESENT == 0) || (DEVICE_STCLK_OFF_DURING_SLEEP == 0) || defined(MBED_TICKLESS) */
00138 #endif /* DEVICE_SLEEP */
00139 #endif /* !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) */
00140 }
00141 
00142 /** Send the microcontroller to deep sleep
00143  *
00144  * @note This function can be a noop if not implemented by the platform.
00145  * @note This function will be a noop in debug mode (debug build profile when MBED_DEBUG is defined)
00146  * @note This function will be a noop while uVisor is in use.
00147  *
00148  * This processor is setup ready for deep sleep, and sent to sleep. This mode
00149  * has the same sleep features as sleep plus it powers down peripherals and clocks. All state
00150  * is still maintained.
00151  *
00152  * The processor can only be woken up by an external interrupt on a pin or a watchdog timer.
00153  *
00154  * @note
00155  *  The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
00156  * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
00157  * able to access the LocalFileSystem
00158  */
00159 
00160 MBED_DEPRECATED_SINCE("mbed-os-5.6", "One entry point for an application, use sleep()")
00161 __INLINE static void deepsleep(void)
00162 {
00163 #if !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED))
00164 #if DEVICE_SLEEP
00165     sleep_manager_sleep_auto();
00166 #endif /* DEVICE_SLEEP */
00167 #endif /* !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) */
00168 }
00169 
00170 #ifdef __cplusplus
00171 }
00172 #endif
00173 
00174 #endif
00175 
00176 /** @}*/
00177 /** @}*/