Skip to content

Commit

Permalink
Update logger to use ITM; print panics in debug console
Browse files Browse the repository at this point in the history
  • Loading branch information
ReeceStevens committed Mar 3, 2018
1 parent 02786cc commit ea390c1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
1 change: 0 additions & 1 deletion f4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ bare-metal = "0.1.1"
vcell = "0.1.0"

[dependencies.cortex-m-rt]
features = ["abort-on-panic"]
version = "0.3.12"

[dependencies.stm32f40x]
Expand Down
2 changes: 1 addition & 1 deletion f4/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ macro_rules! gpio {

// Generic GPIO pin for this bus
pub struct $PXx<MODE> {
i: u8,
pub i: u8,
_mode: PhantomData<MODE>
}

Expand Down
28 changes: 28 additions & 0 deletions f4/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
#![no_std]
#![feature(const_fn)]
#![cfg_attr(target_arch="arm", feature(core_intrinsics))]
#![feature(lang_items, libc)]
#![macro_use]
#![feature(compiler_builtins_lib)]
extern crate compiler_builtins;
pub extern crate stm32f40x;
#[cfg(target_arch = "arm")]
extern crate cortex_m;
pub extern crate cortex_m_semihosting;

#[cfg(target_arch = "arm")]
use core::intrinsics;

mod lang_items {
#[lang = "panic_fmt"]
#[no_mangle]
extern "C" fn panic_fmt(msg: ::core::fmt::Arguments, file: &'static str, line: u32, col: u32) -> ! {
unsafe {
use super::intrinsics;
// use cortex_m_semihosting::hio as hio;
use core::fmt::Write;
// let mut stdout = hio::hstdout().unwrap();
use cortex_m::itm::write_fmt;
// stdout.write_fmt(format_args!("{}:{}:{}", file, line, col));
// stdout.write_fmt(msg);
write_fmt(format_args!("{}:{}:{}", file, line, col));
write_fmt(msg);
intrinsics::abort()
}
}
}

#[macro_use]
pub mod logger;

Expand All @@ -14,3 +41,4 @@ pub mod spi;
pub mod timer;
pub mod usart;
pub mod adc;

13 changes: 6 additions & 7 deletions f4/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// use core::fmt;
pub use cortex_m_semihosting::hio as hio;
#![feature(lang_items)]

#[allow(non_camel_case_types)]
pub enum LogLevel {
Expand All @@ -15,8 +14,6 @@ pub enum LogLevel {
macro_rules! logger {
($level:expr, $($arg:tt)*) => {
{
use core::fmt::Write;
let mut stdout = hio::hstdout().unwrap();
let log_color = match $level {
LogLevel::l_info => "\x1b[00;36m",
LogLevel::l_warn => "\x1b[00;33m",
Expand All @@ -29,9 +26,11 @@ macro_rules! logger {
LogLevel::l_error => "ERROR",
LogLevel::l_fatal => "FATAL",
};
stdout.write_fmt(format_args!("{}{}:{}:{}|\t\t", log_color, log_name, file!(), line!())).unwrap();
stdout.write_fmt(format_args!($($arg)*)).unwrap();
stdout.write_fmt(format_args!("\n")).unwrap();
use core::fmt::Write;
use cortex_m::itm::write_fmt;
write_fmt(format_args!("{}{}:{}:{}|\t\t", log_color, log_name, file!(), line!())).unwrap();
write_fmt(format_args!($($arg)*)).unwrap();
write_fmt(format_args!("\n")).unwrap();
}
};
}

0 comments on commit ea390c1

Please sign in to comment.