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