Add busy_wait_at_least_cycles method (#830)

This commit is contained in:
Graham Sanderson 2022-05-16 16:45:39 -05:00 committed by GitHub
parent 43a5593e8f
commit e44d2c0010
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -400,6 +400,29 @@ uint __get_current_exception(void);
} }
#endif #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__ #else // __ASSEMBLER__
#define WRAPPER_FUNC_NAME(x) __wrap_##x #define WRAPPER_FUNC_NAME(x) __wrap_##x