imxrt1050_evkb/
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 core::ptr::addr_of_mut;
8
9use kernel::debug;
10use kernel::debug::IoWrite;
11use kernel::hil::led;
12use kernel::hil::uart;
13use kernel::hil::uart::Configure;
14
15use crate::imxrt1050;
16use imxrt1050::gpio::PinId;
17
18/// Writer is used by kernel::debug to panic message to the serial port.
19pub struct Writer {
20    initialized: bool,
21}
22
23/// Global static for debug writer
24pub static mut WRITER: Writer = Writer { initialized: false };
25
26impl Writer {
27    /// Indicate that LPUART has already been initialized.
28    pub fn set_initialized(&mut self) {
29        self.initialized = true;
30    }
31}
32
33impl Write for Writer {
34    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
35        self.write(s.as_bytes());
36        Ok(())
37    }
38}
39
40impl IoWrite for Writer {
41    fn write(&mut self, buf: &[u8]) -> usize {
42        let ccm = crate::imxrt1050::ccm::Ccm::new();
43        let uart = imxrt1050::lpuart::Lpuart::new_lpuart1(&ccm);
44
45        if !self.initialized {
46            self.initialized = true;
47
48            let _ = uart.configure(uart::Parameters {
49                baud_rate: 115200,
50                stop_bits: uart::StopBits::One,
51                parity: uart::Parity::None,
52                hw_flow_control: false,
53                width: uart::Width::Eight,
54            });
55        }
56
57        for &c in buf {
58            uart.send_byte(c);
59        }
60        buf.len()
61    }
62}
63
64/// Panic handler.
65#[panic_handler]
66pub unsafe fn panic_fmt(info: &PanicInfo) -> ! {
67    // User Led is connected to AdB0_09
68    let pin = imxrt1050::gpio::Pin::from_pin_id(PinId::AdB0_09);
69    let led = &mut led::LedLow::new(&pin);
70    let writer = &mut *addr_of_mut!(WRITER);
71
72    debug::panic(
73        &mut [led],
74        writer,
75        info,
76        &cortexm7::support::nop,
77        crate::PANIC_RESOURCES.get(),
78    )
79}