Skip to content

Commit

Permalink
Add busy sleep function
Browse files Browse the repository at this point in the history
  • Loading branch information
ReeceStevens committed Mar 15, 2019
1 parent d9b2915 commit 4fcf819
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions f4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bare-metal = "0.2.0"
cortex-m = "0.5.8"
cortex-m-semihosting = "0.3.0"
nb = "0.1.1"
vcell = "0.1.0"

[dependencies.embedded-hal]
features = ["unproven"]
Expand Down
1 change: 1 addition & 0 deletions f4/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg_attr(not(test), no_std)]
#![macro_use]
pub extern crate stm32f40x;
extern crate vcell;
extern crate cortex_m;
pub extern crate cortex_m_semihosting;
extern crate embedded_hal as hal;
Expand Down
15 changes: 15 additions & 0 deletions f4/src/rcc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use vcell::VolatileCell;
use stm32f40x::{RCC, PWR, FLASH};

#[allow(dead_code)]
Expand Down Expand Up @@ -121,5 +122,19 @@ pub fn configure_system_clocks(src: SysClkSource, rcc: &RCC, pwr: &PWR, flash: &
pclk1: get_pclk1(rcc),
pclk2: get_pclk2(rcc)
}
}

/// Perform a busy wait sleep for {ms} milliseconds.
///
/// The wait time is calibrated for the HSE frequency, roughly. This is not guaranteed to be a
/// precise wait time; timers or systick should be used if more precise timing restrictions are
/// needed
pub fn busy_sleep(ms: u32) {
let loop_iterations = ms * (HSE_VALUE / 1000) / 4;
let i_cell: VolatileCell<u32> = VolatileCell::new(0);
let mut current_val = 0;
while current_val < loop_iterations {
i_cell.get();
current_val += 1;
}
}

0 comments on commit 4fcf819

Please sign in to comment.