lpcware

FreeRTOS LwIP task RAM usage LPC1768

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by FlySnake on Tue Jul 10 10:56:56 MST 2012
Hi everyone!
I have a problem with RAM usage by task. My application is based on FreeRTOS+IO Featured Demo #2 http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_IO/Demo_Applications/LPCXpresso_LPC1769/NXP_LPC1769_Demo_Description.shtml
The application is a tcp server:
static void accept_thread(void *socket)
{
    int32_t sock = *((int32_t *)socket);
    uint_fast8_t exit_flag = 1;
    while(exit_flag)
    {
        char temp[20] = {0};
        sprintf(temp, "s %d h %d s %d\n", sock, xPortGetFreeHeapSize(), uxTaskGetStackHighWaterMark(NULL));
        lwip_send(sock, temp, strlen(temp), 0);
        uint8_t inbyte = 0;
        int32_t recvret = lwip_recv(sock, &inbyte, sizeof(inbyte), 0);
        if(recvret > 0u)
        {
            const char * const welcome = "hello\n";
            lwip_send(sock, welcome, strlen(welcome), 0);
        }
        else
        {
            exit_flag = 0;
        }
    }
    lwip_close(sock);
    vTaskDelete(NULL);
}

void server_main(void *parameters)
{
    int32_t socket = lwip_socket(AF_INET, SOCK_STREAM, 0);
    int32_t client_socket;
    struct sockaddr_in local_addr, client_addr;
    size_t client_addr_size = sizeof(client_addr);

    TSPRINTF("Server started\n");

    if(socket >= 0)
    {
        memset((void *)&local_addr, 0, sizeof(local_addr));
        local_addr.sin_family = AF_INET;
        local_addr.sin_len = sizeof(local_addr);
        local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        local_addr.sin_port = ntohs(((uint16_t)6666));
        if(lwip_bind(socket, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0 )
        {
            lwip_close(socket);
            TSPRINTF("Cannot bind port\n");
            vTaskDelete(NULL);
        }

        if(lwip_listen(socket, 20) != 0 )
        {
            lwip_close(socket);
            TSPRINTF("Cannot listen port\n");
            vTaskDelete(NULL);
        }
        uint16_t counter_conns = 0;
        while(1)
        {
            memset((void *)&client_addr, 0, sizeof(client_addr));
            client_socket = 0;
            client_socket = lwip_accept(socket, (struct sockaddr *)&client_addr, &client_addr_size);
            if(client_socket > 0u)
            {
                int32_t temp_socket = client_socket;
                if(xTaskCreate(accept_thread, "accept thread", 128 , (void *)&temp_socket, 2, NULL) == pdPASS)
                {
                    TSPRINTF("Created %d connection\n", ++counter_conns);
                }
                else
                {
                    TSPRINTF("Cannot create connection\n");
                }
            }
        }
    }
    TSPRINTF("Destroy server task\n");
    vTaskDelete(NULL);
}

But every new connection i.e. new task "accept_thread" FreeRTOS's heap (heap2 scheme) decreases by 880 bytes. EMAC buffers and FreeRTOS's heap located in RamAHB32. All other LwIP buffers in RamLoc32, but they are nothing to do with FreeRTOS heap and tasks. According to http://www.freertos.org/FAQMem.html#RAMUse each task should consume not more than (60 + name length + stack size) = 201 byte. What am I doing wrong?

Outcomes