Add: init serial + getting text out of it

This commit is contained in:
2025-12-21 20:33:48 +01:00
parent e6f4200ae9
commit 62302e03d5
6 changed files with 83 additions and 4 deletions

59
src/io/serial.c Normal file
View File

@@ -0,0 +1,59 @@
#include "../kernel.h"
void outb(int port, unsigned char data)
{
__asm__ __volatile__("outb %%al, %%dx" :: "a" (data),"d" (port));
}
unsigned char inb(int port)
{
unsigned char data = 0;
__asm__ __volatile__("inb %%dx, %%al" : "=a" (data) : "d" (port));
return data;
}
// COM1
#define PORT 0x3F8
int serial_init()
{
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)
if (inb(PORT) != 0xAE)
{
return -EIO;
}
// Set normal operation mode
outb(PORT + 4, 0x0F);
return 0;
}
static int is_transmit_empty()
{
return inb(PORT + 5) & 0x20;
}
void write_serial(char c)
{
while (!is_transmit_empty()); // wait for free spot
outb(PORT, c);
}
void serial_kputs(const char* str)
{
unsigned int i=0;
while (str[i])
{
write_serial(str[i]);
i++;
}
}

10
src/io/serial.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef SERIAL_H
#define SERIAL_H
void outb(int port, unsigned char data);
unsigned char inb(int port);
int serial_init();
void serial_kputs(const char* str);
#endif

View File

@@ -3,7 +3,8 @@
enum ErrorCodes
{
ENOMEM
ENOMEM,
EIO
};
#endif

View File

@@ -3,6 +3,7 @@
#include <limine.h>
#include "io/term.h"
#include "io/printf.h"
#include "io/serial.h"
// Limine version used
__attribute__((used, section(".limine_requests")))
@@ -111,6 +112,10 @@ void kmain()
if (term_init()) hcf();
if (serial_init()) kputs("kernel: serial: error: Cannot init serial communication!");
serial_kputs("\n\nkernel: serial: Hello, world from serial!");
// Draw something
printf("%s, %s!", "Hello", "world");