From 273e6a121746570aaff1a004f4e80ccd7eb0afb4 Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Mon, 13 Jun 2022 16:32:55 +0100 Subject: [PATCH] Add an accurate delay loop. The existing 'delay' function is not calibrated to any specific measure of time. Add a new function using a loop with a known cycle count, to produce delays of a given duration at a given CPU clock speed. --- firmware/common/hackrf_core.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/firmware/common/hackrf_core.c b/firmware/common/hackrf_core.c index ba2242cc..5bcf2944 100644 --- a/firmware/common/hackrf_core.c +++ b/firmware/common/hackrf_core.c @@ -299,6 +299,19 @@ void delay(uint32_t duration) __asm__("nop"); } +void delay_us_at_mhz(uint32_t us, uint32_t mhz) +{ + // The loop below takes 4 cycles per iteration. + uint32_t loop_iterations = (us * mhz) / 4; + asm volatile ( + "start%=:\n" + " subs %[ITERATIONS], #1\n" // 1 cycle + " bpl start%=\n" // 3 cycles + : + : [ITERATIONS] "r" (loop_iterations) + ); +} + /* GCD algo from wikipedia */ /* http://en.wikipedia.org/wiki/Greatest_common_divisor */ static uint32_t