Using FreeRTOS with STM32 Devices and VisualGDB
This tutorial shows how to develop and debug a basic FreeRTOS project for the STM32F4Discovery board with VisualGDB. Before you begin, install VisualGDB 5.0 or later and update your STM32 package via Tools->Embedded Tools Manager to the latest version.
If you have not used VisualGDB with STM32 before, follow our basic STM32 tutorial to get started.
- Start Visual Studio and open the VisualGDB Embedded Project Wizard:
- Proceed with the default settings on the first page:
- On the next page select your STM32 device. Take a note of the selected floating point mode (for devices that support it) as you will need it on the next page:
- On the next page select the FreeRTOS sample and specify the LED locations from your board schematic. Also select the CPU core according to the selected device. If you are using software floating-point support, trying to compile a FreeRTOS port that saves FP registers will result in a compilation error. Trying to use a non-FPU port on a project with hardware FP support will prevent the FP registers from being saved and restored while switching tasks.Warning: do not select “Heap3 – use newlib malloc()/free()”, as the default newlib implementations of malloc()/free() rely on the assumption that the stack pointer is behind the heap area. For FreeRTOS tasks where the stack area is allocated from the heap this will not hold!
- Select the debug method supporting your hardware.
If you are using OpenOCD, select “Detect” to automatically
detect the programming interface based on connected USB
devices: - Press Finish to generate your project and then build it by pressing Ctrl-Shift-B:
- If you have previously selected an incorrect FreeRTOS port, you may get a the following build errors:
Assembler messages: error : selected processor does not support Thumb mode `vstmdbeq r0!,{s16-s31}' error : instruction not allowed in IT block -- `stmdb r0!,{r4-r11,r14}' error : selected processor does not support Thumb mode `vldmiaeq r0!,{s16-s31}' error : instruction not allowed in IT block -- `msr psp,r0'
If this happens, go to the Embedded Frameworks page of VisualGDB Project Properties and change the selected FreeRTOS port:
- FreeRTOS heavily uses preprocessor macros. You can quickly look up how a certain macro will look like after being expanded by selecting it and running the “Preprocess Selected Lines” command from the context menu (Clang IntelliSense needs to be enabled for this feature to work):
- Press F5 to start debugging your program. You will notice how 2 LEDs are being controlled by 2 threads running in parallel. Set a breakpoint in one of the thread functions and wait until it is hit:
- You can step into the FreeRTOS code to see what happens when you call the FreeRTOS functions:
- You can also use the Clang-based Code Map to examine relations between various functions in your project:
- Remember not to use malloc() and free() from FreeRTOS tasks as this will result in an error because the standard C library expects the stack to be after the heap area:
- Use the pvPortMalloc()/vPortFree() instead to manage your dynamic memory:
- This will result in allocating actual memory:
- Note that FreeRTOS manages its heap separately from the standard C library. You can change the heap size by editing the configTOTAL_HEAP_SIZE macro in the FreeRTOSConfig.h file in your project directory:
- If you want to modify the FreeRTOS source code, you can convert your project to a stand-alone project via VisualGDB Project Properties. This will copy the FreeRTOS sources and STM32 HAL sources to your project directory and will allow modifying them without affecting other projects. However you will lose the ability to reconfigure various FreeRTOS settings via the Embedded Frameworks GUI as all the frameworks will be merged into your project: