# FreeRTOS+TCP for LPC17xx

**URL:** <https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377>\
**Category:** Libraries\
**Created:** [December 9, 2019, 1:37pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377 "2019-12-09T13:37:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 9, 2019, 1:37pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/1 "2019-12-09T13:37:52Z")

</div>

Hi,

Currently i’m using the library LWIP (v1.4.1) with LPC1788 and FreeRTOS(v7.5.3). But the LWIP stops work when occur many lost of packages in a slow network, and for restart work, i need to hard reset, and this is bad in my project. So, i decide to change LWIP for new FreeRTOS+TCP, and i will need help to do this.  
First thing, i need to download this library [https://www.freertos.org/FreeRTOS-Plus/FreeRTOS\_Plus\_TCP/index.html](https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/index.html)?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [December 9, 2019, 3:11pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/2 "2019-12-09T15:11:41Z")

</div>

Yes. That library is in the main download, so just download that. It is in /FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 9, 2019, 3:11pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/3 "2019-12-09T15:11:59Z")

</div>

The most recent sources of FreeRTOS and libraries are found on [github/aws](https://github.com/aws/amazon-freertos/).

The FreeRTOS+TCP library can be found [here](https://github.com/aws/amazon-freertos/tree/master/libraries/freertos_plus/standard/freertos_plus_tcp).

Note that there is a skeleton of a driver for LPC17xx [here](https://github.com/aws/amazon-freertos/tree/master/libraries/freertos_plus/standard/freertos_plus_tcp/source/portable/NetworkInterface/LPC17xx)

Unfortunately I don’t have a board with a LPC17xx, neither I have time to develop the driver.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 10, 2019, 6:28pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/4 "2019-12-10T18:28:32Z")

</div>

> [@htibosch](#):
>
> skeleton

Hi,

I start the implementation but have many problems on build.  
In this files NetworkBufferManagement.h, NetworkInterface.h don’t found the define NetworkBufferDescriptor\_t, so i saw the typedef on FreeRTOS\_IP.h, so i insert the ‘#include “FreeRTOS\_IP.h”’ on NetworkBufferManagement.h.

Now my file NetworkInterface.c for LPC17xx (in my case LPC1788) have many errors:

1. PINSEL\_CFG\_Type - unknown type
2. Emac\_Config.Mode - has no member named Mode
3. BaseType\_t xNetworkInterfaceOutput( NetworkBufferDescriptor\_t \* const pxNetworkBuffer ) - conflicting types, in header was defined like BaseType\_t xNetworkInterfaceOutput( NetworkBufferDescriptor\_t \* const pxNetworkBuffer, BaseType\_t xReleaseAfterSend );

and many other errors in this file.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 10, 2019, 6:53pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/5 "2019-12-10T18:53:49Z")

</div>

I changed, remove the “#include ‘FreeRTOS\_IP.h’” from NetworkBufferManagement.h and put on NetworkInterface.c and follow the changes for function xNetworkInterfaceOutput like **[alager12345](https://sourceforge.net/u/alager12345/)** do here [FreeRTOS+TCP with LPC17XX](https://forums.freertos.org/t/freertos-tcp-with-lpc17xx/5868/9) .

Now i need to rewrite the function xNetworkInterfaceInitialise.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 12, 2019, 7:43pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/6 "2019-12-12T19:43:59Z")

</div>

Hi,

I used the library NetworkInterface.c of LPC40x8 like Peter Wills said here on [https://sourceforge.net/p/freertos/discussion/382005/thread/0d4d999eab/#2b40](https://sourceforge.net/p/freertos/discussion/382005/thread/0d4d999eab/#2b40).  
I got it the ip from dns using FreeRTOS\_gethostbyname.  
But now i have a problem.  
I’m testing in a local connection, the board can connect to the server but a lot of times return the code -116 on connection, means pdFREERTOS\_ERRNO\_ETIMEDOUT but i don’t know why. Some times the connection works well, but lot of times not.  
In my code i’m open one socket, open connection, try send, wait response, close connection, and delete this task. For redo the test i restart the board (is only a test now).

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 13, 2019, 6:22am UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/7 "2019-12-13T06:22:12Z")

</div>

The only function in the library that can return a `ETIMEDOUT` is `FreeRTOS_connect()`.  
Now I’m curious about the code that you use: how the socket was created, what properties you have set, and what parameters are given to `FreeRTOS_connect()`.

Normally `FreeRTOS_connect()` needs a time-out value of at least a few seconds.

Al time-outs are expressed in ticks. Please use `pdMS_TO_TICKS()` to translate from milliseconds to clock-ticks.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 13, 2019, 6:26pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/8 "2019-12-13T18:26:43Z")

</div>

Hi @htibosch

This is my task created after network connection.  
With this code, the connection with local server was opened but don’t send nothing because on _FreeRTOS\_connect_ return _pdFREERTOS\_ERRNO\_ETIMEDOUT_.

```
static void task_IOT(void *pvParameters){
Socket_t xSocketSend;
struct freertos_sockaddr xRemoteAddress;
BaseType_t xAlreadyTransmitted = 0, xBytesSent = 0;
char *pcBufferToTransmit = "Hello World FreeRTOS-TCP LPC1788";
size_t xTotalLengthToSend = strlen(pcBufferToTransmit);
size_t xLenToSend;

xRemoteAddress.sin_port = FreeRTOS_htons( 1500 );
xRemoteAddress.sin_addr = FreeRTOS_inet_addr_quick( 192, 168, 0, 10 );

xSocketSend = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP);
configASSERT( xSocket != FREERTOS_INVALID_SOCKET );

if( FreeRTOS_connect( xSocketSend, &xRemoteAddress, sizeof( xRemoteAddress ) ) == 0 ){
	/* How many bytes are left to send? */
	xLenToSend = xTotalLengthToSend;// – xAlreadyTransmitted;
	xBytesSent = FreeRTOS_send( /* The socket being sent to. */
			xSocketSend,
			/* The data being sent. */
			&( pcBufferToTransmit[xAlreadyTransmitted] ),
			/* The remaining length of data to send. */
			xLenToSend,
			/* ulFlags. */
			0 );

	if( xBytesSent >= 0 )
	{
		/* Data was sent successfully. */
		xAlreadyTransmitted += xBytesSent;
	}
	else
	{
		/* Error – break out of the loop for graceful socket close. */
	}
}

FreeRTOS_shutdown( xSocketSend, FREERTOS_SHUT_RDWR );

while( FreeRTOS_recv( xSocketSend, pcBufferToTransmit, xTotalLengthToSend, 0 ) >= 0 ){
	vTaskDelay(pdMS_TO_TICKS( 250 ) );
}

/* The socket has shut down and is safe to close. */
FreeRTOS_closesocket( xSocketSend );

DEBUGOUT("task_IOT stop\n");
vTaskDelete(hdl_IOT);
}
```

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 13, 2019, 6:43pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/9 "2019-12-13T18:43:26Z")

</div>

Hi @htibosch

I saw other think now when i was debugging with jtag step-by-step inside _FreeRTOS\_connect_ the code stablished connection and send now. I think is the period of waiting for stablish connection.

Another thing, when is waiting the response from server some times occour an HardFault. This part.

```
while( FreeRTOS_recv( xSocketSend, pcBufferToTransmit, xTotalLengthToSend, 0 ) >= 0 ){
		vTaskDelay(pdMS_TO_TICKS( 250 ) );
}
```

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 13, 2019, 9:47pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/10 "2019-12-13T21:47:35Z")

</div>

Hi,

Works now.  
I put a vTaskDelay(2000) before FreeRTOS\_connect and solve the problem of timeout error.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 14, 2019, 8:47am UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/11 "2019-12-14T08:47:27Z")

</div>

Hi Ulysses, here below your code along with a few changes and comments.

```auto

static void task_IOT(void *pvParameters)
{
    Socket_t xSocketSend;
    struct freertos_sockaddr xRemoteAddress;
    BaseType_t xAlreadyTransmitted = 0, xBytesSent = 0;

    /* _HT_ : oops, you are assigning a literal string to a non-const char pointer.
    That is "not done" but don't use it to receive data. The string might be stored in read-only flash. */
/* char *pcBufferToTransmit = "Hello World FreeRTOS-TCP LPC1788"; */
    const char pcBufferToTransmit[] = "Hello World FreeRTOS-TCP LPC1788";
    char pcBufferToReceive[sizeof pcBufferToTransmit];

    size_t xTotalLengthToSend = strlen(pcBufferToTransmit);
    size_t xLenToSend;

    TickType_t x10Seconds = pdMS_TO_TICKS( 10000u );

    xRemoteAddress.sin_port = FreeRTOS_htons( 1500 );
    xRemoteAddress.sin_addr = FreeRTOS_inet_addr_quick( 192, 168, 0, 10 );

    xSocketSend = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP);

    /* _HT_ it looks like you have not enabled configASSERT().
    The variable 'xSocket' is not declared in this context. */
/* configASSERT( xSocket != FREERTOS_INVALID_SOCKET ); */
    configASSERT( xSocketSend != FREERTOS_INVALID_SOCKET );

    /* Let both send() and recv() block for at most 10 seconds. */
    FreeRTOS_setsockopt( xUDPSocket, 0, FREERTOS_SO_SNDTIMEO, &( x10Seconds ), sizeof( x10Seconds ) );
    FreeRTOS_setsockopt( xUDPSocket, 0, FREERTOS_SO_RCVTIMEO, &( x10Seconds ), sizeof( x10Seconds ) );

    if( FreeRTOS_connect( xSocketSend, &xRemoteAddress, sizeof( xRemoteAddress ) ) == 0 )
    {
        /* How many bytes are left to send? */
        xLenToSend = xTotalLengthToSend;// – xAlreadyTransmitted;
        xBytesSent = FreeRTOS_send( /* The socket being sent to. */
                xSocketSend,
                /* The data being sent. */
                &( pcBufferToTransmit[xAlreadyTransmitted] ),
                /* The remaining length of data to send. */
                xLenToSend,
                /* ulFlags. */
                0 );

        if( xBytesSent >= 0 )
        {
            /* Data was sent successfully. */
            xAlreadyTransmitted += xBytesSent;
        }
        else
        {
            /* Error – break out of the loop for graceful socket close. */
        }
        /* _HT_ Now shutdown the connection and wait for an acknowledgement. */
        FreeRTOS_shutdown( xSocketSend, FREERTOS_SHUT_RDWR );

        while( FreeRTOS_recv( xSocketSend, pcBufferToReceive, sizeof( pcBufferToReceive ), 0 ) >= 0 )
        {
            /* _HT_ when you use blocking sockets calls, there is no need to call vTaskDelay(). */
/* vTaskDelay(pdMS_TO_TICKS( 250 ) ); */
        }
    }

/* _HT_ Only call shutdown() and recv() if a socket is connected. */

    /* The socket has shut down and is safe to close. */
    FreeRTOS_closesocket( xSocketSend );

    DEBUGOUT("task_IOT stop\n");
    vTaskDelete(hdl_IOT);
}

```

I hope it helps to understand it better.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 14, 2019, 5:44pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/12 "2019-12-14T17:44:26Z")

</div>

Hi @htibosch,

About your suggestions.

- pcBufferToTransmit : i changed like you suggest after my last post.
- pcBufferToReceive : i done it too.
- configASSERT( xSocket != FREERTOS\_INVALID\_SOCKET ) : i hadn’t see this, thanks.
- FreeRTOS\_setsockopt : i had changing this on the defines at FreeRTOSIPconfig, but like you show is best.
- “Only call shutdown() and recv() if a socket is connected.” : I saw this, and i though strange, but was in this way on examples. However i will change this.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 16, 2019, 8:41pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/13 "2019-12-16T20:41:28Z")

</div>

Hi,

Now i have another problem, when i enabled the task of USB the FreeRTOS+TCP don’t receive nothing. But the function FreeRTOS\_gethostbyname works.

what may be happening?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [December 16, 2019, 10:27pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/14 "2019-12-16T22:27:24Z")

</div>

I think there are a lot of things that may be happening, from issues in the USB code, memory starvation, issues with USB interrupt priories, etc… If you are able to provide more information on the behaviour you observe we will be able to narrow down the possibilities. You say FreeRTOS\_gethostbyname() works - so networking is working to start with - is that before or after USB activity? Does the issue happen just by creating the USB task, or do you actually need to plug in the USB cable? Once the issue has happened, does the TCP/IP stack still receive incoming packets? Etc.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 17, 2019, 4:59am UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/15 "2019-12-17T04:59:23Z")

</div>

`FreeRTOS_gethostbyname ()` : mind you that if `ipconfigUSE_DNS_CACHE` is defined, the results of lookups will be stored in cache.  
While testing, it is useful to call `FreeRTOS_dnsclear()` before each lookup.

USB disturbing: would it be possible to get TCP/IP totally running before adding USB? As Richard comments, there might be many reasons why one feature disturbs something else.

> - pcBufferToTransmit : i changed like you suggest after my last post.
> - pcBufferToReceive : i done it too.

Does this code work well now?

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 17, 2019, 12:55pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/16 "2019-12-17T12:55:10Z")

</div>

> [@htibosch](#):
>
> - pcBufferToTransmit : i changed like you suggest after my last post.
> - pcBufferToReceive : i done it too.

yes, work well now without USB.

> [@htibosch](#):
>
> `FreeRTOS_gethostbyname ()` : mind you that if `ipconfigUSE_DNS_CACHE` is defined, the results of lookups will be stored in cache.

But that’s after reboot? restart all system.

> [@rtel](#):
>
> If you are able to provide more information on the behaviour you observe we will be able to narrow down the possibilities. You say FreeRTOS\_gethostbyname() works - so networking is working to start with - is that before or after USB activity?

Yes, i thinking the network is working well, but some buffer was corrupting in runtime.  
First i call _FreeRTOS\_IPInit_ and after i create all tasks, including USB. The task IOT is called on _vApplicationIPNetworkEventHook_. The USB task initialize the driver USB, but not mount the until if it isn’t connected. The problem occour without plug usb. The USB used here is Mass Storage. The code called inside is this:

```
USB_Init(FlashDisk_MS_Interface.Config.PortNumber, USB_MODE_Host);
fs_usb = malloc(sizeof (FATFS));

```

> [@rtel](#):
>
> Once the issue has happened, does the TCP/IP stack still receive incoming packets.

I don’t know, because i’m not a expert on ethernet, i tried get but i didn’t catch nothing. I saw that the interrupt keeps working.

I will create a repo for share the code.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 17, 2019, 1:09pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/17 "2019-12-17T13:09:41Z")

</div>

Hi,

I put two debug on uart to see, on NetworkInterface.

```
memcpy(pxNetworkDescriptor->pucEthernetBuffer, 
(void*)VacEnetData.RxDescriptor[ConsumeIdx].Packet, xBytesReceived);;
pxNetworkDescriptor->xDataLength = xBytesReceived;
DEBUGOUT("DATARECEIVED:%d\n",pxNetworkDescriptor->xDataLength);
/* pvData is used to point to the network buffer descriptor that now references the received data. */
xRxEvent.pvData = (void *) pxNetworkDescriptor;
DEBUGOUT("DATA:%s\n",xRxEvent.pvData);

```

Always they are called, without USB the ‘DATARECEIVED’ appers two times, but when USB is enabled both 64 bytes, that is called three times, first 64 bytes, second the length of receive and third 64 bytes too.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 17, 2019, 1:18pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/18 "2019-12-17T13:18:50Z")

</div>

Here are the source.  
[https://bitbucket.org/ulyssesfonseca/freertos-tcp-lpc1788](https://bitbucket.org/ulyssesfonseca/freertos-tcp-lpc1788)

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 17, 2019, 1:51pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/19 "2019-12-17T13:51:23Z")

</div>

Hi,

I tried, create USB task before IOT was working. So after the first connection success, send and receive, i create the task USB. But after this the IOT don’t receive more.

---

<div class="post-metadata">

**Author:** ![ulysses](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ulysses](https://forums.freertos.org/u/ulysses)\
**Post date:** [December 17, 2019, 3:34pm UTC](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377/20 "2019-12-17T15:34:37Z")

</div>

Hi,

Problem solved.  
What was happend, the _configTOTAL\_HEAP\_SIZE_ is few for the new tasks, so i increase this value and work.  
Thanks for all helped me.

[Next page](https://forums.freertos.org/t/freertos-tcp-for-lpc17xx/8377.md?page=2)
