thermostat/src/main.rs

89 lines
2.2 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-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);
let _clocks = dp.RCC.constrain()
.cfgr
.sysclk(168.mhz())
.hclk(84.mhz())
.pclk1(32.mhz())
.pclk2(64.mhz())
.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-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-13 05:52:39 +08:00
let mut t = 0;
loop {
t += 100;
let now = Instant::from_millis(t);
server.poll(now);
let adc_value = adc_input::read();
adc_value.map(|adc_value| {
writeln!(server, "t={},pa3={}", t, adc_value).unwrap();
});
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
}