Step by step debugging firmware on the Aliexpress / EBay STM32 boards

May 9, 2016 in ARM, C, C++, Hardware, Linux, Microcontroller, Programming, STM32, Tips & Tricks, Tutorial by admin

arm_cortex_logoIn some previous topics (here and here) I wrote about some cheap development boards which can be acquired from EBay or Aliexpress. Since System Workbench for STM32 is freely available for a while now, let’s see how can we use it to generate a project, compile it, upload it to a board and debugging it step by step. We’ll use for this the board I got from EBay, but it works the same with the any STM32 other board I have and also with some self-made ones.

For being able to install firmware on the board and debug it, first we need to have a hardware part which will sit between the computer and the board. There are various models and versions of these jtag debugers and they can be ordered online or found pretty cheap on ebay (clones). Another way to get hold of one of these is to have a development board which comes equiped with JTAG adapters, like the STM32 discovery series of boards. Some of these JTAG debuggers allow even breaking apart the JTAG debugger from the development board itself (LPCXpresso series, the nucleo boards).
Regardless of which JTAG interface is used, it should be one which is known to work with OpenOCD, as we’ll use OpenOCD for debugging. In our case we’ll use the stm32f4 discovery board’s stlink2 side. However, Before using it as a JTAG debugger, we need to disconnect the STLink part from the discovery board, by removing two jumpers. Once that is done, the STLink itself won’t be connected to the discovery board and it’s SWD header can be connected to any other board.

(You can of course use any other development environment/hardware interface – like LPCExpresso for example – assuming the development environment and the hardware interface supports your MCU – the connection between the SWD connector and ARM JTAG will be the same)

The boards I have purchased have a standard 20 pin ARM JTAG header, where also the SWD required pins are found. Of these we’ll use only SWDIO, SWCLK, RESET, VRef and GND. The connection diagram between the SWD connector and the 20 pin ARM JTAG connector is bellow:

swd2jtag

For software development, we’ll use Eclipse with System Workbench for STM32. It can be downloaded from here. Some of it’s key features include:

  • Comprehensive support for STM32 microcontrollers, STM32 Nucleo boards, Discovery kits and Evaluation boards, as well as STM32 firmware (Standard Peripheral library or STM32Cube HAL)
  • GCC C/C++ compiler
  • GDB-based debugger
  • Eclipse IDE with team-work management
  • Compatible with Eclipse plug-ins
  • ST-LINK support
  • No code size limit
  • Multiple OS support: Windows® , Linux and OS X®

Once it’s installed (just follow the information on the site), we can proceed fire it up and create the very first project with it. For this, we will need to know the micro-controller on the board (STM32F103VGT6 in our case).

First we need to create the project, either C or C++which uses the appropriate toolchain, so we select Ac6 STM32 MCU project type and select the appropriate configurations:

01-new project 02-select toolchain
03-select configurations 04-creating-time

Next, we need to configure the MCU we use. For this we will need to create a board or select one from the available ones, select the MCU we gonna use, the debug device (should be already connected to the computer) and the debug interface, which in our case is SWD.

05-mcu configuration 06-creating new board
07-board created 08-configure firmware

As you can see, additionally, we gonna need to configure the firmware provided (StdPeriph or Cube Hal)and how this will be added to the project – in a separate folder, in a library, etc. If the firmware is not available, you will have to download it. Pressing finish the project gets created and we end up (with the configuration presented in the images) with two projects, one for the low level firmware and the other for the project itself.

From here all goes as with usual C/C++ project using eclipse CDT: you can build the project as usual, and debug it. For debugging you will have to select from menu ‘debug as Ac6 STM32 C/C++ Application’ which will actually upload the firmware to the board, setup everything required and start debugging your application. The predefined debug configuration works okay – just make sure all the connections are ok. Once started it will break in main, from where you can run it step by step as usual in any C/C++ project:

11-debug perspective-breakpoing

Happy coding