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

gchini
Posts: 18
Joined: Wed Jun 08, 2022 1:41 pm

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

Postby gchini » Wed Jun 08, 2022 2:03 pm

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?

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

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

Postby ESP_Sprite » Thu Jun 09, 2022 2:18 am

Moving to Arduino subforum.

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

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

Postby chegewara » Thu Jun 09, 2022 4:23 am

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)

gchini
Posts: 18
Joined: Wed Jun 08, 2022 1:41 pm

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

Postby gchini » Thu Jun 09, 2022 6:41 am

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

lbernstone
Posts: 831
Joined: Mon Jul 22, 2019 3:20 pm

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

Postby lbernstone » Thu Jun 09, 2022 8:56 am

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() {}

gchini
Posts: 18
Joined: Wed Jun 08, 2022 1:41 pm

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

Postby gchini » Thu Jun 09, 2022 3:58 pm

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

lbernstone
Posts: 831
Joined: Mon Jul 22, 2019 3:20 pm

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

Postby lbernstone » Thu Jun 09, 2022 8:37 pm

Raspberry Pi 3 costs about $30. I don't think you are looking for a MCU.

Miraculix
Posts: 20
Joined: Wed Sep 16, 2020 9:43 am

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

Postby Miraculix » Sat Jun 11, 2022 9:20 am

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

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

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

Postby Craige Hales » Sat Jun 11, 2022 3:18 pm

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
Craige

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

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

Postby tommeyers » Sat Jun 11, 2022 8:40 pm

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
IT Professional, Maker
Santiago, Dominican Republic

Who is online

Users browsing this forum: No registered users and 37 guests