This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SYSBIOS: Dynamically created task fails memory allocation for task stack

Part Number: SYSBIOS
Other Parts Discussed in Thread: 66AK2G12,

Hi,

I'm using:

66AK2G12
CCS 10.2.0
Processor SDK RTOS K2G 6.03.00.106, including PDK K2G 1.0.16
XDCtools 3.61.02.27
SYS/BIOS 6.76.03.01

In main() I am doing this:

int main(void)
{
    Task_Params task_params;
    Task_Handle task0 = NULL;
    Error_Block task0_eb;
    EDMA3_DRV_Handle edma_handle = NULL;

    // Initialize DMA driver
    edma_handle = DmaDriverInit();
    assert(edma_handle);

    //**** Create Draw Task ****

    Task_Params_init(&task_params);
    task_params.priority = 7;
    task_params.stackSize = 2048;

    // Pass DMA driver handle into the task
    task_params.arg0 = (uintptr_t) edma_handle;

    task0 = Task_create(DrawTask, &task_params, &task0_eb);
    assert(!Error_check(&task0_eb));    // FAILS THIS ASSERTION!!!

    BIOS_start();
    return (0);
}

On execution, the console prints the following error message:

[CortexA15] assertion "!Error_check(&task0_eb)" failed: file "../main_arm.cpp", line 339, function: int main()

Using the debugger, I stepped into the Task_create() function, and found that the problem occurs when the code tries to allocate memory for the 2048-byte task stack. Specifically, the sysbios kernel function Task_Instance_init() does this:

    /* deal with undefined Task_Params defaults */
    if (params->stackHeap == NULL) {
        tsk->stackHeap = Task_defaultStackHeap;
    }

but the variable Task_defaultStackHeap contains 0. Further down, Memory_alloc() is called:

    tsk->stack = (Char *)Memory_alloc(tsk->stackHeap, tsk->stackSize, align, eb);

but the first parameter, tsk->stackHeap, is NULL. I believe this causes Memory_alloc() to return with a null pointer (and an error in eb).

How do I solve this?

  • Hi Andy,

    Can you try providing the 

    1. Task stack pointer (stack variable) and

    2. the appropriate size (stackSize) for the Task Create ()?

    Please refer to below:

    http://software-dl.ti.com/dsps/dsps_public_sw/sdo_sb/targetcontent/sysbios/6_50_00_10/exports/bios_6_50_00_10/docs/cdoc/ti/sysbios/knl/Task.html#per-instance_config_parameters

    Example:

    /* provide 16k Stack, this can be changed as per the application need */

    #define APP_TSK_STACK_MAIN              (16U * 1024U)

    /* Test application stack */
    static uint8_t gAppTskStackMain[APP_TSK_STACK_MAIN] __attribute__((aligned(32)));

    TaskP_Params taskParams;
    Error_Block eb;
    TaskP_Params_init(&taskParams);

    taskParams.stack = gAppTskStackMain;
    taskParams.stacksize = sizeof (gAppTskStackMain);
    taskParams.pErrBlk = &eb;

    TaskP_create(App_Test_Task, &taskParams);

    Thanks

  • I did the following:

    #include "ti/osal/TaskP.h"
    #include "assert.h"
    
    void App_Test_Task(UArg a0, UArg a1)
    {
    }
    
    #define APP_TSK_STACK_MAIN              (16U * 1024U)
    
    /* Test application stack */
    static uint8_t gAppTskStackMain[APP_TSK_STACK_MAIN] __attribute__((aligned(32)));
    
    int main(void)
    {
        TaskP_Params taskParams;
        Error_Block eb;
        TaskP_Params_init(&taskParams);
    
        taskParams.stack = gAppTskStackMain;
        taskParams.stacksize = sizeof (gAppTskStackMain);
        taskParams.pErrBlk = &eb;
    
        TaskP_Handle taskH = TaskP_create(App_Test_Task, &taskParams);
        assert(taskH);
    }

    The following error occurs during compile:

    ../main_arm.cpp:490:65: error: invalid conversion from 'void (*)(UArg, UArg) {aka void (*)(unsigned int, unsigned int)}' to 'void*' [-fpermissive]

    I then tried this:

    #include "ti/sysbios/knl/Task.h"
    #include "assert.h"
    
    void App_Test_Task(UArg a0, UArg a1)
    {
    }
    
    #define APP_TSK_STACK_MAIN              (16U * 1024U)
    
    /* Test application stack */
    static uint8_t gAppTskStackMain[APP_TSK_STACK_MAIN] __attribute__((aligned(32)));
    
    int main(void)
    {
        Task_Params taskParams;
        Error_Block eb;
        Task_Params_init(&taskParams);
    
        taskParams.stack = gAppTskStackMain;
        taskParams.stackSize = sizeof (gAppTskStackMain);
    
        Task_Handle taskH = Task_create(App_Test_Task, &taskParams, &eb);
        assert(taskH);
    }

    This builds without error. On execution, Task_create() returns a valid handle.

    Is this the solution you recommend? Most of the example programs I have seen did not need to provide a block of allocated memory to Task_create() (e.g. those in SPRUEX3V).

  • found that the problem occurs when the code tries to allocate memory for the 2048-byte task stack.

    Hi Andy, 

    Glad that you now have resolved the issue. I recommended the solution I proposed earlier to make sure you have memory reserved for the task stack. If you do not provide the memory it would be from the heap. 

    The approach I suggested makes sure there is sufficient memory for application task stack.

    Thanks