Search code examples
cpointersfreertosnxp-microcontroller

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


Short question:

What is the meaning this expression?

if (ptr->exit)

It really looks like it's just asking if the exit value of the struct is true, what I don't understand is that in the senario described below, it seems a false value entered the if condition.

why asking this

I am reading the source code for a shell script for NXP LPC54018 SDK in fsl_shell.c file in utilities folder

The code look like this

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;
        }

...

I noticed this SHELL_Main() works directly when called directly from Main.c. However, if I wrap it in xTaskCreate and then run vTaskStartScheduler(), even though the pointer passed in is the same, the exit condition was trigged.

Method A: Called directly from Main.c (works)

SHELL_Main(&context);

console

SHELL (build: Oct 26 2018)

Copyright (c) 2017 NXP Semiconductor

SHELL>> start loop, context = 0x2fec0

Method B: Wrapped by FreeRTOS task, exit condition trigged

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

Why the same pointer will trigger different condition?

P.S as suggested by the ODYN-Kon and Ashelly I double checked the exit value the pointer points to, and the result doesn't make sense to me...

while (1)
    {
    printf("start loop, context = %p\n", context);
      printf("context.exit=%s", (context->exit)?"true":"false");

        if (context->exit)
        {
            printf("wth");
            break;
        } 

result

Copyright (c) 2017 NXP Semiconductor
SHELL>> start loop, context = 0x2fec0
context.exit=falsewth

Solution

  • exit appears to be a boolean member of p_shell_context_t struct.

    if (context->exit) simply means look at value of exit in struct pointed to by context and if it is true, enter the if statement block, otherwise, skip it.