helpfor studient
Dependents: STM32_F103-C8T6basecanblink_led
Fork of mbed-dev by
hal/mbed_sleep_manager.c@185:08ed48f1de7f, 2018-04-19 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Apr 19 17:12:19 2018 +0100
- Revision:
- 185:08ed48f1de7f
- Parent:
- 174:b96e65c34a4d
mbed-dev library. Release version 161
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 174:b96e65c34a4d | 1 | /* mbed Microcontroller Library |
AnnaBridge | 174:b96e65c34a4d | 2 | * Copyright (c) 2017 ARM Limited |
AnnaBridge | 174:b96e65c34a4d | 3 | * |
AnnaBridge | 174:b96e65c34a4d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 174:b96e65c34a4d | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 174:b96e65c34a4d | 6 | * You may obtain a copy of the License at |
AnnaBridge | 174:b96e65c34a4d | 7 | * |
AnnaBridge | 174:b96e65c34a4d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 174:b96e65c34a4d | 9 | * |
AnnaBridge | 174:b96e65c34a4d | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 174:b96e65c34a4d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 174:b96e65c34a4d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 174:b96e65c34a4d | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 174:b96e65c34a4d | 14 | * limitations under the License. |
AnnaBridge | 174:b96e65c34a4d | 15 | */ |
AnnaBridge | 174:b96e65c34a4d | 16 | |
AnnaBridge | 185:08ed48f1de7f | 17 | #include "mbed_assert.h" |
AnnaBridge | 185:08ed48f1de7f | 18 | #include "mbed_power_mgmt.h" |
AnnaBridge | 174:b96e65c34a4d | 19 | #include "mbed_critical.h" |
AnnaBridge | 174:b96e65c34a4d | 20 | #include "sleep_api.h" |
AnnaBridge | 174:b96e65c34a4d | 21 | #include "mbed_error.h" |
AnnaBridge | 185:08ed48f1de7f | 22 | #include "mbed_debug.h" |
AnnaBridge | 174:b96e65c34a4d | 23 | #include <limits.h> |
AnnaBridge | 185:08ed48f1de7f | 24 | #include <stdio.h> |
AnnaBridge | 174:b96e65c34a4d | 25 | |
AnnaBridge | 174:b96e65c34a4d | 26 | #if DEVICE_SLEEP |
AnnaBridge | 174:b96e65c34a4d | 27 | |
AnnaBridge | 174:b96e65c34a4d | 28 | // deep sleep locking counter. A target is allowed to deep sleep if counter == 0 |
AnnaBridge | 174:b96e65c34a4d | 29 | static uint16_t deep_sleep_lock = 0U; |
AnnaBridge | 174:b96e65c34a4d | 30 | |
AnnaBridge | 185:08ed48f1de7f | 31 | #ifdef MBED_SLEEP_TRACING_ENABLED |
AnnaBridge | 185:08ed48f1de7f | 32 | |
AnnaBridge | 185:08ed48f1de7f | 33 | // Number of drivers that can be stored in the structure |
AnnaBridge | 185:08ed48f1de7f | 34 | #define STATISTIC_COUNT 10 |
AnnaBridge | 185:08ed48f1de7f | 35 | |
AnnaBridge | 185:08ed48f1de7f | 36 | typedef struct sleep_statistic { |
AnnaBridge | 185:08ed48f1de7f | 37 | const char* identifier; |
AnnaBridge | 185:08ed48f1de7f | 38 | uint8_t count; |
AnnaBridge | 185:08ed48f1de7f | 39 | } sleep_statistic_t; |
AnnaBridge | 185:08ed48f1de7f | 40 | |
AnnaBridge | 185:08ed48f1de7f | 41 | static sleep_statistic_t sleep_stats[STATISTIC_COUNT]; |
AnnaBridge | 185:08ed48f1de7f | 42 | |
AnnaBridge | 185:08ed48f1de7f | 43 | static sleep_statistic_t* sleep_tracker_find(const char *const filename) |
AnnaBridge | 185:08ed48f1de7f | 44 | { |
AnnaBridge | 185:08ed48f1de7f | 45 | for (int i = 0; i < STATISTIC_COUNT; ++i) { |
AnnaBridge | 185:08ed48f1de7f | 46 | if (sleep_stats[i].identifier == filename) { |
AnnaBridge | 185:08ed48f1de7f | 47 | return &sleep_stats[i]; |
AnnaBridge | 185:08ed48f1de7f | 48 | } |
AnnaBridge | 185:08ed48f1de7f | 49 | } |
AnnaBridge | 185:08ed48f1de7f | 50 | |
AnnaBridge | 185:08ed48f1de7f | 51 | return NULL; |
AnnaBridge | 185:08ed48f1de7f | 52 | } |
AnnaBridge | 185:08ed48f1de7f | 53 | |
AnnaBridge | 185:08ed48f1de7f | 54 | static sleep_statistic_t* sleep_tracker_add(const char* const filename) |
AnnaBridge | 185:08ed48f1de7f | 55 | { |
AnnaBridge | 185:08ed48f1de7f | 56 | for (int i = 0; i < STATISTIC_COUNT; ++i) { |
AnnaBridge | 185:08ed48f1de7f | 57 | if (sleep_stats[i].identifier == NULL) { |
AnnaBridge | 185:08ed48f1de7f | 58 | sleep_stats[i].identifier = filename; |
AnnaBridge | 185:08ed48f1de7f | 59 | |
AnnaBridge | 185:08ed48f1de7f | 60 | return &sleep_stats[i]; |
AnnaBridge | 185:08ed48f1de7f | 61 | } |
AnnaBridge | 185:08ed48f1de7f | 62 | } |
AnnaBridge | 185:08ed48f1de7f | 63 | |
AnnaBridge | 185:08ed48f1de7f | 64 | debug("No free indexes left to use in mbed sleep tracker.\r\n"); |
AnnaBridge | 185:08ed48f1de7f | 65 | |
AnnaBridge | 185:08ed48f1de7f | 66 | return NULL; |
AnnaBridge | 185:08ed48f1de7f | 67 | } |
AnnaBridge | 185:08ed48f1de7f | 68 | |
AnnaBridge | 185:08ed48f1de7f | 69 | static void sleep_tracker_print_stats(void) |
AnnaBridge | 185:08ed48f1de7f | 70 | { |
AnnaBridge | 185:08ed48f1de7f | 71 | debug("Sleep locks held:\r\n"); |
AnnaBridge | 185:08ed48f1de7f | 72 | for (int i = 0; i < STATISTIC_COUNT; ++i) { |
AnnaBridge | 185:08ed48f1de7f | 73 | if (sleep_stats[i].count == 0) { |
AnnaBridge | 185:08ed48f1de7f | 74 | continue; |
AnnaBridge | 185:08ed48f1de7f | 75 | } |
AnnaBridge | 185:08ed48f1de7f | 76 | |
AnnaBridge | 185:08ed48f1de7f | 77 | if (sleep_stats[i].identifier == NULL) { |
AnnaBridge | 185:08ed48f1de7f | 78 | return; |
AnnaBridge | 185:08ed48f1de7f | 79 | } |
AnnaBridge | 185:08ed48f1de7f | 80 | |
AnnaBridge | 185:08ed48f1de7f | 81 | debug("[id: %s, count: %u]\r\n", sleep_stats[i].identifier, |
AnnaBridge | 185:08ed48f1de7f | 82 | sleep_stats[i].count); |
AnnaBridge | 185:08ed48f1de7f | 83 | } |
AnnaBridge | 185:08ed48f1de7f | 84 | } |
AnnaBridge | 185:08ed48f1de7f | 85 | |
AnnaBridge | 185:08ed48f1de7f | 86 | void sleep_tracker_lock(const char* const filename, int line) |
AnnaBridge | 185:08ed48f1de7f | 87 | { |
AnnaBridge | 185:08ed48f1de7f | 88 | sleep_statistic_t *stat = sleep_tracker_find(filename); |
AnnaBridge | 185:08ed48f1de7f | 89 | |
AnnaBridge | 185:08ed48f1de7f | 90 | // Entry for this driver does not exist, create one. |
AnnaBridge | 185:08ed48f1de7f | 91 | if (stat == NULL) { |
AnnaBridge | 185:08ed48f1de7f | 92 | stat = sleep_tracker_add(filename); |
AnnaBridge | 185:08ed48f1de7f | 93 | } |
AnnaBridge | 185:08ed48f1de7f | 94 | |
AnnaBridge | 185:08ed48f1de7f | 95 | core_util_atomic_incr_u8(&stat->count, 1); |
AnnaBridge | 185:08ed48f1de7f | 96 | |
AnnaBridge | 185:08ed48f1de7f | 97 | debug("LOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock); |
AnnaBridge | 185:08ed48f1de7f | 98 | } |
AnnaBridge | 185:08ed48f1de7f | 99 | |
AnnaBridge | 185:08ed48f1de7f | 100 | void sleep_tracker_unlock(const char* const filename, int line) |
AnnaBridge | 185:08ed48f1de7f | 101 | { |
AnnaBridge | 185:08ed48f1de7f | 102 | sleep_statistic_t *stat = sleep_tracker_find(filename); |
AnnaBridge | 185:08ed48f1de7f | 103 | |
AnnaBridge | 185:08ed48f1de7f | 104 | // Entry for this driver does not exist, something went wrong. |
AnnaBridge | 185:08ed48f1de7f | 105 | if (stat == NULL) { |
AnnaBridge | 185:08ed48f1de7f | 106 | debug("Unlocking sleep for driver that was not previously locked: %s, ln: %i\r\n", filename, line); |
AnnaBridge | 185:08ed48f1de7f | 107 | return; |
AnnaBridge | 185:08ed48f1de7f | 108 | } |
AnnaBridge | 185:08ed48f1de7f | 109 | |
AnnaBridge | 185:08ed48f1de7f | 110 | core_util_atomic_decr_u8(&stat->count, 1); |
AnnaBridge | 185:08ed48f1de7f | 111 | |
AnnaBridge | 185:08ed48f1de7f | 112 | debug("UNLOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock); |
AnnaBridge | 185:08ed48f1de7f | 113 | } |
AnnaBridge | 185:08ed48f1de7f | 114 | |
AnnaBridge | 185:08ed48f1de7f | 115 | #endif // MBED_SLEEP_TRACING_ENABLED |
AnnaBridge | 185:08ed48f1de7f | 116 | |
AnnaBridge | 185:08ed48f1de7f | 117 | void sleep_manager_lock_deep_sleep_internal(void) |
AnnaBridge | 174:b96e65c34a4d | 118 | { |
AnnaBridge | 174:b96e65c34a4d | 119 | core_util_critical_section_enter(); |
AnnaBridge | 174:b96e65c34a4d | 120 | if (deep_sleep_lock == USHRT_MAX) { |
AnnaBridge | 174:b96e65c34a4d | 121 | core_util_critical_section_exit(); |
AnnaBridge | 174:b96e65c34a4d | 122 | error("Deep sleep lock would overflow (> USHRT_MAX)"); |
AnnaBridge | 174:b96e65c34a4d | 123 | } |
AnnaBridge | 174:b96e65c34a4d | 124 | core_util_atomic_incr_u16(&deep_sleep_lock, 1); |
AnnaBridge | 174:b96e65c34a4d | 125 | core_util_critical_section_exit(); |
AnnaBridge | 174:b96e65c34a4d | 126 | } |
AnnaBridge | 174:b96e65c34a4d | 127 | |
AnnaBridge | 185:08ed48f1de7f | 128 | void sleep_manager_unlock_deep_sleep_internal(void) |
AnnaBridge | 174:b96e65c34a4d | 129 | { |
AnnaBridge | 174:b96e65c34a4d | 130 | core_util_critical_section_enter(); |
AnnaBridge | 174:b96e65c34a4d | 131 | if (deep_sleep_lock == 0) { |
AnnaBridge | 174:b96e65c34a4d | 132 | core_util_critical_section_exit(); |
AnnaBridge | 174:b96e65c34a4d | 133 | error("Deep sleep lock would underflow (< 0)"); |
AnnaBridge | 174:b96e65c34a4d | 134 | } |
AnnaBridge | 174:b96e65c34a4d | 135 | core_util_atomic_decr_u16(&deep_sleep_lock, 1); |
AnnaBridge | 174:b96e65c34a4d | 136 | core_util_critical_section_exit(); |
AnnaBridge | 174:b96e65c34a4d | 137 | } |
AnnaBridge | 174:b96e65c34a4d | 138 | |
AnnaBridge | 174:b96e65c34a4d | 139 | bool sleep_manager_can_deep_sleep(void) |
AnnaBridge | 174:b96e65c34a4d | 140 | { |
AnnaBridge | 174:b96e65c34a4d | 141 | return deep_sleep_lock == 0 ? true : false; |
AnnaBridge | 174:b96e65c34a4d | 142 | } |
AnnaBridge | 174:b96e65c34a4d | 143 | |
AnnaBridge | 174:b96e65c34a4d | 144 | void sleep_manager_sleep_auto(void) |
AnnaBridge | 174:b96e65c34a4d | 145 | { |
AnnaBridge | 185:08ed48f1de7f | 146 | #ifdef MBED_SLEEP_TRACING_ENABLED |
AnnaBridge | 185:08ed48f1de7f | 147 | sleep_tracker_print_stats(); |
AnnaBridge | 185:08ed48f1de7f | 148 | #endif |
AnnaBridge | 174:b96e65c34a4d | 149 | core_util_critical_section_enter(); |
AnnaBridge | 174:b96e65c34a4d | 150 | // debug profile should keep debuggers attached, no deep sleep allowed |
AnnaBridge | 174:b96e65c34a4d | 151 | #ifdef MBED_DEBUG |
AnnaBridge | 174:b96e65c34a4d | 152 | hal_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 153 | #else |
AnnaBridge | 174:b96e65c34a4d | 154 | if (sleep_manager_can_deep_sleep()) { |
AnnaBridge | 174:b96e65c34a4d | 155 | hal_deepsleep(); |
AnnaBridge | 174:b96e65c34a4d | 156 | } else { |
AnnaBridge | 174:b96e65c34a4d | 157 | hal_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 158 | } |
AnnaBridge | 174:b96e65c34a4d | 159 | #endif |
AnnaBridge | 174:b96e65c34a4d | 160 | core_util_critical_section_exit(); |
AnnaBridge | 174:b96e65c34a4d | 161 | } |
AnnaBridge | 174:b96e65c34a4d | 162 | |
AnnaBridge | 174:b96e65c34a4d | 163 | #else |
AnnaBridge | 174:b96e65c34a4d | 164 | |
AnnaBridge | 174:b96e65c34a4d | 165 | // locking is valid only if DEVICE_SLEEP is defined |
AnnaBridge | 174:b96e65c34a4d | 166 | // we provide empty implementation |
AnnaBridge | 174:b96e65c34a4d | 167 | |
AnnaBridge | 185:08ed48f1de7f | 168 | void sleep_manager_lock_deep_sleep_internal(void) |
AnnaBridge | 174:b96e65c34a4d | 169 | { |
AnnaBridge | 174:b96e65c34a4d | 170 | |
AnnaBridge | 174:b96e65c34a4d | 171 | } |
AnnaBridge | 174:b96e65c34a4d | 172 | |
AnnaBridge | 185:08ed48f1de7f | 173 | void sleep_manager_unlock_deep_sleep_internal(void) |
AnnaBridge | 174:b96e65c34a4d | 174 | { |
AnnaBridge | 174:b96e65c34a4d | 175 | |
AnnaBridge | 174:b96e65c34a4d | 176 | } |
AnnaBridge | 174:b96e65c34a4d | 177 | |
AnnaBridge | 174:b96e65c34a4d | 178 | bool sleep_manager_can_deep_sleep(void) |
AnnaBridge | 174:b96e65c34a4d | 179 | { |
AnnaBridge | 174:b96e65c34a4d | 180 | // no sleep implemented |
AnnaBridge | 174:b96e65c34a4d | 181 | return false; |
AnnaBridge | 174:b96e65c34a4d | 182 | } |
AnnaBridge | 174:b96e65c34a4d | 183 | |
AnnaBridge | 174:b96e65c34a4d | 184 | #endif |