11// see also: https://wiki.osdev.org/Serial_Ports
22use core:: arch:: asm;
33use core:: fmt;
4+ #[ allow( dead_code) ]
45
56const 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
816unsafe fn outb ( port : u16 , val : u8 ) {
@@ -24,7 +32,7 @@ pub struct SerialPort;
2432
2533impl 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