1 | #include "uart.h" |
---|
2 | |
---|
3 | const int UART_BASE_ADR[1] = {0}; |
---|
4 | const int UART_BAUDS[1] = {0}; |
---|
5 | const int IN_CLK =50000000; |
---|
6 | |
---|
7 | #define REG8(add) *((volatile unsigned char *)(add)) |
---|
8 | |
---|
9 | #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE) |
---|
10 | |
---|
11 | #define WAIT_FOR_XMITR(core) \ |
---|
12 | do { \ |
---|
13 | lsr = REG8(UART_BASE_ADR[core] + UART_LSR); \ |
---|
14 | } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY) |
---|
15 | |
---|
16 | #define WAIT_FOR_THRE(core) \ |
---|
17 | do { \ |
---|
18 | lsr = REG8(UART_BASE_ADR[core] + UART_LSR); \ |
---|
19 | } while ((lsr & UART_LSR_THRE) != UART_LSR_THRE) |
---|
20 | |
---|
21 | #define CHECK_FOR_CHAR(core) (REG8(UART_BASE_ADR[core] + UART_LSR) & UART_LSR_DR) |
---|
22 | |
---|
23 | #define WAIT_FOR_CHAR(core) \ |
---|
24 | do { \ |
---|
25 | lsr = REG8(UART_BASE_ADR[core] + UART_LSR); \ |
---|
26 | } while ((lsr & UART_LSR_DR) != UART_LSR_DR) |
---|
27 | |
---|
28 | #define UART_TX_BUFF_LEN 32 |
---|
29 | #define UART_TX_BUFF_MASK (UART_TX_BUFF_LEN -1) |
---|
30 | |
---|
31 | char tx_buff[UART_TX_BUFF_LEN]; |
---|
32 | volatile int tx_level, rx_level; |
---|
33 | |
---|
34 | void main() __attribute__((noreturn)); |
---|
35 | void main() |
---|
36 | { |
---|
37 | asm("mov 0x00, %sp\n"); |
---|
38 | asm("mov 0x00, %fp\n"); |
---|
39 | uart_init(0); |
---|
40 | for(;;) { |
---|
41 | uart_puts(0,"XOpenSparc is alive \n"); |
---|
42 | } |
---|
43 | //return; |
---|
44 | } |
---|
45 | |
---|
46 | void uart_init(char core) |
---|
47 | { |
---|
48 | long allone=0xffffffffffffffff; |
---|
49 | int divisor; |
---|
50 | float float_divisor; |
---|
51 | |
---|
52 | /* Reset receiver and transmiter */ |
---|
53 | REG8( UART_FCR ) = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_14; |
---|
54 | |
---|
55 | /* Disable all interrupts */ |
---|
56 | REG8(UART_BASE_ADR[core] + UART_IER) = 0x00; |
---|
57 | |
---|
58 | /* Set 8 bit char, 1 stop bit, no parity */ |
---|
59 | REG8(UART_BASE_ADR[core] + UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY); |
---|
60 | |
---|
61 | /* Set baud rate */ |
---|
62 | float_divisor = (float) IN_CLK/(16 * UART_BAUDS[core]); |
---|
63 | float_divisor += 0.50f; // Ensure round up |
---|
64 | divisor = (int) float_divisor; |
---|
65 | |
---|
66 | REG8(UART_BASE_ADR[core] + UART_LCR) |= UART_LCR_DLAB; |
---|
67 | REG8(UART_BASE_ADR[core] + UART_DLL) = divisor & 0x000000ff; |
---|
68 | REG8(UART_BASE_ADR[core] + UART_DLM) = (divisor >> 8) & 0x000000ff; |
---|
69 | REG8(UART_BASE_ADR[core] + UART_LCR) &= ~(UART_LCR_DLAB); |
---|
70 | |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | void uart_putc(char core, char c) |
---|
75 | { |
---|
76 | unsigned char lsr; |
---|
77 | |
---|
78 | WAIT_FOR_THRE(core); |
---|
79 | REG8(UART_BASE_ADR[core] + UART_TX) = c; |
---|
80 | if(c == '\n') { |
---|
81 | WAIT_FOR_THRE(core); |
---|
82 | REG8(UART_BASE_ADR[core] + UART_TX) = '\r'; |
---|
83 | } |
---|
84 | WAIT_FOR_XMITR(core); |
---|
85 | } |
---|
86 | |
---|
87 | void uart_puts (char core, char *s) { |
---|
88 | // loop until *s != NULL |
---|
89 | while (*s) { |
---|
90 | uart_putc(core,*s); |
---|
91 | s++; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | // Only used when we know THRE is empty, typically in interrupt |
---|
98 | /*void uart_putc_noblock(char core, char c) |
---|
99 | { |
---|
100 | REG8(UART_BASE_ADR[core] + UART_TX) = c; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | char uart_getc(char core) |
---|
105 | { |
---|
106 | unsigned char lsr; |
---|
107 | char c; |
---|
108 | |
---|
109 | WAIT_FOR_CHAR(core); |
---|
110 | c = REG8(UART_BASE_ADR[core] + UART_RX); |
---|
111 | return c; |
---|
112 | } |
---|
113 | |
---|
114 | int uart_check_for_char(char core) |
---|
115 | { |
---|
116 | return CHECK_FOR_CHAR(core); |
---|
117 | } |
---|
118 | |
---|
119 | void uart_rxint_enable(char core) |
---|
120 | { |
---|
121 | REG8(UART_BASE_ADR[core] + UART_IER) |= UART_IER_RDI; |
---|
122 | } |
---|
123 | |
---|
124 | void uart_rxint_disable(char core) |
---|
125 | { |
---|
126 | REG8(UART_BASE_ADR[core] + UART_IER) &= ~(UART_IER_RDI); |
---|
127 | } |
---|
128 | |
---|
129 | void uart_txint_enable(char core) |
---|
130 | { |
---|
131 | REG8(UART_BASE_ADR[core] + UART_IER) |= UART_IER_THRI; |
---|
132 | } |
---|
133 | |
---|
134 | void uart_txint_disable(char core) |
---|
135 | { |
---|
136 | REG8(UART_BASE_ADR[core] + UART_IER) &= ~(UART_IER_THRI); |
---|
137 | } |
---|
138 | |
---|
139 | char uart_get_iir(char core) |
---|
140 | { |
---|
141 | return REG8(UART_BASE_ADR[core] + UART_IIR); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | char uart_get_lsr(char core) |
---|
146 | { |
---|
147 | return REG8(UART_BASE_ADR[core] + UART_LSR); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | char uart_get_msr(char core) |
---|
152 | { |
---|
153 | return REG8(UART_BASE_ADR[core] + UART_MSR); |
---|
154 | } |
---|
155 | */ |
---|
156 | |
---|
157 | |
---|