mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Revision:
186:707f6e361f3e
Parent:
167:e84263d55307
Child:
187:0387e8f68319
--- a/platform/mbed_stats.c	Thu Apr 19 17:12:19 2018 +0100
+++ b/platform/mbed_stats.c	Fri Jun 22 16:45:37 2018 +0100
@@ -1,19 +1,40 @@
+#include "mbed_assert.h"
 #include "mbed_stats.h"
+#include "mbed_power_mgmt.h"
 #include <string.h>
 #include <stdlib.h>
-#include "mbed_assert.h"
+
+#include "device.h"
+#ifdef MBED_CONF_RTOS_PRESENT
+#include "cmsis_os2.h"
+#include "rtos_idle.h"
+#elif defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED) || defined(MBED_CPU_STATS_ENABLED)
+#warning Statistics are currently not supported without the rtos.
+#endif
 
-#if MBED_CONF_RTOS_PRESENT
-#include "cmsis_os2.h"
+#if defined(MBED_CPU_STATS_ENABLED) && (!defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP))
+#warning CPU statistics are not supported without low power timer support.
 #endif
 
+void mbed_stats_cpu_get(mbed_stats_cpu_t *stats)
+{
+    MBED_ASSERT(stats != NULL);
+    memset(stats, 0, sizeof(mbed_stats_cpu_t));
+#if defined(MBED_CPU_STATS_ENABLED) && defined(DEVICE_LPTICKER) && defined(DEVICE_SLEEP)
+    stats->uptime = mbed_uptime();
+    stats->idle_time = mbed_time_idle();
+    stats->sleep_time = mbed_time_sleep();
+    stats->deep_sleep_time = mbed_time_deepsleep();
+#endif
+}
+
 // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
-
 void mbed_stats_stack_get(mbed_stats_stack_t *stats)
 {
+    MBED_ASSERT(stats != NULL);
     memset(stats, 0, sizeof(mbed_stats_stack_t));
 
-#if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT
+#if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
     uint32_t thread_n = osThreadGetCount();
     unsigned i;
     osThreadId_t *threads;
@@ -24,7 +45,7 @@
     osKernelLock();
     thread_n = osThreadEnumerate(threads, thread_n);
 
-    for(i = 0; i < thread_n; i++) {
+    for (i = 0; i < thread_n; i++) {
         uint32_t stack_size = osThreadGetStackSize(threads[i]);
         stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
         stats->reserved_size += stack_size;
@@ -38,10 +59,12 @@
 
 size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
 {
-    memset(stats, 0, count*sizeof(mbed_stats_stack_t));
+    MBED_ASSERT(stats != NULL);
+    memset(stats, 0, count * sizeof(mbed_stats_stack_t));
+
     size_t i = 0;
 
-#if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT
+#if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
     osThreadId_t *threads;
 
     threads = malloc(sizeof(osThreadId_t) * count);
@@ -50,7 +73,7 @@
     osKernelLock();
     count = osThreadEnumerate(threads, count);
 
-    for(i = 0; i < count; i++) {
+    for (i = 0; i < count; i++) {
         uint32_t stack_size = osThreadGetStackSize(threads[i]);
         stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
         stats[i].reserved_size = stack_size;
@@ -65,6 +88,55 @@
     return i;
 }
 
-#if MBED_STACK_STATS_ENABLED && !MBED_CONF_RTOS_PRESENT
-#warning Stack statistics are currently not supported without the rtos.
+size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
+{
+    MBED_ASSERT(stats != NULL);
+    memset(stats, 0, count * sizeof(mbed_stats_thread_t));
+    size_t i = 0;
+
+#if defined(MBED_THREAD_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
+    osThreadId_t *threads;
+
+    threads = malloc(sizeof(osThreadId_t) * count);
+    MBED_ASSERT(threads != NULL);
+
+    osKernelLock();
+    count = osThreadEnumerate(threads, count);
+
+    for (i = 0; i < count; i++) {
+        stats[i].id = (uint32_t)threads[i];
+        stats[i].state = (uint32_t)osThreadGetState(threads[i]);
+        stats[i].priority = (uint32_t)osThreadGetPriority(threads[i]);
+        stats[i].stack_size = osThreadGetStackSize(threads[i]);
+        stats[i].stack_space = osThreadGetStackSpace(threads[i]);
+        stats[i].name = osThreadGetName(threads[i]);
+    }
+    osKernelUnlock();
+    free(threads);
 #endif
+    return i;
+}
+
+void mbed_stats_sys_get(mbed_stats_sys_t *stats)
+{
+    MBED_ASSERT(stats != NULL);
+    memset(stats, 0, sizeof(mbed_stats_sys_t));
+
+#if defined(MBED_SYS_STATS_ENABLED)
+#if defined(__CORTEX_M)
+    stats->cpu_id = SCB->CPUID;
+#endif
+#if defined(__IAR_SYSTEMS_ICC__)
+    stats->compiler_id = IAR;
+    stats->compiler_version = __VER__;
+#elif defined(__CC_ARM)
+    stats->compiler_id = ARM;
+    stats->compiler_version = __ARMCC_VERSION;
+#elif defined(__GNUC__)
+    stats->compiler_id = GCC_ARM;
+    stats->compiler_version = (__GNUC__ * 10000 + __GNUC_MINOR__ * 100);
+#endif
+
+#endif
+    return;
+}