AquilaOS documentation
How it works, and its code
Toggle main menu visibility
Loading...
Searching...
No Matches
uart.c
Go to the documentation of this file.
1
5
6
#include <
core/system.h
>
7
#include <
core/string.h
>
8
#include <
console/earlycon.h
>
9
#include <
cpu/io.h
>
10
#include <
mm/mm.h
>
11
12
static
struct
ioaddr
earlycon_uart_ioaddr
= {
13
.addr = 0x3F8,
14
.type =
IOADDR_PORT
,
15
};
16
17
#define UART_IER 1
18
#define UART_FCR 2
19
#define UART_LCR 3
20
#define UART_MCR 4
21
#define UART_DLL 0
22
#define UART_DLH 1
23
#define UART_LCR_DLAB 0x80
24
27
static
void
serial_init
(
void
)
28
{
29
io_out8
(&
earlycon_uart_ioaddr
,
UART_IER
, 0x00);
/* no interrupts */
30
io_out8
(&
earlycon_uart_ioaddr
,
UART_FCR
, 0x00);
/* no FIFO */
31
io_out8
(&
earlycon_uart_ioaddr
,
UART_LCR
, 0x03);
/* 8 bits, no parity, one stop bit */
32
io_out8
(&
earlycon_uart_ioaddr
,
UART_MCR
, 0x03);
/* RTS and DTR */
33
34
uint8_t
lcr =
io_in8
(&
earlycon_uart_ioaddr
,
UART_LCR
);
35
io_out8
(&
earlycon_uart_ioaddr
,
UART_LCR
, lcr |
UART_LCR_DLAB
);
/* the divisor's registers */
36
io_out8
(&
earlycon_uart_ioaddr
,
UART_DLL
, 0x03);
/* divisor 3: 38400 baud (low byte) */
37
io_out8
(&
earlycon_uart_ioaddr
,
UART_DLH
, 0x00);
/* (high byte) */
38
io_out8
(&
earlycon_uart_ioaddr
,
UART_LCR
, lcr & ~
UART_LCR_DLAB
);
/* back to the data registers */
39
}
40
42
static
int
serial_tx_empty
(
void
)
43
{
44
return
io_in8
(&
earlycon_uart_ioaddr
, 5) & 0x20;
45
}
46
50
static
int
serial_chr
(
char
chr)
51
{
52
if
(chr ==
'\n'
)
53
serial_chr
(
'\r'
);
54
55
for
(
int
i = 0; i < 100000 && !
serial_tx_empty
(); ++i)
56
;
57
58
io_out8
(&
earlycon_uart_ioaddr
, 0, chr);
59
60
return
1;
61
}
62
64
static
int
serial_str
(
char
*str)
65
{
66
int
ret = 0;
67
68
while
(*str)
69
ret +=
serial_chr
(*str++);
70
71
return
ret;
72
}
73
74
static
int
earlycon_uart_puts
(
char
*s)
75
{
76
return
serial_str
(s);
77
}
78
79
static
int
earlycon_uart_putc
(
char
c)
80
{
81
return
serial_chr
(c);
82
}
83
84
static
void
earlycon_uart_init
(
void
)
85
{
86
serial_init
();
87
88
/* there's probably a terminal at the other end: reset its attributes */
89
serial_str
(
"\033[0m"
);
90
}
91
92
struct
earlycon
earlycon_uart
= {
93
.init =
earlycon_uart_init
,
94
.putc =
earlycon_uart_putc
,
95
.puts =
earlycon_uart_puts
,
96
};
io_in8
static uint8_t io_in8(struct ioaddr *io, uintptr_t off)
Reads an 8-, 16- or 32-bit register.
Definition
io.h:28
io_out8
static void io_out8(struct ioaddr *io, uintptr_t off, uint8_t val)
Writes an 8-, 16- or 32-bit register.
Definition
io.h:44
earlycon_uart_puts
static int earlycon_uart_puts(char *s)
Definition
uart.c:74
serial_chr
static int serial_chr(char chr)
Writes a character, a newline as CR LF.
Definition
uart.c:50
UART_FCR
#define UART_FCR
Definition
uart.c:18
UART_LCR
#define UART_LCR
Definition
uart.c:19
earlycon_uart_ioaddr
static struct ioaddr earlycon_uart_ioaddr
Definition
uart.c:12
serial_tx_empty
static int serial_tx_empty(void)
Returns whether the UART's transmit holding register is empty.
Definition
uart.c:42
serial_str
static int serial_str(char *str)
Writes a string, and returns how many characters it wrote.
Definition
uart.c:64
UART_DLL
#define UART_DLL
Definition
uart.c:21
UART_MCR
#define UART_MCR
Definition
uart.c:20
UART_IER
#define UART_IER
Definition
uart.c:17
serial_init
static void serial_init(void)
Sets the UART up: no interrupts, no FIFO, 38400 baud, 8 bits, no parity, one stop bit.
Definition
uart.c:27
UART_LCR_DLAB
#define UART_LCR_DLAB
Definition
uart.c:23
earlycon_uart_putc
static int earlycon_uart_putc(char c)
Definition
uart.c:79
UART_DLH
#define UART_DLH
Definition
uart.c:22
earlycon_uart_init
static void earlycon_uart_init(void)
Definition
uart.c:84
IOADDR_PORT
#define IOADDR_PORT
Definition
io.h:89
earlycon.h
earlycon_uart
struct earlycon earlycon_uart
Definition
uart.c:92
system.h
io.h
mm.h
uint8_t
unsigned char uint8_t
Definition
stddef.h:20
string.h
earlycon
An early console's operations.
Definition
earlycon.h:11
ioaddr
Where a device's registers are.
Definition
io.h:95
kernel
arch
i386
earlycon
uart.c
Generated by
1.18.0