From c1b0e545506d6add3538d97f0255c7ed017778e9 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 14 Mar 2019 19:58:41 +0100 Subject: [PATCH] use semihosting with a feature-flag --- Cargo.toml | 9 ++++++--- shell.nix | 2 +- src/main.rs | 42 +++++++++++++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 153f07c..d4dd5bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,11 +19,12 @@ features = [] default-target = "thumbv7em-none-eabihf" [dependencies] -panic-abort = "0.3.1" - +panic-abort = { version = "0.3.1" } +panic-semihosting = { version = "0.5.1", optional = true } +log = "0.4" cortex-m = "0.5" cortex-m-rt = { version = "0.6", features = ["device"] } -cortex-m-semihosting = "0.3" +cortex-m-log = { version = "0.4", features = ["log-integration"] } stm32f4 = { version = "0.6", features = ["rt", "stm32f429"] } embedded-hal = "0.2" stm32f4xx-hal = { git = "https://github.com/stm32-rs/stm32f4xx-hal.git", features = ["rt", "stm32f429"] } @@ -31,6 +32,8 @@ stm32f4xx-hal = { git = "https://github.com/stm32-rs/stm32f4xx-hal.git", feature stm32-eth = { version = "0.1.0", features = ["smoltcp-phy", "nucleo-f429zi"] } smoltcp = { version = "0.5.0", default-features = false, features = ["proto-ipv4", "proto-ipv6", "socket-icmp", "socket-udp", "socket-tcp", "log", "verbose"] } +[features] +semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] [profile.release] codegen-units = 1 diff --git a/shell.nix b/shell.nix index fe8081d..2842905 100644 --- a/shell.nix +++ b/shell.nix @@ -24,7 +24,7 @@ stdenv.mkDerivation { # Let openocd output scroll by sleep 1 - echo "Run 'cargo build --release'" + echo "Run 'cargo build --release --features=semihosting'" echo "Then 'gdb target/thumbv7em-none-eabihf/release/adc2tcp'" ''; } diff --git a/src/main.rs b/src/main.rs index d288f26..7d22169 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,15 @@ #![feature(never_type)] #[allow(unused_extern_crates)] +#[cfg(not(feature = "semihosting"))] extern crate panic_abort; +#[cfg(feature = "semihosting")] +extern crate panic_semihosting; +#[macro_use] +extern crate log; + +use core::fmt::Write; use cortex_m::asm::wfi; use cortex_m_rt::entry; use embedded_hal::watchdog::{WatchdogEnable, Watchdog}; @@ -18,9 +25,6 @@ use stm32f4xx_hal::{ }; use smoltcp::time::Instant; -use core::fmt::Write; -use cortex_m_semihosting::hio; - mod adc_input; mod net; mod server; @@ -29,10 +33,30 @@ mod timer; const OUTPUT_INTERVAL: u32 = 1000; +#[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>> = 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"); +} + #[entry] fn main() -> ! { - let mut stdout = hio::hstdout().unwrap(); - writeln!(stdout, "adc2tcp").unwrap(); + init_log(); + info!("adc2tcp"); let mut cp = CorePeripherals::take().unwrap(); cp.SCB.enable_icache(); @@ -57,19 +81,19 @@ fn main() -> ! { let gpioc = dp.GPIOC.split(); let gpiog = dp.GPIOG.split(); - writeln!(stdout, "ADC init").unwrap(); + info!("ADC init"); adc_input::setup(&mut cp.NVIC, dp.ADC1, gpioa.pa3); - writeln!(stdout, "Eth setup").unwrap(); + info!("Eth setup"); stm32_eth::setup_pins( gpioa.pa1, gpioa.pa2, gpioa.pa7, gpiob.pb13, gpioc.pc1, gpioc.pc4, gpioc.pc5, gpiog.pg11, gpiog.pg13 ); - writeln!(stdout, "Timer setup").unwrap(); + info!("Timer setup"); timer::setup(cp.SYST, clocks); - writeln!(stdout, "Net startup").unwrap(); + info!("Net startup"); net::run(&mut cp.NVIC, dp.ETHERNET_MAC, dp.ETHERNET_DMA, |net| { let mut server = Server::new(net);