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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [hwtests/] [serial/] [sertest/] [sertest.c] - Blame information for rev 262

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

Line No. Rev Author Line
1 14 hellwig
/*
2
 * sertest.c -- serial line test program
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
#include <fcntl.h>
11
#include <unistd.h>
12
#include <termios.h>
13
 
14
 
15
#define NUM_TRIES       10
16
 
17
#define SYN             0x16
18
#define ACK             0x06
19
 
20
 
21
static FILE *diskFile = NULL;
22
static int sfd = 0;
23
static struct termios origOptions;
24
static struct termios currOptions;
25
static int errors;
26
 
27
 
28
void serialClose(void);
29
 
30
 
31
void error(char *fmt, ...) {
32
  va_list ap;
33
 
34
  va_start(ap, fmt);
35
  printf("Error: ");
36
  vprintf(fmt, ap);
37
  printf("\n");
38
  va_end(ap);
39
  if (diskFile != NULL) {
40
    fclose(diskFile);
41
    diskFile = NULL;
42
  }
43
  if (sfd != 0) {
44
    serialClose();
45
    sfd = 0;
46
  }
47
  exit(1);
48
}
49
 
50
 
51 132 hellwig
void serialOpen(char *serialPort) {
52
  sfd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY);
53 14 hellwig
  if (sfd == -1) {
54 132 hellwig
    error("cannot open serial port '%s'", serialPort);
55 14 hellwig
  }
56
  tcgetattr(sfd, &origOptions);
57
  currOptions = origOptions;
58
  cfsetispeed(&currOptions, B38400);
59
  cfsetospeed(&currOptions, B38400);
60
  currOptions.c_cflag |= (CLOCAL | CREAD);
61
  currOptions.c_cflag &= ~PARENB;
62
  currOptions.c_cflag &= ~CSTOPB;
63
  currOptions.c_cflag &= ~CSIZE;
64
  currOptions.c_cflag |= CS8;
65
  currOptions.c_cflag &= ~CRTSCTS;
66
  currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
67
  currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
68
  currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
69
  currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
70
  currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
71
  tcsetattr(sfd, TCSANOW, &currOptions);
72
}
73
 
74
 
75
void serialClose(void) {
76
  tcsetattr(sfd, TCSANOW, &origOptions);
77
  close(sfd);
78
}
79
 
80
 
81
int serialSnd(unsigned char b) {
82
  int n;
83
 
84
  n = write(sfd, &b, 1);
85
  return n == 1;
86
}
87
 
88
 
89
int serialRcv(unsigned char *bp) {
90
  int n;
91
 
92
  n = read(sfd, bp, 1);
93
  return n == 1;
94
}
95
 
96
 
97
void block(void) {
98
  unsigned char src[1000];
99
  unsigned char dst[1000];
100
  unsigned char *p, *q;
101
  int i;
102
 
103
  for (i = 0; i < 1000; i++) {
104
    src[i] = rand();
105
  }
106
  p = src;
107
  q = dst;
108
  while (1) {
109
    if (p != &src[1000] && serialSnd(*p)) {
110
      p++;
111
    }
112
    if (q != &dst[1000] && serialRcv(q)) {
113
      q++;
114
    }
115
    if (q == &dst[1000]) {
116
      break;
117
    }
118
  }
119
  for (i = 0; i < 1000; i++) {
120
    if (((src[i] + 0x5C) & 0xFF) != dst[i]) {
121
      errors++;
122
    }
123
  }
124
}
125
 
126
 
127
int main(int argc, char *argv[]) {
128 132 hellwig
  char *serialPort;
129 14 hellwig
  int i;
130
 
131 132 hellwig
  if (argc != 2) {
132
    printf("Usage: %s <serial port>\n", argv[0]);
133 14 hellwig
    exit(1);
134
  }
135 132 hellwig
  serialPort = argv[1];
136
  serialOpen(serialPort);
137 14 hellwig
  errors = 0;
138
  for (i = 1; i <= 100; i++) {
139
    block();
140
    if (i % 10 == 0) {
141
      printf("%d bytes, errors = %d\n", i * 1000, errors);
142
    }
143
  }
144
  if (sfd != 0) {
145
    serialClose();
146
    sfd = 0;
147
  }
148
  return 0;
149
}

powered by: WebSVN 2.1.0

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