Skip to content

Commit fce677d

Browse files
committed
add asm::delay
1 parent cf262c9 commit fce677d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/asm.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ pub fn bkpt() {
2424
}
2525
}
2626

27+
/// Blocks the program for at least `n` instruction cycles
28+
///
29+
/// This is implemented in assembly so its execution time is the same regardless of the optimization
30+
/// level.
31+
///
32+
/// NOTE that the delay can take much longer if interrupts are serviced during its execution.
33+
#[inline(never)]
34+
pub fn delay(_n: u32) {
35+
match () {
36+
#[cfg(target_arch = "arm")]
37+
() => unsafe {
38+
asm!("1:
39+
subs $0, $$1
40+
bne.n 1b"
41+
:
42+
: "r"(_n / 3 + 1)
43+
:
44+
: "volatile");
45+
},
46+
#[cfg(not(target_arch = "arm"))]
47+
() => unimplemented!(),
48+
}
49+
}
50+
2751
/// A no-operation. Useful to prevent delay loops from being optimized away.
2852
#[inline]
2953
pub fn nop() {

0 commit comments

Comments
 (0)