From eb7faf3087ec96b7f9d80d4c89f2f2a5c57ab692 Mon Sep 17 00:00:00 2001 From: Phillip Johnston Date: Tue, 4 Aug 2020 09:17:22 -0700 Subject: [PATCH] Add a commented interrupt latency measurement example --- examples/c/interrupt_latency.c | 42 ++++++++++++++++++++++++++++++++++ examples/c/meson.build | 4 ++++ 2 files changed, 46 insertions(+) create mode 100644 examples/c/interrupt_latency.c diff --git a/examples/c/interrupt_latency.c b/examples/c/interrupt_latency.c new file mode 100644 index 0000000..915cfa8 --- /dev/null +++ b/examples/c/interrupt_latency.c @@ -0,0 +1,42 @@ +#include + +// 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++; +} + + diff --git a/examples/c/meson.build b/examples/c/meson.build index 97ed32b..347087f 100644 --- a/examples/c/meson.build +++ b/examples/c/meson.build @@ -1,5 +1,9 @@ # C examples Meson Build File +static_library('interrupt_latency', + 'interrupt_latency.c' +) + executable('circular_buffer', [ 'circular_buffer_test.c',