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