NuMaker Pelion Device Management example

Fork of mbed-os-example-pelion by cc li

Files at this revision

API Documentation at this revision

Comitter:
ccli8
Date:
Fri Nov 22 14:40:12 2019 +0800
Parent:
4:e564fd8be648
Child:
6:7e7872c31bef
Commit message:
Support memory statistics

Intercept other host command with:
1. 'h' for printing heap statistics
2. 's' for printing stack statistics

Changed in this revision

main.cpp 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
pre-main/dispatch_host_command.cpp Show annotated file Show diff for this revision Revisions of this file
pre-main/mem_stats.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Nov 22 11:16:39 2019 +0800
+++ b/main.cpp	Fri Nov 22 14:40:12 2019 +0800
@@ -98,6 +98,8 @@
     printf("Update progress = %" PRIu8 "%%\n", percent);
 }
 
+extern "C" MBED_WEAK void dispatch_host_command(int);
+
 int main(void)
 {
     int status;
@@ -201,6 +203,10 @@
             printf("Storage erased, rebooting the device.\n\n");
             wait(1);
             NVIC_SystemReset();
+        } else if (dispatch_host_command && in_char != 0x03) {
+            /* Intercept other host commands */
+            dispatch_host_command(in_char);
+            continue;
         } else if (in_char > 0 && in_char != 0x03) { // Ctrl+C is 0x03 in Mbed OS and Linux returns negative number
             button_press(); // Simulate button press
             continue;
--- a/mbed_app.json	Fri Nov 22 11:16:39 2019 +0800
+++ b/mbed_app.json	Fri Nov 22 14:40:12 2019 +0800
@@ -14,6 +14,8 @@
             "platform.stdio-convert-newlines"           : true,
             "platform.stdio-buffered-serial"            : true,
             "platform.stdio-flush-at-exit"              : true,
+            "platform.heap-stats-enabled"               : 1,
+            "platform.stack-stats-enabled"              : 1,
             "rtos.main-thread-stack-size"               : 5120,
             "update-client.storage-locations"           : 1,
             "mbed-trace.enable"                         : null,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pre-main/dispatch_host_command.cpp	Fri Nov 22 14:40:12 2019 +0800
@@ -0,0 +1,46 @@
+/* 
+ * Copyright (c) 2019 Nuvoton Technology Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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.
+ */
+
+#include "mbed.h"
+
+/* Dispatch host command communicating via USB VCOM
+ *
+ * WARNING: For mass production, remove this file.
+ */
+
+/* Check weak reference/definition at the link:
+ * http://www.keil.com/support/man/docs/ARMLINK/armlink_pge1362065917715.htm */
+
+extern "C" {
+    MBED_USED void dispatch_host_command(int);
+    MBED_WEAK void print_heap_stats(void);
+    MBED_WEAK void print_stack_statistics(void);
+}
+
+void dispatch_host_command(int c)
+{
+    switch (c) {
+        case 'h':
+            print_heap_stats();
+            break;
+                
+        case 's':
+            print_stack_statistics();
+            break;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pre-main/mem_stats.cpp	Fri Nov 22 14:40:12 2019 +0800
@@ -0,0 +1,72 @@
+/* 
+ * Copyright (c) 2019 Nuvoton Technology Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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.
+ */
+
+#include "mbed.h"
+
+#if (MBED_HEAP_STATS_ENABLED) || (MBED_STACK_STATS_ENABLED)
+/* Measure memory footprint */
+#include "mbed_stats.h"
+/* Fix up the compilation on AMRCC for PRIu32 */
+#define __STDC_FORMAT_MACROS
+#include <inttypes.h>
+#endif
+
+/* Support memory footprint */
+
+/* Check weak reference/definition at the link:
+ * http://www.keil.com/support/man/docs/ARMLINK/armlink_pge1362065917715.htm */
+
+extern "C" {
+#if (MBED_HEAP_STATS_ENABLED)
+    MBED_USED void print_heap_stats(void);
+#endif
+#if (MBED_STACK_STATS_ENABLED)
+    MBED_USED void print_stack_statistics();
+#endif
+}
+
+#if (MBED_HEAP_STATS_ENABLED)
+void print_heap_stats(void)
+{
+    mbed_stats_heap_t stats;
+    mbed_stats_heap_get(&stats);
+    printf("** MBED HEAP STATS **\n");
+    printf("**** current_size: %" PRIu32 "\n", stats.current_size);
+    printf("**** max_size    : %" PRIu32 "\n", stats.max_size);
+    printf("*****************************\n\n");
+}
+#endif  // MBED_HEAP_STATS_ENABLED
+
+#if (MBED_STACK_STATS_ENABLED)
+void print_stack_statistics()
+{
+    printf("** MBED THREAD STACK STATS **\n");
+    int cnt = osThreadGetCount();
+    mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
+
+    if (stats) {
+        cnt = mbed_stats_stack_get_each(stats, cnt);
+        for (int i = 0; i < cnt; i++) {
+            printf("Thread: 0x%" PRIx32 ", Stack size: %" PRIu32 ", Max stack: %" PRIu32 "\r\n", stats[i].thread_id, stats[i].reserved_size, stats[i].max_size);
+        }
+
+        free(stats);
+    }
+    printf("*****************************\n\n");
+}
+#endif  // MBED_STACK_STATS_ENABLED