aboutsummaryrefslogtreecommitdiff
path: root/kernel/drivers/serial.c
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-12-06 20:14:28 -0500
committerRaghuram Subramani <raghus2247@gmail.com>2025-12-06 20:14:28 -0500
commit2cf843ae6de1f35e5a5d3a947d0179337e82752f (patch)
tree7e7983be37568fb0f9cbd806de94573a61df83ca /kernel/drivers/serial.c
parent096d9ab61281062b6672c6c9f413de97e034d3f7 (diff)
misc: C++->CHEADmain
I know; it's POINTLESS! However, I do enjoy programming in C more than C++ so I ended up spending the hour it takes converting this project from C++ to C.
Diffstat (limited to 'kernel/drivers/serial.c')
-rw-r--r--kernel/drivers/serial.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/kernel/drivers/serial.c b/kernel/drivers/serial.c
new file mode 100644
index 0000000..bec5245
--- /dev/null
+++ b/kernel/drivers/serial.c
@@ -0,0 +1,75 @@
+/*
+ * bubbl
+ * Copyright (C) 2024-2025 Raghuram Subramani <raghus2247@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <common.h>
+#include <drivers/serial.h>
+#include <kernel/io.h>
+#include <libk/string.h>
+#include <stdbool.h>
+
+/* Implementation adapted from
+ * https://wiki.osdev.org/Inline_Assembly/Examples */
+
+bool
+serial_initialize(void)
+{
+ outb(PORT + 1, 0x00); // Disable all interrupts
+ outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
+ outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
+ outb(PORT + 1, 0x00); // (hi byte)
+ outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
+ outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
+ outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
+ outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
+ outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial
+ // returns same byte)
+
+ // TODO: Check if serial is faulty (i.e: not same byte as sent)
+ if (inb(PORT + 0) != 0xAE) {
+ return false;
+ }
+
+ // If serial is not faulty set it in normal operation mode
+ // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
+ outb(PORT + 4, 0x0F);
+
+ return true;
+}
+
+ALWAYS_INLINE static int
+is_transmit_empty(void)
+{
+ return inb(PORT + 5) & 0x20;
+}
+
+void
+serial_write_char(const char chr)
+{
+ while (is_transmit_empty() == 0)
+ ;
+
+ outb(PORT, chr);
+}
+
+void
+serial_write_string(const char *string)
+{
+ size_t size = strlen(string);
+ for (size_t i = 0; i < size; i++)
+ serial_write_char(string[i]);
+}