Every STM discovery / eval board comes with a ST-Link V2 on board, with one exception; the STM32VL version comes with a ST-Link V1.
The st-flash and st-util tools are needed, provied by texane
Download and compile
$ cd /opt $ git clone git://github.com/texane/stlink.git stlink $ cd stlink $ ./autogen.sh $ ./configure $ make
Set enviroment variable
$ exportline="PATH=$PATH:/opt/stlink" $ if grep -Fxq "$exportline" ~/.profile; then echo nothing to do ; else echo $exportline >> ~/.profile; fi $ source ~/.profile
Add udev rules
$ sudo cp /opt/stlink/49-stlinkv*.rules /etc/udev/rules.d $ sudo udevadm control --reload-rules
ST-LinkV1 requires a bit different treatment, read the Readme.txt
Connect the ST-Link to the MCU. If a complete STM32Fx-Discovery board is used, CN3 jumpers need to be set. To program a external MCU, remove CN3 jumpers and connect the external MCU with the SWD header.
To use SWD via STLink as default:
Update the ST-Link to blackmagic probe
The STM32F103 (STLink MCU) can be flashed with the blackmagic's firware.
Two STLinkV2 Method
Use one STLink to flash another
One STLinkV2 (connected via USB) is programming the other one. Change the 4 Solder jumpers on the bottom of the target STLinkV2 from "DEFAULT" to "REVERSED".
Connections between the two STLinks
|Use||From Programmer||To Target|
Download and build the Firmware
git clone https://github.com/blacksphere/blackmagic cd blackmagic git submodule init git submodule update make cd src make clean make PROBE_HOST=stlink
Remove the readout protection, erase and flash
openocd -f interface/stlink-v2.cfg -f target/stm32f1x_stlink.cfg -c "init" -c "halt" -c "stm32f1x unlock 0" -c "shutdown" st-flash erase st-flash --reset write blackmagic.bin 0x8002000 st-flash write blackmagic_dfu.bin 0x8000000
Use a BMP to flash a STLink
Transfer the solder jumpers from "Default" to "Reversed" and build the firmware as descibed above.
Connect the BMP to the CN2 header (see Pinout above).
Start gdb, erase the option bytes and flash BMP
(gdb) target extended-remote /dev/ttyACM0 (gdb) mon swdp_scan (gdb) att 1 (gdb) mon option erase (gdb) load /dir/to/blackmagic (gdb) load /dir/to/blackmagic_dfu (gdb) detach
The option bytes do not cover the Readout Protection (RDP), but the RDP is disabled anyhow...
Error at loading a new image after removing RDP
Error erasing flash with vFlashErase packet
If this occurs powercycle the target (disconnect and reconnect the 3V3 connection), swd scan, attach and try loading the image again. Usually the reset of the RDP requires a flash erase (done by mon option erase) and reset.
Probably this can also be done via gdb.
There are also a lot of different chinese clones out there. Most with an identical MCU as the original STLinkV2, some with an STM32F101.
It seems that each clone has its own programming pinout, so better double check (continuity tester) than to release the magic smoke...
Most (all?) should work with the "stlink" target from Blackmagic Probe, since they probably use the same firmware from the original STLinkV2.
Seems to work with the standard stlink BMP image.
Programming header (1.27mm, populated on the images) from the Mini USB to the Pin header (right to left on the image), used for programming the onboard MCU.
Some Images and details about this hardware can be found at
Programming header (2.54mm, unpopulated on the images) from the USB to the Pin header (left to the right on the images), used for programming the onboard MCU.
- USART1 RX (PA10)
- USART1 TX (PA9)
Bootloader header (2.54mm, unpopulated on the images), from the USB to the Pin header (left to the right on the images), used for USART bootloader activation
- BOOT0 (pin 44)
The 3V3/5V switch selects the input voltage for the onboard MCU.
Programming header (1.27mm, unpopulated on the images) from the USB to the Pin header (left to the right on the images), used for programming the onboard MCU.
- VDD, VBAT
To disassemble pull the aluminium housing towards the USB connector.
This has not the same hardware as https://github.com/blacksphere/blackmagic/issues/62 , but the same MCU, seems that this has just an other PCB.