code including all sensors

Dependencies:   LAAP AK8963

Committer:
Glaxys_finest1
Date:
Mon Dec 23 14:45:38 2019 +0000
Revision:
2:a79a9c9f83a4
Parent:
0:66a265dc3146
workin progress;

Who changed what in which revision?

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