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

Subversion Repositories fade_ether_protocol

[/] [fade_ether_protocol/] [trunk/] [old_stable_version/] [linux/] [receiver2.c] - Blame information for rev 34

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 wzab
/*
2
 * fpga_l3_fade - driver for L3 communication protocol with FPGA based system
3
 * Copyright (C) 2012 by Wojciech M. Zabolotny
4
 * Institute of Electronic Systems, Warsaw University of Technology
5
 *
6
 *  This code is PUBLIC DOMAIN
7
 */
8
 
9
#include<termios.h>
10
#include <sys/types.h>
11
#include <sys/stat.h>
12
#include <sys/mman.h>
13
#include <sys/ioctl.h>
14
#include <fcntl.h>
15
#include <poll.h>
16
#include <unistd.h>
17
#include <stdio.h>
18
#include <strings.h>
19
#include <stdlib.h>
20
#include <stdint.h>
21
 
22
#include <sys/socket.h>
23
#include <linux/serial.h>
24
#include <sched.h>
25
#include "fpga_l3_fade.h"
26
#include <sys/time.h>
27
 
28
 
29 10 wzab
void main(int argc, char * argv[])
30 2 wzab
{
31 10 wzab
  int active[3]={0,0,0};
32 2 wzab
  struct l3_v1_buf_pointers bp;
33
  struct l3_v1_slave sl[3] = {
34
    {
35
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xef},
36
      .devname = "eth0"
37
    },
38
    {
39
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xe1},
40
      .devname = "eth0"
41
    },
42
    {
43
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xe2},
44
      .devname = "eth0"
45
    }
46
   };
47
  int i,j;
48
  int res;
49
  int blen[3];
50
  uint32_t data[3] = {0,0,0};
51
  long long total_len[3]={0,0,0};
52
  unsigned char * v[3];
53
  int frs[3]={-1,-1,-1};
54
  struct timeval tv;
55
  double tstart=0.0 , tend=0.0;
56
  int stop;
57
  struct sched_param s;
58
  s.sched_priority = 90;
59 10 wzab
  //Read active channels
60
  for(i=1;i<argc;i++) {
61
    int n = atoi(argv[i]);
62
    if ((n>=0) && (n<=2))
63
      active[n]=1;
64
  }
65 2 wzab
  printf("sched=%d\n",sched_setscheduler(0,SCHED_RR,&s));
66
  //Prepare all slaves to work
67
  for(i=0;i<3;i++) {
68
    char devname[30];
69
    sprintf(devname,"/dev/l3_fpga%d",i);
70
    frs[i]=open(devname,O_RDONLY);
71
    if(frs[i]<0) {
72
      printf("I can't open device %s\b",devname);
73
      perror("");
74
      exit(1);
75
    }
76
    //Get the length of the buffer
77
    blen[i] = ioctl(frs[i],L3_V1_IOC_GETBUFLEN,NULL);
78
    //Set the wakeup threshold
79
    res=ioctl(frs[i],L3_V1_IOC_SETWAKEUP,2000000);
80
    printf("length of buffer: %d, result of set wakeup: %d\n",blen[i],res);
81
    v[i]=(unsigned char *)mmap(0,blen[i],PROT_READ,MAP_PRIVATE,frs[i],0);
82
    if(!v[i]) {
83
      printf("mmap for device %s failed\n",devname);
84
      exit(1);
85
    }
86
  }
87
  //Start the transmission
88
  gettimeofday(&tv, NULL);
89
  tstart=tv.tv_sec+1.0e-6*tv.tv_usec;
90 10 wzab
  stop=tv.tv_sec+300;
91 2 wzab
  for(i=0;i<=2;i++) {
92 10 wzab
    if(active[i]) {
93
       res = ioctl(frs[i],L3_V1_IOC_STARTMAC,&sl[i]);
94
       printf("Result of start for slave %d : %d\n",i,res);
95
       }
96 2 wzab
  }
97
  int first_served=0;
98
  do{
99
    struct pollfd pfd[3] = {{.fd = frs[0], .events = POLLIN, .revents = 0},
100
                            {.fd = frs[1], .events = POLLIN, .revents = 0},
101
                            {.fd = frs[2], .events = POLLIN, .revents = 0},
102
                           };
103
    int ptr=0;
104
    int len=0;
105
    int pres;
106
    //Wait for data using "poll"
107
    pres = poll(pfd,3,-1);
108
    if(pres<0) {
109
      perror("Error in poll:");
110
      exit(1);
111
    }
112
    first_served = (first_served+1) %3; //Rotate priority of slaves
113
    for(j=0;j<3;j++) {
114
      i=(j+first_served) % 3;
115
      if(pfd[i].revents) {
116
        len = ioctl(frs[i],L3_V1_IOC_READPTRS,&bp);
117
        total_len[i] += len;
118
        printf("i=%d len=%d total=%lld head:%d tail: %d\n",i,len,total_len[i],bp.head, bp.tail);
119
        //OK. The data are read, let's analyze them
120
        while (bp.head != bp.tail)  {
121
          uint32_t c;
122
          c = *(uint32_t *)(v[i]+bp.tail);
123
          c = ntohl(c);
124
          bp.tail=(bp.tail+4) & (blen[i]-1); //Adjust tail pointer modulo blen[i]-1
125 10 wzab
          if (__builtin_expect((c != data[i]), 0)) {
126 2 wzab
            printf("Error! received: %8.8x expected: %8.8x \n",c,data[i]);
127
            exit(1);
128
          }
129
          data[i] ++;
130
        }
131
        ioctl(frs[i],L3_V1_IOC_WRITEPTRS,len);
132
      }
133
    }
134
    fflush(stdout);
135
    gettimeofday(&tv, NULL);
136
    if(tv.tv_sec > stop) {
137
      tend=tv.tv_sec+1.0e-6*tv.tv_usec;
138
      break;
139
    }
140
  } while (1);
141
  tend=tend-tstart;
142 10 wzab
  fprintf(stderr,"act0:%d act1:%d act2:%d\n",active[0],active[1],active[2]);
143 2 wzab
  for(i=0;i<3;i++) {
144 10 wzab
     fprintf(stderr,"total data %d=%lld time=%g throughput=%g [Mb/s]\n",i,total_len[i], tend, total_len[i]/tend*8.0);
145 2 wzab
  }
146
  for(i=0;i<=2;i++) {
147
    res = ioctl(frs[i],L3_V1_IOC_STOPMAC,0);
148
    munmap(v[i],blen[i]);
149
    close(frs[i]);
150
  }
151
 
152
}

powered by: WebSVN 2.1.0

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