-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart.c
More file actions
72 lines (58 loc) · 1.84 KB
/
uart.c
File metadata and controls
72 lines (58 loc) · 1.84 KB
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
/* OneOS-ARM 1 UART Driver
* PL011 UART for QEMU virt machine
* UART0 base address: 0x09000000
*/
#include "uart.h"
/* PL011 UART Register Offsets */
#define UART_DR 0x00 /* Data Register */
#define UART_LCRH 0x2C /* Line Control Register (High) */
#define UART_CR 0x30 /* Control Register */
#define UART_FR 0x18 /* Flag Register */
/* Flag Register bits */
#define UART_FR_TXFE (1 << 7) /* Transmit FIFO empty */
#define UART_FR_BUSY (1 << 3) /* UART busy */
/* Base address of UART0 */
#define UART0_BASE 0x09000000
void uart_init(void)
{
volatile unsigned int *uart = (volatile unsigned int *)UART0_BASE;
/* Disable UART before configuration */
uart[UART_CR / 4] = 0;
/* Configure line control: 8 bits, no parity, 1 stop bit, FIFO enabled */
uart[UART_LCRH / 4] = 0x60; /* 8-bit mode, FIFO enabled */
/* Set baud rate - skip for now, use default */
/* Enable UART: TX and RX */
uart[UART_CR / 4] = 0x0301; /* TXE | RXE | UARTEN */
}
void uart_send(unsigned char c)
{
volatile unsigned int *uart = (volatile unsigned int *)UART0_BASE;
/* Wait until transmit FIFO is not full */
while (uart[UART_FR / 4] & (1 << 5)) { /* TXFF - Transmit FIFO Full */
/* Spin */
}
/* Send character */
uart[UART_DR / 4] = c;
}
void uart_puts(const char *str)
{
while (*str) {
if (*str == '\n') {
uart_send('\r');
}
uart_send(*str++);
}
}
/* Print 64-bit value in hex (no leading 0x) */
void uart_puthex(unsigned long long v)
{
const char *hex = "0123456789ABCDEF";
int started = 0;
for (int i = (sizeof(v) * 8) - 4; i >= 0; i -= 4) {
unsigned int nibble = (v >> i) & 0xF;
if (nibble || started || i == 0) {
uart_send(hex[nibble]);
started = 1;
}
}
}