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

Subversion Repositories minsoc

[/] [minsoc/] [trunk/] [sw/] [drivers/] [uart.c] - Blame information for rev 79

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

Line No. Rev Author Line
1 64 rfajardo
#include "../../backend/board.h"
2 36 rfajardo
#include "../support/support.h"
3
#include "uart.h"
4
 
5 53 ConX.
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
6
 
7
#define WAIT_FOR_XMITR \
8
        do { \
9
                lsr = REG8(UART_BASE + UART_LSR); \
10
        } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
11
 
12
#define WAIT_FOR_THRE \
13
        do { \
14
                lsr = REG8(UART_BASE + UART_LSR); \
15
        } while ((lsr & UART_LSR_THRE) != UART_LSR_THRE)
16
 
17
#define CHECK_FOR_CHAR (REG8(UART_BASE + UART_LSR) & UART_LSR_DR)
18
 
19
#define WAIT_FOR_CHAR \
20
        do { \
21
                lsr = REG8(UART_BASE + UART_LSR); \
22
        } while ((lsr & UART_LSR_DR) != UART_LSR_DR)
23
 
24
#define UART_TX_BUFF_LEN 32
25
#define UART_TX_BUFF_MASK (UART_TX_BUFF_LEN -1)
26
 
27
char tx_buff[UART_TX_BUFF_LEN];
28
volatile int tx_level, rx_level;
29
 
30
void uart_init(void)
31
{
32
        int divisor;
33
 
34
        /* Reset receiver and transmiter */
35
        /* Set RX interrupt for each byte */
36
        REG8(UART_BASE + UART_FCR) = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_1;
37
 
38
        /* Enable RX interrupt */
39
        REG8(UART_BASE + UART_IER) = UART_IER_RDI | UART_IER_THRI;
40
 
41
        /* Set 8 bit char, 1 stop bit, no parity */
42
        REG8(UART_BASE + UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
43
 
44
        /* Set baud rate */
45
        divisor = IN_CLK/(16 * UART_BAUD_RATE);
46
        REG8(UART_BASE + UART_LCR) |= UART_LCR_DLAB;
47
        REG8(UART_BASE + UART_DLM) = (divisor >> 8) & 0x000000ff;
48
        REG8(UART_BASE + UART_DLL) = divisor & 0x000000ff;
49
        REG8(UART_BASE + UART_LCR) &= ~(UART_LCR_DLAB);
50
 
51
        return;
52
}
53
 
54
void uart_putc(char c)
55
{
56
        unsigned char lsr;
57
 
58
        WAIT_FOR_THRE;
59
        REG8(UART_BASE + UART_TX) = c;
60
        if(c == '\n') {
61
                WAIT_FOR_THRE;
62
                REG8(UART_BASE + UART_TX) = '\r';
63
        }
64
        WAIT_FOR_XMITR;
65
}
66
 
67
 
68
 
69
char uart_getc()
70
{
71
        unsigned char lsr;
72
        char c;
73
 
74
        //        WAIT_FOR_CHAR;
75
        c = REG8(UART_BASE + UART_RX);
76
        return c;
77
}
78
 
79
 
80 36 rfajardo
void uart_interrupt()
81
{
82 53 ConX.
        char lala;
83
        unsigned char interrupt_id;
84
        interrupt_id = REG8(UART_BASE + UART_IIR);
85
        if ( interrupt_id & UART_IIR_RDI )
86
        {
87
                lala = uart_getc();
88
                uart_putc(lala+1);
89
        }
90 36 rfajardo
 
91
}
92
 
93
void uart_print_str(char *p)
94
{
95 53 ConX.
        while(*p != 0) {
96
                uart_putc(*p);
97
                p++;
98
        }
99 36 rfajardo
}
100
 
101
void uart_print_long(unsigned long ul)
102
{
103 53 ConX.
        int i;
104
        char c;
105 36 rfajardo
 
106
 
107 53 ConX.
        uart_print_str("0x");
108
        for(i=0; i<8; i++) {
109 36 rfajardo
 
110 53 ConX.
                c = (char) (ul>>((7-i)*4)) & 0xf;
111
                if(c >= 0x0 && c<=0x9)
112
                        c += '0';
113
                else
114
                        c += 'a' - 10;
115
                uart_putc(c);
116
        }
117
 
118 36 rfajardo
}
119
 
120
void uart_print_short(unsigned long ul)
121
{
122 53 ConX.
        int i;
123
        char c;
124
        char flag=0;
125 36 rfajardo
 
126
 
127 53 ConX.
        uart_print_str("0x");
128
        for(i=0; i<8; i++) {
129 36 rfajardo
 
130 53 ConX.
                c = (char) (ul>>((7-i)*4)) & 0xf;
131
                if(c >= 0x0 && c<=0x9)
132
                        c += '0';
133
                else
134
                        c += 'a' - 10;
135
                if ((c != '0') || (i==7))
136
                        flag=1;
137
                if(flag)
138
                        uart_putc(c);
139
        }
140
 
141 36 rfajardo
}

powered by: WebSVN 2.1.0

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