Skip to content

Commit 5a543f5

Browse files
committed
feat(serial): Completed definitions and functions of COMs.
1 parent cd1d0bd commit 5a543f5

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

crates/rkos-arch/src/x86/serial.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
// see also: https://wiki.osdev.org/Serial_Ports
22
use core::arch::asm;
33
use core::fmt;
4+
#[allow(dead_code)]
45

56
const COM1: u16 = 0x3F8;
7+
const COM2: u16 = 0x2F8;
8+
const COM3: u16 = 0x3E8;
9+
const COM4: u16 = 0x2E8;
10+
const COM5: u16 = 0x5F8;
11+
const COM6: u16 = 0x4F8;
12+
const COM7: u16 = 0x5E8;
13+
const COM8: u16 = 0x4E8;
614

715
/// Write a byte to the serial port
816
unsafe fn outb(port: u16, val: u8) {
@@ -24,7 +32,7 @@ pub struct SerialPort;
2432

2533
impl SerialPort {
2634
/// Initialize the standard COM1 serial port
27-
pub fn init() {
35+
pub fn init() -> Result<(), &'static str> {
2836
unsafe {
2937
outb(COM1 + 1, 0x00); // Disable all interrupts
3038
outb(COM1 + 3, 0x80); // Enable DLAB (set baud rate divisor)
@@ -33,6 +41,18 @@ impl SerialPort {
3341
outb(COM1 + 3, 0x03); // 8 bits, no parity, one stop bit
3442
outb(COM1 + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
3543
outb(COM1 + 4, 0x0B); // IRQs enabled, RTS/DSR set
44+
outb(COM1 + 4, 0x1E); // Set in loopback mode, test the serial chip
45+
outb(COM1, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
46+
47+
// Check if serial is faulty (i.e: not same byte as sent)
48+
if inb(COM1) != 0xAE {
49+
return Err("Serial port faulty");
50+
}
51+
52+
// If serial is not faulty set it in normal operation mode
53+
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
54+
outb(COM1 + 4, 0x0F);
55+
Ok(());
3656
}
3757
}
3858

0 commit comments

Comments
 (0)