aboutsummaryrefslogtreecommitdiff
path: root/examples/teensy4_blinky/examples/common/mod.rs
blob: 9851feab1cda6edf4e696585743cd2d227c0e173 (plain) (blame)
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
macro_rules! uart_panic_handler {
    ($uart: ident, $tx_pin: ident, $rx_pin: ident, $baud: expr) => {
        #[panic_handler]
        fn panic(info: &::core::panic::PanicInfo) -> ! {
            use ::core::fmt::Write as _;
            use ::embedded_hal::serial::Write as _;

            let ::teensy4_bsp::board::Resources {
                $uart: uart, pins, ..
            } = ::teensy4_bsp::board::t40(unsafe { ::teensy4_bsp::ral::Instances::instances() });

            let uart = ::teensy4_bsp::board::lpuart(uart, pins.$tx_pin, pins.$rx_pin, $baud);

            struct UartWriter<P, const N: u8> {
                uart: ::teensy4_bsp::hal::lpuart::Lpuart<P, N>,
            }
            impl<P, const N: u8> ::core::fmt::Write for UartWriter<P, N> {
                fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
                    for &b in s.as_bytes() {
                        if b == b'\n' {
                            let _ = ::nb::block!(self.uart.write(b'\r'));
                        }
                        let _ = ::nb::block!(self.uart.write(b));
                    }
                    Ok(())
                }
            }

            let mut uart = UartWriter { uart };

            ::core::writeln!(uart).ok();
            ::core::writeln!(uart, "{}", info).ok();
            ::core::writeln!(uart).ok();

            let _ = ::nb::block!(uart.uart.flush());

            ::teensy4_panic::sos()
        }
    };
}

pub(crate) use uart_panic_handler;