1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! # Systick for the LM4F120
//!
//! This just wraps the generic Cortex-M4 systick, but it
//! understands the PLL clock rate (which the generic code cannot)

// ****************************************************************************
//
// Imports
//
// ****************************************************************************

pub use super::cortex_m4f::systick::*;
use super::pll;

// ****************************************************************************
//
// Public Types
//
// ****************************************************************************

// None

// ****************************************************************************
//
// Public Data
//
// ****************************************************************************

/// SysTick runs at / 4, so at 16MHz that's 4MHz
pub const SYSTICK_CLOCK: usize = pll::CRYSTAL_CLOCK_HZ / 4;

/// At 4MHz, four ticks per microseconds.
pub const SYSTICK_CLOCK_PER_US: usize = SYSTICK_CLOCK / 1_000_000;

// ****************************************************************************
//
// Private Types
//
// ****************************************************************************

// None

// ****************************************************************************
//
// Private Data
//
// ****************************************************************************

// None

// ****************************************************************************
//
// Public Functions
//
// ****************************************************************************

/// Converts from SysTicks to microseconds
pub fn ticks_to_usecs(ticks: usize) -> usize {
    ticks / SYSTICK_CLOCK_PER_US
}

/// Converts from microseconds to SysTicks
pub fn usecs_to_ticks(usecs: usize) -> usize {
    usecs * SYSTICK_CLOCK_PER_US
}

/// Busy-waits for the given period.
///
/// Uses SysTick to wait the correct amount of time.
///
/// * `ms` - The period to wait, in milliseconds
pub fn delay(ms: u32) {
    // We can manage 4 seconds before SysTick wraps
    // We divide it up into seconds.
    let seconds = ms / 1000;
    let usec = (ms % 1000) * 1000;
    for _ in 0..seconds {
        delay_usec(1_000_000);
    }
    delay_usec(usec);
}

/// Busy-waits a specified number of microseconds.
/// `usec` must be less than 2**22 otherwise SysTick
/// will overflow.
pub fn delay_usec(usec: u32) {
    let start = get_ticks();
    let ticks = usecs_to_ticks(usec as usize);
    loop {
        if get_since(start) >= ticks {
            break;
        }
    }
}

/// How long since the system booted in microseconds.
/// The u64 is good for 584,000 years.
pub fn run_time_us() -> u64 {
    let mut result = run_time_ticks();
    result /= SYSTICK_CLOCK_PER_US as u64;
    result
}

// ****************************************************************************
//
// Private Functions
//
// ****************************************************************************

// None

// ****************************************************************************
//
// End Of File
//
// ****************************************************************************