Porting embedded code with mbed and VisualGDB

This tutorial shows how to create portable embedded applications with the ARM mbed framework and VisualGDB. We will show how to create a basic multi-threaded application for the STM32F4Discovery and then quickly port it to the Freescale FRDM-KL25Z board.

  1. Begin with starting Visual Studio and opening the VisualGDB Embedded Project wizard:01-prjname
  2. Continue with the default “Create a new project” setting on the first page:02-prjtype
  3. On the next page select the first device you want to target. We will start with the STM32F4Discovery board, so we select DISCO_F407 here. It is recommended to select newlib-nano as the C library to reduce the size of your binary: 03-device
  4. On the next page select “LEDBlink (RTOS)” sample and keep the default LED numbers:04-sample
  5. On the last page select OpenOCD as the debug method:05-stlink
  6. Press Finish to generate your project. Build it by pressing Ctrl-Shift-B:06-build
  7. Hit F5 to start debugging. Observe how the two LEDs begin to blink with different periods as 2 independent threads are controlling them:f4disco
  8. If you click on the LED1 text in your code and select “Go to definition”, you will see that it’s defined in the PinName enum as an alias for PD_13. You will also see other meaningful names like USER_BUTTON. Those names are defined independently for each supported mbed platform so that the application developers don’t need to look up the pin numbers in the schematic.07-led1
  9. Now we will switch the project to the KL25Z platform. First, follow this tutorial to ensure that you can program the KL25Z board successfully. Then open VisualGDB Project Properties for the mbed project and switch the device to KL25Z:08-kl25z
  10. Then go to the Debug Settings page and switch the debug method to Segger J-Link. Select the device manually (MKL25Z128xxx4) as the KL25Z name is too generic and check the “Reset device after programming” checkbox:09-segger
  11. Try building your project. The compiler will show an error indicating that the r7 register cannot be accessed explicitly:10-error
  12. This happens because the debug build uses r7 to store the frame pointer and the syscall code on Cortex M0 assumes that it is available. Fix this by adding the following line right after the SVC_Call() definition for Cortex M0:
    #pragma GCC optimize ("-fomit-frame-pointer")

    11-omitfp

  13. Build and run your project. Observe how the red and green channels of the RGB LED show the same behavior as the green and yellow LEDs did on STM32F4Discovery:kl25z
  14. If you go to the definition of the LED1 now, you will see that it is defined differently for KL25Z reflecting different board schematic:12-newledYou can repeat the steps shown above to retarget the project to any device supported by mbed as long as you update the debug settings so that VisualGDB can program and debug your board.
  15. If you are using one of the boards not directly supported by VisualGDB, you can enable manual setup in the OpenOCD settings and pick the interface and board/target scripts manually from the list, however they may need to be edited before they can work:13-openocdThe scripts are located under %LOCALAPPDATA%\VisualGDB\EmbeddedDebugPackages\com.sysprogs.arm.openocd\share\openocd\scripts.
  16. If you are using Segger J-Link, the setup will be much easier. Simply pick the device name from the list and the Segger software will configure everything automatically:14-segger