crash using timers

danchik
Posts: 2
Joined: Thu Mar 16, 2017 7:49 am

crash using timers

Postby danchik » Thu Mar 16, 2017 8:26 am

Hi, i'm trying to run some code on timer, using FreeRTOS xTimerCreate and when timer expires, i get reset... This is on ESP32 (ESP32 thing dev board)

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "freertos/task.h"

int xx=0;

void vCallbackFunction( TimerHandle_t xTimer ) {
    printf("in timer xx=%d\n", xx++);
    fflush(stdout);
}

void app_main()
{
    int myTimerID = 1;
    TimerHandle_t tm = xTimerCreate("refresh", 3000/portTICK_PERIOD_MS, pdTRUE, (void*)&myTimerID, vCallbackFunction);
    BaseType_t res = xTimerStart(tm,0);
    if(tm && res==pdPASS) {
        printf("timer created\n");
    } else {
        printf("faled making timer\n");
    }
    fflush(stdout);

    for(;;) { // also, can I let app_main exit, and expect timer keep expiring to run code in vCallbackFunction, or start new task in vCallbackFunction date app_main was exited
        vTaskDelay(100);
        printf(".");
        fflush(stdout);
    }
}

if I exit vCallbackFunction quicker (without doing printfs just increment xx seems to work), but otherwise it fails with either Stack overflow or some other Guru panicked error (ex errors I get:
Guru Meditation Error of type InstrFetchProhibited occurred on core 0. Exception was unhanded.
or
***ERROR*** A stack overflow in task Tmr Svc has been detected.
abort() was called at PC 0x400853b0
Guru Meditation Error: Core 0 panic'ed (abort)


or is it that some things are just not available form callbacks like printf and fflush ?
or is it limited to how long I can remain in the callback before returning (If I start a task inside the callback instead, and do all the printing in that task it seems to be fine)?

Thank you

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: crash using timers

Postby ESP_Angus » Thu Mar 16, 2017 9:23 am

The default stack size for the timer task is quite small, so performing a printf() overflows it.

There's a permanent fix for this coming soon, but in the meantime you can edit FreeRTOSConfig.h and increase the timer task stack size, there. 2048 bytes should be enough.

danchik
Posts: 2
Joined: Thu Mar 16, 2017 7:49 am

Re: crash using timers

Postby danchik » Thu Mar 16, 2017 6:55 pm

Interesting, that was it... I saw this issue mentioned before while looking around, but since it's been a few month since that post I just assumed it was already fixed :)

Thank you

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: crash using timers

Postby snahmad75 » Wed Apr 10, 2019 9:46 am

Related question is if crash/exception happens during my delete and I did not execute
It gets restart silently.

and esp_restart() does not executed. Does it have impact on OTA. does OTA will fails.

JainamShah
Posts: 16
Joined: Thu Jul 29, 2021 6:52 am

Re: crash using timers

Postby JainamShah » Thu Aug 19, 2021 11:34 am

danchik wrote:
Thu Mar 16, 2017 8:26 am
Hi, i'm trying to run some code on timer, using FreeRTOS xTimerCreate and when timer expires, i get reset... This is on ESP32 (ESP32 thing dev board)

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "freertos/task.h"

int xx=0;

void vCallbackFunction( TimerHandle_t xTimer ) {
    printf("in timer xx=%d\n", xx++);
    fflush(stdout);
}

void app_main()
{
    int myTimerID = 1;
    TimerHandle_t tm = xTimerCreate("refresh", 3000/portTICK_PERIOD_MS, pdTRUE, (void*)&myTimerID, vCallbackFunction);
    BaseType_t res = xTimerStart(tm,0);
    if(tm && res==pdPASS) {
        printf("timer created\n");
    } else {
        printf("faled making timer\n");
    }
    fflush(stdout);

    for(;;) { // also, can I let app_main exit, and expect timer keep expiring to run code in vCallbackFunction, or start new task in vCallbackFunction date app_main was exited
        vTaskDelay(100);
        printf(".");
        fflush(stdout);
    }
}

if I exit vCallbackFunction quicker (without doing printfs just increment xx seems to work), but otherwise it fails with either Stack overflow or some other Guru panicked error (ex errors I get:
Guru Meditation Error of type InstrFetchProhibited occurred on core 0. Exception was unhanded.
or
***ERROR*** A stack overflow in task Tmr Svc has been detected.
abort() was called at PC 0x400853b0
Guru Meditation Error: Core 0 panic'ed (abort)


or is it that some things are just not available form callbacks like printf and fflush ?
or is it limited to how long I can remain in the callback before returning (If I start a task inside the callback instead, and do all the printing in that task it seems to be fine)?

Thank you
I am experiencing same problem.. I already set the timer stack size to 2100. For your reference I am using 4 timers in my project...

Some time at the time of timer_callback I am getting this error.

Code: Select all

***ERROR*** A stack overflow in task Tmr Svc has been detected.

Backtrace:0x4008971b:0x3ffbcdc0 0x40089eed:0x3ffbcde0 0x4008d6b1:0x3ffbce00 0x4008bad5:0x3ffbce80 0x40089fe4:0x3ffbcea0 0x40089f96:0x00011214 |<-CORRUPTED
0x4008971b: panic_abort at H:/Jainam/components/esp_system/panic.c:356

0x40089eed: esp_system_abort at H:/Jainam/components/esp_system/system_api.c:112

0x4008d6b1: vApplicationStackOverflowHook at H:/Jainam/components/freertos/port/xtensa/port.c:489

0x4008bad5: vTaskSwitchContext at H:/Jainam/components/freertos/tasks.c:3290

0x40089fe4: _frxt_dispatch at H:/Jainam/components/freertos/port/xtensa/portasm.S:432

0x40089f96: _frxt_int_exit at H:/Jainam/components/freertos/port/xtensa/portasm.S:231



ELF file SHA256: 27de3df566a2f290

Rebooting...

User avatar
mbratch
Posts: 299
Joined: Fri Jun 11, 2021 1:51 pm

Re: crash using timers

Postby mbratch » Thu Aug 19, 2021 1:31 pm

You can use `ESP_LOGI(...)` instead of `printf` and it uses less stack and it may work. `ESP_LOGI(...)` accepts format string.

DrMickeyLauer
Posts: 129
Joined: Sun May 22, 2022 2:42 pm

Re: crash using timers

Postby DrMickeyLauer » Fri Mar 08, 2024 5:43 pm

ESP_Angus wrote:
Thu Mar 16, 2017 9:23 am
The default stack size for the timer task is quite small, so performing a printf() overflows it.

There's a permanent fix for this coming soon, but in the meantime you can edit FreeRTOSConfig.h and increase the timer task stack size, there. 2048 bytes should be enough.
Resurrecting an old thread, but I just stumbled over this... has this fix been applied yet or do I still need to edit the FreeRTOSConfig header?

ESP_Sprite
Posts: 9022
Joined: Thu Nov 26, 2015 4:08 am

Re: crash using timers

Postby ESP_Sprite » Sat Mar 09, 2024 2:55 am

You can adjust the stack size in menuconfig now, item FREERTOS_TIMER_TASK_STACK_DEPTH.

DrMickeyLauer
Posts: 129
Joined: Sun May 22, 2022 2:42 pm

Re: crash using timers

Postby DrMickeyLauer » Sat Mar 09, 2024 12:51 pm

Excellent, thanks. I stumbled over this already, but I think it's good for documenting, in case someone else finds this thread.

Who is online

Users browsing this forum: No registered users and 206 guests