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

Subversion Repositories test_project

[/] [test_project/] [trunk/] [sw/] [uart/] [uart.c] - Blame information for rev 25

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

Line No. Rev Author Line
1 25 julius
#include "../support/support.h"
2
#include "../support/board.h"
3
#include "uart.h"
4
 
5
 
6
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
7
 
8
#define WAIT_FOR_XMITR \
9
        do { \
10
                lsr = REG8(UART_BASE + UART_LSR); \
11
        } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
12
 
13
#define WAIT_FOR_THRE \
14
        do { \
15
                lsr = REG8(UART_BASE + UART_LSR); \
16
        } while ((lsr & UART_LSR_THRE) != UART_LSR_THRE)
17
 
18
#define CHECK_FOR_CHAR (REG8(UART_BASE + UART_LSR) & UART_LSR_DR)
19
 
20
#define WAIT_FOR_CHAR \
21
         do { \
22
                lsr = REG8(UART_BASE + UART_LSR); \
23
         } while ((lsr & UART_LSR_DR) != UART_LSR_DR)
24
 
25
#define UART_TX_BUFF_LEN 32
26
#define UART_TX_BUFF_MASK (UART_TX_BUFF_LEN -1)
27
 
28
char tx_buff[UART_TX_BUFF_LEN];
29
volatile int tx_level, rx_level;
30
 
31
void uart_init(void)
32
{
33
        int devisor;
34
 
35
        /* Reset receiver and transmiter */
36
        REG8(UART_BASE + UART_FCR) = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_14;
37
 
38
        /* Disable all interrupts */
39
        REG8(UART_BASE + UART_IER) = 0x00;
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
        devisor = IN_CLK/(16 * UART_BAUD_RATE);
46
        REG8(UART_BASE + UART_LCR) |= UART_LCR_DLAB;
47
        REG8(UART_BASE + UART_DLL) = devisor & 0x000000ff;
48
        REG8(UART_BASE + UART_DLM) = (devisor >> 8) & 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
void uart_print_str(char *p)
68
{
69
        while(*p != 0) {
70
                uart_putc(*p);
71
                p++;
72
        }
73
}
74
 
75
 
76
void uart_print_long(unsigned long ul)
77
{
78
  int i;
79
  char c;
80
 
81
 
82
  uart_print_str("0x");
83
  for(i=0; i<8; i++) {
84
 
85
  c = (char) (ul>>((7-i)*4)) & 0xf;
86
  if(c >= 0x0 && c<=0x9)
87
    c += '0';
88
  else
89
    c += 'a' - 10;
90
  uart_putc(c);
91
  }
92
 
93
}
94
 
95
 
96
void uart_print_short(unsigned long ul)
97
{
98
  int i;
99
  char c;
100
  char flag=0;
101
 
102
 
103
  uart_print_str("0x");
104
  for(i=0; i<8; i++) {
105
 
106
  c = (char) (ul>>((7-i)*4)) & 0xf;
107
  if(c >= 0x0 && c<=0x9)
108
    c += '0';
109
  else
110
    c += 'a' - 10;
111
  if ((c != '0') || (i==7))
112
    flag=1;
113
  if(flag)
114
    uart_putc(c);
115
  }
116
 
117
}
118
 
119
 
120
 
121
char uart_getc()
122
{
123
        unsigned char lsr;
124
        char c;
125
 
126
        WAIT_FOR_CHAR;
127
        c = REG8(UART_BASE + UART_RX);
128
        return c;
129
}
130
 
131
 
132
int main()
133
{
134
        uart_init();
135
 
136
        /* We can't use printf because in this simple example
137
           we don't link C library. */
138
        uart_print_str("Hello World.\n\r");
139
 
140
        report(0xdeaddead);
141
        exit(0);
142
}

powered by: WebSVN 2.1.0

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