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