Low Power RTOS Demo - Atmel SAM4L
Using the free Atmel Studio 6 IDE, GCC, and the Atmel Software Framework
The application documented on this page demonstrates how the FreeRTOS
features can be used to minimise the power consumption of an
application running on a SAM4L ARM Cortex-M4 microcontroller
from Atmel. The SAM4L is
designed specifically for use in applications that require extremely low power
The demo uses the FreeRTOS GCC ARM Cortex-M3/4 port, the free
Atmel Studio 6 IDE, and components
of the comprehensive Atmel Software Framework
(ASF). The project is pre-configured to run on the SAM4L-EK evaluation kit.
IMPORTANT! Notes on using the FreeRTOS SAM4L demo project
Please read all the following points before using this RTOS port.
See also the FAQ My application does not run, what could be wrong?
- Source Code Organisation
- The Demo Application
- RTOS Configuration and Usage Details
Source Code Organisation
The FreeRTOS zip file contains the source files for all the FreeRTOS
ports, and all the demo applications, only a few of which are needed by this
See the Source Code Organization
section for a description of the downloaded files and information on creating a
The Atmel Studio 6 Solution file for the ATSAM4L demo application is called
FreeRTOS_Demo.atsln, and is located in the FreeRTOS/Demo/CORTEX_M4_ATSAM4L_Atmel_Studio
The Atmel ARM Cortex-M4 Demo Application
Hardware set up
The demo uses the LEDs built onto the SAM4L-EK and no hardware setup is required.
configCREATE_LOW_POWER_DEMO is a constant that is defined at the top of
FreeRTOSConfig.h. The low power demo is created when the project is built with
configCREATE_LOW_POWER_DEMO set to 1. A standard kernel only demo (without tick
suppression) is created when the project is built with configCREATE_LOW_POWER_DEMO
set to 0.
Functionality with configCREATE_LOW_POWER_DEMO set to 1
When configCREATE_LOW_POWER_DEMO is set to 1 main() calls main_low_power().
main_low_power() creates a very simple demo as follows:
Two tasks are created, an Rx task and a Tx task.
The Rx task blocks on a queue to wait for data, blipping an LED each time
data is received (turning it on and then off again) before returning to
block on the queue once more. The LED is only blipped on briefly so it
does not effect the current reading too much.
The Tx task repeatedly enters the Blocked state for 500ms. On exiting
the blocked state the Tx task sends a value through the queue to the Rx
task (causing the Rx task to exit the blocked state and blip the LED).
Observed behaviour when configCREATE_LOW_POWER_DEMO is set to 1
For optimum low power results the SAM4L-EK must be connected and powered using
only the JTAG USB connector, but the debugger must not be connected (the
application must be executed 'stand alone').
The MCU spends most of its time in the Retention low power state (see the implementation
section below), during
which times the current reading displayed on the board monitor that
is built onto the SAM4L-EK will show a low
Every 500ms the MCU will come out of the low power state to turn the LED on,
then return to the low power state for 20ms, before leaving the low power
state again to turn the LED off. This will be observed as a rapid blip on the LED
every 500ms, and two very brief dots appearing on the graph of current consumption
that is displayed by the board monitor
that is built onto the SAM4L-EK. The two dots will often be observed as a
single dot due to their speed.
Current readings as shown by the board monitor display when the
tickless low power application is executing
RTOS implementation when configCREATE_LOW_POWER_DEMO is set to 1
The SAM4L microcontroller has several different low power modes. The low power
mode that consumes the least energy without loosing the contents of the static
RAM and the CPU registers is called Retention mode. The RTOS turns off the tick
interrupt and places the SAM4L into its Retention low power state whenever both
tasks are in the Blocked state (when only the Idle task is able to execute).
Turning the tick interrupt off allows the microcontroller to remain in Retention
mode until it is time for a task to leave the Blocked state and execute. If the
RTOS tick interrupt was not suppressed in this way the microcontroller would have to
exit Retention mode each time a tick interrupt occurred.
The standard RTOS Cortex-M port uses the Cortex-M SysTick timer to generate
the RTOS tick interrupt, but the SysTick timer is halted when the SAM4L enters
retention mode so it cannot be used in this demo. Instead the RTOS tick is
generated by the Asynchronous Timer (AST). The AST continues
running in Retention mode, and its full 32-bit width and slow clocking allows the
RTOS tick to be suppressed for vastly longer periods than possible when using
Functionality with configCREATE_LOW_POWER_DEMO set to 0
When configCREATE_LOW_POWER_DEMO is set to 0 main()
calls main_full(). main_full() creates a comprehensive test and demo application
The created tasks are from the set of standard demo
tasks. Standard demo tasks are used by all FreeRTOS port demo applications.
They have no specific functionality, and are created just to demonstrate how to use the FreeRTOS API,
and test the RTOS port.
A 'check' software timer is created that periodically inspects the standard
demo tasks to ensure all the tasks are functioning
as expected. The check software timer's
callback function toggles an LED on the SAM4L-EK hardware.
This gives a visual feedback of the
system health. If an LED is toggling every 3 seconds, then the
check software timer has not discovered any problems. If the LED is
toggling every 200 milliseconds, then the check software timer has
discovered a problem in one or more tasks.
Building and executing the demo application
from within the Atmel Studio IDE.
Open FreeRTOSConfig.h, and set configCREATE_LOW_POWER_DEMO to generate either
the tickless low power demo, or the full test and demo application, as
Ensure the target hardware is connected to the host computer through the
J-Link USB connector built onto the SAM4L-EK board.
Select 'Build Solution' from the IDE's 'Build' menu, the
RTOSDemo project should build without any errors or warnings.
After the build completes, select "Start Debug and Break" from the IDE's Debug
menu to program the SAM4L microcontroller flash memory, start a debug
session, and have the debugger break on entry into the main() function.
Two things can prevent the download:
If the SAM4L-EK has not been connected before the J-Link driver
might open a window to ask if you want to update the J-Link
firmware - but the window (at least at the time of writing) will
appear underneath the Atmel Studio 6 window.
The J-Link will default to using JTAG mode, but the SAM4L-EK
requires SWD mode as shown below:
Ensure to use SWD mode
Ensure the debugger is stopped (completely stopped, not just paused) and
that the application is free running before taking current readings.
ARM Cortex-M4 FreeRTOS port specific configuration
Configuration items specific to this demo are contained in FreeRTOS/Demo/CORTEX_M4_ATSAM4L_Atmel_Studio/src/FreeRTOSConfig.h.
The constants defined in this file can be edited to suit your application. In particular -
This sets the frequency of the RTOS tick interrupt. The setting used by
this demo depends on the configCREATE_LOW_POWER_DEMO setting.
configKERNEL_INTERRUPT_PRIORITY and configMAX_SYSCALL_INTERRUPT_PRIORITY
See the RTOS kernel configuration documentation for full information on these configuration constants.
configLIBRARY_LOWEST_INTERRUPT_PRIORITY and configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY
Whereas configKERNEL_INTERRUPT_PRIORITY and configMAX_SYSCALL_INTERRUPT_PRIORITY
are full eight bit shifted values, defined to be used as raw numbers directly
in the ARM Cortex-M4 NVIC registers, configLIBRARY_LOWEST_INTERRUPT_PRIORITY
are equivalents that are defined using just the 4 priority bits implemented in the SAM4
These values are provided because the CMSIS library function NVIC_SetPriority()
requires the un-shifted 4 bit format.
Attention please!: See the page dedicated to setting interrupt priorities on ARM Cortex-M devices. Remember that ARM Cortex-M cores use
numerically low priority numbers to represent HIGH priority interrupts. This
can seem counter-intuitive and is easy to forget! If you wish to assign an
interrupt a low priority do NOT assign it a priority of 0 (or other low numeric
value) as this will result in the interrupt actually having the highest priority
in the system - and therefore potentially make your system crash if this
priority is above configMAX_SYSCALL_INTERRUPT_PRIORITY. Also, do not leave
interrupt priorities unassigned, as by default they will have a priority of 0
and therefore the highest priority possible.
The lowest priority on a ARM Cortex-M core is in fact 255 - however different
ARM Cortex-M microcontroller manufacturers implement a different number of priority bits and supply library
functions that expect priorities to be specified in different ways. For example,
on Atmel SAM4 ARM Cortex-M4 microcontrollers, the lowest priority you can specify is in fact 15 - this is defined by the constant
configLIBRARY_LOWEST_INTERRUPT_PRIORITY in FreeRTOSConfig.h. The highest priority
that can be assigned is always zero.
It is also recommended to ensure that all priority bits are assigned as
being preemption priority bits, and none as sub priority bits, as they are in the provided
Each port #defines 'BaseType_t' to equal the most efficient data type for that
processor. This port defines BaseType_t to be of type long.
Interrupt service routines
Unlike many FreeRTOS ports, interrupt service routines that cause a context switch have
no special requirements, and can be written as per the compiler documentation.
The macro portEND_SWITCHING_ISR() can be used to request a context switch from
within an interrupt service routine.
Note that portEND_SWITCHING_ISR() will leave interrupts enabled.
The following source code snippet is provided as an example. The interrupt
uses a semaphore to synchronise with a task (not shown), and calls portEND_SWITCHING_ISR()
to ensure the interrupt returns directly to the task if the task has an equal
or higher priority than the interrupted task. See the function
USART1_Handler() in the file serial.c included in this demo project for another
long lHigherPriorityTaskWoken = pdFALSE;
/* Clear the interrupt if necessary. */
/* This interrupt does nothing more than demonstrate how to synchronise a
task with an interrupt. A semaphore is used for this purpose. Note
lHigherPriorityTaskWoken is initialised to zero. */
xSemaphoreGiveFromISR( xTestSemaphore, &lHigherPriorityTaskWoken );
/* If there was a task that was blocked on the semaphore, and giving the
semaphore caused the task to unblock, and the unblocked task has a priority
higher than the current Running state task (the task that this interrupt
interrupted), then lHigherPriorityTaskWoken will have been set to pdTRUE
internally within xSemaphoreGiveFromISR(). Passing pdTRUE into the
portEND_SWITCHING_ISR() macro will result in a context switch being pended to
ensure this interrupt returns directly to the unblocked, higher priority,
task. Passing pdFALSE into portEND_SWITCHING_ISR() has no effect. */
portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
Only FreeRTOS API functions that end in "FromISR" can be called from an
interrupt service routine - and then only if the priority of the interrupt
is less than or equal to that set by the configMAX_SYSCALL_INTERRUPT_PRIORITY
configuration constant (or configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY).
Resources used by FreeRTOS
FreeRTOS requires exclusive use of the SysTick and PendSV interrupts. SVC number #0 is also used.
Switching between the pre-emptive and co-operative RTOS kernels
Set the definition configUSE_PREEMPTION within FreeRTOSConfig.h to 1 to use pre-emption or 0
to use co-operative. The full demo application may not execute correctly when the co-operative RTOS scheduler is
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 files.
Source/Portable/MemMang/heap_4.c is included in the ARM Cortex-M4 demo application project to provide the memory
allocation required by the RTOS kernel.
Please refer to the Memory Management section of the API documentation for
Note that vPortEndScheduler() has not been implemented.
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.