Language: EN

programar-stm32-con-ide-de-arduino-y-st-link-v2

Programming STM32 with Arduino IDE and ST-Link v2

In the entries of the series dedicated to STM32 we have seen that there were three ways to program the STM32 with the Arduino environment. In this post we will see the second of them, programming the STM32 with an ST-Link v2.

Just like in the previous entry where we saw how to program the STM32 with a USB-TTL converter, we will use the BluePill development board, one of the most popular among Makers due to its low cost and “similar” format to an Arduino Nano.

This time we will see how to program the STM32 with an ST-Link v2, which is much more convenient than using a simple USB-TTL converter. The connection is simpler, it avoids having to move the jumpers on the board, and it also allows us to have debugging functions (if the IDE we use allows it).

But let’s start by seeing what the ST-Link v2 is and how to use it to program development boards based on the STM32.

The ST-Link v2 is a programmer/debugger built by ST Microelectronics that allows programming and debugging STM8 and STM32 processors. The ST-Link v2 implements SWIM (Single Wire Interface Module) and JTAG/SWD (Serial Wire Debugging) to communicate with the processors on the development board.

st-link-v2-original

The original ST-Link v2 is a bit expensive, with a cost of 20-25€. An alternative is to use a development board like the ST Discovery or the ST Nucleus, which incorporate an ST-Link v2 on the board itself, with a similar or even lower cost.

Fortunately, there are quite a few clones of the ST-Link v2 with a much lower price of 1.5-1.6€, which is more or less in the range of a USB-TTL converter.

st-link-v2-clone

Normally these clone devices have a USB format and, internally, maintain the same electronic scheme as the original (which, by the way, uses an STM8 or 32 inside).

The firmware of these ST-Link v2 clones is extracted from the original (which may not be entirely legal, but that’s their problem). Therefore, the behavior is also identical to that of the original.

However, we can also overwrite the firmware of the ST-Link v2 clone, as it has a DFU bootloader. For example, we can turn it into a Black Magic Probe, something we will see in a future entry.

For the price it has, a clone of the ST-Link v2 is a good purchase if we are going to frequently use development boards with STM32 or similar.

The process of programming the STM32 with the ST-Link v2 is very simple, even more so than using a USB-TTL.

First, we must install the drivers from the ST page. The drivers are free, but we will have to register to be able to download them. From the folder we download, we run ‘install_drivers.bat’ as an administrator.

Connect the device and make sure that our system recognizes it correctly.

stm32-st-link-v2-arduino-4

Now that we have the ST-Link v2 ready, we connect it to the STM32 according to the following diagram, requiring only 4 cables.

USB-TTLSTM32
SWDIODIO
SWCLKCLK
3.3V3.3V
GNDGND

stm32-st-link-v2-arduino

stm32-st-link-v2-arduino-2

Check the pin assignment on your development board and on your ST-Link v2 programmer, as manufacturers tend to change them around.

Do not power the device simultaneously through the ST-Link v2, while also having it connected through the micro-USB of the BluePill, or we can damage the device.

Let’s check that everything works correctly, and upload a sketch to the STM32 Blue Pill. We assume that you have correctly configured the Arduino IDE (right?) as we saw in the previous entry.

We will upload the same blink file that, as we know, we used as “hello world” on processors, and that we adapted for the STM32 in the previous entry. For convenience, here is the code again.

//const int ledPIN = PB1;  //this is what comes in the example but doesn't work
const int ledPIN = PC13;  // depends on each board

void setup()
{
  pinMode(ledPIN, OUTPUT);
}

void loop()
{
  digitalWrite(ledPIN, HIGH);
  delay(1000);
  digitalWrite(ledPIN, LOW);
  delay(1000);
}

Now, in the Arduino IDE configuration, we choose the ‘ST-Link’ loader.

stm32-st-link-v2-arduino-3

And without further ado, we click upload. Unlike with the USB-TTL converter, we don’t have to change any jumpers, or touch anything on the board. The integrated LED on the board will start blinking slowly, which means that everything went well.

That’s how simple it is! In the third and final entry on ways to program the STM32, we will learn to program it directly with the Arduino environment through the board’s micro USB, the most “difficult” but also the most useful of the three ways we are going to see. Until next time!