add serial port

This commit is contained in:
catfood 2022-12-20 21:25:09 +08:00
parent 4f3db9a86e
commit d0cdcdf420
6 changed files with 61 additions and 2 deletions

View File

@ -145,6 +145,7 @@ run: $(IMAGE)
@qemu-system-i386 \
-boot order=a \
-drive file=$<,format=raw \
-serial stdio
gdb: $(IMAGE)
@qemu-system-i386 \

View File

@ -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_ */

View File

@ -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 \

View File

@ -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;

45
kernel/serialport.c Normal file
View File

@ -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);
}

View File

@ -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;
}