particle_boron/
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;
11use kernel::hil::uart::Configure;
12use nrf52840::gpio::Pin;
13use nrf52840::uart::{Uarte, UARTE0_BASE};
14
15// Expand here with more writing methods as required (rtt/cdc etc...)
16enum Writer {
17    WriterUart(/* initialized */ bool),
18}
19
20static mut WRITER: Writer = Writer::WriterUart(false);
21
22impl Write for Writer {
23    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
24        self.write(s.as_bytes());
25        Ok(())
26    }
27}
28
29impl IoWrite for Writer {
30    fn write(&mut self, buf: &[u8]) -> usize {
31        match self {
32            Writer::WriterUart(ref mut initialized) => {
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 !*initialized {
38                    *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 { uart.send_byte(c) }
49                    while !uart.tx_ready() {}
50                }
51            }
52        }
53        buf.len()
54    }
55}
56
57const LED2_R_PIN: Pin = Pin::P0_13;
58
59#[cfg(not(test))]
60#[panic_handler]
61/// Panic handler
62pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
63    // The nRF52840DK LEDs (see back of board)
64
65    use core::ptr::addr_of_mut;
66    let led_kernel_pin = &nrf52840::gpio::GPIOPin::new(LED2_R_PIN);
67    let led = &mut led::LedLow::new(led_kernel_pin);
68    let writer = &mut *addr_of_mut!(WRITER);
69    debug::panic(
70        &mut [led],
71        writer,
72        pi,
73        &cortexm4::support::nop,
74        crate::PANIC_RESOURCES.get(),
75    )
76}