Posted by: Cornelius One Transistor Posted on: November 26, 2017 Updated on: 2020-01-01T18:30:39Z
How to configure STM32 "blue pill" development boards for programming with Arduino IDE. Instructions for Windows and Linux
The "blue pill" is a STM32F103 based development board. Although it is less popular, the board is cheaper than the Arduino Nano. More than that, STM32F103 is a device with Cortex-M3 ARM CPU that runs at 72 MHz, 20 kB of RAM and 64 or 128 kB of flash memory. The microcontroller (MCU) has USB port, two serial ports, 16 bit PWM pins and 12 bit ADC pins. It runs at 3.3V, but some of its pins are 5V tolerant.
Programming the board can be simplified using the popular Arduino IDE. But before this, an Arduino-like bootloader must be flashed to the board. This can be done via serial port or using the debug interface of the MCU with ST-Link tool. There are also some small hardware issues with Chinese boards that need to be fixed. This post will show a straightforward guide for setting up this board to work with Arduino IDE on Linux and Windows. The instructions below are based on STM32duino wiki [down?] documentation.
If you decide to use ST-Link programmer, there is no need for a bootloader. If you prefer a different IDE, see here how to develop for STM32 in PlatformIO. You have access to multiple frameworks if you go for PlatformIO.
STM32duino wiki contributors have reported that some of the boards came with poor soldering of the micro-USB connector. Also some of them have USB D+ pulled up with 4.7 k or 10 k resistors which may cause the USB port not to work on some computers. My board had both these issues. I soldered the USB connector. Even though my computer recognized the USB device and so may yours, I highly recommend pulling up D+ with 1.5 k if you use USB port.
Look on the back of the board and locate R10. If it’s 10 k (marked 103) you can add 1.8 k parallel resistor to obtain 1.5 k equivalent resistance. If it’s 4.7 k (marked 472), a 2.2 k resistor is suitable for parallel connection. Don’t worry, you don’t have to do SMD soldering as D+ is connected to PA12. Just solder the resistor between PA12 and 3.3V pinheader pads.
Another thing that is good to know, is what pin the LED connects to. Depending on this you will choose the correct bootloader.
Blue pill hardware check
Go to STM32duino GitHub repo and look for the bootloader. Roger Clark offers many bootloaders for different boards. If your board has the green LED connected at PC13, you need the bootloader built for this configuration: generic_boot20_pc13.bin. Download the bootloader for your board and keep reading.
The IDE doesn’t come with default support for STM32 boards. This step is common to all operating systems, so let’s do it now. Launch Arduino IDE, go to File — Preferences, and add the following Boards Manager URL:
Click OK to close the dialog.
Arduino IDE — add Boards Manager URLs
Next, go to Tools — Boards — Boards Manager. In the dialog that appears, search for STM32 and choose STM32F1xx for "blue pill". If your board uses a different chipset (i.e. STM32F3) select that one. Install it.
Arduino IDE, Boards Manager — install new board
Before actually uploading sketches to the board via USB port, it needs the STM32duino bootloader. I will be using a ST-Link v2 clone to burn the bootloader. The connection to the development board is straightforward, using SWCLK, SWDIO, GND and 3.3V pins. Connect them using the included jumper cables that came with the ST-Link. Do not power the development board from other source or USB port! It will get its power from the programmer. Right now, you can plug the programmer into an USB port and program the board straight from Arduino IDE using STlink upload method.
If you don’t want to use the programmer all the time and prefer to use the micro USB port to upload software to board, install the bootloader. I found this method not quite reliable, so I’m uploading sketches directly to flash with ST-Link programmer. At the end of the post I share some information about it. Anyway, you may give it a try. Move the jumper to set BOOT0 to 1. Plug the ST-Link into an USB port. From now on, configuration process is different depending on operating system.
Needed tools can be found at ST. You need STSW-LINK009 (this is the Windows driver) and STSW-LINK004 (the programming utility). You need to make an account to download them. Use the included dpinst tools or the bat script to install the driver. After the driver is installed and the ST-Link adapter is recognized by the computer, install and launch STM32 ST-LINK Utility.
Use the File menu to open the bootloader bin file you downloaded earlier. Next, connect to programmer (use the plug icon on the main toolbar, or Target — Connect) then choose Erase and Program option from the same menu. The bootloader will be burned to MCU Flash memory.
While the bootloader is downloading to MCU, you will see the programmer LED flashing. When it is done, you can disconnect the board from programmer, change back BOOT0 to 0 and connect it using board’s USB. Of course, it needs Windows drivers which can be found in the Arduino_STM32 package prepared by Roger Clark. You don’t need to download the entire repository just for the drivers. Most of the files contained in this repository were automatically downloaded by Arduino IDE when you added the board support (but not the driver). Here is a driver only archive. Extract it and run install_drivers script. When you reset the board, for a short moment of time while bootloader is running, it appears as DFU device, then switches to serial port.
ST offers no GUI tools to program the board from Linux. But, when you downloaded the board with Arduino IDE, you also downloaded the tools to program the board using ST-Link adapter. In your home folder, open hidden .arduino15 folder ( nautilus
/.arduino15 ). Browse to packages/stm32duino/tools/stm32tools. Here you will find a folder named like the STM32 package version (i.e. 2017.11.14). Open it, then linux subfolder. There should be an install.sh script here. Run it from terminal with root permissions ( sudo ./install.sh ). You just installed udev rules for ST-Link and board. It is now recommended to log off then back in or restart computer.
/.arduino15/packages/stm32duino/tools/stm32tools/2017.11.14/linux, go to stlink folder. Make sure BOOT0 is set to 1 and run ./st-info —probe . You should see something like this:
This is an indication that everything is OK and you can proceed burning the bootloader with the following command:
Adjust the command with the correct path to bootloader file. Here is the output of this command:
That’s it. Disconnect the programmer, move BOOT0 back to 0 and connect the board using its USB port.
Here is what happens when you reset (plug in) the board. First the DFU device appears then the USB serial port (/dev/ttyACM0).
Sketches are uploaded while in DFU mode (device 1eaf:0003 ).
Uploading a sketch
Launch the IDE and choose the board. For a generic "blue pill", here is the default configuration:
STM32 Generic board in Arduino IDE
On Windows, only the port is different (COMx). Open a sample sketch and compile it. From my experience, on Windows you can just press Upload button to reset the board and burn the sketch binary. But, on Linux I had no luck with automatic reset. I make sure the sketch is compiled then I reset the board by pushing the button and I click quickly on Upload button in Arduino IDE. Otherwise, uploading fails.
This is the output in Arduino IDE:
If dfu-util reports no DFU devices, you didn’t reset the board at the right time. This means sketch was not uploaded to board. The Linux issues seems to be related to driver implementation because shortly after a failed upload (without reset), dmesg reports a failure to set dtr/rts on ttyACM0.
If this method of uploading a sketch (using the bootloader) is a pain, there are alternatives. I regularly use the ST-Link programmer to upload Arduino sketches to blue pill. No need to manually reset the board, no need to move jumpers. Uploading is faster on Linux than on Windows (I don’t know why).
On very rare occasions uploading fails. In this situation I have to move BOOT0 jumper to 1 and upload the sketch. Binaries do not run when BOOT0 is set to 1 therefore I move it back. There are also very rare occasions when blue pill does not reset after upload. I have to push the reset button.
Configure blue pill in Arduino IDE for ST-Link
Programming port of the blue pill has only 3.3V supply. This can be regarded as a good thing, because 5V won’t be available on 5V pin so you can be sure you won’t supply 5V to a non tolerant pin. But, if you fit the blue pill on the breadboard it’s easy to get a 5V supply line from ST-Link using a jumper wire (if you really need it).
Using Arduino IDE with STM32duino bootloader to program the "blue pill" development board isn’t the most effective way to develop STM32 based systems, but it’s easier than with other development kits. It’s recommended to read the API differences and have a look at examples if you will use this method.