1use core::fmt::Write;
6use core::panic::PanicInfo;
7use core::ptr::addr_of;
8use core::ptr::addr_of_mut;
9
10use kernel::debug;
11use kernel::debug::IoWrite;
12use kernel::hil::led;
13use kernel::hil::uart;
14use kernel::hil::uart::Configure;
15
16use stm32f303xc::gpio::PinId;
17
18use crate::CHIP;
19use crate::PROCESSES;
20use crate::PROCESS_PRINTER;
21
22pub struct Writer {
24 initialized: bool,
25}
26
27pub static mut WRITER: Writer = Writer { initialized: false };
29
30impl Writer {
31 pub fn set_initialized(&mut self) {
34 self.initialized = true;
35 }
36}
37
38impl Write for Writer {
39 fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
40 self.write(s.as_bytes());
41 Ok(())
42 }
43}
44
45impl IoWrite for Writer {
46 fn write(&mut self, buf: &[u8]) -> usize {
47 let rcc = stm32f303xc::rcc::Rcc::new();
48 let uart = stm32f303xc::usart::Usart::new_usart1(&rcc);
49
50 if !self.initialized {
51 self.initialized = true;
52
53 let _ = uart.configure(uart::Parameters {
54 baud_rate: 115200,
55 stop_bits: uart::StopBits::One,
56 parity: uart::Parity::None,
57 hw_flow_control: false,
58 width: uart::Width::Eight,
59 });
60 }
61
62 for &c in buf {
63 uart.send_byte(c);
64 }
65 buf.len()
66 }
67}
68
69#[panic_handler]
71pub unsafe fn panic_fmt(info: &PanicInfo) -> ! {
72 let rcc = stm32f303xc::rcc::Rcc::new();
75 let syscfg = stm32f303xc::syscfg::Syscfg::new(&rcc);
76 let exti = stm32f303xc::exti::Exti::new(&syscfg);
77 let pin = stm32f303xc::gpio::Pin::new(PinId::PE09, &exti);
78 let gpio_ports = stm32f303xc::gpio::GpioPorts::new(&rcc, &exti);
79 pin.set_ports_ref(&gpio_ports);
80 let led = &mut led::LedHigh::new(&pin);
81 let writer = &mut *addr_of_mut!(WRITER);
82
83 debug::panic(
84 &mut [led],
85 writer,
86 info,
87 &cortexm4::support::nop,
88 &*addr_of!(PROCESSES),
89 &*addr_of!(CHIP),
90 &*addr_of!(PROCESS_PRINTER),
91 )
92}