-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuart.c
More file actions
125 lines (113 loc) · 1.94 KB
/
Copy pathuart.c
File metadata and controls
125 lines (113 loc) · 1.94 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "io.h"
#include "gpio.h"
#include "config.h"
#include "uart.h"
#define LINE_BUFFER_SIZE 32
struct hw_uart_t {
union {
struct {
union {
_I uint32_t URBR;
_O uint32_t UTHR;
};
_IO uint32_t UIER;
};
struct {
_IO uint32_t UDLLR;
_IO uint32_t UDLHR;
};
};
union {
_I uint32_t UIIR;
_O uint32_t UFCR;
};
_IO uint32_t ULCR;
_IO uint32_t UMCR;
_I uint32_t ULSR;
_I uint32_t UMSR;
_IO uint32_t USPR;
_IO uint32_t ISR;
_IO uint32_t UMR;
_IO uint32_t UACR;
};
static struct hw_uart_t * const uart = UART0_BASE;
void uart_init(void)
{
// Disable UART
uart->UFCR = 0;
// DLAB = 1, 8-bit
uart->ULCR = 0b10000011;
// Baud rate calculations
#if (EXT_CLK_RATE == MHZ(12)) && (BAUDRATE == 115200)
uart->UMR = 17;
uart->UACR = 594;
uart->UDLLR = 6;
uart->UDLHR = 0;
#else
#error Unsupported baud rate
#endif
// DLAB = 0, 8-bit
uart->ULCR = 0b00000011;
// Enable UART, clear FIFO
uart->UFCR = 0b00010111;
}
void uart_putc(char c)
{
while (!(uart->ULSR & (1 << 6))); // TEMP
uart->UTHR = c;
}
void uart_puts(const char *s)
{
while (*s != '\0')
uart_putc(*s++);
}
void uart_puthex(uint32_t v, int w)
{
for (int i = 0; i < w; i++) {
uint8_t fv = (v >> (4 * (w - 1 - i))) & 0xf;
char c = fv < 10 ? fv + '0' : fv + 'a' - 10;
uart_putc(c);
}
}
void uart_putdec(uint32_t v)
{
uint32_t base = 1000000000;
while (v && v / base == 0)
base /= 10;
while (base) {
uint32_t vv = v / base;
v -= vv * base;
base /= 10;
char c = vv + '0';
uart_putc(c);
}
}
char uart_getc(void)
{
while (!(uart->ULSR & 1));
return uart->URBR;
}
char *uart_get_line(void)
{
static char line[LINE_BUFFER_SIZE];
int i = 0;
for (;;) {
char c = uart_getc();
if (c == '\x7f') {
if (i != 0) {
uart_putc(c);
i--;
}
} else if (c == '\r' || c == '\n') {
uart_puts("\r\n");
break;
} else {
if (i != LINE_BUFFER_SIZE - 1) {
uart_putc(c);
line[i++] = c;
}
}
}
line[i] = 0;
return line;
}