-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
64 lines (49 loc) · 1.14 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
59
60
61
62
63
64
#include "uart.h"
void init_uart()
{
uint8_t *reg8;
uint16_t *reg16;
uint32_t ibrd, fbrd;
/* setting baudrate */
reg16 = (uint16_t*)UARTIBRD_ADDR;
reg8 = (uint8_t*)UARTFBRD_ADDR;
ibrd = UARTCLK / (UART_BAUDRATE<<4);
fbrd = (ibrd << 6) - ((UARTCLK / UART_BAUDRATE) << 2);
fbrd = ((fbrd << 1) + 1) >> 1;
*reg16 = ibrd;
*reg8 = fbrd;
/* setting line control */
reg8 = (uint8_t*)UARTLCR_H_ADDR;
*reg8 = UARTLCR_PEN | UARTLCR_EPS | UARTLCR_WLEN;
/* interrupt fifo level skip*/
/* mask all interrupt */
reg16 = (uint16_t*)UARTMIS_ADDR;
*reg16 = 0x7FF;
/* DMA control skip*/
/* setting control */
reg16 = (uint16_t*)UARTCR_ADDR;
*reg16 = UARTCR_RXE | UARTCR_TXE | UARTCR_UARTEN;
}
static int transmit_possible()
{
uint8_t *reg8 = (uint8_t*)UARTFR_ADDR;
return !(*reg8 & UARTFR_TXFF);
}
static int receive_possible()
{
uint8_t *reg8 = (uint8_t*)UARTFR_ADDR;
return !(*reg8 & UARTFR_RXFE);
}
int putchar(int c)
{
uint8_t *reg8 = (uint8_t*)UARTDR_ADDR;
while(!transmit_possible());
*reg8 = (uint8_t)(c & 0xFF);
return c;
}
int getchar()
{
uint8_t *reg8 = (uint8_t*)UARTDR_ADDR;
while(!receive_possible());
return (int)(*reg8);
}