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-15 02:58:41 +08:00
|
|
|
#[cfg(not(feature = "semihosting"))]
|
2019-03-12 01:23:52 +08:00
|
|
|
extern crate panic_abort;
|
2019-03-15 02:58:41 +08:00
|
|
|
#[cfg(feature = "semihosting")]
|
|
|
|
extern crate panic_semihosting;
|
2019-03-07 23:27:33 +08:00
|
|
|
|
2019-03-15 02:58:41 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
use core::fmt::Write;
|
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
|
|
|
|
|
|
|
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;
|
2019-03-15 03:43:35 +08:00
|
|
|
mod led;
|
|
|
|
use led::Led;
|
2019-03-15 01:13:25 +08:00
|
|
|
|
|
|
|
const OUTPUT_INTERVAL: u32 = 1000;
|
2019-03-07 23:27:33 +08:00
|
|
|
|
2019-03-15 02:58:41 +08:00
|
|
|
#[cfg(not(feature = "semihosting"))]
|
|
|
|
fn init_log() {}
|
|
|
|
|
|
|
|
#[cfg(feature = "semihosting")]
|
|
|
|
fn init_log() {
|
|
|
|
use log::LevelFilter;
|
|
|
|
use cortex_m_log::log::{Logger, init};
|
|
|
|
use cortex_m_log::printer::semihosting::{InterruptOk, hio::HStdout};
|
|
|
|
static mut LOGGER: Option<Logger<InterruptOk<HStdout>>> = None;
|
|
|
|
let logger = Logger {
|
|
|
|
inner: InterruptOk::<_>::stdout().expect("semihosting stdout"),
|
|
|
|
level: LevelFilter::Info,
|
|
|
|
};
|
|
|
|
let logger = unsafe {
|
|
|
|
LOGGER.get_or_insert(logger)
|
|
|
|
};
|
|
|
|
|
|
|
|
init(logger).expect("set logger");
|
|
|
|
}
|
|
|
|
|
2019-03-07 23:27:33 +08:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
2019-03-15 02:58:41 +08:00
|
|
|
init_log();
|
|
|
|
info!("adc2tcp");
|
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
|
2019-03-15 01:11:07 +08:00
|
|
|
.sysclk(84.mhz())
|
2019-03-12 01:23:52 +08:00
|
|
|
.hclk(84.mhz())
|
2019-03-15 01:11:07 +08:00
|
|
|
.pclk1(16.mhz())
|
|
|
|
.pclk2(32.mhz())
|
2019-03-12 01:23:52 +08:00
|
|
|
.freeze();
|
|
|
|
|
|
|
|
let mut wd = IndependentWatchdog::new(dp.IWDG);
|
2019-03-15 05:01:01 +08:00
|
|
|
wd.start(1000u32.ms());
|
2019-03-12 01:23:52 +08:00
|
|
|
wd.feed();
|
|
|
|
|
|
|
|
let gpioa = dp.GPIOA.split();
|
|
|
|
let gpiob = dp.GPIOB.split();
|
|
|
|
let gpioc = dp.GPIOC.split();
|
|
|
|
let gpiog = dp.GPIOG.split();
|
|
|
|
|
2019-03-15 03:43:35 +08:00
|
|
|
let mut led_green = Led::green(gpiob.pb0.into_push_pull_output());
|
|
|
|
let mut led_blue = Led::blue(gpiob.pb7.into_push_pull_output());
|
|
|
|
let mut led_red = Led::red(gpiob.pb14.into_push_pull_output());
|
|
|
|
|
2019-03-15 02:58:41 +08:00
|
|
|
info!("ADC init");
|
2019-03-13 05:52:39 +08:00
|
|
|
adc_input::setup(&mut cp.NVIC, dp.ADC1, gpioa.pa3);
|
|
|
|
|
2019-03-15 02:58:41 +08:00
|
|
|
info!("Eth setup");
|
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
|
|
|
|
2019-03-15 02:58:41 +08:00
|
|
|
info!("Timer setup");
|
2019-03-15 01:13:25 +08:00
|
|
|
timer::setup(cp.SYST, clocks);
|
|
|
|
|
2019-03-15 02:58:41 +08:00
|
|
|
info!("Net startup");
|
2019-03-13 05:52:39 +08:00
|
|
|
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 05:07:26 +08:00
|
|
|
led_green.on();
|
2019-03-15 01:13:25 +08:00
|
|
|
let now = timer::now().0;
|
|
|
|
let instant = Instant::from_millis(now as i64);
|
|
|
|
server.poll(instant);
|
2019-03-15 05:07:26 +08:00
|
|
|
led_green.off();
|
2019-03-15 01:13:25 +08:00
|
|
|
|
2019-03-15 05:07:26 +08:00
|
|
|
led_blue.on();
|
|
|
|
let now = timer::now().0;
|
2019-03-15 01:13:25 +08:00
|
|
|
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-15 05:07:26 +08:00
|
|
|
led_blue.off();
|
2019-03-12 01:23:52 +08:00
|
|
|
|
2019-03-13 05:52:39 +08:00
|
|
|
// Update watchdog
|
|
|
|
wd.feed();
|
2019-03-15 03:43:35 +08:00
|
|
|
|
2019-03-15 05:02:08 +08:00
|
|
|
led_red.on();
|
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
if !net::is_pending(cs) {
|
|
|
|
// Wait for interrupts
|
|
|
|
wfi();
|
|
|
|
net::clear_pending(cs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
led_red.off();
|
2019-03-13 05:52:39 +08:00
|
|
|
}
|
|
|
|
})
|
2019-03-07 23:27:33 +08:00
|
|
|
}
|