Rahul Dahiya / Mbed OS STM32F7 Ethernet
Committer:
rahul_dahiya
Date:
Wed Jan 15 15:57:15 2020 +0530
Revision:
0:fb8047b156bb
STM32F7 LWIP

Who changed what in which revision?

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