FreeRtos + TCP accept internal PHY loopback?

Good morning,
I’m working on an automatique UDP test, for testing the TCP stack, MAC and PHY without being connected to local network and configure an other machine to receive our UDP packet. So i activated the internal PHY loopback, and all the packets sent are reflected to the MAC (verified by an oscilloscope ) and forwarded to the network interface (I catch receive interrupts whenever ping or UDP packet are sent ), but i don’t get any response with FreeRTOS_recvfrom() , apparently these packets are rejected somewhere in the IP-TASK because of the IP address conflict.
Do you have please some suggestions or ideas ?
Thanks in advance.

Are you able to set a break point where the packet is received back then follow the packet through the stack to see where it is filtered out?

Hi Richard,
Thanks for your advise, I actually did what you say, and i figure out that the packets looped back was rejected because of the wrong IP and port number, i should put mine IP address and the same port number for UDP applications (client and server).
So that prouves that Freertos+TCP works fine with the internal PHY loopback without any adaptation.

There is also a possibility to create a soft loopback. Please have a look at the function xCheckLoopback() in FreeRTOS_ARP.c.

You can call the function from xNetworkInterfaceOutput() in your NetporkInterface.c

Here is an example:

BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxBuffer,
                                    BaseType_t bReleaseAfterSend )
    if( xCheckLoopback( pxBuffer, bReleaseAfterSend ) != 0 )
        /* The packet has been sent back to the IP-task.
         * The IP-task will further handle it.
         * Do not release the descriptor. */
        return pdTRUE;
    /* Do the normal code. */

This software loopback can be used with any protocol. E.g. you can make a TCP connection to your own sockets.

I also wonder why your hardware loopback doesn’t work. Does ARP work correctly? Does it find the correct MAC-address? Can you break the code in xNetworkInterfaceOutput() and see if the MAC-address is correct?

Hi htibosch,
Thanks for your answer, finally the internal phy loopback works perfectly with the IP stack of freertos, it was my mistake to forget update the IP address and port number in my UDP (client and server ).
The purpose of that loopback is the to test the whole system ( IP Stack + Ethernet MAC + PHY ) without the need of using and configuring any other external devices.

I’m working right now in your solution of Software loopback using the

xCheckLoopback( pxDescriptor, bReleaseAfterSend )

function, The ping to the board IP address (localhost) is working perfectly. but i didn’t yet test the other protocols.

Thanks for reporting back. With the loopback you can test all protocols (TCP, UDP, ARP, ICMP/echo), but it won’t go through the MAC and PHY. But it is a good tool to test your own code without the need for 2 devices.

Hi Hein,
I tested the software loopback to Ping my own IP address (ICMP) and with UDP protocol, it work perfectely, but i needed to add the following lines

if( *pulIPAddress == *ipLOCAL_IP_ADDRESS_POINTER )
	/* Targeted at this device? Make sure that xNetworkInterfaceOutput()
	in NetworkInterface.c calls xCheckLoopback(). */
	memcpy( pxMACAddress->ucBytes, ipLOCAL_MAC_ADDRESS, ipMAC_ADDRESS_LENGTH_BYTES );
	eReturn = eARPCacheHit;

in the *eARPGetCacheEntry( uint32_t pulIPAddress, MACAddress_t * const pxMACAddress ) function to get he mac address instead of sending ARP packet to network to get the mac address.

Yes, very good, I forgot to mention it. I had made the same addition my self.

The normal ARP resolution can not be used because they go to the physical network.

I will make a PR for this so that the loopback can be used by anyone.

I think it’s stable and it can be used by anyone.

B.t.w. I just created a PR in which the loopback change is included:

“FreeRTOS_ARP.c : store local addresses only #120”.