Problem with multiple tasks

Hello everyone,

I am a beginner about FreeRTOS and I only want to do a simple program with 2 tasks.
The problem is that when I start the Scheduler with this 2 tasks. The task with the higher priority will start and then without the end of this task the 2nd task will start…

This is my code :

xTaskCreate(TFT_task_coil, "Coils_task", 128, NULL, 2, NULL);
xTaskCreate(TFT_task_display, "Display", 128, NULL, 1, NULL);
	
/* Scheduler */
vTaskStartScheduler();

void TFT_task_coil(void *param)
{	
	
	int i = 0;
	uint16_t test_coil = 25;
        uint16_t  test;
	
	while(1)
	{	
		if (tft_active != 0)
		{
			printf("Task coil start : %lu \r\n", xTaskGetTickCount());
			
			if(xQueueReceive(Queue_USB_Coils, (void *) &item_coil, 10) == pdTRUE) {
				test= item_coil;
			}
			else if(xQueueReceive(Queue_Ethernet_Coils, (void *) &item_coil, 10) == pdTRUE) {
				test= item_coil;
			}
			else{
				test= 300;			
			}
			
			if(uxQueueSpacesAvailable(Queue_Coils_Display) != 0){
				xQueueSend(Queue_Coils_Display, (void *) &test_coil, 10);
			}
			
			i++;
			if(i>19){
				test_coil = (uint16_t)rand();
				i = 0;
			}
			
			printf("Task coil end : %lu \r\n", xTaskGetTickCount());
			vTaskDelay(100);
			
		}
	}
}

void TFT_task_display(void *param)
{
	while(1)
	{
		if (tft_active != 0)
		{
			printf("Task display start : %lu \r\n", xTaskGetTickCount());
		
			EVE_cmd_text_burst(10, 190, 27, 0, "Coils current");
			EVE_cmd_text_burst(220, 190, 27, 0, "mA");
					
			/* Display of our variables */
			static uint16_t Value_coil = 0.0;
				
			xQueueReceive(Queue_Coils_Display, (void *) &Value_coil, 0);
			
			EVE_cmd_number_burst(170, 190, 27, 0, (uint16_t)Value_coil);
					
			printf("Task display end : %lu \r\n", xTaskGetTickCount());
			vTaskDelay(75);
		}
	}
}

My queue are working well.
The task are working well when I start the Scheduler just with 1 but when I put the 2 tasks this is what I get :
image

The coil task is not finish that the display task is launch and that make crash my program…

Do you have an idea ?

Especially when e.g. using usually stack hungry printf in tasks you have to provide a (much) bigger stack.
I strongly recommend to define configASSERT and also enable stack overflow checking for development/debugging.
Note that the stack overflow checks can’t catch all overflows in all situations, but it helps a lot.

Thank you ! I will try to raise my stack and to define the variables which you gave me.

Are you sure is a stack problem ? Because when I launch the scheduler with 1 of the 2 task that works perfectly…

You have right with a bigger stack that works ! Thank you man.

Great that it works :+1:
The problem with stack or buffer overflows is, that other/internal data structures might get corrupted which might cause any possible problem and sometimes even much later. Programs seem to work but in fact are broken causing headaches later on. Therefore it’s essential to figure out the right stack sizes and other resource usages like heap during development.

Is it normal that, sometimes a task is start before the end of an other ?

image

I thought it was only one task by one…
Is it because my stack is still not big enough ?

In case printf waits internally for something it’d allow the other tasks to run.
Put aside fatal bugs like memory corruption or something FreeRTOS applications behave as configured and/or documented. There are not much surprises.
Did you already find the pretty good documentation like Priority based preemptive RTOS scheduler ? There is a lot more interesting stuff worth reading and helping to get more familiar with FreeRTOS :slight_smile:

Ok thank you man for your help !
Yeah I have already saw how works the scheduler of FreeRTOS but I am a beginner :slight_smile: