litex_arty/
io.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5use core::fmt::Write;
6use core::panic::PanicInfo;
7use core::str;
8use kernel::debug;
9use kernel::debug::IoWrite;
10
11use crate::{PANIC_REFERENCES, PROCESSES};
12
13struct Writer {}
14
15static mut WRITER: Writer = Writer {};
16
17impl Write for Writer {
18    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
19        self.write(s.as_bytes());
20        Ok(())
21    }
22}
23
24impl IoWrite for Writer {
25    fn write(&mut self, buf: &[u8]) -> usize {
26        unsafe {
27            PANIC_REFERENCES.uart.unwrap().transmit_sync(buf);
28        }
29        buf.len()
30    }
31}
32
33/// Panic handler.
34#[cfg(not(test))]
35#[no_mangle]
36#[panic_handler]
37pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
38    use core::ptr::{addr_of, addr_of_mut};
39
40    let panic_led = PANIC_REFERENCES
41        .led_controller
42        .and_then(|ctrl| ctrl.panic_led(0));
43
44    let writer = &mut *addr_of_mut!(WRITER);
45
46    debug::panic(
47        &mut [&mut panic_led.unwrap()],
48        writer,
49        pi,
50        &rv32i::support::nop,
51        &*addr_of!(PROCESSES),
52        &*addr_of!(PANIC_REFERENCES.chip),
53        &*addr_of!(PANIC_REFERENCES.process_printer),
54    )
55}