wm1110dev/
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 2023.
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 nrf52840::gpio::Pin;
13use nrf52840::uart::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 = nrf52840::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    // Red Led
71
72    use core::ptr::addr_of_mut;
73    let led_red_pin = &nrf52840::gpio::GPIOPin::new(Pin::P0_14);
74    let led = &mut led::LedHigh::new(led_red_pin);
75    let writer = &mut *addr_of_mut!(WRITER);
76    debug::panic(
77        &mut [led],
78        writer,
79        pi,
80        &cortexm4::support::nop,
81        crate::PANIC_RESOURCES.get(),
82    )
83}