From 4249addba21ccddea141804edf9ea82ad920565c Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 8 Sep 2019 02:13:02 +0200 Subject: [PATCH] systick: use mutex --- firmware/Cargo.lock | 1 + firmware/Cargo.toml | 1 + firmware/src/board/mod.rs | 4 ++-- firmware/src/board/systick.rs | 24 +++++++++++++++--------- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/firmware/Cargo.lock b/firmware/Cargo.lock index d418b3b..ce4b34a 100644 --- a/firmware/Cargo.lock +++ b/firmware/Cargo.lock @@ -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)", diff --git a/firmware/Cargo.toml b/firmware/Cargo.toml index b541278..ac7f3d3 100644 --- a/firmware/Cargo.toml +++ b/firmware/Cargo.toml @@ -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" diff --git a/firmware/src/board/mod.rs b/firmware/src/board/mod.rs index ac2aa80..ac11193 100644 --- a/firmware/src/board/mod.rs +++ b/firmware/src/board/mod.rs @@ -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); }); } diff --git a/firmware/src/board/systick.rs b/firmware/src/board/systick.rs index 779c2a7..24bf3db 100644 --- a/firmware/src/board/systick.rs +++ b/firmware/src/board/systick.rs @@ -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; +static mut TIME: Mutex> = Mutex::new(RefCell::new(0)); -pub fn init() { - unsafe { TIME = 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() + }) }