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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [hwtests/] [serial/] [pc2fpga/] [send.c] - Rev 230

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

/*
 * send.c -- serial line test program
 */
 
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
 
 
#define NUM_TRIES	10
 
#define SYN		0x16
#define ACK		0x06
 
 
static FILE *diskFile = NULL;
static int sfd = 0;
static struct termios origOptions;
static struct termios currOptions;
 
 
void serialClose(void);
 
 
void error(char *fmt, ...) {
  va_list ap;
 
  va_start(ap, fmt);
  printf("Error: ");
  vprintf(fmt, ap);
  printf("\n");
  va_end(ap);
  if (diskFile != NULL) {
    fclose(diskFile);
    diskFile = NULL;
  }
  if (sfd != 0) {
    serialClose();
    sfd = 0;
  }
  exit(1);
}
 
 
void serialOpen(char *serialPort) {
  sfd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY);
  if (sfd == -1) {
    error("cannot open serial port '%s'", serialPort);
  }
  tcgetattr(sfd, &origOptions);
  currOptions = origOptions;
  cfsetispeed(&currOptions, B38400);
  cfsetospeed(&currOptions, B38400);
  currOptions.c_cflag |= (CLOCAL | CREAD);
  currOptions.c_cflag &= ~PARENB;
  currOptions.c_cflag &= ~CSTOPB;
  currOptions.c_cflag &= ~CSIZE;
  currOptions.c_cflag |= CS8;
  currOptions.c_cflag &= ~CRTSCTS;
  currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
  currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
  currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
  currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
  currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
  tcsetattr(sfd, TCSANOW, &currOptions);
}
 
 
void serialClose(void) {
  tcsetattr(sfd, TCSANOW, &origOptions);
  close(sfd);
}
 
 
int serialSnd(unsigned char b) {
  int n;
 
  n = write(sfd, &b, 1);
  return n == 1;
}
 
 
int serialRcv(unsigned char *bp) {
  int n;
 
  n = read(sfd, bp, 1);
  return n == 1;
}
 
 
int main(int argc, char *argv[]) {
  char *serialPort;
  unsigned char curr;
  int count;
 
  if (argc != 2) {
    printf("Usage: %s <serial port>\n", argv[0]);
    exit(1);
  }
  serialPort = argv[1];
  serialOpen(serialPort);
  curr = 0;
  count = 0;
  while (count < 100000) {
    while (!serialSnd(curr)) ;
    curr = (curr + 1) & 0xFF;
    count++;
  }
  printf("count = %d\n", count);
  while (!serialRcv(&curr)) ;
  printf("answer = %c\n", curr);
  if (sfd != 0) {
    serialClose();
    sfd = 0;
  }
  return 0;
}
 

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

powered by: WebSVN 2.1.0

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