NTUES lab 2 BLE

Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API

Committer:
whz861025
Date:
Tue May 14 12:45:44 2019 +0000
Revision:
0:088a3171bdab
heartRate ble done;

Who changed what in which revision?

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