mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:9db0e321a9f4 1 /* mbed Microcontroller Library
kenjiArai 1:9db0e321a9f4 2 * Copyright (c) 2017-2019 ARM Limited
kenjiArai 1:9db0e321a9f4 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 1:9db0e321a9f4 4 *
kenjiArai 1:9db0e321a9f4 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:9db0e321a9f4 6 * you may not use this file except in compliance with the License.
kenjiArai 1:9db0e321a9f4 7 * You may obtain a copy of the License at
kenjiArai 1:9db0e321a9f4 8 *
kenjiArai 1:9db0e321a9f4 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:9db0e321a9f4 10 *
kenjiArai 1:9db0e321a9f4 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:9db0e321a9f4 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:9db0e321a9f4 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:9db0e321a9f4 14 * See the License for the specific language governing permissions and
kenjiArai 1:9db0e321a9f4 15 * limitations under the License.
kenjiArai 1:9db0e321a9f4 16 */
kenjiArai 1:9db0e321a9f4 17
kenjiArai 1:9db0e321a9f4 18 #include "platform/mbed_power_mgmt.h"
kenjiArai 1:9db0e321a9f4 19 #include "platform/mbed_interface.h"
kenjiArai 1:9db0e321a9f4 20 #include "platform/mbed_atomic.h"
kenjiArai 1:9db0e321a9f4 21 #include "platform/mbed_critical.h"
kenjiArai 1:9db0e321a9f4 22 #include "platform/mbed_error.h"
kenjiArai 1:9db0e321a9f4 23 #include "platform/mbed_stats.h"
kenjiArai 1:9db0e321a9f4 24
kenjiArai 1:9db0e321a9f4 25 #include "hal/sleep_api.h"
kenjiArai 1:9db0e321a9f4 26 #include "hal/us_ticker_api.h"
kenjiArai 1:9db0e321a9f4 27 #include "hal/lp_ticker_api.h"
kenjiArai 1:9db0e321a9f4 28 #include "platform/mbed_wait_api.h"
kenjiArai 1:9db0e321a9f4 29
kenjiArai 1:9db0e321a9f4 30 #include <stdio.h>
kenjiArai 1:9db0e321a9f4 31
kenjiArai 1:9db0e321a9f4 32 #if DEVICE_SLEEP
kenjiArai 1:9db0e321a9f4 33
kenjiArai 1:9db0e321a9f4 34 // deep sleep locking counter. A target is allowed to deep sleep if counter == 0
kenjiArai 1:9db0e321a9f4 35 static uint16_t deep_sleep_lock = 0U;
kenjiArai 1:9db0e321a9f4 36 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 37 static us_timestamp_t sleep_time = 0;
kenjiArai 1:9db0e321a9f4 38 static us_timestamp_t deep_sleep_time = 0;
kenjiArai 1:9db0e321a9f4 39
kenjiArai 1:9db0e321a9f4 40 static const ticker_data_t *sleep_ticker = NULL;
kenjiArai 1:9db0e321a9f4 41 #endif
kenjiArai 1:9db0e321a9f4 42
kenjiArai 1:9db0e321a9f4 43 static inline us_timestamp_t read_us(void)
kenjiArai 1:9db0e321a9f4 44 {
kenjiArai 1:9db0e321a9f4 45 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 46 if (NULL == sleep_ticker) {
kenjiArai 1:9db0e321a9f4 47 sleep_ticker = get_lp_ticker_data();
kenjiArai 1:9db0e321a9f4 48 }
kenjiArai 1:9db0e321a9f4 49 return ticker_read_us(sleep_ticker);
kenjiArai 1:9db0e321a9f4 50 #else
kenjiArai 1:9db0e321a9f4 51 return 0;
kenjiArai 1:9db0e321a9f4 52 #endif
kenjiArai 1:9db0e321a9f4 53 }
kenjiArai 1:9db0e321a9f4 54
kenjiArai 1:9db0e321a9f4 55 us_timestamp_t mbed_time_idle(void)
kenjiArai 1:9db0e321a9f4 56 {
kenjiArai 1:9db0e321a9f4 57 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 58 return (sleep_time + deep_sleep_time);
kenjiArai 1:9db0e321a9f4 59 #else
kenjiArai 1:9db0e321a9f4 60 return 0;
kenjiArai 1:9db0e321a9f4 61 #endif
kenjiArai 1:9db0e321a9f4 62 }
kenjiArai 1:9db0e321a9f4 63
kenjiArai 1:9db0e321a9f4 64 us_timestamp_t mbed_uptime(void)
kenjiArai 1:9db0e321a9f4 65 {
kenjiArai 1:9db0e321a9f4 66 return read_us();
kenjiArai 1:9db0e321a9f4 67 }
kenjiArai 1:9db0e321a9f4 68
kenjiArai 1:9db0e321a9f4 69 us_timestamp_t mbed_time_sleep(void)
kenjiArai 1:9db0e321a9f4 70 {
kenjiArai 1:9db0e321a9f4 71 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 72 return sleep_time;
kenjiArai 1:9db0e321a9f4 73 #else
kenjiArai 1:9db0e321a9f4 74 return 0;
kenjiArai 1:9db0e321a9f4 75 #endif
kenjiArai 1:9db0e321a9f4 76 }
kenjiArai 1:9db0e321a9f4 77
kenjiArai 1:9db0e321a9f4 78 us_timestamp_t mbed_time_deepsleep(void)
kenjiArai 1:9db0e321a9f4 79 {
kenjiArai 1:9db0e321a9f4 80 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 81 return deep_sleep_time;
kenjiArai 1:9db0e321a9f4 82 #else
kenjiArai 1:9db0e321a9f4 83 return 0;
kenjiArai 1:9db0e321a9f4 84 #endif
kenjiArai 1:9db0e321a9f4 85 }
kenjiArai 1:9db0e321a9f4 86
kenjiArai 1:9db0e321a9f4 87 #ifdef MBED_SLEEP_TRACING_ENABLED
kenjiArai 1:9db0e321a9f4 88
kenjiArai 1:9db0e321a9f4 89 // Length of the identifier extracted from the driver name to store for logging.
kenjiArai 1:9db0e321a9f4 90 #define IDENTIFIER_WIDTH 15
kenjiArai 1:9db0e321a9f4 91
kenjiArai 1:9db0e321a9f4 92 // Number of drivers that can be stored in the structure
kenjiArai 1:9db0e321a9f4 93 #define STATISTIC_COUNT 10
kenjiArai 1:9db0e321a9f4 94
kenjiArai 1:9db0e321a9f4 95 typedef struct sleep_statistic {
kenjiArai 1:9db0e321a9f4 96 char identifier[IDENTIFIER_WIDTH];
kenjiArai 1:9db0e321a9f4 97 uint8_t count;
kenjiArai 1:9db0e321a9f4 98 } sleep_statistic_t;
kenjiArai 1:9db0e321a9f4 99
kenjiArai 1:9db0e321a9f4 100 static sleep_statistic_t sleep_stats[STATISTIC_COUNT];
kenjiArai 1:9db0e321a9f4 101
kenjiArai 1:9db0e321a9f4 102 static sleep_statistic_t *sleep_tracker_find(const char *const filename)
kenjiArai 1:9db0e321a9f4 103 {
kenjiArai 1:9db0e321a9f4 104 char temp[IDENTIFIER_WIDTH];
kenjiArai 1:9db0e321a9f4 105 strncpy(temp, filename, IDENTIFIER_WIDTH);
kenjiArai 1:9db0e321a9f4 106 temp[IDENTIFIER_WIDTH - 1] = '\0';
kenjiArai 1:9db0e321a9f4 107
kenjiArai 1:9db0e321a9f4 108 // Search for the a driver matching the current name and return it's index
kenjiArai 1:9db0e321a9f4 109 for (int i = 0; i < STATISTIC_COUNT; ++i) {
kenjiArai 1:9db0e321a9f4 110 if (strcmp(sleep_stats[i].identifier, temp) == 0) {
kenjiArai 1:9db0e321a9f4 111 return &sleep_stats[i];
kenjiArai 1:9db0e321a9f4 112 }
kenjiArai 1:9db0e321a9f4 113 }
kenjiArai 1:9db0e321a9f4 114
kenjiArai 1:9db0e321a9f4 115 return NULL;
kenjiArai 1:9db0e321a9f4 116 }
kenjiArai 1:9db0e321a9f4 117
kenjiArai 1:9db0e321a9f4 118 static sleep_statistic_t *sleep_tracker_add(const char *const filename)
kenjiArai 1:9db0e321a9f4 119 {
kenjiArai 1:9db0e321a9f4 120 char temp[IDENTIFIER_WIDTH];
kenjiArai 1:9db0e321a9f4 121 strncpy(temp, filename, IDENTIFIER_WIDTH);
kenjiArai 1:9db0e321a9f4 122 temp[IDENTIFIER_WIDTH - 1] = '\0';
kenjiArai 1:9db0e321a9f4 123
kenjiArai 1:9db0e321a9f4 124 for (int i = 0; i < STATISTIC_COUNT; ++i) {
kenjiArai 1:9db0e321a9f4 125 if (sleep_stats[i].identifier[0] == '\0') {
kenjiArai 1:9db0e321a9f4 126 core_util_critical_section_enter();
kenjiArai 1:9db0e321a9f4 127 strncpy(sleep_stats[i].identifier, temp, sizeof(temp));
kenjiArai 1:9db0e321a9f4 128 core_util_critical_section_exit();
kenjiArai 1:9db0e321a9f4 129
kenjiArai 1:9db0e321a9f4 130 return &sleep_stats[i];
kenjiArai 1:9db0e321a9f4 131 }
kenjiArai 1:9db0e321a9f4 132 }
kenjiArai 1:9db0e321a9f4 133
kenjiArai 1:9db0e321a9f4 134 mbed_error_printf("No free indexes left to use in mbed sleep tracker.\r\n");
kenjiArai 1:9db0e321a9f4 135
kenjiArai 1:9db0e321a9f4 136 return NULL;
kenjiArai 1:9db0e321a9f4 137 }
kenjiArai 1:9db0e321a9f4 138
kenjiArai 1:9db0e321a9f4 139 static void sleep_tracker_print_stats(void)
kenjiArai 1:9db0e321a9f4 140 {
kenjiArai 1:9db0e321a9f4 141 mbed_error_printf("Sleep locks held:\r\n");
kenjiArai 1:9db0e321a9f4 142 for (int i = 0; i < STATISTIC_COUNT; ++i) {
kenjiArai 1:9db0e321a9f4 143 if (sleep_stats[i].count == 0) {
kenjiArai 1:9db0e321a9f4 144 continue;
kenjiArai 1:9db0e321a9f4 145 }
kenjiArai 1:9db0e321a9f4 146
kenjiArai 1:9db0e321a9f4 147 if (sleep_stats[i].identifier[0] == '\0') {
kenjiArai 1:9db0e321a9f4 148 return;
kenjiArai 1:9db0e321a9f4 149 }
kenjiArai 1:9db0e321a9f4 150
kenjiArai 1:9db0e321a9f4 151 mbed_error_printf("[id: %s, count: %u]\r\n", sleep_stats[i].identifier,
kenjiArai 1:9db0e321a9f4 152 sleep_stats[i].count);
kenjiArai 1:9db0e321a9f4 153 }
kenjiArai 1:9db0e321a9f4 154 }
kenjiArai 1:9db0e321a9f4 155
kenjiArai 1:9db0e321a9f4 156 void sleep_tracker_lock(const char *const filename, int line)
kenjiArai 1:9db0e321a9f4 157 {
kenjiArai 1:9db0e321a9f4 158 sleep_statistic_t *stat = sleep_tracker_find(filename);
kenjiArai 1:9db0e321a9f4 159
kenjiArai 1:9db0e321a9f4 160 // Entry for this driver does not exist, create one.
kenjiArai 1:9db0e321a9f4 161 if (stat == NULL) {
kenjiArai 1:9db0e321a9f4 162 stat = sleep_tracker_add(filename);
kenjiArai 1:9db0e321a9f4 163 }
kenjiArai 1:9db0e321a9f4 164
kenjiArai 1:9db0e321a9f4 165 core_util_atomic_incr_u8(&stat->count, 1);
kenjiArai 1:9db0e321a9f4 166
kenjiArai 1:9db0e321a9f4 167 mbed_error_printf("LOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock);
kenjiArai 1:9db0e321a9f4 168 }
kenjiArai 1:9db0e321a9f4 169
kenjiArai 1:9db0e321a9f4 170 void sleep_tracker_unlock(const char *const filename, int line)
kenjiArai 1:9db0e321a9f4 171 {
kenjiArai 1:9db0e321a9f4 172 sleep_statistic_t *stat = sleep_tracker_find(filename);
kenjiArai 1:9db0e321a9f4 173
kenjiArai 1:9db0e321a9f4 174 // Entry for this driver does not exist, something went wrong.
kenjiArai 1:9db0e321a9f4 175 if (stat == NULL) {
kenjiArai 1:9db0e321a9f4 176 mbed_error_printf("Unlocking sleep for driver that was not previously locked: %s, ln: %i\r\n", filename, line);
kenjiArai 1:9db0e321a9f4 177 return;
kenjiArai 1:9db0e321a9f4 178 }
kenjiArai 1:9db0e321a9f4 179
kenjiArai 1:9db0e321a9f4 180 core_util_atomic_decr_u8(&stat->count, 1);
kenjiArai 1:9db0e321a9f4 181
kenjiArai 1:9db0e321a9f4 182 mbed_error_printf("UNLOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock);
kenjiArai 1:9db0e321a9f4 183 }
kenjiArai 1:9db0e321a9f4 184
kenjiArai 1:9db0e321a9f4 185 #endif // MBED_SLEEP_TRACING_ENABLED
kenjiArai 1:9db0e321a9f4 186
kenjiArai 1:9db0e321a9f4 187 void sleep_manager_lock_deep_sleep_internal(void)
kenjiArai 1:9db0e321a9f4 188 {
kenjiArai 1:9db0e321a9f4 189 if (core_util_atomic_incr_u16(&deep_sleep_lock, 1) == 0) {
kenjiArai 1:9db0e321a9f4 190 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_OVERFLOW), "DeepSleepLock overflow (> 0xFFFF)", deep_sleep_lock);
kenjiArai 1:9db0e321a9f4 191 }
kenjiArai 1:9db0e321a9f4 192 }
kenjiArai 1:9db0e321a9f4 193
kenjiArai 1:9db0e321a9f4 194 void sleep_manager_unlock_deep_sleep_internal(void)
kenjiArai 1:9db0e321a9f4 195 {
kenjiArai 1:9db0e321a9f4 196 if (core_util_atomic_decr_u16(&deep_sleep_lock, 1) == 0xFFFF) {
kenjiArai 1:9db0e321a9f4 197 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNDERFLOW), "DeepSleepLock underflow (< 0)", deep_sleep_lock);
kenjiArai 1:9db0e321a9f4 198 }
kenjiArai 1:9db0e321a9f4 199 }
kenjiArai 1:9db0e321a9f4 200
kenjiArai 1:9db0e321a9f4 201 bool sleep_manager_can_deep_sleep(void)
kenjiArai 1:9db0e321a9f4 202 {
kenjiArai 1:9db0e321a9f4 203 return core_util_atomic_load_u16(&deep_sleep_lock) == 0;
kenjiArai 1:9db0e321a9f4 204 }
kenjiArai 1:9db0e321a9f4 205
kenjiArai 1:9db0e321a9f4 206 bool sleep_manager_can_deep_sleep_test_check()
kenjiArai 1:9db0e321a9f4 207 {
kenjiArai 1:9db0e321a9f4 208 uint32_t check_time_ns = 2000000;
kenjiArai 1:9db0e321a9f4 209
kenjiArai 1:9db0e321a9f4 210 while (check_time_ns) {
kenjiArai 1:9db0e321a9f4 211 if (sleep_manager_can_deep_sleep()) {
kenjiArai 1:9db0e321a9f4 212 return true;
kenjiArai 1:9db0e321a9f4 213 }
kenjiArai 1:9db0e321a9f4 214
kenjiArai 1:9db0e321a9f4 215 wait_ns(100000); // 100 us
kenjiArai 1:9db0e321a9f4 216 check_time_ns -= 100000;
kenjiArai 1:9db0e321a9f4 217 }
kenjiArai 1:9db0e321a9f4 218 return false;
kenjiArai 1:9db0e321a9f4 219 }
kenjiArai 1:9db0e321a9f4 220
kenjiArai 1:9db0e321a9f4 221 void sleep_manager_sleep_auto(void)
kenjiArai 1:9db0e321a9f4 222 {
kenjiArai 1:9db0e321a9f4 223 #ifdef MBED_SLEEP_TRACING_ENABLED
kenjiArai 1:9db0e321a9f4 224 sleep_tracker_print_stats();
kenjiArai 1:9db0e321a9f4 225 #endif
kenjiArai 1:9db0e321a9f4 226 core_util_critical_section_enter();
kenjiArai 1:9db0e321a9f4 227 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 228 us_timestamp_t start = read_us();
kenjiArai 1:9db0e321a9f4 229 bool deep = false;
kenjiArai 1:9db0e321a9f4 230 #endif
kenjiArai 1:9db0e321a9f4 231
kenjiArai 1:9db0e321a9f4 232 // debug profile should keep debuggers attached, no deep sleep allowed
kenjiArai 1:9db0e321a9f4 233 #ifdef MBED_DEBUG
kenjiArai 1:9db0e321a9f4 234 hal_sleep();
kenjiArai 1:9db0e321a9f4 235 #else
kenjiArai 1:9db0e321a9f4 236 if (sleep_manager_can_deep_sleep()) {
kenjiArai 1:9db0e321a9f4 237 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 238 deep = true;
kenjiArai 1:9db0e321a9f4 239 #endif
kenjiArai 1:9db0e321a9f4 240 hal_deepsleep();
kenjiArai 1:9db0e321a9f4 241 } else {
kenjiArai 1:9db0e321a9f4 242 hal_sleep();
kenjiArai 1:9db0e321a9f4 243 }
kenjiArai 1:9db0e321a9f4 244 #endif
kenjiArai 1:9db0e321a9f4 245
kenjiArai 1:9db0e321a9f4 246 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 247 us_timestamp_t end = read_us();
kenjiArai 1:9db0e321a9f4 248 if (true == deep) {
kenjiArai 1:9db0e321a9f4 249 deep_sleep_time += end - start;
kenjiArai 1:9db0e321a9f4 250 } else {
kenjiArai 1:9db0e321a9f4 251 sleep_time += end - start;
kenjiArai 1:9db0e321a9f4 252 }
kenjiArai 1:9db0e321a9f4 253 #endif
kenjiArai 1:9db0e321a9f4 254 core_util_critical_section_exit();
kenjiArai 1:9db0e321a9f4 255 }
kenjiArai 1:9db0e321a9f4 256
kenjiArai 1:9db0e321a9f4 257 #else
kenjiArai 1:9db0e321a9f4 258
kenjiArai 1:9db0e321a9f4 259 // locking is valid only if DEVICE_SLEEP is defined
kenjiArai 1:9db0e321a9f4 260 // we provide empty implementation
kenjiArai 1:9db0e321a9f4 261
kenjiArai 1:9db0e321a9f4 262 void sleep_manager_lock_deep_sleep_internal(void)
kenjiArai 1:9db0e321a9f4 263 {
kenjiArai 1:9db0e321a9f4 264
kenjiArai 1:9db0e321a9f4 265 }
kenjiArai 1:9db0e321a9f4 266
kenjiArai 1:9db0e321a9f4 267 void sleep_manager_unlock_deep_sleep_internal(void)
kenjiArai 1:9db0e321a9f4 268 {
kenjiArai 1:9db0e321a9f4 269
kenjiArai 1:9db0e321a9f4 270 }
kenjiArai 1:9db0e321a9f4 271
kenjiArai 1:9db0e321a9f4 272 bool sleep_manager_can_deep_sleep(void)
kenjiArai 1:9db0e321a9f4 273 {
kenjiArai 1:9db0e321a9f4 274 // no sleep implemented
kenjiArai 1:9db0e321a9f4 275 return false;
kenjiArai 1:9db0e321a9f4 276 }
kenjiArai 1:9db0e321a9f4 277
kenjiArai 1:9db0e321a9f4 278 #endif