STM32 is a popular family of embedded microcontrollers. They are based on the ARM core that allows using various ARM-based tools to develop and debug them once the tools are properly configured. This tip shows how to setup a basic "Blinking LED" project for the STM32100B-EVAL board using Visual Studio, GCC toolchain and OpenOCD debugger.
How the Compilation Works
The STM32 microcontrollers are based on the ARM core. It means they can execute normal instructions supported by the ARM core, i.e., run programs compiled by the ARM compiler. However, simply running instructions is not enough to get even a simple solution working: consider a simple line of code:
GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_SET);
The ARM compiler will transform it to something like this:
mov.w r4, #4096
movt r4, #16385
movw r2, #16960
str r5, [r4, #16]
In order to be able to do it, it should know various information about your STM32 device: which ARM core it has, what peripherals it contains (e.g. what is GPIOC) and what are the memory addresses (where the code and variables will go). This information is contained in a board support package - a collection of .C and .H files providing all the necessary information. In order to get our STM32 project working, we will install and configure a board support package.
Before We Begin
Ensure that you have Microsoft Visual Studio and VisualGDB installed. During our project setup, we will install and configure the following tools:
- GCC compiler for ARM
- GDB debugger for ARM
- OpenOCD tool that allows debugging embedded boards via JTAG with GDB
Creating the Project
- First of all, start Visual Studio and select File->New->Project:
- Select VisualGDB->Embedded Project Wizard:
- Select "Create New Project":
- As the STM32 devices are based on the ARM toolchain, select the 'arm-eabi' on the next page. Then select your device. This will select the correct board support package so that the ARM GCC compiler will know all it needs to know about the layout of your device:
- On the 'debug method' page, select 'OpenOCD' and specify interface and target scripts accordingly:
- Press "Finish" to end the wizard. Once the project is generated, Press F5 to build and start debugging your project:
Under the Hood
So how does Visual Studio know how to build and debug your STM32 project? Very easy: the generated project pretends to be a Makefile project supported by Visual Studio:
- When you build your project, Visual Studio runs GNU Make.
- GNU Make invokes GCC with all the necessary flags.
- All the settings specific to your device, such as the corresponding GCC flags are stored in the Makefile.
- The sources and headers from ST describing the peripherals are mentioned in the Makefile as well.
- The device memory layout is specified in the linker script that is also mentioned in the Makefile.