Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fcc_time_profiling.h Source File

fcc_time_profiling.h

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //  
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //  
00008 //     http://www.apache.org/licenses/LICENSE-2.0
00009 //  
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 // ----------------------------------------------------------------------------
00016 
00017 #ifndef __FCC_TIME_PROFILING_H__
00018 #define __FCC_TIME_PROFILING_H__
00019 
00020 #include <inttypes.h>
00021 #include <stdbool.h>
00022 #include <string.h>
00023 #include "mbed-trace/mbed_trace.h"
00024 #include "pal.h"
00025 
00026 #ifdef __cplusplus
00027 extern "C" {
00028 #endif
00029 
00030 
00031 #ifdef FCC_TIME_PROFILING
00032 
00033 extern uint64_t fcc_gen_timer;
00034 extern uint64_t fcc_bundle_timer;
00035 extern uint64_t fcc_key_timer;
00036 extern uint64_t fcc_certificate_timer;
00037 extern uint64_t fcc_config_param_timer;
00038 extern uint64_t fcc_certificate_chain_timer;
00039 
00040 #define TRACE_GROUP     "fcc"
00041 
00042 /**
00043 * Init timer
00044 */
00045 #define FCC_INIT_TIMER(ticks) calculate_time("",0,pal_osKernelSysTick() - ticks)
00046 /**
00047 * Start timer
00048 */
00049 #define FCC_SET_START_TIMER(ticks) ticks=pal_osKernelSysTick();
00050 
00051 /**
00052 * End timer, print label and the calculated result.
00053 * If the label is string "size" should be 0, and if the label is buffer - "size" should be the size of buffer to print.
00054 */
00055 #define FCC_END_TIMER(label,size, ticks) calculate_time(label,size,pal_osKernelSysTick() - ticks)
00056 /**
00057 *  The function calculates time from started timer, prints the label as string or as buffer with size and the calulated time.
00058 **/
00059 
00060 static inline void calculate_time(const char *label, int size, uint64_t ticks)
00061 {
00062     static double ticks_persecond = 0.0;
00063     static double ticks_permillisecond = 0.0;
00064     static double ticks_permicrosecond = 0.0;
00065 
00066     // Since the tick conversion to time functions on some of the reference platforms give incorrect results,
00067     // we use pal_osDelay() to estimate how many ticks per second. We do this once and then base all
00068     // subsequent calculations on the values that we store in static variables.
00069     // For new platforms the accuracy of pal_osDelay() should be verified before accepting the time results.
00070     if (ticks_persecond == 0.0)
00071     {
00072         // Calculate how many ticks per second
00073         uint64_t tick1 = pal_osKernelSysTick();
00074         // One second delay
00075         pal_osDelay(1000);
00076         uint64_t tick2 = pal_osKernelSysTick();
00077         ticks_persecond = tick2 - tick1;
00078         ticks_permillisecond = ticks_persecond / 1000.0;
00079         ticks_permicrosecond = ticks_persecond / 1000000.0;
00080     }
00081     if (size == 0) {
00082         //Print string
00083         mbed_tracef(TRACE_LEVEL_ERROR, TRACE_GROUP, "%s: %20lu ticks, %10.2lf milli, %10.2lf micro\n", (char*)label, (long unsigned int)ticks, (double)(ticks / ticks_permillisecond), (double)(ticks / ticks_permicrosecond));
00084     }  else {
00085         //Print buffer with size "size"
00086         mbed_tracef(TRACE_LEVEL_ERROR, TRACE_GROUP, "%.*s: %20lu ticks, %10.2lf milli, %10.2lf micro\n",size, label, (long unsigned int)ticks, (double)(ticks / ticks_permillisecond), (double)(ticks / ticks_permicrosecond));
00087     }
00088 }
00089 #else 
00090 #define FCC_INIT_TIMER(ticks)  do {} while (0)
00091 #define FCC_SET_START_TIMER(ticks) do {} while (0)
00092 #define FCC_END_TIMER(label, size, ticks) do {} while (0)
00093 #endif
00094 #ifdef __cplusplus
00095 }
00096 #endif
00097 
00098 #endif  // __PV_MACROS_H__
00099