Skip to content

Commit 2ed27a2

Browse files
Add log on panic feature.
1 parent 8a51576 commit 2ed27a2

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

stm32f0_hal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ cortex-m = "0.3.1"
2424
cortex-m-semihosting = "0.2.0"
2525

2626
[dependencies.cortex-m-rt]
27-
features = ["abort-on-panic"]
2827
version = "=0.3.7"
2928

3029
[dependencies.stm32f0x2]
@@ -34,6 +33,7 @@ path = "../stm32f0x2"
3433
[features]
3534
default = []
3635
use_alloc = ["alloc-cortex-m"]
36+
serial_panic = []
3737

3838
[dependencies.alloc-cortex-m]
3939
git = "https://github.com/pollen-robotics/alloc-cortex-m"

stm32f0_hal/examples/panic.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![no_std]
2+
3+
extern crate cortex_m;
4+
extern crate stm32f0_hal as hal;
5+
6+
use hal::{rcc, uart};
7+
8+
fn main() {
9+
rcc::init();
10+
11+
unsafe {
12+
hal::panic::log_on_serial(uart::Uarts::Uart4);
13+
}
14+
panic!("This will be sent on TX before abort!");
15+
}

stm32f0_hal/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
//! * RCC
99
1010
#![no_std]
11+
#![feature(lang_items)]
12+
#![feature(core_intrinsics)]
1113
#![cfg_attr(feature = "use_alloc", feature(global_allocator))]
1214

1315
extern crate cortex_m;
1416
#[macro_use(exception)]
1517
extern crate cortex_m_rt;
1618
extern crate stm32f0x2;
1719

18-
pub mod gpio;
1920
pub mod adc;
21+
pub mod gpio;
22+
pub mod panic;
2023
pub mod pwm;
2124
pub mod rcc;
2225
pub mod uart;

stm32f0_hal/src/panic.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#[cfg(feature = "serial_panic")]
2+
use core::fmt::Write;
3+
4+
#[lang = "panic_fmt"]
5+
#[allow(unused_variables)]
6+
#[no_mangle]
7+
pub unsafe extern "C" fn panic_fmt(
8+
msg: ::core::fmt::Arguments,
9+
file: &'static str,
10+
line: u32,
11+
column: u32,
12+
) -> ! {
13+
#[cfg(feature = "serial_panic")]
14+
{
15+
if let Some(ref mut log) = serial_panic::LOG {
16+
writeln!(
17+
log,
18+
"*** PANIC *** 'main' panicked at '{}', {}:{}:{}",
19+
msg, file, line, column
20+
).unwrap();
21+
}
22+
}
23+
24+
::core::intrinsics::abort()
25+
}
26+
27+
#[cfg(feature = "serial_panic")]
28+
mod serial_panic {
29+
use core::fmt::{Error, Write};
30+
31+
use uart;
32+
33+
/// Setup a serial TX where the panic will be sent
34+
pub unsafe fn log_on_serial(uart: uart::Uarts) {
35+
let uart = uart::Uart::setup(
36+
uart,
37+
57_600,
38+
uart::NBits::_8bits,
39+
uart::StopBits::_1b,
40+
uart::Parity::None,
41+
);
42+
LOG = Some(SerialLog(uart));
43+
}
44+
45+
pub(crate) static mut LOG: Option<SerialLog> = None;
46+
47+
pub(crate) struct SerialLog(uart::Uart);
48+
impl Write for SerialLog {
49+
fn write_str(&mut self, s: &str) -> Result<(), Error> {
50+
for &b in s.as_bytes() {
51+
while !self.0.transmit_complete() {}
52+
self.0.send(b);
53+
}
54+
55+
Ok(())
56+
}
57+
}
58+
}
59+
#[cfg(feature = "serial_panic")]
60+
pub use self::serial_panic::log_on_serial;

0 commit comments

Comments
 (0)