ESP32-DevKitM-1 freeRTOS timer crashing

Ninjabean
Posts: 5
Joined: Tue Sep 07, 2021 5:44 am

ESP32-DevKitM-1 freeRTOS timer crashing

Postby Ninjabean » Thu Sep 30, 2021 8:47 am

I am using ESP32-DevKitM-1 which has a single core ESP32 SoC, ECLIPSE, ESP-IDF v 4.3.

I started with the uart_events example and when I use xTimerCreate() and run vTaskStartScheduler() this is what happens:
If I don't start the timer before running the scheduler = Crash
assertion "xReturn != ( -1 )" failed: file "C:/ESP32/ESP-IDF/components/freertos
/tasks.c", line 2312, function: vTaskStartScheduler

abort() was called at PC 0x400d513f on core 0
0x400d513f: __assert_func at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-min
gw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdlib/assert.c:62 (discriminator 8
)

Setting breakpoint at 0x400843da and returning...
0x400843da: panic_abort at C:/ESP32/ESP-IDF/components/esp_system/panic.c:367
If I do start the timer before running the scheduler = Crash with stack overflow error
***ERROR*** A stack overflow in task Tmr Svc has been detected.
Setting breakpoint at 0x400843da and returning...
0x400843da: panic_abort at C:/ESP32/ESP-IDF/components/esp_system/panic.c:367
I've look around and some suggested increasing the timer and task stacks. I doubled them and still get the same errors.

My Timer callback is quite light.

Code: Select all

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/timers.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "driver/timer.h"

//...
static TaskHandle_t xHandle = NULL;
static TimerHandle_t xTimer = NULL;

bool timeout;
//...

void vTimerCallback( TimerHandle_t xTimer )
{
	if (xTimer == xModbusTimer){	//modbus timer expired
		Timeout = 1;
		if(xHandle != NULL){
			vTaskResume(xHandle);
		}
	}
}

//...

void app_main(void)
{
	xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(10), pdFALSE, ( void * ) 0, vTimerCallback);
	xTimerStart(xTimer,0);
	xTaskCreate(handler_task, "handler_task", 2048, NULL, 12, &xHandle);
    	//Create a task to handler UART event from ISR
    	xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
    	vTaskStartScheduler();
}
Some help will be much appreciated.
Last edited by Ninjabean on Thu Sep 30, 2021 7:33 pm, edited 1 time in total.

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

Re: ESP32-DevKitM-1 freeRTOS timer crashing

Postby ESP_Sprite » Thu Sep 30, 2021 9:59 am

Ninjabean wrote:
Thu Sep 30, 2021 8:47 am
I started with the uart_events example and when I use xTimerCreate() and run vTaskStartScheduler()
Calling vTaskStartScheduler may be your issue: when ESP-IDF calls your app_main, the scheduler already is running; app_main itself is started in a task context. Note that this also means your timer may fire before your handler_task is started and 'xHandle' is set to something sane.

Ninjabean
Posts: 5
Joined: Tue Sep 07, 2021 5:44 am

Re: ESP32-DevKitM-1 freeRTOS timer crashing

Postby Ninjabean » Sun Oct 03, 2021 1:56 am

Thank you for the reply.
Yes, removing vTaskStartScheduler() solved the problem.
And to get around timer calling xHandle before it is started I put in a check to see if xHandle has been started in the timer callback.

Code: Select all

		if(xHandle != NULL){
			vTaskResume(xHandle);
		}

Who is online

Users browsing this forum: Baidu [Spider], Baldhead and 145 guests