mbed_sleep_manager.c 5.7 KB

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