Please help FreeRTOS+TCP RA6M3

I am using an EK-RA6M3 to test TCPIP communications.
I was able to confirm that FreeRTOS_accept() was executed and a connection to the PC was established.
Unable to receive data.
lBytes=0 or -12
Screenshot attached.
Please let me know if you find out the cause.

This text is created using translation software.
Sorry if it’s hard to understand.

What is the value of xReceiveTimeout? Can you try increasing it? Also, is it possible to capture network traffic?

In your code, you are attempting to set timeouts for a socket that is not existing yet. Aside from what @aggarg wrote: Try putting the calls to setsockopt after the accept.

Also, what is the scope of your xConnectedSocket variable? If it is global, is there a chance of multiple tasks accessing it concurrently? xConnectedSocket is a poster case for a task local variable. Finally, what is the purpose of the FreeRTOS_Set_ip call in that function? Shouldn’t that be before the listen()?

1 Like

Thank you for your response.
value is set as follows.
static const TickType_t xReceiveTimeOut = pdMS_TO_TICKS( 5000 );

I attach a capture.
I sent “12345” from my PC to the board.
Thank you

Thank you for your response.

Do you mean remove the setsockopt before the accept and add it after?
i will try.

set_FreeRTOS_ip() is executed before listen().
Listen() is executed inside createTCPServerSocket().

“scope of your xConnectedSocket variable”
I don’t know what to answer.
Please tell me what to check.

Thank you

I removed the setsockopt before the accept and add it after.
Results were the same.
Unable to receive data.
lBytes=0 or -12

Thank you

“lBytes = -12” has been resolved.
I increased the size of “configTOTAL_HEAP_SIZE”.
#define configTOTAL_HEAP_SIZE (8192) → (16384)

“lBytes=0” is not resolved.

Thank you

To me it looks as if the sequence and acknowledgment numbers of your data stream are incorrect. This is most likely related to an incomplete implementation of your random number generator. Sorry I do not have the details, but this issue has been discussed on the forum before, please query.

Have you implemented ulApplicationGetNextSequenceNumber? Also, can you share your complete code? I do not have this platform but I can try it on a different platform to see if there is an application issue?

Thank you for your response.

I confirmed that “ulApplicationGetNextSequenceNumber” is in the code.

Can you tell me how to share the code?

Thank you

My level has increased and I can now upload codes.

new_thread0_entry.c (6.0 KB)

Sorry to bother you, but please check my code.

thank you

show us the implementation of that function.


FreeRTOS_TCP_State_Handling_IPv4.c (9.2 KB)
FreeRTOS_TCP_Transmission_IPv4.c (22.2 KB)
NetworkInterface.c (17.6 KB)


I forgot to translate it earlier.
Do you mean upload all the code?
I Upload all. (3.0 MB)

Sorry, I don’t have time to wade through all the code. What is your implementation of xApplicationGetRandomNumber()?

Also, it appears that your peer does not compute a TCP checksum over its payload field which might cause FreeRTOS+TCP to drop the incoming packet. There should be a way to disable check sum checking, can you try that just to see if that is what happens?

With some minor modifications in your code, I am able to run a server and talk to it using telnet -

Socket_t xListeningSocket;

#define tcpechoSHUTDOWN_DELAY    ( pdMS_TO_TICKS( 5000 ) )

BaseType_t createTCPServerSocket(void)
    BaseType_t status;
    const BaseType_t xBacklog = 20;
    struct freertos_sockaddr xBindAddress;

    xListeningSocket = FreeRTOS_socket(FREERTOS_AF_INET, /* Or FREERTOS_AF_INET6 for IPv6. */
                                       FREERTOS_SOCK_STREAM,  /* SOCK_STREAM for TCP. */
                                       FREERTOS_IPPROTO_TCP );
    /* Check the socket was created. */
    configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );

    /* Set the listening port to 8080. */
    xBindAddress.sin_port = ( uint16_t ) 8080;
    xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
    xBindAddress.sin_family = FREERTOS_AF_INET;

    status = FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
    FreeRTOS_printf(("Bind Status is %d \r\n",status));

    status = FreeRTOS_listen( xListeningSocket, xBacklog );
    FreeRTOS_printf(("Listen Status is %d \r\n",status));
    return status;

void new_thread0_entry(void *pvParameters)
    int32_t lBytes, lSent, lTotalSent;
    TickType_t xTimeOnShutdown;
    const TickType_t xReceiveTimeOut = pdMS_TO_TICKS( 5000 );
    const TickType_t xSendTimeOut = pdMS_TO_TICKS( 5000 );
    uint8_t *pucRxBuffer;
    Socket_t xConnectedSocket;
    struct freertos_sockaddr xClient;
    socklen_t xSize = sizeof( xClient );


    for( ;; )
        /* Wait for a client to connect. */
        xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize );
        configASSERT( xConnectedSocket != FREERTOS_INVALID_SOCKET );

        pucRxBuffer = ( uint8_t * ) pvPortMalloc( ipconfigTCP_MSS );
        configASSERT( pucRxBuffer != NULL );

        FreeRTOS_setsockopt( xConnectedSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
        FreeRTOS_setsockopt( xConnectedSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xReceiveTimeOut ) );

            /* Zero out the receive array so there is NULL at the end of the string
            when it is printed out. */
            memset( pucRxBuffer, 0x00, ipconfigTCP_MSS );

            /* Receive data on the socket. */
            lBytes = FreeRTOS_recv( xConnectedSocket, pucRxBuffer, ipconfigTCP_MSS, 0 );

            /* If data was received, echo it back. */
            if( lBytes >= 0 )
                lSent = 0;
                lTotalSent = 0;

                /* Call send() until all the data has been sent. */
                while( ( lSent >= 0 ) && ( lTotalSent < lBytes ) )

                    lSent = FreeRTOS_send( xConnectedSocket, pucRxBuffer, lBytes - lTotalSent, 0 );
                    if (lSent > 0)
                        /* Data was sentsuccessfully */
                        lTotalSent += lSent;
                        /* there was an error - break */

                }  /* while loop */
                /* Socket closed? */

            /* Initiate a shutdown in case it has not already been initiated. */
            FreeRTOS_shutdown( xConnectedSocket, FREERTOS_SHUT_RDWR );

            /* Wait for the shutdown to take effect, indicated by FreeRTOS_recv()
            returning an error. */
            xTimeOnShutdown = xTaskGetTickCount();
                if( FreeRTOS_recv( xConnectedSocket, pucRxBuffer, ipconfigTCP_MSS, 0 ) < 0 )
            } while( ( xTaskGetTickCount() - xTimeOnShutdown ) < tcpechoSHUTDOWN_DELAY );

            /* Finished with the socket, buffer, the task. */
            vPortFree( pucRxBuffer );
            FreeRTOS_closesocket( xConnectedSocket );

You should check if your TCP stack is properly up. Are you able to ping your device? Can you try out a client example?

from the pcap posted earlier as a scrennshot, it looks as if the 3 way handshake succeeds, but payload from the peer is not acknowledged, hence my questions about TCP possibly dropping the incoming payload.

Thank you for your reply.

I fixed my code to the code you uploaded.
It didn’t work.
I’ll fix it little by little and check it.

thank you

I have confirmed data transmission and reception.
by mixing the original code and the code I received.

Thank you very much for your help.

If I have any further questions I’ll ask in a new thread.

Thanks for reporting back!

Would you mind sharing the changes you needed to make to your code work?