FreeRTOS Demo for the ST STM32L152 ARM Cortex-M3 Microcontroller
[RTOS Ports]




Introduction

[Also see the project that demonstrates how the FreeRTOS tickless mode can be used to minimise the power consumption of an application running on an STM32L]

This page documents the FreeRTOS demo application for the low power STM32L152 microcontroller from STMicroelectronics. The demo uses the IAR Embedded Workbench for ARM V6.10 from IAR Systems, and targets the official STM32L152-EVAL evaluation board from STMicroelectronics (instructions are provided should you wish to use an alternative development board).

Note: If this project fails to build then it is likely the version of IAR Embedded Workbench being used is too old. If this is the case, then it is also likely that the project file has been (silently) corrupted and will need to be restored to its original state before it can be built even with an updated IAR version.

The FreeRTOS state viewer plug-in is pre-installed in version 6.10, removing the requirement to install it manually.


FreeRTOS features demonstrated by the project presented on this page

The project demonstrates the following FreeRTOS features and techniques:



IMPORTANT! Notes on using the STM32 ARM Cortex-M3 Demo

Please read all the following points before using this RTOS port.

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

Source Code Organisation

The FreeRTOS download contains the source code for all the FreeRTOS ports so includes many more files than are required for this demo. See the Source Code Organization section for a description of the downloaded files and information on creating a new project.

The IAR workspace file for the STM32L152 demo is called RTOSDemo.eww and is located in the FreeRTOS/Demo/CORTEX_STM32L152_IAR directory.


The Demo Application

Demo application hardware setup

The demo application includes an interrupt driven UART test. This creates two tasks, a transmit task and a receive task. The transmit task transmits characters on USART3, and the receive task receives characters on USART3. The receive task expects to receive the characters transmitted by the transmit task, so for correct operation a loopback connector must be fitted to the USART3/CN5 connector of the STM32L152-EVAL evaluation board (pins 2 and 3 must be connected together on the CN5 9Way connector). Further, the USART3 related jumpers JP5, JP7 and JP8 must be set to short the jumper pins 2 and 3 (not set to short pins 1 and 2).

The demo application also uses all four LEDs that are built onto the STM32L152-EVAL board. Jumpers JP18 and JP19 must be closed (shorted) for LED3 and LED4 to be functional.

The port was developed and tested using a J-Link USB JTAG interface connected to CN8 on the target hardware. The evaluation hardware also provides a built in ST-Link debug interface that can be accessed using the USB connector marked ST-Link/CN11.


Building and running the demo application

  1. Open the FreeRTOS/Demo/CORTEX_STM32L152_IAR/RTOSDemo.eww project from within the Embedded Workbench IDE.

  2. Select 'Rebuild All' from the IDE 'Project' menu. The project should build with no errors or warnings.

  3. Select 'Debug' from the IDE 'Project' menu. The microcontroller flash memory will be programmed with the newly built binary and the debugger will break on the entry to main().


Functionality

The top of this documentation page lists the FreeRTOS features that are demonstrated by the STM32L152 demo project. The comments contained in the main.c source file provide more information on how this functionality is implemented and achieved.

The following behaviour will be observed when the demo is executing normally:

The image below is a screen shot taken during a debugging session. The FreeRTOS state viewer Tasks and Queues windows can be seen at the bottom of the screen shot. Run time statistics information can be see in in the terminal IO window.

FreeRTOS STM32L152 Debug Session Screen Shot
Screen shot taken during a FreeRTOS debugging session.
The FreeRTOS state viewer plug in windows can be seen at the bottom of the screen shot.
Run time statistics information can be seen in the terminal IO window.



RTOS Configuration and Usage Details


RTOS port specific configuration

Configuration items specific to these demos are contained in FreeRTOS/Demo/CORTEX_STM32L154_IAR/FreeRTOSConfig.h. The constants defined in this file can be edited to meet the needs of your application. In particular -

Attention please!: Remember that ARM Cortex-M3 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-M3 core is in fact 255 - however different Cortex-M3 vendors implement a different number of priority bits and supply library functions that expect priorities to be specified in different ways. For example, on the STM32 the lowest priority you can specify in an ST driver library call 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 four priority bits are assigned as being premption priority bits. This can be ensured by passing "NVIC_PriorityGroup_4" into the ST library function NVIC_PriorityGroupConfig(). In the demo project this is done from the function prvSetupHardware(), which is itself defined in main.c.

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.

Note that vPortEndScheduler() has not been implemented.


Interrupt service routines

Unlike most 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 ISR.

Note that portEND_SWITCHING_ISR() will leave interrupts enabled.

This demo project provides examples of FreeRTOS interrupt service routines - namely TIM6_IRQHandler() defined in main.c and USART3_IRQHandler() defined in serial.c. Note that USART3_IRQHandler() is implemented to stress the port and demonstrate queues being used from interrupts - it is not intended to be a demonstration of an efficient interrupt service routine!


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

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


Compiler 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 files.


Memory allocation

Source/Portable/MemMang/heap_2.c is included in the ARM Cortex-M3 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 full information.