microbit_v2_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 2022.
4
5use core::fmt::Write;
6use core::panic::PanicInfo;
7
8use kernel::debug;
9use kernel::debug::IoWrite;
10use kernel::hil::led;
11use kernel::hil::uart;
12use nrf52833::gpio::Pin;
13use nrf52833::uart::{Uarte, UARTE0_BASE};
14
15/// Writer is used by kernel::debug to panic message to the serial port.
16pub struct Writer {
17    initialized: bool,
18}
19
20impl Writer {
21    /// Indicate that USART has already been initialized.
22    pub fn set_initialized(&mut self) {
23        self.initialized = true;
24    }
25}
26
27/// Global static for debug writer
28pub static mut WRITER: Writer = Writer { initialized: false };
29
30impl Write for Writer {
31    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
32        self.write(s.as_bytes());
33        Ok(())
34    }
35}
36
37impl IoWrite for Writer {
38    fn write(&mut self, buf: &[u8]) -> usize {
39        let uart = Uarte::new(UARTE0_BASE);
40
41        use kernel::hil::uart::Configure;
42
43        if !self.initialized {
44            self.initialized = true;
45            let _ = uart.configure(uart::Parameters {
46                baud_rate: 115200,
47                stop_bits: uart::StopBits::One,
48                parity: uart::Parity::None,
49                hw_flow_control: false,
50                width: uart::Width::Eight,
51            });
52        }
53
54        unsafe {
55            for &c in buf {
56                uart.send_byte(c);
57                while !uart.tx_ready() {}
58            }
59        }
60        buf.len()
61    }
62}
63
64/// Default panic handler for the microbit board.
65///
66/// We just use the standard default provided by the debug module in the kernel.
67#[cfg(not(test))]
68#[panic_handler]
69pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
70    // MicroBit v2 has an LED matrix, use the upper left LED
71    // let mut led = Led (&gpio::PORT[Pin::P0_28], );
72
73    // MicroBit v2 has a microphone LED, use it for panic
74
75    use core::ptr::addr_of_mut;
76    let led_kernel_pin = &nrf52833::gpio::GPIOPin::new(Pin::P0_20);
77    let led = &mut led::LedLow::new(led_kernel_pin);
78    let writer = &mut *addr_of_mut!(WRITER);
79    debug::panic(
80        &mut [led],
81        writer,
82        pi,
83        &cortexm4::support::nop,
84        crate::PANIC_RESOURCES.get(),
85    )
86}