summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/serial/buffer.rs52
-rw-r--r--src/serial/mod.rs2
2 files changed, 54 insertions, 0 deletions
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<SerialPort>,
+}
+
+impl Serial {
+ pub const fn new() -> Self {
+ Serial { buffer: None }
+ }
+}
+
+impl NullLock<Serial> {
+ 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<Serial> = 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::*;