forked from M-Labs/ionpak-thermostat
systick: use mutex
This commit is contained in:
parent
225f3754a1
commit
4249addba2
|
@ -128,6 +128,7 @@ dependencies = [
|
|||
name = "ionpak-firmware"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"bare-metal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bit_field 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -18,6 +18,7 @@ nb = "0.1"
|
|||
cortex-m-semihosting = "0.3"
|
||||
byteorder = { version = "1.3", default-features = false }
|
||||
bit_field = "0.10"
|
||||
bare-metal = "0.2"
|
||||
|
||||
[dependencies.smoltcp]
|
||||
git = "https://github.com/m-labs/smoltcp"
|
||||
|
|
|
@ -9,7 +9,7 @@ pub mod systick;
|
|||
const UART_DIV: u32 = (((/*sysclk*/120_000_000 * 8) / /*baud*/115200) + 1) / 2;
|
||||
|
||||
pub fn init() {
|
||||
cortex_m::interrupt::free(|_cs| {
|
||||
cortex_m::interrupt::free(|cs| {
|
||||
let sysctl = unsafe { &*tm4c129x::SYSCTL::ptr() };
|
||||
|
||||
// Set up main oscillator
|
||||
|
@ -170,7 +170,7 @@ pub fn init() {
|
|||
setup_timer_pwm!(TIMER4);
|
||||
setup_timer_pwm!(TIMER5);
|
||||
|
||||
systick::init();
|
||||
systick::init(cs);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
use cortex_m_rt::exception;
|
||||
use core::cell::RefCell;
|
||||
use cortex_m::interrupt::Mutex;
|
||||
use cortex_m::peripheral::{SYST, syst::SystClkSource};
|
||||
use cortex_m_rt::exception;
|
||||
use bare_metal::CriticalSection;
|
||||
|
||||
const SYSTICK_RATE: u32 = 1000;
|
||||
const SYSTICK_RATE: u32 = 250;
|
||||
|
||||
static mut TIME: u64 = 0;
|
||||
|
||||
pub fn init() {
|
||||
unsafe { TIME = 0 };
|
||||
static mut TIME: Mutex<RefCell<u64>> = Mutex::new(RefCell::new(0));
|
||||
|
||||
pub fn init(cs: &CriticalSection) {
|
||||
#[allow(mutable_transmutes)]
|
||||
let syst: &mut SYST = unsafe { core::mem::transmute(&*SYST::ptr()) };
|
||||
syst.set_clock_source(SystClkSource::Core);
|
||||
syst.set_clock_source(SystClkSource::External);
|
||||
syst.set_reload(100 * SYST::get_ticks_per_10ms() / SYSTICK_RATE);
|
||||
syst.clear_current();
|
||||
syst.enable_interrupt();
|
||||
|
@ -19,9 +20,14 @@ pub fn init() {
|
|||
|
||||
#[exception]
|
||||
unsafe fn SysTick() {
|
||||
TIME += u64::from(1000 / SYSTICK_RATE);
|
||||
let interval = u64::from(1000 / SYSTICK_RATE);
|
||||
cortex_m::interrupt::free(|cs| {
|
||||
TIME.borrow(cs).replace_with(|time| *time + interval);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn get_time() -> u64 {
|
||||
unsafe { TIME }
|
||||
cortex_m::interrupt::free(|cs| {
|
||||
*unsafe { &mut TIME }.borrow(cs).borrow()
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue