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#[no_mangle]
48#[panic_handler]
49pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
50    use core::ptr::{addr_of, addr_of_mut};
51    let first_led_pin = &mut earlgrey::gpio::GpioPin::new(
52        earlgrey::gpio::GPIO_BASE,
53        earlgrey::pinmux::PadConfig::Output(
54            earlgrey::registers::top_earlgrey::MuxedPads::Ioa6,
55            earlgrey::registers::top_earlgrey::PinmuxOutsel::GpioGpio7,
56        ),
57        earlgrey::gpio::pins::pin7,
58    );
59    first_led_pin.make_output();
60    let first_led = &mut led::LedLow::new(first_led_pin);
61    let writer = &mut *addr_of_mut!(WRITER);
62
63    #[cfg(feature = "sim_verilator")]
64    debug::panic(
65        &mut [first_led],
66        writer,
67        pi,
68        &|| {},
69        &*addr_of!(PROCESSES),
70        &*addr_of!(CHIP),
71        &*addr_of!(PROCESS_PRINTER),
72    );
73
74    #[cfg(not(feature = "sim_verilator"))]
75    debug::panic(
76        &mut [first_led],
77        writer,
78        pi,
79        &rv32i::support::nop,
80        &*addr_of!(PROCESSES),
81        &*addr_of!(CHIP),
82        &*addr_of!(PROCESS_PRINTER),
83    );
84}
85
86#[cfg(test)]
87#[no_mangle]
88#[panic_handler]
89pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
90    let writer = &mut WRITER;
91
92    #[cfg(feature = "sim_verilator")]
93    debug::panic_print(writer, pi, &|| {}, &PROCESSES, &CHIP, &PROCESS_PRINTER);
94    #[cfg(not(feature = "sim_verilator"))]
95    debug::panic_print(
96        writer,
97        pi,
98        &rv32i::support::nop,
99        &PROCESSES,
100        &CHIP,
101        &PROCESS_PRINTER,
102    );
103
104    let _ = writeln!(writer, "{}", pi);
105    // Exit QEMU with a return code of 1
106    crate::tests::semihost_command_exit_failure();
107}