forked from M-Labs/thermostat
add timer with systick
This commit is contained in:
parent
eac7c8232f
commit
7ce7ff2a6d
28
src/main.rs
28
src/main.rs
@ -25,6 +25,9 @@ mod adc_input;
|
|||||||
mod net;
|
mod net;
|
||||||
mod server;
|
mod server;
|
||||||
use server::Server;
|
use server::Server;
|
||||||
|
mod timer;
|
||||||
|
|
||||||
|
const OUTPUT_INTERVAL: u32 = 1000;
|
||||||
|
|
||||||
#[entry]
|
#[entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
@ -37,7 +40,7 @@ fn main() -> ! {
|
|||||||
|
|
||||||
let dp = Peripherals::take().unwrap();
|
let dp = Peripherals::take().unwrap();
|
||||||
stm32_eth::setup(&dp.RCC, &dp.SYSCFG);
|
stm32_eth::setup(&dp.RCC, &dp.SYSCFG);
|
||||||
let _clocks = dp.RCC.constrain()
|
let clocks = dp.RCC.constrain()
|
||||||
.cfgr
|
.cfgr
|
||||||
.sysclk(84.mhz())
|
.sysclk(84.mhz())
|
||||||
.hclk(84.mhz())
|
.hclk(84.mhz())
|
||||||
@ -62,20 +65,27 @@ fn main() -> ! {
|
|||||||
gpioa.pa1, gpioa.pa2, gpioa.pa7, gpiob.pb13, gpioc.pc1,
|
gpioa.pa1, gpioa.pa2, gpioa.pa7, gpiob.pb13, gpioc.pc1,
|
||||||
gpioc.pc4, gpioc.pc5, gpiog.pg11, gpiog.pg13
|
gpioc.pc4, gpioc.pc5, gpiog.pg11, gpiog.pg13
|
||||||
);
|
);
|
||||||
|
|
||||||
|
writeln!(stdout, "Timer setup").unwrap();
|
||||||
|
timer::setup(cp.SYST, clocks);
|
||||||
|
|
||||||
writeln!(stdout, "Net startup").unwrap();
|
writeln!(stdout, "Net startup").unwrap();
|
||||||
net::run(&mut cp.NVIC, dp.ETHERNET_MAC, dp.ETHERNET_DMA, |net| {
|
net::run(&mut cp.NVIC, dp.ETHERNET_MAC, dp.ETHERNET_DMA, |net| {
|
||||||
let mut server = Server::new(net);
|
let mut server = Server::new(net);
|
||||||
|
|
||||||
let mut t = 0;
|
let mut last_output = 0_u32;
|
||||||
loop {
|
loop {
|
||||||
t += 100;
|
let now = timer::now().0;
|
||||||
let now = Instant::from_millis(t);
|
let instant = Instant::from_millis(now as i64);
|
||||||
server.poll(now);
|
server.poll(instant);
|
||||||
|
|
||||||
let adc_value = adc_input::read();
|
if now - last_output >= OUTPUT_INTERVAL {
|
||||||
adc_value.map(|adc_value| {
|
let adc_value = adc_input::read();
|
||||||
writeln!(server, "t={},pa3={}", t, adc_value).unwrap();
|
adc_value.map(|adc_value| {
|
||||||
});
|
write!(server, "t={},pa3={}\r\n", now, adc_value).unwrap();
|
||||||
|
});
|
||||||
|
last_output = now;
|
||||||
|
}
|
||||||
|
|
||||||
// Update watchdog
|
// Update watchdog
|
||||||
wd.feed();
|
wd.feed();
|
||||||
|
37
src/timer.rs
Normal file
37
src/timer.rs
Normal 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()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user