STM32F4-Discovery tutorial with Visual Studio

This tutorial shows how to create a simple project for the STM32F4-Discovery board with Visual Studio.

You will need the following hardware and software to follow this tutorial:

We will create a simple ‘blinking LED’ project, build it and step through it in debugger.

  1. Start Visual Studio. Select File->New->Project. 01-newproj
  2.  Select VisualGDB -> Embedded Project Wizard.
    Pick a directory and a project name:
  3. As we are creating a firmware file without any static libraries, continue with the “embedded binary” option: 03-binary
  4. As we are building firmware for the STM32F4-Discovery board that contains ARM-based STM32F407VG microcontroller, select “ARM” on the toolchain selection page. If the ARM toolchain is not installed yet, VisualGDB will install it automatically:04-arm
  5. If this is your first STM32 project, slick “Download support for more devices”, select “STM32″ and click “Install selected packages” to download STM32 support files:05-stm32
  6. Once the STM32 package is installed, you will see the list of supported devices. Select STM32F407VG from the list:06-stm32f407vg
  7. The STM32F4-Discovery board contains 4 LEDs connected to PD12-PD15 pins (see board schematics for more details):ledsTo make a project that will blink the green LED connected to PD12 select “GPIOD” and “12” as the port group and number:07-ports
  8. The STM32F4-Discovery board includes an on-board ST-Link programmer. There are 2 software packages compatible with it: OpenOCD and Texane ST-Link. Click on “Install support for additional debug methods” and select both tools:08-debug09-methods
  9. The Texane ST-Link tool is easier to configure, but can be unreliable for STM32F4 devices. OpenOCD is a more sophisticated tool that provides more flexibility, but requires a bit of initial setup. Use the quick setup mode and select ST-Link v2 as your connection to get a basic setup done:10-openocd
  10. Press “Test selected OpenOCD settings” to verify your configuration:12-testNote that the ‘gdb connection rejected’ does not indicate an error, as VisualGDB closes the connection immediately after ensuring that OpenOCD has accepted it.
  11. If you familiar with OpenOCD, you can switch to the manual mode and modify the configuration scripts:10a-manual
  12. Press “Finish” in the wizard to complete it. Once the project is created, press Ctrl-Shift-B to build it:13-build
  13. Press F5 to start debugging. Note how the LED turns on and off. Now put a breakpoint on the first GPIO_WriteBit() line. The breakpoint will be triggered immediately and the LED will be off:led_off
  14. Open the Registers window by pressing Ctrl-Alt-G or selecting Debug->Windows->Registers. Right-click inside it and ensure that ‘All Registers’ is enabled:14-breakpoint
  15. Note the value of the PC register. A value in the 0x80xxxxxx range indicates that the program is running from FLASH memory. We will change it to SRAM later. Press F10 to step over one line of code. Notice how the LED turns on:led_on
  16. Exit debugging by pressing Shift-F5. Right-click on the project node in Solution Explorer and select “VisualGDB Project Properties”:15-properties
  17. Click “Change settings” and select SRAM instead of FLASH:16-sram
  18. The previous binary build for FLASH mode won’t run from SRAM. Rebuild the project to fix this:18-buildsram
  19. Start debugging and wait until the breakpoint is hit. Note that the pc register now contains a value in the 0x20xxxxxx range that indicates running from SRAM:19-break-sram

Making your program run from SRAM does not consume FLASH rewrite cycles when downloading the program or setting breakpoints. However, it has the disadvantage over FLASH mode: your entire program including all code and constants should fit in the SRAM, so it may not work for large programs.