OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [OpenRISC_SIM_GCC/] [drivers/] [uart.c] - Blame information for rev 666

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.