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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [testbench/] [acv_uart.c] - Blame information for rev 344

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

Line No. Rev Author Line
1 344 markom
/* UART test using ACV */
2
 
3
#include "spr_defs.h"
4
#include "support.h"
5
 
6
#define UART_ADDR   (0x9c000000)
7
#define UART_RBR    (UART_ADDR + 0)
8
#define UART_THR    (UART_ADDR + 0)
9
#define UART_IER    (UART_ADDR + 1)
10
#define UART_IIR    (UART_ADDR + 2)
11
#define UART_FCR    (UART_ADDR + 2)
12
#define UART_LCR    (UART_ADDR + 3)
13
#define UART_MCR    (UART_ADDR + 4)
14
#define UART_LSR    (UART_ADDR + 5)
15
#define UART_MSR    (UART_ADDR + 6)
16
 
17
#define UART_DLL    (UART_ADDR + 0)
18
#define UART_DLH    (UART_ADDR + 1)
19
 
20
#ifndef __LINE__
21
#define __LINE__  0
22
#endif
23
 
24
/* fails if x is false */
25
#define ASSERT(x) ((x)?1: fail (__FUNCTION__, __LINE__))
26
/* Waits a few cycles that uart can prepare its data */
27
#define WAIT() {asm ("l.nop");asm ("l.nop");asm ("l.nop");asm ("l.nop");}
28
/* fails if there is an error */
29
#define NO_ERROR() { unsigned x = getreg (UART_LSR); if ((x & 0x1e) && !(x & 0x80)) printf ("LSR7 ERR\n"); ASSERT(!(x & 0x80));}
30
 
31
void fail (char *func, int line)
32
{
33
#ifndef __FUNCTION__
34
#define __FUNCTION__ "?"
35
#endif
36
  printf ("Test failed in %s:%i\n", func, line);
37
  exit (1);
38
}
39
 
40
inline void setreg (unsigned long addr, unsigned char value)
41
{
42
  *((volatile unsigned char *)addr) = value;
43
}
44
 
45
inline unsigned long getreg (unsigned long addr)
46
{
47
  return *((volatile unsigned char *)addr);
48
}
49
 
50
void interrupt_handler ()
51
{
52
  printf ("Int\n");
53
}
54
 
55
/* Test reset values and r/w properties of some registers */
56
 
57
void register_test ()
58
{
59
  printf ("register test\n");
60
  { /* test reset values */
61
    ASSERT(getreg (UART_RBR) == 0x00); //0
62
    ASSERT(getreg (UART_IER) == 0x00); //1
63
    ASSERT(getreg (UART_IIR) == 0xc1); //2
64
    ASSERT(getreg (UART_LCR) == 0x03); //3
65
    ASSERT(getreg (UART_MCR) == 0x00); //4
66
    ASSERT(getreg (UART_LSR) == 0x60); //5
67
    ASSERT(getreg (UART_MSR) == 0x00); //6
68
 
69
    setreg(UART_LCR, 0x80); //enable latches
70
    ASSERT(getreg (UART_DLL) == 0x00); //0
71
    ASSERT(getreg (UART_DLH) == 0x00); //1
72
    setreg(UART_LCR, 0x00); //disable latches
73
  }
74
 
75
  { /* test if status registers are read only */
76
    unsigned long tmp;
77
    int i;
78
    tmp = getreg (UART_LSR);
79
    setreg (UART_LSR, ~tmp);
80
    ASSERT(getreg (UART_LSR) == tmp);
81
 
82
    for (i = 0; i < 9; i++) {
83
      setreg (UART_LSR, 1 < i);
84
      ASSERT(getreg (UART_LSR) == tmp);
85
    }
86
 
87
    tmp = getreg (UART_MSR);
88
    setreg (UART_MSR, ~tmp);
89
    ASSERT(getreg (UART_MSR) == tmp);
90
 
91
    for (i = 0; i < 9; i++) {
92
      setreg (UART_MSR, 1 < i);
93
      ASSERT(getreg (UART_MSR) == tmp);
94
    }
95
  }
96
  ASSERT (!(getreg (UART_LSR) & 0x1f));
97
  { /* test whether MCR is write only, be careful not to set the loopback bit */
98
    ASSERT(getreg (UART_MCR) == 0x00);
99
    setreg (UART_MCR, 0x45);
100
    ASSERT(getreg (UART_MCR) == 0x00);
101
    setreg (UART_MCR, 0xaa);
102
    ASSERT(getreg (UART_MCR) == 0x00);
103
  }
104
  ASSERT (!(getreg (UART_LSR) & 0x1f));
105
  { /* Test if Divisor latch byte holds the data */
106
    int i;
107
    setreg(UART_LCR, 0x80); //enable latches
108
    ASSERT(getreg (UART_LCR) == 0x80);
109
    for (i = 0; i < 16; i++) {
110
      unsigned short tmp = 0xdead << i;
111
      setreg (UART_DLH, tmp >> 8);
112
      setreg (UART_DLL, tmp & 0xff);
113
      ASSERT(getreg (UART_DLL) == (tmp & 0xff)); //0
114
      ASSERT(getreg (UART_DLH) == (tmp >> 8)); //1
115
    }
116
    ASSERT (!(getreg (UART_LSR) & 0x1f));
117
    for (i = 0; i < 16; i++) {
118
      unsigned short tmp = 0xdead << i;
119
      setreg (UART_DLL, tmp >> 8);
120
      setreg (UART_DLH, tmp & 0xff);
121
      ASSERT(getreg (UART_DLL) == (tmp >> 8)); //1
122
      ASSERT(getreg (UART_DLH) == (tmp & 0xff)); //0
123
    }
124
    setreg(UART_LCR, 0x00); //disable latches
125
    ASSERT(getreg (UART_LCR) == 0x00);
126
    ASSERT (!(getreg (UART_LSR) & 0x1f));
127
  }
128
  { /* Test LCR, if it holds our data */
129
    int i;
130
    for (i = 0; i < 6; i++) {
131
      unsigned short tmp = (0xde << i) & 0x3f;
132
      setreg (UART_LCR, tmp);
133
      ASSERT(getreg (UART_LCR) == tmp);
134
    }
135
    ASSERT (!(getreg (UART_LSR) & 0x1f));
136
  }
137
  /* Other registers will be tested later, if they function correctly,
138
     since we cannot test them now, without destroying anything.  */
139
}
140
 
141
/* Initializes uart and sends a few bytes to VAPI. It is then activated and send something back. */
142
 
143
void send_recv_test ()
144
{
145
  char *s;
146
  printf ("send_recv_test");
147
  /* Init */
148
  setreg (UART_IER, 0x00); /* disable interrupts */
149
  WAIT();
150
  ASSERT(getreg (UART_IIR) == 0xc1); /* nothing should be happening */
151
  setreg (UART_FCR, 0x06);      /* clear RX and TX FIFOs */
152
  setreg (UART_LCR, 0x80);
153
  setreg (UART_DLH, 10 >> 8);
154
  setreg (UART_DLL, 10 & 0xff);
155
  setreg (UART_LCR, 0x83);    /* 8N1 @ 10 => 160 instructions for one cycle */
156
  ASSERT(getreg (UART_LCR) == 0x83);
157
 
158
  printf ("basic\n");
159
  ASSERT (!(getreg (UART_LSR) & 0x01));
160
 
161
  /* Send a string */
162
  s = "send_";
163
  while (*s) {
164
    /* Wait for tx fifo to be empty */
165
    while (!(getreg (UART_LSR) & 0x20));
166
    NO_ERROR();
167
    setreg (UART_THR, *s); /* send character */
168
    NO_ERROR();
169
    s++;
170
  }
171
  ASSERT (!(getreg (UART_LSR) & 0x01));
172
  s = "test_";
173
  while (*s) {
174
    /* Wait for tx fifo and tx to be empty */
175
    while (!(getreg (UART_LSR) & 0x40));
176
    NO_ERROR();
177
    setreg (UART_THR, *s); /* send character */
178
    NO_ERROR();
179
    s++;
180
  }
181
  ASSERT (!(getreg (UART_LSR) & 0x01));
182
 
183
  /* Send characters with delay inbetween */
184
  s = "is_running";
185
  while (*s) {
186
    int i;
187
    /* Wait for tx fifo and tx to be empty */
188
    while (!(getreg (UART_LSR) & 0x40));
189
    NO_ERROR();
190
    setreg (UART_THR, *s); /* send character */
191
    NO_ERROR();
192
    for (i = 0; i < 1600; i++) /* wait at least ten chars before sending next one */
193
      asm volatile ("l.nop");
194
    s++;
195
  }
196
 
197
  /* Receives and compares the string */
198
  s = "recv_test";
199
  while (*s) {
200
    /* Wait for rx fifo to be  */
201
    while (!(getreg (UART_LSR) & 0x01));
202
    NO_ERROR();
203
    ASSERT (getreg (UART_RBR) == *s); /* compare character */
204
    NO_ERROR();
205
    s++;
206
  }
207
}
208
 
209
void loopback_test ()
210
{
211
  printf ("loopback test\n");
212
  setreg (UART_MCR, 0);
213
}
214
 
215
int main ()
216
{
217
  /* Use our low priority interrupt handler */
218
  excpt_lpint = (unsigned long)interrupt_handler;
219
 
220
  /* Enable interrupts */
221
  mtspr (SPR_SR, mfspr(SPR_SR) | SPR_SR_EXR);
222
 
223
  register_test ();
224
  send_recv_test ();
225
  /*  break_test ();
226
  different_modes_test ();
227
  loopback_send_rcv_test ();
228
  interrupt_test ();
229
  fifo_test ();
230
  loopback_test ();
231
  modem_test ();
232
  line_error_test ();
233
  speed_error_test ();
234
  frame_error_test ();
235
  modem_error_test ();*/
236
  return 0;
237
}

powered by: WebSVN 2.1.0

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