1use core::fmt::Write;
6use core::panic::PanicInfo;
7use kernel::debug;
8use kernel::debug::IoWrite;
9use kernel::hil::led;
10use kernel::hil::uart;
11use kernel::hil::uart::Configure;
12use nrf52840::gpio::Pin;
13use nrf52840::uart::{Uarte, UARTE0_BASE};
14
15enum Writer {
17 WriterUart(bool),
18}
19
20static mut WRITER: Writer = Writer::WriterUart(false);
21
22impl Write for Writer {
23 fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
24 self.write(s.as_bytes());
25 Ok(())
26 }
27}
28
29impl IoWrite for Writer {
30 fn write(&mut self, buf: &[u8]) -> usize {
31 match self {
32 Writer::WriterUart(ref mut initialized) => {
33 let uart = Uarte::new(UARTE0_BASE);
37 if !*initialized {
38 *initialized = true;
39 let _ = uart.configure(uart::Parameters {
40 baud_rate: 115200,
41 stop_bits: uart::StopBits::One,
42 parity: uart::Parity::None,
43 hw_flow_control: false,
44 width: uart::Width::Eight,
45 });
46 }
47 for &c in buf {
48 unsafe { uart.send_byte(c) }
49 while !uart.tx_ready() {}
50 }
51 }
52 }
53 buf.len()
54 }
55}
56
57const LED2_R_PIN: Pin = Pin::P0_13;
58
59#[cfg(not(test))]
60#[panic_handler]
61pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
63 use core::ptr::addr_of_mut;
66 let led_kernel_pin = &nrf52840::gpio::GPIOPin::new(LED2_R_PIN);
67 let led = &mut led::LedLow::new(led_kernel_pin);
68 let writer = &mut *addr_of_mut!(WRITER);
69 debug::panic(
70 &mut [led],
71 writer,
72 pi,
73 &cortexm4::support::nop,
74 crate::PANIC_RESOURCES.get(),
75 )
76}