Page 1 of 1

I'm programming with arduino but i need more stack space

Posted: Wed Jun 08, 2022 2:03 pm
by gchini
Hi at all... I'm new

I'm writing a program with the ARDUINO IDE but i have a problem of stack overflow due to large structures in a library that i use. I don't want change the library because i don't want introduce errors in the code (in particular i need to free() for each malloc()).

I have some questions
1. How can i increase the stack size for my arduino process?

Re: I'm programming with arduino but i need more stack space

Posted: Thu Jun 09, 2022 2:18 am
by ESP_Sprite
Moving to Arduino subforum.

Re: I'm programming with arduino but i need more stack space

Posted: Thu Jun 09, 2022 4:23 am
by chegewara
2 options:
- to create a new task and delete default one
- add this in your code

Code: Select all

#define ARDUINO_LOOP_STACK_SIZE (x * 1024)

Re: I'm programming with arduino but i need more stack space

Posted: Thu Jun 09, 2022 6:41 am
by gchini
I've allredy used

Code: Select all

  size_t getArduinoLoopTaskStackSize() {
    return 32 * 1024; // bytes
  }
but it did not worked.

I'll try even

Code: Select all

#define ARDUINO_LOOP_STACK_SIZE (x * 1024)
But there is a manual or a list of API to understand these variables? I mean where are defined these definitions?

==============================================================

But i'm intrested to understand how i can create a (new task with the stack size) because i'll use in the future.
Do you have any suggestion/link/starting point?

===============================================================

Many thanks

Re: I'm programming with arduino but i need more stack space

Posted: Thu Jun 09, 2022 8:56 am
by lbernstone
Arduino IDE provides no way to modify defines before Arduino.h is included. It is intended for use by people that don't want any of that complexity, so it hides it. In platform.io or Arduino CLI, you can use the defines to change the stack size.

Code: Select all

void newloop(void* param) {
  while(1) {
    delay(10000);
    Serial.printf("memtask: %d\n", uxTaskGetStackHighWaterMark(NULL));
  }
}

void setup() {
  Serial.begin(115200);
  xTaskCreateUniversal(newloop,"newloop", 32*1024, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
  vTaskDelete(NULL);
}
void loop() {}

Re: I'm programming with arduino but i need more stack space

Posted: Thu Jun 09, 2022 3:58 pm
by gchini
Hi, I’ve tried the function that you have told me. In particular i’ve tried this code:

Code: Select all

void newloop(void* param) {
  while(1) {
    delay(10000);
    Serial.printf("memtask: %d\n", uxTaskGetStackHighWaterMark(NULL));
  }
}

void setup() {
  Serial.begin(115200);
  xTaskCreateUniversal(newloop,"newloop", 64*1024, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
  vTaskDelete(NULL);
}
void loop() {}
The output is 64200, that is correct. But i need MORE than 64k of stack. I need around 350k,

Do you know why when i use the xTaskCreateUniversal(newloop,“newloop”, x*1024, NULL, 1, NULL, ARDUINO_RUNNING_CORE); with x>64 the output is <64k?

Many thanks

Re: I'm programming with arduino but i need more stack space

Posted: Thu Jun 09, 2022 8:37 pm
by lbernstone
Raspberry Pi 3 costs about $30. I don't think you are looking for a MCU.

Re: I'm programming with arduino but i need more stack space

Posted: Sat Jun 11, 2022 9:20 am
by Miraculix
Not sure if this works for other MCUs, but it works for ESP32-S3. Put this at the beginning of your sketch (increase size if needed)

SET_LOOP_TASK_STACK_SIZE(16 * 1024); // 16KB

Re: I'm programming with arduino but i need more stack space

Posted: Sat Jun 11, 2022 3:18 pm
by Craige Hales
Do you know why when i use the xTaskCreateUniversal(newloop,“newloop”, x*1024, NULL, 1, NULL, ARDUINO_RUNNING_CORE); with x>64 the output is <64k?
the parameter is a 16 bit int. I'm not sure you'll find 350K of contiguous memory for a stack.

Code: Select all

#ifndef configSTACK_DEPTH_TYPE
	/* Defaults to uint16_t for backward compatibility, but can be overridden
	in FreeRTOSConfig.h if uint16_t is too restrictive. */
	#define configSTACK_DEPTH_TYPE uint16_t
#endif

Re: I'm programming with arduino but i need more stack space

Posted: Sat Jun 11, 2022 8:40 pm
by tommeyers
The replies all address increasing stack size. I suggest a different approach: define the problem the code as a whole is to solve then consider the various solutions and their data structures.

And answer: what data structures besides a stack can be used.

Tom Meyers