-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
355 lines (284 loc) · 8.24 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <iostream>
#include "MyHeader.h"
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include "TCP_UDP.h"
#include "memregisters.h"
#include "memutils.h"
#include "algorithm"
using namespace std;
static void* ProcessingThread1(void* lpParam);
uint32_t recvCnt=0;
uint32_t sendCnt=0;
timeval GetLocalTime(void)
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
return tv;
}
mem_dev regs;
void *mappedCmd;
void InitOLED()
{
regs.base_addr = XPAR_MEMORY_MAPPER_0_S00_AXI_BASEADDR;
regs.high_addr = XPAR_MEMORY_MAPPER_0_S00_AXI_HIGHADDR;
regs.dev_id = 0;
regs.n_regs = 20;
regs.reg_offset = 4;
mappedCmd = MemoryMapping(regs.base_addr,regs.high_addr);
printf("Mapping completed...\n");
//OLED initialization
mWriteReg((u32)mappedCmd,64,1);
}
void UnmapOLED()
{
munmap((void*)mappedCmd, regs.high_addr-regs.base_addr);
printf("Unmapped...\n");
}
/*
write 16Bytes(4 registers) per line
input:
pStr: string
len: string length
output:
n>0: register writed number
*/
int WriteReg_OLED(const char* pStr, int len)
{
if(len == 0)
{
return 0;
}
else if(len > 64)
len = 64;
int len4 = len - len%4;
u32 val = 0;
for(int i=0;i<len4;i++)
{
val |= pStr[i]<<((i%4)<<3);// x<<0-8-16-24
if((i+1)%4 == 0 && i>0)
{
mWriteReg((u32)mappedCmd,i-3,val);
val = 0;
}
}
if(len%4)
{
char len4bottom[4] = {0};
int lenLeft = len -len4;
for(int j=0;j<lenLeft;j++)
len4bottom[j] = pStr[len4+j];
u32 val = 0;
val |= len4bottom[0];
val |= len4bottom[1]<<8;
val |= len4bottom[2]<<16;
val |= len4bottom[3]<<24;
mWriteReg((u32)mappedCmd,len4,val);
}
return len4/4+1;
}
void readReg_OLED(char* pStrOut,int RegCnt)
{
uint32_t ret[8];
for(int i=0;i<RegCnt;i++)
{
ret[i] = mReadReg((u32)mappedCmd, i*4);
pStrOut[i*4+0] = ret[i]&0xff;
pStrOut[i*4+1] = (ret[i]>>8)&0xff;
pStrOut[i*4+2] = (ret[i]>>16)&0xff;
pStrOut[i*4+3] = (ret[i]>>24)&0xff;
}
}
int main()
{
//1 设置printf缓冲区为0
setvbuf(stdout,(char*)NULL,_IONBF,0);
CTCP_UDP tcp_udp;
cout << "input <1 port> : udp server" << endl;
cout << "input <2 address port MB> : udp client" << endl;
cout << "input <3 port> : tcp server" << endl;
cout << "input <4 address port MB> : tcp client" << endl;
cout << "input <5 string times> : OLED test" << endl;
cout << "input <6 port> : tcp_axi server" << endl;
cout << "input <7 address port MB> : tcp_axi client" << endl;
cout << "input <10> : axi_interrupt test" << endl;
cout<<endl;
int i=0,port=0,MB=0;
string straddr;
struct timeval tv1,tv2;
cin>>i;
BYTE recvBuf[4096];
BYTE sendBuf[4096];
memset(recvBuf,0,4096);
memset(sendBuf,0xff,4096);
//create monitor thread
pthread_t ProcessID1;
//udp Server------------------------------------------------------------------
if(1 == i)
{
cin>>port;
tcp_udp.BindServerUDP(port);
pthread_create(&ProcessID1,NULL,ProcessingThread1,&tcp_udp);//monitor thread
while(1)
{
recvCnt += tcp_udp.RecvUDP(recvBuf);
}
}
//udp client------------------------------------------------------------------
else if(2 == i)
{
cin>>straddr;
cin>>port;
cin>>MB;
uint64_t sendCnt = 0;
int j=MB*500;
tv1 = GetLocalTime();
while(j--)
{
sendCnt += tcp_udp.SendUDP(straddr.c_str(),port,sendBuf,2000);
}
tv2 = GetLocalTime();
float timediff = (tv2.tv_sec - tv1.tv_sec) + (tv2.tv_usec - tv1.tv_usec)*1.0/1000000;
cout<<endl;
cout<<"Send Size(MB/MB):"<<sendCnt/1000000<<"/"<< MB <<endl;
cout<<"Send Time(s):"<<timediff<<endl;
cout<<"Send Speed(MB/s):"<<MB*1.0/timediff<<endl;
}
//tcp server------------------------------------------------------------------
else if(3 == i)
{
cin>>port;
pthread_create(&ProcessID1,NULL,ProcessingThread1,&tcp_udp);//monitor thread
//tcp_udp.BindServerTCP("127.0.0.1",port);
tcp_udp.BindServerTCP(port);
if(-1 == tcp_udp.ListenTCP())
return 0;
int tmpRecvCnt=0;
while(1)
{
if(!tcp_udp.GetIsTcpConnected())
if(-1 == tcp_udp.AcceptTCP())
continue;
tmpRecvCnt = tcp_udp.RecvTCP(recvBuf);
if(-1 != tmpRecvCnt || 0 != tmpRecvCnt)
recvCnt += tmpRecvCnt;
}
}
//tcp client------------------------------------------------------------------
else if(4 == i)
{
cin>>straddr;
cin>>port;
cin>>MB;
int j=MB*500;
uint64_t sendCnt = 0;
if(-1 == tcp_udp.ConnectTCP(straddr.c_str(),port))
{
cout<<"connect failed";
return 0;
}
//time delay is neccessary!!!
sleep(1);
tv1 = GetLocalTime();
while(j--)
{
sendCnt += tcp_udp.SendTCP(sendBuf,2000);
}
tv2 = GetLocalTime();
float timediff = (tv2.tv_sec - tv1.tv_sec) + (tv2.tv_usec - tv1.tv_usec)*1.0/1000000;
cout<<endl;
cout<<"Send Size(MB/MB):"<<sendCnt/1000000<<"/"<< MB <<endl;
cout<<"Send Time(s):"<<timediff<<endl;
cout<<"Send Speed(MB/s):"<<MB*1.0/timediff<<endl;
}
//oled test------------------------------------------------------------------
else if(5 == i)
{
InitOLED();
cin>>straddr;
int cnt;
cin>>cnt;
for(int i=0;i<cnt;i++)
WriteReg_OLED(straddr.c_str(),strlen(straddr.c_str()));
UnmapOLED();
}
//tcp_axi server ------------------------------------------------------------------
else if(6 == i)
{
cin>>port;
pthread_create(&ProcessID1,NULL,ProcessingThread1,&tcp_udp);//monitor thread
tcp_udp.BindServerTCP(port);
if(-1 == tcp_udp.ListenTCP())
return 0;
int tmpRecvCnt=0;
while(1)
{
if(!tcp_udp.GetIsTcpConnected())
if(-1 == tcp_udp.AcceptTCP())
continue;
tmpRecvCnt = tcp_udp.RecvTCP(recvBuf);
if(-1 != tmpRecvCnt || 0 != tmpRecvCnt)
{
recvCnt += tmpRecvCnt;
//write 32 bytes to oled register
}
}
}
//tcp_axi client------------------------------------------------------------------
else if(7 == i)
{
cin>>straddr;
cin>>port;
cin>>MB;
int j=MB*500;
uint64_t sendCnt = 0;
if(-1 == tcp_udp.ConnectTCP(straddr.c_str(),port))
{
cout<<"connect failed";
return 0;
}
//time delay is neccessary!!!
sleep(1);
tv1 = GetLocalTime();
while(j--)
{
sendCnt += tcp_udp.SendTCP(sendBuf,2000);
}
tv2 = GetLocalTime();
float timediff = (tv2.tv_sec - tv1.tv_sec) + (tv2.tv_usec - tv1.tv_usec)*1.0/1000000;
cout<<endl;
cout<<"Send Size(MB/MB):"<<sendCnt/1000000<<"/"<< MB <<endl;
cout<<"Send Time(s):"<<timediff<<endl;
cout<<"Send Speed(MB/s):"<<MB*1.0/timediff<<endl;
}
return 0;
}
void* ProcessingThread1(void* lpParam)//
{
uint i=0;
CTCP_UDP* ptcp_udp = (CTCP_UDP*)lpParam;
while(1)
{
//check tcp connect state per 20ms
if(1 == (i)%5)
{
if(0 == ptcp_udp->CheckTcpConnection())
{
cout<<".";
}
}
// print the receive speed per 1s
if(0 == (i)%10)
{
cout<<"\r";
cout<<"receive speed:"<<recvCnt*1.0/1000000<<"MB/s ";
recvCnt = 0;
}
++i;
usleep(100000);
}
return 0;
}