summaryrefslogtreecommitdiff
path: root/kernel/drivers/serial.c
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-30 22:12:14 +0100
committerAnton Kling <anton@kling.gg>2023-10-31 00:18:38 +0100
commit8a9208612eec8ddae4c418485d848ecfa0613699 (patch)
tree2f4b29200c2f0c19ae52f45bdb9b38a41b356e30 /kernel/drivers/serial.c
parentca76600acc8bf7a02346efa5bd8f17072210ec01 (diff)
Meta: Move kernel and userland to their own folders.
This is to allow both the kernel and the userland to share certain header files and to make the folder structure a bit more clear.
Diffstat (limited to 'kernel/drivers/serial.c')
-rw-r--r--kernel/drivers/serial.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/kernel/drivers/serial.c b/kernel/drivers/serial.c
new file mode 100644
index 0000000..549d852
--- /dev/null
+++ b/kernel/drivers/serial.c
@@ -0,0 +1,35 @@
+#include "cpu/io.h"
+
+#define PORT 0x3f8 // COM1
+
+int serial_init(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)
+
+ // Check if serial is faulty (i.e: not same byte as sent)
+ if (inb(PORT + 0) != 0xAE) {
+ return 1;
+ }
+
+ // 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 0;
+}
+
+int is_transmit_empty() { return inb(PORT + 5) & 0x20; }
+
+void write_serial(char a) {
+ while (is_transmit_empty() == 0)
+ ;
+
+ outb(PORT, a);
+}