19
Method of High-Speed Data Acquisition and Continuous Data Transfer using Altera R Stratix R II EP2S60 DSP Development Board Qian Liu and S.W. Ellingson April 27, 2009 Contents 1 Introduction 2 2 Basic Architecture 2 3 System Development 2 3.1 Nios II hardware development .......................... 2 3.2 Nios II software development ........................... 4 3.2.1 Initialization task ............................. 5 3.2.2 Application task ............................. 5 3.3 Host PC Post-Processing ............................. 5 4 Networking Performance Optimization 5 5 System Performance Test 7 5.1 Throughput Rate ................................. 7 5.2 Transfer Reliability ................................ 7 6 User Manual 7 6.1 Run the Project .................................. 7 6.2 Flash Program .................................. 8 6.3 Recompile the Project .............................. 8 A ADC Interface Module 10 B Nios II Application Software 16 C Server Application Software 17 D Data Processing Program 19 1

Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

Method of High-Speed Data Acquisition andContinuous Data Transfer using AlteraR© StratixR© II

EP2S60 DSP Development Board

Qian Liu and S.W. Ellingson

April 27, 2009

Contents

1 Introduction 2

2 Basic Architecture 2

3 System Development 23.1 Nios II hardware development . . . . . . . . . . . . . . . . . . . . . . . . . . 23.2 Nios II software development . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

3.2.1 Initialization task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53.2.2 Application task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3.3 Host PC Post-Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

4 Networking Performance Optimization 5

5 System Performance Test 75.1 Throughput Rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75.2 Transfer Reliability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

6 User Manual 76.1 Run the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76.2 Flash Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86.3 Recompile the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

A ADC Interface Module 10

B Nios II Application Software 16

C Server Application Software 17

D Data Processing Program 19

1

Page 2: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

1 Introduction

Ethernet has become a standard data transport paradigm for embedded systems in manyapplications since the transport technology is cheap, abundant, mature and reliable. Thisreport has implemented an AlteraR© Nios R© II based system for data acquisition and transferover Ethernet using user datagram protocol (UDP). The network client (the data source)is implemented in FPGA using Verilog HDL and C languages; and the network server (thedata recorder) is running on the host PC using Python code.

The goal is to be capable of transferring continuous data at high throughput rate, wherecontinuous here means that no data will be lost during the sampling and transfer process.The designed system has a throughput rate of 80 Mb/s; it acquires 8-bit samples at the rateof 10 MSPS.

2 Basic Architecture

The system is built on the Altera R© StratixR© II EP2S60 DSP development kit, as shownin Figure 1. The on-board 12-bit analog-to-digital converter (ADC) AD9433 is used for thedata sampling; while the 10/100 single chip PHY/MAC SMSC LAN91C111 is used for theUDP communication between the development board and the host PC.

Figure 2 demonstrates the basic architecture of the system, consisting of ADC, FPGA,and Ethernet controller. The Nios II processor runs the MicroC/OS-II real-time operatingsystem (RTOS) and provides the drivers for the devices. The ADC interface controls thedata sampling, and the Ethernet interface is related with the Ethernet controller.

It is also shown in Figure 2 that the ADC interface is implemented in Verilog HDL. TheNios II/f processor and Ethernet interface are instantiated in SOPC Builder and configuredin the Nios II IDE. The Python code runs on the host PC.

3 System Development

3.1 Nios II hardware development

As an Altera R© Nios R© II based system, Quartus R© II software can be used to create andprocess the Nios II system design that interfaces with components on development board [1].The designed system includes the following components:

Nios II/f core with 64 Kbytes of instruction/data cache16 Kbytes on-chip memory256 Kbytes SRAM32 Mbytes SDRAM

2

Page 3: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

ADC

Input

SMA

Connector

Ethernet

Connector

LAN91C111

AD9433

EP2S60F1020C3

Stratix II FPGA

Figure 1: Image of Stratix II EP2S60 DSP Development Board.a

ahttp://www.altera.com/products/devkits/altera/kit-dsp-2S60.html

Figure 2: Block diagram of the system architecture.

3

Page 4: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

16 Mbytes Flash memoryADC interfaceEthernet interfaceTimerJTAG UARTSystem ID component

All the components except the ADC interface – composed of a data width bridge, aswing buffer and a cross clock bridge – can be configured as intellectual property (IP) coresusing Altera’s MegaWizard software. The Nios II/f core, the on-chip memory, and the timerare used for the RTOS. The JTAG UART is used for the USB-BlasterTM download cableto configure the Stratix II FPGA directly using an SRAM Object File (.sof). The SRAMand the SDRAM stores the executable code, function call parameters and temporary data;while the Flash memory stores the firmware for bootload. The Ethernet interface makes thecontrol registers and data registers of the SMSC LAN91C111 accessible through applicationprogramming interface (API).

Both the data width bridge and the swing buffer of the ADC interface use the a finitestate machine (FSM). The data width bridge converts 8-bit data to 32 bits, matching withthe width of the Nios II processor’s 32-bit data bus. The swing buffer is used to preventunderrun and overflow conditions. When one FIFO is in write operation, the other one isin read operation; then the two FIFOs swap the operation. The cross clock bridge is anasynchronous FIFO, interfacing two clock domains. The details about the implementationof the ADC interface are listed in Appendix A.

3.2 Nios II software development

The prototype for the Nios II application was the Nios II C/C++ application projecttemplate simple socket server, using the NicheStack TCP/IP Stack on the basis of the RTOSmultithreaded environment [2]. The NicheStack TCP/IP Stack provides the interrupt-baseddriver support for the SMSC LAN91C111 MAC/PHY device. Our application has modifiedthe C design file simple socket server.c, which is detailed in Appendix B, to implement theUDP communication between the FPGA (as a client) and the host PC (as a server).

There are two fundamental tasks in our application: the SSSInitialTask instantiatesall of the RTOS resources; and the SSSSimpleSocketServerTask manages the socket serverconnection and calls UDP socket routines to transfer data. The SSSInitialTask should havethe higher priority to ensure that the application code does not attempt further initializationuntil the RTOS is running and I/O drivers are available [2].

Note that the IP address of where the data would like to be sent to and the port numbershould be assigned in client program. As illustrated in Appendix B, the IP address of thehost PC in our application is 192.168.1.213, and port 1739 has been used.

4

Page 5: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

3.2.1 Initialization task

The SSSInitialTask initializes the NicheStack TCP/IP Stack at the beginning. It callsthe alt iniche init function to initialize the stack for use with the RTOS and the netmainfunction to start the iniche-specific network tasks as well as initialize the network devices.When the global variable iniche net ready is set to a non-zero value, the NicheStack stackhas completed initialization. It is time to perform the application initialization steps, suchas the initialization of the SSSSimpleSocketServerTask.

3.2.2 Application task

The SSSSimpleSocketServerTask includes the code for the network client and continuesforever after that it first establishes a UDP socket. It sends the data located in the swingbuffer to the server (host PC) using the sendto function continuously. As UDP is a connec-tionless protocol, the client (DSP development board) does not establish a connection withthe server [3].

In order to have the maximum throughput rate, this application task does nothing butsends the data generated from the ADC.

3.3 Host PC Post-Processing

On the host PC, Python code that has been detailed in Appendix C implements thenetwork server using UDP. For a UDP server, Python code creates a socket, sets the socketoptions and binds the IP address with the port number. Both the IP address and portnumber of the host PC should be the same as those assigned in the Nios II applicationsoftware. Then it is ready to receive the data using the recvfrom function. Though UDPis connectionless, it can solve the problem of lost packets in local area network (LAN) withappropriate size of the receive buffer. Once a packet has been received successfully, the datawould be written into a binary file.

4 Networking Performance Optimization

There are many factors affecting the total throughput of an embedded networking system,such as the user application, networking stack, Ethernet device and its driver, as well as thephysical connection for the networking link [4]. Therefore, there are several effective methodsto optimize the application to relieve the load of the Nios II CPU. We configured the softwareand hardware settings as follows.

The socket buffer size for the Socket’s sendto and recvfrom functions should be set largeenough to alleviate the system call overhead. Here, we allocate a 1 MB for the socket buffer.

5

Page 6: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

Nios II compiler optimization level: -O3SSSInitialTASK priority: 1InetMainTask priority: 2ClockTickTask priority: 3SSSSimpleSocketServerTask priority: 4Computational efficiency: Nios II/f coreInstruction/Data Caches: 64 KBClock frequency: 150 MHzData packet size: 1468 bytesSocket buffer size: 1 MB

The maximum packet size depends on the maximum transmit unit (MTU), which is fixedby standards (as is the case with Ethernet) or decided at connect time (as is usually the casewith point-to-point serial links). For Ethernet at the network layer, the MTU is normally1500 bytes; therefore, the maximum data payload is 1472 bytes considering the IP headerand UDP header. However, with the increase of data packet size, the following experimentshows that the highest throughput rate increases to a threshold and then decreases as shownin Figure 3. The optimum data payload is determined as 1468 bytes by comparing thethroughput rate versus different data payload, resulting in a throughput rate of 80 Mb/s for100baseT (100 Mb/s) Ethernet.

1465 1466 1467 1468 1469 1470 1471 147264

66

68

70

72

74

76

78

80

82

Data Packet Size (Bytes)

Thr

ough

put R

ate

(Mbp

s)

Figure 3: Data packet size vs. throughput rate over 100baseT Ethernet.

6

Page 7: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

5 System Performance Test

5.1 Throughput Rate

To make a good approximation of the maximum throughput achievable, we did a testin which the application did very little apart from sending or receiving data through thenetworking stack and calculates the “raw” throughput rate of UDP data transaction. Aver-aging over a large amount of data, we find that the throughput rate is about 80 Mb/s whenthe SMSC LAN91C111 is configured in 100baseT mode.

5.2 Transfer Reliability

To test data integrity, a MATLAB program is used to read the data from the recorded file.After sending 8000 packets of 1468 8-bit samples, the received data are plotted in Figure 4.Figure 4 shows that the received data are recovered without fracture; that is, the data canbe transferred continuously.

−0.5 0 0.5−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Time

Am

plitu

de

Eye Diagram

Figure 4: Eye diagram of ramp signal.

6 User Manual

6.1 Run the Project

The steps to run the project in FPGA through JTAG cable are as follows:

1. Download Hardware Design (standard.sof) to Target FPGA using the Programmer inQuartus II software.

7

Page 8: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

2. Run the Program using Nios II IDE Tool(1) Rigth-click the simple socket server project, point to Run As, and then click

Nios II Hardware. The IDE downloads the program to the FPGA on the target board andstarts execution.

(2) Click Terminate (the red square) on the toolbar at the upper-right hand cornerof the Console view to terminate the run session. At this time, the IDE disconnects fromthe target hardware and leaves the Nios II processor running.

3. Run the Host PC program (Python Code).4. Run the Matlab Code for data post-processing.

6.2 Flash Program

Both the hardware design (standard.sof) and the software project (simple socket server.prj)can be downloaded into the flash memory using Flash Programmer in Nios IDE as the fol-lowing instruction.

1. Download Hardware Design (standard.sof) to Target FPGA using the Programmer inQuartus II software.

2. Click Flash Programmer from the Tools menu in Nios IDE. If reusing an existingflash configuration, click Load JDI File. If it is the first time to program flash, complete thefollowing steps:

(1) Select Flash Programmer at the left side of the dialog box.(2) Click the New launch configuration button in the upper left corner of the flash

programmer window.3. Check the box titled Program software project into flash memory ; check the box titled

Program FPGA configuration data into hardware image region of flash memory. In theFPGA Configuration (SOF) field, type or browse to standard.sof. In the Hardware Imagefield, select the preset location at which you wish to program the .sof file, or select Custom.For example, we choose Custom here and specify the memory name as ext flash and offsetas 0x800000. Also check the box titled Validate Nios II system ID before software download.

4. Click Program Flash button. When it completes, the non-volatile configuration isdone.

5. Disconnect JTAG cable and power off the board. When setting the switch 1 andswitch 4 of SW2 open, we can use the S60 board independently to communicate with hostPC via Ethernet after powering on the board.

6.3 Recompile the Project

In the appendix, the IP address of “192.168.1.213” and the port “1739” have been usedfor the UDP socket. If either the IP address or the port number is needed to be revised,just update the Python code host = ‘192.168.1.213’ and the sentence cliaddr.sin addr.s addr= inet addr(“192.168.1.213”); in simple socket server.c file for the IP address renewal; andmodify the Python code port=1739 and the definition UDP PORT=1739 in simple socket server.hfile. After the revision, the project should be recompiled before running it on the TargetFPGA or downloading it into the Flash memory. It can be implemented by clicking Project

8

Page 9: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

on the Tools menu and then clicking Clean with checking the box titled Start a build imme-diately in the dialog box.

References

[1] Altera Corporation, San Jose, CA 95134, Nios II Hardware Development Tutorial,v2.5 ed., October 2007.

[2] Altera Corporation, San Jose, CA 95134, Nios II Software Developer’s Handbook, May2008.

[3] B. F. W. Richard Stevens and A. M. Rudoff, UNIX Network Programming – The SocketsNetworking API, vol. 1. Boston: Addison-Wesley, 3 ed., 2004.

[4] Altera Corporation, Accelerating Nios II Networking Applications, May 2007.

9

Page 10: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

Appendices: Source Code

A ADC Interface Module

Listing 1: a2d.v file

0 module a2d ( adclk , rdc lk , cs n , r s t n , rd , a2dc ,addr , waitreq , a2do ) ;

2 // −−−− Port De f i n i t i o ninput adc lk ;

4 input rdc lk ;input c s n ;

6 input r s t n ;input rd ;

8 input [ 1 1 : 0 ] a2dc ;input [ 9 : 0 ] addr ;

10 output wait req ;output [ 3 1 : 0 ] a2do ;

12wire wait req ;

14 wire [ 3 1 : 0 ] a2do ;

16 // −−−− Inte rmed iate Var iab lewire [ 7 : 0 ] a2du ;

18 reg [ 1 : 0 ] cur rent byte , next byte ;reg [ 7 : 0 ] a2di0 , a2di1 , a2di2 , a2di3 ;

20 wire [ 3 1 : 0 ] ad q0 , ad q1 ;wire [ 3 1 : 0 ] ccb q ;

22 wire wfu l l0 , rempty0 ;wire wfu l l1 , rempty1 ;

24 reg wrreq , rdreq ;reg [ 2 : 0 ] current ad , next ad ;

26 reg [ 7 : 0 ] counter ;wire wrclk ;

28 reg byte cn t r ;reg [ 1 5 : 0 ] datiL , datiH ;

30 reg [ 3 1 : 0 ] da t i ;wire datready ;

32 wire c l k f u l l , c l k r f u l l ;

34 // −−−−− c l o ck i n s t a n t i a t i o nbu f c l k p l l 0 (

36 . a r e s e t (˜ r s t n ) ,. i n c l k 0 ( adc lk ) ,

10

Page 11: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

38 . c0 ( wrclk ) ,. l ocked ( )

40 ) ;

42 // −−−− 2 ’ s complement to binarya s s i gn a2du [ 7 ] = ˜a2dc [ 1 1 ] ;

44 a s s i gn a2du [ 6 : 0 ] = a2dc [ 1 0 : 4 ] ;

46 // −−−− 8−b i t to 32−b i t// 1 . synchronous s e qu en t i a l b lock

48 always @( negedge adc lk ) begincu r r en t byt e <= next byte ;

50 end// 2 . combination l o g i c b lock

52 always @( cur r en t byt e ) begincase ( cu r r en t byt e )

54 2 ’ b00 : next byte = 2 ’ b01 ; // BYTE1;2 ’ b01 : next byte = 2 ’ b10 ; // BYTE2;

56 2 ’ b10 : next byte = 2 ’ b11 ; // BYTE3;2 ’ b11 : next byte = 2 ’ b00 ; // BYTE0;

58 d e f au l t : next byte = 2 ’ b01 ; // BYTE1;endcase

60 end// 3 . synchronous s e qu en t i a l b lock : g ive output

62 always @( negedge adc lk ) begincase ( next byte )

64 2 ’ b00 : begina2di3 <= a2du ;

66 end2 ’ b01 : begin

68 a2di0 <= a2du ;da t i <= {a2di3 , a2di2 , a2di1 , a2di0 } ;

70 end2 ’ b10 : begin

72 a2di1 <= a2du ;end

74 2 ’ b11 : begina2di2 <= a2du ;

76 endde f au l t : begin

78 a2di0 <= a2du ;a2di1 <= a2du ;

80 a2di2 <= a2du ;a2di3 <= a2du ;

82 end

11

Page 12: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

endcase84 end

86 // ============ Cross Clock Bridge Begin =============== //wire rempty ;

88 adFIFO c r o s s c l k (. data ( da t i ) ,

90 . wrclk ( wrclk ) ,. wrreq (1 ’ b1 ) ,

92 . w r f u l l ( ) ,. q ( ccb q ) ,

94 . rdc l k ( rdc lk ) ,. rdreq (˜ rempty ) ,

96 . r d f u l l ( ) ,. rdempty ( rempty )

98 ) ;// ============ Cross Clock Bridge End ================= //

100

102 // =============== Swing Buf f e r Begin ================== //reg [ 1 : 0 ] cur rent bu f , next buf ;

104 reg i d l e ;reg wren0 , wren1 , rden ;

106 // Control FSMparameter

108 INITIAL = 2 ’ b00 ,BUFFER0 = 2 ’ b01 ,

110 BUFFER1 = 2 ’ b10 ;// synchronous s e qu en t i a l b lock

112 always @( posedge rdc lk or negedge r s t n ) begini f (˜ r s t n )

114 cu r r en t bu f <= INITIAL ;e l s e

116 cu r r en t bu f <= next buf ;end

118 // combinator i a l b lockalways @( cu r r en t bu f or c s n or w fu l l 0 or w fu l l 1

120 or rempty0 or rempty1 ) begincase ( cu r r en t bu f )

122 INITIAL : begini f ( w fu l l 0 & ˜ cs n )

124 next buf = BUFFER0;e l s e

126 next buf = INITIAL ;end

12

Page 13: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

128 BUFFER0: begini f ( w fu l l 1 & rempty0 )

130 next buf = BUFFER1;e l s e

132 next buf = BUFFER0;end

134 BUFFER1: begini f ( w fu l l 0 & rempty1 )

136 next buf = BUFFER0;e l s e

138 next buf = BUFFER1;end

140 de f au l t : beginnext buf = INITIAL ;

142 endendcase

144 end// s e qu en t i a l b lock

146 always @( posedge rdc lk ) begincase ( next buf )

148 INITIAL : beginwren0 <= 1 ’ b1 ;

150 wren1 <= 1 ’ b0 ;rden <= 1 ’ b0 ;

152 rdreq <= 1 ’ bz ;i d l e <= 1 ’ b0 ;

154 endBUFFER0: begin

156 wren0 <= 1 ’ b0 ;i f ( w fu l l 1 & ˜rempty0 ) begin

158 wren1 <= 1 ’ b0 ;end

160 e l s e beginwren1 <= 1 ’ b1 ;

162 endi f ( rempty0 & ˜ wfu l l 1 ) begin

164 rden <= 1 ’ b0 ;i d l e <= 1 ’ b1 ;

166 ende l s e begin

168 rden <= 1 ’ b1 ;i d l e <= 1 ’ b0 ;

170 endrdreq <= 1 ’ b1 ;

172 end

13

Page 14: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

BUFFER1: begin174 i f ( w fu l l 0 & ˜rempty1 ) begin

wren0 <= 1 ’ b0 ;176 end

e l s e begin178 wren0 <= 1 ’ b1 ;

end180 wren1 <= 1 ’ b0 ;

i f ( rempty1 & ˜ wfu l l 0 ) begin182 rden <= 1 ’ b0 ;

i d l e <= 1 ’ b1 ;184 end

e l s e begin186 rden <= 1 ’ b1 ;

i d l e <= 1 ’ b0 ;188 end

rdreq <= 1 ’ b0 ;190 end

de f au l t : begin192 wren0 <= 1 ’ b1 ;

wren1 <= 1 ’ b0 ;194 rden <= 1 ’ b0 ;

rdreq <= 1 ’ bz ;196 i d l e <= 1 ’ b0 ;

end198 endcase

end200

swBUF buf0 (202 . c l o ck ( rdc lk ) ,

. s c l r (˜ r s t n ) ,204 . data ( ccb q ) ,

. wrreq (˜ rempty & wren0 ) ,206 . f u l l ( w fu l l 0 ) ,

. rdreq ( rden & rd & rdreq ) ,208 . empty ( rempty0 ) ,

. q ( ad q0 )210 ) ;

swBUF buf1 (212 . c l o ck ( rdc lk ) ,

. s c l r (˜ r s t n ) ,214 . data ( ccb q ) ,

. wrreq (˜ rempty & wren1 ) ,216 . f u l l ( w fu l l 1 ) ,

. rdreq ( rden & rd & ˜ rdreq ) ,

14

Page 15: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

218 . empty ( rempty1 ) ,. q ( ad q1 )

220 ) ;

222 a s s i gn wait req = i d l e ;a s s i gn a2do = rdreq ? ad q0 : ad q1 ;

224endmodule

15

Page 16: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

B Nios II Application Software

Listing 2: simple socket server.c file

0 #inc lude <s t d i o . h>#inc lude <s t r i n g . h>

2 #inc lude <ctype . h>#inc lude ‘ ‘ i n c l ud e s . h ’ ’

4 #inc lude ‘ ‘ s imp l e s o c k e t s e r v e r . h ’ ’#inc lude ‘ ‘ a l t e r r o r h a n d l e r . h ’ ’

6 #inc lude ‘ ‘ i ppo r t . h ’ ’#inc lude ‘ ‘ t cpport . h ’ ’

8void SSSSimpleSocketServerTask ( )

10 {i n t udp socket ;

12 s t r u c t sockaddr in c l i a dd r ;i n t udp rc = SSS BUF LEN ;

14 i n t nSendBufSize = SSS TX BUF SIZE ;void∗ r x bu f f = A2D INST BASE ;

16c l i a dd r . s i n f am i l y = AF INET ;

18 c l i a dd r . s i n p o r t = htons (UDP PORT) ;c l i a dd r . s i n addr . s addr = ine t addr ( ‘ ‘ 1 9 2 . 1 6 8 . 1 . 2 1 3 ’ ’ ) ;

20whi l e (1 )

22 {udp socket = socket (AF INET, SOCK DGRAM, IPPROTO UDP) ;

24 s e t sockopt ( udp socket , SOL SOCKET, SO SNDBUF,( const char∗)&nSendBufSize , s i z e o f ( i n t ) ) ;

26whi l e ( ! ( udp socket <0) & udp rc==SSS BUF LEN)

28 {udp rc = sendto ( udp socket , rx bu f f , SSS BUF LEN , 0 ,

30 ( s t r u c t sockaddr ∗)& c l i addr , s i z e o f ( c l i a dd r ) ) ;}

32 i f ( udp rc !=SSS BUF LEN)p r i n t f ( ‘ ‘ Transmit f a i l e d ! ’ ’ ) ;

34 c l o s e ( udp socket ) ;}

36 }

16

Page 17: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

C Server Application Software

Listing 3: server.py file

0 from socket import ∗from ctypes import ∗

2 import b i n a s c i i

4 # Set the socke t parametershost = ‘ 192 . 1 68 . 1 . 2 13 ’

6 port = 1739addr = ( host , port )

8 r e c l e n = 1468

10 # Set the packet parameterPacketNum = 8000

12# Set the socke t parameter

14 Bu f f e rS i z e = r e c l e n ∗PacketNum

16 # Create the socke tUDPSock = socket (AF INET, SOCKDGRAM)

18# Set opt ions

20 UDPSock . s e t sockopt (SOL SOCKET, SO RCVBUF, Bu f f e rS i z e )UDPSock . s e t sockopt (SOL SOCKET, SO REUSEADDR, 1)

22# Create a binary f i l e

24 f i l ename = ‘ t e s t . dat ’FILE0 = open ( f i l ename , ‘wb+’ , 0)

26# ================ Receive messages ================= #

28 f o r j in range (0 , loopt ime ∗PacketNum ) :data , address = UDPSock . recvfrom ( rec l en , 0)

30 FILE0 . wr i t e ( data )

32 UDPSock . c l o s e ( )FILE0 . c l o s e ( )

34# ================= Post−pro c e s s i ng ================= #

36 FILE1 = open ( ‘ t e s t . dat ’ , ‘ rb ’ ) ;data bytes = FILE1 . read ( r e c l e n ∗PacketNum∗ loopt ime )

38 FILE1 . c l o s e ( )

40 da t a s t r i n g = bytes . decode ( b i n a s c i i . h e x l i f y ( data bytes ) )

17

Page 18: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

FILE2 = open ( ‘ t e s t . txt ’ , ‘w ’ )42 FILE2 . wr i t e ( d a t a s t r i n g )

FILE2 . c l o s e ( )44

p r i n t ( ‘ Complete ! ’ )

18

Page 19: Method of High-Speed Data Acquisition and Continuous Data ...€¦ · 3. Run the Host PC program (Python Code). 4. Run the Matlab Code for data post-processing. 6.2 Flash Program

D Data Processing Program

Listing 4: fileIO.m file

0 c l e a r a l l ;c l o s e a l l ;

2f i d = fopen ( ‘ t e s t . txt ’ , ‘ r ’ ) ;

4 data = f s c a n f ( f i d , ‘%2X’ ) ;f c l o s e ( f i d ) ;

6eyediagram ( data , 200 ) ;

19