Variable doesn't update between cores
Posted: Mon Apr 01, 2019 2:18 am
I came across this and am looking for some insight. Using arduino to program I have set up two tasks each pinned to a separate core. My application was to check if the core1 stops responding. So in that core I set a variable. In core 0 I check to see if that variable has been set and if so I clear it. However, in this example it does not set the variable unless you have a delay or some other statement also in that loop. Here is the code:
Running this code variable1 will always equal zero and give the error. However, if you uncomment the delay in the Task1code it works properly and does not produce an error. Something is funky. Why would it not adjust the variable1 in task1code unless a delay or some other commands are added after it?
Code: Select all
TaskHandle_t Task0;
TaskHandle_t Task1;
byte variable1=0;
byte variable2=0;
void setup(void) {
Serial.begin(115200);
//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task0code, /* Task function. */
"Task0", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
2, /* priority of the task */
&Task0, /* 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(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
2, /* priority of the task */
// when priority was set to 100 then network scan would not complete
&Task1, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
delay(500);
}
void loop() {
// put your main code here, to run repeatedly:
}
void Task0code( void * pvParameters ){
delay(1000);
for (;;){ //create an infinate loop
//Serial.println("variable1=" + String(variable1));
if(variable1>0){
variable1=0;
}else{
variable2=1;
}
delay(5);
if(variable2==1){
Serial.println("error");
delay(1000);
}
}
}
void Task1code( void * pvParameters ){
for (;;){ //create an infinate loop
variable1=1;
//delayMicroseconds(1);
}
}