components/
debug_writer.rsuse capsules_core::virtualizers::virtual_uart::{MuxUart, UartDevice};
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::collections::ring_buffer::RingBuffer;
use kernel::component::Component;
use kernel::hil;
use kernel::hil::uart;
pub const DEFAULT_DEBUG_BUFFER_KBYTE: usize = 2;
const DEBUG_BUFFER_SPLIT: usize = 64;
#[macro_export]
macro_rules! debug_writer_component_static {
($BUF_SIZE_KB:expr) => {{
let uart = kernel::static_buf!(capsules_core::virtualizers::virtual_uart::UartDevice);
let ring = kernel::static_buf!(kernel::collections::ring_buffer::RingBuffer<'static, u8>);
let buffer = kernel::static_buf!([u8; 1024 * $BUF_SIZE_KB]);
let debug = kernel::static_buf!(kernel::debug::DebugWriter);
let debug_wrapper = kernel::static_buf!(kernel::debug::DebugWriterWrapper);
(uart, ring, buffer, debug, debug_wrapper)
};};
() => {{
$crate::debug_writer_component_static!($crate::debug_writer::DEFAULT_DEBUG_BUFFER_KBYTE)
};};
}
#[macro_export]
macro_rules! debug_writer_no_mux_component_static {
($BUF_SIZE_KB:expr) => {{
let ring = kernel::static_buf!(kernel::collections::ring_buffer::RingBuffer<'static, u8>);
let buffer = kernel::static_buf!([u8; 1024 * $BUF_SIZE_KB]);
let debug = kernel::static_buf!(kernel::debug::DebugWriter);
let debug_wrapper = kernel::static_buf!(kernel::debug::DebugWriterWrapper);
(ring, buffer, debug, debug_wrapper)
};};
() => {{
use $crate::debug_writer::DEFAULT_DEBUG_BUFFER_KBYTE;
$crate::debug_writer_no_mux_component_static!(DEFAULT_DEBUG_BUFFER_KBYTE);
};};
}
pub struct DebugWriterComponent<const BUF_SIZE_BYTES: usize> {
uart_mux: &'static MuxUart<'static>,
marker: core::marker::PhantomData<[u8; BUF_SIZE_BYTES]>,
}
impl<const BUF_SIZE_BYTES: usize> DebugWriterComponent<BUF_SIZE_BYTES> {
pub fn new(uart_mux: &'static MuxUart) -> Self {
Self {
uart_mux,
marker: core::marker::PhantomData,
}
}
}
pub struct Capability;
unsafe impl capabilities::ProcessManagementCapability for Capability {}
impl<const BUF_SIZE_BYTES: usize> Component for DebugWriterComponent<BUF_SIZE_BYTES> {
type StaticInput = (
&'static mut MaybeUninit<UartDevice<'static>>,
&'static mut MaybeUninit<RingBuffer<'static, u8>>,
&'static mut MaybeUninit<[u8; BUF_SIZE_BYTES]>,
&'static mut MaybeUninit<kernel::debug::DebugWriter>,
&'static mut MaybeUninit<kernel::debug::DebugWriterWrapper>,
);
type Output = ();
fn finalize(self, s: Self::StaticInput) -> Self::Output {
let buf = s.2.write([0; BUF_SIZE_BYTES]);
let (output_buf, internal_buf) = buf.split_at_mut(DEBUG_BUFFER_SPLIT);
let debugger_uart = s.0.write(UartDevice::new(self.uart_mux, false));
debugger_uart.setup();
let ring_buffer = s.1.write(RingBuffer::new(internal_buf));
let debugger = s.3.write(kernel::debug::DebugWriter::new(
debugger_uart,
output_buf,
ring_buffer,
));
hil::uart::Transmit::set_transmit_client(debugger_uart, debugger);
let debug_wrapper = s.4.write(kernel::debug::DebugWriterWrapper::new(debugger));
unsafe {
kernel::debug::set_debug_writer_wrapper(debug_wrapper);
}
}
}
pub struct DebugWriterNoMuxComponent<
U: uart::Uart<'static> + uart::Transmit<'static> + 'static,
const BUF_SIZE_BYTES: usize,
> {
uart: &'static U,
marker: core::marker::PhantomData<[u8; BUF_SIZE_BYTES]>,
}
impl<U: uart::Uart<'static> + uart::Transmit<'static> + 'static, const BUF_SIZE_BYTES: usize>
DebugWriterNoMuxComponent<U, BUF_SIZE_BYTES>
{
pub fn new(uart: &'static U) -> Self {
Self {
uart,
marker: core::marker::PhantomData,
}
}
}
impl<U: uart::Uart<'static> + uart::Transmit<'static> + 'static, const BUF_SIZE_BYTES: usize>
Component for DebugWriterNoMuxComponent<U, BUF_SIZE_BYTES>
{
type StaticInput = (
&'static mut MaybeUninit<RingBuffer<'static, u8>>,
&'static mut MaybeUninit<[u8; BUF_SIZE_BYTES]>,
&'static mut MaybeUninit<kernel::debug::DebugWriter>,
&'static mut MaybeUninit<kernel::debug::DebugWriterWrapper>,
);
type Output = ();
fn finalize(self, s: Self::StaticInput) -> Self::Output {
let buf = s.1.write([0; BUF_SIZE_BYTES]);
let (output_buf, internal_buf) = buf.split_at_mut(DEBUG_BUFFER_SPLIT);
let ring_buffer = s.0.write(RingBuffer::new(internal_buf));
let debugger = s.2.write(kernel::debug::DebugWriter::new(
self.uart,
output_buf,
ring_buffer,
));
hil::uart::Transmit::set_transmit_client(self.uart, debugger);
let debug_wrapper = s.3.write(kernel::debug::DebugWriterWrapper::new(debugger));
unsafe {
kernel::debug::set_debug_writer_wrapper(debug_wrapper);
}
let _ = self.uart.configure(uart::Parameters {
baud_rate: 115200,
width: uart::Width::Eight,
stop_bits: uart::StopBits::One,
parity: uart::Parity::None,
hw_flow_control: false,
});
}
}