How to run Freertos on Arduino UNO using eclipse IDE

Hi all, I’m trying to run FreeRTOS on Arduino Uno board using eclipse (mars version) IDE. I did setting up Eclipse for developing freeRTOS applications on Arduino. I installed AVR Eclipse Plugin &WinAVR-20100110.

I created C++ Empty project(AVR Cross Target Application) and Freertos Library (AVR Cross Target Static library)

Simple freertos led blink program I wrote and it compiled successfully.

code:

#include <Arduino_FreeRTOS.h>

// define two tasks for Blink & AnalogRead
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );

// the setup function runs once when you press reset or power the board
void setup() {
  
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
  }

  // Now set up two tasks to run independently.
  xTaskCreate(
    TaskBlink
    ,  "Blink"   // A name just for humans
    ,  128  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  2  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,  NULL );

  xTaskCreate(
    TaskAnalogRead
    ,  "AnalogRead"
    ,  128  // Stack size
    ,  NULL
    ,  1  // Priority
    ,  NULL );

  // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}

void loop()
{
  // Empty. Things are done in Tasks.
}

/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;


  // initialize digital LED_BUILTIN on pin 13 as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  for ( ;; ) // A Task shall never return or exit.
  {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
  }
}

void TaskAnalogRead(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
  

  for ( ;; )
  {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // print out the value you read:
    Serial.println(sensorValue);
    vTaskDelay(1);  // one tick delay (15ms) in between reads for stability
  }
}

Then I upload the code to the arduino uno board, but I’m not able to see any output on Arduino board.

Is there any issue on freertos code side?? or Freertos library??

Also I cannot do debug here, can anyone tell me how to debug this ??

I don’t know I summarize all the things here, if any more details needed for answering me this question please ask me.

Why is this posted in the Arm category? I’m going to move it to the kernel category.

I think the stack size of TaskAnalogRead is too small especially because probably stack hungry println is used. 128 (words) as stack very, very small, anyway.
Did you try to run just TaskBlink i.e. start experimenting with a simple setup and getting more complex step by step later on ?
Also you really should check the return code of xTaskCreate and other FreeRTOS API calls to know if they were successful or not.
Hopefully you also have a debugger attached and make use of configASSERT and stack checking facilities FreeRTOS provides.

@Ramya

AVR Eclipse hosts instructions for debugging with AVR Eclipse Plugin here. There’s a “Debugging with Eclipse” section.

I don’t see a call to vTaskStartScheduler anywhere, does the framework do this for you or is it at least called somewhere?

Need to verify whether the scheduler is running as expected and that the tasks are running. You can add some entry logs, before the infinite for loops of the tasks to verify whether the tasks are even running. Once you can debug you can instead use a breakpoint. If tasks are running as expected, then you can reduce debugging to the output functions.

To confirm, you’re not seeing output from console, from the logs, and no output from the LED either? TaskAnalogRead should be printing periodically, rather fast, but something should come out of the console.

Thanks for the timely reply. As you said I tried only TaskBlink also and eventhough I tried other FreeRTOS API calls, all of them are unsuccessful only.

Below is the code I tried to check the return code of xTaskCreate by making the (yellow colour)led glow if task creation happens. Compilation done good without any issue but on Arduino board side I didn’t see any progress(led is not glowing).

Code:

#include "Arduino.h"
#include "Arduino_FreeRTOS.h"

void TaskBlink1( void *pvParameters );

void setup()
{

	pinMode(7, OUTPUT);

	pinMode(8, OUTPUT);

	pinMode(9, OUTPUT);
}

// The loop function is called in an endless loop
void loop()
{
	BaseType_t sts;

	sts = xTaskCreate(

	    TaskBlink1

	    ,  "task1"

	    ,  128

	    ,  NULL

	    ,  1

	    ,  NULL );

	if(sts == pdPASS)
	{
		digitalWrite(7, HIGH); // yellow led

	}
	else
	{
		digitalWrite(8, HIGH);  // red led

	}
}

void TaskBlink1(void *pvParameters)
{
	TickType_t delayTime = *((TickType_t*)pvParameters); // Use task parameters to define delay
	 for ( ;; )
	  {
		digitalWrite(9, HIGH);
		 vTaskDelay(delayTime / portTICK_PERIOD_MS);
		 digitalWrite(9, LOW);
		 vTaskDelay(delayTime / portTICK_PERIOD_MS);
		   }
}

You should enclose code blocks with 3 tildes ‘~’ or backticks ‘`’ for better readability.
Well, did you have a look at the API documentation ?

You see that there is simply not enough heap available to allocate the required memory (for task stack and internally used task control block TCB).
So you should verify that you provide enough heap for your application.

@Ramya

Which FreeRTOS port and FreeRTOS version are you using?
What content is in your FreeRTOSConfig.h

Compilation done good without any issue but on Arduino board side I didn’t see any progress(led is not glowing).

So no LED output? Is loop() even running? With new form, shouldn’t it have set at least 1 LED – assuming digitalWrite call is working. If it is, and if xTaskCreate is failing then it should return errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY as mentioned.

Can you add logs, or debug traces? There’s not enough information to deduce exactly what the problem is.

Thank you for the link which provides debug info, I’ll lookin to it. Initially I did tried with vTaskStartScheduler call, that also had similar issue. I tested this code on Arduino IDE, both the two tasks are running independently without any issue but comes to eclipse it is not.

Can you please tell me how should I check console output of “.elf” type binary !?? Like serial monitor on Arduino IDE, how to check here.

image

To confirm tasks are running or not, I tried connecting LED and checked but that not happens, also I tried to use some LCD print statement to check whether the tasks are running or not, but LCD also not display anything.

But without any FreeRTOS calls simply I tried checking only LCD printing something(“hello world”) and blinking LED are working successfully.

So this is where I got stuck, without FreeRTOS calls LED blinking happening on Arduino uno but with FreeRTOS LED blink is not happens.

I’m not familiar with Arduino, but creating tasks in an endless loop seems wrong.
Isn’t there a simple example or demo of an FreeRTOS application you could use like this one ?
There in setup the tasks are created and finally vTaskStartScheduler is called to start FreeRTOS and loop() is just empty.

Thanks a lot. I’m using FreeRTOS Kernel V10.0.0 version library. As per your suggestion I created task inside ‘setup’ and make loop() as empty. For testing purpose here I’m creating only one task so I’ll not use vTaskStartScheduler as of now.

code:

BaseType_t sts;

sts = xTaskCreate(

    TaskBlink1

    ,  "task1"

    ,  128

    ,  NULL

    ,  1

    ,  NULL );

if(sts == pdPASS)
{
    digitalWrite(7, HIGH); // yellow led
    delay(1000);
    digitalWrite(7, LOW);
}
else
{
	digitalWrite(8, HIGH);  // red led

}

Now 7th PIN LED is glowing, so that I comes know task creation is happening. But same way inside the task entry function whatever I given is not happening, means from TaskBlink1() function I say LED PIN(9) has to be blink, but that is not happen. Also if I use vTaskDelay(), it’s not happening only delay() is working.

Even with just 1 task you have to start the scheduler. Otherwise no task at all will run. Just follow the example taking into count the hints given before.

Hi, I tried using vTaskStartScheduler call after task creation inside ‘setup’ and tested still I had same result, also I tested by increasing/decreasing the stack size but the results are same, means ‘task creation is happening but from TaskBlink1() entry function nothing works’.

I installed Arduino_FreeRTOS into the stock Arduino IDE and opened the blink_analogRead example. This example seems to be the one shown in the first post.

I built this demo for the MEGA2560 (happened to be the arduino I had handy) and it is blinking (blink task) and printing the serial messages (analog task).

Traditional Arduino is built on the AVR but the build options are not necessarily what you will get with the default winavr install in eclipse. I suggest you do one of the following:

  1. switch to the default arduino IDE.
  2. Use VSCode+PlatformIO with the arduino options.
  3. Analyze how the arduino IDE uses the AVR tools to build a project and duplicate those in your eclipse system.

The library and default example work correctly.