lora_things_plus/
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;
9
10use crate::CHIP;
11use crate::PROCESSES;
12use crate::PROCESS_PRINTER;
13use kernel::debug;
14use kernel::debug::IoWrite;
15use kernel::hil::led;
16
17/// Writer is used by kernel::debug to panic message to the serial port.
18pub struct Writer {}
19
20/// Global static for debug writer
21pub static 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        let uart = apollo3::uart::Uart::new_uart_0(); // Aliases memory for uart0. Okay bc we are panicking.
33        uart.transmit_sync(buf);
34        buf.len()
35    }
36}
37
38/// Panic handler.
39#[no_mangle]
40#[panic_handler]
41pub unsafe fn panic_fmt(info: &PanicInfo) -> ! {
42    // just create a new pin reference here instead of using global
43    let led_pin = &mut apollo3::gpio::GpioPin::new(
44        kernel::utilities::StaticRef::new(
45            apollo3::gpio::GPIO_BASE_RAW as *const apollo3::gpio::GpioRegisters,
46        ),
47        apollo3::gpio::Pin::Pin26,
48    );
49    let led = &mut led::LedLow::new(led_pin);
50    let writer = &mut *addr_of_mut!(WRITER);
51
52    debug::panic(
53        &mut [led],
54        writer,
55        info,
56        &cortexm4::support::nop,
57        &*addr_of!(PROCESSES),
58        &*addr_of!(CHIP),
59        &*addr_of!(PROCESS_PRINTER),
60    )
61}