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#[panic_handler]
36pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
37    use core::ptr::{addr_of, addr_of_mut};
38
39    let panic_led = PANIC_REFERENCES
40        .led_controller
41        .and_then(|ctrl| ctrl.panic_led(0));
42
43    let writer = &mut *addr_of_mut!(WRITER);
44
45    debug::panic(
46        &mut [&mut panic_led.unwrap()],
47        writer,
48        pi,
49        &rv32i::support::nop,
50        &*addr_of!(PROCESSES),
51        &*addr_of!(PANIC_REFERENCES.chip),
52        &*addr_of!(PANIC_REFERENCES.process_printer),
53    )
54}