Initial commit
This commit is contained in:
56
kernel/drivers/serial.c
Normal file
56
kernel/drivers/serial.c
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by rick on 28-01-21.
|
||||
//
|
||||
|
||||
#include "serial.h"
|
||||
#include <types.h>
|
||||
#include <drivers/ports.h>
|
||||
|
||||
int serial_init() {
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_INTERRUPT, 0x00); // Disable all interrupts
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_LINE_CONTROL, 0x80); // Enable DLAB (set baud rate divisor)
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_DLAB_LSB, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_DLAB_MSB, 0x00); // (hi byte)
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_LINE_CONTROL, 0x03); // 8 bits, no parity, one stop bit
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_INTERRUPT_ID_FIFO,
|
||||
0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_MODEM_CONTROL, 0x0B); // IRQs enabled, RTS/DSR set
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_MODEM_CONTROL, 0x1E); // Set in loopback mode, test the serial chip
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_DATA,
|
||||
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 (port_byte_in(PORT_SERIAL_0 + PORT_SERIAL_DATA) != 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)
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_MODEM_CONTROL, 0x0F);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_transmit_empty() {
|
||||
return port_byte_in(PORT_SERIAL_0 + PORT_SERIAL_LINE_STATUS) & 0x20;
|
||||
}
|
||||
|
||||
void write_serial(char a) {
|
||||
while (is_transmit_empty() == 0);
|
||||
|
||||
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_DATA, a);
|
||||
}
|
||||
|
||||
void serial_kprint(char *msg) {
|
||||
u32 i = 0;
|
||||
while (1) {
|
||||
char c = msg[i];
|
||||
if (c == 0) {
|
||||
break;
|
||||
}
|
||||
if (c == '\n') {
|
||||
write_serial('\r');
|
||||
}
|
||||
write_serial(c);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user