-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from pbrucla/serial_driver
Serial driver
- Loading branch information
Showing
10 changed files
with
124 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "string.h" | ||
|
||
enum COM_PORT { | ||
COM1 = 0x3F8, | ||
COM2 = 0x2F8, | ||
COM3 = 0x3E8, | ||
COM4 = 0x2E8, | ||
COM5 = 0x5F8, | ||
COM6 = 0x4F8, | ||
COM7 = 0x5E8, | ||
COM8 = 0x4E8 | ||
}; | ||
|
||
/** | ||
* Return 1 if init is succesful | ||
* Return 0 otherwise | ||
*/ | ||
int serial_driver_init(); | ||
void write_serial(enum COM_PORT port, const char *s); | ||
|
||
/* TODO implement this */ | ||
int read_serial(enum COM_PORT port, char *dest); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
#include "init.h" | ||
#include "serial.h" | ||
#include "terminal_driver.h" | ||
|
||
void init_drivers() { terminal_driver_init(); } | ||
void init_drivers() | ||
{ | ||
serial_driver_init(); | ||
terminal_driver_init(); | ||
} | ||
|
||
void init_kernel_stack() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "serial.h" | ||
#include "io.h" | ||
|
||
static int init_serial_port(enum COM_PORT port); | ||
inline static int putc_serial(enum COM_PORT port, char c); | ||
|
||
int serial_driver_init() | ||
{ | ||
return init_serial_port(COM1); | ||
|
||
/* TODO initialize all COM ports */ | ||
} | ||
|
||
static int init_serial_port(enum COM_PORT port) | ||
{ | ||
outb(port + 1, 0x00); // Disable all interrupts | ||
outb(port + 3, 0x80); // Enable DLAB (set baud rate divisor) | ||
outb(port + 0, 0x03); // Set divisor to 3 (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 to loopback mode for test byte | ||
outb(port + 0, 0xAE); // Send test byte | ||
|
||
// If test byte was not returned, serial is faulty | ||
if (inb(port + 0) != 0xAE) | ||
return 0; | ||
|
||
// // Otherwise, serial is operational, therefore disable loopback mode | ||
outb(port + 4, 0x0F); | ||
return 1; | ||
} | ||
|
||
void write_serial(enum COM_PORT port, const char *s) | ||
{ | ||
for (size_t i = 0; s[i] != '\0'; ++i) | ||
putc_serial(port, s[i]); | ||
} | ||
|
||
int read_serial(enum COM_PORT port, char *dest) | ||
{ | ||
return 0; | ||
} | ||
|
||
inline static int transmit_buffer_empty(enum COM_PORT port) | ||
{ | ||
/* Read from transmit buffer empty register */ | ||
return inb(port + 5) & 0x20; | ||
} | ||
|
||
inline static int putc_serial(enum COM_PORT port, char c) | ||
{ | ||
/* Wait until transmit buffer is empty */ | ||
while (!transmit_buffer_empty(port)) | ||
; | ||
|
||
/* Write character to output buffer */ | ||
outb(port + 0, c); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters