Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20638 Discussions

Ethernet Interface

Altera_Forum
Honored Contributor II
1,546 Views

Hi, 

 

I have a system (a probe) that is currently sending/receiving data from the PC terminal using a UART(RS232) connection. This was easy to configure/program, and the hardware was also easy to make up. However, I wish to upgrade to an Ethernet Interface to improve the speed of the data transfer. I wish to transmit/receive 16-bits of data in one go, I have already done the Web Server and Simple Socket tutorials, but they are way to complex for my needs....is there no equivalent for printf() / getchar() when uning the Ethernet... 

 

Thanks in advance... 

 

Kushan.
0 Kudos
16 Replies
Altera_Forum
Honored Contributor II
734 Views

1 - What I recommend doing is spending a little time analyzing the Simple Socket Server example. Once you create a socket and it's connected to your host PC, you can transfer whatever data you like across it. You just need to understand how that connection is established and managed then doing your data transfers will be simple. You can even write your own printf and getchar wrapper functions around the socket interface if you like. 

 

There are low level methods for accessing the socket but it would require you to do a lot more work on your end to establish and maintain the connection. Using the BSD socket calls is really the easiest way method to get up and running. 

 

2 - If you don't need the TCP/IP protocol layers and you just wanted to use ethernet as a raw transport medium. Then you could get away from the TCP/IP stack altogether and simply write/read raw data to/from the ethernet interface. However, you would have to completely control both ends of the connection and all connection points between. 

 

Jake
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

 

--- Quote Start ---  

Hi, 

I wish to transmit/receive 16-bits of data in one go, I have already done the Web Server and Simple Socket tutorials, but they are way to complex for my needs....is there no equivalent for printf() / getchar() when uning the Ethernet... 

Kushan. 

--- Quote End ---  

 

 

You can use Altera Host-Based File System in which you can write the entire received data in a file and can examine it later. The receive() call in socket programming gives out the data packets.
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

 

--- Quote Start ---  

 

 

2 - If you don't need the TCP/IP protocol layers and you just wanted to use ethernet as a raw transport medium. Then you could get away from the TCP/IP stack altogether and simply write/read raw data to/from the ethernet interface. However, you would have to completely control both ends of the connection and all connection points between. 

 

Jake 

--- Quote End ---  

 

 

 

Any hints/tips/sample code to point me in the right direction for using the ethernet as a raw transport medium...I can't seem to get my head around the whole TCP/IP protocol. 

 

cheers, 

 

Kushan.
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

I'm not sure this is the way to go... If you use Ethernet as a raw transport medium you would need to write your own PC driver to replace the TCP/IP stack with your own and recover the raw Ethernet data. Sounds really difficult. 

It's really simpler to use TCP/IP on your NIOS system, and the socket API is not that difficult to use. Have a deeper look at the simple socket server example. Once you open a TCP socket it's almost as easy to use as a character I/O.
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

Could you post some code for opening a TCP socket...I have successfully completed the Simple Socket Server Tutorial, and it runs, so what would the next step be?

0 Kudos
Altera_Forum
Honored Contributor II
734 Views

 

--- Quote Start ---  

Could you post some code for opening a TCP socket...I have successfully completed the Simple Socket Server Tutorial, and it runs, so what would the next step be? 

--- Quote End ---  

 

 

You can open a socket using a socket() call. This opens socket for both UDP and TCP/IP. Beej's Guide To Network Programming (Using Internet Sockets) ( http://www.beej.us/guide/bgnet/ ) is the best quick-start resource for socket programming. Sample codes (for both client and server) and essential differences between UDP and TCP/IP programming are also given here.
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

Any BSD socket example can be used. The one indicated by Vizziee is a good start. 

Just be careful with the header files to include. You should stick to the ones in the sockets server example, as they are not exactly the same than what you can see in some BSD sockets sample codes.
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

OK so i tried doing some simple socket programming....however am I right in assuming that I will have to use the MicroC/OS-II structure... 

 

I have tried the following code, but Nios II IDE can't find the ipport.h and tcpport.h files, which contain all the socket programming fucntions... 

 

# include "sys/socket.h" 

# include <unistd.h> 

# include <string.h> 

# include <alt_types.h> 

# include <ctype.h> 

# include <errno.h> 

# include <stdio.h> 

# include <sys/alt_flash.h> 

# include "includes.h" 

# include "io.h" 

# include "ipport.h" 

# include "tcpport.h" 

# include "network_utilities.h" 

 

# define LOCAL_SERVER_PORT 1500 

 

# define SERVER_PORT 1500 

# define MAX_MSG 100 

 

int main (int argc, char *argv[]) { 

 

int sd, rc, i; 

struct sockaddr_in localAddr, servAddr; 

struct hostent *h; 

 

if(argc < 3) { 

printf("usage: %s <server> <data1> <data2> ... <dataN>\n",argv[0]); 

exit(1); 

 

h = gethostbyname(argv[1]); 

if(h==NULL) { 

printf("%s: unknown host '%s'\n",argv[0],argv[1]); 

exit(1); 

 

servAddr.sin_family = h->h_addrtype; 

memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); 

servAddr.sin_port = htons(SERVER_PORT); 

 

/* create socket */ 

sd = socket(AF_INET, SOCK_STREAM, 0); 

if(sd<0) { 

perror("cannot open socket "); 

exit(1); 

 

/* bind any port number */ 

localAddr.sin_family = AF_INET; 

localAddr.sin_addr.s_addr = htonl(INADDR_ANY); 

localAddr.sin_port = htons(0); 

 

rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr)); 

if(rc<0) { 

printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT); 

perror("error "); 

exit(1); 

 

/* connect to server */ 

rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)); 

if(rc<0) { 

perror("cannot connect "); 

exit(1); 

 

for(i=2;i<argc;i++) { 

 

rc = send(sd, argv, strlen(argv) + 1, 0); 

 

if(rc<0) { 

perror("cannot send data "); 

close(sd); 

exit(1); 

 

 

printf("%s: data%u sent (%s)\n",argv[0],i-1,argv[i]); 

 

 

 

return 0; 

 

}
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

The version of the InterNiche TCP/IP stack that Altera ships is designed to work with uC/OSII. As such, if you aren't using uC/OSII for your application, the required source files won't even be found. 

 

The InterNiche TCP/IP stack also supports superloop mode which does not require an OS. All of the designs that we have done using the InterNiche stack use superloop mode and thereby do not require uC/OSII. You have to do some minor modifications to the stack to get it to run in superloop mode. 

 

Try these threads for more information: 

http://forum.niosforum.com/forum/index.php?showtopic=8747 

http://nioswiki.jot.com/wikihome/nichestack/%22super%20loop%22%20nichestack 

 

Jake
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

Right....I tried the superloop mode...but it doesn;t work. I think it is because my Quartus and SOPC builder are version 7.2, instead of 7.1 as in the instructions I downloaded, and so some of the folders that need changed are either in different locations or altogether missing....

0 Kudos
Altera_Forum
Honored Contributor II
734 Views

Yes we had to patch 7.2 manually.  

 

Jake
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

and how did you do that??

0 Kudos
Altera_Forum
Honored Contributor II
734 Views

Okay... it seems you've downloaded the patch from the wiki correct? He's included a document that indicates which files he's modified. So what we actually did was run the patch on a 7.1 installation. Then we compared the modified files between the patched 7.1 version and the 7.2 version to see what changes we needed to make to get the superloop mode working. They were minimal and if I remembered off hand what they were I'd just give them to you. Bare in mind that there was other code that changed between 7.1 and 7.2 so you only want the necessary superloop changes. 

 

However, didn't you look at this thread in the NIOS forum? 

http://forum.niosforum.com/forum/index.php?showtopic=8747 

He's got a 7.2 version ready for download. I haven't used it but you could give it a shot. 

 

Jake
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

The whole superloop concept isn't giving me any positive vibes...I installed Quartus 7.1, and Megacore IP library 7.1 and Nios 2 EDS 7.1, just to get the superloop socket server example to work. I followed the instructions in the attached word document to the letter. However, when I came to the stage of building the superloop socket server example...I got errors in the network_utilities.c file that it could not find the headers "ipport.h" and "tcpport.h" 

 

not sure whats going on!!
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

Okay. Well let me assure you the superloop concept is well proven. The InterNiche stack is designed to work in superloop mode. My company now has two products that employ the InterNiche stack in superloop mode. 

 

However, it is certainly fair to consider that going through this process may be beyond your experience level. If you feel that is the case, then you can certainly decide to use the uC/OSII implementation or an alternative TCP/IP stack. 

 

As far as your current problem goes. I don't know that you needed to install all three products (Quartus, Megacore, NIOS). I think the NIOS EDS would have been sufficient. Anyway, did you follow all the instructions in the document. You have to copy the "simple_socket_server_no_rtos" folder to your nios software examples directory. You cannot use the regular simple socket server that's bundled with the NIOS EDS. The reason for this is that your "system.stf" file for your system library has to be hand-edited to include support for the InterNiche stack. 

 

Altera has decided not to support the superloop version and that is why you have to jump through all these hoops. Altera's own documentation indicates that the stack can be run in superloop mode and that higher networking performance can be acheived in superloop mode but for some reason (probably some agreement with uC) they've decided only to support the uC/OSII version. 

 

Obviously I have a patched copy of the stack. However, I'm typically in the habit of teaching a man to fish rather than giving him the fish. On top of that, I've made some custom modifications to the stack and wouldn't want those to cause you any grief. 

 

Jake
0 Kudos
Altera_Forum
Honored Contributor II
734 Views

Oh just take it. You have to unzip this to c:\altera\72\nios2eds\components .Remember, in order for this to work, you'll have to hand edit the system.stf file for your project. Here is an example: 

 

<sw_component id="INICHE_SL" name="NicheStack TCP/IP Stack" sopc_component_dir="altera_iniche_sl"> <sys_defines> <define name="include_tcp" quote="no" value="1"/> <define name="ip_fragments" quote="no" value="1"/> <define name="dhcp_client" quote="no" value="1"/> <define name="INICHE_TIMERS" quote="no" value="1"/> <define name="MEM_WRAPPERS" quote="no" value="1"/> <define name="TCP_ZEROCOPY" quote="no" value="1"/> <define name="ALT_INICHE_SUPERLOOP" quote="no" value="1"/> <define name="TELNET_SVR" quote="no" value="1"/> <define name="IN_MENUS" quote="no" value="1"/> <!--- <define name="ALT_NUMBIGBUFS" quote="no" value="100"/> --> <!--- <define name="PING_APP" quote="no" value="1"/> <define name="INCLUDE_NVPARMS" quote="no" value="1"/> <define name="NET_STATS" quote="no" value="1"/> <define name="DNS_CLIENT" quote="no" value="1"/> <define name="IP_ROUTING" quote="no" value="1"/> <define name="USE_PROFILER" quote="no" value="1"/> <define name="FTP_SERVER" quote="no" value="1"/> <define name="TFTP_SERVER" quote="no" value="1"/> <define name="FTP_CLIENT" quote="no" value="1"/> <define name="TFTP_CLIENT" quote="no" value="1"/> <define name="SERVER_PUSH" quote="no" value="1"/> <define name="VFS_FILES" quote="no" value="1"/> <define name="WEBPORT" quote="no" value="1"/> <define name="HTML_COMPRESSION" quote="no" value="1"/> <define name="FOREIGN_LANG_SUPPORT" quote="no" value="1"/> --> </sys_defines> </sw_component> 

 

Jake
0 Kudos
Reply