From 9c3485d05fd4f077a6210eea8d143833578f57af Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 21 Mar 2020 00:07:18 +0100 Subject: [PATCH] timer: increase rate to 500 Hz timestamp precision: 0.002s --- src/main.rs | 4 ++-- src/timer.rs | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 192a884..f5a9203 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,14 +119,14 @@ fn main() -> ! { net::run(dp.ETHERNET_MAC, dp.ETHERNET_DMA, hwaddr, |iface| { Server::::run(iface, |server| { loop { - let now = timer::now().0; - let instant = Instant::from_millis(i64::from(now)); + let instant = Instant::from_millis(i64::from(timer::now())); cortex_m::interrupt::free(net::clear_pending); server.poll(instant) .unwrap_or_else(|e| { warn!("poll: {:?}", e); }); + let instant = Instant::from_millis(i64::from(timer::now())); // ADC input adc.data_ready().unwrap().map(|channel| { let data = adc.read_data().unwrap(); diff --git a/src/timer.rs b/src/timer.rs index 7d57d28..ba32712 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -1,15 +1,16 @@ use core::cell::RefCell; +use core::ops::Deref; use cortex_m::interrupt::Mutex; use cortex_m_rt::exception; use stm32f4xx_hal::{ rcc::Clocks, - time::{U32Ext, MilliSeconds}, + time::U32Ext, timer::{Timer, Event as TimerEvent}, stm32::SYST, }; /// Rate in Hz -const TIMER_RATE: u32 = 20; +const TIMER_RATE: u32 = 500; /// Interval duration in milliseconds const TIMER_DELTA: u32 = 1000 / TIMER_RATE; /// Elapsed time in milliseconds @@ -31,10 +32,10 @@ fn SysTick() { } /// Obtain current time in milliseconds -pub fn now() -> MilliSeconds { - let ms = cortex_m::interrupt::free(|cs| { +pub fn now() -> u32 { + cortex_m::interrupt::free(|cs| { *TIMER_MS.borrow(cs) .borrow() - }); - ms.ms() + .deref() + }) }