Memory Allocation Fails on FreeRTOS_Socket Call

Now, I have this:

    memset( &xBindAddress, 0 , sizeof(xBindAddress) );
    xBindAddress.sin_len = sizeof(xBindAddress);
    xBindAddress.sin_family = FREERTOS_AF_INET;
    xBindAddress.sin_flowinfo = 0;  // not used?
    xBindAddress.sin_port = ( uint16_t ) usPort;
    xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
    xBindAddress.sin_address.ulIP_IPv4 = FreeRTOS_inet_addr_quick(ucIPAddress[0], ucIPAddress[1], ucIPAddress[2], ucIPAddress[3]);

    rv = FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );

Still no luck. The socket call, bind call, and listen call all return without errors. The accept call doesn’t return and I don’t see this device on my router.

What do you mean by that? Does your target respond to ping requests?

@dougr hint: It’s better to enclose code blocks by 3 tildes ‘~’ or backticks ‘´’.
For intraline code you could use the </> Preformated text button.

No response with ping requests.

My code prints out pass/fail info after the accept call. I never see this so this call is not returning.

My router lists all connected devices. My target doesn’t show up.

hs2, I didn’t know that. Here it is again.

uint8_t ucMACAddress[6] = { 0x00, 0x0A, 0x35, 0xE5, 0xE5, 0xE5 };
uint8_t ucIPAddress[4] = { 192, 168, 1, 69 };
uint8_t ucNetMask[4] = { 255, 255, 255, 0 };
uint8_t ucGatewayAddress[4] = { 10, 10, 10, 1 };
uint8_t ucDNSServerAddress[4] = { 208, 67, 222, 222 };
uint16_t usPort = 45678;
const BaseType_t xBacklog = 20;

NetworkInterface_t xInterfaces[ 1 ];
NetworkEndPoint_t xEndPoints[ 4 ];
Socket_t xListeningSocket;
Socket_t xConnectedSocket;

int main()
{
 	pxZynq_FillInterfaceDescriptor(0, &( xInterfaces[0]));

 	FreeRTOS_FillEndPoint( &( xInterfaces[ 0 ] ), &( xEndPoints[ 0 ] ), ucIPAddress,
 			ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );

 	FreeRTOS_IPInit_Multi();

 	vTaskStartScheduler();
}

void vApplicationIPNetworkEventHook_Multi( eIPCallbackEvent_t eNetworkEvent, struct xNetworkEndPoint * pxEndPoint )
{
	static BaseType_t xTasksAlreadyCreated = pdFALSE;

	printf("vApplicationIPNetworkEventHook_Multi\n\r");

	/* Check this was a network up event, as opposed to a network down event. */
	if( eNetworkEvent == eNetworkUp ) {
		/* Create the tasks that use the TCP/IP stack if they have not already been created. */
		if( xTasksAlreadyCreated == pdFALSE ) {
			/* Create the tasks here. */
            xTaskCreate( NetworkUpTask,                     /* The function that implements the task. */
                         "NetworkUpTask",                   /* Just a text name for the task to aid debugging. */
						 ipconfigIP_TASK_STACK_SIZE_WORDS,  /* The stack size is defined in FreeRTOSIPConfig.h. */
                         NULL,                              /* The task parameter, not used in this case. */
						 ipconfigIP_TASK_PRIORITY,          /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
                         NULL );                            /* The task handle is not used. */

			xTasksAlreadyCreated = pdTRUE;
			FreeRTOS_printf( ( "Application idle hook network up\n" ) );
		}

	}
    else {
        FreeRTOS_printf( ( "Application idle hook network down\n" ) );
    }
}



static void NetworkUpTask( void * pvParameters )
{
	struct freertos_sockaddr xBindAddress;
	struct freertos_sockaddr xClient;
	socklen_t xSize = sizeof( xClient );
	static const TickType_t xReceiveTimeOut = portMAX_DELAY;
	const BaseType_t xBacklog = 20;
	static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 2000 );
	static char cRXData[ RX_IP_BUFFER_SIZE ];
	BaseType_t lBytesReceived;
	BaseType_t rv;

	printf("NetworkUpTask\n\r");

	xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET4,
			FREERTOS_SOCK_STREAM,
			FREERTOS_IPPROTO_TCP );
    configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
    if(xListeningSocket != FREERTOS_INVALID_SOCKET) {
    	printf("Listening Socket created!\n\r");
    }
    else {
    	printf("ERROR: Listening Socket could not be created!\n\r");
    }

    rv = FreeRTOS_setsockopt( xListeningSocket,
    		0,
			FREERTOS_SO_RCVTIMEO,
			&xReceiveTimeOut,
			sizeof( xReceiveTimeOut ) );
    if(rv == 0) {
    	printf("FreeRTOS_setsockopt FREERTOS_SO_RCVTIMEO set.\n\r");
    }
    else {
    	printf("ERROR: FreeRTOS_setsockopt FREERTOS_SO_RCVTIMEO failed.\n\r");
    }

    rv = FreeRTOS_setsockopt( xListeningSocket,
    		0,
			FREERTOS_SO_SNDTIMEO,
			&xSendTimeOut,
			sizeof( xSendTimeOut ) );
    if(rv == 0) {
    	printf("FreeRTOS_setsockopt FREERTOS_SO_SNDTIMEO set.\n\r");
    }
    else {
    	printf("ERROR: FreeRTOS_setsockopt FREERTOS_SO_SNDTIMEO failed.\n\r");
    }

    memset( &xBindAddress, 0 , sizeof(xBindAddress) );
    xBindAddress.sin_len = sizeof(xBindAddress);
    xBindAddress.sin_family = FREERTOS_AF_INET;
    xBindAddress.sin_flowinfo = 0;  // not used?
    xBindAddress.sin_port = ( uint16_t ) usPort;
    xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
    xBindAddress.sin_address.ulIP_IPv4 = FreeRTOS_inet_addr_quick(ucIPAddress[0], ucIPAddress[1], ucIPAddress[2], ucIPAddress[3]);

    rv = FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
    if(rv == 0) {
    	printf("FreeRTOS_bind good.\n\r");
    }
    else {
    	printf("ERROR: FreeRTOS_bind failed.\n\r");
    }

    rv = FreeRTOS_listen( xListeningSocket, xBacklog );
    printf("FreeRTOS_listen: %ld\n\r", rv);\

    for( ; ; )
    {
        xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize );
        configASSERT( xConnectedSocket != FREERTOS_INVALID_SOCKET );
        if(xConnectedSocket != FREERTOS_INVALID_SOCKET) {
        	printf("FreeRTOS_accept good.\n\r");
        }
        else {
        	printf("ERROR: FreeRTOS_accept failed.\n\r");
        }

        lBytesReceived = FreeRTOS_recv( xConnectedSocket, &cRXData, RX_IP_BUFFER_SIZE, 0 );

        if( lBytesReceived > 0 )
        {
            /* Data was received, process it here. */
            printf("TCP RX: %s\n\r", cRXData);
        }
        else if( lBytesReceived == 0 )
        {
            /* No data was received, but FreeRTOS_recv() did not return an error.
            Timeout? */
        }
        else
        {
            /* Error (maybe the connected socket already shut down the socket?).
            Attempt graceful shutdown. */
            FreeRTOS_shutdown( xListeningSocket, FREERTOS_SHUT_RDWR );
            break;
        }
    }

}

well now, if your target does not have connectivity at all, we may have been barking up the wrong tree all along. Take a few steps back.

Has your target ever responded to pings, eg before you did anything in your server code? If yes, is your target stuck at a fault after those changes? Is your sys tick still running (bp on sys tick handler is invoked)?

No, this target never responded to pings. However, I tried some older code running PetaLinux and it responds properly so I know the hardware/cabling is okay.

No, I still see other tasks running and printing on the console. I’m not sure how to check the sys tick. What is bp on sys handler?

Do I need to call FreeRTOS_connect()?

I’m looking over the github code: https://github.com/FreeRTOS/FreeRTOS/blob/main/FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Echo_Posix/TCPEchoClient_SingleTasks.c

I see FreeRTOS_connect() being called after FreeRTOS_socket() and FreeRTOS_setsockopt().

Forget all about connect(), accept() and the whole bit for the time being. There is no point in it if you do nt have basic connectivity. Without your target responding to pings, there is no need in looking at TCP.

So, what do you suggest?

the usual strategy: Strip down your application to everything but the network setup, then pinpoint the reason why you do not have connectivity. May be the network driver setup (something like MII vs. RMII, PHY address selection), your end point configuration or simply an unreachable node due to your PC and the target being in disjoint subnets. Several possible reasons. If you are not familiar with subnets, IP addresses and other network infra structure basics, read up on them. Enable stack logging at the highest possible level and try to determine whether your stack sees incoming packets from the net at all.

My application is pretty stripped down as is.

At what point in the code should the target respond to a ping? After FreeRTOS_socket()?

I’m getting a eNetworkUp event which creates the NetworkUpTask which allows calling FreeRTOS_socket successfully. Previously I was getting errors on this socket call.

I’m pretty familiar with networking stuff–not an expert by any means. I coded the network interface successfully in PetaLinux.

If ipconfigREPLY_TO_INCOMING_PINGS is enabled in your FreeRTOSIPConfig(Defaults).h the stack should do automatically after the network is up. There is no socket needed for ping ICMP messages.

Apologies, no offense implied. I was simply irritated by the fact that there has been a support exchange over 40+ posts that acted in an (as of yet) totally irrelevant corner - sort of like troubleshooting tires when the engine doesn’t even start…

If your board is prefabbed, I strongly recommend looking for a network blinky sample app for your toolset from the manufacturer to ensure that you get the basic connectivity to work, ie make sure your network driver configuration matches the board. If that is not an option, try setting a breakpoint at your NICs Rx ISR to check whether you get receive interrupts at all, then work your way up the network stack.

hs2: Yes, I added this to FreeRTOSIPConfig.h, but I think it was already set:
#define ipconfigREPLY_TO_INCOMING_PINGS 1
In any case, no luck.

RAc: No offense was taken. I just wanted to inform everyone that I’ve done this before with PetaLinux. With PetaLinux, you don’t need to start all those tasks and do all the low level configuration.

I thought I was clear that I was not getting basic connectivity. No pings. No target shown in the router Attached Devices list.

Unfortunately, I have a custom design using a PicoZed SOM plugged into custom PCB.

I’m not sure where the NIC RXISR is located.

I wonder if using WireShark would help?

I’m very thankful for all the support I’ve received!

It would only help if it did reveal any outbound traffic which you could for example enforce by temprarily cnfiguring your target as a DHCP client. If you did this but still see no packets from your target on the net, the next thing to look at would be the network driver, for example, as mentioned above, to see whether the MII/RMII configuration, PHY address and -configuration matches the wiring.

If that is looking ok, again, the next thing to look for is whether you get interrupts from your network at all, there may also be a misconfiguration in, for example, interrupt vector setup.

In any case, you will likely need to dive fairly deeply into the system down to MCU addressing level. Have you queried this forum for similar questions? We tend to have a good deal of network driver issues, maybe there is something in there for you…

Did your PetaLinux implementation run on the same custom board, so you can positively exclude design or wiring problems on your board?

RAc: Did your PetaLinux implementation run on the same custom board, so you can positively exclude design or wiring problems on your board?

Yes, I am running the same hardware. When I power up the board, it loads my previous design running PetaLinux which act correctly.

Next thing I would do then is compare the driver code for your network hardware. Depending on what eco system/tool set you are using, it may also be an option to load both the Linux and FreeRTOS images and then dump all relevant SFRs, then compare those.

Do you know where the ISR is located in the source code?

Do I have to manually enable and setup the ISR?

Sorry, no, I do not know your toolset. As I am sure you know, there is an interrupt vector table located (generally) at address 0, or 0x8000000 at Cortex-M pods. YMMV. You can look at your linker output map file, it should tell you what source file that location belongs to, then consult with your MCUs reference manual at which offset your network ISRs are located (always assuming you are not using external ethernet coprocessors).

You may need additional steps to setup your network.

How close is your wiring to an existing eval board for your MCU? Can you try to locate networking sample code for such an eval board and try to get that to run?