ESP32 S3 accessing the same variable from both cores
Posted: Sun Feb 18, 2024 3:03 pm
I have problems when accessing the same variable. I tried declaring an integer as volatile and then non volatile and the behaviour is not consistent. Sometimes volatile helps to get the right value, sometimes not. Also sometimes accessing a volatile variable affects accessing real values in non volatile vars, for example I have
volatile int Mode;
int test;
If I do something like
Mode=1;
if (Mode) Serial.println ("Test : "+String(test));
will have different result (if other core is writing to a variable "test" at the same time) then just
Serial.println ("Test : "+String(test));
Although the result should be the same. I did objdump and found that assembler instruction memw is executed when accessing a volatile var, which does "Order memory accesses before with memory access after". i.e. something like update values in cache from the memory.
My question is: are there some quidelines on how to manage data access from two cores?
volatile int Mode;
int test;
If I do something like
Mode=1;
if (Mode) Serial.println ("Test : "+String(test));
will have different result (if other core is writing to a variable "test" at the same time) then just
Serial.println ("Test : "+String(test));
Although the result should be the same. I did objdump and found that assembler instruction memw is executed when accessing a volatile var, which does "Order memory accesses before with memory access after". i.e. something like update values in cache from the memory.
My question is: are there some quidelines on how to manage data access from two cores?