Skip to content

Commit e44d2c0

Browse files
authored
Add busy_wait_at_least_cycles method (raspberrypi#830)
1 parent 43a5593 commit e44d2c0

File tree

1 file changed

+23
-0
lines changed
  • src/rp2_common/pico_platform/include/pico

1 file changed

+23
-0
lines changed

src/rp2_common/pico_platform/include/pico/platform.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,29 @@ uint __get_current_exception(void);
400400
}
401401
#endif
402402

403+
/*! \brief Helper method to busy-wait for at least the given number of cycles
404+
* \ingroup pico_platform
405+
*
406+
* This method is useful for introducing very short delays.
407+
*
408+
* 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,
409+
* and this method uses a loop counter rather than a hardware timer, so the method will always take longer than expected if an
410+
* interrupt is handled on the calling core when during the busy-wait; you can of course disable interrupts to prevent this.
411+
*
412+
* 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
413+
* from an actual time duration.
414+
*
415+
* \param minimum_cycles the minimum number of system clock cycles to delay for
416+
*/
417+
static inline void busy_wait_at_least_cycles(uint32_t minimum_cycles) {
418+
__asm volatile (
419+
".syntax unified\n"
420+
"1: subs %0, #3\n"
421+
"bcs 1b\n"
422+
: "+r" (minimum_cycles) : : "memory"
423+
);
424+
}
425+
403426
#else // __ASSEMBLER__
404427

405428
#define WRAPPER_FUNC_NAME(x) __wrap_##x

0 commit comments

Comments
 (0)