why SHELL_Main() doesn't work in xTaskCreate in FreeRTOS?

cancel
Showing results for 
Search instead for 
Did you mean: 

why SHELL_Main() doesn't work in xTaskCreate in FreeRTOS?

471 Views
jren68
Contributor III

I am looking at the shell demo for LPC54018. In the demo the SHELL_Main() function from fsl_shell.c was used, and it works when used this way

script in Main.c

SHELL_Main(&context);

console

SHELL (build: Oct 26 2018)

Copyright (c) 2017 NXP Semiconductor

SHELL>> start loop, context = 0x2fec0

Now if I wrap the SHELL_Main in xTaskCreate, and then run vTaskStartScheduler(). It doesn't work despite the pointer was successfully passed in

script in Main.c

xTaskCreate(

SHELL_Main,

"shell",

1024,

&context,

1,

NULL);

vTaskStartScheduler();

console

SHELL (build: Oct 26 2018)

Copyright (c) 2017 NXP Semiconductor

SHELL>> start loop, user_context = 0x2fec0

context->exit

This is the starting section of the source code, bolded are the printf I added to help debug. As can be seen the context passed into the function is the same, however, when wrapped by xTaskCreate, if (context->exit) was triggered.

int32_t SHELL_Main(p_shell_context_t context)
{

printf("entered shell_main\n");
uint8_t ch;
int32_t i;

if (!context)
{
return -1;
}

context->exit = false;
context->printf_data_func("\r\nSHELL (build: %s)\r\n", __DATE__);
context->printf_data_func("Copyright (c) 2017 NXP Semiconductor\r\n");
context->printf_data_func(context->prompt);


while (1)
{
printf("start loop, context = %p\n", context);
if (context->exit)
{
printf("context->exit");
break;
}

....

Why the same pointer will trigger different conditions when directly called and when wrapped in Tasks?

5 Replies

222 Views
jren68
Contributor III

though i'd say don't use the SHELL_Main()

This IO function uses MUTEX_SEMAPHORE_BLOCKING(), which blocks the rest of the tasks regardless of their priority (facepalm) 

0 Kudos

222 Views
jren68
Contributor III

For some reason, the context->exit is changed from 0 to a value after the vTaskStartScheduler()

0 Kudos

222 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi Jenny:

Please check whether there is a stack overflow,  increase the stack size to a bigger value.

Regards

Daniel

0 Kudos

222 Views
jren68
Contributor III

Hi Daniel,

Thanks for the reply. I have suspected stackoverflow but soon found updating the stack size didn't help either.

I later found the problem is that I have initialized the context in main, outside of the stack of the createTask function. I should have initialized the context within the xTaskCreate() function such that this context pointer is not accidently overwritten by something else

Previously I was

int main(void) {    

    shell_context_struct user_context; // the user_context is not in the stack of the created task

    SHELL_Init(&user_context, ...);

    xTaskCreate(

        SHELL_Main,

        "shell",

        512,

        &user_context,

        1,

        NULL);

}

I should have

int main(void){

    xTaskCreate(

    start_shell,

    "shell",

    512,

    NULL,

    1,

    NULL);

}

void start_shell(void * p ) {

    shell_context_struct user_context;

    SHELL_Init(&user_context,....);

    SHELL_Main(&user_context);

}

222 Views
danielchen
NXP TechSupport
NXP TechSupport

Ah yes, thank you for your sharing!

Regards

Daniel

0 Kudos