nano_rp2040_connect/
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;
7
8use kernel::debug::{self, IoWrite};
9use kernel::hil::led::LedHigh;
10use kernel::hil::uart::{Configure, Parameters, Parity, StopBits, Width};
11use kernel::utilities::cells::OptionalCell;
12
13use rp2040::gpio::{GpioFunction, RPGpio, RPGpioPin};
14use rp2040::uart::Uart;
15
16use crate::CHIP;
17use crate::PROCESSES;
18use crate::PROCESS_PRINTER;
19
20/// Writer is used by kernel::debug to panic message to the serial port.
21pub struct Writer {
22    uart: OptionalCell<&'static Uart<'static>>,
23}
24
25impl Writer {
26    pub fn set_uart(&self, uart: &'static Uart) {
27        self.uart.set(uart);
28    }
29
30    fn write_to_uart(&self, uart: &Uart, buf: &[u8]) {
31        for &c in buf {
32            uart.send_byte(c);
33        }
34    }
35}
36
37/// Global static for debug writer
38pub static mut WRITER: Writer = Writer {
39    uart: OptionalCell::empty(),
40};
41
42impl Write for Writer {
43    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
44        self.write(s.as_bytes());
45        Ok(())
46    }
47}
48
49impl IoWrite for Writer {
50    fn write(&mut self, buf: &[u8]) -> usize {
51        self.uart.map_or_else(
52            || {
53                // If no UART is configured for panic print, use UART0
54                let uart0 = &Uart::new_uart0();
55
56                if !uart0.is_configured() {
57                    let parameters = Parameters {
58                        baud_rate: 115200,
59                        width: Width::Eight,
60                        parity: Parity::None,
61                        stop_bits: StopBits::One,
62                        hw_flow_control: false,
63                    };
64                    //configure parameters of uart for sending bytes
65                    let _result = uart0.configure(parameters);
66                    //set RX and TX pins in UART mode
67                    let gpio_tx = RPGpioPin::new(RPGpio::GPIO0);
68                    let gpio_rx = RPGpioPin::new(RPGpio::GPIO1);
69                    gpio_rx.set_function(GpioFunction::UART);
70                    gpio_tx.set_function(GpioFunction::UART);
71                }
72
73                self.write_to_uart(uart0, buf);
74            },
75            |uart| {
76                self.write_to_uart(uart, buf);
77            },
78        );
79        buf.len()
80    }
81}
82
83/// Default panic handler for the Arduino Nano RP2040 Connect board.
84///
85/// We just use the standard default provided by the debug module in the kernel.
86#[cfg(not(test))]
87#[no_mangle]
88#[panic_handler]
89pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
90    // LED is connected to GPIO 25
91
92    use core::ptr::{addr_of, addr_of_mut};
93    let led_kernel_pin = &RPGpioPin::new(RPGpio::GPIO25);
94    let led = &mut LedHigh::new(led_kernel_pin);
95    let writer = &mut *addr_of_mut!(WRITER);
96
97    debug::panic(
98        &mut [led],
99        writer,
100        pi,
101        &cortexm0p::support::nop,
102        &*addr_of!(PROCESSES),
103        &*addr_of!(CHIP),
104        &*addr_of!(PROCESS_PRINTER),
105    )
106}