nrf52840dk_test_dynamic_app_load/
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 2024.
4
5use core::fmt::Write;
6use kernel::debug::IoWrite;
7use kernel::hil::uart;
8use kernel::hil::uart::Configure;
9
10use nrf52840::uart::{Uarte, UARTE0_BASE};
11
12struct Writer {
13    initialized: bool,
14}
15
16impl Writer {
17    fn new() -> Self {
18        Self { initialized: false }
19    }
20}
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        // Here, we create a second instance of the Uarte struct.
32        // This is okay because we only call this during a panic, and
33        // we will never actually process the interrupts
34        let uart = Uarte::new(UARTE0_BASE);
35        if !self.initialized {
36            self.initialized = true;
37            let _ = uart.configure(uart::Parameters {
38                baud_rate: 115200,
39                stop_bits: uart::StopBits::One,
40                parity: uart::Parity::None,
41                hw_flow_control: false,
42                width: uart::Width::Eight,
43            });
44        }
45        for &c in buf {
46            unsafe {
47                uart.send_byte(c);
48            }
49            while !uart.tx_ready() {}
50        }
51
52        buf.len()
53    }
54}
55
56#[cfg(not(test))]
57#[panic_handler]
58/// Panic handler
59pub unsafe fn panic_fmt(pi: &core::panic::PanicInfo) -> ! {
60    use kernel::debug;
61    use kernel::hil::led;
62    use nrf52840::gpio::Pin;
63
64    // The nRF52840DK LEDs (see back of board)
65    let led_kernel_pin = &nrf52840::gpio::GPIOPin::new(Pin::P0_13);
66    let led = &mut led::LedLow::new(led_kernel_pin);
67    let mut writer = Writer::new();
68    debug::panic(
69        &mut [led],
70        &mut writer,
71        pi,
72        &cortexm4::support::nop,
73        crate::PANIC_RESOURCES.get(),
74    )
75}