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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [hwtests/] [serial/] [pc2fpga/] [send.c] - Blame information for rev 145

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

Line No. Rev Author Line
1 14 hellwig
/*
2
 * send.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 curr;
99
  int count;
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
  curr = 0;
108
  count = 0;
109
  while (count < 100000) {
110
    while (!serialSnd(curr)) ;
111
    curr = (curr + 1) & 0xFF;
112
    count++;
113
  }
114
  printf("count = %d\n", count);
115
  while (!serialRcv(&curr)) ;
116
  printf("answer = %c\n", curr);
117
  if (sfd != 0) {
118
    serialClose();
119
    sfd = 0;
120
  }
121
  return 0;
122
}

powered by: WebSVN 2.1.0

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