From 8c60628bd5db5c238d22fd3ce8b739824606a9de Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Mon, 22 Aug 2022 21:28:11 -0700 Subject: Add serial --- src/serial/buffer.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/serial/mod.rs | 2 ++ 2 files changed, 54 insertions(+) create mode 100644 src/serial/buffer.rs create mode 100644 src/serial/mod.rs (limited to 'src') diff --git a/src/serial/buffer.rs b/src/serial/buffer.rs new file mode 100644 index 0000000..f40771f --- /dev/null +++ b/src/serial/buffer.rs @@ -0,0 +1,52 @@ +use crate::sync::NullLock; +use crate::sync::interface::Mutex; +use uart_16550::SerialPort; + +pub struct Serial { + buffer: Option, +} + +impl Serial { + pub const fn new() -> Self { + Serial { buffer: None } + } +} + +impl NullLock { + pub fn init(&self) { + self.lock(|serial| { + let mut serial_port = unsafe { SerialPort::new(0x3F8) }; + serial_port.init(); + serial.buffer = Some(serial_port); + }); + } +} + +#[doc(hidden)] +pub fn _print(args: ::core::fmt::Arguments) { + use core::fmt::Write; + SERIAL1.lock(|serial| { + if let Some(buffer) = &mut serial.buffer { + buffer.write_fmt(args).expect("printing to serial failed!"); + } + }) +} + +/// Prints to the host through the serial interface. +#[macro_export] +macro_rules! serial_print { + ($($arg:tt)*) => { + $crate::serial::_print(format_args!($($arg)*)); + }; +} + +/// Prints to the host through the serial interface, appending a newline. +#[macro_export] +macro_rules! serial_println { + () => ($crate::serial_print!("\n")); + ($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n"))); + ($fmt:expr, $($arg:tt)*) => ($crate::serial_print!( + concat!($fmt, "\n"), $($arg)*)); +} + +pub static SERIAL1: NullLock = NullLock::new(Serial::new()); diff --git a/src/serial/mod.rs b/src/serial/mod.rs new file mode 100644 index 0000000..dc7dac5 --- /dev/null +++ b/src/serial/mod.rs @@ -0,0 +1,2 @@ +mod buffer; +pub use buffer::*; -- cgit v1.2.1