stack overflow, if i use a varibale in a task

azz-zza
Posts: 45
Joined: Tue Sep 17, 2019 2:58 am

stack overflow, if i use a varibale in a task

Postby azz-zza » Tue Sep 17, 2019 3:10 am

Hello,
my attempts to use a variable in the task fails miserably. What am i doing incorrectly?
thank you in advance:

this works:

Code: Select all

[Codebox=c file=Untitled.c]
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/gpio.h"
#include "nvs_flash.h"
#include <stdint.h>

void blinking_task(void *pvParameter)
{
    // set gpio and its direction
    gpio_pad_select_gpio(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    int current_led = 1;
    while (1)
    {
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
      [b] // printf("Current led is - %d \n",current_led);[/b]
        printf("blah \n");
    }
}

void main{} {
    BaseType_t xReturned;
   xReturned = xTaskCreate(blinking_task, "blinky", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
}[/Codebox]
This does not:

Code: Select all

[Codebox=c file=Untitled.c]
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/gpio.h"
#include "nvs_flash.h"
#include <stdint.h>

void blinking_task(void *pvParameter)
{
    // set gpio and its direction
    gpio_pad_select_gpio(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    int current_led = 1;
    while (1)
    {
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
       [b]printf("Current led is - %d \n",current_led);[/b]
        printf("blah \n");
    }
}

void main{} {
    BaseType_t xReturned;
   xReturned = xTaskCreate(blinking_task, "blinky", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
}
[/Codebox]

the only difference is :
printf("Current led is - %d \n",current_led);

Output:
  1. ␛[0;32mI (246) cpu_start: Starting scheduler on PRO CPU.␛[0m
  2. ␛[0;32mI (0) cpu_start: Starting scheduler on APP CPU.␛[0m
  3. Hello world!
  4. Current led is - 1
  5. blah
  6. ***ERROR*** A stack overflow in task blinky has been detected.
  7. abort() was called at PC 0x40089714 on core 0
  8.  
  9. Backtrace: 0x40089403:0x3ffb3b50 0x400896fd:0x3ffb3b70 0x40089714:0x3ffb3b90 0x400876a9:0x3ffb3bb0 0x40086ac8:0x3ffb3bd0 0x40086a7e:0x00000006
  10.  
  11. Rebooting...
  12. ets Jun  8 2016 00:22:57

Thank you for your advice.
Last edited by azz-zza on Tue Sep 17, 2019 1:04 pm, edited 1 time in total.

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

Re: stack overflow if i use a varible in a task

Postby ESP_Angus » Tue Sep 17, 2019 3:40 am

Hi azz,

printf() formatting uses a non-trivial amount of stack, the other printf() probably fits because it doesn't include any format arguments.

When creating the task, change "configMINIMAL_STACK_SIZE" (768 bytes) to something larger, maybe 2048.

azz-zza
Posts: 45
Joined: Tue Sep 17, 2019 2:58 am

Re: stack overflow if i use a varible in a task

Postby azz-zza » Tue Sep 17, 2019 4:17 am

Thank you ESP32. setting to 2K solved the issue.

Is there a way to see how much stack is used dynamically?
What is the good practice to set the stack size?
thank you.

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

Re: stack overflow if i use a varible in a task

Postby ESP_Angus » Tue Sep 17, 2019 6:48 am

Hi azz,

FreeRTOS has a function to get the "stack high water mark" for any task:
https://docs.espressif.com/projects/esp ... skHandle_t

Suggest leaving some headroom to start with (if you get more "stack overflow" crashes then add more headroom!) Then once everything is implemented then use this function to check if you can scale back to save some RAM.

(If you're not running out of free RAM then there's no harm in having a larger-than-needed stack size, also.)

azz-zza
Posts: 45
Joined: Tue Sep 17, 2019 2:58 am

Re: stack overflow if i use a varible in a task

Postby azz-zza » Tue Sep 17, 2019 1:03 pm

Makes sense. thank you ESP32.

Who is online

Users browsing this forum: Google [Bot] and 156 guests