Example to demonstrate usage of `mbed_stats_cpu_get()` API usage

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Tue Jun 05 17:34:16 2018 +0100
Child:
1:1eaa9a7bd9b1
Commit message:
Initial commit.
Commit copied from https://github.com/ARMmbed/mbed-os-example-cpu-stats

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Tue Jun 05 17:34:16 2018 +0100
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Tue Jun 05 17:34:16 2018 +0100
@@ -0,0 +1,57 @@
+# Getting started with CPU Stats on Mbed OS
+
+This guide reviews the steps required to get CPU statistics on Mbed OS platform.
+
+Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
+
+## Import the example application
+
+From the command-line, import the example:
+
+```
+mbed import mbed-os-example-cpu-stats
+cd mbed-os-example-cpu-stats
+```
+
+### Now compile
+
+Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
+
+```
+mbed compile -m K64F -t ARM
+```
+
+Your PC may take a few minutes to compile your code. At the end, you see the following result:
+
+```
+[snip]
++------------------+-------+-------+------+
+| Module           | .text | .data | .bss |
++------------------+-------+-------+------+
+| [lib]\c_w.l      | 11473 |    16 |  348 |
+| [lib]\cpprt_w.l  |    36 |     0 |    0 |
+| [lib]\fz_wm.l    |    18 |     0 |    0 |
+| [lib]\m_wm.l     |    48 |     0 |    0 |
+| anon$$obj.o      |    32 |     0 | 1024 |
+| main.o           |   530 |     8 |    0 |
+| mbed-os\drivers  |   165 |     0 |    0 |
+| mbed-os\events   |  2139 |     8 | 1568 |
+| mbed-os\features |   132 |     0 |  304 |
+| mbed-os\hal      |  1910 |    34 |  128 |
+| mbed-os\platform |  3695 |   104 |  604 |
+| mbed-os\rtos     | 17302 |  2310 | 4592 |
+| mbed-os\targets  | 10707 |   112 |  324 |
+| Subtotals        | 48187 |  2592 | 8892 |
++------------------+-------+-------+------+
+Total Static RAM memory (data + bss): 11484 bytes
+Total Flash memory (text + data): 50779 bytes
+
+Image: .\BUILD\K64F\ARM\mbed-os-example-cpu-stats.bin
+
+```
+
+### Program your board
+
+1. Connect your Mbed device to the computer over USB.
+1. Copy the binary file to the Mbed device.
+1. Press the reset button to start the program.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 05 17:34:16 2018 +0100
@@ -0,0 +1,61 @@
+#include "mbed.h"
+
+#if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP)
+#error [NOT_SUPPORTED] test not supported
+#endif
+
+DigitalOut led1(LED1);
+
+#define MAX_THREAD_STACK        384
+#define SAMPLE_TIME             1000
+#define LOOP_TIME               3000
+
+uint64_t prev_idle_time = 0;
+int32_t wait_time = 5000;
+
+void busy_thread()
+{
+    volatile uint64_t i = ~0;
+
+    while(i--) {
+        led1 = !led1;
+        wait_us(wait_time);
+    }
+}
+
+void print_stats()
+{
+    mbed_stats_cpu_t stats;
+    mbed_stats_cpu_get(&stats);
+
+    printf("%-20lld", stats.uptime);
+    printf("%-20lld", stats.idle_time);
+    printf("%-20lld", stats.sleep_time);
+    printf("%-20lld\n", stats.deep_sleep_time);
+}
+
+int main()
+{
+    // Request the shared queue
+    EventQueue *stats_queue = mbed_event_queue();
+    Thread *thread;
+    int id;
+
+    id = stats_queue->call_every(SAMPLE_TIME, print_stats);
+    printf("%-20s%-20s%-20s%-20s\n", "Uptime", "Idle Time", "Sleep time", "DeepSleep time");
+
+    thread = new Thread(osPriorityNormal, MAX_THREAD_STACK);
+    thread->start(busy_thread);
+
+    // Steadily increase the system load
+    for (int count = 1; ; count++) {
+        Thread::wait(LOOP_TIME);
+        if (wait_time <= 0) {
+            break;
+        }
+        wait_time -= 1000;
+    }
+    thread->terminate();
+    stats_queue->cancel(id);
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Tue Jun 05 17:34:16 2018 +0100
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#5371c107353f20ab79edcbe8a005cb7c0ea9249e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Tue Jun 05 17:34:16 2018 +0100
@@ -0,0 +1,3 @@
+{
+    "macros": ["MBED_CPU_STATS_ENABLED"]
+}