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

Subversion Repositories eco32

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

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