2017-07-25 14:49:57 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2017 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-02-05 15:14:42 +00:00
|
|
|
#include "mbed_assert.h"
|
2018-02-27 15:25:16 +00:00
|
|
|
#include "mbed_power_mgmt.h"
|
2017-07-25 14:49:57 +00:00
|
|
|
#include "mbed_critical.h"
|
|
|
|
#include "sleep_api.h"
|
|
|
|
#include "mbed_error.h"
|
2018-03-01 23:39:24 +00:00
|
|
|
#include "mbed_debug.h"
|
2017-07-25 14:49:57 +00:00
|
|
|
#include <limits.h>
|
2018-02-05 15:14:42 +00:00
|
|
|
#include <stdio.h>
|
2017-07-25 14:49:57 +00:00
|
|
|
|
|
|
|
#if DEVICE_SLEEP
|
|
|
|
|
|
|
|
// deep sleep locking counter. A target is allowed to deep sleep if counter == 0
|
|
|
|
static uint16_t deep_sleep_lock = 0U;
|
|
|
|
|
2018-02-27 11:35:24 +00:00
|
|
|
#ifdef MBED_SLEEP_TRACING_ENABLED
|
2018-02-05 15:14:42 +00:00
|
|
|
|
|
|
|
// Length of the identifier extracted from the driver name to store for logging.
|
2018-03-01 23:39:24 +00:00
|
|
|
#define IDENTIFIER_WIDTH 15
|
2018-02-05 15:14:42 +00:00
|
|
|
// Number of drivers that can be stored in the structure
|
|
|
|
#define STATISTIC_COUNT 10
|
|
|
|
|
2018-02-26 13:14:08 +00:00
|
|
|
typedef struct sleep_statistic {
|
2018-02-05 15:14:42 +00:00
|
|
|
char identifier[IDENTIFIER_WIDTH];
|
|
|
|
uint8_t count;
|
|
|
|
} sleep_statistic_t;
|
|
|
|
|
|
|
|
static sleep_statistic_t sleep_stats[STATISTIC_COUNT];
|
|
|
|
|
|
|
|
static const char* strip_path(const char* const filename)
|
|
|
|
{
|
|
|
|
char *output = strrchr(filename, '/');
|
|
|
|
|
|
|
|
if (output != NULL) {
|
|
|
|
return output + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
output = strrchr(filename, '\\');
|
|
|
|
|
|
|
|
if (output != NULL) {
|
|
|
|
return output + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2018-03-01 17:14:41 +00:00
|
|
|
static sleep_statistic_t* sleep_tracker_find(const char *const filename)
|
2018-02-05 15:14:42 +00:00
|
|
|
{
|
|
|
|
char temp[IDENTIFIER_WIDTH];
|
|
|
|
strncpy(temp, filename, IDENTIFIER_WIDTH);
|
|
|
|
temp[IDENTIFIER_WIDTH - 1] = '\0';
|
|
|
|
|
|
|
|
// Search for the a driver matching the current name and return it's index
|
|
|
|
for (int i = 0; i < STATISTIC_COUNT; ++i) {
|
|
|
|
if (strcmp(sleep_stats[i].identifier, temp) == 0) {
|
2018-03-01 17:14:41 +00:00
|
|
|
return &sleep_stats[i];
|
2018-02-05 15:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 17:14:41 +00:00
|
|
|
return NULL;
|
2018-03-01 17:03:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 17:14:41 +00:00
|
|
|
static sleep_statistic_t* sleep_tracker_add(const char* const filename)
|
2018-03-01 17:03:28 +00:00
|
|
|
{
|
|
|
|
char temp[IDENTIFIER_WIDTH];
|
|
|
|
strncpy(temp, filename, IDENTIFIER_WIDTH);
|
|
|
|
temp[IDENTIFIER_WIDTH - 1] = '\0';
|
|
|
|
|
2018-02-05 15:14:42 +00:00
|
|
|
for (int i = 0; i < STATISTIC_COUNT; ++i) {
|
|
|
|
if (sleep_stats[i].identifier[0] == '\0') {
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
strncpy(sleep_stats[i].identifier, temp, sizeof(temp));
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
|
2018-03-01 17:14:41 +00:00
|
|
|
return &sleep_stats[i];
|
2018-02-05 15:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 23:39:24 +00:00
|
|
|
debug("No free indexes left to use in mbed sleep tracker.\r\n");
|
2018-02-05 15:14:42 +00:00
|
|
|
|
2018-03-01 17:14:41 +00:00
|
|
|
return NULL;
|
2018-02-05 15:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sleep_tracker_print_stats(void)
|
|
|
|
{
|
2018-03-01 23:39:24 +00:00
|
|
|
debug("Sleep locks held:\r\n");
|
2018-02-05 15:14:42 +00:00
|
|
|
for (int i = 0; i < STATISTIC_COUNT; ++i) {
|
|
|
|
if (sleep_stats[i].count == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sleep_stats[i].identifier[0] == '\0') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-01 23:39:24 +00:00
|
|
|
debug("[id: %s, count: %u]\r\n", sleep_stats[i].identifier,
|
2018-02-05 15:14:42 +00:00
|
|
|
sleep_stats[i].count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_tracker_lock(const char* const filename, int line)
|
|
|
|
{
|
|
|
|
const char* const stripped_path = strip_path(filename);
|
2018-03-01 17:03:28 +00:00
|
|
|
|
2018-03-01 17:14:41 +00:00
|
|
|
sleep_statistic_t* stat = sleep_tracker_find(stripped_path);
|
2018-02-05 15:14:42 +00:00
|
|
|
|
2018-03-01 17:03:28 +00:00
|
|
|
// Entry for this driver does not exist, create one.
|
2018-03-01 17:14:41 +00:00
|
|
|
if (stat == NULL) {
|
2018-03-01 23:39:24 +00:00
|
|
|
stat = sleep_tracker_add(stripped_path);
|
2018-03-01 17:03:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 17:14:41 +00:00
|
|
|
core_util_atomic_incr_u8(&stat->count, 1);
|
2018-02-05 15:14:42 +00:00
|
|
|
|
2018-03-01 23:39:24 +00:00
|
|
|
debug("LOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
|
2018-02-05 15:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_tracker_unlock(const char* const filename, int line)
|
|
|
|
{
|
|
|
|
const char* const stripped_path = strip_path(filename);
|
2018-03-01 17:14:41 +00:00
|
|
|
sleep_statistic_t* stat = sleep_tracker_find(stripped_path);
|
2018-02-05 15:14:42 +00:00
|
|
|
|
2018-03-01 17:03:28 +00:00
|
|
|
// Entry for this driver does not exist, something went wrong.
|
2018-03-01 17:14:41 +00:00
|
|
|
if (stat == NULL) {
|
2018-03-01 23:39:24 +00:00
|
|
|
debug("Unlocking sleep for driver that was not previously locked: %s, ln: %i\r\n", stripped_path, line);
|
|
|
|
return;
|
2018-03-01 17:03:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 17:14:41 +00:00
|
|
|
core_util_atomic_decr_u8(&stat->count, 1);
|
2018-02-05 15:14:42 +00:00
|
|
|
|
2018-03-01 23:39:24 +00:00
|
|
|
debug("UNLOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
|
2018-02-05 15:14:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 11:35:24 +00:00
|
|
|
#endif // MBED_SLEEP_TRACING_ENABLED
|
2018-02-05 15:14:42 +00:00
|
|
|
|
|
|
|
void sleep_manager_lock_deep_sleep_internal(void)
|
2017-07-25 14:49:57 +00:00
|
|
|
{
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
if (deep_sleep_lock == USHRT_MAX) {
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
error("Deep sleep lock would overflow (> USHRT_MAX)");
|
|
|
|
}
|
|
|
|
core_util_atomic_incr_u16(&deep_sleep_lock, 1);
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:14:42 +00:00
|
|
|
void sleep_manager_unlock_deep_sleep_internal(void)
|
2017-07-25 14:49:57 +00:00
|
|
|
{
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
if (deep_sleep_lock == 0) {
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
error("Deep sleep lock would underflow (< 0)");
|
|
|
|
}
|
|
|
|
core_util_atomic_decr_u16(&deep_sleep_lock, 1);
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sleep_manager_can_deep_sleep(void)
|
|
|
|
{
|
|
|
|
return deep_sleep_lock == 0 ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_manager_sleep_auto(void)
|
|
|
|
{
|
2018-03-01 23:39:24 +00:00
|
|
|
#ifdef MBED_SLEEP_TRACING_ENABLED
|
|
|
|
sleep_tracker_print_stats();
|
|
|
|
#endif
|
2017-07-25 14:49:57 +00:00
|
|
|
core_util_critical_section_enter();
|
|
|
|
// debug profile should keep debuggers attached, no deep sleep allowed
|
|
|
|
#ifdef MBED_DEBUG
|
|
|
|
hal_sleep();
|
|
|
|
#else
|
|
|
|
if (sleep_manager_can_deep_sleep()) {
|
|
|
|
hal_deepsleep();
|
|
|
|
} else {
|
|
|
|
hal_sleep();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
}
|
|
|
|
|
2017-08-15 13:38:06 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
// locking is valid only if DEVICE_SLEEP is defined
|
|
|
|
// we provide empty implementation
|
|
|
|
|
2018-02-05 15:14:42 +00:00
|
|
|
void sleep_manager_lock_deep_sleep_internal(void)
|
2017-08-15 13:38:06 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:14:42 +00:00
|
|
|
void sleep_manager_unlock_deep_sleep_internal(void)
|
2017-08-15 13:38:06 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sleep_manager_can_deep_sleep(void)
|
|
|
|
{
|
|
|
|
// no sleep implemented
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-25 14:49:57 +00:00
|
|
|
#endif
|