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