BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /* mbed Microcontroller Library
borlanic 0:fbdae7e6d805 2 * Copyright (c) 2017 ARM Limited
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:fbdae7e6d805 5 * you may not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 6 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 7 *
borlanic 0:fbdae7e6d805 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 9 *
borlanic 0:fbdae7e6d805 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:fbdae7e6d805 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 13 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 14 * limitations under the License.
borlanic 0:fbdae7e6d805 15 */
borlanic 0:fbdae7e6d805 16 #ifndef MBED_DEEPSLEEPLOCK_H
borlanic 0:fbdae7e6d805 17 #define MBED_DEEPSLEEPLOCK_H
borlanic 0:fbdae7e6d805 18
borlanic 0:fbdae7e6d805 19 #include <limits.h>
borlanic 0:fbdae7e6d805 20 #include "platform/mbed_power_mgmt.h"
borlanic 0:fbdae7e6d805 21 #include "platform/mbed_critical.h"
borlanic 0:fbdae7e6d805 22
borlanic 0:fbdae7e6d805 23 namespace mbed {
borlanic 0:fbdae7e6d805 24
borlanic 0:fbdae7e6d805 25 /** \addtogroup platform */
borlanic 0:fbdae7e6d805 26 /** @{*/
borlanic 0:fbdae7e6d805 27 /**
borlanic 0:fbdae7e6d805 28 * \defgroup platform_DeepSleepLock DeepSleepLock functions
borlanic 0:fbdae7e6d805 29 * @{
borlanic 0:fbdae7e6d805 30 */
borlanic 0:fbdae7e6d805 31
borlanic 0:fbdae7e6d805 32 /** RAII object for disabling, then restoring the deep sleep mode
borlanic 0:fbdae7e6d805 33 * Usage:
borlanic 0:fbdae7e6d805 34 * @code
borlanic 0:fbdae7e6d805 35 *
borlanic 0:fbdae7e6d805 36 * void f() {
borlanic 0:fbdae7e6d805 37 * // some code here
borlanic 0:fbdae7e6d805 38 * {
borlanic 0:fbdae7e6d805 39 * DeepSleepLock lock;
borlanic 0:fbdae7e6d805 40 * // Code in this block will run with the deep sleep mode locked
borlanic 0:fbdae7e6d805 41 * }
borlanic 0:fbdae7e6d805 42 * // deep sleep mode will be restored to their previous state
borlanic 0:fbdae7e6d805 43 * }
borlanic 0:fbdae7e6d805 44 * @endcode
borlanic 0:fbdae7e6d805 45 */
borlanic 0:fbdae7e6d805 46 class DeepSleepLock {
borlanic 0:fbdae7e6d805 47 private:
borlanic 0:fbdae7e6d805 48 uint16_t _lock_count;
borlanic 0:fbdae7e6d805 49
borlanic 0:fbdae7e6d805 50 public:
borlanic 0:fbdae7e6d805 51 DeepSleepLock(): _lock_count(1)
borlanic 0:fbdae7e6d805 52 {
borlanic 0:fbdae7e6d805 53 sleep_manager_lock_deep_sleep();
borlanic 0:fbdae7e6d805 54 }
borlanic 0:fbdae7e6d805 55
borlanic 0:fbdae7e6d805 56 ~DeepSleepLock()
borlanic 0:fbdae7e6d805 57 {
borlanic 0:fbdae7e6d805 58 if (_lock_count) {
borlanic 0:fbdae7e6d805 59 sleep_manager_unlock_deep_sleep();
borlanic 0:fbdae7e6d805 60 }
borlanic 0:fbdae7e6d805 61 }
borlanic 0:fbdae7e6d805 62
borlanic 0:fbdae7e6d805 63 /** Mark the start of a locked deep sleep section
borlanic 0:fbdae7e6d805 64 */
borlanic 0:fbdae7e6d805 65 void lock()
borlanic 0:fbdae7e6d805 66 {
borlanic 0:fbdae7e6d805 67 uint16_t count = core_util_atomic_incr_u16(&_lock_count, 1);
borlanic 0:fbdae7e6d805 68 if (1 == count) {
borlanic 0:fbdae7e6d805 69 sleep_manager_lock_deep_sleep();
borlanic 0:fbdae7e6d805 70 }
borlanic 0:fbdae7e6d805 71 if (0 == count) {
borlanic 0:fbdae7e6d805 72 error("DeepSleepLock overflow (> USHRT_MAX)");
borlanic 0:fbdae7e6d805 73 }
borlanic 0:fbdae7e6d805 74 }
borlanic 0:fbdae7e6d805 75
borlanic 0:fbdae7e6d805 76 /** Mark the end of a locked deep sleep section
borlanic 0:fbdae7e6d805 77 */
borlanic 0:fbdae7e6d805 78 void unlock()
borlanic 0:fbdae7e6d805 79 {
borlanic 0:fbdae7e6d805 80 uint16_t count = core_util_atomic_decr_u16(&_lock_count, 1);
borlanic 0:fbdae7e6d805 81 if (count == 0) {
borlanic 0:fbdae7e6d805 82 sleep_manager_unlock_deep_sleep();
borlanic 0:fbdae7e6d805 83 }
borlanic 0:fbdae7e6d805 84 if (count == USHRT_MAX) {
borlanic 0:fbdae7e6d805 85 core_util_critical_section_exit();
borlanic 0:fbdae7e6d805 86 error("DeepSleepLock underflow (< 0)");
borlanic 0:fbdae7e6d805 87 }
borlanic 0:fbdae7e6d805 88 }
borlanic 0:fbdae7e6d805 89 };
borlanic 0:fbdae7e6d805 90
borlanic 0:fbdae7e6d805 91 /**@}*/
borlanic 0:fbdae7e6d805 92
borlanic 0:fbdae7e6d805 93 /**@}*/
borlanic 0:fbdae7e6d805 94
borlanic 0:fbdae7e6d805 95
borlanic 0:fbdae7e6d805 96 }
borlanic 0:fbdae7e6d805 97
borlanic 0:fbdae7e6d805 98 #endif