For a couple of years I try to switch to STM32 microcontrollers. With ups and downs I learned how to handle these versatile microcontrollers. The first step was to be able to program the microcontroller. In this post I wil try to explain how to program and to address some pitfalls.
Basic Principle of STM32 programming
When you are familiar with programming of ATMEL microcontrollers you know how to program them with the SPI interface or with a boot-loader with a serial interface.
The STM32 processor have a proprietary interface called SWD. It can also handle JTAG. It uses 2 pins to program: SWDIO and SWCLK (data and clock). But most hardware/software needs 2 pins extra: RESETn and VCC-sense. The RESETn (active low) is able to reset the controller. The VCC-sense checks the programming voltage. Sometimes this pin is indicated as VCC. Be aware that this pin does not provide power to your board. You have to connect external power. Usually you will connect at least 4 or sometimes 5 pins:
- VCC-Test of Sense
The STM32 microcontrollers pins have different functions. The SWDIO/SWCLK pins can also be programmed or deprogrammed. In the case these pins are NOT programmed you cannot program the chip and you have to do a total reset of the microcontroller. In the video below there is an instruction how to reset the chip.
Programming under reset
In most cases you can release the reset pin from ground just before programming. You can also connect the reset pin with the programmer hardware and set your programming software to “hardware reset” instead of “software reset”.
I’ve experienced that it is very hard to get the timing right on a manual reset when the controller is going into sleep modus right after starting. To help with that you can put an delay on booting sou you have a little time the processor is not in sleep mode after a reset.
PlatformIO and programming under reset
An extra note on programming under reset for PlatformIO. I noticed that on default the openOCD programmer used by PlatformIO is not programming the STM32 onder hardware reset. This can be annoying when you program the controller when it is in deep-sleep modus. The trick I’ve found is to make the following changes: In the file
add the line:
reset_config srst_only srst_nogate connect_assert_srst
Remember that only the original STLINK-V2 and STLINK-V3 have a working reset line, as far I have tested. Here you can see the STLINK-V2 connected with the MiniPill LoRa.
In the couple of years I use STM32 microcontrollers I used a few programmers. The first one was the ST-LINK V2 clone. You can find a lot on the internet for a few Euro. In my experience they are not that reliable and not usable with every IDE (Integrated Development Environment).
So I bought for about 15 Euro a more official ST-LINK/V2 programmer. I had to make a special flat cable to be able to connect to the Bluepill board.
In 2020 a new version of ST-LINK was released: V3. You can buy this one for 10-15 Euro. It has a micro USB connector and very small pitch connector for the development boards.
I made an extender to easily connect to my project boards. You can find the PCB on EasyEDA:
Programming software STM32
There are a few possibilities to program a STM32 microcontroller. Of course the professional Keil software and other professional IDE’s, but I will focus on the free and public IDE’s. Because of my experience of Arduino programming I prefer the IDE that can handle Arduino code and is very flexible and has the tools of a professional IDE build in: version control and debugging: PlatformIO. I prefer this build tool with the Atom editor, but is also available with the Visual Studio Code. In the tools for PlatformIO the software programmer is build in. Sometimes you must explicit set your upload and debug tool in platformio.ini:
debug_tool = stlink upload_protocol = stlink
The other tool I use is Stm32CubeIDE. This is free available from st.com. This professional development environment is supported by STMicroelectronics. For resetting the chip I sometimes use the STM32 ST-LINK Utility, this is also available from st.com.
ST-LINK V2 STM8 & STM32 clone programmer
It is possible to program the MiniPill LoRa with a ST-LINK V2 clone. I’ve tested this with programmer firmware version V2J37S7. Note that this programmer does supply 3V3 on the 3V3 pins and does the VCC sense on this pins. You can connect the RSTn to the GND to reset the board and release it just before programming.