earlgrey_cw310/
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 earlgrey::chip_config::EarlGreyConfig;
9use kernel::debug;
10use kernel::debug::IoWrite;
11
12use crate::CHIP;
13use crate::PROCESSES;
14use crate::PROCESS_PRINTER;
15
16struct Writer {}
17
18static mut WRITER: Writer = Writer {};
19
20impl Write for Writer {
21    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
22        self.write(s.as_bytes());
23        Ok(())
24    }
25}
26
27impl IoWrite for Writer {
28    fn write(&mut self, buf: &[u8]) -> usize {
29        // This creates a second instance of the UART peripheral, and should only be used
30        // during panic.
31        earlgrey::uart::Uart::new(
32            earlgrey::uart::UART0_BASE,
33            crate::ChipConfig::PERIPHERAL_FREQ,
34        )
35        .transmit_sync(buf);
36        buf.len()
37    }
38}
39
40#[cfg(not(test))]
41use kernel::hil::gpio::Configure;
42#[cfg(not(test))]
43use kernel::hil::led;
44
45/// Panic handler.
46#[cfg(not(test))]
47#[panic_handler]
48pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
49    use core::ptr::{addr_of, addr_of_mut};
50    let first_led_pin = &mut earlgrey::gpio::GpioPin::new(
51        earlgrey::gpio::GPIO_BASE,
52        earlgrey::pinmux::PadConfig::Output(
53            earlgrey::registers::top_earlgrey::MuxedPads::Ioa6,
54            earlgrey::registers::top_earlgrey::PinmuxOutsel::GpioGpio7,
55        ),
56        earlgrey::gpio::pins::pin7,
57    );
58    first_led_pin.make_output();
59    let first_led = &mut led::LedLow::new(first_led_pin);
60    let writer = &mut *addr_of_mut!(WRITER);
61
62    #[cfg(feature = "sim_verilator")]
63    debug::panic(
64        &mut [first_led],
65        writer,
66        pi,
67        &|| {},
68        &*addr_of!(PROCESSES),
69        &*addr_of!(CHIP),
70        &*addr_of!(PROCESS_PRINTER),
71    );
72
73    #[cfg(not(feature = "sim_verilator"))]
74    debug::panic(
75        &mut [first_led],
76        writer,
77        pi,
78        &rv32i::support::nop,
79        &*addr_of!(PROCESSES),
80        &*addr_of!(CHIP),
81        &*addr_of!(PROCESS_PRINTER),
82    );
83}
84
85#[cfg(test)]
86#[panic_handler]
87pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
88    let writer = &mut WRITER;
89
90    #[cfg(feature = "sim_verilator")]
91    debug::panic_print(writer, pi, &|| {}, &PROCESSES, &CHIP, &PROCESS_PRINTER);
92    #[cfg(not(feature = "sim_verilator"))]
93    debug::panic_print(
94        writer,
95        pi,
96        &rv32i::support::nop,
97        &PROCESSES,
98        &CHIP,
99        &PROCESS_PRINTER,
100    );
101
102    let _ = writeln!(writer, "{}", pi);
103    // Exit QEMU with a return code of 1
104    crate::tests::semihost_command_exit_failure();
105}