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