I’ve created a Sourceforge project as a place to host all my current tools and working environment. The Sourceforge site is now 4 years old, and there’s a GitHub site too, which is now the most up to date repository
Preferred: Github freeRTOS & libraries for AVR ATMEGA
Secondary: Sourceforge freeRTOS & libraries for AVR ATMEGA
The Sourceforge repository has become so complex, with so many libraries, I thought that it was about time to make a simple version, which has the minimum implementation to get started. No additional libraries included. One timer option, using the watchdog timer. One heap option, using avr-libc malloc. One example application, just a blink with two tasks, for Uno, Mega, and Goldilocks boards.
The thing about open source. Sometime you have to give back.
Things I’m really happy about:
- Arduino Uno family ATmega328p, Freetronics EtherMega (Arduino Mega2560), and Goldilocks ATmega1284p, scheduling and IO works.
- Being able to use any Timer on the AVR as the system Tick. In practice this means Timer0 on 328p (Arduino Uno), Timer3 on 2560 (Arduino Mega) and 1284p (Pololu SVP) and Timer2 on 1284p with 32.768kHz watch crystal (Freetronics Goldilocks). The watchdog timer has also been implemented, and if there is no critical need for accurate timing, this is the lowest resource impact system tick.
- Converting all of the relevant libraries to be friendly to a RTOS system. No delay busy-wait loops etc. Everything defers to (is interruptible by) the scheduler when waiting, or is driven from interrupts.
- Having many finished projects, that are good demonstrations of lots of AVR and freeRTOS capabilities.
- Having the Sparkfun LCD Shield working properly, with printf string formatting.
- Having the Rugged Circuits QuadRAM 512kByte and MegaRAM 128kByte RAM extensions working on ATmega2560.
- Porting ChaN FatF microSD card support for a variety of uSD shield cages.
- Porting Wiznet W5100, W5200, and W5500 drivers for Arduino Ethernet shields.
- Porting Wiznet and uIP DHCP and HTTP applications, creating options for implementing a basic web server.
- Properly implementing semaphores for access to resources (ports, interfaces, ADC, LCD).
- Properly implementing queues for transferring data between tasks (threads).
The repository of files on Sourceforge freeRTOS & libraries for AVR ATMEGA is a working collection for a freeRTOS based platform using the AVR-GCC and AVRDUDE platform. The development environment used was Eclipse IDE.
With the Eclipse IDE the C Development Environment (CDE), and the AVR plug-in are both needed. It is assumed that the AVR avr-libc libraries are installed.
The freeRTOS folder contains the most recent version 8.2.3 of freeRTOS, but it has been abridged down to only those files relevant for AVR GCC. The port.c file has been extensively modified to allow the use of either of the 328p Timer0 or Timer1 timers. And, the use of Timer3 on the Pololu SVP which has uses a 1284p. Timer 3 for Arduino Mega using a 2560 also works. Timer2 support has been added for the Freetronics Goldilocks and its 32,768kHz crystal. A Real Time system_tick is added using time.h functionality added to the system libraries described below.
The freeRTOSBoardDefs.h file contains most of the variables that you’ll need to change regularly.
There are some relevant and often used libraries added to the basic freeRTOS capabilities.
- lib_io: contains often used I/O digital and ADC routines borrowed from Pololu.
- lib_io: contains the tools to use the TWI (non-trademarked I2C) bus. It contains integrated interrupt driven master and slave routines
- lib_io: contains the tools to use the SPI bus.
- lib_io: contains routines to drive the serial interface. there are three versions; avrSerial for use before the freeRTOS scheduler has been enabled, and xSerial for use during normal operations. xSerial is interrupt driven and uses an optimised ring buffer. xSerialN is further generalised to allow multiple simultaneous serial ports.
- lib_ext_ram: contains routines to drive the Rugged Circuits QuadRam on Arduino Mega2560, or Freetronics EtherMega.
- lib_util: Optimised CRC calculations.
- lib_util: Extended alpha (string) to integer (binary, octal, decimal, hexdecimal) conversion.
- lib_time: Real time calculations, from avr-libc upstream, providing esoteric time and date calculations.
- lib_rtc: drivers for the DS1307 RTC using I2C.
- lib_fatf: contains ChaN’s FatF FAT32 libraries for driving the microSD card.
- lib_iinchip: contains the W5100 drivers and the W5200 drivers from Wiznet.
- lib_inet: contains a DHCP, and HTTP implementation.
- lib-uIP: contains the uIP implementation derived from Contiki2.7, implemented on MACRAW mode of W5100/W5200, and extensible.
- lib_ft800: contains optimised drivers for the Gameduino2, a FTDI FT800 implementation, with LCD and touch screen support.
Some more recent posts are here:
Goldilocks Analogue Synthesiser
Goldilocks Analogue Prototyping 4
Melding freeRTOS with ChaN’s FatF & HD44780 LCD on Freetronics EtherMega
Rugged Circuits QuadRAM on Freetronics EtherMega
Quick review of Freetronics EtherMega
Description of the AVR Pong multi-processor game.
Additional steps to use the Mega2560
EtherMega (Arduino Mega2560) and FreeRTOS
Step-by-step Instructions
Our Destination:
On completing these instructions you should have an Eclipse IDE (Integrated Development Environment) installed with all relevant libraries installed, to use the freeRTOS, and the libraries I’ve modified, to build projects (Eclipse term for a set of code) of your own.
We’re Assuming:
These instructions are based on an Ubuntu LTS install, but the path to the destination is not complex, and can be roughly followed for any installation platform.
Step 0. As usual on an Ubuntu (Debian) system, refresh the software sources.
sudo apt-get update
Step 1. Install the AVR Libraries.
Step 2. Install the Arduino environment.
Doesn’t hurt to have the Arduino environment available. It can be used for programming boot-loaders (using AVR-ISP code), and generally for checking health of equipment, using known good example code.
This will pull in some extra libraries that the Arduino platform needs.
Step 3. Install the Eclipse IDE.
It is not necessary to use or install an IDE to develop with freeRTOS, or with any other system. It is easy to use makefiles and the command line with avr-gcc and avrdude. In fact, I didn’t use Eclipse for a long time. And, when I first started to use it, it felt very unnatural and clumsy.
However, now I’ve been using it for some time I highly recommend it, for the ability to see deeper into the code (definitions are detailed on mouse over), and to compare (live differences) and roll-back code to any step of your editing process.
Again, installation is easy with Ubuntu (Debian), but it can take a while. Lots of things get installed along with it.
sudo aptitude install eclipse
Step 4. Select the C & C++ development tools within Eclipse.
Eclipse is a Java based platform, but it works just as well with C, and C++, as it does with a wide variety of languages. Getting the C Development Tools (CDT) is the first step to a C environment that we’ll be using.
Open Eclipse, and lock it to your launcher. You’ll be using it frequently.
Using the Menus, click:
Help>>Install New Software…>>Add…
CDT Indigo http://download.eclipse.org/tools/cdt/releases/indigo
Select only “CDT Main Features”, and install these plugin development tools.
Step 5. Select the AVR development environment within Eclipse.
The AVR environment includes direct access to the avrdude downloading tool for one-click programming of your AVR devices.
Using the Menus, click:
Help>>Install New Software…>>Add…
AVR Plugin http://avr-eclipse.sourceforge.net/updatesite/
Select “CDT Optional Features”, and install these plugin development tools.
Step 5c. Select C/C++ Perspective
First you need to select the right perspective, being C/C++. Top right there is a button showing “Java”. Just to the left is a button (like a window) for selecting perspective. Select
Other…>>C/C++
When that is finished, you should have Eclipse menu button containing a AVR* with a green down arrow. That is the button used to program the device.
Step 6. Define a freeRTOS static library project.
There are lots of short cuts, and alternative ways to achieve things using context sensitive menus in Eclipse. I’ll concentrate on the top menu bar options, though you can get most things from a context menu click in the right window.
File>>New>>C Project: AVR Cross Target Static Library: Empty Project
A static library project is never run by itself. It is always linked to by other projects, called AVR Cross Target Applications.
Give the project a name (perhaps freeRTOS82x).
Now a project will apear in the “Project Explorer” window. Select it. We are going to set some options relating to this project.
Project>>Build Configurations>>Set Active>>Release
Project>>Properties
AVR:Target Hardware: MCU Type: ATmega328p (or other depending on hardware)
AVR:Target Hardware: MCU Clock Frequency: 16000000 (for Arduino hardware or other depending on your hardware)
C/C++ Build: Configuration: [All Configurations] (make sure this is set for all following configurations)
C/C++ Build: Environment: AVRTARGETFCPU: 16000000
C/C++ Build: Environment: AVRTARGETMCU: atmega328p
C/C++ Build: Settings: AVR Compiler: Optimisation: Other Optimisation Flags: -ffunction-sections -fdata-sections -mcall-prologues -mrelax (and use -Os or -O2)
Now we are going to add just the freeRTOS files, from the subdirectory within the freeRTOS82x_All_Files.zip file that you have downloaded from Sourceforge, and extracted somewhere sensible.
File>>Import…>>General:File System
Select the “into folder” as the project name you just created, and “Select All” for the import on the freeRTOS subdirectory. That should import the entire freeRTOS system. Spend some time browsing, if you like.
NOTE. Do NOT import the entire contents of the freeRTOS82x_All_Files.zip file. At this stage just import contents of the freeRTOS subdirectory.
Now we define the include library for the build. Remember to select [All Configurations] first.
Project>>Properties>>C/C++ Build>>Settings: AVR Compiler: Directories
Add the from the “Workspace…”: freeRTOS82x/include
“${workspace_loc:/${ProjName}/include}”
Now there are fouralternative memory management routines, explained in the freeRTOS documentation. We are going to use the heap_2.c version, so we need to exclude the other three files from the build. In the project explorer RIGHT CLICK (context menu) each one then exclude them.
./MemMang/heap_1.c
./MemMang/heap_3.c
./MemMang/heap_4.c
Resource Configurations>>Exclude from Build…: Select All
Following this step, it should be possible to compile the library.
Project>>Build All
If there are any ERRORS, then go back and check the configurations for the project. Sometimes they may be changed, forgotten, or otherwise different from what you expected.
There will be some WARNINGS, relating to the usage of different Timers. I added these warnings to keep these things front of mind, as depending on which hardware I’m using the ./include/FreeRTOSBoardDefs.h file needs to be managed to suit.
Step 7. Define an Application Project.
An Application will generate the final hex code that you upload to the AVR with avrdude. This final code is created from the freeRTOS static library code generated above, together with code contained in the avr-libc, and any other linked projects.
We are going to import the UnoBlink or MegaBlink project as it makes a good example. Without a display, or real-time-clock module, it will only flash a LED. But, least we know it is alive.
To get started create a new project as below.
Give the project a name (perhaps MegaBlink or retrograde).
Now a project will appear in the “Project Explorer” window. Select it. We are going to set some options relating to this project.
Project>>Build Configurations>>Set Active>>Release
Project>>Properties
AVR:AVRDUDE:Programmer:New…
Configuration name: Arduino or Freetronics 2010
Programmer Hardware: Atmel STK500 Version 1.x firmware
Override default port: /dev/ttyUSB0 (FTDI USB) OR /dev/ttyACM0 (AVR USB)
Override default baudrate: as or if required.
AVR:Target Hardware: MCU Type: ATmega328p (or other depending on hardware)
AVR:Target Hardware: MCU Clock Frequency: 16000000 (or other depending on hardware)
C/C++ Build: Configuration: [All Configurations] (make sure this is set for all following configurations)
C/C++ Build: Environment: AVRTARGETFCPU: 16000000
C/C++ Build: Environment: AVRTARGETMCU: atmega328p
C/C++ Build: Settings: AVR Compiler: Directories: “${workspace_loc:/freeRTOS82x/include}”
C/C++ Build: Settings: AVR Compiler: Optimisation: Other Optimisation Flags: -mcall-prologues -mrelax (and use -Os or -O2)
C/C++ Build: Settings: AVR C Linker: General: Other Arguments -Wl,–gc-sections
C/C++ Build: Settings: AVR C Linker: Libraries: Add “m” without quotes. m is the standard math library, which should be included in most projects.
C/C++ Build: Settings: AVR C Linker: Objects: Other Objects Here you need to add the compiled freeRTOS library. And this is the only place where the Debug and Release builds are different.
With Release Build selected, paste “${workspace_loc:/freeRTOS82x/Release/libfreeRTOS82x.a}”
With Debug Build selected, paste “${workspace_loc:/freeRTOS82x/Debug/libfreeRTOS82x.a}”
Or select the Workspace option to navigate to the actual assembler files to be linked into the project.
Project References: freeRTOS82x ticked.
Now we are going to add the MegaBlink (or retrograde) files, from the MegaBlink.zip (or retrograde.zip) file that you have downloaded from sourceforge, and extracted somewhere sensible. If you downloaded the freeRTOSxxx_All_Files.zip, you have all the sources.
File>>Import…>>General:File System
Select the “into folder” as the project name you just created, and “Select All” for the import. That should import the 2 files shown inro the project file system. Spend some time browsing, if you like.
Following this step, it should be possible to compile and link the project.
Project>>Build All
If this step completes successfully, with no additional ERRORS, then the final step is to upload the new application into your Arduino or Freetronics device.
Make sure that you have your device plugged into the USB port, then simply hit the AVR* button in the row of buttons. You will see some green text showing the status of the upload, finishing with the words
avrdude done. Thank you.
Now, you should have a flashing LED.
Now you can import any additional projects, in the same way.
Step 8. Things to watch.
Turn on the serial port by removing the comments around the serial port definitions, and watch to see aspects of the program in action.
Expect to manage the amount of heap allocated in the ./include/FreeRTOSBoardDefs.h file, to ensure that the total SRAM utilised (as noted in the final linker stage when using heap_1.c, heap_2.c or heap_4.c) remains less than 100% or for ATmega328p 2048 bytes.
Expect to manage the amount of stack space allocated to each task during the set up, to ensure you’re not wasting space, nor (worse) you’re over writing another task’s stack.
For the Arduino Uno, keep the total number of tasks to below 4, otherwise too much SRAM is consumed in stack allocations.