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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [hwtests/] [serial/] [fpga2pc/] [receive.c] - Blame information for rev 132

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 hellwig
/*
2
 * receive.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
 
26
 
27
void serialClose(void);
28
 
29
 
30
void error(char *fmt, ...) {
31
  va_list ap;
32
 
33
  va_start(ap, fmt);
34
  printf("Error: ");
35
  vprintf(fmt, ap);
36
  printf("\n");
37
  va_end(ap);
38
  if (diskFile != NULL) {
39
    fclose(diskFile);
40
    diskFile = NULL;
41
  }
42
  if (sfd != 0) {
43
    serialClose();
44
    sfd = 0;
45
  }
46
  exit(1);
47
}
48
 
49
 
50 132 hellwig
void serialOpen(char *serialPort) {
51
  sfd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY);
52 14 hellwig
  if (sfd == -1) {
53 132 hellwig
    error("cannot open serial port '%s'", serialPort);
54 14 hellwig
  }
55
  tcgetattr(sfd, &origOptions);
56
  currOptions = origOptions;
57
  cfsetispeed(&currOptions, B38400);
58
  cfsetospeed(&currOptions, B38400);
59
  currOptions.c_cflag |= (CLOCAL | CREAD);
60
  currOptions.c_cflag &= ~PARENB;
61
  currOptions.c_cflag &= ~CSTOPB;
62
  currOptions.c_cflag &= ~CSIZE;
63
  currOptions.c_cflag |= CS8;
64
  currOptions.c_cflag &= ~CRTSCTS;
65
  currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
66
  currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
67
  currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
68
  currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
69
  currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
70
  tcsetattr(sfd, TCSANOW, &currOptions);
71
}
72
 
73
 
74
void serialClose(void) {
75
  tcsetattr(sfd, TCSANOW, &origOptions);
76
  close(sfd);
77
}
78
 
79
 
80
int serialSnd(unsigned char b) {
81
  int n;
82
 
83
  n = write(sfd, &b, 1);
84
  return n == 1;
85
}
86
 
87
 
88
int serialRcv(unsigned char *bp) {
89
  int n;
90
 
91
  n = read(sfd, bp, 1);
92
  return n == 1;
93
}
94
 
95
 
96
int main(int argc, char *argv[]) {
97 132 hellwig
  char *serialPort;
98 14 hellwig
  unsigned char prev, curr;
99
  int count, errors;
100
 
101 132 hellwig
  if (argc != 2) {
102
    printf("Usage: %s <serial port>\n", argv[0]);
103 14 hellwig
    exit(1);
104
  }
105 132 hellwig
  serialPort = argv[1];
106
  serialOpen(serialPort);
107 14 hellwig
  count = 0;
108
  errors = 0;
109
  while (!serialRcv(&prev)) ;
110
  count++;
111
  while (count < 100000) {
112
    while (!serialRcv(&curr)) ;
113
    count++;
114
    if (((prev + 1) & 0xFF) != curr) {
115
      errors++;
116
    }
117
    prev = curr;
118
  }
119
  if (sfd != 0) {
120
    serialClose();
121
    sfd = 0;
122
  }
123
  printf("count = %d (ok = %d, errors = %d)\n",
124
         count, count - errors, errors);
125
  return 0;
126
}

powered by: WebSVN 2.1.0

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