Overview
The serial driver configures COM1 (I/O base 0x3F8) for 9600 baud, 8 data bits, no parity, and 1 stop bit (8N1). This is a transmit-only implementation focused on output for debugging purposes.The serial port is particularly useful during early boot when the framebuffer may not yet be available, and for remote debugging via serial console.
Hardware Configuration
The driver uses the following x86 I/O ports:| Port | Register | Purpose |
|---|---|---|
| 0x3F8 | Data | Transmit/receive data |
| 0x3F9 | Interrupt Enable | Interrupt configuration (divisor high byte when DLAB=1) |
| 0x3FA | FIFO Control | FIFO buffer configuration |
| 0x3FB | Line Control | Serial line parameters |
| 0x3FC | Modem Control | Modem control signals |
| 0x3FD | Line Status | Transmission status |
API Functions
serial_init
Initializes the serial port hardware with standard 8N1 configuration at 9600 baud.Initialization Sequence
- Calculate baud rate divisor:
115200 / 9600 = 12 - Set DLAB (Divisor Latch Access Bit): Write
0x80to Line Control Register (0x3FB) - Set divisor: Write low byte to 0x3F8, high byte to 0x3F9
- Configure line parameters: Write
0x03to 0x3FB (8 data bits, no parity, 1 stop bit) - Enable FIFO: Write
0xC7to FIFO Control Register (0x3FA) - Set modem control: Write
0x03to Modem Control Register (0x3FC)
The baud rate divisor is calculated as
115200 / desired_baud_rate. The base frequency of 115200 Hz is the standard for PC serial ports.serial_send
Transmits a single byte over the serial port.- Waits for the transmit buffer to be empty
- Sends a carriage return (
\r) before newlines for proper terminal formatting - Transmits the byte
is_transmit_empty (Internal)
Checks if the transmit buffer is ready for data.Baud Rate Configuration
The serial port is configured for 9600 baud, calculated using:serial_init:
| Baud Rate | Divisor |
|---|---|
| 115200 | 1 |
| 57600 | 2 |
| 38400 | 3 |
| 19200 | 6 |
| 9600 | 12 |
| 4800 | 24 |
| 2400 | 48 |
Line Termination
The driver automatically converts Unix-style line endings (\n) to Windows-style (\r\n) for compatibility with most serial terminal programs:
Usage Example
Typical usage pattern for kernel logging:Hardware Requirements
The driver assumes a standard 16550-compatible UART at I/O base 0x3F8 (COM1). This is present in virtually all x86/x86_64 systems and emulators.