A Word of CAUTION: This tutorial is fairly advanced, and makes modifications to hardware that can result in permanent damage, rendering the board unusable. Please read carefully,
check and recheck. It may be that you only need to follow the software parts to get this working.
In a previous post, I have shown you how to use the Arduino IDE to program the STMF103C8T6 “Blue Pill”. In case you missed that post, click here, to read it. At the end of that post, I mentioned that it was possible to use the native USB port on the “Blue Pill” to upload code via the Arduino IDE.
At the time of writing that post, I was however not confident enough that the method that I used to achieve that was completely reproducible by all my readers. I have however done some more research, so you don’t have to, and read through quite a lot of papers and posts on this topic available online.
I am thus extremely happy to tell you that I have found, and tested as well as verified, a reliable method to achieve this. I will share that method with you now, as promised in the mentioned Facebook post.
Please note: Not all “Blue Pill” boards will need a hardware modification. For some, it is only required to load the boot-loader to enable this functionality
To get this to work, you will need a few things, as well as verify a few things on your own “Blue Pill”.
Things you need:
1. An electronics breadboard ( Big enough to fit the “Blue Pill”.)
2.The STM32Duino Bootloader ( I will post a link on the bottom of the page to the one that I have used)
3. A USB to UART converter ( Used to upload the bootloader to the “Blue Pill”.)
I use Cytron’s UC00C USB to UART converter because the UART voltage is adjustable between 3v3 and 5v, making it possible to have only one converter that is usable on 3v3 as well as 5v devices.
4.Software to upload the bootloader to the “Blue Pill” (You can NOT use the Arduino IDE to do this ! )
Since my primary operating system is Linux (Ubuntu to be precise) it is quite easy to find and install software, as it is normally just a sudo apt-get install xxxxx <enter> away 😉
The needed package on Linux ( and also Mac incidentally) is stm32flash To install this, open a terminal or shell, and type sudo apt-get install stmflash and press <enter>.
Note: This will work for all Debian based distributions, but other Linux flavours, please use the specific command for your distribution. If you don’t know, ask me, but tell me your distribution first 🙂
For the Windows guys, I am sorry, I don’t own a Windows computer, but I am sure you can find a windows version of stm32flash on google. If you own an ST-Link you can use its software or any other UART software that will allow you to upload a .bin file to the “Blue Pill”
5. A Magnifying glass, very good eyesight, or a Smartphone (to take a picture of a component and zoom in to read the value).
Some of the “Blue Pill” boards have a wrong value pull-up resistor on the USB D+ Line. This is especially true about some of the boards that were manufactured in China or elsewhere in Asia. This wrong value pull-up resistor causes the USB port to behave in an unstable manner, sometimes working, and sometimes not.
As this is a surface mount resistor, it is quite difficult to read the value with the naked-eye, unless of course, you have supervision 😉 The correct value should be 1k5 or 1.5-kilo ohm, but most of the boards have either a 10k or a 4k7 resistor. Your board may even have another value, who knows…
The surface mount marking for a 1k5 resistor is 152, a 10k is marked 103 and a 4k7 is marked 472. If you are equipped to do so, you can just replace the resistor with the correct value, but we will do something else, and learn to apply some electronics theory in practice as well.
You can reduce the value of a resistor by placing another resistor in parallel with it. The formula to do this is
Req = (R1 x R2) / ( R1 + R2)
(Use this formula if you have two resistors in parallel only)
I have already hinted in the required item 6. that you may need a 2k2 or a 1k8 resistor, but until now, have not said why… As said above, your “Blue Pill” may have a 10k or a 4k7 resistor on the USB D+ line. We need to lower this value to as close to 1k5 as possible for the USB port to function correctly.
Using our formula, let’s calculate :
If you have a 10k resistor ( marked 103 ) then do this:
Req = (10k x 1.8k) / (10 + 1.8)
Req = 18 / 11.8
Req = 1.525
Thus combining the 10k resistor with the 1k8 resistor in parallel will give us a resistor with a value of 1.525k, which is pretty close to the 1k5 that we want.
If you have a 4k7 resistor ( marked 472 ) then do this:
Req = (4.7 x 2.2) / (4.7 + 2.2)
Req = 10.34 / 6.9
Req = 1.498
Thus combining the 4k7 resistor with the 2k2 resistor in parallel will give us a resistor with a value of 1.498k, which is also pretty close to the 1k5 that we want.
If you have a 1k5 resistor ( marked with 152 ) then you DO NOT DO ANYTHING. You are lucky, you got a “Blue Pill” with the correct value resistor from the factory. You may feel free to skip to step 8 or read along if you want to know how to fix it.
Turn your “Blue Pill” upside down. With the USB port towards your left, use a magnifying glass, or take a very clear macro photograph with your smartphone. We are looking for the value of the resistor at R10
Zoomed in on the resistor we can clearly see the value
In my case, I have a 1k5 resistor on R10 already. You can clearly see the 152 marked on it. You may or may not have the same. If you do, continue to step 8, but if you have 103 or 472, please do step 6 and 7 before moving on.
In the odd case that you have something completely different to 103 or 472, you must calculate a correct value resistor to place in parallel to the one you have.
6. A standard 2.2k or 1.8k 1/4 watt resistor ( NOT Surface mount type) – This requirement depends on the outcome of item 5. above, thus you may or may not need this.
7.A fine-tipped soldering iron, solder and flux. This requirement also depends on the outcome of item 5. above. Thus if item 5. fails, you will need both 6. and 7.
You will now have to solder the resistor between two of the pins on your “Blue Pill”. If you have a 103 (10k) resistor at R10, you will need the 1k8, if you have a 472 (4k7) at R10, you will need a 2k2.
And I repeat if yours is a 152 (already a 1k5) do not do this step.
If we look at the top of the board, we will see A12 as well as 3v3.
You need to very carefully solder the selected resistor (from step 5. above) between A12 and 3v3, while taking care NOT to SHORT ANY OTHER PINS. I suggest that you place the resistor into heat shrink tubing, leaving just enough of the legs to solder, and then apply heat to shrink the tubing, before, you try to solder it in place. If you can not solder, please ask someone that can yo help you do this. I do not advise to start learning how to solder on this project.
8. A Micro-USB cable to connect “Blue Pill” to the computer.
Now that the most difficult part of this tutorial is done, let’s look at the software part that we will need.
STM32duino has an Arduino compatible boot-loader that you can upload to your “Blue Pill” in order to enable you to upload code in Arduino fashion. No more changing jumpers back and forth. No more connecting external UART every time you want to upload code to the “Blue Pill”. Enough. You get the picture.
In my previous post, I have already shown you how to load the STM32 drivers into the Arduino IDE, If you have missed that, please refer back to that first. Click here to go to that post.
We now need to load some drivers.
For the Windows guys, in your Arduino Folder:
Arduino\hardware\Arduino_STM32\drivers\win you will find two .bat files
install_drivers.bat and install_STM_COM_drivers.bat
Execute them and restart your computer. Then come back here, as we are not done yet.
For the Linux guys, I assume Mac will be similar,
There will be a file called install.sh
Run this file with SUDO: sudo ./install.sh from a terminal shell in the above directory.
Windows and Linux, possible Mac too
Download the bootloader file and save it to a folder.
use your version of stm32flash or similar to flash generic_boot20_pc13.bin to your “Blue Pill” using the UART connected to PA9 and P10, as in my previous post. Remember to move the boot jumpers to activate boot0 to 1.
Linux and possibly Mac
load stm32flash by using the command:
sudo apt-get install stm32flash
then flash generic_boot_20_pc13.bin to your board using the command:
stm32flash -w generic_boot20_pc13.bin -v -g 0x0 /dev/ttyUSB0
where you need to change /dev/ttyUSB0 to the port your “Blue Pill” is connected to using the UART connected to PA9 and P10, as in my previous post. Remember to move the boot jumpers to activate boot0 to 1.
Mac users can try with the same file, or use the generic_boot_pc13mac.bin file
Now, Linux and Mac, I recommend disconnecting everything from the computer, and reboot.
After the reboot, move the jumpers back to position 0 and power the board from the USB port. The onboard led light should flash, indicating that a boot loader is loaded.
Open your Arduino IDE, you will find a com port called maple mini, use this to connect to your board.
Set everything as shown above.
You can now upload code to the “Blue Pill” with the Native USB port. Linux users may have to quickly press the reset button before an upload, but it is still better than changing jumpers every time.
I hope you found this tutorial helpful, any questions or comments are welcome.