diff --git a/Makefile b/Makefile index 1a64f96..5dfba95 100644 --- a/Makefile +++ b/Makefile @@ -145,6 +145,7 @@ run: $(IMAGE) @qemu-system-i386 \ -boot order=a \ -drive file=$<,format=raw \ + -serial stdio gdb: $(IMAGE) @qemu-system-i386 \ diff --git a/include/tty.h b/include/tty.h index fae4111..ec19b39 100644 --- a/include/tty.h +++ b/include/tty.h @@ -56,4 +56,8 @@ void init_screen(TTY* tty); void out_char(CONSOLE* con, char ch); int is_current_console(CONSOLE* con); +int init_serial(); +char read_serial(); +void write_serial(char a); + #endif /* _ORANGES_TTY_H_ */ \ No newline at end of file diff --git a/kernel/Makefrag b/kernel/Makefrag index 9efc253..eb250a7 100644 --- a/kernel/Makefrag +++ b/kernel/Makefrag @@ -30,6 +30,7 @@ KERN_SRCFILES :=kernel/kernel.asm \ kernel/i8259.c \ kernel/testfunc.c \ kernel/protect.c \ + kernel/serialport.c \ lib/klib.c \ lib/kliba.asm \ diff --git a/kernel/main.c b/kernel/main.c index 70f5d1a..0a4afc9 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -21,6 +21,7 @@ #include "x86.h" #include "assert.h" #include "stdio.h" +#include "tty.h" static int initialize_processes(); //added by xw, 18/5/26 static int initialize_cpus(); //added by xw, 18/6/2 @@ -30,12 +31,13 @@ static int initialize_cpus(); //added by xw, 18/6/2 *======================================================================*/ int kernel_main() { + init_serial(); int error; disp_pos = 0; for (int i = 0; i < 25; i++) { for (int j = 0; j < 80; j++) { - kprintf(" "); + disp_str(" "); } } disp_pos = 0; diff --git a/kernel/serialport.c b/kernel/serialport.c new file mode 100644 index 0000000..1c6861a --- /dev/null +++ b/kernel/serialport.c @@ -0,0 +1,45 @@ +#include "x86.h" +#include "tty.h" + +#define PORT 0x3f8 // COM1 + +int init_serial() { + 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; +} +static inline int is_transmit_empty() { + return inb(PORT + 5) & 0x20; +} + +static inline int serial_received() { + return inb(PORT + 5) & 1; +} + +char read_serial() { + while (serial_received() == 0); + + return inb(PORT); +} + +void write_serial(char a) { + while (is_transmit_empty() == 0); + + outb(PORT,a); +} \ No newline at end of file diff --git a/lib/kprintf.c b/lib/kprintf.c index 66669ec..970dfa7 100644 --- a/lib/kprintf.c +++ b/lib/kprintf.c @@ -3,6 +3,11 @@ #include "proc.h" #include "global.h" #include "proto.h" +#include "tty.h" + +static void serialputch(int ch, void * cnt) { + write_serial(ch); +} static void kprintfputch(int ch, void *b) @@ -15,7 +20,8 @@ int vkprintf(const char *fmt, va_list ap) { - vprintfmt((void*)kprintfputch, NULL, fmt, ap); + // vprintfmt((void*)kprintfputch, NULL, fmt, ap); + vprintfmt((void*)serialputch, NULL, fmt, ap); return 0; }