1 Introduction
Since I spent quite some time figuring out what the best way is to debug the Spark-Core using ST-Link/v2 and how to do it, I thought I'd share my findings hoping it would save someone else some time. My aim is to make this tutorial usable for both beginners and advanced users so I'll start with the basics.
Hardware Requirements:
- PC
- Spark Core
- ST-LINK/v2 ( if you're going to buy one, you might wanna check ebay too )
- JTAG Shield ( buy one or make one )
2 Software Installation
I assume you already have downloaded and installed Eclipse IDE for C/C++ Developers otherwise download it from here and unpack it to a local folder.
2.1 Installing ST-LINK/v2 Device Drivers
Download and install the appropriate driver matching your Windows version from here. You might also want to install ST-Link Utility which not being necessary is a useful program. Once you did that, you should be able to see your STM32 STLink device in Device Manager.
2.2 Installing GNU Tools for ARM Embedded Processors
In order to be able to compile your projects and debug them, you need to download and install the latest version of GNU Tools for ARM Embedded Processors from here.
2.3 Installing EGit.
In order to be able to work with Git repositories, you might wanna consider installing Eclipse Git Team Provider. Go to Help > Install New Software and install Eclipse Git Team Provider.
2.4 Installing GNU ARM Eclipse Plug-ins
This set of Eclipse extensions are an easy, fast, great and portable way of creating, building, debugging and managing your project. You can read more about them here.
Go to Help > Install New Software and click Add. Type in "GNU ARM Eclipse Plug-ins" as the Name and "http://gnuarmeclipse.sourceforge.net/updates" as the Location. Select all the items and continue with the installation.
2.5 Installing The GNU ARM Eclipse OpenOCD
The Open On-Chip Debugger (OpenOCD) aims to provide debugging, in-system programming and boundary-scan testing for embedded target devices. It supports many different adapters including ST-LINK/v2 and has recently added support for SWO tracing which allows you to see the output of STDOUT e.g. printf, right inside your Eclipse terminal. In my opinion, this is an incredible and priceless debugging tool.
- Download the The GNU ARM Eclipse OpenOCD which is basically a compiled and hassle-free binary of the latest OpenOCD source from here.
- Run the setup and install it preferably in the default location.
- Navigate to C:\Program Files\GNU ARM Eclipse\OpenOCD\scripts\board, create a new file called sparkcore.cfg and populate it with the following lines:
This is a SPARK-CORE board with a single STM32F103MD chip.
source [find interface/stlink-v2.cfg]
set WORKAREASIZE 0x4000
source [find target/stm32f1x_stlink.cfg]
# use hardware reset, connect under reset
reset_config srst_only srst_nogate
3 Configuration
3.1 Creating a new project
Now since the Spark Core firmware is currently not compilable on Windows due to makefile issues, for the sake of demonstration, we will create a new project from scratch.
-
Go to File > New > C++ Project.
-
Select STM32F10x C/C++ Project and Cross ARM GCC and give your project a name.
-
Fill in the Target processor settings according to the picture below.
-
Change the directory structure if you so wish.
-
Select the configurations you want to include.
-
Check the path of the Toolchain you installed in section 2.2.
-
Right click on your project and choose Build Project. The build should succeed and you should get a Debug folder containing a .elf file named after you project.
3.2 Creating a Debug Configuration
The next step would be to setup a debug configuration and begin debugging!
-
Go to Window > Preferences > Run/Debug > String Substitution and make sure openocd_path refers to the bin folder of the OpenOCD installation. Click OK.
-
Click on the little green bug icon in the Eclipse toolbar and select Debug Configurations...
-
Right-click on GDB OpenOCD Debugging and select New. The Main tab should already be good to go.
-
In the Debugger tab, enter "-f board\sparkcore.cfg" to pass in the configuration file we created to the OpenOCD.
-
The Startup tab should already be all set as well.
-
In the Common tab, make sure you select Shared file to force the settings to be saved in the project rather than the Workspace.
-
Press the Debug button to flash the firmware to your board and start debugging! The debugger will stop in the beginning of the main function and once you press F8, the CPU will start the execution of your main function.
At this point, your firmware should already be flashed to the Spark Core and you will see the debug messages in the console which counts the seconds.
Happy debugging!