Knight KE
/
Game_Master
游戏王对战板,目前code还是空的
mbed-os/hal/mbed_sleep_manager.c@0:9b3d4731edbb, 2018-06-21 (annotated)
- Committer:
- WFKnight
- Date:
- Thu Jun 21 13:51:43 2018 +0000
- Revision:
- 0:9b3d4731edbb
UART, RTOS, LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WFKnight | 0:9b3d4731edbb | 1 | /* mbed Microcontroller Library |
WFKnight | 0:9b3d4731edbb | 2 | * Copyright (c) 2017 ARM Limited |
WFKnight | 0:9b3d4731edbb | 3 | * |
WFKnight | 0:9b3d4731edbb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
WFKnight | 0:9b3d4731edbb | 5 | * you may not use this file except in compliance with the License. |
WFKnight | 0:9b3d4731edbb | 6 | * You may obtain a copy of the License at |
WFKnight | 0:9b3d4731edbb | 7 | * |
WFKnight | 0:9b3d4731edbb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
WFKnight | 0:9b3d4731edbb | 9 | * |
WFKnight | 0:9b3d4731edbb | 10 | * Unless required by applicable law or agreed to in writing, software |
WFKnight | 0:9b3d4731edbb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
WFKnight | 0:9b3d4731edbb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
WFKnight | 0:9b3d4731edbb | 13 | * See the License for the specific language governing permissions and |
WFKnight | 0:9b3d4731edbb | 14 | * limitations under the License. |
WFKnight | 0:9b3d4731edbb | 15 | */ |
WFKnight | 0:9b3d4731edbb | 16 | |
WFKnight | 0:9b3d4731edbb | 17 | #include "mbed_assert.h" |
WFKnight | 0:9b3d4731edbb | 18 | #include "mbed_power_mgmt.h" |
WFKnight | 0:9b3d4731edbb | 19 | #include "mbed_critical.h" |
WFKnight | 0:9b3d4731edbb | 20 | #include "sleep_api.h" |
WFKnight | 0:9b3d4731edbb | 21 | #include "mbed_error.h" |
WFKnight | 0:9b3d4731edbb | 22 | #include "mbed_debug.h" |
WFKnight | 0:9b3d4731edbb | 23 | #include "mbed_stats.h" |
WFKnight | 0:9b3d4731edbb | 24 | #include "lp_ticker_api.h" |
WFKnight | 0:9b3d4731edbb | 25 | #include <limits.h> |
WFKnight | 0:9b3d4731edbb | 26 | #include <stdio.h> |
WFKnight | 0:9b3d4731edbb | 27 | #include "mbed_stats.h" |
WFKnight | 0:9b3d4731edbb | 28 | |
WFKnight | 0:9b3d4731edbb | 29 | |
WFKnight | 0:9b3d4731edbb | 30 | #if DEVICE_SLEEP |
WFKnight | 0:9b3d4731edbb | 31 | |
WFKnight | 0:9b3d4731edbb | 32 | // deep sleep locking counter. A target is allowed to deep sleep if counter == 0 |
WFKnight | 0:9b3d4731edbb | 33 | static uint16_t deep_sleep_lock = 0U; |
WFKnight | 0:9b3d4731edbb | 34 | static us_timestamp_t sleep_time = 0; |
WFKnight | 0:9b3d4731edbb | 35 | static us_timestamp_t deep_sleep_time = 0; |
WFKnight | 0:9b3d4731edbb | 36 | |
WFKnight | 0:9b3d4731edbb | 37 | #if defined(MBED_CPU_STATS_ENABLED) && defined(DEVICE_LPTICKER) |
WFKnight | 0:9b3d4731edbb | 38 | static ticker_data_t *sleep_ticker = NULL; |
WFKnight | 0:9b3d4731edbb | 39 | #endif |
WFKnight | 0:9b3d4731edbb | 40 | |
WFKnight | 0:9b3d4731edbb | 41 | static inline us_timestamp_t read_us(void) |
WFKnight | 0:9b3d4731edbb | 42 | { |
WFKnight | 0:9b3d4731edbb | 43 | #if defined(MBED_CPU_STATS_ENABLED) && defined(DEVICE_LPTICKER) |
WFKnight | 0:9b3d4731edbb | 44 | if (NULL == sleep_ticker) { |
WFKnight | 0:9b3d4731edbb | 45 | sleep_ticker = (ticker_data_t *)get_lp_ticker_data(); |
WFKnight | 0:9b3d4731edbb | 46 | } |
WFKnight | 0:9b3d4731edbb | 47 | return ticker_read_us(sleep_ticker); |
WFKnight | 0:9b3d4731edbb | 48 | #else |
WFKnight | 0:9b3d4731edbb | 49 | return 0; |
WFKnight | 0:9b3d4731edbb | 50 | #endif |
WFKnight | 0:9b3d4731edbb | 51 | } |
WFKnight | 0:9b3d4731edbb | 52 | |
WFKnight | 0:9b3d4731edbb | 53 | us_timestamp_t mbed_time_idle(void) |
WFKnight | 0:9b3d4731edbb | 54 | { |
WFKnight | 0:9b3d4731edbb | 55 | return (sleep_time + deep_sleep_time); |
WFKnight | 0:9b3d4731edbb | 56 | } |
WFKnight | 0:9b3d4731edbb | 57 | |
WFKnight | 0:9b3d4731edbb | 58 | us_timestamp_t mbed_uptime(void) |
WFKnight | 0:9b3d4731edbb | 59 | { |
WFKnight | 0:9b3d4731edbb | 60 | return read_us(); |
WFKnight | 0:9b3d4731edbb | 61 | } |
WFKnight | 0:9b3d4731edbb | 62 | |
WFKnight | 0:9b3d4731edbb | 63 | us_timestamp_t mbed_time_sleep(void) |
WFKnight | 0:9b3d4731edbb | 64 | { |
WFKnight | 0:9b3d4731edbb | 65 | return sleep_time; |
WFKnight | 0:9b3d4731edbb | 66 | } |
WFKnight | 0:9b3d4731edbb | 67 | |
WFKnight | 0:9b3d4731edbb | 68 | us_timestamp_t mbed_time_deepsleep(void) |
WFKnight | 0:9b3d4731edbb | 69 | { |
WFKnight | 0:9b3d4731edbb | 70 | return deep_sleep_time; |
WFKnight | 0:9b3d4731edbb | 71 | } |
WFKnight | 0:9b3d4731edbb | 72 | |
WFKnight | 0:9b3d4731edbb | 73 | #ifdef MBED_SLEEP_TRACING_ENABLED |
WFKnight | 0:9b3d4731edbb | 74 | |
WFKnight | 0:9b3d4731edbb | 75 | // Number of drivers that can be stored in the structure |
WFKnight | 0:9b3d4731edbb | 76 | #define STATISTIC_COUNT 10 |
WFKnight | 0:9b3d4731edbb | 77 | |
WFKnight | 0:9b3d4731edbb | 78 | typedef struct sleep_statistic { |
WFKnight | 0:9b3d4731edbb | 79 | const char *identifier; |
WFKnight | 0:9b3d4731edbb | 80 | uint8_t count; |
WFKnight | 0:9b3d4731edbb | 81 | } sleep_statistic_t; |
WFKnight | 0:9b3d4731edbb | 82 | |
WFKnight | 0:9b3d4731edbb | 83 | static sleep_statistic_t sleep_stats[STATISTIC_COUNT]; |
WFKnight | 0:9b3d4731edbb | 84 | |
WFKnight | 0:9b3d4731edbb | 85 | static sleep_statistic_t* sleep_tracker_find(const char *const filename) |
WFKnight | 0:9b3d4731edbb | 86 | { |
WFKnight | 0:9b3d4731edbb | 87 | for (int i = 0; i < STATISTIC_COUNT; ++i) { |
WFKnight | 0:9b3d4731edbb | 88 | if (sleep_stats[i].identifier == filename) { |
WFKnight | 0:9b3d4731edbb | 89 | return &sleep_stats[i]; |
WFKnight | 0:9b3d4731edbb | 90 | } |
WFKnight | 0:9b3d4731edbb | 91 | } |
WFKnight | 0:9b3d4731edbb | 92 | |
WFKnight | 0:9b3d4731edbb | 93 | return NULL; |
WFKnight | 0:9b3d4731edbb | 94 | } |
WFKnight | 0:9b3d4731edbb | 95 | |
WFKnight | 0:9b3d4731edbb | 96 | static sleep_statistic_t* sleep_tracker_add(const char* const filename) |
WFKnight | 0:9b3d4731edbb | 97 | { |
WFKnight | 0:9b3d4731edbb | 98 | for (int i = 0; i < STATISTIC_COUNT; ++i) { |
WFKnight | 0:9b3d4731edbb | 99 | if (sleep_stats[i].identifier == NULL) { |
WFKnight | 0:9b3d4731edbb | 100 | sleep_stats[i].identifier = filename; |
WFKnight | 0:9b3d4731edbb | 101 | |
WFKnight | 0:9b3d4731edbb | 102 | return &sleep_stats[i]; |
WFKnight | 0:9b3d4731edbb | 103 | } |
WFKnight | 0:9b3d4731edbb | 104 | } |
WFKnight | 0:9b3d4731edbb | 105 | |
WFKnight | 0:9b3d4731edbb | 106 | debug("No free indexes left to use in mbed sleep tracker.\r\n"); |
WFKnight | 0:9b3d4731edbb | 107 | |
WFKnight | 0:9b3d4731edbb | 108 | return NULL; |
WFKnight | 0:9b3d4731edbb | 109 | } |
WFKnight | 0:9b3d4731edbb | 110 | |
WFKnight | 0:9b3d4731edbb | 111 | static void sleep_tracker_print_stats(void) |
WFKnight | 0:9b3d4731edbb | 112 | { |
WFKnight | 0:9b3d4731edbb | 113 | debug("Sleep locks held:\r\n"); |
WFKnight | 0:9b3d4731edbb | 114 | for (int i = 0; i < STATISTIC_COUNT; ++i) { |
WFKnight | 0:9b3d4731edbb | 115 | if (sleep_stats[i].count == 0) { |
WFKnight | 0:9b3d4731edbb | 116 | continue; |
WFKnight | 0:9b3d4731edbb | 117 | } |
WFKnight | 0:9b3d4731edbb | 118 | |
WFKnight | 0:9b3d4731edbb | 119 | if (sleep_stats[i].identifier == NULL) { |
WFKnight | 0:9b3d4731edbb | 120 | return; |
WFKnight | 0:9b3d4731edbb | 121 | } |
WFKnight | 0:9b3d4731edbb | 122 | |
WFKnight | 0:9b3d4731edbb | 123 | debug("[id: %s, count: %u]\r\n", sleep_stats[i].identifier, |
WFKnight | 0:9b3d4731edbb | 124 | sleep_stats[i].count); |
WFKnight | 0:9b3d4731edbb | 125 | } |
WFKnight | 0:9b3d4731edbb | 126 | } |
WFKnight | 0:9b3d4731edbb | 127 | |
WFKnight | 0:9b3d4731edbb | 128 | void sleep_tracker_lock(const char *const filename, int line) |
WFKnight | 0:9b3d4731edbb | 129 | { |
WFKnight | 0:9b3d4731edbb | 130 | sleep_statistic_t *stat = sleep_tracker_find(filename); |
WFKnight | 0:9b3d4731edbb | 131 | |
WFKnight | 0:9b3d4731edbb | 132 | // Entry for this driver does not exist, create one. |
WFKnight | 0:9b3d4731edbb | 133 | if (stat == NULL) { |
WFKnight | 0:9b3d4731edbb | 134 | stat = sleep_tracker_add(filename); |
WFKnight | 0:9b3d4731edbb | 135 | } |
WFKnight | 0:9b3d4731edbb | 136 | |
WFKnight | 0:9b3d4731edbb | 137 | core_util_atomic_incr_u8(&stat->count, 1); |
WFKnight | 0:9b3d4731edbb | 138 | |
WFKnight | 0:9b3d4731edbb | 139 | debug("LOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock); |
WFKnight | 0:9b3d4731edbb | 140 | } |
WFKnight | 0:9b3d4731edbb | 141 | |
WFKnight | 0:9b3d4731edbb | 142 | void sleep_tracker_unlock(const char* const filename, int line) |
WFKnight | 0:9b3d4731edbb | 143 | { |
WFKnight | 0:9b3d4731edbb | 144 | sleep_statistic_t *stat = sleep_tracker_find(filename); |
WFKnight | 0:9b3d4731edbb | 145 | |
WFKnight | 0:9b3d4731edbb | 146 | // Entry for this driver does not exist, something went wrong. |
WFKnight | 0:9b3d4731edbb | 147 | if (stat == NULL) { |
WFKnight | 0:9b3d4731edbb | 148 | debug("Unlocking sleep for driver that was not previously locked: %s, ln: %i\r\n", filename, line); |
WFKnight | 0:9b3d4731edbb | 149 | return; |
WFKnight | 0:9b3d4731edbb | 150 | } |
WFKnight | 0:9b3d4731edbb | 151 | |
WFKnight | 0:9b3d4731edbb | 152 | core_util_atomic_decr_u8(&stat->count, 1); |
WFKnight | 0:9b3d4731edbb | 153 | |
WFKnight | 0:9b3d4731edbb | 154 | debug("UNLOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock); |
WFKnight | 0:9b3d4731edbb | 155 | } |
WFKnight | 0:9b3d4731edbb | 156 | |
WFKnight | 0:9b3d4731edbb | 157 | #endif // MBED_SLEEP_TRACING_ENABLED |
WFKnight | 0:9b3d4731edbb | 158 | |
WFKnight | 0:9b3d4731edbb | 159 | void sleep_manager_lock_deep_sleep_internal(void) |
WFKnight | 0:9b3d4731edbb | 160 | { |
WFKnight | 0:9b3d4731edbb | 161 | core_util_critical_section_enter(); |
WFKnight | 0:9b3d4731edbb | 162 | if (deep_sleep_lock == USHRT_MAX) { |
WFKnight | 0:9b3d4731edbb | 163 | core_util_critical_section_exit(); |
WFKnight | 0:9b3d4731edbb | 164 | MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_OVERFLOW), "DeepSleepLock overflow (> USHRT_MAX)", deep_sleep_lock); |
WFKnight | 0:9b3d4731edbb | 165 | } |
WFKnight | 0:9b3d4731edbb | 166 | core_util_atomic_incr_u16(&deep_sleep_lock, 1); |
WFKnight | 0:9b3d4731edbb | 167 | core_util_critical_section_exit(); |
WFKnight | 0:9b3d4731edbb | 168 | } |
WFKnight | 0:9b3d4731edbb | 169 | |
WFKnight | 0:9b3d4731edbb | 170 | void sleep_manager_unlock_deep_sleep_internal(void) |
WFKnight | 0:9b3d4731edbb | 171 | { |
WFKnight | 0:9b3d4731edbb | 172 | core_util_critical_section_enter(); |
WFKnight | 0:9b3d4731edbb | 173 | if (deep_sleep_lock == 0) { |
WFKnight | 0:9b3d4731edbb | 174 | core_util_critical_section_exit(); |
WFKnight | 0:9b3d4731edbb | 175 | MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNDERFLOW), "DeepSleepLock underflow (< 0)", deep_sleep_lock); |
WFKnight | 0:9b3d4731edbb | 176 | } |
WFKnight | 0:9b3d4731edbb | 177 | core_util_atomic_decr_u16(&deep_sleep_lock, 1); |
WFKnight | 0:9b3d4731edbb | 178 | core_util_critical_section_exit(); |
WFKnight | 0:9b3d4731edbb | 179 | } |
WFKnight | 0:9b3d4731edbb | 180 | |
WFKnight | 0:9b3d4731edbb | 181 | bool sleep_manager_can_deep_sleep(void) |
WFKnight | 0:9b3d4731edbb | 182 | { |
WFKnight | 0:9b3d4731edbb | 183 | return deep_sleep_lock == 0 ? true : false; |
WFKnight | 0:9b3d4731edbb | 184 | } |
WFKnight | 0:9b3d4731edbb | 185 | |
WFKnight | 0:9b3d4731edbb | 186 | void sleep_manager_sleep_auto(void) |
WFKnight | 0:9b3d4731edbb | 187 | { |
WFKnight | 0:9b3d4731edbb | 188 | #ifdef MBED_SLEEP_TRACING_ENABLED |
WFKnight | 0:9b3d4731edbb | 189 | sleep_tracker_print_stats(); |
WFKnight | 0:9b3d4731edbb | 190 | #endif |
WFKnight | 0:9b3d4731edbb | 191 | core_util_critical_section_enter(); |
WFKnight | 0:9b3d4731edbb | 192 | us_timestamp_t start = read_us(); |
WFKnight | 0:9b3d4731edbb | 193 | bool deep = false; |
WFKnight | 0:9b3d4731edbb | 194 | |
WFKnight | 0:9b3d4731edbb | 195 | // debug profile should keep debuggers attached, no deep sleep allowed |
WFKnight | 0:9b3d4731edbb | 196 | #ifdef MBED_DEBUG |
WFKnight | 0:9b3d4731edbb | 197 | hal_sleep(); |
WFKnight | 0:9b3d4731edbb | 198 | #else |
WFKnight | 0:9b3d4731edbb | 199 | if (sleep_manager_can_deep_sleep()) { |
WFKnight | 0:9b3d4731edbb | 200 | deep = true; |
WFKnight | 0:9b3d4731edbb | 201 | hal_deepsleep(); |
WFKnight | 0:9b3d4731edbb | 202 | } else { |
WFKnight | 0:9b3d4731edbb | 203 | hal_sleep(); |
WFKnight | 0:9b3d4731edbb | 204 | } |
WFKnight | 0:9b3d4731edbb | 205 | #endif |
WFKnight | 0:9b3d4731edbb | 206 | |
WFKnight | 0:9b3d4731edbb | 207 | us_timestamp_t end = read_us(); |
WFKnight | 0:9b3d4731edbb | 208 | if (true == deep) { |
WFKnight | 0:9b3d4731edbb | 209 | deep_sleep_time += end - start; |
WFKnight | 0:9b3d4731edbb | 210 | } else { |
WFKnight | 0:9b3d4731edbb | 211 | sleep_time += end - start; |
WFKnight | 0:9b3d4731edbb | 212 | } |
WFKnight | 0:9b3d4731edbb | 213 | core_util_critical_section_exit(); |
WFKnight | 0:9b3d4731edbb | 214 | } |
WFKnight | 0:9b3d4731edbb | 215 | |
WFKnight | 0:9b3d4731edbb | 216 | #else |
WFKnight | 0:9b3d4731edbb | 217 | |
WFKnight | 0:9b3d4731edbb | 218 | // locking is valid only if DEVICE_SLEEP is defined |
WFKnight | 0:9b3d4731edbb | 219 | // we provide empty implementation |
WFKnight | 0:9b3d4731edbb | 220 | |
WFKnight | 0:9b3d4731edbb | 221 | void sleep_manager_lock_deep_sleep_internal(void) |
WFKnight | 0:9b3d4731edbb | 222 | { |
WFKnight | 0:9b3d4731edbb | 223 | |
WFKnight | 0:9b3d4731edbb | 224 | } |
WFKnight | 0:9b3d4731edbb | 225 | |
WFKnight | 0:9b3d4731edbb | 226 | void sleep_manager_unlock_deep_sleep_internal(void) |
WFKnight | 0:9b3d4731edbb | 227 | { |
WFKnight | 0:9b3d4731edbb | 228 | |
WFKnight | 0:9b3d4731edbb | 229 | } |
WFKnight | 0:9b3d4731edbb | 230 | |
WFKnight | 0:9b3d4731edbb | 231 | bool sleep_manager_can_deep_sleep(void) |
WFKnight | 0:9b3d4731edbb | 232 | { |
WFKnight | 0:9b3d4731edbb | 233 | // no sleep implemented |
WFKnight | 0:9b3d4731edbb | 234 | return false; |
WFKnight | 0:9b3d4731edbb | 235 | } |
WFKnight | 0:9b3d4731edbb | 236 | |
WFKnight | 0:9b3d4731edbb | 237 | #endif |