teensy40/
io.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

use core::fmt::{self, Write};
use core::ptr::addr_of;

use kernel::debug::{self, IoWrite};
use kernel::hil::{
    led,
    uart::{self, Configure},
};

use crate::imxrt1060::gpio;
use crate::imxrt1060::lpuart;

struct Writer<'a> {
    output: &'a mut lpuart::Lpuart<'a>,
}

const BAUD_RATE: u32 = 115_200;

impl<'a> Writer<'a> {
    pub unsafe fn new(output: &'a mut lpuart::Lpuart<'a>) -> Self {
        let _ = output.configure(uart::Parameters {
            baud_rate: BAUD_RATE,
            stop_bits: uart::StopBits::One,
            parity: uart::Parity::None,
            hw_flow_control: false,
            width: uart::Width::Eight,
        });

        Writer { output }
    }
}

impl IoWrite for Writer<'_> {
    fn write(&mut self, bytes: &[u8]) -> usize {
        for byte in bytes {
            self.output.send_byte(*byte);
        }
        bytes.len()
    }
}

impl Write for Writer<'_> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.write(s.as_bytes());
        Ok(())
    }
}

#[no_mangle]
#[panic_handler]
unsafe fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
    let ccm = crate::imxrt1060::ccm::Ccm::new();
    let pin = crate::imxrt1060::gpio::Pin::from_pin_id(gpio::PinId::B0_03);
    let led = &mut led::LedHigh::new(&pin);
    let mut lpuart2 = lpuart::Lpuart::new_lpuart2(&ccm);
    let mut writer = Writer::new(&mut lpuart2);
    debug::panic(
        &mut [led],
        &mut writer,
        panic_info,
        &cortexm7::support::nop,
        &*addr_of!(crate::PROCESSES),
        &*addr_of!(crate::CHIP),
        &*addr_of!(crate::PROCESS_PRINTER),
    )
}