Using STM32 Startup Code Generator with VisualGDB

This tutorial shows how to use the STM32CubeMX startup code generator with VisualGDB version 5.0 or later. We will use the STM32CubeMX tool to generate a basic project and then import it into VisualGDB to build and debug it.

If you have not used VisualGDB with STM32 before, follow our basic STM32 tutorial to get started.

  1. Start the STM32CubeMX tool and click on “New Project”:01-cubeprj
  2. Select your device in the device list:02-device
  3. You can use the Pinout tab to configure initialization of the peripherals. E.g. you can select to automatically initialize USART1 in asynchronous mode with no flow control:03-periph
  4. You can also use the Clock Configuration page to configure the clock speeds for your device:04-clock
  5. Finally you can use the “Configuration” page” to review the settings for the generated project:05-cfg
  6. Select “Project->Generate code” and specify the folder where you want to have the project:06-codegen
  7. On the “Code Generator” page select “Add necessary library files as reference”. This will avoid duplicating the HAL library files, as VisualGDB automatically maintains a copy in a shared location:06a-linkfiles
  8. Press OK to generate the project. If you have not installed the HAL package into the STM32CubeMX repository, it will ask you to download one. You can also skip this step, as VisualGDB automatically installs the necessary files anyway:07-fwpkg
  9. Now we will import the generated project into VisualGDB. Start Visual Studio and select File->New->Project. Then select the VisualGDB Embedded Project Wizard and specify the folder where you have generated a project with STM32CubeMX:08-cubedemo
  10. On the first wizard page select “Import” and uncheck the “Copy imported files” flag as the files are already in the project directory:09-import
  11. On the next page select the toolchain and the device you are targeting:10-device
  12. Then select the current project directory as the source directory for your files:11-source
  13. On the last page select the debug method:12-debug
  14. When you press “Finish”, VisualGDB will generate a project for you. If you try to build it now, you will get errors regarding a missing HAL configuration file:13-errors
  15. This happens because the STM32CubeMX tool has placed the generated include files into a “Inc” subfolder. To fix this, right-click on your project in Solution Explorer, select VisualGDB Project Properties, got to the Makefile Settings page and add “Inc” to the include directory list:14-include
  16. Now you can successfully build your project:15-buildLet’s overview the main source files in the newly created project:
    File Contents Origin
    main.c
    • Clock configuration routine
    • Entry point
    Generated by STM32CubeMX
    stm32xxxx_hal_msp.c Peripheral initialization routines Generated by STM32CubeMX
    stm32xxxx_it.c Interrupt vectors Generated by STM32CubeMX
    system_stm32xxxx.c Low-level hardware initialization Shipped with STM32 HAL
    stm32xxxx_hal_xxx.c Drivers for various peripherals Shipped with STM32 HAL
    startup_stm32fxxxx.c
    • Memory initialization
    • Default interrupt handlers
    Shipped with VisualGDB
  17. Normally the next step would be to modify the main() function to add some functionality to it. In this tutorial we will simply put a breakpoint in it and step through it to ensure that no unexpected condition occurs:16-debug
  18. You can customize the frameworks used by your project (e.g. add FreeRTOS or remove the default system initialization file) via the Embedded Frameworks page of VisualGDB Project Properties:17-frameworks