AD

Thursday 25 June 2015

FreeRTOS on Arduino UNO using Eclipse IDE: Part 3


Previous: Part 2.

In the last part of this tutorial, we are going to create a simple application.

7. Creating a FreeRTOS application



We are going to create a FreeRTOS version of the same "Blink" sketch we used to test the board and our development environment. Everything we are going to do now is quite similar to what we have done in the section 4 of this tutorial.



7.1. Create a new C++ Project and select AVR Cross Target Application / Empty Project as project type. Press next.



Creating a project.

7.2. Deselect the Debug configuration. Press next.

We don't need the Debug configuration.

7.3. Select ATmega328p as MCU Type. Press finish.


Selecting the MCU type.

7.4. We need to add the folder /Source/include from the FreeRTOS static library project to the include directory path of our application project.
- Open the project properties and go to the section C/C++ General / Paths and Symbols.
- Click on Add...
- Fill the dialog according to the following pictures.

Adding FreeRTOS include folder to the include path.

Fill the dialog like this.

7.5. We also need to configure the linker so it find the symbols defined in the FreeRTOS library.
- Still in the project properties, go to the section C/C++ Build / Settings;
- Select AVR C++ Linker / Objects and add the object file as shown in the following pictures.

Adding FreeRTOS object file.

Fill the dialog like this.

7.6. Create a source file (e.g. main.cpp) and type (or copy) this code. The code has basically the same structure as the previous example.
- The main loop (responsible for blinking the LED) is in a separate function (not in main). This is used to create a task instead of being directly called;
- The function vTaskStartScheduler, called at the second line of the main function takes care of executing the tasks. The lines after that call are never executed;
- We need to create a callback named vApplicationStackOverflowHook, that will be executed in case of we run out of memory.


7.7. Build the project (press Ctrl+B). You will probably not find any problem.

7.8. Configure AVRDude. As we already created a programmer, everything we need to do is selecting it (we don't need to create it again).


7.9. Upload the software by pressing the "AVR" button on the toolbar. Sometimes Eclipse "forgets" the project on which you are working and complains that no AVR project is selected. If that happens, just select the project on Project Explorer panel and try it again.



Conclusion

After following this procedure, you will still have the same application you had at the beginning (using Arduino IDE). But you will have plenty of options to create multi-thread applications using threads, queues, semaphores, mutexes, etc. Your possibilities are limited only by your imagination (which I hope to be huge) and the available memory (which I know it is not that big).




FreeRTOS on Arduino UNO using Eclipse IDE: Part 2

Previous: Part 1

5. Create the FreeRTOS static library

We are going to create a static library containing all the FreeRTOS functions. After this is finished, we will link all the applications we create to this library. A lot of this process is about organizing the files provided in FreeRTOS package, and a little of it is really about codding.


5.1. Create a new C++ Project and select AVR Cross Target Static Library / Empty Project as project type. Press next.



Creating a project.

5.2 Deselect the Debug configuration. We don't need it as Arduino UNO board does not allow us to debug applications


We don't need Debug configuration.

5.3. Select ATmega328p as MCU Type. Press finish.

Selection the MCU type.

5.4. Download the file FreeRTOSV8.2.1.zip and extract it. After that, copy the folder Source into your project folder. As you can see in FreeRTOS documentation, the package contains various resources, such as example applications and guides, but all we need for now is the kernel source.


FreeRTOS package contents.


You will have a project like this:

FreeRTOS as an Eclipse project.

5.5. Remove all the portable layer folders in Source/portable, except for GCC and MemMag (I will explain the contents of these folders later). There are portable layers for many compilers, but we are going to keep only GCC.


Deleting unnecessary portable layers.

5.6. Remove also all the folders inside Source/portable/GCC, except for ATMega323. It is not the processor we are using (ATMega328p), but it is the most similar to it. So we are going to use as starting point.

ATMega323 portable layer.

5.7. Rename the folder ATMega323 to ATMega328p. It is not an essential step, but it will make your project more coherent.

ATMega 328p portable layer.

5.8. Remove all the files in Source/portable/MenMang, except for heap_2.c. As explained om FreeRTOS page, each of these files implement a different memory management model. We are going to stick with the model #2 because it enables the memory to be allocated and freed.


Memory management implementation file.

5.9. Open the project properties and go to the section Paths and Symbols. Click on Add...

Paths and Symbols section on project properties.

... and add Source/include to the include search path, as shown in picture below.

Adding include search path.

5.10. But there is still one missing header file. We will copy it from one of the example applications provided in FreeRTOS package. Copy the file FreeRTOSV8.2.1/FreeRTOS/Demo/AVR_ATMega323_IAR/FreeRTOSConfig.h to Source/include.

6. Adapt source files to ATmega328p

6.1. FreeRTOSConf.h

@line 73: Replace

#include <iom323.h>

for

#include <avr/io.h>

That is the generic header file for all AVR processors. It references other header files that are selected according to some processor being used (the right symbols must be defined for that to happen).


@line 90: Replace

#define configUSE_IDLE_HOOK 1

for

#define configUSE_IDLE_HOOK 0

This parameter enables us to define a callback function to be called when the system is in idle. In practice, it is a low priority task that is execute when there is no other task to be executed. It could be used to perform some background work or putting the processor in low power state. We don't want to use it now.

6.2. portable.h

@line 94: Replace

#include "portmacro.h"

for

#include "../portable/GCC/ATMega328p/portmacro.h"


6.3. port.c
This is probably the most important file as regards to porting FreeRTOS to a new processor.

@line 96: Replace

#define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ((uint8_t) 0x10)

for

#define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ((uint8_t) 0x02)


@lines 434 and 436: Replace TIMSK for TIMSK1.


@lines 447, 448, 460, and 461: Replace

SIG_OUTPUT_COMPARE1A

for

TIMER1_COMPA_vect



An that is it for the FreeRTOS static library. All we need to do not is creating some applications and link them to this library.


Next: Part 3

FreeRTOS on Arduino UNO using Eclipse IDE: Part 1


I have been playing with an Arduino UNO board during the last few days, and I wanted to see if it could do more than those "sketches". So I decided to try FreeRTOS on it. It turns out that I've got pretty good results!

I am going to show how to port FreeRTOS to an Arduino UNO board using Eclipse IDE. This tutorial is organized as follows:

Part 1:

  • Configure Eclipse as an Arduino UNO Integrated Development Environment (IDE);
  • Create, build and run a simple "Blink" application for an Arduino UNO board using Eclipse.
Part 2:
  • Create and build an Arduino UNO port of FreeRTOS as a static library;
  • Adapt source files to ATmega328p;
Part 3:
  • Create, build and run a FreeRTOS version of the "Blink" application.

By the way, all the source files are available here.


Arduino UNO clone I used in the tests.


Motivation

Why Arduino UNO
  • It is cheap;
  • It is open-source;
  • It is highly available;
  • It is easy to learn and use;
  • It does not require a separate piece of hardware in order to load new code;
  • It is popular among hobbyists and students.


Why FreeRTOS?

  • It enables us to implement multi-tasking (that is the main reason);
  • It supports both real-time tasks and co-routines;
  • It provides task notifications, queues, binary semaphores, and counting semaphores;
  • It also provides recursive semaphores and mutexes for communication and synchronization between tasks, or between real time tasks and interrupts;
  • It has a tiny footprint, which is good for boards with limited resources (such as Arduino UNO);
  • It has been designed to be small, simple and easy to use;
  • It is open-source (you can find the code here);
  • Royalty free.

Why Eclipse?
  • It is much richer in features than Arduino IDE;
  • It provides syntax-highlighting editor, incremental code compilation, a file/project manager, and interfaces to standard source control systems, such as SVN and Mercurial;
  • It has an extensive plugin library where you can find almost every feature you may need;
  • It enables us to work with many kinds of projects and programming languages so, if you already learned how to use it, you don't need to learn how to use another IDE.

Configuration

I am going to show how to make FreeRTOS work on an Arduino UNO board using Eclipse as IDE.

For this procedure I will use:
- Debian Jessie AMD64 as operating system;
- Eclipse Luna 4.4.2 64 bit as IDE;


1. Install the arduino package


The complete name of this package is AVR development board IDE and built-in libraries. You would not really need to install everything from this package, but it is the easiest way to get what we need: the built-in libraries. Just type:

$ sudo apt-get install arduino

This will install the Arduino IDE on you system. It is OK for simple projects, but it is quite limited in features and certainly does not fit our needs.


2. Test the board


Lets test the board and the installation of the Arduino libraries by running a simple test in the Arduino IDE.

2.1. Open Arduino IDE (it will be added to the laucher in the Development group). A dialog will probably be presented asking to add your user to the group dialout. That is necessary to access the board via serial line.

2.2. Open the “Blink” sketch example by accessing the menu File / Examples / 01.Basics / Blink.

2.3. Press the upload button (the second button on the tool bar). The software will be upload to the board and executed. You will see the onboard LED blinking. If it happens, everything is fine and you are ready to go on.

2.4. Things that may go wrong:

You may fail to access the board via serial line if you are not part of the dialout group. The Arduino IDE usualy verifies it and add your user to that group automatically. If that doesn't happen, you may need to add yourself to the group manually. In either case, you will need to logout and login again so the changes take effect.

You can verify the groups to which you belong by typing:

$ groups

You can add yourself to the dialout group by typing:

$ sudo usermod -a -G dialout username


3. Install and configure Eclipse IDE for C/C++ Developers


3.1. Download the Eclipse.

3.2. Extract it to the folder of your preference (let's say ~/deve/eclipse) and run it.

3.3. Install the AVR Eclipse Plugin. Access the menu Help / Marketplace, search for AVR, and click on “Install”. A next-next installation wizard will be presented. Just follow the presented instructions.


AVR Eclipse Plugin installation.



4. Test the Eclipse IDE


We are going the test out development environment by creating a simple application. This application will be an "Eclipse version" of the same "Blink" sketch we used to test the board before. You can follow the described steps or simply clone the project from my repository.

4.1. Create a new C++ Project and select AVR Cross Target Application / Empty Project as project type. Press next.


Creating a project.

4.2. Deselect the Debug configuration. We don't need it as Arduino UNO board does not allow us to debug applications (please let me know if I am wrong about that). Press next.

We don't need the Debug configuration.

4.3. Select ATmega328p as MCU Type. Press finish.


Selecting the MCU type.

4.4. Add the following source file to the project. You can type it yourself or get the file here.


Source file.


4.4. Build the project (press Ctrl+B). You will probably not find any problem.

4.5. Configure AVRDude. This step is necessary to tell the AVR Plugin how to access the board we are going to program.
- Open the project properties;
- Select AVR / AVRDude section;
- On "Programmer" tab, create a new programmer configuration by pressing the button "New";
- Programmer Hardware = Arduino
- Override default port = /dev/ttyACM0
- Don't you forget of selecting the created configuration on "Programmer" tab.


Creating a programmer configuration.

4.6. Upload the software by pressing the "AVR" button on the toolbar. Sometimes Eclipse "forgets" the project on which you are working and complains that no AVR project is selected. If that happens, just select the project on Project Explorer panel and try it again.


Well, now we have the tools we'll need to create our FreeRTOS static library and any FreeRTOS applications we want.

Next: Part 2.