Accessing Preferencs from core task

stevon
Posts: 7
Joined: Fri Mar 11, 2022 11:04 am

Accessing Preferencs from core task

Postby stevon » Thu Apr 14, 2022 10:38 am

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);
}

mikemoy
Posts: 622
Joined: Fri Jan 12, 2018 9:10 pm

Re: Accessing Preferencs from core task

Postby mikemoy » Thu Apr 14, 2022 2:05 pm

try increasing your stack size for the task.

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

stevon
Posts: 7
Joined: Fri Mar 11, 2022 11:04 am

Re: Accessing Preferencs from core task

Postby stevon » Thu Apr 14, 2022 4:51 pm

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

mikemoy
Posts: 622
Joined: Fri Jan 12, 2018 9:10 pm

Re: Accessing Preferencs from core task

Postby mikemoy » Thu Apr 14, 2022 5:23 pm

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

stevon
Posts: 7
Joined: Fri Mar 11, 2022 11:04 am

Re: Accessing Preferencs from core task

Postby stevon » Thu Apr 14, 2022 5:54 pm

Excellent.

Will look into that.
Thanks again.

steve

Who is online

Users browsing this forum: Baidu [Spider] and 64 guests