From e44d2c0010efa3ab3a3ae747dfc10a29faea7677 Mon Sep 17 00:00:00 2001 From: Graham Sanderson Date: Mon, 16 May 2022 16:45:39 -0500 Subject: [PATCH] Add busy_wait_at_least_cycles method (#830) --- .../pico_platform/include/pico/platform.h | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/rp2_common/pico_platform/include/pico/platform.h b/src/rp2_common/pico_platform/include/pico/platform.h index e5176c8..de64e30 100644 --- a/src/rp2_common/pico_platform/include/pico/platform.h +++ b/src/rp2_common/pico_platform/include/pico/platform.h @@ -400,6 +400,29 @@ uint __get_current_exception(void); } #endif +/*! \brief Helper method to busy-wait for at least the given number of cycles + * \ingroup pico_platform + * + * This method is useful for introducing very short delays. + * + * This method busy-waits in a tight loop for the give number of system clock cycles. The total wait time is only accurate to within 2 cycles, + * and this method uses a loop counter rather than a hardware timer, so the method will always take longer than expected if an + * interrupt is handled on the calling core when during the busy-wait; you can of course disable interrupts to prevent this. + * + * You can use \ref clock_get_hz(clk_sys) to determine the number of clock cycles per second if you want to convert to cycles + * from an actual time duration. + * + * \param minimum_cycles the minimum number of system clock cycles to delay for + */ +static inline void busy_wait_at_least_cycles(uint32_t minimum_cycles) { + __asm volatile ( + ".syntax unified\n" + "1: subs %0, #3\n" + "bcs 1b\n" + : "+r" (minimum_cycles) : : "memory" + ); +} + #else // __ASSEMBLER__ #define WRAPPER_FUNC_NAME(x) __wrap_##x