Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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