From 7a6cf86e4dd3094ed5aa41bf76d93c28c89e85e7 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Wed, 20 Jan 2021 21:31:23 -0600 Subject: i386: add serial port support Add support for serial communication in the i386 architecture. Signed-off-by: Danny Holman --- arch/i386/make.config | 1 + arch/i386/pic.h | 16 ++++++++++++++++ arch/i386/serial.c | 40 ++++++++++++++++++++++++++++++++++++++++ include/kernel/serial.h | 8 ++++++++ kernel/init.c | 3 ++- 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 arch/i386/pic.h create mode 100644 arch/i386/serial.c create mode 100644 include/kernel/serial.h diff --git a/arch/i386/make.config b/arch/i386/make.config index a5049af..680760f 100644 --- a/arch/i386/make.config +++ b/arch/i386/make.config @@ -4,3 +4,4 @@ KERNEL_ARCH_LIBS= KERNEL_ARCH_OBJS=$(ARCHDIR)/boot.o \ $(ARCHDIR)/tty.o \ + $(ARCHDIR)/serial.o \ diff --git a/arch/i386/pic.h b/arch/i386/pic.h new file mode 100644 index 0000000..da4a2ee --- /dev/null +++ b/arch/i386/pic.h @@ -0,0 +1,16 @@ +#ifndef PIC_H +#define PIC_H + +#include + +static inline void outb(uint16_t port, uint8_t value) { + asm volatile("outb %0, %1" : : "a"(value), "Nd"(port)); +} + +static inline uint8_t inb(uint16_t port) { + uint8_t ret; + asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} + +#endif diff --git a/arch/i386/serial.c b/arch/i386/serial.c new file mode 100644 index 0000000..b172e82 --- /dev/null +++ b/arch/i386/serial.c @@ -0,0 +1,40 @@ +#include +#include "pic.h" + +#define PORT 0x3f8 + +static int serial_init(void) { + outb(PORT + 1, 0x00); + outb(PORT + 3, 0x80); + outb(PORT + 0, 0x03); + outb(PORT + 1, 0x00); + outb(PORT + 3, 0x03); + outb(PORT + 2, 0xC7); + outb(PORT + 4, 0x0B); + outb(PORT + 4, 0x1E); + outb(PORT + 0, 0xAE); + + if (inb(PORT + 0) != 0xAE) + return -1; + + outb(PORT + 4, 0x0F); + return 0; +} + +int serial_recieved(void) { + return inb(PORT + 5) & 1; +} + +char read_serial(void) { + while (serial_recieved() == 0); + return inb(PORT); +} + +int is_transmit_empty(void) { + return inb(PORT + 5) & 0x20; +} + +void write_serial(char a) { + while (is_transmit_empty() == 0); + outb(PORT, a); +} diff --git a/include/kernel/serial.h b/include/kernel/serial.h new file mode 100644 index 0000000..a3a54b0 --- /dev/null +++ b/include/kernel/serial.h @@ -0,0 +1,8 @@ +#ifndef SERIAL_H +#define SERIAL_H + +static int serial_init(void); +char read_serial(void); +void write_serial(char a); + +#endif diff --git a/kernel/init.c b/kernel/init.c index 576b7f4..ed6d1a7 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -3,5 +3,6 @@ void kernel_main(void) { tty_init(); - printf("Hello world %x", 100); + serial_init(); + printf("Hello world\n"); } -- cgit v1.2.3