40 lines
1.2 KiB
C
40 lines
1.2 KiB
C
#include <stdbool.h>
|
|
#include <sys.h>
|
|
#include "serial.h"
|
|
#include "../panic.h"
|
|
|
|
#define PORT 0x3f8 // COM1
|
|
|
|
void serial_init(void) {
|
|
outb(PORT + 1, 0x00); // Disable all interrupts
|
|
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
|
outb(PORT + 0, 0x01); // Set divisor to 1 (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)
|
|
uint8_t response = inb(PORT + 0);
|
|
if(response != 0xAE) {
|
|
panic("Serial is faulty: %X\n", response);
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
void serial_write(uint8_t a) {
|
|
while((inb(PORT + 5) & 0x20) != 0);
|
|
outb(PORT, a);
|
|
}
|
|
|
|
void serial_write_s(const char* str) {
|
|
while(*str != '\0') {
|
|
serial_write(*str++);
|
|
}
|
|
}
|