How to set stack size for idle task?

jankok wrote on Tuesday, March 13, 2018:

I’m using an Arduino Uno with the Arduino IDE, using FreeRTOS v10.0.0. The Blink_AnalogRead example runs fine.
When I try to run some code in the loop() function (which I believe is the idle process) and I read the StackHighWaterMark, it sometimes goes to 0 depending on the code I have in the loop() function. Changing configMINIMAL_STACK_SIZE to a larger number has no effect on the StackHighWaterMark, even when it was originally nonzero.
How do I increase the stack space available to the idle process?

Here’s the code I used to test the stack space:

// This function calls itself recursively to a depth of n, and does a simple check
// to try to detect stack corruption.
int memtest(int n)
  Serial.print(n); Serial.print(' '); //%10);
  if (n%50 == 0) Serial.println("");
  int n1;
  if (n) {
    n1 = memtest(n-1);
    if (n1 != (n-1)) {
      Serial.print(" n1=");
      while(1) {};
  Serial.print(n); Serial.print(' ');
  if (n%50 == 49) Serial.println("");
  return n;

void loop(void)
#if USE_FreeRTOS
  UBaseType_t uxHighWaterMark;
  /* Inspect our own high water mark on entering the task. */
  uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
  Serial.print("HighWaterMark ");
  memtest(3);  // <----- this can be 99 no problem without FreeRTOS,
                // but with FreeRTOS, 8 causes HighWaterMark to go to 0

richard_damon wrote on Tuesday, March 13, 2018:

I would doubt that loop would be put in the Idle Task, as that would mean that it wouldn’t be allowed to block, which would be a major issue. The Arduino port likely creates a task specifically for the main processing loop, and you would need to look in the port code for Arduino to see wha size they use of it. Normlly I create my tasks with a size of configMINIMAL_STACK_SIZE+nnn where nnn is based on my estimated usage in the task, but if changing configMINIMAL_STACK_SIZE isn’t helping, the Arduino port looks like it isn’t using that method, or it would help.