Using xCreateStatic in FreeRTOS

Here’s my code:

#include "FreeRTOS.h"

#include "task.h"

#include <bsp_printf.h>

#include <bsp_mem_map.h>

#include <bsp_version.h>

#define STACK_SIZE 200

StaticTask_t xTaskBuffer;

StackType_t xStack[ STACK_SIZE ];

void vTask1(void *pvParameters);

void vTask2(void *pvParameters);

int main(void)

{

    //TaskHandle_t xHandle1 = NULL;

    //TaskHandle_t xHandle2 = NULL;

    

    xTaskCreateStatic(vTask1, "Task 1", STACK_SIZE, (void *) 1, 1, xStack, &xTaskBuffer);

    xTaskCreateStatic(vTask2, "Task 2", STACK_SIZE, (void *) 1, 1, xStack, &xTaskBuffer);

    vTaskStartScheduler();

    return 0;

}

void vTask1(void *pvParameters)

{

    configASSERT( ( uint32_t ) pvParameters == 1UL );

    for (;;)

    {

        printfNexys("Task 1\r\n");

        vTaskDelay(pdMS_TO_TICKS(1000));

    }

    vTaskDelete(NULL);

}

void vTask2(void *pvParameters)

{

    configASSERT( ( uint32_t ) pvParameters == 1UL );

    for (;;)

    {

        printfNexys("Task 2\r\n");

        vTaskDelay(pdMS_TO_TICKS(1000));

    }

    vTaskDelete(NULL);

}

Here, no output is observed while debugging the program. I’m a beginner at FreeRTOS and hence don’t know much about how to write the codes. Please help me resolve this issue. Also, xTaskCreate() isn’t supported by my platform so that doesn’t work.

Do you see output if you call printfNexys() before starting the scheduler?

If you stop the program on the debugger, what is the cpu executing?

If you put breakpoints at the start of both tasks, are they hit?

If you step through the vTaskStartScheduler() fun coin the debugger can you see the first task starting?

Are you giving the same stack and task buffer to 2 tasks ? That can’t work.
Each task needs is OWN stack and task buffer, of course.
And depending on printfNexys implementation the stack might be too small because printf family functions are often stack hungry.
You really should define configASSERT and also enable stack overflow checking for development/debugging. It’s very helpful !

I’ve changed the code to the following:

#include "FreeRTOS.h"
#include "task.h"
#include <bsp_printf.h>
#include <bsp_mem_map.h>
#include <bsp_version.h>

#define STACK_SIZE 1024
StaticTask_t xTaskBuffer;
StackType_t xStack[ STACK_SIZE ];

void vTask1(void *pvParameters);
//void vTask2(void *pvParameters);

int main(void)
{
//TaskHandle_t xHandle1 = NULL;
//TaskHandle_t xHandle2 = NULL;

xTaskCreateStatic(vTask1, "Task 1", STACK_SIZE, (void *) 1, 1, xStack, &xTaskBuffer);
// xTaskCreateStatic(vTask2, "Task 2", STACK_SIZE, (void *) 1, 2, xStack, &xTaskBuffer);
vTaskStartScheduler();

return 0;
}

void vTask1(void *pvParameters)
{
configASSERT( ( uint32_t ) pvParameters == 1UL );
for (;;)
{
printfNexys("Task 1\r\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
vTaskDelete(NULL);
}

/*
void vTask2(void *pvParameters)
{
configASSERT( ( uint32_t ) pvParameters == 1UL );
for (;;)
{
printfNexys("Task 2\r\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
vTaskDelete(NULL);
}
*/

But I’m still observing the same thing. No output of printf is observed. I set the breakpoint to the printfNexys() function also. But it didn’t go to that point. Also, as I am a beginner in this, I’m unable to understand configASSERT and stack overflow checking. Could you tell the exact changes I would have to make ?

There is a FreeRTOS FAQ with helpful hints and also a FreeRTOS - Quick start guide.
Maybe there is a FreeRTOS demo matching or similar to your MCU to look up or try.

Yes there is a demo. But that uses its own header files instead of the FreeRTOS ones.

Yes please revert back if you find something.

From the image you posted, it seems that you are able to set breakpoints. Change the definition of the main to the following and put a breakpoint on the infinite loop (this will tell us if the scheduler started successfully or not):

int main(void)
{
xTaskCreateStatic(vTask1, "Task 1", STACK_SIZE, (void *) 1, 1, xStack, &xTaskBuffer);
vTaskStartScheduler();

/* Set a breakpoint on the next line. */
for( ;; ); 
return 0;
}

Put a breakpoint on the first line of the vTask1 to see if the task is starting. When you do not see any output, break in the debugger and post the call-stack. Share your FreeRTOSConfig.h too.

Thanks.