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