update
Embed:
(wiki syntax)
Show/hide line numbers
DeepSleepLock.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #ifndef MBED_DEEPSLEEPLOCK_H 00018 #define MBED_DEEPSLEEPLOCK_H 00019 00020 #include <limits.h> 00021 #include "platform/mbed_power_mgmt.h" 00022 #include "platform/mbed_critical.h" 00023 00024 namespace mbed { 00025 00026 /** \addtogroup platform */ 00027 /** @{*/ 00028 /** 00029 * \defgroup platform_DeepSleepLock DeepSleepLock functions 00030 * @{ 00031 */ 00032 00033 /** RAII object for disabling, then restoring the deep sleep mode 00034 * Usage: 00035 * @code 00036 * 00037 * void f() { 00038 * // some code here 00039 * { 00040 * DeepSleepLock lock; 00041 * // Code in this block will run with the deep sleep mode locked 00042 * } 00043 * // deep sleep mode will be restored to their previous state 00044 * } 00045 * @endcode 00046 */ 00047 class DeepSleepLock { 00048 private: 00049 uint16_t _lock_count; 00050 00051 public: 00052 DeepSleepLock(): _lock_count(1) 00053 { 00054 sleep_manager_lock_deep_sleep(); 00055 } 00056 00057 ~DeepSleepLock() 00058 { 00059 if (_lock_count) { 00060 sleep_manager_unlock_deep_sleep(); 00061 } 00062 } 00063 00064 /** Mark the start of a locked deep sleep section 00065 */ 00066 void lock() 00067 { 00068 uint16_t count = core_util_atomic_incr_u16(&_lock_count, 1); 00069 if (1 == count) { 00070 sleep_manager_lock_deep_sleep(); 00071 } 00072 if (0 == count) { 00073 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OVERFLOW), "DeepSleepLock overflow (> USHRT_MAX)", count); 00074 } 00075 } 00076 00077 /** Mark the end of a locked deep sleep section 00078 */ 00079 void unlock() 00080 { 00081 uint16_t count = core_util_atomic_decr_u16(&_lock_count, 1); 00082 if (count == 0) { 00083 sleep_manager_unlock_deep_sleep(); 00084 } 00085 if (count == USHRT_MAX) { 00086 core_util_critical_section_exit(); 00087 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNDERFLOW), "DeepSleepLock underflow (< 0)", count); 00088 } 00089 } 00090 }; 00091 00092 /**@}*/ 00093 00094 /**@}*/ 00095 00096 00097 } 00098 00099 #endif
Generated on Fri Jul 15 2022 12:20:00 by 1.7.2