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

Subversion Repositories or1k

[/] [or1k/] [branches/] [oc/] [orp/] [orp_soc/] [sw/] [orp_mon/] [uart.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 748 lampret
#include "board.h"
2
#include "uart.h"
3
 
4
 
5
#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 devisor;
33
 
34
        /* Reset receiver and transmiter */
35
        REG8(UART_BASE + UART_FCR) = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_14;
36
 
37
        /* Disable all interrupts */
38
        REG8(UART_BASE + UART_IER) = 0x00;
39
 
40
        /* Set 8 bit char, 1 stop bit, no parity */
41
        REG8(UART_BASE + UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
42
 
43
        /* Set baud rate */
44
        devisor = IN_CLK/(16 * UART_BAUD_RATE);
45
        REG8(UART_BASE + UART_LCR) |= UART_LCR_DLAB;
46
        REG8(UART_BASE + UART_DLL) = devisor & 0x000000ff;
47
        REG8(UART_BASE + UART_DLM) = (devisor >> 8) & 0x000000ff;
48
        REG8(UART_BASE + UART_LCR) &= ~(UART_LCR_DLAB);
49
 
50
        return;
51
}
52
 
53
void uart_putc(char c)
54
{
55
        unsigned char lsr;
56
 
57
        WAIT_FOR_THRE;
58
        REG8(UART_BASE + UART_TX) = c;
59
        if(c == '\n') {
60
          WAIT_FOR_THRE;
61
          REG8(UART_BASE + UART_TX) = '\r';
62
        }
63
        WAIT_FOR_XMITR;
64
}
65
 
66
void uart_print_str(char *p)
67
{
68
        while(*p != 0) {
69
                uart_putc(*p);
70
                p++;
71
        }
72
}
73
 
74
 
75
void uart_print_long(unsigned long ul)
76
{
77
  int i;
78
  char c;
79
 
80
 
81
  uart_print_str("0x");
82
  for(i=0; i<8; i++) {
83
 
84
  c = (char) (ul>>((7-i)*4)) & 0xf;
85
  if(c >= 0x0 && c<=0x9)
86
    c += '0';
87
  else
88
    c += 'a' - 10;
89
  uart_putc(c);
90
  }
91
 
92
}
93
 
94
 
95
void uart_print_short(unsigned long ul)
96
{
97
  int i;
98
  char c;
99
  char flag=0;
100
 
101
 
102
  uart_print_str("0x");
103
  for(i=0; i<8; i++) {
104
 
105
  c = (char) (ul>>((7-i)*4)) & 0xf;
106
  if(c >= 0x0 && c<=0x9)
107
    c += '0';
108
  else
109
    c += 'a' - 10;
110
  if ((c != '0') || (i==7))
111
    flag=1;
112
  if(flag)
113
    uart_putc(c);
114
  }
115
 
116
}
117
 
118
 
119
 
120
char uart_getc()
121
{
122
        unsigned char lsr;
123
        char c;
124
 
125
        WAIT_FOR_CHAR;
126
        c = REG8(UART_BASE + UART_RX);
127
        return c;
128
}
129
 

powered by: WebSVN 2.1.0

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