systick: use mutex

This commit is contained in:
Astro 2019-09-08 02:13:02 +02:00
parent 225f3754a1
commit 4249addba2
4 changed files with 19 additions and 11 deletions

1
firmware/Cargo.lock generated
View File

@ -128,6 +128,7 @@ dependencies = [
name = "ionpak-firmware" name = "ionpak-firmware"
version = "1.0.0" version = "1.0.0"
dependencies = [ 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)", "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)", "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)", "cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -18,6 +18,7 @@ nb = "0.1"
cortex-m-semihosting = "0.3" cortex-m-semihosting = "0.3"
byteorder = { version = "1.3", default-features = false } byteorder = { version = "1.3", default-features = false }
bit_field = "0.10" bit_field = "0.10"
bare-metal = "0.2"
[dependencies.smoltcp] [dependencies.smoltcp]
git = "https://github.com/m-labs/smoltcp" git = "https://github.com/m-labs/smoltcp"

View File

@ -9,7 +9,7 @@ pub mod systick;
const UART_DIV: u32 = (((/*sysclk*/120_000_000 * 8) / /*baud*/115200) + 1) / 2; const UART_DIV: u32 = (((/*sysclk*/120_000_000 * 8) / /*baud*/115200) + 1) / 2;
pub fn init() { pub fn init() {
cortex_m::interrupt::free(|_cs| { cortex_m::interrupt::free(|cs| {
let sysctl = unsafe { &*tm4c129x::SYSCTL::ptr() }; let sysctl = unsafe { &*tm4c129x::SYSCTL::ptr() };
// Set up main oscillator // Set up main oscillator
@ -170,7 +170,7 @@ pub fn init() {
setup_timer_pwm!(TIMER4); setup_timer_pwm!(TIMER4);
setup_timer_pwm!(TIMER5); setup_timer_pwm!(TIMER5);
systick::init(); systick::init(cs);
}); });
} }

View File

@ -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::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<RefCell<u64>> = Mutex::new(RefCell::new(0));
pub fn init() { pub fn init(cs: &CriticalSection) {
unsafe { TIME = 0 };
#[allow(mutable_transmutes)] #[allow(mutable_transmutes)]
let syst: &mut SYST = unsafe { core::mem::transmute(&*SYST::ptr()) }; 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.set_reload(100 * SYST::get_ticks_per_10ms() / SYSTICK_RATE);
syst.clear_current(); syst.clear_current();
syst.enable_interrupt(); syst.enable_interrupt();
@ -19,9 +20,14 @@ pub fn init() {
#[exception] #[exception]
unsafe fn SysTick() { 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 { pub fn get_time() -> u64 {
unsafe { TIME } cortex_m::interrupt::free(|cs| {
*unsafe { &mut TIME }.borrow(cs).borrow()
})
} }