Nios® V Processor Software Developer Handbook

ID 743810
Date 10/06/2025
Public
Document Table of Contents

15.7. Using the FreeRTOS-Plus-TCP Protocol Stack

Altera provides an example on how to use FreeRTOS-Plus-TCP protocol stack in Altera FPGA Development Kit. You can customize the source code based on your design requirements.

The first API to use is the FreeRTOS_IPInit(), which can be found in FreeRTOS_IP.h file. This API initializes the TCP/IP stack with an IP, subnet, gateway, DNS, and MAC. A code snippet can be seen below:
void vInitialiseNetworkInterface()
{
    const uint8_t ucIPAddress[ 4 ]  = { 192, 168, 1, 40 };
    const uint8_t ucNetMask[ 4 ]    = { 255, 255, 255, 0 };
    const uint8_t ucGateway[ 4 ]    = { 192, 168, 1, 1 };
    const uint8_t ucDNSServer[ 4 ]  = { 8, 8, 8, 8 };
    const uint8_t ucMACAddress[ 6 ] = { 0x00, 0x1C,0x23, 0x17, 0x4A, 0xCB };

	BaseType_t ret = FreeRTOS_IPInit(
        ucIPAddress,
        ucNetMask,
        ucGateway,
        ucDNSServer,
        ucMACAddress
    );
} 
The next step is to create a task, which can be done via using xTaskCreate().
    if (pdFAIL == xTaskCreate( vPingTestTask, "vPingTestTask", 
PING_TASK_STACKSIZE, NULL, PING_TASK_PRIORITY, NULL )){
		printf("Ping Task creation fail!!!!\n");
	}

vTaskStartScheduler() is a critical part of the FreeRTOS application that marks the transition from initialization to actual multitasking execution. It initializes the RTOS tick timer, starts executing the highest priority task that is ready to run. This API can be found in the task.h header file.

vPingTestTask() demonstrates how to use FreeRTOS-Plus-TCP utility APIs to send a ping request and handle the result. This function is designed to be run as a FreeRTOS task.
void vPingTestTask(void *pvParameters)
{
    const uint8_t ucIPAddressToPing[4] = { 192, 168, 1, 50 };
    uint32_t ucIPAddress;
    BaseType_t result = FreeRTOS_inet_pton(FREERTOS_AF_INET, "192.168.1.50", &ucIPAddress);

    const TickType_t xDelayBetweenPings = pdMS_TO_TICKS(5000);

    for (;;)
    {
        BaseType_t xResult = FreeRTOS_SendPingRequest(ucIPAddress,
                                                      32,          // Ping data size
                                                      1000000);    // Timeout in ms
        if (xResult != pdFAIL)
        {
            printf("[PING] Success: %u.%u.%u.%u Identifier %u\n",
                   ucIPAddressToPing[0], ucIPAddressToPing[1],
                   ucIPAddressToPing[2], ucIPAddressToPing[3], xResult);
        }
        else
        {
            printf("[PING] Failed: %u.%u.%u.%u\n",
                   ucIPAddressToPing[0], ucIPAddressToPing[1],
                   ucIPAddressToPing[2], ucIPAddressToPing[3]);
        }
        vTaskDelay(xDelayBetweenPings);
    }
}
vApplicationIPNetworkEventHook() is a user-defined callback function that FreeRTOS-Plus-TCP calls when the network interface changes state — for example, when the device comes online or loses connectivity.
void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
{
    int8_t cBuffer[ 16 ];  
    if (eNetworkEvent == eNetworkUp)  {
        FreeRTOS_printf(("Network is UP!\n"));

        /* Optional: print IP address */
        uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress;  
        FreeRTOS_GetAddressConfiguration(
            &ulIPAddress,
            &ulNetMask,
            &ulGatewayAddress,
            &ulDNSServerAddress
        );
        FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );  
        printf( "IP Address: %s\n", cBuffer );  
        /* Convert the net mask to a string then print it out. */  
        FreeRTOS_inet_ntoa( ulNetMask, cBuffer );  
        printf( "Subnet Mask: %s\n", cBuffer );  
        /* Convert the IP address of the gateway to a string then print it out. */  
        FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );  
        printf( "Gateway IP Address: %s\n", cBuffer );  
        /* Convert the IP address of the DNS server to a string then print it out. */  
        FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );  
        printf( "DNS server IP Address: %s\n", cBuffer ); 
        printf("MAC ID 1 reg = 0x%x\n", TSE_MAC_REGISTERS->mac_id_1);
        printf("MAC ID 2 reg = 0x%x\n", TSE_MAC_REGISTERS->mac_id_2);
        printf("PHY LINK status = %ld\n", tse_phy_link_up());
        printf("PHY STATUS reg = 0x%x\n", read_phy_register(TSE_MAC_BASE_ADDRESS, STATUS_REG));
        printf("PHY ID 0 reg = 0x%x\n", read_phy_register(TSE_MAC_BASE_ADDRESS, PHY_IDENTIFIER_0));
        printf("PHY ID 1 reg = 0x%x\n", read_phy_register(TSE_MAC_BASE_ADDRESS, PHY_IDENTIFIER_1));
    }
}