Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
291 f9daq 1
#include "sender.h"
2
#define binary(bit) strtol(bit,NULL,2)
3
 
4
void SenderInit(){};
5
 
6
void SenderClose(){};
7
 
8
//------------------------------------------------------------------------------------------------------
9
// Send 1 data & address by UDP                                                                                                                                                  
10
void Senderudp_send( unsigned int address, int data){// data: send only 1 data length
11
  SiTCPRBCPskeleton(0xff, 0x80, address, 0x1, address);
12
  SiTCPsndData[0] = data;
13
 
14
  memcpy(SiTCPsndBuf+sizeof(struct bcp_header), SiTCPsndData, sizeof SiTCPsndData);
15
  int len = 0;
16
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
17
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
18
  if(len < 0){
19
    perror("UDP send error");
20
    printf("exit(1);!!!\n");
21
}
22
   SiTCPrcvRBCP_ACK(1);
23
}
24
 
25
//------------------------------------------------------------------------------------------------------  
26
// Recv 1 data & address by UDP
27
 
28
int Senderudp_recv( unsigned int address, int data){// data: send only 1 data length
29
 
30
    SiTCPRBCPskeleton(0xff, 0xc0, address, 0x1, address);
31
 
32
  int len = 0;
33
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
34
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
35
  if(len < 0){
36
    perror("UDP recv error");
37
    printf("exit(1);!!!\n");
38
  }
39
 
40
  int rd_data = 0;
41
  rd_data =  SiTCPrcvRBCP_ACK(1);
42
  // std::cout << rd_data << std::endl;
43
 
44
  return rd_data;
45
}
46
 
47
//------------------------------------------------------------------------------------------------------ 
48
// Send 1 packet data & address by UDP
49
 
50
void SenderRBCPpacket_send( unsigned int address,
51
                             unsigned char length, int* data){
52
 
53
  printf("enter packet function\n");
54
 
55
  SiTCPRBCPskeleton(0xff, 0x80, address, length, address);
56
 
57
  for(int i=0; i<256; ++i){
58
    SiTCPsndData[i] = data[i];// <- sndData: 0~255
59
  }
60
 
61
  memcpy(SiTCPsndBuf+sizeof(struct bcp_header), SiTCPsndData, sizeof SiTCPsndData);
62
 
63
  int len = 0;
64
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, sizeof SiTCPsndData + sizeof(struct bcp_header), 0,
65
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
66
 
67
  if(len < 0){
68
    perror("UDP send error");
69
    printf("exit(1);!!!\n");
70
  }
71
  SiTCPrcvRBCP_ACK(0);
72
}
73
 
74
//------------------------------------------------------------------------------------------------------ 
75
// Recv 1 packet data & address by UDP
76
 
77
void SenderRBCPpacket_recv( unsigned int address,
78
                             unsigned char length, int* data){
79
 
80
  printf("enter packet function\n");
81
 
82
  SiTCPRBCPskeleton(0xff, 0xc0, address, length, address);
83
 
84
 
85
  int len = 0;
86
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, sizeof SiTCPsndData + sizeof(struct bcp_header), 0,
87
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
88
 
89
  if(len < 0){
90
    perror("UDP recv error");
91
    printf("exit(1);!!!\n");
92
  }
93
  SiTCPrcvRBCP_ACK(1);
94
}
95
//------------------------------------------------------------------------------------------------------   
96
//=============== multi packet sending method ===============
97
 
98
void SenderRBCP_multi_packet_send( unsigned int address,
99
                                    unsigned int total_length, int* data){
100
 
101
  unsigned int total_packet = ((total_length/255)+1)*2;
102
  unsigned int packetNo = 0;
103
 
104
  printf("------------------ RBCP : register access ------------------\n");    
105
  printf("\n");    
106
  printf("Start sending packet data");    
107
 
108
  for(packetNo=0; packetNo<total_packet; ++packetNo){
109
 
110
    //    SiTCPrcvRBCP_ACK(0);
111
    SiTCPRBCPskeleton(0xff, 0x80, address+(packetNo*255), 0xff, address+(packetNo*255));
112
 
113
    for(int i=0; i<255; ++i){
114
      SiTCPsndData[i] = data[i+(packetNo*255)];// <- sndData: 0~255
115
    }
116
 
117
    memcpy(SiTCPsndBuf+sizeof(struct bcp_header), SiTCPsndData, sizeof SiTCPsndData);
118
 
119
    int len = 0;
120
    len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, sizeof SiTCPsndData + sizeof(struct bcp_header), 0,
121
                 (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
122
 
123
    if(len < 0){
124
      perror("Send DAQ Start Signal");
125
      printf("exit(1);!!!\n");
126
    }
127
 
128
    SiTCPrcvRBCP_ACK(0);
129
    //    std::cout << SiTCPrcvRBCP_ACK(1) << std::endl;
130
 
131
        printf(".");  
132
  }
133
  printf("Done!!!\n");
134
  printf("\n");
135
 
136
  printf("Number of sent packets = %d\n", packetNo);
137
  printf("\n");
138
 
139
}
140
//------------------------------------------------------------------------------------------------------          
141
//--------------------EASIROC MADC sender---------------------------
142
int Senderread_madc( int data){// data: send only 1 data length
143
 
144
 //Set ADC ch to FPGA reg
145
  SiTCPRBCPskeleton(0xff, 0x80, 0x00000012, 0x1, 0x00000012);
146
  SiTCPsndData[0] = data;
147
 
148
  memcpy(SiTCPsndBuf+sizeof(struct bcp_header), SiTCPsndData, sizeof SiTCPsndData);
149
  int len = 0;
150
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
151
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
152
  if(len < 0){
153
    perror("UDP Send error");
154
    printf("exit(1);!!!\n");
155
  }
156
  SiTCPrcvRBCP_ACK(1);
157
 
158
  SiTCPRBCPskeleton(0xff, 0x80, 0x0000001f, 0x1, 0x0000001f);
159
  SiTCPsndData[0] = 1;
160
 
161
  memcpy(SiTCPsndBuf+sizeof(struct bcp_header), SiTCPsndData, sizeof SiTCPsndData);
162
  len = 0;
163
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
164
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
165
  if(len < 0){
166
    perror("UDP Send error");
167
    printf("exit(1);!!!\n");
168
  }
169
  SiTCPrcvRBCP_ACK(1);
170
 
171
  //Sleep(20000);//wait ADC ch change
172
  Sleep(2000);//wait ADC ch change
173
 
174
  //start Read ADC
175
  SiTCPRBCPskeleton(0xff, 0x80, 0x00000012, 0x1, 0x00000012);
176
  SiTCPsndData[0] = 240;
177
 
178
  memcpy(SiTCPsndBuf+sizeof(struct bcp_header), SiTCPsndData, sizeof SiTCPsndData);
179
  len = 0;
180
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
181
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
182
  if(len < 0){
183
    perror("UDP Send error");
184
    printf("exit(1);!!!\n");
185
  }
186
  SiTCPrcvRBCP_ACK(1);
187
 
188
  SiTCPRBCPskeleton(0xff, 0x80, 0x0000001f, 0x1, 0x0000001f);
189
  SiTCPsndData[0] = 0;
190
 
191
  memcpy(SiTCPsndBuf+sizeof(struct bcp_header), SiTCPsndData, sizeof SiTCPsndData);
192
  len = 0;
193
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
194
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
195
  if(len < 0){
196
    perror("UDP Send error");
197
    printf("exit(1);!!!\n");
198
  }
199
  SiTCPrcvRBCP_ACK(1);
200
 
201
  //read data
202
  int rd_data = 0;
203
  int rd_data1 = 0;
204
  int rd_data2 = 0;
205
  SiTCPRBCPskeleton(0xff, 0xc0, 0x00000004, 0x1, 0x00000004);
206
  len = 0;
207
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
208
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
209
  if(len < 0){
210
    perror("UDP recv error");
211
    printf("exit(1);!!!\n");
212
  }
213
  rd_data1 =  SiTCPrcvRBCP_ACK(1);
214
  // std::cout << rd_data << std::endl;
215
  SiTCPRBCPskeleton(0xff, 0xc0, 0x00000005, 0x1, 0x00000005);
216
  len = 0;
217
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
218
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
219
  if(len < 0){
220
    perror("UDP recv error");
221
    printf("exit(1);!!!\n");
222
  }
223
  rd_data2 =  SiTCPrcvRBCP_ACK(1);
224
  // std::cout << rd_data << std::endl;
225
  rd_data = rd_data1*256 + rd_data2;
226
 
227
  return rd_data;
228
 
229
}
230
 
231
 
232
 
233
//------------------------------------------------------------------------------------------------------ 
234
void Senderclear_all(){
235
  unsigned char address;
236
  int data;
237
  //int nbyte;
238
 
239
  // Send register clear
240
  address = 0x00;
241
  data = 0x0;
242
  Senderudp_send(address, data);
243
 
244
  printf("Clear all registers in FPGA.\n");
245
}
246
 
247