-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_port.h
91 lines (75 loc) · 2.91 KB
/
serial_port.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef INCLUDE_SERIAL_PORT_H
#define INCLUDE_SERIAL_PORT_H
#include "io.h" /* io.h is implement in the section "Moving the cursor" */
/* The I/O port com1: enabled=1, mode=file, dev=com1.outs */
/* All the I/O ports are calculated relative to the data port. This is because
* all serial ports (COM1, COM2, COM3, COM4) have their ports in the same
* order, but they start at different values.
*/
#define SERIAL_COM1_BASE 0x3F8 /* COM1 base port */
#define SERIAL_DATA_PORT(base) (base)
#define SERIAL_FIFO_COMMAND_PORT(base) (base + 2)
#define SERIAL_LINE_COMMAND_PORT(base) (base + 3)
#define SERIAL_MODEM_COMMAND_PORT(base) (base + 4)
#define SERIAL_LINE_STATUS_PORT(base) (base + 5)
/* The I/O port commands */
/* SERIAL_LINE_ENABLE_DLAB:
* Tells the serial port to expect first the highest 8 bits on the data port,
* then the lowest 8 bits will follow
*/
#define SERIAL_LINE_ENABLE_DLAB 0x80
void serial_configure_baud_rate(unsigned short com, unsigned short divisor) {
/* Tell the serial port to first expect the highest 8 bits, then the lowest
* 8 bits. This is done by sending 0x80 to the line command port
*/
outb(SERIAL_LINE_COMMAND_PORT(com), SERIAL_LINE_ENABLE_DLAB);
outb(SERIAL_DATA_PORT(com), (divisor >> 8) & 0x00FF);
outb(SERIAL_DATA_PORT(com), divisor & 0x00FF);
}
void serial_configure_line(unsigned short com) {
/* Bit: | 7 | 6 | 5 4 3 | 2 | 1 0 |
* Content: | d | b | prty | s | dl |
* Value: | 0 | 0 | 0 0 0 | 0 | 1 1 | = 0x03
* data length of 8 bits, one stop bit, no parity bit, break control
* disabled and DLAB disabled
*/
outb(SERIAL_LINE_COMMAND_PORT(com), 0x03);
}
void serial_configure_fifo_buffer(unsigned short com) {
/* Bit: | 7 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* Content: | lvl | bs | r | dma | clt | clr | e |
* Value: | 1 1 | 0 | 0 | 0 | 1 | 1 | 1 | = 0xC7
*/
outb(SERIAL_FIFO_COMMAND_PORT(com), 0xC7);
}
void serial_configure_modem(unsigned short com) {
/* Bit: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* Content: | r | r | af | lb | ao2 | ao1 | rts | dtr |
* Value: | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | = 0x03
*/
outb(SERIAL_MODEM_COMMAND_PORT(com), 0x03);
}
int serial_is_transmit_fifo_empty(unsigned short com) {
/* 0x20 = 0010 0000 */
return inb(SERIAL_LINE_STATUS_PORT(com)) & 0x20;
}
void serial_configure(unsigned short port, unsigned short baudRate) {
serial_configure_baud_rate(port, baudRate);
serial_configure_line(port);
serial_configure_fifo_buffer(port);
serial_configure_modem(port);
}
void serial_write_byte(unsigned short port, char byteData) {
outb(port, byteData);
}
int serial_write(unsigned short com, char *buf, unsigned int len) {
unsigned int indexToBuffer = 0;
while (indexToBuffer < len) {
if (serial_is_transmit_fifo_empty(com)) {
serial_write_byte(com, buf[indexToBuffer]);
indexToBuffer++;
}
}
return 0;
}
#endif