Language: EN

programar-stm32-con-ide-de-arduino-y-conversor-usb-ttl

Programming STM32 with Arduino IDE and USB-TTL converter

In this post we are going to see how to program the STM32 with the Arduino environment, or any other IDE based on Arduino. In particular, we are going to use the BluePill development board that we saw in the previous post.

Using the Arduino IDE (or others based on it) to program the STM32 is a great advantage from the point of view of convenience and usefulness, allowing us to use a simple and well-known environment.

In addition, it will allow us to take advantage of many tools created by the Arduino community. Although it is important to note that many libraries will need modifications to work on an STM32.

We have at least three ways to program the STM32 with Arduino-based environments.

  • With a USB-TTL converter
  • With an ST-Link programmer
  • Directly through the micro-USB port

In this post we will focus on the first of the methods, programming the STM32 using a USB-TTL converter. Although it is not the most useful of the three, it will serve to present the installation of the necessary files to work with the Arduino IDE.

Setting up the Arduino environment

For the Arduino IDE (or other Arduino-based IDEs) to work with STM32 boards, it is necessary to add them to the board manager.

Arduino does not officially support development boards based on the STM32. But, fortunately, thanks to the work of Roger Clark and the community that has generated his work, we have the STM32 available as an unofficial extension.

The STM32 compatibility code repository with Arduino is Open Source, and is available at this link. This, in turn, is based on the work developed by the company Leaflabs for the Maple board, which is very similar to the BluePill.

To add the board definitions we will use the configuration file provided by www.stmduino.com, a website that encompasses much of the STM32 community around Arduino.

First, we open the Arduino IDE and go to the board manager (Tools/Board/Board Manager…). Here we add support for the SAND boards (Zero or similar). The STM32 toolchain needs files for these ARM boards for compilation (in particular “arm-none-eabi-g++”).

programar-stm32-arduino-1

Next, we go to preferences and, in “Additional Board Manager URLs”, click on the right button and paste the following address in the window that appears.

http://dan.drown.org/stm32duino/package_STM32duino_index.json

programar-stm32-arduino-2

If we already had any definition (ESP8266, Digispark) we will put one line for each board.

We go back to the board manager, and we will have the STM32 boards available. We install the ones we need. In particular, for the BluePill (STM32F103C8T6) we will need the STM32F1X.

programar-stm32-arduino-3

And now we can program our BluePill with Arduino!

Connect the STM32 USB-TTL converter

In this first post we are going to program the STM32 via UART, so we will need a USB-TTL converter. These USB to TTL converters are cheap and very useful devices that we can find on AliExpress or eBay for between 0.70€ to 2€.

programar-stm32-arduino-usb-ttl

The supply voltage of the STM32 is 3.3V, so we have to make sure that our TTL converter operates at 3.3V. They usually work at both 5V and 3.3V, and the output voltage can be selected with a configuration jumper, either on the board or on the output pins.

To program the STM32 we must connect the communication pins of the USB-TTL converter (TX and RX) to the UART1 pins of the STM32, which are A10 and A9. Therefore, the connection is as follows,

USB-TTLSTM32
TXA10(PA10)
RXA9 (PA9)
3.3VVcc
GndGnd

So the setup looks like the following image,

programar-stm32-arduino-conexion

Programming STM32 with Arduino

With the default bootloader the STM32 has two boot modes that depend on the state of the BOOT0 pin.

BOOT0MODE
LOWNormal: Boots the last program in flash
HIGHProgramming: Expects a program via UART. When received, it writes it to flash and then starts it

Therefore, to program the STM32 it is necessary to set the BOOT0 pin to HIGH and to return to normal operation it is necessary to set it to LOW.

To make it simple (although not as simple as on Arduino boards) the BluePill boards have configuration jumpers. In order to program the BluePill via UART we have to move the BOOT0 jumper to position 1.

programar-stm32-arduino-jumpers

Now, we connect the USB-TTL converter to the computer and select the generic STM32F103C board,

programar-stm32-arduino-4

And the following configuration parameters,

programar-stm32-arduino-5

To test that everything works correctly, we will try the well-known Blink, the equivalent of “Hello World” on development boards, which simply makes the LED integrated into the board blink.

Therefore, we paste the following text into the Arduino IDE.

//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);
}

We upload it to the board and the LED should start blinking. This is because the bootloader is configured to execute the program immediately after it is loaded.

However, if we disconnect and reconnect the SMT32F we will see that the LED does not blink, because it is still in upload mode. We have to put the BOOT0 jumper back to position 0 so that the STM32 runs the loaded program at startup.

The STM32 library has many examples that we can investigate to delve into this interesting board. We’ll get to that gradually! In the upcoming posts in the STM32 series we will see how to program it with an ST-Link and directly through the micro-usb.