Hello All;
I have two TASKs in a project with Esp32 and it runs separately on the CPU0 and CPU1. What solution should I apply to prevent them from using them at the same time?
Basically what I want to do is as follows.
for Variabla A : CPU-0 can Write / CPU-1 can READ
for Variabla B : CPU-1 can Write / CPU-0 can READ
for Variabla C : CPU-0 can Read&Write / CPU-0 can Read&Write
I added eample easy use:
int Cpu0_Read__Cpu1_Write=0;
int Cpu1_Read__Cpu0_Write=0;
int Cpu1_WriteRead__Cpu0_WriteRead=0;
int Cpu1_Read__Cpu0_Read=0;
How can I do this without the program breaking. I need max speed.
(Please sharing your simple code... )
#include <Arduino.h>
//#include <WiFiUdp.h>
//#include <WiFi.h> //ESP32 wifi
#include <string>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
TaskHandle_t LanCon;
TaskHandle_t Stepper;
void LanConcode( void * pvParameters );
void Steppercode( void * pvParameters );
int Cpu0_Read__Cpu1_Write=0;
int Cpu1_Read__Cpu0_Write=0;
int Cpu1_WriteRead__Cpu0_WriteRead=0;
int Cpu1_Read__Cpu0_Read=0;
void setup(){
Serial.begin(9600);
while (!Serial) {
delay(1); // wait for serial port to connect. Needed for native USB port only
};
//---------------------------------------
xTaskCreatePinnedToCore(
LanConcode, // Task function. //
"LanCon", // name of task. //
10000, // Stack size of task //
NULL, // parameter of the task //
1, // priority of the task //
&LanCon, // Task handle to keep track of created task //
0); // pin task to core 0 //
delay(500);
//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Steppercode, // Task function.
"Stepper", // name of task.
10000, // Stack size of task /
NULL, // parameter of the task /
1, // priority of the task /
&Stepper, // Task handle to keep track of created task /
1); // pin task to core 1 /
delay(500);
}
void loop(){
// empty loop
}
//--- CPU 0
void LanConcode( void * pvParameters ){
Serial.print("##LanConcode## running on core ");
Serial.println(xPortGetCoreID());
delay(100);
//**************************************
/*
int Cpu0_Read__Cpu1_Write=0;
int Cpu1_Read__Cpu0_Write=0;
int Cpu1_WriteRead__Cpu0_WriteRead=0;
int Cpu1_Read__Cpu0_Read=0;
*/
for(;;){
//---------------
if (Cpu0_Read__Cpu1_Write==0){Serial.println("ok-1");};
if (True){
Cpu1_Read__Cpu0_Write=123456;
Serial.println("ok-2");
};
if (Cpu1_WriteRead__Cpu0_WriteRead==0){
Cpu1_WriteRead__Cpu0_WriteRead=1;
Serial.println("ok-3a");
}else{
Cpu1_WriteRead__Cpu0_WriteRead=0;
Serial.println("ok-3b");
};
if (Cpu1_Read__Cpu0_Read==0){Serial.println("ok-4");}
//--------------
};
}
// --- > CPU 1
void Steppercode( void * pvParameters ){
Serial.print("###Steppercode### running on core ");
Serial.println(xPortGetCoreID());
delay(1000);
//--*************************************
/*
int Cpu0_Read__Cpu1_Write=0;
int Cpu1_Read__Cpu0_Write=0;
int Cpu1_WriteRead__Cpu0_WriteRead=0;
int Cpu1_Read__Cpu0_Read=0;
*/
for(;;){
//---------------
if (Cpu1_Read__Cpu0_Write==0){Serial.println("ok-1");};
if (True){
Cpu0_Read__Cpu1_Write=123456;
Serial.println("ok-2");
};
if (Cpu1_WriteRead__Cpu0_WriteRead==0){
Cpu1_WriteRead__Cpu0_WriteRead=1;
Serial.println("ok-3a");
}else{
Cpu1_WriteRead__Cpu0_WriteRead=0;
Serial.println("ok-3b");
};
if (Cpu1_Read__Cpu0_Read==0){Serial.println("ok-4");}
//--------------
};
}