Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training

Atmel AVR (MegaAVR) / WinAVR Port
[RTOS Ports]


There are currently two RTOS ports for the AVR - one using the IAR Embedded WorkbenchTM development tools, and one using WinAVR (GCC). This page provides information on the WinAVR port only.

The AVR WinAVR demo application is configured to run on an Atmel STK500 prototyping board using an AVR ATMega323 embedded processor running at 8MHz (instructions are provided should you wish to use an alternative development board). If an ATMega32 is used the frequency can be increased to 16MHz. The port is also being used with ATMega128 processors.

The 2KBytes of RAM on the ATMega323 is enough to run 10 real time tasks - including the idle task.

From V4.1.0 the AVR demo application demonstrates the use of co-routines.

IMPORTANT! Notes On Using The AVR/WinAVR RTOS Port:

Please read all the following points before using this port.

  1. Source Code Organization
  2. The Demo Application
  3. Configuration and Usage Details
See also the FAQ My application does not run, what could be wrong?

Source Code Organization

The FreeRTOS download contains the source code for all the FreeRTOS ports.

See the Source Code Organization section for a description of the downloaded files and information on creating a new project.

The AVR WinAVR demo application makefile is located in the Demo/AVR_ATMega323_WinAVR directory.

The Demo Application

The FreeRTOS source code download includes a fully preemptive multitasking demo application for the Mega AVR GCC RTOS port.

Demo application hardware setup

The demo application includes tasks that send and receive characters over the serial port. The characters sent by one task need to be received by another - if any character is missed or received out of sequence an error condition is flagged. A loopback connector is required on the serial port for this mechanism to operate (simply connect pins 2 and 3 together on the serial port connector).

For best effect LEDs should be connected to the eight data lines on the standard PC parallel port. Omitting these LEDs will not cause the RTOS demo application to fail, but will remove some visual feedback that everything is working as expected.

The following links must be in place on the STK500 prototyping board for the demo application to operate - these can be seen on the photograph above:

  1. PORTB to LEDS
  2. PORTD bits 0 and 1 to RS
  3. SPROG3 to ISP6PIN (correct link to program an AVR ATMega323

The demo application includes tasks that send and receive characters over the serial port. The characters sent by one task need to be received by another - if any character is missed or received out of sequence an error condition is flagged. A loopback connector is required on the serial port for this mechanism to operate (simply connect pins 2 and 3 together on the serial port connector).

Building the RTOS demo application

FreeRTOS V3.0.0 has replaced the batch files previously used to build the demo application with a single makefile. This was made possible by improvements in ELF and COFF format support in the later versions of WinAVR.

To build the demo application:

  1. Ensure that WinAVR is correctly installed and accessible from your PATH environment.

  2. Open a command prompt and navigate to the Demo/AVR_ATMega323_WinAVR directory.

  3. Type 'make' to build the project. The project should build with no errors or warnings.

  4. To force a complete rebuild type 'make clean'

  5. The optimisation level (option -O) and debug level (option -g) can be adjusted within the makefile to suit your application requirements.
The build process will generate a file called RTOSDemo.elf which is suitable for executing and debugging within the free Atmel AVR Studio IDE, and a file called RTOSDemo.hex which is suitable for burning into the processor.

Ensure the -g option is used within the makefile when generating files for debug and simulation.


The RTOS demo application creates 10 of the standard demo tasks.
  1. LEDs 0 to 2 are under control of the standard 'flash' co-routines and flash at a regular rate. Each LED is flashed by a separate task.

  2. LEDs 4 and 5 are under control of the standard 'comtest' tasks. LED 4 toggles every time an RS232 character is transmitted. LED 5 toggles every time an RS232 character is received and verified.

  3. Not all the tasks update an LED so have no visible indication that they are operating correctly. Therefore a 'Check' task is created whose job it is to ensure that no errors have been detected in any of the other tasks.

    LED 7 is under control of the 'check' task. LED 7 will flash every few seconds provided no errors have been detected in any of the other real time tasks. If an error is detected in any other task then LED 7 will stop flashing.

See the standard demo application section for full details of the demo application tasks.

Configuration and Usage Details

RTOS port specific configuration

Configuration items specific to this port are contained in Demo/AVR_ATMega323_WinAVR/FreeRTOSConfig.h. The constants defined in this file can be edited to suit your application. In particular - the definition configTICK_RATE_HZ is used to set the frequency of the RTOS tick. The supplied value of 1000Hz is useful for testing the RTOS kernel functionality but is faster than most applications require. Lowering this value will improve efficiency.

Each port #defines 'BaseType_t' to equal the most efficient data type for that processor. This port defines BaseType_t to be of type char.

To use a microcontroller other than an AVR ATMega323

  1. Change the MCU definition at the top of the makefile.

  2. Set the correct clock frequency in Demo/AVR_ATMega323_WinAVR/FreeRTOSConfig.h

  3. Ensure the configTOTAL_HEAP_SIZE definition is set to fit within the available RAM.

  4. The tick ISR is generated from a compare match on timer 1. Timer configuration is not identical across all AVR devices. Check the function prvSetupTimerInterrupt() in Source/portable/GCC/ATMega323/port.c to see if any modifications are required for your chosen device.

Whichever part is used, ensure the MCU fuses are blown to provide the correct clock frequency (this can be done from the AVR Studio development tools).

Switching between the pre-emptive and co-operative RTOS kernels

Set the definition configUSE_PREEMPTION within Demo/AVR_ATMega323_WinAVR/FreeRTOSConfig.h to 1 to use pre-emption or 0 to use co-operative.

Memory management

Source/Portable/MemMang/heap_1.c is included in the MegaAVR demo application makefile to provide the memory allocation required by the RTOS kernel. Please refer to the Memory Management section of the API documentation for full information.

Development tool options

As with all the ports, it is essential that the correct compiler options are used. The best way to ensure this is to base your application on the provided demo application makefile.

RTOS Demo application serial port driver

The serial port driver included with the demo application uses the calculation detailed in the AVR ATMega323 manual to set the baud rate registers. At some baud rates I have found it necessary to adjust the calculated setting slightly. I suspect this is due to an inaccuracy in the 8MHz crystal installed in my prototyping board and rounding errors in the baud calculation.

It should also be noted that the serial drivers are written to test some of the RTOS kernel features - and they are not intended to represent an optimised solution.

Notes for Linux users

I have only tested the makefile from a Win2K host but hopefully it is also compatible with Linux builds of GCC.

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists