Difference between revisions of "STLink"

From PaparazziUAV
Jump to navigation Jump to search
(stlink to blackmagic added)
 
(27 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{| align=right
<div style="float: right; width: 20%">
|-
<categorytree style="float:right; clear:right; margin-left:1ex; border: 1px solid gray; padding: 0.7ex;" mode=pages>Firmware Flashing</categorytree>
|<categorytree style="float:right; clear:right; margin-left:1ex; border: 1px solid gray; padding: 0.7ex;" mode=pages>Firmware Flashing</categorytree>
</div>
|}


<div style="float: right; width: 60%">
[[Image:St-link.jpg|right|450px]]
</div>
__TOC__
__TOC__


[[Image:St-link.jpg|right|300px|ST-LinkV2 (stm32f4-Discovery)]]
[http://www.arm.com/products/system-ip/debug-trace/coresight-soc-components/serial-wire-debug.php SWD] can be used to upload the firmware to stm32fx MCU.


A ST-Link is needed, any stm32-discovery board has a ST-LinkV2 on it (starts @ 8€). stm32vl discovery has a ST-LinkV1.
Every STM discovery / eval board comes with a [http://www.st.com/web/catalog/tools/FM146/CL1984/SC724/SS1677/PF251168 ST-Link V2] on board, with one exception; the STM32VL version comes with a ST-Link V1.


A standard ST-Link costs from 17€
==SWD Header==
==SWD Header==
{| border="1" cellspacing="0" style="text-align:center" cellpadding="2%"
 
| align="center"  style="background:#f0f0f0;"|'''Pin\Function'''
[[Image:swd_header_discovery_board.png|250px|Pinout with Apogee connector]]
| align="center"  style="background:#f0f0f0;"|'''CN2'''
 
| align="center"  style="background:#f0f0f0;"|'''Use'''
==Install Software==
 
'''INFO''' Latest version is now available on github in this repository: https://github.com/stlink-org/stlink, with all install instructions (from packages or from sources with CMake). Instruction below are for legacy code and should be removed soon.
 
The ''st-flash'' and ''st-util'' tools are needed, provided by [https://github.com/texane/stlink texane]
 
Download and compile
$ cd /opt
$ git clone git://github.com/texane/stlink.git stlink
$ cd stlink
$ ./autogen.sh
$ ./configure
$ make
 
Set environment 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 [https://github.com/texane/stlink/blob/master/README Readme.txt]
 
==Airframe.xml setup==
 
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:<br/>
Set '''FLASH_MODE=STLINK'''<br/>
 
==Update the ST-Link to blackmagic probe==
 
The STM32F103 (STLink MCU) can be flashed with the blackmagic's firmware.<br/>
 
===Two STLinkV2 Method===
 
Use one STLink to flash another
 
Hardware Setup:
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".<br/>
 
Connections between the two STLinks
{|border="1" cellspacing="0" style="text-align:center" cellpadding="6"
|+'''Connections [Header-Pin]'''
!''Use''!!''From Programmer''!!''To Target''
|-
|-
| style="background:#f0f0f0;"|'''●1'''||VDD_Target||Voltage supply for target
|5V||P2-5V||P2-5V
|-
|-
| style="background:#f0f0f0;"|'''2'''||SWDCLK||SWD clock
|SWDIO||CN2-4||CN3-2
|-
|-
| style="background:#f0f0f0;"|'''3'''||GND||Ground
|SWDCLK||CN2-2||CN3-3
|-
|-
| style="background:#f0f0f0;"|'''4'''||SWDIO||SWD data input/output
|GND||CN2-3||CN3-4
|-
| style="background:#f0f0f0;"|'''5'''||NRST||reset for target
|-
| style="background:#f0f0f0;"|'''6'''||SWO||reserved
|}
|}


Pin 1 has a withe dot on the left side.
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
 
====Documentation====
 
[http://esden.net/2014/12/29/black-magic-discovery Black Magic Discovery from Esden] <-- This also describes how to remove the read out protection. <br/>
[http://embdev.net/articles/STM_Discovery_as_Black_Magic_Probe embdev.net page]


==Needed Tools==
===BMP Method===
st-flash and st-util are needed [https://github.com/texane/stlink Tools from texane]


download and compile
Use a BMP to flash a STLink
  git clone git://github.com/texane/stlink.git stlink
 
  cd stlink
Transfer the solder jumpers from "Default" to "Reversed" and build the firmware as described above.<br/>
./autogen.sh
Connect the BMP to the CN2 header (see Pinout above).
./configure
 
make
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.
 
==Clones==
 
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.
 
===Red PCB===
 
Seems to work with the standard stlink BMP image.
 
[[Image:stlinkv2_clone_red_front.jpg|250px]]
[[Image:stlinkv2_clone_red_back.jpg|250px]]
 
* STM32F103C8T6
 
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.


copy in the suitable directory (so it can be accessed direct with terminal, without enviroment variable)
# SWDIO
sudo cp st-flash /usr/bin
# GND
sudo cp st-util /usr/bin
# SWDCLK
# 3V3


add udev rules (replace user with your username)
Some Images and details about this hardware can be found at[https://www.mikrocontroller.net/articles/IRMP_auf_STM32_-_Bauanleitung]
sudo cp /home/user/stlink/49-stlinkv*.rules /etc/udev/rules.d
sudo udevadm control --reload-rules


===Blue PCB===
NOT TESTED YET
[[Image:stlinkv2_clone_blue_front.jpg|250px]]
[[Image:stlinkv2_clone_blue_back.jpg|250px]]


For ST-LinkV1 take a look at the readme.txt !
* STM32F103C8T6


==Test it==
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.
To copy the file from the MCU
st-flash read ~/STM32F4-Discovery.bin 0x8000000 0x100000


# GND
# 5V
# USART1 RX (PA10)
# USART1 TX (PA9)


Output (just a example for the factory preload image)
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
INFO src/stlink-usb.c: -- exit_dfu_mode
INFO src/stlink-common.c: Loading device parameters....
src/stlink-common.c: Device connected is: F4 device, id 0x10016413
INFO src/stlink-common.c: SRAM size: 0x30000 bytes (192 KiB), Flash: 0x100000 bytes (1024 KiB) in pages of 16384 bytes


# 3V3
# BOOT0 (pin 44)


stm32f4-discovery shoud be found at /dev/stlinkv2_2
The 3V3/5V switch selects the input voltage for the onboard MCU.
'''how can that be tested ? please fix me :)'''


usb device
===Aluminium housing===
lsusb
NOT TESTED YET
Bus 002 Device 007: ID 0483:3748 SGS Thomson Microelectronics ST-LINK/V2
[[Image:stlinkv2_clone_aluminium.jpg|250px]]
[[Image:stlinkv2_clone_aluminium_front.jpg|250px]]
[[Image:stlinkv2_clone_aluminium_back.jpg|250px]]


==Howto==
* STM32F101C8T6


Connect the ST-Link to the 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.
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<br/>
# SWDIO (PA13)
Set '''FLASH_MODE=SWD'''<br/>
# GND
To use stlink (requires line above)<br/>
# SWDCLK(PA14)
Set '''STLINK=y'''<br/>
# VDD, VBAT


==Update the STLink to blackmagic probe==
To disassemble pull the aluminium housing towards the USB connector.
This is currently only a link to a seperate site:


[[http://embdev.net/articles/STM_Discovery_as_Black_Magic_Probe|how to upload the blackmagic probe firmware to the stlink of a discovery board]]
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.


[[Category:Firmware Flashing]]
[[Category:Firmware Flashing]] [[Category:Developer_Documentation]]

Latest revision as of 14:34, 16 December 2020

St-link.jpg


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.

SWD Header

Pinout with Apogee connector

Install Software

INFO Latest version is now available on github in this repository: https://github.com/stlink-org/stlink, with all install instructions (from packages or from sources with CMake). Instruction below are for legacy code and should be removed soon.

The st-flash and st-util tools are needed, provided by texane

Download and compile

$ cd /opt
$ git clone git://github.com/texane/stlink.git stlink
$ cd stlink
$ ./autogen.sh
$ ./configure
$ make

Set environment 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

Airframe.xml setup

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:
Set FLASH_MODE=STLINK

Update the ST-Link to blackmagic probe

The STM32F103 (STLink MCU) can be flashed with the blackmagic's firmware.

Two STLinkV2 Method

Use one STLink to flash another

Hardware Setup: 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

Connections [Header-Pin]
Use From Programmer To Target
5V P2-5V P2-5V
SWDIO CN2-4 CN3-2
SWDCLK CN2-2 CN3-3
GND CN2-3 CN3-4

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

Documentation

Black Magic Discovery from Esden <-- This also describes how to remove the read out protection.
embdev.net page

BMP Method

Use a BMP to flash a STLink

Transfer the solder jumpers from "Default" to "Reversed" and build the firmware as described 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.

Clones

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.

Red PCB

Seems to work with the standard stlink BMP image.

Stlinkv2 clone red front.jpg Stlinkv2 clone red back.jpg

  • STM32F103C8T6

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.

  1. SWDIO
  2. GND
  3. SWDCLK
  4. 3V3

Some Images and details about this hardware can be found at[1]

Blue PCB

NOT TESTED YET Stlinkv2 clone blue front.jpg Stlinkv2 clone blue back.jpg

  • STM32F103C8T6

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.

  1. GND
  2. 5V
  3. USART1 RX (PA10)
  4. 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

  1. 3V3
  2. BOOT0 (pin 44)

The 3V3/5V switch selects the input voltage for the onboard MCU.

Aluminium housing

NOT TESTED YET Stlinkv2 clone aluminium.jpg Stlinkv2 clone aluminium front.jpg Stlinkv2 clone aluminium back.jpg

  • STM32F101C8T6

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.

  1. SWDIO (PA13)
  2. GND
  3. SWDCLK(PA14)
  4. 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.