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.

  1. Start Visual Studio and open the VisualGDB Embedded Project Wizard:01-newprj
  2. Proceed with the default settings on the first page:02-elfbinary
  3. 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:03-device
  4. 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.04-sampleWarning: 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!
  5.  Select the debug method supporting your hardware.
    If you are using OpenOCD, select “Detect” to automatically
    detect the programming interface based on connected USB
  6. Press Finish to generate your project and then build it by pressing Ctrl-Shift-B:06-build
  7. If you have previously selected an incorrect FreeRTOS port, you may get a the following build errors:

    If this happens, go to the Embedded Frameworks page of VisualGDB Project Properties and change the selected FreeRTOS port:07-core
  8. 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):08-prepro
  9. 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:09-bkpt
  10. You can step into the FreeRTOS code to see what happens when you call the FreeRTOS functions:10-stepin
  11. You can also use the Clang-based Code Map to examine relations between various functions in your project:11-codemap
  12. 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:12-heap
  13. Use the pvPortMalloc()/vPortFree() instead to manage your dynamic memory:13-portmalloc
  14. This will result in allocating actual memory:14-allocated
  15. 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:15-heapsize
  16. 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:16-standalone