Skip to content

Commit

Permalink
Add a commented interrupt latency measurement example
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipjohnston committed Aug 4, 2020
1 parent b720fce commit eb7faf3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/c/interrupt_latency.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdint.h>

// The size of these variables should match the size of the timer value.
uint32_t min_latency = UINT32_MAX;
uint32_t max_latency = 0;
// Note that the type of total_latency is larger than min/max latency.
// This is because total_latency is an accumulator and may overflow.
uint64_t total_latency = 0;

/// Later in your program you can calculate average latency using total_latency / count.
uint32_t count = 0;

__attribute__((weak)) inline uint32_t readTimer()
{
// Override this function to read from the target timer peripheral.
// Our advice is to read the raw timer value in as few steps as possible,
// skipping any conversion steps. This will give you the most accurate look
// at interrupt latency. The application code (or a spreadsheet) can handle
// the conversion from count to time for you.
return 0;
}

// Call this function from the timer interrupt handler
void latency(void)
{
uint32_t time = readTimer();

if(time < min_latency)
{
min_latency = time;
}

if(time > max_latency)
{
max_latency = time;
}

total_latency += time;
count++;
}


4 changes: 4 additions & 0 deletions examples/c/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# C examples Meson Build File

static_library('interrupt_latency',
'interrupt_latency.c'
)

executable('circular_buffer',
[
'circular_buffer_test.c',
Expand Down

0 comments on commit eb7faf3

Please sign in to comment.