# lwip task start

**URL:** <https://forums.freertos.org/t/lwip-task-start/2505>\
**Category:** Kernel\
**Created:** [May 21, 2008, 12:09pm UTC](https://forums.freertos.org/t/lwip-task-start/2505 "2008-05-21T12:09:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 21, 2008, 12:09pm UTC](https://forums.freertos.org/t/lwip-task-start/2505/1 "2008-05-21T12:09:59Z")

</div>

**[maufil](https://sourceforge.net/u/maufil/)** wrote on [Wednesday, May 21, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee05b78/#d329):

Hi all,  
I’m doing a demo on udp lwip working for Str91x with Rowley. My demo is not getting send packages udp, in my opinion there is a problem when the FreeRTOS launches task. Below is my code, to understand how to launch the task I have taken inspiration from demo WebServer included in the package FreeRTOS 5.  
someone can help me? it’s my code ok?

===========================================MAIN.C=========  
…  
#ifdef STACK\_LWIP&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* Create the lwIP task.&nbsp; This uses the lwIP RTOS abstraction layer.\*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vlwIPInit();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sys\_set\_state(&nbsp;&nbsp;&nbsp; ( signed portCHAR \* ) "httpd", lwipBASIC\_SERVER\_STACK\_SIZE );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sys\_thread\_new( vBasicWEBServer, ( void \* ) NULL, ( tskIDLE\_PRIORITY + 2 )basicwebWEBSERVER\_PRIORITY );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sys\_set\_default\_state();  
#endif  
…

=========================================UDP\_SENDER.H========  
…  
#ifndef UDP\_SENDER\_H  
#define UDP\_SENDER\_H

#include \<91x\_type.h\>

/\*------------------------------------------------------------------------------\*/  
/\*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MACROS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \*/  
/\*------------------------------------------------------------------------------\*/  
#define basicudp\_SENDER\_PRIORITY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( tskIDLE\_PRIORITY + 2 )

/\* The port on which we listen. \*/  
#define webUDP\_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( 1234 )

/\* Delay on close error. \*/  
#define webSHORT\_DELAY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( 10 / portTICK\_RATE\_MS )

/\* The IP address being used. \*/  
#define emacIPADDR0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10  
#define emacIPADDR1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 200  
#define emacIPADDR2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 200  
#define emacIPADDR3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2

/\* The gateway address being used. \*/  
#define emacGATEWAY\_ADDR0&nbsp;&nbsp; 10  
#define emacGATEWAY\_ADDR1&nbsp;&nbsp; 200  
#define emacGATEWAY\_ADDR2&nbsp;&nbsp; 200  
#define emacGATEWAY\_ADDR3&nbsp;&nbsp; 1

/\* The network mask being used. \*/  
#define emacNET\_MASK0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 255  
#define emacNET\_MASK1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 255  
#define emacNET\_MASK2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 255  
#define emacNET\_MASK3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 252

#define STATIC\_IP&nbsp;&nbsp; 1  
#define DHCP\_IP&nbsp;&nbsp;&nbsp;&nbsp; 2

#define lwipBASIC\_SERVER\_STACK\_SIZE&nbsp;&nbsp;&nbsp; 250

/\*------------------------------------------------------------------------------\*/  
/\*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PROTOTYPES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \*/  
/\*------------------------------------------------------------------------------\*/  
/\* The function that implements the WEB server task. \*/  
void vBasicUDPSender( void \*pvParameters );

/\* Initialisation required by lwIP. \*/  
void vlwIPInit( void );

#endif

=========================================UDP\_SENDER.C==========================

#define basicwebWEBSERVER\_PRIORITY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( tskIDLE\_PRIORITY + 2 )  
#define lwipTCP\_STACK\_SIZE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 600

/\* Standard includes. \*/  
#include \<stdio.h\>  
#include \<string.h\>  
#include \<91x\_type.h\>

/\* Scheduler includes. \*/  
#include "FreeRTOS.h"  
#include "task.h"  
#include "semphr.h"

/\* Demo includes. \*/  
#include "lwIPWebServer/BasicWEB.h"  
#include "udpecho-sender.h"  
/\* lwIP includes. \*/  
#include "lwip/api.h"  
#include "lwip/tcpip.h"  
#include "lwip/memp.h"  
#include "lwip/stats.h"  
#include "Dnetif/loopif.h"  
//#include "lcd.h"  
#include "httpd.h"

//#define lwipTCP\_STACK\_SIZE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 600  
//XXXXXXXXXXXXXXXXXXXXXXXXXXXX  
#include "lwip/udp.h"  
#include "lwip/mem.h"  
#include "lwip/debug.h"  
/\*------------------------------------------------------------------------------\*/  
/\*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GLOBALS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \*/  
/\*------------------------------------------------------------------------------\*/  
static struct netif EMAC\_if;

/\*------------------------------------------------------------------------------\*/  
/\*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FUNCTIONS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \*/  
/\*------------------------------------------------------------------------------\*/  
void vlwIPInit( void )  
{  
&nbsp;&nbsp;&nbsp; /\* Initialize lwIP and its interface layer. \*/  
&nbsp;&nbsp;&nbsp; sys\_init();  
&nbsp;&nbsp;&nbsp; mem\_init();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; memp\_init();  
&nbsp;&nbsp;&nbsp; pbuf\_init();  
&nbsp;&nbsp;&nbsp; netif\_init();  
&nbsp;&nbsp;&nbsp; ip\_init();  
&nbsp;&nbsp;&nbsp; sys\_set\_state(( signed portCHAR \* ) "lwIP", lwipTCP\_STACK\_SIZE);  
&nbsp;&nbsp;&nbsp; udp\_init();&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; sys\_set\_default\_state();  
}

void udp\_echo\_recv(void \*arg, struct udp\_pcb \*pcb, struct pbuf \*p, struct ip\_addr \*addr, u16\_t port)  
{

&nbsp;&nbsp;&nbsp; if (p != NULL)  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* send received packet back to sender \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; udp\_sendto(pcb, p, addr, port);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* free the pbuf \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pbuf\_free(p);  
&nbsp;&nbsp;&nbsp; }  
}

void udp\_echo\_init(void)  
{  
&nbsp;&nbsp;&nbsp; struct udp\_pcb \* pcb;

&nbsp;&nbsp;&nbsp; /\* get new pcb \*/  
&nbsp;&nbsp;&nbsp; pcb = udp\_new();  
&nbsp;&nbsp;&nbsp; if (pcb == NULL) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LWIP\_DEBUGF(UDP\_DEBUG, ("udp\_new failed!\n"));  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;  
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; /\* bind to any IP address on port 7 \*/  
&nbsp;&nbsp;&nbsp; if (udp\_bind(pcb, IP\_ADDR\_ANY, UDP\_PORT) != ERR\_OK) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LWIP\_DEBUGF(UDP\_DEBUG, ("udp\_bind failed!\n"));  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;  
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; /\* set udp\_echo\_recv() as callback function  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for received packets \*/  
&nbsp;&nbsp;&nbsp; udp\_recv(pcb, udp\_echo\_recv, NULL);  
}

void vBasicWEBServer( void \*pvParameters )  
{  
struct ip\_addr xIpAddr, xNetMast, xGateway;  
extern err\_t ethernetif\_init( struct netif \*netif );

&nbsp;&nbsp;&nbsp; /\* Parameters are not used - suppress compiler error. \*/  
&nbsp;&nbsp;&nbsp; ( void ) pvParameters;

&nbsp;&nbsp;&nbsp; /\* Create and configure the EMAC interface. \*/  
&nbsp;&nbsp;&nbsp; IP4\_ADDR( &xIpAddr, emacIPADDR0, emacIPADDR1, emacIPADDR2, emacIPADDR3 );  
&nbsp;&nbsp;&nbsp; IP4\_ADDR( &xNetMast, emacNET\_MASK0, emacNET\_MASK1, emacNET\_MASK2, emacNET\_MASK3 );  
&nbsp;&nbsp;&nbsp; IP4\_ADDR( &xGateway, emacGATEWAY\_ADDR0, emacGATEWAY\_ADDR1, emacGATEWAY\_ADDR2, emacGATEWAY\_ADDR3 );  
&nbsp;&nbsp;&nbsp; netif\_add( &EMAC\_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif\_init, tcpip\_input );

&nbsp;&nbsp;&nbsp; /\* make it the default interface \*/  
&nbsp;&nbsp;&nbsp; netif\_set\_default( &EMAC\_if );

&nbsp;&nbsp;&nbsp; /\* bring it up \*/  
&nbsp;&nbsp;&nbsp; netif\_set\_up(&EMAC\_if);

&nbsp;&nbsp;&nbsp; /\* Initialize HTTP \*/  
&nbsp;&nbsp; udp\_echo\_init();//httpd\_init();

&nbsp;&nbsp;&nbsp; /\* Nothing else to do.&nbsp; No point hanging around. \*/  
&nbsp;&nbsp;&nbsp; vTaskDelete( NULL );  
}

…
