FreeRTOS LwIP task RAM usage LPC1768

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FreeRTOS LwIP task RAM usage LPC1768

597 Views
lpcware
NXP Employee
NXP Employee
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_LPC1...
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?
0 Kudos
1 Reply

380 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Wed Jul 11 05:42:17 MST 2012
I'm fool :(

Quote:
Each task has its own unique stack that is allocated by the kernel to the task
when the task is created. The usStackDepth value tells the kernel how large
to make the stack.
The value specifies the number of words the stack can hold, not the number
of bytes. For example, on an architecture with a 4 byte stack width, if
usStackDepth is passed in as 100, then 400 bytes of stack space will be
allocated (100 * 4 bytes). The stack depth multiplied by the stack width must
not exceed the maximum value that can be contained in a variable of type
size_t.


But, anyway, I pass 128 to xTaskCreate(), thus 128 * 4 = 512 bytes + 60 bytes TCB + task name length (13). It's not 880 bytes yet.
0 Kudos