add timer with systick

This commit is contained in:
Astro 2019-03-14 18:13:25 +01:00
parent eac7c8232f
commit 7ce7ff2a6d
2 changed files with 56 additions and 9 deletions

View File

@ -25,6 +25,9 @@ mod adc_input;
mod net;
mod server;
use server::Server;
mod timer;
const OUTPUT_INTERVAL: u32 = 1000;
#[entry]
fn main() -> ! {
@ -37,7 +40,7 @@ fn main() -> ! {
let dp = Peripherals::take().unwrap();
stm32_eth::setup(&dp.RCC, &dp.SYSCFG);
let _clocks = dp.RCC.constrain()
let clocks = dp.RCC.constrain()
.cfgr
.sysclk(84.mhz())
.hclk(84.mhz())
@ -62,20 +65,27 @@ fn main() -> ! {
gpioa.pa1, gpioa.pa2, gpioa.pa7, gpiob.pb13, gpioc.pc1,
gpioc.pc4, gpioc.pc5, gpiog.pg11, gpiog.pg13
);
writeln!(stdout, "Timer setup").unwrap();
timer::setup(cp.SYST, clocks);
writeln!(stdout, "Net startup").unwrap();
net::run(&mut cp.NVIC, dp.ETHERNET_MAC, dp.ETHERNET_DMA, |net| {
let mut server = Server::new(net);
let mut t = 0;
let mut last_output = 0_u32;
loop {
t += 100;
let now = Instant::from_millis(t);
server.poll(now);
let now = timer::now().0;
let instant = Instant::from_millis(now as i64);
server.poll(instant);
let adc_value = adc_input::read();
adc_value.map(|adc_value| {
writeln!(server, "t={},pa3={}", t, adc_value).unwrap();
});
if now - last_output >= OUTPUT_INTERVAL {
let adc_value = adc_input::read();
adc_value.map(|adc_value| {
write!(server, "t={},pa3={}\r\n", now, adc_value).unwrap();
});
last_output = now;
}
// Update watchdog
wd.feed();

37
src/timer.rs Normal file
View File

@ -0,0 +1,37 @@
use core::cell::RefCell;
use cortex_m::interrupt::Mutex;
use cortex_m_rt::exception;
use stm32f4xx_hal::{
rcc::Clocks,
time::{U32Ext, MilliSeconds},
timer::{Timer, Event as TimerEvent},
stm32::SYST,
};
/// rate in Hz
const TIMER_RATE: u32 = 100;
/// interval duration in milliseconds
const TIMER_DELTA: u32 = 1000 / TIMER_RATE;
/// Elapsed time in milliseconds
static TIMER_MS: Mutex<RefCell<u32>> = Mutex::new(RefCell::new(0));
pub fn setup(syst: SYST, clocks: Clocks) {
let mut timer = Timer::syst(syst, TIMER_RATE.hz(), clocks);
timer.listen(TimerEvent::TimeOut);
}
#[exception]
fn SysTick() {
cortex_m::interrupt::free(|cs| {
*TIMER_MS.borrow(cs)
.borrow_mut() += TIMER_DELTA;
});
}
pub fn now() -> MilliSeconds {
let ms = cortex_m::interrupt::free(|cs| {
*TIMER_MS.borrow(cs)
.borrow()
});
ms.ms()
}