Maxim Integrated / Mbed OS MAXREFDES220#

Dependencies:   USBDevice max32630fthr

Committer:
Shaun Kelsey
Date:
Thu May 24 14:45:27 2018 -0700
Revision:
6:85ac8bf9955e
Add LEDStatus and EventStats

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Shaun Kelsey 6:85ac8bf9955e 1 /***************************************************************************
Shaun Kelsey 6:85ac8bf9955e 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
Shaun Kelsey 6:85ac8bf9955e 3 *
Shaun Kelsey 6:85ac8bf9955e 4 * Permission is hereby granted, free of charge, to any person obtaining a
Shaun Kelsey 6:85ac8bf9955e 5 * copy of this software and associated documentation files (the "Software"),
Shaun Kelsey 6:85ac8bf9955e 6 * to deal in the Software without restriction, including without limitation
Shaun Kelsey 6:85ac8bf9955e 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Shaun Kelsey 6:85ac8bf9955e 8 * and/or sell copies of the Software, and to permit persons to whom the
Shaun Kelsey 6:85ac8bf9955e 9 * Software is furnished to do so, subject to the following conditions:
Shaun Kelsey 6:85ac8bf9955e 10 *
Shaun Kelsey 6:85ac8bf9955e 11 * The above copyright notice and this permission notice shall be included
Shaun Kelsey 6:85ac8bf9955e 12 * in all copies or substantial portions of the Software.
Shaun Kelsey 6:85ac8bf9955e 13 *
Shaun Kelsey 6:85ac8bf9955e 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Shaun Kelsey 6:85ac8bf9955e 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Shaun Kelsey 6:85ac8bf9955e 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Shaun Kelsey 6:85ac8bf9955e 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Shaun Kelsey 6:85ac8bf9955e 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Shaun Kelsey 6:85ac8bf9955e 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Shaun Kelsey 6:85ac8bf9955e 20 * OTHER DEALINGS IN THE SOFTWARE.
Shaun Kelsey 6:85ac8bf9955e 21 *
Shaun Kelsey 6:85ac8bf9955e 22 * Except as contained in this notice, the name of Maxim Integrated
Shaun Kelsey 6:85ac8bf9955e 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Shaun Kelsey 6:85ac8bf9955e 24 * Products, Inc. Branding Policy.
Shaun Kelsey 6:85ac8bf9955e 25 *
Shaun Kelsey 6:85ac8bf9955e 26 * The mere transfer of this software does not imply any licenses
Shaun Kelsey 6:85ac8bf9955e 27 * of trade secrets, proprietary technology, copyrights, patents,
Shaun Kelsey 6:85ac8bf9955e 28 * trademarks, maskwork rights, or any other form of intellectual
Shaun Kelsey 6:85ac8bf9955e 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
Shaun Kelsey 6:85ac8bf9955e 30 * ownership rights.
Shaun Kelsey 6:85ac8bf9955e 31 ****************************************************************************
Shaun Kelsey 6:85ac8bf9955e 32 */
Shaun Kelsey 6:85ac8bf9955e 33
Shaun Kelsey 6:85ac8bf9955e 34 #include "EventStats.h"
Shaun Kelsey 6:85ac8bf9955e 35 #include "Peripherals.h"
Shaun Kelsey 6:85ac8bf9955e 36
Shaun Kelsey 6:85ac8bf9955e 37 EventStats::EventStats() :
Shaun Kelsey 6:85ac8bf9955e 38 single_evt(), all_evts()
Shaun Kelsey 6:85ac8bf9955e 39 {
Shaun Kelsey 6:85ac8bf9955e 40 name = "";
Shaun Kelsey 6:85ac8bf9955e 41 report_period = 1000000;
Shaun Kelsey 6:85ac8bf9955e 42 num_evts = 0;
Shaun Kelsey 6:85ac8bf9955e 43 total_evt_time = 0;
Shaun Kelsey 6:85ac8bf9955e 44
Shaun Kelsey 6:85ac8bf9955e 45 evt_in_prog = false;
Shaun Kelsey 6:85ac8bf9955e 46 }
Shaun Kelsey 6:85ac8bf9955e 47
Shaun Kelsey 6:85ac8bf9955e 48 EventStats::EventStats(int report_period, const char* name) :
Shaun Kelsey 6:85ac8bf9955e 49 single_evt(), all_evts()
Shaun Kelsey 6:85ac8bf9955e 50 {
Shaun Kelsey 6:85ac8bf9955e 51 if (name)
Shaun Kelsey 6:85ac8bf9955e 52 this->name = name;
Shaun Kelsey 6:85ac8bf9955e 53 else
Shaun Kelsey 6:85ac8bf9955e 54 this->name = "";
Shaun Kelsey 6:85ac8bf9955e 55
Shaun Kelsey 6:85ac8bf9955e 56 this->report_period = report_period;
Shaun Kelsey 6:85ac8bf9955e 57 num_evts = 0;
Shaun Kelsey 6:85ac8bf9955e 58 total_evt_time = 0;
Shaun Kelsey 6:85ac8bf9955e 59 evt_in_prog = false;
Shaun Kelsey 6:85ac8bf9955e 60
Shaun Kelsey 6:85ac8bf9955e 61 }
Shaun Kelsey 6:85ac8bf9955e 62
Shaun Kelsey 6:85ac8bf9955e 63 void EventStats::start()
Shaun Kelsey 6:85ac8bf9955e 64 {
Shaun Kelsey 6:85ac8bf9955e 65 if (evt_in_prog) {
Shaun Kelsey 6:85ac8bf9955e 66 pr_debug("EventStats::start() called twice without calling EventStats::stop()!");
Shaun Kelsey 6:85ac8bf9955e 67 }
Shaun Kelsey 6:85ac8bf9955e 68
Shaun Kelsey 6:85ac8bf9955e 69
Shaun Kelsey 6:85ac8bf9955e 70 if (!num_evts)
Shaun Kelsey 6:85ac8bf9955e 71 all_evts.start();
Shaun Kelsey 6:85ac8bf9955e 72
Shaun Kelsey 6:85ac8bf9955e 73 single_evt.start();
Shaun Kelsey 6:85ac8bf9955e 74 evt_in_prog = true;
Shaun Kelsey 6:85ac8bf9955e 75
Shaun Kelsey 6:85ac8bf9955e 76 }
Shaun Kelsey 6:85ac8bf9955e 77
Shaun Kelsey 6:85ac8bf9955e 78 void EventStats::stop()
Shaun Kelsey 6:85ac8bf9955e 79 {
Shaun Kelsey 6:85ac8bf9955e 80 if (!evt_in_prog) {
Shaun Kelsey 6:85ac8bf9955e 81 pr_debug("EventStats::stop() called before EventStats::start()!");
Shaun Kelsey 6:85ac8bf9955e 82 }
Shaun Kelsey 6:85ac8bf9955e 83
Shaun Kelsey 6:85ac8bf9955e 84 single_evt.stop();
Shaun Kelsey 6:85ac8bf9955e 85 num_evts++;
Shaun Kelsey 6:85ac8bf9955e 86 evt_in_prog = false;
Shaun Kelsey 6:85ac8bf9955e 87 total_evt_time += single_evt.read_us();
Shaun Kelsey 6:85ac8bf9955e 88 single_evt.reset();
Shaun Kelsey 6:85ac8bf9955e 89
Shaun Kelsey 6:85ac8bf9955e 90 int time_since_start = all_evts.read_us();
Shaun Kelsey 6:85ac8bf9955e 91 if (time_since_start > report_period) {
Shaun Kelsey 6:85ac8bf9955e 92 print_events(true);
Shaun Kelsey 6:85ac8bf9955e 93 }
Shaun Kelsey 6:85ac8bf9955e 94 }
Shaun Kelsey 6:85ac8bf9955e 95
Shaun Kelsey 6:85ac8bf9955e 96 void EventStats::print_events(bool reset)
Shaun Kelsey 6:85ac8bf9955e 97 {
Shaun Kelsey 6:85ac8bf9955e 98 if (num_evts > 0) {
Shaun Kelsey 6:85ac8bf9955e 99 int time_since_start = all_evts.read_us();
Shaun Kelsey 6:85ac8bf9955e 100 int avg = total_evt_time / num_evts;
Shaun Kelsey 6:85ac8bf9955e 101
Shaun Kelsey 6:85ac8bf9955e 102 pr_debug("[Event (%s) hit %d times in %dus. Avg: %dus, Total: %dus\r\n",
Shaun Kelsey 6:85ac8bf9955e 103 name,
Shaun Kelsey 6:85ac8bf9955e 104 num_evts,
Shaun Kelsey 6:85ac8bf9955e 105 time_since_start,
Shaun Kelsey 6:85ac8bf9955e 106 avg,
Shaun Kelsey 6:85ac8bf9955e 107 total_evt_time);
Shaun Kelsey 6:85ac8bf9955e 108 } else {
Shaun Kelsey 6:85ac8bf9955e 109 pr_debug("Event (%s) has not yet occurred\r\n", name);
Shaun Kelsey 6:85ac8bf9955e 110 }
Shaun Kelsey 6:85ac8bf9955e 111
Shaun Kelsey 6:85ac8bf9955e 112 if (reset) {
Shaun Kelsey 6:85ac8bf9955e 113 num_evts = 0;
Shaun Kelsey 6:85ac8bf9955e 114 all_evts.reset();
Shaun Kelsey 6:85ac8bf9955e 115 total_evt_time = 0;
Shaun Kelsey 6:85ac8bf9955e 116 }
Shaun Kelsey 6:85ac8bf9955e 117 }