xTaskCreate -> Crash

Kazuya91
Posts: 27
Joined: Wed Aug 23, 2017 11:33 am

xTaskCreate -> Crash

Postby Kazuya91 » Mon Jan 01, 2018 2:18 pm

Hello,
i am new to the ESP32 and I am using the espressif Framework (not Arduino) with Eclipse. I try to write code, so Task1 and Task2 synchronize each other with xTaskNotifyGive.

I dont know why, but my ESP32 keeps crashing. The Code is from an example of the official FreeRTOS site:
https://www.freertos.org/xTaskNotifyGive.html

This is my Code:

Code: Select all


#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_attr.h"
#include "esp_deep_sleep.h"
#include "nvs_flash.h"
#include <cJSON.h>


#include "lwip/err.h"
#include "apps/sntp/sntp.h"
#include <curl/curl.h>


/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;

/* The event group allows multiple bits for each event,
   but we only care about one event - are we connected
   to the AP with an IP? */
const int CONNECTED_BIT = BIT0;

static const char *TAG = "example";

/* Variable holding number of times ESP32 restarted since first boot.
 * It is placed into RTC memory using RTC_DATA_ATTR and
 * maintains its value when ESP32 wakes from deep sleep.
 */
RTC_DATA_ATTR static int boot_count = 0;

static void prvTask1( void *pvParameters );
static void prvTask2( void *pvParameters );
static TaskHandle_t xTask1 = NULL, xTask2 = NULL;


static void prvTask1( void *pvParameters )
{
    for( ;; )
    {
        /* Send a notification to prvTask2(), bringing it out of the Blocked
        state. */
    		ESP_LOGI(TAG, "I am Task1, I notify Task2");
        xTaskNotifyGive( xTask2 );


        /* Block to wait for prvTask2() to notify this task. */
        ESP_LOGI(TAG, "I am Task2 and I wait for the notification of Task1");
        ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
    }
}
/*-----------------------------------------------------------*/

static void prvTask2( void *pvParameters )
{
    for( ;; )
    {
        /* Block to wait for prvTask1() to notify this task. */
    		ESP_LOGI(TAG, "I am Task2 and I wait for the notification of Task1");
        ulTaskNotifyTake( pdTRUE, portMAX_DELAY );

        /* Send a notification to prvTask1(), bringing it out of the Blocked
        state. */
        ESP_LOGI(TAG, "I am Task2, I notify Task1");
        xTaskNotifyGive( xTask1 );

    }
}

void app_main(){

	if (boot_count == 0){

		//-----------------Boot-------------------------
		ESP_LOGI(TAG, "COLD START!");
		ESP_LOGI(TAG, "Boot count: %d", boot_count);
		boot_count++;

		//-----------------Task_Creation----------------

		xTaskCreate( prvTask1, "Task1", 200, NULL, tskIDLE_PRIORITY, &xTask1 );
		xTaskCreate( prvTask2, "Task2", 200, NULL, tskIDLE_PRIORITY, &xTask2 );
		vTaskStartScheduler();

		//-----------------Deep_Sleep------------------
		/*
		const int deep_sleep_sec = 5;
		ESP_LOGI(TAG, "Entering deep sleep for %d seconds", deep_sleep_sec);
		esp_deep_sleep(1000000LL * deep_sleep_sec);
		*/
	}

	//nach Sleep
	if(boot_count >= 1){
		ESP_LOGI(TAG, "I am awake after Deep_SLEEP!");
	}
}

As you can see its almost the exact same code from the linked example except for some Printouts.

The Error i get is the following: Guru Meditation Error of type InstrFetchProhibited occurred on core 0. Exception was unhandled.

Code: Select all

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0010,len:4
load:0x3fff0014,len:5096
load:0x40078000,len:0
ho 12 tail 0 room 4
load:0x40078000,len:12664
entry 0x40078f50
I (34) boot: ESP-IDF v3.0-dev-203-gdce7fcb9-dirty 2nd stage bootloader
I (34) boot: compile time 14:29:56
I (139) boot: Enabling RNG early entropy source...
I (140) boot: SPI Speed      : 80MHz
I (140) boot: SPI Mode       : DIO
I (169) boot: SPI Flash Size : 4MB
I (207) boot: Partition Table:
I (241) boot: ## Label            Usage          Type ST Offset   Length
I (309) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (379) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (449) boot:  2 factory          factory app      00 00 00010000 00100000
I (519) boot: End of partition table
I (558) boot: Disabling RNG early entropy source...
I (609) boot: Loading app partition at offset 00010000
I (663) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x076dc ( 30428) map
I (783) esp_image: segment 1: paddr=0x00017704 vaddr=0x3ffb0000 size=0x02d88 ( 11656) load
I (843) esp_image: segment 2: paddr=0x0001a494 vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _iram_start at ??:?

I (911) esp_image: segment 3: paddr=0x0001a89c vaddr=0x40080400 size=0x05774 ( 22388) load
I (1025) esp_image: segment 4: paddr=0x00020018 vaddr=0x400d0018 size=0x26108 (155912) map
0x400d0018: _flash_cache_start at ??:?

I (1271) esp_image: segment 5: paddr=0x00046128 vaddr=0x40085b74 size=0x0d19c ( 53660) load
0x40085b74: prvProcessReceivedCommands at /Users/serhat/esp/esp-idf/components/freertos/./timers.c:660

I (1351) esp_image: segment 6: paddr=0x000532cc vaddr=0x400c0000 size=0x00000 (     0) load
I (1352) esp_image: segment 7: paddr=0x000532d4 vaddr=0x50000000 size=0x00004 (     4) load
I (1452) cpu_start: Pro cpu up.
I (1453) cpu_start: Starting app cpu, entry point is 0x40080e00
0x40080e00: call_start_cpu1 at /Users/serhat/esp/esp-idf/components/esp32/./cpu_start.c:192

I (1514) cpu_start: App cpu up.
I (1552) heap_init: Initializing. RAM available for dynamic allocation:
I (1616) heap_init: At 3FFAE2A0 len 00001D60 (7 KiB): DRAM
I (1673) heap_init: At 3FFB66C8 len 00029938 (166 KiB): DRAM
I (1732) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (1791) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1851) heap_init: At 40092D10 len 0000D2F0 (52 KiB): IRAM
I (1909) cpu_start: Pro cpu start user code
I (2065) cpu_start: Starting scheduler on PRO CPU.
I (2068) cpu_start: Starting scheduler on APP CPU.
I (2068) example: COLD START!
I (2068) example: Boot count: 0
Guru Meditation Error of type InstrFetchProhibited occurred on core  0. Exception was unhandled.
Register dump:
PC      : 0x800e3a38  PS      : 0x00050033  A0      : 0x800e3a38  A1      : 0x3ffb75c0  
A2      : 0x3ffb4e64  A3      : 0x3f404f7c  A4      : 0x50000000  A5      : 0x3ffb7610  
A6      : 0x3ffb75f0  A7      : 0x0000000c  A8      : 0x00000000  A9      : 0x800d07aa  
A10     : 0x3ffb7630  A11     : 0x7fffffff  A12     : 0x50000000  A13     : 0x00000001  
A14     : 0x3ffb5230  A15     : 0x00000000  SAR     : 0x00000000  EXCCAUSE: 0x00000014  
EXCVADDR: 0x800e3a38  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

Backtrace: 0x400e3a38:0x3ffb75c0 0x400e3a35:0x3ffb75f0
0x400e3a38: app_main at /Users/serhat/esp/esp-idf/examples/get-started/hello_world/main/./hello_world_main.c:107

0x400e3a35: app_main at /Users/serhat/esp/esp-idf/examples/get-started/hello_world/main/./hello_world_main.c:96 (discriminator 1)


Rebooting...
If i remove both xTaskCreate Functions, the ESP32 doesnt crash....

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

Re: xTaskCreate -> Crash

Postby ESP_Sprite » Mon Jan 01, 2018 5:16 pm

200 bytes of stack is way too little for newlibs printf function. Try giving it some more, 4K or so, and it should work.

Kazuya91
Posts: 27
Joined: Wed Aug 23, 2017 11:33 am

Re: xTaskCreate -> Crash

Postby Kazuya91 » Mon Jan 01, 2018 9:13 pm

Hey Sprite,
Thanks that helped. I additionally removed the line with "vTaskStartScheduler() ".

But i have another problem now...

I try to send JSON data via curl. I have a local webserver with the django-framework. If my server receives something, it gives me a printout which it does in this case. So my ESP32 is sending data and my server receives it. The problem is that it crashes with an Error called:

Guru Meditation Error of type IllegalInstruction occurred on core 0. Exception was unhandled.

my Code looks like this right now:

Code: Select all

int simplecurl(){

	  CURL *curl;
	  CURLcode res;

	  static const char *postthis = "{ \"mac\" : \"24:0a:c4:04:fd:28\" , \"battery\" : \"100\" , \"message\" : \"You got a message\"}";

	  curl = curl_easy_init();
	  if(curl) {
	    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.178.34:8000/kalender/returnjson/");
	    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);

	    /* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
	       itself */
	    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));

	    /* Perform the request, res will get the return code */
	    res = curl_easy_perform(curl);
	    /* Check for errors */
	    if(res != CURLE_OK)
	      fprintf(stderr, "curl_easy_perform() failed: %s\n",
	              curl_easy_strerror(res));

	    /* always cleanup */
	    curl_easy_cleanup(curl);
	  }
	  return 0;



}

void app_main(){

	//first boot
	//do everything here just once
	if (boot_count == 0){

		//-----------------Boot-------------------------
		ESP_LOGI(TAG, "COLD START!");
		ESP_LOGI(TAG, "Boot count: %d", boot_count);
		boot_count++;

		//------------------WIFI-----------------------

		initialise_wifi();
		xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
		printf("\nI am Connected!!\n");

		//------------------Curl-----------------------
		xTaskCreate(&simplecurl, "Curl", 10240, NULL, 5, NULL);

}

I erased all other code so i can be sure its really the lines of code above.

This is the Error-Log.

Code: Select all

I (5067) wifi: connected with SSID-censored, channel 11
I (6017) event: ip: 192.168.178.31, mask: 255.255.255.0, gw: 192.168.178.1

I am Connected!!
Guru Meditation Error of type IllegalInstruction occurred on core  0. Exception was unhandled.
Register dump:
PC      : 0x40100618  PS      : 0x00060f30  A0      : 0x00000000  A1      : 0x3ffc5f80  
0x40100618: simplecurl at /Users/serhat/esp/esp-idf/examples/get-started/hello_world/main/./hello_world_main.c:164

A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000000  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x80100616  A9      : 0x3ffc5f60  
A10     : 0x3ffc62bc  A11     : 0x0000003c  A12     : 0x00000052  A13     : 0x3ffc600c  
A14     : 0x00000001  A15     : 0x00000000  SAR     : 0x00000010  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x00000000  

Backtrace: 0x40100618:0x3ffc5f80 0x7ffffffd:0x3ffc5fa0
0x40100618: simplecurl at /Users/serhat/esp/esp-idf/examples/get-started/hello_world/main/./hello_world_main.c:164


Rebooting...
ets Jun  8 2016 00:22:57
Thanks in advance!

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: xTaskCreate -> Crash

Postby WiFive » Mon Jan 01, 2018 10:41 pm

Tasks are normally implemented as an infinite loop, and must never attempt to return or exit from their implementing function. Tasks can however delete themselves.
https://www.freertos.org/a00125.html

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: xTaskCreate -> Crash

Postby fly135 » Sat Jan 13, 2018 11:14 pm

I totally was bit by these problems today. I found that adding a printf used up about 2K of stack. And I was crashing on exiting the task until I read the doc that WiFi referenced.

Pkcorp
Posts: 2
Joined: Wed Mar 07, 2018 10:26 pm

Re: xTaskCreate -> Crash

Postby Pkcorp » Wed Mar 07, 2018 10:37 pm

Hey,

reusing this thread because I have a similar problem.
Whenever I am using either xTaskCreatePinnedToCore or even just a normal xTaskCreate, I get a Guru Error Crash:

Code: Select all

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28
Guru Meditation Error: Core  0 panic'ed (IllegalInstruction)
. Exception was unhandled.
Register dump:
PC      : 0x400d15c8  PS      : 0x00060430  A0      : 0x00000000  A1      : 0x3ffd2e20
A2      : 0x3ffc335c  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000000
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x800d15c8  A9      : 0x3ffd2de0
A10     : 0x00000001  A11     : 0x3ffd2e28  A12     : 0x00000001  A13     : 0x3ffd2e38
A14     : 0x00000000  A15     : 0x00000004  SAR     : 0x00000010  EXCCAUSE: 0x00000000
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff

Backtrace: 0x400d15c8:0x3ffd2e20 0x7ffffffd:0x3ffd2e60
This error was created by xTaskCreatePinnedToCore(myFunction, "myFunction", 8192, NULL, 1, NULL, 0).
I get the same result if I try to pin the function to core 1 or when changing the stack size.
I'm using the Arduino framework.

Any suggestions?

Thanks in advance,

Pkcorp.

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

Re: xTaskCreate -> Crash

Postby ESP_Sprite » Thu Mar 08, 2018 6:59 am

Does your myFunction has a vTaskDelete(NULL) or an infinite loop in it? If not, place one of the two in it.

Pkcorp
Posts: 2
Joined: Wed Mar 07, 2018 10:26 pm

Re: xTaskCreate -> Crash

Postby Pkcorp » Thu Mar 08, 2018 9:24 pm

That solved it! I assumed the task would stop with the end of the function, my bad.

Thanks so much!

Who is online

Users browsing this forum: Bing [Bot] and 110 guests