How to flash STM32 via Serial Port

RishabhHardas picture RishabhHardas · May 17, 2016 · Viewed 13.6k times · Source

I have an STM32F102 microcontroller and I want to program it via the Serial Port.

While there is a flasher available for windows, I want to do it on a Linux Machine. I tried doing it with this script

I have set BOOT0 = 1 and BOOT1 = 0, restarted the microcontroller. But it does not work. I get the following output

Can't init. Ensure BOOT0=1, BOOT1=0, and reset device
Traceback (most recent call last):
  File "stm32loader.py", line 552, in <module>
    bootversion = cmd.cmdGet()
  File "stm32loader.py", line 140, in cmdGet
    if self.cmdGeneric(0x00):
  File "stm32loader.py", line 137, in cmdGeneric
    return self._wait_for_ack(hex(cmd))
  File "stm32loader.py", line 88, in _wait_for_ack
    raise CmdException("No response to %s" % info)
__main__.CmdException: No response to 0x0

Answer

Chi Zhang picture Chi Zhang · Jun 29, 2016

Here are a few tips:

  • Connect serial cable before resetting / powering up the board. Otherwise some transients can mess the serial bootloader up.

  • Make sure you are using a TTL level USB-to-serial converter instead of a RS-232 cable. RS-232 has inverted level, and worse still, its -15V to 15V voltage range can burn your STM32.

  • Make sure RX and TX are connected correctly.

  • Try using stm32flash instead.

  • Most STM32's serial bootloader does not support baud rate higher than 115200 as I remember. The bootloader can detect baud rate automatically, the one I usually use is 57600.