Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Ethernet

Altera_Forum
Honored Contributor II
1,135 Views

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif Hello everybody http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif  

 

I&#39;ve a problem with a TCP connection in my development kit nios2 stratix 60_rohs 

 

 

 

 

err_t conectado() {  printf("Estamos conectaos!");  printf("Enviando.......\n");      while(1)    {        tcp_write(pcb, i, sizeof(i), 0);        lan91c111if_service(&netif);        i++;    }  return(ERR_OK); }   int main(void) {    //struct netif netif;    struct ip_addr ipaddr, netmask, gw;    struct ip_addr ipdest;    struct pbuf* pbuf;    //struct tcp_pcb* pcb;    //int i=0;    int *pi;    *pi = i;        alt_avalon_lan91c111_if* dev_list_ptr = (alt_avalon_lan91c111_if*)alt_ethernet_device_list.next;    printf("Prueba con TCP\n\n");    pbuf = pbuf_alloc(PBUF_TRANSPORT,30,PBUF_RAM);    pbuf_free(pbuf);            pbuf -> payload = (int*)pi;    printf("payload = %d\n",pbuf->payload); /* * Initialize lwip */    lwip_init();    printf ("Setting IP address to: %d.%d.%d.%d\n", IPADDR0, IPADDR1, IPADDR2, IPADDR3);    printf ("Setting netmask to: %d.%d.%d.%d\n", NETMASK0, NETMASK1, NETMASK2, NETMASK3);    printf ("Setting gateway address to: %d.%d.%d.%d\n\n\n", GWADDR0, GWADDR1, GWADDR2, GWADDR3);        IP4_ADDR(&ipaddr, IPADDR0, IPADDR1, IPADDR2, IPADDR3);    IP4_ADDR(&netmask, NETMASK0, NETMASK1, NETMASK2, NETMASK3);    IP4_ADDR(&gw, GWADDR0, GWADDR1, GWADDR2, GWADDR3);    netif_add(&netif, &ipaddr, &netmask, &gw,                                            (void*)dev_list_ptr,                                            lan91c111if_init,                                            ip_input);    netif_set_default(&netif); //    IP4_ADDR(&ipdest, IPADDRDEST0, IPADDRDEST1, IPADDRDEST2, IPADDRDEST3); //    printf ("Setting destination IP address to: %d.%d.%d.%d\n", IPADDRDEST0, IPADDRDEST1, IPADDRDEST2, IPADDRDEST3);      printf("iniciant el tema TCP!\n");     

 

 

 

 

I supose that here starts my problems: I don&#39;t know what i&#39;m doing wrong, it&#39;s close to all exemples  

 

   tcp_init();    pcb = tcp_new();    tcp_bind(pcb,&ipaddr, 70);    pcb2=&tcp_listen(pcb)    tcp_accept(pcb2, conectado());    printf("A prendre pel sac!\n");   } 

 

Could anybody help me? I will be very happy,  

 

 

thank you a lot for your time
0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
377 Views

for starters... in your first section of code: 

 

pbuf = pbuf_alloc(PBUF_TRANSPORT,30,PBUF_RAM); 

pbuf_free(pbuf); 

 

pbuf -> payload = (int*)pi; 

 

I believe that the pbuf_free is used incorrectly! You are freeing the pbuf so it is no longer useable in your statement pbuf->payload = ... 

 

What exactly is your goal... and what is your problem. If you could be a little more specific with your questioning we may be able to help better.
0 Kudos
Altera_Forum
Honored Contributor II
377 Views

First of all thank you for your time! 

 

 

--- Quote Start ---  

originally posted by cfavreau@Mar 22 2007, 03:11 PM 

what exactly is your goal... and what is your problem.  if you could be a little more specific with your questioning we may be able to help better. 

--- Quote End ---  

 

 

the problem is that the following code doesn&#39;t work: 

 

  tcp_init();   pcb = tcp_new();   tcp_bind(pcb,&ipaddr, 70);   pcb2=&tcp_listen(pcb)   tcp_accept(pcb2, conectado()); 

 

First of all I&#39;m using the STANDALONE lwIP, because I need an easy single threaded code and no OS. At the begining I tried to use MICRO/OS but I couldn&#39;t succed. 

 

in the code: 

 

I create a tcp entity, after I blind it, and finally I put it to listen using the function tcp_listen. I expect that in this point the code stops the execution waiting for a remote connection and after executes "conectado" through the "tcp_accept" function.  

 

But, this doesn&#39;t work, the code doesn&#39;t wait for the remote connection and it is executed until the end. 

 

in my opinion, one step is missing, but i don&#39;t know which or where. could you help me or give me a piece of advice such as an example?  

 

The main goal is to establish an elemental TCP/IP connection between the nios2 processor, working as server, and a PC, working as client.  

 

Thank you for your time!! 

 

Xavi
0 Kudos
Altera_Forum
Honored Contributor II
377 Views

It appears on second look and your explanation of your problem that you are missing the function calls that actually make the LWIP do its work. This is generally done in a main loop. Here is the code that I have (taken mostly from the LWIP Web Server example included with the NIOS II IDE). 

# if LWIP_DHCP# include "lwip/dhcp.h"# define DHCP_TMR_INTERVAL DHCP_FINE_TIMER_MSECS 

void dhcp_tmr(void);# endif 

# if IP_REASSEMBLY# define IP_REASS_TMO 1000 

void ip_reass_timer(void);# endif 

 

unsigned int net_last_tick = 0; 

unsigned int net_tick = 0; 

unsigned int net_tcp_timer = TCP_TMR_INTERVAL; 

unsigned int net_arp_timer = ARP_TMR_INTERVAL;# if LWIP_DHCP 

unsigned int net_dhcp_timer = DHCP_TMR_INTERVAL;# endif# if IP_REASSEMBLY 

unsigned int net_reass_timer = IP_REASS_TMO;# endif 

 

void net_init() 

// Initialize the LWIP Stack (TCP/UDP ... ETC) 

lwip_stack_init(); 

// Initialize the Hardware 

lwip_devices_init(); 

// Initialize application(s) 

httpd_init(); 

echo_init(); 

 

net_last_tick = alt_nticks(); 

 

void net_service() 

// Check network devices for input 

lwip_dev_rx(); 

 

// Retrieve the millisecond timestamp 

net_tick = alt_nticks(); 

 

// Check to see if a millisecond has passed 

if (net_tick != net_last_tick) 

// Save the current timestamp 

net_last_tick = net_tick; 

 

// Decrement and check the TCP timer 

if (--net_tcp_timer == 0) 

net_tcp_timer = TCP_TMR_INTERVAL; 

tcp_tmr(); 

 

// Decrement and check the ARP timer 

if (--net_arp_timer == 0) 

net_arp_timer = ARP_TMR_INTERVAL; 

etharp_tmr(); 

# if LWIP_DHCP 

// Decrement and check the DHCP timer 

if (--net_dhcp_timer == 0) 

net_dhcp_timer = DHCP_TMR_INTERVAL; 

dhcp_tmr(); 

dhcp_timeout_task(); 

}# endif // LWIP_DHCP 

# if IP_REASSEMBLY 

// Decrement and check the Reassembly Timer 

if (--net_reass_timer == 0) 

net_reass_timer = IP_REASS_TMO; 

ip_reass_timer(); 

} # endif // IP_REASSEMBLY 

 

int main(void) 

printf("Example web server using Light-Weight IP (LWIP)\n"); 

printf("and simple RAM-based file system.\n\n"); 

 

net_init(); 

 

// main loop to service the Ethernet device and expire TCP timers 

while(1) 

net_service();  

 

You will want to pay attention to the net_service(). This is where the LWIP is called to acutally make it do things. The LWIP is a giant state machine... so you have to make it tick in order for it to do stuff. 

 

 

Also I think your callback function for the accept needs to be different... 

 

static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err); 

 

that is the decleration of the http_accept function in the example code. 

 

This might or might not be a problem. I am do not know the LWIP that well to go into it. You may want to check out the rawapi.txt file that comes with the LWIP implementation. 

 

Are you using the LWIP that comes with the OpenCores Ethernet IP?
0 Kudos
Altera_Forum
Honored Contributor II
377 Views

Hi cfavreau!!! Thanks for your answers!!!! http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif  

 

I&#39;m a mate of xbosch, working in the same project and I would like to ask you a question about the code in your previous post.  

 

This code is for the uC/OS version of LwIP, isn&#39;t it??? I&#39;m asking you that because we are using the STANDALONE version of the LwIP (the one that you can download from this forum) because we needed a single threaded and no OS way to access the Ethernet, so we can somehow adapt a code that was running on a NiosI to a new board with a NiosII. 

 

I&#39;ve been searching those initialization functions you mentioned in your last post (such as lwip_stack_init or lwip_devices_init) but I can&#39;t find them in the standalone version of LwIP so I&#39;m wondering if the code you posted works just for the uC/OS version of LwIP or there&#39;s something I&#39;m missing. 

 

I&#39;ll be very grateful if you could help us!!!! 

 

Thanks again for your time and your advices!!!!!!! http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif
0 Kudos
Altera_Forum
Honored Contributor II
377 Views

Did you try the Standalone LWIP example that comes with the NIOS II IDE? 

 

That is the example I am pulling the code out of and it is the one which I based my code on. 

 

I didn&#39;t download anything from the forum in those regards. The code came with the NIOS II IDE.
0 Kudos
Reply