Issue with RTOS - Only 5 Tasks Executing Out of 6-7 Tasks

                          I'm currently facing an issue with my Real-Time Operating System setup, and I'm seeking your expertise to help me troubleshoot and resolve the problem.

Problem Description:
After configuring and adding 6-7 tasks in my RTOS application, I am encountering an unexpected behavior where only 5 tasks seem to be executing properly. I have already adjusted the stack size and configured the priorities for each task, but the issue persists.

Configuration Details:

  • RTOS:FreeRTOS v10.0.0
  • Tasks Added: TASKS 7 added
  • Stack Size Adjusted: #define configMINIMAL_STACK_SIZE ((uint16_t)64)
  • Priority Configured:#define configMAX_PRIORITIES (15).

Code Snippet:

#include <atmel_start.h>
#include <stdlib.h>
#include <string.h>

TaskHandle_t Can_sleep_handle;
TaskHandle_t Can_Read_handle;
TaskHandle_t Can_write_handle;
TaskHandle_t Task3_handle;
TaskHandle_t Task4_handle;
TaskHandle_t Task5_handle;



void Task3()
{
	while (1)
	{
		printf("Task4\n");
		vTaskDelay(1000);
	}

}
void Task4()
{
	while (1)
	{
		printf("Task5\n");
		vTaskDelay(2000);
	}

}
void Task5()
{
	while (1)
	{
		printf("Task6\n");
		vTaskDelay(7000);
	}

}
void Can_Read()
{
	while (1)
	{
		printf("TASK1\n");
		vTaskDelay(100);
	}

}
void can_sleep()
{
	while (1)
	{
	printf("TASK3\n");
	vTaskDelay(1000);
	}
}
void Can_write()
{
	
	while (1)
	{
		printf("TASK2\n");
		vTaskDelay(100);
	}

}
int main(void)
{
	atmel_start_init();
	xTaskCreate(Can_write, "Can_write", configMINIMAL_STACK_SIZE, NULL, 2, &Can_write_handle);
	xTaskCreate(Can_Read, "Can_Read", configMINIMAL_STACK_SIZE, NULL, 1, &Can_Read_handle);
	xTaskCreate(can_sleep, "can_sleep", configMINIMAL_STACK_SIZE, NULL, 3, &Can_sleep_handle);
	xTaskCreate(Task3, "Task3", configMINIMAL_STACK_SIZE, NULL, 4, &Task3_handle);
	xTaskCreate(Task4, "Task4", configMINIMAL_STACK_SIZE, NULL, 5, &Task4_handle);
       xTaskCreate(Task5, "Task5", configMINIMAL_STACK_SIZE, NULL, 6, &Task5_handle);
	
	vTaskStartScheduler();
	while (1) {
	}
}

Have you checked the return values of each call to vTaskCreate()?

1 Like

Yes, My Return value is -1.

for everY task, or just the two that do not get scheduled? -1 is an error return, probably you do not have enough RAM for all tasks.

For every task I’m getting -1 as return value.

show us your entire code then. The code in your first post is not complete.

if that is your full code, how do you know the return values of xTaskCreate()? How do you check those?

Also, can you share your freertosconfig.h file? How many task priorities do you define there?

I printed the return values for all xtaskcreate

int main(void)
{
	atmel_start_init();
	BaseType_t taskCreationResult1,taskCreationResult2,taskCreationResult3,taskCreationResult4,taskCreationResult5,taskCreationResult6;

	taskCreationResult1 = xTaskCreate(Task1, "Task1", configMINIMAL_STACK_SIZE, NULL, 1, &Task1_handle);
	printf("%ld",taskCreationResult1);
	
	taskCreationResult2=xTaskCreate(Task2, "Task2", configMINIMAL_STACK_SIZE, NULL, 2, &Task2_handle);
	printf("%ld",taskCreationResult2);
	
	taskCreationResult3=xTaskCreate(Task3, "Task3", configMINIMAL_STACK_SIZE, NULL, 2, &Task3_handle);
	printf("%ld",taskCreationResult3);
	
	taskCreationResult4=xTaskCreate(Task4, "Task4", configMINIMAL_STACK_SIZE, NULL, 2, &Task4_handle);
	printf("%ld",taskCreationResult4);
	
	taskCreationResult5=xTaskCreate(Task5, "Task5", configMINIMAL_STACK_SIZE, NULL, 2, &Task5_handle);
	printf("%ld",taskCreationResult5);
	
	taskCreationResult6=xTaskCreate(Task6, "Task6,", configMINIMAL_STACK_SIZE, NULL, 2, &Task6_handle);
	printf("%ld",taskCreationResult6);
	vTaskStartScheduler();
	while (1) 
	{
	
	}
}

but in the two code fragments you show us, the 6th parameter is different in each call to xTaskCreate(). If you want us to help you, you need to provide useful information. So which of the two init sequences you show us is the one that poses the problem?

Again, -1 is an error return. Theoretically, with all xTaskCreate invocations returning error, your system should not work at all. Are you using an official FreeRTOS release or a customized one?

That seems too small. Try increasing that to something like 512 for testing.

Is it 1 or -1?

For every task I’m getting 1 as return value.

Goodness. What did you change to miraculously get 1 instead of -1?

So what ARE the priorities you use in your problem? All 2s or 1-6? Again, how many task priorities do you allow in your freertosconfig.h?

prioirties 1-6 and
In freertosconfig.h Priority Configured : #define configMAX_PRIORITIES (15).

Try swapping the delay values in your tasks and see if it is the same tasks being starved. It looks as if your high pri tasks simply starve the lower pri tasks. There is no guarantee that low pri tasks ever execute.

In addition, you can also check if the Preemption( configUSE_PREEMPTION ) and Time Slicing
( configUSE_TIME_SLICING ) are enabled or disabled .
Looking from the initial code, looks like delay for the higher priority tasks( 1000 ) is not long enough, due to which low priority tasks are getting starved. You might need to allocate a longer delay , depending on the priority.

The source code shows the tasks are not implemented in infinite loops so I suspect a few tasks run then crash.

@Sandhi Your first post shows tasks as infinite loops while next one does not. As @RAc pointed out, there are other differences in the code snippets you posted. Please share the complete code you are running.

This is complete nonsense. Are you trying to make fun of us or something?

Please try the following:

#include <atmel_start.h>
#include <stdlib.h>
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include <FreeRTOSConfig.h>

TaskHandle_t TASK1_handle;
TaskHandle_t TASK2_handle;
TaskHandle_t TASK3_handle;
TaskHandle_t TASK4_handle;
TaskHandle_t TASK5_handle;


void TASK1( void * param )
{
    ( void ) param;

    while(1)
    {
        printf("TASK1\n");
        vTaskDelay(1000);
    }
}

void TASK4( void * param )
{
    ( void ) param;

    while(1)
    {
        printf("TASK4\n");
        vTaskDelay(1000);
    }
}

void TASK3( void * param )
{
    ( void ) param;

    while(1)
    {
        printf("TASK3\n");
        vTaskDelay(1000);
    }
}

void TASK5( void * param )
{
    ( void ) param;

    while(1)
    {
        printf("TASK5\n");
        vTaskDelay(1000);
    }
}

void TASK2( void * param )
{
    ( void ) param;

    while (1)
    {
        printf("TASK2\n");
        vTaskDelay(1000);
    }
}

int main(void)
{
    atmel_start_init();
    
    xTaskCreate(TASK1, "TASK1", configMINIMAL_STACK_SIZE, NULL, 1, &TASK1_handle);
    xTaskCreate(TASK2, "TASK2", configMINIMAL_STACK_SIZE, NULL, 1, &TASK2_handle);
    xTaskCreate(TASK3, "TASK3", configMINIMAL_STACK_SIZE, NULL, 1, &TASK3_handle);
    xTaskCreate(TASK4, "TASK4", configMINIMAL_STACK_SIZE, NULL, 1 ,&TASK4_handle);
    xTaskCreate(TASK5, "TASK5", configMINIMAL_STACK_SIZE, NULL, 1, &TASK5_handle);

    vTaskStartScheduler();

    while (1)
    {
    }
}

Note that I have changed all the task priorities to 1. Once you get this working, you can try changing the priorities.

1 Like

The OP was running out of memory. Increasing the heap size fixed the issue.