Stack Overflow In Arduino setup()


Please forgive my newbie nature, to FreeRTOS and to this support site.

I have managed to get up and running with FreeRTOS on an Arduino Mega 2560. My aim is to port an already large program to a real-time platform, thereby simplifying timing issues and the operation of multiple state machines. Unfortunately, after a very promising start, I have hit a wall.

Whittling the code down to the bare minimum, it looks like this:

void setup() {

A “standard” build, without the FreeRTOS include file, does what you’d expect: nothing. However, with the include file, it flashes the LED to indicate a stack overflow!

I think I need to increase the size of the stack that setup() gets to use. What do you think? And, if so, where do I make the change?

Many thanks for your patience and any assistance.

Cheers, 'Nic

I don’t use the Arduino system, but I believe that setup is called BEFORE FreeRTOS is started, so it can not do anything that “blocks”, like vTaskDelay, as the task.interrupt structure isn’t fully setup yet.

setup() should just create the various tasks/objects that you program will use, and the after you return from it, FreeRTOS will start, and the Arduino system will create a task that will call loop() which will then run as well as any other tasks you have created.

1 Like

Hi @richard-damon,
Many thanks. I agree with all that you have said but I was rather confused when the vTaskDelay() call was replaced by a call to Delay(), with the same result. I was under the impression that a suib 15 mS delay would call the native delay() function instead of the full vTaskDelay() function for larger delays and when in real-time mode.
I will follow your guidance. However, I think I may end up creating a setup task to do the original setup() job, including the starting of the tasks which are more to do with the “standard” version of loop().
I shall sleep on it. Things will fall into place by the morning. At this stage of my port, it would be wrong to build dodgy foundations.
Cheers, 'Nic

I don’t know how the Arduino package does things with the “Delay” function.

The normal method would be that setup creates the objects and tasks, which won’t require doing any delays. Hardware initialization that needs to do blocking actions will tend to be in a task, normally the task that uses that hardware, and that initialization code is before that task enters its “forever” loop.

You need to be careful when creating tasks and objects in a Task, as you then need to think carefully about possible race conditions that affect the order things happen.

1 Like