redboard_redv/
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;
10use kernel::hil::gpio;
11use kernel::hil::led;
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        let uart = sifive::uart::Uart::new(e310_g002::uart::UART0_BASE, 16_000_000);
27        uart.transmit_sync(buf);
28        buf.len()
29    }
30}
31
32/// Panic handler.
33#[cfg(not(test))]
34#[panic_handler]
35pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
36    // turn off the non panic leds, just in case
37
38    use core::ptr::addr_of_mut;
39    let led_green = sifive::gpio::GpioPin::new(
40        e310_g002::gpio::GPIO0_BASE,
41        sifive::gpio::pins::pin19,
42        sifive::gpio::pins::pin19::SET,
43        sifive::gpio::pins::pin19::CLEAR,
44    );
45    gpio::Configure::make_output(&led_green);
46    gpio::Output::set(&led_green);
47
48    let led_blue = sifive::gpio::GpioPin::new(
49        e310_g002::gpio::GPIO0_BASE,
50        sifive::gpio::pins::pin21,
51        sifive::gpio::pins::pin21::SET,
52        sifive::gpio::pins::pin21::CLEAR,
53    );
54    gpio::Configure::make_output(&led_blue);
55    gpio::Output::set(&led_blue);
56
57    let led_red_pin = sifive::gpio::GpioPin::new(
58        e310_g002::gpio::GPIO0_BASE,
59        sifive::gpio::pins::pin22,
60        sifive::gpio::pins::pin22::SET,
61        sifive::gpio::pins::pin22::CLEAR,
62    );
63    let led_red = &mut led::LedLow::new(&led_red_pin);
64    let writer = &mut *addr_of_mut!(WRITER);
65
66    debug::panic(
67        &mut [led_red],
68        writer,
69        pi,
70        &rv32i::support::nop,
71        crate::PANIC_RESOURCES.get(),
72    )
73}