FreeRTOS hang issue

Hi There,
I have two task. task2 works fine, but when task1 trigger then CPU got hang.
How to fix it?

CPU: Arduino UNO (ATmega328P)
Code is following:

#include <Arduino_FreeRTOS.h>


/*******************xTask for ****************************/
void scanButton(void *pvParameters)  
{
  int  itr=0;
  bool readBtnStatus;
  
  pinMode(11, INPUT);
  pinMode(0,  OUTPUT);

  while(1)
  {
    readBtnStatus = digitalRead(11);   
    vTaskDelay( 100);

    if(readBtnStatus)
    {
      
      if(itr >= 2)
      {
        digitalWrite(0, HIGH);
        itr = 0;
      }

      else
      {
        digitalWrite(0, LOW);
        itr++;
      }
      
    }
 
    //vTaskDelay(10);
     
  }

}


/*******************xTask for ****************************/
void controlLoad(void *pvParameters)  
{
  bool lowswitch  = 0;
  bool highswitch = 0;
  bool passswitch = 0;
  
  pinMode(1,INPUT); //low switch
  pinMode(2,INPUT); //pass switch
  pinMode(4,INPUT); //high switch

  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  

  while(1)
  {
    lowswitch  = digitalRead(1);
    passswitch = digitalRead(2);
    highswitch = digitalRead(4);

    if (lowswitch == 1 && highswitch == 0 && passswitch == 0)
    {
      digitalWrite(10,LOW);
      digitalWrite(9,HIGH);
    }

    else if (lowswitch == 0 && highswitch == 1 && passswitch == 0)
    {
      digitalWrite(10,HIGH);
      digitalWrite(9,LOW);
    }

    else if (lowswitch == 0 && highswitch == 0 && passswitch == 1)
    {
      digitalWrite(10,HIGH);
      vTaskDelay( 150 );
      digitalWrite(10,LOW);
      vTaskDelay( 150 );
      digitalWrite(9,LOW);
    }

    else if (lowswitch == 1 && highswitch == 0 && passswitch == 1)
    {
      digitalWrite(9,LOW);
      digitalWrite(10,HIGH);
      vTaskDelay( 150 );
      digitalWrite(10,LOW);
      digitalWrite(9,HIGH);
      vTaskDelay( 150 );
    }

    else if (lowswitch == 0 && highswitch == 1 && passswitch == 1)
    {
      
      digitalWrite(10,LOW);
      digitalWrite(9,HIGH);
      vTaskDelay( 150 );
      digitalWrite(9,LOW);
      digitalWrite(10,HIGH);
      vTaskDelay( 150 );
    }

    else if (lowswitch == 1 && highswitch == 1)
    {
      digitalWrite(10,HIGH);
      digitalWrite(9,HIGH);
    }

    
    else
    {
      digitalWrite(10,LOW);
      digitalWrite(9,LOW);
    }
    
    vTaskDelay(10);
  }

}


/******************* Setup ****************************/
void setup()
{
  Serial.begin(9600);

  xTaskCreate(scanButton,  "task1", 512,  NULL, 2,  NULL );
  xTaskCreate(controlLoad, "task2", 512,  NULL, 1,  NULL );
}


void loop()
{

}

Did you try stepping through your code with a debugger to know where exactly task 1 (scanButton) hangs ?
As some quick fixes from a FreeRTOS point of view you could probably try:-

  • Increasing the stack size of your tasks, can you check if this works ? Also, you can check how close you came to stack overflow by this FreeRTOS API, as well as can check FreeRTOS stack usage and stack overflow checking
  • You might want to define configASSERT for development.

Just curious, are you sure you want to keep this commented in the scanButton task?

In addition to what @rohitmdn suggested , you need to start the scheduler as well in the setup function.

void setup()
{
  Serial.begin(9600);

  xTaskCreate(scanButton,  "task1", 512,  NULL, 2,  NULL );
  xTaskCreate(controlLoad, "task2", 512,  NULL, 1,  NULL );

  vTaskStartScheduler();
}

Is it a copy/paste error here?

1 Like

You do not need to call vTaskStartScheduler explicitly as Arduino setup code does that - Arduino_FreeRTOS_Library/src/variantHooks.cpp at master · feilipu/Arduino_FreeRTOS_Library · GitHub.

Here are few more suggestions -

  1. Check the return value of xTaskCreate to ensure that tasks are successfully created.
  2. Just blink 2 different LEDs in the 2 tasks and see if you can get that to work.
  3. Once that works, start adding your code to narrow down which part of your code causes the application to hang.
1 Like

I have tried 1 to 3. Task creation is fine.
Can’t find any issue when two task run separately.

Is there any suggestion for me?

You can try reducing the stack size to 128 for the tasks as the ATmega328P only has 2kB of RAM. Did you enable stack overflow checking as mentioned by @rohitmdn to see that stack is sufficient even after the tasks are created?