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