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

Subversion Repositories minsoc

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

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

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

powered by: WebSVN 2.1.0

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