init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

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