nano_rp2040_connect/
io.rsuse core::fmt::Write;
use core::panic::PanicInfo;
use kernel::debug::{self, IoWrite};
use kernel::hil::led::LedHigh;
use kernel::hil::uart::{Configure, Parameters, Parity, StopBits, Width};
use kernel::utilities::cells::OptionalCell;
use rp2040::gpio::{GpioFunction, RPGpio, RPGpioPin};
use rp2040::uart::Uart;
use crate::CHIP;
use crate::PROCESSES;
use crate::PROCESS_PRINTER;
pub struct Writer {
uart: OptionalCell<&'static Uart<'static>>,
}
impl Writer {
pub fn set_uart(&self, uart: &'static Uart) {
self.uart.set(uart);
}
fn write_to_uart(&self, uart: &Uart, buf: &[u8]) {
for &c in buf {
uart.send_byte(c);
}
}
}
pub static mut WRITER: Writer = Writer {
uart: OptionalCell::empty(),
};
impl Write for Writer {
fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
self.write(s.as_bytes());
Ok(())
}
}
impl IoWrite for Writer {
fn write(&mut self, buf: &[u8]) -> usize {
self.uart.map_or_else(
|| {
let uart0 = &Uart::new_uart0();
if !uart0.is_configured() {
let parameters = Parameters {
baud_rate: 115200,
width: Width::Eight,
parity: Parity::None,
stop_bits: StopBits::One,
hw_flow_control: false,
};
let _result = uart0.configure(parameters);
let gpio_tx = RPGpioPin::new(RPGpio::GPIO0);
let gpio_rx = RPGpioPin::new(RPGpio::GPIO1);
gpio_rx.set_function(GpioFunction::UART);
gpio_tx.set_function(GpioFunction::UART);
}
self.write_to_uart(uart0, buf);
},
|uart| {
self.write_to_uart(uart, buf);
},
);
buf.len()
}
}
#[cfg(not(test))]
#[no_mangle]
#[panic_handler]
pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
use core::ptr::{addr_of, addr_of_mut};
let led_kernel_pin = &RPGpioPin::new(RPGpio::GPIO25);
let led = &mut LedHigh::new(led_kernel_pin);
let writer = &mut *addr_of_mut!(WRITER);
debug::panic(
&mut [led],
writer,
pi,
&cortexm0p::support::nop,
&*addr_of!(PROCESSES),
&*addr_of!(CHIP),
&*addr_of!(PROCESS_PRINTER),
)
}