-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuart.c
58 lines (45 loc) · 1.63 KB
/
uart.c
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
#include "mmio.h"
#include "uart.h"
#include "sys.h"
void uart_init() {
// Disable UART.
mmio_write(UART_CR, 0x00000000);
// Setup the GPIO pin 14 && 15.
// Disable pull up/down for all GPIO pins & delay for 150 cycles.
mmio_write(GPPUD, 0x00000000);
delay(150);
// Disable pull up/down for pin 14,15 & delay for 150 cycles.
mmio_write(GPPUDCLK0, (1 << 14) | (1 << 15));
delay(150);
// Write 0 to GPPUDCLK0 to make it take effect.
mmio_write(GPPUDCLK0, 0x00000000);
// Clear pending interrupts.
mmio_write(UART_ICR, 0x7FF);
// Set integer & fractional part of baud rate.
// Divider = UART_CLOCK/(16 * Baud)
// Fraction part register = (Fractional part * 64) + 0.5
// UART_CLOCK = 3000000; Baud = 115200.
// Divider = 3000000/(16 * 115200) = 1.627 = ~1.
// Fractional part register = (.627 * 64) + 0.5 = 40.6 = ~40.
mmio_write(UART_IBRD, 1);
mmio_write(UART_FBRD, 40);
// Enable FIFO & 8 bit data transmissio (1 stop bit, no parity).
mmio_write(UART_LCRH, (1 << 4) | (1 << 5) | (1 << 6));
// Mask all interrupts.
mmio_write(UART_IMSC, (1 << 1) | (1 << 4) | (1 << 5) |
(1 << 6) | (1 << 7) | (1 << 8) |
(1 << 9) | (1 << 10));
// Enable UART, receive & transfer part of UART.
mmio_write(UART_CR, (1 << 0) | (1 << 8) | (1 << 9));
}
void uart_putc(uint8_t byte) {
while ((mmio_read(UART_FR) & (1 << 5)));
mmio_write(UART_DR, byte);
}
void uart_puts(char *bytes) {
while(*bytes) uart_putc(*(bytes++));
}
uint8_t uart_getc() {
while(mmio_read(UART_FR) & 0x10);
return mmio_read(UART_DR);
}