FreeRTOS+TCP for Renesas RX63N

Hi, I made FreeRTOS+TCP work on a RX63N. Perhaps it was already done before, but I could not find it. If anyone is interrested please let me know and I will send it. Peter

FreeRTOS+TCP for Renesas RX63N

FreeRTOS+TCP for Renesas RX63N

Hi Peter! Some time ago, I started with a port for RX62N. Together with Hein, it finally also worked, but it consumed too much memory on my system to be used usefully, so I switched back to uIP. How much memory do you reserve for it? Regards, Stephan

FreeRTOS+TCP for Renesas RX63N

Hi Stephan, Yes, memory consumption is a point of worry for me as well. To give you an idea: I made an application with a very simple CLI, both over uart as well as over TCP. I assigned 64k ram to this application. I use the heap5 scheme, which assigns all free memory to the heap . at startup the heapsize is 57400 bytes Before ethernet is connected I have 36512 bytes available. After getting a IP address via DHCP I have 31352 available. (minimum ever 26720) After connecting to a simple TCP-CLI server I have 15600 available, (minimum ever 14056) After release from the simple TCP-CLI server I have 27248 available, (minimum ever 12512) I must say I haven’t invested any time in figuring out where the memory went, nor have I tried other IP-stacks. But judging from your story, I think I should, and probably will. Peter

FreeRTOS+TCP for Renesas RX63N

I just noticed I can attach files on a reply, so here it is.

FreeRTOS+TCP for Renesas RX63N

The memory used by the stack is very configurable. You can run with small MSS and no TCP windowing for low memory consumption, but also slow throughput, or larger MSS and TCP windowing for the opposite. For a CLI I would imagine a minimum memory configuration would suffice. https://www.freertos.org/FreeRTOS-Plus/FreeRTOSPlusTCP/TCPIPConfiguration_Examples.html

FreeRTOS+TCP for Renesas RX63N

Hi Peter Thank you for sharing the files! In my application, I need several connections in parallel and I ended up with too much memory usage. The RX62N I used has only 96KB of RAM, and my main application already needs more than 70K. So there is not much RAM remaining for the TCP/IP stack. I have not tried to reduce the MTU, but this is maybe a good approach. Best regards, Stephan

FreeRTOS+TCP for Renesas RX63N

Richard wrote:
The memory used by the stack is very configurable. You can run with small MSS and no TCP windowing for low memory consumption
Those are the best methods to reduce memory usage of +TCP. Adam Dunkels’ earlier TCP/IP stack uIP, was indeed using a minimum of RAM. I has a TCP window of 1 packet (MSS). It wants an ACK after each packet sent. FreeRTOS+TCP also has this “uIP” mode if you define: ~~~ #define ipconfigUSETCPWIN ( 0 ) ~~~ This will save lots of RAM, at the cost of some performance. You also mentioned decreasing the size of the MTU. That is indeed a very effective and compatible way of decreasing memory usage, for instance: ~~~ #define ipconfigNETWORK_MTU ( 512 ) ~~~ When MTU equals 1500 bytes, the MSS equals 1460. So when you’re playing with MTU, use ipconfigTCP_MSS in stead of the constant 1460: ~~~ #define ipconfigTCPRXBUFFERLENGTH ( 3 * ipconfigTCPMSS ) #define ipconfigTCPTXBUFFERLENGTH ( 2 * ipconfigTCPMSS ) ~~~ The above defines the default buffer sizes for reception and transmission. Do not define ipconfigTCP_MSS your self, it is already defined in FreeRTOSIPConfigDefaults.h. If you are using TCP windows, you may want to reduce : ~~~ #define ipconfigTCPWINSEG_COUNT 64 ~~~ FreeRTOS+TCP has a fixed amount of packet buffers. Each buffer can hold a complete Ethernet packet. You can choose to have all buffers allocated at start-up, or dynamically ( using pvPortMalloc() ). Please check BufferAllocation_1.c versus BufferAllocation_2.c. Regards, Hein

FreeRTOS+TCP for Renesas RX63N

Hi Stephan, Peter,
The RX62N I used has only 96KB of RAM, and my main application already needs more than 70K
Does that mean that 96 – 70 = 26 KB remains for TCP/IP ? That is well possible ! I just created a mini-project that creates a UDP port for logging, and a TCP port in listening mode. In this case, I prefer to use BufferAllocation_2.c, because it will malloc() just enough bytes and only when needed. The module BufferAllocation_1.c is preferable for high-performance systems, where there is plenty of RAM available. A few parameters for FreeRTOSIPConfig.h that help reducing the need for RAM: ~~~ /* The maximum number of network buffers: */

define ipconfigNUMNETWORKBUFFER_DESCRIPTORS ( 10 )

/* The Maximum Transmission Unit: */

define ipconfigNETWORK_MTU ( 512 + 40 )

/* Not using DHCP: */

define ipconfigUSE_DHCP 0

/* Each TCP socket has a buffer for RX and one for TX. Make them big enough to hold 1 packet: */

define ipconfigTCPRXBUFFERLENGTH ( 1 * ipconfigTCPMSS )

define ipconfigTCPTXBUFFERLENGTH ( 1 * ipconfigTCPMSS )

~~~ Measuring RAM/heap usage is always difficult. For my test, I was using an STM32F4. The results below are just an estimate and may vary among different compilers and platforms: Starting up the IP-task, creating lists and buffers: 17 KB Creating a UDP socket: 100 bytes Creating a TCP socket in listen mode: 376 bytes Listening socket gets connected and creates buffers: 1088 bytes( 2 x MSS ) DATA and BSS, i.e. memory claimed at compile time: 900 bytes. The listening socket was created with the socket option FREERTOS_SO_REUSE_LISTEN_SOCKET. That means that when a connection comes in, the listening socket turns into a connected socket. Normally, the listening stays in the listening state and it will accept() many connections. The amount of flash used by the +TCP and NetworkInterface.c : 21 KB.

FreeRTOS+TCP for Renesas RX63N

Hi Richard and Hein, Thank you for your input. As a small clarification, the program I have uploaded here with CLI is just a small test to get a working ethernet driver for the RX63N. I am now going to lift the driver over to an existing application that runs UDP at the moment. The RX63N I use has in fact 128kb of RAM, but like in Stephans case, I already use quite a lot of it. In the end I want to get MQTT running, ideally over SSL. I don’t need high throughput. Setting ipconfigUSETCPWIN to 0, and MTU size to 586 already saved 17kb or so of dynamic memory. This is good enough for me now. Disabling DHCP is not an option for me. (at least not at build time) I still have a question though: If I reduce the MTU size, whay will happen if the server I connect to wants to send me a message that is 1500 bytes? Will it get chopped into 3 frames in an underlying layer? Or will it get dropped on the floor?

FreeRTOS+TCP for Renesas RX63N

I still have a question though: If I reduce the MTU size, what will happen if the server I connect to wants to send me a message that is 1500 bytes? Will it get chopped into 3 frames in an underlying layer? Or will it get dropped on the floor?
Good question: For TCP it will work OK. In the SYN packets, there is an option that proposes an MSS for the connection. The peer may come up with 1460, but if your device wants 512, that value will be chosen by both parties. I think that there will be a problem if you need to respond to large (ICMP) ECHO commands, or if you want to use large UDP commands. The other party doesn’t know that your buffers are small (512+40 bytes in the example).
Will it get chopped into 3 frames in an underlying layer?
No, we stopped supporting fragmentation, because it is not often used anymore, and it made the code larger and more complex

FreeRTOS+TCP for Renesas RX63N

Ok, thanks for clearing that out. That also explains why DHCP has a minimum requirement for the MTU size(586 bytes).

FreeRTOS+TCP for Renesas RX63N

Hello Peter! Here are the files of my implementation for RX62N, at the state when I stopped. May it is interesting for you. In my application, I have various tasks sending and receiving network packets. For example, I have a DHCP and an NTP client, a HTTP server, an interface to the HMI, and a remote control interface. When I started the project, FreeRTOS+TCP was no available and I took uIP, but if you succeed with the small memory footprint of the RX, I maybe will give it a try, too. Best regards, Stephan

FreeRTOS+TCP for Renesas RX63N

Here are the files of my implementation for RX62N
Thanks a lot!
but if you succeed with the small memory footprint of the RX I maybe will give it a try, too.
uIP is using a minimum of RAM: it is using the same 1.5K buffer over and again. If your application runs well with “just enough memory”, you’d better stick to uIP. FreeRTOS+TCP is very configurable as I showed here above, but when it comes to saving bytes, uIP will win. I think that +TCP will feel more comfortable though, it is easier to understand its logic and the API’s are BSD compatible. And most of all, FreeRTOS+TCP is very well integrated into the FreeRTOS kernel. Regards.

FreeRTOS+TCP for Renesas RX63N

Hi Hein! Yes, you are right, uIP is not easy to program. Having real sockets it much better than using the PT_THREADS… Best regards, Stephan

FreeRTOS+TCP for Renesas RX63N

Hi, Just a quick update about the code I posted on 2018-01-22. I found an error in it, in that it didn’t handle fragmented packets properly. In fact these packets weren’t cleared from the low level driver, resulting in clogged up buffers. These problems started to show after reducing the MTU size. Some devices on our network were sending broadcasts every once in a while with packets greater than my MTU. And these packets are delivered to the software in fragments. Since these broadcast are not of interest to my device I can just ignore them, but I do have to make shure the buffers are cleared, which can be done by a small change in the function prvNetworkInterfaceInput( void ), in the file “FreeRTOS-Plus-TCPportableNetworkInterfaceRX63NNetworkInterface.c”: Change the first if- statement to: ~~~ if( ( pxDMARxDescriptor->status & ACT )==0 ) { /* Get the Frame Length of the received packet */ xReceivedLength = pxDMARxDescriptor->RFL;
    pucTemp = (uint8_t *) pxDMARxDescriptor->buf_p;

    if( xReceivedLength==0 )
    {
        //When small buffers are used, segmented packets can be expected.
        //These are usually broadcasts which can be ignored.
        //if a packet is not marked with an end bit, these will have zero length
        //So we consume all zero-length packets here.
        EMAC_UpdateRxConsumeIndex();
    }
}
else
{
    xReceivedLength = 0;
}
~~~ With this change I now have been running this driver for a couple of days now without a single problem, and the amount of RAM used is nothing to complain about. So thanks Stephan for sharing the uip driver, but I will proceed with the FreeRTOS+TCP driver, as I highly appriciate the BSD style too. regards, Peter

FreeRTOS+TCP for Renesas RX63N

Hi Peter! The code I have shared is also for FreeRTOS+TCP. I thought it maybe can be helpfull to have another implementation for you to compare. BTW, something off-topic: Do you use the USB ports of the RX63N with FreeRTOS? Best regards, Stephan

FreeRTOS+TCP for Renesas RX63N

Hi Stephan, oops, based on your comments I made a bad assumtion (note to self(again): Never Assume!) I will have a look at your code then. Event though I am pretty happy with how my driver works right now I do admit that the code is a bit messy. About the USB port: Unfortunately I do not use it. I do have the connector on board, but in our case it’s not really needed, so I never came around to start using it. I tried once but that was years ago, I don’t even know anymore how far I got.

FreeRTOS+TCP for Renesas RX63N

Hello Peter!
I do admit that the code is a bit messy. Mine does also not win a prize…
Regarding the USB: I used the code supplied by Renesas, but it does not work very well. Tha’t s why I asked. Thanks & best regards, Stephan