Pages

18/08/2017

Flashing a bootloader on to an STM32 'Blue Pill' using a Raspberry Pi

I am currently preparing some STM32 'Blue Pill' Microcontrollers for use with an Amateur Radio project. From the factory these appear to arrive without USB support, so before we can flash any firmware over USB you must first flash a new bootloader over the UART interface.

I used a USB to Serial adapter set to 3.3v, but you could use the Raspberry Pi's UART if you wish. Remember, if you're using the Raspberry Pi 3 you will need to disable the built in bluetooth and TTY running on the UART first.


The following assumes you have connected to your Raspberry Pi via an SSH session or monitor and keyboard, and you've opened up a terminal.

Switch to the root user

The steps below assume you're running as the root user. You can switch to root using-
sudo su

Install the dependencies

There are a few dependencies required before you can flash the bootloader to the STM32-
apt-get install python git
cd /usr/src
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip install pyserial

Download the bootloader bin file

Roger Clark has written a bootloader for the STM32 Blue Pill that will allow you to flash programs via USB. You can download it from github-
cd /usr/src
wget -O generic_boot20_pc13.bin https://github.com/rogerclarkmelbourne/STM32duino-bootloader/blob/master/bootloader_only_binaries/generic_boot20_pc13.bin?raw=true


Download stm32loader

James Synder has written a python utility for flashing the bootloader to the STM32 Blue Pill. Again you can download it from github-
cd /usr/src
git clone https://github.com/jsnyder/stm32loader.git


Prepare the STM32 Blue Pill for flashing

Before you can flash the STM32 you will need to hook up the cables to the UART and VCC / GND and also move the BOOT0 jumper into the '1' position. You can use the image below as a guide-





Flashing the bootloader

You will need to find which serial port your STM32 is connected to. If you're using a USB to serial adapter, you can run dmesg after plugging it in to see where it was assigned-
dmesg

On a Raspberry Pi, the serial port will be-
/dev/ttyAMA0

Change into the stm32loader directory-
cd /usr/src/stm32loader

Run stm32loader to flash the bootloader-
python stm32loader.py -p /dev/ttyUSB0 -w /usr/src/generic_boot20_pc13.bin

You should see something like the following. Once its complete, you will return to the shell prompt-
Bootloader version 22
Chip id: 0x410 (STM32 Medium-density)
Write 256 bytes at 0x8000000
Write 256 bytes at 0x8000100
Write 256 bytes at 0x8000200
Write 256 bytes at 0x8000300


Finishing up

Now the bootloader is flashed, you should be able to unplug it from the UART and power, move the BOOT0 jumper back to its original place, and start flashing programs over USB. You will know if the bootloader has successfully installed if you see a green flashing light on the STM32 when its first powered up. I will cover one method of flashing firmware to the STM32 in another blog post in the future.

Acknowledgements

Thanks to- 
Param Aggarwal - Wrote this Blog Post, which is the source of this tutorial.
Roger Clark - Maintains a bootloader with USB support, compiled from various sources
James Synder - Wrote the tool for flashing the bootloader.


No comments:

Post a Comment