Stack overflow when trying to use tasks

toxadi
Posts: 4
Joined: Wed Nov 01, 2017 12:00 pm

Stack overflow when trying to use tasks

Postby toxadi » Wed Nov 22, 2017 12:08 pm

Hello all,

I have been trying to start a project using a WROOM module with an SD card, humifdity, temp, GPS and some other sensors but I am stuck at base level. I want to use different tasks for each sensor, sending messages to the SD card task, so that it will write when needed to the card but I have continuous stack overflows and reboots.

I compiled and run successfully the SD card example alone, but when I try to put it in a task I get the following

Code: Select all

Leaving...
Hard resetting...
MONITOR
--- idf_monitor on COM8 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_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:0x3fff0010,len:4
load:0x3fff0014,len:4424
load:0x40078000,len:0
ho 12 tail 0 room 4
load:0x40078000,len:11460
entry 0x40078cc4
W (35) rtc_clk: Possibly invalid CONFIG_ESP32_XTAL_FREQ setting (40MHz). Detected 40 MHz.
I (281) cpu_start: Pro cpu up.
I (281) cpu_start: Single core mode
I (283) heap_init: Initializing. RAM available for dynamic allocation:
I (293) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (312) heap_init: At 3FFB1CF8 len 0002E308 (184 KiB): DRAM
I (331) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (350) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (370) heap_init: At 40087E08 len 000181F8 (96 KiB): IRAM
I (389) cpu_start: Pro cpu start user code
I (452) cpu_start: Starting scheduler on PRO CPU.
I (457) appname: Starting program
I (466) appname: Done nvs_flash_init
I (3466) appname: Starting tasks
I (3466) appname: Done starting tasks
I (3466) appname: Initializing SD car***ERROR*** A stack overflow in task task_SD_card has been detected.
abort() was called at PC 0x4008664c on core 0
0x4008664c: vApplicationStackOverflowHook at C:/tools/msys32/home/kgotsis/esp-idf/components/esp32/panic.c:553


Backtrace: 0x40086534:0x3ffb90b0 0x40086633:0x3ffb90d0 0x4008664c:0x3ffb90f0 0x40083a75:0x3ffb9110 0x40085430:0x3ffb9130 0x400853ea:0x0000d000
0x40086534: invoke_abort at C:/tools/msys32/home/kgotsis/esp-idf/components/esp32/panic.c:553

0x40086633: abort at C:/tools/msys32/home/kgotsis/esp-idf/components/esp32/panic.c:553

0x4008664c: vApplicationStackOverflowHook at C:/tools/msys32/home/kgotsis/esp-idf/components/esp32/panic.c:553

0x40083a75: vTaskSwitchContext at C:/tools/msys32/home/kgotsis/esp-idf/components/freertos/tasks.c:4482

0x40085430: _frxt_dispatch at C:/tools/msys32/home/kgotsis/esp-idf/components/freertos/portasm.S:393

0x400853ea: _frxt_int_exit at C:/tools/msys32/home/kgotsis/esp-idf/components/freertos/portasm.S:205


Rebooting...



My code is quite simple

Code: Select all

#include "freertos/FreeRTOS.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "esp_vfs_fat.h"
#include "driver/gpio.h"
#include "esp_log.h"


static const char *TAG = "appname";


int setupSDCard()
{
       ESP_LOGI(TAG, "Initializing SD card");
       gpio_pullup_en(GPIO_NUM_2);
       vTaskDelay(200 / portTICK_PERIOD_MS);

       ESP_LOGI(TAG, "Using SDMMC peripheral");
       sdmmc_host_t host = SDMMC_HOST_DEFAULT();

       // To use 1-line SD mode, uncomment the following line:
       host.flags = SDMMC_HOST_FLAG_1BIT;

       // This initializes the slot without card detect (CD) and write protect (WP) signals.
       // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
       sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
       //config.max_freq_khz = SDMMC_FREQ_PROBING; // << add this line

      // Options for mounting the filesystem.
      // If format_if_mount_failed is set to true, SD card will be partitioned and
      // formatted in case when mounting fails.
      esp_vfs_fat_sdmmc_mount_config_t mount_config = {
         .format_if_mount_failed = false,
         .max_files = 5
      };

      // Use settings defined above to initialize SD card and mount FAT filesystem.
      // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
      // Please check its source code and implement error recovery when developing
      // production applications.
      sdmmc_card_t* card;
      esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

      if (ret != ESP_OK) {
         if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount filesystem. "
            "If you want the card to be formatted, set format_if_mount_failed = true.");
            } else {
            ESP_LOGE(TAG, "Failed to initialize the card (%d). "
            "Make sure SD card lines have pull-up resistors in place.", ret);
         }
         return ESP_OK;
      }

      return ESP_OK;
}


static void tskSDCard()
{
   setupSDCard();

   // some code will be added here
   while (1){
      vTaskDelay(1000 / portTICK_PERIOD_MS);
   }
}

void app_main(void)
{
    ESP_LOGI(TAG, "Starting program");
    nvs_flash_init();
    ESP_LOGI(TAG, "Done nvs_flash_init");

    gpio_set_direction(GPIO_NUM_4, GPIO_MODE_OUTPUT);

    vTaskDelay(3000 / portTICK_PERIOD_MS);

    int level = 0;

    ESP_LOGI(TAG, "Starting tasks");
    xTaskCreate(tskSDCard, "task_SD_card", 1024, NULL, tskIDLE_PRIORITY, NULL);
    //xTaskCreate(&tskHumidity, "task_Humidity", 1024, NULL, tskIDLE_PRIORITY, NULL);
    //xTaskCreate(&tskTemperature, "task_Temperature", 1024, NULL, tskIDLE_PRIORITY, NULL);
    // additional tasks

    ESP_LOGI(TAG, "Done starting tasks");
    while (true) {   //keepalive indicator
        gpio_set_level(GPIO_NUM_4, level);
        level = !level;
        vTaskDelay(300 / portTICK_PERIOD_MS);
    }
}


Do I have to setup something specifically in menuconfig in order to enable tasks? Am I missing something fundamental in the way a project is being setup and take special consideration is stack sizes or anything similar?

Thank you in advance

User avatar
kolban
Posts: 1086
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Stack overflow when trying to use tasks

Postby kolban » Wed Nov 22, 2017 4:53 pm

Howdy,
Lets look at this statement you have coded:

Code: Select all

xTaskCreate(tskSDCard, "task_SD_card", 1024, NULL, tskIDLE_PRIORITY, NULL);


If we take each parameter apart one by one, we find the third parameter (1024) is your stack size for the task. 1024 is pretty low. Remember you have about 100+ K to play with. Try larger numbers ... for example 8000 or better and see if that helps. Once you have a stable environment, you can "ask" the application's tasks how much stack they actually needed during normal operation and tune downwards as needed. Don't try and allocate too little up front as when you run out of stack, you'll crash. Allocate more than you need (but not so much that there is no memory left) and tune downwards.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ClockToshi
Posts: 2
Joined: Thu Nov 16, 2017 9:09 pm

Re: Stack overflow when trying to use tasks

Postby ClockToshi » Wed Nov 22, 2017 11:35 pm

interesting. how do you check what's the used stack in a task?

chegewara
Posts: 56
Joined: Wed Jun 14, 2017 9:00 pm

Re: Stack overflow when trying to use tasks

Postby chegewara » Thu Nov 23, 2017 7:06 am

It is this value:
/* The minimum amount of stack space that has remained for the task since
the task was created. The closer this value is to zero the closer the task
has come to overflowing its stack. */

Code: Select all

   unsigned short usStackHighWaterMark;


http://www.freertos.org/uxTaskGetSystemState.html

If you set stack, lets say 8kB, and you will find that usStackHighWaterMark returns value is 3kB then your stack is filled in with 5kB data.

ClockToshi
Posts: 2
Joined: Thu Nov 16, 2017 9:09 pm

Re: Stack overflow when trying to use tasks

Postby ClockToshi » Thu Nov 23, 2017 11:41 am

thank you, very informative and useful

toxadi
Posts: 4
Joined: Wed Nov 01, 2017 12:00 pm

Re: Stack overflow when trying to use tasks

Postby toxadi » Thu Nov 23, 2017 2:21 pm

That was it, thank you very much!

I could never think that 1k is "pretty low", being deeply carved by my pic16c84 days with 36 bytes of RAM :) Wow...

It does make sense though, considering what is being run

Thanks again

Who is online

Users browsing this forum: No registered users and 7 guests