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

Subversion Repositories vapi

[/] [vapi/] [trunk/] [uart.c] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 10 markom
/* uart.c -- test for uart, by using VAPI
2
   Copyright (C) 2001, Marko Mlinar, markom@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20 11 erez
 
21
#include "vapi.h"
22
 
23
unsigned long num_vapi_ids = 1;
24
 
25 10 markom
/* UART messages */
26
#define TX_CMD      0xff000000
27
#define TX_CMD0     0x00000000
28
#define TX_CHAR     0x000000ff
29
#define TX_NBITS    0x00000300
30
#define TX_STOPLEN  0x00000400
31
#define TX_PARITY   0x00000800
32
#define TX_EVENP    0x00001000
33
#define TX_STICK    0x00002000
34
#define TX_BREAK    0x00004000
35
 
36
/* Send speed */
37
#define TX_CMD1     0x01000000
38
#define TX_SPEED    0x0000ffff
39
 
40
/* Send receive LCR reg */
41
#define TX_CMD2     0x02000000
42
 
43
/* Send skew */
44
#define TX_CMD3     0x03000000
45
 
46
/* Set break */
47
#define TX_CMD4     0x04000000
48
#define TX_CMD4_BREAK 0x00010000
49
#define TX_CMD4_DELAY 0x0000ffff
50
 
51
/* Check FIFO */
52
#define TX_CMD5     0x05000000
53
#define TX_CMD5_FIFOFULL 0x0000001
54
 
55
#define RX_CMD0     0x00000000
56
#define RX_PARERR   0x00010000
57
#define RX_FRAERR   0x00020000
58
 
59
/* fails if x is false */
60
#define ASSERT(x) ((x)?1:fail (__FUNCTION__, __LINE__))
61
#define MARK() printf ("Passed line %i\n", __LINE__)
62
#define VAPI_READ(x) {unsigned long r = vapi_read (); printf ("expected 0x%08x, read 0x%08x\n", r, (x)); ASSERT(r == (x));}
63
 
64
#ifndef __LINE__
65
#define __LINE__  0
66
#endif
67
 
68
void fail (char *func, int line)
69
{
70
#ifndef __FUNCTION__
71
#define __FUNCTION__ "?"
72
#endif
73
  printf ("Test failed in %s:%i\n", func, line);
74
  exit (1);
75
}
76
char str[5000];
77
 
78
/* current comm. control bits */
79
int control, control_rx;
80
 
81
void recv_char (char c)
82
{
83
  unsigned long tmp = vapi_read ();
84
  printf ("expected %08x, read %08x\n", control_rx | c, tmp);
85
  /* test if something is wrong */
86
  ASSERT ((tmp & 0xffffff00) == control_rx);
87
  tmp &= 0xff;
88
  if (tmp) printf ("'%c'\n", (char)tmp);
89
  else printf ("\\0\n");
90
  printf ("expected %02x, read %02x\n", c, tmp);
91
  ASSERT (c == (char)tmp);
92
}
93
 
94
void send_char (char c)
95
{
96
  vapi_write (c | (control & 0xffffff00));
97
}
98
 
99
char *read_string (char *s)
100
{
101
  char *t = s;
102
  unsigned long tmp = 1;
103
  while (tmp) {
104
    tmp = vapi_read ();
105
    printf ("%08x, %08x\n", tmp, control_rx);
106
    /* test if something is wrong */
107
    ASSERT ((tmp & 0xffffff00) == control_rx);
108
    tmp &= 0xff;
109
    if (tmp) printf ("'%c'\n", (char)tmp);
110
    else printf ("\\0\n");
111
    *(t++) = (char)tmp;
112
  }
113
  return s;
114
}
115
 
116
 
117
void compare_string (char *s) {
118
  while (*s) {
119
    unsigned long tmp = vapi_read ();
120
    /* test if something is wrong */
121
    ASSERT (tmp == (control_rx | *s));
122
    tmp &= 0xff;
123
    if (tmp) printf ("'%c'\n", (char)tmp);
124
    else printf ("\\0\n");
125
    s++;
126
  }
127
}
128
 
129
void send_string (char *s)
130
{
131
  while (*s)
132
    vapi_write (*(s++) | (control & 0xffffff00));
133
}
134
 
135
void init_8n1 ()
136
{
137
  vapi_write(TX_CMD1 | 2); /* Set tx/rx speed */
138
  control = control_rx = TX_CMD0 | TX_NBITS;
139
  vapi_write(TX_CMD2 | control_rx); /* Set rx mode */
140
}
141
 
142
void test_registers ()
143
{
144
  /* This test is performed just by cpu, if it is not stopped, we have an error.  */
145
  printf ("Testing registers... ");
146
  MARK();
147
}
148
 
149
void send_recv_test ()
150
{
151
  printf ("send_recv_test\n");
152
  MARK();
153
  read_string (str);
154
  MARK();
155
  printf ("OK\nread: %s\n",str);
156
  ASSERT (strcmp (str, "send_test_is_running") == 0);
157
  send_string ("recv");
158
  MARK();
159
  printf ("OK\n");
160
}
161
 
162
void break_test ()
163
{
164
  printf ("break_test\n");
165
  /* receive a break */
166
  VAPI_READ (TX_BREAK);
167
  MARK();
168
  vapi_write (control | '*');
169
  MARK();
170
  VAPI_READ (control | '!');
171
  MARK();
172
 
173
  /* send a break */
174
  vapi_write (TX_CMD4 | TX_CMD4_BREAK | (0) & TX_CMD4_DELAY);
175
  vapi_write (control | 'b');
176
  MARK();
177
  VAPI_READ (control | '#');
178
  MARK();
179
  vapi_write (TX_CMD4 | (0) & TX_CMD4_DELAY);
180
  vapi_write (control | '$');
181
  MARK();
182
 
183
  /* Receive a breaked string "ns<brk>*", only "ns" should be received.  */
184
  compare_string ("ns");
185
  VAPI_READ (TX_BREAK);
186
  send_string ("?");
187
  MARK();
188
 
189
  /* Send a break */
190
  VAPI_READ (control | '#');
191
  vapi_write(TX_CMD4 | TX_CMD4_BREAK | (5 & TX_CMD4_DELAY));
192
  vapi_write (control | 0);
193
  MARK();
194
#if 0
195
  /* Wait four chars */
196
  send_string ("234");
197
  MARK();
198
#endif
199
 
200
  /* FIFO should be empty */
201
  vapi_write(TX_CMD5);
202
  send_string ("5");
203
  /* FIFO should be nonempty and */
204
  vapi_write(TX_CMD5 | TX_CMD5_FIFOFULL);
205
  MARK();
206
  /* it should contain '?' */
207
  VAPI_READ ('?' | control);
208
  /* Reset break signal*/
209
  vapi_write(TX_CMD4 | (0) & TX_CMD4_DELAY);
210
  MARK();
211
  vapi_write ('!' | control);
212
  printf ("OK\n");
213
}
214
 
215
/* Tries to send data in different modes in both directions */
216
 
217
/* Utility function, that tests current configuration */
218
void test_mode (int nbits)
219
{
220
  unsigned mask = (1 << nbits) - 1;
221
  recv_char ('U' & mask); //0x55
222
#if DETAILED
223
  recv_char ('U' & mask); //0x55
224
  send_char ('U');        //0x55  
225
#endif
226
  send_char ('U');        //0x55
227
  recv_char ('a' & mask); //0x61
228
#if DETAILED
229
  recv_char ('a' & mask); //0x61
230
  send_char ('a');        //0x61
231
#endif
232
  send_char ('a');        //0x61
233
}
234
 
235
void different_modes_test ()
236
{
237
  int speed, length, parity;
238
  printf ("different modes test\n");
239
  /* Init */
240
  /* Test different speeds */
241
  for (speed = 1; speed < 5; speed++) {
242
    vapi_write(TX_CMD1 | speed); /* Set tx/rx speed */
243
    control_rx = control = 0x03 << 8;    /* 8N1 */
244
    vapi_write(TX_CMD2 | control_rx); /* Set rx mode */
245
    test_mode (8);
246
    MARK();
247
  }
248
  MARK();
249
 
250
  vapi_write(TX_CMD1 | 1); /* Set tx/rx speed */
251
  MARK();
252
 
253
  /* Test all parity modes with different char lengths */
254
  for (parity = 0; parity < 8; parity++)
255
    for (length = 0; length < 4; length++) {
256
      control_rx = control = (length | (0 << 2) | (parity << 3)) << 8;
257
      vapi_write(TX_CMD2 | control_rx);
258
      test_mode (5 + length);
259
      MARK();
260
    }
261
  MARK();
262
 
263
  /* Test configuration, if we have >1 stop bits */
264
  for (length = 0; length < 4; length++) {
265
    control_rx = control = (length | (1 << 2) | (0 << 3)) << 8;
266
    vapi_write(TX_CMD2 | control_rx);
267
    test_mode (5 + length);
268
    MARK();
269
  }
270
  MARK();
271
 
272
  /* Restore normal mode */
273
  recv_char ('T'); /* Wait for acknowledge */
274
  vapi_write(TX_CMD1 | 2); /* Set tx/rx speed */
275
  control_rx = control = 0x03 << 8;    /* 8N1 @ 2*/
276
  vapi_write(TX_CMD2 | control_rx); /* Set rx mode */
277
  send_char ('x'); /* Send a character. It is possible that this char is received unproperly */
278
  recv_char ('T'); /* Wait for acknowledge before ending the test */
279
  MARK();
280
  printf ("OK\n");
281
}
282
 
283
/* Test various FIFO levels, break and framing error interrupt, etc */
284
 
285
void interrupt_test ()
286
{
287
  int i;
288
  vapi_write(TX_CMD1 | 6); /* Set tx/rx speed */
289
  printf ("interrupt_test\n");
290
  /* start interrupt test */
291
  recv_char ('I'); /* Test trigger level 1 */
292
  send_char ('0');
293
 
294
  recv_char ('I'); /* Test trigger level 4 */
295
  send_char ('1');
296
  send_char ('2');
297
  send_char ('3');
298
  send_char ('4');
299
 
300
  recv_char ('I'); /* Test trigger level 8 */
301
  send_char ('5');
302
  send_char ('6');
303
  send_char ('7');
304
  send_char ('8');
305
  send_char ('9');
306
 
307
  recv_char ('I'); /* Test trigger level 14 */
308
  send_char ('a');
309
  send_char ('b');
310
  send_char ('c');
311
  send_char ('d');
312
  send_char ('e');
313
  send_char ('f');
314
  send_char ('g');
315
 
316
  recv_char ('I'); /* Test OE */
317
  send_char ('h');
318
  send_char ('i');
319
  send_char ('j');
320
  send_char ('*'); /* should not be put in the fifo */
321
 
322
  recv_char ('I'); /* test break interrupt */
323
  /* send a break */
324
  vapi_write (TX_CMD4 | TX_CMD4_BREAK | (0) & TX_CMD4_DELAY);
325
  vapi_write (control | 'b');
326
  MARK();
327
  recv_char ('B'); /* Release break */
328
  MARK();
329
  vapi_write (TX_CMD4 | (0) & TX_CMD4_DELAY);
330
  vapi_write (control | '$');
331
  MARK();
332
 
333
  /* TODO: Check for parity error */
334
  /* TODO: Check for frame error */
335
 
336
  /* Check for timeout */
337
  recv_char ('I');
338
  send_char ('T'); /* Send char -> timeout should occur */
339
  recv_char ('T'); /* Wait for acknowledge before changing configuration */
340
  MARK ();
341
 
342
  /* Restore previous mode */
343
  vapi_write(TX_CMD1 | 2); /* Set tx/rx speed */
344
  control = control_rx = TX_CMD0 | TX_NBITS;
345
  vapi_write(TX_CMD2 | control_rx); /* Set rx mode */
346
  recv_char ('T'); /* Wait for acknowledge before ending the test */
347
  printf ("OK\n");
348
}
349
 
350
/* Test if all control bits are set correctly.  Lot of this was already tested
351
   elsewhere and tests are not duplicated.  */
352
 
353
void control_register_test ()
354
{
355
  printf ("control_register_test\n");
356
  recv_char ('*');
357
  send_char ('*');
358
  MARK ();
359
  recv_char ('!');
360
  send_char ('!');
361
  MARK ();
362
 
363
  recv_char ('1');
364
  recv_char ('*');
365
  send_char ('*');
366
  printf ("OK\n");
367
}
368
 
369
void line_error_test ()
370
{
371
  vapi_write(TX_CMD2 | control_rx); /* Set rx mode */
372
  recv_char ('c');
373
  vapi_write(TX_CMD1 | 3); /* Set incorrect tx/rx speed */
374
  send_char ('a');
375
  vapi_write(TX_CMD1 | 2); /* Set correct tx/rx speed */
376
  send_char ('b');
377
  MARK ();
378
 
379
#if COMPLETE
380
  recv_char ('*');
381
  control = TX_CMD0 | TX_NBITS;
382
  control_rx = TX_CMD0 | TX_NBITS | TX_STOPLEN;
383
  vapi_write(TX_CMD2 | control_rx); /* Set rx mode */
384
  recv_char ('*');
385
  MARK ();
386
#endif
387
}
388
 
389
int vapi_main ()
390
{
391
 
392
  /* Test section area */
393
  test_registers ();
394
  init_8n1 ();
395
  send_recv_test ();
396
  break_test();
397
  different_modes_test ();
398
  interrupt_test ();
399
  control_register_test ();
400
  line_error_test ();
401
  /* End of test section area */
402
 
403
  send_char ('@');
404
  return 0;
405
}

powered by: WebSVN 2.1.0

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