Programming STM32

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:

  • GND
  • SWCLK
  • SWDIO
  • VCC-Test or Sense
  • RESETn
Connections MiniPill LoRa and ST-LINK

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

~/.platformio/packages/tool-openocd/scripts/interface/stlink.cfg

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.

On the picture above I have connected
pin 1 (VCC-probe)
pin 4 (GND)
pin 7 (SWDIO)
pin 9 (SWCLK)
pin 15 (RESETn)
pin 19 (VCC supply)

pin 13 (SWO) can be used for debugging purposes (is not connected)

Programming hardware

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). They also do NOT have a proper RESET line to reset your target.

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.

Top view
Bottom view

I made an extender to easily connect to my project boards. You can find the PCB on EasyEDA:

https://easyeda.com/Leo/stlink-v3mini-extender

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.

SEGGER J-LINK mini EDU programmer

Due to testing and programming of NRF52832 chips I needed another programmer. The SEGGER J-LINK mini EDU does the job very well. I could use the STLINK-V3MINI extender and cable to connect it to the ARM10 connector on the SEGGER programmer. Changed the debug and upload parameter in platformio.ini:

debug_tool = jlink
upload_protocol = jlink

5 thoughts on “Programming STM32

  1. Good Morning,
    I have my board here too now.
    Could you please post the exact plan to connect the Mini Pill to an ST-Link V2 Clone?
    This is not so evident in the pictures.
    Thank you

  2. Hi,
    I have now tried the ST-Link Clone as well as the original St-Link.
    Unfortunately, I can’t get a connection with the STLink Utility either?
    It should work with the original St-Link V2. You know that I have to change something. If I see correctly you have connected pins 1,3,7,9,15,19.
    How can I still check it?
    Thank you

  3. Hello Mike,

    Great, I have added a schematic view of the connections. Remember that the Reset pin on most ST-LINK V2 clones do not work at all.
    Most ST-LINK V2 Clones do have 3.3 V output on the 3V3 pin and measure this value also. You do not have to apply external power supply to the MiniPill LoRa.

    Greetings,

    Leo.

  4. Hello Mike,

    The correct pin are: (already added this info to the page)
    pin 1 (VCC-probe)
    pin 4 (GND)
    pin 7 (SWDIO)
    pin 9 (SWCLK)
    pin 15 (RESETn)
    pin 19 (VCC supply)
    pin 13 (SWO) can be used for debugging purposes

    This can explain why it does not work (GND not on pin 3, but on pin 4)
    Sorry for the missing information.

    Leo.

Leave a Reply

Your email address will not be published. Required fields are marked *