thermostat/src/main.rs

99 lines
2.4 KiB
Rust
Raw Normal View History

2019-03-07 23:27:33 +08:00
#![no_std]
#![no_main]
2019-03-13 05:52:39 +08:00
// Enable returning `!`
#![feature(never_type)]
2019-03-07 23:27:33 +08:00
#[allow(unused_extern_crates)]
2019-03-12 01:23:52 +08:00
extern crate panic_abort;
2019-03-07 23:27:33 +08:00
2019-03-12 01:23:52 +08:00
use cortex_m::asm::wfi;
2019-03-07 23:27:33 +08:00
use cortex_m_rt::entry;
2019-03-12 01:23:52 +08:00
use embedded_hal::watchdog::{WatchdogEnable, Watchdog};
use stm32f4xx_hal::{
rcc::RccExt,
gpio::GpioExt,
watchdog::IndependentWatchdog,
time::U32Ext,
stm32::{CorePeripherals, Peripherals},
};
2019-03-13 05:52:39 +08:00
use smoltcp::time::Instant;
2019-03-12 01:23:52 +08:00
use core::fmt::Write;
use cortex_m_semihosting::hio;
mod adc_input;
2019-03-13 05:52:39 +08:00
mod net;
mod server;
use server::Server;
2019-03-15 01:13:25 +08:00
mod timer;
const OUTPUT_INTERVAL: u32 = 1000;
2019-03-07 23:27:33 +08:00
#[entry]
fn main() -> ! {
2019-03-12 01:23:52 +08:00
let mut stdout = hio::hstdout().unwrap();
2019-03-13 05:52:39 +08:00
writeln!(stdout, "adc2tcp").unwrap();
2019-03-12 01:23:52 +08:00
let mut cp = CorePeripherals::take().unwrap();
2019-03-13 05:52:39 +08:00
cp.SCB.enable_icache();
cp.SCB.enable_dcache(&mut cp.CPUID);
2019-03-12 01:23:52 +08:00
let dp = Peripherals::take().unwrap();
stm32_eth::setup(&dp.RCC, &dp.SYSCFG);
2019-03-15 01:13:25 +08:00
let clocks = dp.RCC.constrain()
2019-03-12 01:23:52 +08:00
.cfgr
.sysclk(84.mhz())
2019-03-12 01:23:52 +08:00
.hclk(84.mhz())
.pclk1(16.mhz())
.pclk2(32.mhz())
2019-03-12 01:23:52 +08:00
.freeze();
let mut wd = IndependentWatchdog::new(dp.IWDG);
wd.start(8000u32.ms());
wd.feed();
let gpioa = dp.GPIOA.split();
let gpiob = dp.GPIOB.split();
let gpioc = dp.GPIOC.split();
let gpiog = dp.GPIOG.split();
2019-03-13 05:52:39 +08:00
writeln!(stdout, "ADC init").unwrap();
adc_input::setup(&mut cp.NVIC, dp.ADC1, gpioa.pa3);
writeln!(stdout, "Eth setup").unwrap();
2019-03-12 01:23:52 +08:00
stm32_eth::setup_pins(
gpioa.pa1, gpioa.pa2, gpioa.pa7, gpiob.pb13, gpioc.pc1,
gpioc.pc4, gpioc.pc5, gpiog.pg11, gpiog.pg13
);
2019-03-15 01:13:25 +08:00
writeln!(stdout, "Timer setup").unwrap();
timer::setup(cp.SYST, clocks);
2019-03-13 05:52:39 +08:00
writeln!(stdout, "Net startup").unwrap();
net::run(&mut cp.NVIC, dp.ETHERNET_MAC, dp.ETHERNET_DMA, |net| {
let mut server = Server::new(net);
2019-03-12 01:23:52 +08:00
2019-03-15 01:13:25 +08:00
let mut last_output = 0_u32;
2019-03-13 05:52:39 +08:00
loop {
2019-03-15 01:13:25 +08:00
let now = timer::now().0;
let instant = Instant::from_millis(now as i64);
server.poll(instant);
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;
}
2019-03-12 01:23:52 +08:00
2019-03-13 05:52:39 +08:00
// Update watchdog
wd.feed();
// Wait for interrupts
// if net.is_pending() {
wfi();
// }
}
})
2019-03-07 23:27:33 +08:00
}