Da Vinci Firmware 1
Firmware for the DaVinci-M rocket avionics board.
Loading...
Searching...
No Matches
profiler.h
Go to the documentation of this file.
1
43#ifndef PROFILER_H
44#define PROFILER_H
45
46#include "main.h" // Or wherever DWT and CoreDebug are defined
47
48// --- Configuration ---
49// Comment this line out to disable all profiling code from the build
50#define ENABLE_PROFILING
51
52// --- DWT Initialization (from your previous example) ---
53#ifdef ENABLE_PROFILING
70void initDWT(void){
71 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // Enable trace
72 DWT->CYCCNT = 0; // Reset the counter
73 DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; // Enable cycle counter
74}
75#else
76// If profiling is disabled, the init function does nothing.
77#define initDWT()
78#endif
79
80
81// --- Profiling Macros ---
82#ifdef ENABLE_PROFILING
83
84 // A structure to hold profiling results
85 typedef struct {
86 uint32_t start_time;
87 uint32_t elapsed_cycles;
88 float elapsed_ns;
89 float elapsed_us;
90 float elapsed_ms;
92
93 // Starts the profiler. Pass a ProfileResult variable by address.
94 #define PROFILER_START(p_result) (p_result)->start_time = DWT->CYCCNT
95
96 // Stops the profiler, calculates elapsed time, and stores it.
97 #define PROFILER_STOP(p_result) \
98 do { \
99 (p_result)->elapsed_cycles = DWT->CYCCNT - (p_result)->start_time; \
100 float clock_period_ns = 1000000000.0f / SystemCoreClock; \
101 (p_result)->elapsed_ns = (float)(p_result)->elapsed_cycles * clock_period_ns; \
102 (p_result)->elapsed_us = (p_result)->elapsed_ns / 1000.0f; \
103 (p_result)->elapsed_ms = (p_result)->elapsed_us / 1000.0f; \
104 } while(0)
105
106#else
107 // If profiling is disabled, all macros compile to nothing.
108 // We still need a dummy struct to prevent "undeclared" errors.
109 typedef struct { uint32_t dummy; } ProfileResult;
110 #define PROFILER_START(p_result)
111 #define PROFILER_STOP(p_result)
112
113#endif // ENABLE_PROFILING
114
115#endif // PROFILER_H
: Header for main.c file. This file contains the common defines of the application.
void initDWT(void)
Initializes the DWT (Data Watchpoint and Trace) cycle counter.
Definition profiler.h:70
uint32_t start_time
Raw cycle count at the start of measurement.
Definition profiler.h:86
uint32_t elapsed_cycles
Total CPU cycles elapsed.
Definition profiler.h:87
float elapsed_us
Elapsed time in microseconds.
Definition profiler.h:89
float elapsed_ms
Elapsed time in milliseconds.
Definition profiler.h:90
float elapsed_ns
Elapsed time in nanoseconds.
Definition profiler.h:88