Quality RTOS & Embedded Software

KERNEL
WHAT'S NEW
Featured FreeRTOS IoT Integrations:
Extended Maintenance Plan (EMP):
Delta Over-the-Air Updates:
December 2021 Releases:
FreeRTOS LTS libraries:

FreeRTOS MPS2 QEMU Demo (Arm Cortex-M3)
For both the IAR and arm-none-eabi-gcc compilers (makefile and Eclipse)
[RTOS Ports]


FreeRTOS on QEMU


This page documents a FreeRTOS kernel demo that targets the Arm Cortex-M3 mps2-an385 QEMU model. Preconfigured build projects are provided for both the IAR Embedded Workbench and arm-none-eabi-gcc (GNU GCC) compilers. The GCC project uses a simple makefile that can be built from the command line or the provided Eclipse CDT IDE project.



IMPORTANT! Notes on using the QEMU Cortex-M3 RTOS 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?, noting in particular the recommendation to develop with configASSERT() defined in FreeRTOSConfig.h and configCHECK_FOR_STACK_OVERFLOW set to 2.

Source Code Organisation

The FreeRTOS distribution available from this website site contains the source files for all the FreeRTOS ports, and the projects for all the FreeRTOS demo applications. It therefore contains many more files than are required to use the Cortex-M3 mps2-an385 QEMU demo. See the Source Code Organization section for a description of the directory structure and information on creating a new FreeRTOS project.

The IAR Embedded Workbench for ARM workspace for the mps2-an385 demo application is called RTOSDemo.eww, and is located in the FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/iar directory.

The makefile that builds the project using the arm-none-eabi-gcc (GNU GCC) compiler, and the Eclipse project that builds the same makefile, are both located in the FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/gcc directory.



The mps2-an385 Arm Cortex-M3 QEMU Demo Application

Functionality

The demo projects provide both the simple blinky and comprehensive test/demo configurations described on the FreeRTOS Demos Applications documentation page. Specific to the demo documented on this page, the "check" task periodically prints a message in the following format:
StatusMessageString : aaaa (bb)
Where StatusMessageString is a descriptive text string, aaaa is the RTOS tick count, and bb is the number of times the application detected interrupts becoming nested.


Building and executing the demo application - IAR

  1. Open FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/iar/RTOSDemo.eww from within the IAR Embedded Workbench IDE.

  2. Open main.c, and set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to generate either the simply blinky demo, or the comprehensive test and demo application, as required.

  3. Select 'Rebuild All' from the IDE's 'Project' menu, the RTOS Demo project should build without any errors or warnings. A successful build creates the elf file FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/iar/Debug/Exe/RTOSDemo.out.

    Note: If QEMU is already running the build will fail because QEMU prevents the generated elf file from being overwritten.

  4. Ensure QEMU is installed on your host computer.

  5. Start QEMU with the following command line, replacing [path-to] with the correct path to the RTOSDemo.out file generated by the IAR build.

    qemu-system-arm -machine mps2-an385 -cpu cortex-m3 -kernel [path-to]/RTOSDemo.out -monitor none -nographic -serial stdio -s -S
    QEMU command line

    Omit the "-s -S" if you just want to run the FreeRTOS application in QEMU without attaching the debugger.

  6. After the build completes, select 'Download and Debug' from the IDE's 'Project' menu. The IAR debugger should create a GDB connection to QEMU, start a debug session, and break on entry to the main() function.

    Note: Remember to kill the QEMU session when the debugging session ends, otherwise QEMU will prevent the executable image being overwritten the next time the IAR project is built - resulting in a linker error.


Building and executing the demo application - GCC Makefile

  1. Ensure both the arm-none-eabi-gcc compiler and GNU make utility are installed on your host machine.

  2. Open FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/main.c, and set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to generate either the simply blinky demo, or the comprehensive test and demo application, as required.

  3. Open a command prompt and navigate to the FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/gcc directory.

  4. Type "make" in the command prompt. The project should build without any compiler errors or warnings. Hint: Use the "-j" parameter to speed the compilation by using more cores on your host computer. For example, if you have four cores available you can build four C files at once by entering "make -j4". A successful build creates the elf file FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/gcc/output/RTOSDemo.out.

  5. Ensure QEMU is installed on your host computer.

  6. Start QEMU with the following command line, replacing [path-to] with the correct path to the RTOSDemo.out file generated by the GCC build.

    qemu-system-arm -machine mps2-an385 -cpu cortex-m3 -kernel [path-to]/RTOSDemo.out -monitor none -nographic -serial stdio -s -S
    QEMU command line

    Omit the "-s -S" if you just want to run the FreeRTOS application in QEMU without attaching the debugger.

  7. You can now use arm-none-eabi-gdb to start a command line debug session, although my preference is to start a graphical debug session, as described next for those using the Eclipse IDE.


Building and executing the demo application - Eclipse

  1. Ensure the arm-none-eabi-gcc compiler and Eclipse CDT IDE are installed on your host machine. It may be necessary to install the GNU make utility separately if it is not included with Eclipse.

  2. Select 'Import' from the Eclipse 'File' menu, then 'Existing Projects Into Workspace' in the resultant window before clicking the Next button.


  3. In the next Window, select /FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/gcc as the root directory, check the FreeRTOSDemo project, and crucially ensure the "Copy projects into workspace" checkbox is unchecked, before clicking the Finish button to bring the project into Eclipse.


  4. Open main.c, and set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to generate either the simply blinky demo, or the comprehensive test and demo application, as required.

  5. Select 'Build All' from the Eclipse 'Project' menu. A successful build creates the elf file FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/gcc/output/RTOSDemo.out.

  6. Ensure QEMU is installed on your host computer.

  7. Open a command prompt then start QEMU with the following command line, replacing [path-to] with the correct path to the RTOSDemo.out file generated by the GCC build.

    qemu-system-arm -machine mps2-an385 -cpu cortex-m3 -kernel [path-to]/RTOSDemo.out -monitor none -nographic -serial stdio -s -S
    QEMU command line

    Omit the "-s -S" if you just want to run the FreeRTOS application in QEMU without attaching the debugger.

  8. Click the little arrow next to the green bug speed button, then select "Debug Configurations..." from the resultant menu.


  9. In the debug configurations window, select "FreeRTOSDemo Default" from under "GDB Hardware Debugging", then click the Debug button. The Eclipse debugger should create a GDB connection to QEMU, start a debug session, and break on entry to the main() function.



RTOS Configuration and Usage Details

ARM Cortex-M3 RTOS port specific configuration

Configuration items specific to this demo are contained in FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/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 need. Lowering the frequency will improve efficiency in production applications, but make the self checks within the comprehensive tests fail.

  • configKERNEL_INTERRUPT_PRIORITY and configMAX_SYSCALL_INTERRUPT_PRIORITY

    See the RTOS kernel configuration documentation for full information on these configuration constants. Note the QEMU model has 8 interrupt priority bits.

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 ST STM32F7 ARM Cortex-M7 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.

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() (or portYIELD_FROM_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 direct to task notification to synchronise with a task (not shown), and calls portEND_SWITCHING_ISR to ensure the interrupt returns directly to the task.

void Dummy_IRQHandler(void)
{
long lHigherPriorityTaskWoken = pdFALSE;

    /* Clear the interrupt if necessary. */
    Dummy_ClearITPendingBit();

    /* This interrupt does nothing more than demonstrate how to synchronise a
    task with an interrupt.  A task notification is used for this purpose.  Note
    lHigherPriorityTaskWoken is initialised to zero. */
    vTaskNotifyGiveFromISR()( xTaskToNotify, &lHigherPriorityTaskWoken );

    /* If the task with handle xTaskToNotify was blocked waiting for the notification
    then sending the notification will have removed the task from the Blocked
    state.  If the task left the Blocked state, and if the priority of the task
    is higher than the current Running state task (the task that this interrupt
    interrupted), then lHigherPriorityTaskWoken will have been set to pdTRUE
    internally within vTaskNotifyGiveFromISR().  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 of 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 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_4.c is included in the ARM Cortex-M7 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.