aboutsummaryrefslogtreecommitdiff
path: root/src/asm.rs
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2018-03-12 03:15:07 +0100
committerGravatar Jorge Aparicio <jorge@japaric.io> 2018-05-18 18:45:23 +0200
commitfce677de07af0fc11ec259e1ba2fbc2f017e6f13 (patch)
treefb06098af64e13e74197e72f461c2c1422250e41 /src/asm.rs
parentcf262c922fce7c5b0cff3b5d4acfd4008d194fa6 (diff)
downloadcortex-m-fce677de07af0fc11ec259e1ba2fbc2f017e6f13.tar.gz
cortex-m-fce677de07af0fc11ec259e1ba2fbc2f017e6f13.tar.zst
cortex-m-fce677de07af0fc11ec259e1ba2fbc2f017e6f13.zip
add asm::delay
Diffstat (limited to 'src/asm.rs')
-rw-r--r--src/asm.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/asm.rs b/src/asm.rs
index 6e90f09..4b5d88c 100644
--- a/src/asm.rs
+++ b/src/asm.rs
@@ -24,6 +24,30 @@ pub fn bkpt() {
}
}
+/// Blocks the program for at least `n` instruction cycles
+///
+/// This is implemented in assembly so its execution time is the same regardless of the optimization
+/// level.
+///
+/// NOTE that the delay can take much longer if interrupts are serviced during its execution.
+#[inline(never)]
+pub fn delay(_n: u32) {
+ match () {
+ #[cfg(target_arch = "arm")]
+ () => unsafe {
+ asm!("1:
+ subs $0, $$1
+ bne.n 1b"
+ :
+ : "r"(_n / 3 + 1)
+ :
+ : "volatile");
+ },
+ #[cfg(not(target_arch = "arm"))]
+ () => unimplemented!(),
+ }
+}
+
/// A no-operation. Useful to prevent delay loops from being optimized away.
#[inline]
pub fn nop() {