Sample code for software based timer into ESP32-idf

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

Re: Sample code for software based timer into ESP32-idf

Postby ESP_Angus » Tue Mar 07, 2017 12:28 am

ESP_Angus wrote: I'm able to reproduce this. Will let you know once a fix is available.
The problem I was seeing was a stack overflow in the timer task. Can't use printf() in the timer task!

The stack overflow checking methods that work in other tasks don't appear to trigger in the timer task (I saw an interrupt wdt timeout instead). We'll add a kconfig option to set the timer task stack size, or setting "Enable 'nano' formatting options for printf/scanf family" Component Config -> ESP32-specific in menuconfig will work around that.

However, this doesn't sound like the issue you were seeing - as you mentioned it triggering with an empty timer callback. Do you have some code that exhibits that bug?

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Sample code for software based timer into ESP32-idf

Postby Ritesh » Tue Mar 07, 2017 9:57 am

HardwireIO wrote:Actually I'm facing the same problems with FreeRTOS timers: once timer is triggered, callback function is called correctly, but even though I defined an empty function, system restarts due to an access violation:

Code: Select all

Guru Meditation Error of type LoadProhibited occurred on core  0. Exception was unhandled.
Register dump:
PC      : 0x400856cb  PS      : 0x00060433  A0      : 0x800841c5  A1      : 0x3ffd2740  
A2      : 0x0000001d  A3      : 0x3ffd27d0  A4      : 0x00000010  A5      : 0x3ffcec1c  
A6      : 0x3ffd202c  A7      : 0x401196e8  A8      : 0x80083386  A9      : 0x3ffd2730  
A10     : 0x00000003  A11     : 0x00060423  A12     : 0x00060423  A13     : 0x3ffc0b90  
A14     : 0x00000001  A15     : 0x3ffc0bd0  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x0000002d  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  

Backtrace: 0x400856cb:0x3ffd2740 0x400841c5:0x3ffd2760 0x4008379b:0x3ffd2780 0x4008501c:0x3ffd27c0 0x4011abac:0x3ffd27f0 0x40119851:0x3ffd2810 0x40119a34:0x3ffd2830 0x40133afa:0x3ffd2850 0x401198d9:0x3ffd2870
Honesty ... I'm using FreeRTOS since many years ago, and my impression is that there's something related to FreeRTOS implementation on latest ESP-IDF.

Is anybody using FreeRTOS timers?
Hi,

As I have mentioned earlier that we were facing same system restart issue while using FreeRTOS Software Timer API to use it as software timer into ESP32.

Thanks for providing detailed logs.
Regards,
Ritesh Prajapati

HardwireIO
Posts: 10
Joined: Mon Mar 06, 2017 5:34 pm

Re: Sample code for software based timer into ESP32-idf

Postby HardwireIO » Tue Mar 07, 2017 4:39 pm

ESP_Angus wrote:
ESP_Angus wrote: I'm able to reproduce this. Will let you know once a fix is available.
The problem I was seeing was a stack overflow in the timer task. Can't use printf() in the timer task!

The stack overflow checking methods that work in other tasks don't appear to trigger in the timer task (I saw an interrupt wdt timeout instead). We'll add a kconfig option to set the timer task stack size, or setting "Enable 'nano' formatting options for printf/scanf family" Component Config -> ESP32-specific in menuconfig will work around that.

However, this doesn't sound like the issue you were seeing - as you mentioned it triggering with an empty timer callback. Do you have some code that exhibits that bug?
Hi ESP_Angus,

you were absolutely right, it was a matter of the printf, but honestly I'm still facing strange behaviors related to timer task.
I simply created a queue with xQueueCreate, and then added a specific task blocking on xQueueReceive.

If I send message from any other task, with xQueueSend, everything works fine and first task correctly receive the message.

If I send message from timer task, message is successfully delivered to the task waiting for the queue message, but after message is processed, I get a Panic with a watchdog timeout.

It sounds like something very strange, because there's nothing blocking. Task simply receives message and prints a log (with adeguate amount of stack right now).

Code: Select all

Guru Meditation Error: Core  0 panic'ed (Interrupt wdt timeout on CPU0)
Register dump:
PC      : 0x400de804  PS      : 0x00060b34  A0      : 0x800843b2  A1      : 0x3ffcf3d0  
A2      : 0x3ffcef98  A3      : 0x00060b23  A4      : 0x00060b20  A5      : 0x3ffd0480  
A6      : 0x00000003  A7      : 0x00060d23  A8      : 0x00000009  A9      : 0x00000009  
A10     : 0x00060b23  A11     : 0x00000000  A12     : 0x00060f23  A13     : 0x3ffd0460  
A14     : 0x00000003  A15     : 0x00060d23  SAR     : 0x00000011  EXCCAUSE: 0x00000005  
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffd  

Backtrace: 0x400de804:0x3ffcf3d0 0x400843b2:0x3ffcf3f0 0x4008375c:0x3ffcf410 0x40085020:0x3ffcf450 0x400de98c:0x3ffcf480 0x40085145:0x3ffcf4a0 0x40085178:0x3ffcf4c0 0x4008527f:0x3ffcf4f0
Why do you think I get a wdt timeout on CPU0 only if I send message from timer task?
Hardwire.io / The IoT Platform

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

Re: Sample code for software based timer into ESP32-idf

Postby ESP_Angus » Tue Mar 07, 2017 11:10 pm

The Interrupt WDT may be a misleading error, as I was seeing the same after the timer task stack overflowed (it seems like it gets stuck with interrupts disabled before the normal stack overflow detection has a chance to kick in). Some things you can try:
  • Decode the backtrace by running the command "xtensa-esp32-elf-addr2line -fe build/myapp.elf 0x400de804:0x3ffcf3d0 0x400843b2:0x3ffcf3f0 (etc, etc)
  • If the stack trace doesn't seem possible, it's likely a stack overflow related problem.
  • You can temporarily bump the configTIMER_TASK_STACK_DEPTH in FreeRTOSConfig.h (try 2048 or more). See if the problem reoccurs.
  • Check the stack size in your other tasks is sufficient.
  • Under Component Config -> FreeRTOS enable "Set a debug watchpoint as a stack overflow check"
If none of this seems to lend any clue, could you please post some code that exhibits the problem?

HardwireIO
Posts: 10
Joined: Mon Mar 06, 2017 5:34 pm

Re: Sample code for software based timer into ESP32-idf

Postby HardwireIO » Wed Mar 08, 2017 8:41 am

ESP_Angus wrote:The Interrupt WDT may be a misleading error, as I was seeing the same after the timer task stack overflowed (it seems like it gets stuck with interrupts disabled before the normal stack overflow detection has a chance to kick in). Some things you can try:
  • Decode the backtrace by running the command "xtensa-esp32-elf-addr2line -fe build/myapp.elf 0x400de804:0x3ffcf3d0 0x400843b2:0x3ffcf3f0 (etc, etc)
  • If the stack trace doesn't seem possible, it's likely a stack overflow related problem.
  • You can temporarily bump the configTIMER_TASK_STACK_DEPTH in FreeRTOSConfig.h (try 2048 or more). See if the problem reoccurs.
  • Check the stack size in your other tasks is sufficient.
  • Under Component Config -> FreeRTOS enable "Set a debug watchpoint as a stack overflow check"
If none of this seems to lend any clue, could you please post some code that exhibits the problem?
Hi ESP_Angus,

I tried to get backtrace functions name and they seems to be something of possibile. Find them below:

Code: Select all

bt_manager_disconnect
bt_manager_disconnect 		./components/bluetooth_manager/./bluetooth_manager.c:554
vTaskEnterCritical 			/Users/federico/Esp32/esp-idf/components/freertos/./tasks.c:4409
xQueueGenericSend 			/Users/federico/Esp32/esp-idf/components/freertos/./queue.c:2040
xTimerGenericCommand		/Users/federico/Esp32/esp-idf/components/freertos/./timers.c:839
login_timeout_callback		./components/bluetooth_manager/./bluetooth_protocol.c:58
prvProcessExpiredTimer		/Users/federico/Esp32/esp-idf/components/freertos/./timers.c:839
prvProcessTimerOrBlockTask	/Users/federico/Esp32/esp-idf/components/freertos/./timers.c:839
prvTimerTask 				/Users/federico/Esp32/esp-idf/components/freertos/./timers.c:839
But honestly I already found the answer to the problem: "bt_manager_disconnect" is a generic function that contains a printf :(
I'm sorry, absolutely my fault!
Hardwire.io / The IoT Platform

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: Sample code for software based timer into ESP32-idf

Postby Trialblazer47 » Wed Nov 08, 2017 10:08 am

thanks for the example . :) just in case some wants to test.

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_log.h"
#include "freertos/timers.h"

#define TAG "TIME"

/* timer calls the function ping after interval time. xTimerCreate() takes interval in TICKs so
pdMS_TO_TICKS() converts ms interval to appropriate TICKS. pdTRUE will set timer to call periodically and if Set pdFALSE,
function is called once only */

TimerHandle_t tmr;
int id = 1;
int interval = 5000;
void timerCallBack( TimerHandle_t xTimer )
{
  ESP_LOGI(TAG,"tring tring!!!");
}
void app_main()
{
    ESP_LOGI(TAG,"Timer Test.\n");

    tmr = xTimerCreate("MyTimer", pdMS_TO_TICKS(interval), pdTRUE, ( void * )id, &timerCallBack);
    if( xTimerStart(tmr, 10 ) != pdPASS ) {
     printf("Timer start error");
    }
}
Thanks.

Who is online

Users browsing this forum: No registered users and 239 guests