hifive1/
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::ptr::addr_of_mut;
8use core::str;
9use kernel::debug;
10use kernel::debug::IoWrite;
11use kernel::hil::gpio;
12use kernel::hil::led;
13
14struct Writer {}
15
16static mut WRITER: Writer = Writer {};
17
18impl Write for Writer {
19    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
20        self.write(s.as_bytes());
21        Ok(())
22    }
23}
24
25impl IoWrite for Writer {
26    fn write(&mut self, buf: &[u8]) -> usize {
27        let uart = sifive::uart::Uart::new(e310_g002::uart::UART0_BASE, 16_000_000);
28        uart.transmit_sync(buf);
29        buf.len()
30    }
31}
32
33/// Panic handler.
34#[cfg(not(test))]
35#[panic_handler]
36pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
37    // turn off the non panic leds, just in case
38    let led_green = sifive::gpio::GpioPin::new(
39        e310_g002::gpio::GPIO0_BASE,
40        sifive::gpio::pins::pin19,
41        sifive::gpio::pins::pin19::SET,
42        sifive::gpio::pins::pin19::CLEAR,
43    );
44    gpio::Configure::make_output(&led_green);
45    gpio::Output::set(&led_green);
46
47    let led_blue = sifive::gpio::GpioPin::new(
48        e310_g002::gpio::GPIO0_BASE,
49        sifive::gpio::pins::pin21,
50        sifive::gpio::pins::pin21::SET,
51        sifive::gpio::pins::pin21::CLEAR,
52    );
53    gpio::Configure::make_output(&led_blue);
54    gpio::Output::set(&led_blue);
55
56    let led_red_pin = sifive::gpio::GpioPin::new(
57        e310_g002::gpio::GPIO0_BASE,
58        sifive::gpio::pins::pin22,
59        sifive::gpio::pins::pin22::SET,
60        sifive::gpio::pins::pin22::CLEAR,
61    );
62    let led_red = &mut led::LedLow::new(&led_red_pin);
63    let writer = &mut *addr_of_mut!(WRITER);
64
65    debug::panic(
66        &mut [led_red],
67        writer,
68        pi,
69        &rv32i::support::nop,
70        crate::PANIC_RESOURCES.get(),
71    )
72}