Skip to main content

Flashing an STM32 "Blue Pill"

Flashing this STM32 "Blue Pill" board took me 5 hours.

I finally recieved my microcontrollers. I have at disposal :
  • A brand new WeMos D1 Mini (it's a smallish NodeMCU)
  • An Arduino Pro Mini (it acts just like a Leonardo, but it lacks some interesting pins sadly :(  )
  • An STM32F103C8T6, I'll call this the Blue Pill for short.
The Arduino Pro Mini has an Arduino bootloader out-of-the-box, so  I could play with it directly. It's nice ! Still, it lacks the double Serial of the original Leonardo, and most importantly, there's no A4 and A5 pins, so libraries for I2C won't work without modification.

But the Blue Pill needs more work. Basically, I followed the instructions on this site : http://wiki.stm32duino.com/index.php?title=Installation
Since it's quite complicated, consider the following as a tutorial to set up your Blue Pill. I tried different things, the following has worked for me.

Before doing anything software-related, while reading the Blue Pill page ( http://wiki.stm32duino.com/index.php?title=Blue_Pill ), I found out that the USB connector is not really soldered, and is very fragile. Looking on my Blue Pill, I saw that the 4 legs didn't have any soldering tin. So I took my soldering iron out to add some tin on its 4 legs. It should make it sturdier.

The Blue Pill comes without bootloader. I will hence burn the STM32duino bootloader. The procedure is somewhat detailed here : http://wiki.stm32duino.com/index.php?title=Burning_the_bootloader
A simple way to achieve this is to use an external USB-Serial adapter. To achieve that, I simply used the CH340 chip embedded on my old (and dead) NodeMCU.


My Blue Pill uses the USB-Serial chip from the NodeMCU for its bootloader flashing.

First things first, I connected the NodeMCU to the Blue Pill. I plugged as follows on the little scheme. You can notice that I pulled the NodeMCU RST to GND in order to disable the ESP8266, so that I can use freely the CH340 as USB-TTL adapter. It's a trick I've seen on some Youtube video. If you have for example an Arduino Uno, you can do the same by disabling the Atmel chip with this RST-GND strap.


Then, I put the jumpers to allow flashing. Just see the photo !

Pins I linked together. Notice the RST-GND ! Plus, positions of the jumpers to allow bootloader flashing.
hoto. After doing this, you have to press the Reset button on the board. The onboard LED should stop blinking.

Then, I have to download some useful stuff :
I extracted the archive, installed the Flash Loader Demonstrator, and started it (it's called Demonstrator GUI). I entered these options (see following screenshots). Because my personal Blue Pill has its onboard LED on pin PC13, I chose the bootloader called generic_boot20_pc13. I tried with several other bootloader and the PC13 was the only one working for me. I can only advise you to be extra careful when choosing your bootloader, otherwise you won't find where your errors come from.





 


After burning successfully, I unplugged everything. I installed the Windows drivers by launching the install_drivers.bat in the archive (it's in drivers --> win ). I put both jumpers on 0 (instead of 1 and 0). These jumpers indicate the BOOT mode (the upper one is jumper BOOT0, the lower one is BOOT1. In short, I put BOOT0 and BOOT1 to 0).

I opened my Arduino IDE and configured it to recognize the STM32 family. First, I opened the Board Manager and installed the Arduino Due. According to the STM32duino website, it should install the Cortex M3 compiler.


I added the STM32 boards repositories into the Arduino IDE by going into "Preferences", then adding into the repositories the following URL : http://dan.drown.org/stm32duino/package_STM32duino_index.json

You can see I already had the repository for ESP8266. The STM32duino has been added.
Then, in the Board Manager, I installed the boards STM32F1xxx (just type STM32 in the research and choose the right one). I closed and re-opened the IDE, plugged the Blue Pill with the USB cable, and began my first Blink example :


/*
 Blink: Turns on the built-in LED on for one second, then off for one second, repeatedly.
 Arduino 1.6.0rc1
   Sketch uses 11,900 bytes (11%) of program storage space. Maximum is 108,000 bytes.
   Global variables use 2,592 bytes of dynamic memory.
 Ported to Maple from the Arduino example 27 May 2011 By Marti Bolivar
*/

void setup() {
    // Set up the built-in LED pin as an output:
    pinMode(PC13, OUTPUT);
}

void loop() {
    digitalWrite(PC13,!digitalRead(PC13));// Turn the LED from off to on, or on to off
    delay(1500);          // Wait for 1.5 second (1500 milliseconds)
}


The Arduino IDE uses "user-friendly" names for the pins. I used the pinout from the wiki (names in light yellow. It just corresponds directly to what's written on board, so it's really convenient. I just wrote "PC13" in order to control the Pin written "C13" on the board.
Blue Pill pinout. Credits : http://wiki.stm32duino.com/index.php?title=Blue_Pill


In the Windows Device Manager, before the first flashing, the Blue Pill appears as a Maple DFU. It's like a "ready-to-be-sketched" Maple Mini board. When launching the first upload on the Arduino IDE, it detects the Maple DFU and programs it in order to assign it to a COM port. After the first programming, the Blue Pill is detected as a Maple Serial (COM12) on my PC. I can't remember well, but if the first programming doesn't work because it doesn't find the Maple DFU (which may happen), just try again and quickly press the RESET button on the Blue Pill board when the Arduino IDE is searching for Maple DFU. It worked for me. I had a lot of different problems I can't really recall at this step ; try to re-burn the good bootloader if you can't find anything.

Don't pay attention to the RIM Virtual Serial Port... I had a BlackBerry.

At last, I have a glorious STM32 "Blue Pill", ready to be programmed through the Arduino IDE. It was quite a pain to figure out some of these things, but hey, that's why I write on this blog. I hope this might help someone to set up all of this.

Final words : I though about calling this rant "Flashing open a cold "Blue Pill" one with the "Arduino" boys, but I don't think Google would reference it correctly.

See you next time where I'll play with buttons, LEDs, interrupts, I2C and LCD screen !


Comments

Popular posts from this blog

Anti-optimization, hardware crumbling apart, GitHub

Today, short post about deceiving software. Remember when last time, I said I had to do optmizations on my code ? Well I tried. And it went worse than I expected ! First, I tried to gather similar codes in some tasks, but it came out they were too heavy for FreeRTOS to handle them correctly, resulting in heap panic... So trying to enhance my code, I worsen it ! So I hard to roll it back. Then, I tried to implement real mutexes for some resources that are shared among the tasks, in particular the screen (and maybe the UART one day, but I can manage to use it only in one task, if I split it in several functions). When I though I understood it and implemented it correctly, it turned out to make the STM32 crash at bootup, and being non recognizable by my PC anymore ! So again, I had to rollback to the previous version. I'm still trying to figure out how to re-design my code, while keeping it runnable on that picky Blue Pill. You can FINALLY find my code on Github ! I suggest

Buttons and alarm

Now I have my buttons. And it didn't go smooth sailing. Before all, let's try to review what I want and how to do that : I want buttons to control the KaRadio. 6 buttons are enough. We will detail them a bit afterwards. I want my buttons to also set an alarm clock. This should stay visible, and should use the already implemented buttons to be modified and set. I want that any push in a button results in changes that are visible on the LCD screen. If the volume changes, I want to see it. If the station changes, well that's already implemented. First, I went to read the documentation on how to operate the GPIO. I knew I needed only to read digital states of the GPIO. At first, I found this document straight from Maple docs : http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/0.0.12/libmaple/api/gpio.html It's quite dense, but there is some information. GPIOs must be initialized with gpio_init(), then you need to set their state to output or in