AquilaOS documentation
How it works, and its code
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
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
27static 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
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
42static int serial_tx_empty(void)
43{
44 return io_in8(&earlycon_uart_ioaddr, 5) & 0x20;
45}
46
50static 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
59
60 return 1;
61}
62
64static 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
74static int earlycon_uart_puts(char *s)
75{
76 return serial_str(s);
77}
78
79static int earlycon_uart_putc(char c)
80{
81 return serial_chr(c);
82}
83
84static void earlycon_uart_init(void)
85{
87
88 /* there's probably a terminal at the other end: reset its attributes */
89 serial_str("\033[0m");
90}
91
93 .init = earlycon_uart_init,
94 .putc = earlycon_uart_putc,
95 .puts = earlycon_uart_puts,
96};
static uint8_t io_in8(struct ioaddr *io, uintptr_t off)
Reads an 8-, 16- or 32-bit register.
Definition io.h:28
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
static int earlycon_uart_puts(char *s)
Definition uart.c:74
static int serial_chr(char chr)
Writes a character, a newline as CR LF.
Definition uart.c:50
#define UART_FCR
Definition uart.c:18
#define UART_LCR
Definition uart.c:19
static struct ioaddr earlycon_uart_ioaddr
Definition uart.c:12
static int serial_tx_empty(void)
Returns whether the UART's transmit holding register is empty.
Definition uart.c:42
static int serial_str(char *str)
Writes a string, and returns how many characters it wrote.
Definition uart.c:64
#define UART_DLL
Definition uart.c:21
#define UART_MCR
Definition uart.c:20
#define UART_IER
Definition uart.c:17
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
#define UART_LCR_DLAB
Definition uart.c:23
static int earlycon_uart_putc(char c)
Definition uart.c:79
#define UART_DLH
Definition uart.c:22
static void earlycon_uart_init(void)
Definition uart.c:84
#define IOADDR_PORT
Definition io.h:89
struct earlycon earlycon_uart
Definition uart.c:92
unsigned char uint8_t
Definition stddef.h:20
An early console's operations.
Definition earlycon.h:11
Where a device's registers are.
Definition io.h:95