Page 1 of 1

Accessing Preferencs from core task

Posted: Thu Apr 14, 2022 10:38 am
by stevon
Hi

I have been unable to access the Preferences object from within a core task. I crashes the chip.

Any attemp to read or write to the Preferences object fails. Although, the same code works as expected when in setup or loop.
I tried different core and the task can read and write to other globals.
I also tried to pass it to the task as a parmater but could get this to work either.

Any thoughts would be appreciated.

regards
steve

#######################################
#include <Preferences.h>
Preferences preferences;


TaskHandle_t TaskCore0;
int counter;

void setup() {
Serial.begin(115200);


///////////////////// this works fine //////////////////////////
// preferences.begin("ESP32Pref", false);
// preferences.putInt("counter", 4);
// counter = preferences.getInt("counter", 0);
// Serial.printf("the value of counter from setup: %i\n", counter);
// preferences.end();

xTaskCreatePinnedToCore(
neoTrigger,
"TaskCore0",
1000,
NULL,
0,
&TaskCore0,
0); // this is the core where it will run
delay(500);
}



void neoTrigger( void * parameter) {
///////////////////// this crashes the chip //////////////////////////
// preferences.begin("ESP32Pref", false);
// preferences.putInt("counter", 4);
// counter = preferences.getInt("counter", 0);
// Serial.printf("the value of counter from inside core task: %i\n", counter);
// preferences.end();
for (;;) {
///////////////////// this crashes the chip //////////////////////////
// preferences.begin("ESP32Pref", false);
// preferences.putInt("counter", 4);
// counter = preferences.getInt("counter", 0);
// Serial.printf("the value of counter from inside loop: %i\n", counter);
// preferences.end();
delay(3000);
}
}


void loop() {
///////////////////// this works fine //////////////////////////
// preferences.begin("ESP32Pref", false);
// preferences.putInt("counter", 4);
// counter = preferences.getInt("counter", 0);
// Serial.printf("the value of counter from inside loop: %i\n", counter);
// preferences.end();

delay(3000);
}

Re: Accessing Preferencs from core task

Posted: Thu Apr 14, 2022 2:05 pm
by mikemoy
try increasing your stack size for the task.

xTaskCreatePinnedToCore(
neoTrigger,
"TaskCore0",
4096,
NULL,
0,
&TaskCore0,
0); // this is the core where it will run

Re: Accessing Preferencs from core task

Posted: Thu Apr 14, 2022 4:51 pm
by stevon
Doh !
Thank you.

For future reference:
Is this size relative to the total of the file sizes of the libraries that i am using within the task ?

Steve

Re: Accessing Preferencs from core task

Posted: Thu Apr 14, 2022 5:23 pm
by mikemoy
No. Some calls use more than others. For instance, I would never use printf without a minimum of 2048.
But things like x+7*Y you can get away with 1024 with no problems.
There is a function called uxTaskGetStackHighWaterMark you might want to check into. This way you may know how close to running out of stack for your task.

https://docs.espressif.com/projects/esp ... skHandle_t

Re: Accessing Preferencs from core task

Posted: Thu Apr 14, 2022 5:54 pm
by stevon
Excellent.

Will look into that.
Thanks again.

steve