001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /* mbed Microcontroller Library
ganlikun 0:13413ea9a877 2 * Copyright (c) 2017 ARM Limited
ganlikun 0:13413ea9a877 3 *
ganlikun 0:13413ea9a877 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:13413ea9a877 5 * you may not use this file except in compliance with the License.
ganlikun 0:13413ea9a877 6 * You may obtain a copy of the License at
ganlikun 0:13413ea9a877 7 *
ganlikun 0:13413ea9a877 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:13413ea9a877 9 *
ganlikun 0:13413ea9a877 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:13413ea9a877 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:13413ea9a877 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:13413ea9a877 13 * See the License for the specific language governing permissions and
ganlikun 0:13413ea9a877 14 * limitations under the License.
ganlikun 0:13413ea9a877 15 */
ganlikun 0:13413ea9a877 16
ganlikun 0:13413ea9a877 17 #include "mbed_sleep.h"
ganlikun 0:13413ea9a877 18 #include "mbed_critical.h"
ganlikun 0:13413ea9a877 19 #include "sleep_api.h"
ganlikun 0:13413ea9a877 20 #include "mbed_error.h"
ganlikun 0:13413ea9a877 21 #include <limits.h>
ganlikun 0:13413ea9a877 22
ganlikun 0:13413ea9a877 23 #if DEVICE_SLEEP
ganlikun 0:13413ea9a877 24
ganlikun 0:13413ea9a877 25 // deep sleep locking counter. A target is allowed to deep sleep if counter == 0
ganlikun 0:13413ea9a877 26 static uint16_t deep_sleep_lock = 0U;
ganlikun 0:13413ea9a877 27
ganlikun 0:13413ea9a877 28 void sleep_manager_lock_deep_sleep(void)
ganlikun 0:13413ea9a877 29 {
ganlikun 0:13413ea9a877 30 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 31 if (deep_sleep_lock == USHRT_MAX) {
ganlikun 0:13413ea9a877 32 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 33 error("Deep sleep lock would overflow (> USHRT_MAX)");
ganlikun 0:13413ea9a877 34 }
ganlikun 0:13413ea9a877 35 core_util_atomic_incr_u16(&deep_sleep_lock, 1);
ganlikun 0:13413ea9a877 36 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 37 }
ganlikun 0:13413ea9a877 38
ganlikun 0:13413ea9a877 39 void sleep_manager_unlock_deep_sleep(void)
ganlikun 0:13413ea9a877 40 {
ganlikun 0:13413ea9a877 41 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 42 if (deep_sleep_lock == 0) {
ganlikun 0:13413ea9a877 43 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 44 error("Deep sleep lock would underflow (< 0)");
ganlikun 0:13413ea9a877 45 }
ganlikun 0:13413ea9a877 46 core_util_atomic_decr_u16(&deep_sleep_lock, 1);
ganlikun 0:13413ea9a877 47 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 48 }
ganlikun 0:13413ea9a877 49
ganlikun 0:13413ea9a877 50 bool sleep_manager_can_deep_sleep(void)
ganlikun 0:13413ea9a877 51 {
ganlikun 0:13413ea9a877 52 return deep_sleep_lock == 0 ? true : false;
ganlikun 0:13413ea9a877 53 }
ganlikun 0:13413ea9a877 54
ganlikun 0:13413ea9a877 55 void sleep_manager_sleep_auto(void)
ganlikun 0:13413ea9a877 56 {
ganlikun 0:13413ea9a877 57 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 58 // debug profile should keep debuggers attached, no deep sleep allowed
ganlikun 0:13413ea9a877 59 #ifdef MBED_DEBUG
ganlikun 0:13413ea9a877 60 hal_sleep();
ganlikun 0:13413ea9a877 61 #else
ganlikun 0:13413ea9a877 62 if (sleep_manager_can_deep_sleep()) {
ganlikun 0:13413ea9a877 63 hal_deepsleep();
ganlikun 0:13413ea9a877 64 } else {
ganlikun 0:13413ea9a877 65 hal_sleep();
ganlikun 0:13413ea9a877 66 }
ganlikun 0:13413ea9a877 67 #endif
ganlikun 0:13413ea9a877 68 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 69 }
ganlikun 0:13413ea9a877 70
ganlikun 0:13413ea9a877 71 #else
ganlikun 0:13413ea9a877 72
ganlikun 0:13413ea9a877 73 // locking is valid only if DEVICE_SLEEP is defined
ganlikun 0:13413ea9a877 74 // we provide empty implementation
ganlikun 0:13413ea9a877 75
ganlikun 0:13413ea9a877 76 void sleep_manager_lock_deep_sleep(void)
ganlikun 0:13413ea9a877 77 {
ganlikun 0:13413ea9a877 78
ganlikun 0:13413ea9a877 79 }
ganlikun 0:13413ea9a877 80
ganlikun 0:13413ea9a877 81 void sleep_manager_unlock_deep_sleep(void)
ganlikun 0:13413ea9a877 82 {
ganlikun 0:13413ea9a877 83
ganlikun 0:13413ea9a877 84 }
ganlikun 0:13413ea9a877 85
ganlikun 0:13413ea9a877 86 bool sleep_manager_can_deep_sleep(void)
ganlikun 0:13413ea9a877 87 {
ganlikun 0:13413ea9a877 88 // no sleep implemented
ganlikun 0:13413ea9a877 89 return false;
ganlikun 0:13413ea9a877 90 }
ganlikun 0:13413ea9a877 91
ganlikun 0:13413ea9a877 92 #endif
ganlikun 0:13413ea9a877 93