From 7b6173a7bdba3ccfd06c543e0d35bc5ab5c0d46c Mon Sep 17 00:00:00 2001 From: equation314 Date: Sun, 4 Nov 2018 18:27:21 +0800 Subject: [PATCH] aarch64: move function delay() into mod timer in crate bcm2837 --- crate/bcm2837/src/asm.rs | 10 ---------- crate/bcm2837/src/gpio.rs | 22 ++++++++++------------ crate/bcm2837/src/interrupt.rs | 2 +- crate/bcm2837/src/lib.rs | 2 -- crate/bcm2837/src/mini_uart.rs | 2 +- crate/bcm2837/src/timer/mod.rs | 8 ++++++++ 6 files changed, 20 insertions(+), 26 deletions(-) delete mode 100644 crate/bcm2837/src/asm.rs diff --git a/crate/bcm2837/src/asm.rs b/crate/bcm2837/src/asm.rs deleted file mode 100644 index a496d92..0000000 --- a/crate/bcm2837/src/asm.rs +++ /dev/null @@ -1,10 +0,0 @@ -//! utility assembly instructions - -/// delay for some clocks -#[inline] -pub unsafe fn delay(_clock: u32) { - #[cfg(target_arch = "aarch64")] - asm!("mov x1, x0; 1: subs x1, x1, #1; bne 1b;" - :: "{x0}"(_clock) - :: "volatile"); -} diff --git a/crate/bcm2837/src/gpio.rs b/crate/bcm2837/src/gpio.rs index 1b340c7..1fb7fec 100644 --- a/crate/bcm2837/src/gpio.rs +++ b/crate/bcm2837/src/gpio.rs @@ -1,5 +1,5 @@ -use super::IO_BASE; -use super::asm::delay; +use IO_BASE; +use timer::delay; use core::marker::PhantomData; use volatile::{ReadOnly, Volatile, WriteOnly}; @@ -84,16 +84,14 @@ impl Gpio { /// Set the Gpio pull-up/pull-down state for values in `pin_value` /// (ref: peripherals 6.1, page 101) pub fn set_gpio_pd(&mut self, pud_value: u8) { - unsafe { - let index = if self.pin >= 32 { 1 } else { 0 }; - - self.registers.PUD.write(pud_value as u32); - delay(150); - self.registers.PUDCLK[index as usize].write((1 << self.pin) as u32); - delay(150); - self.registers.PUD.write(0); - self.registers.PUDCLK[index as usize].write(0); - } + let index = if self.pin >= 32 { 1 } else { 0 }; + + self.registers.PUD.write(pud_value as u32); + delay(150); + self.registers.PUDCLK[index as usize].write((1 << self.pin) as u32); + delay(150); + self.registers.PUD.write(0); + self.registers.PUDCLK[index as usize].write(0); } } diff --git a/crate/bcm2837/src/interrupt.rs b/crate/bcm2837/src/interrupt.rs index cf340ee..7324085 100644 --- a/crate/bcm2837/src/interrupt.rs +++ b/crate/bcm2837/src/interrupt.rs @@ -1,4 +1,4 @@ -use super::IO_BASE; +use IO_BASE; use volatile::{ReadOnly, Volatile}; const INT_BASE: usize = IO_BASE + 0xB000 + 0x200; diff --git a/crate/bcm2837/src/lib.rs b/crate/bcm2837/src/lib.rs index 13da9ff..9635fdc 100644 --- a/crate/bcm2837/src/lib.rs +++ b/crate/bcm2837/src/lib.rs @@ -3,8 +3,6 @@ extern crate volatile; -mod asm; - pub mod gpio; pub mod timer; pub mod mini_uart; diff --git a/crate/bcm2837/src/mini_uart.rs b/crate/bcm2837/src/mini_uart.rs index 5816017..28e2c13 100644 --- a/crate/bcm2837/src/mini_uart.rs +++ b/crate/bcm2837/src/mini_uart.rs @@ -1,4 +1,4 @@ -use super::IO_BASE; +use IO_BASE; use gpio::{Function, Gpio}; use volatile::{ReadOnly, Volatile}; diff --git a/crate/bcm2837/src/timer/mod.rs b/crate/bcm2837/src/timer/mod.rs index da501cb..f88b33d 100644 --- a/crate/bcm2837/src/timer/mod.rs +++ b/crate/bcm2837/src/timer/mod.rs @@ -24,3 +24,11 @@ pub fn current_time() -> u64 { pub fn tick_in(us: u32) { Timer::new().tick_in(us); } + +/// wait for `cycle` CPU cycles +#[inline(always)] +pub fn delay(cycle: u32) { + for _ in 0..cycle { + unsafe { asm!("nop") } + } +}