nrf52dk/
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 kernel::debug;
8use kernel::debug::IoWrite;
9use kernel::hil::led;
10use kernel::hil::uart::{self, Configure};
11use nrf52832::gpio::Pin;
12use nrf52832::uart::{Uarte, UARTE0_BASE};
13
14struct Writer {
15    initialized: bool,
16}
17
18static mut WRITER: Writer = Writer { initialized: false };
19
20impl Write for Writer {
21    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
22        self.write(s.as_bytes());
23        Ok(())
24    }
25}
26
27impl IoWrite for Writer {
28    fn write(&mut self, buf: &[u8]) -> usize {
29        // Here, we create a second instance of the Uarte struct.
30        // This is okay because we only call this during a panic, and
31        // we will never actually process the interrupts
32        let uart = Uarte::new(UARTE0_BASE);
33        if !self.initialized {
34            self.initialized = true;
35            let _ = uart.configure(uart::Parameters {
36                baud_rate: 115200,
37                stop_bits: uart::StopBits::One,
38                parity: uart::Parity::None,
39                hw_flow_control: false,
40                width: uart::Width::Eight,
41            });
42        }
43        for &c in buf {
44            unsafe {
45                uart.send_byte(c);
46            }
47            while !uart.tx_ready() {}
48        }
49        buf.len()
50    }
51}
52
53#[cfg(not(test))]
54#[panic_handler]
55/// Panic handler
56pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
57    // The nRF52 DK LEDs (see back of board)
58
59    use core::ptr::addr_of_mut;
60    let led_kernel_pin = &nrf52832::gpio::GPIOPin::new(Pin::P0_17);
61    let led = &mut led::LedLow::new(led_kernel_pin);
62    let writer = &mut *addr_of_mut!(WRITER);
63    debug::panic(
64        &mut [led],
65        writer,
66        pi,
67        &cortexm4::support::nop,
68        crate::PANIC_RESOURCES.get(),
69    )
70}