BG96 Cat.M1 GPS sample for WIZnet IoT Shield

Committer:
hkjung
Date:
Wed Mar 20 05:33:37 2019 +0000
Revision:
0:98226e0983af
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hkjung 0:98226e0983af 1 /* mbed Microcontroller Library
hkjung 0:98226e0983af 2 * Copyright (c) 2018 ARM Limited
hkjung 0:98226e0983af 3 * SPDX-License-Identifier: Apache-2.0
hkjung 0:98226e0983af 4 */
hkjung 0:98226e0983af 5
hkjung 0:98226e0983af 6 #ifndef STATS_REPORT_H
hkjung 0:98226e0983af 7 #define STATS_REPORT
hkjung 0:98226e0983af 8
hkjung 0:98226e0983af 9 #include "mbed.h"
hkjung 0:98226e0983af 10
hkjung 0:98226e0983af 11 /**
hkjung 0:98226e0983af 12 * System Reporting library. Provides runtime information on device:
hkjung 0:98226e0983af 13 * - CPU sleep, idle, and wake times
hkjung 0:98226e0983af 14 * - Heap and stack usage
hkjung 0:98226e0983af 15 * - Thread information
hkjung 0:98226e0983af 16 * - Static system information
hkjung 0:98226e0983af 17 */
hkjung 0:98226e0983af 18 class SystemReport {
hkjung 0:98226e0983af 19 mbed_stats_heap_t heap_stats;
hkjung 0:98226e0983af 20 mbed_stats_cpu_t cpu_stats;
hkjung 0:98226e0983af 21 mbed_stats_sys_t sys_stats;
hkjung 0:98226e0983af 22
hkjung 0:98226e0983af 23 mbed_stats_thread_t *thread_stats;
hkjung 0:98226e0983af 24 uint8_t thread_count;
hkjung 0:98226e0983af 25 uint8_t max_thread_count;
hkjung 0:98226e0983af 26 uint32_t sample_time_ms;
hkjung 0:98226e0983af 27
hkjung 0:98226e0983af 28 public:
hkjung 0:98226e0983af 29 /**
hkjung 0:98226e0983af 30 * SystemReport - Sample rate in ms is required to handle the CPU percent awake logic
hkjung 0:98226e0983af 31 */
hkjung 0:98226e0983af 32 SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate)
hkjung 0:98226e0983af 33 {
hkjung 0:98226e0983af 34 thread_stats = new mbed_stats_thread_t[max_thread_count];
hkjung 0:98226e0983af 35
hkjung 0:98226e0983af 36 // Collect the static system information
hkjung 0:98226e0983af 37 mbed_stats_sys_get(&sys_stats);
hkjung 0:98226e0983af 38
hkjung 0:98226e0983af 39 printf("=============================== SYSTEM INFO ================================\r\n");
hkjung 0:98226e0983af 40 printf("Mbed OS Version: %ld \r\n", sys_stats.os_version);
hkjung 0:98226e0983af 41 printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id);
hkjung 0:98226e0983af 42 printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
hkjung 0:98226e0983af 43 printf("Compiler Version: %ld \r\n", sys_stats.compiler_version);
hkjung 0:98226e0983af 44
hkjung 0:98226e0983af 45 for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
hkjung 0:98226e0983af 46 if (sys_stats.ram_size[i] != 0) {
hkjung 0:98226e0983af 47 printf("RAM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.ram_start[i], sys_stats.ram_size[i]);
hkjung 0:98226e0983af 48 }
hkjung 0:98226e0983af 49 }
hkjung 0:98226e0983af 50 for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
hkjung 0:98226e0983af 51 if (sys_stats.rom_size[i] != 0) {
hkjung 0:98226e0983af 52 printf("ROM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.rom_start[i], sys_stats.rom_size[i]);
hkjung 0:98226e0983af 53 }
hkjung 0:98226e0983af 54 }
hkjung 0:98226e0983af 55 }
hkjung 0:98226e0983af 56
hkjung 0:98226e0983af 57 ~SystemReport(void)
hkjung 0:98226e0983af 58 {
hkjung 0:98226e0983af 59 free(thread_stats);
hkjung 0:98226e0983af 60 }
hkjung 0:98226e0983af 61
hkjung 0:98226e0983af 62 /**
hkjung 0:98226e0983af 63 * Report on each Mbed OS Platform stats API
hkjung 0:98226e0983af 64 */
hkjung 0:98226e0983af 65 void report_state(void)
hkjung 0:98226e0983af 66 {
hkjung 0:98226e0983af 67 report_cpu_stats();
hkjung 0:98226e0983af 68 report_heap_stats();
hkjung 0:98226e0983af 69 report_thread_stats();
hkjung 0:98226e0983af 70
hkjung 0:98226e0983af 71 // Clear next line to separate subsequent report logs
hkjung 0:98226e0983af 72 printf("\r\n");
hkjung 0:98226e0983af 73 }
hkjung 0:98226e0983af 74
hkjung 0:98226e0983af 75 /**
hkjung 0:98226e0983af 76 * Report CPU idle and awake time in terms of percentage
hkjung 0:98226e0983af 77 */
hkjung 0:98226e0983af 78 void report_cpu_stats(void)
hkjung 0:98226e0983af 79 {
hkjung 0:98226e0983af 80 static uint64_t prev_idle_time = 0;
hkjung 0:98226e0983af 81
hkjung 0:98226e0983af 82 printf("================= CPU STATS =================\r\n");
hkjung 0:98226e0983af 83
hkjung 0:98226e0983af 84 // Collect and print cpu stats
hkjung 0:98226e0983af 85 mbed_stats_cpu_get(&cpu_stats);
hkjung 0:98226e0983af 86
hkjung 0:98226e0983af 87 uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
hkjung 0:98226e0983af 88 uint8_t idle = (diff * 100) / (sample_time_ms * 1000); // usec;
hkjung 0:98226e0983af 89 uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000)); // usec;;
hkjung 0:98226e0983af 90 prev_idle_time = cpu_stats.idle_time;
hkjung 0:98226e0983af 91
hkjung 0:98226e0983af 92 printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
hkjung 0:98226e0983af 93 }
hkjung 0:98226e0983af 94
hkjung 0:98226e0983af 95 /**
hkjung 0:98226e0983af 96 * Report current heap stats. Current heap refers to the current amount of
hkjung 0:98226e0983af 97 * allocated heap. Max heap refers to the highest amount of heap allocated
hkjung 0:98226e0983af 98 * since reset.
hkjung 0:98226e0983af 99 */
hkjung 0:98226e0983af 100 void report_heap_stats(void)
hkjung 0:98226e0983af 101 {
hkjung 0:98226e0983af 102 printf("================ HEAP STATS =================\r\n");
hkjung 0:98226e0983af 103
hkjung 0:98226e0983af 104 // Collect and print heap stats
hkjung 0:98226e0983af 105 mbed_stats_heap_get(&heap_stats);
hkjung 0:98226e0983af 106
hkjung 0:98226e0983af 107 printf("Current heap: %lu\r\n", heap_stats.current_size);
hkjung 0:98226e0983af 108 printf("Max heap size: %lu\r\n", heap_stats.max_size);
hkjung 0:98226e0983af 109 }
hkjung 0:98226e0983af 110
hkjung 0:98226e0983af 111 /**
hkjung 0:98226e0983af 112 * Report active thread stats
hkjung 0:98226e0983af 113 */
hkjung 0:98226e0983af 114 void report_thread_stats(void)
hkjung 0:98226e0983af 115 {
hkjung 0:98226e0983af 116 printf("================ THREAD STATS ===============\r\n");
hkjung 0:98226e0983af 117
hkjung 0:98226e0983af 118 // Collect and print running thread stats
hkjung 0:98226e0983af 119 int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);
hkjung 0:98226e0983af 120
hkjung 0:98226e0983af 121 for (int i = 0; i < count; i++) {
hkjung 0:98226e0983af 122 printf("ID: 0x%lx \r\n", thread_stats[i].id);
hkjung 0:98226e0983af 123 printf("Name: %s \r\n", thread_stats[i].name);
hkjung 0:98226e0983af 124 printf("State: %ld \r\n", thread_stats[i].state);
hkjung 0:98226e0983af 125 printf("Priority: %ld \r\n", thread_stats[i].priority);
hkjung 0:98226e0983af 126 printf("Stack Size: %ld \r\n", thread_stats[i].stack_size);
hkjung 0:98226e0983af 127 printf("Stack Space: %ld \r\n", thread_stats[i].stack_space);
hkjung 0:98226e0983af 128 }
hkjung 0:98226e0983af 129 }
hkjung 0:98226e0983af 130 };
hkjung 0:98226e0983af 131
hkjung 0:98226e0983af 132 #endif // STATS_REPORT_H