//Task and Queue handlers
TaskHandle_t Task1; //Task1 Handler
TaskHandle_t Task2; //Task2 Handler
TaskHandle_t Task3; //Task2 Handler
TaskHandle_t Task4; //Task2 Handler
//declarations of Task related functions
static void Task1code( void * parameter);
static void Task2code( void * parameter);
static void Task3code( void * parameter);
static void Task4code( void * parameter);
bool Task1R = false;
bool Task2R = false;
bool Task3R = false;
bool Task4R = false;
int turn = 0;
void setup() {
Serial.begin(115200);
Serial.println("Running");
vTaskDelay(1000 / portTICK_PERIOD_MS); //wait for a second
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
2048, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
NULL); /* pin task to core 1 */
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
2048, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
NULL); /* pin task to core 0 */
xTaskCreatePinnedToCore(
Task3code, /* Task function. */
"Task3", /* name of task. */
2048, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task3, /* Task handle to keep track of created task */
NULL); /* pin task to core 0 */
xTaskCreatePinnedToCore(
Task4code, /* Task function. */
"Task4", /* name of task. */
2048, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task4, /* Task handle to keep track of created task */
NULL); /* pin task to core 0 */
}
void loop() {
}
static void Task1code( void * parameter) {
while(1){
Task1R = true;
turn=1;
while(Task2R && Task3R && Task4R && turn==1){
//T1 Esperando
}
Serial.println("Sección crítica de la T1");
Task1R = false;
vTaskDelay(500 / portTICK_PERIOD_MS); //wait for a second
}
}
static void Task2code( void * parameter) {
while(1){
Task2R = true;
turn=2;
while(Task1R && Task3R && Task4R && turn==2){
//T2 Esperando
}
Serial.println("Sección crítica de la T2");
Task2R = false;
vTaskDelay(1000 / portTICK_PERIOD_MS); //wait for a second
}
}
static void Task3code( void * parameter) {
while(1){
Task3R = true;
turn=3;
while(Task1R && Task2R && Task4R && turn==3){
//T3 Esperando
}
Serial.println("Sección crítica de la T3");
Task3R = false;
vTaskDelay(800 / portTICK_PERIOD_MS); //wait for a second
}
}
static void Task4code( void * parameter) {
while(1){
Task4R = true;
turn=4;
while(Task1R && Task2R && Task3R && turn==4){
//T4 Esperando
}
Serial.println("Sección crítica de la T4");
Task4R = false;
vTaskDelay(900 / portTICK_PERIOD_MS); //wait for a second
}
}