I’m trying to implement the observer pattern on ESP32H2 device (devkit). I have 2 FreeRTOS tasks (in 2 files), there is a register callback function in one file, which is used to pass a callback to be called, when the second task is ready to pass data to the first one.
// task1.c
void myCallback(int e, struct myStruct ms) { // <-- here is the problem
ESP_LOGI(TAG, "myCallback is called %d, %f", e, ms.value);
}
void task1(void *args) {
while (1) {
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(5000));
request(myCallback);
}
}
// task2.c
static func callback;
static void notifyObservers(int e, struct myStruct ms) {
callback(e, ms);
}
void request(func cb) {
callback = cb;
xTaskNotifyGive(task2Handle);
}
void task2(void *args) {
struct myStruct result;
while (1) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// some logic goes here...
notifyObservers(0, result);
}
}
// main.c
void app_main(void) {
// create tasks task1Handle = xTaskCreateStatic(task1,...); task2Handle = ...;
}
When the callback has a signature void f(int e);
or void f(int e, struct myStruct *ms);
everything works as expected: task1
wakes up once in 5 s, requests action from task2
. Task2
unblocks, performs some logic (with delays), then calls the passed callback, callback logs the specified text.
But when the signature is changed to void f(int e, struct myStruct ms);
calling the callback crashes the app with Guru Meditation Error: Core 0 panic'ed (Stack protection fault).
ESP32 error documentation. Also there is Load access fault which describes MTVAL
register value (in my case it’s 0x00008082
). So I can suggest that the problem with the struct instance and the stack.
I always thought that a struct is copied by value when passed as the argument to a function, so there should not be the big difference from int e
case which is also passed by value. Also as far as I know callback is run in context of task2
(even defined in task1
scope), and every task in FreeRTOS is allocated its own stack memory block.
So why does void f(int e);
work and void f(int e, struct myStruct ms);
doesn’t?