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

Subversion Repositories eco32

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

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