arduino MEGA2560. system Task loop 1 time and stop after the first return task. Thank you for your help

jeffou66 wrote on Thursday, August 23, 2018:

Hello, New with RTOS and beginner programming. It’s not right with me but I’m progressing. I have a question, to know why my code is doing a good loop but the second Task stops. I am on arduino MEGA2560. Thanks for your help.

#include <Arduino_FreeRTOS.h>
#include <FreeRTOSConfig.h>
#include <FreeRTOSVariant.h>
#include <task.h>


TaskHandle_t TaskHandle_1;
TaskHandle_t TaskHandle_2;
TaskHandle_t TaskHandle_3;
TaskHandle_t TaskHandle_4;


void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200);
  Serial.println(F("*****Setup function*****"));
  Serial.println(F("************************"));
  delay(2000);


xTaskCreate(TaskReadCalcPID, "Task1", configMINIMAL_STACK_SIZE, NULL, 4, &TaskHandle_1);   // paramètre de la TASK ici.
//xTaskCreate(TaskNxtParamP0_ConsMes, "Task2", configMINIMAL_STACK_SIZE, NULL, 4, &TaskHandle_2);
//xTaskCreate(TaskReadCalcPID, "Task3", configMINIMAL_STACK_SIZE, NULL, 3, &TaskHandle_3);
//xTaskCreate(TaskNxtParamP0_PID, "Task4", configMINIMAL_STACK_SIZE, NULL, 1, &TaskHandle_4);

}

void loop() {
   Serial.println(F("Loop function"));
  delay(4000);
  }

/////////////////////////////// TASKS////////////////////////////////////////////////////
//************************************************1*******************************************************************************

void TaskReadCalcPID(void *pvParameters)  // Ici c'est la tache avec le rappel de ses paramètres: TaskReadCalcPID , Priorité=6/7
{
 
  while(1)
  {


Serial.println("*****START****TASK 1***********");
Serial.println("*******************************");


Serial.println("1_ReadCalcPID : reafect priority 4 at: 1_ReadCalcPID");
vTaskPrioritySet(TaskHandle_1,4); //Now task1 has priority 4

   
Serial.println("1_ReadCalcPID run: Creating Task2(ParamP0_ConsMes)");
xTaskCreate(TaskNxtParamP0_ConsMes, "Task2", configMINIMAL_STACK_SIZE, NULL, 7, &TaskHandle_2);


}
}

//***********************************************2**********************************************************************************

void TaskNxtParamP0_ConsMes(void *pvParameters)  // Ici c'est la tache : TaskNxtParamP0_ConsMes   Priorité=5/7
{ 
 while(1)
  {

    
Serial.println("2_ParamP0_ConsMes : Suspend 1_ReadCalcPID");
vTaskSuspend(TaskHandle_1);  


Serial.println("2_ParamP0_ConsMes s'octroie Prorité=6");
vTaskPrioritySet(TaskHandle_2,6); //Now task2 has priority 6


Serial.println("2_ParamP0_ConsMes run: Creating Task3(CourbP0)");
xTaskCreate(TaskNxtCourbP0, "Task3", configMINIMAL_STACK_SIZE, NULL, 7, &TaskHandle_3);





  }
  }

//***********************************************3***********************************************************************************

void TaskNxtCourbP0(void *pvParameters)  // Ici c'est la tache : TaskNxtCourbP0  Priorité=4/7
{ 
 while(1)
  {


Serial.println("3_CourbP0 delete 2_ParamP0_ConsMes");
vTaskDelete(TaskHandle_2);


Serial.println("3_CourbP0 s'octroie Prorité=6");
vTaskPrioritySet(TaskHandle_3,6); //Now task3 has priority 6


 
Serial.println("3_CourbP0 run: Creating Task4(ParamP0_PID)");
xTaskCreate(TaskNxtParamP0_PID, "Task4", configMINIMAL_STACK_SIZE, NULL, 6, &TaskHandle_4); 



 
}
}

//*********************************************4*************************************************************************************

void TaskNxtParamP0_PID(void *pvParameters)  // Ici c'est la tache : TaskNxtParamP0  Priorité=3/7
{ 
 while(1)
  {


Serial.println("4_ParamP0_PID delete 3_CourbP0");
vTaskDelete(TaskHandle_3);


Serial.println("4_ParamP0_PID run: Make Resume task1(ReadCalcPID)");
vTaskResume(TaskHandle_1);


Serial.println("4_ParamP0_PID autodelete");
vTaskDelete(TaskHandle_4);


}
}

  
  

Serial Line Response :

Setup function


START****TASK 1******


1_ReadCalcPID : reafect priority 4 at: 1_ReadCalcPID
1_ReadCalcPID run: Creating Task2(ParamP0_ConsMes)
2_ParamP0_ConsMes : Suspend 1_ReadCalcPID
2_ParamP0_ConsMes s’octroie Prorité=6
2_ParamP0_ConsMes run: Creating Task3(CourbP0)
3_CourbP0 delete 2_ParamP0_ConsMes
3_CourbP0 s’octroie Prorité=6
3_CourbP0 run: Creating Task4(ParamP0_PID)
4_ParamP0_PID delete 3_CourbP0
4_ParamP0_PID run: Make Resume task1(ReadCalcPID)
4_ParamP0_PID autodelete
START****TASK 1******


1_ReadCalcPID : reafect priority 4 at: 1_ReadCalcPID
1_ReadCalcPID run: Creating Task2(ParamP0_ConsMes)
Loop function
Loop function
Loop function
Loop function
Loop function
Loop function
Loop function
Loop function
Loop function

And blocked on Loop … :-/
I do not understand too much about memory and stack management.

the size of the stack here is configMINIMAL_STACK_SIZE, but must not agree?

Thanks for all for my study

Jeff

rtel wrote on Thursday, August 23, 2018:

What is configMAX_PRIORITIES set to?

I’m not sure I understand your question - are you saying that the first
loop (from task to task then back to the first task again) works ok, but
not the second loop through the four tasks? If so, it might be because
the memory allocated by the scheduler for use as the task stack is not
being freed. That will depend on which version of FreeRTOS you are
using. From memory FreeRTOS V9 and V10 will free the memory as soon as
the task is deleted, unless a task deletes itself, in which case the
memory is not freed until the idle task runs. If you are using an older
version (can’t remember which version the behaviour changed), then
memory will only ever be freed when the idle task runs - in your case
the idle task is never running as there is always a task that has a
priority above 0 that is ready to run. Try putting a vTaskDelay(
pdMS_TO_TICKS( 20 ) ) call to xTaskCreate() that creates the
TaskNxtParamP0_ConsMes task. That will give the idle task some time too.

jeffou66 wrote on Thursday, August 23, 2018:

thanks for this first response :slight_smile:

My arduino FreeRTOS 's V8.2.3
I will update in V9 then V10

Indeed the loop from the SETUP is carried out completely, then for the second loop it stops after the first task. I have indeed the error of stack overload and time of request of creation too important (after reading the wiki of the library …)

I will try to give time for task creation.
I did not do it before because the real program has a lot of calculations and data transmitted to the serial. So I told myself that the calculation time would be “time management”.

jeffou66 wrote on Thursday, August 23, 2018:

Here I use version 10.1 of FreeRTOS for arduino. (the system is indicated as better for AVR, I am on avr MEGA2560 but EDI arduino)

But now it does not work at all, it stops at the first task, here are the details :

https://www.dropbox.com/sh/d50831bb9laezfs/AACg71eqeer1xhrryrpIlKD_a?dl=0

Serial sended :

Setup function


START****TASK 1******


1_ReadCalcPID : reafect priority 4 at: 1_ReadCalcPID
1_ReadCalcPID run: Creating Task2(ParamP0_ConsMes)

BLOCKED BY OVERLOAD MEMORY BY TASK OR PLACE MEMORY NOT ENOUGHT

Thanks in advance

rtel wrote on Friday, August 24, 2018:

Oh, that doesn’t sound good. Do you have a debugger attached to see
where the code stops? Does it get as far as starting the scheduler, and
if so, how far does it get after that (i.e. does ‘stops at the first
task’ mean the first task runs but the second doesn’t, and if so, at
what point in the code does it stop?).

Do you have the normal configASSERT() defined, stack overflow detection
turned on, etc.?

jeffou66 wrote on Friday, August 24, 2018:

Hello,

I have now integrated this formula but it does not change anything.

/ * Set to trap errors during development. * /
#define configASSERT ((x)) if ((x) == 1) vAssertCalled (FILE, LINE)

I think I will redo the entire sequence with Mutex to think about sharing the display resource that will be the purpose of the program.

I understand the concept of each method very well, but in the application there are behaviors that seem inconsistent with the functional description. my knowledge also needs to learn again.

But with V10 it’s not better than with V8, for avr MEGA2560

Thank you