Download FreeRTOS
 

Quality RTOS & Embedded Software

KERNEL
WHAT'S NEW
Simplifying Authenticated Cloud Connectivity for Any Device.
Designing an energy efficient and cloud-connected IoT solution with CoAP.
Introducing FreeRTOS Kernel version 11.0.0:
FreeRTOS Roadmap and Code Contribution process.
OPC-UA over TSN with FreeRTOS.

ST STM32F4xx ARM Cortex-M4F Demo
Using IAR EWARM development tools
[RTOS Ports]


ST STM32F4 ARM Cortex-M4F starter kit
STM32F407ZG_SK starter kit board



This page documents a FreeRTOS ARM Cortex-M4F demo application that targets an STMicroelectronics STM32F4xx microcontroller. An IAR project is provided that is pre-configured to run on the development board provided as part of the STM32F407ZG-SK starter kit.

The FreeRTOS ARM Cortex-M4F port supports a full interrupt nesting model, and never completely disable interrupts. The port can only be used with hardware floating point support turned on in the compile time options of the project used to build the source files. ARM Cortex-M4 devices that don't include a floating point unit should not use this port, but instead use the FreeRTOS ARM Cortex-M3 port layer.

Note: The demo presented on this page tests the FreeRTOS floating point support by forcing interrupts that use the floating point unit to nest to a level of three deep. The nesting starts from the tick hook function - which is called from the FreeRTOS tick interrupt code. Removing this part of the test will greatly improve the efficiency of the RTOS kernel, and can be achieved by setting configUSE_TICK_HOOK to zero in FreeRTOSConfig.h.

Note 2: 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.


IMPORTANT! Notes on using the FreeRTOS IAR STM32F4 demo project

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 zip file download contains source code for all the FreeRTOS ports, and every demo application project. It therefore contains many more files than are required to build and run the ST STM32F407ZG demo. See the Source Code Organization section for a description of the downloaded files, and information on creating a new project.

The IAR Embedded Workbench workspace for the STM32F4xx demo is called RTOSDemo.eww, and is located in the FreeRTOS/Demo/CORTEX_M4F_STM32F407ZG-SK directory of the official FreeRTOS .zip file download.



The STMicro ARM Cortex-M4F Demo Application

Functionality

This demo application demonstrates:

  • Floating point instructions being used from tasks.
  • Floating point instructions being used from interrupts that nest 3 deep. Note: The nesting starts from the tick hook function - which is called from the FreeRTOS tick interrupt code. Removing this part of the test will greatly improve the efficiency of the RTOS kernel, and can be achieved by setting configUSE_TICK_HOOK to zero in FreeRTOSConfig.h.
  • Software timers.
  • Queues.
  • Mutexes.
  • Semaphores.
  • Malloc failed, stack overflow, tick and idle hook functions.
The demo application can be configured to provide very simply 'blinky' style functionality, or a full and comprehensive test and demonstration of the FreeRTOS functionality. The #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant, which is defined in main.c, is used to switch between the two modes.

Demo application tasks are split between standard demo tasks, and demo specific tasks. Standard demo tasks are used by all FreeRTOS ports and demo applications. They have no purpose other than to demonstrate the FreeRTOS API, and test a port.

mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY setting
Description
Set to 1 This creates a very simple example that creates three standard demo "flash" tasks. Each of the three tasks toggles an LED at a fixed but different frequency. LEDs STAT1, STAT2 and STAT3 are used.

Set to 0 This is a very comprehensive demo that creates 46 tasks before starting the RTOS scheduler, then continuously dynamically creates and deletes another two tasks as the application executes.

It creates a lot of queues, a software timer, and different types of semaphore. The tasks consist mainly of the standard demo tasks.

The demo includes application specific "register test" tasks. These are idle priority tasks that start by filling all the generic and floating point registers with known values. The tasks then repeatedly check that each register maintains the value written to it, as it enters and exits the Running state, for the lifetime of the task. Each of the two register check tasks uses a different set of known values, and a value being unexpectedly changed is symptomatic of an error in the context switching mechanism.

The floating point interrupt nesting test starts in the tick hook function. The tick hook function is an optional application defined function that is called from the real time kernels tick interrupt if configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. The tick hook function fills the floating point registers with a known value, then forces a medium priority interrupt to interrupt it. The medium priority interrupt fills the floating point registers with a different value, before it too forces a high priority interrupt to interrupt it. The high priority interrupt fills the floating point registers with yet another value. As the interrupt nesting stack then unwinds, first the medium priority interrupt checks that the floating point registers contain the values that it wrote to them, and then the tick hook function (the lowest priority interrupt) checks that the floating point registers contain the values that it wrote to them.

A 'check' software timer is created and started to periodically inspects the standard demo tasks and register test tasks, to ensure they are all operating as expected, and have never reported an error. The check timer callback function toggles LED STAT4 to give visual feedback of the system status. If LED STAT4 toggles every 3 seconds, then the check software timer has not discovered any problems with the execution of the test and demo tasks. If the rate at which LED STAT4 toggles increases to every 200 milliseconds, then the check software timer has discovered a problem in at least one task.

Like the simple flasher demo, the comprehensive demo creates the standard demo flash tasks, which toggle LEDs STAT1, STAT2 and STAT3 at fixed but different frequencies.


Hardware set up

The demo uses the LEDs that are built onto the starter kit hardware, so no hardware set up is required.


Building and executing the demo application

  1. Ensure the target hardware is connected to the host computer using a suitable debugger interface. A J-Link lite is provided in the starter kit.

  2. Open the RTOSDemo.eww Embedded Workbench workspace from within the Embedded Workbench IDE.

  3. Select "Build All" from the Embedded Workbench "Project" menu - the demo application should build without errors.

  4. When the build completes, select "Download and Debug" from the Embedded Workbench "Project" menu (or just press CTRL+D) to program the microcontroller flash memory, and start a debug session. The application will start to run, and then break at the start of the function main().



RTOS Configuration and Usage Details


Cortex-M4F FreeRTOS port specific configuration

Configuration items specific to this demo are contained in FreeRTOS/Demo/CORTEX_M4F_STM32F407ZG-SK/FreeRTOSConfig.h. The constants defined in this file can be edited to suit your application. In particular -
  • configTICK_RATE_HZ

    This sets the frequency of the RTOS tick interrupt. 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.

  • 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 raw values, defined to be used directly in the ARM Cortex-M4F NVIC registers, configLIBRARY_LOWEST_INTERRUPT_PRIORITY and configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY are equivalents defined using just the 4 priority bits available on the STM32. The ST Standard Peripheral Library function NVIC_Init(), and the CMSIS library function NVIC_SetPriority(), both require the unshifted 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 Cortex-M vendors implement a different number of priority bits and supply library functions that expect priorities to be specified in different ways. For example, on STM32 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 four priority bits are assigned as being preemption priority bits, and none as sub priority bits, as they are in the provided demo.

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 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 interrupt service routine.

Note that portEND_SWITCHING_ISR() will leave interrupts enabled.

As an example, this demo provides an interrupt service routine called EXTI9_5_IRQHandler(), which is defined in main.c. This interrupt is triggered by pressing the button marked "USER" on the starter kit hardware. The interrupt uses a semaphore to synchronise with a task, and calls portEND_SWITCHING_ISR to ensure the interrupt returns directly to the task. The function is shown below:

void EXTI9_5_IRQHandler(void)
{
long lHigherPriorityTaskWoken = pdFALSE;

    /* Only line 6 is enabled, so there is no need to test which line generated
    the interrupt. */
    EXTI_ClearITPendingBit( EXTI_Line6 );

    /* This interrupt does nothing more than demonstrate how to synchronise a
    task with an interrupt.  First the handler releases a semaphore.
    lHigherPriorityTaskWoken has been 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 currently executing task (the task that this interrupt
    interrupted), then lHigherPriorityTaskWoken will have been set to pdTRUE.
    Passing pdTRUE into the following macro call will cause this interrupt to
    return directly to the unblocked, higher priority, task. */
    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.


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 RTOSDemo/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 selected.


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-M4F 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.


Miscellaneous

Note that vPortEndScheduler() has not been implemented.




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.